-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariant_calling.nf
executable file
·294 lines (237 loc) · 7.75 KB
/
variant_calling.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
include { SAMTOOLS_INDEX } from './mapping'
include { SAMTOOLS_SORT } from './mapping'
include { VARIANT_PREPROCESSING } from './variant_preprocessing'
workflow VARIANT_CALLING {
take:
sorted_index_bam // Sample ID + BAM/BAI
ref
main:
def group_size = 5 // How many intervals each GATK command should take
def chromosomes = (1..21) + ['X', 'Y']
def num_lists = ((chromosomes.size() / group_size) + (chromosomes.size() % group_size > 0 ? 1 : 0)) as int
// def groups = Channel.fromList(chromosomes).collate(group_size)
def groups = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, "X", "Y"]]
ref2 = ref.first() // change to value channel from normal path channel
// Create .dict and .fai files for reference fasta file
REF_AUXILLARY(ref2)
bam_split = SplitNCigarReads(sorted_index_bam,
REF_AUXILLARY.out.fai,
REF_AUXILLARY.out.dict,
ref2,
groups).groupTuple() | MergeBams | SAMTOOLS_SORT | MarkDuplicates | SAMTOOLS_INDEX
Mutect2(bam_split,
REF_AUXILLARY.out.fai,
REF_AUXILLARY.out.dict,
ref2,
groups)
// Collect for each sample ID (i.e paired end read set) the (per-chromosome) scattered
// vcfs & f1r2, to be merged. So each of these outputs will give 1vcf,1tar,1stats for each sample
vcfs = Mutect2.out.vcfs.groupTuple(size: num_lists) | MergeVcfs
f1r2 = Mutect2.out.f1r2.groupTuple(size: num_lists) | MergeOrientationModel
stats = Mutect2.out.stats.groupTuple(size: num_lists) | MergeMutectStats
// FILTERING
// Group all needed files together by sample_id and send to Filtering process
sample_grouped = vcfs.concat(f1r2,stats).groupTuple(size: 3)
FilterMutect(sample_grouped,
REF_AUXILLARY.out.fai,
REF_AUXILLARY.out.dict,
ref2)
// haplotype_vcf
// emit:
// FilterMutect.out
}
process REF_AUXILLARY {
// Creation of .fai/.dict files for GATK
input:
path ref_file
output:
path "${ref_file}.fai", emit: fai
path "${ref_file.baseName}.dict", emit: dict
script:
"""
samtools faidx ${ref_file}
gatk CreateSequenceDictionary -R \$PWD/${ref_file} -O \$PWD/${ref_file.baseName}.dict
"""
stub:
"""
touch ${ref_file}.fai
touch ${ref_file.baseName}.dict
"""
}
process MarkDuplicates {
label 'variant_calling'
publishDir "${params.output_dir}/bams/deduped/", mode: 'symlink', overwrite: true, pattern: "*.bam"
publishDir "${params.output_dir}/bams/deduped/", mode: 'symlink', overwrite: true
input:
tuple val(sample_id), path(aligned_bam)
output:
tuple val(sample_id), path("*.bam")
script:
// NOTE: We an add --REMOVE_DUPLICATES=true to remove duplicates from the final BAM file
// intead of just switching the flag for that read
"""
echo "Working on ${aligned_bam}"
gatk MarkDuplicates -I \$PWD/${aligned_bam} -O \$PWD/dedup_${aligned_bam} -M \$PWD/dedup_${aligned_bam}.metrics
"""
stub:
"""
touch dedup_${aligned_bam}
"""
}
process SplitNCigarReads {
tag "${sample_id}"
label 'variant_calling'
label 'forking_heavy'
input:
tuple val(sample_id), path(bam), path(bai)
path ref_fai
path ref_dict
path ref
each chr_interval // i.e [[1,2,..], [3,4,..]], does for each [x,y,..] set
output:
tuple val(sample_id), path("snc_*.bam")
script:
def name = "snc_${bam.simpleName}."
def interval_args = ""
for (chr in chr_interval) {
interval_args += " -L ${chr}"
name += "${chr}_"
}
"""
echo "Working on ${bam}"
gatk SplitNCigarReads -R ${ref} -I ${bam} -O ${name}.bam ${interval_args}
"""
stub:
"""
touch snc_${bam_bai}
"""
}
process Mutect2 {
label 'variant_calling'
label 'forking_heavy'
input:
tuple val(sample_id), path(split_bam), path(bai)
path ref_fai
path ref_dict
path ref
each chr_interval
output:
tuple val(sample_id), path("*.vcf"), emit: vcfs
tuple val(sample_id), path("*.tar.gz"), emit: f1r2
tuple val(sample_id), path("*.stats"), emit: stats
script:
def name = "${split_bam.simpleName}"
def interval_args = ""
for (chr in chr_interval) {
interval_args += " -L ${chr}"
name += "${chr}_"
}
// println "Processing mutect2 in interval ${interval_args}"
"""
echo "Working on ${split_bam}"
gatk --java-options '-Xmx4G -XX:+UseParallelGC -XX:ParallelGCThreads=${task.cpus}' Mutect2 \
--pair-hmm-implementation FASTEST_AVAILABLE \
--smith-waterman FASTEST_AVAILABLE \
--native-pair-hmm-threads ${task.cpus} \
-R ${ref} \
-I ${split_bam} \
--f1r2-tar-gz f1r2_${name}.tar.gz \
${interval_args} \
-O ${name}.vcf \
"""
stub:
"""
touch haplotype_${split_bam.simpleName}.vcf
"""
}
process MergeBams {
input:
tuple val(sample_id), path(bams)
output:
tuple val(sample_id), path("*.bam")
script:
def fname = bams[0].simpleName
def allBams = ""
for (bam in bams) {
allBams += "-I ${bam} " // "${bams} " if using samtools
}
"""
gatk GatherBamFiles ${allBams} -O ${fname}.bam
"""
}
process MergeVcfs {
publishDir "${params.output_dir}/vcf/unfiltered/", mode: 'copy', overwrite: true, pattern: "*.vcf"
// publishDir "${params.output_dir}/vcf/unfiltered/rna_spades", mode: 'copy', overwrite: true, pattern: "*spades_*.vcf"
// publishDir "${params.output_dir}/vcf/unfiltered/Trinity-GG", mode: 'copy', overwrite: true, pattern: "*Trinity-GG_*.vcf"
input:
tuple val(sample_id), path(vcfs)
output:
tuple val(sample_id), path("merged_${sample_id}.vcf")
script:
def fname = vcfs[0].simpleName
def allVCFs = ""
for (vcf in vcfs) {
allVCFs += "-I ${vcf} " // "${bams} " if using samtools
}
"""
gatk MergeVcfs ${allVCFs} -O merged_${sample_id}.vcf
"""
}
process MergeOrientationModel {
label 'variant_calling'
label 'falliable'
// This requires all the f1r2 files from scattered analysis
input:
tuple val(sample_id), path(all_f1r2) // this takes all paths to f1r2 for one sample ID
output:
tuple val(sample_id), path("*.tar.gz")
script:
def input_args = ""
for (f1r2 in all_f1r2) {
input_args += "-I ${f1r2} "
}
"""
gatk LearnReadOrientationModel ${input_args} -O ${sample_id}_read-orientation-model.tar.gz
"""
}
process MergeMutectStats {
label 'variant_calling'
input:
tuple val(sample_id), path(all_stats)
output:
tuple val(sample_id), path("*.stats")
script:
def input_args = ""
for (stats in all_stats) {
input_args += "-stats ${stats} "
}
"""
gatk MergeMutectStats \
${input_args} \
-O ${sample_id}_merged.stats
"""
}
process FilterMutect {
label 'variant_calling'
label 'falliable'
publishDir "${params.output_dir}/vcf/filtered/${sample_id}", mode: 'copy', overwrite: true, pattern: "*.vcf"
input:
tuple val(sample_id), path(req_files)
path fai
path dict
path ref
output:
tuple val(sample_id), path("*.vcf")
script:
def (vcf, read_orient, stats) = req_files
"""
gatk FilterMutectCalls \
-R ${ref} \
-V ${vcf} \
--stats ${stats} \
--ob-priors ${read_orient} \
-O filtered_${sample_id}.vcf
"""
}
// TODO: Avoid sending tuple from sorted_index_bam to all the others (since some of them require )
// use named outputs?