forked from nebiolabs/EM-seq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cov_vs_meth.nf
365 lines (298 loc) · 11.4 KB
/
cov_vs_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
#!/usr/bin/env nextflow
params.genome = '/mnt/galaxy/data/genome/grch38_core+bs_controls/sam_indexes/grch38_core+bs_controls/grch38_core+bs_controls.fa'
//CPG Islands from UCSC table browser
// Database: hg38 Primary Table: cpgIslandExt Row Count: 31,144 Data last updated: 2018-08-10
params.ucsc_cpg_islands_gtf = 'grch38_cpgIsland_ext.gtf.gz'
params.refseq_gtf = 'GRCh38_latest_genomic.gff.gz'
params.high_quality_meth_bed = '../200ng_4cycles_LB_1.downsampled.md.bam_CpG.txt.bgz'
params.bam_files_glob = '../*.downsampled.md.{bam,bam.bai}'
params.tmp_dir = '/state/partition1/sge_tmp/'
params.output_dir = 'output'
params.ncbi_assembly_report = 'GCF_000001405.39_GRCh38.p13_assembly_report.txt'
params.dfam_out_file = 'grch38_dfam405_repeat_mask.fa.out'
Channel.value(file(params.high_quality_meth_bed)).set{ hq_meth_bed }
Channel.fromFilePairs(params.bam_files_glob, checkIfExists: true).into{ bams_for_epd; bams_for_cpgs; bams_for_refseq; bams_for_dfam }
Channel.value(file(params.ucsc_cpg_islands_gtf)).set { ucsc_cpg_islands_gtf }
Channel.value(file(params.refseq_gtf)).set { refseq_gtf }
Channel.value(file(params.ncbi_assembly_report)).set { ncbi_assembly_report }
Channel.value(file(params.dfam_out_file)).set { dfam_out }
Channel.from(['promoter', 'transcriptional_cis_regulatory_region',
'enhancer', 'mobile_genetic_element', 'primary_transcript',
'lnc_RNA', 'exon', 'mRNAexon1', 'mRNAexon' ]).into { refseq_feature_types; refseq_feature_types_for_gtf }
process clean_epd_gtf {
conda "curl ucsc-bedtogenepred ucsc-genepredtogtf"
output:
file('grch38_promoters.gtf') into epd_promoters_gtf
shell:
'''
curl -fsSL "ftp://ccg.epfl.ch/epdnew/H_sapiens/006/Hs_EPDnew_006_hg38.bed" \
| tr ' ' '\t' \
| bedToGenePred /dev/stdin /dev/stdout \
| genePredToGtf file /dev/stdin /dev/stdout > grch38_promoters.gtf
'''
}
process epd_methylation {
conda "bedtools=2.29.2 htslib=1.9"
publishDir "$params.output_dir", mode: 'copy'
input:
file gtf from epd_promoters_gtf
file bed from hq_meth_bed
output:
file 'epd_promoter_methylation.tsv' into epd_promoter_meth
shell:
'''
bedtools intersect -nonamecheck \
-wa -wb -loj \
-a !{gtf} -b <(bgzip -d < !{bed} ) \
| awk -v FS='\\t' -v OFS='\\t' '$14>0 {print $10,$11,$12,$1":"$4-1"-"$5,($15*1.0)/$14 }' \
| bedtools groupby -g 4 -o mean -c 5 \
> epd_promoter_methylation.tsv
'''
}
process epd_promoter_counts{
conda "subread=2.0.0"
cpus 16
input:
file gtf from epd_promoters_gtf
path('*') from bams_for_epd.map{ [it[1][0],it[1][1]] }.flatten().toList()
output:
file 'epd_promoter_counts.tsv' into epd_promoter_counts
shell:
'''
featureCounts --primary --ignoreDup -Q 10 -M -f -o -O --fraction -p -P -B -C \
-a !{gtf} \
--tmpDir !{params.tmp_dir} \
-T !{task.cpus} \
-o epd_promoter_counts.tsv *.bam
'''
}
process clean_cpg_islands_gtf {
input:
file ucsc_cpg_gtf from ucsc_cpg_islands_gtf
output:
file('grch38_cpg_islands.uniqname.gtf') into cpg_islands_gtf
shell:
'''
zcat !{ucsc_cpg_gtf} \
| awk -v FS='\t' -v OFS='\t' '{print $1,$1":"$4"-"$5,$3,$4,$5,$6,$7,$8,$9}' \
> grch38_cpg_islands.uniqname.gtf
'''
}
process cpg_island_methylation {
conda "bedtools=2.29.2 htslib=1.9"
publishDir "$params.output_dir", mode: 'copy'
input:
file gtf from cpg_islands_gtf
file bed from hq_meth_bed
output:
file 'cpg_island_methylation.tsv' into cpg_island_meth
shell:
'''
bedtools intersect -nonamecheck \
-wa -wb -loj \
-a !{gtf} -b <(bgzip -d < !{bed} ) \
| awk -v FS='\\t' -v OFS='\\t' '$14>0 {print $10,$11,$12,$1":"$4-1"-"$5,($15*1.0)/$14 }' \
| bedtools groupby -g 4 -o mean -c 5 \
> cpg_island_methylation.tsv
'''
}
process cpg_island_counts{
conda "subread=2.0.0"
cpus 16
input:
file gtf from cpg_islands_gtf
path('*') from bams_for_cpgs.map{ [it[1][0],it[1][1]] }.flatten().toList()
output:
file 'cpg_island_counts.tsv' into cpg_island_counts
shell:
'''
featureCounts --primary --ignoreDup -Q 10 -M -f -o -O --fraction -p -P -B -C \
-a !{gtf} \
--tmpDir !{params.tmp_dir} \
-T !{task.cpus} \
-o cpg_island_counts.tsv *.bam
'''
}
process refseq_feature_gtfs {
tag {feature}
conda "subread=2.0.0 bedtools=2.29.2"
input:
file(gtf) from refseq_gtf
file(assembly_report) from ncbi_assembly_report
val feature from refseq_feature_types_for_gtf
output:
tuple feature, file('*.gtf') into feature_gtf_for_meth
tuple feature, file('*_flat.saf') into feature_saf_for_counts
shell:
'''
# uses awk to create a hash lookup from the first file (NCBI assembly report)
# translating chr name in the second file
awk -v OFS='\\t' -v FS='\\t' 'NR==FNR {dict[$1]=$2; next} {$1=dict[$1]; print}' \
<(grep -v '^#' !{assembly_report} | cut -f 7,10 | tr -d '\\r') \
<(zcat !{gtf} | grep -v '^#') \
| grep "GeneID:" \
| grep -P -v "_alt\\t" \
| grep -P -v "^na\\t" \
| sed -r 's/;Dbxref(=[^;]*)GeneID:([^,;]+)([;,])/;gene_id=\\2;Dbxref\\1GeneID:\\2\\3/' \
| awk -v OFS='\\t' -v FS='\\t' \
'($3=="exon") && (index($9,"gbkey=mRNA") > 0) && (index($9,"-1;Parent") > 0) \
{ print($1,$2,"mRNAexon1",$4,$5,$6,$7,$8,$9); next }
($3=="exon") && (index($9,"gbkey=mRNA") > 0) \
{ print($1,$2,"mRNAexon",$4,$5,$6,$7,$8,$9); next }
{ print }
' \
> name_converted.gff
#exons overlap, we want only the longest to avoid 0 cov exons from featureCounts
flattenGTF -a name_converted.gff -o flat_name_converted.saf -t !{feature}
#need to switch to bed for intersection later
tail -n +2 flat_name_converted.saf \
| awk -v OFS='\\t' -v FS='\\t' '{print $2,$3-1,$4,$1,"-",$5}' \
| bedtools sort -faidx !{params.genome}.fai -i /dev/stdin > !{feature}_flat.bed
#filters by feature type
awk -v type=!{feature} -v OFS='\\t' -v FS='\\t' '($3==type) { print}' name_converted.gff \
> !{feature}.gtf
#only include those entries that intersect with the desired feature type, back to SAF format
echo "GeneID\tChr\tStart\tEnd\tStrand" > !{feature}_flat.saf
bedtools intersect -a !{feature}_flat.bed -b !{feature}.gtf -u \
| awk -v OFS='\\t' -v FS='\\t' '{print $4,$1,$2+1,$3,$6}' >> !{feature}_flat.saf
'''
}
process refseq_feature_methylation {
tag {feature}
conda "bedtools=2.29.2 htslib=1.9"
publishDir "$params.output_dir", mode: 'copy'
input:
file bed from hq_meth_bed
tuple feature, file(feature_gtf) from feature_gtf_for_meth
output:
file '*_methylation.tsv' into feature_methylation
shell:
'''
bedtools intersect -nonamecheck \
-wa -wb -loj \
-a !{feature_gtf} -b <(bgzip -d < !{bed} ) \
| awk -v FS='\\t' -v OFS='\\t' '$14>0 {print $10,$11,$12,$1":"$4-1"-"$5,($15*1.0)/$14 }' \
| bedtools groupby -g 4 -o mean -c 5 \
> !{feature}_methylation.tsv
'''
}
feature_saf_for_counts
.combine(bams_for_refseq.map{ [it[1][0],it[1][1]] } ) //combination of every saf with every bam/bai pair)
.groupTuple(by: [0,1])
.set{feature_bams_for_refseq}
process refseq_feature_counts {
conda "subread=2.0.0"
publishDir "$params.output_dir", mode: 'copy'
cpus 16
input:
tuple (feature, path(feature_saf), path('*'), path('*') ) from feature_bams_for_refseq
output:
file '*_counts.tsv' into feature_counts
shell:
'''
featureCounts --primary --ignoreDup -Q 10 -M -f -O --fraction -p -P -B -C \
-a !{feature_saf} -F SAF\
-t !{feature} \
-g 'ID' \
--tmpDir !{params.tmp_dir} \
-T !{task.cpus} \
-o !{feature}_counts.tsv *.bam
'''
}
process dfam_out_to_gtf {
conda "ucsc-bedtogenepred ucsc-genepredtogtf"
input:
file rm_out from dfam_out
output:
file '*.gtf' into (dfam_gtf_for_meth, dfam_gtf_for_counts)
shell:
'''
awk 'OFS="\t" {print($5,$6-1,$7,$11,$1,".")}' !{rm_out} \
| tail -n +4 \
| bedToGenePred /dev/stdin /dev/stdout \
| genePredToGtf file /dev/stdin /dev/stdout \
| awk -v FS='\t' -v OFS='\t' '{print $1,$1":"$4"-"$5,$3,$4,$5,$6,$7,$8,$9}' \
> grch38_dfam405_repeat_mask.gtf
'''
}
process dfam_feature_methylation {
conda "bedtools=2.29.2 htslib=1.9"
publishDir "$params.output_dir", mode: 'copy'
input:
file bed from hq_meth_bed
file(gtf) from dfam_gtf_for_meth
output:
file '*_methylation.tsv' into dfam_methylation
shell:
'''
bedtools intersect -nonamecheck \
-wa -wb -loj \
-a !{gtf} -b <(bgzip -d < !{bed} ) \
| awk -v FS='\\t' -v OFS='\\t' '$14>0 {print $10,$11,$12,$1":"$4-1"-"$5,($15*1.0)/$14 }' \
| bedtools groupby -g 4 -o mean -c 5 \
> dfam_methylation.tsv
'''
}
process dfam_feature_counts {
conda "subread=2.0.0"
publishDir "$params.output_dir", mode: 'copy'
cpus 16
input:
file(gtf) from dfam_gtf_for_counts
path('*') from bams_for_dfam.map{ [it[1][0],it[1][1]] }.flatten().toList()
output:
file '*_counts.tsv' into dfam_feature_counts
shell:
'''
featureCounts --primary --ignoreDup -Q 10 -M -f -o -O --fraction -p -P -B -C \
-a !{gtf} \
-t transcript \
-g 'transcript_id' \
--tmpDir !{params.tmp_dir} \
-T !{task.cpus} \
-o dfam_counts.tsv *.bam
'''
}
process combine_methylation {
publishDir "$params.output_dir", mode: 'copy'
input:
file(dfam_meth) from dfam_methylation
file(feature_meth) from feature_methylation.collect()
file(cpg_meth) from cpg_island_meth
file(epd_meth) from epd_promoter_meth
output:
file('combined_methylation.tsv') into combined_meth
shell:
'''
echo 'File Locus Frac Methylated' > combined_methylation.tsv
#adds a column (tab separated) containing the name of the file being processed (repeated on each line)
for f in !{dfam_meth} !{feature_meth} !{cpg_meth} !{epd_meth} ; do
filebase=$(basename "${f}" _methylation.tsv)
lines=$(wc -l <(grep -ve '^\\s*$' -e '^#' "$f") | cut -f 1 -d ' ')
paste <( yes ${filebase} | head -n $lines ) <(grep -ve '^\\s*$' -e '^#' "$f") >> combined_methylation.tsv
done
'''
}
process combine_counts {
publishDir "$params.output_dir", mode: 'copy'
validExitStatus 0,141
input:
file(dfam_counts) from dfam_feature_counts
file(feature_counts) from feature_counts.collect()
file(cpg_counts) from cpg_island_counts
file(epd_counts) from epd_promoter_counts
output:
file('combined_feature_counts.tsv') into combined_counts
shell:
'''
#constructs the header
echo -n 'File\t' > combined_feature_counts.tsv
grep -hve '^\\s*$' -e '^#' !{cpg_counts} | head -n 1 >> combined_feature_counts.tsv
#adds a column (tab separated) containing the name of the file being processed (repeated on each line)
for f in !{dfam_counts} !{feature_counts} !{cpg_counts} !{epd_counts}; do
filebase=$(basename "${f}" _counts.tsv)
lines=$(wc -l <(grep -ve '^\\s*$' -e '^#' "$f") | cut -f 1 -d ' ')
paste <( yes ${filebase} | head -n $lines ) <(grep -ve '^\\s*$' -e '^#' "$f") | tail -n +2 >> combined_feature_counts.tsv
done
'''
}