|
| 1 | +package org.broadinstitute.hellbender.tools.exome.gcbias; |
| 2 | + |
| 3 | +import org.apache.commons.math3.linear.ArrayRealVector; |
| 4 | +import org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor; |
| 5 | +import org.apache.commons.math3.linear.RealMatrix; |
| 6 | +import org.apache.commons.math3.linear.RealVector; |
| 7 | +import org.apache.commons.math3.stat.descriptive.rank.Median; |
| 8 | +import org.broadinstitute.hellbender.tools.exome.ReadCountCollection; |
| 9 | +import org.broadinstitute.hellbender.utils.Utils; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.List; |
| 14 | +import java.util.stream.Collectors; |
| 15 | +import java.util.stream.IntStream; |
| 16 | + |
| 17 | +/** |
| 18 | + * Learn multiplicative correction factors as a function of GC content from coverage vs. GC data. Basically, learn a |
| 19 | + * regression curve of coverage vs. GC, and divide by that curve to get GC-corrected coverage. |
| 20 | + * |
| 21 | + * Our regression curve is obtained by filling GC content bins of width 0.01 with the coverages of targets corresponding to each GC |
| 22 | + * and taking the median to get a robust estimate of the curve. In order to smooth out bins with few data (i.e. extreme |
| 23 | + * GC values that occur rarely) we then convolve these medians with an exponential kernel. |
| 24 | + * |
| 25 | + * @author David Benjamin <[email protected]> |
| 26 | + */ |
| 27 | +class GCCorrector { |
| 28 | + // GC bins are 0%, 1% . . . 100% |
| 29 | + private final static int NUMBER_OF_GC_BINS = 101; |
| 30 | + |
| 31 | + // scale (in units of GCcontent from 0 to 1) over which gc bias correlation decays |
| 32 | + // i.e. the bias at GC content = 0.3 and at 0.2 are correlated ~exp(-0.1/correlationLength) |
| 33 | + // this is effectively a smoothness parameter used for regularizing the GC bias estimate for |
| 34 | + // GC content bins that have few targets) |
| 35 | + private static final double correlationLength = 0.02; |
| 36 | + private static final double correlationDecayRatePerBin = 1.0 / (correlationLength * NUMBER_OF_GC_BINS); |
| 37 | + |
| 38 | + // multiply by these to get a GC correction as a function of GC |
| 39 | + private final double[] gcCorrectionFactors; |
| 40 | + |
| 41 | + //Apache commons median doesn't work on empty arrays; this value is a placeholder to avoid exceptions |
| 42 | + private static final double DUMMY_VALUE_NEVER_USED = 1.0; |
| 43 | + |
| 44 | + /** |
| 45 | + * Learn multiplicative correction factors as a function of GC from coverage vs. GC data. Basically, learn a |
| 46 | + * regression curve of coverage vs. GC in order to divide by that curve later. |
| 47 | + * |
| 48 | + * @param gcContents GC content (from 0.0 to 1.0) of targets in {@code coverage} |
| 49 | + * @param coverage raw of proportional coverage |
| 50 | + */ |
| 51 | + public GCCorrector(final double[] gcContents, final RealVector coverage) { |
| 52 | + Utils.nonNull(gcContents); |
| 53 | + Utils.nonNull(coverage); |
| 54 | + Utils.validateArg(gcContents.length > 0, "must have at lest one datum"); |
| 55 | + Utils.validateArg(gcContents.length == coverage.getDimension(), "must have one gc value per coverage."); |
| 56 | + |
| 57 | + final List<List<Double>> coveragesByGC = new ArrayList<>(NUMBER_OF_GC_BINS); |
| 58 | + IntStream.range(0, NUMBER_OF_GC_BINS).forEach(n -> coveragesByGC.add(new ArrayList<>())); |
| 59 | + IntStream.range(0, gcContents.length).forEach(n -> coveragesByGC.get(gcContentToBinIndex(gcContents[n])).add(coverage.getEntry(n))); |
| 60 | + gcCorrectionFactors = calculateCorrectionFactors(coveragesByGC); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * As described above, calculate medians of each GC bin and convolve with an exponential kernel. |
| 65 | + * |
| 66 | + * @param coveragesByGC list of coverages for each GC bin |
| 67 | + * @return multiplicative correction factors for each GC bin |
| 68 | + */ |
| 69 | + private double[] calculateCorrectionFactors(final List<List<Double>> coveragesByGC) { |
| 70 | + final RealVector medians = new ArrayRealVector(coveragesByGC.stream().mapToDouble(GCCorrector::medianOrDefault).toArray()); |
| 71 | + return IntStream.range(0, NUMBER_OF_GC_BINS).mapToDouble(bin -> { |
| 72 | + final RealVector weights = new ArrayRealVector(IntStream.range(0, NUMBER_OF_GC_BINS) |
| 73 | + .mapToDouble(n -> coveragesByGC.get(n).size() * Math.exp(-Math.abs(bin - n) * correlationDecayRatePerBin)).toArray()); |
| 74 | + return weights.dotProduct(medians) / weights.getL1Norm(); |
| 75 | + }).map(x -> 1/x).toArray(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * |
| 80 | + * @param inputCounts raw coverage before GC correction |
| 81 | + * @param gcContentByTarget array of gc contents, one per target of the input |
| 82 | + * @return GC-corrected coverage |
| 83 | + */ |
| 84 | + public static ReadCountCollection correctCoverage(final ReadCountCollection inputCounts, final double[] gcContentByTarget) { |
| 85 | + // each column (sample) has its own GC bias curve, hence its own GC corrector |
| 86 | + final List<GCCorrector> gcCorrectors = IntStream.range(0, inputCounts.columnNames().size()) |
| 87 | + .mapToObj(n -> new GCCorrector(gcContentByTarget, inputCounts.counts().getColumnVector(n))).collect(Collectors.toList()); |
| 88 | + |
| 89 | + // gc correct a copy of the input counts in-place |
| 90 | + final RealMatrix correctedCounts = inputCounts.counts().copy(); |
| 91 | + correctedCounts.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() { |
| 92 | + @Override |
| 93 | + public double visit(int target, int column, double coverage) { |
| 94 | + return gcCorrectors.get(column).correctedCoverage(coverage, gcContentByTarget[target]); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + // we would like the average correction factor to be 1.0 in the sense that average coverage before and after |
| 99 | + // correction should be equal |
| 100 | + final double[] columnNormalizationFactors = IntStream.range(0, inputCounts.columnNames().size()) |
| 101 | + .mapToDouble(c -> inputCounts.counts().getColumnVector(c).getL1Norm() / correctedCounts.getColumnVector(c).getL1Norm()).toArray(); |
| 102 | + correctedCounts.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() { |
| 103 | + @Override |
| 104 | + public double visit(int target, int column, double coverage) { |
| 105 | + return coverage * columnNormalizationFactors[column]; |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + return new ReadCountCollection(inputCounts.targets(), inputCounts.columnNames(), correctedCounts); |
| 110 | + } |
| 111 | + |
| 112 | + private double correctedCoverage(final double coverage, final double gcContent) { |
| 113 | + return gcCorrectionFactors[gcContentToBinIndex(gcContent)] * coverage; |
| 114 | + } |
| 115 | + |
| 116 | + // return a median of coverages or dummy default value if no coverage exists at this gc bin |
| 117 | + // this default is never used because empty bins get zero weight in {@code calculateCorrectionFactors} |
| 118 | + private static double medianOrDefault(final List<Double> list) { |
| 119 | + return list.size() > 0 ? new Median().evaluate(list.stream().mapToDouble(d->d).toArray()) : DUMMY_VALUE_NEVER_USED; |
| 120 | + } |
| 121 | + |
| 122 | + private static int gcContentToBinIndex(final double gcContent) { |
| 123 | + return (int) Math.round(gcContent * (NUMBER_OF_GC_BINS-1)); |
| 124 | + } |
| 125 | +} |
0 commit comments