Spaces:
Sleeping
Sleeping
Update genesis/api_clients/chembl_api.py
Browse files
genesis/api_clients/chembl_api.py
CHANGED
@@ -1,91 +1,98 @@
|
|
1 |
# genesis/api_clients/chembl_api.py
|
2 |
-
import os
|
3 |
import requests
|
|
|
4 |
|
5 |
CHEMBL_BASE_URL = "https://www.ebi.ac.uk/chembl/api/data"
|
6 |
|
7 |
-
def
|
8 |
-
"""
|
9 |
-
Generic ChEMBL GET request helper.
|
10 |
-
"""
|
11 |
-
url = f"{CHEMBL_BASE_URL}/{endpoint}"
|
12 |
-
res = requests.get(url, params=params)
|
13 |
-
res.raise_for_status()
|
14 |
-
return res.json()
|
15 |
-
|
16 |
-
|
17 |
-
# -----------------------
|
18 |
-
# Drug & Compound Search
|
19 |
-
# -----------------------
|
20 |
-
def search_drug_by_name(drug_name: str, limit: int = 5):
|
21 |
"""
|
22 |
-
Search for
|
23 |
"""
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
|
28 |
def get_drug_details(chembl_id: str):
|
29 |
"""
|
30 |
-
Retrieve detailed information
|
31 |
"""
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
|
35 |
-
|
36 |
-
# Target & Mechanism
|
37 |
-
# -----------------------
|
38 |
-
def search_target_by_name(target_name: str, limit: int = 5):
|
39 |
"""
|
40 |
-
|
41 |
"""
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
def get_target_details(chembl_target_id: str):
|
47 |
-
"""
|
48 |
-
Retrieve target details from ChEMBL.
|
49 |
-
"""
|
50 |
-
return _chembl_get(f"target/{chembl_target_id}")
|
51 |
|
52 |
|
53 |
def get_mechanism_of_action(chembl_id: str):
|
54 |
"""
|
55 |
-
Retrieve
|
56 |
-
"""
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
"""
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
return
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# genesis/api_clients/chembl_api.py
|
|
|
2 |
import requests
|
3 |
+
import os
|
4 |
|
5 |
CHEMBL_BASE_URL = "https://www.ebi.ac.uk/chembl/api/data"
|
6 |
|
7 |
+
def search_drug(drug_name: str, limit: int = 5):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
"""
|
9 |
+
Search ChEMBL for drugs by name.
|
10 |
"""
|
11 |
+
url = f"{CHEMBL_BASE_URL}/molecule/search.json?q={drug_name}&limit={limit}"
|
12 |
+
response = requests.get(url)
|
13 |
+
response.raise_for_status()
|
14 |
+
data = response.json()
|
15 |
+
return [
|
16 |
+
{
|
17 |
+
"molecule_chembl_id": mol.get("molecule_chembl_id"),
|
18 |
+
"pref_name": mol.get("pref_name"),
|
19 |
+
"max_phase": mol.get("max_phase"),
|
20 |
+
"therapeutic_flag": mol.get("therapeutic_flag"),
|
21 |
+
"molecule_type": mol.get("molecule_type"),
|
22 |
+
}
|
23 |
+
for mol in data.get("molecules", [])
|
24 |
+
]
|
25 |
|
26 |
|
27 |
def get_drug_details(chembl_id: str):
|
28 |
"""
|
29 |
+
Retrieve detailed drug information by ChEMBL ID.
|
30 |
"""
|
31 |
+
url = f"{CHEMBL_BASE_URL}/molecule/{chembl_id}.json"
|
32 |
+
response = requests.get(url)
|
33 |
+
response.raise_for_status()
|
34 |
+
return response.json()
|
35 |
|
36 |
|
37 |
+
def get_target_info(chembl_id: str):
|
|
|
|
|
|
|
38 |
"""
|
39 |
+
Get targets for a given drug (by ChEMBL ID).
|
40 |
"""
|
41 |
+
url = f"{CHEMBL_BASE_URL}/target.json?molecule_chembl_id={chembl_id}"
|
42 |
+
response = requests.get(url)
|
43 |
+
response.raise_for_status()
|
44 |
+
return response.json().get("targets", [])
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
|
47 |
def get_mechanism_of_action(chembl_id: str):
|
48 |
"""
|
49 |
+
Retrieve mechanism of action for a drug.
|
50 |
+
"""
|
51 |
+
url = f"{CHEMBL_BASE_URL}/mechanism.json?molecule_chembl_id={chembl_id}"
|
52 |
+
response = requests.get(url)
|
53 |
+
response.raise_for_status()
|
54 |
+
return [
|
55 |
+
{
|
56 |
+
"mechanism_of_action": m.get("mechanism_of_action"),
|
57 |
+
"target_name": m.get("target_name"),
|
58 |
+
"target_type": m.get("target_type"),
|
59 |
+
}
|
60 |
+
for m in response.json().get("mechanisms", [])
|
61 |
+
]
|
62 |
+
|
63 |
+
|
64 |
+
def get_bioactivity_data(chembl_id: str, limit: int = 10):
|
65 |
+
"""
|
66 |
+
Retrieve bioactivity data for a given drug.
|
67 |
+
"""
|
68 |
+
url = f"{CHEMBL_BASE_URL}/activity.json?molecule_chembl_id={chembl_id}&limit={limit}"
|
69 |
+
response = requests.get(url)
|
70 |
+
response.raise_for_status()
|
71 |
+
return [
|
72 |
+
{
|
73 |
+
"assay_description": a.get("assay_description"),
|
74 |
+
"standard_type": a.get("standard_type"),
|
75 |
+
"standard_value": a.get("standard_value"),
|
76 |
+
"units": a.get("units"),
|
77 |
+
"activity_comment": a.get("activity_comment"),
|
78 |
+
}
|
79 |
+
for a in response.json().get("activities", [])
|
80 |
+
]
|
81 |
+
|
82 |
+
|
83 |
+
def search_by_target(target_name: str, limit: int = 5):
|
84 |
+
"""
|
85 |
+
Search drugs by target name.
|
86 |
+
"""
|
87 |
+
url = f"{CHEMBL_BASE_URL}/target/search.json?q={target_name}&limit={limit}"
|
88 |
+
response = requests.get(url)
|
89 |
+
response.raise_for_status()
|
90 |
+
return [
|
91 |
+
{
|
92 |
+
"target_chembl_id": t.get("target_chembl_id"),
|
93 |
+
"pref_name": t.get("pref_name"),
|
94 |
+
"target_type": t.get("target_type"),
|
95 |
+
"organism": t.get("organism"),
|
96 |
+
}
|
97 |
+
for t in response.json().get("targets", [])
|
98 |
+
]
|