Skip to content

Commit a5d5d55

Browse files
author
brynnz22
committed
add envo term api capabiliites and doct strings
1 parent debd353 commit a5d5d55

5 files changed

Lines changed: 236 additions & 71 deletions

File tree

support_code/nmdc/nom/api_info_retriever.py

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import requests
22
from typing import Optional
3+
import yaml
34

45
# TODO: Update og_url to be regular nmdc api url when berkeley is implemented
56

6-
class ApiInfoRetriever:
7+
class NmdcApiInfoRetriever:
78
def __init__(self, collection_name: str):
89
self.collection_name = collection_name
910

1011
def get_id_by_slot_from_collection(self, slot_name: str, slot_field_value: str):
1112
"""
12-
Retrieve the identifier from a specified collection based on a slot name and field value.
13+
Retrieve the NMDC identifier from a specified collection based on a slot name and field value.
1314
1415
Parameters
1516
----------
@@ -51,3 +52,73 @@ def get_id_by_slot_from_collection(self, slot_name: str, slot_field_value: str):
5152

5253
return identifier
5354

55+
class BioOntologyInfoRetriever:
56+
"""
57+
Client for retrieving ENVO term information from BioPortal API.
58+
59+
A class to handle authentication and retrieval of Environmental Ontology (ENVO)
60+
terms using the BioPortal REST API service.
61+
62+
Parameters
63+
----------
64+
config_path : str
65+
Path to YAML configuration file containing BioPortal API credentials
66+
67+
Notes
68+
-----
69+
The configuration file should contain an 'api_key' field with a valid
70+
BioPortal API key.
71+
72+
Examples
73+
--------
74+
>>> retriever = BioOntologyInfoRetriever('config.yaml')
75+
>>> envo_terms = retriever.get_envo_terms('ENVO:00002042')
76+
>>> print(envo_terms)
77+
{'ENVO:00002042': 'surface water'}
78+
"""
79+
def __init__(self, config_path: str):
80+
self.config = config_path
81+
82+
def get_envo_terms(self, envo_id: dict):
83+
"""
84+
Look up an ENVO term label using BioPortal API.
85+
86+
Parameters
87+
----------
88+
envo_id : str
89+
The ENVO identifier to look up (e.g., 'ENVO:00002042')
90+
91+
Returns
92+
-------
93+
dict
94+
Dictionary with envo_id as key and term label as value
95+
Example: {'ENVO:00002042': 'surface water'}
96+
97+
Raises
98+
------
99+
requests.exceptions.RequestException
100+
If the API request fails
101+
KeyError
102+
If the response doesn't contain expected data format
103+
yaml.YAMLError
104+
If the config file cannot be parsed
105+
FileNotFoundError
106+
If the config file is not found
107+
108+
Notes
109+
-----
110+
Makes an authenticated request to BioPortal API to retrieve the
111+
preferred label (prefLabel) for the given ENVO term.
112+
"""
113+
114+
config = yaml.safe_load(open(self.config))
115+
api_key = config['api_key']
116+
117+
url = f"http://data.bioontology.org/ontologies/ENVO/classes/{envo_id}"
118+
headers = {"Authorization": f"apikey token={api_key}"}
119+
120+
response = requests.get(url, headers=headers)
121+
response.raise_for_status()
122+
123+
data = response.json()
124+
return {envo_id: data['prefLabel']}

support_code/nmdc/nom/main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ def main():
3939
- 12
4040
- 15
4141
- 21
42-
- minting_client_config_path: str
43-
The path to the NMDC minting client configuration file. Defaults to 'enviroMS/nmdc_metadata_generation/config.yaml'.
42+
- config_path: str
43+
The path to the NMDC minting client and Bioportal API key configuration yaml file. Defaults to 'enviroMS/nmdc_metadata_generation/config.yaml'.
44+
Should have client_id and client_secret for the NMDC minting client. And api_key for BioPortal API access. To get a BioPortal api_key register here:
45+
https://bioportal.bioontology.org/accounts/new and go to settings
4446
4547
Notes
4648
-----
@@ -66,7 +68,7 @@ def main():
6668
execution_resource=config_data['execution_resource'],
6769
field_strength=config_data['field_strength'],
6870
workflow_version=config_data['workflow_version'],
69-
minting_client_config_path=config_data['minting_client_config_path']
71+
config_path=config_data['minting_client_config_path']
7072
)
7173

7274
generator.run()

0 commit comments

Comments
 (0)