Search in sources :

Example 1 with GenotypeAlleleCounts

use of org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeAlleleCounts in project gatk by broadinstitute.

the class AlleleFrequencyCalculator method log10NormalizedGenotypePosteriors.

private static double[] log10NormalizedGenotypePosteriors(final Genotype g, final GenotypeLikelihoodCalculator glCalc, final double[] log10AlleleFrequencies) {
    final double[] log10Likelihoods = g.getLikelihoods().getAsVector();
    final double[] log10Posteriors = new IndexRange(0, glCalc.genotypeCount()).mapToDouble(genotypeIndex -> {
        final GenotypeAlleleCounts gac = glCalc.genotypeAlleleCountsAt(genotypeIndex);
        return gac.log10CombinationCount() + log10Likelihoods[genotypeIndex] + gac.sumOverAlleleIndicesAndCounts((index, count) -> count * log10AlleleFrequencies[index]);
    });
    return MathUtils.normalizeLog10(log10Posteriors);
}
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) GenotypeAlleleCounts(org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeAlleleCounts)

Example 2 with GenotypeAlleleCounts

use of org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeAlleleCounts in project gatk by broadinstitute.

the class GeneralPloidyExactAFCalculator method computeLofK.

/**
     * Compute likelihood of a particular AC conformation and update AFresult object
     * @param set                     Set of AC counts to compute
     * @param firstGLs                  Original pool likelihoods before combining
     * @param secondGL                  New GL vector with additional pool
     * @param log10AlleleFrequencyPriors     Allele frequency priors
     * @param numAlleles                Number of alleles (including ref)
     * @param ploidy1                   Ploidy of original pool (combined)
     * @param ploidy2                   Ploidy of new pool
     * @return                          log-likelihood of requested conformation
     */
private double computeLofK(final ExactACset set, final CombinedPoolLikelihoods firstGLs, final double[] secondGL, final double[] log10AlleleFrequencyPriors, final int numAlleles, final int ploidy1, final int ploidy2, final StateTracker stateTracker) {
    final int newPloidy = ploidy1 + ploidy2;
    // sanity check
    int totalAltK = set.getACsum();
    if (newPloidy != totalAltK) {
        throw new GATKException("BUG: inconsistent sizes of set.getACsum and passed ploidy values");
    }
    totalAltK -= set.getACcounts().getCounts()[0];
    // special case for k = 0 over all k
    if (totalAltK == 0) {
        // all-ref case
        final double log10Lof0 = firstGLs.getGLOfACZero() + secondGL[HOM_REF_INDEX];
        set.getLog10Likelihoods()[0] = log10Lof0;
        stateTracker.setLog10LikelihoodOfAFzero(log10Lof0);
        stateTracker.setLog10PosteriorOfAFzero(log10Lof0 + log10AlleleFrequencyPriors[0]);
        return log10Lof0;
    } else {
        // initialize result with denominator
        // ExactACset holds by convention the conformation of all alleles, and the sum of all allele count is just the ploidy.
        // To compute n!/k1!k2!k3!... we need to compute first n!/(k2!k3!...) and then further divide by k1! where k1=ploidy-sum_k_i
        final int[] currentCount = set.getACcounts().getCounts();
        final double denom = -MathUtils.log10MultinomialCoefficient(newPloidy, currentCount);
        final GenotypeLikelihoodCalculator glCalc = GL_CALCS.getInstance(ploidy2, numAlleles);
        for (int PLIndex = 0; PLIndex < glCalc.genotypeCount(); PLIndex++) {
            final GenotypeAlleleCounts alleleCounts = glCalc.genotypeAlleleCountsAt(PLIndex);
            final int[] acCount2 = alleleCounts.alleleCountsByIndex(numAlleles - 1);
            final int[] acCount1 = MathUtils.vectorDiff(currentCount, acCount2);
            // for conformation to be valid, all elements of g2 have to be <= elements of current AC set
            if (isValidConformation(acCount1, ploidy1) && firstGLs.hasConformation(acCount1)) {
                final double gl2 = secondGL[PLIndex];
                if (!Double.isInfinite(gl2)) {
                    final double firstGL = firstGLs.getLikelihoodOfConformation(acCount1);
                    final double num1 = MathUtils.log10MultinomialCoefficient(ploidy1, acCount1);
                    final double num2 = MathUtils.log10MultinomialCoefficient(ploidy2, acCount2);
                    final double sum = firstGL + gl2 + num1 + num2;
                    set.getLog10Likelihoods()[0] = MathUtils.approximateLog10SumLog10(set.getLog10Likelihoods()[0], sum);
                }
            }
        }
        set.getLog10Likelihoods()[0] += denom;
    }
    double log10LofK = set.getLog10Likelihoods()[0];
    // update the MLE if necessary
    final int[] altCounts = Arrays.copyOfRange(set.getACcounts().getCounts(), 1, set.getACcounts().getCounts().length);
    // TODO -- GUILLERMO THIS CODE MAY PRODUCE POSITIVE LIKELIHOODS OR -INFINITY
    stateTracker.updateMLEifNeeded(Math.max(log10LofK, -Double.MAX_VALUE), altCounts);
    // apply the priors over each alternate allele
    for (final int ACcount : altCounts) {
        if (ACcount > 0) {
            log10LofK += log10AlleleFrequencyPriors[ACcount];
        }
    }
    // TODO -- GUILLERMO THIS CODE MAY PRODUCE POSITIVE LIKELIHOODS OR -INFINITY
    stateTracker.updateMAPifNeeded(Math.max(log10LofK, -Double.MAX_VALUE), altCounts);
    return log10LofK;
}
Also used : GATKException(org.broadinstitute.hellbender.exceptions.GATKException) GenotypeLikelihoodCalculator(org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeLikelihoodCalculator) GenotypeAlleleCounts(org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeAlleleCounts)

Aggregations

GenotypeAlleleCounts (org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeAlleleCounts)2 GenotypeLikelihoodCalculator (org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeLikelihoodCalculator)2 Allele (htsjdk.variant.variantcontext.Allele)1 Genotype (htsjdk.variant.variantcontext.Genotype)1 VariantContext (htsjdk.variant.variantcontext.VariantContext)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 MathArrays (org.apache.commons.math3.util.MathArrays)1 GATKException (org.broadinstitute.hellbender.exceptions.GATKException)1 GenotypeLikelihoodCalculators (org.broadinstitute.hellbender.tools.walkers.genotyper.GenotypeLikelihoodCalculators)1 Dirichlet (org.broadinstitute.hellbender.utils.Dirichlet)1 IndexRange (org.broadinstitute.hellbender.utils.IndexRange)1 MathUtils (org.broadinstitute.hellbender.utils.MathUtils)1 Utils (org.broadinstitute.hellbender.utils.Utils)1