Search in sources :

Example 96 with Genotype

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

the class Mutect2FilteringEngine method applyContaminationFilter.

// very naive M1-style contamination filter -- remove calls with AF less than the contamination fraction
private void applyContaminationFilter(final M2FiltersArgumentCollection MTFAC, final VariantContext vc, final Collection<String> filters) {
    final Genotype tumorGenotype = vc.getGenotype(tumorSample);
    final double[] alleleFractions = GATKProtectedVariantContextUtils.getAttributeAsDoubleArray(tumorGenotype, VCFConstants.ALLELE_FREQUENCY_KEY, () -> new double[] { 1.0 }, 1.0);
    final double maxFraction = MathUtils.arrayMax(alleleFractions);
    if (maxFraction < contamination) {
        filters.add(CONTAMINATION_FILTER_NAME);
    }
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype)

Example 97 with Genotype

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

the class AlleleFrequencyCalculator method effectiveAlleleCounts.

// effectiveAlleleCounts[allele a] = SUM_{genotypes g} (posterior_probability(g) * num_copies of a in g), which we denote as SUM [n_g p_g]
// for numerical stability we will do this in log space:
// count = SUM 10^(log (n_g p_g)) = SUM 10^(log n_g + log p_g)
// thanks to the log-sum-exp trick this lets us work with log posteriors alone
private double[] effectiveAlleleCounts(final VariantContext vc, final double[] log10AlleleFrequencies) {
    final int numAlleles = vc.getNAlleles();
    Utils.validateArg(numAlleles == log10AlleleFrequencies.length, "number of alleles inconsistent");
    final double[] log10Result = new double[numAlleles];
    Arrays.fill(log10Result, Double.NEGATIVE_INFINITY);
    for (final Genotype g : vc.getGenotypes()) {
        if (!g.hasLikelihoods()) {
            continue;
        }
        final GenotypeLikelihoodCalculator glCalc = GL_CALCS.getInstance(g.getPloidy(), numAlleles);
        final double[] log10GenotypePosteriors = log10NormalizedGenotypePosteriors(g, glCalc, log10AlleleFrequencies);
        new IndexRange(0, glCalc.genotypeCount()).forEach(genotypeIndex -> glCalc.genotypeAlleleCountsAt(genotypeIndex).forEachAlleleIndexAndCount((alleleIndex, count) -> log10Result[alleleIndex] = MathUtils.log10SumLog10(log10Result[alleleIndex], log10GenotypePosteriors[genotypeIndex] + MathUtils.log10(count))));
    }
    return MathUtils.applyToArrayInPlace(log10Result, x -> Math.pow(10.0, x));
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype) IntStream(java.util.stream.IntStream) Allele(htsjdk.variant.variantcontext.Allele) Arrays(java.util.Arrays) MathArrays(org.apache.commons.math3.util.MathArrays) Dirichlet(org.broadinstitute.hellbender.utils.Dirichlet) GenotypeAlleleCounts(org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeAlleleCounts) Collectors(java.util.stream.Collectors) GenotypeLikelihoodCalculator(org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeLikelihoodCalculator) GenotypeLikelihoodCalculators(org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeLikelihoodCalculators) List(java.util.List) MathUtils(org.broadinstitute.hellbender.utils.MathUtils) Map(java.util.Map) VariantContext(htsjdk.variant.variantcontext.VariantContext) Utils(org.broadinstitute.hellbender.utils.Utils) IndexRange(org.broadinstitute.hellbender.utils.IndexRange) IndexRange(org.broadinstitute.hellbender.utils.IndexRange) Genotype(htsjdk.variant.variantcontext.Genotype) GenotypeLikelihoodCalculator(org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeLikelihoodCalculator)

Example 98 with Genotype

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

the class GenotypingEngineUnitTest method testCalculateGenotypes.

// Want to run testCoveredByDeletion first to ensure clean list or recorded deletions
@Test(dependsOnMethods = { "testCoveredByDeletion" })
public void testCalculateGenotypes() {
    genotypingEngine.clearUpstreamDeletionsLoc();
    // Remove deletion
    final List<Genotype> genotypes = Arrays.asList(// first alt
    new GenotypeBuilder("sample1").alleles(gtAlleles).PL(new double[] { 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }).make(), // second alt
    new GenotypeBuilder("sample2").alleles(gtAlleles).PL(new double[] { 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0 }).make());
    final VariantContext vc = new VariantContextBuilder("test", "1", 1, refAllele.length(), allelesDel).genotypes(genotypes).make();
    final VariantContext vcOut = genotypingEngine.calculateGenotypes(vc, GenotypeLikelihoodsCalculationModel.INDEL, null);
    Assert.assertFalse(vcOut.getAlleles().contains(altT));
    // Make sure the spanning deletion is removed since the deletion was removed
    final Allele refAlleleSpanDel = Allele.create("C", true);
    final List<Allele> vcAllelesSpanDel = new ArrayList<>(Arrays.asList(refAlleleSpanDel, Allele.SPAN_DEL, GATKVCFConstants.NON_REF_SYMBOLIC_ALLELE));
    final List<Genotype> genotypesSpanDel = Arrays.asList(// first alt
    new GenotypeBuilder("sample1").alleles(gtAlleles).PL(new double[] { 0, 0, 100, 0, 0, 0 }).make(), new GenotypeBuilder("sample2").alleles(gtAlleles).PL(new double[] { 0, 0, 0, 0, 0, 0 }).make());
    final VariantContext vcSpanDel = new VariantContextBuilder("test1", "1", 2, 2 + refAlleleSpanDel.length() - 1, vcAllelesSpanDel).genotypes(genotypesSpanDel).make();
    final VariantContext vcOut1 = genotypingEngine.calculateGenotypes(vcSpanDel, GenotypeLikelihoodsCalculationModel.INDEL, null);
    Assert.assertFalse(vcOut1.getAlleles().contains(Allele.SPAN_DEL));
}
Also used : Allele(htsjdk.variant.variantcontext.Allele) VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) ArrayList(java.util.ArrayList) Genotype(htsjdk.variant.variantcontext.Genotype) VariantContext(htsjdk.variant.variantcontext.VariantContext) GenotypeBuilder(htsjdk.variant.variantcontext.GenotypeBuilder) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 99 with Genotype

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

