/** * Consume the entirety of the next container from the stream. * @param cramVersion * @param countingStream * @return The next Container from the stream. * @throws IOException */ protected Container containerFromStream(final Version cramVersion, final CountingInputStream countingStream) throws IOException { return ContainerIO.readContainer(cramHeader.getVersion(), countingStream); }
/** * Consume the entirety of the next container from the stream. * @param cramVersion * @param countingStream * @return The next Container from the stream. */ protected Container containerFromStream(final Version cramVersion, final CountingInputStream countingStream) { return ContainerIO.readContainer(cramHeader.getVersion(), countingStream); }
/** * Writes CRAM header into the specified {@link OutputStream}. * * @param cramHeader the {@link CramHeader} object to write * @param outputStream the output stream to write to * @return the number of bytes written out * @throws IOException as per java IO contract */ public static long writeCramHeader(final CramHeader cramHeader, final OutputStream outputStream) throws IOException { // if (cramHeader.getVersion().major < 3) throw new RuntimeException("Deprecated CRAM version: " + cramHeader.getVersion().major); outputStream.write("CRAM".getBytes("US-ASCII")); outputStream.write(cramHeader.getVersion().major); outputStream.write(cramHeader.getVersion().minor); outputStream.write(cramHeader.getId()); for (int i = cramHeader.getId().length; i < 20; i++) outputStream.write(0); final long length = CramIO.writeContainerForSamFileHeader(cramHeader.getVersion().major, cramHeader.getSamFileHeader(), outputStream); return CramIO.DEFINITION_LENGTH + length; }
/** * Read CRAM header from the given {@link InputStream}. * * @param inputStream input stream to read from * @return complete {@link CramHeader} object * @throws IOException as per java IO contract */ public static CramHeader readCramHeader(final InputStream inputStream) throws IOException { final CramHeader header = readFormatDefinition(inputStream); final SAMFileHeader samFileHeader = readSAMFileHeader(header.getVersion(), inputStream, new String(header.getId())); return new CramHeader(header.getVersion(), new String(header.getId()), samFileHeader); }
/** * Writes CRAM header into the specified {@link OutputStream}. * * @param cramHeader the {@link CramHeader} object to write * @param outputStream the output stream to write to * @return the number of bytes written out */ public static long writeCramHeader(final CramHeader cramHeader, final OutputStream outputStream) { // if (cramHeader.getVersion().major < 3) throw new RuntimeException("Deprecated CRAM version: " + cramHeader.getVersion().major); try { outputStream.write("CRAM".getBytes("US-ASCII")); outputStream.write(cramHeader.getVersion().major); outputStream.write(cramHeader.getVersion().minor); outputStream.write(cramHeader.getId()); for (int i = cramHeader.getId().length; i < 20; i++) outputStream.write(0); final long length = CramIO.writeContainerForSamFileHeader(cramHeader.getVersion().major, cramHeader.getSamFileHeader(), outputStream); return CramIO.DEFINITION_LENGTH + length; } catch (final IOException e) { throw new RuntimeIOException(e); } }
public static void main(String[] args) throws IOException, IllegalArgumentException, IllegalAccessException { Log.setGlobalLogLevel(LogLevel.INFO); File cramFile = new File(args[0]); InputStream is = new BufferedInputStream(new FileInputStream(cramFile)); CramHeader header = CramIO.readCramHeader(is); Container c = null; while ((c = ContainerIO.readContainer(header.getVersion(), is)) != null && !c.isEOF()) { for (Slice slice : c.slices) { if (slice.sequenceId == Slice.MULTI_REFERENCE) { System.out.println("Read feature B detected."); System.exit(1); } } } } }
/** * Read CRAM header from the given {@link InputStream}. * * @param inputStream input stream to read from * @return complete {@link CramHeader} object */ public static CramHeader readCramHeader(final InputStream inputStream) { try { final CramHeader header = readFormatDefinition(inputStream); final SAMFileHeader samFileHeader = readSAMFileHeader(header.getVersion(), inputStream, new String(header.getId())); return new CramHeader(header.getVersion(), new String(header.getId()), samFileHeader); } catch (final IOException e) { throw new RuntimeIOException(e); } }
void readNextContainer() { try { nextContainer = containerFromStream(cramHeader.getVersion(), countingInputStream); final long containerSizeInBytes = countingInputStream.getCount() - offset; nextContainer.offset = offset; offset += containerSizeInBytes; } catch (final IOException e) { throw new RuntimeException(e); } if (nextContainer.isEOF()) { eof = true; nextContainer = null; } }
/** * Check if the file: 1) contains proper CRAM header. 2) given the version info from the header check the end of file marker. * * @param file the CRAM file to check * @return true if the file is a valid CRAM file and is properly terminated with respect to the version. * @throws IOException as per java IO contract */ public static boolean checkHeaderAndEOF(final File file) throws IOException { final SeekableStream seekableStream = new SeekableFileStream(file); final CramHeader cramHeader = readCramHeader(seekableStream); return checkEOF(cramHeader.getVersion(), seekableStream); }
void readNextContainer() { nextContainer = containerFromStream(cramHeader.getVersion(), countingInputStream); final long containerSizeInBytes = countingInputStream.getCount() - offset; nextContainer.offset = offset; offset += containerSizeInBytes; if (nextContainer.isEOF()) { eof = true; nextContainer = null; } }
Container next() throws IOException { if (seekableStream.position() < (start >> 16)) seekableStream.seek(start >> 16); if (seekableStream.position() > (end >> 16)) throw new RuntimeException("No more containers in this boundary."); final long offset = seekableStream.position(); final Container c = ContainerIO.readContainer(cramHeader.getVersion(), seekableStream); c.offset = offset; return c; } }
/** * Check if the file: 1) contains proper CRAM header. 2) given the version info from the header check the end of file marker. * * @param file the CRAM file to check * @return true if the file is a valid CRAM file and is properly terminated with respect to the version. */ public static boolean checkHeaderAndEOF(final File file) { try (final SeekableStream seekableStream = new SeekableFileStream(file)) { final CramHeader cramHeader = readCramHeader(seekableStream); return checkEOF(cramHeader.getVersion(), seekableStream); } catch (final IOException e) { throw new RuntimeIOException(e); } }
private static void dist(File file, byte defaultQualityScore) throws IllegalArgumentException, IOException, IllegalAccessException { InputStream is = new FileInputStream(file); CramHeader header = CramIO.readCramHeader(is); Container c = null; ContainerParser parser = new ContainerParser(header.getSamFileHeader()); ArrayList<CramCompressionRecord> records = new ArrayList<CramCompressionRecord>(10000); long[] freq = new long[255]; while ((c = ContainerIO.readContainer(header.getVersion(), is)) != null && !c.isEOF()) { parser.getRecords(c, records, ValidationStringency.SILENT); CramNormalizer.restoreQualityScores(defaultQualityScore, records); for (CramCompressionRecord record : records) { for (byte b : record.qualityScores) freq[b & 0xFF]++; } records.clear(); } print(freq, defaultQualityScore, System.out); }
@Test public void testCheckHeaderAndEOF_v3() throws IOException { final String id = "testid"; final CramHeader cramHeader = new CramHeader(CramVersions.CRAM_v3, id, new SAMFileHeader()); final File file = File.createTempFile("test", ".cram"); file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); CramIO.writeCramHeader(cramHeader, fos); CramIO.issueEOF(cramHeader.getVersion(), fos); fos.close(); Assert.assertTrue(CramIO.checkHeaderAndEOF(file)); file.delete(); }
@Override public Container next() { try { if (seekableStream.position() < (start >> 16)) { seekableStream.seek(start >> 16); } if (!hasNext()) { throw new RuntimeException("No more containers in this boundary."); } final long offset = seekableStream.position(); final Container c = ContainerIO.readContainer(cramHeader.getVersion(), seekableStream); c.offset = offset; return c; } catch (final IOException e) { throw new RuntimeIOException(e); } } }
@Test public void testCheckHeaderAndEOF_v2() throws IOException { final String id = "testid"; final CramHeader cramHeader = new CramHeader(CramVersions.CRAM_v2_1, id, new SAMFileHeader()); final File file = File.createTempFile("test", ".cram"); file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); CramIO.writeCramHeader(cramHeader, fos); CramIO.issueEOF(cramHeader.getVersion(), fos); fos.close(); Assert.assertTrue(CramIO.checkHeaderAndEOF(file)); file.delete(); }
private boolean nextContainer() throws IOException { long offset = is.getCount(); Container c = ContainerIO.readContainer(cramHeader.getVersion(), is); if (c.isEOF()) return false; c.offset = offset; indexer.processContainer(c, ValidationStringency.DEFAULT_STRINGENCY); log.info("INDEXED: " + c.toString()); return true; }
private boolean nextContainer() throws IOException, IllegalArgumentException, IllegalAccessException { long offset = is.getCount(); Container c = ContainerIO.readContainer(cramHeader.getVersion(), is); if (c.isEOF()) return false; c.offset = offset; index.addContainer(c); log.info("INDEXED: " + c.toString()); return true; }
@Test public void testSliceMD5() throws IOException { final CramTestCase test = new CramTestCase(); // read the CRAM: final ByteArrayInputStream bais = new ByteArrayInputStream(test.cramData); final CramHeader cramHeader = CramIO.readCramHeader(bais); final Container container = ContainerIO.readContainer(cramHeader.getVersion(), bais); final Slice slice = container.slices[0]; Assert.assertEquals(slice.alignmentStart, 1); Assert.assertEquals(slice.alignmentSpan, test.referenceBases.length); // check the slice MD5 is the MD5 of upper-cased ref bases: final byte[] ucRefMD5 = SequenceUtil.calculateMD5(test.refBasesFromUCSource, 0, test.refBasesFromUCSource.length); Assert.assertEquals(slice.refMD5, ucRefMD5); // check the CRAM file reads: final CRAMFileReader reader = new CRAMFileReader(new ByteArrayInputStream(test.cramData), (File) null, test.referenceSourceUpperCased, ValidationStringency.STRICT); final SAMRecordIterator iterator = reader.getIterator(); Assert.assertTrue(iterator.hasNext()); Assert.assertEquals(iterator.next(), test.record); }
@Override public CloseableIterator<SAMRecord> queryUnmapped() { final long startOfLastLinearBin = getIndex().getStartOfLastLinearBin(); final SeekableStream seekableStream = getSeekableStreamOrFailWithRTE(); final CRAMIterator newIterator; try { seekableStream.seek(0); newIterator = new CRAMIterator(seekableStream, referenceSource, validationStringency); seekableStream.seek(startOfLastLinearBin >>> 16); final Container container = ContainerIO.readContainerHeader(newIterator.getCramHeader().getVersion().major, seekableStream); seekableStream.seek(seekableStream.position() + container.containerByteSize); iterator = newIterator; boolean atAlignments; do { atAlignments = iterator.advanceToAlignmentInContainer(SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX, SAMRecord.NO_ALIGNMENT_START); } while (!atAlignments && iterator.hasNext()); } catch (final IOException e) { throw new RuntimeEOFException(e); } return iterator; }