private static List<Long> getContainerOffsets(Configuration conf, Path cramFile) throws IOException { SeekableStream seekableStream = WrapSeekable.openPath(conf, cramFile); CramContainerIterator cci = new CramContainerIterator(seekableStream); List<Long> containerOffsets = new ArrayList<Long>(); containerOffsets.add(seekableStream.position()); while (cci.hasNext()) { cci.next(); containerOffsets.add(seekableStream.position()); } containerOffsets.add(seekableStream.length()); return containerOffsets; }
@Override public boolean hasNext() { if (eof) return false; if (nextContainer == null) readNextContainer(); return !eof; }
@Test public void testIteratorFromFileSpan_SecondContainer() throws IOException, IllegalAccessException { CramContainerIterator it = new CramContainerIterator(new ByteArrayInputStream(cramBytes)); it.hasNext(); it.next(); it.hasNext(); Container secondContainer = it.next(); Assert.assertNotNull(secondContainer); final Map<Integer, AlignmentSpan> references = new ContainerParser(it.getCramHeader().getSamFileHeader()).getReferences(secondContainer, ValidationStringency.STRICT); it.close(); int refId = new TreeSet<Integer>(references.keySet()).iterator().next(); final AlignmentSpan alignmentSpan = references.get(refId);
public CRAMIterator(final InputStream inputStream, final CRAMReferenceSource referenceSource, final ValidationStringency validationStringency) throws IOException { if (null == referenceSource) { throw new CRAMException("A reference source is required for CRAM files"); } this.countingInputStream = new CountingInputStream(inputStream); this.referenceSource = referenceSource; this.validationStringency = validationStringency; final CramContainerIterator containerIterator = new CramContainerIterator(this.countingInputStream); cramHeader = containerIterator.getCramHeader(); this.containerIterator = containerIterator; firstContainerOffset = this.countingInputStream.getCount(); records = new ArrayList<SAMRecord>(CRAMContainerStreamWriter.DEFAULT_RECORDS_PER_SLICE); normalizer = new CramNormalizer(cramHeader.getSamFileHeader(), referenceSource); parser = new ContainerParser(cramHeader.getSamFileHeader()); }
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; } }
@Test public void testIteratorSecondContainerSpan() throws IOException, IllegalAccessException { CramContainerIterator it = new CramContainerIterator(new ByteArrayInputStream(cramBytes)); it.hasNext(); it.next(); it.hasNext(); Container secondContainer = it.next(); Assert.assertNotNull(secondContainer); final Map<Integer, AlignmentSpan> references = new ContainerParser(it.getCramHeader().getSamFileHeader()).getReferences(secondContainer, ValidationStringency.STRICT); it.close();
public CRAMIterator(final InputStream inputStream, final CRAMReferenceSource referenceSource, final ValidationStringency validationStringency) { if (null == referenceSource) { throw new CRAMException("A reference source is required for CRAM files"); } this.countingInputStream = new CountingInputStream(inputStream); this.referenceSource = referenceSource; this.validationStringency = validationStringency; final CramContainerIterator containerIterator = new CramContainerIterator(this.countingInputStream); cramHeader = containerIterator.getCramHeader(); this.containerIterator = containerIterator; firstContainerOffset = this.countingInputStream.getCount(); records = new ArrayList<>(CRAMContainerStreamWriter.DEFAULT_RECORDS_PER_SLICE); normalizer = new CramNormalizer(cramHeader.getSamFileHeader(), referenceSource); parser = new ContainerParser(cramHeader.getSamFileHeader()); }
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; } }
private static List<Long> getContainerOffsets(Configuration conf, Path cramFile) throws IOException { SeekableStream seekableStream = WrapSeekable.openPath(conf, cramFile); CramContainerIterator cci = new CramContainerIterator(seekableStream); List<Long> containerOffsets = new ArrayList<Long>(); containerOffsets.add(seekableStream.position()); while (cci.hasNext()) { cci.next(); containerOffsets.add(seekableStream.position()); } containerOffsets.add(seekableStream.length()); return containerOffsets; }
List<Container> fullContainers; try (SeekableFileStream seekableFileStream = new SeekableFileStream(cramFile)) { CramContainerIterator iterator = new CramContainerIterator(seekableFileStream); expectedHeader = iterator.getCramHeader(); fullContainers = Iterables.slurp(iterator);
@Override public boolean hasNext() { if (eof) return false; if (nextContainer == null) readNextContainer(); return !eof; }
private static List<Long> getContainerOffsets(Configuration conf, Path cramFile) throws IOException { SeekableStream seekableStream = WrapSeekable.openPath(conf, cramFile); CramContainerIterator cci = new CramContainerIterator(seekableStream); List<Long> containerOffsets = new ArrayList<Long>(); containerOffsets.add(seekableStream.position()); while (cci.hasNext()) { cci.next(); containerOffsets.add(seekableStream.position()); } containerOffsets.add(seekableStream.length()); return containerOffsets; }
@Test public void testCRAIIndexerFromContainer() throws IOException { final File CRAMFile = new File("src/test/resources/htsjdk/samtools/cram/test2.cram"); final File refFile = new File("src/test/resources/htsjdk/samtools/cram/auxf.fa"); ReferenceSource refSource = new ReferenceSource(refFile); CRAMFileReader reader = new CRAMFileReader( CRAMFile, null, refSource, ValidationStringency.STRICT); SAMFileHeader samHeader = reader.getFileHeader(); Iterator<SAMRecord> it = reader.getIterator(); while(it.hasNext()) { SAMRecord samRec = it.next(); } reader.close(); FileInputStream fis = new FileInputStream(CRAMFile); CramContainerIterator cit = new CramContainerIterator(fis); ByteArrayOutputStream bos = new ByteArrayOutputStream(); CRAMCRAIIndexer craiIndexer = new CRAMCRAIIndexer(bos, samHeader); while (cit.hasNext()) { craiIndexer.processContainer(cit.next()); } craiIndexer.finish(); bos.close(); List<CRAIEntry> craiEntries = CRAMCRAIIndexer.readIndex(new ByteArrayInputStream(bos.toByteArray())).getCRAIEntries(); Assert.assertEquals(craiEntries.size(), 1); }