Search in sources :

Example 16 with VCFReader

use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.

the class VcfSkatSlidingWindow method doWork.

@Override
public int doWork(final List<String> args) {
    if (this.nJobs < 1) {
        this.nJobs = Math.max(1, Runtime.getRuntime().availableProcessors());
        LOG.info("setting njobs to " + this.nJobs);
    }
    VCFIterator r = null;
    try {
        final VCFHeader header;
        final SAMSequenceDictionary dict;
        final File vcfFile = new File(oneAndOnlyOneFile(args));
        try (final VCFReader vr = VCFReaderFactory.makeDefault().open(vcfFile, true)) {
            header = vr.getHeader();
            dict = header.getSequenceDictionary();
        }
        if (dict == null || dict.isEmpty()) {
            throw new JvarkitException.VcfDictionaryMissing(vcfFile);
        }
        if (!this.limit_contigs.isEmpty()) {
            if (this.limit_contigs.stream().anyMatch(C -> dict.getSequence(C) == null)) {
                LOG.error("user contig missing in vcf dictionary.");
                return -1;
            }
        }
        final Pedigree pedigree;
        if (this.pedigreeFile != null) {
            pedigree = new Pedigree.Parser().parse(this.pedigreeFile);
        } else {
            pedigree = new Pedigree.Parser().parse(header);
        }
        final Set<Pedigree.Person> samples = new HashSet<>(pedigree.getPersons());
        samples.removeIf(I -> !(I.isAffected() || I.isUnaffected()) || !header.getSampleNamesInOrder().contains(I.getId()));
        this.writer = super.openFileOrStdoutAsPrintWriter(this.outputFile);
        final Consumer<SkatCallerResult> writeResult = (R) -> {
            synchronized (this.writer) {
                this.writer.println(R.toString());
            }
        };
        for (final SAMSequenceRecord ssr : dict.getSequences()) {
            if (!this.limit_contigs.isEmpty() && !this.limit_contigs.contains(ssr.getSequenceName())) {
                LOG.warning("skipping contig " + ssr.getSequenceName());
                continue;
            }
            LOG.info("contig " + ssr.getSequenceName());
            final ExecutorService executorService = new ThreadPoolExecutor(this.nJobs, this.nJobs, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(this.nJobs));
            final List<Future<Integer>> results = new ArrayList<>(this.nJobs);
            for (int i = 0; i < this.nJobs; i++) {
                final int windowLen = Math.max(1, ssr.getSequenceLength() / this.nJobs);
                final SkatWorker caller = new SkatWorker(vcfFile, new Interval(ssr.getSequenceName(), i * windowLen, Math.min(ssr.getSequenceLength(), (i + 1) * windowLen)), samples, this.skat.build(), writeResult);
                results.add(executorService.submit(caller));
            }
            executorService.shutdown();
            executorService.awaitTermination(365, TimeUnit.DAYS);
            for (final Future<Integer> fl : results) {
                try {
                    if (fl.get() != 0) {
                        LOG.error("An error occured");
                        return -1;
                    }
                } catch (final Exception err) {
                    LOG.error(err);
                    return -1;
                }
            }
        }
        this.writer.flush();
        this.writer.close();
        this.writer = null;
        return 0;
    } catch (final Throwable err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(r);
        CloserUtil.close(this.writer);
    }
}
Also used : CloseableIterator(htsjdk.samtools.util.CloseableIterator) Program(com.github.lindenb.jvarkit.util.jcommander.Program) VCFIterator(htsjdk.variant.vcf.VCFIterator) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Parameter(com.beust.jcommander.Parameter) VCFHeader(htsjdk.variant.vcf.VCFHeader) Callable(java.util.concurrent.Callable) ParametersDelegate(com.beust.jcommander.ParametersDelegate) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Interval(htsjdk.samtools.util.Interval) Future(java.util.concurrent.Future) Pedigree(com.github.lindenb.jvarkit.util.Pedigree) Launcher(com.github.lindenb.jvarkit.util.jcommander.Launcher) VCFReaderFactory(com.github.lindenb.jvarkit.variant.vcf.VCFReaderFactory) ExecutorService(java.util.concurrent.ExecutorService) CloserUtil(htsjdk.samtools.util.CloserUtil) PrintWriter(java.io.PrintWriter) Logger(com.github.lindenb.jvarkit.util.log.Logger) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) VCFReader(htsjdk.variant.vcf.VCFReader) Set(java.util.Set) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) VariantContext(htsjdk.variant.variantcontext.VariantContext) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) ArrayList(java.util.ArrayList) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) VCFReader(htsjdk.variant.vcf.VCFReader) VCFHeader(htsjdk.variant.vcf.VCFHeader) VCFIterator(htsjdk.variant.vcf.VCFIterator) HashSet(java.util.HashSet) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) Pedigree(com.github.lindenb.jvarkit.util.Pedigree) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) File(java.io.File) Interval(htsjdk.samtools.util.Interval)

