-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbx_virus_id.smk
executable file
·461 lines (406 loc) · 14 KB
/
sbx_virus_id.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# -*- mode: Snakemake -*-
#
# Rules for running Cenote-Taker2 and other tools in the viral id pipeline
VIRUS_FP = Cfg["all"]["output_fp"] / "virus"
try:
BENCHMARK_FP
except NameError:
BENCHMARK_FP = Cfg["all"]["output_fp"] / "benchmarks"
try:
LOG_FP
except NameError:
LOG_FP = Cfg["all"]["output_fp"] / "logs"
def get_virus_ext_path() -> Path:
ext_path = Path(sunbeam_dir) / "extensions" / "sbx_virus_id"
if ext_path.exists():
return ext_path
raise Error(
"Filepath for virus_id not found, are you sure it's installed under extensions/sbx_virus_id?"
)
SBX_VIRUS_ID_VERSION = open(get_virus_ext_path() / "VERSION").read().strip()
def virus_sorter_input() -> Path:
if Cfg["sbx_virus_id"]["use_spades"]:
return ASSEMBLY_FP / "virus_id_spades" / "{sample}" / "scaffolds.fasta"
else:
return ASSEMBLY_FP / "megahit" / "{sample}_asm" / "final.contigs.fa"
def virus_sorter_output() -> Path:
if Cfg["sbx_virus_id"]["use_virsorter"]:
return VIRUS_FP / "virsorter" / "{sample}.fasta"
else:
return VIRUS_FP / "cenote_taker" / "{sample}.fasta"
rule all_virus_id:
input:
expand(
VIRUS_FP / "alignments" / "{sample}.gene_coverage.tsv",
sample=Samples.keys(),
),
expand(
VIRUS_FP / "blastx" / "{sample}.btf",
sample=Samples.keys(),
),
VIRUS_FP / "summary" / "all_align_summary.txt",
rule virus_id_spades_paired:
input:
r1=QC_FP / "decontam" / "{sample}_1.fastq.gz",
r2=QC_FP / "decontam" / "{sample}_2.fastq.gz",
output:
ASSEMBLY_FP / "virus_id_spades" / "{sample}" / "scaffolds.fasta",
benchmark:
BENCHMARK_FP / "virus_id_spades_paired_{sample}.tsv"
log:
LOG_FP / "virus_id_spades_paired_{sample}.log",
params:
out_fp=str(ASSEMBLY_FP / "virus_id_spades" / "{sample}"),
threads: 4
conda:
"envs/spades_env.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-spades"
resources:
mem_mb=20000,
runtime=720,
shell:
"""
spades.py -1 {input.r1} -2 {input.r2} -t {threads} -o {params.out_fp} 2>&1 | tee {log}
"""
rule install_cenote_taker:
output:
VIRUS_FP / "cenote_taker" / ".installed",
benchmark:
BENCHMARK_FP / "install_cenote_taker.tsv"
log:
LOG_FP / "install_cenote_taker.log",
params:
db_fp=Cfg["sbx_virus_id"]["cenote_taker_db"],
extra_dbs=Cfg["sbx_virus_id"]["cenote_taker_extra_dbs"],
resources:
runtime=2400,
conda:
"envs/cenote_taker_env.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-cenote-taker"
shell:
"""
conda env config vars set CENOTE_DBS={params.db_fp}
if [ -d {params.db_fp} ] && [ "$(ls -A {params.db_fp})" ]; then
echo "Cenote-Taker database already installed" >> {log}
touch {output}
exit 0
fi
if [[ {params.extra_dbs} == "True" ]]; then
echo "Installing Cenote-Taker database with hhsuite" >> {log}
get_ct3_dbs -o {params.db_fp} --hmm T --hallmark_tax T --refseq_tax T --mmseqs_cdd T --domain_list T --hhCDD T --hhPFAM T --hhPDB T >> {log} 2>&1
else
echo "Installing Cenote-Taker database without hhsuite" >> {log}
get_ct3_dbs -o {params.db_fp} --hmm T --hallmark_tax T --refseq_tax T --mmseqs_cdd T --domain_list T >> {log} 2>&1
fi
touch {output}
"""
rule cenote_taker:
input:
contigs=virus_sorter_input(),
install=VIRUS_FP / "cenote_taker" / ".installed",
output:
VIRUS_FP / "cenote_taker" / "{sample}" / "final.contigs.fasta",
VIRUS_FP
/ "cenote_taker"
/ "{sample}"
/ "{sample}"
/ "{sample}_CONTIG_SUMMARY.tsv",
benchmark:
BENCHMARK_FP / "cenote_taker_{sample}.tsv"
log:
LOG_FP / "cenote_taker_{sample}.log",
params:
run_script=str(get_virus_ext_path() / "Cenote-Taker2" / "run_cenote-taker2.py"),
out_dir=str(VIRUS_FP / "cenote_taker"),
sample="{sample}",
db_fp=Cfg["sbx_virus_id"]["cenote_taker_db"],
resources:
mem_mb=24000,
runtime=720,
conda:
"envs/cenote_taker_env.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-cenote-taker"
shell:
"""
SAMPLE={params.sample}
if [[ ${{#SAMPLE}} -lt 18 ]] && [[ {params.sample} =~ ^[a-zA-Z0-9_]+$ ]]; then
echo "Sample name format is valid" >> {log}
else
echo "Cenote-Taker requires a sample name that is less than 18 characters and contains only alphanumeric characters and underscores" >> {log}
exit 1
fi
if [ -s {input.contigs} ]; then
echo "Contigs file exists and is not empty" >> {log}
else
echo "Contigs file is empty" >> {log}
exit 1
fi
cd {params.out_dir}
cenotetaker3 --contigs {input.contigs} -r {params.sample} -p T >> {log} 2>&1
"""
rule install_virsorter:
output:
VIRUS_FP / "virsorter" / ".installed",
benchmark:
BENCHMARK_FP / "install_virsorter.tsv"
log:
LOG_FP / "install_virsorter.log",
params:
db_fp=Cfg["sbx_virus_id"]["virsorter_db"],
resources:
runtime=2400,
threads: 4
conda:
"envs/virsorter_env.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-virsorter"
shell:
"""
# First check if directory exists and has files
if [ -d {params.db_fp} ] && [ "$(ls -A {params.db_fp})" ]; then
echo "VirSorter database already installed"
touch {output}
exit 0
fi
echo "Installing VirSorter database"
virsorter setup -d {params.db_fp} -j 4
touch {output}
"""
rule virsorter:
input:
contigs=virus_sorter_input(),
install=VIRUS_FP / "virsorter" / ".installed",
output:
combined_viral=VIRUS_FP / "virsorter" / "{sample}" / "final-viral-combined.fa",
scores=VIRUS_FP / "virsorter" / "{sample}" / "final-viral-score.tsv",
boundaries=VIRUS_FP / "virsorter" / "{sample}" / "final-viral-boundary.tsv",
benchmark:
BENCHMARK_FP / "virsorter_{sample}.tsv"
log:
LOG_FP / "virsorter_{sample}.log",
params:
out_dir=str(VIRUS_FP / "virsorter" / "{sample}"),
db_fp=Cfg["sbx_virus_id"]["virsorter_db"],
resources:
mem_mb=24000,
runtime=720,
threads: 4
conda:
"envs/virsorter_env.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-virsorter"
shell:
"""
virsorter run -w {params.out_dir} -i {input.contigs} --min-length 1000 -j {threads} --db-dir {params.db_fp} all
"""
rule filter_cenote_contigs:
input:
contigs=VIRUS_FP / "cenote_taker" / "{sample}" / "final.contigs.fasta",
summary=VIRUS_FP
/ "cenote_taker"
/ "{sample}"
/ "{sample}"
/ "{sample}_CONTIG_SUMMARY.tsv",
output:
VIRUS_FP / "cenote_taker" / "{sample}.fasta",
params:
include_phages=Cfg["sbx_virus_id"]["include_phages"],
script:
"scripts/filter_cenote_contigs.py"
rule filter_virsorter_contigs:
input:
contigs=VIRUS_FP / "virsorter" / "{sample}" / "final-viral-combined.fa",
output:
VIRUS_FP / "virsorter" / "{sample}.fasta",
script:
"scripts/filter_virsorter_contigs.py"
rule build_virus_index:
input:
virus_sorter_output(),
output:
str(virus_sorter_output()) + ".1.bt2", # Don't use f-string, broken with python 3.12
conda:
"envs/sbx_virus_id.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-sbx-virus-id"
threads: Cfg["sbx_virus_id"]["bowtie2_build_threads"]
shell:
"bowtie2-build --threads {threads} -f {input} {input}"
rule align_virus_reads:
input:
r1=QC_FP / "decontam" / "{sample}_1.fastq.gz",
r2=QC_FP / "decontam" / "{sample}_2.fastq.gz",
index=str(virus_sorter_output()) + ".1.bt2", # Don't use f-string, broken with python 3.12
output:
temp(VIRUS_FP / "alignments" / "{sample}.sam"),
params:
index=str(virus_sorter_output()),
threads: 6
conda:
"envs/sbx_virus_id.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-sbx-virus-id"
shell:
"bowtie2 -q --local -t --very-sensitive-local --threads {threads} --no-mixed --no-discordant -x {params.index} -1 {input.r1} -2 {input.r2} -S {output}"
rule process_virus_alignment:
input:
VIRUS_FP / "alignments" / "{sample}.sam",
output:
bam=temp(VIRUS_FP / "alignments" / "{sample}.bam"),
sorted=temp(VIRUS_FP / "alignments" / "{sample}.sorted.bam"),
bai=temp(VIRUS_FP / "alignments" / "{sample}.sorted.bam.bai"),
params:
target=str(virus_sorter_output()),
conda:
"envs/sbx_virus_id.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-sbx-virus-id"
shell:
"""
samtools view -bT {params.target} {input} > {output.bam}
samtools sort -o {output.sorted} {output.bam}
samtools index {output.sorted} {output.bai}
"""
rule calculate_mapping_stats:
input:
bam=VIRUS_FP / "alignments" / "{sample}.sorted.bam",
idx=VIRUS_FP / "alignments" / "{sample}.sorted.bam.bai",
output:
VIRUS_FP / "alignments" / "{sample}.sorted.idxstats.tsv",
conda:
"envs/sbx_virus_id.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-sbx-virus-id"
shell:
"""
samtools idxstats {input.bam} > {output}
"""
rule virus_mpileup:
input:
bam=VIRUS_FP / "alignments" / "{sample}.sorted.bam",
idx=VIRUS_FP / "alignments" / "{sample}.sorted.bam.bai",
contigs=virus_sorter_output(),
output:
VIRUS_FP / "alignments" / "{sample}.mpileup",
conda:
"envs/sbx_virus_id.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-sbx-virus-id"
shell:
"""
samtools mpileup -f {input.contigs} {input.bam} > {output}
"""
rule filter_virus_coverage:
input:
fa=virus_sorter_output(),
idx=VIRUS_FP / "alignments" / "{sample}.sorted.idxstats.tsv",
output:
VIRUS_FP / "final_{sample}_contigs.fasta",
log:
LOG_FP / "filter_virus_coverage_{sample}.log",
script:
"scripts/filter_virus_coverage.py"
rule virus_blastx:
"""Run blastx on untranslated genes against a target db and write to blast tabular format."""
input:
VIRUS_FP / "final_{sample}_contigs.fasta",
output:
VIRUS_FP / "blastx" / "{sample}.btf",
benchmark:
BENCHMARK_FP / "run_virus_blastx_{sample}.tsv"
log:
LOG_FP / "run_virus_blastx_{sample}.log",
params:
blast_db=Cfg["sbx_virus_id"]["blast_db"],
threads: Cfg["sbx_virus_id"]["blastx_threads"]
resources:
mem_mb=24000,
runtime=720,
conda:
"envs/sbx_virus_id.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-sbx-virus-id"
shell:
"""
if [ -s {input} ]; then
export BLASTDB=$(dirname {params.blast_db})
blastx \
-query {input} \
-db $(basename {params.blast_db}) \
-outfmt "7 qacc sacc pident length mismatch gapopen qstart qend sstart send evalue bitscore stitle" \
-num_threads {threads} \
-evalue 0.05 \
-max_target_seqs 100 \
-out {output} \
2>&1 | tee {log}
else
echo "Caught empty query" >> {log}
touch {output}
fi
"""
rule calculate_coverage:
input:
bam=VIRUS_FP / "alignments" / "{sample}.sorted.bam",
idx=VIRUS_FP / "alignments" / "{sample}.sorted.bam.bai",
output:
VIRUS_FP / "alignments" / "{sample}.genomecoverage.txt",
params:
ext_fp=str(get_virus_ext_path()),
conda:
"envs/sbx_virus_id.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-sbx-virus-id"
shell:
"""
samtools view -b {input.bam} | genomeCoverageBed -ibam stdin | grep -v 'genome'| perl {params.ext_fp}/scripts/coverage_counter.pl > {output}
"""
rule combine_coverage_stats:
input:
cov=VIRUS_FP / "alignments" / "{sample}.genomecoverage.txt",
stats=VIRUS_FP / "alignments" / "{sample}.sorted.idxstats.tsv",
output:
VIRUS_FP / "alignments" / "{sample}.align.summary.txt",
benchmark:
BENCHMARK_FP / "combine_coverage_stats_{sample}.tsv"
log:
LOG_FP / "combine_coverage_stats_{sample}.log",
params:
ext_fp=str(get_virus_ext_path()),
conda:
"envs/r_env.yml"
container:
"docker://r-base:latest"
shell:
"""
Rscript {params.ext_fp}/scripts/combine_coverage_stats.R {input.cov} {input.stats} {output} 2>&1 | tee {log}
"""
rule virus_coverage_per_gene:
input:
mpileup=VIRUS_FP / "alignments" / "{sample}.mpileup",
btf=VIRUS_FP / "blastx" / "{sample}.btf",
output:
tsv=VIRUS_FP / "alignments" / "{sample}.gene_coverage.tsv",
params:
contigs=virus_sorter_output(),
conda:
"envs/sbx_virus_id.yml"
container:
f"docker://sunbeamlabs/sbx_virus_id:{SBX_VIRUS_ID_VERSION}-sbx-virus-id"
script:
"scripts/virus_coverage_per_gene.py"
rule all_summary:
input:
expand(
VIRUS_FP / "alignments" / "{sample}.align.summary.txt",
sample=Samples.keys(),
),
output:
VIRUS_FP / "summary" / "all_align_summary.txt",
shell:
"""
echo -e "Sample\tAlignTarget\tFractionCoverage\tTargetLength\tMappedReads" > {output}
cat {input} >> {output}
"""