forked from R-Cardenas/MED-Pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgpmap_fix.nf
320 lines (273 loc) · 7.52 KB
/
cgpmap_fix.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
/*
* create a channel for fastq pairss
*/
read1_ch = Channel .fromFilePairs( params_fq )
read1_ch.into { read2_ch; read3_ch }
println """
==================================
E X O M E - N F P I P E L I N E
N O M E R G E
Mapping and Bam Processing
v0.1
===================================
"""
.stripIndent()
process cgpMAP {
errorStrategy { sleep(Math.pow(2, task.attempt) * 200 as long); return 'retry' }
maxRetries 3
executor 'slurm'
clusterOptions '--qos=hmem'
queue 'hmem-512'
memory { 70.GB * task.attempt }
storeDir "$baseDir/outputCOPY33/cgpMAP/${reads[0]}"
input:
tuple val(read2), file(reads) from read2_ch
output:
file "*.bam" into cgp_ch
script:
"""
## --------------------------- Trim reads -----------------------------------##
mkdir -p fastp
fastp -I ${reads[0]} -i ${reads[1]} \
-O fastp/${reads[0]} \
-o fastp/${reads[1]}
## --------------------------- Run cgpMAP -----------------------------------##
rm -fr $baseDir/outputCOPY33/cgpMAP/${reads[0]} # delete and remake path in case of retry pipeline
mkdir -p $baseDir/outputCOPY33/cgpMAP/${reads[0]}
name=\$(echo '${reads[0]}' | sed -e 's/.*[/]//' -e 's/-.*//')
BaseName=\$(basename ${reads[1]})
ds-cgpmap.pl \
-outdir $baseDir/outputCOPY33/cgpMAP/${reads[0]} \
-r $cgpmap_genome \
-i $cgpmap_index \
-t 10 \
-s \$name \
fastp/${reads[0]} fastp/${reads[1]}
"""
}
process sam_sort {
errorStrategy { sleep(Math.pow(2, task.attempt) * 200 as long); return 'retry' }
maxRetries 7
storeDir "$baseDir/outputCOPY/BAM/sorted"
memory { 6.GB * task.attempt }
input:
file bam from cgp_ch
output:
file "${bam}.sorted.bam" into bam_merge_ch
script:
"""
# create the tmp file as picard can create large temp files
mkdir -p tmp
picard SortSam I=${bam} O=${bam}.sorted.bam SORT_ORDER=coordinate TMP_DIR=tmp
rm -fr tmp
"""
}
// dont forget to add singularity with python3 installed
process bam_merge {
executor 'slurm'
memory { 20.GB * task.attempt }
storeDir "$baseDir/outputCOPY/BAM/merge"
input:
file bam from bam_merge_ch.collect()
output:
file "*-merged.bam" into dup_ch
script:
"""
module add python/anaconda/2020.07/3.8
module add samtools
python $baseDir/bin/python/merge_bam_v2.py --bam '${bam}'
"""
}
process picard_pcr_removal {
errorStrategy { sleep(Math.pow(2, task.attempt) * 200 as long); return 'retry' }
maxRetries 7
storeDir "$baseDir/outputCOPY/BAM/merge/RMD"
input:
file bam from dup_ch.flatten()
output:
file "${bam.simpleName}.rmd.bam" into (index1_ch, hs_ch, bam10_ch, bam11_ch, bam12_ch)
file "${bam.simpleName}.log"
script:
"""
mkdir -p tmp
picard MarkDuplicates I=${bam} O=${bam.simpleName}.rmd.bam M=${bam.simpleName}.log TMP_DIR=tmp
TMP_DIR=tmp
rm -fr tmp
"""
}
process bam_index {
errorStrategy { sleep(Math.pow(2, task.attempt) * 200 as long); return 'retry' }
maxRetries 7
memory { 6.GB * task.attempt }
storeDir "$baseDir/outputCOPY/BAM/merge/RMD"
input:
file bam from index1_ch
output:
file "${bam}.bai" into (index_3ch, index_4ch)
script:
"""
mkdir -p tmp
picard BuildBamIndex \
I=${bam} \
O=${bam}.bai \
TMP_DIR=tmp
rm -fr tmp
"""
}
process hybrid_stats {
errorStrategy { sleep(Math.pow(2, task.attempt) * 200 as long); return 'retry' }
maxRetries 7
memory { 6.GB * task.attempt }
storeDir "$baseDir/outputCOPY/BAM/hybrid_stats"
input:
file bam from hs_ch
output:
file "${bam.simpleName}_hs_metrics.txt"
script:
"""
# create interval files from BED supplied
picard BedToIntervalList \
-I ${bait_interval} \
--CREATE_INDEX true \
-O ${bait_interval}.interval \
-SD $genome_fasta
picard BedToIntervalList \
-I ${target_interval} \
--CREATE_INDEX true \
-O ${target_interval}.interval \
-SD $genome_fasta
# perform hybrid stats
mkdir -p tmp
picard CollectHsMetrics I=${bam} O=${bam.simpleName}_hs_metrics.txt \
R=$genome_fasta \
BAIT_INTERVALS=${bait_interval}.interval \
TARGET_INTERVALS=${target_interval}.interval \
TMP_DIR=tmp
rm -fr tmp
"""
}
process alignment_stats{
errorStrategy { sleep(Math.pow(2, task.attempt) * 200 as long); return 'retry' }
maxRetries 7
memory { 6.GB * task.attempt }
storeDir "$baseDir/outputCOPY/BAM/alignment_stats"
input:
file bam from bam10_ch
output:
file "${bam.simpleName}_align_stats.txt" into verify_ch
script:
"""
mkdir -p tmp
picard CollectAlignmentSummaryMetrics \
R=$genome_fasta \
I=${bam} \
O=${bam.simpleName}_align_stats.txt \
TMP_DIR=tmp
rm -fr tmp
"""
}
process verifybamid{
errorStrategy { sleep(Math.pow(2, task.attempt) * 200 as long); return 'retry' }
maxRetries 7
cpus 3
executor 'slurm'
clusterOptions '--qos=hmem'
queue 'hmem-512'
memory '80 GB'
// stageInMode = 'copy' // somalier doesnt like sym/hardlinks.
storeDir "$baseDir/outputCOPY/BAM/verifyBamID"
input:
file bam from bam11_ch
file idx from index_3ch.collect()
output:
file "${bam.simpleName}.log"
file "${bam.simpleName}.selfSM" into con_ch
script:
"""
verifyBamID --vcf $verifybamid \
--bam ${bam} \
--out ${bam.simpleName} \
--maxDepth 1000 \
--precise \
--verbose \
--ignoreRG \
-ignoreOverlapPair
rm -fr *.bam
"""
}
process somalier{
errorStrategy { sleep(Math.pow(2, task.attempt) * 200 as long); return 'retry' }
maxRetries 7
executor 'slurm'
clusterOptions '--qos=hmem'
queue 'hmem-512'
memory { 50.GB * task.attempt }
storeDir "$baseDir/outputCOPY/BAM/somalier"
input:
file bam from bam12_ch.collect()
file idx from index_4ch.collect()
output:
file "*.html" into som_ch
script:
"""
python $baseDir/bin/python/PED_file.py --bam '$bam'
mkdir -p bin
for f in *.bam; do
/somalier:v0.2.11/somalier extract -d extracted/ --sites $somalier -f $genome_fasta \$f
done
/somalier:v0.2.11/somalier relate --ped project.PED extracted/*.somalier
wget -P ancestry_files https://raw.githubusercontent.com/brentp/somalier/master/scripts/ancestry-labels-1kg.tsv
wget https://zenodo.org/record/3479773/files/1kg.somalier.tar.gz
tar -xzf 1kg.somalier.tar.gz
/somalier:v0.2.11/somalier ancestry --labels ancestry_files/ancestry-labels-1kg.tsv \
1kg-somalier/*.somalier ++ extracted/*.somalier
rm -fr *.bam
"""
}
process multiqc{
errorStrategy { sleep(Math.pow(2, task.attempt) * 200 as long); return 'retry' }
maxRetries 7
executor 'slurm'
storeDir "$baseDir/outputCOPY/multiqc"
input:
file somalier from som_ch
file con from con_ch.collect()
output:
file "*.html"
script:
"""
multiqc $baseDir
# Creating final log files
echo ' Pipeline cgpmap v0.3 completed
Project: $projectname
Time: ${nextflow.timestamp}
trimmomatic - completed
cgpmap - completed
(reference = $cgpmap_genome)
(index = $cgpmap_index)
samsort - completed
picard pcr removal - completed
picard rename bam - completed
samtools bam index - completed
picard collect insert size - completed
picard collect HS stats - completed
(target_intervals = $target_interval)
(bait_intervals = $bait_interval)
merge bams - completed
' >> $baseDir/logs/${projectname}_cgpmap_log.txt
mail -s "cgpMAP successful" [email protected] < $baseDir/logs/${projectname}_cgpmap_log.txt
mail -s "cgpMAP info" [email protected] < $baseDir/logs/${projectname}_cgpmap_log.txt
"""
}
workflow.onError{
// create log files and record output
process finish{
script:
""" echo ' Pipeline cgpmap v0.3 FAILED
Project: $projectname
Time: ${nextflow.timestamp}
' >> $baseDir/logs/cgpMAP.${projectname}.failed.log
mail -s "cgpMAP FAIL $projectname" [email protected] < $baseDir/logs/cgpMAP.${projectname}.failed.log
"""
}
}