Example 17 with VCFReader

use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.

the class VcfToSvg method doWork.

@Override
public int doWork(final List<String> args) {
    VCFReader r = null;
    OutputStream outputStream = null;
    XMLStreamWriter w = null;
    PrintWriter manifestW = null;
    ArchiveFactory archiveFactory = null;
    try {
        r = VCFReaderFactory.makeDefault().open(Paths.get(oneAndOnlyOneFile(args)), true);
        final VCFHeader header = r.getHeader();
        final List<String> samples = new ArrayList<>(header.getSampleNamesInOrder());
        final SAMSequenceDictionary dict = SequenceDictionaryUtils.extractRequired(header);
        intervalListProvider.dictionary(dict);
        /* read gtf if any */
        final IntervalTreeMap<Gene> geneMap = new IntervalTreeMap<>();
        if (this.gtfPath != null) {
            try (GtfReader gtfReader = new GtfReader(this.gtfPath)) {
                gtfReader.setContigNameConverter(ContigNameConverter.fromOneDictionary(dict));
                gtfReader.getAllGenes().stream().filter(G -> !this.removeNonCoding || G.getTranscripts().stream().anyMatch(T -> T.isCoding())).forEach(G -> geneMap.put(new Interval(G), G));
            }
        }
        archiveFactory = ArchiveFactory.open(this.outputPath);
        if (manifestFile != null) {
            manifestW = IOUtils.openPathForPrintWriter(this.manifestFile);
        } else {
            manifestW = new PrintWriter(new NullOuputStream());
        }
        final Pedigree pedigree;
        if (this.pedPath == null) {
            pedigree = PedigreeParser.empty();
        } else {
            pedigree = new PedigreeParser().parse(this.pedPath);
        }
        final Path tmpSvg = Files.createTempFile("vcf.", ".svg");
        final XMLOutputFactory xof = XMLOutputFactory.newInstance();
        for (final Locatable interval : intervalListProvider.dictionary(dict).stream().collect(Collectors.toList())) {
            final List<VariantContext> variants = r.query(interval).stream().filter(V -> this.variantFILTEREDOpacity > 0 || !V.isFiltered()).filter(V -> this.variantIndelOpacity > 0 || !V.isIndel()).collect(Collectors.toCollection(ArrayList::new));
            if (variants.isEmpty())
                continue;
            final List<Transcript> transcripts = geneMap.getOverlapping(interval).stream().flatMap(G -> G.getTranscripts().stream()).filter(T -> !this.removeNonCoding || T.isCoding()).collect(Collectors.toList());
            variants.removeIf(V -> this.gtfPath != null && this.variantsInExonOnly && transcripts.stream().flatMap(T -> T.getExons().stream()).noneMatch(EX -> EX.overlaps(V)));
            if (variants.isEmpty())
                continue;
            final String geneId = transcripts.stream().map(T -> T.getGene().getId()).collect(Collectors.toSet()).stream().collect(HtsCollectors.oneAndOnlyOne()).orElse(null);
            final String geneName = transcripts.stream().map(T -> T.getGene().getGeneName()).collect(Collectors.toSet()).stream().collect(HtsCollectors.oneAndOnlyOne()).orElse(null);
            outputStream = IOUtils.openPathForWriting(tmpSvg);
            w = xof.createXMLStreamWriter(outputStream);
            double featureHeight = 10;
            double TRANSCRIPT_HEIGHT = featureHeight;
            final int all_genotypes_width = variants.size() * this.genotype_width;
            final int drawinAreaWidth = Math.max(all_genotypes_width, 1000);
            final int interline_weight = 6;
            final int margin_top = 10;
            final int margin_bottom = 10;
            final int margin_right = 100;
            final int margin_left = 100;
            w.writeStartDocument("UTF-8", "1.0");
            w.writeStartElement("svg");
            w.writeDefaultNamespace(SVG.NS);
            w.writeNamespace("xlink", XLINK.NS);
            w.writeAttribute("version", "1.1");
            w.writeAttribute("width", String.valueOf(margin_right + margin_right + drawinAreaWidth));
            w.writeAttribute("height", String.valueOf(margin_top + margin_bottom + transcripts.size() * TRANSCRIPT_HEIGHT + interline_weight * featureHeight + samples.size() * this.genotype_width));
            title(w, interval.getContig() + ":" + interval.getStart() + "-" + interval.getEnd());
            w.writeStartElement("desc");
            w.writeCharacters("generated with " + getProgramName() + "\n" + "Author: Pierre Lindenbaum PhD. @yokofakun .");
            w.writeEndElement();
            // defs
            w.writeStartElement("defs");
            // genotypes
            w.writeStartElement("g");
            // 
            w.writeAttribute("id", "g_" + GenotypeType.HOM_REF);
            w.writeEmptyElement("rect");
            w.writeAttribute("style", "fill:lime;stroke;none;");
            w.writeAttribute("x", "0");
            w.writeAttribute("y", "0");
            w.writeAttribute("width", String.valueOf(this.genotype_width));
            w.writeAttribute("height", String.valueOf(this.genotype_width));
            w.writeEndElement();
            w.writeStartElement("g");
            // 
            w.writeAttribute("id", "g_" + GenotypeType.NO_CALL);
            w.writeEmptyElement("rect");
            w.writeAttribute("style", "fill:silver;stroke;gray;");
            w.writeAttribute("x", "0");
            w.writeAttribute("y", "0");
            w.writeAttribute("width", String.valueOf(this.genotype_width));
            w.writeAttribute("height", String.valueOf(this.genotype_width));
            w.writeEndElement();
            w.writeStartElement("g");
            // 
            w.writeAttribute("id", "g_" + GenotypeType.HOM_VAR);
            w.writeEmptyElement("rect");
            w.writeAttribute("style", "fill:crimson;stroke;none;");
            w.writeAttribute("x", "0");
            w.writeAttribute("y", "0");
            w.writeAttribute("width", String.valueOf(this.genotype_width));
            w.writeAttribute("height", String.valueOf(this.genotype_width));
            w.writeEndElement();
            w.writeStartElement("g");
            // 
            w.writeAttribute("id", "g_" + GenotypeType.MIXED);
            w.writeEmptyElement("rect");
            w.writeAttribute("style", "fill:pink;stroke;none;");
            w.writeAttribute("x", "0");
            w.writeAttribute("y", "0");
            w.writeAttribute("width", String.valueOf(this.genotype_width));
            w.writeAttribute("height", String.valueOf(this.genotype_width));
            w.writeEndElement();
            w.writeStartElement("g");
            // 
            w.writeAttribute("id", "g_" + GenotypeType.UNAVAILABLE);
            w.writeEmptyElement("rect");
            w.writeAttribute("style", "fill:gray;stroke;none;");
            w.writeAttribute("x", "0");
            w.writeAttribute("y", "0");
            w.writeAttribute("width", String.valueOf(this.genotype_width));
            w.writeAttribute("height", String.valueOf(this.genotype_width));
            w.writeEndElement();
            w.writeStartElement("g");
            // 
            w.writeAttribute("id", "g_" + GenotypeType.HET);
            w.writeEmptyElement("rect");
            w.writeAttribute("style", "fill:lime;stroke;black;");
            w.writeAttribute("x", "0");
            w.writeAttribute("y", "0");
            w.writeAttribute("width", String.valueOf(genotype_width));
            w.writeAttribute("height", String.valueOf(genotype_width));
            w.writeEmptyElement("polygon");
            w.writeAttribute("style", "fill:crimson;stroke;black;");
            w.writeAttribute("points", "0,0 " + genotype_width + ",0 0," + genotype_width + " 0,0");
            w.writeEndElement();
            // strand
            w.writeEmptyElement("polyline");
            w.writeAttribute("id", "strandF");
            w.writeAttribute("points", "-5,-5 0,0 -5,5");
            w.writeEmptyElement("polyline");
            w.writeAttribute("id", "strandR");
            w.writeAttribute("points", "5,-5 0,0 5,5");
            // gradients
            w.writeStartElement("linearGradient");
            w.writeAttribute("id", "grad01");
            w.writeAttribute("x1", "50%");
            w.writeAttribute("x2", "50%");
            w.writeAttribute("y1", "0%");
            w.writeAttribute("y2", "100%");
            w.writeEmptyElement("stop");
            w.writeAttribute("offset", "0%");
            w.writeAttribute("style", "stop-color:black;stop-opacity:1;");
            w.writeEmptyElement("stop");
            w.writeAttribute("offset", "50%");
            w.writeAttribute("style", "stop-color:white;stop-opacity:1;");
            w.writeEmptyElement("stop");
            w.writeAttribute("offset", "100%");
            w.writeAttribute("style", "stop-color:black;stop-opacity:1;");
            w.writeEndElement();
            // defs
            w.writeEndElement();
            w.writeStartElement("style");
            w.writeCharacters("svg {fill:none; stroke:black;}\n" + "text {fill:black;stroke:none;font-size:" + (featureHeight / 1.5) + "px;}\n" + ".ruler-label { stroke:red;}\n" + ".frame { stroke:black;fill:none;}\n" + ".kgexon {fill:url(#grad01);stroke:black;}\n" + ".gcpercent {fill:url(#grad02);stroke:black;}" + ".coverage {fill:url(#grad03);stroke:black;}" + ".kgcds {fill:yellow;stroke:black;opacity:0.7;}\n" + ".variant{stroke:none;fill:red;opacity:0.2;}\n" + ".xaxis{stroke:gray;fill:none;opacity:0.2;}\n" + ".postick{font-size:9px;stroke:black;stroke-width:1;}");
            // style
            w.writeEndElement();
            final IntFunction<Integer> trim = t -> Math.max(interval.getStart(), Math.min(interval.getEnd(), t));
            final IntFunction<Double> baseToPixel = t -> margin_left + drawinAreaWidth * (t - (double) interval.getStart()) / ((double) interval.getLengthOnReference());
            final IntFunction<Double> variantIndexToPixel = idx -> {
                final double variant_width = drawinAreaWidth / (double) variants.size();
                final double midx = variant_width * idx + variant_width / 2.0;
                return margin_left + midx - genotype_width / 2.0;
            };
            final Function<VariantContext, String> variantTitle = V -> (V.getContig().startsWith("chr") ? V.getContig().substring(3) : V.getContig()) + ":" + V.getStart() + " " + V.getReference().getDisplayString();
            /**
             * title
             */
            double y = 0;
            w.writeStartElement("text");
            w.writeAttribute("x", "0");
            w.writeAttribute("y", String.valueOf(featureHeight));
            w.writeCharacters(interval.toString());
            w.writeEndElement();
            y += featureHeight;
            for (final Transcript g : transcripts) {
                int cdsHeigh = 5;
                double exonHeight = TRANSCRIPT_HEIGHT - 5;
                double midY = TRANSCRIPT_HEIGHT / 2;
                w.writeStartElement("g");
                w.writeAttribute("transform", "translate(0," + y + ")");
                title(w, g.getId());
                w.writeStartElement("text");
                w.writeAttribute("x", String.valueOf(margin_left - 10));
                w.writeAttribute("y", String.valueOf(featureHeight));
                w.writeAttribute("style", "text-anchor:end;");
                w.writeCharacters(g.getId());
                w.writeEndElement();
                /* transcript line */
                w.writeEmptyElement("line");
                w.writeAttribute("class", "kgtr");
                w.writeAttribute("x1", String.valueOf(baseToPixel.apply(trim.apply(g.getTxStart()))));
                w.writeAttribute("y1", String.valueOf(midY));
                w.writeAttribute("x2", String.valueOf(baseToPixel.apply(trim.apply(g.getTxEnd()))));
                w.writeAttribute("y2", String.valueOf(midY));
                /* strand symbols */
                for (double pixX = 0; pixX < drawinAreaWidth; pixX += 30) {
                    double pos0 = interval.getStart() + (pixX / (double) drawinAreaWidth) * interval.getLengthOnReference();
                    if (pos0 + 1 < g.getTxStart())
                        continue;
                    if (pos0 > g.getTxEnd())
                        break;
                    w.writeEmptyElement("use");
                    w.writeAttribute("class", "kgstrand");
                    w.writeAttribute("xlink", XLINK.NS, "href", "#strand" + (g.isPositiveStrand() ? "F" : "R"));
                    w.writeAttribute("x", String.valueOf(margin_left + pixX));
                    w.writeAttribute("y", String.valueOf(midY));
                }
                /* exons */
                for (final Exon exon : g.getExons()) {
                    if (exon.getStart() + 1 >= interval.getEnd())
                        continue;
                    if (exon.getEnd() <= interval.getStart())
                        continue;
                    w.writeStartElement("rect");
                    w.writeAttribute("class", "kgexon");
                    w.writeAttribute("x", String.valueOf(baseToPixel.apply(trim.apply(exon.getStart()))));
                    w.writeAttribute("y", String.valueOf(midY - exonHeight / 2));
                    w.writeAttribute("width", String.valueOf(baseToPixel.apply(trim.apply(exon.getEnd())) - baseToPixel.apply((trim.apply(exon.getStart())))));
                    w.writeAttribute("height", String.valueOf(exonHeight));
                    title(w, exon.getName());
                    w.writeEndElement();
                }
                /* coding line */
                if (!g.isNonCoding() && g.hasCodonStartDefined() && g.hasCodonStopDefined()) {
                    final double codonx1 = baseToPixel.apply(trim.apply(g.getLeftmostCodon().get().getStart()));
                    final double codonx2 = baseToPixel.apply(trim.apply(g.getRightmostCodon().get().getEnd()));
                    w.writeEmptyElement("rect");
                    w.writeAttribute("class", "kgcds");
                    w.writeAttribute("x", String.valueOf(codonx1));
                    w.writeAttribute("y", String.valueOf(midY - cdsHeigh / 4.0));
                    w.writeAttribute("width", String.valueOf(baseToPixel.apply((int) (codonx2 - codonx1))));
                    w.writeAttribute("height", String.valueOf(cdsHeigh / 2.0));
                }
                // String label=String.format("%15s", g.getName());
                // w.writeEmptyElement("path");
                // double fontHeight=Math.min(10,0.8*TRANSCRIPT_HEIGHT);
                // w.writeAttribute("d",this.hershey.svgPath(label,-insets.left,midY-fontHeight/2,insets.left*0.9,fontHeight));
                w.writeEndElement();
                w.writeCharacters("\n");
                y += featureHeight;
            }
            /* draw lines to variants */
            for (int vidx = 0; vidx < variants.size(); ++vidx) {
                final VariantContext vc = variants.get(vidx);
                double x1 = baseToPixel.apply(vc.getStart());
                double x2 = baseToPixel.apply(vc.getEnd());
                final double y2 = y + featureHeight * interline_weight;
                w.writeStartElement("polygon");
                w.writeAttribute("style", "fill:" + (vidx % 2 == 0 ? "ghostwhite" : "lavender") + ";stroke:black;opacity:0.6;stroke-width:0.5;");
                w.writeAttribute("points", "" + x1 + "," + (y - featureHeight / 2.0) + " " + x2 + "," + (y - featureHeight / 2.0) + " " + variantIndexToPixel.apply(vidx) + "," + y2 + " " + (variantIndexToPixel.apply(vidx) + this.genotype_width) + "," + y2);
                title(w, variantTitle.apply(vc));
                w.writeEndElement();
            }
            for (int vidx = 0; vidx < variants.size(); ++vidx) {
                final VariantContext vc = variants.get(vidx);
                final double y2 = y + featureHeight * interline_weight;
                w.writeStartElement("text");
                w.writeAttribute("transform", "translate(" + (String.valueOf(variantIndexToPixel.apply(vidx) + genotype_width / 2.0)) + "," + String.valueOf(y2 - 5) + ") " + "rotate(-45)");
                w.writeAttribute("x", "0");
                w.writeAttribute("y", "0");
                w.writeAttribute("class", "postick");
                w.writeCharacters(variantTitle.apply(vc));
                w.writeEndElement();
                w.writeCharacters("\n");
            }
            y += featureHeight * interline_weight;
            w.writeStartElement("g");
            /* step 0: affected, 1: unaffected, 2: others */
            for (int step = 0; step < 3; ++step) {
                for (final String sample : samples) {
                    final Sample individual = pedigree.getSampleById(sample);
                    if (step == 0 && (individual == null || !individual.isAffected()))
                        continue;
                    if (step == 1 && (individual == null || !individual.isUnaffected()))
                        continue;
                    if (step == 2 && individual != null && individual.isStatusSet())
                        continue;
                    // sample
                    w.writeStartElement("g");
                    switch(step) {
                        case 0:
                            w.writeAttribute("style", "hue-rotate(195deg);");
                            break;
                        case 1:
                            w.writeAttribute("style", "hue-rotate(45deg);");
                            break;
                        default:
                            break;
                    }
                    for (int vidx = 0; vidx < variants.size(); ++vidx) {
                        final VariantContext vc = variants.get(vidx);
                        final Genotype g = vc.getGenotype(sample);
                        double opacity = 1.0;
                        if (vc.isIndel())
                            opacity *= this.variantIndelOpacity;
                        if (vc.isFiltered())
                            opacity *= this.variantFILTEREDOpacity;
                        if (opacity > 1)
                            opacity = 1;
                        if (opacity <= 0)
                            continue;
                        if (opacity < 1) {
                            w.writeStartElement("g");
                            w.writeAttribute("style", "opacity:" + opacity + ";");
                        }
                        w.writeEmptyElement("use");
                        w.writeAttribute("x", String.valueOf(variantIndexToPixel.apply(vidx)));
                        w.writeAttribute("y", String.valueOf(y));
                        w.writeAttribute("xlink", XLINK.NS, "href", "#g_" + g.getType());
                        if (opacity < 1) {
                            w.writeEndElement();
                        }
                    }
                    w.writeCharacters("\n");
                    w.writeStartElement("text");
                    w.writeAttribute("x", String.valueOf(margin_left - 10));
                    w.writeAttribute("y", String.valueOf(y + this.genotype_width / 2.0));
                    w.writeAttribute("style", "text-anchor:end;");
                    w.writeCharacters(sample);
                    // text
                    w.writeEndElement();
                    // g for sample
                    w.writeEndElement();
                    y += this.genotype_width;
                }
            }
            w.writeCharacters("\n");
            w.writeEndDocument();
            w.writeCharacters("\n");
            w.flush();
            w.close();
            final String md5 = StringUtils.md5(interval.getContig() + ":" + interval.getStart() + "-" + interval.getEnd());
            final String filename = md5.substring(0, 2) + File.separatorChar + md5.substring(2) + File.separator + interval.getContig().replaceAll("[/\\:]", "_") + "_" + interval.getStart() + "_" + interval.getEnd() + (StringUtils.isBlank(geneName) ? "" : "." + geneName.replaceAll("[/\\:]", "")) + (StringUtils.isBlank(geneId) ? "" : "." + geneId.replaceAll("[/\\:]", "")) + ".svg";
            OutputStream os = archiveFactory.openOuputStream(filename);
            IOUtils.copyTo(tmpSvg, os);
            os.flush();
            os.close();
            Files.delete(tmpSvg);
            manifestW.print(interval.getContig());
            manifestW.print('\t');
            manifestW.print(interval.getStart() - 1);
            manifestW.print('\t');
            manifestW.print(interval.getEnd());
            manifestW.print('\t');
            manifestW.print(transcripts.stream().map(G -> G.getGene().getId()).collect(Collectors.toSet()).stream().collect(Collectors.joining(";")));
            manifestW.print('\t');
            manifestW.print(transcripts.stream().map(G -> G.getGene().getGeneName()).collect(Collectors.toSet()).stream().collect(Collectors.joining(";")));
            manifestW.print('\t');
            manifestW.print(transcripts.stream().map(G -> G.getId()).collect(Collectors.toSet()).stream().collect(Collectors.joining(";")));
            manifestW.print('\t');
            manifestW.print((archiveFactory.isTarOrZipArchive() ? "" : this.outputPath.toString() + File.separator) + filename);
            manifestW.print('\t');
            manifestW.println(variants.size());
        }
        r.close();
        manifestW.flush();
        manifestW.close();
        manifestW = null;
        archiveFactory.close();
        archiveFactory = null;
        return 0;
    } catch (final Throwable err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(archiveFactory);
        CloserUtil.close(r);
        CloserUtil.close(outputStream);
        CloserUtil.close(manifestW);
    }
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype) ContigNameConverter(com.github.lindenb.jvarkit.util.bio.fasta.ContigNameConverter) XLINK(com.github.lindenb.jvarkit.util.ns.XLINK) Program(com.github.lindenb.jvarkit.util.jcommander.Program) Parameter(com.beust.jcommander.Parameter) NullOuputStream(com.github.lindenb.jvarkit.io.NullOuputStream) Transcript(com.github.lindenb.jvarkit.util.bio.structure.Transcript) VCFHeader(htsjdk.variant.vcf.VCFHeader) Exon(com.github.lindenb.jvarkit.util.bio.structure.Exon) HtsCollectors(com.github.lindenb.jvarkit.stream.HtsCollectors) IntervalListProvider(com.github.lindenb.jvarkit.samtools.util.IntervalListProvider) Gene(com.github.lindenb.jvarkit.util.bio.structure.Gene) Function(java.util.function.Function) SVG(com.github.lindenb.jvarkit.util.svg.SVG) ArrayList(java.util.ArrayList) Interval(htsjdk.samtools.util.Interval) NoSplitter(com.github.lindenb.jvarkit.util.jcommander.NoSplitter) PedigreeParser(com.github.lindenb.jvarkit.pedigree.PedigreeParser) XMLStreamException(javax.xml.stream.XMLStreamException) IOUtils(com.github.lindenb.jvarkit.io.IOUtils) Launcher(com.github.lindenb.jvarkit.util.jcommander.Launcher) VCFReaderFactory(com.github.lindenb.jvarkit.variant.vcf.VCFReaderFactory) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Path(java.nio.file.Path) IntFunction(java.util.function.IntFunction) CloserUtil(htsjdk.samtools.util.CloserUtil) OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) SequenceDictionaryUtils(com.github.lindenb.jvarkit.util.bio.SequenceDictionaryUtils) Locatable(htsjdk.samtools.util.Locatable) Pedigree(com.github.lindenb.jvarkit.pedigree.Pedigree) Files(java.nio.file.Files) IntervalTreeMap(htsjdk.samtools.util.IntervalTreeMap) Logger(com.github.lindenb.jvarkit.util.log.Logger) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) GenotypeType(htsjdk.variant.variantcontext.GenotypeType) VCFReader(htsjdk.variant.vcf.VCFReader) Collectors(java.util.stream.Collectors) File(java.io.File) GtfReader(com.github.lindenb.jvarkit.util.bio.structure.GtfReader) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) List(java.util.List) Paths(java.nio.file.Paths) StringUtils(com.github.lindenb.jvarkit.lang.StringUtils) VariantContext(htsjdk.variant.variantcontext.VariantContext) ArchiveFactory(com.github.lindenb.jvarkit.io.ArchiveFactory) Sample(com.github.lindenb.jvarkit.pedigree.Sample) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) VariantContext(htsjdk.variant.variantcontext.VariantContext) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) PedigreeParser(com.github.lindenb.jvarkit.pedigree.PedigreeParser) Exon(com.github.lindenb.jvarkit.util.bio.structure.Exon) Gene(com.github.lindenb.jvarkit.util.bio.structure.Gene) VCFReader(htsjdk.variant.vcf.VCFReader) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) NullOuputStream(com.github.lindenb.jvarkit.io.NullOuputStream) VCFHeader(htsjdk.variant.vcf.VCFHeader) PrintWriter(java.io.PrintWriter) Path(java.nio.file.Path) ArchiveFactory(com.github.lindenb.jvarkit.io.ArchiveFactory) Transcript(com.github.lindenb.jvarkit.util.bio.structure.Transcript) Sample(com.github.lindenb.jvarkit.pedigree.Sample) Genotype(htsjdk.variant.variantcontext.Genotype) GtfReader(com.github.lindenb.jvarkit.util.bio.structure.GtfReader) Pedigree(com.github.lindenb.jvarkit.pedigree.Pedigree) IntervalTreeMap(htsjdk.samtools.util.IntervalTreeMap) Interval(htsjdk.samtools.util.Interval) Locatable(htsjdk.samtools.util.Locatable)

