Skip to content

Commit

Permalink
fix #2751 (#3062)
Browse files Browse the repository at this point in the history
  • Loading branch information
antgonza authored Jan 7, 2021
1 parent cffb35f commit 8fb9ca0
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 76 deletions.
2 changes: 1 addition & 1 deletion qiita_db/meta_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def generate_biom_and_metadata_release(study_status='public'):
continue
fp = relpath(x['fp'], bdir)
for pt in a.prep_templates:
categories = pt.categories()
categories = pt.categories
platform = ''
target_gene = ''
if 'platform' in categories:
Expand Down
66 changes: 29 additions & 37 deletions qiita_db/metadata_template/base_metadata_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@
QIITA_COLUMN_NAME = 'qiita_sample_column_names'


def _helper_get_categories(table):
"""This is a helper function to avoid duplication of code"""
with qdb.sql_connection.TRN:
sql = """SELECT sample_values->>'columns'
FROM qiita.{0}
WHERE sample_id = '{1}'""".format(table, QIITA_COLUMN_NAME)
qdb.sql_connection.TRN.add(sql)
results = qdb.sql_connection.TRN.execute_fetchflatten()
if results:
results = sorted(loads(results[0]))
return results


class BaseSample(qdb.base.QiitaObject):
r"""Sample object that accesses the db to get the information of a sample
belonging to a PrepTemplate or a SampleTemplate.
Expand Down Expand Up @@ -184,17 +197,7 @@ def _get_categories(self):
set of str
The set of all available metadata categories
"""
with qdb.sql_connection.TRN:
sql = """SELECT sample_values->>'columns'
FROM qiita.{0}
WHERE sample_id = '{1}'""".format(
self._dynamic_table, QIITA_COLUMN_NAME)
qdb.sql_connection.TRN.add(sql)
results = qdb.sql_connection.TRN.execute_fetchflatten()
if results:
results = loads(results[0])

return set(results)
return set(_helper_get_categories(self._dynamic_table))

def _to_dict(self):
r"""Returns the categories and their values in a dictionary
Expand Down Expand Up @@ -729,7 +732,7 @@ def delete_column(self, column_name):
If the column_name is selected as a specimen_id_column in the
study.
"""
if column_name not in self.categories():
if column_name not in self.categories:
raise qdb.exceptions.QiitaDBColumnError(
"'%s' not in info file %d" % (column_name, self._id))
if not self.can_be_updated(columns={column_name}):
Expand All @@ -754,7 +757,7 @@ def delete_column(self, column_name):
qdb.sql_connection.TRN.add(sql, [column_name, QIITA_COLUMN_NAME])

# deleting from QIITA_COLUMN_NAME
columns = self.categories()
columns = self.categories
columns.remove(column_name)
values = '{"columns": %s}' % dumps(columns)
sql = """UPDATE {0}
Expand Down Expand Up @@ -834,7 +837,7 @@ def _common_extend_steps(self, md_template):

# Check if we are adding new columns
headers = md_template.keys().tolist()
new_cols = set(headers).difference(self.categories())
new_cols = set(headers).difference(self.categories)

if not new_cols and not new_samples:
return None, None
Expand All @@ -855,7 +858,7 @@ def _common_extend_steps(self, md_template):
# code). Sorting the new columns to enforce an order
new_cols = sorted(new_cols)

cols = self.categories()
cols = self.categories
cols.extend(new_cols)

values = dumps({"columns": cols})
Expand Down Expand Up @@ -1167,7 +1170,7 @@ def _common_to_dataframe_steps(self):
"""
with qdb.sql_connection.TRN:
# Retrieve all the information from the database
cols = self.categories()
cols = self.categories
sql = """SELECT sample_id, sample_values
FROM qiita.{0}
WHERE sample_id != '{1}'""".format(
Expand Down Expand Up @@ -1237,6 +1240,7 @@ def get_filepaths(self):
self._filepath_table, self._id_column, self.id,
sort='descending')]

@property
def categories(self):
"""Identifies the metadata columns present in an info file
Expand All @@ -1245,19 +1249,7 @@ def categories(self):
cols : list
The category fields
"""
with qdb.sql_connection.TRN:
sql = """SELECT sample_values->>'columns'
FROM qiita.{0}
WHERE sample_id = '{1}'""".format(
self._table_name(self._id), QIITA_COLUMN_NAME)

