Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add gold client methods that use GOLD project and analysis ids #102

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions sample_annotator/clients/gold_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions tests/test_gold.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
'Gb0011929',
'Gb0051032' ## sample with no study
]
TEST_PROJECT_ID = 'Gp0503317'
TEST_ANALYSIS_ID = 'Ga0451502'

#logging.basicConfig(level=logging.DEBUG)

Expand Down Expand Up @@ -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)