forked from jasteen/nextflow-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhiplex_pipeline3.nf
476 lines (384 loc) · 14.1 KB
/
hiplex_pipeline3.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
#!/usr/bin/env nextflow
// Required Inputs
refFolder = file("/projects/vh83/reference/genomes/b37/bwa_0.7.12_index/")
inputDirectory = file('/home/jste0021/vh83_scratch/projects/COMPLEXO_GENESIS_NF/fastqs')
tmp_dir = file('/scratch/vh83/tmp/')
//project specific bed files
vardictBed = file("/projects/vh83/reference/hiplex_complexo/COMPLEXO22_621716iii.8col.bed")
intervalFile = file("/projects/vh83/reference/hiplex_complexo/COMPLEXO22_621716iii_final_b37_sorted.bed")
// Getting Reference Files
refBase = "$refFolder/human_g1k_v37_decoy"
ref = file("${refBase}.fasta")
refDict = file("${refBase}.dict")
refFai = file("${refBase}.fasta.fai")
millsIndels = file("${refFolder}/accessory_files/Mills_and_1000G_gold_standard.indels.b37.vcf")
dbSNP = file("${refFolder}/accessory_files/dbsnp_138.b37.vcf")
genome_file = file("/projects/vh83/reference/genomes/b37/accessory_files/human_g1k_v37_decoy_GenomeFile.txt")
header = file("/home/jste0021/vh83/reference/genomes/b37/vcf_contig_header_lines.txt")
af_thr = 0.1
//Annotation resources
dbsnp_b37 = file("/projects/vh83/reference/genomes/b37/accessory_files/dbsnp_138.b37.vcf")
other_vep = file("/usr/local/vep/90/ensembl-vep/cache")
vep_brcaex = file("/projects/vh83/reference/annotation_databases/BRCA-Exchange/BRCA-exchange_accessed-180118/BRCA-exchange_accessed-180118.sort.vcf.gz")
vep_gnomad = file("/projects/vh83/reference/annotation_databases/gnomAD/gnomad.exomes.r2.0.2.sites/gnomad.exomes.r2.0.2.sites.vcf.gz")
vep_revel = file("/projects/vh83/reference/annotation_databases/REVEL/REVEL-030616/revel_all_chromosomes.vcf.gz")
vep_maxentscan = file("/projects/vh83/reference/annotation_databases/MaxEntScan/MaxEntScan_accessed-240118")
vep_exac = file("/projects/vh83/reference/annotation_databases/ExAC/ExAC_nonTCGA.r0.3.1/ExAC_nonTCGA.r0.3.1.sites.vep.vcf.gz")
vep_dbnsfp = file("/projects/vh83/reference/annotation_databases/dbNSFP/dbNSFPv2.9.3-VEP/dbNSFP-2.9.3.gz")
vep_dbscsnv = file("/projects/vh83/reference/annotation_databases/dbscSNV/dbscSNV1.0-VEP/dbscSNV.txt.gz")
vep_cadd = file("/projects/vh83/reference/annotation_databases/CADD/CADD-v1.3/1000G_phase3.tsv.gz")
// Tools
picardJar = '~/picard.jar'
bwaModule = 'bwa/0.7.17-gcc5'
samtoolsModule = 'samtools/1.9'
// Global Resource Configuration Options
globalExecutor = 'slurm'
globalStageInMode = 'symlink'
globalCores = 1
bwaCores = 4
vepCores = 4
globalMemoryS = '6 GB'
globalMemoryM = '8 GB'
globalMemoryL = '64 GB'
globalTimeS = '8m'
globalTimeM = '1h'
globalTimeL = '24h'
globalQueueS = 'short'
globalQueueL = 'comp'
// Creating channel from input directory
ch_inputFiles = Channel.fromFilePairs("$inputDirectory/*.R{1,2}.fastq.gz")
process align_bwa {
input:
set baseName, file(fastqs) from ch_inputFiles
output:
set baseName, file("${baseName}.hq.sorted.bam"), file("${baseName}.hq.sorted.bam.bai") into ch_mappedBams
publishDir path: './bam_out', mode: 'copy'
executor globalExecutor
stageInMode globalStageInMode
module bwaModule
module samtoolsModule
cpus bwaCores
memory globalMemoryM
time globalTimeM
queue globalQueueL
"""
bwa mem -M -t ${task.cpus} -R "@RG\\tID:${baseName}\\tSM:${baseName}\\tPU:lib1\\tPL:Illumina" $ref ${fastqs[0]} ${fastqs[1]} \
| samtools view -u -h -q 1 -f 2 -F 4 -F 8 -F 256 - \
| samtools sort -@ $bwaCores -o "${baseName}.hq.sorted.bam"
samtools index "${baseName}.hq.sorted.bam" "${baseName}.hq.sorted.bam.bai"
"""
}
ch_mappedBams.into{ch_mappedBam1;ch_mappedBam2;ch_mappedBam3;ch_mappedBam4;ch_mappedBam5}
process run_vardict {
input:
set baseName, file(bam), file(bai) from ch_mappedBam1
output:
set baseName, file("${baseName}.tsv") into ch_vardictTSV
publishDir path: './variants_raw_out', mode: 'copy'
executor globalExecutor
stageInMode globalStageInMode
memory globalMemoryM
time globalTimeM
queue globalQueueL
"""
export PATH=/home/jste0021/scripts/VarDict-1.5.8/bin/:$PATH
VarDict -G ${ref} -f 0.1 -N "${baseName}" -b ${bam} -c 1 -S 2 -E 3 -g 4 ${vardictBed} > "${baseName}.tsv"
"""
}
process makeVCF {
input:
set baseName, file(tsv) from ch_vardictTSV
output:
set baseName, file("${baseName}.vardict.vcf") into ch_vardictVCFs
publishDir path: './variants_raw_out', mode: 'copy'
executor globalExecutor
stageInMode globalStageInMode
cpus 1
memory globalMemoryM
time globalTimeL
queue globalQueueL
script:
"""
module purge
module load R/3.5.1
cat ${tsv} | /home/jste0021/scripts/VarDict-1.5.8/bin/teststrandbias.R | \
/home/jste0021/scripts/VarDict-1.5.8/bin/var2vcf_valid.pl -N "${baseName}" \
-f 0.1 -E > "${baseName}.vardict.vcf"
"""
}
process reheaderVCF {
input:
set baseName, file(vcf) from ch_vardictVCFs
output:
set baseName, file("${baseName}.reheader.vcf.gz") into ch_reheaderVCF
publishDir path: './variants_raw_out', mode: 'copy'
module 'bcftools/1.8'
executor globalExecutor
stageInMode globalStageInMode
module bwaModule
memory globalMemoryM
time globalTimeM
queue globalQueueL
script:
"""
bcftools annotate -h ${header} -O z -o "${baseName}.reheader.vcf.gz" ${vcf}
"""
}
process sortVCFS {
input:
set baseName, file(vcf) from ch_reheaderVCF
output:
set baseName, file("${baseName}.sorted.vcf.gz") into ch_sortedVCF
publishDir path: './variants_raw_out', mode: 'copy'
module 'bcftools/1.8'
executor globalExecutor
stageInMode globalStageInMode
module bwaModule
memory globalMemoryM
time globalTimeM
queue globalQueueL
script:
"""
bcftools sort -o "${baseName}.sorted.vcf.gz" -O z ${vcf}
"""
}
process indexVCFS {
input:
set baseName, file(vcf) from ch_sortedVCF
output:
set baseName, file(vcf), file("${baseName}.sorted.vcf.gz.tbi") into ch_indexedVCF
publishDir path: './variants_raw_out', mode: 'copy'
module 'bcftools/1.8'
executor globalExecutor
stageInMode globalStageInMode
module bwaModule
memory globalMemoryM
time globalTimeM
queue globalQueueL
script:
"""
bcftools index -f --tbi ${vcf} -o ${baseName}.sorted.vcf.gz.tbi
"""
}
//duplicate ch_indexedVCF
ch_indexedVCF.into{ch_list;ch_files}
//set one version to a list of filenames of the VCF
ch_list.map { it -> it[1].name }
.collectFile(name: 'list.txt', newLine: true)
.set {ch_list_f}
//set the second to all the files
ch_files
.collect()
.set {ch_all_files}
//feed both to the merge so that the indexes are available to bcftools
process mergeVCFS {
echo true
publishDir './variants_merged/', mode: 'copy'
input:
file list from ch_list_f
file '*' from ch_all_files
output:
file "merged.vardict.vcf.gz" into ch_mergedVCF
module 'bcftools/1.8'
executor globalExecutor
stageInMode globalStageInMode
memory globalMemoryM
time globalTimeM
queue globalQueueL
script:
"""
bcftools merge -O z -o "merged.vardict.vcf.gz" -l list.txt
"""
}
process vt_decompose_normalise {
input:
file(vcf) from ch_mergedVCF
output:
file("merged.vt.vcf.gz") into ch_vtDecomposeVCF
publishDir path: './variants_merged', mode: 'copy'
executor globalExecutor
stageInMode globalStageInMode
module 'vt'
memory globalMemoryM
time globalTimeM
queue globalQueueL
"""
vt decompose -s $vcf | vt normalize -r $ref -o merged.vt.vcf.gz -
"""
}
process apply_vep {
input:
file(vcf) from ch_vtDecomposeVCF
output:
file("merged.vt.vep.vcf.gz") into ch_vepVCF
publishDir path: './variants_merged', mode: 'copy'
executor globalExecutor
stageInMode globalStageInMode
cpus vepCores
memory globalMemoryM
time globalTimeM
queue globalQueueL
module 'vep/90'
"""
vep --cache --dir_cache $other_vep \
--assembly GRCh37 --refseq --offline \
--fasta $ref \
--sift b --polyphen b --symbol --numbers --biotype \
--total_length --hgvs --format vcf \
--vcf --force_overwrite --flag_pick --no_stats \
--custom $vep_brcaex,brcaex,vcf,exact,0,Clinical_significance_ENIGMA,\
Comment_on_clinical_significance_ENIGMA,Date_last_evaluated_ENIGMA,\
Pathogenicity_expert,HGVS_cDNA,HGVS_Protein,BIC_Nomenclature \
--custom $vep_gnomad,gnomAD,vcf,exact,0,AF_NFE,AN_NFE \
--custom $vep_revel,RVL,vcf,exact,0,REVEL_SCORE \
--plugin MaxEntScan,$vep_maxentscan \
--plugin ExAC,$vep_exac,AC,AN \
--plugin dbNSFP,$vep_dbnsfp,REVEL_score,REVEL_rankscore \
--plugin dbscSNV,$vep_dbscsnv \
--plugin CADD,$vep_cadd \
--fork ${task.cpus} \
-i ${vcf} \
-o merged.vt.vep.vcf.gz
"""
}
/*
Stats Generation Section
*/
process InstersectBed {
input:
set sample, file(bam), file(bai) from ch_mappedBam2
output:
set sample, file("${sample}.intersectbed.bam") into ch_intersectBam
executor globalExecutor
stageInMode globalStageInMode
cpus 1
memory globalMemoryM
time globalTimeL
queue globalQueueL
script:
"""
module load bedtools/2.27.1-gcc5
intersectBed -abam ${bam} -b ${intervalFile} > ${sample}.intersectbed.bam
"""
}
process CoverageBed {
input:
set sample, file(bam), file(bai) from ch_mappedBam3
output:
set sample, file("${sample}.bedtools_hist_all.txt") into ch_bedtools
executor globalExecutor
stageInMode globalStageInMode
cpus 1
memory globalMemoryM
time globalTimeL
queue globalQueueL
errorStrategy 'ignore'
script:
"""
module load bedtools/2.27.1-gcc5
coverageBed -b ${bam} -a ${intervalFile} \
-sorted -hist -g ${genome_file} | \
grep all > "${sample}.bedtools_hist_all.txt"
"""
}
process ReadsMapped {
input:
set sample, file(bam), file(bai) from ch_mappedBam4
output:
set sample, file("${sample}.mapped_to_genome.txt") into ch_onGenome
executor globalExecutor
stageInMode globalStageInMode
cpus 1
module 'samtools/1.9'
memory globalMemoryM
time globalTimeL
queue globalQueueL
errorStrategy 'ignore'
script:
"""
samtools view -c -F4 ${bam} > "${sample}.mapped_to_genome.txt"
"""
}
process ReadsTotal {
input:
set sample, file(bam), file(bai) from ch_mappedBam5
output:
set sample, file("${sample}.total_raw_reads.txt") into ch_onTotal
executor globalExecutor
stageInMode globalStageInMode
cpus 1
module 'samtools/1.9'
memory globalMemoryM
time globalTimeL
queue globalQueueL
errorStrategy 'ignore'
script:
"""
samtools view -c ${bam} > "${sample}.total_raw_reads.txt"
"""
}
process TargetMapped {
input:
set sample, file(bam) from ch_intersectBam
output:
set sample, file("${sample}.mapped_to_target.txt") into ch_onTarget
executor globalExecutor
stageInMode globalStageInMode
cpus 1
memory globalMemoryM
module 'samtools/1.9'
time globalTimeL
queue globalQueueL
errorStrategy 'ignore'
script:
"""
samtools view -c -F4 ${bam} > ${sample}.mapped_to_target.txt
"""
}
ch_final = ch_bedtools.join(ch_onGenome)
ch_final2 = ch_final.join(ch_onTarget)
ch_final3 = ch_final2.join(ch_onTotal)
process collateData {
input:
set sample, file(bedtools), file(onGenome), file(onTarget), file(onTotal) from ch_final3
output:
set sample, file("${sample}_summary_coverage.txt") into ch_out
executor globalExecutor
stageInMode globalStageInMode
cpus 1
memory globalMemoryM
time globalTimeL
queue globalQueueL
errorStrategy 'ignore'
script:
"""
module purge
module load R/3.5.1
Rscript --vanilla /projects/vh83/pipelines/code/modified_summary_stat.R \
${bedtools} \
${onGenome} \
${onTarget} \
${onTotal} \
${sample} \
"${sample}_summary_coverage.txt"
"""
}
ch_out.map{a,b -> b}.collect().set{ch_out2}
process catStats {
input:
file(stats) from ch_out2
output:
file("project_summary.txt") into ch_out3
publishDir path: './metrics/', mode: 'copy'
executor globalExecutor
stageInMode globalStageInMode
cpus 1
memory globalMemoryM
time globalTimeL
queue globalQueueL
errorStrategy 'ignore'
script:
"""
cat ${stats} > "project_summary.txt"
"""
}