Search in sources :

Example 46 with CloseableIterator

use of htsjdk.samtools.util.CloseableIterator in project gridss by PapenfussLab.

the class SAMEvidenceSource method iterator.

public CloseableIterator<DirectedEvidence> iterator(final QueryInterval[] intervals) {
    SamReader reader = getReader();
    // expand query bounds as the alignment for a discordant read pair could fall before or after the breakend interval we are extracting
    SAMRecordIterator it = tryOpenReader(reader, QueryIntervalUtil.padIntervals(getContext().getDictionary(), intervals, getMaxConcordantFragmentSize() + 1));
    Iterator<DirectedEvidence> eit = asEvidence(it);
    eit = Iterators.filter(eit, e -> QueryIntervalUtil.overlaps(intervals, e.getBreakendSummary()));
    return new AutoClosingIterator<>(eit, reader, it);
}
Also used : TextCigarCodec(htsjdk.samtools.TextCigarCodec) Cigar(htsjdk.samtools.Cigar) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Iterables(com.google.common.collect.Iterables) SAMRecordUtil(au.edu.wehi.idsv.sam.SAMRecordUtil) BufferedIterator(au.edu.wehi.idsv.util.BufferedIterator) AsyncBufferedIterator(au.edu.wehi.idsv.util.AsyncBufferedIterator) Iterators(com.google.common.collect.Iterators) ComputeSamTags(gridss.ComputeSamTags) SoftClipsToSplitReads(gridss.SoftClipsToSplitReads) SortOrder(htsjdk.samtools.SAMFileHeader.SortOrder) GridssConfiguration(au.edu.wehi.idsv.configuration.GridssConfiguration) SAMTag(htsjdk.samtools.SAMTag) CollectGridssMetrics(gridss.analysis.CollectGridssMetrics) Lists(com.google.common.collect.Lists) AutoClosingIterator(au.edu.wehi.idsv.util.AutoClosingIterator) ImmutableList(com.google.common.collect.ImmutableList) FileHelper(au.edu.wehi.idsv.util.FileHelper) StructuralVariantReadMetrics(gridss.analysis.StructuralVariantReadMetrics) ChimericAlignment(au.edu.wehi.idsv.sam.ChimericAlignment) CommandLineProgramHelper(gridss.cmdline.CommandLineProgramHelper) PairOrientation(htsjdk.samtools.SamPairUtil.PairOrientation) Iterator(java.util.Iterator) IdsvSamFileMetrics(au.edu.wehi.idsv.metrics.IdsvSamFileMetrics) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) IOException(java.io.IOException) SoftClipConfiguration(au.edu.wehi.idsv.configuration.SoftClipConfiguration) OrderAssertingIterator(au.edu.wehi.idsv.validation.OrderAssertingIterator) SamReader(htsjdk.samtools.SamReader) MetricsFile(htsjdk.samtools.metrics.MetricsFile) Collectors(java.util.stream.Collectors) File(java.io.File) IntervalBed(au.edu.wehi.idsv.bed.IntervalBed) AutoClosingMergedIterator(au.edu.wehi.idsv.util.AutoClosingMergedIterator) SAMRecord(htsjdk.samtools.SAMRecord) CommandLineProgram(picard.cmdline.CommandLineProgram) List(java.util.List) ExtractSVReads(gridss.ExtractSVReads) Log(htsjdk.samtools.util.Log) QueryInterval(htsjdk.samtools.QueryInterval) ReferenceCommandLineProgram(gridss.cmdline.ReferenceCommandLineProgram) SAMFileUtil(au.edu.wehi.idsv.sam.SAMFileUtil) CigarUtil(au.edu.wehi.idsv.sam.CigarUtil) SamReaderFactory(htsjdk.samtools.SamReaderFactory) SamReader(htsjdk.samtools.SamReader) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) AutoClosingIterator(au.edu.wehi.idsv.util.AutoClosingIterator)

Example 47 with CloseableIterator

use of htsjdk.samtools.util.CloseableIterator in project gridss by PapenfussLab.

the class AssemblyEvidenceSource method getAllAssemblies_single_threaded.

