forked from nf-core/exoseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JointDiscoveryGATK.nf
576 lines (475 loc) · 19.6 KB
/
JointDiscoveryGATK.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
#!/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/nf-core/ExoSeq
#### Authors
Senthilkumar Panneerselvam @senthil10 <[email protected]>
Phil Ewels @ewels <[email protected]>
Alex Peltzer @alex_peltzer <[email protected]>
Marie Gauder <[email protected]>
----------------------------------------------------------------------------------------
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)
*/
// Package version
version = '0.8.1'
// Help message
helpMessage = """
===============================================================================
nf-core/ExoSeq : Exome/Targeted sequence capture best practice analysis v${version}
===============================================================================
Usage: nextflow nf-core/ExoSeq --reads '*_R{1,2}.fastq.gz' --genome GRCh37
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']
Kit files:
--kit Kit used to prep samples [Default: 'agilent_v5']
--bait Absolute path to bait file
--target Absolute path to target file
--target_bed Absolute path to target bed file (snpEff compatible format)
Genome/Variation files:
--dbsnp Absolute path to dbsnp file
--thousandg Absolute path to 1000G file
--mills Absolute path to Mills file
--omni Absolute path to Omni file
--gfasta Absolute path to genome fasta file
--bwa_index Absolute path to bwa genome index
Other options:
--exome Exome data, if this is not set, run as genome data
--project Uppnex project to user for SLURM executor
For more detailed information regarding the parameters and usage refer to package
documentation at https:// github.com/nf-core/ExoSeq
"""
// Variables and defaults
params.name = false
params.help = false
params.reads = false
params.singleEnd = false
params.genome = false
params.run_id = false
params.exome = false //default genome, set to true to run restricting to exome positions
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'
params.bait = params.kitFiles[ params.kit ] ? params.kitFiles[ params.kit ].bait ?: false : false
params.target = params.kitFiles[ params.kit ] ? params.kitFiles[ params.kit ].target ?: false : false
params.target_bed = params.kitFiles[ params.kit ] ? params.kitFiles[ params.kit ].target_bed ?: false : false
params.dbsnp = params.metaFiles[ params.genome ] ? params.metaFiles[ params.genome ].dbsnp ?: false : false
params.thousandg = params.metaFiles[ params.genome ] ? params.metaFiles[ params.genome ].thousandg ?: false : false
params.mills = params.metaFiles[ params.genome ] ? params.metaFiles[ params.genome ].mills ?: false : false
params.omni = params.metaFiles[ params.genome ] ? params.metaFiles[ params.genome ].omni ?: false : false
params.gfasta = params.metaFiles[ params.genome ] ? params.metaFiles[ params.genome ].gfasta ?: false : false
params.bwa_index = params.metaFiles[ params.genome ] ? params.metaFiles[ params.genome ].bwa_index ?: false : false
params.multiqc_config = "$baseDir/conf/multiqc_config.yaml"
// 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[ params.kit ] && ['bait', 'target'].count{ params[it] } != 2){
exit 1, "Kit '${params.kit}' is not available in pre-defined config, so " +
"provide all kit specific files with option '--bait' and '--target'"
}
if (!params.metaFiles[ params.genome ] && ['gfasta', 'bwa_index', 'dbsnp', 'thousandg', 'mills', 'omni'].count{ params[it] } != 6){
exit 1, "Genome '${params.genome}' is not available in pre-defined config, so you need to provide all genome specific " +
"files with options '--gfasta', '--bwa_index', '--dbsnp', '--thousandg', '--mills' and '--omni'"
}
// 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" }
}
// Set up input channels for certain files (if required)
multiqc_config = file(params.multiqc_config)
// 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'] = params.genome
summary['WES/WGS'] = params.exome ? 'WES' : 'WGS'
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 "========================================="
// Nextflow version check
nf_required_version = '0.25.0'
try {
if( ! workflow.nextflow.version.matches(">= $nf_required_version") ){
throw GroovyException('Nextflow version too old')
}
} catch (all) {
log.error "====================================================\n" +
" Nextflow version $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" +
"============================================================"
}
//TODO: We need to specify input channels in a separate step. This is solely a start and a result of splitting both files to two separate scripts.
/*
* Step 9 - Genotype generate GVCFs using GATK's GenotypeGVCFs
*
*/
process genotypegvcfs{
tag "${name}"
publishDir "${params.outdir}/GATK_GenotypeGVCFs/", mode: 'copy',
saveAs: {filename -> params.saveIntermediateVariants ? "$filename" : null }
input:
set val(name), file(raw_vcf), file(raw_vcf_idx) from raw_variants
output:
set val(name), file("${name}_gvcf.vcf"), file("${name}_gvcf.vcf.idx") into raw_gvcfs
script:
"""
gatk -T GenotypeGVCFs \\
-R $params.gfasta \\
--variant $raw_vcf \\
-nt $task.cpus \\
-o ${name}_gvcf.vcf \\
"""
}
/*
* Step 10 - Create separate files for SNPs and Indels
*
*/
process variantSelect {
tag "${name}"
publishDir "${params.outdir}/GATK_VariantSelection", mode: 'copy',
saveAs: {filename -> params.saveIntermediateVariants ? "$filename" : null }
input:
set val(name), file(raw_vcf), file(raw_vcf_idx) from raw_gvcfs
output:
set val(name), file("${name}_snp.vcf"), file("${name}_snp.vcf.idx") into raw_snp
set val(name), file("${name}_indels.vcf"), file("${name}_indels.vcf.idx") into raw_indels
script:
"""
gatk -T SelectVariants \\
-R $params.gfasta \\
--variant $raw_vcf \\
--out ${name}_snp.vcf \\
--selectTypeToInclude SNP
gatk -T SelectVariants \\
-R $params.gfasta \\
--variant $raw_vcf \\
--out ${name}_indels.vcf \\
--selectTypeToInclude INDEL \\
--selectTypeToInclude MIXED \\
--selectTypeToInclude MNP \\
--selectTypeToInclude SYMBOLIC \\
--selectTypeToInclude NO_VARIATION
"""
}
/*
* Step 11 - Recalibrate SNPs using Omni, 1000G and DBSNP databases
*
*/
process recalSNPs {
tag "${name}"
publishDir "${params.outdir}/GATK_RecalibrateSNPs/", mode: 'copy',
saveAs: {filename -> params.saveIntermediateVariants ? "$filename" : null }
input:
set val(name), file(raw_snp), file(raw_snp_idx) from raw_snp
output:
set val(name), file("${sample}_filtered_snp.vcf"), file("${sample}_filtered_snp.vcf.idx") into filtered_snp
script:
"""
gatk -T VariantRecalibrator \\
-R $params.gfasta \\
--input $raw_snp \\
--maxGaussians 4 \\
--recal_file ${name}_snp.recal \\
--tranches_file ${name}_snp.tranches \\
-resource:omni,known=false,training=true,truth=true,prior=12.0 $params.omni \\
-resource:1000G,known=false,training=true,truth=false,prior=10.0 $params.thousandg \\
-resource:dbsnp,known=true,training=false,truth=false,prior=2.0 $params.dbsnp \\
--mode SNP \\
-an QD \\
-an FS \\
-an MQ
gatk -T ApplyRecalibration \\
-R $params.gfasta \\
--out ${name}_filtered_snp.vcf \\
--input $raw_snp \\
--mode SNP \\
--tranches_file ${name}_snp.tranches \\
--recal_file ${name}_snp.recal \\
--ts_filter_level 99.5 \\
-mode SNP
"""
}
/*
* Step 12 - Recalibrate INDELS using the Mills golden dataset
*
*/
process recalIndels {
tag "${name}"
publishDir "${params.outdir}/GATK_RecalibrateIndels", mode: 'copy',
saveAs: {filename -> params.saveIntermediateVariants ? "$filename" : null }
input:
set val(name), file(raw_indel), file(raw_indel_idx) from raw_indels
output:
set val(name), file("${name}_filtered_indels.vcf"), file("${name}_filtered_indels.vcf.idx") into filtered_indels
script:
"""
gatk -T VariantRecalibrator \\
-R $params.gfasta \\
--input $raw_indel \\
--maxGaussians 4 \\
--recal_file ${name}_indel.recal \\
--tranches_file ${name}_indel.tranches \\
-resource:mills,known=false,training=true,truth=true,prior=12.0 $params.mills \\
-resource:dbsnp,known=true,training=false,truth=false,prior=2.0 $params.dbsnp \\
-an QD -an DP -an FS -an SOR \\
-mode INDEL
gatk -T ApplyRecalibration \\
-R $params.gfasta \\
--out ${name}_filtered_indels.vcf \\
--input $raw_indel \\
--mode SNP \\
--tranches_file ${name}_indel.tranches \\
--recal_file ${name}_indel.recal \\
--ts_filter_level 99.0 \\
-mode INDEL
"""
}
/*
* Step 13 - Combine recalibrated files again
*
*/
filtered_snp
.cross(filtered_indels)
.map{ it -> [it[0][0], it[0][1], it[0][2], it[1][1], it[1][2]] }
.set{ variants_filtered }
/*
* Step 14 - Combine recalibrated files again using GATK's CombineVariants
*
*/
process combineVariants {
tag "$name"
publishDir "${params.outdir}/GATK_CombineVariants/", mode: 'copy',
saveAs: {filename -> params.saveIntermediateVariants ? "$filename" : null }
input:
set file(fsnp), file(fsnp_idx), file(findel), file(findel_idx) from variants_filtered
output:
set file("${name}_combined_variants.vcf"), file("${name}_combined_variants.vcf.idx") into (combined_variants_evaluate,combined_variants_snpEff, combined_variants_gatk)
script:
"""
gatk -T CombineVariants \\
-R $params.gfasta \\
--out ${name}_combined_variants.vcf \\
--genotypemergeoption PRIORITIZE \\
--variant:${name}_SNP_filtered $fsnp \\
--variant:${name}_indels_filtered $findel \\
--rod_priority_list ${name}_SNP_filtered,${name}_indels_filtered
"""
}
/*
* Step 15 - Annotate Variants with SNPEff
*
*/
process variantAnnotatesnpEff {
tag "$name"
publishDir "${params.outdir}/SNPEFF_AnnotatedVariants/", mode: 'copy',
saveAs: {filename -> params.saveIntermediateVariants ? "$filename" : null }
input:
set file(phased_vcf), file(phased_vcf_ind) from combined_variants_snpEff
output:
file "*.{snpeff}" into combined_variants_gatk_snpeff
file '.command.log' into snpeff_stdout
file 'SnpEffStats.csv' into snpeff_results
script:
"""
snpEff \\
-c /usr/local/lib/snpEff/snpEff.config \\
-i vcf \\
-csvStats SnpEffStats.csv \\
-o gatk \\
-o vcf \\
-filterInterval $params.target_bed GRCh37.75 $phased_vcf \\
> ${name}_combined_phased_variants.snpeff
# Print version number to standard out
echo "GATK version "\$(snpEff -version 2>&1)
"""
}
/*
* Step 16 - Annotate Variants with GATK
*
*/
process variantAnnotateGATK{
tag "$name"
publishDir "${params.outdir}/GATK_AnnotatedVariants", mode: 'copy'
input:
set file(phased_vcf), file(phased_vcf_ind) from combined_variants_gatk
file(phased_vcf_snpeff) from combined_variants_gatk_snpeff
output:
file "*.{vcd,idx}"
script:
"""
gatk -T VariantAnnotator \\
-R $params.gfasta \\
-A SnpEff \\
--variant $phased_vcf \\
--snpEffFile ${name}_combined_phased_variants.snpeff \\
--out ${name}_combined_phased_annotated_variants.vcf
"""
}
/*
* Step 17 - Perform variant evaluation with GATK's VariantEval tool
*/
process variantEvaluate {
tag "$name"
publishDir "${params.outdir}/GATK_VariantEvaluate", mode: 'copy'
input:
set file("${name}_combined_variants.vcf"), file("${name}_combined_variants.vcf.idx") from combined_variants_evaluate
output:
file "${name}_combined_phased_variants.eval"
file "${name}_combined_phased_variants.eval" into gatk_variant_eval_results
script:
"""
gatk -T VariantEval \\
-R $params.gfasta \\
--eval $phased_vcf \\
--dbsnp $params.dbsnp \\
-o ${name}_combined_phased_variants.eval \\
-L $params.target \\
--doNotUseAllStandardModules \\
--evalModule TiTvVariantEvaluator \\
--evalModule CountVariants \\
--evalModule CompOverlap \\
--evalModule ValidationReport \\
--stratificationModule Filter \\
-l INFO
"""
}
/*
* Step 18 - Generate Software Versions Map
*
*/
software_versions = [
'FastQC': null, 'Trim Galore!': null, 'BWA': null, 'Picard MarkDuplicates': null, 'GATK': null,
'SNPEff': null, 'QualiMap': null, 'Nextflow': "v$workflow.nextflow.version"
]
/*
* Step 19 - 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 {
cache false
input:
val fastqc from fastqc_stdout.collect()
val trim_galore from trimgalore_logs.collect()
val bwa from bwa_stdout.collect()
val markDuplicates from markDuplicates_stdout.collect()
val gatk from gatk_stdout.collect()
val qualimap from qualimap_stdout.collect()
val snpeff from snpeff_stdout.collect()
output:
file 'software_versions_mqc.yaml' into software_versions_yaml
exec:
software_versions['FastQC'] = fastqc[0].getText().find(/FastQC v(\S+)/) { match, version -> "v$version"}
software_versions['Trim Galore!'] = trim_galore[0].getText().find(/Trim Galore version: (\S+)/) {match, version -> "v$version"}
software_versions['BWA'] = bwa[0].getText().find(/Version: (\S+)/) {match, version -> "v$version"}
software_versions['Picard MarkDuplicates'] = markDuplicates[0].getText().find(/Picard version ([\d\.]+)/) {match, version -> "v$version"}
software_versions['GATK'] = gatk[0].getText().find(/GATK version ([\d\.]+)/) {match, version -> "v$version"}
software_versions['QualiMap'] = qualimap[0].getText().find(/QualiMap v.(\S+)/) {match, version -> "v$version"}
software_versions['SNPEff'] = snpeff[0].getText().find(/SnpEff (\S+)/) {match, version -> "v$version" }
def sw_yaml_file = task.workDir.resolve('software_versions_mqc.yaml')
sw_yaml_file.text = """
id: 'nf-core/ExoSeq'
section_name: 'nf-core/ExoSeq Software Versions'
section_href: 'https://github.com/nf-core/ExoSeq'
plot_type: 'html'
description: 'are collected at run time from the software output.'
data: |
<dl class=\"dl-horizontal\">
${software_versions.collect{ k,v -> " <dt>$k</dt><dd>${v ?: '<span style=\"color:#999999;\">N/A</a>'}</dd>" }.join("\n")}
</dl>
""".stripIndent()
}
/*
* Step 20 - Collect metrics, stats and other resources with MultiQC in a single call
*/
process multiqc {
tag "$name"
publishDir "${params.outdir}/MultiQC", mode: 'copy'
input:
file multiqc_config
file (fastqc:'fastqc/*') from fastqc_results.collect()
file ('trimgalore/*') from trimgalore_results.collect()
file ('picard/*') from markdup_results.collect()
file ('snpEff/*') from snpeff_results.collect()
file ('gatk_base_recalibration/*') from gatk_base_recalibration_results.collect()
file ('gatk_variant_eval/*') from gatk_variant_eval_results.collect()
file ('qualimap/*') from qualimap_results.collect()
file ('software_versions/*') from software_versions_yaml.collect()
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 $multiqc_config . 2>&1
"""
}