Search in sources :

Example 71 with SAMSequenceDictionaryProgress

use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.

the class VcfRemoveGenotypeJs method doVcfToVcf.

@Override
protected int doVcfToVcf(String inputName, VcfIterator in, VariantContextWriter out) {
    try {
        this.script = super.compileJavascript(scriptExpr, scriptFile);
        final VCFHeader h2 = new VCFHeader(in.getHeader());
        if (!this.filterName.isEmpty()) {
            h2.addMetaDataLine(new VCFFormatHeaderLine(VCFConstants.GENOTYPE_FILTER_KEY, 1, VCFHeaderLineType.String, "Genotype-level filter"));
        }
        addMetaData(h2);
        out.writeHeader(h2);
        final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(in.getHeader());
        final Bindings bindings = this.script.getEngine().createBindings();
        bindings.put("header", in.getHeader());
        while (in.hasNext()) {
            final VariantContext ctx = progress.watch(in.next());
            bindings.put("variant", ctx);
            final VariantContextBuilder vcb = new VariantContextBuilder(ctx);
            List<Genotype> genotypes = new ArrayList<>();
            int countCalled = ctx.getNSamples();
            for (int i = 0; i < ctx.getNSamples(); ++i) {
                Genotype genotype = ctx.getGenotype(i);
                bindings.put("genotype", genotype);
                if (!genotype.isCalled() || genotype.isNoCall() || !genotype.isAvailable()) {
                    countCalled--;
                } else if (genotype.isCalled() && !super.evalJavaScriptBoolean(this.script, bindings)) {
                    if (!this.filterName.isEmpty()) {
                        if (!genotype.isFiltered()) {
                            genotype = new GenotypeBuilder(genotype).filters(this.filterName).make();
                        }
                    } else if (this.replaceByHomRef) {
                        List<Allele> homRefList = new ArrayList<>(genotype.getPloidy());
                        for (int p = 0; p < genotype.getPloidy(); ++p) {
                            homRefList.add(ctx.getReference());
                        }
                        genotype = new GenotypeBuilder(genotype).alleles(homRefList).make();
                    } else {
                        genotype = GenotypeBuilder.createMissing(genotype.getSampleName(), genotype.getPloidy());
                    }
                    countCalled--;
                }
                genotypes.add(genotype);
            }
            if (countCalled == 0 && this.removeCtxNoGenotype) {
                continue;
            }
            vcb.genotypes(genotypes);
            out.add(vcb.make());
        }
        progress.finish();
        return RETURN_OK;
    } catch (Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        this.script = null;
    }
}
Also used : SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) ArrayList(java.util.ArrayList) VariantContext(htsjdk.variant.variantcontext.VariantContext) Genotype(htsjdk.variant.variantcontext.Genotype) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) Bindings(javax.script.Bindings) Allele(htsjdk.variant.variantcontext.Allele) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VCFHeader(htsjdk.variant.vcf.VCFHeader) VCFFormatHeaderLine(htsjdk.variant.vcf.VCFFormatHeaderLine)

Example 72 with SAMSequenceDictionaryProgress

use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.

the class VcfRemoveUnusedAlt method doVcfToVcf.

@Override
protected int doVcfToVcf(final String inputName, final VcfIterator in, final VariantContextWriter delegate) {
    try {
        final VariantContextWriter out = this.component.open(delegate);
        out.writeHeader(in.getHeader());
        final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(in.getHeader()).logger(LOG);
        while (in.hasNext()) {
            out.add(progress.watch(in.next()));
        }
        progress.finish();
        out.close();
        return 0;
    } catch (final Exception err) {
        LOG.error(err);
        return -1;
    }
}
Also used : SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) DelegateVariantContextWriter(com.github.lindenb.jvarkit.util.vcf.DelegateVariantContextWriter) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) PostponedVariantContextWriter(com.github.lindenb.jvarkit.util.vcf.PostponedVariantContextWriter) IOException(java.io.IOException) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException)

Example 73 with SAMSequenceDictionaryProgress

use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.

the class VcfSetSequenceDictionary method doVcfToVcf.

