Skip to content

Commit

Permalink
rm specimen_id
Browse files Browse the repository at this point in the history
  • Loading branch information
antgonza committed Mar 17, 2022
1 parent fb0dd26 commit bae2956
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 245 deletions.
10 changes: 0 additions & 10 deletions qiita_db/metadata_template/base_metadata_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,6 @@ def delete_column(self, column_name):
If the `column_name` doesn't exist
QiitaDBOperationNotPermittedError
If a the info file can't be updated
If the column_name is selected as a specimen_id_column in the
study.
"""
if column_name not in self.categories:
raise qdb.exceptions.QiitaDBColumnError(
Expand All @@ -736,14 +734,6 @@ def delete_column(self, column_name):
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
'%s cannot be deleted' % column_name)

# if a tube identifier column is selected disallow its deletion
specimen_id_column = qdb.study.Study(self.study_id).specimen_id_column
if specimen_id_column == column_name:
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
'"%s" cannot be deleted, this column is currently selected'
' as the tube identifier (specimen_id_column)' %
column_name)

with qdb.sql_connection.TRN:
table_name = 'qiita.{0}{1}'.format(self._table_prefix, self._id)
# deleting from all samples; note that (-) in pgsql jsonb means
Expand Down
13 changes: 0 additions & 13 deletions qiita_db/metadata_template/test/test_sample_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2256,19 +2256,6 @@ def test_delete_column(self):
st.delete_column('dna_extracted')
self.assertNotIn('dna_extracted', st.categories)

def test_delete_column_specimen_id(self):
st = qdb.metadata_template.sample_template.SampleTemplate.create(
self.metadata, self.new_study)
self.new_study.specimen_id_column = 'latitude'

with self.assertRaisesRegex(
qdb.exceptions.QiitaDBOperationNotPermittedError,
'"latitude" cannot be deleted, this column is currently '
r'selected as the tube identifier \(specimen_id_column\)'):
st.delete_column('latitude')

self.new_study.specimen_id_column = None

def test_delete_samples(self):
QE = qdb.exceptions
st = qdb.metadata_template.sample_template.SampleTemplate(1)
Expand Down
57 changes: 0 additions & 57 deletions qiita_db/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class Study(qdb.base.QiitaObject):
status
title
owner
specimen_id_column
autoloaded
Methods
Expand Down Expand Up @@ -778,62 +777,6 @@ def publications(self, values):
qdb.sql_connection.TRN.add(sql, sql_args, many=True)
qdb.sql_connection.TRN.execute()

@property
def specimen_id_column(self):
"""Returns the specimen identifier column
Returns
-------
str
The name of the specimen id column
"""
with qdb.sql_connection.TRN:
sql = """SELECT specimen_id_column
FROM qiita.study
WHERE study_id = %s"""
qdb.sql_connection.TRN.add(sql, [self._id])
return qdb.sql_connection.TRN.execute_fetchlast()

@specimen_id_column.setter
def specimen_id_column(self, value):
"""Sets the specimen identifier column
Parameters
----------
value : str
The name of the column with the specimen identifiers.
Raises
------
QiitaDBLookupError
If value is not in the sample information for this study.
If the study does not have sample information.
QiitaDBColumnError
Category is not unique.
"""
st = self.sample_template
if st is None:
raise qdb.exceptions.QiitaDBLookupError("Study does not have a "
"sample information.")

if value is not None:
if value not in st.categories:
raise qdb.exceptions.QiitaDBLookupError("Category '%s' is not "
"present in the sample"
" information."
% value)

observed_values = st.get_category(value)
if len(observed_values) != len(set(observed_values.values())):
raise qdb.exceptions.QiitaDBColumnError("The category does not"
" contain unique "
"values.")

sql = """UPDATE qiita.study SET
specimen_id_column = %s
WHERE study_id = %s"""
qdb.sql_connection.perform_as_transaction(sql, [value, self._id])

@property
def investigation(self):
""" Returns Investigation this study is part of
Expand Down
5 changes: 5 additions & 0 deletions qiita_db/support_files/patches/85.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ ALTER TABLE qiita.default_workflow
FOREIGN KEY (artifact_type_id)
REFERENCES qiita.artifact_type(artifact_type_id)
ON UPDATE CASCADE;

-- Mar 17, 2022
-- deleting specimen_id_column from qiita.study

ALTER TABLE qiita.study DROP COLUMN specimen_id_column;
41 changes: 5 additions & 36 deletions qiita_db/test/test_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ def setUp(self):
"study_abstract": "Exploring how a high fat diet changes the "
"gut microbiome",
"principal_investigator_id": qdb.study.StudyPerson(3),
"lab_person_id": qdb.study.StudyPerson(1),
'specimen_id_column': None
"lab_person_id": qdb.study.StudyPerson(1)
}

