tripal_organism.admin.inc 15 KB

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