Merge pull request #449 from broadinstitute/eb_move_calc_posteriors_to_protected

Moved CalculatePosteriors from private to protected, in preparation for 3.0
This commit is contained in:
Eric Banks 2013-12-07 22:18:46 -08:00
commit ab33db625f
5 changed files with 916 additions and 522 deletions

View File

@ -52,7 +52,6 @@ import net.sf.samtools.SAMUtils;
import org.apache.log4j.Logger;
import org.broadinstitute.sting.utils.MathUtils;
import org.broadinstitute.sting.utils.QualityUtils;
import org.broadinstitute.sting.utils.Utils;
import org.broadinstitute.sting.utils.exceptions.UserException;
import org.broadinstitute.sting.utils.genotyper.PerReadAlleleLikelihoodMap;
import org.broadinstitute.sting.utils.haplotype.Haplotype;
@ -61,9 +60,7 @@ import org.broadinstitute.sting.utils.recalibration.covariates.RepeatCovariate;
import org.broadinstitute.sting.utils.recalibration.covariates.RepeatLengthCovariate;
import org.broadinstitute.sting.utils.sam.GATKSAMRecord;
import org.broadinstitute.sting.utils.sam.ReadUtils;
import org.broadinstitute.sting.utils.variant.GATKVariantContextUtils;
import org.broadinstitute.variant.variantcontext.*;
import org.broadinstitute.variant.vcf.VCFConstants;
import java.io.File;
import java.io.FileNotFoundException;
@ -622,213 +619,4 @@ public class PairHMMLikelihoodCalculationEngine implements LikelihoodCalculation
readDelQuals[iii-1] = (byte) Math.min(0xff & readDelQuals[iii-1], 0xff & pcrIndelErrorModelCache[repeatLength]);
}
}
// --------------------------------------------------------------------------------
//
// Posterior GL calculations
//
// --------------------------------------------------------------------------------
public static VariantContext calculatePosteriorGLs(final VariantContext vc1,
final Collection<VariantContext> resources,
final int numRefSamplesFromMissingResources,
final double globalFrequencyPriorDirichlet,
final boolean useInputSamples,
final boolean useEM,
final boolean useAC) {
if ( useEM )
throw new IllegalArgumentException("EM loop for posterior GLs not yet implemented");
final Map<Allele,Integer> totalAlleleCounts = new HashMap<>();
for ( final VariantContext resource : resources ) {
addAlleleCounts(totalAlleleCounts,resource,useAC);
}
if ( useInputSamples ) {
addAlleleCounts(totalAlleleCounts,vc1,useAC);
}
totalAlleleCounts.put(vc1.getReference(),totalAlleleCounts.get(vc1.getReference())+numRefSamplesFromMissingResources);
// now extract the counts of the alleles present within vc1, and in order
final double[] alleleCounts = new double[vc1.getNAlleles()];
int alleleIndex = 0;
for ( final Allele allele : vc1.getAlleles() ) {
alleleCounts[alleleIndex++] = globalFrequencyPriorDirichlet + ( totalAlleleCounts.containsKey(allele) ?
totalAlleleCounts.get(allele) : 0 );
}
final List<double[]> likelihoods = new ArrayList<>(vc1.getNSamples());
for ( final Genotype genotype : vc1.getGenotypes() ) {
likelihoods.add(genotype.hasLikelihoods() ? genotype.getLikelihoods().getAsVector() : null );
}
final List<double[]> posteriors = calculatePosteriorGLs(likelihoods,alleleCounts,vc1.getMaxPloidy(2));
final GenotypesContext newContext = GenotypesContext.create();
for ( int genoIdx = 0; genoIdx < vc1.getNSamples(); genoIdx ++ ) {
final GenotypeBuilder builder = new GenotypeBuilder(vc1.getGenotype(genoIdx));
if ( posteriors.get(genoIdx) != null ) {
GATKVariantContextUtils.updateGenotypeAfterSubsetting(vc1.getAlleles(), builder,
GATKVariantContextUtils.GenotypeAssignmentMethod.USE_PLS_TO_ASSIGN, posteriors.get(genoIdx), vc1.getAlleles());
builder.attribute(VCFConstants.GENOTYPE_POSTERIORS_KEY,
Utils.listFromPrimitives(GenotypeLikelihoods.fromLog10Likelihoods(posteriors.get(genoIdx)).getAsPLs()));
}
newContext.add(builder.make());
}
final List<Integer> priors = Utils.listFromPrimitives(
GenotypeLikelihoods.fromLog10Likelihoods(getDirichletPrior(alleleCounts, vc1.getMaxPloidy(2))).getAsPLs());
return new VariantContextBuilder(vc1).genotypes(newContext).attribute("PG",priors).make();
}
/**
* Given genotype likelihoods and known allele counts, calculate the posterior likelihoods
* over the genotype states
* @param genotypeLikelihoods - the genotype likelihoods for the individual
* @param knownAlleleCountsByAllele - the known allele counts in the population. For AC=2 AN=12 site, this is {10,2}
* @param ploidy - the ploidy to assume
* @return - the posterior genotype likelihoods
*/
protected static List<double[]> calculatePosteriorGLs(final List<double[]> genotypeLikelihoods,
final double[] knownAlleleCountsByAllele,
final int ploidy) {
if ( ploidy != 2 ) {
throw new IllegalStateException("Genotype posteriors not yet implemented for ploidy != 2");
}
final double[] genotypePriorByAllele = getDirichletPrior(knownAlleleCountsByAllele,ploidy);
final List<double[]> posteriors = new ArrayList<>(genotypeLikelihoods.size());
for ( final double[] likelihoods : genotypeLikelihoods ) {
double[] posteriorLikelihoods = null;
if ( likelihoods != null ) {
if ( likelihoods.length != genotypePriorByAllele.length ) {
throw new IllegalStateException(String.format("Likelihoods not of correct size: expected %d, observed %d",
knownAlleleCountsByAllele.length*(knownAlleleCountsByAllele.length+1)/2,likelihoods.length));
}
posteriorLikelihoods = new double[genotypePriorByAllele.length];
for ( int genoIdx = 0; genoIdx < likelihoods.length; genoIdx ++ ) {
posteriorLikelihoods[genoIdx] = likelihoods[genoIdx] + genotypePriorByAllele[genoIdx];
}
posteriorLikelihoods = MathUtils.toLog10(MathUtils.normalizeFromLog10(posteriorLikelihoods));
}
posteriors.add(posteriorLikelihoods);
}
return posteriors;
}
// convenience function for a single genotypelikelihoods array. Just wraps.
protected static double[] calculatePosteriorGLs(final double[] genotypeLikelihoods,
final double[] knownAlleleCountsByAllele,
final int ploidy) {
return calculatePosteriorGLs(Arrays.asList(genotypeLikelihoods),knownAlleleCountsByAllele,ploidy).get(0);
}
/**
* Given known allele counts (whether external, from the sample, or both), calculate the prior distribution
* over genotype states. This assumes
* 1) Random sampling of alleles (known counts are unbiased, and frequency estimate is Dirichlet)
* 2) Genotype states are independent (Hardy-Weinberg)
* These assumptions give rise to a Dirichlet-Multinomial distribution of genotype states as a prior
* (the "number of trials" for the multinomial is simply the ploidy)
* @param knownCountsByAllele - the known counts per allele. For an AC=2, AN=12 site this is {10,2}
* @param ploidy - the number of chromosomes in the sample. For now restricted to 2.
* @return - the Dirichlet-Multinomial distribution over genotype states
*/
protected static double[] getDirichletPrior(final double[] knownCountsByAllele, final int ploidy) {
if ( ploidy != 2 ) {
throw new IllegalStateException("Genotype priors not yet implemented for ploidy != 2");
}
// multi-allelic format is
// AA AB BB AC BC CC AD BD CD DD ...
final double sumOfKnownCounts = MathUtils.sum(knownCountsByAllele);
final double[] priors = new double[knownCountsByAllele.length*(knownCountsByAllele.length+1)/2];
int priorIndex = 0;
for ( int allele2 = 0; allele2 < knownCountsByAllele.length; allele2++ ) {
for ( int allele1 = 0; allele1 <= allele2; allele1++) {
final int[] counts = new int[knownCountsByAllele.length];
counts[allele1] += 1;
counts[allele2] += 1;
priors[priorIndex++] = MathUtils.dirichletMultinomial(knownCountsByAllele,sumOfKnownCounts,counts,ploidy);
}
}
return priors;
}
private static void addAlleleCounts(final Map<Allele,Integer> counts, final VariantContext context, final boolean useAC) {
final int[] ac;
if ( context.hasAttribute(VCFConstants.MLE_ALLELE_COUNT_KEY) && ! useAC ) {
ac = extractInts(context.getAttribute(VCFConstants.MLE_ALLELE_COUNT_KEY));
} else if ( context.hasAttribute(VCFConstants.ALLELE_COUNT_KEY) ) {
ac = extractInts(context.getAttribute(VCFConstants.ALLELE_COUNT_KEY));
} else {
ac = new int[context.getAlternateAlleles().size()];
int idx = 0;
for ( final Allele allele : context.getAlternateAlleles() ) {
ac[idx++] = context.getCalledChrCount(allele);
}
}
for ( final Allele allele : context.getAlleles() ) {
final int count;
if ( allele.isReference() ) {
if ( context.hasAttribute(VCFConstants.ALLELE_NUMBER_KEY) ) {
count = context.getAttributeAsInt(VCFConstants.ALLELE_NUMBER_KEY,-1) - (int) MathUtils.sum(ac);
} else {
count = context.getCalledChrCount() - (int) MathUtils.sum(ac);
}
} else {
count = ac[context.getAlternateAlleles().indexOf(allele)];
}
if ( ! counts.containsKey(allele) ) {
counts.put(allele,0);
}
counts.put(allele,count + counts.get(allele));
}
}
public static int[] extractInts(final Object integerListContainingVCField) {
List<Integer> mleList = null;
if ( integerListContainingVCField instanceof List ) {
if ( ((List) integerListContainingVCField).get(0) instanceof String ) {
mleList = new ArrayList<>(((List) integerListContainingVCField).size());
for ( Object s : ((List)integerListContainingVCField)) {
mleList.add(Integer.parseInt((String) s));
}
} else {
mleList = (List<Integer>) integerListContainingVCField;
}
} else if ( integerListContainingVCField instanceof Integer ) {
mleList = Arrays.asList((Integer) integerListContainingVCField);
} else if ( integerListContainingVCField instanceof String ) {
mleList = Arrays.asList(Integer.parseInt((String)integerListContainingVCField));
}
if ( mleList == null )
throw new IllegalArgumentException(String.format("VCF does not have properly formatted "+
VCFConstants.MLE_ALLELE_COUNT_KEY+" or "+VCFConstants.ALLELE_COUNT_KEY));
final int[] mle = new int[mleList.size()];
if ( ! ( mleList.get(0) instanceof Integer ) ) {
throw new IllegalStateException("BUG: The AC values should be an Integer, but was "+mleList.get(0).getClass().getCanonicalName());
}
for ( int idx = 0; idx < mle.length; idx++) {
mle[idx] = mleList.get(idx);
}
return mle;
}
}

View File

