Debian Codegraph @

uk/ac/babraham/FastQC/Modules/SequenceLengthDistribution.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.FastQCConfig;
28 import uk.ac.babraham.FastQC.Graphs.LineGraph;
29 import uk.ac.babraham.FastQC.Report.HTMLReportArchive;
30 import uk.ac.babraham.FastQC.Sequence.Sequence;
31
32 public class SequenceLengthDistribution extends AbstractQCModule {
33
34 private long [] lengthCounts = new long[0];
35 private double [] graphCounts = null;
36 private String [] xCategories = new String[0];
37 private double max = 0;
38 private boolean calculated = false;
39
40 public JPanel getResultsPanel() {
41
42 if (!calculated) calculateDistribution();
43
44 return new LineGraph(new double [][] {graphCounts}, 0d, max, "Sequence Length (bp)",new String [] {"Sequence Length"}, xCategories, "Distribution of sequence lengths over all sequences");
45 }
46
47 public boolean ignoreFilteredSequences() {
48 return true;
49 }
50
51 public boolean ignoreInReport () {
52 if (ModuleConfig.getParam("sequence_length", "ignore") > 0) {
53 return true;
54 }
55 return false;
56 }
57
58 private synchronized void calculateDistribution () {
59 int maxLen = 0;
60 int minLen = -1;
61 max = 0;
62
63 // Find the min and max lengths
64 for (int i=0;i<lengthCounts.length;i++) {
65 if (lengthCounts[i]>0) {
66 if (minLen < 0) {
67 minLen = i;
68 }
69 maxLen = i;
70 }
71 }
72
73 // We can get a -1 value for min if there aren't any valid sequences
74 // at all.
75
76 if (minLen < 0) minLen = 0;
77
78 // We put one extra category either side of the actual size
79 if (minLen>0) minLen--;
80 maxLen++;
81
82 int [] startAndInterval = getSizeDistribution(minLen, maxLen);
83
84 // Work out how many categories we need
85 int categories = 0;
86 int currentValue = startAndInterval[0];
87 while (currentValue<= maxLen) {
88 ++categories;
89 currentValue+= startAndInterval[1];
90 }
91
92 graphCounts = new double[categories];
93 xCategories = new String[categories];
94
95 for (int i=0;i<graphCounts.length;i++) {
96
97 int minValue = startAndInterval[0]+(startAndInterval[1]*i);
98 int maxValue = (startAndInterval[0]+(startAndInterval[1]*(i+1)))-1;
99
100 if (maxValue > maxLen) {
101 maxValue = maxLen;
102 }
103
104 for (int bp=minValue;bp<=maxValue;bp++) {
105 if (bp < lengthCounts.length) {
106 graphCounts[i] += lengthCounts[bp];
107 }
108 }
109
110 if (startAndInterval[1] == 1) {
111 xCategories[i] = ""+minValue;
112 }
113 else {
114 xCategories[i] = minValue+"-"+maxValue;
115 }
116
117 if (graphCounts[i] > max) max = graphCounts[i];
118 }
119 calculated = true;
120 }
121
122 public void processSequence(Sequence sequence) {
123 int seqLen = sequence.getSequence().length();
124
125 if (seqLen+2 > lengthCounts.length) {
126 long [] newLengthCounts = new long[seqLen+2];
127 for (int i=0;i<lengthCounts.length;i++) {
128 newLengthCounts[i] = lengthCounts[i];
129 }
130 lengthCounts = newLengthCounts;
131 }
132
133 ++lengthCounts[seqLen];
134
135 }
136
137 private int [] getSizeDistribution (int min, int max) {
138
139 // We won't group if they've asked us not to
140 if (FastQCConfig.getInstance().nogroup) {
141 return(new int [] {min,1});
142 }
143
144 int base = 1;
145
146 while (base > (max-min)) {
147 base /= 10;
148 }
149
150 int interval;
151 int starting;
152
153 int [] divisions = new int [] {1,2,5};
154
155 OUTER: while (true) {
156
157 for (int d=0;d<divisions.length;d++) {
158 int tester = base * divisions[d];
159 if (((max-min) / tester) <= 50) {
160 interval = tester;
161 break OUTER;
162 }
163 }
164
165 base *=10;
166
167 }
168
169 // Now we work out the first value to be plotted
170 int basicDivision = (int)(min/interval);
171
172 int testStart = basicDivision * interval;
173
174 starting = testStart;
175
176 return new int[] {starting,interval};
177
178 }
179
180
181
182 public void reset () {
183 lengthCounts = new long[0];
184 }
185
186 public String description() {
187 return "Shows the distribution of sequence length over all sequences";
188 }
189
190 public String name() {
191 return "Sequence Length Distribution";
192 }
193
194 public boolean raisesError() {
195 if (!calculated) calculateDistribution();
196
197 // See if they've turned this test off
198 if (ModuleConfig.getParam("sequence_length", "error") == 0) {
199 return false;
200 }
201
202 // We might not have any sequences so only check if we do
203 if (lengthCounts.length > 0) {
204 // Empty sequences get us an error
205 if (lengthCounts[0] > 0) {
206 return true;
207 }
208 }
209 return false;
210 }
211
212 public boolean raisesWarning() {
213 if (!calculated) calculateDistribution();
214
215 // See if they've turned this test off
216 if (ModuleConfig.getParam("sequence_length", "warn") == 0) {
217 return false;
218 }
219
220 // Warn if they're not all the same length
221 boolean seenLength = false;
222 for (int i=0;i<lengthCounts.length;i++) {
223 if (lengthCounts[i] > 0) {
224 if (seenLength) {
225 return true;
226 }
227 else {
228 seenLength = true;
229 }
230 }
231 }
232 return false;
233 }
234
235 public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException {
236 if (!calculated) calculateDistribution();
237
238 writeDefaultImage(report, "sequence_length_distribution.png", "Sequence length distribution", Math.max(800, graphCounts.length*15), 600);
239
240 StringBuffer sb = report.dataDocument();
241 sb.append("#Length\tCount\n");
242 for (int i=0;i<xCategories.length;i++) {
243 // Remove any padding we added to make the graph look better
244 if ((i==0 || i== xCategories.length-1) && graphCounts[i] == 0) {
245 continue;
246 }
247 sb.append(xCategories[i]);
248 sb.append("\t");
249 sb.append(graphCounts[i]);
250 sb.append("\n");
251 }
252 }
253
254 }