-
Notifications
You must be signed in to change notification settings - Fork 0
/
apscale_blast.py
executable file
·423 lines (366 loc) · 14.3 KB
/
apscale_blast.py
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
#!/usr/bin/env python3
"""
A submodule for the abscale wrapper to run BLAST on generated FASTA files and filter BLAST hits.
By Chris Hempel (christopher.hempel@kaust.edu.sa) on 20 Jan 2022
"""
import datetime
import argparse
import warnings
import subprocess
import copy
import os
import gzip
import pandas as pd
# Define that warnings are not printed to console
warnings.filterwarnings("ignore")
# Funtion to print datetime and text
def time_print(text):
print(datetime.datetime.now().strftime("%H:%M:%S"), ": ", text, sep="")
# Function to filter bitscores
def bitscore_cutoff(x, percentage):
min_bitscore = x.max() - x.max() * percentage
return x[x >= min_bitscore]
# Define a custom argument type for a list of strings
def list_of_strings(arg):
return arg.split(",")
# Define a custom argument type for a list of integers
def list_of_integers(arg):
return [int(value) for value in arg.split(",")]
# Function to replace unknown taxonomy in the PR2 database
def replace_pr2_tax(taxon_series):
return taxon_series.apply(
lambda taxon: (
"Unknown in PR2 database"
if taxon.endswith("X") or taxon.endswith("sp.") or taxon.endswith("_sp")
else taxon
)
)
# Function to add suffix to filename despite format
def add_suffix(filename, suffix="_no_cutoff"):
root, extension = os.path.splitext(filename)
return f"{root}{suffix}{extension}"
# Function to get lowest identified taxon and rank per row
def lowest_taxon_and_rank(row):
exceptions = [
"Taxonomy unreliable - multiple matching taxa",
"Taxonomy unreliable - percentage similarity threshold for rank not met",
"Taxonomy unreliable - bitscore and alignment length threshold not met",
"No match in database",
"Unknown in PR2 database",
"Unknown in BOLD database",
"Unknown in SILVA database",
]
lowest_taxon = "Taxonomy unreliable"
lowest_rank = "Taxonomy unreliable"
# Iterate over columns in reverse order
for col in reversed(ranks):
value = row[col]
if pd.notna(value) and value not in exceptions:
lowest_taxon = value
lowest_rank = col
break # Break on the first valid entry found
return pd.Series([lowest_taxon, lowest_rank], index=["lowest_taxon", "lowest_rank"])
# Define function to process tax dfs
def post_processing(df):
# Keep only relevant columns and put species to last column
df_tmp = df[["qseqid"] + ranks]
## Make a df mask: group dfs, check if ranks have more than one taxon, and if yes, True, else False
lca_mask = df_tmp.groupby(["qseqid"]).transform(lambda x: len(set(x)) != 1)
## Replace ranks in df with "Taxonomy unreliable - multiple matching taxa" based on mask
df_tmp = df_tmp.mask(lca_mask, "Taxonomy unreliable - multiple matching taxa")
# Add qseqid and pident info
df_tmp["qseqid"] = df["qseqid"]
df_tmp["pident"] = df["pident"]
## Drop duplicate rows == aggregate taxonomic info
df_processed = df_tmp.drop_duplicates()
# Per ID and identical taxon match, keep the max pident
idx_pident = (
df_processed.groupby(["qseqid"] + ranks)["pident"].transform(max)
== df_processed["pident"]
)
df_processed = df_processed[idx_pident]
df_processed = df_processed.rename(columns={"pident": "percentage_similarity"})
# Change column name, replace _ in species names, and save the df
df_processed = df_processed.rename(columns={"qseqid": "ID"})
df_processed["species"] = df_processed["species"].str.replace("_", " ")
# Add columns for lowest identified rank and taxon
df_ranks = df_processed[ranks]
lowest_columns = df_ranks.apply(lowest_taxon_and_rank, axis=1)
df_processed = pd.concat([df_processed, lowest_columns], axis=1)
return df_processed
# Function to determine the cutoff rank of the processed taxonomy df
def determine_cutoff_rank(id_value):
if id_value == "No match in database":
return "No match in database"
tax = "phylum"
if id_value >= args.blast_cutoff_pidents[5]:
tax = "class"
if id_value >= args.blast_cutoff_pidents[4]:
tax = "order"
if id_value >= args.blast_cutoff_pidents[3]:
tax = "family"
if id_value >= args.blast_cutoff_pidents[2]:
tax = "genus"
if id_value >= args.blast_cutoff_pidents[1]:
tax = "species"
if id_value >= args.blast_cutoff_pidents[0]:
tax = "none"
return tax
# Define arguments
parser = argparse.ArgumentParser(
description="A submodule for the abscale wrapper to run BLAST on generated FASTA files and filter BLAST hits."
)
parser.add_argument(
"--fastafile",
help="Input FASTA file.",
required=True,
metavar="file.fasta",
)
parser.add_argument(
"--blast_database",
help="BLAST database.",
metavar="/PATH/TO/DATABASE",
required=True,
)
parser.add_argument(
"--database_format",
help="Format of the database. Currently available formats are: midori2, pr2, silva, bold. Note: the SILVA and BOLD databases have to have a specific format.",
choices=["midori2", "pr2", "silva", "bold"],
required=True,
)
parser.add_argument(
"--blast_evalue",
help="E-value for BLAST (default: 1e-05).",
metavar="1e[exponent]",
default="1e-05",
)
parser.add_argument(
"--blast_filter_mode",
choices=["soft", "strict"],
help="""Filter mode.
soft:
Keeps the best hit (highest bitscore) for each sequence. If multiple hits have the same highest bitscore, an LCA approach is applied (assigns the taxonomy to each sequence based on all taxonomic ranks that are identical in the remaining hits of each sequence).
strict:
Performs 3 steps:
(1) bitscore filtering - keeps all hits with a bitscore >= --bitscore_treshold, an alignment length >= --length, and within --bitscore_percentage of the best bitscore per sequence.
(2) similarity cutoff - only keeps the taxonomy of hits up to a certain rank, depending on the hits' blast percentage identity and cutoff values given in argument --cutoff_pidents.
(3) LCA approach - assigns the taxonomy to each sequence based on all taxonomic ranks that are identical in the remaining hits of each sequence.
""",
default="strict",
)
parser.add_argument(
"--blast_bitscore_percentage",
metavar="%",
default=2.0,
type=float,
help=(
"""Percentage threshold (in %%) for bitscore filter when choosing
filter_mode option "strict" (default=2.0)."""
),
)
parser.add_argument(
"--blast_alignment_length",
metavar="NNN",
default=100,
type=int,
help=(
"Alignment length threshold to perform bitscore filtering on when "
'choosing filter_mode option "strict" (default=100).'
),
)
parser.add_argument(
"--blast_bitscore_threshold",
metavar="NNN",
default=150,
type=int,
help=(
"""Bitscore threshold to perform bitscore filtering on when choosing
filter_mode option "strict" (default=150)."""
),
)
parser.add_argument(
"--blast_cutoff_pidents",
metavar="N,N,N,N,N,N",
default=[98, 95, 90, 85, 80, 75],
type=list_of_integers,
help=(
"""Similarity cutoff per hit based on BLAST pident values when choosing
filter_mode option "strict". cutoff pident values have to be divided by commas
without spaces, in the order species, genus, family, order,
class, phylum. Domain is always retained. Taxonomy is only kept for a rank if the BLAST hit's
pident is >= the respective cutoff (default=98,95,90,85,80,75)."""
),
)
parser.add_argument(
"--outfile",
required=True,
metavar="OUTFILENAME.csv",
help="Name of output file in .csv format.",
)
parser.add_argument(
"--cores",
metavar="N",
default="2",
help="Number of cores to use (default: 2).",
)
# Set arguments
args = parser.parse_args()
bitscore_percentage = args.blast_bitscore_percentage / 100
ranks = ["domain", "phylum", "class", "order", "family", "genus", "species"]
# Start of pipeline
time_print(f"Running BLAST on {args.fastafile} with database {args.blast_database}...")
# Run BLAST on FASTA file
blastout = os.path.join(
os.path.dirname(args.fastafile), "apscale_wrapper_blast_output.tsv"
)
subprocess.run(
[
"blastn",
"-query",
args.fastafile,
"-db",
args.blast_database,
"-out",
blastout,
"-outfmt",
"6 qseqid sseqid pident length bitscore",
"-evalue",
args.blast_evalue,
"-num_threads",
args.cores,
]
)
# Load in and format BLAST output
time_print("Formatting BLAST output...")
df = pd.read_table(
blastout,
header=None,
names=["qseqid", "sseqid", "pident", "length", "bitscore"],
dtype={"qseqid": str, "bitscore": float, "pident": float, "length": int},
)
# Save space
# with gzip.open(blastout + '.gz', 'wb', compresslevel=9) as f:
# f.write(blastout)
# os.remove(blastout)
with open(blastout, "rb") as file_in:
with gzip.open(blastout + ".gz", "wb", compresslevel=9) as file_out:
file_out.write(file_in.read())
os.remove(blastout)
# Taxonomy formatting
if args.database_format == "silva":
# Split the taxonomy column by semicolon and expand into new columns
df[ranks] = df["sseqid"].str.split(";", expand=True)
# Only keep desired columns and ranks and fill missing values with "NA"
df = df.drop(["sseqid"], axis=1).fillna("NA")
# Replace taxa containing Not_available
df[ranks] = df[ranks].replace("Not_available", "Unknown in SILVA database")
elif args.database_format == "midori2":
# Remove any of "phylum", "class", "order", "family", "genus", and "species" followed by _ as well as _ followed by a number and all the extra information before the domain
df["taxonomy"] = (
df["sseqid"]
.str.replace(r"(phylum|class|order|family|genus|species)_", "", regex=True)
.str.replace(r"_\d+", "", regex=True)
.str.replace(r".*root;", "", regex=True)
)
# Split the taxonomy column by semicolon and expand into new columns
df[ranks] = df["taxonomy"].str.split(";", expand=True)
# Only keep desired columns and ranks and fill missing values with "NA"
df = df.drop(["sseqid", "taxonomy"], axis=1).fillna("NA")
elif args.database_format == "bold":
ranks = ["phylum", "class", "order", "family", "genus", "species"]
# Split the sseqid column by semicolon and expand into new columns
df[ranks] = df["sseqid"].str.split(";", expand=True)
# Replace species names containing " sp. "" or ending with "sp"
mask = (
df["species"].str.endswith("_sp")
| df["species"].str.contains("_sp\._", regex=True)
| df["species"].str.contains("_cf\._", regex=True)
)
df.loc[mask, "species"] = "Unknown in BOLD database"
# Replace taxa containing incertae_sedis
df[ranks] = df[ranks].applymap(
lambda x: "Unknown in BOLD database" if "incertae_sedis" in x else x
)
# Replace 'Unknown_in_BOLD_database' by 'Unknown in BOLD database' in the entire DataFrame
df = df.replace("Unknown_in_BOLD_database", "Unknown in BOLD database")
# Only keep desired columns and ranks and fill missing values with "NA"
df = df.drop(["sseqid"], axis=1).fillna("NA")
# TO BE ADDED AT SOME POINT
# elif args.database_format == "ncbi_nt":
# # Drop rows containing "Unknown" = taxid could not be translated
# df = df[~df[ranks].apply(lambda row: row.str.contains("Unknown")).any(axis=1)]
elif args.database_format == "pr2":
ranks_pr2 = [
"domain",
"supergroup",
"phylum",
"subdivision",
"class",
"order",
"family",
"genus",
"species",
]
# Split the taxonomy column by semicolon and expand into new columns
df[ranks_pr2] = (
df["sseqid"]
.str.rstrip(";")
.str.replace(":nucl", "")
.str.replace(":plas", "")
.str.replace(":apic", "")
.str.replace(":chrom", "")
.str.replace(":mito", "")
.str.split(";", expand=True)
)
# Only keep desired columns and ranks and fill missing values with "NA"
df = df.drop(["sseqid", "supergroup", "subdivision"], axis=1).fillna("NA")
# Replace unknown taxonomy in the PR2 database
df[ranks] = df[ranks].apply(replace_pr2_tax)
if args.blast_filter_mode == "soft":
time_print(
"Grouping IDs and filtering hits based on the highest bitscore of each ID..."
)
idx = df.groupby(["qseqid"])["bitscore"].transform(max) == df["bitscore"]
df = df[idx]
elif args.blast_filter_mode == "strict":
time_print("Filtering hits based on bitscore and length...")
df.loc[
(df["length"] < args.blast_alignment_length)
& (df["bitscore"] < args.blast_bitscore_threshold),
ranks,
] = "Taxonomy unreliable - bitscore and alignment length threshold not met"
time_print(
"Grouping IDs and filtering hits based on bitscore cutoff for each ID..."
)
idx = (
df.groupby(["qseqid"])["bitscore"].transform(
bitscore_cutoff, percentage=bitscore_percentage
)
== df["bitscore"]
)
df = df[idx]
# Copy df to keep version without cutoffs
df_no_cutoffs = copy.deepcopy(df)
time_print("Applying similarity cutoffs and LCA filter...")
# Process the original df
cutoff_term = (
"Taxonomy unreliable - percentage similarity threshold for rank not met"
)
df.loc[df["pident"] < args.blast_cutoff_pidents[0], "species"] = cutoff_term
df.loc[df["pident"] < args.blast_cutoff_pidents[1], "genus"] = cutoff_term
df.loc[df["pident"] < args.blast_cutoff_pidents[2], "family"] = cutoff_term
df.loc[df["pident"] < args.blast_cutoff_pidents[3], "order"] = cutoff_term
df.loc[df["pident"] < args.blast_cutoff_pidents[4], "class"] = cutoff_term
df.loc[df["pident"] < args.blast_cutoff_pidents[5], "phylum"] = cutoff_term
# Process the copied df so that a column for cutoff ranks is used instead of the actual cutoff
df_no_cutoffs = post_processing(df_no_cutoffs)
df_no_cutoffs["cutoff_rank"] = df_no_cutoffs["percentage_similarity"].apply(
determine_cutoff_rank
)
# Save copied df
df_no_cutoffs.to_csv(add_suffix(args.outfile), index=False)
# Process and save df
df = post_processing(df)
df = df.drop("percentage_similarity", axis=1)
df.to_csv(args.outfile, index=False)
time_print("BLAST filtering done.")