| 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.FastQCConfig; |
| 28 |
import uk.ac.babraham.FastQC.Graphs.LineGraph; |
| 29 |
import uk.ac.babraham.FastQC.Report.HTMLReportArchive; |
| 30 |
import uk.ac.babraham.FastQC.Sequence.Sequence; |
| 31 |
|
| 32 |
public class SequenceLengthDistribution extends AbstractQCModule { |
| 33 |
|
| 34 |
private long [] lengthCounts = new long[0]; |
| 35 |
private double [] graphCounts = null; |
| 36 |
private String [] xCategories = new String[0]; |
| 37 |
private double max = 0; |
| 38 |
private boolean calculated = false; |
| 39 |
|
| 40 |
public JPanel getResultsPanel() { |
| 41 |
|
| 42 |
if (!calculated) calculateDistribution(); |
| 43 |
|
| 44 |
return new LineGraph(new double [][] {graphCounts}, 0d, max, "Sequence Length (bp)",new String [] {"Sequence Length"}, xCategories, "Distribution of sequence lengths over all sequences"); |
| 45 |
} |
| 46 |
|
| 47 |
public boolean ignoreFilteredSequences() { |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
public boolean ignoreInReport () { |
| 52 |
if (ModuleConfig.getParam("sequence_length", "ignore") > 0) { |
| 53 |
return true; |
| 54 |
} |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
private synchronized void calculateDistribution () { |
| 59 |
int maxLen = 0; |
| 60 |
int minLen = -1; |
| 61 |
max = 0; |
| 62 |
|
| 63 |
|
| 64 |
for (int i=0;i<lengthCounts.length;i++) { |
| 65 |
if (lengthCounts[i]>0) { |
| 66 |
if (minLen < 0) { |
| 67 |
minLen = i; |
| 68 |
} |
| 69 |
maxLen = i; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
if (minLen < 0) minLen = 0; |
| 77 |
|
| 78 |
|
| 79 |
if (minLen>0) minLen--; |
| 80 |
maxLen++; |
| 81 |
|
| 82 |
int [] startAndInterval = getSizeDistribution(minLen, maxLen); |
| 83 |
|
| 84 |
|
| 85 |
int categories = 0; |
| 86 |
int currentValue = startAndInterval[0]; |
| 87 |
while (currentValue<= maxLen) { |
| 88 |
++categories; |
| 89 |
currentValue+= startAndInterval[1]; |
| 90 |
} |
| 91 |
|
| 92 |
graphCounts = new double[categories]; |
| 93 |
xCategories = new String[categories]; |
| 94 |
|
| 95 |
for (int i=0;i<graphCounts.length;i++) { |
| 96 |
|
| 97 |
int minValue = startAndInterval[0]+(startAndInterval[1]*i); |
| 98 |
int maxValue = (startAndInterval[0]+(startAndInterval[1]*(i+1)))-1; |
| 99 |
|
| 100 |
if (maxValue > maxLen) { |
| 101 |
maxValue = maxLen; |
| 102 |
} |
| 103 |
|
| 104 |
for (int bp=minValue;bp<=maxValue;bp++) { |
| 105 |
if (bp < lengthCounts.length) { |
| 106 |
graphCounts[i] += lengthCounts[bp]; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if (startAndInterval[1] == 1) { |
| 111 |
xCategories[i] = ""+minValue; |
| 112 |
} |
| 113 |
else { |
| 114 |
xCategories[i] = minValue+"-"+maxValue; |
| 115 |
} |
| 116 |
|
| 117 |
if (graphCounts[i] > max) max = graphCounts[i]; |
| 118 |
} |
| 119 |
calculated = true; |
| 120 |
} |
| 121 |
|
| 122 |
public void processSequence(Sequence sequence) { |
| 123 |
int seqLen = sequence.getSequence().length(); |
| 124 |
|
| 125 |
if (seqLen+2 > lengthCounts.length) { |
| 126 |
long [] newLengthCounts = new long[seqLen+2]; |
| 127 |
for (int i=0;i<lengthCounts.length;i++) { |
| 128 |
newLengthCounts[i] = lengthCounts[i]; |
| 129 |
} |
| 130 |
lengthCounts = newLengthCounts; |
| 131 |
} |
| 132 |
|
| 133 |
++lengthCounts[seqLen]; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
private int [] getSizeDistribution (int min, int max) { |
| 138 |
|
| 139 |
|
| 140 |
if (FastQCConfig.getInstance().nogroup) { |
| 141 |
return(new int [] {min,1}); |
| 142 |
} |
| 143 |
|
| 144 |
int base = 1; |
| 145 |
|
| 146 |
while (base > (max-min)) { |
| 147 |
base /= 10; |
| 148 |
} |
| 149 |
|
| 150 |
int interval; |
| 151 |
int starting; |
| 152 |
|
| 153 |
int [] divisions = new int [] {1,2,5}; |
| 154 |
|
| 155 |
OUTER: while (true) { |
| 156 |
|
| 157 |
for (int d=0;d<divisions.length;d++) { |
| 158 |
int tester = base * divisions[d]; |
| 159 |
if (((max-min) / tester) <= 50) { |
| 160 |
interval = tester; |
| 161 |
break OUTER; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
base *=10; |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
int basicDivision = (int)(min/interval); |
| 171 |
|
| 172 |
int testStart = basicDivision * interval; |
| 173 |
|
| 174 |
starting = testStart; |
| 175 |
|
| 176 |
return new int[] {starting,interval}; |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public void reset () { |
| 183 |
lengthCounts = new long[0]; |
| 184 |
} |
| 185 |
|
| 186 |
public String description() { |
| 187 |
return "Shows the distribution of sequence length over all sequences"; |
| 188 |
} |
| 189 |
|
| 190 |
public String name() { |
| 191 |
return "Sequence Length Distribution"; |
| 192 |
} |
| 193 |
|
| 194 |
public boolean raisesError() { |
| 195 |
if (!calculated) calculateDistribution(); |
| 196 |
|
| 197 |
|
| 198 |
if (ModuleConfig.getParam("sequence_length", "error") == 0) { |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
if (lengthCounts.length > 0) { |
| 204 |
|
| 205 |
if (lengthCounts[0] > 0) { |
| 206 |
return true; |
| 207 |
} |
| 208 |
} |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
public boolean raisesWarning() { |
| 213 |
if (!calculated) calculateDistribution(); |
| 214 |
|
| 215 |
|
| 216 |
if (ModuleConfig.getParam("sequence_length", "warn") == 0) { |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
boolean seenLength = false; |
| 222 |
for (int i=0;i<lengthCounts.length;i++) { |
| 223 |
if (lengthCounts[i] > 0) { |
| 224 |
if (seenLength) { |
| 225 |
return true; |
| 226 |
} |
| 227 |
else { |
| 228 |
seenLength = true; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException { |
| 236 |
if (!calculated) calculateDistribution(); |
| 237 |
|
| 238 |
writeDefaultImage(report, "sequence_length_distribution.png", "Sequence length distribution", Math.max(800, graphCounts.length*15), 600); |
| 239 |
|
| 240 |
StringBuffer sb = report.dataDocument(); |
| 241 |
sb.append("#Length\tCount\n"); |
| 242 |
for (int i=0;i<xCategories.length;i++) { |
| 243 |
|
| 244 |
if ((i==0 || i== xCategories.length-1) && graphCounts[i] == 0) { |
| 245 |
continue; |
| 246 |
} |
| 247 |
sb.append(xCategories[i]); |
| 248 |
sb.append("\t"); |
| 249 |
sb.append(graphCounts[i]); |
| 250 |
sb.append("\n"); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
} |