self.infoexp = {
Expand All @@ -165,7 +164,6 @@ def setUp(self):
"gut microbiome",
"principal_investigator": qdb.study.StudyPerson(3),
"lab_person": qdb.study.StudyPerson(1),
'specimen_id_column': None,
'public_raw_download': False
}

Expand Down Expand Up @@ -193,8 +191,7 @@ def setUp(self):
'study_description': 'Analysis of the Cannabis Plant Microbiome',
'study_alias': 'Cannabis Soils',
'most_recent_contact': datetime(2014, 5, 19, 16, 11),
'lab_person': qdb.study.StudyPerson(1),
'specimen_id_column': None}
'lab_person': qdb.study.StudyPerson(1)}

def tearDown(self):
qiita_config.portal = self.portal
Expand Down Expand Up @@ -251,8 +248,7 @@ def test_get_info(self):
'Soils',
'ebi_submission_status': 'submitted',
'ebi_study_accession': 'EBI123456-BB',
'autoloaded': False,
'specimen_id_column': None}
'autoloaded': False}
self.assertDictEqual(obs, exp)

# Test get specific keys for single study
Expand Down Expand Up @@ -446,8 +442,7 @@ def test_create_study_min_data(self):
'study_alias': 'FCM',
'most_recent_contact': None,
'lab_person': qdb.study.StudyPerson(1),
'notes': '',
'specimen_id_column': None}
'notes': ''}
self.assertEqual(obs_info, exp)
# Check the timestamp separately, since it is set by the database
# to the microsecond, and we can't predict it a priori
Expand Down Expand Up @@ -529,8 +524,7 @@ def test_create_study_all_data(self):
'study_alias': 'FCM',
'most_recent_contact': None,
'lab_person': qdb.study.StudyPerson(1),
'notes': 'an analysis was performed \n here and \n here',
'specimen_id_column': None}
'notes': 'an analysis was performed \n here and \n here'}
self.assertEqual(obs.info, exp)
self.assertEqual(obs.shared_with, [])
self.assertEqual(obs.publications, [])
Expand Down Expand Up @@ -912,31 +906,6 @@ def test_study_tags(self):
self.assertEqual(study.tags, [])
self.assertEqual(message, '')

def test_specimen_id_column_get_set(self):
self.assertEqual(self.study.specimen_id_column, None)
self.study.specimen_id_column = 'anonymized_name'
self.assertEqual(self.study.specimen_id_column, 'anonymized_name')
self.study.specimen_id_column = None
self.assertEqual(self.study.specimen_id_column, None)

def test_specimen_id_column_not_unique(self):
with self.assertRaises(qdb.exceptions.QiitaDBColumnError):
self.study.specimen_id_column = 'dna_extracted'

def test_specimen_id_column_doesnt_exist(self):
with self.assertRaises(qdb.exceptions.QiitaDBLookupError):
self.study.specimen_id_column = 'foo'

def test_specimen_id_column_no_sample_information(self):
empty = qdb.study.Study.create(
qdb.user.User('[email protected]'), "Fried duck microbiome",
self.info)
with self.assertRaises(qdb.exceptions.QiitaDBLookupError):
empty.specimen_id_column = 'foo'

# cleaning up the created study
qdb.study.Study.delete(empty._id)


if __name__ == "__main__":
main()
8 changes: 0 additions & 8 deletions qiita_pet/handlers/api_proxy/studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,6 @@ def study_patch_request(user_id, study_id,
message = study.update_tags(User(user_id), req_value)
return {'status': 'success',
'message': message}
elif attribute == 'specimen_id_column':
try:
study.specimen_id_column = req_value
return {'status': 'success',
'message': 'Successfully updated specimen id column'}
except (QiitaDBLookupError, QiitaDBColumnError) as e:
return {'status': 'error',
'message': str(e)}
elif attribute == 'toggle_public_raw_download':
try:
study.public_raw_download = not study.public_raw_download
Expand Down
44 changes: 2 additions & 42 deletions qiita_pet/handlers/api_proxy/tests/test_studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_study_get_req(self):
'rhizospheres from the same location at different time '
'points in the plant lifecycle.'),
'status': 'private', 'spatial_series': False,
'specimen_id_column': None, 'public_raw_download': False,
'public_raw_download': False,
'study_description': (
'Analysis of the Cannabis Plant Microbiome'),
'shared_with': ['[email protected]'], 'publication_doi': [
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_study_get_req(self):
'email': '[email protected]'}, 'study_alias': 'FCM',
'study_id': new_study.id, 'notes': '',
'most_recent_contact': datetime(2015, 5, 19, 16, 11),
'ebi_study_accession': None, 'specimen_id_column': None,
'ebi_study_accession': None,
'study_title': 'Some New Study for test'}, 'message': '',
'editable': True}
self.assertCountEqual(obs, exp)
Expand Down Expand Up @@ -465,38 +465,6 @@ def test_study_patch_request_errors(self):
'path parameter'), 'status': 'error'}
self.assertEqual(obs, exp)

