Search in sources :

Example 1 with Trio

use of com.github.lindenb.jvarkit.pedigree.Trio in project jvarkit by lindenb.

the class VCFTrios method doVcfToVcf.

@Override
public int doVcfToVcf(final String inputName, VCFIterator r, final VariantContextWriter w) {
    long count_incompats = 0L;
    final Set<String> sampleNotFoundInVcf = new HashSet<>();
    Pedigree pedigree = null;
    final List<TrioTriple> trios = new ArrayList<>();
    try {
        final DeNovoDetector detector = new DeNovoDetector();
        detector.setConvertingNoCallToHomRef(this.nocall_to_homref);
        final VCFHeader header = r.getHeader();
        final PedigreeParser pedParser = new PedigreeParser();
        pedigree = pedParser.parse(this.pedigreeFile);
        final VCFHeader h2 = new VCFHeader(header);
        final Set<VCFHeaderLine> meta = new HashSet<>();
        meta.add(new VCFInfoHeaderLine(this.attributeName, VCFHeaderLineCount.UNBOUNDED, VCFHeaderLineType.String, "Samples with mendelian incompatibilities." + (this.pedigreeFile == null ? "" : " Pedigree File was : " + this.pedigreeFile)));
        meta.add(VCFStandardHeaderLines.getFormatLine(VCFConstants.GENOTYPE_FILTER_KEY, true));
        if (!StringUtil.isBlank(this.filterAnyIncompat)) {
            meta.add(new VCFFilterHeaderLine(this.filterAnyIncompat, "Variant contains at least one mendelian incompatibilities"));
        }
        if (!StringUtil.isBlank(this.filterNoIncompat)) {
            meta.add(new VCFFilterHeaderLine(this.filterNoIncompat, "Variant does not contain any mendelian incompatibilities"));
        }
        meta.stream().forEach(H -> h2.addMetaDataLine(H));
        JVarkitVersion.getInstance().addMetaData(this, h2);
        for (final Trio pedTrio : pedigree.getTrios()) {
            final TrioTriple trio = new TrioTriple();
            final Sample child = pedTrio.getChild();
            trio.child_id = header.getSampleNameToOffset().getOrDefault(child.getId(), -1);
            if (trio.child_id < 0)
                continue;
            if (pedTrio.hasFather()) {
                final Sample parent = pedTrio.getFather();
                trio.father_id = header.getSampleNameToOffset().getOrDefault(parent.getId(), -1);
            }
            if (pedTrio.hasMother()) {
                final Sample parent = pedTrio.getMother();
                trio.mother_id = header.getSampleNameToOffset().getOrDefault(parent.getId(), -1);
            }
            if (trio.father_id == -1 && trio.mother_id == -1) {
                continue;
            }
            trios.add(trio);
        }
        LOG.info("trios(s) in pedigree: " + trios.size());
        final ProgressFactory.Watcher<VariantContext> progress = ProgressFactory.newInstance().dictionary(header).logger(LOG).build();
        w.writeHeader(h2);
        while (r.hasNext()) {
            final VariantContext ctx = progress.apply(r.next());
            final Set<String> incompatibilities = new HashSet<String>();
            for (final TrioTriple trio : trios) {
                final Genotype gChild = ctx.getGenotype(trio.child_id);
                if (gChild == null)
                    throw new IllegalStateException();
                final Genotype gFather = trio.father_id < 0 ? null : ctx.getGenotype(trio.father_id);
                final Genotype gMother = trio.mother_id < 0 ? null : ctx.getGenotype(trio.mother_id);
                final DeNovoDetector.DeNovoMutation mut = detector.test(ctx, gFather, gMother, gChild);
                if (mut != null) {
                    incompatibilities.add(gChild.getSampleName());
                }
            }
            final VariantContextBuilder vcb = new VariantContextBuilder(ctx);
            if (!incompatibilities.isEmpty()) {
                // set filter for samples that are not a mendelian violation
                if (!StringUtil.isBlank(this.genotypeFilterNameNoIncompat)) {
                    vcb.genotypes(ctx.getGenotypes().stream().map(G -> incompatibilities.contains(G.getSampleName()) ? G : new GenotypeBuilder(G).filters(this.genotypeFilterNameNoIncompat).make()).collect(Collectors.toList()));
                }
                ++count_incompats;
                // set INFO attribute
                vcb.attribute(attributeName, incompatibilities.toArray());
                // set FILTER
                if (!StringUtil.isBlank(this.filterAnyIncompat)) {
                    vcb.filter(this.filterAnyIncompat);
                } else if (!ctx.isFiltered()) {
                    vcb.passFilters();
                }
            } else // No denovo
            {
                // dicard variant
                if (this.discard_variants_without_mendelian_incompat) {
                    continue;
                }
                // set filters
                if (!StringUtil.isBlank(this.filterNoIncompat)) {
                    vcb.filter(this.filterNoIncompat);
                } else if (!ctx.isFiltered()) {
                    vcb.passFilters();
                }
            }
            w.add(vcb.make());
        }
        progress.close();
        LOG.info("incompatibilitie(s) N=" + count_incompats);
        if (!sampleNotFoundInVcf.isEmpty()) {
            LOG.info("SAMPLE(S) not found: " + String.join(" / ", sampleNotFoundInVcf));
        }
        return 0;
    } catch (final Throwable err) {
        LOG.error(err);
        return -1;
    }
}
Also used : VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) ProgressFactory(com.github.lindenb.jvarkit.util.log.ProgressFactory) ArrayList(java.util.ArrayList) VariantContext(htsjdk.variant.variantcontext.VariantContext) PedigreeParser(com.github.lindenb.jvarkit.pedigree.PedigreeParser) VCFFilterHeaderLine(htsjdk.variant.vcf.VCFFilterHeaderLine) VCFHeader(htsjdk.variant.vcf.VCFHeader) HashSet(java.util.HashSet) Trio(com.github.lindenb.jvarkit.pedigree.Trio) Sample(com.github.lindenb.jvarkit.pedigree.Sample) Genotype(htsjdk.variant.variantcontext.Genotype) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) Pedigree(com.github.lindenb.jvarkit.pedigree.Pedigree) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder)

