| 1 |
/* |
| 2 |
* MIT License |
| 3 |
* |
| 4 |
* Copyright 2017 Broad Institute |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to deal |
| 8 |
* in the Software without restriction, including without limitation the rights |
| 9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in all |
| 14 |
* copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
* SOFTWARE. |
| 23 |
*/ |
| 24 |
package org.broadinstitute.dropseqrna.barnyard.digitalallelecounts.sampleassignment.multisample; |
| 25 |
|
| 26 |
import org.apache.commons.math3.analysis.UnivariateFunction; |
| 27 |
import org.apache.commons.math3.optim.MaxEval; |
| 28 |
import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; |
| 29 |
import org.apache.commons.math3.optim.univariate.*; |
| 30 |
|
| 31 |
import java.util.List; |
| 32 |
|
| 33 |
public class VariantDataCollection implements UnivariateFunction { |
| 34 |
|
| 35 |
private final List<VariantData> vd; |
| 36 |
private final String sampleOne; |
| 37 |
private final String sampleTwo; |
| 38 |
private final String cell; |
| 39 |
|
| 40 |
public VariantDataCollection(final List<VariantData> vd, final String sampleOne, final String sampleTwo, final String cell) { |
| 41 |
this.vd=vd; |
| 42 |
this.sampleOne=sampleOne; |
| 43 |
this.sampleTwo=sampleTwo; |
| 44 |
this.cell=cell; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public double value(final double mixture) { |
| 49 |
double result=0; |
| 50 |
for (VariantData vd: this.vd) { |
| 51 |
double like = vd.getLogLikelihood(mixture); |
| 52 |
result+=like; |
| 53 |
} |
| 54 |
// double result = this.vd.parallelStream().mapToDouble(x -> x.getLogLikelihood(mixture)).sum(); |
| 55 |
return result; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Generate a sample assignment for this cell, and add a likelihood value for the given mixture. |
| 60 |
* @param forcedDonorMixture A mixture to set, can be null. |
| 61 |
* @return |
| 62 |
*/ |
| 63 |
public SamplePairAssignmentForCell optimizeMixture (final Double forcedDonorMixture) { |
| 64 |
UnivariateOptimizer minimizer = new BrentOptimizer(0.00001, 0.00001); |
| 65 |
|
| 66 |
// set the search interval. |
| 67 |
SearchInterval interval = null; |
| 68 |
if (forcedDonorMixture==null) |
| 69 |
interval= new SearchInterval(0, 1, 0.5); |
| 70 |
else |
| 71 |
interval=new SearchInterval(1-forcedDonorMixture, forcedDonorMixture, 0.5); |
| 72 |
|
| 73 |
// point is the mixture, value is the likelihood. |
| 74 |
UnivariatePointValuePair mixtureResult = minimizer.optimize(new MaxEval(100), GoalType.MAXIMIZE, interval, new UnivariateObjectiveFunction(this)); |
| 75 |
double likelihoodSampleOne = value(1); |
| 76 |
double likelihoodSampleTwo=value(0); |
| 77 |
|
| 78 |
int numInformativeSNPs=getNumInformativeSNPs(); |
| 79 |
int numUMIs=getNumUMIs(); |
| 80 |
int numInformativeUMIs = getNumInformativeUMIs(); |
| 81 |
|
| 82 |
double doubletLikelihood=mixtureResult.getValue(); |
| 83 |
double mixture = mixtureResult.getPoint(); |
| 84 |
// because the optimizer doesn't search the exact bounds of 0 and 1, need to correct the results to include those bounds. |
| 85 |
// if the first single sample explains the data better than the doublet, the mixture is 1. |
| 86 |
// don't do this if you're forcing a mixture interval to search around. |
| 87 |
if (likelihoodSampleOne>=doubletLikelihood && forcedDonorMixture==null) { |
| 88 |
doubletLikelihood=likelihoodSampleOne; |
| 89 |
mixture=1; |
| 90 |
} |
| 91 |
// if the second single sample explains the data better than the doublet, the mixture is 0. |
| 92 |
if (likelihoodSampleTwo>=doubletLikelihood && forcedDonorMixture==null) { |
| 93 |
doubletLikelihood=likelihoodSampleTwo; |
| 94 |
mixture=0; |
| 95 |
} |
| 96 |
|
| 97 |
SamplePairAssignmentForCell result = new SamplePairAssignmentForCell(this.cell, sampleOne, sampleTwo, likelihoodSampleOne, likelihoodSampleTwo, doubletLikelihood, |
| 98 |
mixture, getNumImpossibleAlleles(0), getNumImpossibleAlleles(1), numInformativeSNPs, vd.size(), numUMIs, numInformativeUMIs, |
| 99 |
getNumInformativeHomozygousUMIs(0), getNumInformativeHomozygousUMIs(1)); |
| 100 |
|
| 101 |
return result; |
| 102 |
} |
| 103 |
|
| 104 |
public SamplePairAssignmentForCell optimizeMixture () { |
| 105 |
return optimizeMixture(null); |
| 106 |
} |
| 107 |
|
| 108 |
private int getNumImpossibleAlleles(final int sampleIndex) { |
| 109 |
int count=0; |
| 110 |
for (VariantData v: this.vd) |
| 111 |
count+=v.getNumImpossibleAlleles(sampleIndex); |
| 112 |
return count; |
| 113 |
} |
| 114 |
|
| 115 |
private int getNumInformativeSNPs() { |
| 116 |
int count=0; |
| 117 |
for (VariantData v: this.vd) |
| 118 |
if(v.isInformative()) |
| 119 |
count++; |
| 120 |
return count; |
| 121 |
} |
| 122 |
|
| 123 |
private int getNumInformativeUMIs() { |
| 124 |
int count=0; |
| 125 |
for (VariantData v: this.vd) |
| 126 |
if(v.isInformative()) |
| 127 |
count+=v.getNumUMIs(); |
| 128 |
return count; |
| 129 |
} |
| 130 |
|
| 131 |
private int getNumInformativeHomozygousUMIs(final int sampleIndex) { |
| 132 |
int count=0; |
| 133 |
for (VariantData v: this.vd) |
| 134 |
if(v.isInformativeHomozygous(sampleIndex)) |
| 135 |
count+=v.getNumUMIs(); |
| 136 |
return count; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
private int getNumUMIs() { |
| 141 |
int count=0; |
| 142 |
for (VariantData v: this.vd) |
| 143 |
count+=v.getNumUMIs(); |
| 144 |
return count; |
| 145 |
} |
| 146 |
|
| 147 |
public List<VariantData> getVariantData() { |
| 148 |
return this.vd; |
| 149 |
} |
| 150 |
|
| 151 |
} |