File size: 2,642 Bytes
885283d
 
634f4d8
885283d
634f4d8
885283d
634f4d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import requests
from config.settings import settings
from services.logger import app_logger

# This is a very simplified example. Real UMLS/BioPortal integration is complex.

UMLS_BASE_URL = "https://uts-ws.nlm.nih.gov/rest" # Example, check actual UMLS REST API
BIOPORTAL_BASE_URL = "http://data.bioontology.org"

def search_umls_term(term: str) -> dict:
    if not settings.UMLS_API_KEY:
        app_logger.warning("UMLS_API_KEY not set. UMLS search will be mocked.")
        return {"results": [{"term": term, "cui": "C0000000", "definition": "Mocked UMLS definition."}]}
    
    # This is a placeholder for a real UMLS API call.
    # You'd need to handle authentication (e.g., Ticket Granting Ticket) and parsing.
    # Example endpoint (hypothetical):
    # endpoint = f"{UMLS_BASE_URL}/search/current?string={term}&apiKey={settings.UMLS_API_KEY}"
    # For simplicity, we'll mock.
    try:
        # params = {'string': term, 'apiKey': settings.UMLS_API_KEY, 'pageNumber': 1, 'pageSize': 5}
        # response = requests.get(f"{UMLS_BASE_URL}/search/current", params=params) # Fictional endpoint
        # response.raise_for_status()
        # data = response.json()
        # return data.get("result", {}).get("results", [])
        return {"results": [{"name": f"Mocked UMLS result for {term}"}]} # Simplified mock
    except requests.RequestException as e:
        app_logger.error(f"UMLS API request failed: {e}")
        return {"error": str(e)}
    except Exception as e:
        app_logger.error(f"Error processing UMLS search for '{term}': {e}")
        return {"error": f"Unexpected error during UMLS search: {str(e)}"}


def search_bioportal_term(term: str, ontology: str = "SNOMEDCT") -> dict:
    if not settings.BIOPORTAL_API_KEY:
        app_logger.warning("BIOPORTAL_API_KEY not set. BioPortal search will be mocked.")
        return {"collection": [{"prefLabel": term, "cui": ["C0000000"], "definition": ["Mocked BioPortal definition."]}]}

    headers = {"Authorization": f"apikey token={settings.BIOPORTAL_API_KEY}"}
    params = {"q": term, "ontologies": ontology, "include": "prefLabel,synonym,definition,cui"}
    
    try:
        response = requests.get(f"{BIOPORTAL_BASE_URL}/search", params=params, headers=headers)
        response.raise_for_status()
        return response.json()
    except requests.RequestException as e:
        app_logger.error(f"BioPortal API request failed: {e}")
        return {"error": str(e)}
    except Exception as e:
        app_logger.error(f"Error processing BioPortal search for '{term}': {e}")
        return {"error": f"Unexpected error during BioPortal search: {str(e)}"}