Debian Codegraph @

uk/ac/babraham/FastQC/Modules/PerTileQualityScores.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.util.Arrays;
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.BaseGroup;
31 import uk.ac.babraham.FastQC.Graphs.TileGraph;
32 import uk.ac.babraham.FastQC.Report.HTMLReportArchive;
33 import uk.ac.babraham.FastQC.Sequence.Sequence;
34 import uk.ac.babraham.FastQC.Sequence.QualityEncoding.PhredEncoding;
35 import uk.ac.babraham.FastQC.Utilities.QualityCount;
36
37 public class PerTileQualityScores extends AbstractQCModule {
38
39
40 public HashMap<Integer, QualityCount []> perTileQualityCounts = new HashMap<Integer, QualityCount[]>();
41 private int currentLength = 0;
42 private double [][] means = null;
43 private String [] xLabels;
44 private int [] tiles;
45 private int high = 0;
46 PhredEncoding encodingScheme;
47 private boolean calculated = false;
48
49 private long totalCount = 0;
50
51 private int splitPosition = -1;
52
53 private double maxDeviation = 0;
54
55 private boolean ignoreInReport = false;
56
57 public JPanel getResultsPanel() {
58
59 if (!calculated) getPercentages();
60
61 return new TileGraph(xLabels, tiles, means);
62
63 }
64
65 public boolean ignoreFilteredSequences() {
66 return true;
67 }
68
69 public boolean ignoreInReport () {
70 if (ignoreInReport || ModuleConfig.getParam("tile", "ignore") > 0 || currentLength == 0) {
71 return true;
72 }
73 return false;
74 }
75
76 private synchronized void getPercentages () {
77
78 char [] range = calculateOffsets();
79 encodingScheme = PhredEncoding.getFastQEncodingOffset(range[0]);
80 high = range[1] - encodingScheme.offset();
81 if (high < 35) {
82 high = 35;
83 }
84
85 BaseGroup [] groups = BaseGroup.makeBaseGroups(currentLength);
86
87 Integer [] tileNumbers = perTileQualityCounts.keySet().toArray(new Integer[0]);
88
89 Arrays.sort(tileNumbers);
90
91 tiles = new int[tileNumbers.length];
92 for (int i=0;i<tiles.length;i++) {
93 tiles[i] = tileNumbers[i];
94 }
95
96 means = new double[tileNumbers.length][groups.length];
97 xLabels = new String[groups.length];
98
99 for (int t=0;t<tileNumbers.length;t++){
100 for (int i=0;i<groups.length;i++) {
101 if (t==0)
102 xLabels[i] = groups[i].toString();
103
104 int minBase = groups[i].lowerCount();
105 int maxBase = groups[i].upperCount();
106 means[t][i] = getMean(tileNumbers[t],minBase,maxBase,encodingScheme.offset());
107 }
108 }
109
110 // Now we normalise across each column to see if there are any tiles with unusually
111 // high or low quality.
112
113 double maxDeviation = 0;
114
115 double [] averageQualitiesPerGroup = new double[groups.length];
116
117 for (int t=0;t<tileNumbers.length;t++) {
118 for (int i=0;i<groups.length;i++) {
119 averageQualitiesPerGroup[i] += means[t][i];
120 }
121 }
122
123 for (int i=0;i<averageQualitiesPerGroup.length;i++) {
124 averageQualitiesPerGroup[i] /= tileNumbers.length;
125 }
126
127 for (int i=0;i<groups.length;i++) {
128 for (int t=0;t<tileNumbers.length;t++) {
129 means[t][i] -= averageQualitiesPerGroup[i];
130 if (Math.abs(means[t][i])> maxDeviation) {
131 maxDeviation = Math.abs(means[t][i]);
132 }
133 }
134 }
135
136 this.maxDeviation = maxDeviation;
137
138 calculated = true;
139
140 }
141
142 private char [] calculateOffsets () {
143 // Works out from the set of chars what is the most
144 // likely encoding scale for this file.
145
146 char minChar = 0;
147 char maxChar = 0;
148
149 // Iterate through the tiles to check them all in case
150 // we're dealing with unrepresentative data in the first one.
151 Iterator<QualityCount[]> qit = perTileQualityCounts.values().iterator();
152
153 while (qit.hasNext()) {
154
155 QualityCount [] qualityCounts = qit.next();
156
157 for (int q=0;q<qualityCounts.length;q++) {
158 if (minChar == 0) {
159 minChar = qualityCounts[q].getMinChar();
160 maxChar = qualityCounts[q].getMaxChar();
161 }
162 else {
163 if (qualityCounts[q].getMinChar() < minChar) {
164 minChar = qualityCounts[q].getMinChar();
165 }
166 if (qualityCounts[q].getMaxChar() > maxChar) {
167 maxChar = qualityCounts[q].getMaxChar();
168 }
169 }
170 }
171 }
172
173 return new char[] {minChar,maxChar};
174 }
175
176 public void processSequence(Sequence sequence) {
177
178 // Check if we can skip counting because the module is being ignored anyway
179 if (totalCount == 0) {
180 if (ModuleConfig.getParam("tile", "ignore") > 0) {
181 ignoreInReport = true;
182 }
183 }
184
185
186 // Don't waste time calculating this if we're not going to use it anyway
187 if (ignoreInReport) return;
188
189 // Don't bother with sequences with zero length as they don't have any
190 // quality information anyway.
191 if (sequence.getQualityString().length() == 0) {
192 return;
193 }
194
195 calculated = false;
196
197 // Try to find the tile id. This can come in one of two forms:
198 // @HWI-1KL136:211:D1LGAACXX:1:1101:18518:48851 3:N:0:ATGTCA
199 // ^
200 // @HWUSI-EAS493_0001:2:1:1000:16900#0/1
201 // ^
202
203 // These would appear at sections 2 or 3 of an array split on :
204
205 // This module does quite a lot of work and ends up being the limiting
206 // step when calculating. We'll therefore take only a sample of the
207 // sequences to try to get a representative selection. We'll use the
208 // first 10k sequences in case we're dealing with a very small file
209 // and then take 10% of the rest.
210
211 ++totalCount;
212 if (totalCount > 10000 && totalCount % 10 != 0) return;
213
214 // First try to split the id by :
215 int tile = 0;
216
217 String [] splitID = sequence.getID().split(":");
218
219
220 // If there are 7 or more fields then it's a 1.8+ file
221 try {
222
223
224 if (splitPosition >=0) {
225 // We've found a split position before so let's try to use it again
226
227
228 if (splitID.length <= splitPosition) {
229 // There isn't enough data in this header to split the way we did before
230 throw new NumberFormatException("Can't extract a number - not enough data");
231 }
232
233 tile = Integer.parseInt(splitID[splitPosition]);
234 }
235
236 else if (splitID.length>=7) {
237 splitPosition = 4;
238 tile = Integer.parseInt(splitID[4]);
239 }
240 else if (splitID.length >=5) {
241 splitPosition = 2;
242 // We can try the older format
243 tile = Integer.parseInt(splitID[2]);
244 }
245 else {
246 // We're not going to get a tile out of this
247 ignoreInReport = true;
248 return;
249 }
250
251
252 }
253 catch (NumberFormatException nfe) {
254 // This doesn't conform
255 ignoreInReport = true;
256 return;
257 }
258
259 char [] qual = sequence.getQualityString().toCharArray();
260 if (currentLength < qual.length) {
261
262 Iterator<Integer> tiles = perTileQualityCounts.keySet().iterator();
263 while (tiles.hasNext()) {
264 int thisTile = tiles.next();
265
266 QualityCount [] qualityCounts = perTileQualityCounts.get(thisTile);
267 QualityCount [] qualityCountsNew = new QualityCount[qual.length];
268
269 for (int i=0;i<qualityCounts.length;i++) {
270 qualityCountsNew[i] = qualityCounts[i];
271 }
272 for (int i=qualityCounts.length;i<qualityCountsNew.length;i++) {
273 qualityCountsNew[i] = new QualityCount();
274 }
275 perTileQualityCounts.put(thisTile, qualityCountsNew);
276 }
277
278 currentLength = qual.length;
279
280 }
281
282 if (! perTileQualityCounts.containsKey(tile)) {
283
284 if (perTileQualityCounts.size() > 2500) {
285 // There are too many tiles, so we're probably parsing this wrong.
286 // Let's give up
287 System.err.println("Too many tiles (>2500) so giving up trying to do per-tile qualities since we're probably parsing the file wrongly");
288 ignoreInReport = true;
289 perTileQualityCounts.clear();
290 return;
291 }
292
293 QualityCount [] qualityCounts = new QualityCount[currentLength];
294 for (int i=0;i<currentLength;i++) {
295 qualityCounts[i] = new QualityCount();
296 }
297
298 perTileQualityCounts.put(tile, qualityCounts);
299 }
300
301 QualityCount [] qualityCounts = perTileQualityCounts.get(tile);
302
303 for (int i=0;i<qual.length;i++) {
304 qualityCounts[i].addValue(qual[i]);
305 }
306
307 }
308
309 public void reset () {
310 totalCount = 0;
311 perTileQualityCounts = new HashMap<Integer, QualityCount[]>();
312 }
313
314 public String description() {
315 return "Shows the perl tile Quality scores of all bases at a given position in a sequencing run";
316 }
317
318 public String name() {
319 return "Per tile sequence quality";
320 }
321
322 public boolean raisesError() {
323 if (!calculated) getPercentages();
324
325 if (maxDeviation > ModuleConfig.getParam("tile", "error")) return true;
326 return false;
327 }
328
329 public boolean raisesWarning() {
330 if (!calculated) getPercentages();
331
332 if (maxDeviation > ModuleConfig.getParam("tile", "warn")) return true;
333 return false;
334 }
335
336 public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException {
337 if (!calculated) getPercentages();
338
339 writeDefaultImage(report, "per_tile_quality.png", "Per tile quality graph", Math.max(800, xLabels.length*15), 600);
340
341 StringBuffer sb = report.dataDocument();
342 sb.append("#Tile\tBase\tMean\n");
343
344 for (int t=0;t<tiles.length;t++) {
345 for (int i=0;i<means[t].length;i++) {
346
347 sb.append(tiles[t]);
348 sb.append("\t");
349
350 sb.append(xLabels[i]);
351 sb.append("\t");
352
353 sb.append(means[t][i]);
354
355 sb.append("\n");
356 }
357 }
358 }
359
360 private double getMean (int tile, int minbp, int maxbp, int offset) {
361 int count = 0;
362 double total = 0;
363
364 QualityCount [] qualityCounts = perTileQualityCounts.get(tile);
365
366 for (int i=minbp-1;i<maxbp;i++) {
367 if (qualityCounts[i].getTotalCount() > 0) {
368 count++;
369 total += qualityCounts[i].getMean(offset);
370 }
371 }
372
373 if (count > 0) {
374 return total/count;
375 }
376 return 0;
377
378 }
379
380
381 }