Skip to content

Commit 2199bdd

Browse files
committed
Merge pull request #1034 from antgonza/delete-process-data
Delete process data
2 parents 0744eed + c195dad commit 2199bdd

File tree

5 files changed

+134
-5
lines changed

5 files changed

+134
-5
lines changed

qiita_db/data.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,61 @@ def create(cls, processed_params_table, processed_params_id, filepaths,
12571257
pd.add_filepaths(filepaths, conn_handler)
12581258
return cls(pd_id)
12591259

1260+
@classmethod
1261+
def delete(cls, processed_data_id):
1262+
"""Removes the processed data with id processed_data_id
1263+
1264+
Parameters
1265+
----------
1266+
processed_data_id : int
1267+
The processed data id
1268+
1269+
Raises
1270+
------
1271+
QiitaDBStatusError
1272+
If the processed data status is not sandbox
1273+
QiitaDBError
1274+
If the processed data has (meta)analyses
1275+
"""
1276+
if cls(processed_data_id).status != 'sandbox':
1277+
raise QiitaDBStatusError(
1278+
"Illegal operation on non sandbox processed data")
1279+
1280+
conn_handler = SQLConnectionHandler()
1281+
1282+
analyses = [str(n[0]) for n in conn_handler.execute_fetchall(
1283+
"SELECT DISTINCT name FROM qiita.analysis JOIN "
1284+
"qiita.analysis_sample USING (analysis_id) WHERE "
1285+
"processed_data_id = {0} ORDER BY name".format(processed_data_id))]
1286+
1287+
if analyses:
1288+
raise QiitaDBError(
1289+
"Processed data %d cannot be removed because it is linked to "
1290+
"the following (meta)analysis: %s" % (processed_data_id,
1291+
', '.join(analyses)))
1292+
1293+
# delete
1294+
queue = "delete_processed_data_%d" % processed_data_id
1295+
conn_handler.create_queue(queue)
1296+
1297+
sql = ("DELETE FROM qiita.preprocessed_processed_data WHERE "
1298+
"processed_data_id = {0}".format(processed_data_id))
1299+
conn_handler.add_to_queue(queue, sql)
1300+
1301+
sql = ("DELETE FROM qiita.processed_filepath WHERE "
1302+
"processed_data_id = {0}".format(processed_data_id))
1303+
conn_handler.add_to_queue(queue, sql)
1304+
1305+
sql = ("DELETE FROM qiita.study_processed_data WHERE "
1306+
"processed_data_id = {0}".format(processed_data_id))
1307+
conn_handler.add_to_queue(queue, sql)
1308+
1309+
sql = ("DELETE FROM qiita.processed_data WHERE "
1310+
"processed_data_id = {0}".format(processed_data_id))
1311+
conn_handler.add_to_queue(queue, sql)
1312+
1313+
conn_handler.execute_queue(queue)
1314+
12601315
@property
12611316
def preprocessed_data(self):
12621317
r"""The preprocessed data id used to generate the processed data"""

qiita_db/test/test_data.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,29 @@ def test_create(self):
785785
# preprocessed_data_id, processed_Data_id
786786
self.assertEqual(obs, [[1, 2]])
787787

788+
def test_delete(self):
789+
"""Correctly deletes a processed data"""
790+
# testing regular delete
791+
pd = ProcessedData.create(self.params_table, self.params_id,
792+
self.filepaths,
793+
preprocessed_data=self.preprocessed_data,
794+
processed_date=self.date)
795+
ProcessedData.delete(pd.id)
796+
797+
# testing that it raises an error if ID doesn't exist
798+
with self.assertRaises(QiitaDBUnknownIDError):
799+
ProcessedData.delete(pd.id)
800+
801+
# testing that we can not remove cause the processed data != sandbox
802+
with self.assertRaises(QiitaDBStatusError):
803+
ProcessedData.delete(1)
804+
805+
# testing that we can not remove cause processed data has analyses
806+
pd = ProcessedData(1)
807+
pd.status = 'sandbox'
808+
with self.assertRaises(QiitaDBError):
809+
ProcessedData.delete(1)
810+
788811
def test_create_no_date(self):
789812
"""Correctly adds a processed data with no date on it"""
790813
# All the other settings have been already tested on test_create

qiita_pet/handlers/study_handlers/description_handlers.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,33 @@ def delete_prep_template(self, study, user, callback):
697697

698698
callback((msg, msg_level, 'raw_data_tab', prep_id, None))
699699

700+
def delete_processed_data(self, study, user, callback):
701+
"""Delete the selected processed data
702+
703+
Parameters
704+
----------
705+
study : Study
706+
The current study object
707+
user : User
708+
The current user object
709+
callback : function
710+
The callback function to call with the results once the processing
711+
is done
712+
"""
713+
pd_id = int(self.get_argument('processed_data_id'))
714+
715+
try:
716+
ProcessedData.delete(pd_id)
717+
msg = ("Processed data %d has been deleted" % pd_id)
718+
msg_level = "success"
719+
pd_id = None
720+
except Exception as e:
721+
msg = ("Couldn't remove processed data %d: %s" %
722+
(pd_id, str(e)))
723+
msg_level = "danger"
724+
725+
callback((msg, msg_level, 'processed_data_tab', pd_id, None))
726+
700727
@authenticated
701728
def get(self, study_id):
702729
study, user, full_access = self._get_study_and_check_access(study_id)
@@ -728,7 +755,8 @@ def post(self, study_id):
728755
make_sandbox=self.make_sandbox,
729756
update_investigation_type=self.update_investigation_type,
730757
delete_raw_data=self.delete_raw_data,
731-
delete_prep_template=self.delete_prep_template)
758+
delete_prep_template=self.delete_prep_template,
759+
delete_processed_data=self.delete_processed_data)
732760

