Skip to content
Snippets Groups Projects
Verified Commit a68ba39d authored by Bertrand  NÉRON's avatar Bertrand NÉRON
Browse files

use unwinding operator to parse blast output

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