This repository was archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
349 lines (305 loc) · 12.6 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
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
import pandas as pd
shell.executable("bash")
# Settings ------------------------------------------------------------------------------------------------------------------
workdir: config["workdir"]
# Input rule ----------------------------------------------------------------------------------------------------------------
rule all:
input:
"pairwise_alignment/table.tsv",
"pairwise_alignment/alignments.txt",
"reports/distances.tsv",
"reports/sequence_lengths.txt",
"reports/dereplication.tsv",
"reports/cluster_size.tsv",
"reports/report.html"
# Workflow ------------------------------------------------------------------------------------------------------------------
# Add derep report to html
rule export_dbinfo:
output:
temp("db_info.txt")
params:
blast_DB = config["blast_db"],
taxdb = config["taxdb"]
message: "Exporting database information"
conda:
"envs/blast.yaml"
shell:
"""
export BLASTDB={params.taxdb}
blastdbcmd -db {params.blast_DB} -entry all -outfmt '%a\t%T\t%S' > {output}
"""
rule export_sequences:
input:
"db_info.txt"
output:
temp(dynamic("fastadump/{taxid}.fa"))
params:
blast_DB = config["blast_db"],
taxdb = config["taxdb"]
message: "Exporting sequences"
conda:
"envs/blast.yaml"
shell:
"""
export BLASTDB={params.taxdb}
for tax in $(cut -d$'\t' -f2 {input} | sort -u); do
# retrieving sequences per taxa
blastdbcmd -db {params.blast_DB} -taxids $tax -outfmt '%f' \
> fastadump/$tax.fa
done
"""
rule dereplicate:
input:
dynamic("fastadump/{taxid}.fa")
output:
report = temp("derepdump/dereplication.tsv"),
fasta = temp("fasta/sequences_derep.fa"),
tmpfa = temp("derepdump/derep.fa"),
tmptab = temp("derepdump/derep.txt")
message: "Dereplicating sequences"
conda:
"envs/vsearch.yaml"
shell:
"""
for file in {input}; do
vsearch --cluster_fast $file \
--id 1 \
--iddef 1 \
--centroids {output.tmpfa} \
--uc {output.tmptab} \
--quiet
cat {output.tmpfa} >> {output.fasta}
grep -E '^[S|H]' {output.tmptab} \
| cut -d$'\t' -f1,9,10 \
>> {output.report}
done
"""
rule derep_stats:
input:
derep = "derepdump/dereplication.tsv",
table = "db_info.txt"
output:
"reports/dereplication.tsv"
message: "Collecting dereplication stats"
shell:
"""
join --nocheck-order -1 2 -2 1 -t $'\t' \
<(sort -k2 {input.derep}) \
<(sort -k1 {input.table}) \
| sed -e 's/\tH\t/\thit\t/' -e 's/\tS\t/\tcentroid\t/' \
| sort -k4n \
> {output}
sed -i '1 i\seqid\ttype\tcentroid\ttaxid\tname' {output}
"""
rule cluster_size:
input:
"reports/dereplication.tsv"
output:
"reports/cluster_size.tsv"
message: "Collecting cluster sizes"
run:
df = pd.read_csv(input[0], sep= '\t')
taxcount = df.groupby('taxid').agg('count')['seqid'].rename('tax_size')
centroids = df[df['type'] == 'centroid']
centroidSize = df.groupby('centroid').nunique()['seqid']+1
centroidSize = centroidSize[centroidSize.index != '*'].rename("cluster_size")
dfout = centroids.set_index('seqid').join(centroidSize).fillna(1)
dfout = dfout.join(taxcount, on = 'taxid').drop(["type", "centroid"], axis = 1)
dfout = dfout.astype({'cluster_size' : 'int32', 'tax_size': 'int32'})
dfout["size"] = dfout["cluster_size"].map(str) + "/" + dfout["tax_size"].map(str)
dfout["rel_cluster_size"] = round(dfout["cluster_size"] / dfout["tax_size"] *100, 2)
dfout.to_csv(output[0], sep = '\t')
rule filter_seq:
input:
"fasta/sequences_derep.fa"
output:
temp("fasta/sequences.fa")
message: "Filtering ambiguous sequences"
params:
max_n = config["max_n"]
conda:
"envs/cutadapt.yaml"
shell:
"""
cutadapt --max-n {params.max_n} {input} > {output}
"""
rule trim_primers:
input:
"fasta/sequences.fa"
output:
fasta = temp("fasta/sequences_trim.fa"),
report = "reports/trimming_report.txt",
primers_rc = temp("primers_rc.fa")
params:
primers = config["primers"]
message: "Trimming primers"
conda:
"envs/cutadapt.yaml"
shell:
"""
seqtk seq -r {params.primers} > {output.primers_rc}
cutadapt -g file:{params.primers} \
{input} 2> {output.report} \
| cutadapt -a file:{output.primers_rc} - \
> {output.fasta} 2>> {output.report}
"""
rule pariwise_alignement:
input:
"fasta/sequences_trim.fa" if config["trim_primers"] == True else "fasta/sequences.fa"
output:
table = "pairwise_alignment/table.tsv",
align = "pairwise_alignment/alignments.txt"
params:
id = config["min_identity"]
threads: workflow.cores
message: "Performing pairwise global alignment"
conda:
"envs/vsearch.yaml"
shell:
"""
vsearch --allpairs_global {input} \
--iddef 1 \
--gapext 2I/2E \
--gapopen 20I/20E \
--threads {threads} \
--id {params.id} \
--userout {output.table} \
--userfields query+target+id+mism+gaps+alnlen+qlo+qhi+ql+tlo+thi+tl \
--alnout {output.align}
sed -i '1 i\query\ttarget\tid\tmismatch\tgaps\taln_length\tqstart\tqend\tqlength\ttstart\ttend\ttlength' {output.align}
"""
rule collect_descriptors:
input:
aln = "pairwise_alignment/table.tsv",
info = "db_info.txt",
sizes = "reports/cluster_size.tsv"
output:
"reports/distances.tsv",
message: "Collecting sequence informations"
run:
dfaln = pd.read_csv(input.aln, sep='\t', names= ["query", "target", "id", "mismatch", "gaps","aln_length", "qstart", "qend", "qlength", "tstart", "tend", "tlength"])
dfinfo = pd.read_csv(input.info, sep = '\t', names = ["seqid", "taxid", "name"])
dfsize = pd.read_csv(input.sizes, sep = '\t')
dfout = pd.DataFrame({"query": dfaln["query"],
"target": dfaln["target"],
"distance": dfaln["mismatch"] + dfaln["gaps"]})
dfout = dfout.join(dfinfo.set_index("seqid"),
on = "query", how = "inner").rename(columns={'taxid' : 'query_taxid',
'name' : 'query_name'})
dfout = dfout.join(dfinfo.set_index("seqid"),
on = "target", how = "inner").rename(columns={'taxid' : 'target_taxid',
'name' : 'target_name'})
dfout = dfout.join(dfsize.set_index("seqid")[['size', 'rel_cluster_size']],
on = "query", how = "inner").rename(columns={'size' : 'query_size',
'rel_cluster_size' : 'query_relsize'})
dfout = dfout.join(dfsize.set_index("seqid")[['size', 'rel_cluster_size']],
on = "target", how = "inner").rename(columns={'size' : 'target_size',
'rel_cluster_size' : 'target_relsize'})
dfout = dfout[['query', 'query_taxid', 'query_name', 'query_size', 'query_relsize',
'target', 'target_taxid', 'target_name', 'target_size', 'target_relsize',
'distance']]
dfout.to_csv(output[0], sep='\t', index=False)
rule get_seq_sizes:
input:
raw = "fasta/sequences.fa",
trimmed = "fasta/sequences_trim.fa" if config["trim_primers"] == True else "fasta/sequences.fa"
output:
raw = temp("reports/fasta_length_raw.tsv"),
length_trim = temp("reports/fasta_length_trim.txt"),
params:
trim = config["trim_primers"]
message: "Getting sequence length distribution"
shell:
"""
# Get seqid length table
cat {input.raw} \
| awk '$0 ~ ">" {{if (NR > 1) {{print c;}} c=0;printf substr($0,2,100) "\t"; }} $0 !~ ">" {{c+=length($0);}} END {{ print c; }}' \
> {output.raw}
if [ {params.trim} = "True" ]; then
# get trimmed lengths
cat {input.trimmed} \
| awk '$0 ~ ">" {{if (NR > 1) {{print c;}} c=0;printf substr($0,2,100) "\t"; }} $0 !~ ">" {{c+=length($0);}} END {{ print c; }}' \
> {output.length_trim}
else
# if not trimming, just merge infos
touch {output.length_trim} # otherwise snakemake complains about missing output
fi
"""
rule seq_size_table:
input:
raw = "reports/fasta_length_raw.tsv",
trim = "reports/fasta_length_trim.txt",
info = "db_info.txt"
output:
"reports/sequence_lengths.txt"
params:
trim = config["trim_primers"]
message: "Formatting sequence length table"
run:
dfinfo = pd.read_csv(input.info, sep = '\t', names = ["seqid", "taxid", "name"])
dfraw = pd.read_csv(input.raw, sep = '\t', names = ["seqid", "length"])
dfoutraw = dfraw.join(dfinfo.set_index("seqid"),
on = "seqid", how = "inner")
if params.trim:
dfout = dfoutraw.rename(columns={'length': 'db_length'})
dftrim = pd.read_csv(input.trim, sep = '\t', names = ["seqid", "length"])
dfout = dfout.join(dftrim.set_index('seqid'),
on = 'seqid', how = 'inner').rename(columns = {'length' : 'trim_length'})
dfout = dfout[['seqid', 'taxid', 'name', 'db_length', 'trim_length']]
dfout.to_csv(output[0], sep = '\t', index = False)
else:
dfoutraw = dfoutraw[['seqid', 'taxid', 'name', 'length']]
dfoutraw.to_csv(output[0], sep = '\t', index = False)
rule db_stats:
input:
unfiltered = "fasta/sequences_derep.fa",
seq = "fasta/sequences.fa",
table = "db_info.txt",
output:
taxids = temp("reports/taxids_number.txt"),
nseq = temp("reports/seq_number.txt"),
derep = temp("reports/derep_number.txt"),
highN = temp("reports/high_N.txt")
shell:
"""
cut -d$'\t' -f2 {input.table} | sort -u | wc -l | cut -d$' ' -f1 > {output.taxids}
wc -l {input.table} | cut -d$' ' -f1 > {output.nseq}
grep -c "^>" {input.seq} > {output.derep}
echo $(( $(grep -c "^>" {input.unfiltered}) - $(grep -c "^>" {input.seq}) )) > {output.highN}
"""
rule get_consensus_level:
input:
distance_table = "reports/distances.tsv",
output:
cons = "reports/consensus.tsv"
message:
"Determining consensus ranks"
params:
lineage = config["rankedlineage_dmp"],
nodes = config["nodes_dmp"],
script:
"scripts/consensus_levels.py"
rule write_report:
input:
seq = "reports/seq_number.txt",
taxids = "reports/taxids_number.txt",
dist = "reports/distances.tsv",
sizedist = "reports/sequence_lengths.txt",
derep = "reports/dereplication.tsv",
nderep = "reports/derep_number.txt",
nNfilt = "reports/high_N.txt",
clusterSize = "reports/cluster_size.tsv",
consensus = "reports/consensus.tsv",
output:
"reports/report.html"
params:
database = config["blast_db"],
workdir = config["workdir"],
trimming = config["trim_primers"],
id = config["min_identity"],
primers = config["primers"] if config["trim_primers"] == True else config["trim_primers"]
message: "Writting report"
conda:
"envs/rmarkdown.yaml"
script:
"scripts/write_report.Rmd"