| 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.io.IOException; |
| 23 |
import java.util.Arrays; |
| 24 |
import java.util.HashMap; |
| 25 |
import java.util.Iterator; |
| 26 |
|
| 27 |
import javax.swing.JPanel; |
| 28 |
import javax.xml.stream.XMLStreamException; |
| 29 |
|
| 30 |
import uk.ac.babraham.FastQC.Graphs.BaseGroup; |
| 31 |
import uk.ac.babraham.FastQC.Graphs.TileGraph; |
| 32 |
import uk.ac.babraham.FastQC.Report.HTMLReportArchive; |
| 33 |
import uk.ac.babraham.FastQC.Sequence.Sequence; |
| 34 |
import uk.ac.babraham.FastQC.Sequence.QualityEncoding.PhredEncoding; |
| 35 |
import uk.ac.babraham.FastQC.Utilities.QualityCount; |
| 36 |
|
| 37 |
public class PerTileQualityScores extends AbstractQCModule { |
| 38 |
|
| 39 |
|
| 40 |
public HashMap<Integer, QualityCount []> perTileQualityCounts = new HashMap<Integer, QualityCount[]>(); |
| 41 |
private int currentLength = 0; |
| 42 |
private double [][] means = null; |
| 43 |
private String [] xLabels; |
| 44 |
private int [] tiles; |
| 45 |
private int high = 0; |
| 46 |
PhredEncoding encodingScheme; |
| 47 |
private boolean calculated = false; |
| 48 |
|
| 49 |
private long totalCount = 0; |
| 50 |
|
| 51 |
private int splitPosition = -1; |
| 52 |
|
| 53 |
private double maxDeviation = 0; |
| 54 |
|
| 55 |
private boolean ignoreInReport = false; |
| 56 |
|
| 57 |
public JPanel getResultsPanel() { |
| 58 |
|
| 59 |
if (!calculated) getPercentages(); |
| 60 |
|
| 61 |
return new TileGraph(xLabels, tiles, means); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
public boolean ignoreFilteredSequences() { |
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
public boolean ignoreInReport () { |
| 70 |
if (ignoreInReport || ModuleConfig.getParam("tile", "ignore") > 0 || currentLength == 0) { |
| 71 |
return true; |
| 72 |
} |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
private synchronized void getPercentages () { |
| 77 |
|
| 78 |
char [] range = calculateOffsets(); |
| 79 |
encodingScheme = PhredEncoding.getFastQEncodingOffset(range[0]); |
| 80 |
high = range[1] - encodingScheme.offset(); |
| 81 |
if (high < 35) { |
| 82 |
high = 35; |
| 83 |
} |
| 84 |
|
| 85 |
BaseGroup [] groups = BaseGroup.makeBaseGroups(currentLength); |
| 86 |
|
| 87 |
Integer [] tileNumbers = perTileQualityCounts.keySet().toArray(new Integer[0]); |
| 88 |
|
| 89 |
Arrays.sort(tileNumbers); |
| 90 |
|
| 91 |
tiles = new int[tileNumbers.length]; |
| 92 |
for (int i=0;i<tiles.length;i++) { |
| 93 |
tiles[i] = tileNumbers[i]; |
| 94 |
} |
| 95 |
|
| 96 |
means = new double[tileNumbers.length][groups.length]; |
| 97 |
xLabels = new String[groups.length]; |
| 98 |
|
| 99 |
for (int t=0;t<tileNumbers.length;t++){ |
| 100 |
for (int i=0;i<groups.length;i++) { |
| 101 |
if (t==0) |
| 102 |
xLabels[i] = groups[i].toString(); |
| 103 |
|
| 104 |
int minBase = groups[i].lowerCount(); |
| 105 |
int maxBase = groups[i].upperCount(); |
| 106 |
means[t][i] = getMean(tileNumbers[t],minBase,maxBase,encodingScheme.offset()); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
double maxDeviation = 0; |
| 114 |
|
| 115 |
double [] averageQualitiesPerGroup = new double[groups.length]; |
| 116 |
|
| 117 |
for (int t=0;t<tileNumbers.length;t++) { |
| 118 |
for (int i=0;i<groups.length;i++) { |
| 119 |
averageQualitiesPerGroup[i] += means[t][i]; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
for (int i=0;i<averageQualitiesPerGroup.length;i++) { |
| 124 |
averageQualitiesPerGroup[i] /= tileNumbers.length; |
| 125 |
} |
| 126 |
|
| 127 |
for (int i=0;i<groups.length;i++) { |
| 128 |
for (int t=0;t<tileNumbers.length;t++) { |
| 129 |
means[t][i] -= averageQualitiesPerGroup[i]; |
| 130 |
if (Math.abs(means[t][i])> maxDeviation) { |
| 131 |
maxDeviation = Math.abs(means[t][i]); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
this.maxDeviation = maxDeviation; |
| 137 |
|
| 138 |
calculated = true; |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
private char [] calculateOffsets () { |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
char minChar = 0; |
| 147 |
char maxChar = 0; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
Iterator<QualityCount[]> qit = perTileQualityCounts.values().iterator(); |
| 152 |
|
| 153 |
while (qit.hasNext()) { |
| 154 |
|
| 155 |
QualityCount [] qualityCounts = qit.next(); |
| 156 |
|
| 157 |
for (int q=0;q<qualityCounts.length;q++) { |
| 158 |
if (minChar == 0) { |
| 159 |
minChar = qualityCounts[q].getMinChar(); |
| 160 |
maxChar = qualityCounts[q].getMaxChar(); |
| 161 |
} |
| 162 |
else { |
| 163 |
if (qualityCounts[q].getMinChar() < minChar) { |
| 164 |
minChar = qualityCounts[q].getMinChar(); |
| 165 |
} |
| 166 |
if (qualityCounts[q].getMaxChar() > maxChar) { |
| 167 |
maxChar = qualityCounts[q].getMaxChar(); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return new char[] {minChar,maxChar}; |
| 174 |
} |
| 175 |
|
| 176 |
public void processSequence(Sequence sequence) { |
| 177 |
|
| 178 |
|
| 179 |
if (totalCount == 0) { |
| 180 |
if (ModuleConfig.getParam("tile", "ignore") > 0) { |
| 181 |
ignoreInReport = true; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if (ignoreInReport) return; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
if (sequence.getQualityString().length() == 0) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
calculated = false; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
++totalCount; |
| 212 |
if (totalCount > 10000 && totalCount % 10 != 0) return; |
| 213 |
|
| 214 |
|
| 215 |
int tile = 0; |
| 216 |
|
| 217 |
String [] splitID = sequence.getID().split(":"); |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
try { |
| 222 |
|
| 223 |
|
| 224 |
if (splitPosition >=0) { |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
if (splitID.length <= splitPosition) { |
| 229 |
|
| 230 |
throw new NumberFormatException("Can't extract a number - not enough data"); |
| 231 |
} |
| 232 |
|
| 233 |
tile = Integer.parseInt(splitID[splitPosition]); |
| 234 |
} |
| 235 |
|
| 236 |
else if (splitID.length>=7) { |
| 237 |
splitPosition = 4; |
| 238 |
tile = Integer.parseInt(splitID[4]); |
| 239 |
} |
| 240 |
else if (splitID.length >=5) { |
| 241 |
splitPosition = 2; |
| 242 |
|
| 243 |
tile = Integer.parseInt(splitID[2]); |
| 244 |
} |
| 245 |
else { |
| 246 |
|
| 247 |
ignoreInReport = true; |
| 248 |
return; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
} |
| 253 |
catch (NumberFormatException nfe) { |
| 254 |
|
| 255 |
ignoreInReport = true; |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
char [] qual = sequence.getQualityString().toCharArray(); |
| 260 |
if (currentLength < qual.length) { |
| 261 |
|
| 262 |
Iterator<Integer> tiles = perTileQualityCounts.keySet().iterator(); |
| 263 |
while (tiles.hasNext()) { |
| 264 |
int thisTile = tiles.next(); |
| 265 |
|
| 266 |
QualityCount [] qualityCounts = perTileQualityCounts.get(thisTile); |
| 267 |
QualityCount [] qualityCountsNew = new QualityCount[qual.length]; |
| 268 |
|
| 269 |
for (int i=0;i<qualityCounts.length;i++) { |
| 270 |
qualityCountsNew[i] = qualityCounts[i]; |
| 271 |
} |
| 272 |
for (int i=qualityCounts.length;i<qualityCountsNew.length;i++) { |
| 273 |
qualityCountsNew[i] = new QualityCount(); |
| 274 |
} |
| 275 |
perTileQualityCounts.put(thisTile, qualityCountsNew); |
| 276 |
} |
| 277 |
|
| 278 |
currentLength = qual.length; |
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
if (! perTileQualityCounts.containsKey(tile)) { |
| 283 |
|
| 284 |
if (perTileQualityCounts.size() > 2500) { |
| 285 |
|
| 286 |
|
| 287 |
System.err.println("Too many tiles (>2500) so giving up trying to do per-tile qualities since we're probably parsing the file wrongly"); |
| 288 |
ignoreInReport = true; |
| 289 |
perTileQualityCounts.clear(); |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
QualityCount [] qualityCounts = new QualityCount[currentLength]; |
| 294 |
for (int i=0;i<currentLength;i++) { |
| 295 |
qualityCounts[i] = new QualityCount(); |
| 296 |
} |
| 297 |
|
| 298 |
perTileQualityCounts.put(tile, qualityCounts); |
| 299 |
} |
| 300 |
|
| 301 |
QualityCount [] qualityCounts = perTileQualityCounts.get(tile); |
| 302 |
|
| 303 |
for (int i=0;i<qual.length;i++) { |
| 304 |
qualityCounts[i].addValue(qual[i]); |
| 305 |
} |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
public void reset () { |
| 310 |
totalCount = 0; |
| 311 |
perTileQualityCounts = new HashMap<Integer, QualityCount[]>(); |
| 312 |
} |
| 313 |
|
| 314 |
public String description() { |
| 315 |
return "Shows the perl tile Quality scores of all bases at a given position in a sequencing run"; |
| 316 |
} |
| 317 |
|
| 318 |
public String name() { |
| 319 |
return "Per tile sequence quality"; |
| 320 |
} |
| 321 |
|
| 322 |
public boolean raisesError() { |
| 323 |
if (!calculated) getPercentages(); |
| 324 |
|
| 325 |
if (maxDeviation > ModuleConfig.getParam("tile", "error")) return true; |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
public boolean raisesWarning() { |
| 330 |
if (!calculated) getPercentages(); |
| 331 |
|
| 332 |
if (maxDeviation > ModuleConfig.getParam("tile", "warn")) return true; |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException { |
| 337 |
if (!calculated) getPercentages(); |
| 338 |
|
| 339 |
writeDefaultImage(report, "per_tile_quality.png", "Per tile quality graph", Math.max(800, xLabels.length*15), 600); |
| 340 |
|
| 341 |
StringBuffer sb = report.dataDocument(); |
| 342 |
sb.append("#Tile\tBase\tMean\n"); |
| 343 |
|
| 344 |
for (int t=0;t<tiles.length;t++) { |
| 345 |
for (int i=0;i<means[t].length;i++) { |
| 346 |
|
| 347 |
sb.append(tiles[t]); |
| 348 |
sb.append("\t"); |
| 349 |
|
| 350 |
sb.append(xLabels[i]); |
| 351 |
sb.append("\t"); |
| 352 |
|
| 353 |
sb.append(means[t][i]); |
| 354 |
|
| 355 |
sb.append("\n"); |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
private double getMean (int tile, int minbp, int maxbp, int offset) { |
| 361 |
int count = 0; |
| 362 |
double total = 0; |
| 363 |
|
| 364 |
QualityCount [] qualityCounts = perTileQualityCounts.get(tile); |
| 365 |
|
| 366 |
for (int i=minbp-1;i<maxbp;i++) { |
| 367 |
if (qualityCounts[i].getTotalCount() > 0) { |
| 368 |
count++; |
| 369 |
total += qualityCounts[i].getMean(offset); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
if (count > 0) { |
| 374 |
return total/count; |
| 375 |
} |
| 376 |
return 0; |
| 377 |
|
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
} |