tripal_feature-delete.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. function tripal_feature_delete_form() {
  7. // get the list of organisms
  8. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  9. $org_rset = chado_query($sql);
  10. $organisms = array();
  11. $organisms[''] = '';
  12. while ($organism = $org_rset->fetchObject()) {
  13. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  14. }
  15. $form['desc'] = array(
  16. '#type' => 'markup',
  17. '#value' => t("Use one or more of the following fields to identify sets of features to be deleted."),
  18. );
  19. $form['feature_names']= array(
  20. '#type' => 'textarea',
  21. '#title' => t('Feature Names'),
  22. '#description' => t('Please provide a list of feature names or unique names,
  23. separated by spaces or by new lines to be delete. If you specify feature names then
  24. all other options below will be ignored (except the unique checkbox).'),
  25. );
  26. $form['is_unique'] = array(
  27. '#title' => t('Names are Unique Names'),
  28. '#type' => 'checkbox',
  29. '#description' => t('Select this checbox if the names listed in the feature
  30. names box above are the unique name of the feature rather than the human readable names.'),
  31. );
  32. $form['seq_type']= array(
  33. '#type' => 'textfield',
  34. '#title' => t('Sequence Type'),
  35. '#description' => t('Please enter the Sequence Ontology term that describes the features to be deleted. Use in conjunction with an organism or anaylysis.'),
  36. );
  37. $form['organism_id'] = array(
  38. '#title' => t('Organism'),
  39. '#type' => t('select'),
  40. '#description' => t("Choose the organism for which features will be deleted."),
  41. '#options' => $organisms,
  42. );
  43. // get the list of analyses
  44. $sql = "SELECT * FROM {analysis} ORDER BY name";
  45. $org_rset = chado_query($sql);
  46. $analyses = array();
  47. $analyses[''] = '';
  48. while ($analysis = $org_rset->fetchObject()) {
  49. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  50. }
  51. // TODO: ADD THIS BACK IN LATER
  52. //
  53. // $form['analysis']['analysis_id'] = array (
  54. // '#title' => t('Analysis'),
  55. // '#type' => t('select'),
  56. // '#description' => t("Choose the analysis for which associated features will be deleted."),
  57. // '#options' => $analyses,
  58. // );
  59. $form['button'] = array(
  60. '#type' => 'submit',
  61. '#value' => t('Delete Features'),
  62. );
  63. return $form;
  64. }
  65. function tripal_feature_delete_form_validate($form, &$form_state) {
  66. $organism_id = $form_state['values']['organism_id'];
  67. $seq_type = trim($form_state['values']['seq_type']);
  68. $analysis_id = $form_state['values']['analysis_id'];
  69. $is_unique = $form_state['values']['is_unique'];
  70. $feature_names = $form_state['values']['feature_names'];
  71. if (!$organism_id and !$anaysis_id and !$seq_type and !$feature_names) {
  72. form_set_error('feature_names', t("Please select at least one option"));
  73. }
  74. // check to make sure the types exists
  75. if ($seq_type) {
  76. $cvtermsql = "
  77. SELECT CVT.cvterm_id
  78. FROM {cvterm} CVT
  79. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  80. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  81. WHERE cv.name = :cvname and (CVT.name = :name or CVTS.synonym = :synonym)
  82. ";
  83. $cvterm = chado_query($cvtermsql,
  84. array(':cvname' => 'sequence', ':name' => $seq_type, ':synonym' => $seq_type))->fetchObject();
  85. if (!$cvterm) {
  86. form_set_error('seq_type', t("The Sequence Ontology (SO) term selected for the " .
  87. "sequence type is not available in the database. Please check spelling or select another."));
  88. }
  89. }
  90. }
  91. function tripal_feature_delete_form_submit($form, &$form_state) {
  92. global $user;
  93. $organism_id = $form_state['values']['organism_id'];
  94. $seq_type = trim($form_state['values']['seq_type']);
  95. $analysis_id = $form_state['values']['analysis_id'];
  96. $is_unique = $form_state['values']['is_unique'];
  97. $feature_names = $form_state['values']['feature_names'];
  98. $args = array($organism_id, $analysis_id, $seq_type, $is_unique, $feature_names);
  99. tripal_add_job("Delete features", 'tripal_feature',
  100. 'tripal_feature_delete_features', $args, $user->uid);
  101. }
  102. function tripal_feature_delete_features($organism_id, $analysis_id, $seq_type,
  103. $is_unique, $feature_names, $job = NULL) {
  104. global $user;
  105. $match = array();
  106. // if feature names have been provided then handle that separately
  107. if ($feature_names) {
  108. $names = preg_split('/\s+/', $feature_names);
  109. if (sizeof($names) == 1) {
  110. $names = $names[0];
  111. }
  112. if ($is_unique) {
  113. $match['uniquename'] = $names;
  114. }
  115. else {
  116. $match['name'] = $names;
  117. }
  118. $num_deletes = tripal_core_chado_select('feature', array('count(*) as cnt'), $match);
  119. print "Deleting " . $num_deletes[0]->cnt . " features\n";
  120. tripal_core_chado_delete('feature', $match);
  121. }
  122. // if the user has provided an analysis_id then handle that separately
  123. elseif ($analysis_id) {
  124. tripal_feature_delete_by_analysis();
  125. }
  126. else {
  127. if ($organism_id) {
  128. $match['organism_id'] = $organism_id;
  129. }
  130. if ($seq_type) {
  131. $match['type_id'] = array(
  132. 'name' => $seq_type,
  133. 'cv_id' => array(
  134. 'name' => 'sequence'
  135. ),
  136. );
  137. }
  138. $num_deletes = tripal_core_chado_select('feature', array('count(*) as cnt'), $match);
  139. print "Deleting " . $num_deletes[0]->cnt . " features\n";
  140. tripal_core_chado_delete('feature', $match);
  141. }
  142. print "Removing orphaned feature pages\n";
  143. tripal_features_cleanup(array(), $user->uid);
  144. }
  145. function tripal_feature_delete_by_analysis($organism_id, $analysis_id, $seq_type,
  146. $is_unique, $feature_names, $job = NULL) {
  147. }