-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_blast_result_list.py
51 lines (46 loc) · 1.3 KB
/
parse_blast_result_list.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
# Biopython: Parse Blast result
# HB 2017/05
from Bio.Blast import NCBIXML
import sys
import os
# Function to parse Blast result
def parseBlastXML(blastXml):
# Check if this threshold is ok
E_VALUE_THRESH = 0.04
name, extension = os.path.splitext(blastXml)
out = name + ".txt"
# Write file for output
f = open(out, 'w')
# Choose stdout to file
orig_stdout = sys.stdout
sys.stdout = f
# Open result
result_handle = open(blastXml)
# Parse Blast Result
blast_records = NCBIXML.parse(result_handle)
for blast_record in blast_records:
for alignment in blast_record.alignments:
for hsp in alignment.hsps:
if hsp.expect < E_VALUE_THRESH:
print('****Alignment****')
print('sequence:', alignment.title)
print('length:', alignment.length)
print('e value:', hsp.expect)
#print(hsp.query[0:75] + '...')
#print(hsp.match[0:75] + '...')
#print(hsp.sbjct[0:75] + '...')
print("")
# Switch back to orig stdout
sys.stdout = orig_stdout
f.close()
# Now start to process the list
with open(sys.argv[1]) as f:
# Loop through each line
for line in f:
# Remove white space at the end
line = line.strip()
# If the string is not empty
if line:
print "Analyzing Blast of UniprotID ", line
# Parse the blast result
parseBlastXML( line + ".xml" )