-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnakefile
326 lines (291 loc) · 9.62 KB
/
Snakefile
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
# Author: Jeffrey Grover
# Purpose: Run the whole-genome bisulfite sequencing workflow
# Created: 2019-05-22
# Get overall workflow parameters from config.yaml
configfile: 'config.yaml'
SAMPLES = config['samples']
REFERENCE_GENOME = config['reference_genome']
rule all:
input:
expand('6_mosdepth/{sample}.sorted.markdupes.coverage.txt',
sample=SAMPLES),
expand('5_methyldackel_extract/{sample}.sorted.markdupes_{context}.{ext}',
sample=SAMPLES, context=['CpG', 'CHG', 'CHH'],
ext=['bedGraph', 'methylKit']),
expand('2_trim_galore/{sample}_R{mate}_val_{mate}_fastqc.{ext}',
sample=SAMPLES, mate=[1, 2], ext=['html', 'zip'])
# Index the reference genome
# ancient() will assume the reference is older than output files if they exist
rule bwameth_index:
input:
ancient(REFERENCE_GENOME)
output:
REFERENCE_GENOME + '.bwameth.c2t',
REFERENCE_GENOME + '.bwameth.c2t.amb',
REFERENCE_GENOME + '.bwameth.c2t.ann',
REFERENCE_GENOME + '.bwameth.c2t.bwt',
REFERENCE_GENOME + '.bwameth.c2t.pac',
REFERENCE_GENOME + '.bwameth.c2t.sa'
params:
bwameth_path = config['paths']['bwameth_path'],
shell:
'{params.bwameth_path} index {input}'
# Index the reference genome with faidx
# ancient() will assume the reference is older than output files if they exist
rule samtools_faidx:
input:
ancient(REFERENCE_GENOME)
output:
REFERENCE_GENOME + '.fai'
params:
samtools_path = config['paths']['samtools_path']
shell:
'{params.samtools_path} faidx {input}'
# Run fastqc on the raw .fastq.gz files
rule fastqc_raw:
input:
'input_data/{sample}_R{mate}.fastq.gz'
output:
'1_fastqc_raw/{sample}_R{mate}_fastqc.html',
'1_fastqc_raw/{sample}_R{mate}_fastqc.zip'
params:
fastqc_path = config['paths']['fastqc_path'],
out_dir = '1_fastqc_raw/'
shell:
'{params.fastqc_path} -o {params.out_dir} {input}'
# Trim the read pairs
rule trim_galore:
input:
'1_fastqc_raw/{sample}_R1_fastqc.html',
'1_fastqc_raw/{sample}_R1_fastqc.zip',
'1_fastqc_raw/{sample}_R2_fastqc.html',
'1_fastqc_raw/{sample}_R2_fastqc.zip',
R1 = 'input_data/{sample}_R1.fastq.gz',
R2 = 'input_data/{sample}_R2.fastq.gz'
output:
'2_trim_galore/{sample}_R1_val_1.fq.gz',
'2_trim_galore/{sample}_R1.fastq.gz_trimming_report.txt',
'2_trim_galore/{sample}_R2_val_2.fq.gz',
'2_trim_galore/{sample}_R2.fastq.gz_trimming_report.txt'
params:
adapter_seq = config['trim_galore']['adapter_seq'],
out_dir = '2_trim_galore',
trim_galore_path = config['paths']['trim_galore_path']
shell:
'''
{params.trim_galore_path} \
--a {params.adapter_seq} \
--gzip \
--trim-n \
--quality 20 \
--output_dir {params.out_dir} \
--paired \
{input.R1} {input.R2} \
'''
# Run fastqc on the trimmmed reads
rule fastqc_trimmmed:
input:
'2_trim_galore/{sample}_R{mate}.fastq.gz_trimming_report.txt',
fq_gz = '2_trim_galore/{sample}_R{mate}_val_{mate}.fq.gz'
output:
'2_trim_galore/{sample}_R{mate}_val_{mate}_fastqc.html',
'2_trim_galore/{sample}_R{mate}_val_{mate}_fastqc.zip'
params:
fastqc_path = config['paths']['fastqc_path'],
out_dir = '2_trim_galore/'
shell:
'{params.fastqc_path} -o {params.out_dir} {input.fq_gz}'
# Align to the reference
rule bwameth_align:
input:
{rules.bwameth_index.output},
R1 = '2_trim_galore/{sample}_R1_val_1.fq.gz',
R2 = '2_trim_galore/{sample}_R2_val_2.fq.gz'
output:
temp('temp_data/{sample}.bam')
log:
'3_aligned_sorted_markdupes/{sample}.bwameth.log'
threads:
config['bwameth']['threads']
params:
bwameth_path = config['paths']['bwameth_path'],
genome = REFERENCE_GENOME
shell:
'''
{params.bwameth_path} \
-t {threads} \
--reference {params.genome} \
{input.R1} {input.R2} \
2> {log} \
| samtools view -b - \
> {output}
'''
# Sort the output files
rule samtools_sort:
input:
'temp_data/{sample}.bam'
output:
temp('temp_data/{sample}.sorted.bam')
threads:
config['samtools_sort']['threads']
params:
samtools_path = config['paths']['samtools_path'],
mem = config['samtools_sort']['mem']
shell:
'''
{params.samtools_path} sort \
-@ {threads} \
-m {params.mem} \
-O BAM \
-T {input}.samtools_sort.tmp \
-o {output} \
{input} \
'''
# Mark potential PCR duplicates with Picard Tools
rule mark_dupes:
input:
'temp_data/{sample}.sorted.bam'
output:
'3_aligned_sorted_markdupes/{sample}.sorted.markdupes.bam'
log:
'3_aligned_sorted_markdupes/{sample}.sorted.markdupes.log'
params:
picard_path = config['paths']['picard_path']
shell:
'''
{params.picard_path} MarkDuplicates \
I={input} \
O={output} \
M={log}
'''
rule samtools_index:
input:
'3_aligned_sorted_markdupes/{sample}.sorted.markdupes.bam'
output:
'3_aligned_sorted_markdupes/{sample}.sorted.markdupes.bai'
threads:
config['samtools_index']['threads']
params:
samtools_path = config['paths']['samtools_path']
shell:
'''
{params.samtools_path} index \
-@ {threads} \
-b \
{input} \
{output}
'''
# Run MethylDackel to get the inclusion bounds for methylation calling
rule methyldackel_mbias:
input:
{rules.samtools_faidx.output},
'3_aligned_sorted_markdupes/{sample}.sorted.markdupes.bai',
bam = '3_aligned_sorted_markdupes/{sample}.sorted.markdupes.bam'
output:
'4_methyldackel_mbias/{sample}.sorted.markdupes_OB.svg',
'4_methyldackel_mbias/{sample}.sorted.markdupes_OT.svg',
mbias = '4_methyldackel_mbias/{sample}.sorted.markdupes.mbias'
threads:
config['methyldackel']['threads']
params:
methyldackel_path = config['paths']['methyldackel_path'],
out_prefix = '4_methyldackel_mbias/{sample}.sorted.markdupes',
genome = REFERENCE_GENOME
shell:
'''
{params.methyldackel_path} mbias \
--CHG \
--CHH \
-@ {threads} \
{params.genome} \
./{input.bam} \
./{params.out_prefix} \
2> ./{output.mbias}
'''
# Run MethylDackel to extract cytosine stats
rule methyldackel_extract:
input:
{rules.samtools_faidx.output},
'3_aligned_sorted_markdupes/{sample}.sorted.markdupes.bai',
bam = '3_aligned_sorted_markdupes/{sample}.sorted.markdupes.bam',
mbias = '4_methyldackel_mbias/{sample}.sorted.markdupes.mbias'
output:
'5_methyldackel_extract/{sample}.sorted.markdupes_CpG.bedGraph',
'5_methyldackel_extract/{sample}.sorted.markdupes_CHG.bedGraph',
'5_methyldackel_extract/{sample}.sorted.markdupes_CHH.bedGraph',
'5_methyldackel_extract/{sample}.sorted.markdupes_CpG.methylKit',
'5_methyldackel_extract/{sample}.sorted.markdupes_CHG.methylKit',
'5_methyldackel_extract/{sample}.sorted.markdupes_CHH.methylKit'
threads:
config['methyldackel']['threads']
params:
methyldackel_path = config['paths']['methyldackel_path'],
out_prefix = './5_methyldackel_extract/{sample}.sorted.markdupes',
genome = REFERENCE_GENOME
shell:
'''
# Get bounds for inclusion from the .mbias files
OT=$(cut -d ' ' -f 5 {input.mbias})
OB=$(cut -d ' ' -f 7 {input.mbias})
# Get a MethylKit compatible file
{params.methyldackel_path} extract \
--CHG \
--CHH \
--OT $OT \
--OB $OB \
--methylKit \
-@ {threads} \
-o {params.out_prefix} \
{params.genome} \
{input.bam}
# Get the normal bedGraph output file
{params.methyldackel_path} extract \
--CHG \
--CHH \
--OT $OT \
--OB $OB \
-@ {threads} \
-o {params.out_prefix} \
{params.genome} \
{input.bam}
'''
# Get the depth for each sample
rule mosdepth:
input:
'3_aligned_sorted_markdupes/{sample}.sorted.markdupes.bai',
bam = '3_aligned_sorted_markdupes/{sample}.sorted.markdupes.bam'
output:
'6_mosdepth/{sample}.sorted.markdupes.mosdepth.global.dist.txt',
'6_mosdepth/{sample}.sorted.markdupes.mosdepth.summary.txt',
'6_mosdepth/{sample}.sorted.markdupes.per-base.bed.gz',
'6_mosdepth/{sample}.sorted.markdupes.per-base.bed.gz.csi'
threads:
config['mosdepth']['threads']
params:
mapping_quality = config['mosdepth']['mapping_quality'],
mosdepth_path = config['paths']['mosdepth_path'],
out_prefix = '6_mosdepth/{sample}.sorted.markdupes'
shell:
'''
{params.mosdepth_path} \
-x \
-t {threads} \
-Q {params.mapping_quality} \
{params.out_prefix} \
{input.bam}
'''
# Calculate the coverage from the mosdepth output
rule calc_coverage:
input:
bed = '6_mosdepth/{sample}.sorted.markdupes.per-base.bed.gz'
output:
'6_mosdepth/{sample}.sorted.markdupes.coverage.txt'
params:
genome = REFERENCE_GENOME
shell:
'''
scripts/mosdepth_to_x_coverage.py \
-f {params.genome} \
-m {input.bed} \
> {output}
'''