mgbam commited on
Commit
ea8c68b
·
verified ·
1 Parent(s): ee653fa

Update genesis/api_clients/bioportal_api.py

Browse files
Files changed (1) hide show
  1. genesis/api_clients/bioportal_api.py +60 -44
genesis/api_clients/bioportal_api.py CHANGED
@@ -1,73 +1,89 @@
1
  # genesis/api_clients/bioportal_api.py
2
  import os
3
  import requests
4
- from typing import List, Dict, Optional
5
 
6
- BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY") # From your Hugging Face secrets or .env
7
  BIOPORTAL_BASE = "https://data.bioontology.org"
8
 
9
- HEADERS = {
10
- "Authorization": f"apikey token={BIOPORTAL_API_KEY}"
11
- }
12
 
13
- def search_ontology(term: str, ontology: Optional[str] = None, max_results: int = 10) -> List[Dict]:
 
 
 
 
 
 
 
 
14
  """
15
- Search BioPortal for a given term, optionally within a specific ontology.
 
16
  """
17
- params = {
18
  "q": term,
19
  "pagesize": max_results
20
- }
21
- if ontology:
22
- params["ontology"] = ontology
23
-
24
- r = requests.get(f"{BIOPORTAL_BASE}/search", params=params, headers=HEADERS)
25
  r.raise_for_status()
26
  data = r.json()
27
-
28
  results = []
29
- for item in data.get("collection", []):
30
  results.append({
31
- "label": item.get("prefLabel"),
32
- "definition": item.get("definition"),
33
- "synonyms": item.get("synonym", []),
34
- "ontology": item.get("links", {}).get("ontology"),
35
- "iri": item.get("@id"),
36
- "cui": item.get("cui", None),
37
- "semantic_types": item.get("semanticType", [])
38
  })
39
  return results
40
 
41
- def get_term_details(ontology_acronym: str, term_id: str) -> Dict:
42
  """
43
- Get detailed information about a specific term in an ontology.
44
  """
45
- encoded_term_id = term_id.replace(":", "%3A").replace("/", "%2F")
46
- r = requests.get(
47
- f"{BIOPORTAL_BASE}/ontologies/{ontology_acronym}/classes/{encoded_term_id}",
48
- headers=HEADERS
49
- )
50
  r.raise_for_status()
51
  return r.json()
52
 
53
- def map_to_ontologies(term: str) -> List[Dict]:
54
  """
55
- Search multiple ontologies for mappings of a term.
 
56
  """
57
- params = {
58
- "q": term,
59
- "include": "mappings"
60
- }
61
- r = requests.get(f"{BIOPORTAL_BASE}/search", params=params, headers=HEADERS)
62
  r.raise_for_status()
63
  data = r.json()
64
-
65
  mappings = []
66
- for item in data.get("collection", []):
67
- for mapping in item.get("mappings", []):
68
- mappings.append({
69
- "source": mapping.get("source"),
70
- "target": mapping.get("target"),
71
- "relation": mapping.get("relation")
72
- })
73
  return mappings
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # genesis/api_clients/bioportal_api.py
2
  import os
3
  import requests
4
+ from typing import Dict, List, Optional
5
 
6
+ BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
7
  BIOPORTAL_BASE = "https://data.bioontology.org"
8
 
9
+ if not BIOPORTAL_API_KEY:
10
+ raise EnvironmentError("Missing BIOPORTAL_API_KEY in environment variables.")
 
11
 
12
+ # -------------------------
13
+ # CORE HELPERS
14
+ # -------------------------
15
+ def _auth_params(params: Dict) -> Dict:
16
+ """Attach BioPortal API key."""
17
+ params["apikey"] = BIOPORTAL_API_KEY
18
+ return params
19
+
20
+ def search_term(term: str, ontologies: Optional[List[str]] = None, max_results: int = 10) -> List[Dict]:
21
  """
22
+ Search BioPortal for ontology terms.
23
+ `ontologies` is optional list like ["GO", "CHEBI", "NCBITAXON"]
24
  """
25
+ params = _auth_params({
26
  "q": term,
27
  "pagesize": max_results
28
+ })
29
+ if ontologies:
30
+ params["ontologies"] = ",".join(ontologies)
31
+
32
+ r = requests.get(f"{BIOPORTAL_BASE}/search", params=params)
33
  r.raise_for_status()
34
  data = r.json()
35
+
36
  results = []
37
+ for collection in data.get("collection", []):
38
  results.append({
39
+ "prefLabel": collection.get("prefLabel"),
40
+ "ontology": collection.get("links", {}).get("ontology"),
41
+ "definition": collection.get("definition"),
42
+ "cui": collection.get("cui"),
43
+ "uri": collection.get("@id")
 
 
44
  })
45
  return results
46
 
47
+ def get_term_details(term_uri: str) -> Dict:
48
  """
49
+ Get detailed metadata for a term given its URI.
50
  """
51
+ params = _auth_params({})
52
+ r = requests.get(term_uri, params=params)
 
 
 
53
  r.raise_for_status()
54
  return r.json()
55
 
56
+ def get_mappings(term_uri: str) -> List[Dict]:
57
  """
58
+ Get cross-ontology mappings for a term.
59
+ Useful for linking to UMLS, NCBI, ChEMBL.
60
  """
61
+ params = _auth_params({})
62
+ r = requests.get(f"{term_uri}/mappings", params=params)
 
 
 
63
  r.raise_for_status()
64
  data = r.json()
65
+
66
  mappings = []
67
+ for m in data:
68
+ mappings.append({
69
+ "source": m.get("classes", [{}])[0].get("links", {}).get("ontology"),
70
+ "target": m.get("classes", [{}])[-1].get("links", {}).get("ontology"),
71
+ "target_uri": m.get("classes", [{}])[-1].get("@id"),
72
+ "target_label": m.get("classes", [{}])[-1].get("prefLabel")
73
+ })
74
  return mappings
75
+
76
+ def find_related_terms(term: str, ontologies: Optional[List[str]] = None) -> List[Dict]:
77
+ """
78
+ Find related ontology terms using BioPortal search + mappings.
79
+ """
80
+ results = search_term(term, ontologies=ontologies)
81
+ related = []
82
+ for r in results:
83
+ mappings = get_mappings(r["uri"])
84
+ related.append({
85
+ "term": r["prefLabel"],
86
+ "uri": r["uri"],
87
+ "mappings": mappings
88
+ })
89
+ return related