Example 18 with VCFReader

use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.

the class NgsFilesSummary method readVCF.

private void readVCF(final Path f) {
    VCFReader r = null;
    InputStream in = null;
    try {
        boolean indexed = VCFUtils.isTabixVcfPath(f) || VCFUtils.isTribbleVcfPath(f);
        if (this.must_be_indexed && !indexed)
            return;
        in = IOUtils.openPathForReading(f);
        r = VCFReaderFactory.makeDefault().open(f, false);
        final VCFHeader header = r.getHeader();
        if (this.dict != null) {
            SAMSequenceDictionary dict2 = header.getSequenceDictionary();
            if (dict2 == null)
                return;
            if (!SequenceUtil.areSequenceDictionariesEqual(this.dict, dict2))
                return;
        }
        final List<String> sns = header.getSampleNamesInOrder();
        if (sns.isEmpty()) {
            print(null, "VCF", f, String.valueOf(indexed));
        } else {
            for (final String sample : sns) {
                print(sample, "VCF", f, String.valueOf(indexed));
            }
        }
    } catch (final Exception err) {
        LOG.error(err);
    } finally {
        CloserUtil.close(r);
        CloserUtil.close(in);
    }
}
Also used : VCFReader(htsjdk.variant.vcf.VCFReader) InputStream(java.io.InputStream) VCFHeader(htsjdk.variant.vcf.VCFHeader) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) IOException(java.io.IOException)