@SuppressWarnings("unused")
private Iterator<SAMRecord> getAllAssemblies_single_threaded() {
    ProgressLogger progressLog = new ProgressLogger(log);
    List<Iterator<SAMRecord>> list = new ArrayList<>();
    for (BreakendDirection direction : BreakendDirection.values()) {
        CloseableIterator<DirectedEvidence> it = mergedIterator(source, false);
        Iterator<DirectedEvidence> throttledIt = throttled(it);
        ProgressLoggingDirectedEvidenceIterator<DirectedEvidence> loggedIt = new ProgressLoggingDirectedEvidenceIterator<>(getContext(), throttledIt, progressLog);
        Iterator<SAMRecord> evidenceIt = new PositionalAssembler(getContext(), this, new SequentialIdGenerator("asm"), loggedIt, direction);
        list.add(evidenceIt);
    }
    return Iterators.concat(list.iterator());
}
Also used : PositionalAssembler(au.edu.wehi.idsv.debruijn.positional.PositionalAssembler) ArrayList(java.util.ArrayList) ProgressLogger(htsjdk.samtools.util.ProgressLogger) SAMRecord(htsjdk.samtools.SAMRecord) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Iterator(java.util.Iterator)

Example 48 with CloseableIterator

use of htsjdk.samtools.util.CloseableIterator in project jvarkit by lindenb.

the class FindAVariation method scan.

private void scan(final BufferedReader in) throws IOException {
    String line;
    while ((line = in.readLine()) != null) {
        if (line.isEmpty() || line.startsWith("#"))
            continue;
        final File f = new File(line);
        if (!f.isFile())
            continue;
        if (!f.canRead())
            continue;
        if (!VCFUtils.isVcfFile(f))
            continue;
        VcfIterator iter = null;
        if (VCFUtils.isTribbleVcfFile(f)) {
            VCFFileReader r = null;
            try {
                r = new VCFFileReader(f, true);
                final VCFHeader header = r.getFileHeader();
                for (final Mutation m : convertFromVcfHeader(f, header)) {
                    final CloseableIterator<VariantContext> iter2 = r.query(m.chrom, m.pos, m.pos);
                    while (iter2.hasNext()) {
                        final VariantContext ctx = iter2.next();
                        if (this.onlySnp) {
                            if (ctx.getStart() != m.pos || ctx.getEnd() != m.pos)
                                continue;
                        }
                        report(f, header, ctx, m);
                    }
                    CloserUtil.close(iter2);
                }
            } catch (final htsjdk.tribble.TribbleException.InvalidHeader err) {
                LOG.warn(f + "\t" + err.getMessage());
            } catch (final Exception err) {
                LOG.severe("cannot read " + f, err);
            } finally {
                CloserUtil.close(r);
            }
        } else if (VCFUtils.isTabixVcfFile(f)) {
            TabixVcfFileReader r = null;
            try {
                r = new TabixVcfFileReader(f.getPath());
                final VCFHeader header = r.getHeader();
                for (final Mutation m : convertFromVcfHeader(f, header)) {
                    final Iterator<VariantContext> iter2 = r.iterator(m.chrom, m.pos, m.pos);
                    while (iter2.hasNext()) {
                        final VariantContext ctx = iter2.next();
                        if (this.onlySnp) {
                            if (ctx.getStart() != m.pos || ctx.getEnd() != m.pos)
                                continue;
                        }
                        report(f, header, ctx, m);
                    }
                    CloserUtil.close(iter2);
                }
            } catch (final htsjdk.tribble.TribbleException.InvalidHeader err) {
                LOG.warn(f + "\t" + err.getMessage());
            } catch (final Exception err) {
                LOG.severe("cannot read " + f, err);
            } finally {
                CloserUtil.close(r);
            }
        } else if (!this.indexedOnly) {
            try {
                iter = VCFUtils.createVcfIteratorFromFile(f);
                final VCFHeader header = iter.getHeader();
                final Set<Mutation> mutlist = convertFromVcfHeader(f, iter.getHeader());
                while (iter.hasNext()) {
                    final VariantContext ctx = iter.next();
                    final Mutation m = new Mutation(ctx.getContig(), ctx.getStart());
                    for (final Mutation m2 : mutlist) {
                        if (m.equals(m2)) {
                            if (this.onlySnp) {
                                if (ctx.getStart() != m2.pos || ctx.getEnd() != m2.pos)
                                    continue;
                            }
                            report(f, header, ctx, m2);
                            break;
                        }
                    }
                }
            } catch (final htsjdk.tribble.TribbleException.InvalidHeader err) {
                LOG.warn(f + "\t" + err.getMessage());
            } catch (final Exception err) {
                LOG.severe("Error in " + f, err);
            } finally {
                CloserUtil.close(iter);
            }
        }
    }
}
Also used : VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) IOException(java.io.IOException) VcfIterator(com.github.lindenb.jvarkit.util.vcf.VcfIterator) TabixVcfFileReader(com.github.lindenb.jvarkit.util.vcf.TabixVcfFileReader) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Iterator(java.util.Iterator) VcfIterator(com.github.lindenb.jvarkit.util.vcf.VcfIterator) VCFHeader(htsjdk.variant.vcf.VCFHeader) File(java.io.File)

