Debian Codegraph @

src/java/org/broadinstitute/dropseqrna/barnyard/digitalallelecounts/sampleassignment/multisample/SamplePairAssignmentForCell.java

raw
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.stat.StatUtils;
27 import org.broadinstitute.dropseqrna.barnyard.digitalallelecounts.LikelihoodUtils;
28
29 public class SamplePairAssignmentForCell {
30
31 private final String cellBarcode;
32 private final String sampleOne;
33 private final String sampleTwo;
34
35 // all likelihoods should be in log10 space.
36 private final double sampleOneSingleLikelihood;
37 private final double sampleTwoSingleLikelihood;
38 private final double doubletLikelihood;
39
40 // if a particular mixture of two cells is forced, keep that here.
41 // private Double forcedMixture=null;
42 // private Double forcedMixtureLikelihood=null;
43
44 private final double mixture;
45 private final int impossibleAllelesSampleOne;
46 private final int impossibleAllelesSampleTwo;
47 private final int numSNPs;
48 private final int numInformativeSNPs;
49 private final int numUMIs;
50 private final int numInformativeUMIs;
51 private final int numInformativeHomozygousUMIsSampleOne;
52 private final int numInformativeHomozygousUMIsSampleTwo;
53
54 // Normalize the likelihoods across many pairs of models for the same cell.
55 // Likelihoods for each model are scaled by this factor.
56 // This is used to select the best pair of donors out of all possible pairs, but has no other impact.
57 private double scalingFactor;
58
59 public SamplePairAssignmentForCell (final String cellBarcode, final String sampleOne, final String sampleTwo,
60 final double sampleOneSingleLikelihood, final double sampleTwoSingleLikelihood, final double doubletLikelihood, final double mixture) {
61 this.cellBarcode=cellBarcode;
62 this.sampleOne=sampleOne;
63 this.sampleTwo=sampleTwo;
64 this.sampleOneSingleLikelihood=sampleOneSingleLikelihood;
65 this.sampleTwoSingleLikelihood=sampleTwoSingleLikelihood;
66 this.doubletLikelihood=doubletLikelihood;
67 this.mixture=mixture;
68 this.impossibleAllelesSampleOne=0;
69 this.impossibleAllelesSampleTwo=0;
70 this.numSNPs=0;
71 this.numInformativeSNPs=0;
72 this.numUMIs=0;
73 this.numInformativeUMIs=0;
74 this.numInformativeHomozygousUMIsSampleOne=0;
75 this.numInformativeHomozygousUMIsSampleTwo=0;
76 this.scalingFactor=1;
77 }
78
79 public SamplePairAssignmentForCell (final String cellBarcode, final String sampleOne, final String sampleTwo,
80 final double sampleOneSingleLikelihood, final double sampleTwoSingleLikelihood, final double doubletLikelihood, final double mixture,
81 final int impossibleAllelesSampleOne, final int impossibleAllelesSampleTwo, final int numInformativeSNPs, final int numSNPs, final int numUMIs, final int numInformativeUMIs,
82 final int numInformativeHomozygousUMIsSampleOne, final int numInformativeHomozygousUMIsSampleTwo) {
83 this.cellBarcode=cellBarcode;
84 this.sampleOne=sampleOne;
85 this.sampleTwo=sampleTwo;
86 this.sampleOneSingleLikelihood=sampleOneSingleLikelihood;
87 this.sampleTwoSingleLikelihood=sampleTwoSingleLikelihood;
88 this.doubletLikelihood=doubletLikelihood;
89 this.mixture=mixture;
90 this.impossibleAllelesSampleOne=impossibleAllelesSampleOne;
91 this.impossibleAllelesSampleTwo=impossibleAllelesSampleTwo;
92 this.numInformativeSNPs=numInformativeSNPs;
93 this.numSNPs=numSNPs;
94 this.numUMIs=numUMIs;
95 this.numInformativeUMIs=numInformativeUMIs;
96 this.numInformativeHomozygousUMIsSampleOne=numInformativeHomozygousUMIsSampleOne;
97 this.numInformativeHomozygousUMIsSampleTwo=numInformativeHomozygousUMIsSampleTwo;
98 this.scalingFactor=1;
99 }
100
101 public static SamplePairAssignmentForCell constructEmptyResult (final String cellBarcode, final String sampleOne) {
102 SamplePairAssignmentForCell r= new SamplePairAssignmentForCell(cellBarcode, sampleOne, sampleOne, 0d, -100, -101, 1, 0, 0, 0, 0, 0, 0, 0, 0);
103 return (r);
104 }
105
106 public double getSampleOneSingleLikelihood() {
107 return sampleOneSingleLikelihood;
108 }
109
110 public double getScaledSampleOneSingleLikelihood () {
111 return this.sampleOneSingleLikelihood*this.scalingFactor;
112 }
113
114 public double getSampleTwoSingleLikelihood() {
115 return sampleTwoSingleLikelihood;
116 }
117
118 public double getScaledSampleTwoSingleLikelihood() {
119 return this.sampleTwoSingleLikelihood*this.scalingFactor;
120 }
121
122 public double getDoubletLikelihood() {
123 return doubletLikelihood;
124 }
125
126 public double getScaledDoubletLikelihood() {
127 return this.doubletLikelihood * this.scalingFactor;
128 }
129
130 public String getCellBarcode() {
131 return cellBarcode;
132 }
133
134 public String getSampleOne() {
135 return sampleOne;
136 }
137
138 public String getSampleTwo() {
139 return sampleTwo;
140 }
141
142 public double getMixture() {
143 return mixture;
144 }
145
146 public int getNumUMIs () {
147 return this.numUMIs;
148 }
149
150 public int getImpossibleAllelesSampleOne() {
151 return impossibleAllelesSampleOne;
152 }
153
154 public int getImpossibleAllelesSampleTwo() {
155 return impossibleAllelesSampleTwo;
156 }
157
158 public int getNumInformativeHomozygousUMIsSampleOne() {
159 return numInformativeHomozygousUMIsSampleOne;
160 }
161
162 public int getNumInformativeHomozygousUMIsSampleTwo() {
163 return numInformativeHomozygousUMIsSampleTwo;
164 }
165
166 public int getNumSNPs() {
167 return numSNPs;
168 }
169
170 public int getNumInformativeSNPs() {
171 return numInformativeSNPs;
172 }
173
174 public double getDoubletLikelihoodRatio () {
175 return this.doubletLikelihood - Math.max(this.sampleOneSingleLikelihood, this.sampleTwoSingleLikelihood);
176 }
177
178 public double getBestLikelihood () {
179 return Math.max(this.doubletLikelihood, Math.max(this.sampleOneSingleLikelihood, this.sampleTwoSingleLikelihood));
180 }
181
182 public double getScaledBestLikelihood () {
183 return Math.max(getScaledDoubletLikelihood(), Math.max(getScaledSampleOneSingleLikelihood(), getScaledSampleTwoSingleLikelihood()));
184 }
185
186 /**
187 * Probability of a doublet is the likelihood of the doublet mixture divided by the sum of all likelihoods (doublet, singlet 1, singlet 2)
188 * @return The probability this cell is a doublet.
189 */
190 public double getDoubletPvalue () {
191 double [] allLikelihoods = {this.sampleOneSingleLikelihood, this.sampleTwoSingleLikelihood, this.doubletLikelihood};
192 double maxValue = StatUtils.max(allLikelihoods);
193 double totalLikelihood=0;
194
195 for (int i=0; i<allLikelihoods.length; i++) {
196 double d = allLikelihoods[i];
197 d=d-maxValue;
198 d=Math.pow(10, d);
199 totalLikelihood+=d;
200 }
201
202 double mixtureLike = Math.pow(10, doubletLikelihood-maxValue);
203 double result = mixtureLike/totalLikelihood;
204 if (result < Double.MIN_VALUE) result = Double.MIN_VALUE;
205
206 return result;
207 }
208
209
210 public String getCombinedDonorName() {
211 return this.sampleOne+":"+this.sampleTwo;
212 }
213
214 public String getBestSample () {
215 if (this.doubletLikelihood> Math.max(this.sampleOneSingleLikelihood, this.sampleTwoSingleLikelihood))
216 return getCombinedDonorName();
217 else if (this.sampleOneSingleLikelihood>this.sampleTwoSingleLikelihood)
218 return this.sampleOne;
219 else
220 return this.sampleTwo;
221 }
222
223 public double getScalingFactor() {
224 return scalingFactor;
225 }
226
227 public void setScalingFactor(double scalingFactor) {
228 this.scalingFactor = scalingFactor;
229 }
230
231
232
233 @Override
234 public String toString () {
235 return this.cellBarcode +" Mixture [" + this.mixture+"] like [" + this.doubletLikelihood+"] S1 [" + this.sampleOne +"="+this.sampleOneSingleLikelihood+"] S2 ["
236 + this.sampleTwo+"="+this.sampleTwoSingleLikelihood+"]" + " impos alleles S1 ["+ this.impossibleAllelesSampleOne +"] impos alleles S2 [" + this.impossibleAllelesSampleTwo+"] "
237 + "num SNPs ["+this.numSNPs+"] num informative SNPs [" + this.numInformativeSNPs +"] num UMIs [" + this.numUMIs+"] num informative UMIs [" + this.numInformativeUMIs
238 + "] doublet likelihood ratio test [" + getDoubletLikelihoodRatio() +"] " + "bestLikelihood [" + this.getBestLikelihood() + "] best sample [" + this.getBestSample() +"]";
239 }
240
241 public int getNumInformativeUMIs() {
242 return numInformativeUMIs;
243 }
244
245
246 @Override
247 public int hashCode() {
248 final int prime = 31;
249 int result = 1;
250 result = prime * result + ((this.cellBarcode == null) ? 0 : cellBarcode.hashCode());
251 long temp;
252 temp = Double.doubleToLongBits(doubletLikelihood);
253 result = prime * result + (int) (temp ^ (temp >>> 32));
254 result = prime * result + impossibleAllelesSampleOne;
255 result = prime * result + impossibleAllelesSampleTwo;
256 temp = Double.doubleToLongBits(mixture);
257 result = prime * result + (int) (temp ^ (temp >>> 32));
258 result = prime * result + numInformativeSNPs;
259 result = prime * result + numInformativeUMIs;
260 result = prime * result + numSNPs;
261 result = prime * result + numUMIs;
262 result = prime * result + ((sampleOne == null) ? 0 : sampleOne.hashCode());
263 temp = Double.doubleToLongBits(sampleOneSingleLikelihood);
264 result = prime * result + (int) (temp ^ (temp >>> 32));
265 result = prime * result + ((sampleTwo == null) ? 0 : sampleTwo.hashCode());
266 temp = Double.doubleToLongBits(sampleTwoSingleLikelihood);
267 result = prime * result + (int) (temp ^ (temp >>> 32));
268 return result;
269 }
270
271 @Override
272 public boolean equals(Object obj) {
273 if (this == obj)
274 return true;
275 if (obj == null)
276 return false;
277 if (getClass() != obj.getClass())
278 return false;
279 SamplePairAssignmentForCell other = (SamplePairAssignmentForCell) obj;
280 if (cellBarcode == null) {
281 if (other.cellBarcode != null)
282 return false;
283 } else if (!cellBarcode.equals(other.cellBarcode))
284 return false;
285 if (Double.doubleToLongBits(doubletLikelihood) != Double.doubleToLongBits(other.doubletLikelihood))
286 return false;
287 if (impossibleAllelesSampleOne != other.impossibleAllelesSampleOne)
288 return false;
289 if (impossibleAllelesSampleTwo != other.impossibleAllelesSampleTwo)
290 return false;
291 if (Double.doubleToLongBits(mixture) != Double.doubleToLongBits(other.mixture))
292 return false;
293 if (numInformativeSNPs != other.numInformativeSNPs)
294 return false;
295 if (numInformativeUMIs != other.numInformativeUMIs)
296 return false;
297 if (numSNPs != other.numSNPs)
298 return false;
299 if (numUMIs != other.numUMIs)
300 return false;
301 if (sampleOne == null) {
302 if (other.sampleOne != null)
303 return false;
304 } else if (!sampleOne.equals(other.sampleOne))
305 return false;
306 if (Double.doubleToLongBits(sampleOneSingleLikelihood) != Double
307 .doubleToLongBits(other.sampleOneSingleLikelihood))
308 return false;
309 if (sampleTwo == null) {
310 if (other.sampleTwo != null)
311 return false;
312 } else if (!sampleTwo.equals(other.sampleTwo))
313 return false;
314 if (Double.doubleToLongBits(sampleTwoSingleLikelihood) != Double
315 .doubleToLongBits(other.sampleTwoSingleLikelihood))
316 return false;
317 return true;
318 }
319
320
321 /*
322 public Double getForcedMixture() {
323 return forcedMixture;
324 }
325
326 public Double getForcedMixtureLikelihood() {
327 return forcedMixtureLikelihood;
328 }
329 */
330
331 /**
332 * Compares the doublet likelihood to the two single likelihoods.
333 * If the doublet likelihood is equal to the sampleOneSingleLikelihood, then
334 * @param originalMixture
335 * @param doubletLikelihood
336 * @param sampleOneSingleLikelihood
337 * @param sampleTwoSingleLikelihood
338 */
339 /*
340 static double getMixture (final double originalMixture, final double doubletLikelihood, final double sampleOneSingleLikelihood, final double sampleTwoSingleLikelihood) {
341 double epsilon = Math.min(doubletLikelihood,sampleOneSingleLikelihood)*0.01;
342 int cmp = Precision.compareTo(doubletLikelihood, sampleOneSingleLikelihood, epsilon);
343 // the first sample likelihood explains the data as well as the doublet likelihood. The mixture is 1.
344 if (cmp==0) return 1;
345
346 epsilon = Math.min(doubletLikelihood,sampleTwoSingleLikelihood)*0.01;
347 cmp = Precision.compareTo(doubletLikelihood, sampleTwoSingleLikelihood, epsilon);
348 // the second sample likelihood explains the data as well as the doublet likelihood. The mixture is 0.
349 if (cmp==0) return 0;
350
351 return originalMixture;
352
353 }
354 */
355
356
357 }