tripal_feature.delete.inc

  1. 2.x tripal_feature/includes/tripal_feature.delete.inc
  2. 3.x legacy/tripal_feature/includes/tripal_feature.delete.inc

Administration Interface for deleting multiple features

File

legacy/tripal_feature/includes/tripal_feature.delete.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Administration Interface for deleting multiple features
  5. */
  6. /**
  7. * A form for indicating the features to delete
  8. *
  9. * @ingroup tripal_legacy_feature
  10. */
  11. function tripal_feature_delete_form() {
  12. $form['desc'] = array(
  13. '#markup' => t("Use one or more of the following fields to identify sets of features to be deleted."),
  14. );
  15. $form['feature_names']= array(
  16. '#type' => 'textarea',
  17. '#title' => t('Feature Names'),
  18. '#description' => t('Please provide a list of feature names or unique names,
  19. separated by spaces or by new lines to be delete. If you specify feature names then
  20. all other options below will be ignored (except the unique checkbox).'),
  21. );
  22. $form['is_unique'] = array(
  23. '#title' => t('Names are Unique Names'),
  24. '#type' => 'checkbox',
  25. '#description' => t('Select this checbox if the names listed in the feature
  26. names box above are the unique name of the feature rather than the human readable names.'),
  27. );
  28. $cv = tripal_get_cv(array('name' => 'sequence'));
  29. $form['seq_type']= array(
  30. '#title' => t('Feature Type'),
  31. '#type' => 'textfield',
  32. '#description' => t("Choose the feature type."),
  33. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv->cv_id",
  34. );
  35. $organisms = tripal_get_organism_select_options(FALSE);
  36. $form['organism_id'] = array(
  37. '#title' => t('Organism'),
  38. '#type' => 'select',
  39. '#description' => t("Choose the organism for which features will be deleted."),
  40. '#options' => $organisms,
  41. );
  42. $analyses = tripal_get_analysis_select_options(FALSE);
  43. $form['analysis_id'] = array (
  44. '#title' => t('Analysis'),
  45. '#type' => t('select'),
  46. '#description' => t("Choose the analysis for which associated features will be deleted."),
  47. '#options' => $analyses,
  48. );
  49. $form['button'] = array(
  50. '#type' => 'submit',
  51. '#value' => t('Delete Features'),
  52. );
  53. return $form;
  54. }
  55. /**
  56. * Validation for the delete features form
  57. *
  58. * @ingroup tripal_legacy_feature
  59. */
  60. function tripal_feature_delete_form_validate($form, &$form_state) {
  61. $organism_id = $form_state['values']['organism_id'];
  62. $seq_type = trim($form_state['values']['seq_type']);
  63. $analysis_id = $form_state['values']['analysis_id'];
  64. $is_unique = $form_state['values']['is_unique'];
  65. $feature_names = $form_state['values']['feature_names'];
  66. if (!$analysis_id and !$organism_id and !$seq_type and !$feature_names) {
  67. form_set_error('feature_names', t("Please select at least one option"));
  68. }
  69. }
  70. /**
  71. * Submit for the delete features form
  72. *
  73. * @ingroup tripal_legacy_feature
  74. */
  75. function tripal_feature_delete_form_submit($form, &$form_state) {
  76. global $user;
  77. $organism_id = $form_state['values']['organism_id'];
  78. $seq_type = trim($form_state['values']['seq_type']);
  79. $analysis_id = $form_state['values']['analysis_id'];
  80. $is_unique = $form_state['values']['is_unique'];
  81. $feature_names = $form_state['values']['feature_names'];
  82. $args = array($organism_id, $analysis_id, $seq_type, $is_unique, $feature_names);
  83. tripal_add_job("Delete features", 'tripal_feature',
  84. 'tripal_feature_delete_features', $args, $user->uid);
  85. }
  86. /**
  87. * Function to actually delete the features indicated
  88. *
  89. * @param $organism_id
  90. * (Optional) The organism_id of the features to delete
  91. * @param $analysis_id
  92. * (Optional) The analysis_id of the features to delete
  93. * @param $seq_type
  94. * (Optional) The cvterm.name of the feature types to delete
  95. * @param $is_unique
  96. * (Optional) A Boolean stating whether the names are unique (ie: feature.uniquename)
  97. * or not (ie: feature.name)
  98. * @param $feature_names
  99. * (Optional) A space separated list of the names of features to delete
  100. * @param $job
  101. * The tripal_job id
  102. *
  103. * @ingroup tripal_legacy_feature
  104. */
  105. function tripal_feature_delete_features($organism_id, $analysis_id, $seq_type,
  106. $is_unique, $feature_names, $job = NULL) {
  107. global $user;
  108. $match = array();
  109. // Begin the transaction.
  110. $transaction = db_transaction();
  111. print "\nNOTE: Deleting features is performed using a database transaction. \n" .
  112. "If the load fails or is terminated prematurely then the entire set of \n" .
  113. "deletions is rolled back and will not be found in the database\n\n";
  114. try {
  115. // If feature names have been provided then handle those
  116. if ($feature_names) {
  117. $names = preg_split('/\s+/', $feature_names);
  118. if (sizeof($names) == 1) {
  119. $names = $names[0];
  120. }
  121. if ($is_unique) {
  122. $match['uniquename'] = $names;
  123. }
  124. else {
  125. $match['name'] = $names;
  126. }
  127. $num_deletes = chado_select_record('feature', array('count(*) as cnt'), $match);
  128. print "Deleting " . $num_deletes[0]->cnt . " features\n";
  129. chado_delete_record('feature', $match);
  130. return;
  131. }
  132. // Now handle the combintation of all other inputs.
  133. $args = array();
  134. $sql = "";
  135. $join = '';
  136. $where = '';
  137. if ($analysis_id) {
  138. $join .= 'INNER JOIN {analysisfeature} AF on F.feature_id = AF.feature_id ';
  139. $join .= 'INNER JOIN {analysis} A on A.analysis_id = AF.analysis_id ';
  140. $where .= 'AND A.analysis_id = :analysis_id ';
  141. $args[':analysis_id'] = $analysis_id;
  142. }
  143. if ($organism_id) {
  144. $where .= 'AND F.organism_id = :organism_id ';
  145. $args[':organism_id'] = $organism_id;
  146. }
  147. if ($seq_type) {
  148. $join .= 'INNER JOIN {cvterm} CVT ON CVT.cvterm_id = F.type_id';
  149. $where .= 'AND CVT.name = :type_name';
  150. $args[':type_name'] = $seq_type;
  151. }
  152. // Do not perform a delete if we have no additions to the where clause
  153. // otherwise all features will be deleted and this is probably not what
  154. // is wanted.
  155. if (!$where) {
  156. throw new Exception('Cannot delete features as no filters are available');
  157. }
  158. // First, count the number of records to be deleted
  159. $sql = "
  160. SELECT count(F.feature_id)
  161. FROM {feature} F
  162. $join
  163. WHERE 1=1 $where
  164. ";
  165. $num_deleted = chado_query($sql, $args)->fetchField();
  166. // Second, delete the records.
  167. $sql = "
  168. DELETE FROM {feature} WHERE feature_id IN (
  169. SELECT F.feature_id
  170. FROM {feature} F
  171. $join
  172. WHERE 1=1 $where
  173. )
  174. ";
  175. chado_query($sql, $args);
  176. print "Deletiong completed successfully. Deleted $num_deleted feature(s).\n";
  177. print "Now removing orphaned feature pages\n";
  178. chado_cleanup_orphaned_nodes('feature');
  179. }
  180. catch (Exception $e) {
  181. print "\n"; // make sure we start errors on new line
  182. $transaction->rollback();
  183. print "FAILED: Rolling back database changes...\n";
  184. watchdog_exception('tripal_feature', $e);
  185. return 0;
  186. }
  187. print "\nDone\n";
  188. }