Example 49 with CloseableIterator

use of htsjdk.samtools.util.CloseableIterator in project jvarkit by lindenb.

the class VCFComposite method doWork.

@Override
public int doWork(final List<String> args) {
    PrintWriter out = null;
    try {
        out = super.openFileOrStdoutAsPrintWriter(this.outputFile);
        if (listModels) {
            for (final Type t : Type.values()) {
                out.println(t.name());
                out.println("\t" + t.getDescription());
            }
            out.flush();
            return 0;
        }
        this.pedigree = Pedigree.newParser().parse(pedigreeFile);
        if (this.pedigree.getAffected().isEmpty()) {
            LOG.error("No Affected sample in " + this.pedigreeFile);
            return -1;
        }
        if (this.pedigree.getUnaffected().isEmpty()) {
            LOG.error("No Unaffected sample in " + this.pedigreeFile);
            return -1;
        }
        final DiseaseModel model = this.createModel();
        final String inputName = super.oneFileOrNull(args);
        final LineIterator r = (inputName == null ? IOUtils.openStreamForLineIterator(stdin()) : IOUtils.openURIForLineIterator(inputName));
        final VCFCodec codec = new VCFCodec();
        final VCFHeader header = (VCFHeader) codec.readActualHeader(r);
        final AnnPredictionParser annParser = new AnnPredictionParserFactory(header).get();
        final VepPredictionParser vepParser = new VepPredictionParserFactory(header).get();
        // final VCFHeader h2=new VCFHeader(header.getMetaDataInInputOrder(),header.getSampleNamesInOrder());
        // h2.addMetaDataLine(new VCFInfoHeaderLine(this.TAG,1,VCFHeaderLineType.String,"Values from bigwig file: "+BIGWIG));
        SortingCollection<GeneAndVariant> sorting = null;
        String prevContig = null;
        for (; ; ) {
            String line;
            final VariantContext ctx;
            if (r.hasNext()) {
                line = r.next();
                ctx = codec.decode(line);
            } else {
                line = null;
                ctx = null;
            }
            if (ctx == null || !ctx.getContig().equals(prevContig)) {
                if (sorting != null) {
                    LOG.debug("Dump contig " + prevContig);
                    sorting.doneAdding();
                    CloseableIterator<GeneAndVariant> iter2 = sorting.iterator();
                    EqualRangeIterator<GeneAndVariant> eqiter = new EqualRangeIterator<>(iter2, (A, B) -> A.gene.compareTo(B.gene));
                    while (eqiter.hasNext()) {
                        final List<GeneAndVariant> variants = eqiter.next();
                        model.scan(variants.get(0).gene, variants.stream().map(L -> codec.decode(L.ctxLine)).collect(Collectors.toList()), out);
                    }
                    eqiter.close();
                    iter2.close();
                    sorting.cleanup();
                }
                sorting = null;
                if (ctx == null)
                    break;
                prevContig = ctx.getContig();
            }
            if (!ctx.isVariant())
                continue;
            if (!acceptFiltered && ctx.isFiltered())
                continue;
            if (!acceptID && ctx.hasID())
                continue;
            if (!model.accept(ctx))
                continue;
            final Set<String> geneKeys = new HashSet<>();
            for (final AnnPredictionParser.AnnPrediction pred : annParser.getPredictions(ctx)) {
                geneKeys.addAll(pred.getGeneKeys().stream().map(S -> ctx.getContig() + "_" + S).collect(Collectors.toSet()));
            }
            for (final VepPredictionParser.VepPrediction pred : vepParser.getPredictions(ctx)) {
                geneKeys.addAll(pred.getGeneKeys().stream().map(S -> ctx.getContig() + "_" + S).collect(Collectors.toSet()));
            }
            if (sorting == null) {
                sorting = SortingCollection.newInstance(GeneAndVariant.class, new GeneAndVariantCodec(), (A, B) -> {
                    int i = A.gene.compareTo(B.gene);
                    if (i != 0)
                        return i;
                    return A.ctxLine.compareTo(B.ctxLine);
                }, this.writingSortingCollection.getMaxRecordsInRam(), this.writingSortingCollection.getTmpPaths());
                sorting.setDestructiveIteration(true);
            }
            for (final String gk : geneKeys) {
                final GeneAndVariant gav = new GeneAndVariant();
                gav.gene = gk;
                gav.ctxLine = line;
                sorting.add(gav);
            }
        }
        out.flush();
        out.close();
        out = null;
        return 0;
    } catch (Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(out);
    }
}
Also used : AnnPredictionParser(com.github.lindenb.jvarkit.util.vcf.predictions.AnnPredictionParser) Genotype(htsjdk.variant.variantcontext.Genotype) DataInputStream(java.io.DataInputStream) CloseableIterator(htsjdk.samtools.util.CloseableIterator) LineIterator(htsjdk.tribble.readers.LineIterator) Program(com.github.lindenb.jvarkit.util.jcommander.Program) Parameter(com.beust.jcommander.Parameter) VCFHeader(htsjdk.variant.vcf.VCFHeader) AnnPredictionParser(com.github.lindenb.jvarkit.util.vcf.predictions.AnnPredictionParser) ParametersDelegate(com.beust.jcommander.ParametersDelegate) HashSet(java.util.HashSet) ContigPosRef(com.github.lindenb.jvarkit.util.vcf.ContigPosRef) DataOutputStream(java.io.DataOutputStream) AbstractDataCodec(com.github.lindenb.jvarkit.util.picard.AbstractDataCodec) Pedigree(com.github.lindenb.jvarkit.util.Pedigree) IOUtils(com.github.lindenb.jvarkit.io.IOUtils) Launcher(com.github.lindenb.jvarkit.util.jcommander.Launcher) VepPredictionParser(com.github.lindenb.jvarkit.util.vcf.predictions.VepPredictionParser) VCFCodec(htsjdk.variant.vcf.VCFCodec) VepPredictionParserFactory(com.github.lindenb.jvarkit.util.vcf.predictions.VepPredictionParserFactory) CloserUtil(htsjdk.samtools.util.CloserUtil) PrintWriter(java.io.PrintWriter) SortingCollection(htsjdk.samtools.util.SortingCollection) Predicate(java.util.function.Predicate) Logger(com.github.lindenb.jvarkit.util.log.Logger) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) EqualRangeIterator(com.github.lindenb.jvarkit.util.iterator.EqualRangeIterator) AnnPredictionParserFactory(com.github.lindenb.jvarkit.util.vcf.predictions.AnnPredictionParserFactory) VariantContext(htsjdk.variant.variantcontext.VariantContext) VariantContext(htsjdk.variant.variantcontext.VariantContext) LineIterator(htsjdk.tribble.readers.LineIterator) EqualRangeIterator(com.github.lindenb.jvarkit.util.iterator.EqualRangeIterator) AnnPredictionParserFactory(com.github.lindenb.jvarkit.util.vcf.predictions.AnnPredictionParserFactory) VCFHeader(htsjdk.variant.vcf.VCFHeader) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet) VCFCodec(htsjdk.variant.vcf.VCFCodec) IOException(java.io.IOException) VepPredictionParser(com.github.lindenb.jvarkit.util.vcf.predictions.VepPredictionParser) VepPredictionParserFactory(com.github.lindenb.jvarkit.util.vcf.predictions.VepPredictionParserFactory)

