mgbam commited on
Commit
27cd148
·
verified ·
1 Parent(s): 7b3f817

Update genesis/api_clients/chembl_api.py

Browse files
Files changed (1) hide show
  1. genesis/api_clients/chembl_api.py +78 -71
genesis/api_clients/chembl_api.py CHANGED
@@ -1,91 +1,98 @@
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 search_drug(drug_name: str, limit: int = 5):
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  """
9
+ Search ChEMBL for drugs by name.
10
  """
11
+ url = f"{CHEMBL_BASE_URL}/molecule/search.json?q={drug_name}&limit={limit}"
12
+ response = requests.get(url)
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 get_drug_details(chembl_id: str):
28
  """
29
+ Retrieve detailed drug information by ChEMBL ID.
30
  """
31
+ url = f"{CHEMBL_BASE_URL}/molecule/{chembl_id}.json"
32
+ response = requests.get(url)
33
+ response.raise_for_status()
34
+ return response.json()
35
 
36
 
37
+ def get_target_info(chembl_id: str):
 
 
 
38
  """
39
+ Get targets for a given drug (by ChEMBL ID).
40
  """
41
+ url = f"{CHEMBL_BASE_URL}/target.json?molecule_chembl_id={chembl_id}"
42
+ response = requests.get(url)
43
+ response.raise_for_status()
44
+ return response.json().get("targets", [])
 
 
 
 
 
45
 
46
 
47
  def get_mechanism_of_action(chembl_id: str):
48
  """
49
+ Retrieve mechanism of action for a drug.
50
+ """
51
+ url = f"{CHEMBL_BASE_URL}/mechanism.json?molecule_chembl_id={chembl_id}"
52
+ response = requests.get(url)
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 get_bioactivity_data(chembl_id: str, limit: int = 10):
65
+ """
66
+ Retrieve bioactivity data for a given drug.
67
+ """
68
+ url = f"{CHEMBL_BASE_URL}/activity.json?molecule_chembl_id={chembl_id}&limit={limit}"
69
+ response = requests.get(url)
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 search_by_target(target_name: str, limit: int = 5):
84
+ """
85
+ Search drugs by target name.
86
+ """
87
+ url = f"{CHEMBL_BASE_URL}/target/search.json?q={target_name}&limit={limit}"
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
+ ]