diff --git a/src/main/java/htsjdk/samtools/SamReaderFactory.java b/src/main/java/htsjdk/samtools/SamReaderFactory.java index 2b57cbfb2..8769f4879 100644 --- a/src/main/java/htsjdk/samtools/SamReaderFactory.java +++ b/src/main/java/htsjdk/samtools/SamReaderFactory.java @@ -316,7 +316,7 @@ public SamReader open(final SamInputResource resource) { final SeekableStream indexSeekable = indexMaybe == null ? null : indexMaybe.asUnbufferedSeekableStream(); // do not close bufferedStream, it's the same stream we're getting here. SeekableStream sourceSeekable = data.asUnbufferedSeekableStream(); - if (indexFile!=null || null == sourceSeekable || null == indexSeekable) { + if (null == sourceSeekable || null == indexSeekable) { // not seekable. // it's OK that we consumed a bit of the stream already, this ctor expects it. primitiveSamReader = new BAMFileReader(bufferedStream, indexFile, false, asynchronousIO, validationStringency, this.samRecordFactory); @@ -326,7 +326,7 @@ public SamReader open(final SamInputResource resource) { // and read a bit from, and that form of the ctor expects the stream to start at 0. sourceSeekable.seek(0); primitiveSamReader = new BAMFileReader( - sourceSeekable, indexSeekable, false, asynchronousIO, validationStringency, this.samRecordFactory); + sourceSeekable, indexSeekable, false, asynchronousIO, validationStringency, this.samRecordFactory); } } else { bufferedStream.close(); diff --git a/src/test/java/htsjdk/samtools/SamReaderFactoryTest.java b/src/test/java/htsjdk/samtools/SamReaderFactoryTest.java index ece91e220..31ad5c259 100644 --- a/src/test/java/htsjdk/samtools/SamReaderFactoryTest.java +++ b/src/test/java/htsjdk/samtools/SamReaderFactoryTest.java @@ -284,7 +284,43 @@ public void queryInputResourcePermutation(final SamInputResource resource) throw } reader.close(); } - + + + /** + * A path that pretends it's not based upon a file. This helps in cases where we want to test branches + * that apply to non-file based paths without actually having to use non-file based resources (like cloud urls) + */ + private static class NeverFilePathInputResource extends PathInputResource { + public NeverFilePathInputResource(Path pathResource) { + super(pathResource); + } + + @Override + public File asFile() { + return null; + } + } + + @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()); + } + } + + @Test + public void queryStreamingPathBamWithFileIndex() throws IOException { + InputResource bam = new NeverFilePathInputResource(localBam.toPath()); + InputResource index = new FileInputResource(localBamIndex); + + final SamInputResource resource = new SamInputResource(bam, index); + queryInputResourcePermutation(new SamInputResource(bam, index)); + } + @Test public void customReaderFactoryTest() throws IOException { try {