tripal_analysis.admin.inc 15 KB

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