Tripal v1.0 (6.x-1.0)
|
00001 <?php 00002 00021 function tripal_analysis_admin() { 00022 // Create a new administrative form. We'll add main functions to the form 00023 // first (Sync, Reindex, Clean, Taxonify). Thereafter, any sub-module that 00024 // has a setting will be added. 00025 $form = array(); 00026 00027 // before proceeding check to see if we have any 00028 // currently processing jobs. If so, we don't want 00029 // to give the opportunity to sync libraries 00030 $active_jobs = FALSE; 00031 if (tripal_get_module_active_jobs('tripal_organism')) { 00032 $active_jobs = TRUE; 00033 } 00034 00035 // add the field set for syncing libraries 00036 if (!$active_jobs) { 00037 // add the field set for syncing analyses 00038 get_tripal_analysis_admin_form_sync_set($form); 00039 // get_tripal_analysis_admin_form_reindex_set($form); 00040 // get_tripal_analysis_admin_form_taxonomy_set($form); 00041 get_tripal_analysis_admin_form_cleanup_set($form); 00042 } 00043 else { 00044 $form['notice'] = array( 00045 '#type' => 'fieldset', 00046 '#title' => t('Analysis Management Temporarily Unavailable') 00047 ); 00048 $form['notice']['message'] = array( 00049 '#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.'), 00050 ); 00051 } 00052 00053 // Add sub-module settings. Pull all sub-module information from 00054 // {tripal_analysis} table 00055 $sql = "SELECT modulename FROM {tripal_analysis}"; 00056 $result = db_query($sql); 00057 $counter = 0; //keep track of the number of sub-modules 00058 while ($data = db_fetch_object($result)) { 00059 00060 // Check if the hook_get_settings() function is already defined. 00061 $func = $data->modulename . "_get_settings"; 00062 $functions = get_defined_functions(); 00063 $settings; 00064 foreach ($functions['user'] as $function) { 00065 if ($function == $func) { 00066 $settings = $func(); 00067 } 00068 } 00069 00070 // Add sub-module's specific settings to the administrative view 00071 if ($settings) { 00072 // Define a fieldset for the sub-module 00073 $form["field$counter"] = array( 00074 '#type' => 'fieldset', 00075 '#title' => "$settings->title", 00076 '#collapsible' => TRUE 00077 ); 00078 $form["field$counter"]["$settings->title"] = $settings->form; 00079 } 00080 $counter ++; 00081 } 00082 return system_settings_form($form); 00083 } 00084 00096 function get_tripal_analysis_admin_form_taxonomy_set(&$form) { 00097 $form['taxonify'] = array( 00098 '#type' => 'fieldset', 00099 '#title' => t('Assign Drupal Taxonomy to Analysis Features') 00100 ); 00101 00102 // get the list of analyses 00103 $sql = "SELECT * FROM {analysis} ORDER BY name"; 00104 $lib_rset = chado_query($sql); 00105 00106 // iterate through all of the libraries 00107 $lib_boxes = array(); 00108 while ($analysis = db_fetch_object($lib_rset)) { 00109 $lib_boxes[$analysis->analysis_id] = "$analysis->name"; 00110 } 00111 00112 $form['taxonify']['description'] = array( 00113 '#type' => 'item', 00114 '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " . 00115 "nodes. These terms allow for advanced filtering during searching. This option allows ". 00116 "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."), 00117 '#weight' => 1, 00118 ); 00119 00120 $form['taxonify']['tx-analyses'] = array( 00121 '#title' => t('Analyses'), 00122 '#type' => t('checkboxes'), 00123 '#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."), 00124 '#required' => FALSE, 00125 '#prefix' => '<div id="lib_boxes">', 00126 '#suffix' => '</div>', 00127 '#options' => $lib_boxes, 00128 '#weight' => 2 00129 ); 00130 $form['taxonify']['tx-button'] = array( 00131 '#type' => 'submit', 00132 '#value' => t('Set Feature Taxonomy'), 00133 '#weight' => 3 00134 ); 00135 } 00136 00148 function get_tripal_analysis_admin_form_reindex_set(&$form) { 00149 // define the fieldsets 00150 $form['reindex'] = array( 00151 '#type' => 'fieldset', 00152 '#title' => t('Reindex Analysis Features') 00153 ); 00154 00155 // get the list of libraries 00156 $sql = "SELECT * FROM {analysis} ORDER BY name"; 00157 $lib_rset = chado_query($sql); 00158 00159 // iterate through all of the libraries 00160 $lib_boxes = array(); 00161 while ($analysis = db_fetch_object($lib_rset)) { 00162 $lib_boxes[$analysis->analysis_id] = "$analysis->name"; 00163 } 00164 $form['reindex']['description'] = array( 00165 '#type' => 'item', 00166 '#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."), 00167 '#weight' => 1, 00168 ); 00169 00170 $form['reindex']['re-analyses'] = array( 00171 '#title' => t('Libraries'), 00172 '#type' => t('checkboxes'), 00173 '#description' => t("Check the analyses whoee features you want to reindex. Note: this list contains all analyses, even those that may not be synced."), 00174 '#required' => FALSE, 00175 '#prefix' => '<div id="lib_boxes">', 00176 '#suffix' => '</div>', 00177 '#options' => $lib_boxes, 00178 '#weight' => 2, 00179 ); 00180 $form['reindex']['re-button'] = array( 00181 '#type' => 'submit', 00182 '#value' => t('Reindex Features'), 00183 '#weight' => 3, 00184 ); 00185 } 00186 00198 function get_tripal_analysis_admin_form_cleanup_set(&$form) { 00199 $form['cleanup'] = array( 00200 '#type' => 'fieldset', 00201 '#title' => t('Clean Up') 00202 ); 00203 $form['cleanup']['description'] = array( 00204 '#type' => 'item', 00205 '#value' => t("With Drupal and chado residing in different databases ". 00206 "it is possible that nodes in Drupal and analyses in Chado become ". 00207 "\"orphaned\". This can occur if an analysis node in Drupal is ". 00208 "deleted but the corresponding chado analysis is not and/or vice ". 00209 "versa. Click the button below to resolve these discrepancies."), 00210 '#weight' => 1, 00211 ); 00212 $form['cleanup']['button'] = array( 00213 '#type' => 'submit', 00214 '#value' => t('Clean up orphaned analyses'), 00215 '#weight' => 2, 00216 ); 00217 } 00218 00230 function get_tripal_analysis_admin_form_sync_set(&$form) { 00231 // define the fieldsets 00232 $form['sync'] = array( 00233 '#type' => 'fieldset', 00234 '#title' => t('Sync Analyses') 00235 ); 00236 00237 // before proceeding check to see if we have any 00238 // currently processing jobs. If so, we don't want 00239 // to give the opportunity to sync analyses 00240 $active_jobs = FALSE; 00241 if (tripal_get_module_active_jobs('tripal_analysis')) { 00242 $active_jobs = TRUE; 00243 } 00244 00245 if (!$active_jobs) { 00246 00247 // get the list of analyses 00248 $sql = "SELECT * FROM {analysis} ORDER BY name"; 00249 $ana_rset = chado_query($sql); 00250 00251 // if we've added any analyses to the list that can be synced 00252 // then we want to build the form components to allow the user 00253 // to select one or all of them. Otherwise, just present 00254 // a message stating that all analyses are currently synced. 00255 $ana_boxes = array(); 00256 $added = 0; 00257 while ($analysis = db_fetch_object($ana_rset)) { 00258 // check to see if the analysis is already present as a node in drupal. 00259 // if so, then skip it. 00260 $sql = "SELECT * FROM {chado_analysis} WHERE analysis_id = %d"; 00261 if (!db_fetch_object(db_query($sql, $analysis->analysis_id))) { 00262 $ana_boxes[$analysis->analysis_id] = "$analysis->name"; 00263 $added++; 00264 } 00265 } 00266 00267 // if we have analyses we need to add to the checkbox then 00268 // build that form element 00269 if ($added > 0) { 00270 $ana_boxes['all'] = "All analyses"; 00271 00272 $form['sync']['analyses'] = array( 00273 '#title' => t('Available analyses'), 00274 '#type' => t('checkboxes'), 00275 '#description' => t("Check the analyses you want to sync. Drupal ". 00276 "content will be created for each of the analyses listed above. ". 00277 "Select 'All analyses' to sync all of them."), 00278 '#required' => FALSE, 00279 '#prefix' => '<div id="ana_boxes">', 00280 '#suffix' => '</div>', 00281 '#options' => $ana_boxes, 00282 ); 00283 $form['sync']['button'] = array( 00284 '#type' => 'submit', 00285 '#value' => t('Submit Sync Job') 00286 ); 00287 } 00288 // we don't have any analyses to select from 00289 else { 00290 $form['sync']['value'] = array( 00291 '#value' => t('All analyses in Chado are currently synced with Drupal.') 00292 ); 00293 } 00294 } 00295 // we don't want to present a form since we have an active job running 00296 else { 00297 $form['sync']['value'] = array( 00298 '#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.') 00299 ); 00300 } 00301 } 00302 00314 function tripal_analysis_admin_validate($form, &$form_state) { 00315 global $user; // we need access to the user info 00316 $job_args = array(); 00317 00318 if ($form_state['values']['op'] == t('Submit Sync Job')) { 00319 00320 // check to see if the user wants to sync chado and drupal. If 00321 // so then we need to register a job to do so with tripal 00322 $analyses = $form_state['values']['analyses']; 00323 $do_all = FALSE; 00324 $to_sync = array(); 00325 00326 foreach ($analyses as $analysis_id) { 00327 if (preg_match("/^all$/i", $analysis_id)) { 00328 $do_all = TRUE; 00329 } 00330 if ($analysis_id and preg_match("/^\d+$/i", $analysis_id)) { 00331 // get the list of analyses 00332 $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d"; 00333 $analysis = db_fetch_object(chado_query($sql, $analysis_id)); 00334 $to_sync[$analysis_id] = $analysis->name; 00335 } 00336 } 00337 00338 // submit the job the tripal job manager 00339 if ($do_all) { 00340 tripal_add_job('Sync all analyses', 'tripal_analysis', 'tripal_analysis_sync_analyses', $job_args, $user->uid); 00341 } 00342 else{ 00343 foreach ($to_sync as $analysis_id => $name) { 00344 $job_args[0] = $analysis_id; 00345 tripal_add_job("Sync analysis: $name", 'tripal_analysis', 'tripal_analysis_sync_analyses', $job_args, $user->uid); 00346 } 00347 } 00348 } 00349 // ------------------------------------- 00350 // Submit the Reindex Job if selected 00351 if ($form_state['values']['op'] == t('Reindex Features')) { 00352 global $user; // we need access to the user info 00353 $job_args = array(); 00354 $analyses = $form_state['values']['re-analyses']; 00355 foreach ($analyses as $analysis_id) { 00356 if ($analysis_id and preg_match("/^\d+$/i", $analysis_id)) { 00357 // get the analysis info 00358 $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d"; 00359 $analysis = db_fetch_object(chado_query($sql, $analysis_id)); 00360 $job_args[0] = $analysis_id; 00361 tripal_add_job("Reindex features for analysis: $analysis->name", 'tripal_analysis', 00362 'tripal_analysis_reindex_features', $job_args, $user->uid); 00363 } 00364 } 00365 } 00366 00367 // ------------------------------------- 00368 // Submit the Taxonomy Job if selected 00369 if ($form_state['values']['op'] == t('Set Feature Taxonomy')) { 00370 global $user; // we need access to the user info 00371 $job_args = array(); 00372 $analyses = $form_state['values']['tx-analyses']; 00373 foreach ($analyses as $analysis_id) { 00374 if ($analysis_id and preg_match("/^\d+$/i", $analysis_id)) { 00375 // get the analysis info 00376 $sql = "SELECT * FROM {analysis} WHERE analysis_id = %d"; 00377 $analysis = db_fetch_object(chado_query($sql, $analysis_id)); 00378 $job_args[0] = $analysis_id; 00379 tripal_add_job("Set taxonomy for features in analysis: $analysis->name", 'tripal_analysis', 00380 'tripal_analysis_taxonify_features', $job_args, $user->uid); 00381 } 00382 } 00383 } 00384 00385 // ------------------------------------- 00386 // Submit the Cleanup Job if selected 00387 if ($form_state['values']['op'] == t('Clean up orphaned analyses')) { 00388 tripal_add_job('Cleanup orphaned analyses', 'tripal_analysis', 00389 'tripal_analyses_cleanup', $job_args, $user->uid); 00390 } 00391 } 00392 00398 function tripal_analysis_sync_analyses($analysis_id = NULL, $job_id = NULL) { 00399 global $user; 00400 $page_content = ''; 00401 00402 if (!$analysis_id) { 00403 $sql = "SELECT Analysis_id, name, description, program, ". 00404 " programversion, algorithm, sourcename, sourceversion, sourceuri, ". 00405 " timeexecuted ". 00406 "FROM {Analysis} "; 00407 $results = chado_query($sql); 00408 } 00409 else { 00410 $sql = "SELECT Analysis_id, name, description, program, ". 00411 " programversion, algorithm, sourcename, sourceversion, sourceuri, ". 00412 " timeexecuted ". 00413 "FROM {Analysis} ". 00414 "WHERE analysis_id = %d"; 00415 $results = chado_query($sql, $analysis_id); 00416 } 00417 00418 00419 // We'll use the following SQL statement for checking if the analysis 00420 // already exists as a drupal node. 00421 $sql = "SELECT * FROM {chado_analysis} ". 00422 "WHERE analysis_id = %d"; 00423 00424 while ($analysis = db_fetch_object($results)) { 00425 print "syncing analysis "; 00426 print $analysis->name; 00427 print ", "; 00428 print $analysis->analysis_id; 00429 print "\n"; 00430 00431 // check if this analysis already exists in the drupal database. if it 00432 // does then skip this analysis and go to the next one. 00433 if (!db_fetch_object(db_query($sql, $analysis->analysis_id))) { 00434 00435 $new_node = new stdClass(); 00436 00437 // try to access analysis type for this analysis 00438 $sql = "SELECT * FROM {analysisprop} 00439 WHERE analysis_id = %d 00440 AND type_id = 00441 (SELECT cvterm_id from {cvterm} where name = '%s') 00442 "; 00443 $analysis_type = db_fetch_object(chado_query($sql, $analysis->analysis_id, "analysis_type")); 00444 00445 // Get the type of analysis using cvterm_id 00446 // Current possibilities: kegg, unigene, interpro, blast 00447 if ($analysis_type) { 00448 00449 // This is a unigene analysis 00450 if ($analysis_type->value == 'tripal_analysis_unigene') { 00451 $new_node->type = 'chado_analysis_unigene'; 00452 // This is a blast analysis 00453 } 00454 elseif ($analysis_type->value == 'tripal_analysis_blast') { 00455 $new_node->type = 'chado_analysis_blast'; 00456 // This is a interpro analysis 00457 } 00458 elseif ($analysis_type->value == 'tripal_analysis_interpro') { 00459 $new_node->type = 'chado_analysis_interpro'; 00460 // This is a kegg analysis 00461 } 00462 elseif ($analysis_type->value == 'tripal_analysis_kegg' ) { 00463 $new_node->type = 'chado_analysis_kegg'; 00464 } 00465 else { 00466 $new_node->type = 'chado_analysis'; 00467 } 00468 // If it doesn't exist, this analysis is generic 00469 } 00470 else { 00471 $new_node->type = 'chado_analysis'; 00472 } 00473 00474 print "analysis type is $new_node->type\n"; 00475 00476 $new_node->uid = $user->uid; 00477 $new_node->analysis_id = $analysis->analysis_id; 00478 $new_node->analysisname = $analysis->name; 00479 $new_node->description = $analysis->description; 00480 $new_node->program = $analysis->program; 00481 $new_node->programversion = $analysis->programversion; 00482 $new_node->algorithm = $analysis->algorithm; 00483 $new_node->sourcename = $analysis->sourcename; 00484 $new_node->sourceversion = $analysis->sourceversion; 00485 $new_node->sourceuri = $analysis->sourceuri; 00486 $new_node->timeexecuted = $analysis->timeexecuted; 00487 00488 // If the analysis has a name, use it as the node title. If not, 00489 // construct the title using program, programversion, and sourcename 00490 if ($new_node->analysisname) { 00491 $new_node->title = $new_node->analysisname; 00492 } 00493 else { 00494 //Construct node title as "program (version)" 00495 $new_node->title = "$analysis->program ($analysis->programversion)"; 00496 } 00497 00498 node_validate($new_node); 00499 00500 $errors = form_get_errors(); 00501 00502 if ($errors) { 00503 print_r($errors); 00504 } 00505 else{ 00506 // if(!form_get_errors()){ 00507 $node = node_submit($new_node); 00508 node_save($node); 00509 00510 if ($node->nid) { 00511 $page_content .= "Added $new_node->title<br />"; 00512 } 00513 } 00514 } 00515 else { 00516 $page_content .= "Skipped $new_node->title<br />"; 00517 } 00518 } 00519 return $page_content; 00520 } 00521 00532 function tripal_analyses_cleanup($dummy = NULL, $job_id = NULL) { 00533 00534 return tripal_core_clean_orphaned_nodes('analysis', $job_id); 00535 00536 }