Debian Codegraph @

uk/ac/babraham/FastQC/Modules/OverRepresentedSeqs.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 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.JScrollPane;
33 import javax.swing.JTable;
34 import javax.swing.table.AbstractTableModel;
35 import javax.swing.table.TableModel;
36 import javax.xml.stream.XMLStreamException;
37 import javax.xml.stream.XMLStreamWriter;
38
39 import uk.ac.babraham.FastQC.FastQCConfig;
40 import uk.ac.babraham.FastQC.Report.HTMLReportArchive;
41 import uk.ac.babraham.FastQC.Sequence.Sequence;
42 import uk.ac.babraham.FastQC.Sequence.Contaminant.ContaminantHit;
43 import uk.ac.babraham.FastQC.Sequence.Contaminant.ContaminentFinder;
44
45 public class OverRepresentedSeqs extends AbstractQCModule {
46
47 protected HashMap<String, Integer>sequences = new HashMap<String, Integer>();
48 protected long count = 0;
49 private OverrepresentedSeq [] overrepresntedSeqs = null;
50 private boolean calculated = false;
51 private boolean frozen = false;
52 private DuplicationLevel duplicationModule;
53
54 // This is the number of different sequences we want to track
55 private final int OBSERVATION_CUTOFF = 100000;
56 // This is a count of how many unique sequences we've seen so far
57 // so we know when to stop adding them.
58 private int uniqueSequenceCount = 0;
59 // This was the total count at the point at which we saw our total
60 // number of unique sequences, so we know what to correct by when
61 // extrapolating to the whole file
62 protected long countAtUniqueLimit = 0;
63
64 public OverRepresentedSeqs () {
65 duplicationModule = new DuplicationLevel(this);
66 }
67
68 public boolean ignoreInReport () {
69 if (ModuleConfig.getParam("overrepresented", "ignore") > 0) {
70 return true;
71 }
72 return false;
73 }
74
75 public String description() {
76 return "Identifies sequences which are overrepresented in the set";
77 }
78
79 public boolean ignoreFilteredSequences() {
80 return true;
81 }
82
83 public DuplicationLevel duplicationLevelModule () {
84 return duplicationModule;
85 }
86
87 public JPanel getResultsPanel() {
88 JPanel returnPanel = new JPanel();
89 returnPanel.setLayout(new BorderLayout());
90 returnPanel.add(new JLabel("Overrepresented sequences",JLabel.CENTER),BorderLayout.NORTH);
91
92 if (!calculated) getOverrepresentedSeqs();
93
94 if (overrepresntedSeqs.length > 0) {
95 TableModel model = new ResultsTable(overrepresntedSeqs);
96 JTable table = new JTable(model);
97 table.setCellSelectionEnabled(true);
98 returnPanel.add(new JScrollPane(table),BorderLayout.CENTER);
99 }
100 else {
101 returnPanel.add(new JLabel("There are no overrepresented sequences",JLabel.CENTER),BorderLayout.CENTER);
102 }
103
104 return returnPanel;
105
106 }
107
108 public DuplicationLevel getDuplicationLevelModule () {
109 return duplicationModule;
110 }
111 private synchronized void getOverrepresentedSeqs () {
112
113 // If the duplication module hasn't already done
114 // its calculation it needs to do it now before
115 // we stomp all over the data
116 duplicationModule.calculateLevels();
117
118 Iterator<String> s = sequences.keySet().iterator();
119 List<OverrepresentedSeq>keepers = new ArrayList<OverrepresentedSeq>();
120
121 while (s.hasNext()) {
122 String seq = s.next();
123 double percentage = ((double)sequences.get(seq)/count)*100;
124 if (percentage > ModuleConfig.getParam("overrepresented", "warn")) {
125 OverrepresentedSeq os = new OverrepresentedSeq(seq, sequences.get(seq), percentage);
126 keepers.add(os);
127 }
128 }
129
130 overrepresntedSeqs = keepers.toArray(new OverrepresentedSeq[0]);
131 Arrays.sort(overrepresntedSeqs);
132 calculated = true;
133 sequences.clear();
134
135 }
136
137 public void reset () {
138 count = 0;
139 sequences.clear();
140 }
141
142 public String name() {
143 return "Overrepresented sequences";
144 }
145
146 public void processSequence(Sequence sequence) {
147
148 calculated = false;
149
150 ++count;
151
152 // Since we rely on identity to match sequences we can't trust really long
153 // sequences, so anything over 75bp gets truncated to 50bp.
154 String seq = sequence.getSequence();
155
156 // For long sequences (above 50bp) we truncate to 50bp so that random
157 // base miscalls don't mess things up. We also allow the user to
158 // specify a shorter truncation length on the command line in the
159 // dup_length option.
160
161 if (FastQCConfig.getInstance().dupLength != 0) {
162 seq = new String(seq.substring(0, FastQCConfig.getInstance().dupLength));
163 }
164 else if (seq.length() > 50) {
165 seq = new String(seq.substring(0, 50));
166 }
167
168 if (sequences.containsKey(seq)) {
169 sequences.put(seq, sequences.get(seq)+1);
170
171 // We need to increment the count at unique limit just in case
172 // we never hit the unique sequence limit, so we need to know
173 // that we'd actually seen all of the sequences.
174 if (! frozen) {
175 countAtUniqueLimit = count;
176 }
177 }
178 else {
179 if (! frozen) {
180 sequences.put(seq, 1);
181 ++uniqueSequenceCount;
182 countAtUniqueLimit = count;
183 if (uniqueSequenceCount == OBSERVATION_CUTOFF) {
184 frozen = true;
185 }
186
187 }
188 }
189 }
190
191 @SuppressWarnings("serial")
192 private class ResultsTable extends AbstractTableModel {
193
194 private OverrepresentedSeq [] seqs;
195
196 public ResultsTable (OverrepresentedSeq [] seqs) {
197 this.seqs = seqs;
198 }
199
200
201 // Sequence - Count - Percentage
202 public int getColumnCount() {
203 return 4;
204 }
205
206 public int getRowCount() {
207 return seqs.length;
208 }
209
210 public Object getValueAt(int rowIndex, int columnIndex) {
211 switch (columnIndex) {
212 case 0: return seqs[rowIndex].seq();
213 case 1: return seqs[rowIndex].count();
214 case 2: return seqs[rowIndex].percentage();
215 case 3: return seqs[rowIndex].contaminantHit();
216
217 }
218 return null;
219 }
220
221 public String getColumnName (int columnIndex) {
222 switch (columnIndex) {
223 case 0: return "Sequence";
224 case 1: return "Count";
225 case 2: return "Percentage";
226 case 3: return "Possible Source";
227 }
228 return null;
229 }
230
231 public Class<?> getColumnClass (int columnIndex) {
232 switch (columnIndex) {
233 case 0: return String.class;
234 case 1: return Integer.class;
235 case 2: return Double.class;
236 case 3: return String.class;
237 }
238 return null;
239
240 }
241 }
242
243 private class OverrepresentedSeq implements Comparable<OverrepresentedSeq>{
244
245 private String seq;
246 private int count;
247 private double percentage;
248 private ContaminantHit contaminantHit;
249
250 public OverrepresentedSeq (String seq, int count, double percentage) {
251 this.seq = seq;
252 this.count = count;
253 this.percentage = percentage;
254 this.contaminantHit = ContaminentFinder.findContaminantHit(seq);
255 }
256
257 public String seq () {
258 return seq;
259 }
260
261 public int count () {
262 return count;
263 }
264
265 public double percentage () {
266 return percentage;
267 }
268
269 public String contaminantHit () {
270 if (contaminantHit == null) {
271 return "No Hit";
272 }
273 else {
274 return contaminantHit.toString();
275 }
276 }
277
278 public int compareTo(OverrepresentedSeq o) {
279 return o.count-count;
280 }
281 }
282
283 public boolean raisesError() {
284 if (!calculated) getOverrepresentedSeqs();
285 if (overrepresntedSeqs.length>0) {
286 if (overrepresntedSeqs[0].percentage > ModuleConfig.getParam("overrepresented", "error")) {
287 return true;
288 }
289 }
290 return false;
291 }
292
293 public boolean raisesWarning() {
294 if (!calculated) getOverrepresentedSeqs();
295
296 if (overrepresntedSeqs.length > 0) return true;
297 return false;
298 }
299
300 public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException {
301 if (!calculated) getOverrepresentedSeqs();
302 ResultsTable table = new ResultsTable(overrepresntedSeqs);
303
304 if (overrepresntedSeqs.length == 0)
305 {
306 XMLStreamWriter w=report.xhtmlStream();
307 w.writeStartElement("p");
308 w.writeCharacters("No overrepresented sequences");
309 w.writeEndElement();
310 }
311
312 else {
313 super.writeTable(report, table);
314 }
315 }
316
317 }