diff --git a/source/_static/code/parse_blast.py b/source/_static/code/parse_blast.py index de7f34749950b5e8064556aaea2b216d47f26996..1429924e5a52eef85535fb83d55ea65c2ef523af 100644 --- a/source/_static/code/parse_blast.py +++ b/source/_static/code/parse_blast.py @@ -1,8 +1,8 @@ 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') try: - 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] - table.append(col) + 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') - - +