qdb.sql_connection.TRN.add(sql)

results = qdb.sql_connection.TRN.execute_fetchflatten()
if results:
results = sorted(loads(results[0]))

return results
return _helper_get_categories(self._table_name(self._id))

def extend(self, md_template):
"""Adds the given template to the current one
Expand All @@ -1269,7 +1261,7 @@ def extend(self, md_template):
"""
with qdb.sql_connection.TRN:
md_template = self._clean_validate_template(
md_template, self.study_id, current_columns=self.categories())
md_template, self.study_id, current_columns=self.categories)
new_samples, new_columns = self._common_extend_steps(md_template)
if new_samples or new_columns:
self.validate(self.columns_restrictions)
Expand Down Expand Up @@ -1396,7 +1388,7 @@ def _update(self, md_template):
self._table_name(self._id))
qdb.sql_connection.TRN.add(sql, [dumps(values), sid])

nc = list(set(new_columns).union(set(self.categories())))
nc = list(set(new_columns).union(set(self.categories)))
table_name = self._table_name(self.id)
values = dumps({"columns": nc})
sql = """UPDATE qiita.{0}
Expand Down Expand Up @@ -1430,7 +1422,7 @@ def update(self, md_template):
with qdb.sql_connection.TRN:
# Clean and validate the metadata template given
new_map = self._clean_validate_template(
md_template, self.study_id, current_columns=self.categories())
md_template, self.study_id, current_columns=self.categories)
samples, columns = self._update(new_map)
self.validate(self.columns_restrictions)
self.generate_files(samples, columns)
Expand All @@ -1450,7 +1442,7 @@ def extend_and_update(self, md_template):
"""
with qdb.sql_connection.TRN:
md_template = self._clean_validate_template(
md_template, self.study_id, current_columns=self.categories())
md_template, self.study_id, current_columns=self.categories)
new_samples, new_columns = self._common_extend_steps(md_template)
samples, columns = self._update(md_template)
if samples is None:
Expand Down Expand Up @@ -1516,7 +1508,7 @@ def get_category(self, category):
If category is not part of the template
"""
with qdb.sql_connection.TRN:
if category not in self.categories():
if category not in self.categories:
raise qdb.exceptions.QiitaDBColumnError(category)
sql = """SELECT sample_id,
COALESCE(sample_values->>'{0}', 'None') AS {0}
Expand All @@ -1542,7 +1534,7 @@ def check_restrictions(self, restrictions):
cols = {col for restriction in restrictions
for col in restriction.columns}

return cols.difference(self.categories())
return cols.difference(self.categories)

def _get_accession_numbers(self, column):
"""Return the accession numbers stored in `column`
Expand Down Expand Up @@ -1639,7 +1631,7 @@ def validate(self, restriction_dict):
If the values aren't castable
"""
warning_msg = []
columns = self.categories()
columns = self.categories
wrong_msg = 'Sample "%s", column "%s", wrong value "%s"'
for label, restriction in restriction_dict.items():
missing = set(restriction.columns).difference(columns)
Expand Down Expand Up @@ -1796,7 +1788,7 @@ def validate_restrictions(self):
success = True
message = []
restrictions = self.restrictions
categories = self.categories()
categories = self.categories

difference = sorted(set(restrictions.keys()) - set(categories))
if difference:
Expand Down
4 changes: 2 additions & 2 deletions qiita_db/metadata_template/prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def create(cls, md_template, study, data_type, investigation_type=None,

md_template = cls._clean_validate_template(md_template, study.id)
_check_duplicated_columns(list(md_template.columns),
study.sample_template.categories())
study.sample_template.categories)

