forked from marbl/canu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canu.pl
491 lines (350 loc) · 15.2 KB
/
canu.pl
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/usr/bin/env perl
###############################################################################
#
# This file is part of canu, a software program that assembles whole-genome
# sequencing reads into contigs.
#
# This software is based on:
# 'Celera Assembler' (http://wgs-assembler.sourceforge.net)
# the 'kmer package' (http://kmer.sourceforge.net)
# both originally distributed by Applera Corporation under the GNU General
# Public License, version 2.
#
# Canu branched from Celera Assembler at its revision 4587.
# Canu branched from the kmer project at its revision 1994.
#
# This file is derived from:
#
# src/pipelines/ca3g.pl
#
# Modifications by:
#
# Brian P. Walenz from 2015-FEB-27 to 2015-AUG-26
# are Copyright 2015 Battelle National Biodefense Institute, and
# are subject to the BSD 3-Clause License
#
# Brian P. Walenz beginning on 2015-NOV-03
# are a 'United States Government Work', and
# are released in the public domain
#
# File 'README.licenses' in the root directory of this distribution contains
# full conditions and disclaimers for each license.
##
use strict;
use FindBin;
use Cwd;
use lib "$FindBin::RealBin/lib";
use lib "$FindBin::RealBin/lib/canu/lib/perl5";
use lib "$FindBin::RealBin/lib/canu/lib64/perl5";
use File::Path qw(make_path remove_tree);
use Carp;
use canu::Defaults;
use canu::Execution;
use canu::Configure;
use canu::Grid;
use canu::Grid_SGE;
use canu::Grid_Slurm;
use canu::Grid_PBSTorque;
use canu::Grid_LSF;
use canu::Gatekeeper;
use canu::Meryl;
use canu::OverlapInCore;
use canu::OverlapMhap;
use canu::OverlapStore;
use canu::CorrectReads;
use canu::OverlapBasedTrimming;
use canu::OverlapErrorAdjustment;
use canu::Unitig;
use canu::Consensus;
use canu::Output;
use canu::HTML;
my @specFiles; # Files of specs
my @specOpts; # Command line specs
my @inputFiles; # Command line inputs, later inputs in spec files are added
# Initialize our defaults. Must be done before defaults are reported in printOptions() below.
setDefaults();
# The bin directory is needed for -version, can only be set after setDefaults(), but really should be
# set after checkParameters() so it can know pathMap.
my $bin = getBinDirectory(); # Path to binaries, reset later.
my $cmd = undef; # Temporary string passed to system().
my $wrk = undef; # Path to our assembly directory.
my $asm = undef; # Name of our assembly.
# Check for the presence of a -options switch BEFORE we do any work.
# This lets us print the default values of options.
foreach my $arg (@ARGV) {
if (($arg eq "-options") ||
($arg eq "-defaults")) {
printOptions();
exit(0);
}
if (($arg eq "-version") ||
($arg eq "--version")) {
system("$bin/gatekeeperCreate --version");
exit(0);
}
}
# By default, all three steps are run. Options -correct, -trim and -assemble
# can limit the pipeline to just that stage.
# At some pain, we stash the original options for later use. We need
# to use these when we resubmit ourself to the grid. We can't simply dump
# all of @ARGV into here, because we need to fix up relative paths first.
my $mode = undef;
my $step = "run";
my $haveRaw = 0;
my $haveCorrected = 0;
while (scalar(@ARGV)) {
my $arg = shift @ARGV;
if ($arg =~ m/^-d/) {
$wrk = shift @ARGV;
$wrk = "$ENV{'PWD'}/$wrk" if ($wrk !~ m!^/!);
addCommandLineOption("-d \"$wrk\"");
} elsif ($arg eq "-p") {
$asm = shift @ARGV;
addCommandLineOption("-p \"$asm\"");
} elsif ($arg eq "-s") {
my $spec = shift @ARGV;
$spec = "$ENV{'PWD'}/$spec" if ($spec !~ m!^/!);
push @specFiles, $spec;
addCommandLineOption("-s \"$spec\"");
} elsif ($arg eq "-correct") {
$mode = $step = "correct";
addCommandLineOption("-correct");
} elsif ($arg eq "-trim") {
$mode = $step = "trim";
addCommandLineOption("-trim");
} elsif ($arg eq "-assemble") {
$mode = $step = "assemble";
addCommandLineOption("-assemble");
} elsif ($arg eq "-trim-assemble") {
$mode = $step = "trim-assemble";
addCommandLineOption("-trim-assemble");
} elsif (($arg eq "-pacbio-raw") ||
($arg eq "-pacbio-corrected") ||
($arg eq "-nanopore-raw") ||
($arg eq "-nanopore-corrected")) {
if ($arg =~ m/pacbio/) {
setErrorRate(0.025);
setGlobal("corErrorRate", "0.30");
setGlobal("batOptions", "-RS -NS -CS");
setGlobal("cnsMaxCoverage", 20);
} elsif ($arg =~ m/nanopore/) {
setErrorRate(0.045);
setGlobal("corErrorRate", "0.50");
setGlobal("batOptions", "-RS -NS -CS");
setGlobal("cnsMaxCoverage", 20);
}
$mode = "trim-assemble" if (!defined($mode) && ($arg =~ m/corrected/));
$mode = "run" if (!defined($mode) && ($arg =~ m/raw/));
$haveCorrected = 1 if ($arg =~ m/corrected/);
$haveRaw = 1 if ($arg =~ m/raw/);
addCommandLineError("ERROR: File '$ARGV[0]' not found.\n") if (! -e $ARGV[0]);
while (-e $ARGV[0]) {
my $file = shift @ARGV;
$file = "$ENV{'PWD'}/$file" if ($file !~ m!^/!);
push @inputFiles, "$arg:$file";
addCommandLineOption("$arg \"$file\"");
}
} elsif (-e $arg) {
addCommandLineError("ERROR: File supplied on command line; use -s, -pacbio-raw, -pacbio-corrected, -nanopore-raw, or -nanopore-corrected.\n");
} elsif ($arg =~ m/=/) {
push @specOpts, $arg;
addCommandLineOption("\"$arg\"");
} else {
addCommandLineError("ERROR: Invalid command line option '$arg'.\n");
}
}
# Fail if both raw and corrected are supplied.
addCommandLineError("ERROR: Canu does not currently support mixing raw and corrected sequences.\n") if ($haveRaw && $haveCorrected);
# Fail if some obvious things aren't set.
addCommandLineError("ERROR: Assembly name prefix not supplied with -p.\n") if (!defined($asm));
addCommandLineError("ERROR: Directory not supplied with -d.\n") if (!defined($wrk));
# If the mode isn't set - which is allowed only if a gkpStore exists somewhere - be a little smart
# and figure out which store exists.
$mode = "run" if (!defined($mode) && (-d "$wrk/correction/$asm.gkpStore"));
$mode = "trim-assemble" if (!defined($mode) && (-d "$wrk/trimming/$asm.gkpStore"));
$mode = "assemble" if (!defined($mode) && (-d "$wrk/unitigging/$asm.gkpStore"));
#@inputFiles = setParametersFromFile("$bin/spec/runCA.default.specFile", @inputFiles) if (-e "$bin/spec/runCA.default.specFile");
#@inputFiles = setParametersFromFile("$ENV{'HOME'}/.runCA", @inputFiles) if (-e "$ENV{'HOME'}/.runCA");
# For each of the spec files, parse it, setting parameters and remembering any input files discovered.
foreach my $specFile (@specFiles) {
@inputFiles = setParametersFromFile($specFile, @inputFiles);
}
# Set parameters from the command line.
setParametersFromCommandLine(@specOpts);
# Finish setting parameters, then reset the bin directory using pathMap.
checkParameters();
$bin = getBinDirectory();
# If anything complained (invalid option, missing file, etc) printHelp() will trigger and exit.
printHelp();
# Detect grid support. If 'gridEngine' isn't set, the execution methods submitScript() and
# submitOrRunParallelJob() will return without submitting, or run locally (respectively). This
# means that we can leave the default of 'useGrid' to 'true', and execution will do the right thing
# when there isn't a grid.
print STDERR "-- Detected ", getNumberOfCPUs(), " CPUs and ", getPhysicalMemorySize(), " gigabytes of memory.\n";
detectSGE();
detectSlurm();
detectPBSTorque();
detectLSF();
# Report if no grid engine found, or if the user has disabled grid support.
if (!defined(getGlobal("gridEngine"))) {
print STDERR "-- No grid engine detected, grid disabled.\n";
}
if ((getGlobal("useGrid") eq "0") && (defined(getGlobal("gridEngine")))) {
print STDERR "-- Grid engine disabled per useGrid=false option.\n";
setGlobal("gridEngine", undef);
}
# Finish setting up the grid. This is done AFTER parameters are set from the command line, to
# let the user override any of our defaults.
configureSGE();
configureSlurm();
configurePBSTorque();
configureLSF();
configureRemote();
# Based on genomeSize, configure the execution of every component. This needs to be done AFTER the grid is setup!
configureAssembler();
# Fail immediately if we run the script on the grid, and the gkpStore directory doesn't exist and
# we have no input files. Without this check we'd fail only after being scheduled on the grid.
my $cor = (-e "$wrk/correction/$asm.gkpStore") || (-e "$wrk/$asm.correctedReads.fastq") || (-e "$wrk/$asm.correctedReads.gkp");
my $obt = (-e "$wrk/trimming/$asm.gkpStore") || (-e "$wrk/$asm.trimmedReads.fastq") || (-e "$wrk/$asm.trimmedReads.gkp");
my $utg = (-e "$wrk/unitigging/$asm.gkpStore");
if (($cor + $obt + $utg == 0) &&
(scalar(@inputFiles) == 0)) {
caExit("no input files specified, and store not already created", undef);
}
# Check that we were supplied a work directory, and that it exists, or we can create it.
caExit("no run directory (-d option) specified", undef) if (!defined($wrk));
make_path("$wrk") if (! -d "$wrk");
make_path("$wrk/canu-logs") if (! -d "$wrk/canu-logs");
make_path("$wrk/canu-scripts") if (! -d "$wrk/canu-scripts");
caExit("run directory (-d option) '$wrk' doesn't exist and couldn't be created", undef) if (! -d $wrk);
# This environment variable tells the binaries to log their execution in canu-logs/
$ENV{'CANU_DIRECTORY'} = $wrk;
# Report the parameters used.
writeLog($wrk);
# Submit ourself for grid execution? If not grid enabled, or already running on the grid, this
# call just returns. The arg MUST be undef.
submitScript($wrk, $asm, undef);
#
# When doing 'run', this sets options for each stage.
# - overlapper 'mhap' for correction, 'ovl' for trimming and assembly.
# - consensus 'falconpipe' for correction, 'utgcns' for assembly. No consensus in trimming.
# - errorRates 15% for correction and 2% for trimming and assembly. Internally, this is
# multiplied by three for obt, ovl, cns, etc.
#
sub setOptions ($$) {
my $mode = shift @_; # E.g,. "run" or "trim-assemble" or just plain ol' "trim"
my $step = shift @_; # Step we're setting options for.
# Decide if we care about running this step in this mode. I almost applied
# De Morgan's Laws to this. I don't think it would have been any clearer.
if (($mode eq $step) ||
($mode eq "run") ||
(($mode eq "trim-assemble") && ($step eq "trim")) ||
(($mode eq "trim-assemble") && ($step eq "assemble"))) {
# Do run this.
} else {
return("don't run this");
}
# Create directories for the step, if needed.
make_path("$wrk/correction") if ((! -d "$wrk/correction") && ($step eq "correct"));
make_path("$wrk/trimming") if ((! -d "$wrk/trimming") && ($step eq "trim"));
make_path("$wrk/unitigging") if ((! -d "$wrk/unitigging") && ($step eq "assemble"));
# Return that we want to run this step.
return($step);
}
#
# Pipeline piece
#
sub overlap ($$$) {
my $wrk = shift @_;
my $asm = shift @_;
my $tag = shift @_;
my $ovlType = ($tag eq "utg") ? "normal" : "partial";
if (getGlobal("${tag}overlapper") eq "mhap") {
mhapConfigure($wrk, $asm, $tag, $ovlType);
mhapPrecomputeCheck($wrk, $asm, $tag, $ovlType) foreach (1..getGlobal("canuIterationMax"));
# this also does mhapReAlign
mhapCheck($wrk, $asm, $tag, $ovlType) foreach (1..getGlobal("canuIterationMax"));
} else {
overlapConfigure($wrk, $asm, $tag, $ovlType);
overlapCheck($wrk, $asm, $tag, $ovlType) foreach (1..getGlobal("canuIterationMax"));
}
createOverlapStore($wrk, $asm, $tag, getGlobal("ovlStoreMethod"));
}
#
# Begin pipeline
#
if (getGlobal("canuIteration") > 0) {
print STDERR "--\n";
print STDERR "-- This is canu parallel iteration #" . getGlobal("canuIteration") . ", out of a maximum of " . getGlobal("canuIterationMax") . " attempts.\n";
}
print STDERR "--\n";
print STDERR "-- Final error rates before starting pipeline:\n";
showErrorRates("-- ");
if (setOptions($mode, "correct") eq "correct") {
print STDERR "--\n";
print STDERR "--\n";
print STDERR "-- BEGIN CORRECTION\n";
print STDERR "--\n";
gatekeeper($wrk, $asm, "cor", @inputFiles);
merylConfigure($wrk, $asm, "cor");
merylCheck($wrk, $asm, "cor") foreach (1..getGlobal("canuIterationMax"));
merylProcess($wrk, $asm, "cor");
overlap($wrk, $asm, "cor");
buildCorrectionLayouts($wrk, $asm);
generateCorrectedReads($wrk, $asm) foreach (1..getGlobal("canuIterationMax"));
dumpCorrectedReads($wrk, $asm);
buildHTML($wrk, $asm, "cor");
my $correctedReads = sequenceFileExists("$wrk/$asm.correctedReads");
caExit("can't find corrected reads in '$wrk/$asm.correctedReads*'", undef) if (!defined($correctedReads));
undef @inputFiles;
push @inputFiles, "-pacbio-corrected:$correctedReads";
}
if (setOptions($mode, "trim") eq "trim") {
print STDERR "--\n";
print STDERR "--\n";
print STDERR "-- BEGIN TRIMMING\n";
print STDERR "--\n";
gatekeeper($wrk, $asm, "obt", @inputFiles);
merylConfigure($wrk, $asm, "obt");
merylCheck($wrk, $asm, "obt") foreach (1..getGlobal("canuIterationMax"));
merylProcess($wrk, $asm, "obt");
overlap($wrk, $asm, "obt");
trimReads ($wrk, $asm);
splitReads($wrk, $asm);
dumpReads ($wrk, $asm);
#summarizeReads($wrk, $asm);
buildHTML($wrk, $asm, "obt");
my $trimmedReads = sequenceFileExists("$wrk/$asm.trimmedReads");
caExit("can't find trimmed reads in '$wrk/$asm.trimmedReads*'", undef) if (!defined($trimmedReads));
undef @inputFiles;
push @inputFiles, "-pacbio-corrected:$trimmedReads";
}
if (setOptions($mode, "assemble") eq "assemble") {
print STDERR "--\n";
print STDERR "--\n";
print STDERR "-- BEGIN ASSEMBLY\n";
print STDERR "--\n";
gatekeeper($wrk, $asm, "utg", @inputFiles);
merylConfigure($wrk, $asm, "utg");
merylCheck($wrk, $asm, "utg") foreach (1..getGlobal("canuIterationMax"));
merylProcess($wrk, $asm, "utg");
overlap($wrk, $asm, "utg");
#readErrorDetection($wrk, $asm);
readErrorDetectionConfigure($wrk, $asm);
readErrorDetectionCheck($wrk, $asm) foreach (1..getGlobal("canuIterationMax"));
overlapErrorAdjustmentConfigure($wrk, $asm);
overlapErrorAdjustmentCheck($wrk, $asm) foreach (1..getGlobal("canuIterationMax"));
updateOverlapStore($wrk, $asm);
unitig($wrk, $asm);
unitigCheck($wrk, $asm) foreach (1..getGlobal("canuIterationMax"));
consensusConfigure($wrk, $asm);
consensusCheck($wrk, $asm) foreach (1..getGlobal("canuIterationMax"));
consensusLoad($wrk, $asm);
consensusAnalyze($wrk, $asm);
consensusFilter($wrk, $asm);
outputGraph($wrk, $asm);
outputLayout($wrk, $asm);
outputSequence($wrk, $asm);
outputSummary($wrk, $asm);
}
exit(0);