mgbam commited on
Commit
67ef408
·
verified ·
1 Parent(s): a8a0045

Update genesis/api_clients/bioportal_api.py

Browse files
Files changed (1) hide show
  1. genesis/api_clients/bioportal_api.py +27 -16
genesis/api_clients/bioportal_api.py CHANGED
@@ -1,22 +1,33 @@
1
  # genesis/api_clients/bioportal_api.py
2
- import requests
3
  import os
 
4
 
5
  BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
6
- BASE_URL = "https://data.bioontology.org"
 
 
 
 
 
 
 
 
7
 
8
- def search_bioportal(query: str):
9
- """Search BioPortal for ontologies, classes, or terms."""
10
- url = f"{BASE_URL}/search"
11
- params = {"q": query, "apikey": BIOPORTAL_API_KEY}
12
- r = requests.get(url, params=params)
13
- r.raise_for_status()
14
- return r.json()
 
 
15
 
16
- def get_ontology_list():
17
- """Get list of available ontologies."""
18
- url = f"{BASE_URL}/ontologies"
19
- params = {"apikey": BIOPORTAL_API_KEY}
20
- r = requests.get(url, params=params)
21
- r.raise_for_status()
22
- return r.json()
 
 
1
  # genesis/api_clients/bioportal_api.py
 
2
  import os
3
+ import requests
4
 
5
  BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
6
+ BIOPORTAL_BASE = "https://data.bioontology.org"
7
+
8
+ def search_bioportal(term: str, max_results: int = 10):
9
+ """
10
+ Search BioPortal for ontology terms related to the given term.
11
+ Returns a list of related concept labels + IDs.
12
+ """
13
+ if not BIOPORTAL_API_KEY:
14
+ raise ValueError("BIOPORTAL_API_KEY is missing in environment variables")
15
 
16
+ url = f"{BIOPORTAL_BASE}/search"
17
+ params = {
18
+ "q": term,
19
+ "apikey": BIOPORTAL_API_KEY,
20
+ "pagesize": max_results
21
+ }
22
+ res = requests.get(url, params=params)
23
+ res.raise_for_status()
24
+ data = res.json()
25
 
26
+ results = []
27
+ for item in data.get("collection", []):
28
+ results.append({
29
+ "label": item.get("prefLabel"),
30
+ "id": item.get("@id"),
31
+ "ontology": item.get("links", {}).get("ontology")
32
+ })
33
+ return results