diff --git a/nmdc_notebook_tools/biosample.py b/nmdc_notebook_tools/biosample.py index 27ca078..6111c7a 100644 --- a/nmdc_notebook_tools/biosample.py +++ b/nmdc_notebook_tools/biosample.py @@ -12,6 +12,7 @@ def __init__(self): def get_all_biosamples(self, page_size=25) -> List[Dict]: """ + TODO Get all biosamples from the NMDC API. params: page_size: int @@ -66,6 +67,36 @@ def find_biosample_by_filter(self, filter: str, page_size=25) -> Dict: results = response.json() return results + def find_biosample_by_attribute( + self, attribute_name, attribute_value, page_size=25 + ) -> List[Dict]: + """ + Get a biosample from the NMDC API by its name. Biosamples can be filtered based on their attributes found https://microbiomedata.github.io/nmdc-schema/Biosample/. + params: + sample_name: str + The name of the biosample to query. + attribute_name: str + The name of the attribute to filter by. + attribute_value: str + The value of the attribute to filter by. + page_size: int + The number of results to return per page. Default is 25. + """ + api_client = NMDClient() + filter = f"{attribute_name}.search:{attribute_value}" + + # Encode the filter for use in the URL + encoded_filter = urllib.parse.quote(filter) + + url = f"{api_client.base_url}/biosamples?filter={encoded_filter}&per_page={page_size}" + # get the reponse + response = requests.get(url) + # check it came back with OK + if response.status_code != 200: + return (response.status_code, "There was an error.") + results = response.json()["results"] + return results + if __name__ == "__main__": pass diff --git a/nmdc_notebook_tools/study.py b/nmdc_notebook_tools/study.py index 80ddfa3..f497280 100644 --- a/nmdc_notebook_tools/study.py +++ b/nmdc_notebook_tools/study.py @@ -104,6 +104,23 @@ def find_study_by_pi(self, pi_name: str, page_size=25) -> List[Dict]: results = response.json()["results"] return results + def get_study_data_objects(self, study_id: str) -> List[Dict]: + """ + Get the data objects associated with a study. + params: + study_id: str + The id of the study to query. + """ + api_client = NMDClient() + url = f"{api_client.base_url}/data_objects/studies/{study_id}" + # get the reponse + response = requests.get(url) + # check it came back with OK + if response.status_code != 200: + return (response.status_code, "There was an error.") + results = response.json()["results"] + return results + if __name__ == "__main__": pass