Debian Codegraph @

uk/ac/babraham/FastQC/Modules/PerSequenceGCContent.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.LineGraph;
28 import uk.ac.babraham.FastQC.Modules.GCModel.GCModel;
29 import uk.ac.babraham.FastQC.Modules.GCModel.GCModelValue;
30 import uk.ac.babraham.FastQC.Report.HTMLReportArchive;
31 import uk.ac.babraham.FastQC.Sequence.Sequence;
32 import uk.ac.babraham.FastQC.Statistics.NormalDistribution;
33
34 public class PerSequenceGCContent extends AbstractQCModule {
35
36 private double [] gcDistribution = new double[101];
37 private double [] theoreticalDistribution = new double[101];
38 private int [] xCategories = new int[0];
39 private double max = 0;
40 private double deviationPercent;
41 private boolean calculated = false;
42
43 private GCModel [] cachedModels = new GCModel [200];
44
45 public JPanel getResultsPanel() {
46
47 if (!calculated) calculateDistribution();
48
49 return new LineGraph(new double [][] {gcDistribution,theoreticalDistribution}, 0d, max, "Mean GC content (%)", new String [] {"GC count per read","Theoretical Distribution"}, xCategories, "GC distribution over all sequences");
50 }
51
52 public boolean ignoreFilteredSequences() {
53 return true;
54 }
55
56 public boolean ignoreInReport () {
57 if (ModuleConfig.getParam("gc_sequence", "ignore") > 0) {
58 return true;
59 }
60 return false;
61 }
62
63
64 private synchronized void calculateDistribution () {
65 max = 0;
66 xCategories = new int[gcDistribution.length];
67 double totalCount = 0;
68
69
70 // We use the mode to calculate the theoretical distribution
71 // so that we cope better with skewed distributions.
72 int firstMode = 0;
73 double modeCount = 0;
74
75 for (int i=0;i<gcDistribution.length;i++) {
76 xCategories[i] = i;
77 totalCount += gcDistribution[i];
78
79 if (gcDistribution[i] > modeCount) {
80 modeCount = gcDistribution[i];
81 firstMode = i;
82 }
83 if (gcDistribution[i] > max) max = gcDistribution[i];
84 }
85
86 // The mode might not be a very good measure of the centre
87 // of the distribution either due to duplicated vales or
88 // several very similar values next to each other. We therefore
89 // average over adjacent points which stay above 95% of the modal
90 // value
91
92 double mode = 0;
93 int modeDuplicates = 0;
94
95 boolean fellOffTop = true;
96
97 for (int i=firstMode;i<gcDistribution.length;i++) {
98 if (gcDistribution[i] > gcDistribution[firstMode] - (gcDistribution[firstMode]/10)) {
99 mode += i;
100 modeDuplicates++;
101 }
102 else {
103 fellOffTop = false;
104 break;
105 }
106 }
107
108 boolean fellOffBottom = true;
109
110 for (int i=firstMode-1;i>=0;i--) {
111 if (gcDistribution[i] > gcDistribution[firstMode] - (gcDistribution[firstMode]/10)) {
112 mode += i;
113 modeDuplicates++;
114 }
115 else {
116 fellOffBottom = false;
117 break;
118 }
119 }
120
121 if (fellOffBottom || fellOffTop) {
122 // If the distribution is so skewed that 95% of the mode
123 // is off the 0-100% scale then we keep the mode as the
124 // centre of the model
125 mode = firstMode;
126 }
127 else {
128 mode /= modeDuplicates;
129 }
130
131
132
133 // We can now work out a theoretical distribution
134 double stdev = 0;
135
136 for (int i=0;i<gcDistribution.length;i++) {
137 stdev += Math.pow((i-mode),2) * gcDistribution[i];
138 }
139
140 stdev /= totalCount-1;
141
142 stdev = Math.sqrt(stdev);
143
144 NormalDistribution nd = new NormalDistribution(mode, stdev);
145
146 deviationPercent = 0;
147
148 for (int i=0;i<theoreticalDistribution.length;i++) {
149 double probability = nd.getZScoreForValue(i);
150 theoreticalDistribution[i] = probability*totalCount;
151
152 if (theoreticalDistribution[i] > max) {
153 max = theoreticalDistribution[i];
154 }
155
156 deviationPercent += Math.abs(theoreticalDistribution[i]-gcDistribution[i]);
157 }
158
159 deviationPercent /= totalCount;
160 deviationPercent *= 100;
161
162 // System.out.println("Percentage deviation from normality is "+deviationPercent);
163
164
165 calculated = true;
166 }
167
168 public void processSequence(Sequence sequence) {
169
170 // Because we keep a model around for every possible sequence length we
171 // encounter we need to reduce the number of models. We can do this by
172 // rounding off the sequence once we get above a certain size
173
174 char [] seq = truncateSequence(sequence);
175
176 if (seq.length == 0) return; // Ignore empty sequences
177
178
179 int thisSeqGCCount = 0;
180 for (int i=0;i<seq.length;i++) {
181 if (seq[i] == 'G' || seq[i] == 'C') {
182 ++thisSeqGCCount;
183 }
184 }
185
186 if (seq.length >= cachedModels.length) {
187 // We need to extend the length of cached models
188
189 GCModel [] longerModels = new GCModel[seq.length+1];
190 for (int i=0;i<cachedModels.length;i++) {
191 longerModels[i] = cachedModels[i];
192 }
193
194 cachedModels = longerModels;
195 }
196
197 if (cachedModels[seq.length] == null) {
198 cachedModels[seq.length] = new GCModel(seq.length);
199 }
200
201 GCModelValue [] values = cachedModels[seq.length].getModelValues(thisSeqGCCount);
202
203 for (int i=0;i<values.length;i++) {
204 gcDistribution[values[i].percentage()] += values[i].increment();
205 }
206
207 }
208
209 private char [] truncateSequence (Sequence sequence) {
210
211 String seq = sequence.getSequence();
212
213 // TODO: We should return a random chunk of sequence, rather
214 // than the start.
215
216 if (seq.length() > 1000) {
217 int length = (seq.length()/1000)*1000;
218 return seq.substring(0, length).toCharArray();
219 }
220 if (seq.length() > 100) {
221 int length = (seq.length()/100)*100;
222 return seq.substring(0, length).toCharArray();
223 }
224
225 return seq.toCharArray();
226
227 }
228
229 public void reset () {
230 gcDistribution = new double[101];
231 }
232
233 public String description() {
234 return "Shows the distribution of GC contents for whole sequences";
235 }
236
237 public String name() {
238 return "Per sequence GC content";
239 }
240
241 public boolean raisesError() {
242 if (!calculated) calculateDistribution();
243
244 return deviationPercent > ModuleConfig.getParam("gc_sequence", "error");
245 }
246
247 public boolean raisesWarning() {
248 if (!calculated) calculateDistribution();
249
250 return deviationPercent > ModuleConfig.getParam("gc_sequence", "warn");
251 }
252
253 public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException {
254
255 writeDefaultImage(report, "per_sequence_gc_content.png", "Per sequence GC content graph", 800, 600);
256
257 StringBuffer sb = report.dataDocument();
258 sb.append("#GC Content\tCount\n");
259 for (int i=0;i<xCategories.length;i++) {
260 sb.append(xCategories[i]);
261 sb.append("\t");
262 sb.append(gcDistribution[i]);
263 sb.append("\n");
264 }
265 }
266 }