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 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 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); }
reader.setValidationStringency(ValidationStringency.SILENT);
@Test public void scanAllUnmappedReads() throws IOException { SamReader samReader = SamReaderFactory.makeDefault().open(BAM_FILE); CRAMFileReader reader = new CRAMFileReader(new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(baiBytes), source, ValidationStringency.SILENT); reader.setValidationStringency(ValidationStringency.SILENT); int counter = 0; SAMRecordIterator unmappedSamIterator = samReader.queryUnmapped(); CloseableIterator<SAMRecord> unmappedCramIterator = reader.queryUnmapped(); while (unmappedSamIterator.hasNext()) { Assert.assertTrue(unmappedCramIterator.hasNext()); SAMRecord r1 = unmappedSamIterator.next(); SAMRecord r2 = unmappedCramIterator.next(); Assert.assertEquals(r1.getReadName(), r2.getReadName()); Assert.assertEquals(r1.getBaseQualityString(), r2.getBaseQualityString()); counter++; } Assert.assertFalse(unmappedCramIterator.hasNext()); Assert.assertEquals(counter, nofUnmappedReads); reader.close(); }
@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); }