This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Snakefile
160 lines (143 loc) · 5.11 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
import os
from os import path
import pandas as pd
from collections import OrderedDict
if not workflow.overwrite_configfiles:
configfile: "config.yml"
workdir: path.join(config["workdir_top"], config["pipeline"])
WORKDIR = path.join(config["workdir_top"], config["pipeline"])
RESDIR = config["resdir"]
SNAKEDIR = path.dirname(workflow.snakefile)
PY2_EXEC = "python2 {}/scripts".format(SNAKEDIR)
include: "snakelib/utils.snake"
control_samples = config["control_samples"]
treated_samples = config["treated_samples"]
all_samples = config["control_samples"].copy()
all_samples.update(config["treated_samples"])
datasets = [path.basename(x).rsplit(".", 1)[0] for x in all_samples.values()]
rule dump_versions:
output:
ver = "versions.txt"
conda: "env.yml"
shell:"""
conda list > {output.ver}
"""
rule build_minimap_index: ## build minimap2 index
input:
genome = config["transcriptome"]
output:
index = "index/transcriptome_index.mmi"
params:
opts = config["minimap_index_opts"]
conda: "env.yml"
threads: config["threads"]
shell:"""
minimap2 -t {threads} {params.opts} -I 1000G -d {output.index} {input.genome}
"""
rule map_reads: ## map reads using minimap2
input:
index = rules.build_minimap_index.output.index,
fastq = lambda wildcards: all_samples[wildcards.sample]
output:
bam = "alignments/{sample}.bam",
sbam = "sorted_alignments/{sample}.bam",
params:
opts = config["minimap2_opts"],
msec = config["maximum_secondary"],
psec = config["secondary_score_ratio"],
conda: "env.yml"
threads: config["threads"]
shell:"""
minimap2 -t {threads} -ax map-ont -p {params.psec} -N {params.msec} {params.opts} {input.index} {input.fastq}\
| samtools view -Sb > {output.bam};
samtools sort -@ {threads} {output.bam} -o {output.sbam};
samtools index {output.sbam};
"""
rule count_reads:
input:
bam = rules.map_reads.output.bam,
trs = config["transcriptome"],
output:
tsv = "counts/{sample}_salmon/quant.sf",
params:
tsv_dir = "counts/{sample}_salmon",
libtype = config["salmon_libtype"],
conda: "env.yml"
threads: config["threads"]
shell: """
salmon quant --noErrorModel -p {threads} -t {input.trs} -l {params.libtype} -a {input.bam} -o {params.tsv_dir}
"""
rule merge_counts:
input:
count_tsvs = expand("counts/{sample}_salmon/quant.sf", sample=all_samples.keys()),
output:
tsv = "merged/all_counts.tsv"
conda: "env.yml"
shell:"""
{SNAKEDIR}/scripts/merge_count_tsvs.py -z -o {output.tsv} {input.count_tsvs}
"""
rule write_coldata:
input:
output:
coldata = "de_analysis/coldata.tsv"
run:
samples, conditions, types = [], [], []
for sample in control_samples.keys():
samples.append(sample)
conditions.append("untreated")
types.append("single-read")
for sample in treated_samples.keys():
samples.append(sample)
conditions.append("treated")
types.append("single-read")
df = pd.DataFrame(OrderedDict([('sample', samples),('condition', conditions),('type', types)]))
df.to_csv(output.coldata, sep="\t", index=False)
rule write_de_params:
input:
output:
de_params = "de_analysis/de_params.tsv"
run:
d = OrderedDict()
d["Annotation"] = [config["annotation"]]
d["min_samps_gene_expr"] = [config["min_samps_gene_expr"]]
d["min_samps_feature_expr"] = [config["min_samps_feature_expr"]]
d["min_gene_expr"] = [config["min_gene_expr"]]
d["min_feature_expr"] = [config["min_feature_expr"]]
df = pd.DataFrame(d)
df.to_csv(output.de_params, sep="\t", index=False)
rule de_analysis:
input:
de_params = rules.write_de_params.output.de_params,
coldata = rules.write_coldata.output.coldata,
tsv = rules.merge_counts.output.tsv,
output:
res_dge = "de_analysis/results_dge.tsv",
pdf_dge = "de_analysis/results_dge.pdf",
res_dtu_gene = "de_analysis/results_dtu_gene.tsv",
res_dtu_trs = "de_analysis/results_dtu_transcript.tsv",
res_dtu_stager = "de_analysis/results_dtu_stageR.tsv",
flt_counts = "merged/all_counts_filtered.tsv",
flt_counts_gens = "merged/all_gene_counts.tsv",
conda: "env.yml"
shell:"""
{SNAKEDIR}/scripts/de_analysis.R
"""
rule plot_dtu_res:
input:
res_dtu_stager = "de_analysis/results_dtu_stageR.tsv",
flt_counts = "merged/all_counts_filtered.tsv",
output:
dtu_pdf = "de_analysis/dtu_plots.pdf",
conda: "env.yml"
shell: """
{SNAKEDIR}/scripts/plot_dtu_results.R
"""
rule all:
input:
ver = rules.dump_versions.output.ver,
count_tsvs = expand("counts/{sample}_salmon/quant.sf", sample=all_samples.keys()),
merged_tsv = "merged/all_counts.tsv",
coldata = "de_analysis/coldata.tsv",
de_params = "de_analysis/de_params.tsv",
res_dge = "de_analysis/results_dge.pdf",
dtu_pdf = "de_analysis/dtu_plots.pdf",