Debian Codegraph @

uk/ac/babraham/FastQC/Modules/BasicStats.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.awt.BorderLayout;
23 import java.io.IOException;
24
25 import javax.swing.JLabel;
26 import javax.swing.JPanel;
27 import javax.swing.JScrollPane;
28 import javax.swing.JTable;
29 import javax.swing.table.AbstractTableModel;
30 import javax.swing.table.TableModel;
31 import javax.xml.stream.XMLStreamException;
32
33 import uk.ac.babraham.FastQC.Report.HTMLReportArchive;
34 import uk.ac.babraham.FastQC.Sequence.Sequence;
35 import uk.ac.babraham.FastQC.Sequence.QualityEncoding.PhredEncoding;
36
37 public class BasicStats extends AbstractQCModule {
38
39 private String name = null;
40 private long actualCount = 0;
41 private long filteredCount = 0;
42 private int minLength = 0;
43 private int maxLength = 0;
44 private long totalBases = 0;
45 private long gCount = 0;
46 private long cCount = 0;
47 private long aCount = 0;
48 private long tCount = 0;
49 @SuppressWarnings("unused")
50 private long nCount = 0;
51 private char lowestChar = 126;
52 private String fileType = null;
53
54 public String description() {
55 return "Calculates some basic statistics about the file";
56 }
57
58 public boolean ignoreFilteredSequences() {
59 return false;
60 }
61
62 public JPanel getResultsPanel() {
63 JPanel returnPanel = new JPanel();
64 returnPanel.setLayout(new BorderLayout());
65 returnPanel.add(new JLabel("Basic sequence stats",JLabel.CENTER),BorderLayout.NORTH);
66
67 TableModel model = new ResultsTable();
68 returnPanel.add(new JScrollPane(new JTable(model)),BorderLayout.CENTER);
69
70 return returnPanel;
71
72 }
73
74 public void reset () {
75 minLength = 0;
76 maxLength = 0;
77 gCount = 0;
78 cCount = 0;
79 aCount = 0;
80 tCount = 0;
81 nCount = 0;
82 }
83
84 public String name() {
85 return "Basic Statistics";
86 }
87
88 public void setFileName (String name) {
89 this.name = name;
90
91 this.name = this.name.replaceFirst("stdin:", "");
92 }
93
94 public void processSequence(Sequence sequence) {
95
96 if (name == null) setFileName(sequence.file().name());
97
98 // If this is a filtered sequence we simply count it and move on.
99 if (sequence.isFiltered()) {
100 filteredCount++;
101 return;
102 }
103
104 actualCount++;
105
106 totalBases += sequence.getSequence().length();
107
108 if (fileType == null) {
109 if (sequence.getColorspace() != null) {
110 fileType = "Colorspace converted to bases";
111 }
112 else {
113 fileType = "Conventional base calls";
114 }
115 }
116
117 if (actualCount == 1) {
118 minLength = sequence.getSequence().length();
119 maxLength = sequence.getSequence().length();
120 }
121 else {
122 if (sequence.getSequence().length() < minLength) minLength = sequence.getSequence().length();
123 if (sequence.getSequence().length() > maxLength) maxLength = sequence.getSequence().length();
124 }
125
126 char [] chars = sequence.getSequence().toCharArray();
127 for (int c=0;c<chars.length;c++) {
128 switch (chars[c]) {
129 case 'G': ++gCount;break;
130 case 'A': ++aCount;break;
131 case 'T': ++tCount;break;
132 case 'C': ++cCount;break;
133 case 'N': ++nCount;break;
134 }
135 }
136
137 chars = sequence.getQualityString().toCharArray();
138 for (int c=0;c<chars.length;c++) {
139 if (chars[c] < lowestChar) {
140 lowestChar = chars[c];
141 }
142 }
143 }
144
145 public boolean raisesError() {
146 return false;
147 }
148
149 public boolean raisesWarning() {
150 return false;
151 }
152
153 public boolean ignoreInReport () {
154 return false;
155 }
156
157 public static String formatLength (long originalLength) {
158
159 double length = originalLength;
160
161 String unit = " bp";
162
163 if (length >= 1000000000) {
164 length /= 1000000000;
165 unit = " Gbp";
166 }
167
168 else if (length >= 1000000) {
169 length /= 1000000;
170 unit = " Mbp";
171 }
172 else if (length >= 1000) {
173 length /=1000;
174 unit = " kbp";
175 }
176
177
178 String rawLength = ""+length;
179 char [] chars = rawLength.toCharArray();
180
181 int lastIndex = 0;
182
183 // Go through until we find a dot (if there is one)
184 for (int i=0;i<chars.length;i++) {
185 lastIndex = i;
186 if (chars[i] == '.') break;
187 }
188
189 // We keep the next char as well if they are non
190 // zero numbers
191
192 if (lastIndex+1 < chars.length && chars[lastIndex+1] != '0') {
193 lastIndex+=1;
194 }
195 else if (lastIndex > 0 && chars[lastIndex] == '.') {
196 lastIndex -= 1; // Lose the dot if its the last character
197 }
198
199 char [] finalChars = new char[lastIndex+1];
200 for (int i=0;i<=lastIndex;i++) {
201 finalChars[i] = chars[i];
202 }
203
204 return new String(finalChars)+unit;
205 }
206
207
208 public void makeReport(HTMLReportArchive report) throws XMLStreamException,IOException {
209 super.writeTable(report, new ResultsTable());
210 }
211
212 @SuppressWarnings("serial")
213 private class ResultsTable extends AbstractTableModel {
214
215 private String [] rowNames = new String [] {
216 "Filename",
217 "File type",
218 "Encoding",
219 "Total Sequences",
220 "Total Bases",
221 "Sequences flagged as poor quality",
222 "Sequence length",
223 "%GC",
224 };
225
226 // Sequence - Count - Percentage
227 public int getColumnCount() {
228 return 2;
229 }
230
231 public int getRowCount() {
232 return rowNames.length;
233 }
234
235 public Object getValueAt(int rowIndex, int columnIndex) {
236 switch (columnIndex) {
237 case 0: return rowNames[rowIndex];
238 case 1:
239 switch (rowIndex) {
240 case 0 : return name;
241 case 1 : return fileType;
242 case 2 : return PhredEncoding.getFastQEncodingOffset(lowestChar);
243 case 3 : return ""+actualCount;
244 case 4 : return BasicStats.formatLength(totalBases);
245 case 5 : return ""+filteredCount;
246 case 6 :
247 if (minLength == maxLength) {
248 return ""+minLength;
249 }
250 else {
251 return minLength+"-"+maxLength;
252 }
253
254
255 case 7 :
256 if (aCount+tCount+gCount+cCount > 0) {
257 return ""+(((gCount+cCount)*100)/(aCount+tCount+gCount+cCount));
258 }
259 else {
260 return 0;
261 }
262
263 }
264 }
265 return null;
266 }
267
268 public String getColumnName (int columnIndex) {
269 switch (columnIndex) {
270 case 0: return "Measure";
271 case 1: return "Value";
272 }
273 return null;
274 }
275
276 public Class<?> getColumnClass (int columnIndex) {
277 switch (columnIndex) {
278 case 0: return String.class;
279 case 1: return String.class;
280 }
281 return null;
282
283 }
284 }
285
286
287 }