@Test(dataProvider = "variousFormatReaderTestCases") public void variousFormatReaderTest(final String inputFile) throws IOException { final File input = new File(TEST_DATA_DIR, inputFile); final SamReader reader = SamReaderFactory.makeDefault().open(input); for (final SAMRecord ignored : reader) {} reader.close(); }
@Test public void checkHasIndexForStreamingPathBamWithFileIndex() throws IOException { InputResource bam = new NeverFilePathInputResource(localBam.toPath()); InputResource index = new FileInputResource(localBamIndex); // ensure that the index is being used, not checked in queryInputResourcePermutation try (final SamReader reader = SamReaderFactory.makeDefault().open(new SamInputResource(bam, index))) { Assert.assertTrue(reader.hasIndex()); } }
// See https://github.com/samtools/htsjdk/issues/76 @Test(dataProvider = "queryIntervalIssue76TestCases") public void queryIntervalIssue76( final String sequenceName, final int start, final int end, final int expectedCount) throws IOException { final File input = new File(TEST_DATA_DIR, "issue76.bam"); final SamReader reader = SamReaderFactory.makeDefault().open(input); final QueryInterval interval = new QueryInterval( reader.getFileHeader().getSequence(sequenceName).getSequenceIndex(), start, end); Assert.assertEquals(countRecordsInQueryInterval(reader, interval), expectedCount); reader.close(); }
@Test(dataProvider = "composeAllPermutationsOfSamInputResource") public void exhaustInputResourcePermutation(final SamInputResource resource) throws IOException { final SamReader reader = SamReaderFactory.makeDefault().open(resource); LOG.info(String.format("Reading from %s ...", resource)); final List<SAMRecord> slurped = Iterables.slurp(reader); final SAMFileHeader fileHeader = reader.getFileHeader(); reader.hasIndex(); reader.indexing().hasBrowseableIndex(); reader.close(); /* Ensure all tests have read the same records in the same order or, if this is the first test, set it as the template. */ observedHeaders.add(fileHeader); observedRecordOrdering.add(slurped); Assert.assertEquals(observedHeaders.size(), 1, "read different headers than other testcases"); Assert.assertEquals( observedRecordOrdering.size(), 1, "read different records than other testcases"); }
private int countRecords(final SamReader reader) { int count = 0; try (final SAMRecordIterator iter = reader.iterator()) { while (iter.hasNext()) { iter.next(); count++; } } return count; }
private int countRecordsInQueryInterval(final SamReader reader, final QueryInterval query) { final SAMRecordIterator iter = reader.queryOverlapping(new QueryInterval[] {query}); int count = 0; while (iter.hasNext()) { iter.next(); count++; } iter.close(); return count; }
@Test public void openPath() throws IOException { final Path path = localBam.toPath(); final List<SAMRecord> records; final SAMFileHeader fileHeader; try (final SamReader reader = SamReaderFactory.makeDefault().open(path)) { LOG.info(String.format("Reading from %s ...", path)); records = Iterables.slurp(reader); fileHeader = reader.getFileHeader(); reader.close(); } try (final SamReader fileReader = SamReaderFactory.makeDefault().open(localBam)) { final List<SAMRecord> expectedRecords = Iterables.slurp(fileReader); final SAMFileHeader expectedFileHeader = fileReader.getFileHeader(); Assert.assertEquals(records, expectedRecords); Assert.assertEquals(fileHeader, expectedFileHeader); } }
@Test(dataProvider = "variousFormatReaderTestCases") public void samRecordFactoryTest(final String inputFile) throws IOException { final File input = new File(TEST_DATA_DIR, inputFile); final SAMRecordFactoryTester recordFactory = new SAMRecordFactoryTester(); final SamReaderFactory readerFactory = SamReaderFactory.makeDefault().samRecordFactory(recordFactory); final SamReader reader = readerFactory.open(input); int i = 0; for (final SAMRecord ignored : reader) { ++i; } reader.close(); Assert.assertTrue(i > 0); if (inputFile.endsWith(".sam") || inputFile.endsWith(".sam.gz")) Assert.assertEquals(recordFactory.samRecordsCreated, i); else if (inputFile.endsWith(".bam")) Assert.assertEquals(recordFactory.bamRecordsCreated, i); }
@Test(dataProvider = "composeAllPermutationsOfSamInputResource") public void queryInputResourcePermutation(final SamInputResource resource) throws IOException { final SamReader reader = SamReaderFactory.makeDefault().open(resource); LOG.info(String.format("Query from %s ...", resource)); if (reader.hasIndex()) { final StopWatch stopWatch = new StopWatch(); stopWatch.start(); final SAMRecordIterator q1 = reader.query("chr1", 500000, 100000000, true); observedRecordOrdering1.add(Iterables.slurp(q1)); q1.close(); final SAMRecordIterator q20 = reader.query("chr20", 1, 1000000, true); observedRecordOrdering20.add(Iterables.slurp(q20)); q20.close(); final SAMRecordIterator q3 = reader.query("chr3", 1, 10000000, true); observedRecordOrdering3.add(Iterables.slurp(q3)); q3.close(); stopWatch.stop(); LOG.info(String.format("Finished queries in %sms", stopWatch.getElapsedTime())); Assert.assertEquals( observedRecordOrdering1.size(), 1, "read different records for chromosome 1"); Assert.assertEquals( observedRecordOrdering20.size(), 1, "read different records for chromosome 20"); Assert.assertEquals( observedRecordOrdering3.size(), 1, "read different records for chromosome 3"); } else if (resource.indexMaybe() != null) { LOG.warn("Resource has an index source, but is not indexed: " + resource); } else { LOG.info("Skipping query operation: no index."); } reader.close(); }
@Test public void customReaderFactoryTest() throws IOException { try { CustomReaderFactory.setInstance( new CustomReaderFactory( "https://www.googleapis.com/genomics/v1beta/reads/," + "htsjdk.samtools.SamReaderFactoryTest$TestReaderFactory")); final SamReader reader = SamReaderFactory.makeDefault() .open( SamInputResource.of( "https://www.googleapis.com/genomics/v1beta/reads/?uncompressed.sam")); int i = 0; for (@SuppressWarnings("unused") final SAMRecord ignored : reader) { ++i; } reader.close(); Assert.assertTrue(i > 0); } finally { CustomReaderFactory.resetToDefaultInstance(); } }