forked from sunbeam-labs/sbx_sga
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsbx_sga.smk
More file actions
338 lines (311 loc) · 9.97 KB
/
sbx_sga.smk
File metadata and controls
338 lines (311 loc) · 9.97 KB
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
ISOLATE_FP = Cfg["all"]["output_fp"] / "isolate"
TOOLS = {
"shovill": ["number of contigs", "min coverage", "max coverage", "mean coverage"],
"checkm": ["Completeness", "Contamination"],
"sylph": ["Taxonomic_abundance", "Contig_name"],
"mlst": ["Schema", "ST", "Alleles"],
"bakta": [
"Length",
"GC",
"N50",
"CDSs",
"tRNAs",
"tmRNAs",
"rRNAs",
"hypotheticals",
"CRISPR arrays",
],
"mash": ["Mash_Contamination", "Contaminated_Spp"],
}
try:
SBX_SGA_VERSION = get_ext_version("sbx_sga")
except NameError:
# For backwards compatibility with older versions of Sunbeam
SBX_SGA_VERSION = "0.0.0"
localrules:
all_sga,
sga_report,
rule all_sga:
input:
expand(ISOLATE_FP / "quast" / "{sample}" / "report.tsv", sample=Samples),
assembly_qcs=ISOLATE_FP / "assembly_qcs.tsv",
taxonomic_assignments=ISOLATE_FP / "taxonomic_assignments.tsv",
antimicrobials=ISOLATE_FP / "antimicrobials.tsv",
## Assembly
rule sga_shovill:
input:
rp1=QC_FP / "decontam" / "{sample}_1.fastq.gz",
rp2=QC_FP / "decontam" / "{sample}_2.fastq.gz",
output:
contigs=ISOLATE_FP / "shovill" / "{sample}" / "{sample}.fa",
log:
LOG_FP / "sga_shovill_{sample}.log",
benchmark:
BENCHMARK_FP / "sga_shovill_{sample}.tsv"
conda:
"envs/shovill.yml"
resources:
mem_mb=32000,
shell:
"""
(shovill --force --assembler skesa --outdir $(dirname {output.contigs}) --R1 {input.rp1} --R2 {input.rp2} &> {log} &&
mv $(dirname {output.contigs})/contigs.fa {output.contigs}) ||
touch {output.contigs}
"""
# Taxonomic classification
rule sga_sylph:
input:
rp1=QC_FP / "decontam" / "{sample}_1.fastq.gz",
rp2=QC_FP / "decontam" / "{sample}_2.fastq.gz",
output:
report=ISOLATE_FP / "sylph" / "{sample}" / "{sample}.tsv",
threads: 8
params:
ref=Cfg["sbx_sga"]["sylph_ref"],
log:
LOG_FP / "sga_sylph_{sample}.log",
benchmark:
BENCHMARK_FP / "sga_sylph_{sample}.tsv"
conda:
"envs/sylph.yml"
resources:
mem_mb=32000,
runtime=120,
shell:
"""
if [ $(zcat {input.rp1} | wc -l) -ge 4 ] && [ $(zcat {input.rp2} | wc -l) -ge 4 ]; then
sylph sketch -1 "{input.rp1}" -2 "{input.rp2}" -t 1 -d "$(dirname "{output.report}")" &> "{log}"
sylph profile "{params.ref}" "$(dirname "{output.report}")"/*.sylsp -t {threads} -o "{output.report}" &>> "{log}"
else
echo "Input reads are empty, creating empty sylph report" > {log}
touch {output.report}
fi
"""
### Assembly QC
rule sga_checkm:
input:
contigs=ISOLATE_FP / "shovill" / "{sample}" / "{sample}.fa",
output:
quality_report=ISOLATE_FP / "checkm" / "{sample}" / "quality_report.tsv",
params:
ref=Cfg["sbx_sga"]["checkm_ref"],
log:
LOG_FP / "sga_checkm_{sample}.log",
benchmark:
BENCHMARK_FP / "sga_checkm_{sample}.tsv"
conda:
"envs/checkm2.yml"
resources:
mem_mb=16000,
shell:
"""
if [ -s {input.contigs} ]; then
checkm2 predict \\
--force \\
-x fa \\
-i {input.contigs} \\
-o $(dirname {output.quality_report}) \\
--database_path {params.ref} \\
&> {log} || touch {output.quality_report}
else
echo "Empty assembly file {input.contigs}, creating empty checkm2 quality report" > {log}
touch {output.quality_report}
fi
"""
rule sga_quast:
input:
contigs=ISOLATE_FP / "shovill" / "{sample}" / "{sample}.fa",
output:
report=ISOLATE_FP / "quast" / "{sample}" / "report.tsv",
log:
LOG_FP / "sga_quast_{sample}.log",
benchmark:
BENCHMARK_FP / "sga_quast_{sample}.tsv"
conda:
"envs/quast.yml"
shell:
"""
if [ -s {input.contigs} ]; then
quast.py \\
-o $(dirname {output.report}) \\
{input.contigs} \\
> {log} 2>&1 || touch {output.report}
else
echo "Empty assembly file {input.contigs}, creating empty quast report" > {log}
touch {output.report}
fi
"""
rule sga_mash:
input:
reads=expand(QC_FP / "decontam" / "{{sample}}_{rp}.fastq.gz", rp=Pairs),
output:
agg=temp(ISOLATE_FP / "mash" / "{sample}" / "{sample}.fastq"),
win=temp(ISOLATE_FP / "mash" / "{sample}" / "{sample}_winning.tab"),
sort=ISOLATE_FP / "mash" / "{sample}" / "{sample}_sorted_winning.tab",
params:
ref=Cfg["sbx_sga"]["mash_ref"],
log:
LOG_FP / "sga_mash_{sample}.log",
benchmark:
BENCHMARK_FP / "sga_mash_{sample}.tsv"
conda:
"envs/mash.yml"
resources:
mem_mb=16000,
shell:
"""
zcat {input.reads} > {output.agg}
if [ -s {output.agg} ]; then
mash screen -w -p 8 {params.ref} {output.agg} > {output.win} 2> {log}
sort -gr {output.win} > {output.sort} 2>> {log}
else
echo "Empty aggregated reads file {output.agg}, creating empty mash output files" > {log}
touch {output.win} {output.sort}
fi
"""
# Typing
rule sga_mlst:
input:
contigs=ISOLATE_FP / "shovill" / "{sample}" / "{sample}.fa",
output:
mlst=ISOLATE_FP / "mlst" / "{sample}" / "{sample}.mlst",
log:
LOG_FP / "sga_mlst_{sample}.log",
benchmark:
BENCHMARK_FP / "sga_mlst_{sample}.tsv"
conda:
"envs/mlst.yml"
shell:
"""
if [ -s {input.contigs} ]; then
mlst --nopath {input.contigs} > {output.mlst} 2> {log}
else
echo "Empty assembly file {input.contigs}, creating empty mlst output" > {log}
mkdir -p $(dirname {output.mlst})
touch {output.mlst}
fi
"""
### Annotation
rule sga_bakta:
input:
contigs=ISOLATE_FP / "shovill" / "{sample}" / "{sample}.fa",
output:
bakta=ISOLATE_FP / "bakta" / "{sample}" / "{sample}.txt",
params:
ref=Cfg["sbx_sga"]["bakta_ref"],
log:
LOG_FP / "sga_bakta_{sample}.log",
benchmark:
BENCHMARK_FP / "sga_bakta_{sample}.tsv"
conda:
"envs/bakta.yml"
resources:
mem_mb=32000,
runtime=180,
shell:
"""
if [ -s {input.contigs} ]; then
bakta --force --db {params.ref} \\
--output $(dirname {output.bakta}) \\
--prefix {wildcards.sample} \\
--skip-plot {input.contigs} \\
> {log} 2>&1
else
echo "Empty assembly file {input.contigs}, creating empty bakta output" > {log}
touch {output.bakta}
fi
"""
### AMR Profiling
rule sga_abritamr:
input:
contigs=ISOLATE_FP / "shovill" / "{sample}" / "{sample}.fa",
output:
abritamr=ISOLATE_FP / "abritamr" / "{sample}" / "amrfinder.out",
log:
LOG_FP / "sga_abritamr_{sample}.log",
benchmark:
BENCHMARK_FP / "sga_abritamr_{sample}.tsv"
conda:
"envs/abritamr.yml"
shell:
"""
if [ -s {input.contigs} ]; then
abritamr run \\
--contigs {input.contigs} \\
--prefix {wildcards.sample} \\
> {log} 2>&1
mv {wildcards.sample} $(dirname $(dirname {output.abritamr}))
else
echo "Writing empty AMR output file because assembly is empty" > {log}
mkdir -p $(dirname {output.abritamr})
touch {output.abritamr}
fi
"""
rule sga_report:
input:
shovill=expand(
ISOLATE_FP / "shovill" / "{sample}" / "{sample}.fa", sample=Samples
),
sylph=expand(ISOLATE_FP / "sylph" / "{sample}" / "{sample}.tsv", sample=Samples),
checkm=expand(
ISOLATE_FP / "checkm" / "{sample}" / "quality_report.tsv", sample=Samples
),
mlst=expand(ISOLATE_FP / "mlst" / "{sample}" / "{sample}.mlst", sample=Samples),
bakta=expand(ISOLATE_FP / "bakta" / "{sample}" / "{sample}.txt", sample=Samples),
mash=expand(
ISOLATE_FP / "mash" / "{sample}" / "{sample}_sorted_winning.tab",
sample=Samples,
),
abritamr=expand(
ISOLATE_FP / "abritamr" / "{sample}" / "amrfinder.out", sample=Samples
),
output:
tool_reports=expand(
ISOLATE_FP / "reports" / "{tool}.tsv",
tool=[
"shovill",
"sylph",
"checkm",
"mlst",
"bakta",
"mash",
"abritamr",
],
),
assemblies=ISOLATE_FP / "assemblies.tsv",
assembly_qcs=ISOLATE_FP / "assembly_qcs.tsv",
taxonomic_assignments=ISOLATE_FP / "taxonomic_assignments.tsv",
contaminants=ISOLATE_FP / "contaminants.tsv",
antimicrobials=ISOLATE_FP / "antimicrobials.tsv",
params:
mash_identity=Cfg["sbx_sga"]["mash_identity"],
mash_hits=Cfg["sbx_sga"]["mash_hits"],
mash_median_multiplicity_factor=Cfg["sbx_sga"][
"mash_median_multiplicity_factor"
],
sga_version=SBX_SGA_VERSION,
log:
LOG_FP / "sga_report.log",
benchmark:
BENCHMARK_FP / "sga_report.tsv"
script:
"scripts/summarize_all.py"
# TODO: Should we update this to only leave behind our reports and assembled fastas?
rule clean_sga:
input:
QC_FP / ".decontam_cleaned",
rules.all_sga.input,
output:
touch(ISOLATE_FP / ".sga_cleaned"),
params:
log_dir=LOG_FP,
benchmark_dir=BENCHMARK_FP,
shell:
"""
isolate_dir=$(dirname {output[0]})
log_dir={params.log_dir}
benchmark_dir={params.benchmark_dir}
rm -rf $log_dir
rm -rf $benchmark_dir
find "$isolate_dir/mash" -type f -name "*.fastq" -delete
"""