use of htsjdk.samtools.SamInputResource in project jvarkit by lindenb.
the class BamFile method reOpen.
@Override
public BamFile reOpen() throws IOException {
final String url = this.getSource();
final SamReaderFactory srf = SamReaderFactory.makeDefault();
srf.validationStringency(ValidationStringency.LENIENT);
final SamInputResource sir;
if (IOUtil.isUrl(url)) {
sir = SamInputResource.of(new URL(url));
if (!this.indexFile.isPresent())
throw new IOException("Boum");
sir.index(this.indexFile.get());
} else {
sir = SamInputResource.of(new File(url));
}
final BamFile bf = new BamFile(url, srf.open(sir), this.indexFile);
bf.delete_index_on_close = false;
return bf;
}
use of htsjdk.samtools.SamInputResource in project hmftools by hartwigmedical.
the class BamSlicerApplication method createCachingReader.
private static SamReader createCachingReader(@NotNull final File indexFile, @NotNull final URL bamUrl, @NotNull final CommandLine cmd, @NotNull final List<Chunk> sliceChunks) throws IOException {
final OkHttpClient httpClient = SlicerHttpClient.create(Integer.parseInt(cmd.getOptionValue(MAX_CONCURRENT_REQUESTS, MAX_CONCURRENT_REQUESTS_DEFAULT)));
final int maxBufferSize = readMaxBufferSize(cmd);
final SamInputResource bamResource = SamInputResource.of(new CachingSeekableHTTPStream(httpClient, bamUrl, sliceChunks, maxBufferSize)).index(indexFile);
return SamReaderFactory.makeDefault().open(bamResource);
}
use of htsjdk.samtools.SamInputResource in project jvarkit by lindenb.
the class TView method initialize.
public int initialize() throws IOException {
if (this.referenceFile != null) {
this.indexedFastaSequenceFile = new IndexedFastaSequenceFile(this.referenceFile);
}
if (this.samRecordFilter == null) {
this.samRecordFilter = SamFilterParser.ACCEPT_ALL;
}
final SamReaderFactory srf = SamReaderFactory.makeDefault().referenceSequence(this.referenceFile).validationStringency(ValidationStringency.LENIENT);
for (final SamInputResource sir : this.samInputResources) {
final SamReader samReader = srf.open(sir);
this.samReaders.add(samReader);
}
for (final File vcfFile : IOUtils.unrollFile(this.variantFiles)) {
final VcfSource vcfSource = new VcfSource();
LOG.debug("OPEN " + vcfFile);
vcfSource.vcfFile = vcfFile;
vcfSource.vcfFileReader = new VCFFileReader(vcfFile, true);
this.vcfReaders.add(vcfSource);
}
if (this.tabixKnownGene != null) {
this.tabixKnownGene = new TabixKnownGeneFileReader(this.knownGeneUri);
}
return 0;
}
use of htsjdk.samtools.SamInputResource in project jvarkit by lindenb.
the class TViewCmd method doWork.
@Override
public int doWork(List<String> args) {
PrintStream out = null;
try {
args = new ArrayList<>(IOUtils.unrollFiles(args));
if (args.isEmpty()) {
LOG.error("no BAM file defined");
return -1;
}
final List<SamInputResource> samInputResources = args.stream().map(S -> SamInputResource.of(S)).collect(Collectors.toList());
tview.setBamFiles(samInputResources);
if (tview.initialize() != 0) {
LOG.error("cannot initialize tview");
return -1;
}
final List<Interval> intervals = this.intervalStr.stream().map(S -> parseInterval(S)).collect(Collectors.toList());
if (!nodefaultinterval && intervals.isEmpty() && !args.isEmpty()) {
final SamReader samReader = super.createSamReaderFactory().open(SamInputResource.of(args.get(0)));
final SAMSequenceDictionary dict = samReader.getFileHeader() != null && samReader.getFileHeader().getSequenceDictionary() != null ? samReader.getFileHeader().getSequenceDictionary() : null;
samReader.close();
if (dict != null && !dict.isEmpty()) {
intervals.add(parseInterval(dict.getSequence(0).getSequenceName()));
}
}
out = super.openFileOrStdoutAsPrintStream(outputFile);
for (final Interval interval : intervals) {
tview.setInterval(interval);
tview.paint(out);
}
out.flush();
return 0;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(out);
CloserUtil.close(tview);
}
}
use of htsjdk.samtools.SamInputResource in project jvarkit by lindenb.
the class BamFile method newInstance.
public static BamFile newInstance(final URL url) throws IOException {
final File baiFile = File.createTempFile("tmp.", BAMIndex.BAMIndexSuffix);
Optional<File> savedBaiFile = Optional.empty();
baiFile.deleteOnExit();
for (int i = 0; i < 2; ++i) {
final String bamurl = url.toExternalForm();
final String baiurl = (i == 0 ? bamurl : bamurl.substring(0, bamurl.length() - BamFileIoUtils.BAM_FILE_EXTENSION.length())) + BAMIndex.BAMIndexSuffix;
InputStream in = null;
FileOutputStream out = null;
try {
LOG.info("trying " + baiurl);
in = new URL(baiurl).openStream();
out = new FileOutputStream(baiFile);
IOUtil.copyStream(in, out);
out.flush();
out.close();
in.close();
savedBaiFile = Optional.of(baiFile);
break;
} catch (final IOException err) {
baiFile.delete();
LOG.info("Cannot fetch " + baiurl + " : " + err.getMessage());
} finally {
CloserUtil.close(out);
CloserUtil.close(in);
}
}
if (!savedBaiFile.isPresent()) {
throw new IOException("cannot get a bam index file for " + url);
}
final SamReaderFactory srf = SamReaderFactory.makeDefault();
srf.validationStringency(ValidationStringency.LENIENT);
final SamInputResource sir = SamInputResource.of(url);
sir.index(savedBaiFile.get());
return new BamFile(url.toExternalForm(), srf.open(sir), savedBaiFile);
}
Aggregations