@Override
protected int doVcfToVcf(final String inputName, final VcfIterator in, final VariantContextWriter delegate) {
    final VariantContextWriter out = this.component.open(delegate);
    out.writeHeader(in.getHeader());
    final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(in.getHeader()).logger(LOG);
    while (in.hasNext()) {
        out.add(progress.watch(in.next()));
    }
    progress.finish();
    return 0;
}
Also used : SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) DelegateVariantContextWriter(com.github.lindenb.jvarkit.util.vcf.DelegateVariantContextWriter) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter)

Example 74 with SAMSequenceDictionaryProgress

use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.

the class SortVcfOnRef2 method sortvcf.

protected int sortvcf(BufferedReader in) throws IOException {
    if (this.refdict != null) {
        LOG.info("load dict from " + this.refdict);
        this.dict = SAMSequenceDictionaryExtractor.extractDictionary(this.refdict);
        if (this.dict == null) {
            LOG.error("cannot find sam sequence dictionary from " + refdict);
        }
    }
    final VCFUtils.CodecAndHeader cah = VCFUtils.parseHeader(in);
    final VCFHeader h2 = new VCFHeader(cah.header);
    if (this.dict != null) {
        h2.setSequenceDictionary(this.dict);
    } else {
        this.dict = h2.getSequenceDictionary();
        if (this.dict == null) {
            LOG.error("No internal sequence dictionay found in input");
            return -1;
        }
    }
    addMetaData(h2);
    if (this.dict.isEmpty()) {
        LOG.warn("SEQUENCE DICTIONARY IS EMPTY/NULL");
    }
    CloseableIterator<ChromPosLine> iter = null;
    SortingCollection<ChromPosLine> array = null;
    VariantContextWriter w = null;
    try {
        array = SortingCollection.newInstance(ChromPosLine.class, new VariantCodec(), new VariantComparator(), this.writingSortingCollection.getMaxRecordsInRam(), this.writingSortingCollection.getTmpPaths());
        array.setDestructiveIteration(true);
        final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(this.dict);
        String line;
        while ((line = in.readLine()) != null) {
            final ChromPosLine cpl = new ChromPosLine(line);
            progress.watch(cpl.tid, cpl.pos);
            array.add(cpl);
        }
        array.doneAdding();
        progress.finish();
        w = super.openVariantContextWriter(outputFile);
        w.writeHeader(h2);
        iter = array.iterator();
        while (iter.hasNext()) {
            w.add(cah.codec.decode(iter.next().line));
            if (w.checkError())
                break;
        }
        return RETURN_OK;
    } catch (Exception e) {
        LOG.error(e);
        return -1;
    } finally {
        CloserUtil.close(w);
        CloserUtil.close(iter);
        if (array != null)
            array.cleanup();
    }
}
Also used : SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) VCFUtils(com.github.lindenb.jvarkit.util.vcf.VCFUtils) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFHeader(htsjdk.variant.vcf.VCFHeader) IOException(java.io.IOException)

Example 75 with SAMSequenceDictionaryProgress

use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.

the class VcfGnomad method doVcfToVcf.

@Override
protected int doVcfToVcf(final String inputName, final VcfIterator iter, final VariantContextWriter delegate) {
    final VariantContextWriter out = this.component.open(delegate);
    final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(iter.getHeader()).logger(LOG);
    out.writeHeader(iter.getHeader());
    while (iter.hasNext()) {
        out.add(progress.watch(iter.next()));
    }
    out.close();
    progress.finish();
    return 0;
}
Also used : SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) DelegateVariantContextWriter(com.github.lindenb.jvarkit.util.vcf.DelegateVariantContextWriter) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter)

Aggregations

SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)146 ArrayList (java.util.ArrayList)64 VariantContext (htsjdk.variant.variantcontext.VariantContext)59 VCFHeader (htsjdk.variant.vcf.VCFHeader)57 SAMRecord (htsjdk.samtools.SAMRecord)54 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)54 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)48 IOException (java.io.IOException)48 File (java.io.File)47 SamReader (htsjdk.samtools.SamReader)40 SAMFileHeader (htsjdk.samtools.SAMFileHeader)38 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)37 HashSet (java.util.HashSet)34 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)32 VcfIterator (com.github.lindenb.jvarkit.util.vcf.VcfIterator)30 List (java.util.List)30 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)29 HashMap (java.util.HashMap)28 Parameter (com.beust.jcommander.Parameter)27 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)27