tripal_organism.admin.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Administrative settings for chado_orgnism
  4. *
  5. * @ingroup tripal_organism
  6. */
  7. function tripal_organism_admin() {
  8. $form = array();
  9. get_tripal_organism_admin_form_reindex_set($form);
  10. get_tripal_organism_admin_form_taxonomy_set($form);
  11. return system_settings_form($form);
  12. }
  13. /**
  14. *
  15. *
  16. * @ingroup tripal_organism
  17. */
  18. function get_tripal_organism_admin_form_taxonomy_set(&$form) {
  19. $form['taxonify'] = array(
  20. '#type' => 'fieldset',
  21. '#title' => t('Assign Drupal Taxonomy to Organism Features')
  22. );
  23. // get the list of libraries
  24. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  25. $org_rset = chado_query($sql);
  26. // iterate through all of the libraries
  27. $org_boxes = array();
  28. foreach ($org_rset as $organism) {
  29. $org_boxes[$organism->organism_id] = "$organism->genus $organism->species";
  30. }
  31. $form['taxonify']['description'] = array(
  32. '#type' => 'item',
  33. '#value' => t(
  34. "Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  35. "nodes. These terms allow for advanced filtering during searching. This option allows " .
  36. "for setting taxonomy only for features that belong to the selected organisms below. All " .
  37. "other features will be unaffected. To set taxonomy for all features in the site see the Feature Administration page."),
  38. '#weight' => 1,
  39. );
  40. $form['taxonify']['tx-organisms'] = array(
  41. '#title' => t('Organisms'),
  42. '#type' => t('checkboxes'),
  43. '#description' => t("Check the organisms whose features you want to reset taxonomy. Note: this list contains all organisms, even those that may not be synced."),
  44. '#required' => FALSE,
  45. '#prefix' => '<div id="lib_boxes">',
  46. '#suffix' => '</div>',
  47. '#options' => $org_boxes,
  48. '#weight' => 2
  49. );
  50. $form['taxonify']['tx-button'] = array(
  51. '#type' => 'submit',
  52. '#value' => t('Set Feature Taxonomy'),
  53. '#weight' => 3
  54. );
  55. }
  56. /**
  57. *
  58. * @ingroup tripal_organism
  59. */
  60. function get_tripal_organism_admin_form_reindex_set(&$form) {
  61. // define the fieldsets
  62. $form['reindex'] = array(
  63. '#type' => 'fieldset',
  64. '#title' => t('Reindex Organism Features')
  65. );
  66. // get the list of libraries
  67. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  68. $org_rset = chado_query($sql);
  69. // iterate through all of the libraries
  70. $org_boxes = array();
  71. foreach ($org_rset as $organism) {
  72. $org_boxes[$organism->organism_id] = "$organism->genus $organism->species";
  73. }
  74. $form['reindex']['description'] = array(
  75. '#type' => 'item',
  76. '#value' => t("This option allows for reindexing of only those features that belong to the selected organisms below. All other features will be unaffected. To reindex all features in the site see the Feature Administration page."),
  77. '#weight' => 1,
  78. );
  79. $form['reindex']['re-organisms'] = array(
  80. '#title' => t('Organisms'),
  81. '#type' => t('checkboxes'),
  82. '#description' => t("Check the organisms whose features you want to reindex. Note: this list contains all organisms, even those that may not be synced."),
  83. '#required' => FALSE,
  84. '#prefix' => '<div id="lib_boxes">',
  85. '#suffix' => '</div>',
  86. '#options' => $org_boxes,
  87. '#weight' => 2,
  88. );
  89. $form['reindex']['re-button'] = array(
  90. '#type' => 'submit',
  91. '#value' => t('Reindex Features'),
  92. '#weight' => 3,
  93. );
  94. }
  95. /**
  96. *
  97. * @ingroup tripal_organism
  98. */
  99. function tripal_organism_admin_validate($form, &$form_state) {
  100. global $user; // we need access to the user info
  101. $job_args = array();
  102. // -------------------------------------
  103. // Submit the Reindex Job if selected
  104. if ($form_state['values']['op'] == t('Reindex Features')) {
  105. $organisms = $form_state['values']['re-organisms'];
  106. foreach ($organisms as $organism_id) {
  107. if ($organism_id and preg_match("/^\d+$/i" , $organism_id)) {
  108. // get the organism info
  109. $sql = "SELECT * FROM {organism} WHERE organism_id = :organism_id";
  110. $organism = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  111. $job_args[0] = $organism_id;
  112. tripal_add_job("Reindex features for organism: $organism->genus " .
  113. "$organism->species", 'tripal_organism' ,
  114. 'tripal_organism_reindex_features', $job_args, $user->uid);
  115. }
  116. }
  117. }
  118. // -------------------------------------
  119. // Submit the taxonomy Job if selected
  120. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  121. $organisms = $form_state['values']['tx-organisms'];
  122. foreach ($organisms as $organism_id) {
  123. if ($organism_id and preg_match("/^\d+$/i", $organism_id)) {
  124. // get the organism info
  125. $sql = "SELECT * FROM {organism} WHERE organism_id = :organism_id";
  126. $organism = chado_query($sql, array(':organism_id' => $organism_id))->fetchObject();
  127. $job_args[0] = $organism_id;
  128. tripal_add_job("Set taxonomy for features in organism: " .
  129. "$organism->genus $organism->species" , 'tripal_organism',
  130. 'tripal_organism_taxonify_features', $job_args, $user->uid);
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. *
  137. * @ingroup tripal_organism
  138. */
  139. function tripal_organism_reindex_features($organism_id = NULL, $job_id = NULL) {
  140. $i = 0;
  141. if (!$organism_id) {
  142. return;
  143. }
  144. $sql = "
  145. SELECT *
  146. FROM {feature}
  147. WHERE organism_id = :organism_id
  148. ORDER BY feature_id";
  149. $results = chado_query($sql, array(':organism_id' => $organism_id));
  150. // load into ids array
  151. $count = 0;
  152. $ids = array();
  153. foreach ($results as $id) {
  154. $ids[$count] = $id->feature_id;
  155. $count++;
  156. }
  157. $interval = intval($count * 0.01);
  158. foreach ($ids as $feature_id) {
  159. // update the job status every 1% features
  160. if ($job_id and $i % $interval == 0) {
  161. tripal_job_set_progress($job_id , intval(($i/$count)*100));
  162. }
  163. $i++;
  164. }
  165. }
  166. /**
  167. *
  168. * @ingroup tripal_organism
  169. */
  170. function tripal_organism_taxonify_features($organism_id = NULL, $job_id = NULL) {
  171. $i = 0;
  172. if (!$organism_id) {
  173. return;
  174. }
  175. $sql = "
  176. SELECT *
  177. FROM {feature}
  178. WHERE organism_id = :organism_id
  179. ORDER BY feature_id
  180. ";
  181. $results = chado_query($sql, array(':organism_id' => $organism_id));
  182. // load into ids array
  183. $count = 0;
  184. $ids = array();
  185. foreach ($results as $id) {
  186. $ids[$count] = $id->feature_id;
  187. $count++;
  188. }
  189. // make sure our vocabularies are set before proceeding
  190. tripal_feature_set_vocabulary();
  191. // use this SQL for getting the nodes
  192. $nsql = "SELECT * FROM {chado_feature} CF " .
  193. " INNER JOIN {node} N ON N.nid = CF.nid " .
  194. "WHERE feature_id = :feature_id";
  195. // iterate through the features and set the taxonomy
  196. $interval = intval($count * 0.01);
  197. foreach ($ids as $feature_id) {
  198. // update the job status every 1% features
  199. if ($job_id and $i % $interval == 0) {
  200. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  201. }
  202. $node = db_query($nsql, array(':feature_id' => $feature_id))->fetchObject();
  203. tripal_feature_set_taxonomy($node, $feature_id);
  204. $i++;
  205. }
  206. }