mgbam's picture
Update genesis/api_clients/chembl_api.py
53ae019 verified
raw
history blame
2.38 kB
# genesis/api_clients/chembl_api.py
import os
import requests
from typing import Dict, List, Optional
CHEMBL_BASE = "https://www.ebi.ac.uk/chembl/api/data"
CHEMBL_API_KEY = os.getenv("CHEMBL_API_KEY") # If you have API key access, else can be None (public API works without auth)
def search_molecule(query: str, max_results: int = 10) -> List[Dict]:
"""
Search ChEMBL for a molecule by name, synonym, or ChEMBL ID.
"""
params = {"format": "json", "limit": max_results, "molecule_synonyms__icontains": query}
r = requests.get(f"{CHEMBL_BASE}/molecule", params=params)
r.raise_for_status()
data = r.json()
return data.get("molecules", [])
def get_molecule_details(chembl_id: str) -> Dict:
"""
Retrieve full details for a specific molecule (properties, SMILES, InChI).
"""
r = requests.get(f"{CHEMBL_BASE}/molecule/{chembl_id}.json")
r.raise_for_status()
return r.json()
def get_target_details(target_chembl_id: str) -> Dict:
"""
Retrieve details about a protein target.
"""
r = requests.get(f"{CHEMBL_BASE}/target/{target_chembl_id}.json")
r.raise_for_status()
return r.json()
def get_bioactivities(chembl_id: str, max_results: int = 20) -> List[Dict]:
"""
Retrieve bioactivity assays for a given molecule.
"""
params = {"format": "json", "limit": max_results}
r = requests.get(f"{CHEMBL_BASE}/activity?molecule_chembl_id={chembl_id}", params=params)
r.raise_for_status()
return r.json().get("activities", [])
def get_mechanism_of_action(chembl_id: str) -> List[Dict]:
"""
Retrieve mechanism of action data for a drug.
"""
r = requests.get(f"{CHEMBL_BASE}/mechanism.json?molecule_chembl_id={chembl_id}")
r.raise_for_status()
return r.json().get("mechanisms", [])
def get_clinical_trials(chembl_id: str) -> List[Dict]:
"""
Retrieve clinical trial data linked to a drug.
"""
r = requests.get(f"{CHEMBL_BASE}/clinical_trial.json?molecule_chembl_id={chembl_id}")
r.raise_for_status()
return r.json().get("clinical_trials", [])
def get_structural_image(chembl_id: str, size: str = "500") -> Optional[str]:
"""
Retrieve PNG image of molecule from ChEMBL's structure rendering service.
Returns image URL.
"""
return f"https://www.ebi.ac.uk/chembl/api/utils/image/{chembl_id}?dimensions={size}"