File size: 2,376 Bytes
792fe00
d68c20d
53ae019
d68c20d
5bfa97f
53ae019
 
5bfa97f
53ae019
5bfa97f
53ae019
5bfa97f
53ae019
 
5bfa97f
53ae019
 
792fe00
 
53ae019
5404a41
53ae019
5404a41
53ae019
d68c20d
53ae019
5404a41
6fa7402
53ae019
5404a41
53ae019
5404a41
53ae019
d68c20d
 
6fa7402
5404a41
53ae019
5404a41
53ae019
5404a41
53ae019
 
5bfa97f
53ae019
6fa7402
 
53ae019
6fa7402
53ae019
27cd148
53ae019
d68c20d
 
27cd148
 
53ae019
27cd148
53ae019
27cd148
53ae019
d68c20d
53ae019
27cd148
 
53ae019
27cd148
53ae019
 
27cd148
53ae019
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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}"