mgbam commited on
Commit
87c5f3b
·
verified ·
1 Parent(s): 27e909a

Update genesis/ontology.py

Browse files
Files changed (1) hide show
  1. genesis/ontology.py +38 -36
genesis/ontology.py CHANGED
@@ -1,42 +1,44 @@
1
  # genesis/ontology.py
2
- import requests
3
- from typing import List
 
 
4
 
5
- def expand_terms_with_ontology(query: str, umls_api_key: str, bioportal_api_key: str) -> List[str]:
6
- """Expand query terms using UMLS and BioPortal ontology APIs."""
7
- expanded_terms = set()
8
 
9
- # Always include original query
10
- expanded_terms.add(query)
11
 
12
- # UMLS expansion
13
- if umls_api_key:
14
- try:
15
- umls_url = "https://uts-ws.nlm.nih.gov/rest/search/current"
16
- params = {"string": query, "apiKey": umls_api_key, "pageSize": 5}
17
- r = requests.get(umls_url, params=params, timeout=10)
18
- r.raise_for_status()
19
- data = r.json()
20
- for result in data.get("result", {}).get("results", []):
21
- name = result.get("name")
22
- if name and name.lower() != query.lower():
23
- expanded_terms.add(name)
24
- except Exception as e:
25
- print(f"[Ontology] UMLS expansion failed: {e}")
26
 
27
- # BioPortal expansion
28
- if bioportal_api_key:
29
- try:
30
- bp_url = "https://data.bioontology.org/search"
31
- params = {"q": query, "apikey": bioportal_api_key}
32
- r = requests.get(bp_url, params=params, timeout=10)
33
- r.raise_for_status()
34
- data = r.json()
35
- for coll in data.get("collection", []):
36
- label = coll.get("prefLabel")
37
- if label and label.lower() != query.lower():
38
- expanded_terms.add(label)
39
- except Exception as e:
40
- print(f"[Ontology] BioPortal expansion failed: {e}")
41
 
42
- return list(expanded_terms)
 
 
 
 
 
 
 
 
 
 
1
  # genesis/ontology.py
2
+ """
3
+ Ontology Expansion for GENESIS-AI
4
+ Uses UMLS & BioPortal to find related terms, synonyms, and hierarchies.
5
+ """
6
 
7
+ import os
8
+ import requests
 
9
 
10
+ UMLS_API_KEY = os.getenv("UMLS_API_KEY")
11
+ BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
12
 
13
+ def expand_with_bioportal(term):
14
+ """Expand term using BioPortal ontology search."""
15
+ try:
16
+ url = f"https://data.bioontology.org/search?q={term}&apikey={BIOPORTAL_API_KEY}"
17
+ r = requests.get(url)
18
+ r.raise_for_status()
19
+ data = r.json()
20
+ return list({res["prefLabel"] for res in data.get("collection", []) if "prefLabel" in res})
21
+ except Exception as e:
22
+ return {"error": str(e)}
 
 
 
 
23
 
24
+ def expand_with_umls(term):
25
+ """Expand term using UMLS API."""
26
+ try:
27
+ url = f"https://uts-ws.nlm.nih.gov/rest/search/current?string={term}&apiKey={UMLS_API_KEY}"
28
+ r = requests.get(url)
29
+ r.raise_for_status()
30
+ data = r.json()
31
+ return [res["name"] for res in data.get("result", {}).get("results", []) if "name" in res]
32
+ except Exception as e:
33
+ return {"error": str(e)}
 
 
 
 
34
 
35
+ def expand_terms_with_ontology(term):
36
+ """Combine BioPortal and UMLS expansions."""
37
+ bioportal_terms = expand_with_bioportal(term)
38
+ umls_terms = expand_with_umls(term)
39
+ results = set()
40
+ if isinstance(bioportal_terms, list):
41
+ results.update(bioportal_terms)
42
+ if isinstance(umls_terms, list):
43
+ results.update(umls_terms)
44
+ return list(results) if results else [term]