Merge pull request #685 from broadinstitute/ldg_inbreedingCoeffForMultiallelics

Update inbreeding coefficient calculation to give a better estimate for ...
This commit is contained in:
Eric Banks 2014-08-07 09:00:09 -04:00
commit 0b72f7a16d
5 changed files with 191 additions and 31 deletions

View File

@ -46,6 +46,7 @@
package org.broadinstitute.gatk.tools.walkers.annotator;
import htsjdk.variant.variantcontext.Allele;
import org.broadinstitute.gatk.engine.contexts.AlignmentContext;
import org.broadinstitute.gatk.engine.contexts.ReferenceContext;
import org.broadinstitute.gatk.engine.refdata.RefMetaDataTracker;
@ -78,6 +79,7 @@ public class InbreedingCoeff extends InfoFieldAnnotation implements StandardAnno
private static final int MIN_SAMPLES = 10;
private static final String INBREEDING_COEFFICIENT_KEY_NAME = "InbreedingCoeff";
private Set<String> founderIds;
private int sampleCount;
@Override
public Map<String, Object> annotate(final RefMetaDataTracker tracker,
@ -89,51 +91,69 @@ public class InbreedingCoeff extends InfoFieldAnnotation implements StandardAnno
//If available, get the founder IDs and cache them. the IC will only be computed on founders then.
if(founderIds == null && walker != null)
founderIds = ((Walker)walker).getSampleDB().getFounderIds();
return calculateIC(vc);
return makeCoeffAnnotation(vc);
}
private Map<String, Object> calculateIC(final VariantContext vc) {
final GenotypesContext genotypes = (founderIds == null || founderIds.isEmpty()) ? vc.getGenotypes() : vc.getGenotypes(founderIds);
if (genotypes == null || genotypes.size() < MIN_SAMPLES || !vc.isVariant())
return null;
protected double calculateIC(final VariantContext vc, final GenotypesContext genotypes) {
final boolean doMultiallelicMapping = !vc.isBiallelic();
int idxAA = 0, idxAB = 1, idxBB = 2;
if (!vc.isBiallelic()) {
// for non-bliallelic case, do test with most common alt allele.
// Get then corresponding indices in GL vectors to retrieve GL of AA,AB and BB.
final int[] idxVector = vc.getGLIndecesOfAlternateAllele(vc.getAltAlleleWithHighestAlleleCount());
idxAA = idxVector[0];
idxAB = idxVector[1];
idxBB = idxVector[2];
}
double refCount = 0.0;
double hetCount = 0.0;
double homCount = 0.0;
int N = 0; // number of samples that have likelihoods
sampleCount = 0; // number of samples that have likelihoods
for ( final Genotype g : genotypes ) {
if ( g.isNoCall() || !g.hasLikelihoods() )
continue;
if (g.getPloidy() != 2) // only work for diploid samples
continue;
N++;
if ( g.isCalled() && g.hasLikelihoods() && g.getPloidy() == 2) // only work for diploid samples
sampleCount++;
final double[] normalizedLikelihoods = MathUtils.normalizeFromLog10( g.getLikelihoods().getAsVector() );
if (doMultiallelicMapping)
{
if (g.isHetNonRef()) {
//all likelihoods go to homCount
homCount += 1;
continue;
}
//get alternate allele for each sample
final Allele a1 = g.getAllele(0);
final Allele a2 = g.getAllele(1);
if (a2.isNonReference()) {
final int[] idxVector = vc.getGLIndecesOfAlternateAllele(a2);
idxAA = idxVector[0];
idxAB = idxVector[1];
idxBB = idxVector[2];
}
//I expect hets to be reference first, but there are no guarantees (e.g. phasing)
else if (a1.isNonReference()) {
final int[] idxVector = vc.getGLIndecesOfAlternateAllele(a1);
idxAA = idxVector[0];
idxAB = idxVector[1];
idxBB = idxVector[2];
}
}
refCount += normalizedLikelihoods[idxAA];
hetCount += normalizedLikelihoods[idxAB];
homCount += normalizedLikelihoods[idxBB];
}
if( N < MIN_SAMPLES ) {
return null;
}
final double p = ( 2.0 * refCount + hetCount ) / ( 2.0 * (refCount + hetCount + homCount) ); // expected reference allele frequency
final double q = 1.0 - p; // expected alternative allele frequency
final double F = 1.0 - ( hetCount / ( 2.0 * p * q * (double)N ) ); // inbreeding coefficient
final double F = 1.0 - ( hetCount / ( 2.0 * p * q * (double) sampleCount) ); // inbreeding coefficient
return F;
}
protected Map<String, Object> makeCoeffAnnotation(final VariantContext vc) {
final GenotypesContext genotypes = (founderIds == null || founderIds.isEmpty()) ? vc.getGenotypes() : vc.getGenotypes(founderIds);
if (genotypes == null || genotypes.size() < MIN_SAMPLES || !vc.isVariant())
return null;
double F = calculateIC(vc, genotypes);
if (sampleCount < MIN_SAMPLES)
return null;
return Collections.singletonMap(getKeyNames().get(0), (Object)String.format("%.4f", F));
}

View File

@ -0,0 +1,140 @@
/*
* 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.gatk.tools.walkers.annotator;
import htsjdk.variant.variantcontext.*;
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;
/**
* Created with IntelliJ IDEA.
* User: gauthier
* Date: 7/10/14
*/
public class InbreedingCoeffUnitTest {
private static double DELTA_PRECISION = 0.001;
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 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
public void testInbreedingCoeffForMultiallelicVC() {
//make sure that compound gets (with no ref) don't add to het count
VariantContext test1 = makeVC("1",Arrays.asList(Aref,T,C),
makeG("s1",Aref,T,2530,0,7099,366,3056,14931),
makeG("s2",T,T,7099,2530,0,7099,366,3056,14931),
makeG("s3",T,C,7099,2530,7099,3056,0,14931),
makeG("s4",Aref,T,2530,0,7099,366,3056,14931),
makeG("s5",T,T,7099,2530,0,7099,366,3056,14931),
makeG("s6",Aref,T,2530,0,7099,366,3056,14931),
makeG("s7",T,T,7099,2530,0,7099,366,3056,14931),
makeG("s8",Aref,T,2530,0,7099,366,3056,14931),
makeG("s9",T,T,7099,2530,0,7099,366,3056,14931),
makeG("s10",Aref,T,2530,0,7099,366,3056,14931));
final double ICresult1 = new InbreedingCoeff().calculateIC(test1, test1.getGenotypes());
Assert.assertEquals(ICresult1, -0.3333333, DELTA_PRECISION, "Pass");
//make sure that hets with different alternate alleles all get counted
VariantContext test2 = makeVC("2", Arrays.asList(Aref,T,C),
makeG("s1",Aref,C,4878,1623,11297,0,7970,8847),
makeG("s2",Aref,T,2530,0,7099,366,3056,14931),
makeG("s3",Aref,T,3382,0,6364,1817,5867,12246),
makeG("s4",Aref,T,2488,0,9110,3131,9374,12505),
makeG("s5",Aref,C,4530,2006,18875,0,6847,23949),
makeG("s6",Aref,T,5325,0,18692,389,16014,24570),
makeG("s7",Aref,T,2936,0,29743,499,21979,38630),
makeG("s8",Aref,T,6902,0,8976,45,5844,9061),
makeG("s9",Aref,T,5732,0,10876,6394,11408,17802),
makeG("s10",Aref,T,2780,0,25045,824,23330,30939));
final double ICresult2 = new InbreedingCoeff().calculateIC(test2, test2.getGenotypes());
Assert.assertEquals(ICresult2, -1.0, DELTA_PRECISION, "Pass");
}
}

View File

@ -135,7 +135,7 @@ public class UnifiedGenotyperIndelCallingIntegrationTest extends WalkerTest {
WalkerTest.WalkerTestSpec spec2 = new WalkerTest.WalkerTestSpec(
baseCommandIndels + " --genotyping_mode GENOTYPE_GIVEN_ALLELES -alleles " + result.get(0).getAbsolutePath() + " -I " + validationDataLocation +
"low_coverage_CEU.chr1.10k-11k.bam -o %s -L " + result.get(0).getAbsolutePath(), 1,
Arrays.asList("5cec2cf712abf11662081471a911ab1f"));
Arrays.asList("d101beb3d1f71a14a31438cce9786881"));
executeTest("test MultiSample Pilot1 CEU indels using GENOTYPE_GIVEN_ALLELES", spec2);
}

