| 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.text.DecimalFormat; |
| 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.LineGraph; |
| 31 |
import uk.ac.babraham.FastQC.Report.HTMLReportArchive; |
| 32 |
import uk.ac.babraham.FastQC.Sequence.Sequence; |
| 33 |
|
| 34 |
public class DuplicationLevel extends AbstractQCModule { |
| 35 |
|
| 36 |
private OverRepresentedSeqs overrepresentedModule; |
| 37 |
private double [] totalPercentages = null; |
| 38 |
private double maxCount = 100; |
| 39 |
private double percentDifferentSeqs = 0; |
| 40 |
private String [] labels; |
| 41 |
private static final DecimalFormat df = new DecimalFormat("#.##"); |
| 42 |
|
| 43 |
protected DuplicationLevel (OverRepresentedSeqs overrepresentedModule) { |
| 44 |
this.overrepresentedModule = overrepresentedModule; |
| 45 |
} |
| 46 |
|
| 47 |
public String description() { |
| 48 |
return "Plots the number of sequences which are duplicated to different levels"; |
| 49 |
} |
| 50 |
|
| 51 |
public boolean ignoreFilteredSequences() { |
| 52 |
if (ModuleConfig.getParam("duplication", "ignore") > 0) { |
| 53 |
return true; |
| 54 |
} |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
public boolean ignoreInReport () { |
| 59 |
if (ModuleConfig.getParam("duplication", "ignore") > 0) { |
| 60 |
return true; |
| 61 |
} |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
protected synchronized void calculateLevels () { |
| 66 |
|
| 67 |
if (totalPercentages != null) return; |
| 68 |
|
| 69 |
totalPercentages = new double[16]; |
| 70 |
|
| 71 |
HashMap<Integer, Integer> collatedCounts = new HashMap<Integer, Integer>(); |
| 72 |
|
| 73 |
Iterator<String> it = overrepresentedModule.sequences.keySet().iterator(); |
| 74 |
|
| 75 |
while (it.hasNext()) { |
| 76 |
int thisCount = overrepresentedModule.sequences.get(it.next()); |
| 77 |
|
| 78 |
if (collatedCounts.containsKey(thisCount)) { |
| 79 |
collatedCounts.put(thisCount,collatedCounts.get(thisCount)+1); |
| 80 |
} |
| 81 |
else { |
| 82 |
collatedCounts.put(thisCount,1); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
HashMap<Integer, Double> correctedCounts = new HashMap<Integer, Double>(); |
| 89 |
|
| 90 |
Iterator<Integer> itr = collatedCounts.keySet().iterator(); |
| 91 |
|
| 92 |
while (itr.hasNext()) { |
| 93 |
int dupLevel = itr.next(); |
| 94 |
int count = collatedCounts.get(dupLevel); |
| 95 |
|
| 96 |
correctedCounts.put(dupLevel,getCorrectedCount(overrepresentedModule.countAtUniqueLimit, overrepresentedModule.count, dupLevel, count)); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
double dedupTotal = 0; |
| 103 |
double rawTotal = 0; |
| 104 |
|
| 105 |
Iterator<Integer> itc = correctedCounts.keySet().iterator(); |
| 106 |
|
| 107 |
while (itc.hasNext()) { |
| 108 |
int dupLevel = itc.next(); |
| 109 |
double count = correctedCounts.get(dupLevel); |
| 110 |
|
| 111 |
dedupTotal += count; |
| 112 |
rawTotal += count * dupLevel; |
| 113 |
|
| 114 |
int dupSlot = dupLevel - 1; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
if (dupSlot > 9999 || dupSlot < 0) dupSlot = 15; |
| 121 |
else if (dupSlot > 4999) dupSlot = 14; |
| 122 |
else if (dupSlot > 999) dupSlot = 13; |
| 123 |
else if (dupSlot > 499) dupSlot = 12; |
| 124 |
else if (dupSlot > 99) dupSlot = 11; |
| 125 |
else if (dupSlot > 49) dupSlot = 10; |
| 126 |
else if (dupSlot > 9) dupSlot = 9; |
| 127 |
|
| 128 |
|
| 129 |
totalPercentages[dupSlot] += count * dupLevel; |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
labels = new String [16]; |
| 136 |
for (int i=0;i<totalPercentages.length;i++) { |
| 137 |
if (i<9) labels[i] = ""+(i+1); |
| 138 |
else if (i==9) labels[i]=">10"; |
| 139 |
else if (i==10) labels[i]=">50"; |
| 140 |
else if (i==11) labels[i]=">100"; |
| 141 |
else if (i==12) labels[i]=">500"; |
| 142 |
else if (i==13) labels[i]=">1k"; |
| 143 |
else if (i==14) labels[i]=">5k"; |
| 144 |
else if (i==15) labels[i]=">10k"; |
| 145 |
|
| 146 |
|
| 147 |
totalPercentages[i] /= rawTotal; |
| 148 |
totalPercentages[i] *= 100; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
percentDifferentSeqs = (dedupTotal/rawTotal)*100; |
| 153 |
if (rawTotal == 0) percentDifferentSeqs = 100; |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
private static double getCorrectedCount (long countAtLimit, long totalCount, int duplicationLevel, int numberOfObservations) { |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
if (countAtLimit == totalCount) return numberOfObservations; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
if (totalCount - numberOfObservations < countAtLimit) return numberOfObservations; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
double pNotSeeingAtLimit = 1; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
double limitOfCaring = 1d - (numberOfObservations/(numberOfObservations+0.01d)); |
| 184 |
|
| 185 |
for (int i=0;i<countAtLimit;i++) { |
| 186 |
pNotSeeingAtLimit *= ((totalCount-i)-duplicationLevel)/(double)(totalCount-i); |
| 187 |
|
| 188 |
if (pNotSeeingAtLimit < limitOfCaring) { |
| 189 |
pNotSeeingAtLimit = 0; |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
double pSeeingAtLimit = 1 - pNotSeeingAtLimit; |
| 197 |
|
| 198 |
|
| 199 |
double trueCount = numberOfObservations/pSeeingAtLimit; |
| 200 |
|
| 201 |
return trueCount; |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
public JPanel getResultsPanel() { |
| 207 |
if (totalPercentages == null) calculateLevels(); |
| 208 |
|
| 209 |
return new LineGraph(new double [][] {totalPercentages}, 0d, maxCount, "Sequence Duplication Level",new String [] {"% Total sequences"}, labels, "Percent of seqs remaining if deduplicated "+df.format(percentDifferentSeqs)+"%"); |
| 210 |
} |
| 211 |
|
| 212 |
public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException { |
| 213 |
if (totalPercentages == null) calculateLevels(); |
| 214 |
|
| 215 |
writeDefaultImage(report, "duplication_levels.png", "Duplication level graph", 800, 600); |
| 216 |
|
| 217 |
StringBuffer sb = report.dataDocument(); |
| 218 |
|
| 219 |
sb.append("#Total Deduplicated Percentage\t"); |
| 220 |
sb.append(percentDifferentSeqs); |
| 221 |
sb.append("\n"); |
| 222 |
|
| 223 |
sb.append("#Duplication Level\tPercentage of total\n"); |
| 224 |
for (int i=0;i<labels.length;i++) { |
| 225 |
sb.append(labels[i]); |
| 226 |
if (i == labels.length-1) { |
| 227 |
sb.append("+"); |
| 228 |
} |
| 229 |
sb.append("\t"); |
| 230 |
sb.append(totalPercentages[i]); |
| 231 |
sb.append("\n"); |
| 232 |
} |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
public String name() { |
| 237 |
return "Sequence Duplication Levels"; |
| 238 |
} |
| 239 |
|
| 240 |
public void processSequence(Sequence sequence) { |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
public boolean raisesError() { |
| 247 |
if (totalPercentages == null) calculateLevels(); |
| 248 |
|
| 249 |
|
| 250 |
if (percentDifferentSeqs < ModuleConfig.getParam("duplication", "error")) { |
| 251 |
return true; |
| 252 |
} |
| 253 |
|
| 254 |
return false; |
| 255 |
} |
| 256 |
|
| 257 |
public boolean raisesWarning() { |
| 258 |
if (totalPercentages == null) calculateLevels(); |
| 259 |
|
| 260 |
|
| 261 |
if (percentDifferentSeqs < ModuleConfig.getParam("duplication", "warn")) { |
| 262 |
return true; |
| 263 |
} |
| 264 |
|
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
public void reset() { |
| 269 |
totalPercentages = null; |
| 270 |
} |
| 271 |
|
| 272 |
} |