Search in sources :

Example 31 with VariantContextBuilder

use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.

the class SelectVariants method apply.

@Override
public void apply(VariantContext vc, ReadsContext readsContext, ReferenceContext ref, FeatureContext featureContext) {
    if (fullyDecode) {
        vc = vc.fullyDecode(getHeaderForVariants(), lenientVCFProcessing);
    }
    if (mendelianViolations && invertLogic((mv.countFamilyViolations(sampleDB, samples, vc) == 0), invertMendelianViolations)) {
        return;
    }
    if (discordanceOnly && !isDiscordant(vc, featureContext.getValues(discordanceTrack))) {
        return;
    }
    if (concordanceOnly && !isConcordant(vc, featureContext.getValues(concordanceTrack))) {
        return;
    }
    if (alleleRestriction.equals(NumberAlleleRestriction.BIALLELIC) && !vc.isBiallelic()) {
        return;
    }
    if (alleleRestriction.equals(NumberAlleleRestriction.MULTIALLELIC) && vc.isBiallelic()) {
        return;
    }
    if (containsIndelLargerOrSmallerThan(vc, maxIndelSize, minIndelSize)) {
        return;
    }
    if (considerFilteredGenotypes()) {
        final int numFilteredSamples = numFilteredGenotypes(vc);
        final double fractionFilteredGenotypes = samples.isEmpty() ? 0.0 : numFilteredSamples / samples.size();
        if (numFilteredSamples > maxFilteredGenotypes || numFilteredSamples < minFilteredGenotypes || fractionFilteredGenotypes > maxFractionFilteredGenotypes || fractionFilteredGenotypes < minFractionFilteredGenotypes)
            return;
    }
    if (considerNoCallGenotypes()) {
        final int numNoCallSamples = numNoCallGenotypes(vc);
        final double fractionNoCallGenotypes = samples.isEmpty() ? 0.0 : ((double) numNoCallSamples) / samples.size();
        if (numNoCallSamples > maxNOCALLnumber || fractionNoCallGenotypes > maxNOCALLfraction)
            return;
    }
    // Initialize the cache of PL index to a list of alleles for each ploidy.
    initalizeAlleleAnyploidIndicesCache(vc);
    final VariantContext sub = subsetRecord(vc, preserveAlleles, removeUnusedAlternates);
    final VariantContextBuilder builder = new VariantContextBuilder(vc);
    if (setFilteredGenotypesToNocall) {
        GATKVariantContextUtils.setFilteredGenotypeToNocall(builder, sub, setFilteredGenotypesToNocall, this::getGenotypeFilters);
    }
    final VariantContext filteredGenotypeToNocall = setFilteredGenotypesToNocall ? builder.make() : sub;
    // Not excluding non-variants or subsetted polymorphic variants AND including filtered loci or subsetted variant is not filtered
    if ((!XLnonVariants || filteredGenotypeToNocall.isPolymorphicInSamples()) && (!XLfiltered || !filteredGenotypeToNocall.isFiltered())) {
        // Write the subsetted variant if it matches all of the expressions
        boolean failedJexlMatch = false;
        try {
            for (VariantContextUtils.JexlVCMatchExp jexl : jexls) {
                if (invertLogic(!VariantContextUtils.match(filteredGenotypeToNocall, jexl), invertSelect)) {
                    failedJexlMatch = true;
                    break;
                }
            }
        } catch (IllegalArgumentException e) {
            //  expression detected...")
            throw new UserException(e.getMessage() + "\nSee https://www.broadinstitute.org/gatk/guide/article?id=1255 for documentation on using JEXL in GATK", e);
        }
        if (!failedJexlMatch && (!selectRandomFraction || Utils.getRandomGenerator().nextDouble() < fractionRandom)) {
            vcfWriter.add(filteredGenotypeToNocall);
        }
    }
}
Also used : VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VariantContext(htsjdk.variant.variantcontext.VariantContext) VariantContextUtils(htsjdk.variant.variantcontext.VariantContextUtils) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Example 32 with VariantContextBuilder

use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.

the class SelectVariants method subsetRecord.

/**
     * Helper method to subset a VC record, modifying some metadata stored in the INFO field (i.e. AN, AC, AF).
     *
     * @param vc       the VariantContext record to subset
     * @param preserveAlleles should we trim constant sequence from the beginning and/or end of all alleles, or preserve it?
     * @param removeUnusedAlternates removes alternate alleles with AC=0
     * @return the subsetted VariantContext
     */