@ -0,0 +1,264 @@
/*
* By downloading the PROGRAM you agree to the following terms of use:
*
* BROAD INSTITUTE - SOFTWARE LICENSE AGREEMENT - FOR ACADEMIC NON-COMMERCIAL RESEARCH PURPOSES ONLY
*
* This Agreement is made between the Broad Institute, Inc. with a principal address at 7 Cambridge Center, Cambridge, MA 02142 (BROAD) and the LICENSEE and is effective at the date the downloading is completed (EFFECTIVE DATE).
*
* WHEREAS, LICENSEE desires to license the PROGRAM, as defined hereinafter, and BROAD wishes to have this PROGRAM utilized in the public interest, subject only to the royalty-free, nonexclusive, nontransferable license rights of the United States Government pursuant to 48 CFR 52.227-14; and
* WHEREAS, LICENSEE desires to license the PROGRAM and BROAD desires to grant a license on the following terms and conditions.
* NOW, THEREFORE, in consideration of the promises and covenants made herein, the parties hereto agree as follows:
*
* 1. DEFINITIONS
* 1.1 PROGRAM shall mean copyright in the object code and source code known as GATK2 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute/GATK on the EFFECTIVE DATE.
*
* 2. LICENSE
* 2.1 Grant. Subject to the terms of this Agreement, BROAD hereby grants to LICENSEE, solely for academic non-commercial research purposes, a non-exclusive, non-transferable license to: (a) download, execute and display the PROGRAM and (b) create bug fixes and modify the PROGRAM.
* The LICENSEE may apply the PROGRAM in a pipeline to data owned by users other than the LICENSEE and provide these users the results of the PROGRAM provided LICENSEE does so for academic non-commercial purposes only. For clarification purposes, academic sponsored research is not a commercial use under the terms of this Agreement.
* 2.2 No Sublicensing or Additional Rights. LICENSEE shall not sublicense or distribute the PROGRAM, in whole or in part, without prior written permission from BROAD. LICENSEE shall ensure that all of its users agree to the terms of this Agreement. LICENSEE further agrees that it shall not put the PROGRAM on a network, server, or other similar technology that may be accessed by anyone other than the LICENSEE and its employees and users who have agreed to the terms of this agreement.
* 2.3 License Limitations. Nothing in this Agreement shall be construed to confer any rights upon LICENSEE by implication, estoppel, or otherwise to any computer software, trademark, intellectual property, or patent rights of BROAD, or of any other entity, except as expressly granted herein. LICENSEE agrees that the PROGRAM, in whole or part, shall not be used for any commercial purpose, including without limitation, as the basis of a commercial software or hardware product or to provide services. LICENSEE further agrees that the PROGRAM shall not be copied or otherwise adapted in order to circumvent the need for obtaining a license for use of the PROGRAM.
*
* 3. OWNERSHIP OF INTELLECTUAL PROPERTY
* LICENSEE acknowledges that title to the PROGRAM shall remain with BROAD. The PROGRAM is marked with the following BROAD copyright notice and notice of attribution to contributors. LICENSEE shall retain such notice on all copies. LICENSEE agrees to include appropriate attribution if any results obtained from use of the PROGRAM are included in any publication.
* Copyright 2012 Broad Institute, Inc.
* Notice of attribution: The GATK2 program was made available through the generosity of Medical and Population Genetics program at the Broad Institute, Inc.
* LICENSEE shall not use any trademark or trade name of BROAD, or any variation, adaptation, or abbreviation, of such marks or trade names, or any names of officers, faculty, students, employees, or agents of BROAD except as states above for attribution purposes.
*
* 4. INDEMNIFICATION
* LICENSEE shall indemnify, defend, and hold harmless BROAD, and their respective officers, faculty, students, employees, associated investigators and agents, and their respective successors, heirs and assigns, (Indemnitees), against any liability, damage, loss, or expense (including reasonable attorneys fees and expenses) incurred by or imposed upon any of the Indemnitees in connection with any claims, suits, actions, demands or judgments arising out of any theory of liability (including, without limitation, actions in the form of tort, warranty, or strict liability and regardless of whether such action has any factual basis) pursuant to any right or license granted under this Agreement.
*
* 5. NO REPRESENTATIONS OR WARRANTIES
* THE PROGRAM IS DELIVERED AS IS. BROAD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE PROGRAM OR THE COPYRIGHT, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. BROAD EXTENDS NO WARRANTIES OF ANY KIND AS TO PROGRAM CONFORMITY WITH WHATEVER USER MANUALS OR OTHER LITERATURE MAY BE ISSUED FROM TIME TO TIME.
* IN NO EVENT SHALL BROAD OR ITS RESPECTIVE DIRECTORS, OFFICERS, EMPLOYEES, AFFILIATED INVESTIGATORS AND AFFILIATES BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING, WITHOUT LIMITATION, ECONOMIC DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER BROAD SHALL BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE FOREGOING.
*
* 6. ASSIGNMENT
* This Agreement is personal to LICENSEE and any rights or obligations assigned by LICENSEE without the prior written consent of BROAD shall be null and void.
*
* 7. MISCELLANEOUS
* 7.1 Export Control. LICENSEE gives assurance that it will comply with all United States export control laws and regulations controlling the export of the PROGRAM, including, without limitation, all Export Administration Regulations of the United States Department of Commerce. Among other things, these laws and regulations prohibit, or require a license for, the export of certain types of software to specified countries.
* 7.2 Termination. LICENSEE shall have the right to terminate this Agreement for any reason upon prior written notice to BROAD. If LICENSEE breaches any provision hereunder, and fails to cure such breach within thirty (30) days, BROAD may terminate this Agreement immediately. Upon termination, LICENSEE shall provide BROAD with written assurance that the original and all copies of the PROGRAM have been destroyed, except that, upon prior written authorization from BROAD, LICENSEE may retain a copy for archive purposes.
* 7.3 Survival. The following provisions shall survive the expiration or termination of this Agreement: Articles 1, 3, 4, 5 and Sections 2.2, 2.3, 7.3, and 7.4.
* 7.4 Notice. Any notices under this Agreement shall be in writing, shall specifically refer to this Agreement, and shall be sent by hand, recognized national overnight courier, confirmed facsimile transmission, confirmed electronic mail, or registered or certified mail, postage prepaid, return receipt requested. All notices under this Agreement shall be deemed effective upon receipt.
* 7.5 Amendment and Waiver; Entire Agreement. This Agreement may be amended, supplemented, or otherwise modified only by means of a written instrument signed by all parties. Any waiver of any rights or failure to act in a specific instance shall relate only to such instance and shall not be construed as an agreement to waive any rights or fail to act in any other instance, whether or not similar. This Agreement constitutes the entire agreement among the parties with respect to its subject matter and supersedes prior agreements or understandings between the parties relating to its subject matter.
* 7.6 Binding Effect; Headings. This Agreement shall be binding upon and inure to the benefit of the parties and their respective permitted successors and assigns. All headings are for convenience only and shall not affect the meaning of any provision of this Agreement.
* 7.7 Governing Law. This Agreement shall be construed, governed, interpreted and applied in accordance with the internal laws of the Commonwealth of Massachusetts, U.S.A., without regard to conflict of laws principles.
*/
package org.broadinstitute.sting.gatk.walkers.variantutils;
import org.broadinstitute.sting.commandline.ArgumentCollection;
import org.broadinstitute.sting.commandline.Input;
import org.broadinstitute.sting.commandline.Output;
import org.broadinstitute.sting.commandline.RodBinding;
import org.broadinstitute.sting.gatk.arguments.StandardVariantContextInputArgumentCollection;
import org.broadinstitute.sting.gatk.contexts.AlignmentContext;
import org.broadinstitute.sting.gatk.contexts.ReferenceContext;
import org.broadinstitute.sting.gatk.refdata.RefMetaDataTracker;
import org.broadinstitute.sting.gatk.walkers.RodWalker;
import org.broadinstitute.sting.gatk.walkers.genotyper.UnifiedGenotyperEngine;
import org.broadinstitute.sting.gatk.walkers.haplotypecaller.PairHMMLikelihoodCalculationEngine;
import org.broadinstitute.sting.utils.SampleUtils;
import org.broadinstitute.sting.utils.exceptions.UserException;
import org.broadinstitute.sting.utils.variant.GATKVCFUtils;
import org.broadinstitute.sting.utils.variant.GATKVariantContextUtils;
import org.broadinstitute.variant.variantcontext.VariantContext;
import org.broadinstitute.variant.variantcontext.writer.VariantContextWriter;
import org.broadinstitute.sting.commandline.*;
import org.broadinstitute.variant.vcf.*;
import java.util.*;
/**
* Calculates genotype posterior likelihoods given panel data
*
* <p>
* Given a VCF with genotype likelihoods from the HaplotypeCaller, UnifiedGenotyper, or another source which provides
* -unbiased- GLs, calculate the posterior genotype state and likelihood given allele frequency information from
* both the samples themselves and input VCFs describing allele frequencies in related populations.
*
* VCFs to use for informing the genotype likelihoods (e.g. a population-specific VCF from 1000 genomes) should have
* at least one of:
* - AC field and AN field
* - MLEAC field and AN field
* - genotypes
*
* The AF field will not be used in this calculation as it does not provide a way to estimate the confidence interval
* or uncertainty around the allele frequency, while AN provides this necessary information. This uncertainty is
* modeled by a Dirichlet distribution: that is, the frequency is known up to a Dirichlet distribution with
* parameters AC1+q,AC2+q,...,(AN-AC1-AC2-...)+q, where "q" is the global frequency prior (typically q << 1). The
* genotype priors applied then follow a Dirichlet-Multinomial distribution, where 2 alleles per sample are drawn
* independently. This assumption of independent draws is the assumption Hardy-Weinberg Equilibrium. Thus, HWE is
* imposed on the likelihoods as a result of CalculateGenotypePosteriors.
*
* <h3>Input</h3>
* <p>
* A VCF with genotype likelihoods, and optionally genotypes, AC/AN fields, or MLEAC/AN fields
* </p>
*
* <p>
* A collection of VCFs to use for informing allele frequency priors. Each VCF must have one of
* - AC field and AN field
* - MLEAC field and AN field
* - genotypes
* </p>
*
* <h3>Output</h3>
* <p>
* A new VCF with:
* 1) Genotype posteriors added to the genotype fields ("GP")
* 2) Genotypes and GQ assigned according to these posteriors
* 3) Per-site genotype priors added to the INFO field ("PG")
* </p>
*
* <h3>Examples</h3>
* <pre>
* Inform the genotype assignment of NA12878 using the 1000G Euro panel
* java -Xmx2g -jar GenomeAnalysisTK.jar \
* -R ref.fasta \
* -T CalculateGenotypePosteriors \
* -V NA12878.wgs.HC.vcf \
* -VV 1000G_EUR.genotypes.combined.vcf \
* -o NA12878.wgs.HC.posteriors.vcf \
*
* Refine the genotypes of a large panel based on the discovered allele frequency
* java -Xmx2g -jar GenomeAnalysisTK.jar \
* -R ref.fasta \
* -T CalculateGenotypePosteriors \
* -V input.vcf \
* -o output.withPosteriors.vcf
*
* Apply frequency and HWE-based priors to the genotypes of a family without including the family allele counts
* in the allele frequency estimates
* java -Xmx2g -jar GenomeAnalysisTK.jar \
* -R ref.fasta \
* -T CalculateGenotypePosteriors \
* -V input.vcf \
* -o output.withPosteriors.vcf \
* --ignoreInputSamples
*
* Calculate the posterior genotypes of a callset, and impose that a variant *not seen* in the external panel
* is tantamount to being AC=0, AN=100 within that panel
* java -Xmx2g -jar GenomeAnalysisTK.jar \
* -R ref.fasta \
* -T CalculateGenotypePosteriors \
* -VV external.panel.vcf \
* -V input.vcf \
* -o output.withPosteriors.vcf
* --numRefSamplesIfNoCall 100
*
* </pre>
*
*/
public class CalculateGenotypePosteriors extends RodWalker<Integer,Integer> {
/**
* The input VCF (posteriors will be calculated for these samples, and written to the output)
*/
@ArgumentCollection
protected StandardVariantContextInputArgumentCollection variantCollection = new StandardVariantContextInputArgumentCollection();
/**
* Supporting external panels. Allele counts from these panels (taken from AC,AN or MLEAC,AN or raw genotypes) will
* be used to inform the frequency distribution underying the genotype priors.
*/
@Input(fullName="supporting", shortName = "VV", doc="Other callsets to use in generating genotype posteriors", required=false)
public List<RodBinding<VariantContext>> supportVariants = new ArrayList<RodBinding<VariantContext>>();
/**
* The global prior of a variant site -- i.e. the expected allele frequency distribution knowing only that N alleles
* exist, and having observed none of them. This is the "typical" 1/x trend, modeled here as not varying
* across alleles. The calculation for this parameter is (Effective population size) * (steady state mutation rate)
*
*/
@Argument(fullName="globalPrior",shortName="G",doc="The global Dirichlet prior parameters for the allele frequency",required=false)
public double globalPrior = UnifiedGenotyperEngine.HUMAN_SNP_HETEROZYGOSITY;
/**
* When a variant is not seen in a panel, whether to infer (and with what effective strength) that only reference
* alleles were ascertained at that site. E.g. "If not seen in 1000Genomes, treat it as AC=0, AN=2000". This is
* applied across all external panels, so if numRefIsMissing = 10, and the variant is absent in two panels, this
* confers evidence of AC=0,AN=20
*/
@Argument(fullName="numRefSamplesIfNoCall",shortName="nrs",doc="The number of homozygous reference to infer were " +
"seen at a position where an \"other callset\" contains no site or genotype information",required=false)
public int numRefIfMissing = 1;
/**
* Rather than looking for the MLEAC field first, and then falling back to AC; first look for the AC field and then
* fall back to MLEAC or raw genotypes
*/
@Argument(fullName="defaultToAC",shortName="useAC",doc="Use the AC field as opposed to MLEAC. Does nothing if VCF lacks MLEAC field",required=false)
public boolean defaultToAC = false;
/**
* Do not use the [MLE] allele count from the input samples (the ones for which you're calculating posteriors)
* in the site frequency distribution; only use the AC and AN calculated from external sources.
*/
@Argument(fullName="ignoreInputSamples",shortName="ext",doc="Use external information only; do not inform genotype priors by "+
"the discovered allele frequency in the callset whose posteriors are being calculated. Useful for callsets containing "+
"related individuals.",required=false)
public boolean ignoreInputSamples = false;
@Output(doc="File to which variants should be written")
protected VariantContextWriter vcfWriter = null;
private final boolean NO_EM = false;
public void initialize() {
// Get list of samples to include in the output
final List<String> rodNames = Arrays.asList(variantCollection.variants.getName());
final Map<String,VCFHeader> vcfRods = GATKVCFUtils.getVCFHeadersFromRods(getToolkit(), rodNames);
if ( vcfRods.size() > 1 )
throw new IllegalStateException("Somehow more than one variant was bound?");
final VCFHeader header = new ArrayList<>(vcfRods.values()).get(0); // pure laziness
if ( ! header.hasGenotypingData() ) {
throw new UserException("VCF has no genotypes");
}
if ( header.hasInfoLine(VCFConstants.MLE_ALLELE_COUNT_KEY) ) {
final VCFInfoHeaderLine mleLine = header.getInfoHeaderLine(VCFConstants.MLE_ALLELE_COUNT_KEY);
if ( mleLine.getCountType() != VCFHeaderLineCount.A ) {
throw new UserException("VCF does not have a properly formatted MLEAC field: the count type should be \"A\"");
}
if ( mleLine.getType() != VCFHeaderLineType.Integer ) {
throw new UserException("VCF does not have a properly formatted MLEAC field: the field type should be \"Integer\"");
}
}
final TreeSet<String> vcfSamples = new TreeSet<>(SampleUtils.getSampleList(vcfRods, GATKVariantContextUtils.GenotypeMergeType.REQUIRE_UNIQUE));
// Initialize VCF header
final Set<VCFHeaderLine> headerLines = VCFUtils.smartMergeHeaders(vcfRods.values(), true);
headerLines.add(new VCFFormatHeaderLine(VCFConstants.GENOTYPE_POSTERIORS_KEY, VCFHeaderLineCount.G, VCFHeaderLineType.Integer, "Posterior Genotype Likelihoods"));
headerLines.add(new VCFInfoHeaderLine("PG", VCFHeaderLineCount.G, VCFHeaderLineType.Integer, "Genotype Likelihood Prior"));
headerLines.add(new VCFHeaderLine("source", "CalculateGenotypePosteriors"));
vcfWriter.writeHeader(new VCFHeader(headerLines, vcfSamples));
}
public Integer reduceInit() { return 0; }
public Integer map(RefMetaDataTracker tracker, ReferenceContext ref, AlignmentContext context) {
if ( tracker == null || context == null || ref == null ) {
return 0;
}
final Collection<VariantContext> vcs = tracker.getValues(variantCollection.variants, ref.getLocus());
final Collection<VariantContext> otherVCs = tracker.getValues(supportVariants, context.getLocation());
final int missing = supportVariants.size() - otherVCs.size();
for ( VariantContext vc : vcs ) {
vcfWriter.add(PosteriorLikelihoodsUtils.calculatePosteriorGLs(vc, otherVCs, missing * numRefIfMissing, globalPrior, !ignoreInputSamples, NO_EM, defaultToAC));
}
return 1;
}
public Integer reduce(Integer l, Integer r) { return r + l; }
}

