Verified Commit a68ba39d authored by Bertrand  NÉRON's avatar Bertrand NÉRON
Browse files

use unwinding operator to parse blast output

parent 0b208e19
from operator import itemgetter
from collections import namedtuple
Hit = namedtuple("Hit" ,"id percent identity align_len mis_num, open_gap_num\
query_start, query_end, subject_start, subject_end, E_value, HSP_bit_score")
Hit = namedtuple("Hit", ("query", "subject", "identity", "align_len", "mis_num", "open_gap_num",
"query_start", "query_end", "subject_start", "subject_end", "E_value", "HSP_bit_score"))
def parse_blast_output(input_file):
......@@ -14,13 +14,13 @@ def parse_blast_output(input_file):
with open(input_file, 'r') as infile:
table = []
for line in infile:
col = line.split('\t')
query, subject, identity, *stuff, bit_score = line.split('\t')
col[2] = float(col[2])
identity = float(identity)
except ValueError as err:
raise RuntimeError("error in parsing {} : {}".format(input_file, err))
col[-1] = col[-1][:-1]
bit_score = bit_score.strip()
table.append([query, subject, identity, *stuff, bit_score])
return table
......@@ -42,10 +42,8 @@ def write_blast_output(hits, output_file):
if __name__ == '__main__':
table_hits = parse_blast_output('blast2.txt')
#table_hits = parse_blast_output('blast.txt')
table_sorted = sorted(table_hits, key = itemgetter(2), reverse = True)
table_sorted = sorted(table_hits, key=itemgetter(2), reverse=True)
# alternative
# table_sorted = sorted(table, key = lambda x : x[2], reversed = True)
write_blast_output(table_hits, 'blast_sorted.txt')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment