forked from UPHL-BioNGS/Cecret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
samtools.nf
384 lines (301 loc) · 11.5 KB
/
samtools.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
process samtools_stats {
tag "${sample}"
publishDir "${params.outdir}", mode: 'copy'
container 'staphb/ivar:1.4.2'
//#UPHLICA maxForks 10
//#UPHLICA errorStrategy { task.attempt < 2 ? 'retry' : 'ignore'}
//#UPHLICA pod annotation: 'scheduler.illumina.com/presetSize', value: 'standard-xlarge'
//#UPHLICA memory 60.GB
//#UPHLICA cpus 14
//#UPHLICA time '45m'
when:
params.samtools_stats
input:
tuple val(sample), file(bam)
output:
path "samtools_stats/${sample}.stats.txt", emit: samtools_stats_files
path "logs/${task.process}/${sample}.${workflow.sessionId}.log"
shell:
'''
mkdir -p samtools_stats logs/!{task.process}
log=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
date > $log
samtools --version >> $log
samtools stats !{params.samtools_stats_options} !{bam} > samtools_stats/!{sample}.stats.txt
'''
}
process samtools_coverage {
tag "${sample}"
publishDir "${params.outdir}", mode: 'copy'
container 'staphb/ivar:1.4.2'
//#UPHLICA maxForks 10
//#UPHLICA errorStrategy { task.attempt < 2 ? 'retry' : 'ignore'}
//#UPHLICA pod annotation: 'scheduler.illumina.com/presetSize', value: 'standard-xlarge'
//#UPHLICA memory 60.GB
//#UPHLICA cpus 14
//#UPHLICA time '45m'
when:
params.samtools_coverage
input:
tuple val(sample), file(bam)
output:
path "samtools_coverage/${sample}.cov.{txt,hist}", emit: files
path "samtools_coverage/${sample}.cov.txt", emit: samtools_coverage
path "logs/${task.process}/${sample}.${workflow.sessionId}.log"
shell:
'''
mkdir -p samtools_coverage logs/!{task.process}
log=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
date > $log
samtools --version >> $log
samtools coverage !{params.samtools_coverage_options} !{bam} -m -o samtools_coverage/!{sample}.cov.hist | tee -a $log
samtools coverage !{params.samtools_coverage_options} !{bam} | awk -v sample=!{sample} '{print sample "\\t" $0 }' | sed '0,/!{sample}/s//sample/' > samtools_coverage/!{sample}.cov.txt | tee -a $log
'''
}
process samtools_flagstat {
tag "${sample}"
publishDir "${params.outdir}", mode: 'copy'
container 'staphb/ivar:1.4.2'
//#UPHLICA maxForks 10
//#UPHLICA errorStrategy { task.attempt < 2 ? 'retry' : 'ignore'}
//#UPHLICA pod annotation: 'scheduler.illumina.com/presetSize', value: 'standard-xlarge'
//#UPHLICA memory 60.GB
//#UPHLICA cpus 14
//#UPHLICA time '45m'
input:
tuple val(sample), file(bam)
when:
params.samtools_flagstat
output:
path "samtools_flagstat/${sample}.flagstat.txt", emit: samtools_flagstat_files
path "logs/${task.process}/${sample}.${workflow.sessionId}.log"
shell:
'''
mkdir -p samtools_flagstat logs/!{task.process}
log=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
date > $log
samtools --version >> $log
samtools flagstat !{params.samtools_flagstat_options} \
!{bam} | \
tee samtools_flagstat/!{sample}.flagstat.txt
'''
}
process samtools_depth {
tag "${sample}"
publishDir "${params.outdir}", mode: 'copy'
container 'staphb/ivar:1.4.2'
//#UPHLICA maxForks 10
//#UPHLICA errorStrategy { task.attempt < 2 ? 'retry' : 'ignore'}
//#UPHLICA pod annotation: 'scheduler.illumina.com/presetSize', value: 'standard-xlarge'
//#UPHLICA memory 60.GB
//#UPHLICA cpus 14
//#UPHLICA time '45m'
input:
tuple val(sample), file(bam)
when:
params.samtools_depth
output:
path "samtools_depth/${sample}.depth.txt", emit: file
path "logs/${task.process}/${sample}.${workflow.sessionId}.log"
shell:
'''
mkdir -p samtools_depth logs/!{task.process}
log=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
date > $log
samtools --version >> $log
samtools depth !{params.samtools_depth_options} \
!{bam} > samtools_depth/!{sample}.depth.txt
'''
}
process samtools_ampliconstats {
tag "${sample}"
publishDir "${params.outdir}", mode: 'copy'
container 'staphb/ivar:1.4.2'
//#UPHLICA maxForks 10
//#UPHLICA errorStrategy { task.attempt < 2 ? 'retry' : 'ignore'}
//#UPHLICA pod annotation: 'scheduler.illumina.com/presetSize', value: 'standard-xlarge'
//#UPHLICA memory 60.GB
//#UPHLICA cpus 14
//#UPHLICA time '45m'
when:
params.samtools_ampliconstats && ( params.trimmer != 'none' )
input:
tuple val(sample), file(bam), file(primer_bed)
output:
tuple val(sample), file("samtools_ampliconstats/${sample}_ampliconstats.txt"), emit: samtools_ampliconstats_files
path "logs/${task.process}/${sample}.${workflow.sessionId}.log"
shell:
'''
mkdir -p samtools_ampliconstats logs/!{task.process}
log=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
date > $log
samtools --version >> $log
samtools ampliconstats !{params.samtools_ampliconstats_options} \
!{primer_bed} \
!{bam} > samtools_ampliconstats/!{sample}_ampliconstats.txt
'''
}
process samtools_plot_ampliconstats {
tag "${sample}"
errorStrategy { task.attempt < 2 ? 'retry' : 'ignore'}
publishDir "${params.outdir}", mode: 'copy'
container 'staphb/ivar:1.4.2'
//#UPHLICA maxForks 10
//#UPHLICA pod annotation: 'scheduler.illumina.com/presetSize', value: 'standard-xlarge'
//#UPHLICA memory 60.GB
//#UPHLICA cpus 14
//#UPHLICA time '45m'
when:
params.samtools_plot_ampliconstats
input:
tuple val(sample), file(ampliconstats)
output:
path "samtools_plot_ampliconstats/${sample}*", emit: files
path "logs/${task.process}/${sample}.${workflow.sessionId}.log"
shell:
'''
mkdir -p samtools_plot_ampliconstats/!{sample} logs/!{task.process}
log=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
date > $log
samtools --version >> $log
plot-ampliconstats !{params.samtools_plot_ampliconstats_options} \
samtools_plot_ampliconstats/!{sample} \
!{ampliconstats}
'''
}
process samtools_sort {
tag "${sample}"
label "maxcpus"
publishDir "${params.outdir}", mode: 'copy'
container 'staphb/ivar:1.4.2'
//#UPHLICA maxForks 10
//#UPHLICA errorStrategy { task.attempt < 2 ? 'retry' : 'ignore'}
//#UPHLICA pod annotation: 'scheduler.illumina.com/presetSize', value: 'standard-xlarge'
//#UPHLICA memory 60.GB
//#UPHLICA cpus 14
//#UPHLICA time '45m'
input:
tuple val(sample), file(sam)
output:
tuple val(sample), file("aligned/${sample}.sorted.bam"), file("aligned/${sample}.sorted.bam.bai"), emit: bam_bai
path "logs/${task.process}/${sample}.${workflow.sessionId}.log"
shell:
'''
mkdir -p aligned logs/!{task.process}
log=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
# time stamp + capturing tool versions
date > $log
samtools --version >> $log
samtools sort -@ !{task.cpus} !{sam} | \
samtools view -F 4 -o aligned/!{sample}.sorted.bam | tee -a $log
# indexing the bams
samtools index aligned/!{sample}.sorted.bam | tee -a $log
'''
}
process samtools_filter {
tag "${sample}"
publishDir "${params.outdir}", mode: 'copy'
container 'staphb/ivar:1.4.2'
//#UPHLICA maxForks 10
//#UPHLICA errorStrategy { task.attempt < 2 ? 'retry' : 'ignore'}
//#UPHLICA pod annotation: 'scheduler.illumina.com/presetSize', value: 'standard-xlarge'
//#UPHLICA memory 60.GB
//#UPHLICA cpus 14
//#UPHLICA time '45m'
when:
params.filter
input:
tuple val(sample), file(sam)
output:
tuple val(sample), file("filter/${sample}_filtered_{R1,R2}.fastq.gz"), optional: true, emit: filtered_reads
path "filter/${sample}_filtered_unpaired.fastq.gz", optional: true, emit: unpaired
path "logs/${task.process}/${sample}.${workflow.sessionId}.log"
shell:
'''
mkdir -p filter logs/!{task.process}
log=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
# time stamp + capturing tool versions
date > $log
samtools --version >> $log
samtools sort -n !{sam} | \
samtools fastq -F 4 !{params.filter_options} \
-s filter/!{sample}_filtered_unpaired.fastq.gz \
-1 filter/!{sample}_filtered_R1.fastq.gz \
-2 filter/!{sample}_filtered_R2.fastq.gz \
| tee -a $log
'''
}
process samtools_ampliconclip {
tag "${sample}"
publishDir "${params.outdir}", mode: 'copy'
container 'staphb/ivar:1.4.2'
//#UPHLICA maxForks 10
//#UPHLICA errorStrategy { task.attempt < 2 ? 'retry' : 'ignore'}
//#UPHLICA pod annotation: 'scheduler.illumina.com/presetSize', value: 'standard-xlarge'
//#UPHLICA memory 60.GB
//#UPHLICA cpus 14
//#UPHLICA time '45m'
input:
tuple val(sample), file(bam), file(primer_bed)
output:
tuple val(sample), file("ampliconclip/${sample}.primertrim.sorted.bam"), file("ampliconclip/${sample}.primertrim.sorted.bam.bai"), emit: bam_bai
path "logs/${task.process}/${sample}.${workflow.sessionId}.log"
tuple val(sample), env(trimmer_version), emit: trimmer_version
shell:
'''
mkdir -p ampliconclip logs/!{task.process}
log=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
# time stamp + capturing tool versions
date > $log
samtools --version >> $log
trimmer_version="samtools ampliconclip : $(samtools --version | head -n 1)"
# trimming the reads
samtools ampliconclip !{params.samtools_ampliconclip_options} -b !{primer_bed} !{bam} | \
samtools sort | \
samtools view -F 4 -o ampliconclip/!{sample}.primertrim.sorted.bam | tee -a $log
samtools index ampliconclip/!{sample}.primertrim.sorted.bam | tee -a $log
'''
}
process samtools_markdup {
tag "${sample}"
publishDir "${params.outdir}", mode: 'copy'
container 'staphb/ivar:1.4.2'
//#UPHLICA maxForks 10
//#UPHLICA errorStrategy { task.attempt < 2 ? 'retry' : 'ignore'}
//#UPHLICA pod annotation: 'scheduler.illumina.com/presetSize', value: 'standard-xlarge'
//#UPHLICA memory 60.GB
//#UPHLICA cpus 14
//#UPHLICA time '45m'
input:
tuple val(sample), val(type), file(sam)
output:
tuple val(sample), file("markdup/${sample}.markdup.sorted.bam"), file("markdup/${sample}.markdup.sorted.bam.bai"), emit: bam_bai
path "markdup/${sample}_markdupstats.txt", emit: stats
path "logs/${task.process}/${sample}.${workflow.sessionId}.log"
shell:
if ( type == 'single' ) {
'''
mkdir -p markdup logs/!{task.process}
log=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
# time stamp + capturing tool versions
date > $log
samtools --version >> $log
samtools sort !{sam} | \
samtools markdup !{params.samtools_markdup_options} -@ !{task.cpus} -s -f markdup/!{sample}_markdupstats.txt - markdup/!{sample}.markdup.sorted.bam | tee -a $log
samtools index markdup/!{sample}.markdup.sorted.bam | tee -a $log
'''
} else if (type == 'paired') {
'''
mkdir -p markdup logs/!{task.process}
log=logs/!{task.process}/!{sample}.!{workflow.sessionId}.log
# time stamp + capturing tool versions
date > $log
samtools --version >> $log
samtools sort -n !{sam} | \
samtools fixmate !{params.samtools_fixmate_options} -m -@ !{task.cpus} - - | \
samtools sort | \
samtools markdup !{params.samtools_markdup_options} -@ !{task.cpus} -s -f markdup/!{sample}_markdupstats.txt - markdup/!{sample}.markdup.sorted.bam | tee -a $log
samtools index markdup/!{sample}.markdup.sorted.bam | tee -a $log
'''
}
}