@Test public void inputResourceFromStringTest() throws IOException { Assert.assertEquals( SamInputResource.of("http://test.url").data().type(), InputResource.Type.URL); Assert.assertEquals( SamInputResource.of("https://test.url").data().type(), InputResource.Type.URL); Assert.assertEquals( SamInputResource.of("ftp://test.url").data().type(), InputResource.Type.URL); Assert.assertEquals(SamInputResource.of("/a/b/c").data().type(), InputResource.Type.FILE); }
@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 testSamReaderFromURL() throws IOException { final String samFilePath = new File(TEST_DATA_DIR, "unsorted.sam").getAbsolutePath(); final URL samURL = new URL("file://" + samFilePath); final SamReaderFactory factory = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT); final SamReader reader = factory.open(SamInputResource.of(samURL)); Assert.assertEquals(countRecords(reader), 10); }
@Test public void testSamReaderFromSeekableStream() throws IOException { // even though a SAM isn't indexable, make sure we can open one // using a seekable stream final File samFile = new File(TEST_DATA_DIR, "unsorted.sam"); final SamReaderFactory factory = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT); final SamReader reader = factory.open(SamInputResource.of(new SeekableFileStream(samFile))); Assert.assertEquals(countRecords(reader), 10); }
@Test(expectedExceptions = RuntimeIOException.class) public void testCRAMReaderFromURLBadIndexFile() throws IOException { // deliberately specify a bad index file to ensure we get an IOException getCRAMReaderFromInputResource( (cramURL, indexURL) -> { return SamInputResource.of(cramURL).index(new File("nonexistent.bai")); }, true, 3); }
@Test public void testCRAMReaderFromURLNoIndexFile() throws IOException { // get just a CRAM reader (no index) from an URL-backed resource getCRAMReaderFromInputResource( (cramURL, indexURL) -> { return SamInputResource.of(cramURL); }, false, 11); }
@Test public void testCRAMReaderFromURL() throws IOException { // get a CRAM reader with an index from a URL-backed resource getCRAMReaderFromInputResource( (cramURL, indexURL) -> { return SamInputResource.of(cramURL).index(indexURL); }, true, 3); }
@Test(expectedExceptions = SAMFormatException.class) public void testSamReaderFromMalformedSeekableStream() throws IOException { // use a bogus (.bai file) to force SamReaderFactory to fall through to the // fallback code that assumes a SAM File when it can't determine the // format of the input, to ensure that it results in a SAMFormatException final File samFile = new File(TEST_DATA_DIR, "cram_with_bai_index.cram.bai"); final SamReaderFactory factory = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT); final SamReader reader = factory.open(SamInputResource.of(new SeekableFileStream(samFile))); countRecords(reader); }
@Test public void testCRAMReaderFromURLStream() throws IOException { // get a CRAM reader with an index from a stream-backed resource created from a URL getCRAMReaderFromInputResource( (cramURL, indexURL) -> { try { ISeekableStreamFactory streamFactory = SeekableStreamFactory.getInstance(); return SamInputResource.of(streamFactory.getStreamFor(cramURL)) .index(streamFactory.getStreamFor(indexURL)); } catch (IOException e) { throw new RuntimeIOException(e); } }, true, 3); }
@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(); } }