Download this file

73 lines (61 with data), 2.2 kB

 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
This module contains the API for fetching ols database
"""
from typing import List, Dict
import requests
def fetch_from_ols(term: str) -> str:
"""
Fetch the label for a single term from OLS.
Args:
term (str): The term in the format "ONTOLOGY:TERM_ID".
Returns:
str: The label for the term or an error message.
"""
try:
ontology, _ = term.split(":")
base_url = f"https://www.ebi.ac.uk/ols4/api/ontologies/{ontology.lower()}/terms"
params = {"obo_id": term}
response = requests.get(
base_url,
params=params,
headers={"Accept": "application/json"},
timeout=10
)
response.raise_for_status()
data = response.json()
label = '-'
# Extract and return the label
if "_embedded" in data and "terms" in data["_embedded"] \
and len(data["_embedded"]["terms"]) > 0:
label = data["_embedded"]["terms"][0].get("label", "Label not found")
return label
except (requests.exceptions.RequestException, KeyError, IndexError) as e:
return f"Error: {str(e)}"
def fetch_ols_labels(terms: List[str]) -> Dict[str, str]:
"""
Fetch labels for multiple terms from OLS.
Args:
terms (List[str]): A list of terms in the format "ONTOLOGY:TERM_ID".
Returns:
Dict[str, str]: A mapping of term IDs to their labels or error messages.
"""
results = {}
for term in terms:
results[term] = fetch_from_ols(term)
return results
def search_ols_labels(data: List[Dict[str, str]]) -> Dict[str, Dict[str, str]]:
"""
Fetch OLS annotations grouped by ontology type.
Args:
data (List[Dict[str, str]]): A list of dictionaries containing 'Id' and 'Database'.
Returns:
Dict[str, Dict[str, str]]: A mapping of ontology type to term labels.
"""
grouped_data = {}
for entry in data:
ontology = entry["Database"].lower()
grouped_data.setdefault(ontology, []).append(entry["Id"])
results = {}
for ontology, terms in grouped_data.items():
results[ontology] = fetch_ols_labels(terms)
return results