forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFastqcRunner.java
More file actions
461 lines (388 loc) · 14.7 KB
/
FastqcRunner.java
File metadata and controls
461 lines (388 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* Copyright (c) 2012 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.sequenceanalysis.run.util;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.junit.Assert;
import org.junit.Test;
import org.labkey.api.module.Module;
import org.labkey.api.module.ModuleLoader;
import org.labkey.api.pipeline.PipelineJobService;
import org.labkey.api.resource.DirectoryResource;
import org.labkey.api.resource.FileResource;
import org.labkey.api.resource.Resource;
import org.labkey.api.sequenceanalysis.pipeline.SequencePipelineService;
import org.labkey.api.settings.AppProps;
import org.labkey.api.util.Compress;
import org.labkey.api.util.FileType;
import org.labkey.api.util.FileUtil;
import org.labkey.api.util.Path;
import org.labkey.api.util.StringUtilsLabKey;
import org.labkey.sequenceanalysis.SequenceAnalysisModule;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.GZIPInputStream;
/**
* User: bbimber
* Date: 4/23/12
* Time: 2:02 PM
*/
// Handles the basics of testing the Fastqc configuration, creating the fastqc command line, and invoking fastqc. This code
// is patterned after DotRunner.
public class FastqcRunner
{
private final Logger _logger;
private int _threads = 1;
private boolean _cacheResults = true;
public FastqcRunner(@Nullable Logger log)
{
if (log == null)
{
_logger = LogManager.getLogger(FastqcRunner.class);
}
else
{
_logger = log;
}
}
public void setCacheResults(boolean cacheResults)
{
_cacheResults = cacheResults;
}
public String execute(List<File> sequenceFiles, @Nullable Map<File, String> fileLabels) throws IOException
{
//remove duplicates
List<File> uniqueFiles = new ArrayList<>();
for (File f : sequenceFiles)
{
if (!uniqueFiles.contains(f))
uniqueFiles.add(f);
}
Set<File> filesCreated = new HashSet<>();
for (File f : uniqueFiles)
{
//first see if we have cached HTML, otherwise run
File expectedHtml = getExpectedHtmlFile(f);
File zip = getExpectedZipFile(f);
if (!expectedHtml.exists() || !zip.exists())
{
runForFile(f);
if (zip.exists())
{
_logger.info("adding ZIP: " + zip.getPath());
filesCreated.add(zip);
}
else
{
throw new IOException("ZIP file not found, expected: " + zip.getPath());
}
//force compression
getExpectedHtmlFile(f);
filesCreated.add(expectedHtml);
}
else
{
_logger.debug("Existing FASTQC output found, will not re-run");
}
}
return processOutput(uniqueFiles, filesCreated, fileLabels);
}
private void runForFile(File f)
{
try
{
List<String> params = getParams(f);
params.add(f.getAbsolutePath());
_logger.info("running fastqc:");
_logger.info(StringUtils.join(params, " "));
ProcessBuilder pb = new ProcessBuilder(params);
pb.redirectErrorStream(true);
pb.directory(f.getParentFile());
Process p = pb.start();
try (BufferedReader procReader = new BufferedReader(new InputStreamReader(p.getInputStream(), StringUtilsLabKey.DEFAULT_CHARSET)))
{
String line;
while ((line = procReader.readLine()) != null)
{
_logger.info(line);
}
int returnCode = p.waitFor();
if (returnCode != 0)
{
throw new IOException("FastQC failed with error code " + returnCode);
}
}
catch (Exception e)
{
_logger.error(e.getMessage(), e);
throw new RuntimeException("Failed writing output for process in '" + (pb != null && pb.directory() != null ? pb.directory().getPath() : "") + "'.", e);
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public String getExpectedBasename(File f)
{
String basename = FileUtil.getBaseName(f);
FileType gz = new FileType(".gz");
if (gz.isType(f))
{
basename = FileUtil.getBaseName(basename);
}
return basename;
}
public File getExpectedZipFile(File f)
{
File expectedHtml = getExpectedHtmlFile(f);
return new File(expectedHtml.getParentFile(), FileUtil.getBaseName(FileUtil.getBaseName(expectedHtml)) + ".zip");
}
public File getExpectedHtmlFile(File f)
{
File uncompressed = new File(f.getParentFile().getAbsolutePath(), getExpectedBasename(f) + "_fastqc.html");
//to handle legacy installs with existing uncompressed files
File compressed = new File(uncompressed.getPath() + ".gz");
if (uncompressed.exists())
{
_logger.info("compressing existing file: " + uncompressed.getPath());
Compress.compressGzip(uncompressed, compressed);
uncompressed.delete();
}
return compressed;
}
private String processOutput(List<File> inputFiles, Set<File> filesCreated, @Nullable Map<File, String> fileLabels)
{
//NOTE: this allows remote servers to run/cache the data. AppProps.getContextPath() will fail remotely, so abort.
if (PipelineJobService.get().getLocationType() != PipelineJobService.LocationType.WebServer)
{
return "";
}
StringBuilder output = new StringBuilder();
StringBuilder header = new StringBuilder("<div class=\"fastqc_overview\"><h2>File Summary:</h2><ul>");
try
{
String delim = "";
String css = AppProps.getInstance().getContextPath() + "/SequenceAnalysis/fastqc.css";
int counter = 0;
for (File f : inputFiles)
{
File htmlFile = getExpectedHtmlFile(f);
if (!htmlFile.exists())
{
output.append("<p>Unable to find output for file: ").append(f.getName()).append("</p>");
continue;
}
String html = readCompressedHtmlReport(htmlFile);
//add an outer DIV so we can apply styles only to the report
html = html.replaceAll("<body>", "<div class=\"fastqc\">");
html = html.replaceAll("</body>", "</div>");
//update IDs so links will point to correct file after concatenated:
String suffix = f.getName().replaceAll("\\.", "_");
String title;
if (fileLabels == null || !fileLabels.containsKey(f))
{
title = f.getName();
}
else
{
title = fileLabels.get(f) + " (" + f.getName() + ")";
}
html = html.replaceAll("<h2>Summary</h2>", "<h2 id=\"report_" + suffix + "\">Overview: " + title + "</h2>");
for (int i=0;i < 10;i++)
{
html = html.replaceAll("#M" + i, "#M" + i + "_" + suffix);
html = html.replaceAll("id=\"M" + i, "id=\"M" + i + "_" + suffix);
}
//only load the new CSS file for the 1st file
html = html.replaceAll("@@css@@", css);
css = "";
html = html.replaceAll("<link href=\"\" type=\"text/css\" rel=\"stylesheet\">\n", "");
output.append(delim).append(html);
delim = "<hr>";
//also build a header:
header.append("<li><a href=\"#report_").append(suffix).append("\">").append(title).append("</a></li>");
//remove footer except on final file
if (counter < inputFiles.size() - 1)
{
output = new StringBuilder(output.toString().replaceAll("<div class=\"footer\">.*</div>", ""));
}
counter++;
}
if (inputFiles.size() > 1)
{
header.append("</ul><p /></div><hr>");
String tag = "<div class=\"fastqc\">";
output = new StringBuilder(output.toString().replace(tag, tag + header));
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
if (!_cacheResults)
{
for (File f : filesCreated)
{
_logger.debug("deleting fastcq file: " + f.getPath());
f.delete();
}
}
return output.toString();
}
private String readCompressedHtmlReport(File htmlFile) throws IOException
{
StringWriter writer = new StringWriter();
try (InputStream is = new GZIPInputStream(new FileInputStream(htmlFile)))
{
IOUtils.copy(is, writer, StringUtilsLabKey.DEFAULT_CHARSET);
}
return writer.toString();
}
private File lookupFile(String path) throws FileNotFoundException
{
Module module = ModuleLoader.getInstance().getModule(SequenceAnalysisModule.class);
DirectoryResource resource = (DirectoryResource)module.getModuleResolver().lookup(Path.parse(path));
assert resource != null : "Unable to find resource with path: " + path;
File file = null;
for (Resource r : resource.list())
{
if (r instanceof FileResource)
{
file = ((FileResource) r).getFile().getParentFile();
break;
}
}
if (file == null)
throw new FileNotFoundException("Not found: " + path);
if (!file.exists())
throw new FileNotFoundException("Not found: " + file.getPath());
return file;
}
private List<String> getBaseParams() throws FileNotFoundException
{
List<String> params = new LinkedList<>();
params.add(SequencePipelineService.get().getJavaFilepath());
int threads = getThreads();
Integer maxRam = SequencePipelineService.get().getMaxRam();
if (PipelineJobService.get().getLocationType() != PipelineJobService.LocationType.WebServer && maxRam != null)
{
params.add("-Xmx" + maxRam + "g");
}
else
{
params.add("-Xmx" + (250 * threads) + "m");
}
if (threads > 1)
{
params.add("-Dfastqc.threads=" + threads);
}
File libDir = new File(ModuleLoader.getInstance().getModule(SequenceAnalysisModule.NAME).getExplodedPath(), "lib");
File fastqcDir = new File(libDir.getParentFile(), "external/fastqc");
File jbzip2 = new File(libDir, "bzip2-0.9.1.jar");
if (!jbzip2.exists())
{
throw new RuntimeException("Not found: " + jbzip2.getPath());
}
File htsjdkJar = findJar(libDir, "htsjdk-");
if (!htsjdkJar.exists())
{
throw new RuntimeException("Not found: " + htsjdkJar.getPath());
}
File apiLibDir = new File(ModuleLoader.getInstance().getModule("api").getExplodedPath(), "lib");
File commonsMath = new File(apiLibDir, "commons-math3-3.6.1.jar");
if (!commonsMath.exists())
{
throw new RuntimeException("Not found: " + commonsMath.getPath());
}
File jhdf5 = new File(libDir, "jhdf5-19.04.1.jar");
if (!jhdf5.exists())
{
throw new RuntimeException("Not found: " + jhdf5.getPath());
}
List<String> classPath = new ArrayList<>();
classPath.add(".");
classPath.add(fastqcDir.getPath());
classPath.add(htsjdkJar.getPath());
classPath.add(jbzip2.getPath());
classPath.add(commonsMath.getPath());
classPath.add(jhdf5.getPath());
params.add("-classpath");
params.add(StringUtils.join(classPath, File.pathSeparator));
params.add("-Djava.awt.headless=true");
return params;
}
private File findJar(final File libDir, final String prefix)
{
if (!libDir.exists())
{
throw new RuntimeException("Missing directory: " + libDir);
}
List<String> jarNames = Arrays.stream(libDir.list()).filter(fn -> fn.startsWith(prefix)).sorted().toList();
if (jarNames.isEmpty())
{
throw new RuntimeException("Unable to find JAR with prefix: " + prefix);
}
if (jarNames.size() > 1)
{
_logger.info("More than one JAR found with prefix: " + prefix);
}
return new File(libDir, jarNames.get(jarNames.size() - 1));
}
private int getThreads()
{
return _threads;
}
public void setThreads(int threads)
{
_threads = threads;
}
private List<String> getParams(File f) throws FileNotFoundException
{
List<String> params = getBaseParams();
params.add("-Dfastqc.output_dir=" + f.getParentFile().getAbsolutePath());
//params.add("-Dfastqc.quiet=true");
params.add("uk.ac.babraham.FastQC.FastQCApplication");
return params;
}
public static class TestCase extends Assert
{
@Test
public void testApacheJar() throws Exception
{
//This will error if JARs are not found:
FastqcRunner runner = new FastqcRunner(null);
List<String> params = runner.getBaseParams();
assertEquals("Incorrect params", 5, params.size());
}
}
}