View File

@ -96,7 +96,7 @@ public class UnifiedGenotyperNormalCallingIntegrationTest extends WalkerTest{
public void testMultipleSNPAlleles() {
WalkerTest.WalkerTestSpec spec = new WalkerTest.WalkerTestSpec(
"-T UnifiedGenotyper --contamination_fraction_to_filter 0.05 --disableDithering -R " + b37KGReference + " --no_cmdline_in_header -glm BOTH --dbsnp " + b37dbSNP129 + " -I " + privateTestDir + "multiallelic.snps.bam -o %s -L " + privateTestDir + "multiallelic.snps.intervals", 1,
Arrays.asList("c13b5cfd5e0dc47d9709b9e9a948b1e7"));
Arrays.asList("120270ddf48dfd461f756c89ea1ab074"));
executeTest("test Multiple SNP alleles", spec);
}
@ -120,7 +120,7 @@ public class UnifiedGenotyperNormalCallingIntegrationTest extends WalkerTest{
public void testMismatchedPLs() {
WalkerTest.WalkerTestSpec spec = new WalkerTest.WalkerTestSpec(
"-T UnifiedGenotyper --contamination_fraction_to_filter 0.05 --disableDithering -R " + b37KGReference + " --no_cmdline_in_header -glm INDEL -I " + privateTestDir + "mismatchedPLs.bam -o %s -L 1:24020341", 1,
Arrays.asList("822981d2707cb78475bc1b4a5b23f889"));
Arrays.asList("02fd6357bd1082115822c8a931cbb6a3"));
executeTest("test mismatched PLs", spec);
}
}

View File

@ -64,7 +64,7 @@ public class HaplotypeCallerComplexAndSymbolicVariantsIntegrationTest extends Wa
@Test
public void testHaplotypeCallerMultiSampleComplex1() {
HCTestComplexVariants(privateTestDir + "AFR.complex.variants.bam", "", "b9a5d461e4bc93742ab741f491780438");
HCTestComplexVariants(privateTestDir + "AFR.complex.variants.bam", "", "ef038555cf729d7f2c3a4e635f1302d1");
}
private void HCTestSymbolicVariants(String bam, String args, String md5) {