-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniproBlastToPdb.py
executable file
·179 lines (147 loc) · 4.85 KB
/
UniproBlastToPdb.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# coding: utf-8
"""
UniproBlastToPDB
=================
Description
------------
Given a Uniprot code will run blastp to retreive fasta sequence and blast xml output
and parse the output in the following way:
Uniprot | PDBid | chain | query_to | query_from | Iter Query Len | e-value | Query Coverage | Sequence Identity
Usage
-----
```
UniproBlastToPdb.py UNIPROid
```
Output
-------
- UniproID.fasta
- Unipro_blast.xml
- Unipro.csv
"""
import sys
import random
import Bio
from Bio.Blast import NCBIXML
from Bio.Blast import NCBIWWW
from Bio import SeqIO
from Bio import Entrez
from Bio.Blast.Applications import NcbiblastpCommandline
from Bio.Seq import Seq
import pandas as pd
emails = ['avishek.kumar@asu.edu','akumar67@asu.edu','avishek.kumar@outlook.com','avishekkumar87@gmail.com','brandon.mac.butler@gmail.com']
def UniBLAST(code,Verbose=False):
"""
Input
------
Uniprot Code
UniBLAST(code)
e.g. UniBLAST('O00238')
Description
-----------
Outputs a Fasta sequence and
Runs blasp looking through pdb database with the Uniprot code
Output
------
- UniproID.fasta FASTA Sequence
- UniproID_blast.xml Blast output in XML Format
"""
Entrez.email = random.choice(emails)
if(Verbose):
print "Using email: %s"%(Entrez.email)
with open(code + ".fasta", "w") as out_file:
net_handle = Entrez.efetch(db="nucleotide", id=code, rettype="fasta")
out_file.write(net_handle.read())
if(Verbose):
print "Running blastp"
result_handle = NCBIWWW.qblast("blastp", "pdb", code)
if(Verbose):
print "Done running blastp"
with open(code + "_blast.xml", "w") as save_file:
save_file.write(result_handle.read())
result_handle.close()
def getfasta(code,Verbose=True):
"""
Input
-----
Uniprot code
Description
------------
getfasta(code,Verbose=True)
Query Webserver for FASTA sequence
Output
------
code.fasta FASTA sequence
"""
from Bio import Entrez
Entrez.email = random.choice(emails)
if(Verbose):
print "Using email: %s"%(Entrez.email)
with open(code + ".fasta", "w") as out_file:
net_handle = Entrez.efetch(db="nucleotide", id=code, rettype="fasta")
out_file.write(net_handle.read())
def parseBlastFile(xmlfil):
"""
Input
-----
Uniprot XML Output
parseBlastFile(xmlfil)
e.g. parsebBlastFile('O00238_blast.xml')
Description
-----------
Parses the following information out of the bast output xml file.
Uniprot | PDBid | chain | query_to | query_from | Iter Query Len | e-value | Query Coverage | Sequence Identity
Output
-------
Uniprot.csv
"""
NP_id = xmlfil.split('_')[0]
result_handle = open(xmlfil)
blast_record = NCBIXML.read(result_handle)
result_handle.close()
#E_VALUE_THRESH = 1E-25
outfilname = NP_id+'.csv'
with open(outfilname,"w") as out_file:
print "Writing output to %s"%(outfilname)
out_file.write('Uniprot,PDBid,chain,query_to,query_from,IterQueryLen,e-value,QueryCov,SeqId\n')
sequencequeryLength = blast_record.query_length
for alignment in blast_record.alignments:
for hsp in alignment.hsps:
first = float(hsp.identities)
second = len(hsp.query)
identity = 100*float(first/second)
identity = int(round(identity,0))
coverage = round(100*float(hsp.query_end - hsp.query_start)/sequencequeryLength,0)
line1=alignment.title
b=line1.split('|')
pdbid = str(b[3])
out_file.write(NP_id+",")
out_file.write(pdbid+",")
line2=b[4]
chain=line2.split()
out_file.write(str(chain[0]) +",")
out_file.write(str(hsp.query_end)+",")
out_file.write(str(hsp.query_start)+",")
out_file.write(str(sequencequeryLength)+",")
out_file.write(str(hsp.expect) +",")
out_file.write("%f"%coverage +",")
out_file.write(str(identity) +"\n")
def tohtml(csvfile):
"""
Reads the csvfile fromt the blast output
Finds all hits that are greater than 80 Sequence Coverage
and greater than 80 Query Identity
Outputs the results as an html table
"""
data = pd.read_csv(csvfile)
mask = ( (data['QueryCov'] > 80) & (data['SeqId'] > 80) )
data[mask].to_html(sys.stdout)
if __name__ == "__main__" and len(sys.argv) > 2:
code = sys.argv[1]
blastfile = code+'_blast.xml'
csvfile = code+'.csv'
UniBLAST(code)
parseBlastFile(blastfile)
tohtml(csvfile)
else:
print __doc__