Debian Codegraph @

uk/ac/babraham/FastQC/Modules/PerBaseSequenceContent.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
24 import javax.swing.JPanel;
25 import javax.xml.stream.XMLStreamException;
26
27 import uk.ac.babraham.FastQC.Graphs.BaseGroup;
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 PerBaseSequenceContent extends AbstractQCModule {
33
34 public long [] gCounts = new long [0];
35 public long [] aCounts = new long [0];
36 public long [] cCounts = new long [0];
37 public long [] tCounts = new long [0];
38 private double [][] percentages = null;
39 private String [] xCategories = new String[0];
40 private boolean calculated = false;
41
42
43 public JPanel getResultsPanel() {
44
45 if (!calculated) getPercentages();
46
47 return new LineGraph(percentages, 0d, 100d, "Position in read (bp)", new String [] {"%T","%C","%A","%G"}, xCategories, "Sequence content across all bases");
48 }
49
50 public boolean ignoreFilteredSequences() {
51 return true;
52 }
53
54 public boolean ignoreInReport () {
55 if (ModuleConfig.getParam("sequence", "ignore") > 0) {
56 return true;
57 }
58 return false;
59 }
60
61 private synchronized void getPercentages () {
62
63 BaseGroup [] groups = BaseGroup.makeBaseGroups(gCounts.length);
64
65 xCategories = new String[groups.length];
66
67
68 double [] gPercent = new double[groups.length];
69 double [] aPercent = new double[groups.length];
70 double [] tPercent = new double[groups.length];
71 double [] cPercent = new double[groups.length];
72
73 long total;
74 long gCount;
75 long aCount;
76 long tCount;
77 long cCount;
78
79 for (int i=0;i<groups.length;i++) {
80
81 xCategories[i] = groups[i].toString();
82
83 gCount = 0;
84 aCount = 0;
85 tCount = 0;
86 cCount = 0;
87 total = 0;
88
89 for (int bp=groups[i].lowerCount()-1;bp<groups[i].upperCount();bp++) {
90
91 total += gCounts[bp];
92 total += cCounts[bp];
93 total += aCounts[bp];
94 total += tCounts[bp];
95
96 aCount += aCounts[bp];
97 tCount += tCounts[bp];
98 cCount += cCounts[bp];
99 gCount += gCounts[bp];
100 }
101
102 gPercent[i] = (gCount/(double)total)*100;
103 aPercent[i] = (aCount/(double)total)*100;
104 tPercent[i] = (tCount/(double)total)*100;
105 cPercent[i] = (cCount/(double)total)*100;
106
107 }
108
109 percentages = new double [][] {tPercent,cPercent,aPercent,gPercent};
110
111 calculated = true;
112 }
113
114 public void processSequence(Sequence sequence) {
115 calculated = false;
116 char [] seq = sequence.getSequence().toCharArray();
117 if (gCounts.length < seq.length) {
118
119 long [] gCountsNew = new long [seq.length];
120 long [] aCountsNew = new long [seq.length];
121 long [] cCountsNew = new long [seq.length];
122 long [] tCountsNew = new long [seq.length];
123
124 for (int i=0;i<gCounts.length;i++) {
125 gCountsNew[i] = gCounts[i];
126 aCountsNew[i] = aCounts[i];
127 tCountsNew[i] = tCounts[i];
128 cCountsNew[i] = cCounts[i];
129 }
130
131 gCounts = gCountsNew;
132 aCounts = aCountsNew;
133 tCounts = tCountsNew;
134 cCounts = cCountsNew;
135 }
136
137 for (int i=0;i<seq.length;i++) {
138 if (seq[i] == 'G') {
139 ++gCounts[i];
140 }
141 else if (seq[i] == 'A') {
142 ++aCounts[i];
143 }
144 else if (seq[i] == 'T') {
145 ++tCounts[i];
146 }
147 else if (seq[i] == 'C') {
148 ++cCounts[i];
149 }
150 }
151
152 }
153
154 public void reset () {
155 gCounts = new long[0];
156 aCounts = new long[0];
157 tCounts = new long[0];
158 cCounts = new long[0];
159 }
160
161 public String description() {
162 return "Shows the relative amounts of each base at each position in a sequencing run";
163 }
164
165 public String name() {
166 return "Per base sequence content";
167 }
168
169 public boolean raisesError() {
170 if (!calculated) getPercentages();
171
172 // Percentages come in the order TCAG
173 for (int i=0;i<percentages[0].length;i++) {
174
175 double gcDiff = Math.abs(percentages[1][i]-percentages[3][i]);
176 double atDiff = Math.abs(percentages[0][i]-percentages[2][i]);
177
178 if (gcDiff > ModuleConfig.getParam("sequence", "error") || atDiff > ModuleConfig.getParam("sequence", "error")) return true;
179
180 }
181 return false;
182 }
183
184 public boolean raisesWarning() {
185
186 if (!calculated) getPercentages();
187
188 // Percentages come in the order TCAG
189 for (int i=0;i<percentages[0].length;i++) {
190
191 double gcDiff = Math.abs(percentages[1][i]-percentages[3][i]);
192 double atDiff = Math.abs(percentages[0][i]-percentages[2][i]);
193
194 if (gcDiff > ModuleConfig.getParam("sequence", "warn") || atDiff > ModuleConfig.getParam("sequence", "warn")) return true;
195
196 }
197 return false;
198 }
199
200 public void makeReport(HTMLReportArchive report) throws IOException,XMLStreamException {
201
202 if (!calculated) getPercentages();
203
204 writeDefaultImage(report, "per_base_sequence_content.png", "Per base sequence content", Math.max(800, xCategories.length*15), 600);
205
206 StringBuffer sb = report.dataDocument();
207 sb.append("#Base\tG\tA\tT\tC\n");
208 for (int i=0;i<xCategories.length;i++) {
209 sb.append(xCategories[i]);
210 sb.append("\t");
211 sb.append(percentages[3][i]);
212 sb.append("\t");
213 sb.append(percentages[2][i]);
214 sb.append("\t");
215 sb.append(percentages[0][i]);
216 sb.append("\t");
217 sb.append(percentages[1][i]);
218 sb.append("\n");
219 }
220
221 }
222
223 }