This repository has been archived by the owner on Jan 27, 2020. It is now read-only.
forked from nf-core/sarek
-
Notifications
You must be signed in to change notification settings - Fork 7
/
germlineVC.nf
550 lines (458 loc) · 18.9 KB
/
germlineVC.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
#!/usr/bin/env nextflow
/*
kate: syntax groovy; space-indent on; indent-width 2;
================================================================================
= S A R E K =
================================================================================
New Germline (+ Somatic) Analysis Workflow. Started March 2016.
--------------------------------------------------------------------------------
@Authors
Sebastian DiLorenzo <[email protected]> [@Sebastian-D]
Jesper Eisfeldt <[email protected]> [@J35P312]
Phil Ewels <[email protected]> [@ewels]
Maxime Garcia <[email protected]> [@MaxUlysse]
Szilveszter Juhos <[email protected]> [@szilvajuhos]
Max Käller <[email protected]> [@gulfshores]
Malin Larsson <[email protected]> [@malinlarsson]
Marcel Martin <[email protected]> [@marcelm]
Björn Nystedt <[email protected]> [@bjornnystedt]
Pall Olason <[email protected]> [@pallolason]
--------------------------------------------------------------------------------
@Homepage
http://opensource.scilifelab.se/projects/sarek/
--------------------------------------------------------------------------------
@Documentation
https://github.com/SciLifeLab/Sarek/README.md
--------------------------------------------------------------------------------
Processes overview
- CreateIntervalBeds - Create and sort intervals into bed files
- RunHaplotypecaller - Run HaplotypeCaller for Germline Variant Calling (Parallelized processes)
- RunGenotypeGVCFs - Run HaplotypeCaller for Germline Variant Calling (Parallelized processes)
- ConcatVCF - Merge results from paralellized callers
- RunSingleStrelka - Run Strelka for Germline Variant Calling
- RunSingleManta - Run Manta for Single Structural Variant Calling
- RunBcftoolsStats - Run BCFTools stats on vcf files
- RunVcftools - Run VCFTools on vcf files
================================================================================
= C O N F I G U R A T I O N =
================================================================================
*/
if (params.help) exit 0, helpMessage()
if (!SarekUtils.isAllowedParams(params)) exit 1, "params unknown, see --help for more information"
if (!checkUppmaxProject()) exit 1, "No UPPMAX project ID found! Use --project <UPPMAX Project ID>"
if (params.verbose) SarekUtils.verbose()
// Check for awsbatch profile configuration
// make sure queue is defined
if (workflow.profile == 'awsbatch') {
if (!params.awsqueue) exit 1, "Provide the job queue for aws batch!"
}
tools = params.tools ? params.tools.split(',').collect{it.trim().toLowerCase()} : []
toolList = defineToolList()
if (!SarekUtils.checkParameterList(tools,toolList)) exit 1, 'Unknown tool(s), see --help for more information'
referenceMap = defineReferenceMap(tools)
if (!SarekUtils.checkReferenceMap(referenceMap)) exit 1, 'Missing Reference file(s), see --help for more information'
if (params.test && params.genome in ['GRCh37', 'GRCh38']) {
referenceMap.intervals = file("$workflow.projectDir/repeats/tiny_${params.genome}.list")
}
// TODO
// FreeBayes does not need recalibrated BAMs, but we need to test whether
// the channels are set up correctly when we disable it
tsvPath = ''
if (params.sample) tsvPath = params.sample
else tsvPath = "${params.outDir}/Preprocessing/Recalibrated/recalibrated.tsv"
// Set up the bamFiles channel
bamFiles = Channel.empty()
if (tsvPath) {
tsvFile = file(tsvPath)
bamFiles = SarekUtils.extractBams(tsvFile, "germline")
} else exit 1, 'No sample were defined, see --help'
/*
================================================================================
= P R O C E S S E S =
================================================================================
*/
startMessage()
bamFiles = bamFiles.dump(tag:'BAM')
// Here we have a recalibrated bam set
// The sample tsv config file which is formatted like: "idPatient status idSample bamFile baiFile"
// Manta will be run in Germline mode, the Tumor mode is run in somaticVC.nf
// HaplotypeCaller and Strelka will be run for Normal and Tumor samples
(bamsForGermlineManta, bamsForGermlineStrelka, bamFiles) = bamFiles.into(3)
// To speed Variant Callers up we are chopping the reference into smaller pieces.
// Do variant calling by this intervals, and re-merge the VCFs.
// Since we are on a cluster, this can parallelize the variant call processes.
// And push down the variant call wall clock time significanlty.
process CreateIntervalBeds {
tag {intervals.fileName}
input:
file(intervals) from Channel.value(referenceMap.intervals)
output:
file '*.bed' into bedIntervals mode flatten
script:
// If the interval file is BED format, the fifth column is interpreted to
// contain runtime estimates, which is then used to combine short-running jobs
if (intervals.getName().endsWith('.bed'))
"""
awk -vFS="\t" '{
t = \$5 # runtime estimate
if (t == "") {
# no runtime estimate in this row, assume default value
t = (\$3 - \$2) / ${params.nucleotidesPerSecond}
}
if (name == "" || (chunk > 600 && (chunk + t) > longest * 1.05)) {
# start a new chunk
name = sprintf("%s_%d-%d.bed", \$1, \$2+1, \$3)
chunk = 0
longest = 0
}
if (t > longest)
longest = t
chunk += t
print \$0 > name
}' ${intervals}
"""
else
"""
awk -vFS="[:-]" '{
name = sprintf("%s_%d-%d", \$1, \$2, \$3);
printf("%s\\t%d\\t%d\\n", \$1, \$2-1, \$3) > name ".bed"
}' ${intervals}
"""
}
bedIntervals = bedIntervals
.map { intervalFile ->
def duration = 0.0
for (line in intervalFile.readLines()) {
final fields = line.split('\t')
if (fields.size() >= 5) duration += fields[4].toFloat()
else {
start = fields[1].toInteger()
end = fields[2].toInteger()
duration += (end - start) / params.nucleotidesPerSecond
}
}
[duration, intervalFile]
}.toSortedList({ a, b -> b[0] <=> a[0] })
.flatten().collate(2)
.map{duration, intervalFile -> intervalFile}
bedIntervals = bedIntervals.dump(tag:'Intervals')
bamsForHC = bamFiles.combine(bedIntervals)
process RunHaplotypecaller {
tag {idSample + "-" + intervalBed.baseName}
input:
set idPatient, status, idSample, file(bam), file(bai), file(intervalBed) from bamsForHC
set file(genomeFile), file(genomeIndex), file(genomeDict), file(dbsnp), file(dbsnpIndex) from Channel.value([
referenceMap.genomeFile,
referenceMap.genomeIndex,
referenceMap.genomeDict,
referenceMap.dbsnp,
referenceMap.dbsnpIndex
])
output:
set val("HaplotypeCallerGVCF"), idPatient, idSample, file("${intervalBed.baseName}_${idSample}.g.vcf") into hcGenomicVCF
set idPatient, status, idSample, file(intervalBed), file("${intervalBed.baseName}_${idSample}.g.vcf") into vcfsToGenotype
when: 'haplotypecaller' in tools && !params.onlyQC
script:
"""
gatk --java-options "-Xmx${task.memory.toGiga()}g -Xms6000m -XX:GCTimeLimit=50 -XX:GCHeapFreeLimit=10" \
HaplotypeCaller \
-R ${genomeFile} \
-I ${bam} \
-L ${intervalBed} \
-D ${dbsnp} \
-O ${intervalBed.baseName}_${idSample}.g.vcf \
-ERC GVCF
"""
}
hcGenomicVCF = hcGenomicVCF.groupTuple(by:[0,1,2])
if (params.noGVCF) hcGenomicVCF.close()
process RunGenotypeGVCFs {
tag {idSample + "-" + intervalBed.baseName}
input:
set idPatient, status, idSample, file(intervalBed), file(gvcf) from vcfsToGenotype
set file(genomeFile), file(genomeIndex), file(genomeDict), file(dbsnp), file(dbsnpIndex) from Channel.value([
referenceMap.genomeFile,
referenceMap.genomeIndex,
referenceMap.genomeDict,
referenceMap.dbsnp,
referenceMap.dbsnpIndex
])
output:
set val("HaplotypeCaller"), idPatient, idSample, file("${intervalBed.baseName}_${idSample}.vcf") into hcGenotypedVCF
when: 'haplotypecaller' in tools && !params.onlyQC
script:
// Using -L is important for speed and we have to index the interval files also
"""
gatk --java-options -Xmx${task.memory.toGiga()}g \
IndexFeatureFile -F ${gvcf}
gatk --java-options -Xmx${task.memory.toGiga()}g \
GenotypeGVCFs \
-R ${genomeFile} \
-L ${intervalBed} \
-D ${dbsnp} \
-V ${gvcf} \
-O ${intervalBed.baseName}_${idSample}.vcf
"""
}
hcGenotypedVCF = hcGenotypedVCF.groupTuple(by:[0,1,2,3])
// we are merging the VCFs that are called separatelly for different intervals
// so we can have a single sorted VCF containing all the calls for a given caller
vcfsToMerge = hcGenomicVCF.mix(hcGenotypedVCF)
vcfsToMerge = vcfsToMerge.dump(tag:'VCFsToMerge')
process ConcatVCF {
tag {variantCaller + "-" + idSample}
publishDir "${params.outDir}/VariantCalling/${idPatient}/${"$variantCaller"}", mode: params.publishDirMode
input:
set variantCaller, idPatient, idSample, file(vcFiles) from vcfsToMerge
file(genomeIndex) from Channel.value(referenceMap.genomeIndex)
file(targetBED) from Channel.value(params.targetBED ? file(params.targetBED) : "null")
output:
// we have this funny *_* pattern to avoid copying the raw calls to publishdir
set variantCaller, idPatient, idSample, file("*_*.vcf.gz"), file("*_*.vcf.gz.tbi") into vcfConcatenated
when: 'haplotypecaller' in tools && !params.onlyQC
script:
if (variantCaller == 'HaplotypeCaller') outputFile = "${variantCaller}_${idSample}.vcf"
else if (variantCaller == 'HaplotypeCallerGVCF') outputFile = "haplotypecaller_${idSample}.g.vcf"
options = params.targetBED ? "-t ${targetBED}" : ""
"""
concatenateVCFs.sh -i ${genomeIndex} -c ${task.cpus} -o ${outputFile} ${options}
"""
}
vcfConcatenated = vcfConcatenated.dump(tag:'VCFs')
process RunSingleStrelka {
tag {idSample}
publishDir "${params.outDir}/VariantCalling/${idPatient}/Strelka", mode: params.publishDirMode
input:
set idPatient, status, idSample, file(bam), file(bai) from bamsForGermlineStrelka
file(targetBED) from Channel.value(params.targetBED ? file(params.targetBED) : "null")
set file(genomeFile), file(genomeIndex) from Channel.value([
referenceMap.genomeFile,
referenceMap.genomeIndex
])
output:
set val("Strelka"), idPatient, idSample, file("*.vcf.gz"), file("*.vcf.gz.tbi") into singleStrelkaOutput
when: 'strelka' in tools && !params.onlyQC
script:
beforeScript = params.targetBED ? "bgzip --threads ${task.cpus} -c ${targetBED} > call_targets.bed.gz ; tabix call_targets.bed.gz" : ""
options = params.targetBED ? "--exome --callRegions call_targets.bed.gz" : ""
"""
${beforeScript}
configureStrelkaGermlineWorkflow.py \
--bam ${bam} \
--referenceFasta ${genomeFile} \
${options} \
--runDir Strelka
python Strelka/runWorkflow.py -m local -j ${task.cpus}
mv Strelka/results/variants/genome.*.vcf.gz Strelka_${idSample}_genome.vcf.gz
mv Strelka/results/variants/genome.*.vcf.gz.tbi Strelka_${idSample}_genome.vcf.gz.tbi
mv Strelka/results/variants/variants.vcf.gz Strelka_${idSample}_variants.vcf.gz
mv Strelka/results/variants/variants.vcf.gz.tbi Strelka_${idSample}_variants.vcf.gz.tbi
"""
}
singleStrelkaOutput = singleStrelkaOutput.dump(tag:'Single Strelka')
process RunSingleManta {
tag {idSample + " - Single Diploid"}
publishDir "${params.outDir}/VariantCalling/${idPatient}/Manta", mode: params.publishDirMode
input:
set idPatient, status, idSample, file(bam), file(bai) from bamsForGermlineManta
file(targetBED) from Channel.value(params.targetBED ? file(params.targetBED) : "null")
set file(genomeFile), file(genomeIndex) from Channel.value([
referenceMap.genomeFile,
referenceMap.genomeIndex
])
output:
set val("Manta"), idPatient, idSample, file("*.vcf.gz"), file("*.vcf.gz.tbi") into singleMantaOutput
when: 'manta' in tools && status == 0 && !params.onlyQC
script:
beforeScript = params.targetBED ? "bgzip --threads ${task.cpus} -c ${targetBED} > call_targets.bed.gz ; tabix call_targets.bed.gz" : ""
options = params.targetBED ? "--exome --callRegions call_targets.bed.gz" : ""
"""
${beforeScript}
configManta.py \
--bam ${bam} \
--reference ${genomeFile} \
${options} \
--runDir Manta
python Manta/runWorkflow.py -m local -j ${task.cpus}
mv Manta/results/variants/candidateSmallIndels.vcf.gz \
Manta_${idSample}.candidateSmallIndels.vcf.gz
mv Manta/results/variants/candidateSmallIndels.vcf.gz.tbi \
Manta_${idSample}.candidateSmallIndels.vcf.gz.tbi
mv Manta/results/variants/candidateSV.vcf.gz \
Manta_${idSample}.candidateSV.vcf.gz
mv Manta/results/variants/candidateSV.vcf.gz.tbi \
Manta_${idSample}.candidateSV.vcf.gz.tbi
mv Manta/results/variants/diploidSV.vcf.gz \
Manta_${idSample}.diploidSV.vcf.gz
mv Manta/results/variants/diploidSV.vcf.gz.tbi \
Manta_${idSample}.diploidSV.vcf.gz.tbi
"""
}
singleMantaOutput = singleMantaOutput.dump(tag:'Single Manta')
vcfForQC = Channel.empty().mix(
vcfConcatenated.map {
variantcaller, idPatient, idSample, vcf, tbi ->
[variantcaller, vcf]
},
singleStrelkaOutput.map {
variantcaller, idPatient, idSample, vcf, tbi ->
[variantcaller, vcf[1]]
},
singleMantaOutput.map {
variantcaller, idPatient, idSample, vcf, tbi ->
[variantcaller, vcf[2]]
})
(vcfForBCFtools, vcfForVCFtools) = vcfForQC.into(2)
process RunBcftoolsStats {
tag {"${variantCaller} - ${vcf}"}
publishDir "${params.outDir}/Reports/BCFToolsStats", mode: params.publishDirMode
input:
set variantCaller, file(vcf) from vcfForBCFtools
output:
file ("*.bcf.tools.stats.out") into bcfReport
when: !params.noReports
script: QC.bcftools(vcf)
}
bcfReport.dump(tag:'BCFTools')
process RunVcftools {
tag {"${variantCaller} - ${vcf}"}
publishDir "${params.outDir}/Reports/VCFTools", mode: params.publishDirMode
input:
set variantCaller, file(vcf) from vcfForVCFtools
output:
file ("${reducedVCF}.*") into vcfReport
when: !params.noReports
script:
reducedVCF = SarekUtils.reduceVCF(vcf)
QC.vcftools(vcf)
}
vcfReport.dump(tag:'VCFTools')
/*
================================================================================
= F U N C T I O N S =
================================================================================
*/
def checkParamReturnFile(item) {
params."${item}" = params.genomes[params.genome]."${item}"
return file(params."${item}")
}
def checkUppmaxProject() {
// check if UPPMAX project number is specified
return !(workflow.profile == 'slurm' && !params.project)
}
def defineReferenceMap(tools) {
if (!(params.genome in params.genomes)) exit 1, "Genome ${params.genome} not found in configuration"
def referenceMap =
[
'genomeFile' : checkParamReturnFile("genomeFile"),
'genomeIndex' : checkParamReturnFile("genomeIndex"),
'intervals' : checkParamReturnFile("intervals")
]
if ('haplotypecaller' in tools) {
referenceMap.putAll(
'dbsnp' : checkParamReturnFile("dbsnp"),
'dbsnpIndex' : checkParamReturnFile("dbsnpIndex"),
'genomeDict' : checkParamReturnFile("genomeDict")
)
}
return referenceMap
}
def defineToolList() {
return [
'ascat',
'freebayes',
'haplotypecaller',
'manta',
'mutect2',
'strelka'
]
}
def grabRevision() {
// Return the same string executed from github or not
return workflow.revision ?: workflow.commitId ?: workflow.scriptId.substring(0,10)
}
def helpMessage() {
// Display help message
this.sarekMessage()
log.info " Usage:"
log.info " nextflow run germlineVC.nf --sample <file.tsv> [--tools TOOL[,TOOL]] --genome <Genome>"
log.info " --sample <file.tsv>"
log.info " Specify a TSV file containing paths to sample files."
log.info " --test"
log.info " Use a test sample."
log.info " --noReports"
log.info " Disable QC tools and MultiQC to generate a HTML report"
log.info " --tools"
log.info " Option to configure which tools to use in the workflow."
log.info " Different tools to be separated by commas."
log.info " Possible values are:"
log.info " strelka (use Strelka for VC)"
log.info " haplotypecaller (use HaplotypeCaller for normal bams VC)"
log.info " manta (use Manta for SV)"
log.info " --genome <Genome>"
log.info " Use a specific genome version."
log.info " Possible values are:"
log.info " GRCh37"
log.info " GRCh38 (Default)"
log.info " smallGRCh37 (Use a small reference (Tests only))"
log.info " --onlyQC"
log.info " Run only QC tools and gather reports"
log.info " --help"
log.info " you're reading it"
}
def minimalInformationMessage() {
// Minimal information message
log.info "Command Line: " + workflow.commandLine
log.info "Profile : " + workflow.profile
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 "TSV file : " + tsvFile
log.info "Genome : " + params.genome
log.info "Genome_base : " + params.genome_base
log.info "Target BED : " + params.targetBED
log.info "Tools : " + tools.join(', ')
log.info "Containers"
if (params.repository != "") log.info " Repository : " + params.repository
if (params.containerPath != "") log.info " ContainerPath: " + params.containerPath
log.info "Reference files used:"
log.info " dbsnp :\n\t" + referenceMap.dbsnp
log.info "\t" + referenceMap.dbsnpIndex
log.info " genome :\n\t" + referenceMap.genomeFile
log.info "\t" + referenceMap.genomeDict
log.info "\t" + referenceMap.genomeIndex
log.info " intervals :\n\t" + referenceMap.intervals
}
def nextflowMessage() {
// Nextflow message (version + build)
log.info "N E X T F L O W ~ version ${workflow.nextflow.version} ${workflow.nextflow.build}"
}
def sarekMessage() {
// Display Sarek message
log.info "Sarek - Workflow For Somatic And Germline Variations ~ ${workflow.manifest.version} - " + this.grabRevision() + (workflow.commitId ? " [${workflow.commitId}]" : "")
}
def startMessage() {
// Display start message
SarekUtils.sarek_ascii()
this.sarekMessage()
this.minimalInformationMessage()
}
workflow.onComplete {
// Display complete message
this.nextflowMessage()
this.sarekMessage()
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.sarekMessage()
log.info "Workflow execution stopped with the following message:"
log.info " " + workflow.errorMessage
}