Debian Codegraph @

uk/ac/babraham/FastQC/Sequence/FastQFile.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.Sequence;
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.nio.file.Files;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31 import org.itadaki.bzip2.BZip2InputStream;
32
33 import uk.ac.babraham.FastQC.FastQCConfig;
34 import uk.ac.babraham.FastQC.Utilities.MultiMemberGZIPInputStream;
35
36 public class FastQFile implements SequenceFile {
37
38 private Sequence nextSequence = null;
39 private File file;
40 private long fileSize = 0;
41
42 private boolean casavaMode = false;
43 private boolean nofilter = false;
44
45 // We actually read our final data from this buffered reader
46 private BufferedReader br;
47 // We'll keep count of the number of lines read for the error message
48 private long lineNumber = 0;
49
50 // We keep the file stream around just so we can see how far through
51 // the file we've got. We don't read from this directly, but it's the
52 // only way to access the file pointer.
53 private FileInputStream fis;
54
55 private String name;
56 private boolean isColorspace = false;
57
58 protected FastQFile(FastQCConfig config,File file) throws SequenceFormatException, IOException {
59 this.file = file;
60 if (file.getName().startsWith("stdin")) {
61 fileSize = Long.MAX_VALUE;
62 }
63 else {
64 fileSize = file.length();
65 }
66 name = file.getName();
67
68 if (config.casava) {
69 casavaMode = true;
70 if (config.nofilter) {
71 nofilter = true;
72 }
73 }
74
75 System.out.println(Files.probeContentType(file.toPath()));
76 if (!file.getName().startsWith("stdin")) {
77 fis = new FileInputStream(file);
78 }
79
80 if (file.getName().startsWith("stdin")) {
81 br = new BufferedReader(new InputStreamReader(System.in));
82 }
83 else if (file.getName().toLowerCase().endsWith(".gz") || (Files.probeContentType(file.toPath()) != null && (Files.probeContentType(file.toPath()).equals("application/x-gzip") || Files.probeContentType(file.toPath()).equals("application/gzip")))) {
84 br = new BufferedReader(new InputStreamReader(new MultiMemberGZIPInputStream(fis)));
85 }
86 else if (file.getName().toLowerCase().endsWith(".bz2")) {
87 br = new BufferedReader(new InputStreamReader(new BZip2InputStream(fis,false)));
88 }
89
90 else {
91 br = new BufferedReader(new InputStreamReader(fis));
92 }
93 readNext();
94 }
95
96 public String name() {
97 return name;
98 }
99
100 public int getPercentComplete() {
101 if (! hasNext()) return 100;
102 if (file.getName().startsWith("stdin")) {
103 return 0;
104 }
105 try {
106 int percent = (int) (((double)fis.getChannel().position()/ fileSize)*100);
107 return percent;
108 }
109 catch (IOException e) {
110 e.printStackTrace();
111 }
112 return 0;
113 }
114
115 public boolean isColorspace() {
116 return isColorspace;
117 }
118
119 public void setIsColorspace(boolean isColorspace) {
120 this.isColorspace = isColorspace;
121 }
122
123 public boolean hasNext() {
124 return nextSequence != null;
125 }
126
127 public Sequence next() throws SequenceFormatException {
128 Sequence seq = nextSequence;
129 readNext();
130 return seq;
131 }
132
133 private void readNext() throws SequenceFormatException {
134 try {
135 // First line should be the id
136
137 // We might have blank lines between entries or at the end
138 // so allow for this
139 String id;
140
141 while (true) {
142 id = br.readLine();
143 ++lineNumber;
144
145 if (id == null) {
146 nextSequence = null;
147 br.close();
148 if (fis != null) {
149 fis.close();
150 }
151 return;
152 }
153 if (id.length()==0) {
154 continue;
155 }
156
157 break;
158 }
159
160
161 if (!id.startsWith("@")) {
162 nextSequence = null;
163 throw new SequenceFormatException("ID line didn't start with '@' at line "+lineNumber);
164 }
165
166 String seq;
167 String midLine;
168 String quality;
169 try {
170 // Then the sequence
171 seq = br.readLine();
172 if (seq == null) throw new IOException("No more data, expected sequence, at line "+lineNumber);
173 ++lineNumber;
174 // Then another id which we don't need
175 midLine = br.readLine();
176 if (midLine == null) throw new IOException("No more data, expected midline, at line "+lineNumber);
177 ++lineNumber;
178 if (!midLine.startsWith("+")) {
179 throw new SequenceFormatException("Midline '"+midLine+"' didn't start with '+' at "+lineNumber);
180 }
181 // Then the quality string
182 quality = br.readLine();
183 if (quality == null) throw new IOException("No more data, expected quality, at line "+lineNumber);
184 ++lineNumber;
185 }
186 catch (IOException ioe) {
187 throw new SequenceFormatException("Ran out of data in the middle of a fastq entry. Your file is probably truncated");
188 }
189
190
191 // We only check for colourspace on the first entry. After that we assume
192 // the rest of the file is the same. For the first entry the nextSequence
193 // will be null, but we'll have real data in seq
194 if (nextSequence == null && seq != null) {
195 checkColorspace(seq);
196 }
197
198 if (isColorspace()) {
199 nextSequence = new Sequence(this,convertColorspaceToBases(seq.toUpperCase()), seq.toUpperCase(), quality, id);
200 }
201 else {
202 nextSequence = new Sequence(this, seq.toUpperCase(),quality, id);
203 }
204
205 // If we're running in --casava mode then we will flag any sequences which
206 // are marked as being filtered.
207 if (casavaMode && !nofilter) {
208
209 // This is the test illumina suggest, but it's a bit flakey, and I'm not
210 // sure it's not going to catch things it shouldn't.
211 if (id.indexOf(":Y:") > 0) {
212 nextSequence.setIsFiltered(true);
213 }
214 }
215
216
217
218
219 }
220 // We could have other errors like zip format exceptions which we need to catch
221 catch (IOException ioe) {
222 throw new SequenceFormatException(ioe.getLocalizedMessage());
223 }
224 }
225
226 private void checkColorspace(String seq) {
227 // Some basecalled files can be all dots, which leads to them
228 // being identified as colorspace data. This check should find
229 // only true colorspace files.
230 String regex = "^[GATCNgatcn][\\.0123456]+$";
231 Pattern pattern = Pattern.compile(regex);
232 Matcher matcher = pattern.matcher(seq);
233 if (matcher.find()) {
234 isColorspace = true;
235 } else {
236 isColorspace = false;
237 }
238 }
239
240 private String convertColorspaceToBases(String s) {
241
242 char[] cs = s.toUpperCase().toCharArray();
243
244 // We've had a crash report where a file contained a zero length
245 // colorspace entry. This is completely invalid, but we should
246 // handle it anyway.
247 if (cs.length == 0) {
248 return "";
249 }
250
251 char[] bp = new char[cs.length - 1];
252
253 char refBase;
254
255 for (int i = 1; i < cs.length; i++) {
256 if (i == 1) {
257 refBase = cs[i - 1];
258 } else {
259 refBase = bp[i - 2];
260 }
261 if (!(refBase == 'G' || refBase == 'A' || refBase == 'T' || refBase == 'C')) {
262 throw new IllegalArgumentException("Colourspace sequence data should always start with a real DNA letter. Line '"+s+"' started with " + refBase
263 + " at position " + i);
264 }
265 switch (cs[i]) {
266 case ('0'):
267 switch (refBase) {
268 case ('G'):
269 bp[i - 1] = 'G';
270 break;
271 case ('A'):
272 bp[i - 1] = 'A';
273 break;
274 case ('T'):
275 bp[i - 1] = 'T';
276 break;
277 case ('C'):
278 bp[i - 1] = 'C';
279 break;
280 }
281 break;
282 case ('1'):
283 switch (refBase) {
284 case ('G'):
285 bp[i - 1] = 'T';
286 break;
287 case ('A'):
288 bp[i - 1] = 'C';
289 break;
290 case ('T'):
291 bp[i - 1] = 'G';
292 break;
293 case ('C'):
294 bp[i - 1] = 'A';
295 break;
296 }
297 break;
298
299 case ('2'):
300 switch (refBase) {
301 case ('G'):
302 bp[i - 1] = 'A';
303 break;
304 case ('A'):
305 bp[i - 1] = 'G';
306 break;
307 case ('T'):
308 bp[i - 1] = 'C';
309 break;
310 case ('C'):
311 bp[i - 1] = 'T';
312 break;
313 }
314 break;
315
316 case ('3'):
317 switch (refBase) {
318 case ('G'):
319 bp[i - 1] = 'C';
320 break;
321 case ('A'):
322 bp[i - 1] = 'T';
323 break;
324 case ('T'):
325 bp[i - 1] = 'A';
326 break;
327 case ('C'):
328 bp[i - 1] = 'G';
329 break;
330 }
331 break;
332
333 case ('.'):
334 case ('4'):
335 case ('5'):
336 case ('6'):
337 for (; i < cs.length; i++) {
338 bp[i - 1] = 'N';
339 }
340 break;
341 default:
342 throw new IllegalArgumentException("Unexpected cs char "
343 + cs[i]);
344 }
345 }
346
347 return new String(bp);
348 }
349
350 public void remove() {
351 // No action here
352 }
353
354 public File getFile() {
355 return file;
356 }
357
358 }