Debian Codegraph @

src/java/org/broadinstitute/dropseqrna/barnyard/digitalallelecounts/sampleassignment/multisample/AllPairedSampleAssignmentsForCell.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 java.util.ArrayList;
27 import java.util.List;
28 import java.util.stream.Collectors;
29
30 import org.broadinstitute.dropseqrna.barnyard.digitalallelecounts.LikelihoodUtils;
31
32 public class AllPairedSampleAssignmentsForCell {
33
34 private SamplePairAssignmentForCell best;
35 private List<SamplePairAssignmentForCell> all;
36 private final String cell;
37 private boolean dataAdded=false;
38 private double bestPairPvalue;
39 private final boolean scaleLikelihoods;
40
41 public AllPairedSampleAssignmentsForCell (final String cell, final boolean scaleLikelihoods) {
42 this.cell=cell;
43 this.scaleLikelihoods=scaleLikelihoods;
44 all=new ArrayList<>();
45 }
46
47 public void add (final SamplePairAssignmentForCell s) {
48 all.add(s);
49 dataAdded=true;
50 }
51
52 /**
53 * This looks for the pair of donors that is most likely to have generated the data
54 * It searches through all donor pairs where the results are well defined - they have at least one informative UMI to differentiate the two
55 * In the case where no donor pairs have at least one informative UMI (genetically similar donors with very few observations),
56 * select the pair with the most data, and set the mixture to be 0.5. It's impossible to say what the correct mixture is, as all 3
57 * results are equally valid.
58 */
59 private void calculateBest() {
60 // set the scaling on the likelihoods of all donor pairs before recalculating.
61 if (scaleLikelihoods) setScaling();
62
63 // recalculate
64 // TODO: should I instead compare all doublet likelihoods when computing the confident model doublet probability?
65 // if the number of doublet likelihoods < 2, then the pvalue should be 1.
66 // double [] doubleLikelihoods = all.stream().filter(x -> x.getDoubletPvalue()>=0.9d).mapToDouble(x -> x.getScaledDoubletLikelihood()).toArray();
67 double [] likelihoods = all.stream().mapToDouble(x -> x.getScaledDoubletLikelihood()).toArray();
68 double bestPairPvalue = LikelihoodUtils.getInstance().getPvalueFromLog10Likelihood(likelihoods);
69
70 double maxLike=-Double.MAX_VALUE;
71 SamplePairAssignmentForCell best = null;
72
73 for (SamplePairAssignmentForCell spac: all) {
74 double bestLike = spac.getScaledBestLikelihood();
75 // important - you need to have informative UMIs for this to be a good call
76 if (bestLike>maxLike & spac.getNumInformativeUMIs()>0) {
77 maxLike=bestLike;
78 best=spac;
79 }
80 }
81
82 this.best=best;
83 this.bestPairPvalue=bestPairPvalue;
84 dataAdded=false;
85
86 // fall back on finding the best uninformative pair.
87 if (best==null) calculateBestUninformative();
88 }
89
90 /**
91 * Sets a scaling metric where donor pairs with fewer UMIs than the max have an additional penalty to their likelihoods to make them more consistent.
92 */
93 private void setScaling () {
94 int maxUMIs = all.stream().mapToInt(x -> x.getNumUMIs()).max().getAsInt();
95 for (SamplePairAssignmentForCell s: all) {
96 double scaling = (double) maxUMIs / s.getNumUMIs();
97 s.setScalingFactor(scaling);
98 }
99 }
100
101 /**
102 * Scale likelihoods
103 */
104
105 /**
106 * In the case where no donor pairs have at least one informative UMI (genetically similar donors with very few observations),
107 * select the pair with the most data, and set the mixture to be 0.5. It's impossible to say what the correct mixture is, as all 3
108 * results are equally valid.
109 * This can also result in the "best" pair having no UMIs at all.
110 */
111 private void calculateBestUninformative() {
112 double maxSize=0;
113 SamplePairAssignmentForCell best = null;
114 for (SamplePairAssignmentForCell spac: all) {
115 int size = spac.getNumUMIs();
116 if (size>=maxSize) {
117 maxSize=size;
118 best=spac;
119 }
120 }
121
122 // construct a new object and set the mixture to be 0.5
123 SamplePairAssignmentForCell result = new SamplePairAssignmentForCell(best.getCellBarcode(), best.getSampleOne(), best.getSampleTwo(),
124 best.getSampleOneSingleLikelihood(), best.getSampleTwoSingleLikelihood(), best.getDoubletLikelihood(), -1.0d, best.getImpossibleAllelesSampleOne(),
125 best.getImpossibleAllelesSampleTwo(), best.getNumInformativeSNPs(), best.getNumSNPs(), best.getNumUMIs(), best.getNumInformativeUMIs(),
126 best.getNumInformativeHomozygousUMIsSampleOne(), best.getNumInformativeHomozygousUMIsSampleTwo());
127 this.best=result;
128
129 }
130
131
132
133 public String getCell() {
134 return cell;
135 }
136
137 public SamplePairAssignmentForCell getBestAssignment () {
138 // if data has been added, recalculate best.
139 if (this.dataAdded) calculateBest();
140 return this.best;
141 }
142
143 public double getBestPairPvalue () {
144 // if data has been added, recalculate best.
145 if (this.dataAdded) calculateBest();
146 return this.bestPairPvalue;
147 }
148
149 public List<SamplePairAssignmentForCell> getOtherAssignments() {
150 List<SamplePairAssignmentForCell> result = new ArrayList<>(this.all);
151 result.remove(this.best);
152 return result;
153 }
154
155 /**
156 * Returns the results for a pair of donors. If one of the donors given isn't in this data set, returns null.
157 * @param donorOne
158 * @param donorTwo
159 * @return
160 */
161 public SamplePairAssignmentForCell getAssignmentForDonorPair(final String donorOne, final String donorTwo) {
162 for (SamplePairAssignmentForCell o: this.all)
163 if (o.getSampleOne().equals(donorOne) && o.getSampleTwo().equals(donorTwo))
164 return o;
165 return null;
166 }
167
168
169
170
171 }