Tripal 0.3b
tripal_analysis/tripal_analysis.admin.inc
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Copyright 2009 Clemson University
00004 //
00005 
00006 /**
00007  * Purpose: Provide Guidance to new Tripal Admin
00008  *
00009  * @return 
00010  *   HTML Formatted text
00011  *
00012  * @ingroup tripal_analysis
00013  */
00014 function tripal_analysis_module_description_page() {
00015 
00016   $text .= '<h3>Tripal Analysis Administrative Tools Quick Links:</h3>';
00017   $text .= "<ul>
00018              <li><a href=\"".url("admin/tripal/tripal_analysis/configuration") . "\">Analysis Configuration</a></li>
00019            </ul>";
00020  
00021 
00022   $text .= '<h3>Module Description:</h3>';
00023   $text .= '<p>The Tripal Analysis module provides a generic analysis content type that is intended to be
00024             used when a more specialized analysis module does not exist.  Because it is generic, it does not
00025             provide any visualization or data loading tools for analysis data.  Specialized analysis modules,
00026             such as the Tripal Analysis Blast or Tripal Analysis KEGG modules provide loading and custom
00027             visualizations for the analysis results.  These modules must be installed separately.
00028             </p>';
00029 
00030   $text .= '<h3>Setup Instructions:</h3>';
00031   $text .= '<p>After installation of the analysis module or any specialized analysis module.  The following tasks should be performed
00032             <ol>
00033               <li><p><b>Set Permissions</b>: Each analysis module supports the Drupal user permissions interface for 
00034                controlling access to the content and functions. These permissions include viewing, 
00035                creating, editing or administering of
00036                analysis content. The default is that only the original site administrator has these 
00037                permissions.  You can <a href="'.url('admin/user/roles').'">add roles</a> for classifying users, 
00038                <a href="'.url('admin/user/user').'">assign users to roles</a> and
00039                <a href="'.url('admin/user/permissions').'">assign permissions</a> for the analysis content to 
00040                those roles.  For a simple setup, allow anonymous users access to view organism content and 
00041                allow the site administrator all other permissions.</p></li>
00042 
00043 
00044                <li><p><b>Create Analysis</b>:  An analysis should be <a href="'.url('node/add').'">created</a> before data is imported into
00045                chado.  The generic analysis type should only be used when a more specialized analysis module
00046                (e.g. Tripal Analysis Blast module) does not already exists.  All data imported into 
00047                Chado should be associated with an analysis.    
00048 
00049                <li><p><b>Sync Analyses</b>:  If Chado has preloaded analyses then you can sync those.  This process is what
00050                creates the pages for viewing an analysis on the site.  Analyses can be synced using the 
00051                <a href="'.url('admin/tripal/tripal_analysis/configuration').'">Analysis Configuration page</a>. 
00052                However, syncing an analyses will always create a generic analysis content type.  If you would like
00053                to use a specialized analysis module for visualization of data then do not sync the analysis but recreate it
00054                using the appropriate specialized analysis content type.</p></li>
00055  
00056             </ol>
00057             </p>';
00058   
00059   
00060   $text .= '<h3>Features of this Module:</h3>';
00061   $text .= '<p>Aside from providing a generic content type the Tripal Analysis module also provides the following functionality
00062             <ul>
00063             
00064               <li><p><b>Basic Analysis Lookup View</b>: This module provides a basic <a href="'.url('analyses').'">analysis search 
00065               tool</a> for finding or listing analyses in Chado. It does not require indexing for Drupal searching but relies
00066               on Drupal Views.  <a href="http://drupal.org/project/views">Drupal Views</a> must be installed. </p></li>              
00067 
00068             </ul>
00069             </p>';
00070 
00071    $text .= '<h3>Page Customizations</h3>';
00072    $text .= '<p>There are several ways to customize the look-and-feel for the way Chado data is presented through Tripal. 
00073              Below is a description of several methods.  These methods may be used in conjunction with one another to
00074              provide fine-grained control. 
00075              <ul>
00076 
00077              <li><p><b>Integration with Drupal Panels</b>:  <a href="http://drupal.org/project/views">Drupal Panels</a> 
00078               allows for customization of a page layout if you don\'t want to do PHP/Javascript/CSS programming.  
00079               Tripal comes with pre-set layouts for analysis pages.  However, 
00080               Panels become useful if you prefer a layout that is different from the pre-set layouts.  Chado content
00081               is provided to Panels in the form of Drupal "blocks" which you can then place anywhere on a page using the 
00082               Panel\'s GUI.</p></li>
00083 
00084              <li><p><b>Drupal\'s Content Construction Kit (CCK)</b>: the 
00085              <a href="http://drupal.org/project/cck">Content Construction Kit (CCK) </a> is a powerful way to add non-Chado content
00086              to any page without need to edit template files or knowing PHP.  You must first download and install CCK.
00087              With CCK, the site administartor can create a new field to appear on the page.  For example, currently,
00088              the Chado publication module is not yet supported by Tripal.  Therefore, the site administrator can add a text 
00089              field to the analysis pages.  This content is not stored in Chado, but will appear on the analysis page.  A field
00090              added by CCK will also appear in the form when editing a analysis to allow users to manually enter the appropriate
00091              text.  If the default pre-set layout and themeing for Tripal is used, it is better to create the CCK element,
00092              indicate that it is not to be shown (using the CCK interface), then manually add the new content type 
00093              where desired by editing the templates (as described below).  If using Panels, the CCK field can be added to the
00094              location desired using the Panels interface.</p></li>
00095 
00096              <li><p><b>Drupal Node Templates</b>:  The Tripal packages comes with a "theme_tripal" directory that contains the
00097              themeing for Chado content.    The analysis module has a template file for analysis "nodes" (Tripal analysis pages).  This file
00098              is named "node-chado_analysis.tpl.php", and provides javascript, HTML and PHP code for display of the analysis
00099              pages.  Specialized analysis modules will have their own template files as well, such as "node-chado_analysis-blast.tpl.php" for the
00100              Tripal Analysis Blast module.  You can edit the template file to control which types of information (or which analysis "blocks") are displayed 
00101              for analysis. Be sure to 
00102              copy these template to your primary theme directory for editing. Do not edit them in the "theme_tripal" directory as
00103              future Tripal updates may overwrite your customizations. See the <a href="http://tripal.sourceforge.net/">Tripal website </a>
00104              for instructions on how to access variables and other Chado content within the template file.</p></li>
00105 
00106              <li><p><b>Analysis "Block" Templates</b>:  In the "theme_tripal" directory are subdirectories named after each tripal module (e.g. "tripal_feature", "tripal_library", etc.).
00107              Inside each directory is a set of templates that control distinct types of information for each content type.  For example,
00108              there is a "base" template for displaying of data directly from the Chado feature table, and a "references" 
00109              template for showing external site references for a feature (data from the feature_dbxref table). 
00110               These templates are used both by Drupal blocks
00111              for use in Drupal Panels (as described above) or for use in the default pre-set layout that the node template 
00112              provides (also desribed above).  Analyses block templates can exist in any of these directories.  For example, the Tripal Analysis Unigene
00113              module uses templates in the tripal_analysis_unigene, tripal_organism, and tripal_feature directories.  Content for a unigene is then
00114              cusotmizable within each of these contexts.
00115              You can customize block template as you desire.  Be sure to copy the
00116              template to your primary theme directory for editing. Do not edit them in the "theme_tripal" directory as
00117              future Tripal updates may overwrite your customizations.  See the <a href="http://tripal.sourceforge.net/">Tripal website </a>
00118              for instructions on how to access variables and other Chado content within the template files.</p></li>
00119              </li>
00120 
00121              <li><p><b>Adding Links to the "Resources" Sidebar</b>: If you use the pre-set default Tripal layout for theming, you
00122              will see a "Resources" sidebar on each page.  The links that appear on the sidebar are automatically generated
00123              using Javascript for all of the analysis "Blocks" that appear on the page. If you want to add additional links 
00124              (e.g. a dynamic link to GBrowse for the analysis) and you want that link to appear in the 
00125              "Resources" sidebar, simply edit the Drupal Node Template (as described above) and add the link to the 
00126              section at the bottom of the template file where the resources section is found.</p></li>
00127 
00128              </ul>
00129              </p>';
00130 
00131   return $text;
00132 }
00133 
00134 /*******************************************************************************
00135  * Administration page callbacks for the Tripal Analysis module
00136  * We have defined a hook_get_settings() function. When a sub-module
00137  * is enabled, we'll look for this function to provide a form for the
00138  * administrative setting.
00139  *
00140  * @ingroup tripal_analysis
00141  */
00142 function tripal_analysis_admin() {
00143   // Create a new administrative form. We'll add main functions to the form
00144   // first (Sync, Reindex, Clean, Taxonify). Thereafter, any sub-module that
00145   // has a setting will be added.
00146   $form = array();
00147 
00148   // before proceeding check to see if we have any
00149   // currently processing jobs. If so, we don't want
00150   // to give the opportunity to sync libraries
00151   $active_jobs = FALSE;
00152   if(tripal_get_module_active_jobs('tripal_organism')){
00153     $active_jobs = TRUE;
00154   }
00155 
00156   // add the field set for syncing libraries
00157   if(!$active_jobs){
00158     // add the field set for syncing analyses
00159     get_tripal_analysis_admin_form_sync_set($form);
00160 //    get_tripal_analysis_admin_form_reindex_set($form);
00161 //    get_tripal_analysis_admin_form_taxonomy_set($form);
00162     get_tripal_analysis_admin_form_cleanup_set($form);
00163   } else {
00164     $form['notice'] = array(
00165        '#type' => 'fieldset',
00166        '#title' => t('Analysis Management Temporarily Unavailable')
00167     );
00168     $form['notice']['message'] = array(
00169           '#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.'),
00170     );
00171   }
00172 
00173   // Add sub-module settings. Pull all sub-module information from
00174   // {tripal_analysis} table
00175   $sql = "SELECT modulename FROM {tripal_analysis}";
00176   $result = db_query($sql);
00177   $counter = 0;  //keep track of the number of sub-modules
00178   while ($data = db_fetch_object($result)) {
00179 
00180     // Check if the hook_get_settings() function is already defined.
00181     $func = $data->modulename."_get_settings";
00182     $functions = get_defined_functions();
00183     $settings;
00184     foreach($functions['user'] as $function) {
00185       if ($function == $func) {
00186         $settings = $func();
00187       }
00188     }
00189     
00190     // Add sub-module's specific settings to the administrative view
00191     if ($settings) {
00192       // Define a fieldset for the sub-module
00193       $form["field$counter"] = array(
00194             '#type' => 'fieldset',
00195             '#title' => "$settings->title",
00196             '#collapsible' => TRUE
00197       );
00198       $form["field$counter"]["$settings->title"] = $settings->form;
00199     }
00200     $counter ++;
00201   }
00202   return system_settings_form($form);
00203 }
00204 
00205 /**
00206  *
00207  * @ingroup tripal_analysis
00208  */
00209 function get_tripal_analysis_admin_form_taxonomy_set(&$form) {
00210   $form['taxonify'] = array(
00211       '#type' => 'fieldset',
00212       '#title' => t('Assign Drupal Taxonomy to Analysis Features')
00213   );
00214 
00215   // get the list of analyses
00216   $sql = "SELECT * FROM {Analysis} ORDER BY name";
00217   $previous_db = tripal_db_set_active('chado');  // use chado database
00218   $lib_rset = db_query($sql);
00219   tripal_db_set_active($previous_db);  // now use drupal database
00220 
00221   // iterate through all of the libraries
00222   $lib_boxes = array();
00223   while($analysis = db_fetch_object($lib_rset)){
00224     $lib_boxes[$analysis->analysis_id] = "$analysis->name";
00225   }
00226 
00227   $form['taxonify']['description'] = array(
00228        '#type' => 'item',
00229        '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
00230           "nodes. These terms allow for advanced filtering during searching. This option allows ".
00231           "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."),
00232      '#weight' => 1,
00233   );
00234 
00235   $form['taxonify']['tx-analyses'] = array (
00236      '#title'       => t('Analyses'),
00237      '#type'        => t('checkboxes'),
00238      '#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."),
00239      '#required'    => FALSE,
00240      '#prefix'      => '<div id="lib_boxes">',
00241      '#suffix'      => '</div>',
00242      '#options'     => $lib_boxes,
00243      '#weight'      => 2
00244   );
00245   $form['taxonify']['tx-button'] = array(
00246       '#type' => 'submit',
00247       '#value' => t('Set Feature Taxonomy'),
00248       '#weight'      => 3
00249   );
00250 }
00251 /** 
00252  *
00253  * @ingroup tripal_analysis
00254  */
00255 function get_tripal_analysis_admin_form_reindex_set(&$form) {
00256   // define the fieldsets
00257   $form['reindex'] = array(
00258       '#type' => 'fieldset',
00259       '#title' => t('Reindex Analysis Features')
00260   );
00261 
00262   // get the list of libraries
00263   $sql = "SELECT * FROM {Analysis} ORDER BY name";
00264   $previous_db = tripal_db_set_active('chado');  // use chado database
00265   $lib_rset = db_query($sql);
00266   tripal_db_set_active($previous_db);  // now use drupal database
00267 
00268   // iterate through all of the libraries
00269   $lib_boxes = array();
00270   while($analysis = db_fetch_object($lib_rset)){
00271     $lib_boxes[$analysis->analysis_id] = "$analysis->name";
00272   }
00273   $form['reindex']['description'] = array(
00274        '#type' => 'item',
00275        '#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."),
00276      '#weight' => 1,
00277   );
00278 
00279   $form['reindex']['re-analyses'] = array (
00280      '#title'       => t('Libraries'),
00281      '#type'        => t('checkboxes'),
00282      '#description' => t("Check the analyses whoee features you want to reindex. Note: this list contains all analyses, even those that may not be synced."),
00283      '#required'    => FALSE,
00284      '#prefix'      => '<div id="lib_boxes">',
00285      '#suffix'      => '</div>',
00286      '#options'     => $lib_boxes,
00287      '#weight' => 2,
00288   );
00289   $form['reindex']['re-button'] = array(
00290       '#type' => 'submit',
00291       '#value' => t('Reindex Features'),
00292       '#weight' => 3,
00293   );
00294 }
00295 /** 
00296  *
00297  * @ingroup tripal_analysis
00298  */
00299 function get_tripal_analysis_admin_form_cleanup_set(&$form) {
00300   $form['cleanup'] = array(
00301       '#type' => 'fieldset',
00302       '#title' => t('Clean Up')
00303   );
00304   $form['cleanup']['description'] = array(
00305        '#type' => 'item',
00306        '#value' => t("With Drupal and chado residing in different databases ".
00307           "it is possible that nodes in Drupal and analyses in Chado become ".
00308           "\"orphaned\".  This can occur if an analysis node in Drupal is ".
00309           "deleted but the corresponding chado analysis is not and/or vice ".
00310           "versa. Click the button below to resolve these discrepancies."),
00311        '#weight' => 1,
00312   );
00313   $form['cleanup']['button'] = array(
00314       '#type' => 'submit',
00315       '#value' => t('Clean up orphaned analyses'),
00316       '#weight' => 2,
00317   );
00318 }
00319 /** 
00320  *
00321  * @ingroup tripal_analysis
00322  */
00323 function get_tripal_analysis_admin_form_sync_set (&$form) {
00324   // define the fieldsets
00325   $form['sync'] = array(
00326       '#type' => 'fieldset',
00327       '#title' => t('Sync Analyses')
00328   );
00329 
00330   // before proceeding check to see if we have any
00331   // currently processing jobs. If so, we don't want
00332   // to give the opportunity to sync analyses
00333   $active_jobs = FALSE;
00334   if(tripal_get_module_active_jobs('tripal_analysis')){
00335     $active_jobs = TRUE;
00336   }
00337 
00338   if(!$active_jobs){
00339       
00340     // get the list of analyses
00341     $sql = "SELECT * FROM {analysis} ORDER BY name";
00342     $previous_db = tripal_db_set_active('chado');  // use chado database
00343     $ana_rset = db_query($sql);
00344     tripal_db_set_active($previous_db);  // now use drupal database
00345       
00346     // if we've added any analyses to the list that can be synced
00347     // then we want to build the form components to allow the user
00348     // to select one or all of them.  Otherwise, just present
00349     // a message stating that all analyses are currently synced.
00350     $ana_boxes = array();
00351     $added = 0;
00352     while($analysis = db_fetch_object($ana_rset)){
00353       // check to see if the analysis is already present as a node in drupal.
00354       // if so, then skip it.
00355       $sql = "SELECT * FROM {chado_analysis} WHERE analysis_id = %d";
00356       if(!db_fetch_object(db_query($sql,$analysis->analysis_id))){
00357         $ana_boxes[$analysis->analysis_id] = "$analysis->name";
00358         $added++;
00359       }
00360     }
00361 
00362     // if we have analyses we need to add to the checkbox then
00363     // build that form element
00364     if($added > 0){
00365       $ana_boxes['all'] = "All analyses";
00366 
00367       $form['sync']['analyses'] = array (
00368            '#title'       => t('Available analyses'),
00369            '#type'        => t('checkboxes'),
00370            '#description' => t("Check the analyses you want to sync.  Drupal ".
00371               "content will be created for each of the analyses listed above. ".
00372               "Select 'All analyses' to sync all of them."),
00373            '#required'    => FALSE,
00374            '#prefix'      => '<div id="ana_boxes">',
00375            '#suffix'      => '</div>',
00376            '#options'     => $ana_boxes,
00377       );
00378       $form['sync']['button'] = array(
00379             '#type' => 'submit',
00380             '#value' => t('Submit Sync Job')
00381       );
00382     }
00383     // we don't have any analyses to select from
00384     else {
00385       $form['sync']['value'] = array(
00386             '#value' => t('All analyses in Chado are currently synced with Drupal.')
00387       );
00388     }
00389   }
00390   // we don't want to present a form since we have an active job running
00391   else {
00392     $form['sync']['value'] = array(
00393           '#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.')
00394     );
00395   }
00396 }
00397 /**
00398  *
00399  * @ingroup tripal_analysis
00400  */
00401 function tripal_analysis_admin_validate($form, &$form_state) {
00402   global $user;  // we need access to the user info
00403   $job_args = array();
00404 
00405   if ($form_state['values']['op'] == t('Submit Sync Job')) {
00406 
00407     // check to see if the user wants to sync chado and drupal.  If
00408     // so then we need to register a job to do so with tripal
00409     $analyses = $form_state['values']['analyses'];
00410     $do_all = FALSE;
00411     $to_sync = array();
00412 
00413     foreach ($analyses as $analysis_id){
00414       if(preg_match("/^all$/i",$analysis_id)){
00415         $do_all = TRUE;
00416       }
00417       if($analysis_id and preg_match("/^\d+$/i",$analysis_id)){
00418         // get the list of analyses
00419         $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d";
00420         $previous_db = tripal_db_set_active('chado');  // use chado database
00421         $analysis = db_fetch_object(db_query($sql,$analysis_id));
00422         tripal_db_set_active($previous_db);  // now use drupal database
00423         $to_sync[$analysis_id] = $analysis->name;
00424       }
00425     }
00426 
00427     // submit the job the tripal job manager
00428     if($do_all){
00429       tripal_add_job('Sync all analyses','tripal_analysis','tripal_analysis_sync_analyses',$job_args,$user->uid);
00430     }
00431     else{
00432       foreach($to_sync as $analysis_id => $name){
00433         $job_args[0] = $analysis_id;
00434         tripal_add_job("Sync analysis: $name",'tripal_analysis','tripal_analysis_sync_analyses',$job_args,$user->uid);
00435       }
00436     }
00437   }
00438   // -------------------------------------
00439   // Submit the Reindex Job if selected
00440   if ($form_state['values']['op'] == t('Reindex Features')) {
00441     global $user;  // we need access to the user info
00442     $job_args = array();
00443     $analyses = $form_state['values']['re-analyses'];
00444     foreach ($analyses as $analysis_id){
00445       if($analysis_id and preg_match("/^\d+$/i",$analysis_id)){
00446         // get the analysis info
00447         $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d";
00448         $previous_db = tripal_db_set_active('chado');  // use chado database
00449         $analysis = db_fetch_object(db_query($sql,$analysis_id));
00450         tripal_db_set_active($previous_db);  // now use drupal database
00451         $job_args[0] = $analysis_id;
00452         tripal_add_job("Reindex features for analysis: $analysis->name",'tripal_analysis',
00453              'tripal_analysis_reindex_features',$job_args,$user->uid);
00454       }
00455     }
00456   }
00457 
00458   // -------------------------------------
00459   // Submit the Taxonomy Job if selected
00460   if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
00461     global $user;  // we need access to the user info
00462     $job_args = array();
00463     $analyses = $form_state['values']['tx-analyses'];
00464     foreach ($analyses as $analysis_id){
00465       if($analysis_id and preg_match("/^\d+$/i",$analysis_id)){
00466         // get the analysis info
00467         $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d";
00468         $previous_db = tripal_db_set_active('chado');  // use chado database
00469         $analysis = db_fetch_object(db_query($sql,$analysis_id));
00470         tripal_db_set_active($previous_db);  // now use drupal database
00471         $job_args[0] = $analysis_id;
00472         tripal_add_job("Set taxonomy for features in analysis: $analysis->name",'tripal_analysis',
00473              'tripal_analysis_taxonify_features',$job_args,$user->uid);
00474       }
00475     }
00476   }
00477 
00478   // -------------------------------------
00479   // Submit the Cleanup Job if selected
00480   if ($form_state['values']['op'] == t('Clean up orphaned analyses')) {
00481     tripal_add_job('Cleanup orphaned analyses','tripal_analysis',
00482          'tripal_analyses_cleanup',$job_args,$user->uid);
00483   }
00484 
00485   // -------------------------------------
00486   // Save blast regular expression settings
00487   if ($form_state['values']['op'] == t('Save settings')) {
00488     $db_id = $form_state['values']['blastdb'];
00489     $name = $form_state['values']['displayname'];
00490     $gbstyle = $form_state['values']['gb_style_parser'];
00491     $reg1 = $form_state['values']['hit_id'];
00492     $reg2 = $form_state['values']['hit_def'];
00493     $reg3 = $form_state['values']['hit_accession'];
00494     
00495     // Check if the blast settings exists
00496     
00497     $sql = "SELECT db_id FROM {tripal_analysis_blast} WHERE db_id=%d";
00498     $check = db_result(db_query($sql, $db_id));
00499 
00500     // If setting exists, update it
00501     if ($check) {
00502       $sql = "UPDATE {tripal_analysis_blast} ".
00503               "SET displayname = '%s', ".
00504               "    regex_hit_id = '%s', ".
00505               "    regex_hit_def = '%s', ".
00506               "    regex_hit_accession = '%s', ".
00507              "    genbank_style = %d ".
00508               "WHERE db_id=%d";
00509       db_query($sql, $name, $reg1, $reg2, $reg3, $gbstyle, $db_id);
00510       // Otherwise, insert a new setting for the db
00511     } else {
00512       $sql = "INSERT INTO {tripal_analysis_blast} (db_id, displayname, ".
00513              "           regex_hit_id, regex_hit_def, regex_hit_accession, genbank_style) ".
00514              "VALUES (%d, '%s', '%s', '%s', '%s', %d) ";
00515       db_query($sql, $db_id, $name, $reg1, $reg2, $reg3, $gbstyle);
00516     }
00517   }
00518 }
00519 
 All Classes Files Functions Variables