tripal_feature-delete.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 = db_fetch_object($org_rset)) {
  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 = db_fetch_object($org_rset)) {
  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 = "SELECT CVT.cvterm_id
  77. FROM {cvterm} CVT
  78. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  79. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  80. WHERE cv.name = '%s' and (CVT.name = '%s' or CVTS.synonym = '%s')";
  81. $cvterm = db_fetch_object(chado_query($cvtermsql, 'sequence', $seq_type, $seq_type));
  82. if (!$cvterm) {
  83. form_set_error('seq_type', t("The Sequence Ontology (SO) term selected for the sequence type is not available in the database. Please check spelling or select another."));
  84. }
  85. }
  86. }
  87. function tripal_feature_delete_form_submit($form, &$form_state) {
  88. global $user;
  89. $organism_id = $form_state['values']['organism_id'];
  90. $seq_type = trim($form_state['values']['seq_type']);
  91. $analysis_id = $form_state['values']['analysis_id'];
  92. $is_unique = $form_state['values']['is_unique'];
  93. $feature_names = $form_state['values']['feature_names'];
  94. $args = array($organism_id, $analysis_id, $seq_type, $is_unique, $feature_names);
  95. tripal_add_job("Delete features", 'tripal_feature',
  96. 'tripal_feature_delete_features', $args, $user->uid);
  97. }
  98. function tripal_feature_delete_features($organism_id, $analysis_id, $seq_type,
  99. $is_unique, $feature_names, $job = NULL) {
  100. global $user;
  101. $match = array();
  102. // if feature names have been provided then handle that separately
  103. if ($feature_names) {
  104. $names = preg_split('/\s+/', $feature_names);
  105. if (sizeof($names) == 1) {
  106. $names = $names[0];
  107. }
  108. if ($is_unique) {
  109. $match['uniquename'] = $names;
  110. }
  111. else {
  112. $match['name'] = $names;
  113. }
  114. $num_deletes = tripal_core_chado_select('feature', array('count(*) as cnt'), $match);
  115. print "Deleting " . $num_deletes[0]->cnt . " features\n";
  116. tripal_core_chado_delete('feature', $match);
  117. }
  118. // if the user has provided an analysis_id then handle that separately
  119. elseif ($analysis_id) {
  120. tripal_feature_delete_by_analysis();
  121. }
  122. else {
  123. if ($organism_id) {
  124. $match['organism_id'] = $organism_id;
  125. }
  126. if ($seq_type) {
  127. $match['type_id'] = array(
  128. 'name' => $seq_type,
  129. 'cv_id' => array(
  130. 'name' => 'sequence'
  131. ),
  132. );
  133. }
  134. $num_deletes = tripal_core_chado_select('feature', array('count(*) as cnt'), $match);
  135. print "Deleting " . $num_deletes[0]->cnt . " features\n";
  136. tripal_core_chado_delete('feature', $match);
  137. }
  138. print "Removing orphaned feature pages\n";
  139. tripal_features_cleanup(array(), $user->uid);
  140. }
  141. function tripal_feature_delete_by_analysis($organism_id, $analysis_id, $seq_type,
  142. $is_unique, $feature_names, $job = NULL) {
  143. }