-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblast2taxonomy.py
executable file
·43 lines (33 loc) · 1.46 KB
/
blast2taxonomy.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
#!/usr/bin/env python3
import argparse
import logging
from pathlib import Path
from Bio.Blast import NCBIXML
def read_nxml(xml_path):
print("query", "hit_id", "identity", "align_length", "expect", sep="\t")
with xml_path.open("r") as f:
handle = NCBIXML.parse(f)
for record in handle:
if len(record.alignments) == 0:
print(record.query, "NA", "NA", "NA", "NA", sep="\t")
for alignment in record.alignments:
id_acc = alignment.title
e_value = alignment.hsps[0].expect
aln_length = alignment.hsps[0].align_length
identity = alignment.hsps[0].identities
print(record.query, id_acc, identity, aln_length, e_value, sep="\t")
print("#query: ", alignment.hsps[0].query)
print("# ", alignment.hsps[0].match)
dot_format = ""
for i, m in enumerate(alignment.hsps[0].match):
dot_format += "." if m == "|" else alignment.hsps[0].sbjct[i]
print("#target:", dot_format)
break
print("#")
if __name__ == "__main__":
logging.basicConfig(format="[%(asctime)s] %(message)s", level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument("nxml_file", metavar="NXML", help="NCBI-XML BLAST results file.")
args = parser.parse_args()
nxml_file = Path(args.nxml_file)
read_nxml(nxml_file)