private VariantContext subsetRecord(final VariantContext vc, final boolean preserveAlleles, final boolean removeUnusedAlternates) {
    //subContextFromSamples() always decodes the vc, which is a fairly expensive operation.  Avoid if possible
    if (noSamplesSpecified && !removeUnusedAlternates) {
        return vc;
    }
    // strip out the alternate alleles that aren't being used
    final VariantContext sub = vc.subContextFromSamples(samples, removeUnusedAlternates);
    // If no subsetting happened, exit now
    if (sub.getNSamples() == vc.getNSamples() && sub.getNAlleles() == vc.getNAlleles()) {
        return vc;
    }
    // fix the PL and AD values if sub has fewer alleles than original vc and remove a fraction of the genotypes if needed
    final GenotypesContext oldGs = sub.getGenotypes();
    GenotypesContext newGC = sub.getNAlleles() == vc.getNAlleles() ? oldGs : AlleleSubsettingUtils.subsetAlleles(oldGs, 0, vc.getAlleles(), sub.getAlleles(), GenotypeAssignmentMethod.DO_NOT_ASSIGN_GENOTYPES, vc.getAttributeAsInt(VCFConstants.DEPTH_KEY, 0));
    if (fractionGenotypes > 0) {
        final List<Genotype> genotypes = newGC.stream().map(genotype -> randomGenotypes.nextDouble() > fractionGenotypes ? genotype : new GenotypeBuilder(genotype).alleles(getNoCallAlleles(genotype.getPloidy())).noGQ().make()).collect(Collectors.toList());
        newGC = GenotypesContext.create(new ArrayList<>(genotypes));
    }
    // since the VC has been subset (either by sample or allele), we need to strip out the MLE tags
    final VariantContextBuilder builder = new VariantContextBuilder(sub);
    builder.rmAttributes(Arrays.asList(GATKVCFConstants.MLE_ALLELE_COUNT_KEY, GATKVCFConstants.MLE_ALLELE_FREQUENCY_KEY));
    builder.genotypes(newGC);
    addAnnotations(builder, vc, sub.getSampleNames());
    final VariantContext subset = builder.make();
    return preserveAlleles ? subset : GATKVariantContextUtils.trimAlleles(subset, true, true);
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype) VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) DocumentedFeature(org.broadinstitute.barclay.help.DocumentedFeature) Allele(htsjdk.variant.variantcontext.Allele) CommandLineProgramProperties(org.broadinstitute.barclay.argparser.CommandLineProgramProperties) java.util(java.util) GenotypeLikelihoods(htsjdk.variant.variantcontext.GenotypeLikelihoods) VCFStandardHeaderLines(htsjdk.variant.vcf.VCFStandardHeaderLines) VCFHeader(htsjdk.variant.vcf.VCFHeader) Argument(org.broadinstitute.barclay.argparser.Argument) GenotypeAssignmentMethod(org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeAssignmentMethod) StandardArgumentDefinitions(org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions) SampleDB(org.broadinstitute.hellbender.utils.samples.SampleDB) VariantProgramGroup(org.broadinstitute.hellbender.cmdline.programgroups.VariantProgramGroup) VariantIDsVariantFilter(org.broadinstitute.hellbender.engine.filters.VariantIDsVariantFilter) VariantContextUtils(htsjdk.variant.variantcontext.VariantContextUtils) SampleDBBuilder(org.broadinstitute.hellbender.utils.samples.SampleDBBuilder) ReferenceContext(org.broadinstitute.hellbender.engine.ReferenceContext) FeatureContext(org.broadinstitute.hellbender.engine.FeatureContext) VCFConstants(htsjdk.variant.vcf.VCFConstants) VariantWalker(org.broadinstitute.hellbender.engine.VariantWalker) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) Predicate(java.util.function.Predicate) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) VariantFilterLibrary(org.broadinstitute.hellbender.engine.filters.VariantFilterLibrary) Collectors(java.util.stream.Collectors) VariantFilter(org.broadinstitute.hellbender.engine.filters.VariantFilter) File(java.io.File) GenotypesContext(htsjdk.variant.variantcontext.GenotypesContext) UserException(org.broadinstitute.hellbender.exceptions.UserException) AlleleSubsettingUtils(org.broadinstitute.hellbender.tools.walkers.genotyper.AlleleSubsettingUtils) org.broadinstitute.hellbender.utils.variant(org.broadinstitute.hellbender.utils.variant) MendelianViolation(org.broadinstitute.hellbender.utils.samples.MendelianViolation) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VariantTypesVariantFilter(org.broadinstitute.hellbender.engine.filters.VariantTypesVariantFilter) ChromosomeCounts(org.broadinstitute.hellbender.tools.walkers.annotator.ChromosomeCounts) PedigreeValidationType(org.broadinstitute.hellbender.utils.samples.PedigreeValidationType) VariantContext(htsjdk.variant.variantcontext.VariantContext) VCFUtils(htsjdk.variant.vcf.VCFUtils) Utils(org.broadinstitute.hellbender.utils.Utils) Hidden(org.broadinstitute.barclay.argparser.Hidden) FeatureInput(org.broadinstitute.hellbender.engine.FeatureInput) ReadsContext(org.broadinstitute.hellbender.engine.ReadsContext) Pattern(java.util.regex.Pattern) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) GenotypesContext(htsjdk.variant.variantcontext.GenotypesContext) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VariantContext(htsjdk.variant.variantcontext.VariantContext) Genotype(htsjdk.variant.variantcontext.Genotype) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder)

