tripal_organism.admin.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. /**
  3. * Administrative settings for chado_orgnism
  4. *
  5. * @ingroup tripal_organism
  6. */
  7. function tripal_organism_admin() {
  8. $form = array();
  9. // before proceeding check to see if we have any
  10. // currently processing jobs. If so, we don't want
  11. // to give the opportunity to sync libraries
  12. $active_jobs = FALSE;
  13. if (tripal_get_module_active_jobs('tripal_organism')) {
  14. $active_jobs = TRUE;
  15. }
  16. // add the field set for syncing libraries
  17. if (!$active_jobs) {
  18. get_tripal_organism_admin_form_sync_set($form);
  19. get_tripal_organism_admin_form_reindex_set($form);
  20. get_tripal_organism_admin_form_taxonomy_set($form);
  21. get_tripal_organism_admin_form_cleanup_set($form);
  22. }
  23. else {
  24. $form['notice'] = array(
  25. '#type' => 'fieldset',
  26. '#title' => t('Organism Management Temporarily Unavailable')
  27. );
  28. $form['notice']['message'] = array(
  29. '#value' => t('Currently, organism management jobs are waiting or are running. . Managemment features have been hidden until these jobs complete. Please check back later once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.'),
  30. );
  31. }
  32. return system_settings_form($form);
  33. }
  34. /**
  35. *
  36. *
  37. * @ingroup tripal_organism
  38. */
  39. function get_tripal_organism_admin_form_cleanup_set(&$form) {
  40. $form['cleanup'] = array(
  41. '#type' => 'fieldset',
  42. '#title' => t('Clean Up')
  43. );
  44. $form['cleanup']['description'] = array(
  45. '#type' => 'item',
  46. '#value' => t("With Drupal and chado residing in different databases ".
  47. "it is possible that nodes in Drupal and organisms in Chado become ".
  48. "\"orphaned\". This can occur if an organism node in Drupal is ".
  49. "deleted but the corresponding chado organism is not and/or vice ".
  50. "versa. Click the button below to resolve these discrepancies."),
  51. '#weight' => 1,
  52. );
  53. $form['cleanup']['button'] = array(
  54. '#type' => 'submit',
  55. '#value' => t('Clean up orphaned organisms'),
  56. '#weight' => 2,
  57. );
  58. }
  59. /**
  60. *
  61. *
  62. * @ingroup tripal_organism
  63. */
  64. function get_tripal_organism_admin_form_taxonomy_set(&$form) {
  65. $form['taxonify'] = array(
  66. '#type' => 'fieldset',
  67. '#title' => t('Assign Drupal Taxonomy to Organism Features')
  68. );
  69. // get the list of libraries
  70. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  71. $org_rset = chado_query($sql);
  72. // iterate through all of the libraries
  73. $org_boxes = array();
  74. while ($organism = db_fetch_object($org_rset)) {
  75. $org_boxes[$organism->organism_id] = "$organism->genus $organism->species";
  76. }
  77. $form['taxonify']['description'] = array(
  78. '#type' => 'item',
  79. '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  80. "nodes. These terms allow for advanced filtering during searching. This option allows ".
  81. "for setting taxonomy only for features that belong to the selected organisms below. All other features will be unaffected. To set taxonomy for all features in the site see the Feature Administration page."),
  82. '#weight' => 1,
  83. );
  84. $form['taxonify']['tx-organisms'] = array(
  85. '#title' => t('Organisms'),
  86. '#type' => t('checkboxes'),
  87. '#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."),
  88. '#required' => FALSE,
  89. '#prefix' => '<div id="lib_boxes">',
  90. '#suffix' => '</div>',
  91. '#options' => $org_boxes,
  92. '#weight' => 2
  93. );
  94. $form['taxonify']['tx-button'] = array(
  95. '#type' => 'submit',
  96. '#value' => t('Set Feature Taxonomy'),
  97. '#weight' => 3
  98. );
  99. }
  100. /**
  101. *
  102. * @ingroup tripal_organism
  103. */
  104. function get_tripal_organism_admin_form_reindex_set(&$form) {
  105. // define the fieldsets
  106. $form['reindex'] = array(
  107. '#type' => 'fieldset',
  108. '#title' => t('Reindex Organism Features')
  109. );
  110. // get the list of libraries
  111. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  112. $org_rset = chado_query($sql);
  113. // iterate through all of the libraries
  114. $org_boxes = array();
  115. while ($organism = db_fetch_object($org_rset)) {
  116. $org_boxes[$organism->organism_id] = "$organism->genus $organism->species";
  117. }
  118. $form['reindex']['description'] = array(
  119. '#type' => 'item',
  120. '#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."),
  121. '#weight' => 1,
  122. );
  123. $form['reindex']['re-organisms'] = array(
  124. '#title' => t('Organisms'),
  125. '#type' => t('checkboxes'),
  126. '#description' => t("Check the organisms whoee features you want to reindex. Note: this list contains all organisms, even those that may not be synced."),
  127. '#required' => FALSE,
  128. '#prefix' => '<div id="lib_boxes">',
  129. '#suffix' => '</div>',
  130. '#options' => $org_boxes,
  131. '#weight' => 2,
  132. );
  133. $form['reindex']['re-button'] = array(
  134. '#type' => 'submit',
  135. '#value' => t('Reindex Features'),
  136. '#weight' => 3,
  137. );
  138. }
  139. /**
  140. *
  141. * @ingroup tripal_organism
  142. */
  143. function get_tripal_organism_admin_form_sync_set(&$form) {
  144. // define the fieldsets
  145. $form['sync'] = array(
  146. '#type' => 'fieldset',
  147. '#title' => t('Sync Organisms')
  148. );
  149. // before proceeding check to see if we have any
  150. // currently processing jobs. If so, we don't want
  151. // to give the opportunity to sync libraries
  152. $active_jobs = FALSE;
  153. if (tripal_get_module_active_jobs('tripal_organism')) {
  154. $active_jobs = TRUE;
  155. }
  156. if (!$active_jobs) {
  157. // get the list of organisms
  158. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  159. $org_rset = chado_query($sql);
  160. // if we've added any organisms to the list that can be synced
  161. // then we want to build the form components to allow the user
  162. // to select one or all of them. Otherwise, just present
  163. // a message stating that all organisms are currently synced.
  164. $org_boxes = array();
  165. $added = 0;
  166. while ($organism = db_fetch_object($org_rset)) {
  167. // check to see if the organism is already present as a node in drupal.
  168. // if so, then skip it.
  169. $sql = "SELECT * FROM {chado_organism} WHERE organism_id = %d";
  170. if (!db_fetch_object(db_query($sql, $organism->organism_id))) {
  171. $org_boxes[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  172. $added++;
  173. }
  174. }
  175. // if we have organisms we need to add to the checkbox then
  176. // build that form element
  177. if ($added > 0) {
  178. $org_boxes['all'] = "All Organisms";
  179. $form['sync']['organisms'] = array(
  180. '#title' => t('Available Organisms'),
  181. '#type' => t('checkboxes'),
  182. '#description' => t("Check the organisms you want to sync. Drupal content will be created for each of the organisms listed above. Select 'All Organisms' to sync all of them."),
  183. '#required' => FALSE,
  184. '#prefix' => '<div id="org_boxes">',
  185. '#suffix' => '</div>',
  186. '#options' => $org_boxes,
  187. );
  188. $form['sync']['button'] = array(
  189. '#type' => 'submit',
  190. '#value' => t('Submit Sync Job')
  191. );
  192. }
  193. // we don't have any organisms to select from
  194. else {
  195. $form['sync']['value'] = array(
  196. '#value' => t('All organisms in Chado are currently synced with Drupal.')
  197. );
  198. }
  199. }
  200. // we don't want to present a form since we have an active job running
  201. else {
  202. $form['sync']['value'] = array(
  203. '#value' => t('Currently, jobs exist related to chado organisms. Please check back later for organisms that can by synced once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.')
  204. );
  205. }
  206. }
  207. /**
  208. *
  209. * @ingroup tripal_organism
  210. */
  211. function tripal_organism_admin_validate($form, &$form_state) {
  212. global $user; // we need access to the user info
  213. $job_args = array();
  214. if ($form_state['values']['op'] == t('Submit Sync Job')) {
  215. // check to see if the user wants to sync chado and drupal. If
  216. // so then we need to register a job to do so with tripal
  217. $organisms = $form_state['values']['organisms'];
  218. $do_all = FALSE;
  219. $to_sync = array();
  220. foreach ($organisms as $organism_id) {
  221. if (preg_match("/^all$/i" , $organism_id)) {
  222. $do_all = TRUE;
  223. }
  224. if ($organism_id and preg_match("/^\d+$/i" , $organism_id)) {
  225. // get the list of organisms
  226. $sql = "SELECT * FROM {Organism} WHERE organism_id = %d";
  227. $organism = db_fetch_object(chado_query($sql, $organism_id));
  228. $to_sync[$organism_id] = "$organism->genus $organism->species";
  229. }
  230. }
  231. // submit the job the tripal job manager
  232. if ($do_all) {
  233. tripal_add_job('Sync all organisms' , 'tripal_organism',
  234. 'tripal_organism_sync_organisms' , $job_args , $user->uid);
  235. }
  236. else{
  237. foreach ($to_sync as $organism_id => $name) {
  238. $job_args[0] = $organism_id;
  239. tripal_add_job("Sync organism: $name" , 'tripal_organism',
  240. 'tripal_organism_sync_organisms' , $job_args , $user->uid);
  241. }
  242. }
  243. }
  244. // -------------------------------------
  245. // Submit the Reindex Job if selected
  246. if ($form_state['values']['op'] == t('Reindex Features')) {
  247. $organisms = $form_state['values']['re-organisms'];
  248. foreach ($organisms as $organism_id) {
  249. if ($organism_id and preg_match("/^\d+$/i" , $organism_id)) {
  250. // get the organism info
  251. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  252. $organism = db_fetch_object(chado_query($sql , $organism_id));
  253. $job_args[0] = $organism_id;
  254. tripal_add_job("Reindex features for organism: $organism->genus ".
  255. "$organism->species", 'tripal_organism' ,
  256. 'tripal_organism_reindex_features', $job_args, $user->uid);
  257. }
  258. }
  259. }
  260. // -------------------------------------
  261. // Submit the taxonomy Job if selected
  262. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  263. $organisms = $form_state['values']['tx-organisms'];
  264. foreach ($organisms as $organism_id) {
  265. if ($organism_id and preg_match("/^\d+$/i", $organism_id)) {
  266. // get the organism info
  267. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  268. $organism = db_fetch_object(chado_query($sql , $organism_id));
  269. $job_args[0] = $organism_id;
  270. tripal_add_job("Set taxonomy for features in organism: ".
  271. "$organism->genus $organism->species" , 'tripal_organism',
  272. 'tripal_organism_taxonify_features', $job_args, $user->uid);
  273. }
  274. }
  275. }
  276. // -------------------------------------
  277. // Submit the Cleanup Job if selected
  278. if ($form_state['values']['op'] == t('Clean up orphaned organisms')) {
  279. tripal_add_job('Cleanup orphaned organisms', 'tripal_organism',
  280. 'tripal_organism_cleanup', $job_args, $user->uid);
  281. }
  282. }
  283. /**
  284. * Synchronize organisms from chado to drupal
  285. *
  286. * @ingroup tripal_organism
  287. */
  288. function tripal_organism_sync_organisms($organism_id = NULL, $job_id = NULL) {
  289. global $user;
  290. $page_content = '';
  291. if (!$organism_id) {
  292. $sql = "SELECT * FROM {Organism} O";
  293. $results = chado_query($sql);
  294. }
  295. else {
  296. $sql = "SELECT * FROM {organism} L WHERE organism_id = %d";
  297. $results = chado_query($sql, $organism_id);
  298. }
  299. // We'll use the following SQL statement for checking if the organism
  300. // already exists as a drupal node.
  301. $sql = "SELECT * FROM {chado_organism} ".
  302. "WHERE organism_id = %d";
  303. while ($organism = db_fetch_object($results)) {
  304. // check if this organism already exists in the drupal database. if it
  305. // does then skip this organism and go to the next one.
  306. if (!db_fetch_object(db_query($sql, $organism->organism_id))) {
  307. $new_node = new stdClass();
  308. $new_node->type = 'chado_organism';
  309. $new_node->uid = $user->uid;
  310. $new_node->title = "$organism->genus $organism->species";
  311. $new_node->organism_id = $organism->organism_id;
  312. $new_node->genus = $organism->genus;
  313. $new_node->species = $organism->species;
  314. $new_node->description = '';
  315. node_validate($new_node);
  316. if (!form_get_errors()) {
  317. $node = node_submit($new_node);
  318. node_save($node);
  319. if ($node->nid) {
  320. print "Added $organism->common_name\n";
  321. }
  322. }
  323. else {
  324. print "Failed to insert organism $organism->common_name\n";
  325. }
  326. }
  327. else {
  328. print "Skipped $organism->common_name\n";
  329. }
  330. }
  331. return $page_content;
  332. }
  333. /**
  334. *
  335. * @ingroup tripal_organism
  336. */
  337. function tripal_organism_reindex_features($organism_id = NULL, $job_id = NULL) {
  338. $i = 0;
  339. if (!$organism_id) {
  340. return;
  341. }
  342. $sql = "SELECT * ".
  343. "FROM {feature} ".
  344. "WHERE organism_id = $organism_id ".
  345. "ORDER BY feature_id";
  346. $results = chado_query($sql);
  347. // load into ids array
  348. $count = 0;
  349. $ids = array();
  350. while ($id = db_fetch_object($results)) {
  351. $ids[$count] = $id->feature_id;
  352. $count++;
  353. }
  354. $interval = intval($count * 0.01);
  355. foreach ($ids as $feature_id) {
  356. // update the job status every 1% features
  357. if ($job_id and $i % $interval == 0) {
  358. tripal_job_set_progress($job_id , intval(($i/$count)*100));
  359. }
  360. $i++;
  361. }
  362. }
  363. /**
  364. *
  365. * @ingroup tripal_organism
  366. */
  367. function tripal_organism_taxonify_features($organism_id = NULL, $job_id = NULL) {
  368. $i = 0;
  369. if (!$organism_id) {
  370. return;
  371. }
  372. $sql = "SELECT * ".
  373. "FROM {feature} ".
  374. "WHERE organism_id = $organism_id ".
  375. "ORDER BY feature_id";
  376. $results = chado_query($sql);
  377. // load into ids array
  378. $count = 0;
  379. $ids = array();
  380. while ($id = db_fetch_object($results)) {
  381. $ids[$count] = $id->feature_id;
  382. $count++;
  383. }
  384. // make sure our vocabularies are set before proceeding
  385. tripal_feature_set_vocabulary();
  386. // use this SQL for getting the nodes
  387. $nsql = "SELECT * FROM {chado_feature} CF ".
  388. " INNER JOIN {node} N ON N.nid = CF.nid ".
  389. "WHERE feature_id = %d";
  390. // iterate through the features and set the taxonomy
  391. $interval = intval($count * 0.01);
  392. foreach ($ids as $feature_id) {
  393. // update the job status every 1% features
  394. if ($job_id and $i % $interval == 0) {
  395. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  396. }
  397. $node = db_fetch_object(db_query($nsql, $feature_id));
  398. tripal_feature_set_taxonomy($node, $feature_id);
  399. $i++;
  400. }
  401. }
  402. /**
  403. * Remove orphaned drupal nodes
  404. *
  405. * @param $dummy
  406. * Not Used -kept for backwards compatibility
  407. * @param $job_id
  408. * The id of the tripal job executing this function
  409. *
  410. * @ingroup tripal_organism
  411. */
  412. function tripal_organism_cleanup($dummy = NULL, $job_id = NULL) {
  413. return tripal_core_clean_orphaned_nodes('organism', $job_id);
  414. }