Skip to content

Commit

Permalink
more functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hesspnnl committed Dec 27, 2024
1 parent e323463 commit 05369fc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
31 changes: 31 additions & 0 deletions nmdc_notebook_tools/biosample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
17 changes: 17 additions & 0 deletions nmdc_notebook_tools/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 05369fc

Please sign in to comment.