tripal_feature.delete.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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_feature
  10. */
  11. function tripal_feature_delete_form() {
  12. // get the list of organisms
  13. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  14. $org_rset = chado_query($sql);
  15. $organisms = array();
  16. $organisms[''] = '';
  17. while ($organism = $org_rset->fetchObject()) {
  18. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  19. }
  20. $form['desc'] = array(
  21. '#markup' => t("Use one or more of the following fields to identify sets of features to be deleted."),
  22. );
  23. $form['feature_names']= array(
  24. '#type' => 'textarea',
  25. '#title' => t('Feature Names'),
  26. '#description' => t('Please provide a list of feature names or unique names,
  27. separated by spaces or by new lines to be delete. If you specify feature names then
  28. all other options below will be ignored (except the unique checkbox).'),
  29. );
  30. $form['is_unique'] = array(
  31. '#title' => t('Names are Unique Names'),
  32. '#type' => 'checkbox',
  33. '#description' => t('Select this checbox if the names listed in the feature
  34. names box above are the unique name of the feature rather than the human readable names.'),
  35. );
  36. $form['seq_type']= array(
  37. '#type' => 'textfield',
  38. '#title' => t('Sequence Type'),
  39. '#description' => t('Please enter the Sequence Ontology term that describes the features to be deleted. Use in conjunction with an organism or anaylysis.'),
  40. );
  41. $form['organism_id'] = array(
  42. '#title' => t('Organism'),
  43. '#type' => t('select'),
  44. '#description' => t("Choose the organism for which features will be deleted."),
  45. '#options' => $organisms,
  46. );
  47. // get the list of analyses
  48. $sql = "SELECT * FROM {analysis} ORDER BY name";
  49. $org_rset = chado_query($sql);
  50. $analyses = array();
  51. $analyses[''] = '';
  52. while ($analysis = $org_rset->fetchObject()) {
  53. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  54. }
  55. // TODO: ADD THIS BACK IN LATER
  56. //
  57. // $form['analysis']['analysis_id'] = array (
  58. // '#title' => t('Analysis'),
  59. // '#type' => t('select'),
  60. // '#description' => t("Choose the analysis for which associated features will be deleted."),
  61. // '#options' => $analyses,
  62. // );
  63. $form['button'] = array(
  64. '#type' => 'submit',
  65. '#value' => t('Delete Features'),
  66. );
  67. return $form;
  68. }
  69. /**
  70. * Validation for the delete features form
  71. *
  72. * @ingroup tripal_feature
  73. */
  74. function tripal_feature_delete_form_validate($form, &$form_state) {
  75. $organism_id = $form_state['values']['organism_id'];
  76. $seq_type = trim($form_state['values']['seq_type']);
  77. $analysis_id = $form_state['values']['analysis_id'];
  78. $is_unique = $form_state['values']['is_unique'];
  79. $feature_names = $form_state['values']['feature_names'];
  80. if (!$organism_id and !$anaysis_id and !$seq_type and !$feature_names) {
  81. form_set_error('feature_names', t("Please select at least one option"));
  82. }
  83. // check to make sure the types exists
  84. if ($seq_type) {
  85. $cvtermsql = "
  86. SELECT CVT.cvterm_id
  87. FROM {cvterm} CVT
  88. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  89. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  90. WHERE cv.name = :cvname and (CVT.name = :name or CVTS.synonym = :synonym)
  91. ";
  92. $cvterm = chado_query($cvtermsql,
  93. array(':cvname' => 'sequence', ':name' => $seq_type, ':synonym' => $seq_type))->fetchObject();
  94. if (!$cvterm) {
  95. form_set_error('seq_type', t("The Sequence Ontology (SO) term selected for the " .
  96. "sequence type is not available in the database. Please check spelling or select another."));
  97. }
  98. }
  99. }
  100. /**
  101. * Submit for the delete features form
  102. *
  103. * @ingroup tripal_feature
  104. */
  105. function tripal_feature_delete_form_submit($form, &$form_state) {
  106. global $user;
  107. $organism_id = $form_state['values']['organism_id'];
  108. $seq_type = trim($form_state['values']['seq_type']);
  109. $analysis_id = $form_state['values']['analysis_id'];
  110. $is_unique = $form_state['values']['is_unique'];
  111. $feature_names = $form_state['values']['feature_names'];
  112. $args = array($organism_id, $analysis_id, $seq_type, $is_unique, $feature_names);
  113. tripal_add_job("Delete features", 'tripal_feature',
  114. 'tripal_feature_delete_features', $args, $user->uid);
  115. }
  116. /**
  117. * Function to actually delete the features indicated
  118. *
  119. * @param $organism_id
  120. * (Optional) The organism_id of the features to delete
  121. * @param $analysis_id
  122. * (Optional) The analysis_id of the features to delete
  123. * @param $seq_type
  124. * (Optional) The cvterm.name of the feature types to delete
  125. * @param $is_unique
  126. * (Optional) A Boolean stating whether the names are unique (ie: feature.uniquename)
  127. * or not (ie: feature.name)
  128. * @param $feature_names
  129. * (Optional) A space separated list of the names of features to delete
  130. * @param $job
  131. * The tripal_job id
  132. *
  133. * @ingroup tripal_feature
  134. */
  135. function tripal_feature_delete_features($organism_id, $analysis_id, $seq_type,
  136. $is_unique, $feature_names, $job = NULL) {
  137. global $user;
  138. $match = array();
  139. // if feature names have been provided then handle that separately
  140. if ($feature_names) {
  141. $names = preg_split('/\s+/', $feature_names);
  142. if (sizeof($names) == 1) {
  143. $names = $names[0];
  144. }
  145. if ($is_unique) {
  146. $match['uniquename'] = $names;
  147. }
  148. else {
  149. $match['name'] = $names;
  150. }
  151. $num_deletes = chado_select_record('feature', array('count(*) as cnt'), $match);
  152. print "Deleting " . $num_deletes[0]->cnt . " features\n";
  153. chado_delete_record('feature', $match);
  154. }
  155. // if the user has provided an analysis_id then handle that separately
  156. elseif ($analysis_id) {
  157. tripal_feature_delete_by_analysis();
  158. }
  159. else {
  160. if ($organism_id) {
  161. $match['organism_id'] = $organism_id;
  162. }
  163. if ($seq_type) {
  164. $match['type_id'] = array(
  165. 'name' => $seq_type,
  166. 'cv_id' => array(
  167. 'name' => 'sequence'
  168. ),
  169. );
  170. }
  171. $num_deletes = chado_select_record('feature', array('count(*) as cnt'), $match);
  172. print "Deleting " . $num_deletes[0]->cnt . " features\n";
  173. chado_delete_record('feature', $match);
  174. }
  175. print "Removing orphaned feature pages\n";
  176. tripal_features_cleanup(array(), $user->uid);
  177. }
  178. /**
  179. * Function to delete features based on an analysis passed in. This has not yet been
  180. * implemented in the form
  181. *
  182. * @todo: Implement this functionality and then add back in the form field
  183. *
  184. * @param $organism_id
  185. * (Optional) The organism_id of the features to delete
  186. * @param $analysis_id
  187. * (Optional) The analysis_id of the features to delete
  188. * @param $seq_type
  189. * (Optional) The cvterm.name of the feature types to delete
  190. * @param $is_unique
  191. * (Optional) A Boolean stating whether the names are unique (ie: feature.uniquename)
  192. * or not (ie: feature.name)
  193. * @param $feature_names
  194. * (Optional) A space separated list of the names of features to delete
  195. * @param $job
  196. * The tripal_job id
  197. *
  198. * @ingroup tripal_feature
  199. */
  200. function tripal_feature_delete_by_analysis($organism_id, $analysis_id, $seq_type,
  201. $is_unique, $feature_names, $job = NULL) {
  202. }