mgbam commited on
Commit
6fa7402
·
verified ·
1 Parent(s): 5755f5b

Update genesis/api_clients/chembl_api.py

Browse files
Files changed (1) hide show
  1. genesis/api_clients/chembl_api.py +70 -44
genesis/api_clients/chembl_api.py CHANGED
@@ -1,65 +1,91 @@
1
  # genesis/api_clients/chembl_api.py
 
2
  import requests
3
 
4
- CHEMBL_BASE = "https://www.ebi.ac.uk/chembl/api/data"
5
 
6
- def search_molecule(query: str, limit: int = 10):
7
  """
8
- Search ChEMBL for a molecule by name, ChEMBL ID, or synonyms.
9
  """
10
- url = f"{CHEMBL_BASE}/molecule"
11
- params = {
12
- "molecule_structures__standard_inchi_key__flexmatch": query,
13
- "limit": limit,
14
- "format": "json"
15
- }
16
  res = requests.get(url, params=params)
17
  res.raise_for_status()
18
- return res.json().get("molecules", [])
 
19
 
20
- def get_molecule_details(chembl_id: str):
 
 
 
21
  """
22
- Get detailed information for a given ChEMBL molecule ID.
23
  """
24
- url = f"{CHEMBL_BASE}/molecule/{chembl_id}.json"
25
- res = requests.get(url)
26
- res.raise_for_status()
27
- return res.json()
28
 
29
- def get_bioactivity(chembl_id: str, limit: int = 20):
 
30
  """
31
- Get bioactivity data for a given molecule ID.
32
- Includes IC50, EC50, Ki, etc.
33
  """
34
- url = f"{CHEMBL_BASE}/activity"
35
- params = {
36
- "molecule_chembl_id": chembl_id,
37
- "limit": limit,
38
- "format": "json"
39
- }
40
- res = requests.get(url, params=params)
41
- res.raise_for_status()
42
- return res.json().get("activities", [])
43
 
44
- def search_target(target_name: str, limit: int = 10):
 
 
 
45
  """
46
- Search for a biological target (protein, enzyme, receptor, etc.).
47
  """
48
- url = f"{CHEMBL_BASE}/target"
49
- params = {
50
- "target_synonym__icontains": target_name,
51
- "limit": limit,
52
- "format": "json"
53
- }
54
- res = requests.get(url, params=params)
55
- res.raise_for_status()
56
- return res.json().get("targets", [])
57
 
58
  def get_target_details(chembl_target_id: str):
59
  """
60
- Get detailed target info from ChEMBL.
61
  """
62
- url = f"{CHEMBL_BASE}/target/{chembl_target_id}.json"
63
- res = requests.get(url)
64
- res.raise_for_status()
65
- return res.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # genesis/api_clients/chembl_api.py
2
+ import os
3
  import requests
4
 
5
+ CHEMBL_BASE_URL = "https://www.ebi.ac.uk/chembl/api/data"
6
 
7
+ def _chembl_get(endpoint: str, params: dict = None):
8
  """
9
+ Generic ChEMBL GET request helper.
10
  """
11
+ url = f"{CHEMBL_BASE_URL}/{endpoint}"
 
 
 
 
 
12
  res = requests.get(url, params=params)
13
  res.raise_for_status()
14
+ return res.json()
15
+
16
 
17
+ # -----------------------
18
+ # Drug & Compound Search
19
+ # -----------------------
20
+ def search_drug_by_name(drug_name: str, limit: int = 5):
21
  """
22
+ Search for a drug/compound by name in ChEMBL.
23
  """
24
+ params = {"pref_name__icontains": drug_name, "limit": limit}
25
+ return _chembl_get("molecule", params)
 
 
26
 
27
+
28
+ def get_drug_details(chembl_id: str):
29
  """
30
+ Retrieve detailed information for a specific ChEMBL compound.
 
31
  """
32
+ return _chembl_get(f"molecule/{chembl_id}")
33
+
 
 
 
 
 
 
 
34
 
35
+ # -----------------------
36
+ # Target & Mechanism
37
+ # -----------------------
38
+ def search_target_by_name(target_name: str, limit: int = 5):
39
  """
40
+ Search for a biological target (e.g., protein) by name.
41
  """
42
+ params = {"pref_name__icontains": target_name, "limit": limit}
43
+ return _chembl_get("target", params)
44
+
 
 
 
 
 
 
45
 
46
  def get_target_details(chembl_target_id: str):
47
  """
48
+ Retrieve target details from ChEMBL.
49
  """
50
+ return _chembl_get(f"target/{chembl_target_id}")
51
+
52
+
53
+ def get_mechanism_of_action(chembl_id: str):
54
+ """
55
+ Retrieve the mechanism of action for a given compound.
56
+ """
57
+ params = {"molecule_chembl_id": chembl_id}
58
+ return _chembl_get("mechanism", params)
59
+
60
+
61
+ # -----------------------
62
+ # Bioactivity Data
63
+ # -----------------------
64
+ def get_bioactivity_for_target(chembl_target_id: str, limit: int = 10):
65
+ """
66
+ Get bioactivity data (IC50, Ki, etc.) for a target.
67
+ """
68
+ params = {"target_chembl_id": chembl_target_id, "limit": limit}
69
+ return _chembl_get("activity", params)
70
+
71
+
72
+ def get_bioactivity_for_drug(chembl_id: str, limit: int = 10):
73
+ """
74
+ Get bioactivity data for a compound.
75
+ """
76
+ params = {"molecule_chembl_id": chembl_id, "limit": limit}
77
+ return _chembl_get("activity", params)
78
+
79
+
80
+ # -----------------------
81
+ # Clinical Candidates
82
+ # -----------------------
83
+ def get_clinical_candidates(drug_name: str):
84
+ """
85
+ Find drugs in clinical trials from ChEMBL.
86
+ """
87
+ compounds = search_drug_by_name(drug_name, limit=1)
88
+ if compounds["page_meta"]["total_count"] > 0:
89
+ chembl_id = compounds["molecules"][0]["molecule_chembl_id"]
90
+ return get_mechanism_of_action(chembl_id)
91
+ return None