Spaces:
Sleeping
Sleeping
Update genesis/api_clients/chembl_api.py
Browse files
genesis/api_clients/chembl_api.py
CHANGED
@@ -1,88 +1,75 @@
|
|
1 |
# genesis/api_clients/chembl_api.py
|
2 |
-
import os
|
3 |
import requests
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
|
8 |
-
def
|
9 |
"""
|
10 |
-
|
|
|
11 |
"""
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
r = requests.get(
|
17 |
r.raise_for_status()
|
18 |
-
|
19 |
-
|
20 |
-
results.extend(page_items)
|
21 |
-
url = data.get("page_meta", {}).get("next")
|
22 |
-
params = {} # clear params after first page to avoid duplication
|
23 |
-
return results
|
24 |
-
|
25 |
-
|
26 |
-
def search_molecule_by_name(name: str, max_results: int = 10) -> List[dict]:
|
27 |
-
"""
|
28 |
-
Search molecules in ChEMBL by name or synonym.
|
29 |
-
"""
|
30 |
-
params = {"molecule_synonyms__icontains": name, "limit": max_results}
|
31 |
-
return _fetch_paginated("molecule", params)
|
32 |
-
|
33 |
-
|
34 |
-
def get_molecule_details(chembl_id: str) -> dict:
|
35 |
-
"""
|
36 |
-
Retrieve detailed information for a molecule by ChEMBL ID.
|
37 |
-
"""
|
38 |
-
r = requests.get(f"{CHEMBL_BASE}/molecule/{chembl_id}")
|
39 |
r.raise_for_status()
|
40 |
return r.json()
|
41 |
|
42 |
|
43 |
-
def
|
44 |
"""
|
45 |
-
Retrieve bioactivity
|
46 |
"""
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
|
50 |
|
51 |
-
def
|
52 |
"""
|
53 |
-
|
54 |
"""
|
55 |
-
|
56 |
-
|
|
|
57 |
|
58 |
|
59 |
-
def
|
60 |
"""
|
61 |
-
|
62 |
"""
|
63 |
-
r = requests.get(f"{
|
64 |
r.raise_for_status()
|
65 |
return r.json()
|
66 |
|
67 |
|
68 |
-
def
|
69 |
"""
|
70 |
-
Retrieve
|
71 |
"""
|
72 |
-
|
73 |
-
|
|
|
74 |
|
75 |
|
76 |
-
def
|
77 |
"""
|
78 |
-
|
79 |
"""
|
80 |
-
|
81 |
-
|
|
|
82 |
|
83 |
|
84 |
-
def
|
85 |
"""
|
86 |
-
|
87 |
"""
|
88 |
-
return f"https://www.ebi.ac.uk/chembl/api/
|
|
|
1 |
# genesis/api_clients/chembl_api.py
|
|
|
2 |
import requests
|
3 |
+
import os
|
4 |
+
from typing import Dict, List, Optional
|
5 |
|
6 |
+
CHEMBL_BASE_URL = "https://www.ebi.ac.uk/chembl/api/data"
|
7 |
|
8 |
+
def chembl_search_molecule(name_or_id: str) -> Dict:
|
9 |
"""
|
10 |
+
Search for a molecule in ChEMBL by name or ChEMBL ID.
|
11 |
+
Returns detailed information including structure, properties, and targets.
|
12 |
"""
|
13 |
+
r = requests.get(f"{CHEMBL_BASE_URL}/molecule/{name_or_id}.json")
|
14 |
+
if r.status_code == 404:
|
15 |
+
# Try text search
|
16 |
+
search_url = f"{CHEMBL_BASE_URL}/molecule/search.json?q={name_or_id}"
|
17 |
+
r = requests.get(search_url)
|
18 |
r.raise_for_status()
|
19 |
+
results = r.json().get("molecules", [])
|
20 |
+
return results[0] if results else {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
r.raise_for_status()
|
22 |
return r.json()
|
23 |
|
24 |
|
25 |
+
def chembl_bioactivity_for_target(target_chembl_id: str, limit: int = 20) -> List[Dict]:
|
26 |
"""
|
27 |
+
Retrieve bioactivity data for a given target (e.g., CHEMBL203).
|
28 |
"""
|
29 |
+
r = requests.get(f"{CHEMBL_BASE_URL}/activity.json?target_chembl_id={target_chembl_id}&limit={limit}")
|
30 |
+
r.raise_for_status()
|
31 |
+
activities = r.json().get("activities", [])
|
32 |
+
return activities
|
33 |
|
34 |
|
35 |
+
def chembl_target_info(target_chembl_id: str) -> Dict:
|
36 |
"""
|
37 |
+
Get target details including organism, target type, and components.
|
38 |
"""
|
39 |
+
r = requests.get(f"{CHEMBL_BASE_URL}/target/{target_chembl_id}.json")
|
40 |
+
r.raise_for_status()
|
41 |
+
return r.json()
|
42 |
|
43 |
|
44 |
+
def chembl_assay_info(assay_chembl_id: str) -> Dict:
|
45 |
"""
|
46 |
+
Get detailed information about a specific assay.
|
47 |
"""
|
48 |
+
r = requests.get(f"{CHEMBL_BASE_URL}/assay/{assay_chembl_id}.json")
|
49 |
r.raise_for_status()
|
50 |
return r.json()
|
51 |
|
52 |
|
53 |
+
def chembl_mechanism(molecule_chembl_id: str) -> List[Dict]:
|
54 |
"""
|
55 |
+
Retrieve mechanism of action for a molecule.
|
56 |
"""
|
57 |
+
r = requests.get(f"{CHEMBL_BASE_URL}/mechanism.json?molecule_chembl_id={molecule_chembl_id}")
|
58 |
+
r.raise_for_status()
|
59 |
+
return r.json().get("mechanisms", [])
|
60 |
|
61 |
|
62 |
+
def chembl_drug_indications(molecule_chembl_id: str) -> List[Dict]:
|
63 |
"""
|
64 |
+
Get drug indications linked to a molecule.
|
65 |
"""
|
66 |
+
r = requests.get(f"{CHEMBL_BASE_URL}/drug_indication.json?molecule_chembl_id={molecule_chembl_id}")
|
67 |
+
r.raise_for_status()
|
68 |
+
return r.json().get("drug_indications", [])
|
69 |
|
70 |
|
71 |
+
def chembl_structure_image(molecule_chembl_id: str, image_size: str = "500") -> str:
|
72 |
"""
|
73 |
+
Get the URL to the molecule’s structure image.
|
74 |
"""
|
75 |
+
return f"https://www.ebi.ac.uk/chembl/api/utils/image/{molecule_chembl_id}?format=png&size={image_size}"
|