forked from emreg00/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_openphacts.py
72 lines (60 loc) · 2.01 KB
/
parse_openphacts.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
import urllib2, json, urllib
import configuration
import ssl
CONFIG = configuration.Configuration()
try:
API_USER_KEY = CONFIG.get("OPENPHACTS_API_KEY") # change this value with custom API key
API_APP_ID = CONFIG.get("OPENPHACTS_APP_ID")
except:
print "Warning: OPENPHACTS API key not found!"
API_USER_KEY = None
API_APP_ID = None
FIELD_DRUG = "http://aers.data2semantics.org/resource/drug/"
FIELD_TARGET = "https://beta.openphacts.org/2.1/compound/pharmacology/pages?uri=" # proteinBinding
FIELD_COMPOUND = "https://beta.openphacts.org/2.1/mapUri?Uri=" # hasTarget
def main():
drug = "paracetamol"
print get_drug_targets(drug)
return
def get_data(command, parameter):
if command == "target":
txt = '%s%s' % (urllib.quote_plus(FIELD_DRUG), parameter)
else:
raise ValueError("Unknown command: " + command)
if API_USER_KEY is None:
raise ValueError("API key missing!")
else:
url = FIELD_TARGET + '%s&app_id=%s&app_key=%s' % (txt, API_APP_ID, API_USER_KEY)
#print url
req = urllib2.Request(url)
gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
try:
#response = urllib2.urlopen(req) # without context - also works
response = urllib2.urlopen(req, context=gcontext)
except urllib2.HTTPError:
print "Problem with response (probably no info):", url
return None
while True:
try:
response = json.load(response)
break
except:
print "Problem with response:", parameter
response = urllib2.urlopen(req) # without context
return response["result"]
def get_drug_targets(drug):
try:
response = get_data("target", drug)
except urllib2.HTTPError:
print "No info for", drug
return []
values = []
values_all = []
for row in response["items"]:
term = row["hasAssay"]["hasTarget"]
#print term
if "targetOrganismName" in term and term["targetOrganismName"] == "Homo sapiens":
values.append(term["title"])
return values, values_all
if __name__ == "__main__":
main()