Debian Codegraph @

uk/ac/babraham/FastQC/Modules/PerSequenceQualityScores.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2010-17 Simon Andrews
3 *
4 * This file is part of FastQC.
5 *
6 * FastQC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FastQC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FastQC; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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
26 import javax.swing.JPanel;
27 import javax.xml.stream.XMLStreamException;
28
29 import uk.ac.babraham.FastQC.Graphs.LineGraph;
30 import uk.ac.babraham.FastQC.Report.HTMLReportArchive;
31 import uk.ac.babraham.FastQC.Sequence.Sequence;
32 import uk.ac.babraham.FastQC.Sequence.QualityEncoding.PhredEncoding;
33
34 public class PerSequenceQualityScores extends AbstractQCModule {
35
36 private HashMap<Integer, Long> averageScoreCounts = new HashMap<Integer, Long>();
37 private double [] qualityDistribution = null;
38 private int [] xCategories = new int[0];
39 private char lowestChar = 126;
40 private int maxCount = 0;
41 private int mostFrequentScore;
42 private boolean calculated = false;
43
44 public JPanel getResultsPanel() {
45
46 if (!calculated) calculateDistribution();
47
48 return new LineGraph(new double [][] {qualityDistribution}, 0d, maxCount, "Mean Sequence Quality (Phred Score)",new String [] {"Average Quality per read"}, xCategories, "Quality score distribution over all sequences");
49 }
50
51 public boolean ignoreInReport () {
52 // We don't show this if they didn't have any quality data.
53 if (ModuleConfig.getParam("quality_sequence", "ignore") > 0 || averageScoreCounts.size() == 0) {
54 return true;
55 }
56 return false;
57 }
58
59 private synchronized void calculateDistribution () {
60
61 PhredEncoding encoding = PhredEncoding.getFastQEncodingOffset(lowestChar);
62
63 Integer [] rawScores = averageScoreCounts.keySet().toArray(new Integer [0]);
64 Arrays.sort(rawScores);
65
66 // We'll run from the lowest to the highest
67 qualityDistribution = new double [1+(rawScores[rawScores.length-1]-rawScores[0])] ;
68
69 xCategories = new int[qualityDistribution.length];
70
71 for (int i=0;i<qualityDistribution.length;i++) {
72 xCategories[i] = (rawScores[0]+i)-encoding.offset();
73 if (averageScoreCounts.containsKey(rawScores[0]+i)) {
74 qualityDistribution[i] = averageScoreCounts.get(rawScores[0]+i);
75 }
76 }
77
78
79 for (int i=0;i<qualityDistribution.length;i++) {
80 if (qualityDistribution[i]>maxCount) {
81 maxCount = (int)qualityDistribution[i];
82 mostFrequentScore = xCategories[i];
83 }
84 }
85
86 calculated = true;
87 }
88
89 public void processSequence(Sequence sequence) {
90
91 char [] seq = sequence.getQualityString().toCharArray();
92 int averageQuality = 0;
93
94 for (int i=0;i<seq.length;i++) {
95 if (seq[i] < lowestChar) {
96 lowestChar = seq[i];
97 }
98 averageQuality += seq[i];
99 }
100
101 if (seq.length > 0) {
102 averageQuality /= seq.length;
103
104 if (averageScoreCounts.containsKey(averageQuality)) {
105 long currentCount = averageScoreCounts.get(averageQuality);
106 currentCount++;
107 averageScoreCounts.put(averageQuality, currentCount);
108 }
109 else {
110 averageScoreCounts.put(averageQuality, 1L);
111 }
112 }
113 }
114
115 public void reset () {
116 averageScoreCounts.clear();
117 lowestChar = 126;
118 maxCount = 0;
119 calculated = false;
120 }
121
122 public String description() {
123 return "Shows the distribution of average quality scores for whole sequences";
124 }
125
126 public String name() {
127 return "Per sequence quality scores";
128 }
129
130 public boolean raisesError() {
131 if (!calculated) calculateDistribution();
132
133 if (mostFrequentScore <= ModuleConfig.getParam("quality_sequence", "error")) return true;
134
135 return false;
136 }
137
138 public boolean raisesWarning() {
139 if (!calculated) calculateDistribution();
140
141 if (mostFrequentScore <= ModuleConfig.getParam("quality_sequence", "warn")) return true;
142
143 return false;
144 }
145
146 public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException {
147 if (!calculated) calculateDistribution();
148
149 writeDefaultImage(report, "per_sequence_quality.png", "Per Sequence quality graph", 800, 600);
150
151 StringBuffer sb = report.dataDocument();
152 sb.append("#Quality\tCount\n");
153 for (int i=0;i<xCategories.length;i++) {
154 sb.append(xCategories[i]);
155 sb.append("\t");
156 sb.append(qualityDistribution[i]);
157 sb.append("\n");
158 }
159 }
160
161
162 public boolean ignoreFilteredSequences() {
163 return true;
164 }
165
166 }