Spaces:
Sleeping
Sleeping
Update genesis/api_clients/chembl_api.py
Browse files
genesis/api_clients/chembl_api.py
CHANGED
@@ -1,65 +1,91 @@
|
|
1 |
# genesis/api_clients/chembl_api.py
|
|
|
2 |
import requests
|
3 |
|
4 |
-
|
5 |
|
6 |
-
def
|
7 |
"""
|
8 |
-
|
9 |
"""
|
10 |
-
url = f"{
|
11 |
-
params = {
|
12 |
-
"molecule_structures__standard_inchi_key__flexmatch": query,
|
13 |
-
"limit": limit,
|
14 |
-
"format": "json"
|
15 |
-
}
|
16 |
res = requests.get(url, params=params)
|
17 |
res.raise_for_status()
|
18 |
-
return res.json()
|
|
|
19 |
|
20 |
-
|
|
|
|
|
|
|
21 |
"""
|
22 |
-
|
23 |
"""
|
24 |
-
|
25 |
-
|
26 |
-
res.raise_for_status()
|
27 |
-
return res.json()
|
28 |
|
29 |
-
|
|
|
30 |
"""
|
31 |
-
|
32 |
-
Includes IC50, EC50, Ki, etc.
|
33 |
"""
|
34 |
-
|
35 |
-
|
36 |
-
"molecule_chembl_id": chembl_id,
|
37 |
-
"limit": limit,
|
38 |
-
"format": "json"
|
39 |
-
}
|
40 |
-
res = requests.get(url, params=params)
|
41 |
-
res.raise_for_status()
|
42 |
-
return res.json().get("activities", [])
|
43 |
|
44 |
-
|
|
|
|
|
|
|
45 |
"""
|
46 |
-
Search for a biological target (protein
|
47 |
"""
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
"limit": limit,
|
52 |
-
"format": "json"
|
53 |
-
}
|
54 |
-
res = requests.get(url, params=params)
|
55 |
-
res.raise_for_status()
|
56 |
-
return res.json().get("targets", [])
|
57 |
|
58 |
def get_target_details(chembl_target_id: str):
|
59 |
"""
|
60 |
-
|
61 |
"""
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 _chembl_get(endpoint: str, params: dict = None):
|
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 a drug/compound by name in ChEMBL.
|
23 |
"""
|
24 |
+
params = {"pref_name__icontains": drug_name, "limit": limit}
|
25 |
+
return _chembl_get("molecule", params)
|
|
|
|
|
26 |
|
27 |
+
|
28 |
+
def get_drug_details(chembl_id: str):
|
29 |
"""
|
30 |
+
Retrieve detailed information for a specific ChEMBL compound.
|
|
|
31 |
"""
|
32 |
+
return _chembl_get(f"molecule/{chembl_id}")
|
33 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
# -----------------------
|
36 |
+
# Target & Mechanism
|
37 |
+
# -----------------------
|
38 |
+
def search_target_by_name(target_name: str, limit: int = 5):
|
39 |
"""
|
40 |
+
Search for a biological target (e.g., protein) by name.
|
41 |
"""
|
42 |
+
params = {"pref_name__icontains": target_name, "limit": limit}
|
43 |
+
return _chembl_get("target", params)
|
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 the mechanism of action for a given compound.
|
56 |
+
"""
|
57 |
+
params = {"molecule_chembl_id": chembl_id}
|
58 |
+
return _chembl_get("mechanism", params)
|
59 |
+
|
60 |
+
|
61 |
+
# -----------------------
|
62 |
+
# Bioactivity Data
|
63 |
+
# -----------------------
|
64 |
+
def get_bioactivity_for_target(chembl_target_id: str, limit: int = 10):
|
65 |
+
"""
|
66 |
+
Get bioactivity data (IC50, Ki, etc.) for a target.
|
67 |
+
"""
|
68 |
+
params = {"target_chembl_id": chembl_target_id, "limit": limit}
|
69 |
+
return _chembl_get("activity", params)
|
70 |
+
|
71 |
+
|
72 |
+
def get_bioactivity_for_drug(chembl_id: str, limit: int = 10):
|
73 |
+
"""
|
74 |
+
Get bioactivity data for a compound.
|
75 |
+
"""
|
76 |
+
params = {"molecule_chembl_id": chembl_id, "limit": limit}
|
77 |
+
return _chembl_get("activity", params)
|
78 |
+
|
79 |
+
|
80 |
+
# -----------------------
|
81 |
+
# Clinical Candidates
|
82 |
+
# -----------------------
|
83 |
+
def get_clinical_candidates(drug_name: str):
|
84 |
+
"""
|
85 |
+
Find drugs in clinical trials from ChEMBL.
|
86 |
+
"""
|
87 |
+
compounds = search_drug_by_name(drug_name, limit=1)
|
88 |
+
if compounds["page_meta"]["total_count"] > 0:
|
89 |
+
chembl_id = compounds["molecules"][0]["molecule_chembl_id"]
|
90 |
+
return get_mechanism_of_action(chembl_id)
|
91 |
+
return None
|