Debian Codegraph @

src/java/org/broadinstitute/dropseqrna/barnyard/digitalallelecounts/sampleassignment/CellSampleLikelihoodCollection.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;
25
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.List;
30 import java.util.Set;
31 import java.util.stream.Collectors;
32 import java.util.stream.IntStream;
33
34 import org.apache.commons.math3.stat.descriptive.rank.Median;
35 import org.broadinstitute.dropseqrna.barnyard.digitalallelecounts.LikelihoodUtils;
36
37 import com.google.common.collect.ImmutableMap;
38 import com.google.common.collect.ListMultimap;
39 import com.google.common.collect.MultimapBuilder;
40
41 /**
42 * For each cell, collect the likelihoods that the cell is taken from a particular sample
43 * This collects the likelihoods across all samples, and stores the total likelihood across all SNPs
44 * @author nemesh
45 *
46 */
47 public class CellSampleLikelihoodCollection {
48
49 private final String cellBarcode;
50 private int numUMIs;
51 private int numSNPs;
52
53 private final ImmutableMap<String, Integer> sampleIndexMap;
54 private final double [] likelihoods;
55 private double globalPenaltyScore;
56
57 CellSampleLikelihoodCollection(final String cellBarcode, final int numSNPs, final int numUMIs, final double globalPenaltyScore, ImmutableMap<String, Integer> sampleIndexMap) {
58 this.numSNPs=numSNPs;
59 this.numUMIs=numUMIs;
60 this.cellBarcode=cellBarcode;
61 this.sampleIndexMap=sampleIndexMap;
62 this.likelihoods = new double [sampleIndexMap.size()];
63 // explicitly set values to 0.
64 Arrays.fill(this.likelihoods, 0);
65 this.globalPenaltyScore=globalPenaltyScore;
66 }
67
68 void add (final CellSampleLikelihoodCollection other) {
69 if (!other.getCellBarcode().equals(this.cellBarcode))
70 throw new IllegalArgumentException("Adding a collection in with the wrong cell barcode. Original ["+this.cellBarcode+"] new ["+ other.getCellBarcode()+"]");
71 this.numSNPs+=other.getNumSNPs();
72 this.numUMIs+=other.getNumUMIs();
73 for (String sample: this.sampleIndexMap.keySet())
74 addLikelihood(sample, other.getLoglikelihood(sample));
75 this.globalPenaltyScore+=other.getGlobalPenaltyScore();
76
77 }
78
79 void addLikelihood(final String sample, final double logLikelihood) {
80 int index = this.sampleIndexMap.get(sample);
81 this.likelihoods[index]+=logLikelihood;
82 }
83
84 /**
85 * Increase the global likelihood penalty score by this log likelihood.
86 * @param logLikelihood
87 */
88 void addGlobalPenalty(final double logLikelihood) {
89 this.globalPenaltyScore+=logLikelihood;
90 }
91
92 /**
93 * Get the median likelihood across all donors.
94 * @return
95 */
96 public double getMedianLikelihood () {
97 Median median = new Median();
98 return median.evaluate(likelihoods);
99 }
100
101 public String getCellBarcode () {
102 return this.cellBarcode;
103 }
104
105 public void incrementNumSNPs (final int num) {
106 this.numSNPs+=num;
107 }
108
109 public void incrementNumUMIs (final int num) {
110 this.numUMIs+=num;
111 }
112
113 public int getNumUMIs() {
114 return numUMIs;
115 }
116
117 public int getNumSNPs() {
118 return numSNPs;
119 }
120
121
122 /**
123 * Returns the log likelihood gathered for this sample, or null if this sample isn't in the data set.
124 * @param sample
125 * @return
126 */
127 public Double getLoglikelihood (final String sample) {
128 Integer index=this.sampleIndexMap.get(sample);
129 if (index==null) return null;
130 return this.likelihoods[index];
131 }
132
133 public boolean isSampleInCollection (final String sample) {
134 return this.sampleIndexMap.containsKey(sample);
135 }
136
137 public Set<String> getSamples () {
138 return this.sampleIndexMap.keySet();
139 }
140
141
142 /**
143 * Based on all data added, computes the best log likelihood and 2nd best likelihood sample assignments.
144 * @return An object containing the best sample, 1st and 2nd best log likelihoods.
145 */
146 //
147 public BestSampleAssignmentForCell getBestSampleAssignment () {
148 // track best/second best likelihoods.
149 Double bestLogLike = Double.NEGATIVE_INFINITY;
150 Double secondBestLogLike=Double.NEGATIVE_INFINITY;
151 String bestSample=null;
152
153 for (String sample: sampleIndexMap.keySet()) {
154 double val = this.getLoglikelihood(sample);
155 if (val>bestLogLike) {
156 // make the best the 2nd best.
157 secondBestLogLike=bestLogLike;
158 bestSample=sample;
159 bestLogLike=val;
160 } else {
161 if (val>secondBestLogLike) {
162 secondBestLogLike=val;
163 }
164 }
165 }
166
167 double pval = LikelihoodUtils.getInstance().getOneMinusPvalueFromLog10Likelihood(this.likelihoods);
168 BestSampleAssignmentForCell best = new BestSampleAssignmentForCell(this.cellBarcode, bestSample, bestLogLike, secondBestLogLike, pval);
169 return best;
170 }
171
172 public List<String> getDonorsRankedByAssignmentLikelihood () {
173 String[] sampleNames = sampleIndexMap.keySet().stream().toArray(String []::new);
174
175 List<String> sortedNames = IntStream.range(0, likelihoods.length)
176 .mapToObj(i -> i)
177 .sorted(Comparator.comparing(i -> likelihoods[i], Comparator.reverseOrder()))
178 .map(i -> sampleNames[i])
179 .collect(Collectors.toList());
180
181 return sortedNames;
182
183 /*
184 ListMultimap<Double, String> treeListMultimap = MultimapBuilder.treeKeys().arrayListValues().build();
185 for (String donor: sampleIndexMap.keySet()) {
186 int index = sampleIndexMap.get(donor);
187 double value = this.likelihoods[index];
188 treeListMultimap.put(value, donor);
189 }
190 */
191 // map sorted by key already? Which direction?
192
193
194
195
196
197 }
198
199 public double getGlobalPenaltyScore() {
200 return globalPenaltyScore;
201 }
202
203 }