From b1ac4b81d5bd3dc03a796917ce60d8b0a66d0108 Mon Sep 17 00:00:00 2001 From: ebanks Date: Mon, 28 Dec 2009 17:28:51 +0000 Subject: [PATCH] Optimization: look up diploid genotypes from a static matrix instead of creating them on the fly (with String.format); bases no longer need to be ordered appropriately git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2448 348d0f76-0448-11de-a6fe-93d51630548a --- .../DiploidGenotypeCalculationModel.java | 4 +- .../sting/utils/genotype/DiploidGenotype.java | 37 +++++++++++-------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/DiploidGenotypeCalculationModel.java b/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/DiploidGenotypeCalculationModel.java index 1a1b205e1..d2f579eb1 100755 --- a/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/DiploidGenotypeCalculationModel.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/DiploidGenotypeCalculationModel.java @@ -46,7 +46,7 @@ public class DiploidGenotypeCalculationModel extends JointEstimateGenotypeCalcul DiploidGenotype refGenotype = DiploidGenotype.createHomGenotype(ref); for ( char alt : BaseUtils.BASES ) { if ( alt != ref ) { - DiploidGenotype hetGenotype = DiploidGenotype.unorderedValueOf(ref, alt); + DiploidGenotype hetGenotype = DiploidGenotype.createDiploidGenotype(ref, alt); DiploidGenotype homGenotype = DiploidGenotype.createHomGenotype(alt); AFMatrixMap.get(alt).setLikelihoods(posteriors[refGenotype.ordinal()], posteriors[hetGenotype.ordinal()], posteriors[homGenotype.ordinal()], sample); } @@ -78,7 +78,7 @@ public class DiploidGenotypeCalculationModel extends JointEstimateGenotypeCalcul // set up some variables we'll need in the loop AlleleFrequencyMatrix matrix = AFMatrixMap.get(alt); DiploidGenotype refGenotype = DiploidGenotype.createHomGenotype(ref); - DiploidGenotype hetGenotype = DiploidGenotype.unorderedValueOf(ref, alt); + DiploidGenotype hetGenotype = DiploidGenotype.createDiploidGenotype(ref, alt); DiploidGenotype homGenotype = DiploidGenotype.createHomGenotype(alt); for ( String sample : GLs.keySet() ) { diff --git a/java/src/org/broadinstitute/sting/utils/genotype/DiploidGenotype.java b/java/src/org/broadinstitute/sting/utils/genotype/DiploidGenotype.java index 51652d0cf..19117be2a 100755 --- a/java/src/org/broadinstitute/sting/utils/genotype/DiploidGenotype.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/DiploidGenotype.java @@ -1,6 +1,7 @@ package org.broadinstitute.sting.utils.genotype; import org.broadinstitute.sting.utils.Utils; +import org.broadinstitute.sting.utils.BaseUtils; /** * Created by IntelliJ IDEA. @@ -53,28 +54,32 @@ public enum DiploidGenotype { * @return the diploid genotype */ public static DiploidGenotype createHomGenotype(char hom) { - hom = Character.toUpperCase(hom); - switch (hom) { - case 'A': return DiploidGenotype.AA; - case 'C': return DiploidGenotype.CC; - case 'G': return DiploidGenotype.GG; - case 'T': return DiploidGenotype.TT; - } - throw new IllegalArgumentException(hom + " is not a valid base character"); + int index = BaseUtils.simpleBaseToBaseIndex(hom); + if ( index == -1 ) + throw new IllegalArgumentException(hom + " is not a valid base character"); + return conversionMatrix[index][index]; } /** - * get the genotype, given a string of 2 chars which may not necessarily be ordered correctly + * create a diploid genotype, given 2 chars which may not necessarily be ordered correctly * @param base1 base1 * @param base2 base2 * @return the diploid genotype */ - public static DiploidGenotype unorderedValueOf(char base1, char base2) { - if ( base1 > base2 ) { - char temp = base1; - base1 = base2; - base2 = temp; - } - return valueOf(String.format("%c%c", base1, base2)); + public static DiploidGenotype createDiploidGenotype(char base1, char base2) { + int index1 = BaseUtils.simpleBaseToBaseIndex(base1); + if ( index1 == -1 ) + throw new IllegalArgumentException(base1 + " is not a valid base character"); + int index2 = BaseUtils.simpleBaseToBaseIndex(base2); + if ( index2 == -1 ) + throw new IllegalArgumentException(base2 + " is not a valid base character"); + return conversionMatrix[index1][index2]; } + + private static final DiploidGenotype[][] conversionMatrix = { + { DiploidGenotype.AA, DiploidGenotype.AC, DiploidGenotype.AG, DiploidGenotype.AT }, + { DiploidGenotype.AC, DiploidGenotype.CC, DiploidGenotype.CG, DiploidGenotype.CT }, + { DiploidGenotype.AG, DiploidGenotype.CG, DiploidGenotype.GG, DiploidGenotype.GT }, + { DiploidGenotype.AT, DiploidGenotype.CT, DiploidGenotype.GT, DiploidGenotype.TT } + }; } \ No newline at end of file