-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.nf
More file actions
255 lines (230 loc) · 9.53 KB
/
main.nf
File metadata and controls
255 lines (230 loc) · 9.53 KB
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
#!/usr/bin/env nextflow
// Runs MACS2 to estimate fragment size and build a peak model.
process getPeakModelFragmentSize {
publishDir "${params.outdir}", mode: 'copy'
label 'quick' // This process is supposed to run very quickly
input:
path chip_bam // ChIP BAM file
val effgsize // Effective genome size for normalization
val format // Format of the BAM files (i.e., 'BAM' for single-end or 'BAMPE' for paired-end)
val prefix // Prefix for output files
output:
path "${prefix}.fragment_size.txt" // Output file with fragment size
path "${prefix}.model.pdf" // Output PDF with the peak model plot
stdout emit: fragment_size // Fragment size value emitted to stdout
script:
"""
macs2 predictd -i $chip_bam -f $format -g $effgsize --rfile model.R
parse_model_script.py model.R > ${prefix}.fragment_size.txt
cat ${prefix}.fragment_size.txt
Rscript model.R > /dev/null
mv model.R_model.pdf ${prefix}.model.pdf
"""
}
// Runs MACS2 to call peaks
process callPeaks {
publishDir "${params.outdir}", mode: 'copy'
label 'peaks' // This process is supposed to run for a longer time and consume more RAM
input:
path chip_bam // ChIP BAM file
path input_bam // Input BAM file
val prefix // Prefix for output files
val effgsize // Effective genome size for normalization
val format // Format of the BAM files (i.e., 'BAM' for single-end or 'BAMPE' for paired-end)
output:
path "${prefix}.peaks.narrowPeak" // Output peaks file in narrowPeak format
script:
"""
macs2 callpeak -t $chip_bam -c $input_bam -g $effgsize -f $format -n chip
mv chip_peaks.narrowPeak ${prefix}.peaks.narrowPeak
"""
}
// Filter peaks by blacklist
process filterPeaksByBlacklist {
publishDir "${params.outdir}", mode: 'copy'
label 'quick' // This process is supposed to run very quickly
input:
path peaks // Peaks file in narrowPeak format
path blacklist // Blacklist file to filter out regions
val prefix // Prefix for output files
output:
path "${prefix}.peaks_noblacklist.narrowPeak" // Output filtered peaks file
script:
"""
bedtools intersect -v -a $peaks -b $blacklist > ${prefix}.peaks_noblacklist.narrowPeak
"""
}
// Runs deeptools bamCoverage to get CPM bigwigs
process makeCpmTrack {
publishDir "${params.outdir}", mode: 'copy'
label 'tracks' // This process is supposed to be parallelized
input:
path bam // BAM file to be converted to bigwig
path bai // BAM index file
val kind // Type of the BAM file (i.e., 'chip' or 'input')
val prefix // Prefix for output files
path blacklist // Blacklist file to filter out regions
val binsize // Bin size for bigwig files
val fragsize // Fragment size for read extension
val effgsize // Effective genome size for normalization
output:
path "${prefix}.${kind}.bw" // Output bigwig file with CPM normalization
script:
"""
bamCoverage -b $bam \
-o ${prefix}.${kind}.bw \
-p $task.cpus \
--normalizeUsing CPM \
--blackListFileName $blacklist \
--binSize $binsize \
--extendReads $fragsize \
--ignoreDuplicates \
--effectiveGenomeSize $effgsize
"""
}
// Runs deeptools bamCompare to get log2 ratio of ChIP and Input CPMs
process makeRatioTrack {
publishDir "${params.outdir}", mode: 'copy'
label 'tracks' // This process is supposed to be parallelized
input:
path bam_chip // ChIP BAM file
path bai_chip // ChIP BAM index file
path bam_input // Input BAM file
path bai_input // Input BAM index file
val prefix // Prefix for output files
path blacklist // Blacklist file
val binsize // Bin size for bigwig files
val fragsize // Fragment size for read extension
val effgsize // Effective genome size for normalization
output:
path "${prefix}.ratio.bw" // Output bigwig file with log2 ratio
script:
"""
bamCompare -b1 $bam_chip \
-b2 $bam_input \
-o ${prefix}.ratio.bw \
-p $task.cpus \
--normalizeUsing CPM \
--operation log2 \
--scaleFactorsMethod None \
--blackListFileName $blacklist \
--binSize $binsize \
--extendReads $fragsize \
--ignoreDuplicates \
--effectiveGenomeSize $effgsize
"""
}
// Runs deeptools bamCompare to get difference of ChIP and Input CPMs
process makeDifferenceTrack {
publishDir "${params.outdir}", mode: 'copy'
label 'tracks' // This process is supposed to be parallelized
input:
path bam_chip // ChIP BAM file
path bai_chip // ChIP BAM index file
path bam_input // Input BAM file
path bai_input // Input BAM index file
val prefix // Prefix for output files
path blacklist // Blacklist file
val binsize // Bin size for bigwig files
val fragsize // Fragment size for read extension
val effgsize // Effective genome size for normalization
output:
path "${prefix}.diff.bw" // Output bigwig file with difference
script:
"""
bamCompare -b1 $bam_chip \
-b2 $bam_input \
-o ${prefix}.diff.bw \
-p $task.cpus \
--normalizeUsing CPM \
--operation subtract \
--scaleFactorsMethod None \
--blackListFileName $blacklist \
--binSize $binsize \
--extendReads $fragsize \
--ignoreDuplicates \
--effectiveGenomeSize $effgsize
"""
}
params.samplesheet = './samplesheet.csv' // Path to the samplesheet
params.blacklist = './assets/hg19-blacklist.v2.bed' // Path to the blacklist file
params.outdir = "./results" // Path to the output folder
params.prefix = 'sample' // Prefix for output files
params.binsize = 1000 // Bin size for bigwig files
params.callpeaks = true // Whether to call peaks or not
params.maketracks = true // Whether to make bigwig tracks or not
params.extreads = true // Whether to extend reads based on fragment size
params.effgsize = 2736124898 // Effective genome size for normalization (hg19 by default)
params.pairedend = false // Whether the input BAM files are paired-end or single-end
workflow {
ch_bams = Channel.fromPath( params.samplesheet ).splitCsv( header:true ) // Parse samplesheet
bam_chip = ch_bams.filter { it.type == 'chip' }.map { row -> file(row.path) } // Separate channel for ChIP BAM
bam_input = ch_bams.filter { it.type == 'input' }.map { row -> file(row.path) } // Separate channel for Input BAM
blacklist = Channel.fromPath(params.blacklist) // Blacklist file
if (params.pairedend) {
bam_format = "BAMPE"
}
else {
bam_format = "BAM"
}
if (params.extreads) {
getPeakModelFragmentSize(
bam_chip,
params.effgsize,
bam_format,
params.prefix
)
fragment_size = getPeakModelFragmentSize.out.fragment_size.first()
}
else {
fragment_size = Channel.value(0) // No read extension
}
if (params.callpeaks) {
callPeaks(
bam_chip,
bam_input,
params.prefix,
params.effgsize,
bam_format
)
filterPeaksByBlacklist(
callPeaks.out,
blacklist.first(),
params.prefix
)
}
if (params.maketracks) {
makeCpmTrack(
ch_bams.map { row -> file(row.path) },
ch_bams.map { row -> file("${row.path}.bai") },
ch_bams.map { row -> row.type },
params.prefix,
blacklist.first(),
params.binsize,
fragment_size,
params.effgsize
)
makeDifferenceTrack(
bam_chip,
bam_chip.map{ "${it}.bai" },
bam_input,
bam_input.map{ "${it}.bai" },
params.prefix,
blacklist.first(),
params.binsize,
fragment_size,
params.effgsize
)
makeRatioTrack(
bam_chip,
bam_chip.map{ "${it}.bai" },
bam_input,
bam_input.map{ "${it}.bai" },
params.prefix,
blacklist.first(),
params.binsize,
fragment_size,
params.effgsize
)
}
}