Skip to content
Merged
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
4 changes: 2 additions & 2 deletions qiita_db/metadata_template/base_metadata_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1158,8 +1158,8 @@ def to_file(self, fp, samples=None):
df.to_csv(fp, index_label='sample_name', na_rep="", sep='\t',
encoding='utf-8')

def to_dataframe(self):
"""Returns the metadata template as a dataframe
def _common_to_dataframe_steps(self):
"""Perform the common to_dataframe steps

Returns
-------
Expand Down
17 changes: 17 additions & 0 deletions qiita_db/metadata_template/prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,3 +767,20 @@ def name(self, value):
WHERE prep_template_id = %s"""
qdb.sql_connection.TRN.add(sql, [value, self.id])
qdb.sql_connection.TRN.execute()

def to_dataframe(self, add_ebi_accessions=False):
"""Returns the metadata template as a dataframe

Parameters
----------
add_ebi_accessions : bool, optional
If this should add the ebi accessions
"""
df = self._common_to_dataframe_steps()

if add_ebi_accessions:
accessions = self.ebi_experiment_accessions
df['qiita_ebi_experiment_accessions'] = df.index.map(
lambda sid: accessions[sid])

return df
17 changes: 17 additions & 0 deletions qiita_db/metadata_template/sample_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,20 @@ def biosample_accessions(self, value):
If a sample in `value` already has an accession number
"""
self._update_accession_numbers('biosample_accession', value)

def to_dataframe(self, add_ebi_accessions=False):
"""Returns the metadata template as a dataframe

Parameters
----------
add_ebi_accessions : bool, optional
If this should add the ebi accessions
"""
df = self._common_to_dataframe_steps()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about:

accessions = None
if add_ebi_accessions:
    accessions = self.ebi_sample_accessions

return self._common_to_dataframe_steps(accessions, 'ebi_sample_accessions')

...similar for the prep template to_dataframe, and then add into the common function:

if accessions is not None:
    df[f'qiita_{accession_type}'] = df.index.map(lambda sid: accessions[sid])

Would reduce a little of code duplication

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not strong feeling about leaving as is or changing ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool cool, then :shipit: i think :)


if add_ebi_accessions:
accessions = self.ebi_sample_accessions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all samples assured to be represented in this object? If not, would it be safer to have the lambda issue a accessions.get?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, AFAIK all samples in the prep/sample are represented.

df['qiita_ebi_sample_accessions'] = df.index.map(
lambda sid: accessions[sid])

return df
6 changes: 6 additions & 0 deletions qiita_db/metadata_template/test/test_prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,12 @@ def test_to_dataframe(self):
u'illumina_technology', u'sample_center', u'pcr_primers',
u'study_center', 'qiita_prep_id'})

# test with add_ebi_accessions as True
obs = self.tester.to_dataframe(True)
self.assertEqual(
self.tester.ebi_experiment_accessions,
obs.qiita_ebi_experiment_accessions.to_dict())

def test_clean_validate_template_error_bad_chars(self):
"""Raises an error if there are invalid characters in the sample names
"""
Expand Down
6 changes: 6 additions & 0 deletions qiita_db/metadata_template/test/test_sample_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,12 @@ def test_to_dataframe(self):
'anonymized_name', 'tot_org_carb', 'description_duplicate',
'env_feature', 'scientific_name', 'qiita_study_id'})

# test with add_ebi_accessions as True
obs = self.tester.to_dataframe(True)
self.assertEqual(
self.tester.ebi_sample_accessions,
obs.qiita_ebi_sample_accessions.to_dict())

def test_check_restrictions(self):
obs = self.tester.check_restrictions(
[STC['EBI']])
Expand Down