Spaces:
Sleeping
Sleeping
# genesis/api_clients/chembl_api.py | |
import os | |
import requests | |
CHEMBL_BASE_URL = "https://www.ebi.ac.uk/chembl/api/data" | |
def _chembl_get(endpoint: str, params: dict = None): | |
""" | |
Generic ChEMBL GET request helper. | |
""" | |
url = f"{CHEMBL_BASE_URL}/{endpoint}" | |
res = requests.get(url, params=params) | |
res.raise_for_status() | |
return res.json() | |
# ----------------------- | |
# Drug & Compound Search | |
# ----------------------- | |
def search_drug_by_name(drug_name: str, limit: int = 5): | |
""" | |
Search for a drug/compound by name in ChEMBL. | |
""" | |
params = {"pref_name__icontains": drug_name, "limit": limit} | |
return _chembl_get("molecule", params) | |
def get_drug_details(chembl_id: str): | |
""" | |
Retrieve detailed information for a specific ChEMBL compound. | |
""" | |
return _chembl_get(f"molecule/{chembl_id}") | |
# ----------------------- | |
# Target & Mechanism | |
# ----------------------- | |
def search_target_by_name(target_name: str, limit: int = 5): | |
""" | |
Search for a biological target (e.g., protein) by name. | |
""" | |
params = {"pref_name__icontains": target_name, "limit": limit} | |
return _chembl_get("target", params) | |
def get_target_details(chembl_target_id: str): | |
""" | |
Retrieve target details from ChEMBL. | |
""" | |
return _chembl_get(f"target/{chembl_target_id}") | |
def get_mechanism_of_action(chembl_id: str): | |
""" | |
Retrieve the mechanism of action for a given compound. | |
""" | |
params = {"molecule_chembl_id": chembl_id} | |
return _chembl_get("mechanism", params) | |
# ----------------------- | |
# Bioactivity Data | |
# ----------------------- | |
def get_bioactivity_for_target(chembl_target_id: str, limit: int = 10): | |
""" | |
Get bioactivity data (IC50, Ki, etc.) for a target. | |
""" | |
params = {"target_chembl_id": chembl_target_id, "limit": limit} | |
return _chembl_get("activity", params) | |
def get_bioactivity_for_drug(chembl_id: str, limit: int = 10): | |
""" | |
Get bioactivity data for a compound. | |
""" | |
params = {"molecule_chembl_id": chembl_id, "limit": limit} | |
return _chembl_get("activity", params) | |
# ----------------------- | |
# Clinical Candidates | |
# ----------------------- | |
def get_clinical_candidates(drug_name: str): | |
""" | |
Find drugs in clinical trials from ChEMBL. | |
""" | |
compounds = search_drug_by_name(drug_name, limit=1) | |
if compounds["page_meta"]["total_count"] > 0: | |
chembl_id = compounds["molecules"][0]["molecule_chembl_id"] | |
return get_mechanism_of_action(chembl_id) | |
return None | |