This repository has been archived by the owner on Mar 12, 2018. It is now read-only.
forked from nf-core/methylseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bwa-meth.nf
437 lines (380 loc) · 12.3 KB
/
bwa-meth.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
#!/usr/bin/env nextflow
/*
vim: syntax=groovy
-*- mode: groovy;-*-
========================================================================================
B S - S E Q M E T H Y L A T I O N : B W A - M E T H
========================================================================================
Methylation (BS-Seq) Analysis Pipeline using bwa-meth. Started November 2016.
#### Homepage / Documentation
https://github.com/SciLifeLab/NGI-MethylSeq
#### Authors
Phil Ewels <[email protected]>
----------------------------------------------------------------------------------------
*/
/*
* SET UP CONFIGURATION VARIABLES
*/
// Pipeline version
version = "0.3dev"
// Configurable variables
params.project = false
params.genome = false
params.fasta = params.genome ? params.genomes[ params.genome ].fasta ?: false : false
params.fasta_index = params.genome ? params.genomes[ params.genome ].fasta_index ?: false : false
params.bwa_meth_index = params.genome ? params.genomes[ params.genome ].bwa_meth ?: false : false
params.saveReference = true
params.reads = "data/*_R{1,2}.fastq.gz"
params.outdir = './results'
params.notrim = false
params.nodedup = false
params.allcontexts = false
params.mindepth = 0
params.ignoreFlags = false
// Validate inputs
if( params.bwa_meth_index ){
bwa_meth_index = file("${params.bwa_meth_index}.bwameth.c2t.bwt")
bwa_meth_indices = Channel.fromPath( "${params.bwa_meth_index}*" ).toList()
if( !bwa_meth_index.exists() ) exit 1, "bwa-meth index not found: ${params.bwa_meth_index}"
}
if( params.fasta_index ){
fasta_index = file(params.fasta_index)
if( !fasta_index.exists() ) exit 1, "Fasta file not found: ${params.fasta_index}"
}
if ( params.fasta ){
fasta = file(params.fasta)
if( !fasta.exists() ) exit 1, "Fasta file not found: ${params.fasta}"
} else {
exit 1, "No reference Fasta file specified! Please use --fasta"
}
params.rrbs = false
params.pbat = false
params.single_cell = false
params.epignome = false
params.accel = false
params.zymo = false
params.cegx = false
if(params.pbat){
params.clip_r1 = 6
params.clip_r2 = 9
params.three_prime_clip_r1 = 6
params.three_prime_clip_r2 = 9
} else if(params.single_cell){
params.clip_r1 = 6
params.clip_r2 = 6
params.three_prime_clip_r1 = 6
params.three_prime_clip_r2 = 6
} else if(params.epignome){
params.clip_r1 = 8
params.clip_r2 = 8
params.three_prime_clip_r1 = 8
params.three_prime_clip_r2 = 8
} else if(params.accel || params.zymo){
params.clip_r1 = 10
params.clip_r2 = 15
params.three_prime_clip_r1 = 10
params.three_prime_clip_r2 = 10
} else if(params.cegx){
params.clip_r1 = 6
params.clip_r2 = 6
params.three_prime_clip_r1 = 2
params.three_prime_clip_r2 = 2
} else {
params.clip_r1 = 0
params.clip_r2 = 0
params.three_prime_clip_r1 = 0
params.three_prime_clip_r2 = 0
}
def single
log.info "==================================================="
log.info " NGI-MethylSeq : Bisulfite-Seq BWA-Meth v${version}"
log.info "==================================================="
log.info "Reads : ${params.reads}"
log.info "Genome : ${params.genome}"
log.info "BWA Index : ${params.bwa_meth_index}"
log.info "Current home : $HOME"
log.info "Current user : $USER"
log.info "Current path : $PWD"
log.info "Script dir : $baseDir"
log.info "Working dir : $workDir"
log.info "Output dir : ${params.outdir}"
log.info "---------------------------------------------------"
if(params.rrbs){ log.info "RRBS Mode : On" }
log.info "Deduplication : ${params.nodedup ? 'No' : 'Yes'}"
log.info "MethylDackel : C Contexts - ${params.allcontexts ? 'All (CpG, CHG, CHH)' : 'CpG only'}"
log.info "MethylDackel : Minimum Depth - ${params.mindepth}"
if(params.ignoreFlags){ log.info "MethylDackel: : Ignoring SAM Flags" }
log.info "---------------------------------------------------"
if(params.notrim){ log.info "Trimming Step : Skipped" }
if(params.pbat){ log.info "Trim Profile : PBAT" }
if(params.single_cell){ log.info "Trim Profile : Single Cell" }
if(params.epignome){ log.info "Trim Profile : Epignome" }
if(params.accel){ log.info "Trim Profile : Accel" }
if(params.cegx){ log.info "Trim Profile : CEGX" }
if(params.clip_r1 > 0) log.info "Trim R1 : ${params.clip_r1}"
if(params.clip_r2 > 0) log.info "Trim R2 : ${params.clip_r2}"
if(params.three_prime_clip_r1 > 0) log.info "Trim 3' R1 : ${params.three_prime_clip_r1}"
if(params.three_prime_clip_r2 > 0) log.info "Trim 3' R2 : ${params.three_prime_clip_r2}"
log.info "---------------------------------------------------"
log.info "Config Profile : ${workflow.profile}"
if(params.project) log.info "UPPMAX Project : ${params.project}"
log.info "==================================================="
// Validate inputs
if( workflow.profile == 'standard' && !params.project ) exit 1, "No UPPMAX project ID found! Use --project"
/*
* Create a channel for input read files
*/
Channel
.fromFilePairs( params.reads, size: -1 )
.ifEmpty { exit 1, "Cannot find any reads matching: ${params.reads}" }
.into { read_files_fastqc; read_files_trimming }
/*
* PREPROCESSING - Build bwa-mem index
*/
if(!params.bwa_meth_index){
process makeBwaMemIndex {
tag fasta
publishDir path: "${params.outdir}/reference_genome", saveAs: { params.saveReference ? it : null }, mode: 'copy'
input:
file fasta from fasta
output:
file "${fasta}.bwameth.c2t.bwt" into bwa_meth_index
file "${fasta}*" into bwa_meth_indices
script:
"""
bwameth.py index $fasta
"""
}
}
/*
* PREPROCESSING - Index Fasta file
*/
if(!params.fasta_index){
process makeFastaIndex {
tag fasta
publishDir path: "${params.outdir}/reference_genome", saveAs: { params.saveReference ? it : null }, mode: 'copy'
input:
file fasta
output:
file "${fasta}.fai" into fasta_index
script:
"""
samtools faidx $fasta
"""
}
}
/*
* STEP 1 - FastQC
*/
process fastqc {
tag "$name"
publishDir "${params.outdir}/fastqc", mode: 'copy'
input:
set val(name), file(reads) from read_files_fastqc
output:
file '*_fastqc.{zip,html}' into fastqc_results
script:
"""
fastqc -q $reads
"""
}
/*
* STEP 2 - Trim Galore!
*/
if(params.notrim){
trimmed_reads = read_files_trimming
trimgalore_results = []
} else {
process trim_galore {
tag "$name"
publishDir "${params.outdir}/trim_galore", mode: 'copy'
input:
set val(name), file(reads) from read_files_trimming
output:
set val(name), file('*fq.gz') into trimmed_reads
file '*trimming_report.txt' into trimgalore_results
script:
single = reads instanceof Path
c_r1 = params.clip_r1 > 0 ? "--clip_r1 ${params.clip_r1}" : ''
c_r2 = params.clip_r2 > 0 ? "--clip_r2 ${params.clip_r2}" : ''
tpc_r1 = params.three_prime_clip_r1 > 0 ? "--three_prime_clip_r1 ${params.three_prime_clip_r1}" : ''
tpc_r2 = params.three_prime_clip_r2 > 0 ? "--three_prime_clip_r2 ${params.three_prime_clip_r2}" : ''
rrbs = params.rrbs ? "--rrbs" : ''
if (single) {
"""
trim_galore --gzip $rrbs $c_r1 $tpc_r1 $reads
"""
} else {
"""
trim_galore --paired --gzip $rrbs $c_r1 $c_r2 $tpc_r1 $tpc_r2 $reads
"""
}
}
}
/*
* STEP 3 - align with bwa-mem
*/
process bwamem_align {
tag "$name"
publishDir "${params.outdir}/bwa-mem_alignments", mode: 'copy'
input:
set val(name), file(reads) from trimmed_reads
file index from bwa_meth_index
file bwa_meth_indices from bwa_meth_indices
output:
file '*.bam' into bam_aligned, bam_flagstat
script:
fasta = index.toString() - '.bwameth.c2t.bwt'
prefix = reads[0].toString() - ~/(_R1)?(_trimmed)?(_val_1)?(\.fq)?(\.fastq)?(\.gz)?$/
"""
set -o pipefail # Capture exit codes from bwa-meth
bwameth.py \\
--threads ${task.cpus} \\
--reference $fasta \\
$reads | samtools view -bS - > ${prefix}.bam
"""
}
/*
* STEP 4.1 - samtools flagstat on samples
*/
process samtools_flagstat {
tag "${bam.baseName}"
publishDir "${params.outdir}/bwa-mem_alignments", mode: 'copy'
input:
file bam from bam_flagstat
output:
file "${bam.baseName}_flagstat.txt" into flagstat_results
file "${bam.baseName}_stats.txt" into samtools_stats_results
script:
"""
samtools flagstat $bam > ${bam.baseName}_flagstat.txt
samtools stats $bam > ${bam.baseName}_stats.txt
"""
}
/*
* STEP 4.2 - sort and index alignments
*/
process samtools_sort {
tag "${bam.baseName}"
publishDir "${params.outdir}/bwa-mem_alignments_sorted", mode: 'copy'
executor 'local'
input:
file bam from bam_aligned
output:
file "${bam.baseName}.sorted.bam" into bam_sorted, bam_for_index
script:
"""
samtools sort \\
$bam \\
-m ${task.memory.toBytes() / task.cpus} \\
-@ ${task.cpus} \\
> ${bam.baseName}.sorted.bam
"""
}
/*
* STEP 4.3 - sort and index alignments
*/
process samtools_index {
tag "${bam.baseName}"
publishDir "${params.outdir}/bwa-mem_alignments_sorted", mode: 'copy'
input:
file bam from bam_for_index
output:
file "${bam}.bai" into bam_index
script:
"""
samtools index $bam
"""
}
/*
* STEP 5 - Mark duplicates
*/
process markDuplicates {
tag "${bam.baseName}"
publishDir "${params.outdir}/bwa-mem_markDuplicates", mode: 'copy'
input:
file bam from bam_sorted
output:
file "${bam.baseName}.markDups.bam" into bam_md, bam_md_qualimap
file "${bam.baseName}.markDups_metrics.txt" into picard_results
script:
"""
java -Xmx2g -jar \$PICARD_HOME/picard.jar MarkDuplicates \\
INPUT=$bam \\
OUTPUT=${bam.baseName}.markDups.bam \\
METRICS_FILE=${bam.baseName}.markDups_metrics.txt \\
REMOVE_DUPLICATES=false \\
ASSUME_SORTED=true \\
PROGRAM_RECORD_ID='null' \\
VALIDATION_STRINGENCY=LENIENT
# Print version number to standard out
echo "File name: $bam Picard version "\$(java -Xmx2g -jar \$PICARD_HOME/picard.jar MarkDuplicates --version 2>&1)
"""
}
/*
* STEP 6 - extract methylation with MethylDackel
*/
process methyldackel {
tag "${bam.baseName}"
publishDir "${params.outdir}/MethylDackel", mode: 'copy'
input:
file bam from bam_md
file fasta from fasta
file fasta_index from fasta
output:
file '*' into methyldackel_results
script:
allcontexts = params.allcontexts ? '--CHG --CHH' : ''
mindepth = params.mindepth > 0 ? "--minDepth ${params.mindepth}" : ''
ignoreFlags = params.ignoreFlags ? "--ignoreFlags" : ''
"""
MethylDackel extract $allcontexts $ignoreFlags $mindepth $fasta $bam
MethylDackel mbias $allcontexts $ignoreFlags $fasta $bam ${bam.baseName}
"""
}
/*
* STEP 7 - Qualimap
*/
process qualimap {
tag "${bam.baseName}"
publishDir "${params.outdir}/Qualimap", mode: 'copy'
input:
file bam from bam_md_qualimap
output:
file "${bam.baseName}_qualimap" into qualimap_results
script:
gcref = ''
if(params.genome == 'GRCh37') gcref = '-gd HUMAN'
if(params.genome == 'GRCm38') gcref = '-gd MOUSE'
"""
samtools sort $bam -o ${bam.baseName}.sorted.bam
qualimap bamqc $gcref \\
-bam ${bam.baseName}.sorted.bam \\
-outdir ${bam.baseName}_qualimap \\
--skip-duplicated \\
--collect-overlap-pairs \\
--java-mem-size=${task.memory.toGiga()}G \\
-nt ${task.cpus}
"""
}
/*
* STEP 8 - MultiQC
*/
process multiqc {
publishDir "${params.outdir}/MultiQC", mode: 'copy'
input:
file ('fastqc/*') from fastqc_results.flatten().toList()
file ('trimgalore/*') from trimgalore_results.flatten().toList()
file ('samtools/*') from flagstat_results.flatten().toList()
file ('samtools/*') from samtools_stats_results.flatten().toList()
file ('picard/*') from picard_results.flatten().toList()
file ('methyldackel/*') from methyldackel_results.flatten().toList()
file ('qualimap/*') from qualimap_results.flatten().toList()
output:
file '*multiqc_report.html'
file '*multiqc_data'
script:
"""
multiqc -f .
"""
}