diff --git a/sample_annotator/clients/gold_client.py b/sample_annotator/clients/gold_client.py index ac600b3..952b485 100644 --- a/sample_annotator/clients/gold_client.py +++ b/sample_annotator/clients/gold_client.py @@ -251,6 +251,68 @@ def fetch_studies_from_file(self, path: str, **kwargs) -> List[StudyDict]: ids.append(line.strip()) return self.fetch_studies(ids, **kwargs) + def fetch_biosample_by_project(self, id: str) -> List[SampleDict]: + """Fetch the biosample from which the sequencing project + was generated. + + :param id: GOLD project id. Ex.: Gp0503330 + :return: List of SampleDict objects + """ + id = self._normalize_id(id) + results = self._call("biosamples", {"projectGoldId": id}) + + biosample_id = results[0]["biosampleGoldId"] + return biosample_id + + def fetch_study_by_project(self, id: str) -> List[SampleDict]: + """Fetch the study for which the sequencing project + was performed. + + :param id: GOLD project id. Ex.: Gp0503330 + :return: List of SampleDict objects + """ + id = self._normalize_id(id) + results = self._call("studies", {"projectGoldId": id}) + + study_id = results[0]["studyGoldId"] + return study_id + + def fetch_study_by_analysis_id(self, id: str) -> List[SampleDict]: + """Fetch the study id for which the informatics processing + of a sequencing project was performed. + :param id: GOLD Analysis id. Ex.: Ga0466468 + :return: List of SampleDict objects + """ + id = self._normalize_id(id) + results = self._call("studies", {"apGoldId": id}) + + study_id = results[0]["studyGoldId"] + return study_id + + def fetch_biosample_by_analysis_id(self, id: str) -> List[SampleDict]: + """Fetch the biosample id for which the informatics processing + of a sequencing project was performed. + :param id: GOLD Analysis id. Ex.: Ga0466468 + :return: List of SampleDict objects + """ + id = self._normalize_id(id) + results = self._call("biosamples", {"apGoldId": id}) + + biosample_id = results[0]["biosampleGoldId"] + return biosample_id + + def fetch_project_by_analysis_id(self, id: str) -> List[SampleDict]: + """Fetch the project id for which the informatics processing + of a sequencing project was performed. + :param id: GOLD Analysis id. Ex.: Ga0466468 + :return: List of SampleDict objects + """ + id = self._normalize_id(id) + results = self._call("projects", {"apGoldId": id}) + + project_id = results[0]["projectGoldId"] + return project_id + @click.group() @click.option("-v", "--verbose", count=True) diff --git a/tests/test_gold.py b/tests/test_gold.py index b7b8e6a..a5ccb12 100644 --- a/tests/test_gold.py +++ b/tests/test_gold.py @@ -21,6 +21,8 @@ 'Gb0011929', 'Gb0051032' ## sample with no study ] +TEST_PROJECT_ID = 'Gp0503317' +TEST_ANALYSIS_ID = 'Ga0451502' #logging.basicConfig(level=logging.DEBUG) @@ -88,3 +90,51 @@ def test_get_biosamples(self): else: print(f'Skipping sample tests') print(f'To enable these, add your apikey to {KEYPATH}') + + def test_fetches_by_project(self): + """Tests for all methods in the library that seek to fetch + biosample and study information from gold database based on + supplied project ids. + """ + gc = GoldClient() + gc.clear_cache() + + if os.path.exists(KEYPATH): + gc.load_key(KEYPATH) + + expected_biosample_id = 'Gb0258249' + actual_biosample_id = gc.fetch_biosample_by_project(TEST_PROJECT_ID) + + self.assertEqual(expected_biosample_id, actual_biosample_id) + + expected_study_id = 'Gs0149396' + actual_study_id = gc.fetch_study_by_project(TEST_PROJECT_ID) + + self.assertEqual(expected_study_id, actual_study_id) + + def test_fetches_by_analysis(self): + """Tests for all methods in the library that seek to fetch + biosample, study and project information from gold database based + on supplied project analysis ids. + """ + gc = GoldClient() + gc.clear_cache() + + if os.path.exists(KEYPATH): + gc.load_key(KEYPATH) + + expected_biosample_id = 'Gb0258249' + actual_biosample_id = gc.fetch_biosample_by_analysis_id(TEST_ANALYSIS_ID) + + self.assertEqual(expected_biosample_id, actual_biosample_id) + + expected_study_id = 'Gs0149396' + actual_study_id = gc.fetch_study_by_analysis_id(TEST_ANALYSIS_ID) + + self.assertEqual(expected_study_id, actual_study_id) + + expected_project_id = TEST_PROJECT_ID + actual_project_id = gc.fetch_project_by_analysis_id(TEST_ANALYSIS_ID) + + self.assertEqual(expected_project_id, actual_project_id) + \ No newline at end of file