# Insert the metadata template
sql = """INSERT INTO qiita.prep_template
Expand Down Expand Up @@ -394,7 +394,7 @@ def can_be_extended(self, new_samples, new_columns):
"prep template")

_check_duplicated_columns(list(new_columns), qdb.study.Study(
self.study_id).sample_template.categories())
self.study_id).sample_template.categories)

return True, ""

Expand Down
6 changes: 3 additions & 3 deletions qiita_db/metadata_template/test/test_prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ def _common_creation_checks(self, pt, fp_count, name):
'instrument_model', 'experiment_design_description',
'library_construction_protocol', 'center_name',
'center_project_name', 'emp_status'}
self.assertCountEqual(pt.categories(), exp_categories)
self.assertCountEqual(pt.categories, exp_categories)
exp_dict = {
'%s.SKB7.640196' % self.test_study.id: {
'barcode': 'CCTCTGAGAGCT',
Expand Down Expand Up @@ -1058,7 +1058,7 @@ def test_create_warning(self):
'instrument_model', 'experiment_design_description',
'library_construction_protocol', 'center_name',
'center_project_name', 'emp_status'}
self.assertCountEqual(pt.categories(), exp_categories)
self.assertCountEqual(pt.categories, exp_categories)
exp_dict = {
'%s.SKB7.640196' % self.test_study.id: {
'ebi_submission_accession': None,
Expand Down Expand Up @@ -1628,7 +1628,7 @@ def test_delete_column(self):
pt = qdb.metadata_template.prep_template.PrepTemplate.create(
self.metadata, self.test_study, self.data_type)
pt.delete_column('str_column')
self.assertNotIn('str_column', pt.categories())
self.assertNotIn('str_column', pt.categories)

# testing errors
pt = qdb.metadata_template.prep_template.PrepTemplate(1)
Expand Down
22 changes: 11 additions & 11 deletions qiita_db/metadata_template/test/test_sample_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ def test_categories(self):
'physical_specimen_remaining', 'dna_extracted',
'sample_type', 'collection_timestamp', 'host_subject_id',
'description', 'latitude', 'longitude', 'scientific_name'}
obs = set(self.tester.categories())
obs = set(self.tester.categories)
self.assertCountEqual(obs, exp)

def test_iter(self):
Expand Down Expand Up @@ -974,7 +974,7 @@ def test_create(self):
'longitude', 'physical_specimen_location',
'physical_specimen_remaining', 'sample_type',
'scientific_name', 'taxon_id'}
self.assertCountEqual(st.categories(), exp_categories)
self.assertCountEqual(st.categories, exp_categories)
exp_dict = {
"%s.Sample1" % new_id: {
'collection_timestamp': '2014-05-29 12:24:15',
Expand Down Expand Up @@ -1040,7 +1040,7 @@ def test_create_int_prefix(self):
'longitude', 'physical_specimen_location',
'physical_specimen_remaining', 'sample_type',
'scientific_name', 'taxon_id'}
self.assertCountEqual(st.categories(), exp_categories)
self.assertCountEqual(st.categories, exp_categories)
exp_dict = {
"%s.12.Sample1" % new_id: {
'collection_timestamp': '2014-05-29 12:24:15',
Expand Down Expand Up @@ -1106,7 +1106,7 @@ def test_create_str_prefixes(self):
'longitude', 'physical_specimen_location',
'physical_specimen_remaining', 'sample_type',
'scientific_name', 'taxon_id'}
self.assertCountEqual(st.categories(), exp_categories)
self.assertCountEqual(st.categories, exp_categories)
exp_dict = {
"%s.foo.Sample1" % new_id: {
'collection_timestamp': '2014-05-29 12:24:15',
Expand Down Expand Up @@ -1172,7 +1172,7 @@ def test_create_already_prefixed_samples(self):
'longitude', 'physical_specimen_location',
'physical_specimen_remaining', 'sample_type',
'scientific_name', 'taxon_id'}
self.assertCountEqual(st.categories(), exp_categories)
self.assertCountEqual(st.categories, exp_categories)
exp_dict = {
"%s.Sample1" % new_id: {
'collection_timestamp': '2014-05-29 12:24:15',
Expand Down Expand Up @@ -1463,7 +1463,7 @@ def test_extend_add_samples(self):
'longitude', 'physical_specimen_location',
'physical_specimen_remaining', 'sample_type',
'scientific_name', 'taxon_id'}
self.assertCountEqual(st.categories(), exp_categories)
self.assertCountEqual(st.categories, exp_categories)
exp_dict = {
"%s.Sample1" % st.id: {
'collection_timestamp': '2014-05-29 12:24:15',
Expand Down Expand Up @@ -1568,7 +1568,7 @@ def test_extend_add_duplicate_samples(self):
'longitude', 'physical_specimen_location',
'physical_specimen_remaining', 'sample_type',
'scientific_name', 'taxon_id'}
self.assertCountEqual(st.categories(), exp_categories)
self.assertCountEqual(st.categories, exp_categories)
exp_dict = {
"%s.Sample1" % st.id: {
'collection_timestamp': '2014-05-29 12:24:15',
Expand Down Expand Up @@ -1650,7 +1650,7 @@ def test_extend_new_columns(self):
'physical_specimen_remaining', 'sample_type',
'scientific_name', 'taxon_id',
'texture', 'tot_nitro'}
self.assertCountEqual(st.categories(), exp_categories)
self.assertCountEqual(st.categories, exp_categories)
exp_dict = {
"%s.Sample1" % st.id: {
'collection_timestamp': '2014-05-29 12:24:15',
Expand Down Expand Up @@ -1736,7 +1736,7 @@ def test_extend_new_samples_and_columns(self):
'longitude', 'physical_specimen_location',
'physical_specimen_remaining', 'sample_type',
'scientific_name', 'taxon_id', 'tot_nitro'}
self.assertCountEqual(st.categories(), exp_categories)
self.assertCountEqual(st.categories, exp_categories)
exp_dict = {
"%s.Sample1" % st.id: {
'collection_timestamp': '2014-05-29 12:24:15',
Expand Down Expand Up @@ -1835,7 +1835,7 @@ def test_extend_update(self):
'longitude', 'physical_specimen_location',
'physical_specimen_remaining', 'sample_type',
'scientific_name', 'taxon_id', 'tot_nitro'}
self.assertCountEqual(st.categories(), exp_categories)
self.assertCountEqual(st.categories, exp_categories)
exp_dict = {
"%s.Sample1" % st.id: {
'collection_timestamp': '2014-05-29 12:24:15',
Expand Down Expand Up @@ -2247,7 +2247,7 @@ def test_delete_column(self):
st = qdb.metadata_template.sample_template.SampleTemplate.create(
self.metadata, self.new_study)
st.delete_column('dna_extracted')
self.assertNotIn('dna_extracted', st.categories())
self.assertNotIn('dna_extracted', st.categories)

def test_delete_column_specimen_id(self):
st = qdb.metadata_template.sample_template.SampleTemplate.create(
Expand Down
2 changes: 1 addition & 1 deletion qiita_db/processing_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,7 @@ def shape(self):
pass
else:
samples = len(st)
columns = len(st.categories())
columns = len(st.categories)
elif analysis_id is not None:
try:
analysis = qdb.analysis.Analysis(analysis_id)
Expand Down
2 changes: 1 addition & 1 deletion qiita_db/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ def specimen_id_column(self, value):
"sample information.")

if value is not None:
if value not in st.categories():
if value not in st.categories:
raise qdb.exceptions.QiitaDBLookupError("Category '%s' is not "
"present in the sample"
" information."
Expand Down
2 changes: 1 addition & 1 deletion qiita_db/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ def get_artifacts_information(artifact_ids, only_biom=True):
if prep_template_id is not None:
if prep_template_id not in ps:
pt = PT(prep_template_id)
categories = pt.categories()
categories = pt.categories
if 'platform' in categories:
platform = ', '.join(
set(pt.get_category('platform').values()))
Expand Down
2 changes: 1 addition & 1 deletion qiita_pet/handlers/api_proxy/prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def prep_template_ajax_get_req(user_id, prep_id):

# The call to list is needed because keys is an iterator
num_samples = len(list(pt.keys()))
num_columns = len(pt.categories())
num_columns = len(pt.categories)
investigation_type = pt.investigation_type

download_prep_id = None
Expand Down
2 changes: 1 addition & 1 deletion qiita_pet/handlers/api_proxy/sample_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def sample_template_meta_cats_get_req(samp_id, user_id):

return {'status': 'success',
'message': '',
'categories': sorted(SampleTemplate(int(samp_id)).categories())
'categories': sorted(SampleTemplate(int(samp_id)).categories)
}


Expand Down
2 changes: 1 addition & 1 deletion qiita_pet/handlers/api_proxy/tests/test_prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ def test_prep_template_patch_req(self):
exp = {'status': 'success', 'message': '', 'row_id': '10'}
self.assertEqual(obs, exp)
self._wait_for_parallel_job('prep_template_%s' % pt.id)
self.assertNotIn('target_subfragment', pt.categories())
self.assertNotIn('target_subfragment', pt.categories)

# Change the name of the prep template
obs = prep_template_patch_req(
Expand Down
Loading

0 comments on commit 8fb9ca0

Please sign in to comment.