Skip to content

Commit

Permalink
Fixed bug disallowing file-based index and seekable path based source (
Browse files Browse the repository at this point in the history
…#769)

* Added support for use case where index is a file, and the source stream is a seekable path (e.g. a GCS location)
  • Loading branch information
kcibul authored and droazen committed Dec 6, 2016
1 parent a5ee55b commit 4d0070b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/java/htsjdk/samtools/SamReaderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down
38 changes: 37 additions & 1 deletion src/test/java/htsjdk/samtools/SamReaderFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4d0070b

Please sign in to comment.