mgbam commited on
Commit
c37fc6d
·
verified ·
1 Parent(s): 5404a41

Update genesis/api_clients/bioportal_api.py

Browse files
Files changed (1) hide show
  1. genesis/api_clients/bioportal_api.py +37 -17
genesis/api_clients/bioportal_api.py CHANGED
@@ -5,29 +5,49 @@ import requests
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
 
 
5
  BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
6
  BIOPORTAL_BASE = "https://data.bioontology.org"
7
 
8
+ if not BIOPORTAL_API_KEY:
9
+ raise ValueError("Missing BIOPORTAL_API_KEY in environment variables")
10
+
11
+ def search_terms(query: str, limit: int = 10):
12
  """
13
+ Search BioPortal for a term across all ontologies.
 
14
  """
 
 
 
15
  url = f"{BIOPORTAL_BASE}/search"
16
  params = {
17
+ "q": query,
18
  "apikey": BIOPORTAL_API_KEY,
19
+ "pagesize": limit
20
  }
21
  res = requests.get(url, params=params)
22
  res.raise_for_status()
23
+ return res.json().get("collection", [])
24
+
25
+ def get_term_details(ontology_acronym: str, term_id: str):
26
+ """
27
+ Get details about a specific term from a specific ontology.
28
+ """
29
+ url = f"{BIOPORTAL_BASE}/ontologies/{ontology_acronym}/classes/{term_id}"
30
+ params = {"apikey": BIOPORTAL_API_KEY}
31
+ res = requests.get(url, params=params)
32
+ res.raise_for_status()
33
+ return res.json()
34
+
35
+ def list_ontologies():
36
+ """
37
+ List all available ontologies in BioPortal.
38
+ """
39
+ url = f"{BIOPORTAL_BASE}/ontologies"
40
+ params = {"apikey": BIOPORTAL_API_KEY}
41
+ res = requests.get(url, params=params)
42
+ res.raise_for_status()
43
+ return res.json()
44
 
45
+ def get_ontology_details(acronym: str):
46
+ """
47
+ Get metadata for a specific ontology.
48
+ """
49
+ url = f"{BIOPORTAL_BASE}/ontologies/{acronym}"
50
+ params = {"apikey": BIOPORTAL_API_KEY}
51
+ res = requests.get(url, params=params)
52
+ res.raise_for_status()
53
+ return res.json()