-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnakeshiba.smk
387 lines (364 loc) · 10.9 KB
/
snakeshiba.smk
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
VERSION = "v0.5.1"
'''
SnakeShiba: A snakemake-based workflow of Shiba for differential RNA splicing analysis between two groups of samples
Usage:
snakemake -s snakeshiba.smk --configfile config.yaml --cores 32 --use-singularity --singularity-args "--bind $HOME:$HOME"
'''
import os
from pathlib import Path
import sys
args = sys.argv
def load_experiment(file):
experiment_dict = {}
with open(file, "r") as f:
for line in f:
sample, bam, group = line.strip().split("\t")
if sample == "sample":
continue
experiment_dict[sample] = {"bam": bam, "group": group}
return experiment_dict
experiment_dict = load_experiment(config["experiment_table"])
juncfiles_list = []
base_dir = os.path.dirname(workflow.snakefile)
command = " ".join(args)
# Replace snakefile path with the absolute path
command = command.replace(workflow.snakefile, os.path.join(base_dir, workflow.snakefile))
# Replace config yaml path with the absolute path
configfile_path= args[args.index("--configfile") + 1]
# Absolute path of the directory containing the config yaml file
configfile_dir_path = Path(configfile_path).resolve().parent
command = command.replace(configfile_path, os.path.join(str(configfile_dir_path), configfile_path))
workdir: config["workdir"]
container: config["container"]
rule all:
input:
event_all = expand("events/EVENT_{sample}.txt", sample = ["SE", "FIVE", "THREE", "MXE", "RI", "MSE", "AFE", "ALE"]),
PSI = expand("results/splicing/PSI_{sample}.txt", sample = ["SE", "FIVE", "THREE", "MXE", "RI", "MSE", "AFE", "ALE"]),
summary = "plots/summary.html",
tpm = "results/expression/TPM.txt",
cpm = "results/expression/CPM.txt",
counts = "results/expression/counts.txt",
deg = "results/expression/DEG.txt",
tpm_pca = "results/pca/tpm_pca.tsv",
tpm_contribution = "results/pca/tpm_contribution.tsv",
psi_pca = "results/pca/psi_pca.tsv",
psi_contribution = "results/pca/psi_contribution.tsv"
params:
version = VERSION
shell:
"""
echo \
'
All analysis finished successfully!
SnakeShiba version: {params.version}
https://github.com/NaotoKubota/Shiba
'
"""
rule bam2gtf:
wildcard_constraints:
sample = "|".join(experiment_dict)
input:
bam = lambda wildcards: experiment_dict[wildcards.sample]["bam"],
gtf = config["gtf"]
output:
temp("annotation/{sample}.gtf")
threads:
8
benchmark:
"benchmark/bam2gtf/{sample}.txt"
log:
"log/bam2gtf/{sample}.log"
shell:
"""
stringtie -p {threads} \
-G {input.gtf} \
-o {output} \
{input.bam} >& {log}
"""
rule merge_gtf:
wildcard_constraints:
sample = "|".join(experiment_dict)
input:
gtflist = expand("annotation/{sample}.gtf", sample = experiment_dict),
gtf = config["gtf"]
output:
"annotation/assembled.gtf"
threads:
workflow.cores
benchmark:
"benchmark/merge_gtf.txt"
log:
"log/merge_gtf.log"
shell:
"""
stringtie --merge \
-p {threads} \
-G {input.gtf} \
-o {output} \
{input.gtflist} >& {log}
"""
rule gtf2event:
input:
assembled_gtf = "annotation/assembled.gtf",
gtf = config["gtf"]
output:
events = directory("events"),
events_all = expand("events/EVENT_{sample}.txt", sample = ["SE", "FIVE", "THREE", "MXE", "RI", "MSE", "AFE", "ALE"])
threads:
workflow.cores
benchmark:
"benchmark/gtf2event.txt"
log:
"log/gtf2event.log"
params:
base_dir = base_dir
shell:
"""
python {params.base_dir}/src/gtf2event.py \
-i {input.assembled_gtf} \
-r {input.gtf} \
-o {output.events} \
-p {threads} \
-v \
>& {log}
"""
rule bam2junc:
wildcard_constraints:
sample = "|".join(experiment_dict)
input:
bam = lambda wildcards: experiment_dict[wildcards.sample]["bam"]
output:
junc = temp("junctions/{sample}.junc")
threads:
1
benchmark:
"benchmark/bam2junc/{sample}_regtools.txt"
log:
"log/bam2junc/{sample}_regtools.log"
shell:
"""
regtools junctions extract \
-a {config[minimum_anchor_length]} \
-m {config[minimum_intron_length]} \
-M {config[maximum_intron_length]} \
-s {config[strand]} \
-o {output.junc} \
{input.bam} >& {log}
"""
rule make_RI_saf:
input:
"events"
output:
temp("junctions/RI.saf")
shell:
"""
cat {input}/EVENT_RI.txt | \
cut -f 6,7 | \
sed -e 1d | \
awk -F'\t' -v OFS='\t' '{{split($1,l,":"); split(l[2],m,"-"); print l[1]":"m[1]"-"m[1]+1,l[1],m[1],m[1]+1,$2; print l[1]":"m[2]-1"-"m[2],l[1],m[2]-1,m[2],$2}}' | \
awk '!a[$0]++' > {output}
"""
rule bam2junc_RI:
wildcard_constraints:
sample = "|".join(experiment_dict)
input:
RI = "junctions/RI.saf",
bam = lambda wildcards: experiment_dict[wildcards.sample]["bam"]
output:
junc = temp("junctions/{sample}_exon-intron.junc"),
junc_summary = temp("junctions/{sample}_exon-intron.junc.summary")
threads:
8
benchmark:
"benchmark/bam2junc/{sample}_featureCounts_RI.txt"
log:
"log/bam2junc/{sample}_featureCounts_RI.log"
params:
base_dir = base_dir
shell:
"""
python {params.base_dir}/src/bam2junc_RI_snakemake.py \
-b {input.bam} \
-r {input.RI} \
-o {output.junc} \
-t {threads} \
-v \
&> {log}
"""
rule merge_junc:
input:
exonexon = expand("junctions/{sample}.junc", sample = experiment_dict),
exonintron = expand("junctions/{sample}_exon-intron.junc", sample = experiment_dict)
output:
"junctions/junctions.bed"
benchmark:
"benchmark/merge_junc.txt"
log:
"log/merge_junc.log"
params:
base_dir = base_dir
shell:
"""
python {params.base_dir}/src/merge_junc_snakemake.py \
--exonexon {input.exonexon} \
--exonintron {input.exonintron} \
--output {output} \
-v \
>& {log}
"""
rule psi:
input:
junc = "junctions/junctions.bed",
events_all = expand("events/EVENT_{sample}.txt", sample = ["SE", "FIVE", "THREE", "MXE", "RI", "MSE", "AFE", "ALE"])
output:
results = directory("results/splicing"),
PSI = expand("results/splicing/PSI_{sample}.txt", sample = ["SE", "FIVE", "THREE", "MXE", "RI", "MSE", "AFE", "ALE"]),
PSI_matrix_sample = "results/splicing/PSI_matrix_sample.txt"
threads:
1
benchmark:
"benchmark/psi.txt"
log:
"log/psi.log"
params:
base_dir = base_dir
shell:
"""
python {params.base_dir}/src/psi_snakemake.py \
-p {threads} \
-g {config[experiment_table]} \
-f {config[fdr]} \
-d {config[delta_psi]} \
-m {config[minimum_reads]} \
-r {config[reference_group]} \
-a {config[alternative_group]} \
-i {config[individual_psi]} \
-t {config[ttest]} \
--onlypsi False \
--onlypsi-group False \
--excel {config[excel]} \
-v \
{input.junc} \
events \
{output.results} >& {log}
"""
rule expression_featureCounts:
wildcard_constraints:
sample = "|".join(experiment_dict)
input:
bam = lambda wildcards: experiment_dict[wildcards.sample]["bam"],
output:
counts = temp("results/expression/{sample}_counts.txt"),
counts_summary = temp("results/expression/{sample}_counts.txt.summary")
threads:
8
benchmark:
"benchmark/expression/{sample}_featureCounts.txt"
log:
"log/expression/{sample}_featureCounts.log"
params:
base_dir = base_dir
shell:
"""
python {params.base_dir}/src/expression_featureCounts_snakemake.py \
-b {input.bam} \
-g {config[gtf]} \
-o {output.counts} \
-t {threads} \
-v \
&> {log}
"""
rule expression_tpm:
input:
counts = expand("results/expression/{sample}_counts.txt", sample = experiment_dict)
output:
tpm = "results/expression/TPM.txt",
cpm = "results/expression/CPM.txt",
counts = "results/expression/counts.txt"
benchmark:
"benchmark/expression/TPM_CPM.txt"
log:
"log/expression/TPM_CPM.log"
params:
base_dir = base_dir
shell:
"""
python {params.base_dir}/src/tpm_snakemake.py \
--countfiles {input.counts} \
--output results/expression/ \
-v \
&> {log}
"""
rule deseq2:
input:
counts = "results/expression/counts.txt"
output:
deseq2 = "results/expression/DEG.txt"
benchmark:
"benchmark/expression/DESeq2.txt"
log:
"log/expression/DESeq2.log"
params:
base_dir = base_dir
shell:
"""
python {params.base_dir}/src/deseq2_snakemake.py \
--count {input.counts} \
--experiment-table {config[experiment_table]} \
--reference {config[reference_group]} \
--alternative {config[alternative_group]} \
--output {output.deseq2} \
-v \
&> {log}
"""
rule pca:
input:
tpm = "results/expression/TPM.txt",
PSI_matrix_sample = "results/splicing/PSI_matrix_sample.txt"
output:
tpm_pca = "results/pca/tpm_pca.tsv",
tpm_contribution = "results/pca/tpm_contribution.tsv",
psi_pca = "results/pca/psi_pca.tsv",
psi_contribution = "results/pca/psi_contribution.tsv"
benchmark:
"benchmark/pca/pca.txt"
log:
"log/pca.log"
params:
base_dir = base_dir
shell:
"""
python {params.base_dir}/src/pca.py \
--input-tpm {input.tpm} \
--input-psi {input.PSI_matrix_sample} \
-g 3000 \
-o results/pca \
-v \
&> {log}
"""
rule plots:
input:
PSI = expand("results/splicing/PSI_{sample}.txt", sample = ["SE", "FIVE", "THREE", "MXE", "RI", "MSE", "AFE", "ALE"]),
tpm_pca = "results/pca/tpm_pca.tsv",
tpm_contribution = "results/pca/tpm_contribution.tsv",
psi_pca = "results/pca/psi_pca.tsv",
psi_contribution = "results/pca/psi_contribution.tsv"
output:
summary = "plots/summary.html"
benchmark:
"benchmark/plots.txt"
log:
"log/plots.log"
params:
base_dir = base_dir,
command = command
shell:
"""
python {params.base_dir}/src/plots.py \
-i results \
-e {config[experiment_table]} \
-s "{params.command}" \
-o plots \
-v \
>& {log}
"""