Spaces:
Sleeping
Sleeping
Update genesis/api_clients/chembl_api.py
Browse files
genesis/api_clients/chembl_api.py
CHANGED
|
@@ -1,98 +1,88 @@
|
|
| 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
|
| 8 |
"""
|
| 9 |
-
|
| 10 |
"""
|
| 11 |
-
|
| 12 |
-
|
| 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
|
| 28 |
"""
|
| 29 |
-
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
response.raise_for_status()
|
| 34 |
-
return response.json()
|
| 35 |
|
| 36 |
|
| 37 |
-
def
|
| 38 |
"""
|
| 39 |
-
|
| 40 |
"""
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
return response.json().get("targets", [])
|
| 45 |
|
| 46 |
|
| 47 |
-
def
|
| 48 |
"""
|
| 49 |
-
Retrieve
|
| 50 |
"""
|
| 51 |
-
|
| 52 |
-
|
| 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
|
| 65 |
"""
|
| 66 |
-
Retrieve
|
| 67 |
"""
|
| 68 |
-
|
| 69 |
-
|
| 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
|
| 84 |
"""
|
| 85 |
-
|
| 86 |
"""
|
| 87 |
-
|
| 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 |
-
]
|
|
|
|
| 1 |
# genesis/api_clients/chembl_api.py
|
|
|
|
| 2 |
import os
|
| 3 |
+
import requests
|
| 4 |
+
from typing import List, Dict, Optional
|
| 5 |
+
|
| 6 |
+
CHEMBL_BASE = "https://www.ebi.ac.uk/chembl/api/data"
|
| 7 |
+
|
| 8 |
+
def _fetch_paginated(endpoint: str, params: dict) -> List[dict]:
|
| 9 |
+
"""
|
| 10 |
+
Handles paginated ChEMBL API responses.
|
| 11 |
+
"""
|
| 12 |
+
results = []
|
| 13 |
+
url = f"{CHEMBL_BASE}/{endpoint}"
|
| 14 |
+
|
| 15 |
+
while url:
|
| 16 |
+
r = requests.get(url, params=params)
|
| 17 |
+
r.raise_for_status()
|
| 18 |
+
data = r.json()
|
| 19 |
+
page_items = data.get(endpoint, [])
|
| 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 get_molecule_bioactivities(chembl_id: str, max_results: int = 50) -> List[dict]:
|
| 44 |
"""
|
| 45 |
+
Retrieve bioactivity records for a given molecule.
|
| 46 |
"""
|
| 47 |
+
params = {"molecule_chembl_id": chembl_id, "limit": max_results}
|
| 48 |
+
return _fetch_paginated("activity", params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
+
def search_target_by_name(name: str, max_results: int = 10) -> List[dict]:
|
| 52 |
"""
|
| 53 |
+
Search for biological targets by name.
|
| 54 |
"""
|
| 55 |
+
params = {"target_synonym__icontains": name, "limit": max_results}
|
| 56 |
+
return _fetch_paginated("target", params)
|
|
|
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
+
def get_target_details(chembl_id: str) -> dict:
|
| 60 |
"""
|
| 61 |
+
Retrieve detailed target information by ChEMBL ID.
|
| 62 |
"""
|
| 63 |
+
r = requests.get(f"{CHEMBL_BASE}/target/{chembl_id}")
|
| 64 |
+
r.raise_for_status()
|
| 65 |
+
return r.json()
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
+
def search_assays_by_target(target_chembl_id: str, max_results: int = 50) -> List[dict]:
|
| 69 |
"""
|
| 70 |
+
Retrieve assays related to a specific target.
|
| 71 |
"""
|
| 72 |
+
params = {"target_chembl_id": target_chembl_id, "limit": max_results}
|
| 73 |
+
return _fetch_paginated("assay", params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
|
| 76 |
+
def get_drugs_in_clinical_phase(phase: int, max_results: int = 20) -> List[dict]:
|
| 77 |
"""
|
| 78 |
+
Retrieve drugs by clinical trial phase.
|
| 79 |
"""
|
| 80 |
+
params = {"max_phase": phase, "limit": max_results}
|
| 81 |
+
return _fetch_paginated("molecule", params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
+
def get_molecule_image_url(chembl_id: str) -> str:
|
| 85 |
"""
|
| 86 |
+
Generate a URL to the 2D structure image of a molecule.
|
| 87 |
"""
|
| 88 |
+
return f"https://www.ebi.ac.uk/chembl/api/data/image/{chembl_id}.svg"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|