Debian Codegraph @

uk/ac/babraham/FastQC/Modules/PerBaseQualityScores.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
24 import javax.swing.JPanel;
25 import javax.xml.stream.XMLStreamException;
26
27 import uk.ac.babraham.FastQC.Graphs.BaseGroup;
28 import uk.ac.babraham.FastQC.Graphs.QualityBoxPlot;
29 import uk.ac.babraham.FastQC.Report.HTMLReportArchive;
30 import uk.ac.babraham.FastQC.Sequence.Sequence;
31 import uk.ac.babraham.FastQC.Sequence.QualityEncoding.PhredEncoding;
32 import uk.ac.babraham.FastQC.Utilities.QualityCount;
33
34 public class PerBaseQualityScores extends AbstractQCModule {
35
36 public QualityCount [] qualityCounts = new QualityCount[0];
37 double [] means = null;
38 double [] medians = null;
39 double [] lowerQuartile = null;
40 double [] upperQuartile = null;
41 double [] lowest = null;
42 double [] highest = null;
43 String [] xLabels;
44 int low = 0;
45 int high = 0;
46 PhredEncoding encodingScheme;
47 private boolean calculated = false;
48
49 public JPanel getResultsPanel() {
50
51 if (!calculated) getPercentages();
52
53 return new QualityBoxPlot(means,medians,lowest,highest,lowerQuartile,upperQuartile, low, high, 2d, xLabels, "Quality scores across all bases ("+encodingScheme+" encoding)");
54 }
55
56 public boolean ignoreFilteredSequences() {
57 return true;
58 }
59
60 public boolean ignoreInReport () {
61 // We don't show this if there is no quality data.
62 if (ModuleConfig.getParam("quality_base", "ignore") > 0 || qualityCounts.length == 0) {
63 return true;
64 }
65 return false;
66 }
67
68 private synchronized void getPercentages () {
69
70 char [] range = calculateOffsets();
71 encodingScheme = PhredEncoding.getFastQEncodingOffset(range[0]);
72 low = 0;
73 high = range[1] - encodingScheme.offset();
74 if (high < 35) {
75 high = 35;
76 }
77
78 BaseGroup [] groups = BaseGroup.makeBaseGroups(qualityCounts.length);
79
80 means = new double[groups.length];
81 medians = new double[groups.length];
82 lowest = new double[groups.length];
83 highest = new double[groups.length];
84 lowerQuartile = new double[groups.length];
85 upperQuartile = new double[groups.length];
86 xLabels = new String[groups.length];
87
88 for (int i=0;i<groups.length;i++) {
89 xLabels[i] = groups[i].toString();
90 int minBase = groups[i].lowerCount();
91 int maxBase = groups[i].upperCount();
92 lowest[i] = getPercentile(minBase, maxBase, encodingScheme.offset(), 10);
93 highest[i] = getPercentile(minBase, maxBase, encodingScheme.offset(), 90);
94 means[i] = getMean(minBase,maxBase,encodingScheme.offset());
95 medians[i] = getPercentile(minBase, maxBase, encodingScheme.offset(), 50);
96 lowerQuartile[i] = getPercentile(minBase, maxBase, encodingScheme.offset(), 25);
97 upperQuartile[i] = getPercentile(minBase, maxBase, encodingScheme.offset(), 75);
98 }
99
100 calculated = true;
101
102 }
103
104 private char [] calculateOffsets () {
105 // Works out from the set of chars what is the most
106 // likely encoding scale for this file.
107
108 char minChar = 0;
109 char maxChar = 0;
110
111 for (int q=0;q<qualityCounts.length;q++) {
112 if (q == 0) {
113 minChar = qualityCounts[q].getMinChar();
114 maxChar = qualityCounts[q].getMaxChar();
115 }
116 else {
117 if (qualityCounts[q].getMinChar() < minChar) {
118 minChar = qualityCounts[q].getMinChar();
119 }
120 if (qualityCounts[q].getMaxChar() > maxChar) {
121 maxChar = qualityCounts[q].getMaxChar();
122 }
123 }
124 }
125
126 return new char[] {minChar,maxChar};
127 }
128
129 public void processSequence(Sequence sequence) {
130
131 calculated = false;
132 char [] qual = sequence.getQualityString().toCharArray();
133 if (qualityCounts.length < qual.length) {
134
135 QualityCount [] qualityCountsNew = new QualityCount[qual.length];
136
137 for (int i=0;i<qualityCounts.length;i++) {
138 qualityCountsNew[i] = qualityCounts[i];
139 }
140 for (int i=qualityCounts.length;i<qualityCountsNew.length;i++) {
141 qualityCountsNew[i] = new QualityCount();
142 }
143 qualityCounts = qualityCountsNew;
144
145 }
146
147 for (int i=0;i<qual.length;i++) {
148 qualityCounts[i].addValue(qual[i]);
149 }
150
151 }
152
153 public void reset () {
154 qualityCounts = new QualityCount[0];
155 }
156
157 public String description() {
158 return "Shows the Quality scores of all bases at a given position in a sequencing run";
159 }
160
161 public String name() {
162 return "Per base sequence quality";
163 }
164
165 public boolean raisesError() {
166 if (!calculated) getPercentages();
167
168 for (int i=0;i<lowerQuartile.length;i++) {
169 if (Double.isNaN(lowerQuartile[i])) {
170 // There wasn't enough data for this group to make an assessment
171 continue;
172 }
173 if (lowerQuartile[i] < ModuleConfig.getParam("quality_base_lower", "error") || medians[i] < ModuleConfig.getParam("quality_base_median", "error")) {
174 return true;
175 }
176 }
177 return false;
178 }
179
180 public boolean raisesWarning() {
181 if (!calculated) getPercentages();
182
183 for (int i=0;i<lowerQuartile.length;i++) {
184 if (Double.isNaN(lowerQuartile[i])) {
185 // There wasn't enough data for this group to make an assessment
186 continue;
187 }
188 if (lowerQuartile[i] < ModuleConfig.getParam("quality_base_lower", "warn") || medians[i] < ModuleConfig.getParam("quality_base_median", "warn")) {
189 return true;
190 }
191 }
192 return false;
193 }
194
195 public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException
196 {
197 if (!calculated) getPercentages();
198
199 writeDefaultImage(report, "per_base_quality.png", "Per base quality graph", Math.max(800, means.length*15), 600);
200
201 StringBuffer sb = report.dataDocument();
202 sb.append("#Base\tMean\tMedian\tLower Quartile\tUpper Quartile\t10th Percentile\t90th Percentile\n");
203 for (int i=0;i<means.length;i++) {
204 sb.append(xLabels[i]);
205 sb.append("\t");
206
207 sb.append(means[i]);
208 sb.append("\t");
209
210 sb.append(medians[i]);
211 sb.append("\t");
212
213 sb.append(lowerQuartile[i]);
214 sb.append("\t");
215
216 sb.append(upperQuartile[i]);
217 sb.append("\t");
218
219 sb.append(lowest[i]);
220 sb.append("\t");
221
222 sb.append(highest[i]);
223
224 sb.append("\n");
225 }
226 }
227
228 private double getPercentile (int minbp, int maxbp, int offset, int percentile) {
229 int count = 0;
230 double total = 0;
231
232 for (int i=minbp-1;i<maxbp;i++) {
233 if (qualityCounts[i].getTotalCount() > 100) {
234 count++;
235 total += qualityCounts[i].getPercentile(offset, percentile);
236 }
237 }
238
239 if (count > 0) {
240 return total/count;
241 }
242 return Double.NaN;
243
244 }
245
246 private double getMean (int minbp, int maxbp, int offset) {
247 int count = 0;
248 double total = 0;
249
250 for (int i=minbp-1;i<maxbp;i++) {
251 if (qualityCounts[i].getTotalCount() > 0) {
252 count++;
253 total += qualityCounts[i].getMean(offset);
254 }
255 }
256
257 if (count > 0) {
258 return total/count;
259 }
260 return 0;
261
262 }
263
264 }