-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpughlab_emseq_pipeline.pl
executable file
·487 lines (396 loc) · 13.8 KB
/
pughlab_emseq_pipeline.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
#!/usr/bin/env perl
### pughlab_emseq_pipeline.pl ######################################################################
use AutoLoader 'AUTOLOAD';
use strict;
use warnings;
use Carp;
use POSIX qw(strftime);
use Getopt::Std;
use Getopt::Long;
use File::Basename;
use File::Path qw(make_path);
use YAML qw(LoadFile);
use List::Util qw(any none);
use Data::Dumper;
my $cwd = dirname(__FILE__);
require "$cwd/scripts/utilities.pl";
####################################################################################################
# version author comment
# 1.0 sprokopec script to run PughLab EMSeq pipeline
### USAGE ##########################################################################################
# pughlab_emseq_pipeline.pl -c tool_config.yaml -d data.yaml
#
# where:
# - tool_config.yaml contains tool versions and parameters, output directory, reference
# information, etc.
# - data_config.yaml contains sample information (YAML file containing paths to FASTQ files,
# generated by create_fastq_yaml.pl)
### SUBROUTINES ####################################################################################
### MAIN ###########################################################################################
sub main {
my %args = (
tool_config => undef,
data_config => undef,
step1 => undef,
step2 => undef,
step3 => undef,
step4 => undef,
step5 => undef,
cleanup => undef,
cluster => undef,
dry_run => undef,
@_
);
my $tool_config = $args{tool_config};
my $data_config = $args{data_config};
### PREAMBLE ######################################################################################
# load tool config
my $tool_data = LoadFile($tool_config);
my $date = strftime "%F", localtime;
my $timestamp = strftime "%F_%H-%M-%S", localtime;
# check for and/or create output directories
my $output_directory = $tool_data->{output_dir};
$output_directory =~ s/\/$//;
my $log_directory = join('/', $output_directory, 'logs', 'run_EMSeq_pipeline_' . $timestamp);
unless(-e $output_directory) { make_path($output_directory); }
unless(-e $log_directory) { make_path($log_directory); }
# start logging
my $log_file = join('/', $log_directory, 'run_EMSeq_pipeline.log');
open (my $log, '>', $log_file) or die "Could not open $log_file for writing.";
print $log "---\n";
print $log "Running PughLab EM-Seq pipeline.\n";
print $log "\n Tool config used: $tool_config";
print $log "\n Output directory: $output_directory";
print $log "\n Sample config used: $data_config";
print $log "\n---\n\n";
my $seq_type = $tool_data->{seq_type};
# indicate maximum time limit for parent jobs to wait
my $max_time = '5-00:00:00';
my $perl = 'perl/' . $tool_data->{perl_version};
# get optional HPC group
my $hpc_group = defined($tool_data->{hpc_group}) ? "-A $tool_data->{hpc_group}" : undef;
### MAIN ###########################################################################################
my ($run_script, $trim_run_id, $fastqc_run_id, $bwa_run_id, $qc_run_id, $md_run_id);
my (@step1_job_ids, @step2_job_ids, @step3_job_ids, @step4_job_ids, @job_ids);
my $current_dependencies = '';
# prepare directory structure
my $trim_directory = join('/', $output_directory, 'fastq_trimmed');
my $fastqc_directory = join('/', $output_directory, 'fastqc');
my $bwa_directory = join('/', $output_directory, 'BWA');
my $qc_directory = join('/', $output_directory, 'BAMQC');
my $methylD_directory = join('/', $output_directory, 'MethylDackel');
# check which tools have been requested
my %tool_set = (
'trim_adapters' => defined($tool_data->{trim_adapters}->{run}) ? $tool_data->{trim_adapters}->{run} : 'N',
'fastqc' => defined($tool_data->{fastqc}->{run}) ? $tool_data->{fastqc}->{run} : 'N',
'bwa' => defined($tool_data->{bwa}->{run}) ? $tool_data->{bwa}->{run} : 'N',
'bamqc' => defined($tool_data->{bamqc}->{run}) ? $tool_data->{bamqc}->{run} : 'N',
'methylD' => defined($tool_data->{methyldackel}->{run}) ? $tool_data->{methyldackel}->{run} : 'N'
);
print $log Dumper \%tool_set;
# indicate YAML files for processed files
my $fastq_trimmed_output_yaml = join('/', $trim_directory, 'fastq_trimmed_config.yaml');
my $bwa_output_yaml = join('/', $bwa_directory, 'bwa_bam_config_' . $timestamp . '.yaml');
# are we running step1 (fastq prep)?
if ( (!$args{step1}) ) {
$fastq_trimmed_output_yaml = $data_config;
}
# are we running step2 (alignments) or are BAMs provided as input?
if ( (!$args{step2}) ) {
$bwa_output_yaml = $data_config;
}
# Should pre-processing of fastqs (adapter trimming + QC) be performed?
if ($args{step1}) {
## run AdapterTrim pipeline
unless(-e $trim_directory) { make_path($trim_directory); }
if ('Y' eq $tool_set{'trim_adapters'}) {
my $trim_command = join(' ',
"perl $cwd/scripts/trim_adapters.pl",
"-o", $trim_directory,
"-t", $tool_config,
"-d", $data_config,
"-b", $fastq_trimmed_output_yaml,
"-c", $args{cluster}
);
# record command (in log directory) and then run job
print $log "Submitting job for trim_adapters.pl\n";
print $log " COMMAND: $trim_command\n\n";
$run_script = write_script(
log_dir => $log_directory,
name => 'pughlab_dna_pipeline__run_trim_adapters',
cmd => $trim_command,
modules => [$perl],
mem => '256M',
max_time => '48:00:00',
hpc_driver => $args{cluster},
extra_args => [$hpc_group]
);
if ($args{dry_run}) {
$trim_command .= " --dry-run";
`$trim_command`;
$trim_run_id = 'pughlab_dna_pipeline__run_trim_adapters';
} else {
$trim_run_id = submit_job(
jobname => $log_directory,
shell_command => $run_script,
hpc_driver => $args{cluster},
dry_run => $args{dry_run},
log_file => $log
);
print $log ">>> AdapterTrim job id: $trim_run_id\n\n";
push @job_ids, $trim_run_id;
}
}
## run FASTQC pipeline
unless(-e $fastqc_directory) { make_path($fastqc_directory); }
if ('Y' eq $tool_set{'fastqc'}) {
my $fastqc_command = join(' ',
"perl $cwd/scripts/collect_fastqc_metrics.pl",
"-o", $fastqc_directory,
"-t", $tool_config,
"-d", $fastq_trimmed_output_yaml,
"-c", $args{cluster}
);
# record command (in log directory) and then run job
print $log "Submitting job for collect_fastqc_metrics.pl\n";
print $log " COMMAND: $fastqc_command\n\n";
$run_script = write_script(
log_dir => $log_directory,
name => 'pughlab_dna_pipeline__run_fastqc',
cmd => $fastqc_command,
modules => [$perl],
dependencies => $trim_run_id,
mem => '256M',
max_time => '48:00:00',
hpc_driver => $args{cluster},
extra_args => [$hpc_group]
);
if ($args{dry_run}) {
$fastqc_command .= " --dry-run";
`$fastqc_command`;
$fastqc_run_id = 'pughlab_dna_pipeline__run_fastqc';
} else {
$fastqc_run_id = submit_job(
jobname => $log_directory,
shell_command => $run_script,
hpc_driver => $args{cluster},
dry_run => $args{dry_run},
log_file => $log
);
print $log ">>> FASTQC job id: $fastqc_run_id\n\n";
push @job_ids, $fastqc_run_id;
}
}
}
# Should alignment be performed?
if ($args{step2}) {
## run BWA-alignment pipeline
unless(-e $bwa_directory) { make_path($bwa_directory); }
if ('Y' eq $tool_set{'bwa'}) {
my $bwa_command = join(' ',
"perl $cwd/scripts/bwa.pl",
"-o", $bwa_directory,
"-t", $tool_config,
"-d", $fastq_trimmed_output_yaml,
"-b", $bwa_output_yaml,
"-c", $args{cluster}
);
if ($args{cleanup}) {
$bwa_command .= " --remove";
}
# record command (in log directory) and then run job
print $log "Submitting job for bwa.pl\n";
print $log " COMMAND: $bwa_command\n\n";
$run_script = write_script(
log_dir => $log_directory,
name => 'pughlab_dna_pipeline__run_bwa',
cmd => $bwa_command,
modules => [$perl],
dependencies => $trim_run_id,
mem => '256M',
max_time => $max_time,
hpc_driver => $args{cluster},
extra_args => [$hpc_group]
);
if ($args{dry_run}) {
$bwa_command .= " --dry-run";
`$bwa_command`;
$bwa_run_id = 'pughlab_dna_pipeline__run_bwa';
} else {
$bwa_run_id = submit_job(
jobname => $log_directory,
shell_command => $run_script,
hpc_driver => $args{cluster},
dry_run => $args{dry_run},
log_file => $log
);
print $log ">>> BWA job id: $bwa_run_id\n\n";
push @job_ids, $bwa_run_id;
}
}
}
## Collect various alignment metrics
if ($args{step3}) {
unless(-e $qc_directory) { make_path($qc_directory); }
if ('Y' eq $tool_set{'bamqc'}) {
# QC (Picard QC functions) pipeline
my $qc_command = join(' ',
"perl $cwd/scripts/get_sequencing_metrics.pl",
"-o", $qc_directory,
"-t", $tool_config,
"-d", $bwa_output_yaml,
"-c", $args{cluster}
);
if ($args{cleanup}) {
$qc_command .= " --remove";
}
# record command (in log directory) and then run job
print $log "Submitting job for get_sequencing_metrics.pl\n";
print $log " COMMAND: $qc_command\n\n";
$run_script = write_script(
log_dir => $log_directory,
name => 'pughlab_dna_pipeline__run_qc',
cmd => $qc_command,
modules => [$perl],
dependencies => $bwa_run_id,
mem => '256M',
max_time => $max_time,
extra_args => [$hpc_group],
hpc_driver => $args{cluster}
);
if ($args{dry_run}) {
$qc_command .= " --dry-run";
`$qc_command`;
$qc_run_id = 'pughlab_dna_pipeline__run_qc';
} else {
$qc_run_id = submit_job(
jobname => $log_directory,
shell_command => $run_script,
hpc_driver => $args{cluster},
dry_run => $args{dry_run},
log_file => $log
);
print $log ">>> QC job id: $qc_run_id\n\n";
push @job_ids, $qc_run_id;
}
}
}
## Should methylation analyses be performed?
if ($args{step4}) {
unless(-e $methylD_directory) { make_path($methylD_directory); }
if ('Y' eq $tool_set{'methylD'}) {
# methyldackel to extract site-wise methylation
my $md_command = join(' ',
"perl $cwd/scripts/methyldackel.pl",
"-o", $methylD_directory,
"-t", $tool_config,
"-d", $bwa_output_yaml,
"-c", $args{cluster}
);
# if ($args{cleanup}) {
# $qc_command .= " --remove";
# }
# record command (in log directory) and then run job
print $log "Submitting job for methyldackel.pl\n";
print $log " COMMAND: $md_command\n\n";
$run_script = write_script(
log_dir => $log_directory,
name => 'pughlab_dna_pipeline__run_methyldackel',
cmd => $md_command,
modules => [$perl],
dependencies => $qc_run_id,
mem => '256M',
max_time => $max_time,
extra_args => [$hpc_group],
hpc_driver => $args{cluster}
);
if ($args{dry_run}) {
$md_command .= " --dry-run";
`$md_command`;
$md_run_id = 'pughlab_dna_pipeline__run_methyldackel';
} else {
$md_run_id = submit_job(
jobname => $log_directory,
shell_command => $run_script,
hpc_driver => $args{cluster},
dry_run => $args{dry_run},
log_file => $log
);
print $log ">>> MethylDackel job id: $md_run_id\n\n";
push @job_ids, $md_run_id;
}
}
}
# finish up
print $log "\nProgramming terminated successfully.\n\n";
close $log;
}
### GETOPTS AND DEFAULT VALUES #####################################################################
# declare variables
my ($tool_config, $data_config);
my ($fastq_prep, $alignment, $qc, $analysis, $summarize, $create_report);
my $hpc_driver = 'slurm';
my ($remove_junk, $dry_run);
my $help;
# read in command line arguments
GetOptions(
'h|help' => \$help,
't|tool=s' => \$tool_config,
'd|data=s' => \$data_config,
'fastq_prep' => \$fastq_prep,
'alignment' => \$alignment,
'qc' => \$qc,
'analysis' => \$analysis,
# 'summarize' => \$summarize,
# 'create_report' => \$create_report,
'c|cluster=s' => \$hpc_driver,
'remove' => \$remove_junk,
'dry-run' => \$dry_run
);
if ($help) {
my $help_msg = join("\n",
"Options:",
"\t--help|-h\tPrint this help message",
"\t--data|-d\t<string> data config (yaml format)",
"\t--tool|-t\t<string> tool config (yaml format)",
"\t--fastq_prep\t<boolean> should fastqs be trimmed? (default: false)",
"\t--alignment\t<boolean> should read alignment be performed? (default: false)",
"\t--qc\t\t<boolean> should QC metrics be generated on the BAMs? (default: false)",
"\t--analysis\t<boolean> should methylation analyses be performed? (default: false)",
# "\t--summarize\t<boolean> should output be summarized? (default: false)",
# "\t--create_report\t<boolean> should a report be generated? (default: false)",
"\t--cluster|-c\t<string> cluster scheduler (default: slurm)",
"\t--remove\t<boolean> should intermediates be removed? (default: false)",
"\t--dry-run\t<boolean> should jobs be submitted? (default: false)"
);
print "$help_msg\n";
exit;
}
if ( (!$alignment) && (!$analysis) && (!$summarize) && (!$qc) && (!$create_report) ) {
die("Please choose a step to run (at least one of --alignment, --qc, --analysis, --summarize, --create_report )");
}
if (!defined($tool_config)) {
die("No tool config file defined; please provide -t | --tool (ie, tool_config.yaml)");
}
if (!defined($data_config)) {
die("No data config file defined; please provide -d | --data (ie, sample_config.yaml)");
}
# check for compatible HPC driver; if not found, change dry_run to Y
my @compatible_drivers = qw(slurm);
if ( (none { $_ =~ m/$hpc_driver/ } @compatible_drivers ) && (!$dry_run) ) {
print "Unrecognized HPC driver requested: setting dry_run to true -- jobs will not be submitted but commands will be written to file.\n";
$dry_run = 1;
}
main(
tool_config => $tool_config,
data_config => $data_config,
step1 => $fastq_prep,
step2 => $alignment,
step3 => $qc,
step4 => $analysis,
# step4 => $summarize,
# step5 => $create_report,
cluster => $hpc_driver,
cleanup => $remove_junk,
dry_run => $dry_run
);