Debian Codegraph @

uk/ac/babraham/FastQC/Modules/DuplicationLevel.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2010-17 Simon Andrews
3 *
4 * This file is part of FastQC.
5 *
6 * FastQC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FastQC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FastQC; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 package uk.ac.babraham.FastQC.Modules;
21
22 import java.io.IOException;
23 import java.text.DecimalFormat;
24 import java.util.HashMap;
25 import java.util.Iterator;
26
27 import javax.swing.JPanel;
28 import javax.xml.stream.XMLStreamException;
29
30 import uk.ac.babraham.FastQC.Graphs.LineGraph;
31 import uk.ac.babraham.FastQC.Report.HTMLReportArchive;
32 import uk.ac.babraham.FastQC.Sequence.Sequence;
33
34 public class DuplicationLevel extends AbstractQCModule {
35
36 private OverRepresentedSeqs overrepresentedModule;
37 private double [] totalPercentages = null;
38 private double maxCount = 100;
39 private double percentDifferentSeqs = 0;
40 private String [] labels;
41 private static final DecimalFormat df = new DecimalFormat("#.##");
42
43 protected DuplicationLevel (OverRepresentedSeqs overrepresentedModule) {
44 this.overrepresentedModule = overrepresentedModule;
45 }
46
47 public String description() {
48 return "Plots the number of sequences which are duplicated to different levels";
49 }
50
51 public boolean ignoreFilteredSequences() {
52 if (ModuleConfig.getParam("duplication", "ignore") > 0) {
53 return true;
54 }
55 return false;
56 }
57
58 public boolean ignoreInReport () {
59 if (ModuleConfig.getParam("duplication", "ignore") > 0) {
60 return true;
61 }
62 return false;
63 }
64
65 protected synchronized void calculateLevels () {
66
67 if (totalPercentages != null) return;
68
69 totalPercentages = new double[16];
70
71 HashMap<Integer, Integer> collatedCounts = new HashMap<Integer, Integer>();
72
73 Iterator<String> it = overrepresentedModule.sequences.keySet().iterator();
74
75 while (it.hasNext()) {
76 int thisCount = overrepresentedModule.sequences.get(it.next());
77
78 if (collatedCounts.containsKey(thisCount)) {
79 collatedCounts.put(thisCount,collatedCounts.get(thisCount)+1);
80 }
81 else {
82 collatedCounts.put(thisCount,1);
83 }
84 }
85
86 // Now we can correct each of these
87
88 HashMap<Integer, Double> correctedCounts = new HashMap<Integer, Double>();
89
90 Iterator<Integer> itr = collatedCounts.keySet().iterator();
91
92 while (itr.hasNext()) {
93 int dupLevel = itr.next();
94 int count = collatedCounts.get(dupLevel);
95
96 correctedCounts.put(dupLevel,getCorrectedCount(overrepresentedModule.countAtUniqueLimit, overrepresentedModule.count, dupLevel, count));
97
98 }
99
100 // From the corrected counts we can now work out the raw and deduplicated proportions
101
102 double dedupTotal = 0;
103 double rawTotal = 0;
104
105 Iterator<Integer> itc = correctedCounts.keySet().iterator();
106
107 while (itc.hasNext()) {
108 int dupLevel = itc.next();
109 double count = correctedCounts.get(dupLevel);
110
111 dedupTotal += count;
112 rawTotal += count * dupLevel;
113
114 int dupSlot = dupLevel - 1;
115
116 // The dupSlot < 0 is a kludge to fix a problem we see if we have
117 // a duplication level > 2^31. It it gets bigger than 2^64 then
118 // we're really stuffed but I think this will work for all practical
119 // purposes and this is a really corner case anyway.
120 if (dupSlot > 9999 || dupSlot < 0) dupSlot = 15;
121 else if (dupSlot > 4999) dupSlot = 14;
122 else if (dupSlot > 999) dupSlot = 13;
123 else if (dupSlot > 499) dupSlot = 12;
124 else if (dupSlot > 99) dupSlot = 11;
125 else if (dupSlot > 49) dupSlot = 10;
126 else if (dupSlot > 9) dupSlot = 9;
127
128
129 totalPercentages[dupSlot] += count * dupLevel;
130
131 }
132
133 // System.err.println("True total = "+overrepresentedModule.count+" inferred total is "+rawTotal+" dedup total is "+dedupTotal);
134
135 labels = new String [16];
136 for (int i=0;i<totalPercentages.length;i++) {
137 if (i<9) labels[i] = ""+(i+1);
138 else if (i==9) labels[i]=">10";
139 else if (i==10) labels[i]=">50";
140 else if (i==11) labels[i]=">100";
141 else if (i==12) labels[i]=">500";
142 else if (i==13) labels[i]=">1k";
143 else if (i==14) labels[i]=">5k";
144 else if (i==15) labels[i]=">10k";
145
146
147 totalPercentages[i] /= rawTotal;
148 totalPercentages[i] *= 100;
149 }
150
151
152 percentDifferentSeqs = (dedupTotal/rawTotal)*100;
153 if (rawTotal == 0) percentDifferentSeqs = 100;
154
155 }
156
157 private static double getCorrectedCount (long countAtLimit, long totalCount, int duplicationLevel, int numberOfObservations) {
158
159 // System.err.println("Count at limit = "+countAtLimit+" total = "+totalCount+" Dup level = "+duplicationLevel+" no obs = "+numberOfObservations);
160
161 // See if we can bail out early
162 if (countAtLimit == totalCount) return numberOfObservations;
163
164 // If there aren't enough sequences left to hide another sequence with this count then
165 // we can also skip the calculation
166 if (totalCount - numberOfObservations < countAtLimit) return numberOfObservations;
167
168 // If not then we need to see what the likelihood is that we had another sequence
169 // with this number of observations which we would have missed.
170
171 // We'll start by working out the probability of NOT seeing a sequence with this duplication level
172 // within the first countAtLimit sequences of numberOfObservations. This is easier than calculating
173 // the probability of seeing it.
174
175 double pNotSeeingAtLimit = 1;
176
177
178 // To save doing long calculations which are never going to produce anything meaningful
179 // we'll set a limit to our p-value calculation. This is the probability below which we
180 // won't increase our count by 0.01 of an observation. Once we're below this we stop caring
181 // about the corrected value since it's going to be so close to the observed value that
182 // we can just return that instead.
183 double limitOfCaring = 1d - (numberOfObservations/(numberOfObservations+0.01d));
184
185 for (int i=0;i<countAtLimit;i++) {
186 pNotSeeingAtLimit *= ((totalCount-i)-duplicationLevel)/(double)(totalCount-i);
187
188 if (pNotSeeingAtLimit < limitOfCaring) {
189 pNotSeeingAtLimit = 0;
190 break;
191 }
192
193 }
194
195 // Now we can invert this to get the chance of seeing a sequence with this count
196 double pSeeingAtLimit = 1 - pNotSeeingAtLimit;
197
198 // Now we can assume that the number we observed can be scaled up by this proportion
199 double trueCount = numberOfObservations/pSeeingAtLimit;
200
201 return trueCount;
202
203 }
204
205
206 public JPanel getResultsPanel() {
207 if (totalPercentages == null) calculateLevels();
208
209 return new LineGraph(new double [][] {totalPercentages}, 0d, maxCount, "Sequence Duplication Level",new String [] {"% Total sequences"}, labels, "Percent of seqs remaining if deduplicated "+df.format(percentDifferentSeqs)+"%");
210 }
211
212 public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException {
213 if (totalPercentages == null) calculateLevels();
214
215 writeDefaultImage(report, "duplication_levels.png", "Duplication level graph", 800, 600);
216
217 StringBuffer sb = report.dataDocument();
218
219 sb.append("#Total Deduplicated Percentage\t");
220 sb.append(percentDifferentSeqs);
221 sb.append("\n");
222
223 sb.append("#Duplication Level\tPercentage of total\n");
224 for (int i=0;i<labels.length;i++) {
225 sb.append(labels[i]);
226 if (i == labels.length-1) {
227 sb.append("+");
228 }
229 sb.append("\t");
230 sb.append(totalPercentages[i]);
231 sb.append("\n");
232 }
233
234 }
235
236 public String name() {
237 return "Sequence Duplication Levels";
238 }
239
240 public void processSequence(Sequence sequence) {
241 // We don't need to do anything since we use
242 // the data structure from the overrepresented sequences
243 // module.
244 }
245
246 public boolean raisesError() {
247 if (totalPercentages == null) calculateLevels();
248
249 // Anything over 50% duplicate gets us a error
250 if (percentDifferentSeqs < ModuleConfig.getParam("duplication", "error")) {
251 return true;
252 }
253
254 return false;
255 }
256
257 public boolean raisesWarning() {
258 if (totalPercentages == null) calculateLevels();
259
260 // Anything over 20% duplicate gets us a warning
261 if (percentDifferentSeqs < ModuleConfig.getParam("duplication", "warn")) {
262 return true;
263 }
264
265 return false;
266 }
267
268 public void reset() {
269 totalPercentages = null;
270 }
271
272 }