def test_study_patch_request_specimen_id(self):
obs = study_patch_request('[email protected]', 1,
'replace', '/specimen_id_column',
'anonymized_name')
exp = {'status': 'success', 'message': 'Successfully updated '
'specimen id column'}
self.assertEqual(obs, exp)

obs = study_patch_request('[email protected]', 1,
'replace', '/specimen_id_column',
'host_subject_id')
exp = {'status': 'success', 'message': 'Successfully updated '
'specimen id column'}
self.assertEqual(obs, exp)

qdb.study.Study(1).specimen_id_column = None

def test_study_patch_request_specimen_id_errors(self):
obs = study_patch_request('[email protected]', 1,
'replace', '/specimen_id_column',
'taxon_id')
exp = {'status': 'error', 'message': 'The category does not contain'
' unique values.'}
self.assertEqual(obs, exp)

obs = study_patch_request('[email protected]', 1,
'replace', '/specimen_id_column',
'bleep_bloop')
exp = {'status': 'error', 'message': "Category 'bleep_bloop' is not"
" present in the sample information."}
self.assertEqual(obs, exp)

def test_study_patch_request_toggle_public_raw_download(self):
study_id = 1
study = qdb.study.Study(study_id)
Expand All @@ -508,14 +476,6 @@ def test_study_patch_request_toggle_public_raw_download(self):
self.assertEqual(obs, exp)
self.assertTrue(study.public_raw_download)

obs = study_patch_request('[email protected]', study_id,
'replace', '/specimen_id_column',
'host_subject_id')
exp = {'status': 'error',
'message': 'User does not have access to study'}
self.assertEqual(obs, exp)
self.assertTrue(study.public_raw_download)

# returning to default status
study.public_raw_download = False

Expand Down
5 changes: 1 addition & 4 deletions qiita_pet/handlers/study_handlers/sample_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ def sample_template_overview_handler_get_request(study_id, user):
num_samples = 0
num_cols = 0
columns = []
specimen_id_column = None
sample_restrictions = ''
if exists:
# If it exists we need to provide:
Expand All @@ -379,7 +378,6 @@ def sample_template_overview_handler_get_request(study_id, user):
columns = st.categories
# The number of columns
num_cols = len(columns)
specimen_id_column = Study(study_id).specimen_id_column
_, sample_restrictions = st.validate_restrictions()
else:
# It doesn't exist, we also need to provide the data_types in case
Expand All @@ -396,8 +394,7 @@ def sample_template_overview_handler_get_request(study_id, user):
'num_samples': num_samples,
'num_columns': num_cols,
'columns': columns,
'sample_restrictions': sample_restrictions,
'specimen_id_column': specimen_id_column}
'sample_restrictions': sample_restrictions}


class SampleTemplateOverviewHandler(BaseHandler):
Expand Down
9 changes: 0 additions & 9 deletions qiita_pet/handlers/study_handlers/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,6 @@ def test_patch_not_allowed(self):
data=arguments, asjson=True)
self.assertEqual(obs.code, 405)

def test_patch_specimen_id_column(self):
data = {'op': 'replace', 'path': '/specimen_id_column',
'value': "anonymized_name"}
obs = self.patch('/study/1', headers=self.header,
data=data, asjson=True)
self.assertEqual(obs.code, 200)
self.assertEqual(json_decode(obs.body), {"status": "success",
"message": "Successfully updated specimen id column"})


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ def test_sample_template_overview_handler_get_request(self):
'sample_type', 'collection_timestamp',
'host_subject_id', 'description', 'latitude',
'longitude', 'scientific_name', 'env_package']),
'sample_restrictions': '',
'specimen_id_column': None}
'sample_restrictions': ''}
self.assertEqual(obs, exp)

# Test sample template doesn't exist
Expand All @@ -317,8 +316,7 @@ def test_sample_template_overview_handler_get_request(self):
'num_samples': 0,
'num_columns': 0,
'columns': [],
'sample_restrictions': '',
'specimen_id_column': None}
'sample_restrictions': ''}
self.assertEqual(obs, exp)

def test_sample_template_columns_get_req(self):
Expand Down Expand Up @@ -511,8 +509,7 @@ def test_get(self):
'sample_type', 'collection_timestamp',
'host_subject_id', 'description', 'latitude',
'longitude', 'scientific_name', 'env_package']),
'sample_restrictions': '',
'specimen_id_column': None}
'sample_restrictions': ''}
self.assertDictEqual(obs, exp)


Expand Down
Loading

0 comments on commit bae2956

Please sign in to comment.