Example 50 with CloseableIterator

use of htsjdk.samtools.util.CloseableIterator in project jvarkit by lindenb.

the class SortVcfOnInfo method doWork.

@Override
public int doWork(final List<String> args) {
    CloseableIterator<VcfLine> iter = null;
    VariantContextWriter w = null;
    SortingCollection<VcfLine> sorted = null;
    LineIterator r = null;
    try {
        if (args.isEmpty()) {
            LOG.info("reading from stdin");
            r = IOUtils.openStreamForLineIterator(stdin());
        } else if (args.size() == 1) {
            String filename = args.get(0);
            LOG.info("Reading " + filename);
            r = IOUtils.openURIForLineIterator(filename);
        } else {
            LOG.error("Illegal number of arguments.");
            return -1;
        }
        final VCFUtils.CodecAndHeader ch = VCFUtils.parseHeader(r);
        VCFHeader header = ch.header;
        this.codec = ch.codec;
        this.infoDecl = header.getInfoHeaderLine(this.infoField);
        if (this.infoDecl == null) {
            final StringBuilder msg = new StringBuilder("VCF doesn't contain the INFO field :" + infoField + ". Available:");
            for (VCFInfoHeaderLine vil : header.getInfoHeaderLines()) msg.append(" ").append(vil.getID());
            LOG.error(msg.toString());
            return -1;
        }
        final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header);
        header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "CmdLine", String.valueOf(getProgramCommandLine())));
        header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "Version", String.valueOf(getVersion())));
        header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "HtsJdkVersion", HtsjdkVersion.getVersion()));
        header.addMetaDataLine(new VCFHeaderLine(getClass().getSimpleName() + "HtsJdkHome", HtsjdkVersion.getHome()));
        w = super.openVariantContextWriter(this.outputFile);
        w.writeHeader(header);
        sorted = SortingCollection.newInstance(VcfLine.class, new VariantCodec(), (V1, V2) -> V1.compareTo(V2), this.writingSortingCollection.getMaxRecordsInRam(), this.writingSortingCollection.getTmpPaths());
        sorted.setDestructiveIteration(true);
        while (r.hasNext()) {
            final VcfLine vc = new VcfLine(r.next());
            progress.watch(vc.getContext());
            sorted.add(vc);
        }
        CloserUtil.close(r);
        r = null;
        sorted.doneAdding();
        progress.finish();
        LOG.info("now writing...");
        iter = sorted.iterator();
        while (iter.hasNext()) {
            w.add(iter.next().getContext());
        }
        iter.close();
        iter = null;
        w.close();
        w = null;
        return 0;
    } catch (final Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(r);
        CloserUtil.close(iter);
        try {
            if (sorted != null)
                sorted.cleanup();
        } catch (Exception err) {
        }
        CloserUtil.close(w);
    }
}
Also used : VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) DataInputStream(java.io.DataInputStream) VCFUtils(com.github.lindenb.jvarkit.util.vcf.VCFUtils) CloseableIterator(htsjdk.samtools.util.CloseableIterator) Arrays(java.util.Arrays) LineIterator(htsjdk.tribble.readers.LineIterator) Program(com.github.lindenb.jvarkit.util.jcommander.Program) Parameter(com.beust.jcommander.Parameter) VCFHeader(htsjdk.variant.vcf.VCFHeader) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) ParametersDelegate(com.beust.jcommander.ParametersDelegate) BigDecimal(java.math.BigDecimal) DataOutputStream(java.io.DataOutputStream) AbstractDataCodec(com.github.lindenb.jvarkit.util.picard.AbstractDataCodec) IOUtils(com.github.lindenb.jvarkit.io.IOUtils) Launcher(com.github.lindenb.jvarkit.util.jcommander.Launcher) BigInteger(java.math.BigInteger) CloserUtil(htsjdk.samtools.util.CloserUtil) SortingCollection(htsjdk.samtools.util.SortingCollection) AbstractVCFCodec(htsjdk.variant.vcf.AbstractVCFCodec) Logger(com.github.lindenb.jvarkit.util.log.Logger) IOException(java.io.IOException) File(java.io.File) List(java.util.List) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) VariantContext(htsjdk.variant.variantcontext.VariantContext) HtsjdkVersion(com.github.lindenb.jvarkit.util.htsjdk.HtsjdkVersion) VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) VCFUtils(com.github.lindenb.jvarkit.util.vcf.VCFUtils) LineIterator(htsjdk.tribble.readers.LineIterator) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) IOException(java.io.IOException) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFHeader(htsjdk.variant.vcf.VCFHeader)

Aggregations

CloseableIterator (htsjdk.samtools.util.CloseableIterator)103 List (java.util.List)86 Logger (com.github.lindenb.jvarkit.util.log.Logger)85 Parameter (com.beust.jcommander.Parameter)82 Program (com.github.lindenb.jvarkit.util.jcommander.Program)78 ArrayList (java.util.ArrayList)73 Collectors (java.util.stream.Collectors)71 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)69 Path (java.nio.file.Path)69 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)66 CloserUtil (htsjdk.samtools.util.CloserUtil)64 Set (java.util.Set)64 VCFHeader (htsjdk.variant.vcf.VCFHeader)59 VariantContext (htsjdk.variant.variantcontext.VariantContext)54 IOException (java.io.IOException)53 SequenceDictionaryUtils (com.github.lindenb.jvarkit.util.bio.SequenceDictionaryUtils)51 SAMRecord (htsjdk.samtools.SAMRecord)51 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)51 IOUtils (com.github.lindenb.jvarkit.io.IOUtils)50 StringUtils (com.github.lindenb.jvarkit.lang.StringUtils)49