Example 19 with VCFReader

use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.

the class FindAVariation method scanRemote.

private void scanRemote(final String url) {
    VCFReader tabix = null;
    try {
        tabix = VCFReaderFactory.makeDefault().open(url);
        final VCFHeader header = tabix.getHeader();
        final SAMSequenceDictionary dict = header.getSequenceDictionary();
        for (final SimplePosition m : convertFromVcfHeader(url, header)) {
            if (dict.getSequence(m.getContig()) == null)
                continue;
            final java.util.Iterator<VariantContext> iter2 = tabix.query(new Interval(m));
            while (iter2.hasNext()) {
                final VariantContext ctx = iter2.next();
                if (this.onlySnp && (ctx.getStart() != m.getPosition() || ctx.getEnd() != m.getPosition()))
                    continue;
                report(url, header, ctx, m);
            }
            CloserUtil.close(iter2);
        }
        tabix.close();
        tabix = null;
    } catch (final htsjdk.tribble.TribbleException.InvalidHeader err) {
        LOG.warn(url + "\t" + err.getMessage());
    } catch (final Throwable err) {
        LOG.severe("cannot read " + url, err);
    } finally {
        CloserUtil.close(tabix);
    }
}
Also used : VCFReader(htsjdk.variant.vcf.VCFReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) SimplePosition(com.github.lindenb.jvarkit.samtools.util.SimplePosition) VCFHeader(htsjdk.variant.vcf.VCFHeader) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) Interval(htsjdk.samtools.util.Interval)

