Debian Codegraph @

uk/ac/babraham/FastQC/Modules/AdapterContent.java

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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.BufferedReader;
24 import java.io.FileNotFoundException;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.util.Vector;
30
31 import javax.swing.JLabel;
32 import javax.swing.JPanel;
33 import javax.swing.table.AbstractTableModel;
34 import javax.xml.stream.XMLStreamException;
35 import javax.xml.stream.XMLStreamWriter;
36
37
38 import uk.ac.babraham.FastQC.FastQCConfig;
39 import uk.ac.babraham.FastQC.Graphs.BaseGroup;
40 import uk.ac.babraham.FastQC.Graphs.LineGraph;
41 import uk.ac.babraham.FastQC.Report.HTMLReportArchive;
42 import uk.ac.babraham.FastQC.Sequence.Sequence;
43 import uk.ac.babraham.FastQC.Sequence.Contaminant.ContaminentFinder;
44
45 public class AdapterContent extends AbstractQCModule {
46
47 private int longestSequence = 0;
48 private int longestAdapter = 0;
49
50 private long totalCount = 0;
51
52 public boolean calculated = false;
53
54 // This is the full set of Kmers to be reported
55 private Adapter [] adapters;
56
57 // This is the data for the Kmers which are going to be placed on the graph
58 private double [][] enrichments = null;
59 private String [] labels;
60
61 private String [] xLabels = new String[0];
62
63 BaseGroup [] groups;
64 public AdapterContent () {
65
66 Vector<Adapter>c = new Vector<Adapter>();
67 Vector<String>l = new Vector<String>();
68
69 try {
70
71 BufferedReader br = null;
72 if (FastQCConfig.getInstance().adapter_file == null) {
73 InputStream rsrc=ContaminentFinder.class.getResourceAsStream("/Configuration/adapter_list.txt");
74 if (rsrc==null) throw new FileNotFoundException("cannot find Configuration/adapter_list.txt");
75 br =new BufferedReader(new InputStreamReader(rsrc));
76 }
77 else {
78 br=new BufferedReader(new FileReader(FastQCConfig.getInstance().adapter_file));
79 }
80
81
82 String line;
83 while ((line = br.readLine())!= null){
84
85 if (line.startsWith("#")) continue; // Skip comments
86 if (line.trim().length() == 0) continue; // Skip blank lines
87
88 String [] sections = line.split("\\t+");
89 if (sections.length != 2) {
90 System.err.println("Expected 2 sections for contaminant line but got "+sections.length+" from "+line);
91 continue;
92 }
93 Adapter adapter = new Adapter(sections[0], sections[1]);
94 c.add(adapter);
95 l.add(adapter.name());
96 if (adapter.sequence().length() > longestAdapter) longestAdapter = adapter.sequence().length();
97 }
98 labels = l.toArray(new String[0]);
99
100 br.close();
101 }
102 catch (IOException e) {
103 e.printStackTrace();
104 }
105
106 adapters = c.toArray(new Adapter[0]);
107
108 }
109
110 public boolean ignoreFilteredSequences() {
111 return true;
112 }
113
114 public boolean ignoreInReport () {
115 if (ModuleConfig.getParam("adapter", "ignore") > 0) {
116 return true;
117 }
118 return false;
119 }
120
121 public JPanel getResultsPanel() {
122
123 if (longestAdapter > longestSequence) {
124 // We can't display sensible results
125 JPanel failPanel = new JPanel();
126 failPanel.setLayout(new BorderLayout());
127 failPanel.add(new JLabel("Can't analyse adapters as read length is too short ("+longestAdapter+" vs "+longestSequence+")",JLabel.CENTER),BorderLayout.CENTER);
128 return failPanel;
129 }
130
131 if (!calculated) calculateEnrichment();
132
133 // System.err.println("Xlabels="+xLabels.length+" labels="+labels.length+" enrichments="+enrichments.length+" enrichments[0]="+enrichments[0].length+" groups="+groups.length);
134
135 return (new LineGraph(enrichments, 0, 100, "Position in read (bp)", labels, xLabels, "% Adapter"));
136
137 }
138
139
140
141 public void processSequence(Sequence sequence) {
142 calculated = false;
143 ++totalCount;
144
145 // We need to be careful about making sure that a sequence is not only longer
146 // than we've seen before, but also that the last position we could find a hit
147 // is a positive position.
148
149 // If the sequence is longer than it was then we need to expand the storage in
150 // all of the adapter objects to account for this.
151
152 if (sequence.getSequence().length() > longestSequence && sequence.getSequence().length() - longestAdapter > 0) {
153 longestSequence = sequence.getSequence().length();
154 for (int a=0;a<adapters.length;a++) {
155 adapters[a].expandLengthTo((longestSequence-longestAdapter)+1);
156 }
157 }
158
159 // Now we go through all of the Adapters to see where they occur
160
161 for (int a=0;a<adapters.length;a++) {
162
163 int index = sequence.getSequence().indexOf(adapters[a].sequence());
164 if (index >=0) {
165 for (int i=index;i<=longestSequence-longestAdapter;i++) {
166 adapters[a].incrementCount(i);
167 }
168 }
169 }
170
171
172 }
173
174 public synchronized void calculateEnrichment () {
175 int maxLength = 0;
176 for (int a=0;a<adapters.length;a++) {
177 if (adapters[a].getPositions().length > maxLength) {
178 maxLength = adapters[a].getPositions().length;
179 }
180 }
181
182 // We'll be grouping together positions later so make up the groups now
183 groups = BaseGroup.makeBaseGroups(maxLength);
184
185 // System.err.println("Made "+groups.length+" groups from "+maxLength);
186
187 xLabels = new String[groups.length];
188 for (int i=0;i<xLabels.length;i++) {
189 xLabels[i] = groups[i].toString();
190 }
191
192 enrichments = new double [adapters.length][groups.length];
193
194
195 for (int a=0;a<adapters.length;a++) {
196 long [] positions = adapters[a].positions;
197
198 for (int g=0;g<groups.length;g++) {
199
200 // System.err.println("Looking at group "+groups[g]);
201
202 for (int p=groups[g].lowerCount()-1;p<groups[g].upperCount() && p<positions.length;p++) {
203 // System.err.println("Count at position "+p+" is "+ positions[p]);
204 enrichments[a][g] += (positions[p]*100d)/totalCount;
205 // System.err.println("Percentage at position "+p+" is "+ ((positions[p]*100d)/totalCount)+" total count is "+totalCount);
206 }
207
208 enrichments[a][g] /= (groups[g].upperCount()-groups[g].lowerCount())+1;
209 // System.err.println("Averge Percetage for group "+groups[g]+" is "+ enrichments[a][g]);
210 }
211 }
212
213 calculated = true;
214
215 }
216
217 public void reset () {
218 calculated = false;
219 totalCount = 0;
220 longestSequence = 0;
221 for (int a=0;a<adapters.length;a++) {
222 adapters[a].reset();
223 }
224 }
225
226 public String description() {
227 return "Searches for specific adapter sequences in a library";
228 }
229
230 public String name() {
231 return "Adapter Content";
232 }
233
234 public boolean raisesError() {
235 if (!calculated) calculateEnrichment();
236
237 for (int i=0;i<enrichments.length;i++) {
238 for (int j=0;j<enrichments[i].length;j++) {
239 if (enrichments[i][j] > ModuleConfig.getParam("adapter", "error")) return true;
240 }
241 }
242 return false;
243 }
244
245 public boolean raisesWarning() {
246
247 // We warn if we just couldn't run the analysis
248 if (longestAdapter > longestSequence) return true;
249
250 if (!calculated) calculateEnrichment();
251
252 for (int i=0;i<enrichments.length;i++) {
253 for (int j=0;j<enrichments[i].length;j++) {
254 if (enrichments[i][j] > ModuleConfig.getParam("adapter", "warn")) return true;
255 }
256 }
257 return false;
258 }
259
260 public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException {
261
262 if (longestAdapter > longestSequence) {
263 XMLStreamWriter xhtml = report.xhtmlStream();
264
265 xhtml.writeStartElement("p");
266 xhtml.writeCharacters("Can't analyse adapters as read length is too short ("+longestAdapter+" vs "+longestSequence+")");
267 xhtml.writeEndElement();
268 }
269
270 else {
271
272 if (!calculated) calculateEnrichment();
273
274 writeDefaultImage(report, "adapter_content.png", "Adapter graph", Math.max(800, groups.length*15), 600);
275
276 StringBuffer sb = report.dataDocument();
277
278 ResultsTable table = new ResultsTable();
279 // Header
280 sb.append("#");
281 for (int i=0;i<table.getColumnCount();i++) {
282 if (i>0) {
283 sb.append("\t");
284 }
285 sb.append(table.getColumnName(i));
286 }
287 sb.append("\n");
288
289 for (int r=0;r<table.getRowCount();r++) {
290 for (int c=0;c<table.getColumnCount();c++) {
291 if (c>0) {
292 sb.append("\t");
293 }
294 sb.append(table.getValueAt(r, c));
295 }
296 sb.append("\n");
297 }
298
299 }
300 }
301
302 private class Adapter {
303
304 private String name;
305 private String sequence;
306 private long [] positions = new long[0];
307
308 public Adapter (String name, String sequence) {
309
310 this.name = name;
311 this.sequence = sequence;
312 positions = new long[1];
313 }
314
315 public void incrementCount (int position) {
316
317 // Don't ever check or expand the storage within this
318 // function as it ends up double counting previously
319 // incremented positions. Rely on the upstream code
320 // having done the expansion correctly already.
321
322 ++positions[position];
323
324 }
325
326 public void expandLengthTo (int newLength) {
327 long [] newPositions = new long[newLength];
328 for (int i=0;i<positions.length;i++) {
329 newPositions[i] = positions[i];
330 // System.err.println("Copied value "+positions[i]+" at position "+i);
331 }
332 // Copy the current longest value to the newly added slots
333 if (positions.length > 0) {
334 for (int i=positions.length;i<newPositions.length;i++) {
335 newPositions[i] = positions[positions.length-1];
336 }
337 }
338
339 positions = newPositions;
340
341 }
342
343 public long [] getPositions () {
344 return positions;
345 }
346
347 public String sequence () {
348 return sequence;
349 }
350
351 public void reset () {
352 positions = new long[0];
353 }
354
355 public String name () {
356 return name;
357 }
358 }
359
360
361 private class ResultsTable extends AbstractTableModel {
362 private static final long serialVersionUID = 1L;
363
364 public ResultsTable () {}
365
366 // Sequence - Count - Obs/Exp
367 public int getColumnCount() {
368 return adapters.length+1;
369 }
370
371 public int getRowCount() {
372 return enrichments[0].length;
373 }
374
375 public Object getValueAt(int rowIndex, int columnIndex) {
376 if (columnIndex == 0) return xLabels[rowIndex];
377 return enrichments[columnIndex-1][rowIndex];
378 }
379
380 public String getColumnName (int columnIndex) {
381 if (columnIndex == 0) return "Position";
382 return (labels[columnIndex-1]);
383 }
384
385 public Class<?> getColumnClass (int columnIndex) {
386 switch (columnIndex) {
387 case 0: return String.class;
388 }
389 return Double.class;
390
391 }
392 }
393
394 }