tripal_analysis.admin.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions displaying administrative pages and forms
  5. */
  6. /**
  7. * Administration page callbacks for the Tripal Analysis module
  8. *
  9. * We have defined a hook_get_settings() function. When a sub-module
  10. * is enabled, we'll look for this function to provide a form for the
  11. * administrative setting.
  12. *
  13. * @return
  14. * A form API array describing an administrative form
  15. *
  16. * @ingroup tripal_analysis
  17. */
  18. function tripal_analysis_admin() {
  19. // Create a new administrative form. We'll add main functions to the form
  20. // first (Sync, Reindex, Clean, Taxonify). Thereafter, any sub-module that
  21. // has a setting will be added.
  22. $form = array();
  23. // before proceeding check to see if we have any
  24. // currently processing jobs. If so, we don't want
  25. // to give the opportunity to sync libraries
  26. $active_jobs = FALSE;
  27. if (tripal_get_module_active_jobs('tripal_organism')) {
  28. $active_jobs = TRUE;
  29. }
  30. // add the field set for syncing libraries
  31. if (!$active_jobs) {
  32. // add the field set for syncing analyses
  33. get_tripal_analysis_admin_form_sync_set($form);
  34. // get_tripal_analysis_admin_form_reindex_set($form);
  35. // get_tripal_analysis_admin_form_taxonomy_set($form);
  36. get_tripal_analysis_admin_form_cleanup_set($form);
  37. }
  38. else {
  39. $form['notice'] = array(
  40. '#type' => 'fieldset',
  41. '#title' => t('Analysis Management Temporarily Unavailable')
  42. );
  43. $form['notice']['message'] = array(
  44. '#value' => t('Currently, analysis 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.'),
  45. );
  46. }
  47. // Add sub-module settings. Pull all sub-module information from
  48. // {tripal_analysis} table
  49. $sql = "SELECT modulename FROM {tripal_analysis}";
  50. $result = db_query($sql);
  51. $counter = 0; //keep track of the number of sub-modules
  52. while ($data = db_fetch_object($result)) {
  53. // Check if the hook_get_settings() function is already defined.
  54. $func = $data->modulename . "_get_settings";
  55. $functions = get_defined_functions();
  56. $settings;
  57. foreach ($functions['user'] as $function) {
  58. if ($function == $func) {
  59. $settings = $func();
  60. }
  61. }
  62. // Add sub-module's specific settings to the administrative view
  63. if ($settings) {
  64. // Define a fieldset for the sub-module
  65. $form["field$counter"] = array(
  66. '#type' => 'fieldset',
  67. '#title' => "$settings->title",
  68. '#collapsible' => TRUE
  69. );
  70. $form["field$counter"]["$settings->title"] = $settings->form;
  71. }
  72. $counter ++;
  73. }
  74. return system_settings_form($form);
  75. }
  76. /**
  77. * Displays the Set Drupal Taxonomy for Analysis Features From
  78. *
  79. * @param $form
  80. * The administrative form as it is currently
  81. *
  82. * @return
  83. * A form API array describing an administrative form
  84. *
  85. * @ingroup tripal_analysis
  86. */
  87. function get_tripal_analysis_admin_form_taxonomy_set(&$form) {
  88. $form['taxonify'] = array(
  89. '#type' => 'fieldset',
  90. '#title' => t('Assign Drupal Taxonomy to Analysis Features')
  91. );
  92. // get the list of analyses
  93. $sql = "SELECT * FROM {analysis} ORDER BY name";
  94. $lib_rset = chado_query($sql);
  95. // iterate through all of the libraries
  96. $lib_boxes = array();
  97. while ($analysis = db_fetch_object($lib_rset)) {
  98. $lib_boxes[$analysis->analysis_id] = "$analysis->name";
  99. }
  100. $form['taxonify']['description'] = array(
  101. '#type' => 'item',
  102. '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  103. "nodes. These terms allow for advanced filtering during searching. This option allows ".
  104. "for setting taxonomy only for features that belong to the selected analyses below. All other features will be unaffected. To set taxonomy for all features in the site see the Feature Administration page."),
  105. '#weight' => 1,
  106. );
  107. $form['taxonify']['tx-analyses'] = array(
  108. '#title' => t('Analyses'),
  109. '#type' => t('checkboxes'),
  110. '#description' => t("Check the analyses whose features you want to reset taxonomy. Note: this list contains all analyses, even those that may not be synced."),
  111. '#required' => FALSE,
  112. '#prefix' => '<div id="lib_boxes">',
  113. '#suffix' => '</div>',
  114. '#options' => $lib_boxes,
  115. '#weight' => 2
  116. );
  117. $form['taxonify']['tx-button'] = array(
  118. '#type' => 'submit',
  119. '#value' => t('Set Feature Taxonomy'),
  120. '#weight' => 3
  121. );
  122. }
  123. /**
  124. * The "Reindex Analysis Nodes" form
  125. *
  126. * @param $form
  127. * The administrative form as it is currently
  128. *
  129. * @return
  130. * A form API array describing an administrative form
  131. *
  132. * @ingroup tripal_analysis
  133. */
  134. function get_tripal_analysis_admin_form_reindex_set(&$form) {
  135. // define the fieldsets
  136. $form['reindex'] = array(
  137. '#type' => 'fieldset',
  138. '#title' => t('Reindex Analysis Features')
  139. );
  140. // get the list of libraries
  141. $sql = "SELECT * FROM {analysis} ORDER BY name";
  142. $lib_rset = chado_query($sql);
  143. // iterate through all of the libraries
  144. $lib_boxes = array();
  145. while ($analysis = db_fetch_object($lib_rset)) {
  146. $lib_boxes[$analysis->analysis_id] = "$analysis->name";
  147. }
  148. $form['reindex']['description'] = array(
  149. '#type' => 'item',
  150. '#value' => t("This option allows for reindexing of only those features that belong to the selected analyses below. All other features will be unaffected. To reindex all features in the site see the Feature Administration page."),
  151. '#weight' => 1,
  152. );
  153. $form['reindex']['re-analyses'] = array(
  154. '#title' => t('Libraries'),
  155. '#type' => t('checkboxes'),
  156. '#description' => t("Check the analyses whoee features you want to reindex. Note: this list contains all analyses, even those that may not be synced."),
  157. '#required' => FALSE,
  158. '#prefix' => '<div id="lib_boxes">',
  159. '#suffix' => '</div>',
  160. '#options' => $lib_boxes,
  161. '#weight' => 2,
  162. );
  163. $form['reindex']['re-button'] = array(
  164. '#type' => 'submit',
  165. '#value' => t('Reindex Features'),
  166. '#weight' => 3,
  167. );
  168. }
  169. /**
  170. * The "Clean-up orphaned analysis & nodes" Form
  171. *
  172. * @param $form
  173. * The administrative form as it is currently
  174. *
  175. * @return
  176. * A form API array describing an administrative form
  177. *
  178. * @ingroup tripal_analysis
  179. */
  180. function get_tripal_analysis_admin_form_cleanup_set(&$form) {
  181. $form['cleanup'] = array(
  182. '#type' => 'fieldset',
  183. '#title' => t('Clean Up')
  184. );
  185. $form['cleanup']['description'] = array(
  186. '#type' => 'item',
  187. '#value' => t("With Drupal and chado residing in different databases ".
  188. "it is possible that nodes in Drupal and analyses in Chado become ".
  189. "\"orphaned\". This can occur if an analysis node in Drupal is ".
  190. "deleted but the corresponding chado analysis is not and/or vice ".
  191. "versa. Click the button below to resolve these discrepancies."),
  192. '#weight' => 1,
  193. );
  194. $form['cleanup']['button'] = array(
  195. '#type' => 'submit',
  196. '#value' => t('Clean up orphaned analyses'),
  197. '#weight' => 2,
  198. );
  199. }
  200. /**
  201. * The "sync Analysis in chado with drupal" form
  202. *
  203. * @param $form
  204. * The administrative form as it is currently
  205. *
  206. * @return
  207. * A form API array describing an administrative form
  208. *
  209. * @ingroup tripal_analysis
  210. */
  211. function get_tripal_analysis_admin_form_sync_set(&$form) {
  212. // define the fieldsets
  213. $form['sync'] = array(
  214. '#type' => 'fieldset',
  215. '#title' => t('Sync Analyses')
  216. );
  217. // before proceeding check to see if we have any
  218. // currently processing jobs. If so, we don't want
  219. // to give the opportunity to sync analyses
  220. $active_jobs = FALSE;
  221. if (tripal_get_module_active_jobs('tripal_analysis')) {
  222. $active_jobs = TRUE;
  223. }
  224. if (!$active_jobs) {
  225. // get the list of analyses
  226. $sql = "SELECT * FROM {analysis} ORDER BY name";
  227. $ana_rset = chado_query($sql);
  228. // if we've added any analyses to the list that can be synced
  229. // then we want to build the form components to allow the user
  230. // to select one or all of them. Otherwise, just present
  231. // a message stating that all analyses are currently synced.
  232. $ana_boxes = array();
  233. $added = 0;
  234. while ($analysis = db_fetch_object($ana_rset)) {
  235. // check to see if the analysis is already present as a node in drupal.
  236. // if so, then skip it.
  237. $sql = "SELECT * FROM {chado_analysis} WHERE analysis_id = %d";
  238. if (!db_fetch_object(db_query($sql, $analysis->analysis_id))) {
  239. $ana_boxes[$analysis->analysis_id] = "$analysis->name";
  240. $added++;
  241. }
  242. }
  243. // if we have analyses we need to add to the checkbox then
  244. // build that form element
  245. if ($added > 0) {
  246. $ana_boxes['all'] = "All analyses";
  247. $form['sync']['analyses'] = array(
  248. '#title' => t('Available analyses'),
  249. '#type' => t('checkboxes'),
  250. '#description' => t("Check the analyses you want to sync. Drupal ".
  251. "content will be created for each of the analyses listed above. ".
  252. "Select 'All analyses' to sync all of them."),
  253. '#required' => FALSE,
  254. '#prefix' => '<div id="ana_boxes">',
  255. '#suffix' => '</div>',
  256. '#options' => $ana_boxes,
  257. );
  258. $form['sync']['button'] = array(
  259. '#type' => 'submit',
  260. '#value' => t('Submit Sync Job')
  261. );
  262. }
  263. // we don't have any analyses to select from
  264. else {
  265. $form['sync']['value'] = array(
  266. '#value' => t('All analyses in Chado are currently synced with Drupal.')
  267. );
  268. }
  269. }
  270. // we don't want to present a form since we have an active job running
  271. else {
  272. $form['sync']['value'] = array(
  273. '#value' => t('Currently, jobs exist related to chado analyses. Please check back later for analyses that can by synced once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.')
  274. );
  275. }
  276. }
  277. /**
  278. * Validate the administrative form
  279. * @todo Stephen: Why is validate used rather then submit?
  280. *
  281. * @param $form
  282. * The form API array of the form to be validated
  283. * @form_state
  284. * The user submitted values
  285. *
  286. * @ingroup tripal_analysis
  287. */
  288. function tripal_analysis_admin_validate($form, &$form_state) {
  289. global $user; // we need access to the user info
  290. $job_args = array();
  291. if ($form_state['values']['op'] == t('Submit Sync Job')) {
  292. // check to see if the user wants to sync chado and drupal. If
  293. // so then we need to register a job to do so with tripal
  294. $analyses = $form_state['values']['analyses'];
  295. $do_all = FALSE;
  296. $to_sync = array();
  297. foreach ($analyses as $analysis_id) {
  298. if (preg_match("/^all$/i", $analysis_id)) {
  299. $do_all = TRUE;
  300. }
  301. if ($analysis_id and preg_match("/^\d+$/i", $analysis_id)) {
  302. // get the list of analyses
  303. $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d";
  304. $analysis = db_fetch_object(chado_query($sql, $analysis_id));
  305. $to_sync[$analysis_id] = $analysis->name;
  306. }
  307. }
  308. // submit the job the tripal job manager
  309. if ($do_all) {
  310. tripal_add_job('Sync all analyses', 'tripal_analysis', 'tripal_analysis_sync_analyses', $job_args, $user->uid);
  311. }
  312. else{
  313. foreach ($to_sync as $analysis_id => $name) {
  314. $job_args[0] = $analysis_id;
  315. tripal_add_job("Sync analysis: $name", 'tripal_analysis', 'tripal_analysis_sync_analyses', $job_args, $user->uid);
  316. }
  317. }
  318. }
  319. // -------------------------------------
  320. // Submit the Reindex Job if selected
  321. if ($form_state['values']['op'] == t('Reindex Features')) {
  322. global $user; // we need access to the user info
  323. $job_args = array();
  324. $analyses = $form_state['values']['re-analyses'];
  325. foreach ($analyses as $analysis_id) {
  326. if ($analysis_id and preg_match("/^\d+$/i", $analysis_id)) {
  327. // get the analysis info
  328. $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d";
  329. $analysis = db_fetch_object(chado_query($sql, $analysis_id));
  330. $job_args[0] = $analysis_id;
  331. tripal_add_job("Reindex features for analysis: $analysis->name", 'tripal_analysis',
  332. 'tripal_analysis_reindex_features', $job_args, $user->uid);
  333. }
  334. }
  335. }
  336. // -------------------------------------
  337. // Submit the Taxonomy Job if selected
  338. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  339. global $user; // we need access to the user info
  340. $job_args = array();
  341. $analyses = $form_state['values']['tx-analyses'];
  342. foreach ($analyses as $analysis_id) {
  343. if ($analysis_id and preg_match("/^\d+$/i", $analysis_id)) {
  344. // get the analysis info
  345. $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d";
  346. $analysis = db_fetch_object(chado_query($sql, $analysis_id));
  347. $job_args[0] = $analysis_id;
  348. tripal_add_job("Set taxonomy for features in analysis: $analysis->name", 'tripal_analysis',
  349. 'tripal_analysis_taxonify_features', $job_args, $user->uid);
  350. }
  351. }
  352. }
  353. // -------------------------------------
  354. // Submit the Cleanup Job if selected
  355. if ($form_state['values']['op'] == t('Clean up orphaned analyses')) {
  356. tripal_add_job('Cleanup orphaned analyses', 'tripal_analysis',
  357. 'tripal_analyses_cleanup', $job_args, $user->uid);
  358. }
  359. }
  360. /**
  361. * Synchronize analyses from chado to drupal
  362. *
  363. * @ingroup tripal_analysis
  364. */
  365. function tripal_analysis_sync_analyses($analysis_id = NULL, $job_id = NULL) {
  366. global $user;
  367. $page_content = '';
  368. if (!$analysis_id) {
  369. $sql = "SELECT Analysis_id, name, description, program, ".
  370. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  371. " timeexecuted ".
  372. "FROM {Analysis} ";
  373. $results = chado_query($sql);
  374. }
  375. else {
  376. $sql = "SELECT Analysis_id, name, description, program, ".
  377. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  378. " timeexecuted ".
  379. "FROM {Analysis} ".
  380. "WHERE analysis_id = %d";
  381. $results = chado_query($sql, $analysis_id);
  382. }
  383. // We'll use the following SQL statement for checking if the analysis
  384. // already exists as a drupal node.
  385. $sql = "SELECT * FROM {chado_analysis} ".
  386. "WHERE analysis_id = %d";
  387. while ($analysis = db_fetch_object($results)) {
  388. print "syncing analysis ";
  389. print $analysis->name;
  390. print ", ";
  391. print $analysis->analysis_id;
  392. print "\n";
  393. // check if this analysis already exists in the drupal database. if it
  394. // does then skip this analysis and go to the next one.
  395. if (!db_fetch_object(db_query($sql, $analysis->analysis_id))) {
  396. $new_node = new stdClass();
  397. // try to access analysis type for this analysis
  398. $sql = "SELECT * FROM {analysisprop}
  399. WHERE analysis_id = %d
  400. AND type_id =
  401. (SELECT cvterm_id from {cvterm} where name = '%s')
  402. ";
  403. $analysis_type = db_fetch_object(chado_query($sql, $analysis->analysis_id, "analysis_type"));
  404. // Get the type of analysis using cvterm_id
  405. // Current possibilities: kegg, unigene, interpro, blast
  406. if ($analysis_type) {
  407. // This is a unigene analysis
  408. if ($analysis_type->value == 'tripal_analysis_unigene') {
  409. $new_node->type = 'chado_analysis_unigene';
  410. // This is a blast analysis
  411. }
  412. elseif ($analysis_type->value == 'tripal_analysis_blast') {
  413. $new_node->type = 'chado_analysis_blast';
  414. // This is a interpro analysis
  415. }
  416. elseif ($analysis_type->value == 'tripal_analysis_interpro') {
  417. $new_node->type = 'chado_analysis_interpro';
  418. // This is a kegg analysis
  419. }
  420. elseif ($analysis_type->value == 'tripal_analysis_kegg' ) {
  421. $new_node->type = 'chado_analysis_kegg';
  422. }
  423. else {
  424. $new_node->type = 'chado_analysis';
  425. }
  426. // If it doesn't exist, this analysis is generic
  427. }
  428. else {
  429. $new_node->type = 'chado_analysis';
  430. }
  431. print "analysis type is $new_node->type\n";
  432. $new_node->uid = $user->uid;
  433. $new_node->analysis_id = $analysis->analysis_id;
  434. $new_node->analysisname = $analysis->name;
  435. $new_node->description = $analysis->description;
  436. $new_node->program = $analysis->program;
  437. $new_node->programversion = $analysis->programversion;
  438. $new_node->algorithm = $analysis->algorithm;
  439. $new_node->sourcename = $analysis->sourcename;
  440. $new_node->sourceversion = $analysis->sourceversion;
  441. $new_node->sourceuri = $analysis->sourceuri;
  442. $new_node->timeexecuted = $analysis->timeexecuted;
  443. // If the analysis has a name, use it as the node title. If not,
  444. // construct the title using program, programversion, and sourcename
  445. if ($new_node->analysisname) {
  446. $new_node->title = $new_node->analysisname;
  447. }
  448. else {
  449. //Construct node title as "program (version)"
  450. $new_node->title = "$analysis->program ($analysis->programversion)";
  451. }
  452. node_validate($new_node);
  453. $errors = form_get_errors();
  454. if ($errors) {
  455. print_r($errors);
  456. }
  457. else{
  458. // if(!form_get_errors()){
  459. $node = node_submit($new_node);
  460. node_save($node);
  461. if ($node->nid) {
  462. $page_content .= "Added $new_node->title<br />";
  463. }
  464. }
  465. }
  466. else {
  467. $page_content .= "Skipped $new_node->title<br />";
  468. }
  469. }
  470. return $page_content;
  471. }
  472. /**
  473. * Remove orphaned drupal nodes
  474. *
  475. * @param $dummy
  476. * Not Used -kept for backwards compatibility
  477. * @param $job_id
  478. * The id of the tripal job executing this function
  479. *
  480. * @ingroup tripal_analysis
  481. */
  482. function tripal_analyses_cleanup($dummy = NULL, $job_id = NULL) {
  483. return tripal_core_clean_orphaned_nodes('analysis', $job_id);
  484. }