| 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 |
|
| 24 |
import javax.swing.JPanel; |
| 25 |
import javax.xml.stream.XMLStreamException; |
| 26 |
|
| 27 |
import uk.ac.babraham.FastQC.Graphs.LineGraph; |
| 28 |
import uk.ac.babraham.FastQC.Modules.GCModel.GCModel; |
| 29 |
import uk.ac.babraham.FastQC.Modules.GCModel.GCModelValue; |
| 30 |
import uk.ac.babraham.FastQC.Report.HTMLReportArchive; |
| 31 |
import uk.ac.babraham.FastQC.Sequence.Sequence; |
| 32 |
import uk.ac.babraham.FastQC.Statistics.NormalDistribution; |
| 33 |
|
| 34 |
public class PerSequenceGCContent extends AbstractQCModule { |
| 35 |
|
| 36 |
private double [] gcDistribution = new double[101]; |
| 37 |
private double [] theoreticalDistribution = new double[101]; |
| 38 |
private int [] xCategories = new int[0]; |
| 39 |
private double max = 0; |
| 40 |
private double deviationPercent; |
| 41 |
private boolean calculated = false; |
| 42 |
|
| 43 |
private GCModel [] cachedModels = new GCModel [200]; |
| 44 |
|
| 45 |
public JPanel getResultsPanel() { |
| 46 |
|
| 47 |
if (!calculated) calculateDistribution(); |
| 48 |
|
| 49 |
return new LineGraph(new double [][] {gcDistribution,theoreticalDistribution}, 0d, max, "Mean GC content (%)", new String [] {"GC count per read","Theoretical Distribution"}, xCategories, "GC distribution over all sequences"); |
| 50 |
} |
| 51 |
|
| 52 |
public boolean ignoreFilteredSequences() { |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
public boolean ignoreInReport () { |
| 57 |
if (ModuleConfig.getParam("gc_sequence", "ignore") > 0) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
private synchronized void calculateDistribution () { |
| 65 |
max = 0; |
| 66 |
xCategories = new int[gcDistribution.length]; |
| 67 |
double totalCount = 0; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
int firstMode = 0; |
| 73 |
double modeCount = 0; |
| 74 |
|
| 75 |
for (int i=0;i<gcDistribution.length;i++) { |
| 76 |
xCategories[i] = i; |
| 77 |
totalCount += gcDistribution[i]; |
| 78 |
|
| 79 |
if (gcDistribution[i] > modeCount) { |
| 80 |
modeCount = gcDistribution[i]; |
| 81 |
firstMode = i; |
| 82 |
} |
| 83 |
if (gcDistribution[i] > max) max = gcDistribution[i]; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
double mode = 0; |
| 93 |
int modeDuplicates = 0; |
| 94 |
|
| 95 |
boolean fellOffTop = true; |
| 96 |
|
| 97 |
for (int i=firstMode;i<gcDistribution.length;i++) { |
| 98 |
if (gcDistribution[i] > gcDistribution[firstMode] - (gcDistribution[firstMode]/10)) { |
| 99 |
mode += i; |
| 100 |
modeDuplicates++; |
| 101 |
} |
| 102 |
else { |
| 103 |
fellOffTop = false; |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
boolean fellOffBottom = true; |
| 109 |
|
| 110 |
for (int i=firstMode-1;i>=0;i--) { |
| 111 |
if (gcDistribution[i] > gcDistribution[firstMode] - (gcDistribution[firstMode]/10)) { |
| 112 |
mode += i; |
| 113 |
modeDuplicates++; |
| 114 |
} |
| 115 |
else { |
| 116 |
fellOffBottom = false; |
| 117 |
break; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if (fellOffBottom || fellOffTop) { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
mode = firstMode; |
| 126 |
} |
| 127 |
else { |
| 128 |
mode /= modeDuplicates; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
double stdev = 0; |
| 135 |
|
| 136 |
for (int i=0;i<gcDistribution.length;i++) { |
| 137 |
stdev += Math.pow((i-mode),2) * gcDistribution[i]; |
| 138 |
} |
| 139 |
|
| 140 |
stdev /= totalCount-1; |
| 141 |
|
| 142 |
stdev = Math.sqrt(stdev); |
| 143 |
|
| 144 |
NormalDistribution nd = new NormalDistribution(mode, stdev); |
| 145 |
|
| 146 |
deviationPercent = 0; |
| 147 |
|
| 148 |
for (int i=0;i<theoreticalDistribution.length;i++) { |
| 149 |
double probability = nd.getZScoreForValue(i); |
| 150 |
theoreticalDistribution[i] = probability*totalCount; |
| 151 |
|
| 152 |
if (theoreticalDistribution[i] > max) { |
| 153 |
max = theoreticalDistribution[i]; |
| 154 |
} |
| 155 |
|
| 156 |
deviationPercent += Math.abs(theoreticalDistribution[i]-gcDistribution[i]); |
| 157 |
} |
| 158 |
|
| 159 |
deviationPercent /= totalCount; |
| 160 |
deviationPercent *= 100; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
calculated = true; |
| 166 |
} |
| 167 |
|
| 168 |
public void processSequence(Sequence sequence) { |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
char [] seq = truncateSequence(sequence); |
| 175 |
|
| 176 |
if (seq.length == 0) return; |
| 177 |
|
| 178 |
|
| 179 |
int thisSeqGCCount = 0; |
| 180 |
for (int i=0;i<seq.length;i++) { |
| 181 |
if (seq[i] == 'G' || seq[i] == 'C') { |
| 182 |
++thisSeqGCCount; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if (seq.length >= cachedModels.length) { |
| 187 |
|
| 188 |
|
| 189 |
GCModel [] longerModels = new GCModel[seq.length+1]; |
| 190 |
for (int i=0;i<cachedModels.length;i++) { |
| 191 |
longerModels[i] = cachedModels[i]; |
| 192 |
} |
| 193 |
|
| 194 |
cachedModels = longerModels; |
| 195 |
} |
| 196 |
|
| 197 |
if (cachedModels[seq.length] == null) { |
| 198 |
cachedModels[seq.length] = new GCModel(seq.length); |
| 199 |
} |
| 200 |
|
| 201 |
GCModelValue [] values = cachedModels[seq.length].getModelValues(thisSeqGCCount); |
| 202 |
|
| 203 |
for (int i=0;i<values.length;i++) { |
| 204 |
gcDistribution[values[i].percentage()] += values[i].increment(); |
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
private char [] truncateSequence (Sequence sequence) { |
| 210 |
|
| 211 |
String seq = sequence.getSequence(); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
if (seq.length() > 1000) { |
| 217 |
int length = (seq.length()/1000)*1000; |
| 218 |
return seq.substring(0, length).toCharArray(); |
| 219 |
} |
| 220 |
if (seq.length() > 100) { |
| 221 |
int length = (seq.length()/100)*100; |
| 222 |
return seq.substring(0, length).toCharArray(); |
| 223 |
} |
| 224 |
|
| 225 |
return seq.toCharArray(); |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
public void reset () { |
| 230 |
gcDistribution = new double[101]; |
| 231 |
} |
| 232 |
|
| 233 |
public String description() { |
| 234 |
return "Shows the distribution of GC contents for whole sequences"; |
| 235 |
} |
| 236 |
|
| 237 |
public String name() { |
| 238 |
return "Per sequence GC content"; |
| 239 |
} |
| 240 |
|
| 241 |
public boolean raisesError() { |
| 242 |
if (!calculated) calculateDistribution(); |
| 243 |
|
| 244 |
return deviationPercent > ModuleConfig.getParam("gc_sequence", "error"); |
| 245 |
} |
| 246 |
|
| 247 |
public boolean raisesWarning() { |
| 248 |
if (!calculated) calculateDistribution(); |
| 249 |
|
| 250 |
return deviationPercent > ModuleConfig.getParam("gc_sequence", "warn"); |
| 251 |
} |
| 252 |
|
| 253 |
public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException { |
| 254 |
|
| 255 |
writeDefaultImage(report, "per_sequence_gc_content.png", "Per sequence GC content graph", 800, 600); |
| 256 |
|
| 257 |
StringBuffer sb = report.dataDocument(); |
| 258 |
sb.append("#GC Content\tCount\n"); |
| 259 |
for (int i=0;i<xCategories.length;i++) { |
| 260 |
sb.append(xCategories[i]); |
| 261 |
sb.append("\t"); |
| 262 |
sb.append(gcDistribution[i]); |
| 263 |
sb.append("\n"); |
| 264 |
} |
| 265 |
} |
| 266 |
} |