/** * Reads next container from the stream. * * @param inputStream the stream to read from * @return CRAM container or null if no more data * @throws IOException */ private static Container readContainer(final int major, final InputStream inputStream) throws IOException { return readContainer(major, inputStream, 0, Integer.MAX_VALUE); }
/** * Reads next container from the stream. * * @param inputStream the stream to read from * @return CRAM container or null if no more data */ private static Container readContainer(final int major, final InputStream inputStream) { return readContainer(major, inputStream, 0, Integer.MAX_VALUE); }
/** * 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); }
/** * Reads a CRAM container from the input stream. Returns an EOF container when there is no more data or the EOF marker found. * * @param version CRAM version to expect * @param inputStream the stream to read from * @return a new container object read from the stream */ public static Container readContainer(final Version version, final InputStream inputStream) { final Container container = readContainer(version.major, inputStream); if (container == null) { // this will cause System.exit(1): CramVersionPolicies.eofNotFound(version); return readContainer(version.major, new ByteArrayInputStream(CramIO.ZERO_B_EOF_MARKER)); } if (container.isEOF()) log.debug("EOF marker found, file/stream is complete."); return container; }
/** * Reads a CRAM container from the input stream. Returns an EOF container when there is no more data or the EOF marker found. * * @param version CRAM version to expect * @param inputStream the stream to read from * @return a new container object read from the stream * @throws IOException as per java IO contract */ public static Container readContainer(final Version version, final InputStream inputStream) throws IOException { final Container container = readContainer(version.major, inputStream); if (container == null) { // this will cause System.exit(1): CramVersionPolicies.eofNotFound(version); return readContainer(version.major, new ByteArrayInputStream(CramIO.ZERO_B_EOF_MARKER)); } if (container.isEOF()) log.debug("EOF marker found, file/stream is complete."); return container; }
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); } } } } }
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; } }
CramHeader header = CramIO.readCramHeader(is); Container c = null; while ((c = ContainerIO.readContainer(header.getVersion(), is)) != null && !c.isEOF()) { for (Slice slice : c.slices) { final DataReaderFactory dataReaderFactory = new DataReaderFactory();
@Test(dataProvider = "eof") public void testEOF(final Version version) throws IOException { final ContainerParser parser = new ContainerParser(ContainerFactoryTest.getSAMFileHeaderForTests()); try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { CramIO.issueEOF(version, baos); final Container container = ContainerIO.readContainer(version, new ByteArrayInputStream(baos.toByteArray())); Assert.assertTrue(container.isEOF()); Assert.assertTrue(parser.getRecords(container, null, ValidationStringency.STRICT).isEmpty()); } }
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); }
@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); } } }
Container container; try { container = ContainerIO.readContainer(header.getVersion(), new ByteArrayInputStream(object.bytes)); if (container.isEOF()) return null;
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; }
/** * Generate and write a CRAI index to an output stream from a CRAM input stream * * @param cramStream CRAM stream to index; must be coordinate sorted * @param craiStream stream for output index */ public static void writeIndex(final SeekableStream cramStream, OutputStream craiStream) { try { final CramHeader cramHeader = CramIO.readCramHeader(cramStream); final CRAMCRAIIndexer indexer = new CRAMCRAIIndexer(craiStream, cramHeader.getSamFileHeader()); final Version cramVersion = cramHeader.getVersion(); // get the first container and it's offset long offset = cramStream.position(); Container container = ContainerIO.readContainer(cramVersion, cramStream); while (container != null && !container.isEOF()) { container.offset = offset; indexer.processContainer(container); offset = cramStream.position(); container = ContainerIO.readContainer(cramVersion, cramStream); } indexer.finish(); } catch (IOException e) { throw new RuntimeIOException("Error writing CRAI index to output stream"); } }
@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); }
/** * Generate and write a CRAI index to an output stream from a CRAM input stream * * @param cramStream CRAM stream to index; must be coordinate sorted * @param craiStream stream for output index */ public static void writeIndex(final SeekableStream cramStream, OutputStream craiStream) { try { final CramHeader cramHeader = CramIO.readCramHeader(cramStream); final CRAMCRAIIndexer indexer = new CRAMCRAIIndexer(craiStream, cramHeader.getSamFileHeader()); final Version cramVersion = cramHeader.getVersion(); // get the first container and it's offset long offset = cramStream.position(); Container container = ContainerIO.readContainer(cramVersion, cramStream); while (container != null && !container.isEOF()) { container.offset = offset; indexer.processContainer(container); offset = cramStream.position(); container = ContainerIO.readContainer(cramVersion, cramStream); } indexer.finish(); } catch (IOException e) { throw new RuntimeIOException("Error writing CRAI index to output stream"); } }
Container container = ContainerIO.readContainer(actualHeader.getVersion(), seekableFileStream); Assert.assertEquals(container.alignmentStart, fullContainer.alignmentStart); Assert.assertEquals(container.alignmentSpan, fullContainer.alignmentSpan);
try { final long offset = stream.position(); container = ContainerIO.readContainer(cramHeader.getVersion(), stream); if (container == null || container.isEOF()) { break;