Search in sources :

Example 1 with SamInputResource

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;
}
Also used : SamReaderFactory(htsjdk.samtools.SamReaderFactory) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) SamInputResource(htsjdk.samtools.SamInputResource)

Example 2 with SamInputResource

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);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) SamInputResource(htsjdk.samtools.SamInputResource)

Example 3 with SamInputResource

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;
}
Also used : SamReader(htsjdk.samtools.SamReader) SamReaderFactory(htsjdk.samtools.SamReaderFactory) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) TabixKnownGeneFileReader(com.github.lindenb.jvarkit.util.ucsc.TabixKnownGeneFileReader) File(java.io.File) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) SamInputResource(htsjdk.samtools.SamInputResource)

Example 4 with SamInputResource

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);
    }
}
Also used : PrintStream(java.io.PrintStream) Program(com.github.lindenb.jvarkit.util.jcommander.Program) Parameter(com.beust.jcommander.Parameter) Logger(com.github.lindenb.jvarkit.util.log.Logger) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) Set(java.util.Set) SamReader(htsjdk.samtools.SamReader) Collectors(java.util.stream.Collectors) File(java.io.File) ParametersDelegate(com.beust.jcommander.ParametersDelegate) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Interval(htsjdk.samtools.util.Interval) List(java.util.List) SamInputResource(htsjdk.samtools.SamInputResource) IOUtils(com.github.lindenb.jvarkit.io.IOUtils) Launcher(com.github.lindenb.jvarkit.util.jcommander.Launcher) CloserUtil(htsjdk.samtools.util.CloserUtil) PrintStream(java.io.PrintStream) SamReader(htsjdk.samtools.SamReader) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) SamInputResource(htsjdk.samtools.SamInputResource) Interval(htsjdk.samtools.util.Interval)

Example 5 with SamInputResource

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);
}
Also used : SamReaderFactory(htsjdk.samtools.SamReaderFactory) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) SamInputResource(htsjdk.samtools.SamInputResource)

Aggregations

SamInputResource (htsjdk.samtools.SamInputResource)5 File (java.io.File)4 SamReaderFactory (htsjdk.samtools.SamReaderFactory)3 SamReader (htsjdk.samtools.SamReader)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Parameter (com.beust.jcommander.Parameter)1 ParametersDelegate (com.beust.jcommander.ParametersDelegate)1 IOUtils (com.github.lindenb.jvarkit.io.IOUtils)1 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)1 Program (com.github.lindenb.jvarkit.util.jcommander.Program)1 Logger (com.github.lindenb.jvarkit.util.log.Logger)1 TabixKnownGeneFileReader (com.github.lindenb.jvarkit.util.ucsc.TabixKnownGeneFileReader)1 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)1 IndexedFastaSequenceFile (htsjdk.samtools.reference.IndexedFastaSequenceFile)1 CloserUtil (htsjdk.samtools.util.CloserUtil)1 Interval (htsjdk.samtools.util.Interval)1 VCFFileReader (htsjdk.variant.vcf.VCFFileReader)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1