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);
}
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;
}
Aggregations