Debian Codegraph @

uk/ac/babraham/FastQC/Analysis/OfflineRunner.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.Analysis;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Vector;
25 import java.util.concurrent.atomic.AtomicInteger;
26
27 import uk.ac.babraham.FastQC.FastQCConfig;
28 import uk.ac.babraham.FastQC.Modules.ModuleFactory;
29 import uk.ac.babraham.FastQC.Modules.QCModule;
30 import uk.ac.babraham.FastQC.Report.HTMLReportArchive;
31 import uk.ac.babraham.FastQC.Sequence.SequenceFactory;
32 import uk.ac.babraham.FastQC.Sequence.SequenceFile;
33 import uk.ac.babraham.FastQC.Utilities.CasavaBasename;
34 import uk.ac.babraham.FastQC.Utilities.NanoporeBasename;
35
36 public class OfflineRunner implements AnalysisListener {
37
38 private AtomicInteger filesRemaining;
39 private boolean showUpdates = true;
40
41 // We'll set a flag which will tell us if any of the sequences failed
42 // so we can exit with an error state so the calling process can tell
43 // that something went wrong.
44
45 private boolean somethingFailed = false;
46
47 public OfflineRunner (String [] filenames) {
48
49 // See if we need to show updates
50 showUpdates = !FastQCConfig.getInstance().quiet;
51
52 Vector<File> files = new Vector<File>();
53
54 // We make a special case if they supply a single filename
55 // which is stdin. In this case we'll take data piped to us
56 // rather than trying to read the actual file. We'll also
57 // skip the existence check.
58
59 if (filenames.length == 1 && filenames[0].startsWith("stdin")) {
60 files.add(new File(filenames[0]));
61 }
62 else {
63 for (int f=0;f<filenames.length;f++) {
64 File file = new File(filenames[f]);
65
66 if (!file.exists() || ! file.canRead()) {
67 System.err.println("Skipping '"+filenames[f]+"' which didn't exist, or couldn't be read");
68 continue;
69 }
70
71 if (FastQCConfig.getInstance().nano && file.isDirectory()) {
72 File [] fast5files = file.listFiles();
73 for (int i=0;i<fast5files.length;i++) {
74 if (fast5files[i].getName().endsWith(".fast5")) {
75 files.add(fast5files[i]);
76 }
77 }
78
79 // In newer nanopore software instances the fast5 files are
80 // put into subdirectories of the main one specified so we
81 // also need to look into those as well.
82 for (int i=0;i<fast5files.length;i++) {
83 if (fast5files[i].isDirectory()) {
84 File [] subFast5files = fast5files[i].listFiles();
85
86 for (int j=0;j<subFast5files.length;j++) {
87 if (subFast5files[j].getName().endsWith(".fast5")) {
88 files.add(subFast5files[j]);
89 }
90 }
91
92 }
93 }
94
95 }
96 else {
97 files.add(file);
98 }
99 }
100 }
101
102
103 File [][] fileGroups;
104
105 // See if we need to group together files from a casava group
106 if (FastQCConfig.getInstance().casava) {
107 fileGroups = CasavaBasename.getCasavaGroups(files.toArray(new File[0]));
108 }
109 else if (FastQCConfig.getInstance().nano) {
110 fileGroups = NanoporeBasename.getNanoporeGroups(files.toArray(new File[0]));
111 }
112 else {
113 fileGroups = new File [files.size()][1];
114 for (int f=0;f<files.size();f++) {
115 fileGroups[f][0] = files.elementAt(f);
116 }
117 }
118
119
120 filesRemaining = new AtomicInteger(fileGroups.length);
121
122 for (int i=0;i<fileGroups.length;i++) {
123
124 try {
125 processFile(fileGroups[i]);
126 }
127 catch (OutOfMemoryError e) {
128 System.err.println("Ran out of memory for "+fileGroups[i][0]);
129 e.printStackTrace();
130 System.exit(2);
131 }
132 catch (Exception e) {
133 System.err.println("Failed to process "+fileGroups[i][0]);
134 e.printStackTrace();
135 filesRemaining.decrementAndGet();
136 somethingFailed = true;
137 }
138
139
140 }
141
142 // We need to hold this class open as otherwise the main method
143 // exits when it's finished.
144 while (filesRemaining.intValue() > 0) {
145 try {
146 Thread.sleep(1000);
147 }
148 catch (InterruptedException e) {}
149 }
150 if (somethingFailed) {
151 System.exit(1);
152 }
153 System.exit(0);
154
155 }
156
157 public void processFile (File [] files) throws Exception {
158 for (int f=0;f<files.length;f++) {
159 if (!files[f].getName().startsWith("stdin") && !files[f].exists()) {
160 throw new IOException(files[f].getName()+" doesn't exist");
161 }
162 }
163 SequenceFile sequenceFile = SequenceFactory.getSequenceFile(files);
164
165 AnalysisRunner runner = new AnalysisRunner(sequenceFile);
166 runner.addAnalysisListener(this);
167
168 QCModule [] module_list = ModuleFactory.getStandardModuleList();
169
170 runner.startAnalysis(module_list);
171
172 }
173
174 public void analysisComplete(SequenceFile file, QCModule[] results) {
175 File reportFile;
176
177 if (showUpdates) System.out.println("Analysis complete for "+file.name());
178
179
180 if (FastQCConfig.getInstance().output_dir != null) {
181 String fileName = file.getFile().getName().replaceAll("stdin:","").replaceAll("\\.gz$","").replaceAll("\\.bz2$","").replaceAll("\\.txt$","").replaceAll("\\.fastq$", "").replaceAll("\\.fq$", "").replaceAll("\\.csfastq$", "").replaceAll("\\.sam$", "").replaceAll("\\.bam$", "").replaceAll("\\.ubam$", "")+"_fastqc.html";
182 reportFile = new File(FastQCConfig.getInstance().output_dir+"/"+fileName);
183 }
184 else {
185 reportFile = new File(file.getFile().getAbsolutePath().replaceAll("stdin:","").replaceAll("\\.gz$","").replaceAll("\\.bz2$","").replaceAll("\\.txt$","").replaceAll("\\.fastq$", "").replaceAll("\\.fq$", "").replaceAll("\\.csfastq$", "").replaceAll("\\.sam$", "").replaceAll("\\.bam$", "").replaceAll("\\.ubam$", "")+"_fastqc.html");
186 }
187
188 try {
189 new HTMLReportArchive(file, results, reportFile);
190 }
191 catch (Exception e) {
192 analysisExceptionReceived(file, e);
193 return;
194 }
195 filesRemaining.decrementAndGet();
196
197 }
198
199 public void analysisUpdated(SequenceFile file, int sequencesProcessed, int percentComplete) {
200
201 if (percentComplete % 5 == 0) {
202 if (percentComplete == 105) {
203 if (showUpdates) System.err.println("It seems our guess for the total number of records wasn't very good. Sorry about that.");
204 }
205 if (percentComplete > 100) {
206 if (showUpdates) System.err.println("Still going at "+percentComplete+"% complete for "+file.name());
207 }
208 else {
209 if (showUpdates) System.err.println("Approx "+percentComplete+"% complete for "+file.name());
210 }
211 }
212 }
213
214 public void analysisExceptionReceived(SequenceFile file, Exception e) {
215 System.err.println("Failed to process file "+file.name());
216 somethingFailed = true;
217 e.printStackTrace();
218 filesRemaining.decrementAndGet();
219 }
220
221 public void analysisStarted(SequenceFile file) {
222 if (showUpdates) System.err.println("Started analysis of "+file.name());
223
224 }
225
226 }