Example 20 with VCFReader

use of htsjdk.variant.vcf.VCFReader in project jvarkit by lindenb.

the class FindAVariation method scanPath.

private void scanPath(final String vcfPathString) {
    final Path vcfPath = Paths.get(vcfPathString);
    if (Files.isDirectory(vcfPath))
        return;
    if (!Files.isReadable(vcfPath))
        return;
    VCFIterator iter = null;
    VCFReader r = null;
    try {
        if (vcfPathString.endsWith(FileExtensions.BCF) && BcfToolsUtils.isBcfToolsRequired(Paths.get(vcfPathString))) {
            if (!this.use_bcf)
                return;
        }
        if (isIndexed(vcfPath)) {
            r = VCFReaderFactory.makeDefault().open(vcfPath, true);
            final VCFHeader header = r.getHeader();
            for (final SimplePosition m : convertFromVcfHeader(vcfPath.toString(), header)) {
                try (CloseableIterator<VariantContext> iter2 = r.query(m)) {
                    while (iter2.hasNext()) {
                        final VariantContext ctx = iter2.next();
                        if (this.onlySnp && (ctx.getStart() != m.getPosition() || ctx.getEnd() != m.getPosition()))
                            continue;
                        report(vcfPathString, header, ctx, m);
                    }
                }
            }
            r.close();
            r = null;
        } else if (!this.indexedOnly) {
            iter = VCFUtils.createVCFIteratorFromPath(vcfPath);
            final VCFHeader header = iter.getHeader();
            final Set<SimplePosition> mutlist = convertFromVcfHeader(vcfPath.toString(), iter.getHeader());
            while (iter.hasNext()) {
                final VariantContext ctx = iter.next();
                if (this.onlySnp && ctx.getLengthOnReference() != 1)
                    continue;
                for (final SimplePosition m2 : mutlist) {
                    if (!m2.overlaps(ctx))
                        continue;
                    if (this.onlySnp && (ctx.getStart() != m2.getPosition() || ctx.getEnd() != m2.getPosition()))
                        continue;
                    report(vcfPathString, header, ctx, m2);
                }
            }
            iter.close();
            iter = null;
        }
    } catch (final htsjdk.tribble.TribbleException.InvalidHeader err) {
        LOG.warn(vcfPathString + "\t" + err.getMessage());
    } catch (final Throwable err) {
        LOG.severe("cannot read " + vcfPathString, err);
    } finally {
        CloserUtil.close(r);
        CloserUtil.close(iter);
    }
}
Also used : Path(java.nio.file.Path) HashSet(java.util.HashSet) Set(java.util.Set) VCFReader(htsjdk.variant.vcf.VCFReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) SimplePosition(com.github.lindenb.jvarkit.samtools.util.SimplePosition) VCFHeader(htsjdk.variant.vcf.VCFHeader) VCFIterator(htsjdk.variant.vcf.VCFIterator)

Aggregations

VCFReader (htsjdk.variant.vcf.VCFReader)60 VariantContext (htsjdk.variant.variantcontext.VariantContext)45 Path (java.nio.file.Path)41 VCFHeader (htsjdk.variant.vcf.VCFHeader)37 VCFReaderFactory (com.github.lindenb.jvarkit.variant.vcf.VCFReaderFactory)34 CloseableIterator (htsjdk.samtools.util.CloseableIterator)32 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)31 Collectors (java.util.stream.Collectors)30 ArrayList (java.util.ArrayList)29 List (java.util.List)29 Logger (com.github.lindenb.jvarkit.util.log.Logger)28 Parameter (com.beust.jcommander.Parameter)27 Program (com.github.lindenb.jvarkit.util.jcommander.Program)26 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)24 Set (java.util.Set)22 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)21 IOException (java.io.IOException)21 CloserUtil (htsjdk.samtools.util.CloserUtil)20 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)20 StringUtils (com.github.lindenb.jvarkit.lang.StringUtils)19