Debian Codegraph @

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

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