-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblast_parser.py
executable file
·42 lines (38 loc) · 1.22 KB
/
blast_parser.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
#! /usr/bin/env python
#Parser for blast -m 8 output file with description inserted
import sys
##Query id,Subject id,Description, % identity, alignment length,mismatches,gap openings,q. start,q. end,s. start,s. end,e-value,bit score
dict={}
for line in open(sys.argv[1]):
data = line.split('\t')
# print(data)
query = data[0]
hit = data[1]
hit_desc = data[2]
identity = float(data[3])
length = int(data[4])
q_start = int(data[7])
q_end = int(data[8])
e_value = float(data[-3])
bit_score = float(data[-2])
taxa = data[-1]
if e_value <= 1e-5:
# if "hydrogenase" in hit_desc and e_value <= 1e-5:
if query in dict:
dict[query].append([hit, hit_desc, identity, length, q_start, q_end, e_value, taxa])
else:
dict[query] = [[hit, hit_desc, identity, length, q_start, q_end, e_value, taxa]]
#print dict
dict2={}
for key in list(dict.keys()):
if len(dict[key]) > 1:
if dict[key][0][-1] <= dict[key][1][-1]:
dict2[key] = dict[key][0]
else:
dict2[key] = dict[key][1]
else:
continue
#print dict2
output = open(sys.argv[2], 'w')
for key in list(dict2.keys()):
output.write('%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s' % (key, dict2[key][0], dict2[key][1], dict2[key][2], dict2[key][3], dict2[key][-3], dict2[key][-2], dict2[key][-1]))