733761
# Get the action that we need to perform
734762
action = self.get_argument("action", None)

qiita_pet/templates/study_description.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,26 @@
222222
}
223223
}
224224

225+
function delete_processed_data(processed_data_id) {
226+
if (confirm('Are you sure you want to delete processed data ID: ' + processed_data_id + '?')) {
227+
var form = $("<form>")
228+
.attr("action", window.location.href)
229+
.attr("method", "post")
230+
.append($("<input>")
231+
.attr("type", "hidden")
232+
.attr("name", "processed_data_id")
233+
.attr("value", processed_data_id))
234+
.append($("<input>")
235+
.attr("type", "hidden")
236+
.attr("name", "action")
237+
.attr("value", "delete_processed_data"));
238+
$("body").append(form);
239+
form.submit();
240+
} else {
241+
return false;
242+
}
243+
}
244+
225245
function make_public(pd_id) {
226246
if (confirm("Are you sure you want to make this study public?")) {
227247
var form = $("<form>")

qiita_pet/templates/study_description_templates/processed_data_tab.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
<li class="active"><a href="#add_processed_data_tab" role="tab" data-toggle="tab">Add processed data</a></li>
77
<!-- Create the tabs for each processed data -->
88
{% for pd_id, pd, (class_icon1, class_icon2, color) in available_processed_data %}
9-
<li><a href="#processed_data_info_{{pd_id}}" role="tab" data-toggle="tab">ID: {{pd_id}}&nbsp;
10-
<div class="{{class_icon1}}" style="color: {{color}};"></div>
11-
<div class="{{class_icon2}}" style="color: {{color}};"></div>
12-
</a></li>
9+
<li>
10+
<a href="#processed_data_info_{{pd_id}}" role="tab" data-toggle="tab">ID: {{pd_id}}&nbsp;
11+
<div class="{{class_icon1}}" style="color: {{color}};"></div>
12+
<div class="{{class_icon2}}" style="color: {{color}};"></div>
13+
<button class="close" title="Remove this processed data" type="button" onclick="delete_processed_data({{pd_id}})">&nbsp; ×</button>
14+
</a>
15+
</li>
1316
{% end %}
1417
</ul>
1518
<!-- Create the tab panes -->

0 commit comments

Comments
 (0)