| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 56 |
private Adapter [] adapters; |
| 57 |
|
| 58 |
|
| 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 |
|
| 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; |
| 88 |
if (line.trim().length() == 0) continue; |
| 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 |
|
| 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 |
|
| 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 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 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 |
|
| 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 |
|
| 185 |
groups = BaseGroup.makeBaseGroups(maxLength); |
| 186 |
|
| 187 |
|
| 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 |
|
| 203 |
|
| 204 |
for (int p=groups[g].lowerCount()-1;p<groups[g].upperCount() && p<positions.length;p++) { |
| 205 |
|
| 206 |
enrichments[a][g] += (positions[p]*100d)/totalCount; |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
enrichments[a][g] /= (groups[g].upperCount()-groups[g].lowerCount())+1; |
| 211 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 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 |
|
| 333 |
} |
| 334 |
|
| 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 |
|
| 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 |
} |