the class IndependentAllelesDiploidExactAFCalculatorUnitTest method makeTestMakeAlleleConditionalContexts.

@DataProvider(name = "TestMakeAlleleConditionalContexts")
public Object[][] makeTestMakeAlleleConditionalContexts() {
    List<Object[]> tests = new ArrayList<>();
    final VariantContextBuilder root = new VariantContextBuilder("x", "1", 1, 1, Arrays.asList(A));
    final VariantContextBuilder vcAC = new VariantContextBuilder(root).alleles(Arrays.asList(A, C));
    final VariantContextBuilder vcAG = new VariantContextBuilder(root).alleles(Arrays.asList(A, G));
    final VariantContextBuilder vcACG = new VariantContextBuilder(root).alleles(Arrays.asList(A, C, G));
    final VariantContextBuilder vcAGC = new VariantContextBuilder(root).alleles(Arrays.asList(A, G, C));
    final Genotype gACG = makePL(0, 1, 2, 3, 4, 5);
    final Genotype gAGC = makePL(0, 4, 5, 1, 3, 2);
    final Genotype gACcombined = makePL(0, 2, 5);
    final Genotype gACcombined2 = makePL(0, 1, 4);
    final Genotype gAGcombined = makePL(0, 4, 9);
    // biallelic
    tests.add(new Object[] { vcAC.genotypes(gACcombined).make(), Arrays.asList(vcAC.genotypes(gACcombined).make()) });
    // tri-allelic
    tests.add(new Object[] { vcACG.genotypes(gACG).make(), Arrays.asList(vcAC.genotypes(gACcombined).make(), vcAG.genotypes(gAGcombined).make()) });
    tests.add(new Object[] { vcAGC.genotypes(gAGC).make(), Arrays.asList(vcAG.genotypes(gAGcombined).make(), vcAC.genotypes(gACcombined2).make()) });
    return tests.toArray(new Object[][] {});
}
Also used : VariantContextBuilder(htsjdk.variant.variantcontext.VariantContextBuilder) Genotype(htsjdk.variant.variantcontext.Genotype) DataProvider(org.testng.annotations.DataProvider)

Example 100 with Genotype

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

the class IndependentAllelesDiploidExactAFCalculatorUnitTest method testCombinePrecise.

@Test(enabled = true, dataProvider = "TestCombineGLs")
public void testCombinePrecise(final int altIndex, final int nAlts, final Genotype testg, final Genotype expected) {
    final Genotype combined = IndependentAllelesDiploidExactAFCalculator.combineGLsPrecise(testg, altIndex, nAlts);
    Assert.assertEquals(combined.getPL(), expected.getPL(), "Combined PLs " + Utils.join(",", combined.getPL()) + " != expected " + Utils.join(",", expected.getPL()));
}
Also used : Genotype(htsjdk.variant.variantcontext.Genotype) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Aggregations

Genotype (htsjdk.variant.variantcontext.Genotype)150 VariantContext (htsjdk.variant.variantcontext.VariantContext)97 Allele (htsjdk.variant.variantcontext.Allele)82 ArrayList (java.util.ArrayList)54 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)52 GenotypeBuilder (htsjdk.variant.variantcontext.GenotypeBuilder)51 File (java.io.File)48 VCFHeader (htsjdk.variant.vcf.VCFHeader)46 IOException (java.io.IOException)45 Collectors (java.util.stream.Collectors)42 Test (org.testng.annotations.Test)37 HashSet (java.util.HashSet)35 VariantContextWriter (htsjdk.variant.variantcontext.writer.VariantContextWriter)29 List (java.util.List)29 VCFHeaderLine (htsjdk.variant.vcf.VCFHeaderLine)27 VCFFormatHeaderLine (htsjdk.variant.vcf.VCFFormatHeaderLine)25 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)23 HashMap (java.util.HashMap)23 java.util (java.util)22 Set (java.util.Set)22