Example 2 with Trio

use of com.github.lindenb.jvarkit.pedigree.Trio in project jvarkit by lindenb.

the class SwingTrioTableModel method setVariant.

public void setVariant(final VariantContext ctx) {
    final List<DeNovoDetector.DeNovoMutation> L;
    if (ctx == null || !ctx.hasGenotypes() || this.trios.isEmpty()) {
        L = Collections.emptyList();
    } else {
        L = new ArrayList<>();
        for (final Trio trio : this.trios) {
            final DeNovoMutation mut = this.detector.test(ctx, trio);
            if (mut != null)
                L.add(mut);
        }
    }
    setRows(L);
}
Also used : Trio(com.github.lindenb.jvarkit.pedigree.Trio) DeNovoMutation(com.github.lindenb.jvarkit.tools.vcftrios.DeNovoDetector.DeNovoMutation)

Aggregations

Trio (com.github.lindenb.jvarkit.pedigree.Trio)2 Pedigree (com.github.lindenb.jvarkit.pedigree.Pedigree)1 PedigreeParser (com.github.lindenb.jvarkit.pedigree.PedigreeParser)1 Sample (com.github.lindenb.jvarkit.pedigree.Sample)1 DeNovoMutation (com.github.lindenb.jvarkit.tools.vcftrios.DeNovoDetector.DeNovoMutation)1 ProgressFactory (com.github.lindenb.jvarkit.util.log.ProgressFactory)1 Genotype (htsjdk.variant.variantcontext.Genotype)1 GenotypeBuilder (htsjdk.variant.variantcontext.GenotypeBuilder)1 VariantContext (htsjdk.variant.variantcontext.VariantContext)1 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)1 VCFFilterHeaderLine (htsjdk.variant.vcf.VCFFilterHeaderLine)1 VCFHeader (htsjdk.variant.vcf.VCFHeader)1 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)1 VCFInfoHeaderLine (htsjdk.variant.vcf.VCFInfoHeaderLine)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1