-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblast_filter_output.py
64 lines (52 loc) · 1.89 KB
/
blast_filter_output.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
# Author: Jeffrey Grover
# Purpose: Filter the output from NCBI Blast+ format #7 or 6
# Created: 2019-07-13
from argparse import ArgumentParser
# Filter and output line by line
def filter_blast7(input_blast_results, min_perc_id, min_al_len, max_evalue):
with open(input_blast_results, 'r') as input_handle:
header = ('# query', 'subject', 'perc_identity', 'alignment_length',
'mismatches', 'gap_opens', 'q_start', 'q_end', 's_start',
's_end', 'evalue', 'bit_score')
print('\t'.join(header))
for line in input_handle:
if not line.startswith('#'):
entry = line.strip().split()
perc_id = float(entry[2])
al_len = int(entry[3])
evalue = float(entry[10])
if (
perc_id >= min_perc_id
and al_len >= min_al_len
and evalue <= max_evalue
):
print('\t'.join(entry))
def get_args():
parser = ArgumentParser(
description='Filter the output from NCBI BLAST+ based on criteria.'
' Currently works with format type 6 and 7 only.')
parser.add_argument(
'blast_output',
help='Output from NCBI BLAST+ in format type 7',
metavar='FILE')
parser.add_argument(
'-i', '--min_id',
help='Minimum percent identity',
type=float,
metavar='ID')
parser.add_argument(
'-l', '--min_len',
help='Minimum alignment length',
type=int,
metavar='LENGTH')
parser.add_argument(
'-e', '--max_evalue',
help='Maximum e-evalue',
type=float,
metavar='EVALUE')
return parser.parse_args()
def main(args):
filter_blast7(args.blast_output, args.min_id, args.min_len, args.max_evalue)
if __name__ == '__main__':
main(get_args())