View File

@ -0,0 +1,261 @@
/*
* By downloading the PROGRAM you agree to the following terms of use:
*
* BROAD INSTITUTE - SOFTWARE LICENSE AGREEMENT - FOR ACADEMIC NON-COMMERCIAL RESEARCH PURPOSES ONLY
*
* This Agreement is made between the Broad Institute, Inc. with a principal address at 7 Cambridge Center, Cambridge, MA 02142 (BROAD) and the LICENSEE and is effective at the date the downloading is completed (EFFECTIVE DATE).
*
* WHEREAS, LICENSEE desires to license the PROGRAM, as defined hereinafter, and BROAD wishes to have this PROGRAM utilized in the public interest, subject only to the royalty-free, nonexclusive, nontransferable license rights of the United States Government pursuant to 48 CFR 52.227-14; and
* WHEREAS, LICENSEE desires to license the PROGRAM and BROAD desires to grant a license on the following terms and conditions.
* NOW, THEREFORE, in consideration of the promises and covenants made herein, the parties hereto agree as follows:
*
* 1. DEFINITIONS
* 1.1 PROGRAM shall mean copyright in the object code and source code known as GATK2 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute/GATK on the EFFECTIVE DATE.
*
* 2. LICENSE
* 2.1 Grant. Subject to the terms of this Agreement, BROAD hereby grants to LICENSEE, solely for academic non-commercial research purposes, a non-exclusive, non-transferable license to: (a) download, execute and display the PROGRAM and (b) create bug fixes and modify the PROGRAM.
* The LICENSEE may apply the PROGRAM in a pipeline to data owned by users other than the LICENSEE and provide these users the results of the PROGRAM provided LICENSEE does so for academic non-commercial purposes only. For clarification purposes, academic sponsored research is not a commercial use under the terms of this Agreement.
* 2.2 No Sublicensing or Additional Rights. LICENSEE shall not sublicense or distribute the PROGRAM, in whole or in part, without prior written permission from BROAD. LICENSEE shall ensure that all of its users agree to the terms of this Agreement. LICENSEE further agrees that it shall not put the PROGRAM on a network, server, or other similar technology that may be accessed by anyone other than the LICENSEE and its employees and users who have agreed to the terms of this agreement.
* 2.3 License Limitations. Nothing in this Agreement shall be construed to confer any rights upon LICENSEE by implication, estoppel, or otherwise to any computer software, trademark, intellectual property, or patent rights of BROAD, or of any other entity, except as expressly granted herein. LICENSEE agrees that the PROGRAM, in whole or part, shall not be used for any commercial purpose, including without limitation, as the basis of a commercial software or hardware product or to provide services. LICENSEE further agrees that the PROGRAM shall not be copied or otherwise adapted in order to circumvent the need for obtaining a license for use of the PROGRAM.
*
* 3. OWNERSHIP OF INTELLECTUAL PROPERTY
* LICENSEE acknowledges that title to the PROGRAM shall remain with BROAD. The PROGRAM is marked with the following BROAD copyright notice and notice of attribution to contributors. LICENSEE shall retain such notice on all copies. LICENSEE agrees to include appropriate attribution if any results obtained from use of the PROGRAM are included in any publication.
* Copyright 2012 Broad Institute, Inc.
* Notice of attribution: The GATK2 program was made available through the generosity of Medical and Population Genetics program at the Broad Institute, Inc.
* LICENSEE shall not use any trademark or trade name of BROAD, or any variation, adaptation, or abbreviation, of such marks or trade names, or any names of officers, faculty, students, employees, or agents of BROAD except as states above for attribution purposes.
*
* 4. INDEMNIFICATION
* LICENSEE shall indemnify, defend, and hold harmless BROAD, and their respective officers, faculty, students, employees, associated investigators and agents, and their respective successors, heirs and assigns, (Indemnitees), against any liability, damage, loss, or expense (including reasonable attorneys fees and expenses) incurred by or imposed upon any of the Indemnitees in connection with any claims, suits, actions, demands or judgments arising out of any theory of liability (including, without limitation, actions in the form of tort, warranty, or strict liability and regardless of whether such action has any factual basis) pursuant to any right or license granted under this Agreement.
*
* 5. NO REPRESENTATIONS OR WARRANTIES
* THE PROGRAM IS DELIVERED AS IS. BROAD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE PROGRAM OR THE COPYRIGHT, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. BROAD EXTENDS NO WARRANTIES OF ANY KIND AS TO PROGRAM CONFORMITY WITH WHATEVER USER MANUALS OR OTHER LITERATURE MAY BE ISSUED FROM TIME TO TIME.
* IN NO EVENT SHALL BROAD OR ITS RESPECTIVE DIRECTORS, OFFICERS, EMPLOYEES, AFFILIATED INVESTIGATORS AND AFFILIATES BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING, WITHOUT LIMITATION, ECONOMIC DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER BROAD SHALL BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE FOREGOING.
*
* 6. ASSIGNMENT
* This Agreement is personal to LICENSEE and any rights or obligations assigned by LICENSEE without the prior written consent of BROAD shall be null and void.
*
* 7. MISCELLANEOUS
* 7.1 Export Control. LICENSEE gives assurance that it will comply with all United States export control laws and regulations controlling the export of the PROGRAM, including, without limitation, all Export Administration Regulations of the United States Department of Commerce. Among other things, these laws and regulations prohibit, or require a license for, the export of certain types of software to specified countries.
* 7.2 Termination. LICENSEE shall have the right to terminate this Agreement for any reason upon prior written notice to BROAD. If LICENSEE breaches any provision hereunder, and fails to cure such breach within thirty (30) days, BROAD may terminate this Agreement immediately. Upon termination, LICENSEE shall provide BROAD with written assurance that the original and all copies of the PROGRAM have been destroyed, except that, upon prior written authorization from BROAD, LICENSEE may retain a copy for archive purposes.
* 7.3 Survival. The following provisions shall survive the expiration or termination of this Agreement: Articles 1, 3, 4, 5 and Sections 2.2, 2.3, 7.3, and 7.4.
* 7.4 Notice. Any notices under this Agreement shall be in writing, shall specifically refer to this Agreement, and shall be sent by hand, recognized national overnight courier, confirmed facsimile transmission, confirmed electronic mail, or registered or certified mail, postage prepaid, return receipt requested. All notices under this Agreement shall be deemed effective upon receipt.
* 7.5 Amendment and Waiver; Entire Agreement. This Agreement may be amended, supplemented, or otherwise modified only by means of a written instrument signed by all parties. Any waiver of any rights or failure to act in a specific instance shall relate only to such instance and shall not be construed as an agreement to waive any rights or fail to act in any other instance, whether or not similar. This Agreement constitutes the entire agreement among the parties with respect to its subject matter and supersedes prior agreements or understandings between the parties relating to its subject matter.
* 7.6 Binding Effect; Headings. This Agreement shall be binding upon and inure to the benefit of the parties and their respective permitted successors and assigns. All headings are for convenience only and shall not affect the meaning of any provision of this Agreement.
* 7.7 Governing Law. This Agreement shall be construed, governed, interpreted and applied in accordance with the internal laws of the Commonwealth of Massachusetts, U.S.A., without regard to conflict of laws principles.
*/
package org.broadinstitute.sting.gatk.walkers.variantutils;
import org.broadinstitute.sting.utils.MathUtils;
import org.broadinstitute.sting.utils.Utils;
import org.broadinstitute.sting.utils.variant.GATKVariantContextUtils;
import org.broadinstitute.variant.variantcontext.*;
import org.broadinstitute.variant.vcf.VCFConstants;
import java.util.*;
public class PosteriorLikelihoodsUtils {
public static VariantContext calculatePosteriorGLs(final VariantContext vc1,
final Collection<VariantContext> resources,
final int numRefSamplesFromMissingResources,
final double globalFrequencyPriorDirichlet,
final boolean useInputSamples,
final boolean useEM,
final boolean useAC) {
if ( useEM )
throw new IllegalArgumentException("EM loop for posterior GLs not yet implemented");
final Map<Allele,Integer> totalAlleleCounts = new HashMap<>();
for ( final VariantContext resource : resources ) {
addAlleleCounts(totalAlleleCounts,resource,useAC);
}
if ( useInputSamples ) {
addAlleleCounts(totalAlleleCounts,vc1,useAC);
}
totalAlleleCounts.put(vc1.getReference(),totalAlleleCounts.get(vc1.getReference())+numRefSamplesFromMissingResources);
// now extract the counts of the alleles present within vc1, and in order
final double[] alleleCounts = new double[vc1.getNAlleles()];
int alleleIndex = 0;
for ( final Allele allele : vc1.getAlleles() ) {
alleleCounts[alleleIndex++] = globalFrequencyPriorDirichlet + ( totalAlleleCounts.containsKey(allele) ?
totalAlleleCounts.get(allele) : 0 );
}
final List<double[]> likelihoods = new ArrayList<>(vc1.getNSamples());
for ( final Genotype genotype : vc1.getGenotypes() ) {
likelihoods.add(genotype.hasLikelihoods() ? genotype.getLikelihoods().getAsVector() : null );
}
final List<double[]> posteriors = calculatePosteriorGLs(likelihoods,alleleCounts,vc1.getMaxPloidy(2));
final GenotypesContext newContext = GenotypesContext.create();
for ( int genoIdx = 0; genoIdx < vc1.getNSamples(); genoIdx ++ ) {
final GenotypeBuilder builder = new GenotypeBuilder(vc1.getGenotype(genoIdx));
if ( posteriors.get(genoIdx) != null ) {
GATKVariantContextUtils.updateGenotypeAfterSubsetting(vc1.getAlleles(), builder,
GATKVariantContextUtils.GenotypeAssignmentMethod.USE_PLS_TO_ASSIGN, posteriors.get(genoIdx), vc1.getAlleles());
builder.attribute(VCFConstants.GENOTYPE_POSTERIORS_KEY,
Utils.listFromPrimitives(GenotypeLikelihoods.fromLog10Likelihoods(posteriors.get(genoIdx)).getAsPLs()));
}
newContext.add(builder.make());
}
final List<Integer> priors = Utils.listFromPrimitives(
GenotypeLikelihoods.fromLog10Likelihoods(getDirichletPrior(alleleCounts, vc1.getMaxPloidy(2))).getAsPLs());
return new VariantContextBuilder(vc1).genotypes(newContext).attribute("PG",priors).make();
}
/**
* Given genotype likelihoods and known allele counts, calculate the posterior likelihoods
* over the genotype states
* @param genotypeLikelihoods - the genotype likelihoods for the individual
* @param knownAlleleCountsByAllele - the known allele counts in the population. For AC=2 AN=12 site, this is {10,2}
* @param ploidy - the ploidy to assume
* @return - the posterior genotype likelihoods
*/
protected static List<double[]> calculatePosteriorGLs(final List<double[]> genotypeLikelihoods,
final double[] knownAlleleCountsByAllele,
final int ploidy) {
if ( ploidy != 2 ) {
throw new IllegalStateException("Genotype posteriors not yet implemented for ploidy != 2");
}
final double[] genotypePriorByAllele = getDirichletPrior(knownAlleleCountsByAllele,ploidy);
final List<double[]> posteriors = new ArrayList<>(genotypeLikelihoods.size());
for ( final double[] likelihoods : genotypeLikelihoods ) {
double[] posteriorLikelihoods = null;
if ( likelihoods != null ) {
if ( likelihoods.length != genotypePriorByAllele.length ) {
throw new IllegalStateException(String.format("Likelihoods not of correct size: expected %d, observed %d",
knownAlleleCountsByAllele.length*(knownAlleleCountsByAllele.length+1)/2,likelihoods.length));
}
posteriorLikelihoods = new double[genotypePriorByAllele.length];
for ( int genoIdx = 0; genoIdx < likelihoods.length; genoIdx ++ ) {
posteriorLikelihoods[genoIdx] = likelihoods[genoIdx] + genotypePriorByAllele[genoIdx];
}
posteriorLikelihoods = MathUtils.toLog10(MathUtils.normalizeFromLog10(posteriorLikelihoods));
}
posteriors.add(posteriorLikelihoods);
}
return posteriors;
}
// convenience function for a single genotypelikelihoods array. Just wraps.
protected static double[] calculatePosteriorGLs(final double[] genotypeLikelihoods,
final double[] knownAlleleCountsByAllele,
final int ploidy) {
return calculatePosteriorGLs(Arrays.asList(genotypeLikelihoods),knownAlleleCountsByAllele,ploidy).get(0);
}
/**
* Given known allele counts (whether external, from the sample, or both), calculate the prior distribution
* over genotype states. This assumes
* 1) Random sampling of alleles (known counts are unbiased, and frequency estimate is Dirichlet)
* 2) Genotype states are independent (Hardy-Weinberg)
* These assumptions give rise to a Dirichlet-Multinomial distribution of genotype states as a prior
* (the "number of trials" for the multinomial is simply the ploidy)
* @param knownCountsByAllele - the known counts per allele. For an AC=2, AN=12 site this is {10,2}
* @param ploidy - the number of chromosomes in the sample. For now restricted to 2.
* @return - the Dirichlet-Multinomial distribution over genotype states
*/
protected static double[] getDirichletPrior(final double[] knownCountsByAllele, final int ploidy) {
if ( ploidy != 2 ) {
throw new IllegalStateException("Genotype priors not yet implemented for ploidy != 2");
}
// multi-allelic format is
// AA AB BB AC BC CC AD BD CD DD ...
final double sumOfKnownCounts = MathUtils.sum(knownCountsByAllele);
final double[] priors = new double[knownCountsByAllele.length*(knownCountsByAllele.length+1)/2];
int priorIndex = 0;
for ( int allele2 = 0; allele2 < knownCountsByAllele.length; allele2++ ) {
for ( int allele1 = 0; allele1 <= allele2; allele1++) {
final int[] counts = new int[knownCountsByAllele.length];
counts[allele1] += 1;
counts[allele2] += 1;
priors[priorIndex++] = MathUtils.dirichletMultinomial(knownCountsByAllele,sumOfKnownCounts,counts,ploidy);
}
}
return priors;
}
private static void addAlleleCounts(final Map<Allele,Integer> counts, final VariantContext context, final boolean useAC) {
final int[] ac;
if ( context.hasAttribute(VCFConstants.MLE_ALLELE_COUNT_KEY) && ! useAC ) {
ac = extractInts(context.getAttribute(VCFConstants.MLE_ALLELE_COUNT_KEY));
} else if ( context.hasAttribute(VCFConstants.ALLELE_COUNT_KEY) ) {
ac = extractInts(context.getAttribute(VCFConstants.ALLELE_COUNT_KEY));
} else {
ac = new int[context.getAlternateAlleles().size()];
int idx = 0;
for ( final Allele allele : context.getAlternateAlleles() ) {
ac[idx++] = context.getCalledChrCount(allele);
}
}
for ( final Allele allele : context.getAlleles() ) {
final int count;
if ( allele.isReference() ) {
if ( context.hasAttribute(VCFConstants.ALLELE_NUMBER_KEY) ) {
count = context.getAttributeAsInt(VCFConstants.ALLELE_NUMBER_KEY,-1) - (int) MathUtils.sum(ac);
} else {
count = context.getCalledChrCount() - (int) MathUtils.sum(ac);
}
} else {
count = ac[context.getAlternateAlleles().indexOf(allele)];
}
if ( ! counts.containsKey(allele) ) {
counts.put(allele,0);
}
counts.put(allele,count + counts.get(allele));
}
}
public static int[] extractInts(final Object integerListContainingVCField) {
List<Integer> mleList = null;
if ( integerListContainingVCField instanceof List ) {
if ( ((List) integerListContainingVCField).get(0) instanceof String ) {
mleList = new ArrayList<>(((List) integerListContainingVCField).size());
for ( Object s : ((List)integerListContainingVCField)) {
mleList.add(Integer.parseInt((String) s));
}
} else {
mleList = (List<Integer>) integerListContainingVCField;
}
} else if ( integerListContainingVCField instanceof Integer ) {
mleList = Arrays.asList((Integer) integerListContainingVCField);
} else if ( integerListContainingVCField instanceof String ) {
mleList = Arrays.asList(Integer.parseInt((String)integerListContainingVCField));
}
if ( mleList == null )
throw new IllegalArgumentException(String.format("VCF does not have properly formatted "+
VCFConstants.MLE_ALLELE_COUNT_KEY+" or "+VCFConstants.ALLELE_COUNT_KEY));
final int[] mle = new int[mleList.size()];
if ( ! ( mleList.get(0) instanceof Integer ) ) {
throw new IllegalStateException("BUG: The AC values should be an Integer, but was "+mleList.get(0).getClass().getCanonicalName());
}
for ( int idx = 0; idx < mle.length; idx++) {
mle[idx] = mleList.get(idx);
}
return mle;
}
}

