-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.nf
1161 lines (953 loc) · 35 KB
/
main.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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env nextflow
genome_file = file(params.genome_file)
OUTDIR = params.outdir+'/'+params.subdir
CRONDIR = params.crondir
csv = file(params.csv)
println(csv)
workflow.onComplete {
def msg = """\
Pipeline execution summary
---------------------------
Completed at: ${workflow.complete}
Duration : ${workflow.duration}
Success : ${workflow.success}
scriptFile : ${workflow.scriptFile}
workDir : ${workflow.workDir}
exit status : ${workflow.exitStatus}
errorMessage: ${workflow.errorMessage}
errorReport :
"""
.stripIndent()
def error = """\
${workflow.errorReport}
"""
.stripIndent()
base = csv.getBaseName()
logFile = file("/fs1/results/cron/logs/" + base + ".complete")
logFile.text = msg
logFile.append(error)
}
// Print commit-version of active deployment
file(params.git)
.readLines()
.each { println "git commit-hash: "+it }
// Print active container
container = file(params.container).toRealPath()
println("container: "+container)
Channel
.fromPath(params.csv).splitCsv(header:true)
.map{ row-> tuple(row.group, row.id, row.type, file(row.read1), file(row.read2)) }
.into { fastq_umi; fastq_noumi; meta_nocnv }
Channel
.fromPath(params.csv).splitCsv(header:true)
.map{ row-> tuple(row.group, row.id, row.type, (row.containsKey("ffpe") ? row.ffpe : false)) }
.into { meta_aggregate; meta_germline; meta_pon; meta_cnvkit; meta_melt; meta_cnvplot }
Channel
.fromPath(params.csv).splitCsv(header:true)
.map{ row-> tuple(row.group, row.type, row.clarity_sample_id, row.clarity_pool_id, row.diagnosis) }
.set { meta_coyote }
Channel
.fromPath(params.csv).splitCsv(header:true)
.map{ row-> tuple(row.id, row.read1, row.read2) }
.set{ meta_qc }
Channel
.fromPath(params.csv).splitCsv(header:true)
.map{ row-> tuple(row.group, row.id, row.type, row.read1, row.read2) }
.set{ meta_contamination }
Channel
.fromPath(params.csv).splitCsv(header:true)
.map{ row-> tuple(row.group, row.id, row.type, row.clarity_sample_id, row.clarity_pool_id) }
.set { meta_const }
// Split bed file in to smaller parts to be used for parallel variant calling
Channel
.fromPath("${params.regions_bed}")
.ifEmpty { exit 1, "Regions bed file not found: ${params.regions_bed}" }
.splitText( by: 200, file: 'bedpart.bed' )
.into { beds_mutect; beds_freebayes; beds_tnscope; beds_vardict }
process bwa_umi {
publishDir "${OUTDIR}/bam", mode: 'copy', overwrite: true, pattern: '*umi.sort.bam*'
cpus params.cpu_all
memory '128 GB'
time '2h'
errorStrategy 'retry'
maxErrors 5
tag "$id"
scratch true
stageInMode 'copy'
stageOutMode 'copy'
input:
set group, id, type, file(r1), file(r2) from fastq_umi
output:
set group, id, type, file("${id}.${type}.bwa.umi.sort.bam"), file("${id}.${type}.bwa.umi.sort.bam.bai") into bam_umi_bqsr, bam_umi_confirm
set group, id, type, file("${id}.${type}.bwa.sort.bam"), file("${id}.${type}.bwa.sort.bam.bai") into bam_umi_markdup
when:
params.umi
"""
export skip_coord_end=true
sentieon umi extract -d 3M2S+T,3M2S+T $r1 $r2 \\
|sentieon bwa mem \\
-R "@RG\\tID:$id\\tSM:$id\\tLB:$id\\tPL:illumina" \\
-t ${task.cpus} \\
-p -C $genome_file - \\
|tee -a noumi.sam \\
|sentieon umi consensus --copy_tags XR,RX,MI,XZ -o consensus.fastq.gz
sentieon bwa mem \\
-R "@RG\\tID:$id\\tSM:$id\\tLB:$id\\tPL:illumina" \\
-t ${task.cpus} \\
-p -C $genome_file consensus.fastq.gz \\
|sentieon util sort -i - \\
-o ${id}.${type}.bwa.umi.sort.bam \\
--sam2bam --umi_post_process
sentieon util sort -i noumi.sam -o ${id}.${type}.bwa.sort.bam --sam2bam
rm noumi.sam
touch dedup_metrics.txt
"""
}
process bwa_align {
cpus params.cpu_all
memory '64 GB'
time '2h'
tag "$id"
input:
set group, id, type, file(r1), file(r2) from fastq_noumi
output:
set group, id, type, file("${id}.${type}.bwa.sort.bam"), file("${id}.${type}.bwa.sort.bam.bai") into bam_markdup
when:
!params.umi
script:
if( params.sentieon_bwa ) {
"""
sentieon bwa mem -M -R '@RG\\tID:${id}\\tSM:${id}\\tPL:illumina' -t ${task.cpus} $genome_file $r1 $r2 \\
| sentieon util sort -r $genome_file -o ${id}.${type}.bwa.sort.bam -t ${task.cpus} --sam2bam -i -
"""
}
else {
"""
bwa mem -R '@RG\\tID:${id}\\tSM:${id}\\tPL:illumina' -M -t ${task.cpus} $genome_file $r1 $r2 \\
| samtools view -Sb - \\
| samtools sort -o ${id}.${type}.bwa.sort.bam -
samtools index ${id}.${type}.bwa.sort.bam
"""
}
}
process markdup {
cpus params.cpu_many
memory '64 GB'
time '1h'
tag "$id"
scratch true
stageInMode 'copy'
stageOutMode 'copy'
input:
set group, id, type, file(bam), file(bai) from bam_markdup.mix(bam_umi_markdup)
output:
set group, id, type, file("${id}.${type}.dedup.bam"), file("${id}.${type}.dedup.bam.bai") into bam_bqsr
set group, id, type, file("${id}.${type}.dedup.bam"), file("${id}.${type}.dedup.bam.bai"), file("dedup_metrics.txt") into bam_qc, bam_bqsr2, bam_lowcov
"""
sentieon driver -t ${task.cpus} -i $bam --algo LocusCollector --fun score_info score.gz
sentieon driver -t ${task.cpus} -i $bam --algo Dedup --score_info score.gz --metrics dedup_metrics.txt ${id}.${type}.dedup.bam
"""
}
// FIXME: Temporarily broke the non-UMI track since bam_umi_bqsr
// and bam_bqsr collide here for UMI track. Figure out how
// to use only bam_umi_bqsr when params.umi==true
process bqsr_umi {
cpus params.cpu_some
memory '16 GB'
time '1h'
tag "$id"
scratch true
stageInMode 'copy'
stageOutMode 'copy'
input:
set group, id, type, file(bam), file(bai) from bam_umi_bqsr
output:
set group, id, type, file(bam), file(bai), file("${id}.bqsr.table") into bam_freebayes, bam_vardict, bam_tnscope, bam_cnvkit, bam_varli
when:
params.umi
"""
sentieon driver -t ${task.cpus} -r $genome_file -i $bam --algo QualCal ${id}.bqsr.table
"""
}
process bqsr_to_constitutional {
cpus params.cpu_some
memory '16 GB'
time '1h'
tag "$id"
publishDir "${OUTDIR}/bqsr", mode: 'copy', overwrite: true, pattern: '*.bqsr*'
publishDir "${OUTDIR}/csv", mode: 'copy', overwrite: true, pattern: '*.csv*'
//scratch true
//stageInMode 'copy'
//stageOutMode 'copy'
when:
mode == "neverever"
input:
set group, id, type, file(bam), file(bai), file(dedup), cid, poolid from bam_bqsr2.join(meta_const, by: [0,1,2]).filter{ item -> item[2] == 'N'}
output:
set group, id, type, file(bam), file(bai), file("${id}.const.bqsr") into input_const
file("${id}.const.csv") into csv_const
"""
sentieon driver -t ${task.cpus} -r $genome_file -i $bam --algo QualCal ${id}.const.bqsr
cat $params.const_csv_template > ${id}.const.csv
echo $cid,$id,proband,oncov1-0-test,M,ovarian-normal,affected,$id,,,$poolid,illumina,${OUTDIR}/bam/$bam,${OUTDIR}/bqsr/${id}.const.bqsr,,screening >> ${id}.const.csv
/fs1/bjorn/bnf-scripts/start_nextflow_analysis.pl ${id}.const.csv
"""
}
process sentieon_qc {
cpus params.cpu_many
memory '32 GB'
publishDir "${OUTDIR}/QC", mode: 'copy', overwrite: 'true', pattern: '*.QC*'
time '1h'
tag "$id"
scratch true
stageInMode 'copy'
stageOutMode 'copy'
input:
set group, id, type, file(bam), file(bai), file(dedup) from bam_qc
output:
set group, id, type, file(bam), file(bai), file("${id}_is_metrics.txt") into all_pindel, bam_manta, bam_melt, bam_delly
set id, type, file("${id}_${type}.QC") into qc_cdm
set group, id, type, file("${id}_${type}.QC") into qc_melt
file("*.txt")
"""
sentieon driver \\
--interval $params.regions_bed -r $genome_file -t ${task.cpus} -i ${bam} \\
--algo MeanQualityByCycle mq_metrics.txt --algo QualDistribution qd_metrics.txt \\
--algo GCBias --summary gc_summary.txt gc_metrics.txt --algo AlignmentStat aln_metrics.txt \\
--algo InsertSizeMetricAlgo is_metrics.txt \\
--algo CoverageMetrics --cov_thresh 1 --cov_thresh 10 --cov_thresh 30 --cov_thresh 100 --cov_thresh 250 --cov_thresh 500 cov_metrics.txt
sentieon driver \\
-r $genome_file -t ${task.cpus} -i ${bam} \\
--algo HsMetricAlgo --targets_list $params.interval_list --baits_list $params.interval_list hs_metrics.txt
cp is_metrics.txt ${id}_is_metrics.txt
qc_sentieon.pl ${id}_${type} panel > ${id}_${type}.QC
"""
}
process lowcov {
cpus 1
memory '5 GB'
publishDir "${OUTDIR}/QC", mode: 'copy', overwrite: 'true'
time '1h'
tag "$id"
input:
set group, id, type, file(bam), file(bai), file(dedup) from bam_lowcov
output:
set group, type, file("${id}.lowcov.bed") into lowcov_coyote
"""
source activate sambamba
panel_depth.pl $bam $params.regions_proteincoding > lowcov.bed
overlapping_genes.pl lowcov.bed $params.gene_regions > ${id}.lowcov.bed
"""
}
// Load QC data into CDM (via middleman)
process qc_to_cdm {
cpus 1
publishDir "${CRONDIR}/qc", mode: 'copy' , overwrite: 'true'
tag "$id"
time '10m'
memory '50 MB'
input:
set id, type, file(qc), r1, r2 from qc_cdm.join(meta_qc)
output:
file("${id}.cdm") into cdm_done
when:
!params.noupload
script:
parts = r1.split('/')
idx = parts.findIndexOf {it ==~ /......_......_...._........../}
rundir = parts[0..idx].join("/")
"""
echo "--run-folder $rundir --sample-id $id --assay $params.cdm --qc ${OUTDIR}/QC/${id}_${type}.QC" > ${id}.cdm
"""
}
process qc_values {
tag "$id"
time '2m'
memory '50 MB'
tag "$id"
input:
set group, id, type, qc from qc_melt
output:
set group, id, type, val(INS_SIZE), val(MEAN_DEPTH), val(COV_DEV) into qc_melt_val
set group, id, val(INS_SIZE), val(MEAN_DEPTH), val(COV_DEV) into qc_cnvkit_val
script:
// Collect qc-data if possible from normal sample, if only tumor; tumor
def ins_dev
def coverage
def ins_size
qc.readLines().each{
if (it =~ /\"(ins_size_dev)\" : \"(\S+)\"/) {
ins_dev = it =~ /\"(ins_size_dev)\" : \"(\S+)\"/
}
if (it =~ /\"(mean_coverage)\" : \"(\S+)\"/) {
coverage = it =~ /\"(mean_coverage)\" : \"(\S+)\"/
}
if (it =~ /\"(ins_size)\" : \"(\S+)\"/) {
ins_size = it =~ /\"(ins_size)\" : \"(\S+)\"/
}
}
INS_SIZE = ins_size[0][2]
MEAN_DEPTH = coverage[0][2]
COV_DEV = ins_dev[0][2]
"""
echo $INS_SIZE $MEAN_DEPTH $COV_DEV > qc.val
"""
}
process freebayes {
cpus 1
time '40m'
tag "$group"
input:
set group, id, type, file(bams), file(bais), file(bqsr) from bam_freebayes.groupTuple()
each file(bed) from beds_freebayes
output:
set val("freebayes"), group, file("freebayes_${bed}.vcf") into vcfparts_freebayes
when:
params.freebayes
script:
if( id.size() >= 2 ) {
tumor_idx = type.findIndexOf{ it == 'tumor' || it == 'T' }
normal_idx = type.findIndexOf{ it == 'normal' || it == 'N' }
"""
freebayes -f $genome_file -t $bed --pooled-continuous --pooled-discrete --min-repeat-entropy 1 -F 0.03 ${bams[tumor_idx]} ${bams[normal_idx]} > freebayes_${bed}.vcf.raw
vcffilter -F LowCov -f "DP > 500" -f "QA > 1500" freebayes_${bed}.vcf.raw | vcffilter -F LowFrq -o -f "AB > 0.05" -f "AB = 0" | vcfglxgt > freebayes_${bed}.filt1.vcf
filter_freebayes_somatic.pl freebayes_${bed}.filt1.vcf ${id[tumor_idx]} ${id[normal_idx]} > freebayes_${bed}.vcf
"""
}
else if( id.size() == 1 ) {
"""
freebayes -f $genome_file -t $bed --pooled-continuous --pooled-discrete --min-repeat-entropy 1 -F 0.03 $bams > freebayes_${bed}.vcf.raw
vcffilter -F LowCov -f "DP > 500" -f "QA > 1500" freebayes_${bed}.vcf.raw | vcffilter -F LowFrq -o -f "AB > 0.05" -f "AB = 0" | vcfglxgt > freebayes_${bed}.filt1.vcf
filter_freebayes_unpaired.pl freebayes_${bed}.filt1.vcf > freebayes_${bed}.vcf
"""
}
}
process vardict {
cpus 1
time '2h'
tag "$group"
memory '15GB'
input:
set group, id, type, file(bams), file(bais), file(bqsr) from bam_vardict.groupTuple()
each file(bed) from beds_vardict
output:
set val("vardict"), group, file("vardict_${bed}.vcf") into vcfparts_vardict
when:
params.vardict
script:
if( id.size() >= 2 ) {
tumor_idx = type.findIndexOf{ it == 'tumor' || it == 'T' }
normal_idx = type.findIndexOf{ it == 'normal' || it == 'N' }
"""
vardict-java -G $genome_file -f 0.01 -N ${id[tumor_idx]} -b "${bams[tumor_idx]}|${bams[normal_idx]}" -c 1 -S 2 -E 3 -g 4 -U $bed \\
| testsomatic.R | var2vcf_paired.pl -N "${id[tumor_idx]}|${id[normal_idx]}" -f 0.01 > vardict_${bed}.vcf.raw
filter_vardict_somatic.pl vardict_${bed}.vcf.raw ${id[tumor_idx]} ${id[normal_idx]} > vardict_${bed}.vcf
"""
}
else if( id.size() == 1 ) {
"""
vardict-java -G $genome_file -f 0.03 -N ${id[0]} -b ${bams[0]} -c 1 -S 2 -E 3 -g 4 -U $bed | teststrandbias.R | var2vcf_valid.pl -N ${id[0]} -E -f 0.01 > vardict_${bed}.vcf.raw
filter_vardict_unpaired.pl vardict_${bed}.vcf.raw > vardict_${bed}.vcf
"""
}
}
process tnscope {
cpus params.cpu_some
time '2h'
tag "$group"
input:
set group, id, type, file(bams), file(bais), file(bqsr) from bam_tnscope.groupTuple()
each file(bed) from beds_tnscope
output:
set val("tnscope"), group, file("tnscope_${bed}.vcf") into vcfparts_tnscope
when:
params.tnscope
script:
tumor_idx = type.findIndexOf{ it == 'tumor' || it == 'T' }
normal_idx = type.findIndexOf{ it == 'normal' || it == 'N' }
if( id.size() >= 2 ) {
"""
sentieon driver -t ${task.cpus} \\
-r $genome_file \\
-i ${bams[tumor_idx]} -q ${bqsr[tumor_idx]} \\
-i ${bams[normal_idx]} -q ${bqsr[normal_idx]} \\
--interval $bed --algo TNscope \\
--tumor_sample ${id[tumor_idx]} --normal_sample ${id[normal_idx]} \\
--clip_by_minbq 1 --max_error_per_read 3 --min_init_tumor_lod 2.0 \\
--min_base_qual 10 --min_base_qual_asm 10 --min_tumor_allele_frac 0.0005 \\
tnscope_${bed}.vcf.raw
filter_tnscope_somatic.pl tnscope_${bed}.vcf.raw ${id[tumor_idx]} ${id[normal_idx]} > tnscope_${bed}.vcf
"""
}
else {
"""
sentieon driver -t ${task.cpus} -r $genome_file \\
-i ${bams} -q ${bqsr} \\
--interval $bed --algo TNscope \\
--tumor_sample ${id[0]} \\
--clip_by_minbq 1 --max_error_per_read 3 --min_init_tumor_lod 2.0 \\
--min_base_qual 10 --min_base_qual_asm 10 --min_tumor_allele_frac 0.0005 \\
tnscope_${bed}.vcf.raw
filter_tnscope_unpaired.pl tnscope_${bed}.vcf.raw > tnscope_${bed}.vcf
"""
}
}
process pindel {
cpus params.cpu_some
time '1h'
publishDir "${OUTDIR}/vcf", mode: 'copy', overwrite: true
tag "$group"
input:
set group, id, type, file(bams), file(bais), file(ins_size) from all_pindel.groupTuple()
output:
set group, val("pindel"), file("${group}_pindel.vcf") into vcf_pindel
when:
params.pindel
script:
if( id.size() >= 2 ) {
tumor_idx = type.findIndexOf{ it == 'tumor' || it == 'T' }
normal_idx = type.findIndexOf{ it == 'normal' || it == 'N' }
ins_tumor = ins_size[tumor_idx]
ins_normal = ins_size[normal_idx]
bam_tumor = bams[tumor_idx]
bam_normal = bams[normal_idx]
id_tumor = id[tumor_idx]
id_normal = id[normal_idx]
"""
INS_T="\$(sed -n '3p' $ins_tumor | cut -f 1 | awk '{print int(\$1+0.5)}')"
INS_N="\$(sed -n '3p' $ins_normal | cut -f 1 | awk '{print int(\$1+0.5)}')"
echo "$bam_tumor\t\$INS_T\t$id_tumor" > pindel_config
echo "$bam_normal\t\$INS_N\t$id_normal" >> pindel_config
pindel -f $genome_file -w 0.1 -x 2 -i pindel_config -j $params.pindel_regions_bed -o tmpout -T ${task.cpus}
pindel2vcf -P tmpout -r $genome_file -R hg19 -d 2015-01-01 -v ${group}_pindel_unfilt.vcf -is 10 -e 30 -he 0.01
filter_pindel_somatic.pl ${group}_pindel_unfilt.vcf ${group}_pindel.vcf
"""
}
else {
tumor_idx = type.findIndexOf{ it == 'tumor' || it == 'T' }
ins_tumor = ins_size[tumor_idx]
bam_tumor = bams[tumor_idx]
id_tumor = id[tumor_idx]
"""
INS_T="\$(sed -n '3p' $ins_tumor | cut -f 1 | awk '{print int(\$1+0.5)}')"
echo "$bam_tumor\t\$INS_T\t$id_tumor" > pindel_config
pindel -f $genome_file -w 0.1 -x 2 -i pindel_config -j $params.pindel_regions_bed -o tmpout -T ${task.cpus}
pindel2vcf -P tmpout -r $genome_file -R hg19 -d 2015-01-01 -v ${group}_pindel_unfilt.vcf -is 10 -e 30 -he 0.01
filter_pindel_somatic.pl ${group}_pindel_unfilt.vcf ${group}_pindel.vcf
"""
}
}
// Prepare vcf parts for concatenation
vcfparts_freebayes = vcfparts_freebayes.groupTuple(by:[0,1])
vcfparts_tnscope = vcfparts_tnscope.groupTuple(by:[0,1])
vcfparts_vardict = vcfparts_vardict.groupTuple(by:[0,1])
vcfs_to_concat = vcfparts_freebayes.mix(vcfparts_vardict).mix(vcfparts_tnscope)
process concatenate_vcfs {
cpus 1
publishDir "${OUTDIR}/vcf", mode: 'copy', overwrite: true
time '20m'
tag "$group"
input:
set vc, group, file(vcfs) from vcfs_to_concat
output:
set group, vc, file("${group}_${vc}.vcf.gz") into concatenated_vcfs, vcf_cnvkit
"""
vcf-concat $vcfs | vcf-sort -c | gzip -c > ${vc}.concat.vcf.gz
vt decompose ${vc}.concat.vcf.gz -o ${vc}.decomposed.vcf.gz
vt normalize ${vc}.decomposed.vcf.gz -r $genome_file | vt uniq - -o ${group}_${vc}.vcf.gz
"""
}
process cnvkit {
publishDir "${OUTDIR}/vcf", mode: 'copy', overwrite: true, pattern: '*.vcf'
publishDir "${OUTDIR}/gens", mode: 'copy', overwrite: true, pattern: '*.bed.gz*'
publishDir "${CRONDIR}/gens", mode: 'copy', overwrite: true, pattern: '*.gens'
cpus 1
time '1h'
tag "$id"
scratch true
stageInMode 'copy'
stageOutMode 'copy'
input:
set gr, id, type, file(bam), file(bai), file(bqsr), val(INS_SIZE), val(MEAN_DEPTH), val(COV_DEV), vc, file(vcf) from bam_cnvkit.join(qc_cnvkit_val, by:[0,1]) \
.combine(vcf_cnvkit.filter { item -> item[1] == 'freebayes' }, by:[0])
output:
set gr, id, type, file("${gr}.${id}.cnvkit_overview.png"), file("${gr}.${id}.call.cns"), file("${gr}.${id}.cnr"), file("${gr}.${id}.filtered") into geneplot_cnvkit
set gr, id, type, file("${gr}.${id}.filtered.vcf") into cnvkit_vcf
file("${gr}.${id}.cns") into cns_notcalled
file("*.bed.gz*")
file("${id}.gens") into gens_middleman
when:
params.cnvkit
script:
freebayes_idx = vc.findIndexOf{ it == 'freebayes' }
"""
set +eu
source activate py2
set -eu
cnvkit.py batch $bam -r $params.cnvkit_reference -d results/
cnvkit.py call results/*.cns -v $vcf -o ${gr}.${id}.call.cns
filter_cnvkit.pl ${gr}.${id}.call.cns $MEAN_DEPTH > ${gr}.${id}.filtered
cnvkit.py export vcf ${gr}.${id}.filtered -i "$id" > ${gr}.${id}.filtered.vcf
cnvkit.py scatter -s results/*.cn{s,r} -o ${gr}.${id}.cnvkit_overview.png -v ${vcf[freebayes_idx]} -i $id
cp results/*.cnr ${gr}.${id}.cnr
cp results/*.cns ${gr}.${id}.cns
generate_gens_data_from_cnvkit.pl ${gr}.${id}.cnr $vcf $id
echo "gens load sample --sample-id $id --genome-build 38 --baf ${params.gens_accessdir}/${id}.baf.bed.gz --coverage ${params.gens_accessdir}/${id}.cov.bed.gz" > ${id}.gens
"""
}
// Plot specific gene-regions. CNVkit 0.9.6 and forward introduced a bug in region plot, use 0.9.5 (091 wrong name, container has 0.9.5)
process gene_plot {
publishDir "${OUTDIR}/plots", mode: 'copy', overwrite: true, pattern: '*.png'
cpus 1
time '5m'
tag "$id"
input:
set gr, id, type, file(overview), file(cns), file(cnr), file(filtered) from geneplot_cnvkit
output:
set gr, id, type, file("${gr}.${id}.cnvkit.png") into cnvplot_coyote
script:
if (params.assay == "PARP_inhib") {
"""
set +eu
source activate old-cnvkit
set -eu
cnvkit.py scatter -s $cns $cnr -c 13:32165479-32549672 -o brca2.png --title 'BRCA2'
cnvkit.py scatter -s $cns $cnr -c 17:42894294-43350132 -o brca1.png --title 'BRCA1'
montage -mode concatenate -tile 1x *.png ${gr}.${id}.cnvkit.png
"""
}
else {
"""
mv ${gr}.${id}.cnvkit_overview.png ${gr}.${id}.cnvkit.png
"""
}
}
process melt {
publishDir "${OUTDIR}/vcf", mode: 'copy', overwrite: true
cpus 2
//container = '/fs1/resources/containers/container_twist-brca.sif'
memory '50 GB'
tag "$group"
scratch true
stageInMode 'copy'
stageOutMode 'copy'
input:
set group, id, type, file(bam), file(bai), file(bqsr), val(INS_SIZE), val(MEAN_DEPTH), val(COV_DEV) from bam_melt \
.join(qc_melt_val, by: [0,1,2])
when:
params.melt
output:
set group, id, type, file("${id}.melt.merged.vcf") into melt_vcf
"""
set +eu
source activate java8
set -eu
java -jar /opt/MELT.jar Single \\
-bamfile $bam \\
-r 150 \\
-h $genome_file \\
-n $params.bed_melt \\
-z 50000 \\
-d 50 -t $params.mei_list \\
-w . \\
-b 1/2/3/4/5/6/7/8/9/10/11/12/14/15/16/18/19/20/21/22 \\
-c $MEAN_DEPTH \\
-cov $COV_DEV \\
-e $INS_SIZE
source deactivate
merge_melt.pl $params.meltheader $id
"""
}
// MANTA SINGLE AND PAIRED
process manta {
publishDir "${OUTDIR}/vcf", mode: 'copy', overwrite: true
cpus 16
time '10h'
//container = '/fs1/resources/containers/wgs_2020-03-25.sif'
tag "$group"
scratch true
memory '10GB'
stageInMode 'copy'
stageOutMode 'copy'
input:
set group, id, type, file(bam), file(bai), file(bqsr) from bam_manta.groupTuple()
output:
set group, file("${group}_manta.vcf") into manta_vcf
when:
params.manta
script:
if(id.size() >= 2) {
tumor_idx = type.findIndexOf{ it == 'tumor' || it == 'T' }
normal_idx = type.findIndexOf{ it == 'normal' || it == 'N' }
normal = bam[normal_idx]
normal_id = id[normal_idx]
tumor = bam[tumor_idx]
tumor_id = id[tumor_idx]
"""
set +eu
source activate py2
set -eu
configManta.py \\
--tumorBam $tumor \\
--normalBam $normal \\
--reference $genome_file \\
--exome \\
--callRegions $params.bedgz \\
--generateEvidenceBam \\
--runDir .
python runWorkflow.py -m local -j ${task.cpus}
#filter_manta_paired.pl results/variants/somaticSV.vcf.gz > ${group}_manta.vcf
mv results/variants/somaticSV.vcf.gz ${group}_manta.vcf.gz
gunzip ${group}_manta.vcf.gz
"""
}
else {
"""
set +eu
source activate py2
set -eu
configManta.py \\
--tumorBam $bam \\
--reference $genome_file \\
--exome \\
--callRegions $params.bedgz \\
--generateEvidenceBam \\
--runDir .
python runWorkflow.py -m local -j ${task.cpus}
#filter_manta.pl results/variants/tumorSV.vcf.gz > ${group}_manta.vcf
mv results/variants/tumorSV.vcf.gz ${group}_manta.vcf.gz
gunzip ${group}_manta.vcf.gz
"""
}
}
// Delly SINGLE AND PAIRED
process delly {
publishDir "${OUTDIR}/vcf", mode: 'copy', overwrite: true
cpus 2
time '20h'
memory '10GB'
//container = '/fs1/resources/containers/wgs_2020-03-25.sif'
tag "$group"
input:
set group, id, type, file(bam), file(bai), file(bqsr) from bam_delly.groupTuple()
output:
set group, file("${group}.delly.filtered.vcf") into delly_vcf
when:
params.manta
script:
if(id.size() >= 2) {
tumor_idx = type.findIndexOf{ it == 'tumor' || it == 'T' }
normal_idx = type.findIndexOf{ it == 'normal' || it == 'N' }
normal = bam[normal_idx]
normal_id = id[normal_idx]
tumor = bam[tumor_idx]
tumor_id = id[tumor_idx]
"""
delly call -g $genome_file -o ${group}.delly.bcf $tumor $normal
bcftools view ${group}.delly.bcf > ${group}.delly.vcf
filter_delly.pl --vcf ${group}.delly.vcf --bed $params.regions_bed > ${group}.delly.filtered.vcf
"""
}
else {
"""
delly call -g $genome_file -o ${group}.delly.bcf $bam
bcftools view ${group}.delly.bcf > ${group}.delly.vcf
filter_delly.pl --vcf ${group}.delly.vcf --bed $params.regions_bed > ${group}.delly.filtered.vcf
"""
}
}
process single_cnv_pipe {
time '2m'
tag "$group"
when:
params.single_cnvcaller
input:
set group, id, type, file(read1), file(read2) from meta_nocnv
output:
set group, file("${group}.cnvs.agg.vcf") into cnvs_singlecaller
script:
"""
echo singe_cnv_caller_pipeline > ${group}.cnvs.agg.vcf
"""
}
process concat_cnv {
cpus 1
memory '1GB'
publishDir "${OUTDIR}/vcf", mode: 'copy', overwrite: true
//container = '/fs1/resources/containers/wgs_2020-03-25.sif'
time '20m'
tag "$group"
input:
set group, file(mantavcf), file(dellyvcf), id_c, type_c, file(cnvkitvcf), tissue_c, id_m, type_m, file(meltvcf), tissue_m from manta_vcf.join(delly_vcf) \
.join(cnvkit_vcf.join(meta_cnvkit, by:[0,1,2]).groupTuple()) \
.join(melt_vcf.join(meta_melt, by:[0,1,2]).groupTuple()).view()
output:
file("${group}_cnvkitagg.vcf") into aggcnvkit
set group, file("${group}.cnvs.agg.vcf") into cnvs
script:
if( id_c.size() >= 2 ) {
tumor_idx_c = type_c.findIndexOf{ it == 'tumor' || it == 'T' }
tumor_idx_m = type_m.findIndexOf{ it == 'tumor' || it == 'T' }
normal_idx_c = type_c.findIndexOf{ it == 'normal' || it == 'N' }
normal_idx_m = type_m.findIndexOf{ it == 'normal' || it == 'N' }
if (tissue_c[tumor_idx_c] == 'ffpe') {
cnvkitvcf2 = cnvkitvcf[normal_idx_c]
meltvcf = meltvcf[normal_idx_m]
}
else {
cnvkitvcf2 = cnvkitvcf[tumor_idx_c]
meltvcf = meltvcf[tumor_idx_m]
}
tmp = mantavcf.collect {it + ':manta ' } + dellyvcf.collect {it + ':delly ' }
vcfs = tmp.join(' ')
"""
aggregate_CNVkit.pl ${cnvkitvcf[tumor_idx_c]} ${id_c[tumor_idx_c]} ${cnvkitvcf[normal_idx_c]} ${id_c[normal_idx_c]} > ${group}_cnvkitagg.vcf
svdb --merge --vcf $vcfs ${group}_cnvkitagg.vcf:cnvkit --no_intra --pass_only --bnd_distance 2500 --overlap 0.7 --priority manta,delly,cnvkit > ${group}.merged.vcf
aggregate_cnv2_vcf.pl --vcfs ${group}.merged.vcf,$meltvcf \\
--tumor-id ${id_c[tumor_idx_c]} \\
--normal-id ${id_c[normal_idx_c]} \\
--paired paired \\
--sample-order ${id_c[tumor_idx_c]},${id_c[normal_idx_c]} > ${group}.cnvs.agg.vcf
"""
}
else {
tmp = mantavcf.collect {it + ':manta ' } + dellyvcf.collect {it + ':delly ' } + cnvkitvcf.collect {it + ':cnvkit ' }
vcfs = tmp.join(' ')
"""
touch ${group}_cnvkitagg.vcf
svdb --merge --vcf $vcfs --no_intra --pass_only --bnd_distance 2500 --overlap 0.7 --priority manta,delly,cnvkit > ${group}.merged.vcf
aggregate_cnv2_vcf.pl --vcfs ${group}.merged.vcf,$meltvcf --paired no > ${group}.cnvs.agg.vcf
"""
}
}
process aggregate_vcfs {
cpus 1
publishDir "${OUTDIR}/vcf", mode: 'copy', overwrite: true
time '20m'
tag "$group"
input:
set group, vc, file(vcfs), id, type, tissue, file(cnvs) from concatenated_vcfs.mix(vcf_pindel).groupTuple().join(meta_aggregate.groupTuple()).join(cnvs.mix(cnvs_singlecaller))
output:
set group, file("${group}.agg.vcf") into vcf_pon, vcf_done
script:
sample_order = id[0]
if( id.size() >= 2 ) {
tumor_idx = type.findIndexOf{ it == 'tumor' || it == 'T' }
normal_idx = type.findIndexOf{ it == 'normal' || it == 'N' }
sample_order = id[tumor_idx]+","+id[normal_idx]
}
if (params.single_cnvcaller) {
"""
aggregate_vcf.pl --vcf ${vcfs.sort(false) { a, b -> a.getBaseName() <=> b.getBaseName() }.join(",")} --sample-order ${sample_order} |vcf-sort -c > ${group}.agg.vcf
"""
}
else {
"""
aggregate_vcf.pl --vcf ${vcfs.sort(false) { a, b -> a.getBaseName() <=> b.getBaseName() }.join(",")} --sample-order ${sample_order} |vcf-sort -c > ${group}.agg.tmp.vcf
vcf-concat ${group}.agg.tmp.vcf $cnvs | vcf-sort -c > ${group}.agg.vcf
"""
}
}
process pon_filter {
publishDir "${OUTDIR}/vcf", mode: 'copy', overwrite: true
cpus 1
time '1h'
tag "$group"
memory '32 GB'
input:
set group, file(vcf), id, type, tissue from vcf_pon.join(meta_pon.groupTuple())
output:
set group, file("${group}.agg.pon.vcf") into vcf_vep
script:
if (params.assay == 'myeloid') {
def pons = []
if( params.freebayes ) { pons.push("freebayes="+params.PON_freebayes) }
if( params.vardict ) { pons.push("vardict="+params.PON_vardict) }
if( params.tnscope ) { pons.push("tnscope="+params.PON_tnscope) }
def pons_str = pons.join(",")
tumor_idx = type.findIndexOf{ it == 'tumor' || it == 'T' }
"""
filter_with_pon.pl --vcf $vcf --pons $pons_str --tumor-id ${id[tumor_idx]} > ${group}.agg.pon.vcf
"""
}
// werid placement, no PON for PARP_inhib, Adds enigma-db to vcf. Move to separate process?
else if (params.assay == 'PARP_inhib') {
"""
vcfanno_linux64 -lua /fs1/resources/ref/hg19/bed/scout/sv_tracks/silly.lua $params.vcfanno $vcf > ${group}.agg.pon.vcf
"""
}
}
process annotate_vep {
container = params.vepcon
publishDir "${OUTDIR}/vcf", mode: 'copy', overwrite: true
cpus params.cpu_many
time '1h'
tag "$group"
input:
set group, file(vcf) from vcf_vep
output:
set group, file("${group}.agg.pon.vep.vcf") into vcf_germline, vcf_contamination
"""
vep -i ${vcf} -o ${group}.agg.pon.vep.vcf \\
--offline --merged --everything --vcf --no_stats \\
--fork ${task.cpus} \\
--force_overwrite \\
--plugin CADD $params.CADD --plugin LoFtool \\
--fasta $params.VEP_FASTA \\
--dir_cache $params.VEP_CACHE --dir_plugins $params.VEP_CACHE/Plugins \\
--distance 200 \\
--custom $params.GNOMAD,gnomADg,vcf,exact,0,AF_popmax,AF,popmax \\
--custom $params.COSMIC,COSMIC,vcf,exact,0,CNT \\
--cache \\
"""
}
process mark_germlines {
publishDir "${OUTDIR}/vcf", mode: 'copy', overwrite: true
cpus params.cpu_many
time '20m'
tag "$group"
input:
set group, file(vcf), id, type, tissue from vcf_germline.join(meta_germline.groupTuple())
output:
set group, file("${group}.agg.pon.vep.markgerm.vcf") into vcf_umi
script:
if( id.size() >= 2 ) {
tumor_idx = type.findIndexOf{ it == 'tumor' || it == 'T' }