Skip to content

Commit 656128f

Browse files
committed
deleting code to qiita_db
1 parent 16ff388 commit 656128f

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

qiita_db/data.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class BaseData(QiitaObject):
103103
--------
104104
RawData
105105
PreprocessedData
106-
PreprocessedData
106+
ProcessedData
107107
"""
108108
_filepath_table = "filepath"
109109

@@ -807,6 +807,72 @@ def create(cls, study, preprocessed_params_table, preprocessed_params_id,
807807
ppd.add_filepaths(filepaths, conn_handler)
808808
return ppd
809809

810+
@classmethod
811+
def delete(cls, ppd_id):
812+
"""Removes the preprocessed data with id ppd_id
813+
814+
Parameters
815+
----------
816+
ppd_id : int
817+
The preprocessed data id
818+
819+
Raises
820+
------
821+
QiitaDBStatusError
822+
If the preprocessed data status is not sandbox or if the
823+
preprocessed data EBI and VAMPS submission is not in a valid state
824+
['not submitted', 'failed']
825+
QiitaDBError
826+
If the preprocessed data has (meta)analyses
827+
"""
828+
valid_submission_states = ['not submitted', 'failed']
829+
ppd = cls(ppd_id)
830+
if ppd.status != 'sandbox':
831+
raise QiitaDBStatusError(
832+
"Illegal operation on non sandbox preprocessed data")
833+
elif (ppd.submitted_to_vamps_status() not in valid_submission_states or
834+
ppd.submitted_to_insdc_status() not in valid_submission_states):
835+
raise QiitaDBStatusError(
836+
"Illegal operation due to EBI submission status ('%s') or "
837+
"VAMPS submission status ('%s')" % (
838+
ppd.submitted_to_insdc_status(),
839+
ppd.submitted_to_vamps_status()))
840+
841+
conn_handler = SQLConnectionHandler()
842+
843+
processed_data = [str(n[0]) for n in conn_handler.execute_fetchall(
844+
"SELECT processed_data_id FROM qiita.preprocessed_processed_data "
845+
"WHERE preprocessed_data_id = {0} ORDER BY "
846+
"processed_data_id".format(ppd_id))]
847+
848+
if processed_data:
849+
raise QiitaDBError(
850+
"Preprocessed data %d cannot be removed because it was used "
851+
"to generate the following processed data: %s" % (
852+
ppd_id, ', '.join(processed_data)))
853+
854+
# delete
855+
queue = "delete_preprocessed_data_%d" % ppd_id
856+
conn_handler.create_queue(queue)
857+
858+
sql = ("DELETE FROM qiita.prep_template_preprocessed_data WHERE "
859+
"preprocessed_data_id = {0}".format(ppd_id))
860+
conn_handler.add_to_queue(queue, sql)
861+
862+
sql = ("DELETE FROM qiita.preprocessed_filepath WHERE "
863+
"preprocessed_data_id = {0}".format(ppd_id))
864+
conn_handler.add_to_queue(queue, sql)
865+
866+
sql = ("DELETE FROM qiita.study_preprocessed_data WHERE "
867+
"preprocessed_data_id = {0}".format(ppd_id))
868+
conn_handler.add_to_queue(queue, sql)
869+
870+
sql = ("DELETE FROM qiita.preprocessed_data WHERE "
871+
"preprocessed_data_id = {0}".format(ppd_id))
872+
conn_handler.add_to_queue(queue, sql)
873+
874+
conn_handler.execute_queue(queue)
875+
810876
@property
811877
def processed_data(self):
812878
r"""The processed data list generated from this preprocessed data"""

qiita_db/test/test_data.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,38 @@ def test_create_data_type_only(self):
478478
# preprocessed_data_id, filepath_id
479479
self.assertEqual(obs, [[3, obs_id - 1], [3, obs_id]])
480480

481+
def test_delete(self):
482+
"""Correctly deletes a preprocessed data"""
483+
# testing regular delete
484+
ppd = PreprocessedData.create(
485+
self.study, self.params_table,
486+
self.params_id, self.filepaths, prep_template=self.prep_template,
487+
ebi_submission_accession=self.ebi_submission_accession,
488+
ebi_study_accession=self.ebi_study_accession)
489+
PreprocessedData.delete(ppd.id)
490+
491+
# testing that it raises an error if ID doesn't exist
492+
with self.assertRaises(QiitaDBUnknownIDError):
493+
PreprocessedData.delete(ppd.id)
494+
495+
# testing that we can not remove cause the preprocessed data != sandbox
496+
with self.assertRaises(QiitaDBStatusError):
497+
PreprocessedData.delete(1)
498+
499+
# testing that we can not remove cause preprocessed data has been
500+
# submitted to EBI or VAMPS
501+
pd = ProcessedData(1)
502+
pd.status = 'sandbox'
503+
with self.assertRaises(QiitaDBError):
504+
PreprocessedData.delete(pd.preprocessed_data)
505+
506+
# testing that we can not remove cause preprocessed data has processed
507+
# data
508+
ppd = PreprocessedData(pd.preprocessed_data)
509+
ppd.update_insdc_status('not submitted')
510+
with self.assertRaises(QiitaDBError):
511+
PreprocessedData.delete(ppd.id)
512+
481513
def test_create_error_dynamic_table(self):
482514
"""Raises an error if the preprocessed_params_table does not exist"""
483515
with self.assertRaises(IncompetentQiitaDeveloperError):

0 commit comments

Comments
 (0)