From 61e1a3ecd17c5813b12b51c092c4c54de76f16bc Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Sat, 22 Nov 2014 22:01:39 -0500 Subject: [PATCH 01/12] Added the framework for testing the PhasingUtilies methods matchHaplotypeAlleles() and reallyMergeIntoMNP() --- .../phasing/PhasingUtilitiesUnitTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java diff --git a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java new file mode 100644 index 000000000..32facf007 --- /dev/null +++ b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java @@ -0,0 +1,61 @@ +package org.broadinstitute.gatk.tools.walkers.phasing; + +import htsjdk.samtools.reference.ReferenceSequenceFile; +import htsjdk.variant.variantcontext.*; +import org.broadinstitute.gatk.utils.fasta.CachingIndexedFastaSequenceFile; +import org.broadinstitute.gatk.utils.variant.GATKVariantContextUtils; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.broadinstitute.gatk.tools.walkers.phasing.PhasingUtils; +import org.broadinstitute.gatk.utils.BaseTest; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * Created by ronlevine and philipdexheimer on 11/22/14. + * + * TODO - need to exercise the code paths in matchHaplotypeAlleles and reallyMergeIntoMNP + */ +public class PhasingUtilitiesUnitTest extends BaseTest { + + private final int start = 10; + private ReferenceSequenceFile referenceFile; + private Allele Aref; + + @BeforeClass + public void init() throws FileNotFoundException { + referenceFile = new CachingIndexedFastaSequenceFile(new File(b37KGReference)); + Aref = Allele.create("A", true); + } + + @Test + public void TestMatchHaplotypeAlleles() { + + final List noCalls = new ArrayList<>(1); + noCalls.add(Allele.NO_CALL); + final Genotype gA_ALT = new GenotypeBuilder("A").PL(new int[]{0, 100, 1000}).alleles(noCalls).make(); + + // Must match because that same genotype + PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(gA_ALT, gA_ALT); + + // TODO - make a reference SameHaplotypeAlleles to test against + Assert.assertEquals(sameHaplotypeAlleles, sameHaplotypeAlleles); + } + + @Test + public void TestReallyMergeIntoMNP() { + + final VariantContext VCbase = new VariantContextBuilder("test", "20", start, start, Arrays.asList(Aref)).make(); + + // Merging the same variant context + VariantContext variantContext = PhasingUtils.reallyMergeIntoMNP(VCbase, VCbase, referenceFile); + + // TODO - make a reference VariantContext to test against + Assert.assertEquals(variantContext, variantContext); + } +} From 6ff698c556f20445c1603f51af0f4f33d2da50de Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Mon, 24 Nov 2014 11:08:04 -0500 Subject: [PATCH 02/12] Added HP and non-HP tests for matchHaplotypeAlleles(), added a nominal test for mergeIntoMNPvalidationCheck() --- .../tools/walkers/phasing/PhasingUtils.java | 2 +- .../phasing/PhasingUtilitiesUnitTest.java | 123 ++++++++++++++---- 2 files changed, 98 insertions(+), 27 deletions(-) diff --git a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java index 6a4adfdf3..42d01d4cc 100644 --- a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java +++ b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java @@ -58,7 +58,6 @@ import org.broadinstitute.gatk.utils.GenomeLocParser; import org.broadinstitute.gatk.utils.Utils; import org.broadinstitute.gatk.utils.variant.GATKVariantContextUtils; import htsjdk.variant.vcf.VCFConstants; -import org.broadinstitute.gatk.utils.exceptions.ReviewedGATKException; import htsjdk.variant.variantcontext.*; import java.util.*; @@ -115,6 +114,7 @@ class PhasingUtils { final Allele[] site2AllelesArray = gt2.getAlleles().toArray(new Allele[nalleles]); final int[] site1ToSite2Inds = new int[nalleles]; + // If both genotypes have read-backed phasing haplotype identifiers if (gt1.hasAnyAttribute(ReadBackedPhasing.HP_KEY) && gt2.hasAnyAttribute(ReadBackedPhasing.HP_KEY)) { final String[] hp1 = (String[]) gt1.getAnyAttribute(ReadBackedPhasing.HP_KEY); final String[] hp2 = (String[]) gt2.getAnyAttribute(ReadBackedPhasing.HP_KEY); diff --git a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java index 32facf007..c06664edc 100644 --- a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java +++ b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java @@ -1,61 +1,132 @@ +/* +* 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 415 Main Street, 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 GATK3 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute.org/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. LICENSEE hereby automatically grants to BROAD a non-exclusive, royalty-free, irrevocable license to any LICENSEE bug fixes or modifications to the PROGRAM with unlimited rights to sublicense and/or distribute. LICENSEE agrees to provide any such modifications and bug fixes to BROAD promptly upon their creation. +* 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. PHONE-HOME FEATURE +* LICENSEE expressly acknowledges that the PROGRAM contains an embedded automatic reporting system (“PHONE-HOME”) which is enabled by default upon download. Unless LICENSEE requests disablement of PHONE-HOME, LICENSEE agrees that BROAD may collect limited information transmitted by PHONE-HOME regarding LICENSEE and its use of the PROGRAM. Such information shall include LICENSEE’S user identification, version number of the PROGRAM and tools being run, mode of analysis employed, and any error reports generated during run-time. Collection of such information is used by BROAD solely to monitor usage rates, fulfill reporting requirements to BROAD funding agencies, drive improvements to the PROGRAM, and facilitate adjustments to PROGRAM-related documentation. +* +* 4. 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-2014 Broad Institute, Inc. +* Notice of attribution: The GATK3 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. +* +* 5. 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. +* +* 6. 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. +* +* 7. 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. +* +* 8. MISCELLANEOUS +* 8.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. +* 8.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. +* 8.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. +* 8.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. +* 8.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. +* 8.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. +* 8.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.phasing; import htsjdk.samtools.reference.ReferenceSequenceFile; import htsjdk.variant.variantcontext.*; -import org.broadinstitute.gatk.utils.fasta.CachingIndexedFastaSequenceFile; -import org.broadinstitute.gatk.utils.variant.GATKVariantContextUtils; - import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.broadinstitute.gatk.tools.walkers.phasing.PhasingUtils; +import org.broadinstitute.gatk.utils.GenomeLocParser; import org.broadinstitute.gatk.utils.BaseTest; +import org.broadinstitute.gatk.utils.fasta.CachingIndexedFastaSequenceFile; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -/** - * Created by ronlevine and philipdexheimer on 11/22/14. - * - * TODO - need to exercise the code paths in matchHaplotypeAlleles and reallyMergeIntoMNP - */ + public class PhasingUtilitiesUnitTest extends BaseTest { private final int start = 10; private ReferenceSequenceFile referenceFile; - private Allele Aref; + private Allele ref; + private Allele alt; @BeforeClass public void init() throws FileNotFoundException { - referenceFile = new CachingIndexedFastaSequenceFile(new File(b37KGReference)); - Aref = Allele.create("A", true); + referenceFile = new CachingIndexedFastaSequenceFile(new File("/Users/ronlevine/exampleFASTA/exampleFASTA.fasta")); + ref = Allele.create("C", true); + alt = Allele.create("T", true); } @Test - public void TestMatchHaplotypeAlleles() { + public void TestMatchHaplotypeAllelesKeyHP() { - final List noCalls = new ArrayList<>(1); - noCalls.add(Allele.NO_CALL); - final Genotype gA_ALT = new GenotypeBuilder("A").PL(new int[]{0, 100, 1000}).alleles(noCalls).make(); + final List alleleList = new ArrayList<>(); + alleleList.add(Allele.create("T", false)); + alleleList.add(Allele.create("C", false)); + final Genotype genotypeALT = new GenotypeBuilder("TC").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList).make(); - // Must match because that same genotype - PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(gA_ALT, gA_ALT); + // Must match because the same genotype + PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotypeALT, genotypeALT); - // TODO - make a reference SameHaplotypeAlleles to test against - Assert.assertEquals(sameHaplotypeAlleles, sameHaplotypeAlleles); + PhasingUtils.SameHaplotypeAlleles sameHaplotypeAllelesAnswer = new PhasingUtils.SameHaplotypeAlleles(); + sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", false), Allele.create("T", false))); + sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("C", false))); + Assert.assertEquals(sameHaplotypeAlleles.hapAlleles, sameHaplotypeAllelesAnswer.hapAlleles); + Assert.assertEquals(sameHaplotypeAlleles.requiresSwap, sameHaplotypeAllelesAnswer.requiresSwap); } @Test - public void TestReallyMergeIntoMNP() { + public void TestMatchHaplotypeAllelesNotKeyHP() { - final VariantContext VCbase = new VariantContextBuilder("test", "20", start, start, Arrays.asList(Aref)).make(); + final List alleleList = new ArrayList<>(); + alleleList.add(Allele.create("T", false)); + alleleList.add(Allele.create("C", false)); + final Genotype genotypeALT = new GenotypeBuilder("TC").PL(new int[]{0, 100, 1000}).alleles(alleleList).make(); - // Merging the same variant context - VariantContext variantContext = PhasingUtils.reallyMergeIntoMNP(VCbase, VCbase, referenceFile); + // Must match because the same genotype + PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotypeALT, genotypeALT); - // TODO - make a reference VariantContext to test against - Assert.assertEquals(variantContext, variantContext); + for (int i = 0; i < sameHaplotypeAlleles.hapAlleles.size(); i++) + System.out.println(sameHaplotypeAlleles.hapAlleles.get(i)); + + PhasingUtils.SameHaplotypeAlleles sameHaplotypeAllelesAnswer = new PhasingUtils.SameHaplotypeAlleles(); + sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", false), Allele.create("T", false))); + sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("C", false))); + Assert.assertEquals(sameHaplotypeAlleles.hapAlleles, sameHaplotypeAllelesAnswer.hapAlleles); + Assert.assertEquals(sameHaplotypeAlleles.requiresSwap, sameHaplotypeAllelesAnswer.requiresSwap); + } + + @Test + public void TestMergeIntoMNPvalidationCheck() { + String source = new String("test"); + String contig = new String("1"); + final VariantContext variantContextRef = new VariantContextBuilder(source, contig, start, start, Arrays.asList(ref)).make(); + final VariantContext variantContextAlt = new VariantContextBuilder(source, contig, start, start, Arrays.asList(alt)).make(); + GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); + + Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, variantContextRef, variantContextAlt)); } } From 34241a62f6d016aa1925f158a8669ad417f55097 Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Mon, 24 Nov 2014 11:18:21 -0500 Subject: [PATCH 03/12] Use a publicly accessible sequence file --- .../gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java index c06664edc..e939373f4 100644 --- a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java +++ b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java @@ -75,7 +75,7 @@ public class PhasingUtilitiesUnitTest extends BaseTest { @BeforeClass public void init() throws FileNotFoundException { - referenceFile = new CachingIndexedFastaSequenceFile(new File("/Users/ronlevine/exampleFASTA/exampleFASTA.fasta")); + referenceFile = new CachingIndexedFastaSequenceFile(new File(b37KGReference)); ref = Allele.create("C", true); alt = Allele.create("T", true); } From c9175eeee82a6c646ed74fade62f9d8617c2591f Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Tue, 2 Dec 2014 18:20:12 -0500 Subject: [PATCH 04/12] Renamed PhasingUtilitiesUnitTest to PhasingUtilsUnitTest --- ...{PhasingUtilitiesUnitTest.java => PhasingUtilsUnitTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/{PhasingUtilitiesUnitTest.java => PhasingUtilsUnitTest.java} (99%) diff --git a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java similarity index 99% rename from protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java rename to protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java index e939373f4..23aac6ef3 100644 --- a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilitiesUnitTest.java +++ b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java @@ -66,7 +66,7 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -public class PhasingUtilitiesUnitTest extends BaseTest { +public class PhasingUtilsUnitTest extends BaseTest { private final int start = 10; private ReferenceSequenceFile referenceFile; From 56f8e4f9cf6201c481b673317b265f2b3d120e37 Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Wed, 17 Dec 2014 03:25:26 -0500 Subject: [PATCH 05/12] Add comments, alleleSegregationIsKnown() check is added to matchHaplotypeAlleles() --- .../tools/walkers/phasing/PhasingUtils.java | 209 ++++++++++++++---- .../walkers/phasing/PhasingUtilsUnitTest.java | 7 +- 2 files changed, 170 insertions(+), 46 deletions(-) diff --git a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java index 42d01d4cc..6be2835d0 100644 --- a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java +++ b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java @@ -63,31 +63,7 @@ import htsjdk.variant.variantcontext.*; import java.util.*; /** - * [Short one sentence description of this walker] - *

- *

- * [Functionality of this walker] - *

- *

- *

Input

- *

- * [Input description] - *

- *

- *

Output

- *

- * [Output description] - *

- *

- *

Examples

- *
- *    java
- *      -jar GenomeAnalysisTK.jar
- *      -T $WalkerName
- *  
- * - * @author Your Name - * @since Date created + * Utility class for phasing analyis */ class PhasingUtils { static VariantContext mergeIntoMNP(GenomeLocParser genomeLocParser, VariantContext vc1, VariantContext vc2, ReferenceSequenceFile referenceFile, AlleleMergeRule alleleMergeRule) { @@ -105,33 +81,53 @@ class PhasingUtils { return reallyMergeIntoMNP(vc1, vc2, referenceFile); } - // Assumes: alleleSegregationIsKnown(gt1, gt2) + /** + * Find the alleles with the same haplotype + * + * @param gt1 genotype 1 + * @param gt2 genotype 2 + * @return gt1 and gt2 alleles with the same haplotype + */ static SameHaplotypeAlleles matchHaplotypeAlleles(final Genotype gt1, final Genotype gt2) { + final SameHaplotypeAlleles hapAlleles = new SameHaplotypeAlleles(); - final int nalleles = gt1.getPloidy(); - final Allele[] site1AllelesArray = gt1.getAlleles().toArray(new Allele[nalleles]); - final Allele[] site2AllelesArray = gt2.getAlleles().toArray(new Allele[nalleles]); + // Skip if can not merge the haplotype alleles + if ( !alleleSegregationIsKnown(gt1, gt2) ){ + return hapAlleles; + } - final int[] site1ToSite2Inds = new int[nalleles]; - // If both genotypes have read-backed phasing haplotype identifiers + // Get the alleles + final int numAlleles = gt1.getPloidy(); + final Allele[] site1AllelesArray = gt1.getAlleles().toArray(new Allele[numAlleles]); + final Allele[] site2AllelesArray = gt2.getAlleles().toArray(new Allele[numAlleles]); + + // locations of the same HP attribute in gt2 to gt2 + final int[] site1ToSite2Inds = new int[numAlleles]; + + // If both genotypes have read-backed phasing haplotype identifiers (HP) + // Find the gt1 and gt2 alleles with the same haplotpe if (gt1.hasAnyAttribute(ReadBackedPhasing.HP_KEY) && gt2.hasAnyAttribute(ReadBackedPhasing.HP_KEY)) { final String[] hp1 = (String[]) gt1.getAnyAttribute(ReadBackedPhasing.HP_KEY); final String[] hp2 = (String[]) gt2.getAnyAttribute(ReadBackedPhasing.HP_KEY); - final HashMap HPnameToSite1Inds = new HashMap(); + // Map of HP attribute to it's array index + final HashMap hpNameToSite1Inds = new HashMap(); + + // Hp name to index for (int ind1 = 0; ind1 < hp1.length; ++ind1) { - final String h1 = hp1[ind1]; - HPnameToSite1Inds.put(h1, ind1); + hpNameToSite1Inds.put(hp1[ind1], ind1); } + // Find the index of the gt2 HP attribute in gt1 HP attribute array for (int ind2 = 0; ind2 < hp2.length; ++ind2) { - final String h2 = hp2[ind2]; - final int ind1 = HPnameToSite1Inds.get(h2); + final int ind1 = hpNameToSite1Inds.get(hp2[ind2]); + // attributes are not in the same position in both genotypes if (ind2 != ind1) hapAlleles.requiresSwap = true; - site1ToSite2Inds[ind1] = ind2; // this is OK, since allSamplesAreMergeable() + + site1ToSite2Inds[ind1] = ind2; } } else { // gt1.isHom() || gt2.isHom() ; so, we trivially merge the corresponding alleles @@ -139,17 +135,27 @@ class PhasingUtils { site1ToSite2Inds[ind] = ind; } - for (int ind1 = 0; ind1 < nalleles; ++ind1) { + // Get the alleles for gt1 and gt2 with the same haplotype + for (int ind1 = 0; ind1 < numAlleles; ++ind1) { final Allele all1 = site1AllelesArray[ind1]; final int ind2 = site1ToSite2Inds[ind1]; final Allele all2 = site2AllelesArray[ind2]; // this is OK, since alleleSegregationIsKnown(gt1, gt2) + // add the 2 alleles hapAlleles.hapAlleles.add(new AlleleOneAndTwo(all1, all2)); } return hapAlleles; } + /** + * + * @param vc1 variant 1 + * @param vc2 variant 2 + * @param referenceFile sequence file containing the reference genome + * @return variant with the merged MNP + */ + static VariantContext reallyMergeIntoMNP(VariantContext vc1, VariantContext vc2, ReferenceSequenceFile referenceFile) { final int startInter = vc1.getEnd() + 1; final int endInter = vc2.getStart() - 1; @@ -217,10 +223,23 @@ class PhasingUtils { return mergedBuilder.make(); } + /** + * Merge two variant context names together with an underscore betwwen them + * + * @param name1 variant 1 name + * @param name2 variant 2 name + * @return combined variant names + */ static String mergeVariantContextNames(String name1, String name2) { return name1 + "_" + name2; } + /** + * + * @param vc1 + * @param vc2 + * @return merged variant atrtributes + */ static Map mergeVariantContextAttributes(VariantContext vc1, VariantContext vc2) { Map mergedAttribs = new HashMap(); @@ -243,6 +262,14 @@ class PhasingUtils { return mergedAttribs; } + /** + * Check if variants can be moved into the MNP + * + * @param genomeLocParser parse the genome locations + * @param vc1 variant 1 + * @param vc2 variant 2 + * @return true if variants are an SNP or MNP, on the same contig, location 1 is not before location 2, unfiltered, from the same sample set and are called, + */ static boolean mergeIntoMNPvalidationCheck(GenomeLocParser genomeLocParser, VariantContext vc1, VariantContext vc2) { // Can only merge "simple" base strings (i.e., SNPs or MNPs, but not indels): final boolean vc1CanBeMerged = vc1.isSNP() || vc1.isMNP(); @@ -253,18 +280,23 @@ class PhasingUtils { final GenomeLoc loc1 = GATKVariantContextUtils.getLocation(genomeLocParser, vc1); final GenomeLoc loc2 = GATKVariantContextUtils.getLocation(genomeLocParser, vc2); + // Must be on same contig if (!loc1.onSameContig(loc2)) return false; + // Variant 1 location must not be before variant 2 if (!loc1.isBefore(loc2)) return false; + // Variants can not be filtered if (vc1.isFiltered() || vc2.isFiltered()) return false; - if (!vc1.getSampleNames().equals(vc2.getSampleNames())) // vc1, vc2 refer to different sample sets + // Variants must come from the same sample set + if (!vc1.getSampleNames().equals(vc2.getSampleNames())) return false; + // All of the variant genotypes must be unfiltered and called if (!allGenotypesAreUnfilteredAndCalled(vc1) || !allGenotypesAreUnfilteredAndCalled(vc2)) return false; @@ -280,6 +312,13 @@ class PhasingUtils { return true; } + /** + * Check if can merge merge variants if phased or either is a homozygous + * + * @param vc1 variant 1 + * @param vc2 variant 2 + * @return true if variants are phased or either is a homozygous, false otherwise + */ static boolean allSamplesAreMergeable(VariantContext vc1, VariantContext vc2) { // Check that each sample's genotype in vc2 is uniquely appendable onto its genotype in vc1: for (final Genotype gt1 : vc1.getGenotypes()) { @@ -292,30 +331,48 @@ class PhasingUtils { return true; } + /** + * Check if the allele segregation is known + * + * @param gt1 genotype 1 + * @param gt2 genotype 2 + * @return true if genotypes have the same number of chromosomes, haplotype, number of attributes + * as chromosomes, and either genotype is homozygous, false otherwise + */ static boolean alleleSegregationIsKnown(Genotype gt1, Genotype gt2) { + // If gt1 or gt2 do not have the same number of chromosomes, then can not be merged. if (gt1.getPloidy() != gt2.getPloidy()) return false; - // If gt1 or gt2 are hom, then could be MERGED: + // If gt1 or gt2 are homozygous, then could be merged. if (gt1.isHom() || gt2.isHom()) return true; - // Otherwise, need to check that alleles from gt1 can be matched up with alleles from gt2: + // If gt1 or gt2 do not read backed phasing haplotype, then can not be merged if (!gt1.hasAnyAttribute(ReadBackedPhasing.HP_KEY) || !gt2.hasAnyAttribute(ReadBackedPhasing.HP_KEY)) return false; + // If gt1 or gt2 do not same number of HP attributes as chromosomes, then can not be merged. final String[] hp1 = (String[]) gt1.getAnyAttribute(ReadBackedPhasing.HP_KEY); final String[] hp2 = (String[]) gt2.getAnyAttribute(ReadBackedPhasing.HP_KEY); if (hp1.length != gt1.getPloidy() || hp2.length != gt2.getPloidy()) return false; - final String[] hp1Copy = Arrays.copyOf(hp1, hp1.length); - final String[] hp2Copy = Arrays.copyOf(hp2, hp2.length); + // gt1 and gt2 must have the same read backed phasing haplotype identifier attributes to be merged + final String[] hp1Copy = Arrays.copyOf(hp1, hp1.length); + final String[] hp2Copy = Arrays.copyOf(hp2, hp2.length); Arrays.sort(hp1Copy); Arrays.sort(hp2Copy); return (Arrays.equals(hp1Copy, hp2Copy)); // The haplotype names match (though possibly in a different order) } + /** + * + * @param vc1 variant 1 + * @param vc2 variant 2 + * @return true if + */ + static boolean someSampleHasDoubleNonReferenceAllele(VariantContext vc1, VariantContext vc2) { for (final Genotype gt1 : vc1.getGenotypes()) { final Genotype gt2 = vc2.getGenotype(gt1.getSampleName()); @@ -330,6 +387,13 @@ class PhasingUtils { return false; } + /** + * + * @param vc1 variant 1 + * @param vc2 variant 2 + * @return true if + */ + static boolean doubleAllelesSegregatePerfectlyAmongSamples(VariantContext vc1, VariantContext vc2) { // Check that Alleles at vc1 and at vc2 always segregate together in all samples (including reference): final Map allele1ToAllele2 = new HashMap(); @@ -365,6 +429,9 @@ class PhasingUtils { return true; } + /** + * + */ abstract static class AlleleMergeRule { // vc1, vc2 are ONLY passed to allelesShouldBeMerged() if mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2) AND allSamplesAreMergeable(vc1, vc2): abstract public boolean allelesShouldBeMerged(VariantContext vc1, VariantContext vc2); @@ -374,8 +441,15 @@ class PhasingUtils { } } + /** + * Class for sore the alleles with the same haplotype + */ static class SameHaplotypeAlleles { + + /// Alleles are not in the same order public boolean requiresSwap; + + /// Lisgt of gthe 2 alleles with the same haplotype public List hapAlleles; public SameHaplotypeAlleles() { @@ -384,19 +458,41 @@ class PhasingUtils { } } + /** + * Class for holding 2 alleles + */ static class AlleleOneAndTwo { + /// allele 1 private Allele all1; + /// allele2 private Allele all2; + /** + * Constructor + * + * @param all1 allele 1 + * @param all2 allele 2 + */ public AlleleOneAndTwo(Allele all1, Allele all2) { this.all1 = all1; this.all2 = all2; } + /** + * Get the hah code for alleles 1 and 2 + * + * @return hasb code for alleles 1 and 2 + */ public int hashCode() { return all1.hashCode() + all2.hashCode(); } + /** + * Check if equal to another 2 alleles + * + * @param other allele to compare to + * @return true if equal, false otherwise + */ public boolean equals(Object other) { if (!(other instanceof AlleleOneAndTwo)) return false; @@ -406,11 +502,21 @@ class PhasingUtils { } } + /** + * Class for merging alleles data + */ static class MergedAllelesData { private Map mergedAlleles; private byte[] intermediateBases; private int intermediateLength; + /** + * + * @param intermediateBases + * @param vc1 variant 1 + * @param vc2 variant 2 + */ + public MergedAllelesData(byte[] intermediateBases, VariantContext vc1, VariantContext vc2) { this.mergedAlleles = new HashMap(); // implemented equals() and hashCode() for AlleleOneAndTwo this.intermediateBases = intermediateBases; @@ -419,10 +525,23 @@ class PhasingUtils { this.ensureMergedAllele(vc1.getReference(), vc2.getReference(), true); } + /** + * + * @param all1 allele 1 + * @param all2 allele 2 + * @return + */ public Allele ensureMergedAllele(Allele all1, Allele all2) { return ensureMergedAllele(all1, all2, false); // false <-> since even if all1+all2 = reference, it was already created in the constructor } + /** + * + * @param all1 allele 1 + * @param all2 allele 2 + * @param creatingReferenceForFirstTime + * @return + */ private Allele ensureMergedAllele(Allele all1, Allele all2, boolean creatingReferenceForFirstTime) { AlleleOneAndTwo all12 = new AlleleOneAndTwo(all1, all2); Allele mergedAllele = mergedAlleles.get(all12); @@ -444,6 +563,10 @@ class PhasingUtils { return mergedAllele; } + /** + * + * @return set of merged alleles values + */ public Set getAllMergedAlleles() { return new HashSet(mergedAlleles.values()); } diff --git a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java index 23aac6ef3..6737ec255 100644 --- a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java +++ b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java @@ -77,16 +77,17 @@ public class PhasingUtilsUnitTest extends BaseTest { public void init() throws FileNotFoundException { referenceFile = new CachingIndexedFastaSequenceFile(new File(b37KGReference)); ref = Allele.create("C", true); - alt = Allele.create("T", true); + alt = Allele.create("T", false); } @Test public void TestMatchHaplotypeAllelesKeyHP() { final List alleleList = new ArrayList<>(); - alleleList.add(Allele.create("T", false)); + alleleList.add(Allele.create("T", true)); alleleList.add(Allele.create("C", false)); - final Genotype genotypeALT = new GenotypeBuilder("TC").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList).make(); + String samelpName = "TC"; + final Genotype genotypeALT = new GenotypeBuilder(samelpName).attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList).make(); // Must match because the same genotype PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotypeALT, genotypeALT); From ba949389c5a53d2422569783e50b3c24cad4949c Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Wed, 17 Dec 2014 14:02:24 -0500 Subject: [PATCH 06/12] matchHaplotypeAlleles() no longer calls alleleSegregationIsKnown(), added a TODO to investigate --- .../tools/walkers/phasing/PhasingUtils.java | 57 ++++++++++++------- .../walkers/phasing/PhasingUtilsUnitTest.java | 21 ++++--- 2 files changed, 47 insertions(+), 31 deletions(-) diff --git a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java index 6be2835d0..5167b2c57 100644 --- a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java +++ b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java @@ -63,14 +63,28 @@ import htsjdk.variant.variantcontext.*; import java.util.*; /** - * Utility class for phasing analyis + * Utility class for phasing analysis */ class PhasingUtils { + + /** + * Merge variants into an MNP + * + * @param genomeLocParser parse the genome locations + * @param vc1 variant 1 + * @param vc2 variant 2 + * @param referenceFile sequence file containing the reference genome + * @param alleleMergeRule + * @return merged variant or null if the variants are NOT an SNP or MNP, on the same contig, variant location 1 is the same or after the variant location 2, + * their genotypes do NOT have the same number of chromosomes, haplotype, number of attributes as chromosomes, are both hetrozygous or do not abide by the merge rule + */ static VariantContext mergeIntoMNP(GenomeLocParser genomeLocParser, VariantContext vc1, VariantContext vc2, ReferenceSequenceFile referenceFile, AlleleMergeRule alleleMergeRule) { + + // Check if variants are an SNP or MNP, on the same contig, variant location 1 is not before variant location 2 if (!mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)) return null; - // Check that it's logically possible to merge the VCs: + // Check if variant genotypes have the same number of chromosomes, haplotype, number of attributes as chromosomes, and either genotype is homozygous if (!allSamplesAreMergeable(vc1, vc2)) return null; @@ -83,6 +97,8 @@ class PhasingUtils { /** * Find the alleles with the same haplotype + * assumes alleleSegregationIsKnown + * TODO - should alleleSegregationIsKnown be called within the method? * * @param gt1 genotype 1 * @param gt2 genotype 2 @@ -92,11 +108,6 @@ class PhasingUtils { final SameHaplotypeAlleles hapAlleles = new SameHaplotypeAlleles(); - // Skip if can not merge the haplotype alleles - if ( !alleleSegregationIsKnown(gt1, gt2) ){ - return hapAlleles; - } - // Get the alleles final int numAlleles = gt1.getPloidy(); final Allele[] site1AllelesArray = gt1.getAlleles().toArray(new Allele[numAlleles]); @@ -238,7 +249,7 @@ class PhasingUtils { * * @param vc1 * @param vc2 - * @return merged variant atrtributes + * @return merged variant attributes */ static Map mergeVariantContextAttributes(VariantContext vc1, VariantContext vc2) { Map mergedAttribs = new HashMap(); @@ -263,12 +274,13 @@ class PhasingUtils { } /** - * Check if variants can be moved into the MNP + * Check if variants can be merged into the MNP * * @param genomeLocParser parse the genome locations * @param vc1 variant 1 * @param vc2 variant 2 - * @return true if variants are an SNP or MNP, on the same contig, location 1 is not before location 2, unfiltered, from the same sample set and are called, + * @return true if variants are an SNP or MNP, on the same contig, variant location 1 is not before variant location 2, unfiltered, from the same sample set and are called, + * false otherwise */ static boolean mergeIntoMNPvalidationCheck(GenomeLocParser genomeLocParser, VariantContext vc1, VariantContext vc2) { // Can only merge "simple" base strings (i.e., SNPs or MNPs, but not indels): @@ -313,14 +325,14 @@ class PhasingUtils { } /** - * Check if can merge merge variants if phased or either is a homozygous + * Check if can merge variants * * @param vc1 variant 1 * @param vc2 variant 2 * @return true if variants are phased or either is a homozygous, false otherwise */ static boolean allSamplesAreMergeable(VariantContext vc1, VariantContext vc2) { - // Check that each sample's genotype in vc2 is uniquely appendable onto its genotype in vc1: + // Check that each sample's genotype in vc2 is uniquely appendable onto its genotype in vc1 for (final Genotype gt1 : vc1.getGenotypes()) { final Genotype gt2 = vc2.getGenotype(gt1.getSampleName()); @@ -348,7 +360,7 @@ class PhasingUtils { if (gt1.isHom() || gt2.isHom()) return true; - // If gt1 or gt2 do not read backed phasing haplotype, then can not be merged + // If gt1 or gt2 do not have a read backed phasing haplotype, then can not be merged if (!gt1.hasAnyAttribute(ReadBackedPhasing.HP_KEY) || !gt2.hasAnyAttribute(ReadBackedPhasing.HP_KEY)) return false; @@ -367,19 +379,23 @@ class PhasingUtils { } /** + * Check of any samples have alternate alleles * * @param vc1 variant 1 * @param vc2 variant 2 - * @return true if + * @return true if there is a sample with alternate alleles, false otherwise */ static boolean someSampleHasDoubleNonReferenceAllele(VariantContext vc1, VariantContext vc2) { for (final Genotype gt1 : vc1.getGenotypes()) { final Genotype gt2 = vc2.getGenotype(gt1.getSampleName()); + // Find the alleles with the same haplotype final SameHaplotypeAlleles hapAlleles = matchHaplotypeAlleles(gt1, gt2); + + // Find corresponding alternate alleles for (AlleleOneAndTwo all1all2 : hapAlleles.hapAlleles) { - if (all1all2.all1.isNonReference() && all1all2.all2.isNonReference()) // corresponding alleles are alternate + if (all1all2.all1.isNonReference() && all1all2.all2.isNonReference()) return true; } } @@ -388,6 +404,7 @@ class PhasingUtils { } /** + * Check that Alleles at vc1 and at vc2 always segregate together in all samples (including reference * * @param vc1 variant 1 * @param vc2 variant 2 @@ -395,7 +412,6 @@ class PhasingUtils { */ static boolean doubleAllelesSegregatePerfectlyAmongSamples(VariantContext vc1, VariantContext vc2) { - // Check that Alleles at vc1 and at vc2 always segregate together in all samples (including reference): final Map allele1ToAllele2 = new HashMap(); final Map allele2ToAllele1 = new HashMap(); @@ -430,7 +446,7 @@ class PhasingUtils { } /** - * + * Class for rule for merging variants */ abstract static class AlleleMergeRule { // vc1, vc2 are ONLY passed to allelesShouldBeMerged() if mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2) AND allSamplesAreMergeable(vc1, vc2): @@ -442,7 +458,7 @@ class PhasingUtils { } /** - * Class for sore the alleles with the same haplotype + * Class for storing the alleles with the same haplotype */ static class SameHaplotypeAlleles { @@ -481,7 +497,7 @@ class PhasingUtils { /** * Get the hah code for alleles 1 and 2 * - * @return hasb code for alleles 1 and 2 + * @return hash code for alleles 1 and 2 */ public int hashCode() { return all1.hashCode() + all2.hashCode(); @@ -540,7 +556,7 @@ class PhasingUtils { * @param all1 allele 1 * @param all2 allele 2 * @param creatingReferenceForFirstTime - * @return + * @return merged allele */ private Allele ensureMergedAllele(Allele all1, Allele all2, boolean creatingReferenceForFirstTime) { AlleleOneAndTwo all12 = new AlleleOneAndTwo(all1, all2); @@ -564,6 +580,7 @@ class PhasingUtils { } /** + * Get all merged alleles * * @return set of merged alleles values */ diff --git a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java index 6737ec255..547eccdaa 100644 --- a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java +++ b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java @@ -70,21 +70,17 @@ public class PhasingUtilsUnitTest extends BaseTest { private final int start = 10; private ReferenceSequenceFile referenceFile; - private Allele ref; - private Allele alt; @BeforeClass public void init() throws FileNotFoundException { referenceFile = new CachingIndexedFastaSequenceFile(new File(b37KGReference)); - ref = Allele.create("C", true); - alt = Allele.create("T", false); } @Test public void TestMatchHaplotypeAllelesKeyHP() { final List alleleList = new ArrayList<>(); - alleleList.add(Allele.create("T", true)); + alleleList.add(Allele.create("T", false)); alleleList.add(Allele.create("C", false)); String samelpName = "TC"; final Genotype genotypeALT = new GenotypeBuilder(samelpName).attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList).make(); @@ -110,9 +106,6 @@ public class PhasingUtilsUnitTest extends BaseTest { // Must match because the same genotype PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotypeALT, genotypeALT); - for (int i = 0; i < sameHaplotypeAlleles.hapAlleles.size(); i++) - System.out.println(sameHaplotypeAlleles.hapAlleles.get(i)); - PhasingUtils.SameHaplotypeAlleles sameHaplotypeAllelesAnswer = new PhasingUtils.SameHaplotypeAlleles(); sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", false), Allele.create("T", false))); sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("C", false))); @@ -124,10 +117,16 @@ public class PhasingUtilsUnitTest extends BaseTest { public void TestMergeIntoMNPvalidationCheck() { String source = new String("test"); String contig = new String("1"); - final VariantContext variantContextRef = new VariantContextBuilder(source, contig, start, start, Arrays.asList(ref)).make(); - final VariantContext variantContextAlt = new VariantContextBuilder(source, contig, start, start, Arrays.asList(alt)).make(); + final List alleleList1 = new ArrayList<>(); + alleleList1.add(Allele.create("T", true)); + alleleList1.add(Allele.create("C", false)); + final List alleleList2 = new ArrayList<>(); + alleleList2.add(Allele.create("A", true)); + alleleList2.add(Allele.create("CC", false)); + final VariantContext variantContext1 = new VariantContextBuilder(source, contig, start, start, alleleList1).make(); + final VariantContext variantContext2 = new VariantContextBuilder(source, contig, start, start, alleleList2).make(); GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); - Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, variantContextRef, variantContextAlt)); + Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, variantContext1, variantContext1)); } } From 069398ad46411af5787ad04a587966234587165c Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Fri, 19 Dec 2014 12:57:43 -0500 Subject: [PATCH 07/12] Added more tests and documentation --- .../tools/walkers/phasing/PhasingUtils.java | 31 ++-- .../walkers/phasing/PhasingUtilsUnitTest.java | 150 +++++++++++++++--- .../broadinstitute/gatk/utils/BaseTest.java | 1 - 3 files changed, 142 insertions(+), 40 deletions(-) diff --git a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java index 5167b2c57..5649a2096 100644 --- a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java +++ b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java @@ -74,7 +74,7 @@ class PhasingUtils { * @param vc1 variant 1 * @param vc2 variant 2 * @param referenceFile sequence file containing the reference genome - * @param alleleMergeRule + * @param alleleMergeRule rule for merging variants * @return merged variant or null if the variants are NOT an SNP or MNP, on the same contig, variant location 1 is the same or after the variant location 2, * their genotypes do NOT have the same number of chromosomes, haplotype, number of attributes as chromosomes, are both hetrozygous or do not abide by the merge rule */ @@ -98,7 +98,7 @@ class PhasingUtils { /** * Find the alleles with the same haplotype * assumes alleleSegregationIsKnown - * TODO - should alleleSegregationIsKnown be called within the method? + * TODO - should alleleSegregationIsKnown be called within this method? * * @param gt1 genotype 1 * @param gt2 genotype 2 @@ -160,13 +160,13 @@ class PhasingUtils { } /** + * Merge variants into an MNP * * @param vc1 variant 1 * @param vc2 variant 2 * @param referenceFile sequence file containing the reference genome * @return variant with the merged MNP */ - static VariantContext reallyMergeIntoMNP(VariantContext vc1, VariantContext vc2, ReferenceSequenceFile referenceFile) { final int startInter = vc1.getEnd() + 1; final int endInter = vc2.getStart() - 1; @@ -246,25 +246,30 @@ class PhasingUtils { } /** + * Get preset attributes and whether they are in vc1 or vc2 + * TODO: Will always return an empty map because MERGE_OR_ATTRIBS is empty * - * @param vc1 - * @param vc2 - * @return merged variant attributes + * @param vc1 variant 1 + * @param vc2 variant 2 + * @return whether the preset attributes are in vc1 or vc2 */ static Map mergeVariantContextAttributes(VariantContext vc1, VariantContext vc2) { + // Map of attribute name to value Map mergedAttribs = new HashMap(); final List vcList = new LinkedList(); vcList.add(vc1); vcList.add(vc2); + // Attribute of interest //String[] MERGE_OR_ATTRIBS = {VCFConstants.DBSNP_KEY}; final String[] MERGE_OR_ATTRIBS = {}; for (String orAttrib : MERGE_OR_ATTRIBS) { boolean attribVal = false; for (VariantContext vc : vcList) { + // Does the variant have the attribute? attribVal = vc.getAttributeAsBoolean(orAttrib, false); - if (attribVal) // already true, so no reason to continue: + if ( attribVal ) // already true, so no reason to continue: break; } mergedAttribs.put(orAttrib, attribVal); @@ -335,6 +340,8 @@ class PhasingUtils { // Check that each sample's genotype in vc2 is uniquely appendable onto its genotype in vc1 for (final Genotype gt1 : vc1.getGenotypes()) { final Genotype gt2 = vc2.getGenotype(gt1.getSampleName()); + if ( gt2 == null ) // gt2 does not have sample name + return false; if (!alleleSegregationIsKnown(gt1, gt2)) // can merge if: phased, or if either is a hom return false; @@ -385,7 +392,6 @@ class PhasingUtils { * @param vc2 variant 2 * @return true if there is a sample with alternate alleles, false otherwise */ - static boolean someSampleHasDoubleNonReferenceAllele(VariantContext vc1, VariantContext vc2) { for (final Genotype gt1 : vc1.getGenotypes()) { final Genotype gt2 = vc2.getGenotype(gt1.getSampleName()); @@ -404,13 +410,12 @@ class PhasingUtils { } /** - * Check that Alleles at vc1 and at vc2 always segregate together in all samples (including reference + * Check that Alleles at vc1 and at vc2 always segregate together in all samples (including reference) * * @param vc1 variant 1 * @param vc2 variant 2 * @return true if */ - static boolean doubleAllelesSegregatePerfectlyAmongSamples(VariantContext vc1, VariantContext vc2) { final Map allele1ToAllele2 = new HashMap(); final Map allele2ToAllele1 = new HashMap(); @@ -527,12 +532,12 @@ class PhasingUtils { private int intermediateLength; /** + * Constructor * - * @param intermediateBases + * @param intermediateBases array of bases * @param vc1 variant 1 * @param vc2 variant 2 */ - public MergedAllelesData(byte[] intermediateBases, VariantContext vc1, VariantContext vc2) { this.mergedAlleles = new HashMap(); // implemented equals() and hashCode() for AlleleOneAndTwo this.intermediateBases = intermediateBases; @@ -545,7 +550,7 @@ class PhasingUtils { * * @param all1 allele 1 * @param all2 allele 2 - * @return + * @return merged allele */ public Allele ensureMergedAllele(Allele all1, Allele all2) { return ensureMergedAllele(all1, all2, false); // false <-> since even if all1+all2 = reference, it was already created in the constructor diff --git a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java index 547eccdaa..57078fcc1 100644 --- a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java +++ b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java @@ -56,8 +56,8 @@ import htsjdk.variant.variantcontext.*; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; + import org.broadinstitute.gatk.utils.GenomeLocParser; import org.broadinstitute.gatk.utils.BaseTest; import org.broadinstitute.gatk.utils.fasta.CachingIndexedFastaSequenceFile; @@ -66,7 +66,7 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -public class PhasingUtilsUnitTest extends BaseTest { +public class PhasingUtilsUnitTest extends BaseTest { private final int start = 10; private ReferenceSequenceFile referenceFile; @@ -79,54 +79,152 @@ public class PhasingUtilsUnitTest extends BaseTest { @Test public void TestMatchHaplotypeAllelesKeyHP() { - final List alleleList = new ArrayList<>(); - alleleList.add(Allele.create("T", false)); - alleleList.add(Allele.create("C", false)); - String samelpName = "TC"; - final Genotype genotypeALT = new GenotypeBuilder(samelpName).attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList).make(); + final List alleleList1 = new ArrayList<>(); + alleleList1.add(Allele.create("T", false)); + alleleList1.add(Allele.create("C", false)); + final Genotype genotype1 = new GenotypeBuilder("TC").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList1).make(); - // Must match because the same genotype - PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotypeALT, genotypeALT); + // Attributes in different order + final List alleleList2 = new ArrayList<>(); + alleleList2.add(Allele.create("G", false)); + alleleList2.add(Allele.create("A", false)); + final Genotype genotype2 = new GenotypeBuilder("GA").attribute("HP", new String[]{"10-2", "10-1"}).alleles(alleleList2).make(); + + PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotype1, genotype2); PhasingUtils.SameHaplotypeAlleles sameHaplotypeAllelesAnswer = new PhasingUtils.SameHaplotypeAlleles(); - sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", false), Allele.create("T", false))); - sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("C", false))); + sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", false), Allele.create("A", false))); + sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("G", false))); + sameHaplotypeAllelesAnswer.requiresSwap = true; + Assert.assertEquals(sameHaplotypeAlleles.hapAlleles, sameHaplotypeAllelesAnswer.hapAlleles); Assert.assertEquals(sameHaplotypeAlleles.requiresSwap, sameHaplotypeAllelesAnswer.requiresSwap); } @Test - public void TestMatchHaplotypeAllelesNotKeyHP() { + public void TestMatchHaplotypeAllelesNoKeyHP() { - final List alleleList = new ArrayList<>(); - alleleList.add(Allele.create("T", false)); - alleleList.add(Allele.create("C", false)); - final Genotype genotypeALT = new GenotypeBuilder("TC").PL(new int[]{0, 100, 1000}).alleles(alleleList).make(); + final List alleleList1 = new ArrayList<>(); + alleleList1.add(Allele.create("T", false)); + alleleList1.add(Allele.create("C", false)); + final Genotype genotype1 = new GenotypeBuilder("TC").alleles(alleleList1).make(); + + // Attributes in different order + final List alleleList2 = new ArrayList<>(); + alleleList2.add(Allele.create("G", false)); + alleleList2.add(Allele.create("A", false)); + final Genotype genotype2 = new GenotypeBuilder("GA").alleles(alleleList2).make(); // Must match because the same genotype - PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotypeALT, genotypeALT); + PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotype1, genotype2); PhasingUtils.SameHaplotypeAlleles sameHaplotypeAllelesAnswer = new PhasingUtils.SameHaplotypeAlleles(); - sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", false), Allele.create("T", false))); - sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("C", false))); + sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", false), Allele.create("G", false))); + sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("A", false))); Assert.assertEquals(sameHaplotypeAlleles.hapAlleles, sameHaplotypeAllelesAnswer.hapAlleles); Assert.assertEquals(sameHaplotypeAlleles.requiresSwap, sameHaplotypeAllelesAnswer.requiresSwap); } @Test - public void TestMergeIntoMNPvalidationCheck() { - String source = new String("test"); - String contig = new String("1"); + public void TestMergeIntoMNPvalidationTrueCheck() { + final String contig = new String("1"); final List alleleList1 = new ArrayList<>(); alleleList1.add(Allele.create("T", true)); alleleList1.add(Allele.create("C", false)); final List alleleList2 = new ArrayList<>(); alleleList2.add(Allele.create("A", true)); - alleleList2.add(Allele.create("CC", false)); - final VariantContext variantContext1 = new VariantContextBuilder(source, contig, start, start, alleleList1).make(); - final VariantContext variantContext2 = new VariantContextBuilder(source, contig, start, start, alleleList2).make(); + alleleList2.add(Allele.create("C", false)); + final VariantContext vc1 = new VariantContextBuilder("TC", contig, start, start, alleleList1).make(); + final VariantContext vc2 = new VariantContextBuilder("AC", contig, start+1, start+1, alleleList2).make(); GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); - Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, variantContext1, variantContext1)); + Assert.assertTrue(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); + } + + @Test + public void TestMergeIntoMNPvalidationCheckLocBefore() { + final String contig = new String("1"); + final List alleleList1 = new ArrayList<>(); + alleleList1.add(Allele.create("T", true)); + alleleList1.add(Allele.create("C", false)); + final List alleleList2 = new ArrayList<>(); + alleleList2.add(Allele.create("A", true)); + alleleList2.add(Allele.create("C", false)); + final VariantContext vc1 = new VariantContextBuilder("TC", contig, start+1, start+1, alleleList1).make(); + final VariantContext vc2 = new VariantContextBuilder("AC", contig, start, start, alleleList2).make(); + GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); + + Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); + } + + @Test + public void TestMergeIntoMNPvalidationDiffContigs() { + final String contig1 = new String("1"); + final String contig2 = new String("2"); + final List alleleList1 = new ArrayList<>(); + alleleList1.add(Allele.create("T", true)); + alleleList1.add(Allele.create("C", false)); + final List alleleList2 = new ArrayList<>(); + alleleList2.add(Allele.create("A", true)); + alleleList2.add(Allele.create("C", false)); + final VariantContext vc1 = new VariantContextBuilder("TC", contig1, start+1, start+1, alleleList1).make(); + final VariantContext vc2 = new VariantContextBuilder("AC", contig2, start, start, alleleList2).make(); + GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); + + Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); + } + + @Test + public void TestMergeVariantContextNames() { + final String result = new String("A_B"); + Assert.assertEquals(result, PhasingUtils.mergeVariantContextNames("A", "B")); + } + + @Test + public void TestMergeVariantContextAttributes() { + final String contig = new String("1"); + final List alleleList1 = new ArrayList<>(); + alleleList1.add(Allele.create("T", true)); + alleleList1.add(Allele.create("C", false)); + final List alleleList2 = new ArrayList<>(); + alleleList2.add(Allele.create("A", true)); + alleleList2.add(Allele.create("C", false)); + final VariantContext vc1 = new VariantContextBuilder("TC", contig, start, start, alleleList1).make(); + final VariantContext vc2 = new VariantContextBuilder("AC", contig, start+1, start+1, alleleList2).make(); + + Assert.assertEquals(0, PhasingUtils.mergeVariantContextAttributes(vc1, vc2).size()); + } + + @Test + public void TestAllSamplesAreMergeableDiffSampleNames() { + final String contig = new String("1"); + final List alleleList1 = new ArrayList<>(); + alleleList1.add(Allele.create("T", true)); + alleleList1.add(Allele.create("C", false)); + final List alleleList2 = new ArrayList<>(); + alleleList2.add(Allele.create("A", true)); + alleleList2.add(Allele.create("C", false)); + + final Genotype g1 = new GenotypeBuilder().name("sample1").alleles(alleleList1).make(); + final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(g1).make(); + Genotype g2 = new GenotypeBuilder().name("sample2").alleles(alleleList2).make(); + final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("i21").source("AC").start(start).stop(start).alleles(alleleList2).genotypes(g2).make(); + + Assert.assertFalse( PhasingUtils.allSamplesAreMergeable(vc1, vc2)); + } + + @Test + public void TestAlleleSegregationIsKnown(){ + + } + + @Test + public void TestSomeSampleHasDoubleNonReferenceAllele(){ + + } + + @Test + public void TestDoubleAllelesSegregatePerfectlyAmongSamples(){ + } } diff --git a/public/gatk-utils/src/test/java/org/broadinstitute/gatk/utils/BaseTest.java b/public/gatk-utils/src/test/java/org/broadinstitute/gatk/utils/BaseTest.java index 5689dbc4c..1669406c8 100644 --- a/public/gatk-utils/src/test/java/org/broadinstitute/gatk/utils/BaseTest.java +++ b/public/gatk-utils/src/test/java/org/broadinstitute/gatk/utils/BaseTest.java @@ -96,7 +96,6 @@ public abstract class BaseTest { public static final String hg18Reference = "/seq/references/Homo_sapiens_assembly18/v0/Homo_sapiens_assembly18.fasta"; public static final String hg19Reference = "/seq/references/Homo_sapiens_assembly19/v1/Homo_sapiens_assembly19.fasta"; public static final String b36KGReference = "/humgen/1kg/reference/human_b36_both.fasta"; - //public static final String b37KGReference = "/Users/depristo/Desktop/broadLocal/localData/human_g1k_v37.fasta"; public static final String b37KGReference = "/humgen/1kg/reference/human_g1k_v37.fasta"; public static final String b37KGReferenceWithDecoy = "/humgen/gsa-hpprojects/GATK/bundle/current/b37/human_g1k_v37_decoy.fasta"; public static final String hg19RefereneWithChrPrefixInChromosomeNames = "/humgen/gsa-hpprojects/GATK/bundle/current/hg19/ucsc.hg19.fasta"; From a7fba5c2096a879e0f77842162fbd4f20bb261ca Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Thu, 25 Dec 2014 13:57:54 -0500 Subject: [PATCH 08/12] restructure and add more tests --- .../walkers/phasing/PhasingUtilsUnitTest.java | 146 ++++++++---------- 1 file changed, 65 insertions(+), 81 deletions(-) diff --git a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java index 57078fcc1..d544e9e38 100644 --- a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java +++ b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java @@ -56,13 +56,15 @@ import htsjdk.variant.variantcontext.*; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.broadinstitute.gatk.utils.GenomeLocParser; import org.broadinstitute.gatk.utils.BaseTest; import org.broadinstitute.gatk.utils.fasta.CachingIndexedFastaSequenceFile; import org.testng.Assert; -import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -70,31 +72,30 @@ public class PhasingUtilsUnitTest extends BaseTest { private final int start = 10; private ReferenceSequenceFile referenceFile; + private Genotype genotype1; + private Genotype genotype2; + private String contig; + private List alleleList1; + private List alleleList2; - @BeforeClass + @BeforeSuite public void init() throws FileNotFoundException { - referenceFile = new CachingIndexedFastaSequenceFile(new File(b37KGReference)); + referenceFile = new CachingIndexedFastaSequenceFile(new File("/Users/ronlevine/src/human_g1k_v37.fasta")); + alleleList1 = Arrays.asList(Allele.create("T", true), Allele.create("C", false)); + alleleList2 = Arrays.asList(Allele.create("G", true), Allele.create("A", false)); + genotype1 = new GenotypeBuilder().name("sample1").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList1).make(); + genotype2 = new GenotypeBuilder().name("sample2").attribute("HP", new String[]{"10-2", "10-1"}).alleles(alleleList2).make(); + contig = new String("1"); } @Test public void TestMatchHaplotypeAllelesKeyHP() { - final List alleleList1 = new ArrayList<>(); - alleleList1.add(Allele.create("T", false)); - alleleList1.add(Allele.create("C", false)); - final Genotype genotype1 = new GenotypeBuilder("TC").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList1).make(); - - // Attributes in different order - final List alleleList2 = new ArrayList<>(); - alleleList2.add(Allele.create("G", false)); - alleleList2.add(Allele.create("A", false)); - final Genotype genotype2 = new GenotypeBuilder("GA").attribute("HP", new String[]{"10-2", "10-1"}).alleles(alleleList2).make(); - PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotype1, genotype2); PhasingUtils.SameHaplotypeAlleles sameHaplotypeAllelesAnswer = new PhasingUtils.SameHaplotypeAlleles(); - sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", false), Allele.create("A", false))); - sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("G", false))); + sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", true), Allele.create("A", false))); + sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("G", true))); sameHaplotypeAllelesAnswer.requiresSwap = true; Assert.assertEquals(sameHaplotypeAlleles.hapAlleles, sameHaplotypeAllelesAnswer.hapAlleles); @@ -104,22 +105,12 @@ public class PhasingUtilsUnitTest extends BaseTest { @Test public void TestMatchHaplotypeAllelesNoKeyHP() { - final List alleleList1 = new ArrayList<>(); - alleleList1.add(Allele.create("T", false)); - alleleList1.add(Allele.create("C", false)); - final Genotype genotype1 = new GenotypeBuilder("TC").alleles(alleleList1).make(); - - // Attributes in different order - final List alleleList2 = new ArrayList<>(); - alleleList2.add(Allele.create("G", false)); - alleleList2.add(Allele.create("A", false)); - final Genotype genotype2 = new GenotypeBuilder("GA").alleles(alleleList2).make(); - - // Must match because the same genotype - PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotype1, genotype2); + Genotype genotype3 = new GenotypeBuilder().name("TC").alleles(alleleList1).make(); + Genotype genotype4 = new GenotypeBuilder().name("GA").alleles(alleleList2).make(); + PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotype3, genotype4); PhasingUtils.SameHaplotypeAlleles sameHaplotypeAllelesAnswer = new PhasingUtils.SameHaplotypeAlleles(); - sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", false), Allele.create("G", false))); + sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", true), Allele.create("G", true))); sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("A", false))); Assert.assertEquals(sameHaplotypeAlleles.hapAlleles, sameHaplotypeAllelesAnswer.hapAlleles); Assert.assertEquals(sameHaplotypeAlleles.requiresSwap, sameHaplotypeAllelesAnswer.requiresSwap); @@ -127,31 +118,49 @@ public class PhasingUtilsUnitTest extends BaseTest { @Test public void TestMergeIntoMNPvalidationTrueCheck() { - final String contig = new String("1"); - final List alleleList1 = new ArrayList<>(); - alleleList1.add(Allele.create("T", true)); - alleleList1.add(Allele.create("C", false)); - final List alleleList2 = new ArrayList<>(); - alleleList2.add(Allele.create("A", true)); - alleleList2.add(Allele.create("C", false)); - final VariantContext vc1 = new VariantContextBuilder("TC", contig, start, start, alleleList1).make(); - final VariantContext vc2 = new VariantContextBuilder("AC", contig, start+1, start+1, alleleList2).make(); + // TODO - genotypes must have the same sample name GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); + final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).make(); + final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).make(); Assert.assertTrue(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); } @Test public void TestMergeIntoMNPvalidationCheckLocBefore() { - final String contig = new String("1"); - final List alleleList1 = new ArrayList<>(); - alleleList1.add(Allele.create("T", true)); - alleleList1.add(Allele.create("C", false)); - final List alleleList2 = new ArrayList<>(); - alleleList2.add(Allele.create("A", true)); - alleleList2.add(Allele.create("C", false)); - final VariantContext vc1 = new VariantContextBuilder("TC", contig, start+1, start+1, alleleList1).make(); - final VariantContext vc2 = new VariantContextBuilder("AC", contig, start, start, alleleList2).make(); + final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start+1).stop(start+1).alleles(alleleList1).make(); + final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start).stop(start).alleles(alleleList2).make(); + GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); + + Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); + } + + @Test + public void TestMergeIntoMNPvalidationFiltered() { + List filters = Arrays.asList("filter"); + Genotype genotype = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList1).filters(filters).make(); + final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype).make(); + final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); + GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); + + Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); + } + + @Test + public void TestMergeIntoMNPvalidationNoCall() { + List alleleList = Arrays.asList(Allele.create("T", true), Allele.create(".", false)); + Genotype genotype = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList).make(); + final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype).make(); + final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); + GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); + + Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); + } + + @Test + public void TestMergeIntoMNPvalidationDiffSampleNames() { + final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype1).make(); + final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); @@ -159,16 +168,8 @@ public class PhasingUtilsUnitTest extends BaseTest { @Test public void TestMergeIntoMNPvalidationDiffContigs() { - final String contig1 = new String("1"); - final String contig2 = new String("2"); - final List alleleList1 = new ArrayList<>(); - alleleList1.add(Allele.create("T", true)); - alleleList1.add(Allele.create("C", false)); - final List alleleList2 = new ArrayList<>(); - alleleList2.add(Allele.create("A", true)); - alleleList2.add(Allele.create("C", false)); - final VariantContext vc1 = new VariantContextBuilder("TC", contig1, start+1, start+1, alleleList1).make(); - final VariantContext vc2 = new VariantContextBuilder("AC", contig2, start, start, alleleList2).make(); + final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype1).make(); + final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); @@ -182,40 +183,23 @@ public class PhasingUtilsUnitTest extends BaseTest { @Test public void TestMergeVariantContextAttributes() { - final String contig = new String("1"); - final List alleleList1 = new ArrayList<>(); - alleleList1.add(Allele.create("T", true)); - alleleList1.add(Allele.create("C", false)); - final List alleleList2 = new ArrayList<>(); - alleleList2.add(Allele.create("A", true)); - alleleList2.add(Allele.create("C", false)); - final VariantContext vc1 = new VariantContextBuilder("TC", contig, start, start, alleleList1).make(); - final VariantContext vc2 = new VariantContextBuilder("AC", contig, start+1, start+1, alleleList2).make(); + final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype1).make(); + final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); Assert.assertEquals(0, PhasingUtils.mergeVariantContextAttributes(vc1, vc2).size()); } @Test public void TestAllSamplesAreMergeableDiffSampleNames() { - final String contig = new String("1"); - final List alleleList1 = new ArrayList<>(); - alleleList1.add(Allele.create("T", true)); - alleleList1.add(Allele.create("C", false)); - final List alleleList2 = new ArrayList<>(); - alleleList2.add(Allele.create("A", true)); - alleleList2.add(Allele.create("C", false)); + final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype1).make(); + final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); - final Genotype g1 = new GenotypeBuilder().name("sample1").alleles(alleleList1).make(); - final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(g1).make(); - Genotype g2 = new GenotypeBuilder().name("sample2").alleles(alleleList2).make(); - final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("i21").source("AC").start(start).stop(start).alleles(alleleList2).genotypes(g2).make(); - - Assert.assertFalse( PhasingUtils.allSamplesAreMergeable(vc1, vc2)); + Assert.assertFalse(PhasingUtils.allSamplesAreMergeable(vc1, vc2)); } @Test public void TestAlleleSegregationIsKnown(){ - + Assert.assertTrue(PhasingUtils.alleleSegregationIsKnown(genotype1, genotype2)); } @Test From 714d575e3b3b6983700fd6e1e00b0058022ebd6c Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Thu, 25 Dec 2014 14:00:39 -0500 Subject: [PATCH 09/12] correct reference file name --- .../gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java index d544e9e38..5c0760dfa 100644 --- a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java +++ b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java @@ -80,7 +80,7 @@ public class PhasingUtilsUnitTest extends BaseTest { @BeforeSuite public void init() throws FileNotFoundException { - referenceFile = new CachingIndexedFastaSequenceFile(new File("/Users/ronlevine/src/human_g1k_v37.fasta")); + referenceFile = new CachingIndexedFastaSequenceFile(new File(b37KGReference)); alleleList1 = Arrays.asList(Allele.create("T", true), Allele.create("C", false)); alleleList2 = Arrays.asList(Allele.create("G", true), Allele.create("A", false)); genotype1 = new GenotypeBuilder().name("sample1").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList1).make(); From bb94833750cdc7ca46c51fd468d6d46b2fb2ec74 Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Tue, 30 Dec 2014 22:45:44 -0500 Subject: [PATCH 10/12] Add more tests --- .../tools/walkers/phasing/PhasingUtils.java | 10 +- .../walkers/phasing/PhasingUtilsUnitTest.java | 159 ++++++++++-------- 2 files changed, 98 insertions(+), 71 deletions(-) diff --git a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java index 5649a2096..e9bcb0eea 100644 --- a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java +++ b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java @@ -557,17 +557,19 @@ class PhasingUtils { } /** + * Ensure that the alleles are merged. + * Put the bases from all1 before and all2 after the previously merged bases * * @param all1 allele 1 * @param all2 allele 2 - * @param creatingReferenceForFirstTime + * @param isRef is this a reference allele * @return merged allele */ - private Allele ensureMergedAllele(Allele all1, Allele all2, boolean creatingReferenceForFirstTime) { + private Allele ensureMergedAllele(Allele all1, Allele all2, boolean isRef) { AlleleOneAndTwo all12 = new AlleleOneAndTwo(all1, all2); Allele mergedAllele = mergedAlleles.get(all12); - if (mergedAllele == null) { + if (mergedAllele == null) { final byte[] bases1 = all1.getBases(); final byte[] bases2 = all2.getBases(); @@ -577,7 +579,7 @@ class PhasingUtils { System.arraycopy(intermediateBases, 0, mergedBases, bases1.length, intermediateLength); System.arraycopy(bases2, 0, mergedBases, bases1.length + intermediateLength, bases2.length); - mergedAllele = Allele.create(mergedBases, creatingReferenceForFirstTime); + mergedAllele = Allele.create(mergedBases, isRef); mergedAlleles.put(all12, mergedAllele); } diff --git a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java index 5c0760dfa..b48db1894 100644 --- a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java +++ b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java @@ -55,9 +55,10 @@ import htsjdk.samtools.reference.ReferenceSequenceFile; import htsjdk.variant.variantcontext.*; import java.io.File; import java.io.FileNotFoundException; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.HashMap; +import java.util.Map; import org.broadinstitute.gatk.utils.GenomeLocParser; import org.broadinstitute.gatk.utils.BaseTest; @@ -67,134 +68,117 @@ import org.testng.annotations.BeforeSuite; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; +class AlwaysTrueMergeRule extends PhasingUtils.AlleleMergeRule { + public boolean allelesShouldBeMerged(VariantContext vc1, VariantContext vc2) { + return true; + } +} public class PhasingUtilsUnitTest extends BaseTest { private final int start = 10; + GenomeLocParser genomeLocParser; private ReferenceSequenceFile referenceFile; private Genotype genotype1; private Genotype genotype2; private String contig; private List alleleList1; private List alleleList2; + VariantContext vc1; + VariantContext vc2; @BeforeSuite public void init() throws FileNotFoundException { referenceFile = new CachingIndexedFastaSequenceFile(new File(b37KGReference)); + genomeLocParser = new GenomeLocParser(referenceFile); alleleList1 = Arrays.asList(Allele.create("T", true), Allele.create("C", false)); alleleList2 = Arrays.asList(Allele.create("G", true), Allele.create("A", false)); - genotype1 = new GenotypeBuilder().name("sample1").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList1).make(); - genotype2 = new GenotypeBuilder().name("sample2").attribute("HP", new String[]{"10-2", "10-1"}).alleles(alleleList2).make(); + genotype1 = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList1).make(); + genotype2 = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-2", "10-1"}).alleles(alleleList2).make(); contig = new String("1"); + vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype1).make(); + vc2 = new VariantContextBuilder().chr(contig).id("id2").source("GA").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); } @Test public void TestMatchHaplotypeAllelesKeyHP() { - PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotype1, genotype2); + final PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotype1, genotype2); - PhasingUtils.SameHaplotypeAlleles sameHaplotypeAllelesAnswer = new PhasingUtils.SameHaplotypeAlleles(); - sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", true), Allele.create("A", false))); - sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("G", true))); - sameHaplotypeAllelesAnswer.requiresSwap = true; + final PhasingUtils.SameHaplotypeAlleles sameHaplotypeAllelesExpected = new PhasingUtils.SameHaplotypeAlleles(); + sameHaplotypeAllelesExpected.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", true), Allele.create("A", false))); + sameHaplotypeAllelesExpected.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("G", true))); + sameHaplotypeAllelesExpected.requiresSwap = true; - Assert.assertEquals(sameHaplotypeAlleles.hapAlleles, sameHaplotypeAllelesAnswer.hapAlleles); - Assert.assertEquals(sameHaplotypeAlleles.requiresSwap, sameHaplotypeAllelesAnswer.requiresSwap); + Assert.assertEquals(sameHaplotypeAlleles.hapAlleles, sameHaplotypeAllelesExpected.hapAlleles); + Assert.assertEquals(sameHaplotypeAlleles.requiresSwap, sameHaplotypeAllelesExpected.requiresSwap); } @Test public void TestMatchHaplotypeAllelesNoKeyHP() { - Genotype genotype3 = new GenotypeBuilder().name("TC").alleles(alleleList1).make(); - Genotype genotype4 = new GenotypeBuilder().name("GA").alleles(alleleList2).make(); + final Genotype genotypeNoKeyHP1 = new GenotypeBuilder().name("TC").alleles(alleleList1).make(); + final Genotype genotypeNoKeyHP2 = new GenotypeBuilder().name("GA").alleles(alleleList2).make(); - PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotype3, genotype4); - PhasingUtils.SameHaplotypeAlleles sameHaplotypeAllelesAnswer = new PhasingUtils.SameHaplotypeAlleles(); - sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", true), Allele.create("G", true))); - sameHaplotypeAllelesAnswer.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("A", false))); - Assert.assertEquals(sameHaplotypeAlleles.hapAlleles, sameHaplotypeAllelesAnswer.hapAlleles); - Assert.assertEquals(sameHaplotypeAlleles.requiresSwap, sameHaplotypeAllelesAnswer.requiresSwap); + final PhasingUtils.SameHaplotypeAlleles sameHaplotypeAlleles = PhasingUtils.matchHaplotypeAlleles(genotypeNoKeyHP1, genotypeNoKeyHP2); + final PhasingUtils.SameHaplotypeAlleles sameHaplotypeAllelesExpected = new PhasingUtils.SameHaplotypeAlleles(); + sameHaplotypeAllelesExpected.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("T", true), Allele.create("G", true))); + sameHaplotypeAllelesExpected.hapAlleles.add(new PhasingUtils.AlleleOneAndTwo(Allele.create("C", false), Allele.create("A", false))); + Assert.assertEquals(sameHaplotypeAlleles.hapAlleles, sameHaplotypeAllelesExpected.hapAlleles); + Assert.assertEquals(sameHaplotypeAlleles.requiresSwap, sameHaplotypeAllelesExpected.requiresSwap); } @Test public void TestMergeIntoMNPvalidationTrueCheck() { - // TODO - genotypes must have the same sample name - GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); - final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).make(); - final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).make(); - Assert.assertTrue(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); } @Test public void TestMergeIntoMNPvalidationCheckLocBefore() { final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start+1).stop(start+1).alleles(alleleList1).make(); - final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start).stop(start).alleles(alleleList2).make(); - GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); - + final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("GA").start(start).stop(start).alleles(alleleList2).make(); Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); } @Test public void TestMergeIntoMNPvalidationFiltered() { - List filters = Arrays.asList("filter"); - Genotype genotype = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList1).filters(filters).make(); - final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype).make(); - final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); - GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); - - Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); + final List filters = Arrays.asList("filter"); + final Genotype genotype = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList1).filters(filters).make(); + final VariantContext vc = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype).make(); + Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc, vc2)); } @Test - public void TestMergeIntoMNPvalidationNoCall() { - List alleleList = Arrays.asList(Allele.create("T", true), Allele.create(".", false)); - Genotype genotype = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList).make(); - final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype).make(); - final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); - GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); - - Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); + public void TestMergeIntoMNPvalidationFilterNoCall() { + final List filters = Arrays.asList("filter"); + final List alleleList = Arrays.asList(Allele.create("T", true), Allele.create(".", false)); + final Genotype genotype = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList).filters(filters).make(); + final VariantContext vc = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype).make(); + Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc, vc2)); } @Test public void TestMergeIntoMNPvalidationDiffSampleNames() { - final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype1).make(); - final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); - GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); - - Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); + final Genotype genotype = new GenotypeBuilder().name("sample1").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList1).make(); + final VariantContext vc = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype).make(); + Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc, vc2)); } @Test public void TestMergeIntoMNPvalidationDiffContigs() { - final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype1).make(); - final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); - GenomeLocParser genomeLocParser = new GenomeLocParser(referenceFile); - - Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); - } - - @Test - public void TestMergeVariantContextNames() { - final String result = new String("A_B"); - Assert.assertEquals(result, PhasingUtils.mergeVariantContextNames("A", "B")); + final String contig = new String("2"); + final VariantContext vc = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); + Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc)); } @Test public void TestMergeVariantContextAttributes() { - final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype1).make(); - final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); - Assert.assertEquals(0, PhasingUtils.mergeVariantContextAttributes(vc1, vc2).size()); } @Test - public void TestAllSamplesAreMergeableDiffSampleNames() { - final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype1).make(); - final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("AC").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); - - Assert.assertFalse(PhasingUtils.allSamplesAreMergeable(vc1, vc2)); + public void TestAllSamplesAreMergeable() { + Assert.assertTrue(PhasingUtils.allSamplesAreMergeable(vc1, vc2)); } @Test @@ -204,11 +188,52 @@ public class PhasingUtilsUnitTest extends BaseTest { @Test public void TestSomeSampleHasDoubleNonReferenceAllele(){ - + Assert.assertFalse(PhasingUtils.someSampleHasDoubleNonReferenceAllele(vc1, vc2)); } @Test public void TestDoubleAllelesSegregatePerfectlyAmongSamples(){ - + Assert.assertFalse(PhasingUtils.doubleAllelesSegregatePerfectlyAmongSamples(vc1, vc2)); } + + @Test + public void TestMergeVariantContextNames() { + final String result = new String("A_B"); + Assert.assertEquals(result, PhasingUtils.mergeVariantContextNames("A", "B")); + } + + @Test + public void TestMergeIntoMNP(){ + AlwaysTrueMergeRule alleleMergeRule = new AlwaysTrueMergeRule(); + VariantContext vc = PhasingUtils.mergeIntoMNP(genomeLocParser, vc1, vc2, referenceFile, alleleMergeRule); + + Map attributes = new HashMap(); + attributes.put("AC", new String[]{"1", "1"}); + attributes.put("AF", new String[]{"0.5", "0.5"}); + attributes.put("AN", new String("2")); + List alleleList = Arrays.asList(Allele.create("TG", true), Allele.create("TA", false), Allele.create("CG", false)); + Genotype genotype = new GenotypeBuilder().name("sample").attributes(attributes).alleles(alleleList).make(); + final VariantContext vcExpected = new VariantContextBuilder().chr(contig).id("id1;id2").source("TC_AC").start(start).stop(start+1).alleles(alleleList).genotypes(genotype).make(); + } + + @Test + public void TestReallyMergeIntoMNP(final VariantContext vc1, final VariantContext vc2 ){ + VariantContext vc = PhasingUtils.reallyMergeIntoMNP(vc1, vc2, referenceFile); + } + + @Test + public void TestAllGenotypesAreUnfilteredAndCalled(){ + final VariantContext vc = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype1).make(); + Assert.assertTrue(PhasingUtils.allGenotypesAreUnfilteredAndCalled(vc)); + } + + @Test + public void TestEnsureMergedAllele(){ + byte[] intermediateBases = new byte[]{'A','T'}; + final PhasingUtils.MergedAllelesData mergeData = new PhasingUtils.MergedAllelesData(intermediateBases, vc1, vc2); + Allele allele = mergeData.ensureMergedAllele(Allele.create("T", true), Allele.create("C", true)); + Allele expectedAllele = Allele.create(new byte[]{'T', 'A', 'T', 'C'}, false); + Assert.assertEquals(allele, expectedAllele); + } + } From 85dc7034619dccf9278a0c2d0cc9c1a04b7b04f0 Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Thu, 1 Jan 2015 09:51:20 -0500 Subject: [PATCH 11/12] Add TestMergeIntoMNP() and TestReallyMergeIntoMNP() --- .../tools/walkers/phasing/PhasingUtils.java | 77 +++++++++++-------- .../walkers/phasing/PhasingUtilsUnitTest.java | 73 +++++++++++++++--- 2 files changed, 107 insertions(+), 43 deletions(-) diff --git a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java index e9bcb0eea..43f6376eb 100644 --- a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java +++ b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java @@ -71,8 +71,8 @@ class PhasingUtils { * Merge variants into an MNP * * @param genomeLocParser parse the genome locations - * @param vc1 variant 1 - * @param vc2 variant 2 + * @param vc1 variant context 1 + * @param vc2 variant context 2 * @param referenceFile sequence file containing the reference genome * @param alleleMergeRule rule for merging variants * @return merged variant or null if the variants are NOT an SNP or MNP, on the same contig, variant location 1 is the same or after the variant location 2, @@ -162,8 +162,8 @@ class PhasingUtils { /** * Merge variants into an MNP * - * @param vc1 variant 1 - * @param vc2 variant 2 + * @param vc1 variant context 1 + * @param vc2 variant context 2 * @param referenceFile sequence file containing the reference genome * @return variant with the merged MNP */ @@ -171,16 +171,21 @@ class PhasingUtils { final int startInter = vc1.getEnd() + 1; final int endInter = vc2.getStart() - 1; byte[] intermediateBases = null; + + // get bases between vc1 and vc2 in the reference sequence if (startInter <= endInter) { intermediateBases = referenceFile.getSubsequenceAt(vc1.getChr(), startInter, endInter).getBases(); StringUtil.toUpperCase(intermediateBases); } + + // merge the reference bases with vc1 and vc2 final MergedAllelesData mergeData = new MergedAllelesData(intermediateBases, vc1, vc2); // ensures that the reference allele is added final GenotypesContext mergedGenotypes = GenotypesContext.create(); for (final Genotype gt1 : vc1.getGenotypes()) { final Genotype gt2 = vc2.getGenotype(gt1.getSampleName()); + // Alleles with the same haplotype final SameHaplotypeAlleles hapAlleles = matchHaplotypeAlleles(gt1, gt2); boolean isPhased = gt1.isPhased() && gt2.isPhased(); @@ -197,6 +202,7 @@ class PhasingUtils { final Map mergedGtAttribs = new HashMap(); + // get the min read backed phasing quality double PQ = Double.MAX_VALUE; if (gt1.hasAnyAttribute(ReadBackedPhasing.PQ_KEY)) { PQ = Math.min(PQ, (double) gt1.getAnyAttribute(ReadBackedPhasing.PQ_KEY)); @@ -207,6 +213,7 @@ class PhasingUtils { if (PQ != Double.MAX_VALUE) mergedGtAttribs.put(ReadBackedPhasing.PQ_KEY, PQ); + // get the read backed phasing phasing haplotype identifier if (gt1.hasAnyAttribute(ReadBackedPhasing.HP_KEY)) { mergedGtAttribs.put(ReadBackedPhasing.HP_KEY, gt1.getAnyAttribute(ReadBackedPhasing.HP_KEY)); } @@ -214,21 +221,24 @@ class PhasingUtils { mergedGtAttribs.put(ReadBackedPhasing.HP_KEY, gt2.getAnyAttribute(ReadBackedPhasing.HP_KEY)); } + // make the merged genotype final Genotype mergedGt = new GenotypeBuilder(gt1.getSampleName(), mergedAllelesForSample).log10PError(mergedGQ).attributes(mergedGtAttribs).phased(isPhased).make(); mergedGenotypes.add(mergedGt); } + // get the merged name final String mergedName = mergeVariantContextNames(vc1.getSource(), vc2.getSource()); final double mergedLog10PError = Math.min(vc1.getLog10PError(), vc2.getLog10PError()); final Set mergedFilters = new HashSet(); // Since vc1 and vc2 were unfiltered, the merged record remains unfiltered final Map mergedAttribs = mergeVariantContextAttributes(vc1, vc2); - // ids + // get the merged ID final List mergedIDs = new ArrayList(); if ( vc1.hasID() ) mergedIDs.add(vc1.getID()); if ( vc2.hasID() ) mergedIDs.add(vc2.getID()); final String mergedID = mergedIDs.isEmpty() ? VCFConstants.EMPTY_ID_FIELD : Utils.join(VCFConstants.ID_FIELD_SEPARATOR, mergedIDs); + // make the merged variant context final VariantContextBuilder mergedBuilder = new VariantContextBuilder(mergedName, vc1.getChr(), vc1.getStart(), vc2.getEnd(), mergeData.getAllMergedAlleles()).id(mergedID).genotypes(mergedGenotypes).log10PError(mergedLog10PError).filters(mergedFilters).attributes(mergedAttribs); VariantContextUtils.calculateChromosomeCounts(mergedBuilder, true); return mergedBuilder.make(); @@ -237,8 +247,8 @@ class PhasingUtils { /** * Merge two variant context names together with an underscore betwwen them * - * @param name1 variant 1 name - * @param name2 variant 2 name + * @param name1 variant context 1 name + * @param name2 variant context 2 name * @return combined variant names */ static String mergeVariantContextNames(String name1, String name2) { @@ -249,8 +259,8 @@ class PhasingUtils { * Get preset attributes and whether they are in vc1 or vc2 * TODO: Will always return an empty map because MERGE_OR_ATTRIBS is empty * - * @param vc1 variant 1 - * @param vc2 variant 2 + * @param vc1 variant context 1 + * @param vc2 variant context 2 * @return whether the preset attributes are in vc1 or vc2 */ static Map mergeVariantContextAttributes(VariantContext vc1, VariantContext vc2) { @@ -282,8 +292,8 @@ class PhasingUtils { * Check if variants can be merged into the MNP * * @param genomeLocParser parse the genome locations - * @param vc1 variant 1 - * @param vc2 variant 2 + * @param vc1 variant context 1 + * @param vc2 variant context 2 * @return true if variants are an SNP or MNP, on the same contig, variant location 1 is not before variant location 2, unfiltered, from the same sample set and are called, * false otherwise */ @@ -301,7 +311,7 @@ class PhasingUtils { if (!loc1.onSameContig(loc2)) return false; - // Variant 1 location must not be before variant 2 + // Variant 1 location must not be before variant context 2 if (!loc1.isBefore(loc2)) return false; @@ -332,8 +342,8 @@ class PhasingUtils { /** * Check if can merge variants * - * @param vc1 variant 1 - * @param vc2 variant 2 + * @param vc1 variant context 1 + * @param vc2 variant context 2 * @return true if variants are phased or either is a homozygous, false otherwise */ static boolean allSamplesAreMergeable(VariantContext vc1, VariantContext vc2) { @@ -386,23 +396,26 @@ class PhasingUtils { } /** - * Check of any samples have alternate alleles + * Check if some samples have double alternate alleles * - * @param vc1 variant 1 - * @param vc2 variant 2 - * @return true if there is a sample with alternate alleles, false otherwise + * @param vc1 variant context 1 + * @param vc2 variant context 2 + * @return true if there is a sample with double alternate alleles, false otherwise */ static boolean someSampleHasDoubleNonReferenceAllele(VariantContext vc1, VariantContext vc2) { for (final Genotype gt1 : vc1.getGenotypes()) { + // gt2 from the same sample as gt1 final Genotype gt2 = vc2.getGenotype(gt1.getSampleName()); - // Find the alleles with the same haplotype - final SameHaplotypeAlleles hapAlleles = matchHaplotypeAlleles(gt1, gt2); + if ( gt2 != null ) { + // Find the alleles with the same haplotype + final SameHaplotypeAlleles hapAlleles = matchHaplotypeAlleles(gt1, gt2); - // Find corresponding alternate alleles - for (AlleleOneAndTwo all1all2 : hapAlleles.hapAlleles) { - if (all1all2.all1.isNonReference() && all1all2.all2.isNonReference()) - return true; + // Find corresponding alternate alleles + for (AlleleOneAndTwo all1all2 : hapAlleles.hapAlleles) { + if (all1all2.all1.isNonReference() && all1all2.all2.isNonReference()) + return true; + } } } @@ -412,9 +425,9 @@ class PhasingUtils { /** * Check that Alleles at vc1 and at vc2 always segregate together in all samples (including reference) * - * @param vc1 variant 1 - * @param vc2 variant 2 - * @return true if + * @param vc1 variant context 1 + * @param vc2 variant context 2 + * @return true if alleles segregate together, false otherwise */ static boolean doubleAllelesSegregatePerfectlyAmongSamples(VariantContext vc1, VariantContext vc2) { final Map allele1ToAllele2 = new HashMap(); @@ -535,18 +548,20 @@ class PhasingUtils { * Constructor * * @param intermediateBases array of bases - * @param vc1 variant 1 - * @param vc2 variant 2 + * @param vc1 variant context 1 + * @param vc2 variant context 2 */ public MergedAllelesData(byte[] intermediateBases, VariantContext vc1, VariantContext vc2) { this.mergedAlleles = new HashMap(); // implemented equals() and hashCode() for AlleleOneAndTwo this.intermediateBases = intermediateBases; this.intermediateLength = this.intermediateBases != null ? this.intermediateBases.length : 0; + // merge the reference bases from vc1 before and vc2 after the reference (intermediate) bases this.ensureMergedAllele(vc1.getReference(), vc2.getReference(), true); } /** + * Ensure that the alleles are merged. The merged allele is alternate. * * @param all1 allele 1 * @param all2 allele 2 @@ -558,11 +573,11 @@ class PhasingUtils { /** * Ensure that the alleles are merged. - * Put the bases from all1 before and all2 after the previously merged bases + * Put the bases from all1 before and all2 after the intermediate bases * * @param all1 allele 1 * @param all2 allele 2 - * @param isRef is this a reference allele + * @param isRef if true, merged allele is reference, if false, merged allele is alternate * @return merged allele */ private Allele ensureMergedAllele(Allele all1, Allele all2, boolean isRef) { diff --git a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java index b48db1894..6dfe0f416 100644 --- a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java +++ b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java @@ -56,6 +56,7 @@ import htsjdk.variant.variantcontext.*; import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; +import java.util.ArrayList; import java.util.List; import java.util.HashMap; import java.util.Map; @@ -65,7 +66,6 @@ import org.broadinstitute.gatk.utils.BaseTest; import org.broadinstitute.gatk.utils.fasta.CachingIndexedFastaSequenceFile; import org.testng.Assert; import org.testng.annotations.BeforeSuite; -import org.testng.annotations.DataProvider; import org.testng.annotations.Test; class AlwaysTrueMergeRule extends PhasingUtils.AlleleMergeRule { @@ -93,8 +93,8 @@ public class PhasingUtilsUnitTest extends BaseTest { genomeLocParser = new GenomeLocParser(referenceFile); alleleList1 = Arrays.asList(Allele.create("T", true), Allele.create("C", false)); alleleList2 = Arrays.asList(Allele.create("G", true), Allele.create("A", false)); - genotype1 = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList1).make(); - genotype2 = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-2", "10-1"}).alleles(alleleList2).make(); + genotype1 = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).attribute("PQ", 100.0).alleles(alleleList1).make(); + genotype2 = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-2", "10-1"}).attribute("PQ", 200.0).alleles(alleleList2).make(); contig = new String("1"); vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start).stop(start).alleles(alleleList1).genotypes(genotype1).make(); vc2 = new VariantContextBuilder().chr(contig).id("id2").source("GA").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype2).make(); @@ -187,13 +187,22 @@ public class PhasingUtilsUnitTest extends BaseTest { } @Test - public void TestSomeSampleHasDoubleNonReferenceAllele(){ + public void TestSomeSampleHasDoubleNonReferenceAlleleTrue(){ + Genotype genotype = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList2).make(); + VariantContext vc = new VariantContextBuilder().chr(contig).id("id2").source("GA").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype).make(); + Assert.assertTrue(PhasingUtils.someSampleHasDoubleNonReferenceAllele(vc1, vc)); + } + + @Test + public void TestSomeSampleHasDoubleNonReferenceAlleleFalse(){ Assert.assertFalse(PhasingUtils.someSampleHasDoubleNonReferenceAllele(vc1, vc2)); } @Test public void TestDoubleAllelesSegregatePerfectlyAmongSamples(){ - Assert.assertFalse(PhasingUtils.doubleAllelesSegregatePerfectlyAmongSamples(vc1, vc2)); + Genotype genotype = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList2).make(); + VariantContext vc = new VariantContextBuilder().chr(contig).id("id2").source("GA").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype).make(); + Assert.assertTrue(PhasingUtils.doubleAllelesSegregatePerfectlyAmongSamples(vc1, vc)); } @Test @@ -207,18 +216,58 @@ public class PhasingUtilsUnitTest extends BaseTest { AlwaysTrueMergeRule alleleMergeRule = new AlwaysTrueMergeRule(); VariantContext vc = PhasingUtils.mergeIntoMNP(genomeLocParser, vc1, vc2, referenceFile, alleleMergeRule); - Map attributes = new HashMap(); - attributes.put("AC", new String[]{"1", "1"}); - attributes.put("AF", new String[]{"0.5", "0.5"}); - attributes.put("AN", new String("2")); List alleleList = Arrays.asList(Allele.create("TG", true), Allele.create("TA", false), Allele.create("CG", false)); - Genotype genotype = new GenotypeBuilder().name("sample").attributes(attributes).alleles(alleleList).make(); - final VariantContext vcExpected = new VariantContextBuilder().chr(contig).id("id1;id2").source("TC_AC").start(start).stop(start+1).alleles(alleleList).genotypes(genotype).make(); + Map attributes = new HashMap(){{ + put("AC", new ArrayList(Arrays.asList(1, 1))); + put("AF", new ArrayList(Arrays.asList(0.5, 0.5))); + put("AN", 2); + }}; + final Map extendedAttributes = new HashMap(){{ + put("PQ", 100.0); put("HP", new String[]{"10-1", "10-2"}); + }}; + List alleleListMeged = Arrays.asList(Allele.create("TA"), Allele.create("CG")); + Genotype genotype = new GenotypeBuilder().name("sample").attributes(extendedAttributes).alleles(alleleListMeged).make(); + final VariantContext vcExpected = new VariantContextBuilder().chr(contig).id("id1;id2").source("TC_GA").start(start).stop(start+1).alleles(alleleList).genotypes(genotype).attributes(attributes).make(); + Assert.assertTrue(genotype.sameGenotype(vcExpected.getGenotypes().get("sample"))); + Assert.assertTrue(vcExpected.hasSameAllelesAs(vc)); + Assert.assertEquals(vcExpected.getChr(), vc.getChr()); + Assert.assertEquals(vcExpected.getStart(), vc.getStart()); + Assert.assertEquals(vcExpected.getEnd(), vc.getEnd()); + Assert.assertEquals(vcExpected.getID(), vc.getID()); + Assert.assertEquals(vcExpected.getSource(), vc.getSource()); + Assert.assertEquals(vcExpected.isFiltered(), vc.isFiltered()); + Assert.assertEquals(vcExpected.getPhredScaledQual(), vc.getPhredScaledQual()); + Assert.assertEquals(vcExpected.getAttribute("PQ"), vc.getAttribute("PQ")); + Assert.assertEquals(vcExpected.getAttribute("HP"), vc.getAttribute("HP")); } @Test - public void TestReallyMergeIntoMNP(final VariantContext vc1, final VariantContext vc2 ){ + public void TestReallyMergeIntoMNP( ){ VariantContext vc = PhasingUtils.reallyMergeIntoMNP(vc1, vc2, referenceFile); + + List alleleList = Arrays.asList(Allele.create("TG", true), Allele.create("TA", false), Allele.create("CG", false)); + Map attributes = new HashMap(){{ + put("AC", new ArrayList(Arrays.asList(1, 1))); + put("AF", new ArrayList(Arrays.asList(0.5, 0.5))); + put("AN", 2); + }}; + final Map extendedAttributes = new HashMap(){{ + put("PQ", 100.0); put("HP", new String[]{"10-1", "10-2"}); + }}; + List alleleListMeged = Arrays.asList(Allele.create("TA"), Allele.create("CG")); + Genotype genotype = new GenotypeBuilder().name("sample").attributes(extendedAttributes).alleles(alleleListMeged).make(); + final VariantContext vcExpected = new VariantContextBuilder().chr(contig).id("id1;id2").source("TC_GA").start(start).stop(start+1).alleles(alleleList).genotypes(genotype).attributes(attributes).make(); + Assert.assertTrue(genotype.sameGenotype(vcExpected.getGenotypes().get("sample"))); + Assert.assertTrue(vcExpected.hasSameAllelesAs(vc)); + Assert.assertEquals(vcExpected.getChr(), vc.getChr()); + Assert.assertEquals(vcExpected.getStart(), vc.getStart()); + Assert.assertEquals(vcExpected.getEnd(), vc.getEnd()); + Assert.assertEquals(vcExpected.getID(), vc.getID()); + Assert.assertEquals(vcExpected.getSource(), vc.getSource()); + Assert.assertEquals(vcExpected.isFiltered(), vc.isFiltered()); + Assert.assertEquals(vcExpected.getPhredScaledQual(), vc.getPhredScaledQual()); + Assert.assertEquals(vcExpected.getAttribute("PQ"), vc.getAttribute("PQ")); + Assert.assertEquals(vcExpected.getAttribute("HP"), vc.getAttribute("HP")); } @Test From c6840124fe3b76bd610b6a89bbb18f254c377a38 Mon Sep 17 00:00:00 2001 From: Ron Levine Date: Sun, 4 Jan 2015 23:01:24 -0500 Subject: [PATCH 12/12] clean up, add final --- .../tools/walkers/phasing/PhasingUtils.java | 29 ++++++++------- .../walkers/phasing/PhasingUtilsUnitTest.java | 36 +++++++++---------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java index 43f6376eb..df70f8458 100644 --- a/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java +++ b/protected/gatk-tools-protected/src/main/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtils.java @@ -68,7 +68,7 @@ import java.util.*; class PhasingUtils { /** - * Merge variants into an MNP + * Merge variants into a multi-nucleotide polymorphism (MNP) * * @param genomeLocParser parse the genome locations * @param vc1 variant context 1 @@ -160,7 +160,7 @@ class PhasingUtils { } /** - * Merge variants into an MNP + * Merge variants into a multi-nucleotide polymorphism (MNP) * * @param vc1 variant context 1 * @param vc2 variant context 2 @@ -245,23 +245,23 @@ class PhasingUtils { } /** - * Merge two variant context names together with an underscore betwwen them + * Merge variant context names * * @param name1 variant context 1 name * @param name2 variant context 2 name - * @return combined variant names + * @return merged variant names (name1_name2) */ static String mergeVariantContextNames(String name1, String name2) { return name1 + "_" + name2; } /** - * Get preset attributes and whether they are in vc1 or vc2 + * Get preset attributes and that are in vc1 or vc2 * TODO: Will always return an empty map because MERGE_OR_ATTRIBS is empty * * @param vc1 variant context 1 * @param vc2 variant context 2 - * @return whether the preset attributes are in vc1 or vc2 + * @return merged attributes in vc1 or vc2 */ static Map mergeVariantContextAttributes(VariantContext vc1, VariantContext vc2) { // Map of attribute name to value @@ -289,7 +289,7 @@ class PhasingUtils { } /** - * Check if variants can be merged into the MNP + * Check if variants can be merged into the multi-nucleotide polymorphism (MNP) * * @param genomeLocParser parse the genome locations * @param vc1 variant context 1 @@ -340,7 +340,7 @@ class PhasingUtils { } /** - * Check if can merge variants + * Check if can merge genotypes from the same sample * * @param vc1 variant context 1 * @param vc2 variant context 2 @@ -423,7 +423,7 @@ class PhasingUtils { } /** - * Check that Alleles at vc1 and at vc2 always segregate together in all samples (including reference) + * Check that alleles at vc1 and at vc2 always segregate together in all samples (including reference) * * @param vc1 variant context 1 * @param vc2 variant context 2 @@ -464,7 +464,7 @@ class PhasingUtils { } /** - * Class for rule for merging variants + * Class for variants merging rules */ abstract static class AlleleMergeRule { // vc1, vc2 are ONLY passed to allelesShouldBeMerged() if mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2) AND allSamplesAreMergeable(vc1, vc2): @@ -537,11 +537,16 @@ class PhasingUtils { } /** - * Class for merging alleles data + * Class for merging alleles */ static class MergedAllelesData { + /// merged alleles private Map mergedAlleles; + + /// bases between the alleles private byte[] intermediateBases; + + /// number of bases between the alleles private int intermediateLength; /** @@ -573,7 +578,7 @@ class PhasingUtils { /** * Ensure that the alleles are merged. - * Put the bases from all1 before and all2 after the intermediate bases + * all1 is before all2, if there is a gap between them, join with the intermediate bases * * @param all1 allele 1 * @param all2 allele 2 diff --git a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java index 6dfe0f416..26a4a92f7 100644 --- a/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java +++ b/protected/gatk-tools-protected/src/test/java/org/broadinstitute/gatk/tools/walkers/phasing/PhasingUtilsUnitTest.java @@ -135,9 +135,9 @@ public class PhasingUtilsUnitTest extends BaseTest { @Test public void TestMergeIntoMNPvalidationCheckLocBefore() { - final VariantContext vc1 = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start+1).stop(start+1).alleles(alleleList1).make(); - final VariantContext vc2 = new VariantContextBuilder().chr(contig).id("id2").source("GA").start(start).stop(start).alleles(alleleList2).make(); - Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1, vc2)); + final VariantContext vc1before = new VariantContextBuilder().chr(contig).id("id1").source("TC").start(start+1).stop(start+1).alleles(alleleList1).make(); + final VariantContext vc2after = new VariantContextBuilder().chr(contig).id("id2").source("GA").start(start).stop(start).alleles(alleleList2).make(); + Assert.assertFalse(PhasingUtils.mergeIntoMNPvalidationCheck(genomeLocParser, vc1before, vc2after)); } @Test @@ -200,8 +200,8 @@ public class PhasingUtilsUnitTest extends BaseTest { @Test public void TestDoubleAllelesSegregatePerfectlyAmongSamples(){ - Genotype genotype = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList2).make(); - VariantContext vc = new VariantContextBuilder().chr(contig).id("id2").source("GA").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype).make(); + final Genotype genotype = new GenotypeBuilder().name("sample").attribute("HP", new String[]{"10-1", "10-2"}).alleles(alleleList2).make(); + final VariantContext vc = new VariantContextBuilder().chr(contig).id("id2").source("GA").start(start+1).stop(start+1).alleles(alleleList2).genotypes(genotype).make(); Assert.assertTrue(PhasingUtils.doubleAllelesSegregatePerfectlyAmongSamples(vc1, vc)); } @@ -213,11 +213,11 @@ public class PhasingUtilsUnitTest extends BaseTest { @Test public void TestMergeIntoMNP(){ - AlwaysTrueMergeRule alleleMergeRule = new AlwaysTrueMergeRule(); - VariantContext vc = PhasingUtils.mergeIntoMNP(genomeLocParser, vc1, vc2, referenceFile, alleleMergeRule); + final AlwaysTrueMergeRule alleleMergeRule = new AlwaysTrueMergeRule(); + final VariantContext vc = PhasingUtils.mergeIntoMNP(genomeLocParser, vc1, vc2, referenceFile, alleleMergeRule); - List alleleList = Arrays.asList(Allele.create("TG", true), Allele.create("TA", false), Allele.create("CG", false)); - Map attributes = new HashMap(){{ + final List alleleList = Arrays.asList(Allele.create("TG", true), Allele.create("TA", false), Allele.create("CG", false)); + final Map attributes = new HashMap(){{ put("AC", new ArrayList(Arrays.asList(1, 1))); put("AF", new ArrayList(Arrays.asList(0.5, 0.5))); put("AN", 2); @@ -225,8 +225,8 @@ public class PhasingUtilsUnitTest extends BaseTest { final Map extendedAttributes = new HashMap(){{ put("PQ", 100.0); put("HP", new String[]{"10-1", "10-2"}); }}; - List alleleListMeged = Arrays.asList(Allele.create("TA"), Allele.create("CG")); - Genotype genotype = new GenotypeBuilder().name("sample").attributes(extendedAttributes).alleles(alleleListMeged).make(); + final List alleleListMeged = Arrays.asList(Allele.create("TA"), Allele.create("CG")); + final Genotype genotype = new GenotypeBuilder().name("sample").attributes(extendedAttributes).alleles(alleleListMeged).make(); final VariantContext vcExpected = new VariantContextBuilder().chr(contig).id("id1;id2").source("TC_GA").start(start).stop(start+1).alleles(alleleList).genotypes(genotype).attributes(attributes).make(); Assert.assertTrue(genotype.sameGenotype(vcExpected.getGenotypes().get("sample"))); Assert.assertTrue(vcExpected.hasSameAllelesAs(vc)); @@ -243,10 +243,10 @@ public class PhasingUtilsUnitTest extends BaseTest { @Test public void TestReallyMergeIntoMNP( ){ - VariantContext vc = PhasingUtils.reallyMergeIntoMNP(vc1, vc2, referenceFile); + final VariantContext vc = PhasingUtils.reallyMergeIntoMNP(vc1, vc2, referenceFile); - List alleleList = Arrays.asList(Allele.create("TG", true), Allele.create("TA", false), Allele.create("CG", false)); - Map attributes = new HashMap(){{ + final List alleleList = Arrays.asList(Allele.create("TG", true), Allele.create("TA", false), Allele.create("CG", false)); + final Map attributes = new HashMap(){{ put("AC", new ArrayList(Arrays.asList(1, 1))); put("AF", new ArrayList(Arrays.asList(0.5, 0.5))); put("AN", 2); @@ -254,8 +254,8 @@ public class PhasingUtilsUnitTest extends BaseTest { final Map extendedAttributes = new HashMap(){{ put("PQ", 100.0); put("HP", new String[]{"10-1", "10-2"}); }}; - List alleleListMeged = Arrays.asList(Allele.create("TA"), Allele.create("CG")); - Genotype genotype = new GenotypeBuilder().name("sample").attributes(extendedAttributes).alleles(alleleListMeged).make(); + final List alleleListMeged = Arrays.asList(Allele.create("TA"), Allele.create("CG")); + final Genotype genotype = new GenotypeBuilder().name("sample").attributes(extendedAttributes).alleles(alleleListMeged).make(); final VariantContext vcExpected = new VariantContextBuilder().chr(contig).id("id1;id2").source("TC_GA").start(start).stop(start+1).alleles(alleleList).genotypes(genotype).attributes(attributes).make(); Assert.assertTrue(genotype.sameGenotype(vcExpected.getGenotypes().get("sample"))); Assert.assertTrue(vcExpected.hasSameAllelesAs(vc)); @@ -280,8 +280,8 @@ public class PhasingUtilsUnitTest extends BaseTest { public void TestEnsureMergedAllele(){ byte[] intermediateBases = new byte[]{'A','T'}; final PhasingUtils.MergedAllelesData mergeData = new PhasingUtils.MergedAllelesData(intermediateBases, vc1, vc2); - Allele allele = mergeData.ensureMergedAllele(Allele.create("T", true), Allele.create("C", true)); - Allele expectedAllele = Allele.create(new byte[]{'T', 'A', 'T', 'C'}, false); + final Allele allele = mergeData.ensureMergedAllele(Allele.create("T", true), Allele.create("C", true)); + final Allele expectedAllele = Allele.create(new byte[]{'T', 'A', 'T', 'C'}, false); Assert.assertEquals(allele, expectedAllele); }