mgbam commited on
Commit
701c750
·
verified ·
1 Parent(s): 026bd38

Update genesis/api_clients/bioportal_api.py

Browse files
Files changed (1) hide show
  1. genesis/api_clients/bioportal_api.py +37 -37
genesis/api_clients/bioportal_api.py CHANGED
@@ -3,71 +3,71 @@ import os
3
  import requests
4
  from typing import List, Dict, Optional
5
 
6
- # BioPortal API
7
  BIOPORTAL_BASE = "https://data.bioontology.org"
8
- BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY") # You must add this to Hugging Face secrets or .env
9
-
10
- if not BIOPORTAL_API_KEY:
11
- raise EnvironmentError("BIOPORTAL_API_KEY is missing. Add it to your environment variables or Hugging Face secrets.")
12
 
13
  HEADERS = {
14
  "Authorization": f"apikey token={BIOPORTAL_API_KEY}"
15
  }
16
 
17
- def search_terms(query: str, ontologies: Optional[List[str]] = None, max_results: int = 20) -> List[Dict]:
18
  """
19
- Search BioPortal for ontology terms.
20
- Optionally limit search to specific ontologies like 'GO', 'CHEBI', 'DOID', etc.
21
  """
22
  params = {
23
- "q": query,
24
  "pagesize": max_results
25
  }
26
- if ontologies:
27
- params["ontologies"] = ",".join(ontologies)
28
 
29
- r = requests.get(f"{BIOPORTAL_BASE}/search", headers=HEADERS, params=params)
30
  r.raise_for_status()
31
  data = r.json()
32
 
33
  results = []
34
  for item in data.get("collection", []):
35
  results.append({
36
- "prefLabel": item.get("prefLabel"),
37
- "definition": item.get("definition", []),
38
  "synonyms": item.get("synonym", []),
39
  "ontology": item.get("links", {}).get("ontology"),
40
- "uri": item.get("@id"),
41
- "cui": item.get("cui", []),
42
- "semanticType": item.get("semanticType", []),
43
  })
44
  return results
45
 
46
-
47
- def get_term_details(term_uri: str) -> Dict:
48
  """
49
- Fetch full details for a given ontology term URI.
50
  """
51
- encoded_uri = requests.utils.quote(term_uri, safe="")
52
- r = requests.get(f"{BIOPORTAL_BASE}/ontologies/{encoded_uri}", headers=HEADERS)
 
 
 
53
  r.raise_for_status()
54
  return r.json()
55
 
56
-
57
- def find_related_terms(term_uri: str) -> List[Dict]:
58
  """
59
- Find related terms for a given ontology concept.
60
  """
61
- encoded_uri = requests.utils.quote(term_uri, safe="")
62
- r = requests.get(f"{BIOPORTAL_BASE}/ontologies/{encoded_uri}/related", headers=HEADERS)
 
 
 
63
  r.raise_for_status()
64
- return r.json()
65
-
66
-
67
- def map_to_ontology(entity_name: str, preferred_ontologies: Optional[List[str]] = None) -> Optional[Dict]:
68
- """
69
- Try to map a free-text biomedical entity name to an ontology concept.
70
- Useful for linking PubMed entities, ChEMBL molecules, clinical trials to ontology terms.
71
- """
72
- matches = search_terms(entity_name, ontologies=preferred_ontologies, max_results=1)
73
- return matches[0] if matches else None
 
 
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