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
11 changes: 11 additions & 0 deletions qiita_db/metadata_template/base_metadata_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,13 +653,24 @@ 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(
"'%s' not in info file %d" % (column_name, self._id))
if not self.can_be_updated(columns={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:
sql = 'ALTER TABLE qiita.%s%d DROP COLUMN %s' % (
self._table_prefix, self._id, column_name)
Expand Down
13 changes: 13 additions & 0 deletions qiita_db/metadata_template/test/test_sample_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2159,6 +2159,19 @@ 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.assertRaisesRegexp(
qdb.exceptions.QiitaDBOperationNotPermittedError,
'"latitude" cannot be deleted, this column is currently '
'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
60 changes: 60 additions & 0 deletions qiita_db/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Study(qdb.base.QiitaObject):
status
title
owner
specimen_id_column

Methods
-------
Expand Down Expand Up @@ -643,6 +644,65 @@ 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.")

with qdb.sql_connection.TRN:
# Set the new ones
sql = """UPDATE qiita.study SET
specimen_id_column = %s
WHERE study_id = %s"""
qdb.sql_connection.TRN.add(sql, [value, self._id])
qdb.sql_connection.TRN.execute()

@property
def investigation(self):
""" Returns Investigation this study is part of
Expand Down
7 changes: 7 additions & 0 deletions qiita_db/support_files/patches/66.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- August 22, 2018
-- add specimen_id_column to study table (needed to plate samples in labman)

ALTER TABLE qiita.study ADD specimen_id_column varchar(256);

COMMENT ON COLUMN qiita.study.specimen_id_column IS 'The name of the column that describes the specimen identifiers (such as what is written on the tubes).';

5 changes: 4 additions & 1 deletion qiita_db/support_files/qiita-db.dbs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,9 @@ Controlled Vocabulary]]></comment>
<column name="study_abstract" type="text" jt="12" mandatory="y" />
<column name="vamps_id" type="varchar" jt="12" />
<column name="ebi_study_accession" type="varchar" jt="12" />
<column name="specimen_id_column" type="varchar" length="256" jt="12" >
<comment><![CDATA[The name of the column that describes the specimen identifiers (such as what is written on the tubes).]]></comment>
</column>
<index name="pk_study" unique="PRIMARY_KEY" >
<column name="study_id" />
</index>
Expand Down Expand Up @@ -1691,7 +1694,6 @@ Controlled Vocabulary]]></comment>
<entity schema="qiita" name="study_environmental_package" color="b2cdf7" x="2085" y="45" />
<entity schema="qiita" name="environmental_package" color="b2cdf7" x="2160" y="180" />
<entity schema="qiita" name="study_person" color="c0d4f3" x="2010" y="135" />
<entity schema="qiita" name="study" color="d0def5" x="1785" y="240" />
<entity schema="qiita" name="per_study_tags" color="b2cdf7" x="2145" y="450" />
<entity schema="qiita" name="study_tags" color="b2cdf7" x="2295" y="480" />
<entity schema="qiita" name="timeseries_type" color="c0d4f3" x="2010" y="540" />
Expand Down Expand Up @@ -1725,6 +1727,7 @@ Controlled Vocabulary]]></comment>
<entity schema="qiita" name="software_command" color="b2cdf7" x="2160" y="1245" />
<entity schema="qiita" name="processing_job_validator" color="b2cdf7" x="2160" y="1410" />
<entity schema="qiita" name="processing_job" color="b2cdf7" x="1935" y="1140" />
<entity schema="qiita" name="study" color="d0def5" x="1785" y="240" />
<group name="Group_analyses" color="c4e0f9" >
<comment>analysis tables</comment>
<entity schema="qiita" name="analysis" />
Expand Down
Loading