mgbam commited on
Commit
d68c20d
·
verified ·
1 Parent(s): 325ee75

Update genesis/api_clients/chembl_api.py

Browse files
Files changed (1) hide show
  1. genesis/api_clients/chembl_api.py +40 -53
genesis/api_clients/chembl_api.py CHANGED
@@ -1,88 +1,75 @@
1
  # genesis/api_clients/chembl_api.py
2
- import os
3
  import requests
4
- from typing import List, Dict, Optional
 
5
 
6
- CHEMBL_BASE = "https://www.ebi.ac.uk/chembl/api/data"
7
 
8
- def _fetch_paginated(endpoint: str, params: dict) -> List[dict]:
9
  """
10
- Handles paginated ChEMBL API responses.
 
11
  """
12
- results = []
13
- url = f"{CHEMBL_BASE}/{endpoint}"
14
-
15
- while url:
16
- r = requests.get(url, params=params)
17
  r.raise_for_status()
18
- data = r.json()
19
- page_items = data.get(endpoint, [])
20
- results.extend(page_items)
21
- url = data.get("page_meta", {}).get("next")
22
- params = {} # clear params after first page to avoid duplication
23
- return results
24
-
25
-
26
- def search_molecule_by_name(name: str, max_results: int = 10) -> List[dict]:
27
- """
28
- Search molecules in ChEMBL by name or synonym.
29
- """
30
- params = {"molecule_synonyms__icontains": name, "limit": max_results}
31
- return _fetch_paginated("molecule", params)
32
-
33
-
34
- def get_molecule_details(chembl_id: str) -> dict:
35
- """
36
- Retrieve detailed information for a molecule by ChEMBL ID.
37
- """
38
- r = requests.get(f"{CHEMBL_BASE}/molecule/{chembl_id}")
39
  r.raise_for_status()
40
  return r.json()
41
 
42
 
43
- def get_molecule_bioactivities(chembl_id: str, max_results: int = 50) -> List[dict]:
44
  """
45
- Retrieve bioactivity records for a given molecule.
46
  """
47
- params = {"molecule_chembl_id": chembl_id, "limit": max_results}
48
- return _fetch_paginated("activity", params)
 
 
49
 
50
 
51
- def search_target_by_name(name: str, max_results: int = 10) -> List[dict]:
52
  """
53
- Search for biological targets by name.
54
  """
55
- params = {"target_synonym__icontains": name, "limit": max_results}
56
- return _fetch_paginated("target", params)
 
57
 
58
 
59
- def get_target_details(chembl_id: str) -> dict:
60
  """
61
- Retrieve detailed target information by ChEMBL ID.
62
  """
63
- r = requests.get(f"{CHEMBL_BASE}/target/{chembl_id}")
64
  r.raise_for_status()
65
  return r.json()
66
 
67
 
68
- def search_assays_by_target(target_chembl_id: str, max_results: int = 50) -> List[dict]:
69
  """
70
- Retrieve assays related to a specific target.
71
  """
72
- params = {"target_chembl_id": target_chembl_id, "limit": max_results}
73
- return _fetch_paginated("assay", params)
 
74
 
75
 
76
- def get_drugs_in_clinical_phase(phase: int, max_results: int = 20) -> List[dict]:
77
  """
78
- Retrieve drugs by clinical trial phase.
79
  """
80
- params = {"max_phase": phase, "limit": max_results}
81
- return _fetch_paginated("molecule", params)
 
82
 
83
 
84
- def get_molecule_image_url(chembl_id: str) -> str:
85
  """
86
- Generate a URL to the 2D structure image of a molecule.
87
  """
88
- return f"https://www.ebi.ac.uk/chembl/api/data/image/{chembl_id}.svg"
 
1
  # genesis/api_clients/chembl_api.py
 
2
  import requests
3
+ import os
4
+ from typing import Dict, List, Optional
5
 
6
+ CHEMBL_BASE_URL = "https://www.ebi.ac.uk/chembl/api/data"
7
 
8
+ def chembl_search_molecule(name_or_id: str) -> Dict:
9
  """
10
+ Search for a molecule in ChEMBL by name or ChEMBL ID.
11
+ Returns detailed information including structure, properties, and targets.
12
  """
13
+ r = requests.get(f"{CHEMBL_BASE_URL}/molecule/{name_or_id}.json")
14
+ if r.status_code == 404:
15
+ # Try text search
16
+ search_url = f"{CHEMBL_BASE_URL}/molecule/search.json?q={name_or_id}"
17
+ r = requests.get(search_url)
18
  r.raise_for_status()
19
+ results = r.json().get("molecules", [])
20
+ return results[0] if results else {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  r.raise_for_status()
22
  return r.json()
23
 
24
 
25
+ def chembl_bioactivity_for_target(target_chembl_id: str, limit: int = 20) -> List[Dict]:
26
  """
27
+ Retrieve bioactivity data for a given target (e.g., CHEMBL203).
28
  """
29
+ r = requests.get(f"{CHEMBL_BASE_URL}/activity.json?target_chembl_id={target_chembl_id}&limit={limit}")
30
+ r.raise_for_status()
31
+ activities = r.json().get("activities", [])
32
+ return activities
33
 
34
 
35
+ def chembl_target_info(target_chembl_id: str) -> Dict:
36
  """
37
+ Get target details including organism, target type, and components.
38
  """
39
+ r = requests.get(f"{CHEMBL_BASE_URL}/target/{target_chembl_id}.json")
40
+ r.raise_for_status()
41
+ return r.json()
42
 
43
 
44
+ def chembl_assay_info(assay_chembl_id: str) -> Dict:
45
  """
46
+ Get detailed information about a specific assay.
47
  """
48
+ r = requests.get(f"{CHEMBL_BASE_URL}/assay/{assay_chembl_id}.json")
49
  r.raise_for_status()
50
  return r.json()
51
 
52
 
53
+ def chembl_mechanism(molecule_chembl_id: str) -> List[Dict]:
54
  """
55
+ Retrieve mechanism of action for a molecule.
56
  """
57
+ r = requests.get(f"{CHEMBL_BASE_URL}/mechanism.json?molecule_chembl_id={molecule_chembl_id}")
58
+ r.raise_for_status()
59
+ return r.json().get("mechanisms", [])
60
 
61
 
62
+ def chembl_drug_indications(molecule_chembl_id: str) -> List[Dict]:
63
  """
64
+ Get drug indications linked to a molecule.
65
  """
66
+ r = requests.get(f"{CHEMBL_BASE_URL}/drug_indication.json?molecule_chembl_id={molecule_chembl_id}")
67
+ r.raise_for_status()
68
+ return r.json().get("drug_indications", [])
69
 
70
 
71
+ def chembl_structure_image(molecule_chembl_id: str, image_size: str = "500") -> str:
72
  """
73
+ Get the URL to the molecule’s structure image.
74
  """
75
+ return f"https://www.ebi.ac.uk/chembl/api/utils/image/{molecule_chembl_id}?format=png&size={image_size}"