mgbam commited on
Commit
d24e0a6
·
verified ·
1 Parent(s): 94b3916

Update genesis/api_clients/bioportal_api.py

Browse files
Files changed (1) hide show
  1. genesis/api_clients/bioportal_api.py +51 -54
genesis/api_clients/bioportal_api.py CHANGED
@@ -1,81 +1,78 @@
1
  # genesis/api_clients/bioportal_api.py
2
- import os
3
  import requests
 
4
  from urllib.parse import quote
5
 
6
- BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
7
  BIOPORTAL_BASE_URL = "https://data.bioontology.org"
8
 
9
  if not BIOPORTAL_API_KEY:
10
- raise ValueError("Missing BIOPORTAL_API_KEY in environment variables")
11
-
12
 
13
- def _bioportal_get(endpoint: str, params: dict = None):
14
- """
15
- Generic BioPortal GET request helper.
16
- """
17
- headers = {"Authorization": f"apikey token={BIOPORTAL_API_KEY}"}
18
- url = f"{BIOPORTAL_BASE_URL}{endpoint}"
19
- res = requests.get(url, headers=headers, params=params)
20
- res.raise_for_status()
21
- return res.json()
22
 
23
 
24
- def search_terms(query: str, ontologies: list = None, pagesize: int = 10):
25
  """
26
- Search for biomedical terms in BioPortal.
 
 
 
 
 
27
  """
28
  params = {
29
- "q": query,
30
- "pagesize": pagesize
31
  }
32
- if ontologies:
33
- params["ontologies"] = ",".join(ontologies)
34
- return _bioportal_get("/search", params)
35
 
 
 
 
 
36
 
37
- def get_term_details(ontology_acronym: str, term_id: str):
38
- """
39
- Retrieve details for a specific ontology term.
40
- """
41
- encoded_term_id = quote(term_id, safe="")
42
- return _bioportal_get(f"/ontologies/{ontology_acronym}/classes/{encoded_term_id}")
43
 
44
-
45
- def map_to_standard_ontology(term: str, ontologies: list = None):
46
  """
47
- Map a free-text biomedical term to standardized ontology concepts.
48
  """
49
- search_results = search_terms(term, ontologies=ontologies, pagesize=1)
50
- if search_results.get("collection"):
51
- top_hit = search_results["collection"][0]
52
- return {
53
- "label": top_hit.get("prefLabel"),
54
- "ontology": top_hit.get("links", {}).get("ontology"),
55
- "definition": top_hit.get("definition"),
56
- "synonyms": top_hit.get("synonym", []),
57
- "cui": top_hit.get("@id")
58
- }
59
- return None
60
 
61
 
62
- def search_disease_to_gene(disease_term: str):
63
  """
64
- Example: find genes linked to a disease using ontology mappings.
65
  """
66
- # First, normalize the disease term
67
- mapped = map_to_standard_ontology(disease_term, ontologies=["DOID", "MONDO"])
68
- if not mapped:
69
- return None
70
-
71
- # For demo, this function could be extended to query
72
- # linked datasets or internal Neo4j graph for related genes
73
- return mapped
74
 
75
 
76
- def search_pathway_for_disease(disease_term: str):
77
  """
78
- Example: find pathways linked to a disease term.
79
  """
80
- mapped = map_to_standard_ontology(disease_term, ontologies=["PW", "Reactome"])
81
- return mapped
 
 
 
 
 
 
 
 
 
 
 
1
  # genesis/api_clients/bioportal_api.py
 
2
  import requests
3
+ import os
4
  from urllib.parse import quote
5
 
6
+ BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY") # Store this in Hugging Face secrets
7
  BIOPORTAL_BASE_URL = "https://data.bioontology.org"
8
 
9
  if not BIOPORTAL_API_KEY:
10
+ raise ValueError("BIOPORTAL_API_KEY not found in environment variables.")
 
11
 
12
+ HEADERS = {
13
+ "Authorization": f"apikey token={BIOPORTAL_API_KEY}"
14
+ }
 
 
 
 
 
 
15
 
16
 
17
+ def search_concept(term: str, ontology: str = None, max_results: int = 10):
18
  """
19
+ Search for a biomedical concept across BioPortal ontologies.
20
+
21
+ Args:
22
+ term (str): The search term (e.g., "BRCA1", "glioblastoma").
23
+ ontology (str): Optional ontology acronym (e.g., "NCIT", "SNOMEDCT").
24
+ max_results (int): Maximum number of results.
25
  """
26
  params = {
27
+ "q": term,
28
+ "pagesize": max_results
29
  }
30
+ if ontology:
31
+ params["ontology"] = ontology
 
32
 
33
+ url = f"{BIOPORTAL_BASE_URL}/search"
34
+ response = requests.get(url, headers=HEADERS, params=params)
35
+ response.raise_for_status()
36
+ return response.json().get("collection", [])
37
 
 
 
 
 
 
 
38
 
39
+ def get_concept_details(ontology_acronym: str, concept_id: str):
 
40
  """
41
+ Fetch detailed information about a concept given ontology acronym and concept ID.
42
  """
43
+ encoded_id = quote(concept_id, safe="")
44
+ url = f"{BIOPORTAL_BASE_URL}/ontologies/{ontology_acronym}/classes/{encoded_id}"
45
+ response = requests.get(url, headers=HEADERS)
46
+ response.raise_for_status()
47
+ return response.json()
 
 
 
 
 
 
48
 
49
 
50
+ def get_ontologies():
51
  """
52
+ List all available ontologies in BioPortal.
53
  """
54
+ url = f"{BIOPORTAL_BASE_URL}/ontologies"
55
+ response = requests.get(url, headers=HEADERS)
56
+ response.raise_for_status()
57
+ return [
58
+ {"acronym": o.get("acronym"), "name": o.get("name"), "description": o.get("description")}
59
+ for o in response.json()
60
+ ]
 
61
 
62
 
63
+ def map_term_across_ontologies(term: str, ontologies: list = None):
64
  """
65
+ Map a term across multiple ontologies to unify synonyms and cross-references.
66
  """
67
+ mappings = {}
68
+ target_ontologies = ontologies or [o["acronym"] for o in get_ontologies()]
69
+
70
+ for ont in target_ontologies:
71
+ results = search_concept(term, ontology=ont, max_results=1)
72
+ if results:
73
+ mappings[ont] = {
74
+ "label": results[0].get("prefLabel"),
75
+ "uri": results[0].get("@id"),
76
+ "synonyms": results[0].get("synonym", []),
77
+ }
78
+ return mappings