From 9e28d1e3476aace1bed04a5e76b0617ddf99de0d Mon Sep 17 00:00:00 2001 From: Mark DePristo Date: Sat, 9 Feb 2013 13:04:30 -0500 Subject: [PATCH 1/6] Cleanup and unit tests for QualityUtils -- Fixed a few conversion bugs with edge case quals (ones that were very high) -- Fixed a critical bug in the conversion of quals that was causing near capped quals to fall below their actual value. Will undoubtedly need to fix md5s -- More precise prob -> qual calculations for very high confidence events in phredScaleCorrectRate, trueProbToQual, and errorProbToQual. Very likely to improve accuracy of many calculations in the GATK -- Added errorProbToQual and trueProbToQual calculations that accept an integer cap, and perform the (tricky) conversion from int to byte correctly. -- Full docs and unit tests for phredScaleCorrectRate and phredScaleErrorRate. -- Renamed probToQual to trueProbToQual -- Added goodProbability and log10OneMinusX to MathUtils -- Went through the GATK and cleaned up many uses of QualityUtils -- Cleanup constants in QualityUtils -- Added full docs for all of the constants -- Rename MAX_QUAL_SCORE to MAX_SAM_QUAL_SCORE for clarity -- Moved MAX_GATK_USABLE_Q_SCORE to RecalDatum, as it's s BQSR specific feature -- Convert uses of QualityUtils.errorProbToQual(1-x) to QualityUtils.trueProbToQual(x) -- Cleanup duplicate quality score routines in MathUtils. Moved and renamed MathUtils.log10ProbabilityToPhredScale => QualityUtils.phredScaleLog10ErrorRate. Removed 3 routines from MathUtils, and remapped their usages into the better routines in QualityUtils --- .../walkers/bqsr/ReadRecalibrationInfo.java | 2 +- .../DiploidSNPGenotypeLikelihoods.java | 2 +- .../gatk/walkers/genotyper/ErrorModel.java | 5 +- .../genotyper/UnifiedGenotyperEngine.java | 2 +- .../walkers/phasing/PhaseByTransmission.java | 3 +- .../walkers/phasing/ReadBackedPhasing.java | 2 +- .../recalibration/BaseRecalibration.java | 3 +- .../utils/recalibration/QualQuantizer.java | 6 +- .../utils/recalibration/QuantizationInfo.java | 6 +- .../sting/utils/recalibration/RecalDatum.java | 16 +- .../recalibration/RecalibrationReport.java | 6 +- .../covariates/QualityScoreCovariate.java | 2 +- .../recalibration/QualQuantizerUnitTest.java | 2 +- .../recalibration/RecalDatumUnitTest.java | 8 +- .../RecalibrationReportUnitTest.java | 8 +- .../diagnostics/ErrorRatePerCycle.java | 2 +- .../variantutils/VariantsToBinaryPed.java | 3 +- .../broadinstitute/sting/utils/MathUtils.java | 52 +-- .../sting/utils/QualityUtils.java | 364 ++++++++++++++---- .../sting/utils/duplicates/DupUtils.java | 3 +- .../utils/locusiterator/LIBSPerformance.java | 2 +- .../sting/utils/QualityUtilsUnitTest.java | 110 ++++++ .../locusiterator/LocusIteratorBenchmark.java | 3 +- .../LocusIteratorByStateBaseTest.java | 2 +- .../LocusIteratorByStateUnitTest.java | 2 +- 25 files changed, 470 insertions(+), 146 deletions(-) diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/bqsr/ReadRecalibrationInfo.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/bqsr/ReadRecalibrationInfo.java index 83d5bb29b..94d1c5501 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/bqsr/ReadRecalibrationInfo.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/bqsr/ReadRecalibrationInfo.java @@ -179,6 +179,6 @@ public final class ReadRecalibrationInfo { } private boolean validQual(final byte result) { - return result >= 0 && result <= QualityUtils.MAX_QUAL_SCORE; + return result >= 0 && result <= QualityUtils.MAX_SAM_QUAL_SCORE; } } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/DiploidSNPGenotypeLikelihoods.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/DiploidSNPGenotypeLikelihoods.java index 2baa89999..941b11b36 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/DiploidSNPGenotypeLikelihoods.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/DiploidSNPGenotypeLikelihoods.java @@ -267,7 +267,7 @@ public class DiploidSNPGenotypeLikelihoods implements Cloneable { // // ------------------------------------------------------------------------------------- - static DiploidSNPGenotypeLikelihoods[][][][][] CACHE = new DiploidSNPGenotypeLikelihoods[BaseUtils.BASES.length][QualityUtils.MAX_QUAL_SCORE+1][BaseUtils.BASES.length+1][QualityUtils.MAX_QUAL_SCORE+1][MAX_PLOIDY]; + static DiploidSNPGenotypeLikelihoods[][][][][] CACHE = new DiploidSNPGenotypeLikelihoods[BaseUtils.BASES.length][QualityUtils.MAX_SAM_QUAL_SCORE +1][BaseUtils.BASES.length+1][QualityUtils.MAX_SAM_QUAL_SCORE +1][MAX_PLOIDY]; protected boolean inCache(byte observedBase1, byte qualityScore1, byte observedBase2, byte qualityScore2, int ploidy) { return getCache(CACHE, observedBase1, qualityScore1, observedBase2, qualityScore2, ploidy) != null; diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/ErrorModel.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/ErrorModel.java index 1b004d889..49494ebb0 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/ErrorModel.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/ErrorModel.java @@ -51,6 +51,7 @@ import org.broadinstitute.sting.gatk.contexts.ReferenceContext; import org.broadinstitute.sting.gatk.walkers.indels.PairHMMIndelErrorModel; import org.broadinstitute.sting.utils.Haplotype; import org.broadinstitute.sting.utils.MathUtils; +import org.broadinstitute.sting.utils.QualityUtils; import org.broadinstitute.sting.utils.genotyper.PerReadAlleleLikelihoodMap; import org.broadinstitute.sting.utils.pileup.PileupElement; import org.broadinstitute.sting.utils.pileup.ReadBackedPileup; @@ -123,7 +124,7 @@ public class ErrorModel { } } - double p = MathUtils.phredScaleToLog10Probability((byte)(maxQualityScore-minQualityScore)); + double p = QualityUtils.qualToErrorProbLog10((byte)(maxQualityScore-minQualityScore)); if (refSamplePileup == null || refSampleVC == null || !hasCalledAlleles) { for (byte q=minQualityScore; q<=maxQualityScore; q++) { // maximum uncertainty if there's no ref data at site @@ -270,7 +271,7 @@ public class ErrorModel { }) private double log10PoissonProbabilitySiteGivenQual(byte q, int coverage, int mismatches) { // same as log10ProbabilitySiteGivenQual but with Poisson approximation to avoid numerical underflows - double lambda = MathUtils.phredScaleToProbability(q) * (double )coverage; + double lambda = QualityUtils.qualToErrorProb(q) * (double )coverage; // log10(e^-lambda*lambda^k/k!) = -lambda + k*log10(lambda) - log10factorial(k) return Math.log10(lambda)*mismatches - lambda*log10MinusE- MathUtils.log10Factorial(mismatches); } diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngine.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngine.java index 19d218023..4cfd8c7bc 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngine.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngine.java @@ -613,7 +613,7 @@ public class UnifiedGenotyperEngine { P_of_ref *= 1.0 - (theta / 2.0) * getRefBinomialProb(depth); } - return new VariantCallContext(vc, QualityUtils.phredScaleErrorRate(1.0 - P_of_ref) >= UAC.STANDARD_CONFIDENCE_FOR_CALLING, false); + return new VariantCallContext(vc, QualityUtils.phredScaleCorrectRate(P_of_ref) >= UAC.STANDARD_CONFIDENCE_FOR_CALLING, false); } protected void printVerboseData(String pos, VariantContext vc, double PofF, double phredScaledConfidence, final GenotypeLikelihoodsCalculationModel.Model model) { diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhaseByTransmission.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhaseByTransmission.java index 21f2bd8db..54a324411 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhaseByTransmission.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhaseByTransmission.java @@ -57,6 +57,7 @@ import org.broadinstitute.sting.gatk.refdata.RefMetaDataTracker; import org.broadinstitute.sting.gatk.samples.Sample; import org.broadinstitute.sting.gatk.walkers.RodWalker; import org.broadinstitute.sting.utils.MathUtils; +import org.broadinstitute.sting.utils.QualityUtils; import org.broadinstitute.sting.utils.SampleUtils; import org.broadinstitute.sting.utils.help.HelpConstants; import org.broadinstitute.sting.utils.variant.GATKVCFUtils; @@ -395,7 +396,7 @@ public class PhaseByTransmission extends RodWalker, HashMa int phredScoreTransmission = -1; if(transmissionProb != NO_TRANSMISSION_PROB){ - double dphredScoreTransmission = MathUtils.log10ProbabilityToPhredScale(Math.log10(1-(transmissionProb))); + double dphredScoreTransmission = QualityUtils.phredScaleLog10ErrorRate(Math.log10(1 - (transmissionProb))); phredScoreTransmission = dphredScoreTransmission < Byte.MAX_VALUE ? (byte)dphredScoreTransmission : Byte.MAX_VALUE; } //Handle null, missing and unavailable genotypes diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/phasing/ReadBackedPhasing.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/phasing/ReadBackedPhasing.java index e8388a3d7..7f2cdd3d0 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/phasing/ReadBackedPhasing.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/phasing/ReadBackedPhasing.java @@ -1017,7 +1017,7 @@ public class ReadBackedPhasing extends RodWalker= 0", "nErrors >= 0", "nErrors <= nObservations", - "fixedQual >= -1 && fixedQual <= QualityUtils.MAX_QUAL_SCORE", + "fixedQual >= -1 && fixedQual <= QualityUtils.MAX_SAM_QUAL_SCORE", "mergeOrder >= 0"}) protected final class QualInterval implements Comparable { final int qStart, qEnd, fixedQual, level; @@ -224,10 +224,10 @@ public class QualQuantizer { /** * @return the QUAL of the error rate of this interval, or the fixed qual if this interval was created with a fixed qual. */ - @Ensures("result >= 0 && result <= QualityUtils.MAX_QUAL_SCORE") + @Ensures("result >= 0 && result <= QualityUtils.MAX_SAM_QUAL_SCORE") public byte getQual() { if ( ! hasFixedQual() ) - return QualityUtils.probToQual(1-getErrorRate(), 0); + return QualityUtils.errorProbToQual(getErrorRate()); else return (byte)fixedQual; } diff --git a/protected/java/src/org/broadinstitute/sting/utils/recalibration/QuantizationInfo.java b/protected/java/src/org/broadinstitute/sting/utils/recalibration/QuantizationInfo.java index eb4f61266..464390b99 100644 --- a/protected/java/src/org/broadinstitute/sting/utils/recalibration/QuantizationInfo.java +++ b/protected/java/src/org/broadinstitute/sting/utils/recalibration/QuantizationInfo.java @@ -76,7 +76,7 @@ public class QuantizationInfo { } public QuantizationInfo(final RecalibrationTables recalibrationTables, final int quantizationLevels) { - final Long [] qualHistogram = new Long[QualityUtils.MAX_QUAL_SCORE+1]; // create a histogram with the empirical quality distribution + final Long [] qualHistogram = new Long[QualityUtils.MAX_SAM_QUAL_SCORE +1]; // create a histogram with the empirical quality distribution for (int i = 0; i < qualHistogram.length; i++) qualHistogram[i] = 0L; @@ -100,7 +100,7 @@ public class QuantizationInfo { } public void noQuantization() { - this.quantizationLevels = QualityUtils.MAX_QUAL_SCORE; + this.quantizationLevels = QualityUtils.MAX_SAM_QUAL_SCORE; for (int i = 0; i < this.quantizationLevels; i++) quantizedQuals.set(i, (byte) i); } @@ -124,7 +124,7 @@ public class QuantizationInfo { quantizedTable.addColumn(RecalUtils.QUANTIZED_COUNT_COLUMN_NAME); quantizedTable.addColumn(RecalUtils.QUANTIZED_VALUE_COLUMN_NAME); - for (int qual = 0; qual <= QualityUtils.MAX_QUAL_SCORE; qual++) { + for (int qual = 0; qual <= QualityUtils.MAX_SAM_QUAL_SCORE; qual++) { quantizedTable.set(qual, RecalUtils.QUALITY_SCORE_COLUMN_NAME, qual); quantizedTable.set(qual, RecalUtils.QUANTIZED_COUNT_COLUMN_NAME, empiricalQualCounts.get(qual)); quantizedTable.set(qual, RecalUtils.QUANTIZED_VALUE_COLUMN_NAME, quantizedQuals.get(qual)); diff --git a/protected/java/src/org/broadinstitute/sting/utils/recalibration/RecalDatum.java b/protected/java/src/org/broadinstitute/sting/utils/recalibration/RecalDatum.java index be537f294..ea3781204 100644 --- a/protected/java/src/org/broadinstitute/sting/utils/recalibration/RecalDatum.java +++ b/protected/java/src/org/broadinstitute/sting/utils/recalibration/RecalDatum.java @@ -74,10 +74,10 @@ package org.broadinstitute.sting.utils.recalibration; import com.google.java.contract.Ensures; import com.google.java.contract.Invariant; import com.google.java.contract.Requires; +import net.sf.samtools.SAMUtils; import org.apache.commons.math.optimization.fitting.GaussianFunction; import org.broadinstitute.sting.utils.MathUtils; import org.broadinstitute.sting.utils.QualityUtils; -import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; /** @@ -100,6 +100,7 @@ import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; "numMismatches <= numObservations" }) public class RecalDatum { + public final static byte MAX_RECALIBRATED_Q_SCORE = SAMUtils.MAX_PHRED_SCORE; private static final double UNINITIALIZED = -1.0; /** @@ -337,7 +338,7 @@ public class RecalDatum { // This is the old and busted point estimate approach: //final double empiricalQual = -10 * Math.log10(getEmpiricalErrorRate()); - empiricalQuality = Math.min(empiricalQual, (double) QualityUtils.MAX_RECALIBRATED_Q_SCORE); + empiricalQuality = Math.min(empiricalQual, (double) MAX_RECALIBRATED_Q_SCORE); } //static final boolean DEBUG = false; @@ -369,7 +370,12 @@ public class RecalDatum { return Qemp; } - static private final double[] log10QempPriorCache = new double[QualityUtils.MAX_GATK_USABLE_Q_SCORE + 1]; + /** + * Quals above this value should be capped down to this value (because they are too high) + * in the base quality score recalibrator + */ + public final static byte MAX_GATK_USABLE_Q_SCORE = 40; + static private final double[] log10QempPriorCache = new double[MAX_GATK_USABLE_Q_SCORE + 1]; static { // f(x) = a + b*exp(-((x - c)^2 / (2*d^2))) // Note that b is the height of the curve's peak, c is the position of the center of the peak, and d controls the width of the "bell". @@ -379,7 +385,7 @@ public class RecalDatum { final double GF_d = 0.5; // with these parameters, deltas can shift at most ~20 Q points final GaussianFunction gaussian = new GaussianFunction(GF_a, GF_b, GF_c, GF_d); - for ( int i = 0; i <= QualityUtils.MAX_GATK_USABLE_Q_SCORE; i++ ) { + for ( int i = 0; i <= MAX_GATK_USABLE_Q_SCORE; i++ ) { double log10Prior = Math.log10(gaussian.value((double) i)); if ( Double.isInfinite(log10Prior) ) log10Prior = -Double.MAX_VALUE; @@ -388,7 +394,7 @@ public class RecalDatum { } static protected double log10QempPrior(final double Qempirical, final double Qreported) { - final int difference = Math.min(Math.abs((int) (Qempirical - Qreported)), QualityUtils.MAX_GATK_USABLE_Q_SCORE); + final int difference = Math.min(Math.abs((int) (Qempirical - Qreported)), MAX_GATK_USABLE_Q_SCORE); //if ( DEBUG ) // System.out.println(String.format("Qemp = %f, log10Priors = %f", Qempirical, log10QempPriorCache[difference])); return log10QempPriorCache[difference]; diff --git a/protected/java/src/org/broadinstitute/sting/utils/recalibration/RecalibrationReport.java b/protected/java/src/org/broadinstitute/sting/utils/recalibration/RecalibrationReport.java index e5860b4ad..a3fec6a22 100644 --- a/protected/java/src/org/broadinstitute/sting/utils/recalibration/RecalibrationReport.java +++ b/protected/java/src/org/broadinstitute/sting/utils/recalibration/RecalibrationReport.java @@ -263,11 +263,11 @@ public class RecalibrationReport { * Parses the quantization table from the GATK Report and turns it into a map of original => quantized quality scores * * @param table the GATKReportTable containing the quantization mappings - * @return an ArrayList with the quantization mappings from 0 to MAX_QUAL_SCORE + * @return an ArrayList with the quantization mappings from 0 to MAX_SAM_QUAL_SCORE */ private QuantizationInfo initializeQuantizationTable(GATKReportTable table) { - final Byte[] quals = new Byte[QualityUtils.MAX_QUAL_SCORE + 1]; - final Long[] counts = new Long[QualityUtils.MAX_QUAL_SCORE + 1]; + final Byte[] quals = new Byte[QualityUtils.MAX_SAM_QUAL_SCORE + 1]; + final Long[] counts = new Long[QualityUtils.MAX_SAM_QUAL_SCORE + 1]; for ( int i = 0; i < table.getNumRows(); i++ ) { final byte originalQual = (byte)i; final Object quantizedObject = table.get(i, RecalUtils.QUANTIZED_VALUE_COLUMN_NAME); diff --git a/protected/java/src/org/broadinstitute/sting/utils/recalibration/covariates/QualityScoreCovariate.java b/protected/java/src/org/broadinstitute/sting/utils/recalibration/covariates/QualityScoreCovariate.java index 4d5af87a8..46284b27e 100644 --- a/protected/java/src/org/broadinstitute/sting/utils/recalibration/covariates/QualityScoreCovariate.java +++ b/protected/java/src/org/broadinstitute/sting/utils/recalibration/covariates/QualityScoreCovariate.java @@ -119,6 +119,6 @@ public class QualityScoreCovariate implements RequiredCovariate { @Override public int maximumKeyValue() { - return QualityUtils.MAX_QUAL_SCORE; + return QualityUtils.MAX_SAM_QUAL_SCORE; } } \ No newline at end of file diff --git a/protected/java/test/org/broadinstitute/sting/utils/recalibration/QualQuantizerUnitTest.java b/protected/java/test/org/broadinstitute/sting/utils/recalibration/QualQuantizerUnitTest.java index 8f228c154..696cf846f 100644 --- a/protected/java/test/org/broadinstitute/sting/utils/recalibration/QualQuantizerUnitTest.java +++ b/protected/java/test/org/broadinstitute/sting/utils/recalibration/QualQuantizerUnitTest.java @@ -90,7 +90,7 @@ public class QualQuantizerUnitTest extends BaseTest { this.exError = exError; this.exTotal = exTotal; this.exErrorRate = (leftE + rightE + 1) / (1.0 * (leftN + rightN + 1)); - this.exQual = QualityUtils.probToQual(1-this.exErrorRate, 0); + this.exQual = QualityUtils.errorProbToQual(this.exErrorRate); } } diff --git a/protected/java/test/org/broadinstitute/sting/utils/recalibration/RecalDatumUnitTest.java b/protected/java/test/org/broadinstitute/sting/utils/recalibration/RecalDatumUnitTest.java index da78932d1..5b7b95be9 100644 --- a/protected/java/test/org/broadinstitute/sting/utils/recalibration/RecalDatumUnitTest.java +++ b/protected/java/test/org/broadinstitute/sting/utils/recalibration/RecalDatumUnitTest.java @@ -50,7 +50,6 @@ package org.broadinstitute.sting.utils.recalibration; // the imports for unit testing. -import org.apache.commons.lang.ArrayUtils; import org.broadinstitute.sting.BaseTest; import org.broadinstitute.sting.utils.MathUtils; import org.broadinstitute.sting.utils.QualityUtils; @@ -58,7 +57,6 @@ import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -207,8 +205,8 @@ public class RecalDatumUnitTest extends BaseTest { @Test public void testlog10QempPrior() { - for ( int Qemp = 0; Qemp <= QualityUtils.MAX_QUAL_SCORE; Qemp++ ) { - for ( int Qrep = 0; Qrep <= QualityUtils.MAX_QUAL_SCORE; Qrep++ ) { + for ( int Qemp = 0; Qemp <= QualityUtils.MAX_SAM_QUAL_SCORE; Qemp++ ) { + for ( int Qrep = 0; Qrep <= QualityUtils.MAX_SAM_QUAL_SCORE; Qrep++ ) { final double log10prior = RecalDatum.log10QempPrior(Qemp, Qrep); Assert.assertTrue(log10prior < 0.0); Assert.assertFalse(Double.isInfinite(log10prior)); @@ -219,7 +217,7 @@ public class RecalDatumUnitTest extends BaseTest { final int Qrep = 20; int maxQemp = -1; double maxQempValue = -Double.MAX_VALUE; - for ( int Qemp = 0; Qemp <= QualityUtils.MAX_QUAL_SCORE; Qemp++ ) { + for ( int Qemp = 0; Qemp <= QualityUtils.MAX_SAM_QUAL_SCORE; Qemp++ ) { final double log10prior = RecalDatum.log10QempPrior(Qemp, Qrep); if ( log10prior > maxQempValue ) { maxQemp = Qemp; diff --git a/protected/java/test/org/broadinstitute/sting/utils/recalibration/RecalibrationReportUnitTest.java b/protected/java/test/org/broadinstitute/sting/utils/recalibration/RecalibrationReportUnitTest.java index e82f1338a..7d1e51385 100644 --- a/protected/java/test/org/broadinstitute/sting/utils/recalibration/RecalibrationReportUnitTest.java +++ b/protected/java/test/org/broadinstitute/sting/utils/recalibration/RecalibrationReportUnitTest.java @@ -67,7 +67,7 @@ public class RecalibrationReportUnitTest { final Random random = new Random(); final int nObservations = random.nextInt(maxObservations); final int nErrors = Math.min(random.nextInt(maxErrors), nObservations); - final int qual = random.nextInt(QualityUtils.MAX_QUAL_SCORE); + final int qual = random.nextInt(QualityUtils.MAX_SAM_QUAL_SCORE); return new RecalDatum((long)nObservations, (double)nErrors, (byte)qual); } @@ -75,10 +75,10 @@ public class RecalibrationReportUnitTest { public void testOutput() { final int length = 100; - List quals = new ArrayList(QualityUtils.MAX_QUAL_SCORE + 1); - List counts = new ArrayList(QualityUtils.MAX_QUAL_SCORE + 1); + List quals = new ArrayList(QualityUtils.MAX_SAM_QUAL_SCORE + 1); + List counts = new ArrayList(QualityUtils.MAX_SAM_QUAL_SCORE + 1); - for (int i = 0; i<= QualityUtils.MAX_QUAL_SCORE; i++) { + for (int i = 0; i<= QualityUtils.MAX_SAM_QUAL_SCORE; i++) { quals.add((byte) i); counts.add(1L); } diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/ErrorRatePerCycle.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/ErrorRatePerCycle.java index f361d5e2b..76f5478a4 100644 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/ErrorRatePerCycle.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/diagnostics/ErrorRatePerCycle.java @@ -200,7 +200,7 @@ public class ErrorRatePerCycle extends LocusWalker { final int mismatches = (Integer)table.get(key, "mismatches"); final int count = (Integer)table.get(key, "counts"); final double errorRate = (mismatches + 1) / (1.0*(count + 1)); - final int qual = QualityUtils.probToQual(1-errorRate, 0.0); + final int qual = QualityUtils.errorProbToQual(errorRate); table.set(key, "qual", qual); table.set(key, "errorrate", errorRate); } diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/variantutils/VariantsToBinaryPed.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/variantutils/VariantsToBinaryPed.java index ce9e28c4b..8d16e6ca2 100644 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/variantutils/VariantsToBinaryPed.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/variantutils/VariantsToBinaryPed.java @@ -38,6 +38,7 @@ import org.broadinstitute.sting.gatk.walkers.RodWalker; import org.broadinstitute.sting.gatk.walkers.Window; import org.broadinstitute.sting.utils.MathUtils; import org.broadinstitute.sting.utils.help.HelpConstants; +import org.broadinstitute.sting.utils.QualityUtils; import org.broadinstitute.sting.utils.variant.GATKVCFUtils; import org.broadinstitute.variant.vcf.VCFHeader; import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; @@ -408,7 +409,7 @@ public class VariantsToBinaryPed extends RodWalker { return genotype.getGQ() >= minGenotypeQuality; } else if ( genotype.hasLikelihoods() ) { double log10gq = GenotypeLikelihoods.getGQLog10FromLikelihoods(genotype.getType().ordinal()-1,genotype.getLikelihoods().getAsVector()); - return MathUtils.log10ProbabilityToPhredScale(log10gq) >= minGenotypeQuality; + return QualityUtils.phredScaleLog10ErrorRate(log10gq) >= minGenotypeQuality; } return minGenotypeQuality <= 0; diff --git a/public/java/src/org/broadinstitute/sting/utils/MathUtils.java b/public/java/src/org/broadinstitute/sting/utils/MathUtils.java index 0c3ed87c0..4db55b275 100644 --- a/public/java/src/org/broadinstitute/sting/utils/MathUtils.java +++ b/public/java/src/org/broadinstitute/sting/utils/MathUtils.java @@ -1251,6 +1251,16 @@ public class MathUtils { return result <= 0.0 && ! Double.isInfinite(result) && ! Double.isNaN(result); } + /** + * Checks that the result is a well-formed probability + * + * @param result a supposedly well-formed probability value + * @return true if result is really well formed + */ + public static boolean goodProbability(final double result) { + return result >= 0.0 && result <= 1.0 && ! Double.isInfinite(result) && ! Double.isNaN(result); + } + /** * A utility class that computes on the fly average and standard deviation for a stream of numbers. * The number of observations does not have to be known in advance, and can be also very big (so that @@ -1343,28 +1353,6 @@ public class MathUtils { return Math.max(a, x2); } - public static double phredScaleToProbability(byte q) { - return Math.pow(10, (-q) / 10.0); - } - - public static double phredScaleToLog10Probability(byte q) { - return ((-q) / 10.0); - } - - /** - * Returns the phred scaled value of probability p - * - * @param p probability (between 0 and 1). - * @return phred scaled probability of p - */ - public static byte probabilityToPhredScale(double p) { - return (byte) ((-10) * Math.log10(p)); - } - - public static double log10ProbabilityToPhredScale(double log10p) { - return (-10) * log10p; - } - /** * Converts LN to LOG10 * @@ -1774,4 +1762,24 @@ public class MathUtils { return values; } + + /** + * Compute in a numerical correct way the quanity log10(1-x) + * + * Uses the approximation log10(1-x) = log10(1/x - 1) + log10(x) to avoid very quick underflow + * in 1-x when x is very small + * + * @param x a positive double value between 0.0 and 1.0 + * @return an estimate of log10(1-x) + */ + @Requires("x >= 0.0 && x <= 1.0") + @Ensures("result <= 0.0") + public static double log10OneMinusX(final double x) { + if ( x == 1.0 ) + return Double.NEGATIVE_INFINITY; + else if ( x == 0.0 ) + return 0.0; + else + return Math.log10(1 / x - 1) + Math.log10(x); + } } diff --git a/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java b/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java index 4519a656b..9dd9b735d 100644 --- a/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java +++ b/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java @@ -25,38 +25,50 @@ package org.broadinstitute.sting.utils; +import com.google.java.contract.Ensures; +import com.google.java.contract.Requires; import net.sf.samtools.SAMUtils; /** * QualityUtils is a static class (no instantiation allowed!) with some utility methods for manipulating * quality scores. * - * @author Kiran Garimella + * @author Kiran Garimella, Mark DePristo + * @since Way back */ public class QualityUtils { - public final static byte MAX_RECALIBRATED_Q_SCORE = SAMUtils.MAX_PHRED_SCORE; - public final static byte MAX_QUAL_SCORE = SAMUtils.MAX_PHRED_SCORE; - public final static double ERROR_RATE_OF_MAX_QUAL_SCORE = qualToErrorProbRaw(MAX_QUAL_SCORE); + /** + * Maximum quality score that can be encoded in a SAM/BAM file + */ + public final static byte MAX_SAM_QUAL_SCORE = SAMUtils.MAX_PHRED_SCORE; - public final static double MIN_REASONABLE_ERROR = 0.0001; - public final static byte MAX_REASONABLE_Q_SCORE = 60; // bams containing quals above this value are extremely suspicious and we should warn the user - public final static byte MAX_GATK_USABLE_Q_SCORE = 40; // quals above this value should be capped down to this value (because they are too high) + + private final static double RAW_MIN_PHRED_SCALED_QUAL = Math.log10(Double.MIN_VALUE); + protected final static double MIN_PHRED_SCALED_QUAL = -10.0 * RAW_MIN_PHRED_SCALED_QUAL; + + /** + * bams containing quals above this value are extremely suspicious and we should warn the user + */ + public final static byte MAX_REASONABLE_Q_SCORE = 60; + + /** + * The lowest quality score for a base that is considered reasonable for statistical analysis. This is + * because Q 6 => you stand a 25% of being right, which means all bases are equally likely + */ public final static byte MIN_USABLE_Q_SCORE = 6; public final static int MAPPING_QUALITY_UNAVAILABLE = 255; + /** + * Cached values for qual as byte calculations so they are very fast + */ private static double qualToErrorProbCache[] = new double[256]; - static { - for (int i = 0; i < 256; i++) qualToErrorProbCache[i] = qualToErrorProbRaw(i); - } - - private static double qualToErrorProbLog10Cache[] = new double[256]; - static { - for (int i = 0; i < 256; i++) qualToErrorProbLog10Cache[i] = qualToErrorProbLog10Raw(i); - } - private static double qualToProbLog10Cache[] = new double[256]; + static { - for (int i = 0; i < 256; i++) qualToProbLog10Cache[i] = qualToProbLog10Raw(i); + for (int i = 0; i < 256; i++) { + qualToErrorProbCache[i] = qualToErrorProb((double) i); + qualToProbLog10Cache[i] = Math.log10(1.0 - qualToErrorProbCache[i]); + } } /** @@ -64,111 +76,301 @@ public class QualityUtils { */ private QualityUtils() {} + // ---------------------------------------------------------------------- + // + // These are all functions to convert a phred-scaled quality score to a probability + // + // ---------------------------------------------------------------------- + /** - * Convert a quality score to a probability. This is the Phred-style - * conversion, *not* the Illumina-style conversion (though asymptotically, they're the same). + * Convert a phred-scaled quality score to its probability of being true (Q30 => 0.999) + * + * This is the Phred-style conversion, *not* the Illumina-style conversion. + * + * Because the input is a discretized byte value, this function uses a cache so is very efficient + * + * WARNING -- because this function takes a byte for maxQual, you must be careful in converting + * integers to byte. The appropriate way to do this is ((byte)(myInt & 0xFF)) * * @param qual a quality score (0-255) * @return a probability (0.0-1.0) */ - static public double qualToProb(byte qual) { + @Ensures("result >= 0.0 && result <= 1.0") + public static double qualToProb(final byte qual) { return 1.0 - qualToErrorProb(qual); } - static public double qualToProb(double qual) { - return 1.0 - Math.pow(10.0, qual/(-10.0)); + /** + * Convert a phred-scaled quality score to its probability of being true (Q30 => 0.999) + * + * This is the Phred-style conversion, *not* the Illumina-style conversion. + * + * Because the input is a double value, this function must call Math.pow so can be quite expensive + * + * @param qual a phred-scaled quality score encoded as a double. Can be non-integer values (30.5) + * @return a probability (0.0-1.0) + */ + @Requires("qual >= 0.0") + @Ensures("result >= 0.0 && result <= 1.0") + public static double qualToProb(final double qual) { + return 1.0 - qualToErrorProb(qual); } - static private double qualToProbLog10Raw(int qual) { - return Math.log10(1.0 - qualToErrorProbRaw(qual)); - } - - static public double qualToProbLog10(byte qual) { + /** + * Convert a phred-scaled quality score to its log10 probability of being true (Q30 => log10(0.999)) + * + * This is the Phred-style conversion, *not* the Illumina-style conversion. + * + * Because the input is a double value, this function must call Math.pow so can be quite expensive + * + * WARNING -- because this function takes a byte for maxQual, you must be careful in converting + * integers to byte. The appropriate way to do this is ((byte)(myInt & 0xFF)) + * + * @param qual a phred-scaled quality score encoded as a double. Can be non-integer values (30.5) + * @return a probability (0.0-1.0) + */ + @Ensures("result <= 0.0") + public static double qualToProbLog10(final byte qual) { return qualToProbLog10Cache[(int)qual & 0xff]; // Map: 127 -> 127; -128 -> 128; -1 -> 255; etc. } /** - * Convert a quality score to a probability of error. This is the Phred-style - * conversion, *not* the Illumina-style conversion (though asymptotically, they're the same). + * Convert a phred-scaled quality score to its probability of being wrong (Q30 => 0.001) * - * @param qual a quality score (0 - 255) - * @return a probability (0.0 - 1.0) + * This is the Phred-style conversion, *not* the Illumina-style conversion. + * + * Because the input is a double value, this function must call Math.pow so can be quite expensive + * + * @param qual a phred-scaled quality score encoded as a double. Can be non-integer values (30.5) + * @return a probability (0.0-1.0) */ - static private double qualToErrorProbRaw(int qual) { - return qualToErrorProb((double) qual); - } - + @Requires("qual >= 0.0") + @Ensures("result >= 0.0 && result <= 1.0") public static double qualToErrorProb(final double qual) { - return Math.pow(10.0, qual/-10.0); + return Math.pow(10.0, qual / -10.0); } - - static public double qualToErrorProb(byte qual) { + /** + * Convert a phred-scaled quality score to its probability of being wrong (Q30 => 0.001) + * + * This is the Phred-style conversion, *not* the Illumina-style conversion. + * + * Because the input is a byte value, this function uses a cache so is very efficient + * + * WARNING -- because this function takes a byte for maxQual, you must be careful in converting + * integers to byte. The appropriate way to do this is ((byte)(myInt & 0xFF)) + * + * @param qual a phred-scaled quality score encoded as a byte + * @return a probability (0.0-1.0) + */ + @Ensures("result >= 0.0 && result <= 1.0") + public static double qualToErrorProb(final byte qual) { return qualToErrorProbCache[(int)qual & 0xff]; // Map: 127 -> 127; -128 -> 128; -1 -> 255; etc. } - static private double qualToErrorProbLog10Raw(int qual) { - return ((double) qual)/-10.0; - } - static public double qualToErrorProbLog10(byte qual) { - return qualToErrorProbLog10Cache[(int)qual & 0xff]; // Map: 127 -> 127; -128 -> 128; -1 -> 255; etc. - } - - static public double qualToErrorProbLog10(final double qual) { - return qual/-10.0; + /** + * Convert a phred-scaled quality score to its log10 probability of being wrong (Q30 => log10(0.001)) + * + * This is the Phred-style conversion, *not* the Illumina-style conversion. + * + * The calculation is extremely efficient + * + * WARNING -- because this function takes a byte for maxQual, you must be careful in converting + * integers to byte. The appropriate way to do this is ((byte)(myInt & 0xFF)) + * + * @param qual a phred-scaled quality score encoded as a byte + * @return a probability (0.0-1.0) + */ + @Ensures("result <= 0.0") + public static double qualToErrorProbLog10(final byte qual) { + return qualToErrorProbLog10((double)(qual & 0xFF)); } /** - * Convert a probability to a quality score. Note, this is capped at Q40. + * Convert a phred-scaled quality score to its log10 probability of being wrong (Q30 => log10(0.001)) * - * @param prob a probability (0.0-1.0) - * @return a quality score (0-40) + * This is the Phred-style conversion, *not* the Illumina-style conversion. + * + * The calculation is extremely efficient + * + * @param qual a phred-scaled quality score encoded as a double + * @return a probability (0.0-1.0) */ - static public byte probToQual(double prob) { - return probToQual(prob, MIN_REASONABLE_ERROR); - //return (byte) Math.round(-10.0*Math.log10(1.0 - prob + 0.0001)); + @Ensures("result <= 0.0") + public static double qualToErrorProbLog10(final double qual) { + return qual / -10.0; + } + + // ---------------------------------------------------------------------- + // + // Functions to convert a probability to a phred-scaled quality score + // + // ---------------------------------------------------------------------- + + /** + * Convert a probability of being wrong to a phred-scaled quality score (0.01 => 20). + * + * Note, this function caps the resulting quality score by the public static value MAX_SAM_QUAL_SCORE + * and by 1 at the low-end. + * + * @param errorRate a probability (0.0-1.0) of being wrong (i.e., 0.01 is 1% change of being wrong) + * @return a quality score (0-MAX_SAM_QUAL_SCORE) + */ + @Requires("errorRate >= 0.0 && errorRate <= 1.0") + public static byte errorProbToQual(final double errorRate) { + return errorProbToQual(errorRate, MAX_SAM_QUAL_SCORE); } /** - * Convert a probability to a quality score. Note, this is capped at a quality score which is determined by _eps_. + * Convert a probability of being wrong to a phred-scaled quality score (0.01 => 20). * - * @param prob a probability (0.0-1.0) - * @param eps min probabilty allowed (0.0-1.0) - * @return a quality score (0-255) + * Note, this function caps the resulting quality score by the public static value MIN_REASONABLE_ERROR + * and by 1 at the low-end. + * + * WARNING -- because this function takes a byte for maxQual, you must be careful in converting + * integers to byte. The appropriate way to do this is ((byte)(myInt & 0xFF)) + * + * @param errorRate a probability (0.0-1.0) of being wrong (i.e., 0.01 is 1% change of being wrong) + * @return a quality score (0-maxQual) */ - static public byte probToQual(double prob, double eps) { - double lp = Math.round(-10.0*Math.log10(1.0 - prob + eps)); - //System.out.printf("LP is %f, byte is %d%n", lp, b); - return boundQual((int)lp); - } - - static public double phredScaleCorrectRate(double trueRate) { - return phredScaleErrorRate(1-trueRate); - } - - static public double phredScaleErrorRate(double errorRate) { - return Math.abs(-10.0*Math.log10(errorRate)); + @Requires("errorRate >= 0.0 && errorRate <= 1.0") + public static byte errorProbToQual(final double errorRate, final byte maxQual) { + final double d = Math.round(-10.0*Math.log10(errorRate)); + return boundQual((int)d, maxQual); } /** - * Return a quality score, capped at max qual. - * - * @param qual the uncapped quality score - * @return the capped quality score + * @see #errorProbToQual(double, byte) with proper conversion of maxQual integer to a byte */ - static public byte boundQual(int qual) { - return boundQual(qual, MAX_QUAL_SCORE); + @Requires("maxQual >= 0 && maxQual < 255") + public static byte errorProbToQual(final double prob, final int maxQual) { + return errorProbToQual(prob, (byte)(maxQual & 0xFF)); } /** - * Returns an integer quality score bounded by 1 - maxQual. + * Convert a probability of being right to a phred-scaled quality score (0.99 => 20). * - * @param qual the quality score - * @param maxQual the maximum quality - * @return the integer betwen 1 and maxqual. + * Note, this function caps the resulting quality score by the public static value MAX_SAM_QUAL_SCORE + * and by 1 at the low-end. + * + * @param prob a probability (0.0-1.0) of being right + * @return a quality score (0-MAX_SAM_QUAL_SCORE) */ - static public byte boundQual(int qual, byte maxQual) { - return (byte) Math.max(Math.min(qual, maxQual), 1); + @Requires("prob >= 0.0 && prob <= 1.0") + public static byte trueProbToQual(final double prob) { + return trueProbToQual(prob, MAX_SAM_QUAL_SCORE); + } + + /** + * Convert a probability of being right to a phred-scaled quality score (0.99 => 20). + * + * Note, this function caps the resulting quality score by the min probability allowed (EPS). + * So for example, if prob is 1e-6, which would imply a Q-score of 60, and EPS is 1e-4, + * the result of this function is actually Q40. + * + * Note that the resulting quality score, regardless of EPS, is capped by MAX_SAM_QUAL_SCORE and + * bounded on the low-side by 1. + * + * WARNING -- because this function takes a byte for maxQual, you must be careful in converting + * integers to byte. The appropriate way to do this is ((byte)(myInt & 0xFF)) + * + * @param prob a probability (0.0-1.0) of being right + * @param maxQual the maximum quality score we are allowed to emit here, regardless of the error rate + * @return a phred-scaled quality score (0-maxQualScore) as a byte + */ + @Requires({ + "prob >= 0.0 && prob <= 1.0" + }) + @Ensures("(result & 0xFF) >= 1 && (result & 0xFF) <= (maxQual & 0xFF)") + public static byte trueProbToQual(final double prob, final byte maxQual) { + final double lp = Math.round(-10.0*MathUtils.log10OneMinusX(prob)); + return boundQual((int)lp, maxQual); + } + + /** + * @see #trueProbToQual(double, byte) with proper conversion of maxQual to a byte + */ + @Requires("maxQual >= 0 && maxQual < 255") + public static byte trueProbToQual(final double prob, final int maxQual) { + return trueProbToQual(prob, (byte)(maxQual & 0xFF)); + } + + /** + * Convert a probability of being right to a phred-scaled quality score of being wrong as a double + * + * This is a very generic method, that simply computes a phred-scaled double quality + * score given an error rate. It has the same precision as a normal double operation + * + * @param trueRate the probability of being right (0.0-1.0) + * @return a phred-scaled version of the error rate implied by trueRate + */ + @Requires("MathUtils.goodProbability(trueRate)") + @Ensures("result >= 0.0") + public static double phredScaleCorrectRate(final double trueRate) { + return phredScaleLog10ErrorRate(MathUtils.log10OneMinusX(trueRate)); + } + + /** + * Convert a probability of being wrong to a phred-scaled quality score of being wrong as a double + * + * This is a very generic method, that simply computes a phred-scaled double quality + * score given an error rate. It has the same precision as a normal double operation + * + * @param errorRate the probability of being wrong (0.0-1.0) + * @return a phred-scaled version of the error rate + */ + @Requires("MathUtils.goodProbability(errorRate)") + @Ensures("result >= 0.0") + public static double phredScaleErrorRate(final double errorRate) { + return phredScaleLog10ErrorRate(Math.log10(errorRate)); + } + + /** + * Convert a log10 probability of being wrong to a phred-scaled quality score of being wrong as a double + * + * This is a very generic method, that simply computes a phred-scaled double quality + * score given an error rate. It has the same precision as a normal double operation + * + * @param errorRateLog10 the log10 probability of being wrong (0.0-1.0) + * @return a phred-scaled version of the error rate + */ + @Ensures("result >= 0.0") + public static double phredScaleLog10ErrorRate(final double errorRateLog10) { + return -10.0 * Math.max(errorRateLog10, RAW_MIN_PHRED_SCALED_QUAL); + } + + // ---------------------------------------------------------------------- + // + // Routines to bound a quality score to a reasonable range + // + // ---------------------------------------------------------------------- + + /** + * Return a quality score that bounds qual by MAX_SAM_QUAL_SCORE and 1 + * + * @param qual the uncapped quality score as an integer + * @return the bounded quality score + */ + @Requires("qual >= 0") + @Ensures("(result & 0xFF) >= 1 && (result & 0xFF) <= (MAX_SAM_QUAL_SCORE & 0xFF)") + public static byte boundQual(int qual) { + return boundQual(qual, MAX_SAM_QUAL_SCORE); + } + + /** + * Return a quality score that bounds qual by maxQual and 1 + * + * WARNING -- because this function takes a byte for maxQual, you must be careful in converting + * integers to byte. The appropriate way to do this is ((byte)(myInt & 0xFF)) + * + * @param qual the uncapped quality score as an integer + * @param maxQual the maximum quality score, must be less < 255 + * @return the bounded quality score + */ + @Requires({"qual >= 0"}) + @Ensures("(result & 0xFF) >= 1 && (result & 0xFF) <= (maxQual & 0xFF)") + public static byte boundQual(final int qual, final byte maxQual) { + return (byte) (Math.max(Math.min(qual, maxQual & 0xFF), 1) & 0xFF); } } diff --git a/public/java/src/org/broadinstitute/sting/utils/duplicates/DupUtils.java b/public/java/src/org/broadinstitute/sting/utils/duplicates/DupUtils.java index c78294505..afd51eb26 100644 --- a/public/java/src/org/broadinstitute/sting/utils/duplicates/DupUtils.java +++ b/public/java/src/org/broadinstitute/sting/utils/duplicates/DupUtils.java @@ -94,8 +94,7 @@ public class DupUtils { Arrays.sort(probs); double normalizedP = Math.pow(10, bestProb) / sumProbs; - double eps = Math.pow(10, -maxQScore/10.0); - byte qual = QualityUtils.probToQual(normalizedP, eps); + byte qual = QualityUtils.trueProbToQual(normalizedP, maxQScore); // if ( false ) { // System.out.printf("Best base is %s %.8f%n", bestBase, bestProb); // System.out.printf("2nd base is %.8f%n", probs[1]); diff --git a/public/java/src/org/broadinstitute/sting/utils/locusiterator/LIBSPerformance.java b/public/java/src/org/broadinstitute/sting/utils/locusiterator/LIBSPerformance.java index 8069ea29f..17d09c844 100644 --- a/public/java/src/org/broadinstitute/sting/utils/locusiterator/LIBSPerformance.java +++ b/public/java/src/org/broadinstitute/sting/utils/locusiterator/LIBSPerformance.java @@ -129,7 +129,7 @@ public class LIBSPerformance extends CommandLineProgram { // read.setReadBases(Utils.dupBytes((byte) 'A', readLength)); // final byte[] quals = new byte[readLength]; // for ( int i = 0; i < readLength; i++ ) -// quals[i] = (byte)(i % QualityUtils.MAX_QUAL_SCORE); +// quals[i] = (byte)(i % QualityUtils.MAX_SAM_QUAL_SCORE); // read.setBaseQualities(quals); // read.setCigarString(cigar); // diff --git a/public/java/test/org/broadinstitute/sting/utils/QualityUtilsUnitTest.java b/public/java/test/org/broadinstitute/sting/utils/QualityUtilsUnitTest.java index 997c8750c..1efce3cb0 100644 --- a/public/java/test/org/broadinstitute/sting/utils/QualityUtilsUnitTest.java +++ b/public/java/test/org/broadinstitute/sting/utils/QualityUtilsUnitTest.java @@ -34,6 +34,7 @@ package org.broadinstitute.sting.utils; import org.broadinstitute.sting.BaseTest; import org.testng.Assert; import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.util.*; @@ -42,10 +43,64 @@ import java.util.*; * Basic unit test for QualityUtils class */ public class QualityUtilsUnitTest extends BaseTest { + final private static double TOLERANCE = 1e-9; + @BeforeClass public void init() { } + @DataProvider(name = "QualTest") + public Object[][] makeMyDataProvider() { + List tests = new ArrayList(); + + for ( int qual = 0; qual < 255; qual++ ) { + tests.add(new Object[]{(byte)(qual & 0xFF), Math.pow(10.0, ((double)qual)/-10.0)}); + } + + return tests.toArray(new Object[][]{}); + } + + /** + * Example testng test using MyDataProvider + */ + @Test(dataProvider = "QualTest") + public void testMyData(final byte qual, final double errorRate) { + final double trueRate = 1 - errorRate; + + final double actualErrorRate = QualityUtils.qualToErrorProb(qual); + Assert.assertEquals(actualErrorRate, errorRate, TOLERANCE); + final double actualTrueRate = QualityUtils.qualToProb(qual); + Assert.assertEquals(actualTrueRate, trueRate, TOLERANCE); + + // log10 tests + final double actualLog10ErrorRate = QualityUtils.qualToErrorProbLog10(qual); + Assert.assertEquals(actualLog10ErrorRate, Math.log10(errorRate), TOLERANCE); + final double actualLog10TrueRate = QualityUtils.qualToProbLog10(qual); + Assert.assertEquals(actualLog10TrueRate, Math.log10(trueRate), TOLERANCE); + + // test that we can convert our error rates to quals, accounting for boundaries + final int expectedQual = Math.max(Math.min(qual & 0xFF, QualityUtils.MAX_SAM_QUAL_SCORE), 1); + final byte actualQual = QualityUtils.trueProbToQual(trueRate); + Assert.assertEquals(actualQual, expectedQual & 0xFF); + final byte actualQualFromErrorRate = QualityUtils.errorProbToQual(errorRate); + Assert.assertEquals(actualQualFromErrorRate, expectedQual & 0xFF); + + for ( int maxQual = 10; maxQual < QualityUtils.MAX_SAM_QUAL_SCORE; maxQual++ ) { + final byte maxAsByte = (byte)(maxQual & 0xFF); + final byte expectedQual2 = (byte)(Math.max(Math.min(qual & 0xFF, maxQual), 1) & 0xFF); + final byte actualQual2 = QualityUtils.trueProbToQual(trueRate, maxAsByte); + Assert.assertEquals(actualQual2, expectedQual2, "Failed with max " + maxQual); + final byte actualQualFromErrorRate2 = QualityUtils.errorProbToQual(errorRate, maxAsByte); + Assert.assertEquals(actualQualFromErrorRate2, expectedQual2, "Failed with max " + maxQual); + + // test the integer routines + final byte actualQualInt2 = QualityUtils.trueProbToQual(trueRate, maxQual); + Assert.assertEquals(actualQualInt2, expectedQual2, "Failed with max " + maxQual); + final byte actualQualFromErrorRateInt2 = QualityUtils.errorProbToQual(errorRate, maxQual); + Assert.assertEquals(actualQualFromErrorRateInt2, expectedQual2, "Failed with max " + maxQual); + } + } + @Test public void testQualCaches() { Assert.assertEquals(QualityUtils.qualToErrorProb((byte) 20), 0.01, 1e-6); @@ -63,4 +118,59 @@ public class QualityUtilsUnitTest extends BaseTest { Assert.assertEquals(QualityUtils.qualToProb((byte) 40), 0.9999, 1e-6); Assert.assertEquals(QualityUtils.qualToProbLog10((byte) 40), -4.34316198e-5, 1e-6); } + + @Test() + public void testBoundingDefault() { + for ( int qual = 0; qual < 1000; qual++ ) { + final byte expected = (byte)Math.max(Math.min(qual, QualityUtils.MAX_SAM_QUAL_SCORE), 1); + Assert.assertEquals(QualityUtils.boundQual(qual), expected); + } + } + + @Test() + public void testBoundingWithMax() { + for ( int max = 10; max < 255; max += 50 ) { + for ( int qual = 0; qual < 1000; qual++ ) { + final int expected = Math.max(Math.min(qual, max), 1); + Assert.assertEquals(QualityUtils.boundQual(qual, (byte)(max & 0xFF)) & 0xFF, expected & 0xFF, "qual " + qual + " max " + max); + } + } + } + + @DataProvider(name = "PhredScaleDoubleOps") + public Object[][] makePhredDoubleTest() { + List tests = new ArrayList(); + + tests.add(new Object[]{0.0, -10 * Math.log10(Double.MIN_VALUE)}); + tests.add(new Object[]{1.0, 0.0}); + for ( int pow = 1; pow < 20; pow++ ) { + tests.add(new Object[]{Math.pow(10.0, -1.0 * pow), pow * 10}); + tests.add(new Object[]{Math.pow(10.0, -1.5 * pow), pow * 15}); + } + + return tests.toArray(new Object[][]{}); + } + + @Test() + public void testQualToErrorProbDouble() { + for ( double qual = 3.0; qual < 255.0; qual += 0.1 ) { + final double expected = Math.pow(10.0, qual / -10.0); + Assert.assertEquals(QualityUtils.qualToErrorProb(qual), expected, TOLERANCE, "failed qual->error prob for double qual " + qual); + } + } + + + @Test(dataProvider = "PhredScaleDoubleOps") + public void testPhredScaleDoubleOps(final double errorRate, final double expectedPhredScaled) { + final double actualError = QualityUtils.phredScaleErrorRate(errorRate); + Assert.assertEquals(actualError, expectedPhredScaled, TOLERANCE); + final double trueRate = 1 - errorRate; + final double actualTrue = QualityUtils.phredScaleCorrectRate(trueRate); + if ( trueRate == 1.0 ) { + Assert.assertEquals(actualTrue, QualityUtils.MIN_PHRED_SCALED_QUAL); + } else { + final double tol = errorRate < 1e-10 ? 10.0 : 1e-3; + Assert.assertEquals(actualTrue, expectedPhredScaled, tol); + } + } } \ No newline at end of file diff --git a/public/java/test/org/broadinstitute/sting/utils/locusiterator/LocusIteratorBenchmark.java b/public/java/test/org/broadinstitute/sting/utils/locusiterator/LocusIteratorBenchmark.java index e52cd46cc..9c3472752 100644 --- a/public/java/test/org/broadinstitute/sting/utils/locusiterator/LocusIteratorBenchmark.java +++ b/public/java/test/org/broadinstitute/sting/utils/locusiterator/LocusIteratorBenchmark.java @@ -28,7 +28,6 @@ package org.broadinstitute.sting.utils.locusiterator; import com.google.caliper.Param; import com.google.caliper.SimpleBenchmark; import net.sf.samtools.SAMFileHeader; -import net.sf.samtools.SAMRecord; import org.broadinstitute.sting.gatk.contexts.AlignmentContext; import org.broadinstitute.sting.utils.GenomeLocParser; import org.broadinstitute.sting.utils.QualityUtils; @@ -63,7 +62,7 @@ public class LocusIteratorBenchmark extends SimpleBenchmark { read.setReadBases(Utils.dupBytes((byte) 'A', readLength)); final byte[] quals = new byte[readLength]; for ( int i = 0; i < readLength; i++ ) - quals[i] = (byte)(i % QualityUtils.MAX_QUAL_SCORE); + quals[i] = (byte)(i % QualityUtils.MAX_SAM_QUAL_SCORE); read.setBaseQualities(quals); read.setCigarString(cigar); reads.add(read); diff --git a/public/java/test/org/broadinstitute/sting/utils/locusiterator/LocusIteratorByStateBaseTest.java b/public/java/test/org/broadinstitute/sting/utils/locusiterator/LocusIteratorByStateBaseTest.java index 1a51440ad..ee65109ca 100644 --- a/public/java/test/org/broadinstitute/sting/utils/locusiterator/LocusIteratorByStateBaseTest.java +++ b/public/java/test/org/broadinstitute/sting/utils/locusiterator/LocusIteratorByStateBaseTest.java @@ -141,7 +141,7 @@ public class LocusIteratorByStateBaseTest extends BaseTest { read.setReadBases(Utils.dupBytes((byte) 'A', readLength)); final byte[] quals = new byte[readLength]; for ( int i = 0; i < readLength; i++ ) - quals[i] = (byte)(i % QualityUtils.MAX_QUAL_SCORE); + quals[i] = (byte)(i % QualityUtils.MAX_SAM_QUAL_SCORE); read.setBaseQualities(quals); read.setCigarString(cigarString); return read; diff --git a/public/java/test/org/broadinstitute/sting/utils/locusiterator/LocusIteratorByStateUnitTest.java b/public/java/test/org/broadinstitute/sting/utils/locusiterator/LocusIteratorByStateUnitTest.java index eb7e61ed8..fd87c1c12 100644 --- a/public/java/test/org/broadinstitute/sting/utils/locusiterator/LocusIteratorByStateUnitTest.java +++ b/public/java/test/org/broadinstitute/sting/utils/locusiterator/LocusIteratorByStateUnitTest.java @@ -315,7 +315,7 @@ public class LocusIteratorByStateUnitTest extends LocusIteratorByStateBaseTest { read.setReadBases(("TT" + eventBases + "A").getBytes()); final byte[] quals = new byte[readLength]; for ( int i = 0; i < readLength; i++ ) - quals[i] = (byte)(i % QualityUtils.MAX_QUAL_SCORE); + quals[i] = (byte)(i % QualityUtils.MAX_SAM_QUAL_SCORE); read.setBaseQualities(quals); read.setCigarString(cigar); From 9a29d6d4be4b929313a5e59b39c02f6a57d10daa Mon Sep 17 00:00:00 2001 From: Mark DePristo Date: Mon, 11 Feb 2013 10:42:37 -0800 Subject: [PATCH 2/6] Fix an catastrophic bug (WoW!) in the reference calculation of the UG -- The UG was using MathUtils binomial probability backward, so that the estimated confidence was always NaN, and was as a side effect other utils converted this to a meaningless 0.0. This is all because there wasn't a unit test. -- I've fixed the calculation, so it's now log10 based, uses robust MathUtils and QualityUtils functions to compute probabilities, and added a unit test. --- .../genotyper/UnifiedGenotyperEngine.java | 49 +++++--- .../UnifiedGenotyperEngineUnitTest.java | 105 ++++++++++++++++++ .../sting/utils/QualityUtils.java | 18 ++- 3 files changed, 154 insertions(+), 18 deletions(-) create mode 100644 protected/java/test/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngineUnitTest.java diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngine.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngine.java index 4cfd8c7bc..ede0741ff 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngine.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngine.java @@ -46,6 +46,7 @@ package org.broadinstitute.sting.gatk.walkers.genotyper; +import com.google.java.contract.Ensures; import com.google.java.contract.Requires; import org.apache.log4j.Logger; import org.broadinstitute.sting.commandline.RodBinding; @@ -138,6 +139,10 @@ public class UnifiedGenotyperEngine { this(toolkit, UAC, Logger.getLogger(UnifiedGenotyperEngine.class), null, null, SampleUtils.getSAMFileSamples(toolkit.getSAMFileHeader()), GATKVariantContextUtils.DEFAULT_PLOIDY); } + protected UnifiedGenotyperEngine(GenomeAnalysisEngine toolkit, Set samples, UnifiedArgumentCollection UAC) { + this(toolkit, UAC, Logger.getLogger(UnifiedGenotyperEngine.class), null, null, samples, GATKVariantContextUtils.DEFAULT_PLOIDY); + } + @Requires({"toolkit != null", "UAC != null", "logger != null", "samples != null && samples.size() > 0","ploidy>0"}) public UnifiedGenotyperEngine(GenomeAnalysisEngine toolkit, UnifiedArgumentCollection UAC, Logger logger, PrintStream verboseWriter, VariantAnnotatorEngine engine, Set samples, int ploidy) { this.BAQEnabledOnCMDLine = toolkit.getArguments().BAQMode != BAQ.CalculationMode.OFF; @@ -577,43 +582,53 @@ public class UnifiedGenotyperEngine { } private final static double[] binomialProbabilityDepthCache = new double[10000]; + private final static double REF_BINOMIAL_PROB_LOG10_0_5 = Math.log10(0.5); + static { for ( int i = 1; i < binomialProbabilityDepthCache.length; i++ ) { - binomialProbabilityDepthCache[i] = MathUtils.binomialProbability(0, i, 0.5); + binomialProbabilityDepthCache[i] = MathUtils.log10BinomialProbability(i, 0, REF_BINOMIAL_PROB_LOG10_0_5); } } - private final double getRefBinomialProb(final int depth) { + private final double getRefBinomialProbLog10(final int depth) { if ( depth < binomialProbabilityDepthCache.length ) return binomialProbabilityDepthCache[depth]; else - return MathUtils.binomialProbability(0, depth, 0.5); + return MathUtils.log10BinomialProbability(depth, 0, REF_BINOMIAL_PROB_LOG10_0_5); } - private VariantCallContext estimateReferenceConfidence(VariantContext vc, Map contexts, double theta, boolean ignoreCoveredSamples, double initialPofRef) { if ( contexts == null ) return null; - double P_of_ref = initialPofRef; + double log10POfRef = Math.log10(initialPofRef); // for each sample that we haven't examined yet for ( String sample : samples ) { - boolean isCovered = contexts.containsKey(sample); - if ( ignoreCoveredSamples && isCovered ) + final AlignmentContext context = contexts.get(sample); + if ( ignoreCoveredSamples && context != null ) continue; - - - int depth = 0; - - if ( isCovered ) { - depth = contexts.get(sample).getBasePileup().depthOfCoverage(); - } - - P_of_ref *= 1.0 - (theta / 2.0) * getRefBinomialProb(depth); + final int depth = context == null ? 0 : context.getBasePileup().depthOfCoverage(); + log10POfRef += estimateLog10ReferenceConfidenceForOneSample(depth, theta); } - return new VariantCallContext(vc, QualityUtils.phredScaleCorrectRate(P_of_ref) >= UAC.STANDARD_CONFIDENCE_FOR_CALLING, false); + return new VariantCallContext(vc, QualityUtils.phredScaleLog10CorrectRate(log10POfRef) >= UAC.STANDARD_CONFIDENCE_FOR_CALLING, false); + } + + /** + * Compute the log10 probability of a sample with sequencing depth and no alt allele is actually truly homozygous reference + * + * Assumes the sample is diploid + * + * @param depth the depth of the sample + * @param theta the heterozygosity of this species (between 0 and 1) + * @return a valid log10 probability of the sample being hom-ref + */ + @Requires({"depth >= 0", "theta >= 0.0 && theta <= 1.0"}) + @Ensures("MathUtils.goodLog10Probability(result)") + protected double estimateLog10ReferenceConfidenceForOneSample(final int depth, final double theta) { + final double log10PofNonRef = Math.log10(theta / 2.0) + getRefBinomialProbLog10(depth); + return MathUtils.log10OneMinusX(Math.pow(10.0, log10PofNonRef)); } protected void printVerboseData(String pos, VariantContext vc, double PofF, double phredScaledConfidence, final GenotypeLikelihoodsCalculationModel.Model model) { diff --git a/protected/java/test/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngineUnitTest.java b/protected/java/test/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngineUnitTest.java new file mode 100644 index 000000000..23596db83 --- /dev/null +++ b/protected/java/test/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngineUnitTest.java @@ -0,0 +1,105 @@ +/* +* By downloading the PROGRAM you agree to the following terms of use: +* +* BROAD INSTITUTE - SOFTWARE LICENSE AGREEMENT - FOR ACADEMIC NON-COMMERCIAL RESEARCH PURPOSES ONLY +* +* This Agreement is made between the Broad Institute, Inc. with a principal address at 7 Cambridge Center, Cambridge, MA 02142 (BROAD) and the LICENSEE and is effective at the date the downloading is completed (EFFECTIVE DATE). +* +* WHEREAS, LICENSEE desires to license the PROGRAM, as defined hereinafter, and BROAD wishes to have this PROGRAM utilized in the public interest, subject only to the royalty-free, nonexclusive, nontransferable license rights of the United States Government pursuant to 48 CFR 52.227-14; and +* WHEREAS, LICENSEE desires to license the PROGRAM and BROAD desires to grant a license on the following terms and conditions. +* NOW, THEREFORE, in consideration of the promises and covenants made herein, the parties hereto agree as follows: +* +* 1. DEFINITIONS +* 1.1 PROGRAM shall mean copyright in the object code and source code known as GATK2 and related documentation, if any, as they exist on the EFFECTIVE DATE and can be downloaded from http://www.broadinstitute/GATK on the EFFECTIVE DATE. +* +* 2. LICENSE +* 2.1 Grant. Subject to the terms of this Agreement, BROAD hereby grants to LICENSEE, solely for academic non-commercial research purposes, a non-exclusive, non-transferable license to: (a) download, execute and display the PROGRAM and (b) create bug fixes and modify the PROGRAM. +* The LICENSEE may apply the PROGRAM in a pipeline to data owned by users other than the LICENSEE and provide these users the results of the PROGRAM provided LICENSEE does so for academic non-commercial purposes only. For clarification purposes, academic sponsored research is not a commercial use under the terms of this Agreement. +* 2.2 No Sublicensing or Additional Rights. LICENSEE shall not sublicense or distribute the PROGRAM, in whole or in part, without prior written permission from BROAD. LICENSEE shall ensure that all of its users agree to the terms of this Agreement. LICENSEE further agrees that it shall not put the PROGRAM on a network, server, or other similar technology that may be accessed by anyone other than the LICENSEE and its employees and users who have agreed to the terms of this agreement. +* 2.3 License Limitations. Nothing in this Agreement shall be construed to confer any rights upon LICENSEE by implication, estoppel, or otherwise to any computer software, trademark, intellectual property, or patent rights of BROAD, or of any other entity, except as expressly granted herein. LICENSEE agrees that the PROGRAM, in whole or part, shall not be used for any commercial purpose, including without limitation, as the basis of a commercial software or hardware product or to provide services. LICENSEE further agrees that the PROGRAM shall not be copied or otherwise adapted in order to circumvent the need for obtaining a license for use of the PROGRAM. +* +* 3. OWNERSHIP OF INTELLECTUAL PROPERTY +* LICENSEE acknowledges that title to the PROGRAM shall remain with BROAD. The PROGRAM is marked with the following BROAD copyright notice and notice of attribution to contributors. LICENSEE shall retain such notice on all copies. LICENSEE agrees to include appropriate attribution if any results obtained from use of the PROGRAM are included in any publication. +* Copyright 2012 Broad Institute, Inc. +* Notice of attribution: The GATK2 program was made available through the generosity of Medical and Population Genetics program at the Broad Institute, Inc. +* LICENSEE shall not use any trademark or trade name of BROAD, or any variation, adaptation, or abbreviation, of such marks or trade names, or any names of officers, faculty, students, employees, or agents of BROAD except as states above for attribution purposes. +* +* 4. INDEMNIFICATION +* LICENSEE shall indemnify, defend, and hold harmless BROAD, and their respective officers, faculty, students, employees, associated investigators and agents, and their respective successors, heirs and assigns, (Indemnitees), against any liability, damage, loss, or expense (including reasonable attorneys fees and expenses) incurred by or imposed upon any of the Indemnitees in connection with any claims, suits, actions, demands or judgments arising out of any theory of liability (including, without limitation, actions in the form of tort, warranty, or strict liability and regardless of whether such action has any factual basis) pursuant to any right or license granted under this Agreement. +* +* 5. NO REPRESENTATIONS OR WARRANTIES +* THE PROGRAM IS DELIVERED AS IS. BROAD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE PROGRAM OR THE COPYRIGHT, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE. BROAD EXTENDS NO WARRANTIES OF ANY KIND AS TO PROGRAM CONFORMITY WITH WHATEVER USER MANUALS OR OTHER LITERATURE MAY BE ISSUED FROM TIME TO TIME. +* IN NO EVENT SHALL BROAD OR ITS RESPECTIVE DIRECTORS, OFFICERS, EMPLOYEES, AFFILIATED INVESTIGATORS AND AFFILIATES BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, INCLUDING, WITHOUT LIMITATION, ECONOMIC DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER BROAD SHALL BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE FOREGOING. +* +* 6. ASSIGNMENT +* This Agreement is personal to LICENSEE and any rights or obligations assigned by LICENSEE without the prior written consent of BROAD shall be null and void. +* +* 7. MISCELLANEOUS +* 7.1 Export Control. LICENSEE gives assurance that it will comply with all United States export control laws and regulations controlling the export of the PROGRAM, including, without limitation, all Export Administration Regulations of the United States Department of Commerce. Among other things, these laws and regulations prohibit, or require a license for, the export of certain types of software to specified countries. +* 7.2 Termination. LICENSEE shall have the right to terminate this Agreement for any reason upon prior written notice to BROAD. If LICENSEE breaches any provision hereunder, and fails to cure such breach within thirty (30) days, BROAD may terminate this Agreement immediately. Upon termination, LICENSEE shall provide BROAD with written assurance that the original and all copies of the PROGRAM have been destroyed, except that, upon prior written authorization from BROAD, LICENSEE may retain a copy for archive purposes. +* 7.3 Survival. The following provisions shall survive the expiration or termination of this Agreement: Articles 1, 3, 4, 5 and Sections 2.2, 2.3, 7.3, and 7.4. +* 7.4 Notice. Any notices under this Agreement shall be in writing, shall specifically refer to this Agreement, and shall be sent by hand, recognized national overnight courier, confirmed facsimile transmission, confirmed electronic mail, or registered or certified mail, postage prepaid, return receipt requested. All notices under this Agreement shall be deemed effective upon receipt. +* 7.5 Amendment and Waiver; Entire Agreement. This Agreement may be amended, supplemented, or otherwise modified only by means of a written instrument signed by all parties. Any waiver of any rights or failure to act in a specific instance shall relate only to such instance and shall not be construed as an agreement to waive any rights or fail to act in any other instance, whether or not similar. This Agreement constitutes the entire agreement among the parties with respect to its subject matter and supersedes prior agreements or understandings between the parties relating to its subject matter. +* 7.6 Binding Effect; Headings. This Agreement shall be binding upon and inure to the benefit of the parties and their respective permitted successors and assigns. All headings are for convenience only and shall not affect the meaning of any provision of this Agreement. +* 7.7 Governing Law. This Agreement shall be construed, governed, interpreted and applied in accordance with the internal laws of the Commonwealth of Massachusetts, U.S.A., without regard to conflict of laws principles. +*/ + +package org.broadinstitute.sting.gatk.walkers.genotyper; + + +// the imports for unit testing. + + +import org.broadinstitute.sting.BaseTest; +import org.broadinstitute.sting.gatk.GenomeAnalysisEngine; +import org.broadinstitute.sting.gatk.arguments.GATKArgumentCollection; +import org.broadinstitute.sting.utils.MathUtils; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.util.*; + +public class UnifiedGenotyperEngineUnitTest extends BaseTest { + private final static double TOLERANCE = 1e-5; + private UnifiedGenotyperEngine ugEngine; + + @BeforeClass + public void setUp() throws Exception { + final GenomeAnalysisEngine engine = new GenomeAnalysisEngine(); + engine.setArguments(new GATKArgumentCollection()); + final UnifiedArgumentCollection args = new UnifiedArgumentCollection(); + final Set fakeSamples = Collections.singleton("fake"); + ugEngine = new UnifiedGenotyperEngine(engine, fakeSamples, args); + } + + private UnifiedGenotyperEngine getEngine() { + return ugEngine; + } + + @DataProvider(name = "ReferenceQualityCalculation") + public Object[][] makeReferenceQualityCalculation() { + List tests = new ArrayList(); + + // this functionality can be adapted to provide input data for whatever you might want in your data + final double p = Math.log10(0.5); + for ( final double theta : Arrays.asList(0.1, 0.01, 0.001) ) { + for ( final int depth : Arrays.asList(0, 1, 2, 10, 100, 1000, 10000) ) { + final double log10PofNonRef = Math.log10(theta / 2.0) + MathUtils.log10BinomialProbability(depth, 0, p); + final double log10POfRef = MathUtils.log10OneMinusX(Math.pow(10.0, log10PofNonRef)); + tests.add(new Object[]{depth, theta, log10POfRef}); + } + } + + return tests.toArray(new Object[][]{}); + } + + @Test(dataProvider = "ReferenceQualityCalculation") + public void testReferenceQualityCalculation(final int depth, final double theta, final double expected) { + final double ref = getEngine().estimateLog10ReferenceConfidenceForOneSample(depth, theta); + Assert.assertTrue(MathUtils.goodLog10Probability(ref), "Reference calculation wasn't a well formed log10 prob " + ref); + Assert.assertEquals(ref, expected, TOLERANCE, "Failed reference confidence for single sample"); + } +} \ No newline at end of file diff --git a/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java b/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java index 9dd9b735d..a7552ca9c 100644 --- a/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java +++ b/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java @@ -311,6 +311,21 @@ public class QualityUtils { return phredScaleLog10ErrorRate(MathUtils.log10OneMinusX(trueRate)); } + /** + * Convert a log10 probability of being right to a phred-scaled quality score of being wrong as a double + * + * This is a very generic method, that simply computes a phred-scaled double quality + * score given an error rate. It has the same precision as a normal double operation + * + * @param trueRateLog10 the probability of being right (0.0-1.0) + * @return a phred-scaled version of the error rate implied by trueRate + */ + @Requires("MathUtils.goodLog10Probability(trueRateLog10)") + @Ensures("result >= 0.0") + public static double phredScaleLog10CorrectRate(final double trueRateLog10) { + return phredScaleCorrectRate(Math.pow(10.0, trueRateLog10)); + } + /** * Convert a probability of being wrong to a phred-scaled quality score of being wrong as a double * @@ -337,7 +352,8 @@ public class QualityUtils { */ @Ensures("result >= 0.0") public static double phredScaleLog10ErrorRate(final double errorRateLog10) { - return -10.0 * Math.max(errorRateLog10, RAW_MIN_PHRED_SCALED_QUAL); + // abs is necessary for edge base with errorRateLog10 = 0 producing -0.0 doubles + return Math.abs(-10.0 * Math.max(errorRateLog10, RAW_MIN_PHRED_SCALED_QUAL)); } // ---------------------------------------------------------------------- From 3231031c1a5c5ef124982262ccd99ccb05f09fc2 Mon Sep 17 00:00:00 2001 From: Mark DePristo Date: Mon, 11 Feb 2013 11:16:19 -0800 Subject: [PATCH 3/6] Bugfix for FisherStrand -- FisherStrand pValues can sum to slightly greater than 1.0, so they need to be capped to convert to a Phred-scaled quality score --- .../gatk/walkers/annotator/FisherStrand.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/protected/java/src/org/broadinstitute/sting/gatk/walkers/annotator/FisherStrand.java b/protected/java/src/org/broadinstitute/sting/gatk/walkers/annotator/FisherStrand.java index ff3d7940f..14c785678 100644 --- a/protected/java/src/org/broadinstitute/sting/gatk/walkers/annotator/FisherStrand.java +++ b/protected/java/src/org/broadinstitute/sting/gatk/walkers/annotator/FisherStrand.java @@ -142,7 +142,7 @@ public class FisherStrand extends InfoFieldAnnotation implements StandardAnnotat public List getDescriptions() { return Arrays.asList( - new VCFInfoHeaderLine(FS, 1, VCFHeaderLineType.Float, "Phred-scaled p-value using Fisher's exact test to detect strand bias")); + new VCFInfoHeaderLine(FS, 1, VCFHeaderLineType.Float, "Phred-scaled p-value using Fisher's exact test to detect strand bias")); } private Double pValueForContingencyTable(int[][] originalTable) { @@ -176,7 +176,8 @@ public class FisherStrand extends InfoFieldAnnotation implements StandardAnnotat //System.out.printf("P-cutoff: %f\n", pCutoff); //System.out.printf("P-value: %f\n\n", pValue); - return pValue; + // min is necessary as numerical precision can result in pValue being slightly greater than 1.0 + return Math.min(pValue, 1.0); } private static int [][] copyContingencyTable(int [][] t) { @@ -222,14 +223,14 @@ public class FisherStrand extends InfoFieldAnnotation implements StandardAnnotat // calculate in log space so we don't die with high numbers double pCutoff = Arithmetic.logFactorial(rowSums[0]) - + Arithmetic.logFactorial(rowSums[1]) - + Arithmetic.logFactorial(colSums[0]) - + Arithmetic.logFactorial(colSums[1]) - - Arithmetic.logFactorial(table[0][0]) - - Arithmetic.logFactorial(table[0][1]) - - Arithmetic.logFactorial(table[1][0]) - - Arithmetic.logFactorial(table[1][1]) - - Arithmetic.logFactorial(N); + + Arithmetic.logFactorial(rowSums[1]) + + Arithmetic.logFactorial(colSums[0]) + + Arithmetic.logFactorial(colSums[1]) + - Arithmetic.logFactorial(table[0][0]) + - Arithmetic.logFactorial(table[0][1]) + - Arithmetic.logFactorial(table[1][0]) + - Arithmetic.logFactorial(table[1][1]) + - Arithmetic.logFactorial(N); return Math.exp(pCutoff); } From b393c27f0791748e5d00876d90753efd06e0a3c8 Mon Sep 17 00:00:00 2001 From: Mark DePristo Date: Mon, 11 Feb 2013 11:03:15 -0800 Subject: [PATCH 4/6] QualityUtils now uses runtime argument checks instead of contract -- There's some runtime cost for these tests, but it's not big enough to outweigh the value of catching errors quickly --- .../sting/utils/QualityUtils.java | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java b/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java index a7552ca9c..dd958cbb0 100644 --- a/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java +++ b/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java @@ -26,7 +26,6 @@ package org.broadinstitute.sting.utils; import com.google.java.contract.Ensures; -import com.google.java.contract.Requires; import net.sf.samtools.SAMUtils; /** @@ -110,9 +109,9 @@ public class QualityUtils { * @param qual a phred-scaled quality score encoded as a double. Can be non-integer values (30.5) * @return a probability (0.0-1.0) */ - @Requires("qual >= 0.0") @Ensures("result >= 0.0 && result <= 1.0") public static double qualToProb(final double qual) { + if ( qual < 0.0 ) throw new IllegalArgumentException("qual must be >= 0.0 but got " + qual); return 1.0 - qualToErrorProb(qual); } @@ -144,9 +143,9 @@ public class QualityUtils { * @param qual a phred-scaled quality score encoded as a double. Can be non-integer values (30.5) * @return a probability (0.0-1.0) */ - @Requires("qual >= 0.0") @Ensures("result >= 0.0 && result <= 1.0") public static double qualToErrorProb(final double qual) { + if ( qual < 0.0 ) throw new IllegalArgumentException("qual must be >= 0.0 but got " + qual); return Math.pow(10.0, qual / -10.0); } @@ -199,6 +198,7 @@ public class QualityUtils { */ @Ensures("result <= 0.0") public static double qualToErrorProbLog10(final double qual) { + if ( qual < 0.0 ) throw new IllegalArgumentException("qual must be >= 0.0 but got " + qual); return qual / -10.0; } @@ -217,7 +217,6 @@ public class QualityUtils { * @param errorRate a probability (0.0-1.0) of being wrong (i.e., 0.01 is 1% change of being wrong) * @return a quality score (0-MAX_SAM_QUAL_SCORE) */ - @Requires("errorRate >= 0.0 && errorRate <= 1.0") public static byte errorProbToQual(final double errorRate) { return errorProbToQual(errorRate, MAX_SAM_QUAL_SCORE); } @@ -234,8 +233,8 @@ public class QualityUtils { * @param errorRate a probability (0.0-1.0) of being wrong (i.e., 0.01 is 1% change of being wrong) * @return a quality score (0-maxQual) */ - @Requires("errorRate >= 0.0 && errorRate <= 1.0") public static byte errorProbToQual(final double errorRate, final byte maxQual) { + if ( ! MathUtils.goodProbability(errorRate) ) throw new IllegalArgumentException("errorRate must be good probability but got " + errorRate); final double d = Math.round(-10.0*Math.log10(errorRate)); return boundQual((int)d, maxQual); } @@ -243,8 +242,8 @@ public class QualityUtils { /** * @see #errorProbToQual(double, byte) with proper conversion of maxQual integer to a byte */ - @Requires("maxQual >= 0 && maxQual < 255") public static byte errorProbToQual(final double prob, final int maxQual) { + if ( maxQual < 0 || maxQual > 255 ) throw new IllegalArgumentException("maxQual must be between 0-255 but got " + maxQual); return errorProbToQual(prob, (byte)(maxQual & 0xFF)); } @@ -257,7 +256,6 @@ public class QualityUtils { * @param prob a probability (0.0-1.0) of being right * @return a quality score (0-MAX_SAM_QUAL_SCORE) */ - @Requires("prob >= 0.0 && prob <= 1.0") public static byte trueProbToQual(final double prob) { return trueProbToQual(prob, MAX_SAM_QUAL_SCORE); } @@ -275,24 +273,22 @@ public class QualityUtils { * WARNING -- because this function takes a byte for maxQual, you must be careful in converting * integers to byte. The appropriate way to do this is ((byte)(myInt & 0xFF)) * - * @param prob a probability (0.0-1.0) of being right + * @param trueProb a probability (0.0-1.0) of being right * @param maxQual the maximum quality score we are allowed to emit here, regardless of the error rate * @return a phred-scaled quality score (0-maxQualScore) as a byte */ - @Requires({ - "prob >= 0.0 && prob <= 1.0" - }) @Ensures("(result & 0xFF) >= 1 && (result & 0xFF) <= (maxQual & 0xFF)") - public static byte trueProbToQual(final double prob, final byte maxQual) { - final double lp = Math.round(-10.0*MathUtils.log10OneMinusX(prob)); + public static byte trueProbToQual(final double trueProb, final byte maxQual) { + if ( ! MathUtils.goodProbability(trueProb) ) throw new IllegalArgumentException("trueProb must be good probability but got " + trueProb); + final double lp = Math.round(-10.0*MathUtils.log10OneMinusX(trueProb)); return boundQual((int)lp, maxQual); } /** * @see #trueProbToQual(double, byte) with proper conversion of maxQual to a byte */ - @Requires("maxQual >= 0 && maxQual < 255") public static byte trueProbToQual(final double prob, final int maxQual) { + if ( maxQual < 0 || maxQual > 255 ) throw new IllegalArgumentException("maxQual must be between 0-255 but got " + maxQual); return trueProbToQual(prob, (byte)(maxQual & 0xFF)); } @@ -305,7 +301,6 @@ public class QualityUtils { * @param trueRate the probability of being right (0.0-1.0) * @return a phred-scaled version of the error rate implied by trueRate */ - @Requires("MathUtils.goodProbability(trueRate)") @Ensures("result >= 0.0") public static double phredScaleCorrectRate(final double trueRate) { return phredScaleLog10ErrorRate(MathUtils.log10OneMinusX(trueRate)); @@ -320,7 +315,6 @@ public class QualityUtils { * @param trueRateLog10 the probability of being right (0.0-1.0) * @return a phred-scaled version of the error rate implied by trueRate */ - @Requires("MathUtils.goodLog10Probability(trueRateLog10)") @Ensures("result >= 0.0") public static double phredScaleLog10CorrectRate(final double trueRateLog10) { return phredScaleCorrectRate(Math.pow(10.0, trueRateLog10)); @@ -335,7 +329,6 @@ public class QualityUtils { * @param errorRate the probability of being wrong (0.0-1.0) * @return a phred-scaled version of the error rate */ - @Requires("MathUtils.goodProbability(errorRate)") @Ensures("result >= 0.0") public static double phredScaleErrorRate(final double errorRate) { return phredScaleLog10ErrorRate(Math.log10(errorRate)); @@ -352,6 +345,7 @@ public class QualityUtils { */ @Ensures("result >= 0.0") public static double phredScaleLog10ErrorRate(final double errorRateLog10) { + if ( ! MathUtils.goodLog10Probability(errorRateLog10) ) throw new IllegalArgumentException("errorRateLog10 must be good probability but got " + errorRateLog10); // abs is necessary for edge base with errorRateLog10 = 0 producing -0.0 doubles return Math.abs(-10.0 * Math.max(errorRateLog10, RAW_MIN_PHRED_SCALED_QUAL)); } @@ -368,7 +362,6 @@ public class QualityUtils { * @param qual the uncapped quality score as an integer * @return the bounded quality score */ - @Requires("qual >= 0") @Ensures("(result & 0xFF) >= 1 && (result & 0xFF) <= (MAX_SAM_QUAL_SCORE & 0xFF)") public static byte boundQual(int qual) { return boundQual(qual, MAX_SAM_QUAL_SCORE); @@ -384,9 +377,9 @@ public class QualityUtils { * @param maxQual the maximum quality score, must be less < 255 * @return the bounded quality score */ - @Requires({"qual >= 0"}) @Ensures("(result & 0xFF) >= 1 && (result & 0xFF) <= (maxQual & 0xFF)") public static byte boundQual(final int qual, final byte maxQual) { + if ( qual < 0 ) throw new IllegalArgumentException("qual must be >= 0 " + qual); return (byte) (Math.max(Math.min(qual, maxQual & 0xFF), 1) & 0xFF); } } From 3b67aa8aeeeb2631b98b04cd7ebf26e35ae5a46a Mon Sep 17 00:00:00 2001 From: Mark DePristo Date: Mon, 11 Feb 2013 12:42:50 -0800 Subject: [PATCH 5/6] Final edge case bug fixes to QualityUtil routines -- log10 functions in QualityUtils allow -Infinity to allow log10(0.0) values -- Fix edge condition of log10OneMinusX failing with Double.MIN_VALUE -- Fix another edge condition of log10OneMinusX failing with a small but not min_value double --- .../broadinstitute/sting/utils/MathUtils.java | 22 +++++++++++++++---- .../sting/utils/QualityUtils.java | 6 +++-- .../sting/utils/QualityUtilsUnitTest.java | 12 ++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/public/java/src/org/broadinstitute/sting/utils/MathUtils.java b/public/java/src/org/broadinstitute/sting/utils/MathUtils.java index 4db55b275..2459c1d36 100644 --- a/public/java/src/org/broadinstitute/sting/utils/MathUtils.java +++ b/public/java/src/org/broadinstitute/sting/utils/MathUtils.java @@ -1244,11 +1244,23 @@ public class MathUtils { /** * Checks that the result is a well-formed log10 probability * - * @param result a supposedly well-formed log10 probability value + * @param result a supposedly well-formed log10 probability value. By default allows + * -Infinity values, as log10(0.0) == -Infinity. * @return true if result is really well formed */ public static boolean goodLog10Probability(final double result) { - return result <= 0.0 && ! Double.isInfinite(result) && ! Double.isNaN(result); + return goodLog10Probability(result, true); + } + + /** + * Checks that the result is a well-formed log10 probability + * + * @param result a supposedly well-formed log10 probability value + * @param allowNegativeInfinity should we consider a -Infinity value ok? + * @return true if result is really well formed + */ + public static boolean goodLog10Probability(final double result, final boolean allowNegativeInfinity) { + return result <= 0.0 && result != Double.POSITIVE_INFINITY && (allowNegativeInfinity || result != Double.NEGATIVE_INFINITY) && ! Double.isNaN(result); } /** @@ -1779,7 +1791,9 @@ public class MathUtils { return Double.NEGATIVE_INFINITY; else if ( x == 0.0 ) return 0.0; - else - return Math.log10(1 / x - 1) + Math.log10(x); + else { + final double d = Math.log10(1 / x - 1) + Math.log10(x); + return Double.isInfinite(d) || d > 0.0 ? 0.0 : d; + } } } diff --git a/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java b/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java index dd958cbb0..1dcd5a9ae 100644 --- a/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java +++ b/public/java/src/org/broadinstitute/sting/utils/QualityUtils.java @@ -312,7 +312,8 @@ public class QualityUtils { * This is a very generic method, that simply computes a phred-scaled double quality * score given an error rate. It has the same precision as a normal double operation * - * @param trueRateLog10 the probability of being right (0.0-1.0) + * @param trueRateLog10 the log10 probability of being right (0.0-1.0). Can be -Infinity to indicate + * that the result is impossible in which MIN_PHRED_SCALED_QUAL is returned * @return a phred-scaled version of the error rate implied by trueRate */ @Ensures("result >= 0.0") @@ -340,7 +341,8 @@ public class QualityUtils { * This is a very generic method, that simply computes a phred-scaled double quality * score given an error rate. It has the same precision as a normal double operation * - * @param errorRateLog10 the log10 probability of being wrong (0.0-1.0) + * @param errorRateLog10 the log10 probability of being wrong (0.0-1.0). Can be -Infinity, in which case + * the result is MIN_PHRED_SCALED_QUAL * @return a phred-scaled version of the error rate */ @Ensures("result >= 0.0") diff --git a/public/java/test/org/broadinstitute/sting/utils/QualityUtilsUnitTest.java b/public/java/test/org/broadinstitute/sting/utils/QualityUtilsUnitTest.java index 1efce3cb0..f5c7a14df 100644 --- a/public/java/test/org/broadinstitute/sting/utils/QualityUtilsUnitTest.java +++ b/public/java/test/org/broadinstitute/sting/utils/QualityUtilsUnitTest.java @@ -101,6 +101,18 @@ public class QualityUtilsUnitTest extends BaseTest { } } + @Test + public void testTrueProbWithMinDouble() { + final byte actual = QualityUtils.trueProbToQual(Double.MIN_VALUE); + Assert.assertEquals(actual, 1, "Failed to convert true prob of min double to 1 qual"); + } + + @Test + public void testTrueProbWithVerySmallValue() { + final byte actual = QualityUtils.trueProbToQual(1.7857786272673852E-19); + Assert.assertEquals(actual, 1, "Failed to convert true prob of very small value 1.7857786272673852E-19 to 1 qual"); + } + @Test public void testQualCaches() { Assert.assertEquals(QualityUtils.qualToErrorProb((byte) 20), 0.01, 1e-6); From 73a363b1667f1c8cdace21618cc9baa3330c94dc Mon Sep 17 00:00:00 2001 From: Mark DePristo Date: Mon, 11 Feb 2013 13:47:31 -0800 Subject: [PATCH 6/6] Update MD5s due to new QualityUtils calculations -- Increase the allowed runtime of one UG integration test -- The GGA indels mode runs two UG commands, and was barely under the 10 minute limit before. Some updates can push this right over the edge. Increased limit -- CalibrateGenotypeLikelihoods runs on a small data set now, so it's faster -- Updating MD5s due to more correct quality utils. DuplicatesWalkers quality estimates have changed. One UG test has different FS and rank sum tests because the conversion to phred scores are slightly (second decimal place) different --- .../walkers/genotyper/UnifiedGenotyperIntegrationTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protected/java/test/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperIntegrationTest.java b/protected/java/test/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperIntegrationTest.java index df530f995..eb7549bed 100644 --- a/protected/java/test/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperIntegrationTest.java +++ b/protected/java/test/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperIntegrationTest.java @@ -387,7 +387,7 @@ public class UnifiedGenotyperIntegrationTest extends WalkerTest { executeTest("test MultiSample Pilot2 indels with alleles passed in and emitting all sites", spec); } - @Test + @Test(timeOut = 20*1000*60) // this guy can take a long time because it's two steps, so give it 12 minutes public void testMultiSampleIndels1() { // since we're going to test the MD5s with GGA only do one here WalkerTest.WalkerTestSpec spec1 = new WalkerTest.WalkerTestSpec( @@ -397,7 +397,7 @@ public class UnifiedGenotyperIntegrationTest extends WalkerTest { WalkerTest.WalkerTestSpec spec2 = new WalkerTest.WalkerTestSpec( baseCommandIndels + " --genotyping_mode GENOTYPE_GIVEN_ALLELES -alleles " + result.get(0).getAbsolutePath() + " -I " + validationDataLocation + - "low_coverage_CEU.chr1.10k-11k.bam -o %s -L 1:10450700-10551000", 1, + "low_coverage_CEU.chr1.10k-11k.bam -o %s -L " + result.get(0).getAbsolutePath(), 1, Arrays.asList("08b3a85be00c8f6a4fefd3c671463ecf")); executeTest("test MultiSample Pilot1 CEU indels using GENOTYPE_GIVEN_ALLELES", spec2); }