mgbam commited on
Commit
634f4d8
·
verified ·
1 Parent(s): 5ed5b69

Update clinical_nlp/umls_bioportal.py

Browse files
Files changed (1) hide show
  1. clinical_nlp/umls_bioportal.py +48 -6
clinical_nlp/umls_bioportal.py CHANGED
@@ -1,10 +1,52 @@
1
  import requests
2
  from config.settings import settings
 
3
 
4
- def lookup_umls(term):
5
- url = f"https://uts-ws.nlm.nih.gov/rest/search/current?string={term}&apiKey={settings.umls_api_key}"
6
- return requests.get(url).json().get("result",{}).get("results",[])[:5]
7
 
8
- def lookup_bioportal(term):
9
- url = f"https://data.bioontology.org/search?q={term}&apikey={settings.bioportal_api_key}"
10
- return requests.get(url).json().get("collection",[])[:5]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import requests
2
  from config.settings import settings
3
+ from services.logger import app_logger
4
 
5
+ # This is a very simplified example. Real UMLS/BioPortal integration is complex.
 
 
6
 
7
+ UMLS_BASE_URL = "https://uts-ws.nlm.nih.gov/rest" # Example, check actual UMLS REST API
8
+ BIOPORTAL_BASE_URL = "http://data.bioontology.org"
9
+
10
+ def search_umls_term(term: str) -> dict:
11
+ if not settings.UMLS_API_KEY:
12
+ app_logger.warning("UMLS_API_KEY not set. UMLS search will be mocked.")
13
+ return {"results": [{"term": term, "cui": "C0000000", "definition": "Mocked UMLS definition."}]}
14
+
15
+ # This is a placeholder for a real UMLS API call.
16
+ # You'd need to handle authentication (e.g., Ticket Granting Ticket) and parsing.
17
+ # Example endpoint (hypothetical):
18
+ # endpoint = f"{UMLS_BASE_URL}/search/current?string={term}&apiKey={settings.UMLS_API_KEY}"
19
+ # For simplicity, we'll mock.
20
+ try:
21
+ # params = {'string': term, 'apiKey': settings.UMLS_API_KEY, 'pageNumber': 1, 'pageSize': 5}
22
+ # response = requests.get(f"{UMLS_BASE_URL}/search/current", params=params) # Fictional endpoint
23
+ # response.raise_for_status()
24
+ # data = response.json()
25
+ # return data.get("result", {}).get("results", [])
26
+ return {"results": [{"name": f"Mocked UMLS result for {term}"}]} # Simplified mock
27
+ except requests.RequestException as e:
28
+ app_logger.error(f"UMLS API request failed: {e}")
29
+ return {"error": str(e)}
30
+ except Exception as e:
31
+ app_logger.error(f"Error processing UMLS search for '{term}': {e}")
32
+ return {"error": f"Unexpected error during UMLS search: {str(e)}"}
33
+
34
+
35
+ def search_bioportal_term(term: str, ontology: str = "SNOMEDCT") -> dict:
36
+ if not settings.BIOPORTAL_API_KEY:
37
+ app_logger.warning("BIOPORTAL_API_KEY not set. BioPortal search will be mocked.")
38
+ return {"collection": [{"prefLabel": term, "cui": ["C0000000"], "definition": ["Mocked BioPortal definition."]}]}
39
+
40
+ headers = {"Authorization": f"apikey token={settings.BIOPORTAL_API_KEY}"}
41
+ params = {"q": term, "ontologies": ontology, "include": "prefLabel,synonym,definition,cui"}
42
+
43
+ try:
44
+ response = requests.get(f"{BIOPORTAL_BASE_URL}/search", params=params, headers=headers)
45
+ response.raise_for_status()
46
+ return response.json()
47
+ except requests.RequestException as e:
48
+ app_logger.error(f"BioPortal API request failed: {e}")
49
+ return {"error": str(e)}
50
+ except Exception as e:
51
+ app_logger.error(f"Error processing BioPortal search for '{term}': {e}")
52
+ return {"error": f"Unexpected error during BioPortal search: {str(e)}"}