private static SamInputResource openBAMFile(Storage.Objects storageClient, String gcsStoragePath, SeekableStream index) throws IOException { SeekableGCSStream s = new SeekableGCSStream(storageClient, gcsStoragePath); SamInputResource samInputResource = SamInputResource.of(s); if (index != null) { samInputResource.index(index); } LOG.info("getReadsFromBAMFile - got input resources"); return samInputResource; }
@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); }
/** Creates a {@link SamInputResource} reading from the provided resource, with no index. */ public static SamInputResource of(final Path path) { // in the case of named pipes and other non-seekable paths there's a bug in the implementation of // java's GZIPInputStream which inappropriately uses .available() and then gets confused with the result // of 0. For reference see: // https://bugs.java.com/view_bug.do?bug_id=7036144 // https://github.com/samtools/htsjdk/pull/1077 // https://github.com/samtools/htsjdk/issues/898 // This still doesn't support the case where someone is creating a named pipe in a non-default // file system and then using it as input and passing a GZIPed into the other end of the pipe. // To work around this bug, we fall back to using a FileInputResource rather than a PathInputResource // when we encounter a non-regular file using the default NIO filesystem (file://) if (path.getFileSystem() == FileSystems.getDefault() && !Files.isRegularFile(path)) { return of(path.toFile()); } else { return new SamInputResource(new PathInputResource(path)); } }
/** Creates a {@link SamInputResource} from a string specifying *either* a url or a file path */ public static SamInputResource of(final String string) { try { URL url = new URL(string); // this will throw if its not a url return of(url); } catch (MalformedURLException e) { // ignore } return of(new File(string)); }
@Test public void testWrappersAreAccessed() throws Exception { Path path = Paths.get(localBam); Path indexPath = Paths.get(localBam + ".bai"); HashMap<String, Boolean> fired = new HashMap<>(); Function<SeekableByteChannel, SeekableByteChannel> wrapData = (SeekableByteChannel in) -> { fired.put("data", true); return in; }; Function<SeekableByteChannel, SeekableByteChannel> wrapIndex = (SeekableByteChannel in) -> { fired.put("index", true); return in; }; SamInputResource in = SamInputResource.of(path, wrapData); in.index(indexPath, wrapIndex); InputResource indexResource = in.indexMaybe(); Assert.assertNotNull(indexResource); Assert.assertFalse(fired.containsKey("data")); Assert.assertFalse(fired.containsKey("index")); indexResource.asUnbufferedSeekableStream(); Assert.assertFalse(fired.containsKey("data")); Assert.assertTrue(fired.containsKey("index")); in.data().asUnbufferedSeekableStream(); Assert.assertTrue(fired.containsKey("data")); Assert.assertTrue(fired.containsKey("index")); }
/** Creates a {@link SamInputResource} reading from the provided resource, with no index, * and with a wrapper to apply to the SeekableByteChannel for custom prefetching/buffering. */ public static SamInputResource of(final Path path, Function<SeekableByteChannel, SeekableByteChannel> wrapper) { return new SamInputResource(new PathInputResource(path, wrapper)); }
final SamReader.PrimitiveSamReader primitiveSamReader; try { final InputResource data = resource.data(); final InputResource indexMaybe = resource.indexMaybe(); final boolean indexDefined = indexMaybe != null;
public SeekingBAMFileReader(final SamInputResource resource, final boolean eagerDecode, final ValidationStringency validationStringency, final SAMRecordFactory factory, long offset) throws IOException { super(resource.data().asUnbufferedSeekableStream(), (SeekableStream)null, eagerDecode, validationStringency, factory); this.offset = offset; this.stream = resource.data().asUnbufferedSeekableStream(); }
@Test(dataProvider = "composeAllPermutationsOfSamInputResource") public void queryInputResourcePermutation(final SamInputResource resource) throws IOException { try(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."); } } }
/** Creates a {@link SamInputResource} from a string specifying *either* a url or a file path */ public static SamInputResource of(final String string) { try { URL url = new URL(string); // this will throw if its not a url return of(url); } catch (MalformedURLException e) { // ignore } return of(new File(string)); }
/** Creates a {@link SamInputResource} reading from the provided resource, with no index, * and with a wrapper to apply to the SeekableByteChannel for custom prefetching/buffering. */ public static SamInputResource of(final Path path, Function<SeekableByteChannel, SeekableByteChannel> wrapper) { return new SamInputResource(new PathInputResource(path, wrapper)); }
final SamReader.PrimitiveSamReader primitiveSamReader; try { final InputResource data = resource.data(); final InputResource indexMaybe = resource.indexMaybe(); final boolean indexDefined = indexMaybe != null;
public SeekingBAMFileReader(final SamInputResource resource, final boolean eagerDecode, final ValidationStringency validationStringency, final SAMRecordFactory factory, long offset) throws IOException { super(resource.data().asUnbufferedSeekableStream(), (SeekableStream)null, eagerDecode, validationStringency, factory); this.offset = offset; this.stream = resource.data().asUnbufferedSeekableStream(); }
private static SamInputResource openBAMFile(Storage.Objects storageClient, String gcsStoragePath, SeekableStream index) throws IOException { SeekableGCSStream s = new SeekableGCSStream(storageClient, gcsStoragePath); SamInputResource samInputResource = SamInputResource.of(s); if (index != null) { samInputResource.index(index); } LOG.info("getReadsFromBAMFile - got input resources"); return samInputResource; }
synchronized private static SamReader fetchReader(String file, ValidationStringency stringency) { return threadLocalSAMReaders.get().computeIfAbsent( file, (f) -> SamReaderFactory.makeDefault().validationStringency(stringency).open(SamInputResource.of(f)) ); }
/** Creates a {@link SamInputResource} reading from the provided resource, with no index. */ public static SamInputResource of(final Path path) { // in the case of named pipes and other non-seekable paths there's a bug in the implementation of // java's GZIPInputStream which inappropriately uses .available() and then gets confused with the result // of 0. For reference see: // https://bugs.java.com/view_bug.do?bug_id=7036144 // https://github.com/samtools/htsjdk/pull/1077 // https://github.com/samtools/htsjdk/issues/898 // This still doesn't support the case where someone is creating a named pipe in a non-default // file system and then using it as input and passing a GZIPed into the other end of the pipe. // To work around this bug, we fall back to using a FileInputResource rather than a PathInputResource // when we encounter a non-regular file using the default NIO filesystem (file://) if (path.getFileSystem() == FileSystems.getDefault() && !Files.isRegularFile(path)) { return of(path.toFile()); } else { return new SamInputResource(new PathInputResource(path)); } }
/** Creates a {@link SamInputResource} reading from the provided resource, with no index. */ public static SamInputResource of(final URL url) { return new SamInputResource(new UrlInputResource(url)); }
final SamReader.PrimitiveSamReader primitiveSamReader; try { final InputResource data = resource.data(); final InputResource indexMaybe = resource.indexMaybe(); final boolean indexDefined = indexMaybe != null;
private void readAllAlignments(final SamReaderFactory readerFactory) { SamReader fromExternalProgram = readerFactory.open(SamInputResource.of(aligner.getInputStream())); SAMRecordIterator it = fromExternalProgram.iterator(); while (it.hasNext()) { SAMRecord r = it.next(); buffer.add(r); outstandingReads.decrementAndGet(); } log.info(String.format("Reader thread complete. %s reads in output buffer", buffer.size())); } /**