-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbx_silva18S.rules
167 lines (146 loc) · 5.8 KB
/
sbx_silva18S.rules
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
# -*- mode: Snakemake -*-
# The original Anaconda environment path is stored here so it can be referred to.
from io import StringIO
import os
CONDA_PREFIX = os.getenv("CONDA_PREFIX")
rule _all_silva_r1:
input:
expand(str(MAPPING_FP/'sbx_silva18S'/'vsearch_r1_{id}_aln_{idcut}_tophits'/'{sample}.vgout'),
sample=Samples.keys(), id=(0.9,), idcut=(0.9,))
rule vsearch_silva_r1:
input:
fa= str(MAPPING_FP/'sbx_silva18S'/'R1'/'{sample}_1.fasta'),
db = str(MAPPING_FP/'sbx_silva18S'/'databases'/'vsearch_{id}'/'SILVA_132_SSURef_Nr99_tax_silva_fungi.udb')
output:
str(MAPPING_FP/'sbx_silva18S'/'vsearch_r1_{id}_aln_{idcut}_tophits'/'{sample}.vgout')
threads:
Cfg['sbx_silva18S']['threads']
log:
str(MAPPING_FP/'sbx_silva18S'/'log'/'vsearch_r1_{id}_aln_{idcut}_tophits'/'{sample}.log')
shell:
"""
vsearch --usearch_global {input.fa} --id {wildcards.idcut} \
--db {input.db} --blast6out {output} \
--threads {threads} --top_hits_only 2> {log}
"""
## This is redundant with shortbred rules. At some time, we need to unify R1.fast
rule fq_2_fa_silva:
input:
str(QC_FP/'decontam'/'{sample}_1.fastq.gz')
output:
str(MAPPING_FP/'sbx_silva18S'/'R1'/'{sample}_1.fasta')
shell:
"""
vsearch --fastq_filter {input} -fastaout {output}
"""
rule _all_fungal_vdb:
input:
str(MAPPING_FP/'sbx_silva18S'/'databases'/'vsearch_{id}'/'SILVA_132_SSURef_Nr99_tax_silva_fungi.udb')
rule build_fungal_vdb:
input:
str(MAPPING_FP/'sbx_silva18S'/'databases'/'vsearch_{id}'/'SILVA_132_SSURef_Nr99_tax_silva_fungi.fasta')
output:
str(MAPPING_FP/'sbx_silva18S'/'databases'/'vsearch_{id}'/'SILVA_132_SSURef_Nr99_tax_silva_fungi.udb')
shell:
"""
vsearch --makeudb_usearch {input} --output {output}
"""
rule vsearch_cluster:
input:
str(MAPPING_FP/'sbx_silva18S'/'databases'/'SILVA_132_SSURef_Nr99_tax_silva_fungi_fixed.fasta')
output:
str(MAPPING_FP/'sbx_silva18S'/'databases'/'vsearch_{id}'/'SILVA_132_SSURef_Nr99_tax_silva_fungi.fasta')
shell:
"""
vsearch --cluster_size {input} --centroids {output} --id {wildcards.id}
"""
rule _all_fungal_db:
input:
str(MAPPING_FP/'sbx_silva18S'/'databases'/'SILVA_132_SSURef_Nr99_tax_silva_fungi_fixed.fasta')
rule silva_fungal_fix:
input:
str(MAPPING_FP/'sbx_silva18S'/'databases'/'SILVA_132_SSURef_Nr99_tax_silva_fungi.fasta')
output:
str(MAPPING_FP/'sbx_silva18S'/'databases'/'SILVA_132_SSURef_Nr99_tax_silva_fungi_fixed.fasta')
run:
seqs = load_fasta(input[0])
with open(output[0], "w") as out:
write_fasta(out, seqs)
# Get the fungal sequences from Silva database and symlink to it in the sbx_silva18S output dir.
rule silva_fungal:
input:
taxa = str(Path(CONDA_PREFIX)/'opt'/'silva_databases'/'taxmap_embl_ssu_ref_nr99_132.txt.gz'),
fasta = str(Path(CONDA_PREFIX)/'opt'/'silva_databases'/'SILVA_132_SSURef_Nr99_tax_silva.fasta.gz')
output:
tmp = temp(str(Path(CONDA_PREFIX)/'opt'/'silva_databases'/'tmp.fasta')),
fungal = str(Path(CONDA_PREFIX)/'opt'/'silva_databases'/'SILVA_132_SSURef_Nr99_tax_silva_fungi.fasta'),
dbdir = str(MAPPING_FP/'sbx_silva18S'/'databases'/'SILVA_132_SSURef_Nr99_tax_silva_fungi.fasta')
shell:
"""
zcat {input.taxa} | grep "Fungi;" | cut -f 1-3 | \
sed "s/\t/\./g" > {output.tmp} && \
seqtk subseq {input.fasta} {output.tmp} > {output.fungal}
cp {output.fungal} {output.dbdir}
"""
# Make sure to download the SILVA database and store it inside Sunbeam environment
# in opt/silva_databases.
rule silva_download:
output:
taxa = str(Path(CONDA_PREFIX)/'opt'/'silva_databases'/'taxmap_embl_ssu_ref_nr99_132.txt.gz'),
fasta = str(Path(CONDA_PREFIX)/'opt'/'silva_databases'/'SILVA_132_SSURef_Nr99_tax_silva.fasta.gz')
shell:
"""
set +o pipefail
path_db={CONDA_PREFIX}/opt/silva_databases
mkdir -p $path_db
wget www.arb-silva.de/fileadmin/silva_databases/release_132/Exports/taxonomy/taxmap_embl_ssu_ref_nr99_132.txt.gz -P $path_db
wget www.arb-silva.de/fileadmin/silva_databases/release_132/Exports/SILVA_132_SSURef_Nr99_tax_silva.fasta.gz -P $path_db
"""
##### HELPER functions
def parse_fasta(f, trim_desc=False):
"""Parse a FASTA format file.
Parameters
----------
f : File object or iterator returning lines in FASTA format.
Returns
-------
An iterator of tuples containing two strings
First string is the sequence description, second is the
sequence.
Notes
-----
This function removes whitespace in the sequence and translates
"U" to "T", in order to accommodate FASTA files downloaded from
SILVA and the Living Tree Project.
"""
f = iter(f)
desc = next(f).strip()[1:]
if trim_desc:
desc = desc.split()[0]
seq = StringIO()
for line in f:
line = line.strip()
if line.startswith(">"):
yield desc, seq.getvalue()
desc = line[1:]
if trim_desc:
desc = desc.split()[0]
seq = StringIO()
else:
seq.write(line.replace(" ", "").replace("U", "T"))
yield desc, seq.getvalue()
def write_fasta(f, seqs):
for desc, seq in seqs.items():
f.write(">{0}\n{1}\n".format(desc, seq))
def load_fasta(filepath, trim_desc=True):
"""Load all sequences from a FASTA file
Parameters
----------
fasta_fp : Input filepath, FASTA format.
Returns
-------
A dictionary mapping sequence identifiers to sequences.
"""
with open(filepath) as f:
seqs = parse_fasta(f, trim_desc=trim_desc)
return dict(seqs)