generated from sunbeam-labs/sbx_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbx_shotgun_unifrac.smk
335 lines (302 loc) · 10.5 KB
/
sbx_shotgun_unifrac.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
def get_shotgun_unifrac_path() -> Path:
for fp in sys.path:
if fp.split("/")[-1] == "sbx_shotgun_unifrac":
return Path(fp)
raise Error(
"Filepath for sbx_shotgun_unifrac not found, are you sure it's installed under extensions/sbx_shotgun_unifrac?"
)
UNIFRAC_FP = Cfg["all"]["output_fp"] / "shotgun_unifrac"
SBX_SHOTGUN_UNIFRAC_VERSION = (
open(get_shotgun_unifrac_path() / "VERSION").read().strip()
)
SBX_SHOTGUN_UNIFRAC_GG_VERSION = Cfg["sbx_shotgun_unifrac"]["green_genes_version"]
try:
BENCHMARK_FP
except NameError:
BENCHMARK_FP = output_subdir(Cfg, "benchmarks")
try:
LOG_FP
except NameError:
LOG_FP = output_subdir(Cfg, "logs")
localrules:
all_shotgun_unifrac,
rule all_shotgun_unifrac:
input:
UNIFRAC_FP / "faith",
UNIFRAC_FP / "unweighted",
UNIFRAC_FP / "weighted",
# rule su_download_green_genes:
# """Download greengenes db"""
# output:
# phy=Cfg["sbx_shotgun_unifrac"]["green_genes_fp"]
# / f"{SBX_SHOTGUN_UNIFRAC_GG_VERSION}.phylogeny.id.nwk",
# seqs_fp=Cfg["sbx_shotgun_unifrac"]["green_genes_fp"]
# / f"bwa.{SBX_SHOTGUN_UNIFRAC_GG_VERSION}.seqs",
# log:
# LOG_FP / "su_download_green_genes.log",
# benchmark:
# BENCHMARK_FP / "su_download_green_genes.tsv"
# conda:
# "envs/sbx_shotgun_unifrac_env.yml"
# container:
# f"docker://sunbeamlabs/sbx_shotgun_unifrac:{SBX_SHOTGUN_UNIFRAC_VERSION}"
# shell:
# """
# echo "RULE NOT IMPLEMENTED, ASSUMING PREEXISTING DB" > {log}
# """
# rule su_import_green_genes_objects_to_qiime:
# """Probably necessary to create the qza versions of the database objects"""
# input:
# Cfg["sbx_shotgun_unifrac"]["green_genes_fp"]
# / f"{SBX_SHOTGUN_UNIFRAC_GG_VERSION}.phylogeny.id.nwk",
# output:
# phy=Cfg["sbx_shotgun_unifrac"]["green_genes_fp"]
# / f"{SBX_SHOTGUN_UNIFRAC_GG_VERSION}.phylogeny.id.nwk.qza",
# log:
# LOG_FP / "su_import_green_genes_objects_to_qiime.log",
# benchmark:
# BENCHMARK_FP / "su_import_green_genes_objects_to_qiime.tsv"
# conda:
# "envs/sbx_shotgun_unifrac_env.yml"
# container:
# f"docker://sunbeamlabs/sbx_shotgun_unifrac:{SBX_SHOTGUN_UNIFRAC_VERSION}"
# shell:
# """
# echo "RULE NOT IMPLEMENTED, ASSUMING PREEXISTING IMPORTS" > {log}
# """
rule su_temp_install_pip:
"""TEMPORARY: install pip packages because the conda file can't handle it"""
output:
temp(UNIFRAC_FP / ".pip_installed"),
log:
LOG_FP / "su_temp_install_pip.log",
benchmark:
BENCHMARK_FP / "su_temp_install_pip.tsv"
conda:
"envs/sbx_shotgun_unifrac_env.yml"
container:
f"docker://sunbeamlabs/sbx_shotgun_unifrac:{SBX_SHOTGUN_UNIFRAC_VERSION}"
shell:
"""
${{CONDA_PREFIX}}/bin/python -m pip install numpy > {log}
${{CONDA_PREFIX}}/bin/python -m pip install cython >> {log}
${{CONDA_PREFIX}}/bin/python -m pip install q2-greengenes2 >> {log}
touch {output}
"""
rule su_align_to_green_genes:
"""Align reads to greengenes db"""
input:
reads=expand(QC_FP / "decontam" / "{{sample}}_{rp}.fastq.gz", rp=Pairs),
green_genes_bwa_seqs_fp=Cfg["sbx_shotgun_unifrac"]["green_genes_fp"]
/ f"bwa.{SBX_SHOTGUN_UNIFRAC_GG_VERSION}.seqs",
green_genes_bwa_seqs_indexes_fp=[
Cfg["sbx_shotgun_unifrac"]["green_genes_fp"]
/ f"bwa.{SBX_SHOTGUN_UNIFRAC_GG_VERSION}.seqs.{ext}"
for ext in ["amb", "ann", "bwt", "pac", "sa"]
],
pip=UNIFRAC_FP / ".pip_installed",
output:
UNIFRAC_FP / "aligned" / "{sample}.sam",
log:
LOG_FP / "su_align_to_green_genes_{sample}.log",
benchmark:
BENCHMARK_FP / "su_align_to_green_genes_{sample}.tsv"
threads: Cfg["sbx_shotgun_unifrac"]["threads"]
resources:
runtime=240,
conda:
"envs/sbx_shotgun_unifrac_env.yml"
container:
f"docker://sunbeamlabs/sbx_shotgun_unifrac:{SBX_SHOTGUN_UNIFRAC_VERSION}"
shell:
"bwa mem -t {threads} {input.green_genes_bwa_seqs_fp} {input.reads} > {output} 2> {log}"
rule su_woltka_classify:
"""Classify reads using woltka"""
input:
expand(UNIFRAC_FP / "aligned" / "{sample}.sam", sample=Samples),
output:
biom=UNIFRAC_FP / "classified" / "ogu.biom",
log:
LOG_FP / "su_woltka_classify.log",
benchmark:
BENCHMARK_FP / "su_woltka_classify.tsv"
params:
aligned_fp=UNIFRAC_FP / "aligned",
resources:
runtime=240,
conda:
"envs/sbx_shotgun_unifrac_env.yml"
container:
f"docker://sunbeamlabs/sbx_shotgun_unifrac:{SBX_SHOTGUN_UNIFRAC_VERSION}"
shell:
"""
woltka classify -i {params.aligned_fp} -f sam -o {output.biom} > {log} 2>&1
"""
rule su_convert_biom_to_qza:
input:
biom=UNIFRAC_FP / "classified" / "ogu.biom",
output:
qza=UNIFRAC_FP / "classified" / "ogu.table.qza",
log:
LOG_FP / "su_convert_biom_to_qza.log",
benchmark:
BENCHMARK_FP / "su_convert_biom_to_qza.tsv"
resources:
runtime=240,
conda:
"envs/sbx_shotgun_unifrac_env.yml"
container:
f"docker://sunbeamlabs/sbx_shotgun_unifrac:{SBX_SHOTGUN_UNIFRAC_VERSION}"
shell:
"""
qiime tools import --type FeatureTable[Frequency] \
--input-path {input.biom} \
--output-path {output.qza} > {log} 2>&1
"""
rule su_taxonomy_from_table:
"""Get taxonomy from table"""
input:
ogu=UNIFRAC_FP / "classified" / "ogu.table.qza",
tax=Cfg["sbx_shotgun_unifrac"]["green_genes_fp"]
/ f"{SBX_SHOTGUN_UNIFRAC_GG_VERSION}.taxonomy.id.nwk.qza",
output:
UNIFRAC_FP / "classified" / "ogu.taxonomy.qza",
log:
LOG_FP / "su_taxonomy_from_table.log",
benchmark:
BENCHMARK_FP / "su_taxonomy_from_table.tsv"
resources:
runtime=240,
conda:
"envs/sbx_shotgun_unifrac_env.yml"
container:
f"docker://sunbeamlabs/sbx_shotgun_unifrac:{SBX_SHOTGUN_UNIFRAC_VERSION}"
shell:
"""
qiime greengenes2 taxonomy-from-table \
--i-reference-taxonomy {input.tax} \
--i-table {input.ogu} \
--o-classification {output} > {log} 2>&1
"""
rule su_filter_table:
"""Filter table"""
input:
ogu=UNIFRAC_FP / "classified" / "ogu.table.qza",
phy=Cfg["sbx_shotgun_unifrac"]["green_genes_fp"]
/ f"{SBX_SHOTGUN_UNIFRAC_GG_VERSION}.phylogeny.id.nwk.qza",
output:
UNIFRAC_FP / "classified" / "ogu.filtered.table.qza",
log:
LOG_FP / "su_filter_table.log",
benchmark:
BENCHMARK_FP / "su_filter_table.tsv"
conda:
"envs/sbx_shotgun_unifrac_env.yml"
container:
f"docker://sunbeamlabs/sbx_shotgun_unifrac:{SBX_SHOTGUN_UNIFRAC_VERSION}"
shell:
"""
qiime greengenes2 filter-features \
--i-feature-table {input.ogu} \
--i-reference {input.phy} \
--o-filtered-feature-table {output} > {log} 2>&1
"""
rule su_alpha_phylogenetic_diversity:
"""Calculate alpha phylogenetic diversity using Faith's PD"""
input:
phy=Cfg["sbx_shotgun_unifrac"]["green_genes_fp"]
/ f"{SBX_SHOTGUN_UNIFRAC_GG_VERSION}.phylogeny.id.nwk.qza",
ogu=UNIFRAC_FP / "classified" / "ogu.filtered.table.qza",
output:
qza=temp(UNIFRAC_FP / "faith.qza"),
log:
LOG_FP / "su_alpha_phylogenetic_diversity.log",
benchmark:
BENCHMARK_FP / "su_alpha_phylogenetic_diversity.tsv"
conda:
"envs/sbx_q2_diversity_env.yml"
container:
f"docker://sunbeamlabs/sbx_shotgun_unifrac:{SBX_SHOTGUN_UNIFRAC_VERSION}"
shell:
"""
qiime diversity alpha-phylogenetic \
--i-phylogeny {input.phy} \
--i-table {input.ogu} \
--p-metric faith_pd \
--o-alpha-diversity {output.qza} > {log} 2>&1
"""
rule su_weighted_unifrac_distance:
input:
phy=Cfg["sbx_shotgun_unifrac"]["green_genes_fp"]
/ f"{SBX_SHOTGUN_UNIFRAC_GG_VERSION}.phylogeny.id.nwk.qza",
ogu=UNIFRAC_FP / "classified" / "ogu.filtered.table.qza",
output:
qza=temp(UNIFRAC_FP / "weighted.qza"),
log:
LOG_FP / "su_weighted_unifrac_distance.log",
benchmark:
BENCHMARK_FP / "su_weighted_unifrac_distance.tsv"
conda:
"envs/sbx_q2_diversity_env.yml"
container:
f"docker://sunbeamlabs/sbx_shotgun_unifrac:{SBX_SHOTGUN_UNIFRAC_VERSION}"
shell:
"""
qiime diversity beta-phylogenetic \
--i-phylogeny {input.phy} \
--i-table {input.ogu} \
--p-metric weighted_unifrac \
--o-distance-matrix {output.qza} > {log} 2>&1
"""
rule su_unweighted_unifrac_distance:
input:
phy=Cfg["sbx_shotgun_unifrac"]["green_genes_fp"]
/ f"{SBX_SHOTGUN_UNIFRAC_GG_VERSION}.phylogeny.id.nwk.qza",
ogu=UNIFRAC_FP / "classified" / "ogu.filtered.table.qza",
output:
qza=temp(UNIFRAC_FP / "unweighted.qza"),
log:
LOG_FP / "su_unweighted_unifrac_distance.log",
benchmark:
BENCHMARK_FP / "su_unweighted_unifrac_distance.tsv"
conda:
"envs/sbx_q2_diversity_env.yml"
container:
f"docker://sunbeamlabs/sbx_shotgun_unifrac:{SBX_SHOTGUN_UNIFRAC_VERSION}"
shell:
"""
qiime diversity beta-phylogenetic \
--i-phylogeny {input.phy} \
--i-table {input.ogu} \
--p-metric unweighted_unifrac \
--o-distance-matrix {output.qza} > {log} 2>&1
"""
rule su_export_qzas:
input:
faith=UNIFRAC_FP / "faith.qza",
weighted=UNIFRAC_FP / "weighted.qza",
unweighted=UNIFRAC_FP / "unweighted.qza",
output:
faith=UNIFRAC_FP / "faith",
weighted=UNIFRAC_FP / "weighted",
unweighted=UNIFRAC_FP / "unweighted",
log:
LOG_FP / "su_export_qzas.log",
benchmark:
BENCHMARK_FP / "su_export_qzas.tsv"
conda:
"envs/sbx_q2_diversity_env.yml"
container:
f"docker://sunbeamlabs/sbx_shotgun_unifrac:{SBX_SHOTGUN_UNIFRAC_VERSION}"
shell:
"""
qiime tools export \
--input-path {input.faith} \
--output-path {output.faith} > {log} 2>&1
qiime tools export \
--input-path {input.weighted} \
--output-path {output.weighted} >> {log} 2>&1
qiime tools export \
--input-path {input.unweighted} \
--output-path {output.unweighted} >> {log} 2>&1
"""