View File

@ -59,7 +59,6 @@ import org.broadinstitute.sting.utils.pairhmm.PairHMM;
import org.broadinstitute.sting.utils.recalibration.covariates.RepeatCovariate;
import org.broadinstitute.sting.utils.recalibration.covariates.RepeatLengthCovariate;
import org.broadinstitute.variant.variantcontext.*;
import org.broadinstitute.variant.vcf.VCFConstants;
import org.testng.Assert;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
@ -279,313 +278,4 @@ public class PairHMMLikelihoodCalculationEngineUnitTest extends BaseTest {
}
return true;
}
private String arraysEq(int[] a, int[] b) {
if ( a.length != b.length ) {
return String.format("NEQ: %s | %s",Arrays.toString(a),Arrays.toString(b));
}
for ( int idx = 0; idx < a.length; idx++) {
if ( a[idx] - b[idx] > 1 || b[idx] - a[idx] > 1) {
return String.format("NEQ: %s | %s",Arrays.toString(a),Arrays.toString(b));
}
}
return "";
}
private int[] _mleparse(List<Integer> s) {
int[] mle = new int[s.size()];
for ( int idx = 0; idx < mle.length; idx ++) {
mle[idx] = s.get(idx);
}
return mle;
}
private Genotype makeGwithPLs(String sample, Allele a1, Allele a2, double[] pls) {
Genotype gt = new GenotypeBuilder(sample, Arrays.asList(a1, a2)).PL(pls).make();
if ( pls != null && pls.length > 0 ) {
Assert.assertNotNull(gt.getPL());
Assert.assertTrue(gt.getPL().length > 0);
for ( int i : gt.getPL() ) {
Assert.assertTrue(i >= 0);
}
Assert.assertNotEquals(Arrays.toString(gt.getPL()),"[0]");
}
return gt;
}
private Genotype makeG(String sample, Allele a1, Allele a2) {
return GenotypeBuilder.create(sample, Arrays.asList(a1, a2));
}
private Genotype makeG(String sample, Allele a1, Allele a2, int... pls) {
return new GenotypeBuilder(sample, Arrays.asList(a1, a2)).PL(pls).make();
}
private VariantContext makeVC(String source, List<Allele> alleles, Genotype... genotypes) {
int start = 10;
int stop = start; // alleles.contains(ATC) ? start + 3 : start;
return new VariantContextBuilder(source, "1", start, stop, alleles).genotypes(Arrays.asList(genotypes)).filters(null).make();
}
@Test
private void testCalculatePosteriorNoExternalData() {
VariantContext test1 = makeVC("1",Arrays.asList(Aref,T), makeG("s1",Aref,T,20,0,10),
makeG("s2",T,T,60,40,0),
makeG("s3",Aref,Aref,0,30,90));
test1 = new VariantContextBuilder(test1).attribute(VCFConstants.MLE_ALLELE_COUNT_KEY,3).make();
VariantContext test1result = PairHMMLikelihoodCalculationEngine.calculatePosteriorGLs(test1, new ArrayList<VariantContext>(), 0, 0.001, true, false, false);
Genotype test1exp1 = makeGwithPLs("s1",Aref,T,new double[]{-2.20686, -0.03073215, -1.20686});
Assert.assertTrue(test1exp1.hasPL());
Genotype test1exp2 = makeGwithPLs("s2",T,T,new double[]{-6.000066, -3.823938, -6.557894e-05});
Genotype test1exp3 = makeGwithPLs("s3",Aref,Aref,new double[]{-0.0006510083, -2.824524, -9.000651});
Assert.assertEquals("java.util.ArrayList",test1result.getGenotype(0).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY).getClass().getCanonicalName());
Assert.assertEquals(arraysEq(test1exp1.getPL(), _mleparse((List<Integer>)test1result.getGenotype(0).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test1exp2.getPL(),_mleparse((List<Integer>)test1result.getGenotype(1).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test1exp3.getPL(),_mleparse((List<Integer>)test1result.getGenotype(2).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
// AA AB BB AC BC CC
// AA AC CC AT CT TT
VariantContext test2 = makeVC("2",Arrays.asList(Aref,C,T),
makeG("s1",Aref,T,30,10,60,0,15,90),
makeG("s2",Aref,C,40,0,10,30,40,80),
makeG("s3",Aref,Aref,0,5,8,15,20,40),
makeG("s4",C,T,80,40,12,20,0,10));
test2 = new VariantContextBuilder(test2).attribute(VCFConstants.MLE_ALLELE_COUNT_KEY,new ArrayList<Integer>(Arrays.asList(2,2))).make();
VariantContext test2result = PairHMMLikelihoodCalculationEngine.calculatePosteriorGLs(test2,new ArrayList<VariantContext>(),5,0.001,true,false,false);
Genotype test2exp1 = makeGwithPLs("s1",Aref,T,new double[]{-2.647372, -1.045139, -6.823193, -0.04513873, -2.198182, -9.823193});
Genotype test2exp2 = makeGwithPLs("s2",Aref,C,new double[]{-3.609957, -0.007723248, -1.785778, -3.007723, -4.660767, -8.785778});
Genotype test2exp3 = makeGwithPLs("s3",Aref,Aref,new double[] {-0.06094877, -0.9587151, -2.03677,-1.958715, -3.111759, -5.23677});
Genotype test2exp4 = makeGwithPLs("s4",C,T,new double[]{-7.016534, -3.4143, -1.392355, -1.4143, -0.06734388, -1.192355});
Assert.assertEquals(arraysEq(test2exp1.getPL(),(int[]) _mleparse((List<Integer>)test2result.getGenotype(0).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test2exp2.getPL(),(int[]) _mleparse((List<Integer>)test2result.getGenotype(1).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test2exp3.getPL(),(int[]) _mleparse((List<Integer>)test2result.getGenotype(2).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test2exp4.getPL(),(int[]) _mleparse((List<Integer>)test2result.getGenotype(3).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
}
@Test
private void testCalculatePosteriorSamplePlusExternal() {
VariantContext testOverlappingBase = makeVC("1", Arrays.asList(Aref,T), makeG("s1",T,T,40,20,0),
makeG("s2",Aref,T,18,0,24),
makeG("s3",Aref,T,22,0,12));
List<VariantContext> supplTest1 = new ArrayList<>(3);
supplTest1.add(new VariantContextBuilder(makeVC("2",Arrays.asList(Aref,T))).attribute(VCFConstants.MLE_ALLELE_COUNT_KEY,2).attribute(VCFConstants.ALLELE_NUMBER_KEY,10).make());
supplTest1.add(new VariantContextBuilder(makeVC("3",Arrays.asList(Aref,T))).attribute(VCFConstants.ALLELE_COUNT_KEY,4).attribute(VCFConstants.ALLELE_NUMBER_KEY,22).make());
supplTest1.add(makeVC("4",Arrays.asList(Aref,T),
makeG("s_1",T,T),
makeG("s_2",Aref,T)));
VariantContext test1result = PairHMMLikelihoodCalculationEngine.calculatePosteriorGLs(testOverlappingBase,supplTest1,0,0.001,true,false,false);
// the counts here are ref=30, alt=14
Genotype test1exp1 = makeGwithPLs("t1",T,T,new double[]{-3.370985, -1.415172, -0.01721766});
Genotype test1exp2 = makeGwithPLs("t2",Aref,T,new double[]{-1.763792, -0.007978791, -3.010024});
Genotype test1exp3 = makeGwithPLs("t3",Aref,T,new double[]{-2.165587, -0.009773643, -1.811819});
Assert.assertEquals(arraysEq(test1exp1.getPL(),_mleparse((List<Integer>) test1result.getGenotype(0).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test1exp2.getPL(),_mleparse((List<Integer>) test1result.getGenotype(1).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test1exp3.getPL(),_mleparse((List<Integer>) test1result.getGenotype(2).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
VariantContext testNonOverlapping = makeVC("1", Arrays.asList(Aref,T), makeG("s1",T,T,3,1,0));
List<VariantContext> other = Arrays.asList(makeVC("2",Arrays.asList(Aref,C),makeG("s2",C,C,10,2,0)));
VariantContext test2result = PairHMMLikelihoodCalculationEngine.calculatePosteriorGLs(testNonOverlapping,other,0,0.001,true,false,false);
Genotype test2exp1 = makeGwithPLs("SGV",T,T,new double[]{-4.078345, -3.276502, -0.0002661066});
Assert.assertEquals(arraysEq(test2exp1.getPL(),_mleparse((List<Integer>) test2result.getGenotype(0).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
}
private double[] pl2gl(int[] pl) {
double[] gl = new double[pl.length];
for ( int idx = 0; idx < gl.length; idx++ ) {
gl[idx] = pl[idx]/(-10.0);
}
return MathUtils.normalizeFromLog10(gl,true);
}
@Test
private void testCalculatePosterior() {
int[][] likelihood_PLs = new int[][]{
new int[]{3,0,3},
new int[]{99,0,99},
new int[]{50,20,0},
new int[]{10,0,50},
new int[]{80,60,0},
new int[]{0,42,44}};
int[] altCounts = new int[]{10,40,90};
int[] altAlleleNum = new int[]{100,500,1000};
double[] expected_post_10_100 = new double[] {
9.250326e-03, 3.020208e-01, 6.887289e-01,
7.693433e-12, 1.000000e+00, 5.728111e-10,
1.340156e-07, 2.192982e-03, 9.978069e-01,
6.073718e-03, 9.938811e-01, 4.522159e-05,
1.343101e-10, 2.197802e-07, 9.999998e-01,
9.960193e-01, 1.028366e-03, 2.952290e-03
};
double[] expected_post_10_500 = new double[] {
4.226647e-04, 7.513277e-02, 9.244446e-01,
1.413080e-12, 1.000000e+00, 3.090662e-09,
4.570232e-09, 4.071661e-04, 9.995928e-01,
1.120916e-03, 9.986339e-01, 2.451646e-04,
4.572093e-12, 4.073320e-08, 1.000000e+00,
9.151689e-01, 5.144399e-03, 7.968675e-02
};
double[] expected_post_10_1000 = new double[] {
1.077685e-04, 3.870477e-02, 9.611875e-01,
6.994030e-13, 1.000000e+00, 6.237975e-09,
1.120976e-09, 2.017756e-04, 9.997982e-01,
5.549722e-04, 9.989500e-01, 4.949797e-04,
1.121202e-12, 2.018163e-08, 1.000000e+00,
7.318346e-01, 8.311615e-03, 2.598538e-01
};
double[] expected_post_40_100 = new double[] {
1.102354e-01, 6.437516e-01, 2.460131e-01,
4.301328e-11, 1.000000e+00, 9.599306e-11,
4.422850e-06, 1.294493e-02, 9.870507e-01,
3.303763e-02, 9.669550e-01, 7.373032e-06,
4.480868e-09, 1.311474e-06, 9.999987e-01,
9.997266e-01, 1.846199e-04, 8.882157e-05
};
double[] expected_post_40_500 = new double[] {
5.711785e-03, 2.557266e-01, 7.385617e-01,
5.610428e-12, 1.000000e+00, 7.254558e-10,
7.720262e-08, 1.732352e-03, 9.982676e-01,
4.436495e-03, 9.955061e-01, 5.736604e-05,
7.733659e-11, 1.735358e-07, 9.999998e-01,
9.934793e-01, 1.406575e-03, 5.114153e-03
};
double[] expected_post_40_1000 = new double[] {
1.522132e-03, 1.422229e-01, 8.562549e-01,
2.688330e-12, 1.000000e+00, 1.512284e-09,
1.776184e-08, 8.317737e-04, 9.991682e-01,
2.130611e-03, 9.977495e-01, 1.198547e-04,
1.777662e-11, 8.324661e-08, 9.999999e-01,
9.752770e-01, 2.881677e-03, 2.184131e-02
};
double[] expected_post_90_100 = new double[] {
6.887289e-01, 3.020208e-01, 9.250326e-03,
5.728111e-10, 1.000000e+00, 7.693433e-12,
6.394346e-04, 1.405351e-01, 8.588255e-01,
3.127146e-01, 6.872849e-01, 4.200075e-07,
7.445327e-07, 1.636336e-05, 9.999829e-01,
9.999856e-01, 1.386699e-05, 5.346906e-07
};
double[] expected_post_90_500 = new double[] {
2.528165e-02, 4.545461e-01, 5.201723e-01,
1.397100e-11, 1.000000e+00, 2.874546e-10,
4.839050e-07, 4.360463e-03, 9.956391e-01,
1.097551e-02, 9.890019e-01, 2.258221e-05,
4.860244e-10, 4.379560e-07, 9.999996e-01,
9.986143e-01, 5.677671e-04, 8.179741e-04
};
double[] expected_post_90_1000 = new double[] {
7.035938e-03, 2.807708e-01, 7.121932e-01,
6.294627e-12, 1.000000e+00, 6.371561e-10,
9.859771e-08, 1.971954e-03, 9.980279e-01,
4.974874e-03, 9.949748e-01, 5.035678e-05,
9.879252e-11, 1.975850e-07, 9.999998e-01,
9.947362e-01, 1.255272e-03, 4.008518e-03
};
double[][] expectations = new double[][] {
expected_post_10_100,
expected_post_10_500,
expected_post_10_1000,
expected_post_40_100,
expected_post_40_500,
expected_post_40_1000,
expected_post_90_100,
expected_post_90_500,
expected_post_90_1000
};
int testIndex = 0;
for ( int altCount : altCounts ) {
for ( int numAlt : altAlleleNum ) {
double[] knownCounts = new double[2];
knownCounts[0] = altCount;
knownCounts[1] = numAlt-altCount;
int expected_index = 0;
for ( int gl_index = 0; gl_index < likelihood_PLs.length; gl_index++ ) {
double[] post = PairHMMLikelihoodCalculationEngine.calculatePosteriorGLs(pl2gl(likelihood_PLs[gl_index]), knownCounts, 2);
for ( int i = 0; i < post.length; i++ ) {
double expected = expectations[testIndex][expected_index++];
double observed = Math.pow(10.0,post[i]);
double err = Math.abs( (expected-observed)/expected );
Assert.assertTrue(err < 1e-4, String.format("Counts: %s | Expected: %e | Observed: %e | pre %s | prior %s | post %s",
Arrays.toString(knownCounts), expected,observed, Arrays.toString(pl2gl(likelihood_PLs[gl_index])),
Arrays.toString(PairHMMLikelihoodCalculationEngine.getDirichletPrior(knownCounts,2)),Arrays.toString(post)));
}
}
testIndex++;
}
}
}
private boolean arraysApproxEqual(double[] a, double[] b, double tol) {
if ( a.length != b.length ) {
return false;
}
for ( int idx = 0; idx < a.length; idx++ ) {
if ( Math.abs(a[idx]-b[idx]) > tol ) {
return false;
}
}
return true;
}
private String errMsgArray(double[] a, double[] b) {
return String.format("Expected %s, Observed %s", Arrays.toString(a), Arrays.toString(b));
}
@Test
private void testPosteriorMultiAllelic() {
// AA AB BB AC BC CC AD BD CD DD
int[] PL_one = new int[] {40,20,30,0,15,25};
int[] PL_two = new int[] {0,20,10,99,99,99};
int[] PL_three = new int[] {50,40,0,30,30,10,20,40,80,50};
int[] PL_four = new int[] {99,90,85,10,5,30,40,20,40,30,0,12,20,14,5};
int[] PL_five = new int[] {60,20,30,0,40,10,8,12,18,22,40,12,80,60,20};
double[] counts_one = new double[]{100.001,40.001,2.001};
double[] counts_two = new double[]{2504.001,16.001,218.001};
double[] counts_three = new double[]{10000.001,500.001,25.001,0.001};
double[] counts_four = new double[]{4140.001,812.001,32.001,104.001,12.001};
double[] counts_five = new double[]{80.001,40.001,8970.001,200.001,1922.001};
double expected_one[] = new double[] { -2.684035, -0.7852596, -2.4735, -0.08608339, -1.984017, -4.409852 };
double expected_two[] = new double[] { -5.736189e-05, -3.893688, -5.362878, -10.65938, -12.85386, -12.0186};
double expected_three[] = new double[] {-2.403234, -2.403276, -0.004467802, -2.70429, -4.005319, -3.59033, -6.102247, -9.403276, -14.70429, -13.40284};
double expected_four[] = new double[] {-7.828677, -7.335196, -7.843136, -0.7395892, -0.947033, -5.139092, -3.227715,
-1.935159, -5.339552, -4.124552, -0.1655353, -2.072979, -4.277372, -3.165498, -3.469589 };
double expected_five[] = new double[] { -9.170334, -5.175724, -6.767055, -0.8250021, -5.126027, -0.07628661, -3.276762,
-3.977787, -2.227065, -4.57769, -5.494041, -2.995066, -7.444344, -7.096104, -2.414187};
double[] post1 = PairHMMLikelihoodCalculationEngine.calculatePosteriorGLs(pl2gl(PL_one),counts_one,2);
double[] post2 = PairHMMLikelihoodCalculationEngine.calculatePosteriorGLs(pl2gl(PL_two),counts_two,2);
double[] post3 = PairHMMLikelihoodCalculationEngine.calculatePosteriorGLs(pl2gl(PL_three),counts_three,2);
double[] post4 = PairHMMLikelihoodCalculationEngine.calculatePosteriorGLs(pl2gl(PL_four),counts_four,2);
double[] post5 = PairHMMLikelihoodCalculationEngine.calculatePosteriorGLs(pl2gl(PL_five),counts_five,2);
double[] expecPrior5 = new double[] {-4.2878195, -4.2932090, -4.8845400, -1.9424874, -2.2435120, -0.1937719, -3.5942477,
-3.8952723, -1.5445506, -3.4951749, -2.6115263, -2.9125508, -0.5618292, -2.2135895,
-1.5316722};
Assert.assertTrue(arraysApproxEqual(expecPrior5, PairHMMLikelihoodCalculationEngine.getDirichletPrior(counts_five,2),1e-5),errMsgArray(expecPrior5,PairHMMLikelihoodCalculationEngine.getDirichletPrior(counts_five,2)));
Assert.assertTrue(arraysApproxEqual(expected_one,post1,1e-6),errMsgArray(expected_one,post1));
Assert.assertTrue(arraysApproxEqual(expected_two,post2,1e-5),errMsgArray(expected_two,post2));
Assert.assertTrue(arraysApproxEqual(expected_three,post3,1e-5),errMsgArray(expected_three,post3));
Assert.assertTrue(arraysApproxEqual(expected_four,post4,1e-5),errMsgArray(expected_four,post4));
Assert.assertTrue(arraysApproxEqual(expected_five,post5,1e-5),errMsgArray(expected_five,post5));
}
}

View File

@ -0,0 +1,391 @@
/*
* By downloading the PROGRAM you agree to the following terms of use:
*
* BROAD INSTITUTE - SOFTWARE LICENSE AGREEMENT - FOR ACADEMIC NON-COMMERCIAL RESEARCH PURPOSES ONLY
*
* This Agreement is made between the Broad Institute, Inc. with a principal address at 7 Cambridge Center, Cambridge, MA 02142 (BROAD) and the LICENSEE and is effective at the date the downloading is completed (EFFECTIVE DATE).
*
* WHEREAS, LICENSEE desires to license the PROGRAM, as defined hereinafter, and BROAD wishes to have this PROGRAM utilized in the public interest, subject only to the royalty-free, nonexclusive, nontransferable license rights of the United States Government pursuant to 48 CFR 52.227-14; and
* WHEREAS, LICENSEE desires to license the PROGRAM and BROAD desires to grant a license on the following terms and conditions.
* NOW, THEREFORE, in consideration of the promises and covenants made herein, the parties hereto agree as follows:
*
* 1. DEFINITIONS
* 1.1 PROGRAM shall mean copyright in the object code and source code known as GATK2 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute/GATK on the EFFECTIVE DATE.
*
* 2. LICENSE
* 2.1 Grant. Subject to the terms of this Agreement, BROAD hereby grants to LICENSEE, solely for academic non-commercial research purposes, a non-exclusive, non-transferable license to: (a) download, execute and display the PROGRAM and (b) create bug fixes and modify the PROGRAM.
* The LICENSEE may apply the PROGRAM in a pipeline to data owned by users other than the LICENSEE and provide these users the results of the PROGRAM provided LICENSEE does so for academic non-commercial purposes only. For clarification purposes, academic sponsored research is not a commercial use under the terms of this Agreement.
* 2.2 No Sublicensing or Additional Rights. LICENSEE shall not sublicense or distribute the PROGRAM, in whole or in part, without prior written permission from BROAD. LICENSEE shall ensure that all of its users agree to the terms of this Agreement. LICENSEE further agrees that it shall not put the PROGRAM on a network, server, or other similar technology that may be accessed by anyone other than the LICENSEE and its employees and users who have agreed to the terms of this agreement.
* 2.3 License Limitations. Nothing in this Agreement shall be construed to confer any rights upon LICENSEE by implication, estoppel, or otherwise to any computer software, trademark, intellectual property, or patent rights of BROAD, or of any other entity, except as expressly granted herein. LICENSEE agrees that the PROGRAM, in whole or part, shall not be used for any commercial purpose, including without limitation, as the basis of a commercial software or hardware product or to provide services. LICENSEE further agrees that the PROGRAM shall not be copied or otherwise adapted in order to circumvent the need for obtaining a license for use of the PROGRAM.
*
* 3. OWNERSHIP OF INTELLECTUAL PROPERTY
* LICENSEE acknowledges that title to the PROGRAM shall remain with BROAD. The PROGRAM is marked with the following BROAD copyright notice and notice of attribution to contributors. LICENSEE shall retain such notice on all copies. LICENSEE agrees to include appropriate attribution if any results obtained from use of the PROGRAM are included in any publication.
* Copyright 2012 Broad Institute, Inc.
* Notice of attribution: The GATK2 program was made available through the generosity of Medical and Population Genetics program at the Broad Institute, Inc.
* LICENSEE shall not use any trademark or trade name of BROAD, or any variation, adaptation, or abbreviation, of such marks or trade names, or any names of officers, faculty, students, employees, or agents of BROAD except as states above for attribution purposes.
*
* 4. INDEMNIFICATION
* LICENSEE shall indemnify, defend, and hold harmless BROAD, and their respective officers, faculty, students, employees, associated investigators and agents, and their respective successors, heirs and assigns, (Indemnitees), against any liability, damage, loss, or expense (including reasonable attorneys fees and expenses) incurred by or imposed upon any of the Indemnitees in connection with any claims, suits, actions, demands or judgments arising out of any theory of liability (including, without limitation, actions in the form of tort, warranty, or strict liability and regardless of whether such action has any factual basis) pursuant to any right or license granted under this Agreement.
*
* 5. NO REPRESENTATIONS OR WARRANTIES
* THE PROGRAM IS DELIVERED AS IS. BROAD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE PROGRAM OR THE COPYRIGHT, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. BROAD EXTENDS NO WARRANTIES OF ANY KIND AS TO PROGRAM CONFORMITY WITH WHATEVER USER MANUALS OR OTHER LITERATURE MAY BE ISSUED FROM TIME TO TIME.
* IN NO EVENT SHALL BROAD OR ITS RESPECTIVE DIRECTORS, OFFICERS, EMPLOYEES, AFFILIATED INVESTIGATORS AND AFFILIATES BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING, WITHOUT LIMITATION, ECONOMIC DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER BROAD SHALL BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE FOREGOING.
*
* 6. ASSIGNMENT
* This Agreement is personal to LICENSEE and any rights or obligations assigned by LICENSEE without the prior written consent of BROAD shall be null and void.
*
* 7. MISCELLANEOUS
* 7.1 Export Control. LICENSEE gives assurance that it will comply with all United States export control laws and regulations controlling the export of the PROGRAM, including, without limitation, all Export Administration Regulations of the United States Department of Commerce. Among other things, these laws and regulations prohibit, or require a license for, the export of certain types of software to specified countries.
* 7.2 Termination. LICENSEE shall have the right to terminate this Agreement for any reason upon prior written notice to BROAD. If LICENSEE breaches any provision hereunder, and fails to cure such breach within thirty (30) days, BROAD may terminate this Agreement immediately. Upon termination, LICENSEE shall provide BROAD with written assurance that the original and all copies of the PROGRAM have been destroyed, except that, upon prior written authorization from BROAD, LICENSEE may retain a copy for archive purposes.
* 7.3 Survival. The following provisions shall survive the expiration or termination of this Agreement: Articles 1, 3, 4, 5 and Sections 2.2, 2.3, 7.3, and 7.4.
* 7.4 Notice. Any notices under this Agreement shall be in writing, shall specifically refer to this Agreement, and shall be sent by hand, recognized national overnight courier, confirmed facsimile transmission, confirmed electronic mail, or registered or certified mail, postage prepaid, return receipt requested. All notices under this Agreement shall be deemed effective upon receipt.
* 7.5 Amendment and Waiver; Entire Agreement. This Agreement may be amended, supplemented, or otherwise modified only by means of a written instrument signed by all parties. Any waiver of any rights or failure to act in a specific instance shall relate only to such instance and shall not be construed as an agreement to waive any rights or fail to act in any other instance, whether or not similar. This Agreement constitutes the entire agreement among the parties with respect to its subject matter and supersedes prior agreements or understandings between the parties relating to its subject matter.
* 7.6 Binding Effect; Headings. This Agreement shall be binding upon and inure to the benefit of the parties and their respective permitted successors and assigns. All headings are for convenience only and shall not affect the meaning of any provision of this Agreement.
* 7.7 Governing Law. This Agreement shall be construed, governed, interpreted and applied in accordance with the internal laws of the Commonwealth of Massachusetts, U.S.A., without regard to conflict of laws principles.
*/
package org.broadinstitute.sting.gatk.walkers.variantutils;
/**
* Created by IntelliJ IDEA.
* User: ebanks
* Date: 12/8/13
*/
import org.broadinstitute.sting.BaseTest;
import org.broadinstitute.sting.utils.MathUtils;
import org.broadinstitute.variant.variantcontext.*;
import org.broadinstitute.variant.vcf.VCFConstants;
import org.testng.Assert;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PosteriorLikelihoodsUtilsUnitTest extends BaseTest {
Allele Aref, T, C, G, Cref, ATC, ATCATC;
@BeforeSuite
public void setup() {
// alleles
Aref = Allele.create("A", true);
Cref = Allele.create("C", true);
T = Allele.create("T");
C = Allele.create("C");
G = Allele.create("G");
ATC = Allele.create("ATC");
ATCATC = Allele.create("ATCATC");
}
private String arraysEq(int[] a, int[] b) {
if ( a.length != b.length ) {
return String.format("NEQ: %s | %s",Arrays.toString(a),Arrays.toString(b));
}
for ( int idx = 0; idx < a.length; idx++) {
if ( a[idx] - b[idx] > 1 || b[idx] - a[idx] > 1) {
return String.format("NEQ: %s | %s",Arrays.toString(a),Arrays.toString(b));
}
}
return "";
}
private int[] _mleparse(List<Integer> s) {
int[] mle = new int[s.size()];
for ( int idx = 0; idx < mle.length; idx ++) {
mle[idx] = s.get(idx);
}
return mle;
}
private Genotype makeGwithPLs(String sample, Allele a1, Allele a2, double[] pls) {
Genotype gt = new GenotypeBuilder(sample, Arrays.asList(a1, a2)).PL(pls).make();
if ( pls != null && pls.length > 0 ) {
Assert.assertNotNull(gt.getPL());
Assert.assertTrue(gt.getPL().length > 0);
for ( int i : gt.getPL() ) {
Assert.assertTrue(i >= 0);
}
Assert.assertNotEquals(Arrays.toString(gt.getPL()),"[0]");
}
return gt;
}
private Genotype makeG(String sample, Allele a1, Allele a2) {
return GenotypeBuilder.create(sample, Arrays.asList(a1, a2));
}
private Genotype makeG(String sample, Allele a1, Allele a2, int... pls) {
return new GenotypeBuilder(sample, Arrays.asList(a1, a2)).PL(pls).make();
}
private VariantContext makeVC(String source, List<Allele> alleles, Genotype... genotypes) {
int start = 10;
int stop = start; // alleles.contains(ATC) ? start + 3 : start;
return new VariantContextBuilder(source, "1", start, stop, alleles).genotypes(Arrays.asList(genotypes)).filters(null).make();
}
@Test
private void testCalculatePosteriorNoExternalData() {
VariantContext test1 = makeVC("1",Arrays.asList(Aref,T), makeG("s1",Aref,T,20,0,10),
makeG("s2",T,T,60,40,0),
makeG("s3",Aref,Aref,0,30,90));
test1 = new VariantContextBuilder(test1).attribute(VCFConstants.MLE_ALLELE_COUNT_KEY,3).make();
VariantContext test1result = PosteriorLikelihoodsUtils.calculatePosteriorGLs(test1, new ArrayList<VariantContext>(), 0, 0.001, true, false, false);
Genotype test1exp1 = makeGwithPLs("s1",Aref,T,new double[]{-2.20686, -0.03073215, -1.20686});
Assert.assertTrue(test1exp1.hasPL());
Genotype test1exp2 = makeGwithPLs("s2",T,T,new double[]{-6.000066, -3.823938, -6.557894e-05});
Genotype test1exp3 = makeGwithPLs("s3",Aref,Aref,new double[]{-0.0006510083, -2.824524, -9.000651});
Assert.assertEquals("java.util.ArrayList",test1result.getGenotype(0).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY).getClass().getCanonicalName());
Assert.assertEquals(arraysEq(test1exp1.getPL(), _mleparse((List<Integer>)test1result.getGenotype(0).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test1exp2.getPL(),_mleparse((List<Integer>)test1result.getGenotype(1).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test1exp3.getPL(),_mleparse((List<Integer>)test1result.getGenotype(2).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
// AA AB BB AC BC CC
// AA AC CC AT CT TT
VariantContext test2 = makeVC("2",Arrays.asList(Aref,C,T),
makeG("s1",Aref,T,30,10,60,0,15,90),
makeG("s2",Aref,C,40,0,10,30,40,80),
makeG("s3",Aref,Aref,0,5,8,15,20,40),
makeG("s4",C,T,80,40,12,20,0,10));
test2 = new VariantContextBuilder(test2).attribute(VCFConstants.MLE_ALLELE_COUNT_KEY,new ArrayList<Integer>(Arrays.asList(2,2))).make();
VariantContext test2result = PosteriorLikelihoodsUtils.calculatePosteriorGLs(test2,new ArrayList<VariantContext>(),5,0.001,true,false,false);
Genotype test2exp1 = makeGwithPLs("s1",Aref,T,new double[]{-2.647372, -1.045139, -6.823193, -0.04513873, -2.198182, -9.823193});
Genotype test2exp2 = makeGwithPLs("s2",Aref,C,new double[]{-3.609957, -0.007723248, -1.785778, -3.007723, -4.660767, -8.785778});
Genotype test2exp3 = makeGwithPLs("s3",Aref,Aref,new double[] {-0.06094877, -0.9587151, -2.03677,-1.958715, -3.111759, -5.23677});
Genotype test2exp4 = makeGwithPLs("s4",C,T,new double[]{-7.016534, -3.4143, -1.392355, -1.4143, -0.06734388, -1.192355});
Assert.assertEquals(arraysEq(test2exp1.getPL(),(int[]) _mleparse((List<Integer>)test2result.getGenotype(0).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test2exp2.getPL(),(int[]) _mleparse((List<Integer>)test2result.getGenotype(1).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test2exp3.getPL(),(int[]) _mleparse((List<Integer>)test2result.getGenotype(2).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test2exp4.getPL(),(int[]) _mleparse((List<Integer>)test2result.getGenotype(3).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
}
@Test
private void testCalculatePosteriorSamplePlusExternal() {
VariantContext testOverlappingBase = makeVC("1", Arrays.asList(Aref,T), makeG("s1",T,T,40,20,0),
makeG("s2",Aref,T,18,0,24),
makeG("s3",Aref,T,22,0,12));
List<VariantContext> supplTest1 = new ArrayList<>(3);
supplTest1.add(new VariantContextBuilder(makeVC("2",Arrays.asList(Aref,T))).attribute(VCFConstants.MLE_ALLELE_COUNT_KEY,2).attribute(VCFConstants.ALLELE_NUMBER_KEY,10).make());
supplTest1.add(new VariantContextBuilder(makeVC("3",Arrays.asList(Aref,T))).attribute(VCFConstants.ALLELE_COUNT_KEY,4).attribute(VCFConstants.ALLELE_NUMBER_KEY,22).make());
supplTest1.add(makeVC("4",Arrays.asList(Aref,T),
makeG("s_1",T,T),
makeG("s_2",Aref,T)));
VariantContext test1result = PosteriorLikelihoodsUtils.calculatePosteriorGLs(testOverlappingBase,supplTest1,0,0.001,true,false,false);
// the counts here are ref=30, alt=14
Genotype test1exp1 = makeGwithPLs("t1",T,T,new double[]{-3.370985, -1.415172, -0.01721766});
Genotype test1exp2 = makeGwithPLs("t2",Aref,T,new double[]{-1.763792, -0.007978791, -3.010024});
Genotype test1exp3 = makeGwithPLs("t3",Aref,T,new double[]{-2.165587, -0.009773643, -1.811819});
Assert.assertEquals(arraysEq(test1exp1.getPL(),_mleparse((List<Integer>) test1result.getGenotype(0).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test1exp2.getPL(),_mleparse((List<Integer>) test1result.getGenotype(1).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
Assert.assertEquals(arraysEq(test1exp3.getPL(),_mleparse((List<Integer>) test1result.getGenotype(2).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
VariantContext testNonOverlapping = makeVC("1", Arrays.asList(Aref,T), makeG("s1",T,T,3,1,0));
List<VariantContext> other = Arrays.asList(makeVC("2",Arrays.asList(Aref,C),makeG("s2",C,C,10,2,0)));
VariantContext test2result = PosteriorLikelihoodsUtils.calculatePosteriorGLs(testNonOverlapping,other,0,0.001,true,false,false);
Genotype test2exp1 = makeGwithPLs("SGV",T,T,new double[]{-4.078345, -3.276502, -0.0002661066});
Assert.assertEquals(arraysEq(test2exp1.getPL(),_mleparse((List<Integer>) test2result.getGenotype(0).getAnyAttribute(VCFConstants.GENOTYPE_POSTERIORS_KEY))), "");
}
private double[] pl2gl(int[] pl) {
double[] gl = new double[pl.length];
for ( int idx = 0; idx < gl.length; idx++ ) {
gl[idx] = pl[idx]/(-10.0);
}
return MathUtils.normalizeFromLog10(gl,true);
}
@Test
private void testCalculatePosterior() {
int[][] likelihood_PLs = new int[][]{
new int[]{3,0,3},
new int[]{99,0,99},
new int[]{50,20,0},
new int[]{10,0,50},
new int[]{80,60,0},
new int[]{0,42,44}};
int[] altCounts = new int[]{10,40,90};
int[] altAlleleNum = new int[]{100,500,1000};
double[] expected_post_10_100 = new double[] {
9.250326e-03, 3.020208e-01, 6.887289e-01,
7.693433e-12, 1.000000e+00, 5.728111e-10,
1.340156e-07, 2.192982e-03, 9.978069e-01,
6.073718e-03, 9.938811e-01, 4.522159e-05,
1.343101e-10, 2.197802e-07, 9.999998e-01,
9.960193e-01, 1.028366e-03, 2.952290e-03
};
double[] expected_post_10_500 = new double[] {
4.226647e-04, 7.513277e-02, 9.244446e-01,
1.413080e-12, 1.000000e+00, 3.090662e-09,
4.570232e-09, 4.071661e-04, 9.995928e-01,
1.120916e-03, 9.986339e-01, 2.451646e-04,
4.572093e-12, 4.073320e-08, 1.000000e+00,
9.151689e-01, 5.144399e-03, 7.968675e-02
};
double[] expected_post_10_1000 = new double[] {
1.077685e-04, 3.870477e-02, 9.611875e-01,
6.994030e-13, 1.000000e+00, 6.237975e-09,
1.120976e-09, 2.017756e-04, 9.997982e-01,
5.549722e-04, 9.989500e-01, 4.949797e-04,
1.121202e-12, 2.018163e-08, 1.000000e+00,
7.318346e-01, 8.311615e-03, 2.598538e-01
};
double[] expected_post_40_100 = new double[] {
1.102354e-01, 6.437516e-01, 2.460131e-01,
4.301328e-11, 1.000000e+00, 9.599306e-11,
4.422850e-06, 1.294493e-02, 9.870507e-01,
3.303763e-02, 9.669550e-01, 7.373032e-06,
4.480868e-09, 1.311474e-06, 9.999987e-01,
9.997266e-01, 1.846199e-04, 8.882157e-05
};
double[] expected_post_40_500 = new double[] {
5.711785e-03, 2.557266e-01, 7.385617e-01,
5.610428e-12, 1.000000e+00, 7.254558e-10,
7.720262e-08, 1.732352e-03, 9.982676e-01,
4.436495e-03, 9.955061e-01, 5.736604e-05,
7.733659e-11, 1.735358e-07, 9.999998e-01,
9.934793e-01, 1.406575e-03, 5.114153e-03
};
double[] expected_post_40_1000 = new double[] {
1.522132e-03, 1.422229e-01, 8.562549e-01,
2.688330e-12, 1.000000e+00, 1.512284e-09,
1.776184e-08, 8.317737e-04, 9.991682e-01,
2.130611e-03, 9.977495e-01, 1.198547e-04,
1.777662e-11, 8.324661e-08, 9.999999e-01,
9.752770e-01, 2.881677e-03, 2.184131e-02
};
double[] expected_post_90_100 = new double[] {
6.887289e-01, 3.020208e-01, 9.250326e-03,
5.728111e-10, 1.000000e+00, 7.693433e-12,
6.394346e-04, 1.405351e-01, 8.588255e-01,
3.127146e-01, 6.872849e-01, 4.200075e-07,
7.445327e-07, 1.636336e-05, 9.999829e-01,
9.999856e-01, 1.386699e-05, 5.346906e-07
};
double[] expected_post_90_500 = new double[] {
2.528165e-02, 4.545461e-01, 5.201723e-01,
1.397100e-11, 1.000000e+00, 2.874546e-10,
4.839050e-07, 4.360463e-03, 9.956391e-01,
1.097551e-02, 9.890019e-01, 2.258221e-05,
4.860244e-10, 4.379560e-07, 9.999996e-01,
9.986143e-01, 5.677671e-04, 8.179741e-04
};
double[] expected_post_90_1000 = new double[] {
7.035938e-03, 2.807708e-01, 7.121932e-01,
6.294627e-12, 1.000000e+00, 6.371561e-10,
9.859771e-08, 1.971954e-03, 9.980279e-01,
4.974874e-03, 9.949748e-01, 5.035678e-05,
9.879252e-11, 1.975850e-07, 9.999998e-01,
9.947362e-01, 1.255272e-03, 4.008518e-03
};
double[][] expectations = new double[][] {
expected_post_10_100,
expected_post_10_500,
expected_post_10_1000,
expected_post_40_100,
expected_post_40_500,
expected_post_40_1000,
expected_post_90_100,
expected_post_90_500,
expected_post_90_1000
};
int testIndex = 0;
for ( int altCount : altCounts ) {
for ( int numAlt : altAlleleNum ) {
double[] knownCounts = new double[2];
knownCounts[0] = altCount;
knownCounts[1] = numAlt-altCount;
int expected_index = 0;
for ( int gl_index = 0; gl_index < likelihood_PLs.length; gl_index++ ) {
double[] post = PosteriorLikelihoodsUtils.calculatePosteriorGLs(pl2gl(likelihood_PLs[gl_index]), knownCounts, 2);
for ( int i = 0; i < post.length; i++ ) {
double expected = expectations[testIndex][expected_index++];
double observed = Math.pow(10.0,post[i]);
double err = Math.abs( (expected-observed)/expected );
Assert.assertTrue(err < 1e-4, String.format("Counts: %s | Expected: %e | Observed: %e | pre %s | prior %s | post %s",
Arrays.toString(knownCounts), expected,observed, Arrays.toString(pl2gl(likelihood_PLs[gl_index])),
Arrays.toString(PosteriorLikelihoodsUtils.getDirichletPrior(knownCounts,2)),Arrays.toString(post)));
}
}
testIndex++;
}
}
}
private boolean arraysApproxEqual(double[] a, double[] b, double tol) {
if ( a.length != b.length ) {
return false;
}
for ( int idx = 0; idx < a.length; idx++ ) {
if ( Math.abs(a[idx]-b[idx]) > tol ) {
return false;
}
}
return true;
}
private String errMsgArray(double[] a, double[] b) {
return String.format("Expected %s, Observed %s", Arrays.toString(a), Arrays.toString(b));
}
@Test
private void testPosteriorMultiAllelic() {
// AA AB BB AC BC CC AD BD CD DD
int[] PL_one = new int[] {40,20,30,0,15,25};
int[] PL_two = new int[] {0,20,10,99,99,99};
int[] PL_three = new int[] {50,40,0,30,30,10,20,40,80,50};
int[] PL_four = new int[] {99,90,85,10,5,30,40,20,40,30,0,12,20,14,5};
int[] PL_five = new int[] {60,20,30,0,40,10,8,12,18,22,40,12,80,60,20};
double[] counts_one = new double[]{100.001,40.001,2.001};
double[] counts_two = new double[]{2504.001,16.001,218.001};
double[] counts_three = new double[]{10000.001,500.001,25.001,0.001};
double[] counts_four = new double[]{4140.001,812.001,32.001,104.001,12.001};
double[] counts_five = new double[]{80.001,40.001,8970.001,200.001,1922.001};
double expected_one[] = new double[] { -2.684035, -0.7852596, -2.4735, -0.08608339, -1.984017, -4.409852 };
double expected_two[] = new double[] { -5.736189e-05, -3.893688, -5.362878, -10.65938, -12.85386, -12.0186};
double expected_three[] = new double[] {-2.403234, -2.403276, -0.004467802, -2.70429, -4.005319, -3.59033, -6.102247, -9.403276, -14.70429, -13.40284};
double expected_four[] = new double[] {-7.828677, -7.335196, -7.843136, -0.7395892, -0.947033, -5.139092, -3.227715,
-1.935159, -5.339552, -4.124552, -0.1655353, -2.072979, -4.277372, -3.165498, -3.469589 };
double expected_five[] = new double[] { -9.170334, -5.175724, -6.767055, -0.8250021, -5.126027, -0.07628661, -3.276762,
-3.977787, -2.227065, -4.57769, -5.494041, -2.995066, -7.444344, -7.096104, -2.414187};
double[] post1 = PosteriorLikelihoodsUtils.calculatePosteriorGLs(pl2gl(PL_one),counts_one,2);
double[] post2 = PosteriorLikelihoodsUtils.calculatePosteriorGLs(pl2gl(PL_two),counts_two,2);
double[] post3 = PosteriorLikelihoodsUtils.calculatePosteriorGLs(pl2gl(PL_three),counts_three,2);
double[] post4 = PosteriorLikelihoodsUtils.calculatePosteriorGLs(pl2gl(PL_four),counts_four,2);
double[] post5 = PosteriorLikelihoodsUtils.calculatePosteriorGLs(pl2gl(PL_five),counts_five,2);
double[] expecPrior5 = new double[] {-4.2878195, -4.2932090, -4.8845400, -1.9424874, -2.2435120, -0.1937719, -3.5942477,
-3.8952723, -1.5445506, -3.4951749, -2.6115263, -2.9125508, -0.5618292, -2.2135895,
-1.5316722};
Assert.assertTrue(arraysApproxEqual(expecPrior5, PosteriorLikelihoodsUtils.getDirichletPrior(counts_five,2),1e-5),errMsgArray(expecPrior5,PosteriorLikelihoodsUtils.getDirichletPrior(counts_five,2)));
Assert.assertTrue(arraysApproxEqual(expected_one,post1,1e-6),errMsgArray(expected_one,post1));
Assert.assertTrue(arraysApproxEqual(expected_two,post2,1e-5),errMsgArray(expected_two,post2));
Assert.assertTrue(arraysApproxEqual(expected_three,post3,1e-5),errMsgArray(expected_three,post3));
Assert.assertTrue(arraysApproxEqual(expected_four,post4,1e-5),errMsgArray(expected_four,post4));
Assert.assertTrue(arraysApproxEqual(expected_five,post5,1e-5),errMsgArray(expected_five,post5));
}
}