| 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.IOException; |
| 24 |
|
| 25 |
import javax.swing.JLabel; |
| 26 |
import javax.swing.JPanel; |
| 27 |
import javax.swing.JScrollPane; |
| 28 |
import javax.swing.JTable; |
| 29 |
import javax.swing.table.AbstractTableModel; |
| 30 |
import javax.swing.table.TableModel; |
| 31 |
import javax.xml.stream.XMLStreamException; |
| 32 |
|
| 33 |
import uk.ac.babraham.FastQC.Report.HTMLReportArchive; |
| 34 |
import uk.ac.babraham.FastQC.Sequence.Sequence; |
| 35 |
import uk.ac.babraham.FastQC.Sequence.QualityEncoding.PhredEncoding; |
| 36 |
|
| 37 |
public class BasicStats extends AbstractQCModule { |
| 38 |
|
| 39 |
private String name = null; |
| 40 |
private long actualCount = 0; |
| 41 |
private long filteredCount = 0; |
| 42 |
private int minLength = 0; |
| 43 |
private int maxLength = 0; |
| 44 |
private long totalBases = 0; |
| 45 |
private long gCount = 0; |
| 46 |
private long cCount = 0; |
| 47 |
private long aCount = 0; |
| 48 |
private long tCount = 0; |
| 49 |
@SuppressWarnings("unused") |
| 50 |
private long nCount = 0; |
| 51 |
private char lowestChar = 126; |
| 52 |
private String fileType = null; |
| 53 |
|
| 54 |
public String description() { |
| 55 |
return "Calculates some basic statistics about the file"; |
| 56 |
} |
| 57 |
|
| 58 |
public boolean ignoreFilteredSequences() { |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
public JPanel getResultsPanel() { |
| 63 |
JPanel returnPanel = new JPanel(); |
| 64 |
returnPanel.setLayout(new BorderLayout()); |
| 65 |
returnPanel.add(new JLabel("Basic sequence stats",JLabel.CENTER),BorderLayout.NORTH); |
| 66 |
|
| 67 |
TableModel model = new ResultsTable(); |
| 68 |
returnPanel.add(new JScrollPane(new JTable(model)),BorderLayout.CENTER); |
| 69 |
|
| 70 |
return returnPanel; |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
public void reset () { |
| 75 |
minLength = 0; |
| 76 |
maxLength = 0; |
| 77 |
gCount = 0; |
| 78 |
cCount = 0; |
| 79 |
aCount = 0; |
| 80 |
tCount = 0; |
| 81 |
nCount = 0; |
| 82 |
} |
| 83 |
|
| 84 |
public String name() { |
| 85 |
return "Basic Statistics"; |
| 86 |
} |
| 87 |
|
| 88 |
public void setFileName (String name) { |
| 89 |
this.name = name; |
| 90 |
|
| 91 |
this.name = this.name.replaceFirst("stdin:", ""); |
| 92 |
} |
| 93 |
|
| 94 |
public void processSequence(Sequence sequence) { |
| 95 |
|
| 96 |
if (name == null) setFileName(sequence.file().name()); |
| 97 |
|
| 98 |
|
| 99 |
if (sequence.isFiltered()) { |
| 100 |
filteredCount++; |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
actualCount++; |
| 105 |
|
| 106 |
totalBases += sequence.getSequence().length(); |
| 107 |
|
| 108 |
if (fileType == null) { |
| 109 |
if (sequence.getColorspace() != null) { |
| 110 |
fileType = "Colorspace converted to bases"; |
| 111 |
} |
| 112 |
else { |
| 113 |
fileType = "Conventional base calls"; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if (actualCount == 1) { |
| 118 |
minLength = sequence.getSequence().length(); |
| 119 |
maxLength = sequence.getSequence().length(); |
| 120 |
} |
| 121 |
else { |
| 122 |
if (sequence.getSequence().length() < minLength) minLength = sequence.getSequence().length(); |
| 123 |
if (sequence.getSequence().length() > maxLength) maxLength = sequence.getSequence().length(); |
| 124 |
} |
| 125 |
|
| 126 |
char [] chars = sequence.getSequence().toCharArray(); |
| 127 |
for (int c=0;c<chars.length;c++) { |
| 128 |
switch (chars[c]) { |
| 129 |
case 'G': ++gCount;break; |
| 130 |
case 'A': ++aCount;break; |
| 131 |
case 'T': ++tCount;break; |
| 132 |
case 'C': ++cCount;break; |
| 133 |
case 'N': ++nCount;break; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
chars = sequence.getQualityString().toCharArray(); |
| 138 |
for (int c=0;c<chars.length;c++) { |
| 139 |
if (chars[c] < lowestChar) { |
| 140 |
lowestChar = chars[c]; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
public boolean raisesError() { |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
public boolean raisesWarning() { |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
public boolean ignoreInReport () { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
public static String formatLength (long originalLength) { |
| 158 |
|
| 159 |
double length = originalLength; |
| 160 |
|
| 161 |
String unit = " bp"; |
| 162 |
|
| 163 |
if (length >= 1000000000) { |
| 164 |
length /= 1000000000; |
| 165 |
unit = " Gbp"; |
| 166 |
} |
| 167 |
|
| 168 |
else if (length >= 1000000) { |
| 169 |
length /= 1000000; |
| 170 |
unit = " Mbp"; |
| 171 |
} |
| 172 |
else if (length >= 1000) { |
| 173 |
length /=1000; |
| 174 |
unit = " kbp"; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
String rawLength = ""+length; |
| 179 |
char [] chars = rawLength.toCharArray(); |
| 180 |
|
| 181 |
int lastIndex = 0; |
| 182 |
|
| 183 |
|
| 184 |
for (int i=0;i<chars.length;i++) { |
| 185 |
lastIndex = i; |
| 186 |
if (chars[i] == '.') break; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
if (lastIndex+1 < chars.length && chars[lastIndex+1] != '0') { |
| 193 |
lastIndex+=1; |
| 194 |
} |
| 195 |
else if (lastIndex > 0 && chars[lastIndex] == '.') { |
| 196 |
lastIndex -= 1; |
| 197 |
} |
| 198 |
|
| 199 |
char [] finalChars = new char[lastIndex+1]; |
| 200 |
for (int i=0;i<=lastIndex;i++) { |
| 201 |
finalChars[i] = chars[i]; |
| 202 |
} |
| 203 |
|
| 204 |
return new String(finalChars)+unit; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
public void makeReport(HTMLReportArchive report) throws XMLStreamException,IOException { |
| 209 |
super.writeTable(report, new ResultsTable()); |
| 210 |
} |
| 211 |
|
| 212 |
@SuppressWarnings("serial") |
| 213 |
private class ResultsTable extends AbstractTableModel { |
| 214 |
|
| 215 |
private String [] rowNames = new String [] { |
| 216 |
"Filename", |
| 217 |
"File type", |
| 218 |
"Encoding", |
| 219 |
"Total Sequences", |
| 220 |
"Total Bases", |
| 221 |
"Sequences flagged as poor quality", |
| 222 |
"Sequence length", |
| 223 |
"%GC", |
| 224 |
}; |
| 225 |
|
| 226 |
|
| 227 |
public int getColumnCount() { |
| 228 |
return 2; |
| 229 |
} |
| 230 |
|
| 231 |
public int getRowCount() { |
| 232 |
return rowNames.length; |
| 233 |
} |
| 234 |
|
| 235 |
public Object getValueAt(int rowIndex, int columnIndex) { |
| 236 |
switch (columnIndex) { |
| 237 |
case 0: return rowNames[rowIndex]; |
| 238 |
case 1: |
| 239 |
switch (rowIndex) { |
| 240 |
case 0 : return name; |
| 241 |
case 1 : return fileType; |
| 242 |
case 2 : return PhredEncoding.getFastQEncodingOffset(lowestChar); |
| 243 |
case 3 : return ""+actualCount; |
| 244 |
case 4 : return BasicStats.formatLength(totalBases); |
| 245 |
case 5 : return ""+filteredCount; |
| 246 |
case 6 : |
| 247 |
if (minLength == maxLength) { |
| 248 |
return ""+minLength; |
| 249 |
} |
| 250 |
else { |
| 251 |
return minLength+"-"+maxLength; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
case 7 : |
| 256 |
if (aCount+tCount+gCount+cCount > 0) { |
| 257 |
return ""+(((gCount+cCount)*100)/(aCount+tCount+gCount+cCount)); |
| 258 |
} |
| 259 |
else { |
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
} |
| 264 |
} |
| 265 |
return null; |
| 266 |
} |
| 267 |
|
| 268 |
public String getColumnName (int columnIndex) { |
| 269 |
switch (columnIndex) { |
| 270 |
case 0: return "Measure"; |
| 271 |
case 1: return "Value"; |
| 272 |
} |
| 273 |
return null; |
| 274 |
} |
| 275 |
|
| 276 |
public Class<?> getColumnClass (int columnIndex) { |
| 277 |
switch (columnIndex) { |
| 278 |
case 0: return String.class; |
| 279 |
case 1: return String.class; |
| 280 |
} |
| 281 |
return null; |
| 282 |
|
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
} |