Example 33 with VariantContextBuilder

use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.

the class AnnotateVcfWithBamDepth method apply.

@Override
public void apply(final VariantContext vc, final ReadsContext readsContext, final ReferenceContext refContext, final FeatureContext fc) {
    final MutableInt depth = new MutableInt(0);
    for (final GATKRead read : readsContext) {
        if (!read.failsVendorQualityCheck() && !read.isDuplicate() && !read.isUnmapped() && read.getEnd() > read.getStart() && new SimpleInterval(read).contains(vc)) {
            depth.increment();
        }
    }
    vcfWriter.add(new VariantContextBuilder(vc).attribute(POOLED_BAM_DEPTH_ANNOTATION_NAME, depth.intValue()).make());
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) MutableInt(org.apache.commons.lang.mutable.MutableInt) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval)

Example 34 with VariantContextBuilder

use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.

the class AnnotateVcfWithExpectedAlleleFraction method apply.

@Override
public void apply(final VariantContext vc, final ReadsContext readsContext, final ReferenceContext refContext, final FeatureContext fc) {
    final double[] weights = vc.getGenotypes().stream().mapToDouble(g -> weight(g)).toArray();
    final double expectedAlleleFraction = MathUtils.sum(MathArrays.ebeMultiply(weights, mixingFractionsInSampleOrder));
    vcfWriter.add(new VariantContextBuilder(vc).attribute(EXPECTED_ALLELE_FRACTION_NAME, expectedAlleleFraction).make());
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype) VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) DocumentedFeature(org.broadinstitute.barclay.help.DocumentedFeature) CommandLineProgramProperties(org.broadinstitute.barclay.argparser.CommandLineProgramProperties) VCFHeader(htsjdk.variant.vcf.VCFHeader) MathArrays(org.apache.commons.math3.util.MathArrays) Argument(org.broadinstitute.barclay.argparser.Argument) StandardArgumentDefinitions(org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions) VariantProgramGroup(org.broadinstitute.hellbender.cmdline.programgroups.VariantProgramGroup) HashSet(java.util.HashSet) Map(java.util.Map) ReferenceContext(org.broadinstitute.hellbender.engine.ReferenceContext) FeatureContext(org.broadinstitute.hellbender.engine.FeatureContext) VCFHeaderLineType(htsjdk.variant.vcf.VCFHeaderLineType) VariantWalker(org.broadinstitute.hellbender.engine.VariantWalker) Set(java.util.Set) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) MathUtils(org.broadinstitute.hellbender.utils.MathUtils) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) VariantContext(htsjdk.variant.variantcontext.VariantContext) ReadsContext(org.broadinstitute.hellbender.engine.ReadsContext) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder)

Example 35 with VariantContextBuilder

use of htsjdk.variant.variantcontext.VariantContextBuilder in project gatk by broadinstitute.

the class CreateSomaticPanelOfNormals method processVariantsAtSamePosition.

//TODO: this is the old Mutect behavior that just looks for multiple hits
//TODO: we should refine this
private static void processVariantsAtSamePosition(final List<VariantContext> variants, final VariantContextWriter writer) {
    if (variants.size() > 1) {
        final VariantContext mergedVc = AssemblyBasedCallerUtils.makeMergedVariantContext(variants);
        final VariantContext outputVc = new VariantContextBuilder().source(mergedVc.getSource()).loc(mergedVc.getContig(), mergedVc.getStart(), mergedVc.getEnd()).alleles(mergedVc.getAlleles()).make();
        writer.add(outputVc);
    }
}
Also used : VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) VariantContext(htsjdk.variant.variantcontext.VariantContext)

Aggregations

VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)57 VariantContext (htsjdk.variant.variantcontext.VariantContext)40 Test (org.testng.annotations.Test)26 Allele (htsjdk.variant.variantcontext.Allele)25 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)21 Genotype (htsjdk.variant.variantcontext.Genotype)12 ReferenceContext (org.broadinstitute.hellbender.engine.ReferenceContext)8 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)7 GenotypeBuilder (htsjdk.variant.variantcontext.GenotypeBuilder)6 GenotypesContext (htsjdk.variant.variantcontext.GenotypesContext)6 File (java.io.File)6 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)5 VCFHeader (htsjdk.variant.vcf.VCFHeader)5 CommandLineProgramTest (org.broadinstitute.hellbender.CommandLineProgramTest)5 BeforeTest (org.testng.annotations.BeforeTest)5 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)4 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)4 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3