-
Notifications
You must be signed in to change notification settings - Fork 24
/
nextNEOpi.nf
6983 lines (5712 loc) · 206 KB
/
nextNEOpi.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
// enable DSL 1
nextflow.enable.dsl = 1
import org.yaml.snakeyaml.Yaml
import java.nio.file.Files
log.info ""
log.info " NEXTFLOW ~ version ${workflow.nextflow.version} ${workflow.nextflow.build}"
log.info "-------------------------------------------------------------------------"
log.info " Nextfow NeoAntigen Prediction Pipeline - nextNEOpi "
log.info "-------------------------------------------------------------------------"
log.info ""
log.info " Features: "
log.info " - somatic variants from tumor + matched normal samples"
log.info " - CNV analysis"
log.info " - tumor muational burden"
log.info " - class I and class II HLA typing"
log.info " - gene fusion peptide prediction using RNAseq data"
log.info " - peptide MHC binding perdiction"
log.info " - clonality of neoantigens"
log.info " - expression of neoantigens"
log.info ""
log.info "-------------------------------------------------------------------------"
log.info "C O N F I G U R A T I O N"
log.info ""
log.info "Command Line: \t\t " + workflow.commandLine
log.info "Working Directory: \t " + workflow.workDir
log.info "Output Directory: \t " + params.outputDir
log.info ""
log.info "I N P U T"
log.info ""
log.info "batch file: \t\t " + params.batchFile
log.info ""
log.info "Please check --help for further instruction"
log.info "-------------------------------------------------------------------------"
// Check if License(s) were accepted
params.accept_license = false
if (params.accept_license) {
acceptLicense()
} else {
checkLicense()
}
/*
________________________________________________________________________________
C O N F I G U R A T I O N
________________________________________________________________________________
*/
if (params.help) exit 0, helpMessage()
// switch for enable/disable processes (debug/devel only: use if(params.RUNTHIS) { .... })
params.RUNTHIS = false
// default is not to get bams as input data
bamInput = false
// initialize RNA tag seq
have_RNA_tag_seq = params.RNA_tag_seq
// set and initialize the Exome capture kit
setExomeCaptureKit(params.exomeCaptureKit)
// check conda channels
if (params.enable_conda) {
checkCondaChannels()
}
// check IEDB dir
check_iedb_dir(params.databases.IEDB_dir)
// check MHCflurry dir
check_mhcflurry_dir(params.databases.MHCFLURRY_dir)
// set and check references and databases
reference = defineResources('references', params.WES, params.HLAHD_DIR)
database = defineResources('databases', params.WES, params.HLAHD_DIR)
// create tmp dir and make sure we have the realpath for it
tmpDir = mkTmpDir(params.tmpDir)
/*--------------------------------------------------
For workflow summary
---------------------------------------------------*/
// 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
}
// Summary
def summary = [:]
summary['Pipeline Name'] = 'icbi/nextNEOpi'
summary['Pipeline Version'] = workflow.manifest.version
summary['Batch file'] = params.batchFile
summary['Read length'] = params.readLength
summary['Exome capture kit'] = params.exomeCaptureKit
summary['Fasta Ref'] = params.references.RefFasta
summary['MillsGold'] = params.databases.MillsGold
summary['hcSNPS1000G'] = params.databases.hcSNPS1000G
summary['HapMap'] = params.databases.HapMap
summary['Cosmic'] = params.databases.Cosmic
summary['DBSNP'] = params.databases.DBSNP
summary['GnomAD'] = params.databases.GnomAD
summary['GnomADfull'] = params.databases.GnomADfull
summary['KnownIndels'] = params.databases.KnownIndels
summary['BlastDB'] = params.references.ProteinBlastDBdir
summary['priority variant Caller'] = params.primaryCaller
summary['Mutect 1 and 2 minAD'] = params.minAD
summary['VarScan min_cov'] = params.min_cov
summary['VarScan min_cov_tumor'] = params.min_cov_tumor
summary['VarScan min_cov_normal'] = params.min_cov_normal
summary['VarScan min_freq_for_hom'] = params.min_freq_for_hom
summary['VarScan somatic_pvalue'] = params.somatic_pvalue
summary['VarScan somatic_somaticpvalue'] = params.somatic_somaticpvalue
summary['VarScan strand_filter'] = params.strand_filter
summary['VarScan processSomatic_pvalue'] = params.processSomatic_pvalue
summary['VarScan max_normal_freq'] = params.max_normal_freq
summary['VarScan min_tumor_freq'] = params.min_tumor_freq
summary['VarScan min_map_q'] = params.min_map_q
summary['VarScan min_base_q'] = params.min_base_q
summary['VEP assembly'] = params.vep_assembly
summary['VEP species'] = params.vep_species
summary['VEP options'] = params.vep_options
summary['Number of scatters'] = params.scatter_count
summary['Output dir'] = params.outputDir
summary['Working dir'] = workflow.workDir
summary['TMP dir'] = tmpDir
summary['Current home'] = "$HOME"
summary['Current user'] = "$USER"
summary['Current path'] = "$PWD"
summary['Picard maxRecordsInRam'] = params.maxRecordsInRam
summary['Script dir'] = workflow.projectDir
summary['Config Profile'] = workflow.profile
if(params.email) summary['E-mail Address'] = params.email
log.info summary.collect { k,v -> "${k.padRight(30)}: $v" }.join("\n")
log.info "-------------------------------------------------------------------------"
// End Summary
// determine the publishDirMode
def publishDirMode = get_publishMode(params.outputDir, params.publishDirMode)
// Check if we got a batch file
params.batchFile = null
if (params.batchFile == null) {
log.error "No sample sheet specified, please use --batchFile to pass the sample sheet"
exit(1)
}
def batchCSV = file(params.batchFile).splitCsv(header:true)
def validFQfields = ["sampleName",
"reads1",
"reads2",
"sampleType",
"HLAfile",
"sex"]
def validBAMfields = ["sampleName",
"bam",
"sampleType",
"HLAfile",
"sex"]
def validSampleTypes = ["tumor_DNA", "normal_DNA", "tumor_RNA"]
if (batchCSV.size() > 0) {
if (batchCSV[0].keySet().sort() == validFQfields.sort()) {
bamInput = false
} else if (batchCSV[0].keySet().sort() == validBAMfields.sort()) {
bamInput = true
} else {
exit 1, "Error: Incorrect fields in batch file, please check your batchFile"
}
} else {
exit 1, "Error: No samples found, please check your batchFile"
}
def raw_data = []
def custom_HLA_data = []
def t_map = [:]
def n_map = [:]
def r_map = [:]
def lt_map = [:]
for ( row in batchCSV ) {
def meta = [:]
def reads = []
meta.sampleName = row.sampleName.toString()
meta.sampleType = row.sampleType.toString()
t_map[meta.sampleName] = (t_map[meta.sampleName] == null) ? 0 : t_map[meta.sampleName]
n_map[meta.sampleName] = (n_map[meta.sampleName] == null) ? 0 : n_map[meta.sampleName]
if(row.sex) {
if (row.sex.toLowerCase() in ["xx", "female"]) {
meta.sex = "XX"
} else if (row.sex.toLowerCase() in ["xy", "male"]) {
meta.sex = "XY"
} else if (row.sex.toLowerCase() in ["none", "na"]) {
meta.sex = "None"
println("WARNING: " + row.sampleName + " sex not specified will infer from data")
} else {
exit 1, "sex should be one of: XX, xx, XY, xy, Female, female, Male, male, None, none, NA, got: " + row.sex
}
meta.maleRef = (meta.sex == "XY") ? true : false
} else {
println("WARNING: " + row.sampleName + " sex not specified will infer from data")
meta.sex = "None"
meta.maleRef = true
}
if (row.sampleType in validSampleTypes) {
t_map[meta.sampleName] += (row.sampleType == "tumor_DNA") ? 1 : 0
n_map[meta.sampleName] += (row.sampleType == "normal_DNA") ? 1 : 0
} else {
exit 1, "Error: Incorrect sampleType [got: " + row.sampleType + " - allowed: " + validSampleTypes + "]: please check your batchFile"
}
// remove stuff that is not required in the HLAfile channel
def meta_min = meta.clone()
meta_min.keySet().removeAll(['sampleType', 'libType'])
if (row.HLAfile) {
custom_HLA_data.add([meta_min, file(row.HLAfile, checkIfExists: true)])
} else {
custom_HLA_data.add([meta_min, []])
}
if (row.sampleType == "tumor_RNA") {
meta.have_RNA = true
r_map[row.sampleName] = true
}
if (! row.bam) {
meta.libType = "SE"
if (row.reads1) { reads.add(file(row.reads1, checkIfExists: true)) }
if (row.reads2) {
reads.add(file(row.reads2, checkIfExists: true))
meta.libType = "PE"
}
if (meta.sampleType != "tumor_RNA") {
if (lt_map[meta.sampleName] == null) {
lt_map[meta.sampleName] = meta.libType
} else {
if (lt_map[meta.sampleName] != meta.libType) {
exit 1, "Please do not mix pe and se for tumor/normal pairs: " + meta.sampleName + " - Not supported"
}
}
}
raw_data.add([meta, reads])
} else {
raw_data.add([meta, file(row.bam, checkIfExists: true)])
}
}
// check if we have T/N DNA pairs for all patients
raw_data.each {
record ->
if (t_map[record[0].sampleName] == 0) {
exit 1, "NO tumor DNA sample specified for: " + record[0].sampleName
}
if (n_map[record[0].sampleName] == 0) {
exit 1, "NO normal DNA sample specified for: " + record[0].sampleName
}
}
// update meta for RNA
raw_data.each {
record ->
record[0].have_RNA = (r_map[record[0].sampleName]) ? true : false
}
custom_HLA_data = custom_HLA_data.each {
record ->
record[0].have_RNA = (r_map[record[0].sampleName]) ? true : false
}.unique()
use_custom_hlas = (custom_HLA_data.size() > 0) ? true : false
batch_raw_data_ch = Channel.fromList(raw_data)
batch_custom_HLA_data_ch = Channel.fromList(custom_HLA_data)
if (bamInput == false) {
batch_raw_data_ch.map {
meta, fastq ->
[ meta, fastq ]
}
.groupTuple(by: [0])
.branch {
meta, fastq ->
single : fastq.unique().size() == 1
return [ meta, fastq.flatten().unique() ]
multiple: fastq.unique().size() > 1
return [ meta, fastq.flatten().unique() ]
}
.set { fastq_ch }
} else {
fastq_ch = Channel.of().branch{single: []; multiple: []}
}
// optional panel of normals file
pon_file = file(params.mutect2ponFile)
scatter_count = Channel.from(params.scatter_count)
padding = params.readLength + 100
MiXMHC2PRED = ( params.MiXMHC2PRED != "" ) ? file(params.MiXMHC2PRED) : ""
// check HLAHD & OptiType
have_HLAHD = false
run_OptiType = (params.disable_OptiType) ? false : true
if (params.HLAHD_DIR != "") {
HLAHD = file(params.HLAHD_DIR + "/bin/hlahd.sh")
if (checkToolAvailable(HLAHD, "exists", "warn")) {
HLAHD_DIR = file(params.HLAHD_DIR)
HLAHD_PATH = HLAHD_DIR + "/bin"
if(params.HLAHD_module != "") {
if (checkToolAvailable("bowtie2", "inPath", "warn", module=params.HLAHD_module)) {
have_HLAHD = true
}
} else {
if (checkToolAvailable("bowtie2", "inPath", "warn")) {
have_HLAHD = true
}
}
}
}
if (! have_HLAHD && run_OptiType) {
log.warn "WARNING: HLAHD not available - can not predict Class II neoepitopes"
} else if (! have_HLAHD && ! run_OptiType && use_custom_hlas) {
log.warn "WARNING: HLAHD not available and OptiType disabled - using only user supplied HLA types"
} else if (! have_HLAHD && ! run_OptiType && ! use_custom_hlas) {
exit 1, "ERROR: HLAHD not available and OptiType disabled - can not predict HLA types"
}
// check if all tools are installed when not running conda or singularity
have_vep = false
if (! workflow.profile.contains('conda') && ! workflow.profile.contains('singularity')) {
def execTools = ["fastqc", "fastp", "bwa", "samtools", "sambamba", "gatk", "vep", "bam-readcount",
"perl", "bgzip", "tabix", "bcftools", "yara_mapper", "python", "cnvkit.py",
"OptiTypePipeline.py", "alleleCounter", "freec", "Rscript", "java", "multiqc",
"sequenza-utils"]
for (tool in execTools) {
checkToolAvailable(tool, "inPath", "error")
}
VARSCAN = "java -jar " + file(params.VARSCAN)
have_vep = true
} else {
VARSCAN = "varscan "
}
// check if we have mutect1 installed
have_Mutect1 = false
if (params.MUTECT1 != "" && file(params.MUTECT1) && params.JAVA7 != "" && file(params.JAVA7)) {
if(checkToolAvailable(params.JAVA7, "exists", "warn") && checkToolAvailable(params.MUTECT1, "exists", "warn")) {
JAVA7 = file(params.JAVA7)
MUTECT1 = file(params.MUTECT1)
have_Mutect1 = true
}
}
// check if we have GATK3 installed
have_GATK3 = false
if (params.GATK3 != "" && file(params.GATK3) && params.JAVA8 != "" && file(params.JAVA8) && ! workflow.profile.contains('conda') && ! workflow.profile.contains('singularity')) {
if(checkToolAvailable(params.JAVA8, "inPath", "warn") && checkToolAvailable(params.GATK3, "exists", "warn")) {
JAVA8 = file(params.JAVA8)
GATK3 = file(params.GATK3)
have_GATK3 = true
}
} else if (workflow.profile.contains('singularity')) {
JAVA8 = "java"
GATK3 = "/usr/local/opt/gatk-3.8/GenomeAnalysisTK.jar"
have_GATK3 = true
} else if (workflow.profile.contains('conda')) {
JAVA8 = "java"
GATK3 = "\$CONDA_PREFIX/opt/gatk-3.8/GenomeAnalysisTK.jar"
have_GATK3 = true
}
// check MIXCR licence
if (params.TCR && params.MIXCR_lic != "") {
checkToolAvailable(params.MIXCR_lic, "exists", "error")
} else if (params.TCR && params.MIXCR_lic == "") {
exit 1, "ERROR: no MiXCR license file specified, please provide a MiXCR license file in params.config or by using the --MIXCR_lic option.\nIf you do not have a MiXCR license you may:\n\ta) run nextNEOpi with --TCR false\n\tb) request one at https://licensing.milaboratories.com"
}
/*
________________________________________________________________________________
P R O C E S S E S
________________________________________________________________________________
*/
gatk4_chck_file = file(baseDir + "/assets/.gatk4_install_ok.chck")
gatk4_chck_file.append()
nextNEOpiENV_setup_ch0 = Channel.value("OK")
/*
*********************************************
** P R E P R O C E S S I N G **
*********************************************
*/
// Handle BAM input files. We need to convert BAMs to Fastq
if(bamInput) {
process check_PE {
label 'nextNEOpiENV'
tag "$meta.sampleName : $meta.sampleType"
input:
tuple(
val(meta),
path(bam)
) from batch_raw_data_ch
val (nextNEOpiENV_setup) from nextNEOpiENV_setup_ch0
output:
tuple(
val(meta),
path(bam),
stdout
) into bam_ch
script:
"""
check_pe.py $bam
"""
}
(bam_ch, check_seqLib_ch) = bam_ch.into(2)
seqLibTypes_ok = Channel.value(check_seqLibTypes_ok(check_seqLib_ch))
process bam2fastq {
label 'nextNEOpiENV'
tag "$meta.sampleName : $meta.sampleType"
publishDir "${params.outputDir}/analyses/${meta.sampleName}/01_preprocessing",
mode: publishDirMode
input:
tuple(
val(meta),
path(bam),
val(libType),
val(libOK)
) from bam_ch
.combine(seqLibTypes_ok)
val (nextNEOpiENV_setup) from nextNEOpiENV_setup_ch0
output:
tuple(
val(meta),
path("${prefix}_R*.fastq.gz"),
) into (
bam_fastq_ch
)
script:
def STperThreadMem = (int) Math.max(((int) Math.floor((task.memory.toGiga() - 4) / task.cpus)), 1)
prefix = meta.sampleName + "_" + meta.sampleType
meta.libType = libType
if (libType == "PE")
"""
samtools sort -@ ${task.cpus} -m ${STperThreadMem}G -l 0 -n ${bam} | \\
samtools fastq \\
-@ ${task.cpus} \\
-c 5 \\
-1 ${prefix}_R1.fastq.gz \\
-2 ${prefix}_R2.fastq.gz \\
-0 /dev/null -s /dev/null \\
-n \\
/dev/stdin
"""
else if (libType == "SE")
"""
samtools fastq \\
-@ ${task.cpus} \\
-n \\
${bam} | \\
bgzip -@ ${task.cpus} -c /dev/stdin > ${prefix}_R1.fastq.gz
"""
}
} else {
bam_fastq_ch = channel.empty()
}
// END BAM input handling
// merge multi run or multi lane sample fastq
process merge_fastq {
tag "$meta.sampleName : $meta.sampleType"
publishDir "${params.outputDir}/${meta.sampleName}/01_preprocessing/",
mode: publishDirMode,
enabled: params.fullOutput
input:
tuple meta, path("R1/R1.*"), path("R2/R2.*") from fastq_ch.multiple.map{
meta, r -> {
def r1 = []
def r2 = []
r.eachWithIndex{ v, ix -> ( ix & 1 ? r2 : r1 ) << v }
tuple( meta, r1.sort(), r2.sort())
}
}
output:
tuple val(meta), path("*_R{1,2}.merged.fastq.gz") into merged_fastq_ch
script:
def prefix = meta.sampleName + "_" + meta.sampleType
"""
cat R1/* > ${prefix}_R1.merged.fastq.gz
cat R2/* > ${prefix}_R2.merged.fastq.gz
"""
}
// here we have our final raw fastq files:
// merged if multi lane runs are used
// bam2fastq if bam files are used
(raw_reads_ch, fastqc_reads_ch) = merged_fastq_ch.mix(fastq_ch.single, bam_fastq_ch).into(2)
// Common region files preparation for faster processing
if (params.WES) {
process 'RegionsBedToIntervalList' {
label 'nextNEOpiENV'
tag 'RegionsBedToIntervalList'
publishDir "$params.outputDir/supplemental/00_prepare_Intervals/",
mode: publishDirMode
input:
tuple(
path(RefDict),
path(RegionsBed)
) from Channel.value(
[ reference.RefDict,
reference.RegionsBed ]
)
val (nextNEOpiENV_setup) from nextNEOpiENV_setup_ch0
output:
path(
"${RegionsBed.baseName}.interval_list"
) into (
RegionsBedToIntervalList_out_ch0,
RegionsBedToIntervalList_out_ch1
)
script:
def JAVA_Xmx = '-Xmx' + task.memory.toGiga() + "G"
"""
gatk --java-options ${JAVA_Xmx} BedToIntervalList \\
-I ${RegionsBed} \\
-O ${RegionsBed.baseName}.interval_list \\
-SD $RefDict
"""
}
process 'BaitsBedToIntervalList' {
label 'nextNEOpiENV'
tag 'BaitsBedToIntervalList'
publishDir "$params.outputDir/supplemental/00_prepare_Intervals/",
mode: publishDirMode
input:
tuple(
path(RefDict),
path(BaitsBed)
) from Channel.value(
[ reference.RefDict,
reference.BaitsBed ]
)
val (nextNEOpiENV_setup) from nextNEOpiENV_setup_ch0
output:
path(
"${BaitsBed.baseName}.interval_list"
) into BaitsBedToIntervalList_out_ch0
script:
def JAVA_Xmx = '-Xmx' + task.memory.toGiga() + "G"
"""
gatk --java-options ${JAVA_Xmx} BedToIntervalList \\
-I ${BaitsBed} \\
-O ${BaitsBed.baseName}.interval_list \\
-SD $RefDict
"""
}
} else {
RegionsBedToIntervalList_out_ch0 = Channel.fromPath('NO_FILE')
RegionsBedToIntervalList_out_ch1 = Channel.empty()
BaitsBedToIntervalList_out_ch0 = Channel.empty()
}
process 'preprocessIntervalList' {
label 'nextNEOpiENV'
tag 'preprocessIntervalList'
publishDir "$params.outputDir/supplemental/00_prepare_Intervals/",
mode: publishDirMode
input:
tuple(
path(RefFasta),
path(RefIdx),
path(RefDict)
) from Channel.value(
[ reference.RefFasta,
reference.RefIdx,
reference.RefDict ]
)
path(interval_list) from RegionsBedToIntervalList_out_ch0
val (nextNEOpiENV_setup) from nextNEOpiENV_setup_ch0
output:
path(
outFileName
) into (
preprocessIntervalList_out_ch0,
preprocessIntervalList_out_ch1,
preprocessIntervalList_out_ch2,
preprocessIntervalList_out_ch3,
preprocessIntervalList_out_ch4,
preprocessIntervalList_out_ch5,
preprocessIntervalList_out_ch6,
preprocessIntervalList_out_ch7,
preprocessIntervalList_out_ch8
)
script:
outFileName = (params.WES) ? interval_list.baseName + "_merged_padded.interval_list" : "wgs_ScatterIntervalsByNs.interval_list"
def JAVA_Xmx = '-Xmx' + task.memory.toGiga() + "G"
if(params.WES)
"""
gatk PreprocessIntervals \\
-R $RefFasta \\
-L ${interval_list} \\
--bin-length 0 \\
--padding ${padding} \\
--interval-merging-rule OVERLAPPING_ONLY \\
-O ${outFileName}
"""
else
"""
gatk --java-options ${JAVA_Xmx} ScatterIntervalsByNs \\
--REFERENCE $RefFasta \\
--OUTPUT_TYPE ACGT \\
--OUTPUT ${outFileName}
"""
}
// Splitting interval file in 20(default) files for scattering Mutect2
process 'SplitIntervals' {
label 'nextNEOpiENV'
tag "SplitIntervals"
publishDir "$params.outputDir/supplemental/00_prepare_Intervals/SplitIntervals/",
mode: publishDirMode,
enabled: params.fullOutput
input:
tuple(
path(RefFasta),
path(RefIdx),
path(RefDict)
) from Channel.value(
[ reference.RefFasta,
reference.RefIdx,
reference.RefDict ]
)
path(IntervalsList) from preprocessIntervalList_out_ch0
val x from scatter_count
val (nextNEOpiENV_setup) from nextNEOpiENV_setup_ch0
output:
path(
"${IntervalName}/*-scattered.interval_list"
) into (
SplitIntervals_out_ch0,
SplitIntervals_out_ch1,
SplitIntervals_out_ch2,
SplitIntervals_out_scatterBaseRecalTumorGATK4_ch,
SplitIntervals_out_scatterTumorGATK4applyBQSRS_ch,
SplitIntervals_out_scatterBaseRecalNormalGATK4_ch,
SplitIntervals_out_scatterNormalGATK4applyBQSRS_ch,
SplitIntervals_out_ch3,
SplitIntervals_out_ch4,
SplitIntervals_out_ch5,
SplitIntervals_out_ch6,
)
val("${IntervalName}") into SplitIntervals_out_ch0_Name
script:
IntervalName = IntervalsList.baseName
"""
mkdir -p ${tmpDir}
gatk SplitIntervals \\
--tmp-dir ${tmpDir} \\
-R ${RefFasta} \\
-scatter ${x} \\
--interval-merging-rule ALL \\
--subdivision-mode BALANCING_WITHOUT_INTERVAL_SUBDIVISION_WITH_OVERFLOW \\
-L ${IntervalsList} \\
-O ${IntervalName}
"""
}
// convert padded interval list to Bed file (used by varscan)
// generate a padded tabix indexed region BED file for strelka
process 'IntervalListToBed' {
label 'nextNEOpiENV'
tag 'BedFromIntervalList'
publishDir "$params.outputDir/supplemental/00_prepare_Intervals/",
mode: publishDirMode
input:
path(paddedIntervalList) from preprocessIntervalList_out_ch1
val (nextNEOpiENV_setup) from nextNEOpiENV_setup_ch0
output:
path("${paddedIntervalList.baseName}.{bed.gz,bed.gz.tbi}") into (
RegionsBedToTabix_out_ch0,
RegionsBedToTabix_out_ch1
)
script:
def JAVA_Xmx = '-Xmx' + task.memory.toGiga() + "G"
"""
gatk --java-options ${JAVA_Xmx} IntervalListToBed \\
-I ${paddedIntervalList} \\
-O ${paddedIntervalList.baseName}.bed
bgzip -c ${paddedIntervalList.baseName}.bed > ${paddedIntervalList.baseName}.bed.gz &&
tabix -p bed ${paddedIntervalList.baseName}.bed.gz
"""
}
// convert scattered padded interval list to Bed file (used by varscan)
process 'ScatteredIntervalListToBed' {
label 'nextNEOpiENV'
tag 'ScatteredIntervalListToBed'
publishDir "$params.outputDir/supplemental/00_prepare_Intervals/SplitIntervals/${IntervalName}",
mode: publishDirMode,
enabled: params.fullOutput
input:
tuple(
val(IntervalName),
file(IntervalsList)
) from SplitIntervals_out_ch0_Name
.combine(
SplitIntervals_out_ch0.flatten()
)
val (nextNEOpiENV_setup) from nextNEOpiENV_setup_ch0
output:
path(
"*.bed"
) into (
ScatteredIntervalListToBed_out_ch0,
ScatteredIntervalListToBed_out_ch1
)
script:
def JAVA_Xmx = '-Xmx' + task.memory.toGiga() + "G"
"""
gatk --java-options ${JAVA_Xmx} IntervalListToBed \\
-I ${IntervalsList} \\
-O ${IntervalsList.baseName}.bed
"""
}
// FastQC
process FastQC {
label 'fastqc'
tag "$meta.sampleName : $meta.sampleType"
publishDir "${params.outputDir}/analyses/${meta.sampleName}/QC/fastqc",
mode: publishDirMode,
saveAs: { filename -> filename.indexOf(".zip") > 0 ? "zips/$filename" : "$filename"}
input:
tuple val(meta), path(reads) from fastqc_reads_ch
output:
tuple val(meta), path("*_fastqc.zip") into ch_fastqc // multiQC
script:
def reads_R1_ext = (reads[0].getExtension() == "gz") ? "fastq.gz" : reads[0].getExtension()
def reads_R1 = meta.sampleName + "_" + meta.sampleType + "_R1." + reads_R1_ext
// do we have PE reads?
def reads_R2 = "_missing_"
if(meta.libType == "PE") {
def reads_R2_ext = (reads[1].getExtension() == "gz") ? "fastq.gz" : reads[1].getExtension()
reads_R2 = meta.sampleName + "_" + meta.sampleType + "_R2." + reads_R2_ext
}
"""
if [ ! -e ${reads_R1} ]; then
ln -s ${reads[0]} ${reads_R1}
fi
if [ "${reads_R2}" != "_missing_" ] && [ ! -e ${reads_R2} ]; then
ln -s ${reads[1]} ${reads_R2}
fi
fastqc --quiet --threads ${task.cpus} \\
${reads_R1} ${reads_R2}
"""
}
// adapter trimming
//
// We first check if we need to trim DNA and RNA or only one of them.
// If it is only one of them we need to combine the trimmed and raw
// reads again
def trim_adapters = false
if (params.trim_adapters || params.trim_adapters_RNAseq) {
trim_adapters = true
reads_to_trim = raw_reads_ch
if (params.trim_adapters && params.trim_adapters_RNAseq) {
reads_to_trim_ch = raw_reads_ch
reads_to_keep_ch = Channel.empty()
}
if (params.trim_adapters && ! params.trim_adapters_RNAseq) {
raw_reads_ch.branch {
DNA: it[0].sampleType != "tumor_RNA"
RNA: it[0].sampleType == "tumor_RNA"
}
.set{ raw_reads_ch }
reads_to_trim_ch = raw_reads_ch.DNA
reads_to_keep_ch = raw_reads_ch.RNA
}
if (! params.trim_adapters && params.trim_adapters_RNAseq) {
raw_reads_ch.branch {
DNA: it[0].sampleType != "tumor_RNA"
RNA: it[0].sampleType == "tumor_RNA"
}
.set{ raw_reads_ch }
reads_to_trim_ch = raw_reads_ch.RNA
reads_to_keep_ch = raw_reads_ch.DNA
}
}
if (trim_adapters) {
process fastp {
label 'fastp'
tag "$meta.sampleName : $meta.sampleType"
publishDir "$params.outputDir/analyses/${meta.sampleName}/",
mode: publishDirMode,
saveAs: {
filename ->
if(filename.indexOf(".json") > 0) {
return "QC/fastp/$filename"
} else if(filename.indexOf("NO_FILE") >= 0) {
return null
} else {
return "01_preprocessing/$filename"
}
}
input:
tuple val(meta), path(reads) from reads_to_trim_ch
output:
tuple val(meta), path("*_trimmed_R{1,2}.fastq.gz") into reads_trimmed_ch, fastqc_reads_trimmed_ch
tuple val(meta), path("*.json") into ch_fastp // multiQC
script:
def reads_R1 = "--in1 " + reads[0]
def trimmed_reads_R1 = "--out1 " + meta.sampleName + "_" + meta.sampleType + "_trimmed_R1.fastq.gz"
// do we have PE reads?
def reads_R2 = ""
def trimmed_reads_R2 = ""
if(meta.libType == "PE") {
reads_R2 = "--in2 " + reads[1]
trimmed_reads_R2 = "--out2 " + meta.sampleName + "_" + meta.sampleType + "_trimmed_R2.fastq.gz"
}
def fastpAdapter = ''
def adapterSeqFile
def aseq = false
def aseqR2 = false
def afile = false
if (meta.sampleType.indexOf("DNA") > 0) {
afile = params.adapterSeqFile
aseq = params.adapterSeq
aseqR2 = params.adapterSeqR2
} else {
afile = params.adapterSeqFileRNAseq
aseq = params.adapterSeqRNAseq
aseqR2 = params.adapterSeqR2RNAseq
}
if(afile != false) {
adapterSeqFile = Channel.fromPath(afile)
fastpAdapter = "--adapter_fasta " + adapterSeqFile
} else {
if(aseq != false) {
adapterSeq = Channel.value(aseq)
fastpAdapter = "--adapter_sequence " + aseq.getVal()
if(aseqR2 != false && meta.libType == "PE") {
adapterSeqR2 = Channel.value(aseqR2)
fastpAdapter += " --adapter_sequence_r2 " + adapterSeqR2.getVal()
}
}
}
"""
fastp --thread ${task.cpus} \\
${reads_R1} \\
${reads_R2} \\
${trimmed_reads_R1} \\
${trimmed_reads_R2} \\
--json ${meta.sampleName}_${meta.sampleType}_fastp.json \\
${fastpAdapter} \\
${params.fastpOpts}
"""
}
// FastQC after adapter trimming
process FastQC_trimmed {
label 'fastqc'
tag "$meta.sampleName : $meta.sampleType"
publishDir "${params.outputDir}/analyses/${meta.sampleName}/QC/fastqc",
mode: publishDirMode,
saveAs: { filename -> filename.indexOf(".zip") > 0 ? "zips/$filename" : "$filename"}