This repository has been archived by the owner on May 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
PairedSingleSampleWF.nf
675 lines (570 loc) · 21.1 KB
/
PairedSingleSampleWF.nf
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#!/usr/bin/env nextflow
/*
========================================================================================
nf-core/ E X O S E Q B E S T P R A C T I C E
========================================================================================
#### Homepage / Documentation
https://github.com/scilifelab/NGI-ExoSeq
#### Authors
Senthilkumar Panneerselvam @senthil10 <[email protected]>
Phil Ewels @ewels <[email protected]>
Alex Peltzer @alex_peltzer <[email protected]>
Marie Gauder <[email protected]>
Some code parts were lent from other NGI-Pipelines (e.g. CAW), specifically the error
handling, logging messages. Thanks for that @CAW guys.
----------------------------------------------------------------------------------------
Developed based on GATK's best practise, takes set of FASTQ files and performs:
- alignment (BWA)
- recalibration (GATK)
- realignment (GATK)
- variant calling (GATK)
- variant evaluation (SnpEff)
*/
// Help message
helpMessage = """
===============================================================================
nf-core/ExoSeq : Exome/Targeted sequence capture best practice analysis v${params.version}
===============================================================================
Usage: nextflow nf-core/ExoSeq --reads '*_R{1,2}.fastq.gz' --genome GRCh37 --kitfiles 'kitpath' --metafiles 'metapath'
This is a typical usage where the required parameters (with no defaults) were
given. The available paramaters are listed below based on category
Required parameters:
--reads Absolute path to project directory
--genome Name of iGenomes reference
Output:
--outdir Path where the results to be saved [Default: './results']
-w/--work-dir The temporary directory where intermediate data will be saved
Kit files:
--kitfiles Path to kitfiles defined in metafiles.config
--metafiles Path to metafiles defined in metafiles.config
--kit Kit used to prep samples [Default: 'agilent_v5']
AWSBatch options:
--awsqueue The AWSBatch JobQueue that needs to be set when running on AWSBatch
--awsregion The AWS Region for your AWS Batch job to run on
For more detailed information regarding the parameters and usage refer to package
documentation at https://github.com/nf-core/ExoSeq""".stripIndent()
// Variables and defaults
params.name = false
params.help = false
params.reads = false
params.singleEnd = false
params.run_id = false
params.aligner = 'bwa' //Default, but stay tuned for later ;-)
params.saveReference = true
// Output configuration
params.outdir = './results'
params.saveAlignedIntermediates = false
params.saveIntermediateVariants = false
// Clipping options
params.notrim = false
params.clip_r1 = 0
params.clip_r2 = 0
params.three_prime_clip_r1 = 0
params.three_prime_clip_r2 = 0
// Kit options
params.kit = 'agilent_v5'
// Has the run name been specified by the user?
// this has the bonus effect of catching both -name and --name
custom_runName = params.name
if( !(workflow.runName ==~ /[a-z]+_[a-z]+/) ){
custom_runName = workflow.runName
}
// Show help when needed
if (params.help){
log.info helpMessage
exit 0
}
// Check blocks for certain required parameters, to see they are given and exist
if (!params.reads || !params.genome){
exit 1, "Parameters '--reads' and '--genome' are required to run the pipeline"
}
if (!params.kitfiles){
exit 1, "No Exome Capturing Kit specified!"
}
if (!params.metafiles){
exit 1, "No Exome Metafiles specified!"
}
//AWSBatch sanity checking
if(workflow.profile == 'awsbatch'){
if (!params.awsqueue || !params.awsregion) exit 1, "Specify correct --awsqueue and --awsregion parameters on AWSBatch!"
if (!workflow.workDir.startsWith('s3') || !params.outdir.startsWith('s3')) exit 1, "Specify S3 URLs for workDir and outdir parameters on AWSBatch!"
}
// Create a channel for input files
Channel
.fromFilePairs( params.reads, size: params.singleEnd ? 1 : 2 )
.ifEmpty { exit 1, "Cannot find any reads matching: ${params.reads}\nNB: Path needs to be enclosed in quotes!\nNB: Path requires at least one * wildcard!\nIf this is single-end data, please specify --singleEnd on the command line." }
.into { read_files_fastqc; read_files_trimming }
// Validate Input indices for BWA Mem and GATK
if(params.aligner == 'bwa' ){
bwaId = Channel
.fromPath("${params.gfasta}.bwt")
.ifEmpty { exit 1, "BWA index not found: ${params.gfasta}.bwt" }
}
// Create a summary for the logfile
def summary = [:]
summary['Run Name'] = custom_runName ?: workflow.runName
summary['Reads'] = params.reads
summary['Data Type'] = params.singleEnd ? 'Single-End' : 'Paired-End'
summary['Genome Assembly'] = params.genome
summary['Trim R1'] = params.clip_r1
summary['Trim R2'] = params.clip_r2
summary["Trim 3' R1"] = params.three_prime_clip_r1
summary["Trim 3' R2"] = params.three_prime_clip_r2
if(params.aligner == 'bwa'){
summary['Aligner'] = "BWA Mem"
if(params.bwa_index) summary['BWA Index'] = params.bwa_index
else if(params.gfasta) summary['Fasta Ref'] = params.gfasta
}
summary['Save Intermediate Aligned Files'] = params.saveAlignedIntermediates ? 'Yes' : 'No'
summary['Save Intermediate Variant Files'] = params.saveIntermediateVariants ? 'Yes' : 'No'
summary['Max Memory'] = params.max_memory
summary['Max CPUs'] = params.max_cpus
summary['Max Time'] = params.max_time
summary['Output dir'] = params.outdir
summary['Working dir'] = workflow.workDir
summary['Container'] = workflow.container
if(workflow.revision) summary['Pipeline Release'] = workflow.revision
summary['Current home'] = "$HOME"
summary['Current user'] = "$USER"
summary['Current path'] = "$PWD"
summary['Script dir'] = workflow.projectDir
summary['Config Profile'] = workflow.profile
log.info summary.collect { k,v -> "${k.padRight(15)}: $v" }.join("\n")
log.info "========================================="
try {
if( ! workflow.nextflow.version.matches(">= $params.nf_required_version") ){
throw GroovyException('Nextflow version too old')
}
} catch (all) {
log.error "====================================================\n" +
" Nextflow version $params.nf_required_version required! You are running v$workflow.nextflow.version.\n" +
" Pipeline execution will continue, but things may break.\n" +
" Please run `nextflow self-update` to update Nextflow.\n" +
"============================================================"
}
// Build BWA Index if this is required
if(params.aligner == 'bwa' && !params.bwa_index){
// Create Channels
fasta_for_bwa_index = Channel
.fromPath("${params.gfasta}")
fasta_for_samtools_index = Channel
.fromPath("${params.gfasta}")
// Create a BWA index for non-indexed genomes
process makeBWAIndex {
tag "$params.gfasta"
publishDir path: { params.saveReference ? "${params.outdir}/reference_genome" : params.outdir },
saveAs: { params.saveReference ? it : null }, mode: 'copy'
input:
file fasta from fasta_for_bwa_index
output:
file "*.{amb,ann,bwt,pac,sa}" into bwa_index
script:
"""
bwa index $fasta
"""
}
// Create a FastA index for non-indexed genomes
process makeFastaIndex {
tag "$params.gfasta"
publishDir path: { params.saveReference ? "${params.outdir}/reference_genome" : params.outdir },
saveAs: { params.saveReference ? it : null }, mode: 'copy'
input:
file fasta from fasta_for_samtools_index
output:
file "*.fai" into samtools_index
script:
"""
samtools faidx $fasta
"""
}
} else {
bwa_index = file("${params.bwa_index}")
}
/*
*
* STEP 0 - FastQC
*
*/
process fastqc {
tag "$name"
publishDir "${params.outdir}/fastqc", mode: 'copy',
saveAs: {filename -> filename.indexOf(".zip") > 0 ? "zips/$filename" : "$filename"}
input:
set val(name), file(reads) from read_files_fastqc
output:
file '*_fastqc.{zip,html}' into fastqc_results
file '.command.out' into fastqc_stdout
script:
"""
fastqc -q $reads
"""
}
/*
* STEP 1 - trim with trim galore
*/
if(params.notrim){
trimmed_reads = read_files_trimming
trimgalore_results = []
trimgalore_logs = []
} else {
process trim_galore {
tag "$name"
publishDir "${params.outdir}/trim_galore", mode: 'copy',
saveAs: {filename ->
if (filename.indexOf("_fastqc") > 0) "FastQC/$filename"
else if (filename.indexOf("trimming_report.txt") > 0) "logs/$filename"
else params.saveTrimmed ? filename : null
}
input:
set val(name), file(reads) from read_files_trimming
output:
set val(name), file(reads) into trimmed_reads
file '*trimming_report.txt' into trimgalore_results, trimgalore_logs
script:
single = reads instanceof Path
c_r1 = params.clip_r1 > 0 ? "--clip_r1 ${params.clip_r1}" : ''
c_r2 = params.clip_r2 > 0 ? "--clip_r2 ${params.clip_r2}" : ''
tpc_r1 = params.three_prime_clip_r1 > 0 ? "--three_prime_clip_r1 ${params.three_prime_clip_r1}" : ''
tpc_r2 = params.three_prime_clip_r2 > 0 ? "--three_prime_clip_r2 ${params.three_prime_clip_r2}" : ''
if (params.singleEnd) {
"""
trim_galore --gzip $c_r1 $tpc_r1 $reads --fastqc
"""
} else {
"""
trim_galore --paired --gzip $c_r1 $c_r2 $tpc_r1 $tpc_r2 $reads --fastqc
"""
}
}
}
/*
* STEP 2 - Map with BWA Mem
*/
process bwamem {
tag "$name"
input:
set val(name), file(reads) from trimmed_reads
file(bwa_index) from bwa_index
output:
set val(name), file("${name}_bwa.bam") into samples_sorted_bam
file '.command.log' into bwa_stdout
script:
def avail_mem = task.memory ? "-m ${task.memory.toMega().intdiv(task.cpus)}M" : ''
rg="\'@RG\\tID:${params.run_id}\\tSM:${params.run_id}\\tPL:illumina\'"
"""
bwa mem \\
-R $rg \\
-t ${task.cpus} \\
$params.gfasta \\
$reads \\
| samtools ${avail_mem} sort -O bam - > $outfile ${name}_bwa.bam
"""
}
/*
* STEP 4 - Mark PCR duplicates in sorted BAM file
*/
process markDuplicates {
tag "${name}"
publishDir "${params.outdir}/Picard_Markduplicates/metrics", mode: 'copy',
saveAs: { filename -> filename.indexOf(".dup_metrics") > 0 ? filename : null }
input:
set val(name), file(sorted_bam) from samples_sorted_bam
output:
set val(name), file("${name}_markdup.bam"), file("${name}_markdup.bai") into samples_markdup_bam, samples_for_applyBQSR
file("${name}.dup_metrics") into markdup_results
file '.command.log' into markDuplicates_stdout
script:
"""
mkdir `pwd`/tmp
gatk-launch MarkDuplicates \\
--INPUT $sorted_bam \\
--OUTPUT ${name}_markdup.bam \\
--METRICS_FILE ${name}.dup_metrics \\
--REMOVE_DUPLICATES false \\
--CREATE_INDEX true \\
--TMP_DIR=`pwd`/tmp \\
--java-options -Xmx${task.memory.toGiga()}g
"""
}
/*
* Step 5 - Recalibrate BAM file with known variants and BaseRecalibrator
*
*/
process recal_bam_files {
tag "${name}"
publishDir "${params.outdir}/GATK_Recalibration", mode: 'copy'
input:
set val(name), file(markdup_bam), file(markdup_bam_ind) from samples_markdup_bam
output:
set val(name), file("${name}_table.recal") into samples_recal_reports
file '.command.log' into gatk_stdout
file '.command.log' into gatk_base_recalibration_results
script:
if(params.exome){
"""
gatk-launch BaseRecalibrator \\
-R $params.gfasta \\
-I $markdup_bam \\
-O ${name}_table.recal \\
-L $params.target \\
--known-sites $params.dbsnp \\
--verbosity INFO \\
--java-options -Xmx${task.memory.toGiga()}g
"""
} else {
"""
gatk-launch BaseRecalibrator \\
-R $params.gfasta \\
-I $markdup_bam \\
-O ${name}_table.recal \\
--known-sites $params.dbsnp \\
--verbosity INFO \\
--java-options -Xmx${task.memory.toGiga()}g
"""
}
}
process applyBQSR {
tag "${name}"
publishDir "${params.outdir}/GATK_ApplyBQSR", mode: 'copy'
input:
set val(name), file("${name}_table.recal") from samples_recal_reports
set val(name), file(markdup_bam), file(markdup_bam_ind) from samples_for_applyBQSR
output:
set val(name), file("${name}.bam"), file("${name}.bai") into bam_vcall, bam_metrics
script:
if(params.exome){
"""
gatk-launch ApplyBQSR \\
-R $params.gfasta \\
-I $markdup_bam \\
--bqsr-recal-file ${name}_table.recal \\
-O ${name}.bam \\
-L $params.target \\
--create-output-bam-index true \\
--java-options -Xmx${task.memory.toGiga()}g
"""
} else {
"""
gatk-launch ApplyBQSR \\
-R $params.gfasta \\
-I $markdup_bam \\
--bqsr-recal-file ${name}_table.recal \\
-O ${name}.bam \\
--create-output-bam-index true \\
--java-options -Xmx${task.memory.toGiga()}g
"""
}
}
/*
* Step 7 - Determine quality metrics of mapped BAM files using QualiMap 2
*
*/
process qualiMap {
tag "${name}"
publishDir "${params.outdir}/Qualimap", mode: 'copy'
input:
set val(name), file(realign_bam), file(realign_bam_ind) from bam_metrics
output:
file "${name}" into qualimap_results
file '.command.log' into qualimap_stdout
script:
gcref = ''
gff = ''
if(params.genome == 'GRCh37') gcref = '-gd HUMAN'
if(params.genome == 'GRCm38') gcref = '-gd MOUSE'
if(params.exome) gff ="-gff ${params.target_bed}"
"""
qualimap bamqc $gcref \\
-bam $realign_bam \\
-outdir ${name} \\
--skip-duplicated \\
--collect-overlap-pairs \\
-nt ${task.cpus} \\
$gff \\
--java-mem-size=${task.memory.toGiga()}G \\
"""
}
/*
* Step 8 - Call Variants with HaplotypeCaller in GVCF mode (differentiate between exome and whole genome data here)
*
*/
process variantCall {
tag "${name}"
publishDir "${params.outdir}/GATK_VariantCalling/", mode: 'copy',
saveAs: {filename -> filename.replaceFirst(/variants/, "raw_variants")}
input:
set val(name), file(realign_bam), file(realign_bam_ind) from bam_vcall
output:
set val(name), file("${name}_variants.vcf"), file("${name}_variants.vcf.idx") into raw_variants
script:
if(params.exome){
"""
gatk-launch HaplotypeCaller \\
-I $realign_bam \\
-R $params.gfasta \\
-O ${name}_variants.vcf \\
-ERC GVCF \\
-L $params.target \\
--create-output-variant-index \\
--annotation MappingQualityRankSumTest \\
--annotation QualByDepth \\
--annotation ReadPosRankSumTest \\
--annotation RMSMappingQuality \\
--annotation FisherStrand \\
--annotation Coverage \\
--dbsnp $params.dbsnp \\
--verbosity INFO \\
--java-options -Xmx${task.memory.toGiga()}g
"""
} else { //We have a winner (genome)
"""
gatk-launch HaplotypeCaller \\
-I $realign_bam \\
-R $params.gfasta \\
-O ${name}_variants.vcf \\
-ERC GVCF \\
--create-output-variant-index \\
--annotation MappingQualityRankSumTest \\
--annotation QualByDepth \\
--annotation ReadPosRankSumTest \\
--annotation RMSMappingQuality \\
--annotation FisherStrand \\
--annotation Coverage \\
--dbsnp $params.dbsnp \\
--verbosity INFO \\
--java-options -Xmx${task.memory.toGiga()}g
"""
}
}
/*
* Step 9 - Generate a YAML file for software versions in the pipeline
* This is then parsed by MultiQC and the report feature to produce a final report with the software Versions in the pipeline.
*/
process get_software_versions {
output:
file 'software_versions_mqc.yaml' into software_versions_yaml
script:
"""
echo "$params.version" &> v_nfcore_exoseq.txt
echo "$workflow.nextflow.version" &> v_nextflow.txt
fastqc --version &> v_fastqc.txt
cutadapt --version &> v_cutadapt.txt
trim_galore --version &> v_trim_galore.txt
samtools --version &> v_samtools.txt
bwa &> v_bwa.txt 2>&1 || true
qualimap --version &> v_qualimap.txt
gatk-launch BaseRecalibrator --version &> v_gatk.txt
multiqc --version &> v_multiqc.txt
scrape_software_versions.py &> software_versions_mqc.yaml
"""
}
/**
* Step 10 - Generate MultiQC config file
*
*/
process GenerateMultiQCconfig {
publishDir "${params.outdir}/MultiQC/", mode: 'copy'
input:
output:
file("multiqc_config.yaml") into multiQCconfig
script:
"""
touch multiqc_config.yaml
echo "custom_logo_title: 'Exome Analysis Workflow'" >> multiqc_config.yaml
echo "extra_fn_clean_exts:" >> multiqc_config.yaml
echo "- _R1" >> multiqc_config.yaml
echo "- _R2" >> multiqc_config.yaml
echo "report_header_info:" >> multiqc_config.yaml
echo "- Exoseq version: ${params.version}" >> multiqc_config.yaml
echo "- Command Line: ${workflow.commandLine}" >> multiqc_config.yaml
echo "- Directory: ${workflow.launchDir}" >> multiqc_config.yaml
echo "- Genome: "${params.gfasta} >> multiqc_config.yaml
echo " dbSNP : ${params.dbsnp}" >> multiqc_config.yaml
echo " Omni: ${params.omni}" >> multiqc_config.yaml
echo " Mills: ${params.mills}" >> multiqc_config.yaml
echo "top_modules:" >> multiqc_config.yaml
echo "- 'fastqc'" >> multiqc_config.yaml
echo "- 'cutadapt'" >> multiqc_config.yaml
echo "- 'bwa'" >> multiqc_config.yaml
echo "- 'samtools'" >> multiqc_config.yaml
echo "- 'qualimap'" >> multiqc_config.yaml
echo "- 'gatk'" >> multiqc_config.yaml
"""
}
/*
* Step 12 - Collect metrics, stats and other resources with MultiQC in a single call
*/
process multiqc {
tag "$name"
publishDir "${params.outdir}/MultiQC", mode: 'copy'
input:
file multiQCconfig
file (fastqc:'fastqc/*') from fastqc_results.toList()
file ('trimgalore/*') from trimgalore_results.toList()
file ('gatk_base_recalibration/T*') from gatk_base_recalibration_results.toList()
file ('gatk_picard_duplicates/*') from markdup_results.toList()
file ('qualimap/*') from qualimap_results.toList()
file ('software_versions/*') from software_versions_yaml.toList()
output:
file '*multiqc_report.html' into multiqc_report
file '*_data' into multiqc_data
file '.command.err' into multiqc_stderr
val prefix into multiqc_prefix
script:
prefix = fastqc[0].toString() - '_fastqc.html' - 'fastqc/'
rtitle = custom_runName ? "--title \"$custom_runName\"" : ''
rfilename = custom_runName ? "--filename " + custom_runName.replaceAll('\\W','_').replaceAll('_+','_') + "_multiqc_report" : ''
"""
multiqc -f $rtitle $rfilename --config $multiQCconfig .
"""
}
/*
================================================================================
= F U N C T I O N S =
================================================================================
*/
def exoMessage() {
// Display ExoSeq message
log.info "nf-core/ExoSeq ANALYSIS WORKFLOW ~ ${params.version} - " + this.grabRevision() + (workflow.commitId ? " [${workflow.commitId}]" : "")
}
def grabRevision() {
// Return the same string executed from github or not
return workflow.revision ?: workflow.commitId ?: workflow.scriptId.substring(0,10)
}
def minimalInformationMessage() {
// Minimal information message
log.info "Command Line: " + workflow.commandLine
log.info "Project Dir : " + workflow.projectDir
log.info "Launch Dir : " + workflow.launchDir
log.info "Work Dir : " + workflow.workDir
log.info "Out Dir : " + params.outdir
log.info "Genome : " + params.gfasta
}
def nextflowMessage() {
// Nextflow message (version + build)
log.info "N E X T F L O W ~ version ${workflow.nextflow.version} ${workflow.nextflow.build}"
}
def versionMessage() {
// Display version message
log.info "nf-core/ExoSeq ANALYSIS WORKFLOW"
log.info " version : " + version
log.info workflow.commitId ? "Git info : ${workflow.repository} - ${workflow.revision} [${workflow.commitId}]" : " revision : " + this.grabRevision()
}
workflow.onComplete {
// Display complete message
this.nextflowMessage()
this.exoMessage()
this.minimalInformationMessage()
log.info "Completed at: " + workflow.complete
log.info "Duration : " + workflow.duration
log.info "Success : " + workflow.success
log.info "Exit status : " + workflow.exitStatus
log.info "Error report: " + (workflow.errorReport ?: '-')
}
workflow.onError {
// Display error message
this.nextflowMessage()
this.exoMessage()
log.info "Workflow execution stopped with the following message:"
log.info " " + workflow.errorMessage
}