@Test public void testCRAMReader3_ShouldUseCRAMIndex() { CRAMFileReader reader = new CRAMFileReader(CRAM_WITH_CRAI, INDEX_FILE, REFERENCE); reader.getIndex(); Assert.assertTrue(reader.hasIndex(), "Can't find existing CRAM index."); }
private int getCRAMRecordCount(final File cramFile) { final CRAMFileReader cramReader = new CRAMFileReader(cramFile, (File)null, testReferenceSource); final Iterator<SAMRecord> it = cramReader.getIterator(); int recCount = 0; while (it.hasNext()) { it.next(); recCount++; } cramReader.close(); return recCount; }
@Override void applyTo(final CRAMFileReader underlyingReader, final SamReader reader) { underlyingReader.enableFileSource(reader, true); }
/** * Create a CRAMFileReader from a file using the supplied reference source. * * @param cramFile CRAM file to read. Can not be null. * @param referenceSource a {@link htsjdk.samtools.cram.ref.CRAMReferenceSource source} of * reference sequences. May not be null. * @throws IllegalArgumentException if the {@code cramFile} or the {@code CRAMReferenceSource} is null */ public CRAMFileReader(final File cramFile, final CRAMReferenceSource referenceSource) { if (cramFile == null) throw new IllegalArgumentException("CRAM file cannot be null."); if (referenceSource == null) { throw new IllegalArgumentException("A reference is required for CRAM readers"); } this.cramFile = cramFile; this.referenceSource = referenceSource; mIndexFile = findIndexForFile(null, cramFile); getIterator(); }
/** * Create a CRAMFileReader from a CRAM file and optional index file using the supplied reference * source and validation stringency. * * @param cramFile CRAM stream to read. May not be null. * @param indexFile index file to be used for random access. May be null. * @param referenceSource a {@link htsjdk.samtools.cram.ref.CRAMReferenceSource source} of * reference sequences. May not be null. * @param validationStringency Validation stringency to be used when reading * * @throws IllegalArgumentException if the {@code cramFile} or the {@code CRAMReferenceSource} is null */ public CRAMFileReader(final File cramFile, final File indexFile, final CRAMReferenceSource referenceSource, final ValidationStringency validationStringency) throws IOException { if (cramFile == null) { throw new IllegalArgumentException("Input file can not be null for CRAM reader"); } if (referenceSource == null) { throw new IllegalArgumentException("A reference is required for CRAM readers"); } this.cramFile = cramFile; this.referenceSource = referenceSource; this.mIndexFile = findIndexForFile(indexFile, cramFile); final SeekableFileStream indexStream = this.mIndexFile == null ? null : new SeekableFileStream(this.mIndexFile); initWithStreams(new FileInputStream(cramFile), indexStream, validationStringency); }
@Test(expectedExceptions = SAMException.class) public void testCRAMReader7_WithoutCRAMIndex() throws IOException { File indexFile = null; CRAMFileReader reader = new CRAMFileReader(CRAM_WITHOUT_CRAI, indexFile, REFERENCE, ValidationStringency.STRICT); reader.getIndex(); }
@Test(expectedExceptions = CRAMException.class) public void testExceptionWhileReadingWithWrongReference() throws IOException { final CramTestCase test = new CramTestCase(); // try reading the CRAM file with the incorrect ref source that does not upper case bases: final CRAMFileReader reader = new CRAMFileReader(new ByteArrayInputStream(test.cramData), (File) null, test.referenceSourceMixedCase, ValidationStringency.STRICT); final SAMRecordIterator iterator = reader.getIterator(); // expect an exception here due to slice MD5 mismatch: iterator.hasNext(); }
@Test(expectedExceptions = SAMException.class) public void testFileFileConstructorNoIndex () throws IOException { CRAMFileReader reader = new CRAMFileReader( new SeekableFileStream(tmpCramFile), (File) null, source, ValidationStringency.STRICT); try { reader.queryAlignmentStart("chrM", 1500); } finally { reader.close(); } }
@Test(description = "Test CRAMReader 7 reference required", expectedExceptions = IllegalArgumentException.class) public void testCRAMReader7_ReferenceRequired() throws IOException { ReferenceSource refSource = null; new CRAMFileReader(CRAM_WITH_CRAI, CRAM_WITH_CRAI, refSource, ValidationStringency.STRICT); }
@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); }
final AlignmentSpan alignmentSpan = references.get(refId); CRAMFileReader reader = new CRAMFileReader(new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(baiBytes), source, ValidationStringency.SILENT); reader.setValidationStringency(ValidationStringency.SILENT); final BAMIndex index = reader.getIndex(); final SAMFileSpan spanOfSecondContainer = index.getSpanOverlapping(refId, alignmentSpan.getStart(), alignmentSpan.getStart()+ alignmentSpan.getSpan()); Assert.assertNotNull(spanOfSecondContainer); Assert.assertTrue(spanOfSecondContainer instanceof BAMFileSpan); final CloseableIterator<SAMRecord> iterator = reader.getIterator(spanOfSecondContainer); Assert.assertTrue(iterator.hasNext()); int counter = 0;
private SAMRecordIterator getCramFileIterator(ValidationStringency valStringency) { final File refFile = new File("src/test/resources/htsjdk/samtools/cram/ce.fa"); final File cramFile = new File("src/test/resources/htsjdk/samtools/cram/ce#containsInvalidRecords.3.0.cram"); final ReferenceSource source = new ReferenceSource(refFile); final CRAMFileReader cramFileReader = new CRAMFileReader(cramFile, (SeekableStream) null, source); cramFileReader.setValidationStringency(valStringency); return cramFileReader.getIterator(); } }
@Test public void testIteratorFromFileSpan_WholeFile() throws IOException { CRAMFileReader reader = new CRAMFileReader(new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(baiBytes), source, ValidationStringency.SILENT); reader.setValidationStringency(ValidationStringency.SILENT); final SAMFileSpan allContainers = reader.getFilePointerSpanningReads(); final CloseableIterator<SAMRecord> iterator = reader.getIterator(allContainers); Assert.assertTrue(iterator.hasNext()); int counter = 0; while (iterator.hasNext()) { iterator.next(); counter++; } Assert.assertEquals(counter, nofReads); }
final AlignmentSpan alignmentSpan = references.get(refId); CRAMFileReader reader = new CRAMFileReader( new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(craiBytes), ValidationStringency.STRICT); final BAMIndex index = reader.getIndex(); final SAMFileSpan spanOfSecondContainer = index.getSpanOverlapping(refId, alignmentSpan.getStart(), alignmentSpan.getStart()+ alignmentSpan.getSpan()); Assert.assertNotNull(spanOfSecondContainer); Assert.assertTrue(spanOfSecondContainer instanceof BAMFileSpan); final CloseableIterator<SAMRecord> iterator = reader.getIterator(spanOfSecondContainer); Assert.assertTrue(iterator.hasNext()); int counter = 0;
@Test public void testQueryIntervalWithFilePointers() throws IOException { CRAMFileReader reader = new CRAMFileReader( new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(craiBytes), source, ValidationStringency.STRICT); QueryInterval[] query = new QueryInterval[]{new QueryInterval(0, 1519, 1520), new QueryInterval(1, 470535, 470536)}; BAMFileSpan fileSpan = BAMFileReader.getFileSpan(query, reader.getIndex()); final CloseableIterator<SAMRecord> iterator = reader.createIndexIterator(query, false, fileSpan.toCoordinateArray()); Assert.assertTrue(iterator.hasNext()); SAMRecord r1 = iterator.next(); Assert.assertEquals(r1.getReadName(), "3968040"); Assert.assertTrue(iterator.hasNext()); SAMRecord r2 = iterator.next(); Assert.assertEquals(r2.getReadName(), "140419"); Assert.assertFalse(iterator.hasNext()); iterator.close(); reader.close(); }
@Test public void testIteratorWholeFileSpan() throws IOException { CRAMFileReader reader = new CRAMFileReader( new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(craiBytes), source, ValidationStringency.STRICT); final SAMFileSpan allContainers = reader.getFilePointerSpanningReads(); final CloseableIterator<SAMRecord> iterator = reader.getIterator(allContainers); Assert.assertTrue(iterator.hasNext()); long count = getIteratorCount(iterator); Assert.assertEquals(count, nofReads); }
@Test public void testCRAIIndexerFromStream() 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); // get the header to use CRAMFileReader reader = new CRAMFileReader( CRAMFile, null, refSource, ValidationStringency.STRICT); SAMFileHeader samHeader = reader.getFileHeader(); reader.close(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); CRAMCRAIIndexer craiIndexer = new CRAMCRAIIndexer(bos, samHeader); craiIndexer.writeIndex(new SeekableFileStream(CRAMFile), bos); List<CRAIEntry> craiEntries = CRAMCRAIIndexer.readIndex(new ByteArrayInputStream(bos.toByteArray())).getCRAIEntries(); Assert.assertEquals(craiEntries.size(), 1); }
@Test public void testCRAMReader7_ShouldUseCRAMIndex() throws IOException { CRAMFileReader reader = new CRAMFileReader(CRAM_WITH_CRAI, INDEX_FILE, REFERENCE, ValidationStringency.STRICT); Assert.assertTrue(reader.hasIndex(), "Can't find existing CRAM index."); }
@Test public void testQueryInterval() throws IOException { CRAMFileReader reader = new CRAMFileReader(new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(baiBytes), source, ValidationStringency.SILENT); QueryInterval[] query = new QueryInterval[]{new QueryInterval(0, 1519, 1520), new QueryInterval(1, 470535, 470536)}; final CloseableIterator<SAMRecord> iterator = reader.query(query, false); Assert.assertTrue(iterator.hasNext()); SAMRecord r1 = iterator.next(); Assert.assertEquals(r1.getReadName(), "3968040"); Assert.assertTrue(iterator.hasNext()); SAMRecord r2 = iterator.next(); Assert.assertEquals(r2.getReadName(), "140419"); Assert.assertFalse(iterator.hasNext()); iterator.close(); reader.close(); }
@Test public void test() throws IOException { CRAMFileReader reader = new CRAMFileReader(new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(indexBytes), source, ValidationStringency.SILENT); for (SAMSequenceRecord sequenceRecord : reader.getFileHeader().getSequenceDictionary().getSequences()) { final CloseableIterator<SAMRecord> iterator = reader.queryAlignmentStart(sequenceRecord.getSequenceName(), 1); Assert.assertNotNull(iterator); Assert.assertTrue(iterator.hasNext()); SAMRecord record = iterator.next(); Assert.assertEquals(record.getReferenceName(), sequenceRecord.getSequenceName()); Assert.assertEquals(record.getAlignmentStart(), 1); } }