tripal_analysis.admin.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. //
  3. // Copyright 2009 Clemson University
  4. //
  5. /*******************************************************************************
  6. * Administration page callbacks for the Tripal Analysis module
  7. * We have defined a hook_get_settings() function. When a sub-module
  8. * is enabled, we'll look for this function to provide a form for the
  9. * administrative setting.
  10. */
  11. function tripal_analysis_admin() {
  12. // Create a new administrative form. We'll add main functions to the form
  13. // first (Sync, Reindex, Clean, Taxonify). Thereafter, any sub-module that
  14. // has a setting will be added.
  15. $form = array();
  16. // before proceeding check to see if we have any
  17. // currently processing jobs. If so, we don't want
  18. // to give the opportunity to sync libraries
  19. $active_jobs = FALSE;
  20. if(tripal_get_module_active_jobs('tripal_organism')){
  21. $active_jobs = TRUE;
  22. }
  23. // add the field set for syncing libraries
  24. if(!$active_jobs){
  25. // add the field set for syncing analyses
  26. get_tripal_analysis_admin_form_sync_set($form);
  27. get_tripal_analysis_admin_form_reindex_set($form);
  28. get_tripal_analysis_admin_form_taxonomy_set($form);
  29. get_tripal_analysis_admin_form_cleanup_set($form);
  30. } else {
  31. $form['notice'] = array(
  32. '#type' => 'fieldset',
  33. '#title' => t('Analysis Management Temporarily Unavailable')
  34. );
  35. $form['notice']['message'] = array(
  36. '#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.'),
  37. );
  38. }
  39. // Add sub-module settings. Pull all sub-module information from
  40. // {tripal_analysis} table
  41. $sql = "SELECT modulename FROM {tripal_analysis}";
  42. $result = db_query($sql);
  43. $counter = 0; //keep track of the number of sub-modules
  44. while ($data = db_fetch_object($result)) {
  45. // Check if the hook_get_settings() function is already defined.
  46. $func = $data->modulename."_get_settings";
  47. $functions = get_defined_functions();
  48. $settings;
  49. foreach($functions['user'] as $function) {
  50. if ($function == $func) {
  51. $settings = $func();
  52. }
  53. }
  54. // Add sub-module's specific settings to the administrative view
  55. if ($settings) {
  56. // Define a fieldset for the sub-module
  57. $form["field$counter"] = array(
  58. '#type' => 'fieldset',
  59. '#title' => "$settings->title",
  60. '#collapsible' => TRUE
  61. );
  62. $form["field$counter"]["$settings->title"] = $settings->form;
  63. }
  64. $counter ++;
  65. }
  66. return system_settings_form($form);
  67. }
  68. /************************************************************************
  69. *
  70. */
  71. function get_tripal_analysis_admin_form_taxonomy_set(&$form) {
  72. $form['taxonify'] = array(
  73. '#type' => 'fieldset',
  74. '#title' => t('Assign Drupal Taxonomy to Analysis Features')
  75. );
  76. // get the list of analyses
  77. $sql = "SELECT * FROM {Analysis} ORDER BY name";
  78. $previous_db = tripal_db_set_active('chado'); // use chado database
  79. $lib_rset = db_query($sql);
  80. tripal_db_set_active($previous_db); // now use drupal database
  81. // iterate through all of the libraries
  82. $lib_boxes = array();
  83. while($analysis = db_fetch_object($lib_rset)){
  84. $lib_boxes[$analysis->analysis_id] = "$analysis->name";
  85. }
  86. $form['taxonify']['description'] = array(
  87. '#type' => 'item',
  88. '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  89. "nodes. These terms allow for advanced filtering during searching. This option allows ".
  90. "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."),
  91. '#weight' => 1,
  92. );
  93. $form['taxonify']['tx-analyses'] = array (
  94. '#title' => t('Analyses'),
  95. '#type' => t('checkboxes'),
  96. '#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."),
  97. '#required' => FALSE,
  98. '#prefix' => '<div id="lib_boxes">',
  99. '#suffix' => '</div>',
  100. '#options' => $lib_boxes,
  101. '#weight' => 2
  102. );
  103. $form['taxonify']['tx-button'] = array(
  104. '#type' => 'submit',
  105. '#value' => t('Set Feature Taxonomy'),
  106. '#weight' => 3
  107. );
  108. }
  109. /************************************************************************
  110. *
  111. */
  112. function get_tripal_analysis_admin_form_reindex_set(&$form) {
  113. // define the fieldsets
  114. $form['reindex'] = array(
  115. '#type' => 'fieldset',
  116. '#title' => t('Reindex Analysis Features')
  117. );
  118. // get the list of libraries
  119. $sql = "SELECT * FROM {Analysis} ORDER BY name";
  120. $previous_db = tripal_db_set_active('chado'); // use chado database
  121. $lib_rset = db_query($sql);
  122. tripal_db_set_active($previous_db); // now use drupal database
  123. // iterate through all of the libraries
  124. $lib_boxes = array();
  125. while($analysis = db_fetch_object($lib_rset)){
  126. $lib_boxes[$analysis->analysis_id] = "$analysis->name";
  127. }
  128. $form['reindex']['description'] = array(
  129. '#type' => 'item',
  130. '#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."),
  131. '#weight' => 1,
  132. );
  133. $form['reindex']['re-analyses'] = array (
  134. '#title' => t('Libraries'),
  135. '#type' => t('checkboxes'),
  136. '#description' => t("Check the analyses whoee features you want to reindex. Note: this list contains all analyses, even those that may not be synced."),
  137. '#required' => FALSE,
  138. '#prefix' => '<div id="lib_boxes">',
  139. '#suffix' => '</div>',
  140. '#options' => $lib_boxes,
  141. '#weight' => 2,
  142. );
  143. $form['reindex']['re-button'] = array(
  144. '#type' => 'submit',
  145. '#value' => t('Reindex Features'),
  146. '#weight' => 3,
  147. );
  148. }
  149. /************************************************************************
  150. *
  151. */
  152. function get_tripal_analysis_admin_form_cleanup_set(&$form) {
  153. $form['cleanup'] = array(
  154. '#type' => 'fieldset',
  155. '#title' => t('Clean Up')
  156. );
  157. $form['cleanup']['description'] = array(
  158. '#type' => 'item',
  159. '#value' => t("With Drupal and chado residing in different databases ".
  160. "it is possible that nodes in Drupal and analyses in Chado become ".
  161. "\"orphaned\". This can occur if an analysis node in Drupal is ".
  162. "deleted but the corresponding chado analysis is not and/or vice ".
  163. "versa. Click the button below to resolve these discrepancies."),
  164. '#weight' => 1,
  165. );
  166. $form['cleanup']['button'] = array(
  167. '#type' => 'submit',
  168. '#value' => t('Clean up orphaned analyses'),
  169. '#weight' => 2,
  170. );
  171. }
  172. /************************************************************************
  173. *
  174. */
  175. function get_tripal_analysis_admin_form_sync_set (&$form) {
  176. // define the fieldsets
  177. $form['sync'] = array(
  178. '#type' => 'fieldset',
  179. '#title' => t('Sync Analyses')
  180. );
  181. // before proceeding check to see if we have any
  182. // currently processing jobs. If so, we don't want
  183. // to give the opportunity to sync analyses
  184. $active_jobs = FALSE;
  185. if(tripal_get_module_active_jobs('tripal_analysis')){
  186. $active_jobs = TRUE;
  187. }
  188. if(!$active_jobs){
  189. // get the list of analyses
  190. $sql = "SELECT * FROM {analysis} ORDER BY name";
  191. $previous_db = tripal_db_set_active('chado'); // use chado database
  192. $ana_rset = db_query($sql);
  193. tripal_db_set_active($previous_db); // now use drupal database
  194. // if we've added any analyses to the list that can be synced
  195. // then we want to build the form components to allow the user
  196. // to select one or all of them. Otherwise, just present
  197. // a message stating that all analyses are currently synced.
  198. $ana_boxes = array();
  199. $added = 0;
  200. while($analysis = db_fetch_object($ana_rset)){
  201. // check to see if the analysis is already present as a node in drupal.
  202. // if so, then skip it.
  203. $sql = "SELECT * FROM {chado_analysis} WHERE analysis_id = %d";
  204. if(!db_fetch_object(db_query($sql,$analysis->analysis_id))){
  205. $ana_boxes[$analysis->analysis_id] = "$analysis->name";
  206. $added++;
  207. }
  208. }
  209. // if we have analyses we need to add to the checkbox then
  210. // build that form element
  211. if($added > 0){
  212. $ana_boxes['all'] = "All analyses";
  213. $form['sync']['analyses'] = array (
  214. '#title' => t('Available analyses'),
  215. '#type' => t('checkboxes'),
  216. '#description' => t("Check the analyses you want to sync. Drupal ".
  217. "content will be created for each of the analyses listed above. ".
  218. "Select 'All analyses' to sync all of them."),
  219. '#required' => FALSE,
  220. '#prefix' => '<div id="ana_boxes">',
  221. '#suffix' => '</div>',
  222. '#options' => $ana_boxes,
  223. );
  224. $form['sync']['button'] = array(
  225. '#type' => 'submit',
  226. '#value' => t('Submit Sync Job')
  227. );
  228. }
  229. // we don't have any analyses to select from
  230. else {
  231. $form['sync']['value'] = array(
  232. '#value' => t('All analyses in Chado are currently synced with Drupal.')
  233. );
  234. }
  235. }
  236. // we don't want to present a form since we have an active job running
  237. else {
  238. $form['sync']['value'] = array(
  239. '#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.')
  240. );
  241. }
  242. }
  243. /************************************************************************
  244. *
  245. */
  246. function tripal_analysis_admin_validate($form, &$form_state) {
  247. global $user; // we need access to the user info
  248. $job_args = array();
  249. if ($form_state['values']['op'] == t('Submit Sync Job')) {
  250. // check to see if the user wants to sync chado and drupal. If
  251. // so then we need to register a job to do so with tripal
  252. $analyses = $form_state['values']['analyses'];
  253. $do_all = FALSE;
  254. $to_sync = array();
  255. foreach ($analyses as $analysis_id){
  256. if(preg_match("/^all$/i",$analysis_id)){
  257. $do_all = TRUE;
  258. }
  259. if($analysis_id and preg_match("/^\d+$/i",$analysis_id)){
  260. // get the list of analyses
  261. $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d";
  262. $previous_db = tripal_db_set_active('chado'); // use chado database
  263. $analysis = db_fetch_object(db_query($sql,$analysis_id));
  264. tripal_db_set_active($previous_db); // now use drupal database
  265. $to_sync[$analysis_id] = $analysis->name;
  266. }
  267. }
  268. // submit the job the tripal job manager
  269. if($do_all){
  270. tripal_add_job('Sync all analyses','tripal_analysis','tripal_analysis_sync_analyses',$job_args,$user->uid);
  271. }
  272. else{
  273. foreach($to_sync as $analysis_id => $name){
  274. $job_args[0] = $analysis_id;
  275. tripal_add_job("Sync analysis: $name",'tripal_analysis','tripal_analysis_sync_analyses',$job_args,$user->uid);
  276. }
  277. }
  278. }
  279. // -------------------------------------
  280. // Submit the Reindex Job if selected
  281. if ($form_state['values']['op'] == t('Reindex Features')) {
  282. global $user; // we need access to the user info
  283. $job_args = array();
  284. $analyses = $form_state['values']['re-analyses'];
  285. foreach ($analyses as $analysis_id){
  286. if($analysis_id and preg_match("/^\d+$/i",$analysis_id)){
  287. // get the analysis info
  288. $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d";
  289. $previous_db = tripal_db_set_active('chado'); // use chado database
  290. $analysis = db_fetch_object(db_query($sql,$analysis_id));
  291. tripal_db_set_active($previous_db); // now use drupal database
  292. $job_args[0] = $analysis_id;
  293. tripal_add_job("Reindex features for analysis: $analysis->name",'tripal_analysis',
  294. 'tripal_analysis_reindex_features',$job_args,$user->uid);
  295. }
  296. }
  297. }
  298. // -------------------------------------
  299. // Submit the Taxonomy Job if selected
  300. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  301. global $user; // we need access to the user info
  302. $job_args = array();
  303. $analyses = $form_state['values']['tx-analyses'];
  304. foreach ($analyses as $analysis_id){
  305. if($analysis_id and preg_match("/^\d+$/i",$analysis_id)){
  306. // get the analysis info
  307. $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d";
  308. $previous_db = tripal_db_set_active('chado'); // use chado database
  309. $analysis = db_fetch_object(db_query($sql,$analysis_id));
  310. tripal_db_set_active($previous_db); // now use drupal database
  311. $job_args[0] = $analysis_id;
  312. tripal_add_job("Set taxonomy for features in analysis: $analysis->name",'tripal_analysis',
  313. 'tripal_analysis_taxonify_features',$job_args,$user->uid);
  314. }
  315. }
  316. }
  317. // -------------------------------------
  318. // Submit the Cleanup Job if selected
  319. if ($form_state['values']['op'] == t('Clean up orphaned analyses')) {
  320. tripal_add_job('Cleanup orphaned analyses','tripal_analysis',
  321. 'tripal_analyses_cleanup',$job_args,$user->uid);
  322. }
  323. // -------------------------------------
  324. // Save blast regular expression settings
  325. if ($form_state['values']['op'] == t('Save settings')) {
  326. $db_id = $form_state['values']['blastdb'];
  327. $name = $form_state['values']['displayname'];
  328. $gbstyle = $form_state['values']['gb_style_parser'];
  329. $reg1 = $form_state['values']['hit_id'];
  330. $reg2 = $form_state['values']['hit_def'];
  331. $reg3 = $form_state['values']['hit_accession'];
  332. // Check if the blast settings exists
  333. $sql = "SELECT db_id FROM {tripal_analysis_blast} WHERE db_id=%d";
  334. $check = db_result(db_query($sql, $db_id));
  335. // If setting exists, update it
  336. if ($check) {
  337. $sql = "UPDATE {tripal_analysis_blast} ".
  338. "SET displayname = '%s', ".
  339. " regex_hit_id = '%s', ".
  340. " regex_hit_def = '%s', ".
  341. " regex_hit_accession = '%s', ".
  342. " genbank_style = %d ".
  343. "WHERE db_id=%d";
  344. db_query($sql, $name, $reg1, $reg2, $reg3, $gbstyle, $db_id);
  345. // Otherwise, insert a new setting for the db
  346. } else {
  347. $sql = "INSERT INTO {tripal_analysis_blast} (db_id, displayname, ".
  348. " regex_hit_id, regex_hit_def, regex_hit_accession, genbank_style) ".
  349. "VALUES (%d, '%s', '%s', '%s', '%s', %d) ";
  350. db_query($sql, $db_id, $name, $reg1, $reg2, $reg3, $gbstyle);
  351. }
  352. }
  353. }