-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpqrpy.py
79 lines (60 loc) · 2.16 KB
/
pqrpy.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
from urllib2 import Request, urlopen, URLError
class PQRPy(object):
def __init__(self, key):
self.key = key
self.json_str = None
self.mol2_str = None
def json(self):
"""Make a JSON API call to PQR
Returns a JSON string containing the data for that molecule
"""
request = Request("http://pqr.pitt.edu/api/json/" + self.key)
try:
response = urlopen(request)
read_json = response.read()
self.json_str = read_json
return read_json
except:
raise Exception("That key is invalid")
def json_dict(self):
import json
if not self.json_str:
self.json_str = self.json()
return json.loads(self.json_str)
def mol2(self, key):
"""Make a MOL2 API call to PQR
Keyword arguments:
key -- the InChIKey for the molecule
Returns a string containing the MOL2 data for that molecule
"""
request = Request("http://pqr.pitt.edu/api/mol/" + key)
try:
response = urlopen(request)
read_mol2 = response.read()
self.mol2_str = read_mol2
return read_mol2
except:
raise Exception("That key is invalid")
def weekly(self):
"""Make a API call to PQR to get the molecules of the week
Returns a list of tuples, each tuple being (InChIKey, Name)
"""
request = Request("http://pqr.pitt.edu/api/weekly/")
try:
response = urlopen(request)
mol_list = response.read().split("\n")
weekly = [tuple(x.split(",")) for x in mol_list]
return weekly
except URLError:
raise Exception("Something went wrong")
def inchikeys(self):
"""Make a API call to PQR to get all the InChIKeys PQR indexes
Returns a list containing InChIKeys
"""
request = Request("http://pqr.pitt.edu/api/inchikeys")
try:
response = urlopen(request)
read_inchi = response.read().split("\n")
return read_inchi
except:
raise Exception("Something went wrong")