@Test public void test_chrM_1500_location() throws IOException { CRAMFileReader reader = new CRAMFileReader(cramFile, indexFile, source); reader.setValidationStringency(ValidationStringency.SILENT); CloseableIterator<SAMRecord> iterator = reader.queryAlignmentStart("chrM", 1500); Assert.assertTrue(iterator.hasNext()); SAMRecord record = iterator.next(); Assert.assertEquals(record.getReferenceName(), "chrM"); Assert.assertTrue(record.getAlignmentStart() >= 1500); }
@Test public void testFileFileConstructor () throws IOException { CRAMFileReader reader = new CRAMFileReader( tmpCramFile, tmpCraiFile, source, ValidationStringency.STRICT); CloseableIterator<SAMRecord> iterator = reader.queryAlignmentStart("chrM", 1500); Assert.assertTrue(iterator.hasNext()); SAMRecord record = iterator.next(); Assert.assertEquals(record.getReferenceName(), "chrM"); Assert.assertTrue(record.getAlignmentStart() >= 1500); reader.close(); }
@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(expectedExceptions = SAMException.class) public void testStreamStreamConstructorNoIndex () throws IOException { CRAMFileReader reader = new CRAMFileReader( new SeekableFileStream(tmpCramFile), (SeekableFileStream) null, source, ValidationStringency.STRICT); try { reader.queryAlignmentStart("chrM", 1500); } finally { reader.close(); } }
@Test public void testStreamFileConstructor () throws IOException { CRAMFileReader reader = new CRAMFileReader( new SeekableFileStream(tmpCramFile), tmpCraiFile, source, ValidationStringency.STRICT); CloseableIterator<SAMRecord> iterator = reader.queryAlignmentStart("chrM", 1500); Assert.assertTrue(iterator.hasNext()); SAMRecord record = iterator.next(); Assert.assertEquals(record.getReferenceName(), "chrM"); Assert.assertTrue(record.getAlignmentStart() >= 1500); reader.close(); }
@Test public void testStreamStreamConstructor() throws IOException { CRAMFileReader reader = new CRAMFileReader( new SeekableFileStream(tmpCramFile), new SeekableFileStream(tmpCraiFile), source, ValidationStringency.STRICT); CloseableIterator<SAMRecord> iterator = reader.queryAlignmentStart("chrM", 1500); Assert.assertTrue(iterator.hasNext()); SAMRecord record = iterator.next(); Assert.assertEquals(record.getReferenceName(), "chrM"); Assert.assertTrue(record.getAlignmentStart() >= 1500); 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); } }
@Test public void testConstructors () throws IOException { CRAMFileReader reader = new CRAMFileReader(cramFile, indexFile, source, ValidationStringency.SILENT); CloseableIterator<SAMRecord> iterator = reader.queryAlignmentStart("chrM", 1500); Assert.assertTrue(iterator.hasNext()); SAMRecord record = iterator.next(); iterator = reader.queryAlignmentStart("chrM", 1500); Assert.assertTrue(iterator.hasNext()); record = iterator.next(); iterator = reader.queryAlignmentStart("chrM", 1500); Assert.assertTrue(iterator.hasNext()); record = iterator.next(); reader.queryAlignmentStart("chrM", 1500); Assert.fail("Expecting query to fail when there is no index"); } catch (SAMException e) { reader.queryAlignmentStart("chrM", 1500); Assert.fail("Expecting query to fail when there is no index"); } catch (SAMException e) {
/** * This is to check that the indexing actually works and not just skips records. The approach is to forbid reading of the first * container and try accessing reads from the first and the second containers. The first attempt should fail but the second should succeed. * * @throws IOException */ @Test public void testUnnecessaryIO() throws IOException { final SeekableStream baiStream = SamIndexes.asBaiSeekableStreamOrNull(new ByteArraySeekableStream(indexBytes), header.getSequenceDictionary()); BAMIndex index = new CachingBAMFileIndex(baiStream, header.getSequenceDictionary()); int refID = 0; long start = index.getSpanOverlapping(refID, 1, Integer.MAX_VALUE).getFirstOffset(); long end = index.getSpanOverlapping(refID + 1, 1, Integer.MAX_VALUE).getFirstOffset(); TabuRegionInputStream tabuIS = new TabuRegionInputStream(Arrays.asList(new Chunk[]{new Chunk(start, end)}), new ByteArraySeekableStream(cramBytes)); CRAMFileReader reader = new CRAMFileReader(tabuIS, new ByteArraySeekableStream(indexBytes), source, ValidationStringency.SILENT); try { // the attempt to read 1st container, which will happen when the iterator is initialized, must throw CloseableIterator<SAMRecord> it = reader.queryAlignmentStart(header.getSequence(refID).getSequenceName(), 1); Assert.fail(); } catch (TabuError e) { } // reading after the 1st container should be ok: refID = 2; final CloseableIterator<SAMRecord> iterator = reader.queryAlignmentStart(header.getSequence(refID).getSequenceName(), 1); Assert.assertNotNull(iterator); Assert.assertTrue(iterator.hasNext()); }
CloseableIterator<SAMRecord> iterator = cramReader.queryAlignmentStart( samRecord.getReferenceName(), samRecord.getAlignmentStart());
@Test public void scanMappedReads() throws IOException { SamReader samReader = SamReaderFactory.makeDefault().open(BAM_FILE); SAMRecordIterator samRecordIterator = samReader.iterator(); CRAMFileReader reader = new CRAMFileReader(new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(baiBytes), source, ValidationStringency.SILENT); reader.setValidationStringency(ValidationStringency.SILENT); int counter = 0; while (samRecordIterator.hasNext()) { SAMRecord samRecord = samRecordIterator.next(); if (samRecord.getReferenceIndex() == SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX) break; // test only 1st and 2nd in every 100 to speed the test up: if (counter++ %100 > 1) continue; String s1 = samRecord.getSAMString(); CloseableIterator<SAMRecord> iterator = reader.queryAlignmentStart(samRecord.getReferenceName(), samRecord.getAlignmentStart()); Assert.assertTrue(iterator.hasNext(), counter + ": " + s1); SAMRecord cramRecord = iterator.next(); String s2 = cramRecord.getSAMString(); Assert.assertEquals(samRecord.getReferenceName(), cramRecord.getReferenceName(), s1 + s2); // default 'overlap' is true, so test records intersect the query: Assert.assertTrue(CoordMath.overlaps(cramRecord.getAlignmentStart(), cramRecord.getAlignmentEnd(), samRecord.getAlignmentStart(), samRecord.getAlignmentEnd()), s1 + s2); } samRecordIterator.close(); reader.close(); Assert.assertEquals(counter, nofMappedReads); }