This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSnakefile
228 lines (198 loc) · 7.64 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
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
# Generates a BLAST formatted database of predicted amplicons from an existing
# BLAST database and a fasta file containing primers.
import os
shell.executable("bash")
# Settings ------------------------------------------------------------------------------------------------------------------
workdir: config["workdir"]
# Functions -----------------------------------------------------------------------------------------------------------------
def generate_db_name(wildcards=None):
path, dbname = os.path.split(config["blast_db"])
path, primername = os.path.split(config["primers"])
return dbname + '_' + primername.split('.')[0]
# Input rule ----------------------------------------------------------------------------------------------------------------
rule all:
input:
# "primer_blast/missing_barcodes.txt",
"primer_blast/no_barcodes.txt",
"blast_db/{name}.fasta".format(name = generate_db_name()),
expand("blast_db/{name}.{ext}", name = generate_db_name(), ext= ["nto", "ntf", "nsq", "not", "nos", "nog", "nin", "nhr", "ndb"]),
"report.txt"
# Workflow ------------------------------------------------------------------------------------------------------------------
rule get_taxid_from_db:
output:
temp("db_filtering/taxid_list.txt")
params:
blast_DB = config["blast_db"],
taxdb = config["taxdb"]
message: "Extracting taxid list from database"
conda: "./envs/blast.yaml"
shell:
"""
export BLASTDB={params.taxdb}
blastdbcmd -db {params.blast_DB} -tax_info -outfmt '%T' > {output}
"""
rule filter_taxid:
input:
"db_filtering/taxid_list.txt"
output:
mask = "db_filtering/taxid_mask.txt",
failed = "db_filtering/taxid_missing.txt"
params:
taxid = config["parent_node"],
lineage = config["rankedlineage_dmp"],
nodes = config["nodes_dmp"]
message: "Extracting taxids under parent node"
script:
"./scripts/make_blast_mask.py"
rule get_seqidlist:
input:
"db_filtering/taxid_mask.txt"
output:
seqids = temp("db_filtering/seqids.txt"),
binary = temp("db_filtering/seqids.acc"),
id_table = temp("db_filtering/table.tsv")
params:
blast_DB = config["blast_db"],
taxdb = config["taxdb"]
message: "Retrieving SeqIDs to search"
conda: "./envs/blast.yaml"
shell:
"""
export BLASTDB={params.taxdb}
blastdbcmd -db {params.blast_DB} -taxidlist {input} -outfmt '%a\t%T\t%S' > {output.id_table}
cat {output.id_table} | cut -d$'\t' -f1 > {output.seqids}
blastdb_aliastool -seqid_file_in {output.seqids} -seqid_file_out {output.binary}
"""
rule primers_explicit:
input:
config["primers"]
output:
"primers_explicit.fa"
message:
"Disambiguating primers"
script:
"scripts/IUPAC_translate.py"
rule find_primer_matches:
input:
seqids = "db_filtering/seqids.txt",
binary = "db_filtering/seqids.acc",
primers = "primers_explicit.fa"
output:
"primer_blast/primer_blast.tsv"
params:
blast_DB = config["blast_db"],
taxdb = config["taxdb"],
cov = config["primerBlast_coverage"],
identity = config["primerBlast_identity"]
threads: workflow.cores
message:
"Blasting primers"
conda: "./envs/blast.yaml"
shell:
"""
export BLASTDB={params.taxdb}
blastn -db {params.blast_DB} \
-query {input.primers} \
-task blastn-short \
-seqidlist {input.binary} \
-outfmt '6 saccver qseqid staxid sstart send length sstrand mismatch' \
-ungapped -qcov_hsp_perc {params.cov} -perc_identity {params.identity} \
-subject_besthit \
-max_target_seqs 1000000000 \
-num_threads {threads} \
| sort -k1 | sed '1 i\seqid\tquery\ttaxid\tstart\tend\tlength\tstrand\tmismatch' > {output}
"""
rule extract_barcodes_pos:
input:
"primer_blast/primer_blast.tsv"
output:
"primer_blast/barcode_pos.tsv"
message: "Extracting barcodes sequences"
script:
"./scripts/extract_barcodes.py"
rule extract_barcodes_seq:
input:
"primer_blast/barcode_pos.tsv"
output:
"blast_db/{name}.fasta"
message: "Extracting barcode sequences"
params:
blast_DB = config["blast_db"],
taxdb = config["taxdb"]
conda: "./envs/blast.yaml"
shell:
"""
export BLASTDB={params.taxdb}
while IFS=$'\t' read -r acc tax start stop length; do
blastdbcmd -entry $acc \
-db {params.blast_DB} \
-range $start-$stop \
-outfmt %f | sed -e 's/:[[:digit:]-]*//' >> {output}
done < {input}
"""
rule missing_barcodes:
input:
seqids = "db_filtering/seqids.txt",
barcodes = "primer_blast/barcode_pos.tsv",
table = "db_filtering/table.tsv"
output:
acc = temp("primer_blast/no_barcodes.txt"),
full = "primer_blast/missing_barcodes.txt"
message: "Identifying missing barcodes"
params:
blast_DB = config["blast_db"],
taxdb = config["taxdb"]
conda: "./envs/blast.yaml"
shell:
"""
comm -13 <(cat {input.barcodes} | cut -d$'\t' -f1 | sort -k1) \
<(cat {input.seqids} | sort -k1) \
| tr -d "\t" \
> {output.acc}
join --nocheck-order -t $'\t' <(sort -b -k1d {input.table}) <(sort -b -k1d {output.acc}) > {output.full}
"""
rule make_barcode_db:
input:
fasta = "blast_db/{name}.fasta".format(name=generate_db_name()),
id_table = "db_filtering/table.tsv"
output:
taxid_mapper = temp("blast_db/taxmap.tsv"),
DB = expand("blast_db/{name}.{ext}", name = generate_db_name(),
ext= ["nto", "ntf", "nsq", "not", "nos", "nog", "nin", "nhr", "ndb"])
params:
blast_DB = config["blast_db"],
taxdb = config["taxdb"],
dbname = generate_db_name
message: "Formatting barcodes to BLAST database"
conda: "./envs/blast.yaml"
shell:
"""
export BLASTDB={params.taxdb}
cat {input.id_table} | cut -d$'\t' -f1,2 > {output.taxid_mapper}
makeblastdb -in {input.fasta} -dbtype nucl -parse_seqids -blastdb_version 5 -taxid_map {output.taxid_mapper} -out blast_db/{params.dbname}
"""
rule write_report:
input:
db_txd = "db_filtering/taxid_list.txt",
txd_mask = "db_filtering/taxid_mask.txt",
txd_failed = "db_filtering/taxid_missing.txt",
primers = config["primers"],
fasta = "blast_db/{name}.fasta".format(name=generate_db_name()),
missing = "primer_blast/no_barcodes.txt"
output:
"report.txt"
params:
blast_DB = config["blast_db"],
parent = config["parent_node"]
message: "Logging session info"
shell:
"""
date > {output}
echo "Filtering BLAST database {params.blast_DB}" >> {output}
echo " Extracting $(grep -c . {input.db_txd}) taxid" >> {output}
echo " $(grep -c . {input.txd_mask}) are descendent of taxid {params.parent}" >> {output}
echo " $(grep -c . {input.txd_failed}) were missing from taxdump and ignored" >> {output}
echo "Blasting primers {input.primers}" >> {output}
echo " $(grep -c '^>' {input.fasta}) barcode sequences were found" >> {output}
echo " $(grep -c . {input.missing}) sequences did not yield a barcode" >> {output}
"""