Tripal 0.3b
tripal_analysis/tripal_analysis.module
Go to the documentation of this file.
00001 <?php
00002 // $Id:
00003 
00004 /**
00005  * @file
00006  * Contains all the main hook implementations for the tripal_analysis module
00007  */
00008 
00009 /**
00010  * @defgroup tripal_analysis Analysis Module
00011  * @{
00012  * Provides functions for managing chado analysis' including creating details pages for each one
00013  * @}
00014  * @ingroup tripal_modules
00015  */
00016 
00017 require('tripal_analysis.api.inc');
00018 
00019 
00020 /**
00021  *
00022  *
00023  * @ingroup tripal_analysis
00024  */
00025 function tripal_analysis_register_child($modulename){
00026    $sql = "INSERT INTO {tripal_analysis} (modulename) VALUES ('%s')";
00027    db_query($sql, $modulename);
00028 }
00029 /**
00030  *
00031  *
00032  * @ingroup tripal_analysis
00033  */
00034 function tripal_analysis_unregister_child($modulename){
00035    if (db_table_exists('tripal_analysis')) {
00036       $sql = "DELETE FROM {tripal_analysis} WHERE modulename = '%s'";
00037       db_query($sql, $modulename);
00038    }
00039 }
00040 
00041 /**
00042  *
00043  * @ingroup tripal_analysis
00044  */
00045 function tripal_analysis_init(){
00046    drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_analysis.js');
00047 }
00048 
00049 /**
00050  * tripal_analysis_menu()
00051  * HOOK: Implementation of hook_menu()
00052  * Entry points and paths of the module
00053  *
00054  * @ingroup tripal_analysis
00055  */
00056 function tripal_analysis_menu() {
00057   // Display available analyses
00058   $items['analyses'] = array(
00059       'menu_name' => ('primary-links'), //Enable the 'Analysis' primary link
00060       'title' => t('Analyses'),
00061       'page callback' => 'tripal_analysis_show_analyses',
00062       'access arguments' => array('access chado_analysis content'),
00063       'type' => MENU_NORMAL_ITEM
00064   );
00065   //Sync analysis
00066   $items['chado_sync_analyses'] = array(
00067      'title' => t('Sync Data'),
00068      'page callback' => 'tripal_analysis_sync_analyses',
00069      'access arguments' => array('administer site configuration'),
00070      'type' => MENU_CALLBACK
00071   );
00072   // Tripal Analysis administrative settings
00073   $items['admin/tripal/tripal_analysis'] = array(
00074       'title' => 'Analyses',
00075       'description' => 'Basic Description of Tripal Analysis Module Functionality.',
00076       'page callback' => 'tripal_analysis_module_description_page',
00077       'access arguments' => array('administer site configuration'),
00078       'type' => MENU_NORMAL_ITEM,
00079       'file' => 'tripal_analysis.admin.inc',
00080   );
00081   
00082   $items['admin/tripal/tripal_analysis/configuration'] = array(
00083       'title' => 'Configuration',
00084       'description' => 'Settings for the displays of analysis results.',
00085       'page callback' => 'drupal_get_form',
00086       'page arguments' => array('tripal_analysis_admin'),
00087       'access arguments' => array('administer site configuration'),
00088       'type' => MENU_NORMAL_ITEM,
00089       'file' => 'tripal_analysis.admin.inc',
00090   );
00091   return $items;
00092 }
00093 
00094 /** 
00095  * Display the summary view of analyses when click on the 'Analyses'
00096  * primary-link
00097  *
00098  * @ingroup tripal_analysis
00099  */
00100 function tripal_analysis_show_analyses (){
00101   // Show libraries stored in Drupal's {chado_analysis} table
00102   $sql = "SELECT COUNT(analysis_id) FROM {chado_analysis}";
00103   $no_ana = db_result(db_query ($sql));
00104   if($no_ana != 0) {
00105     $analyses = get_chado_analyses ();
00106     return theme('tripal_analysis_analysis_page', $analyses);
00107   } else {
00108     return t("No analysis available at this time.");
00109   }
00110 
00111 }
00112 
00113 /** 
00114  *  Provide information to drupal about the node types that we're creating
00115  *  in this module
00116  *
00117  * @ingroup tripal_analysis
00118  */
00119 function tripal_analysis_node_info() {
00120   $nodes = array();
00121   $nodes['chado_analysis'] = array(
00122       'name' => t('Analysis'),
00123       'module' => 'chado_analysis',
00124       'description' => t('An analysis from the chado database'),
00125       'has_title' => FALSE,
00126       'title_label' => t('Analysis'),
00127       'has_body' => FALSE,
00128       'body_label' => t('Analysis Description'),
00129       'locked' => TRUE
00130   );
00131   return $nodes;
00132 }
00133 /** 
00134  *  When a new chado_analysis node is created we also need to add information
00135  *  to our chado_analysis table.  This function is called on insert of a new
00136  *  node of type 'chado_analysis' and inserts the necessary information.
00137  *
00138  * @ingroup tripal_analysis
00139  */
00140 function chado_analysis_insert($node){
00141   global $user;
00142   // Create a timestamp so we can insert it into the chado database
00143   $time = $node->timeexecuted;
00144   $month = $time['month'];
00145   $day = $time['day'];
00146   $year = $time['year'];
00147   $timestamp = $month.'/'.$day.'/'.$year;
00148 
00149   // If this analysis already exists then don't recreate it in chado
00150   $analysis_id = $node->analysis_id;
00151   if ($analysis_id) {
00152     $sql = "SELECT analysis_id ".
00153                "FROM {Analysis} ".
00154                "WHERE analysis_id = %d ";
00155     $previous_db = tripal_db_set_active('chado');
00156     $analysis = db_fetch_object(db_query($sql, $node->analysis_id));
00157     tripal_db_set_active($previous_db);
00158   }
00159 
00160   // If the analysis doesn't exist then let's create it in chado.
00161   if(!$analysis){
00162       // insert and then get the newly inserted analysis record
00163       $values = array(
00164           'name' => $node->analysisname, 
00165           'description' => $node->description,
00166           'program' => $node->program,
00167           'programversion' => $node->programversion,
00168           'algorithm' => $node->algorithm,
00169           'sourcename' => $node->sourcename,
00170           'sourceversion' => $node->sourceversion,
00171           'sourceuri' => $node->sourceuri,
00172           'timeexecuted' => $timestamp
00173       );
00174       if(tripal_core_chado_insert('analysis',$values)){
00175          $analysis = tripal_core_chado_select('analysis',array('*'),$values);
00176          $analysis_id = $analysis[0]->analysis_id;
00177       }
00178   }
00179 
00180   // Make sure the entry for this analysis doesn't already exist in the
00181   // chado_analysis table if it doesn't exist then we want to add it.
00182   $node_check_sql = "SELECT * FROM {chado_analysis} ".
00183                      "WHERE analysis_id = %d";
00184   $node_check = db_fetch_object(db_query($node_check_sql, $analysis_id));
00185   if(!$node_check){
00186     // next add the item to the drupal table
00187     $sql = "INSERT INTO {chado_analysis} (nid, vid, analysis_id) ".
00188              "VALUES (%d, %d, %d)";
00189     db_query($sql,$node->nid,$node->vid,$analysis_id);
00190     // Create a title for the analysis node using the unique keys so when the
00191     // node is saved, it will have a title
00192     $record = new stdClass();
00193     // If the analysis has a name, use it as the node title. If not, construct
00194     // the title using program, programversion, and sourcename
00195     if ($node->analysisname) {
00196       $record->title = $node->analysisname;
00197     } else {
00198       //Construct node title as "program (version)
00199       $record->title = "$node->program ($node->programversion)";
00200     }
00201     $record->nid = $node->nid;
00202     drupal_write_record('node',$record,'nid');
00203     drupal_write_record('node_revisions',$record,'nid');
00204   }
00205 
00206    // add the analysis to the node object for
00207    // use by other analysis modules that may be using this function
00208    $node->analysis = $analysis;
00209    $node->analysis_id = $analysis_id; // we need to set this for children
00210 }
00211 /** 
00212  *
00213  * @ingroup tripal_analysis
00214  */
00215 function chado_analysis_delete($node){
00216   // Before removing, get analysis_id so we can remove it from chado database
00217   // later
00218   $sql_drupal = "SELECT analysis_id ".
00219                  "FROM {chado_analysis} ".
00220                  "WHERE nid = %d ".
00221                  "AND vid = %d";
00222   $analysis_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
00223 
00224   // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
00225   $sql_del = "DELETE FROM {chado_analysis} ".
00226               "WHERE nid = %d ".
00227               "AND vid = %d";
00228   db_query($sql_del, $node->nid, $node->vid);
00229   $sql_del = "DELETE FROM {node} ".
00230               "WHERE nid = %d ".
00231               "AND vid = %d";
00232   db_query($sql_del, $node->nid, $node->vid);
00233   $sql_del = "DELETE FROM {node_revisions} ".
00234               "WHERE nid = %d ".
00235               "AND vid = %d";
00236   db_query($sql_del, $node->nid, $node->vid);
00237 
00238   //Remove from analysis and analysisprop tables of chado database as well
00239   $previous_db = tripal_db_set_active('chado');
00240   db_query("DELETE FROM {analysis} WHERE analysis_id = %d", $analysis_id);
00241   tripal_db_set_active($previous_db);
00242 }
00243 
00244 /**
00245  * Update analyses
00246  *
00247  * @ingroup tripal_analysis
00248  */
00249 function chado_analysis_update($node){
00250   global $user;
00251   if($node->revision){
00252     // TODO -- decide what to do about revisions
00253   } else {
00254     // Create a timestamp so we can insert it into the chado database
00255     $time = $node->timeexecuted;
00256     $month = $time['month'];
00257     $day = $time['day'];
00258     $year = $time['year'];
00259     $timestamp = $month.'/'.$day.'/'.$year;
00260 
00261     // get the analysis_id for this node:
00262     $sql = "SELECT analysis_id ".
00263              "FROM {chado_analysis} ".
00264              "WHERE vid = %d";
00265     $analysis_id = db_fetch_object(db_query($sql, $node->vid))->analysis_id;
00266 
00267     $sql = "UPDATE {analysis} ".
00268              "SET name = '%s', ".
00269              "    description = '%s', ".
00270              "    program = '%s', ".
00271              "    programversion = '%s', ".
00272              "    algorithm = '%s', ".
00273              "    sourcename = '%s', ".
00274              "    sourceversion = '%s', ".
00275              "    sourceuri = '%s', ".
00276              "    timeexecuted = '%s' ".
00277              "WHERE analysis_id = %d ";
00278 
00279     $previous_db = tripal_db_set_active('chado');  // use chado database
00280     db_query($sql, $node->analysisname, $node->description, $node->program,
00281     $node->programversion,$node->algorithm,$node->sourcename,
00282     $node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
00283       tripal_db_set_active($previous_db);  // switch back to drupal database
00284     
00285     // Create a title for the analysis node using the unique keys so when the
00286     // node is saved, it will have a title
00287     $record = new stdClass();
00288     // If the analysis has a name, use it as the node title. If not, construct
00289     // the title using program, programversion, and sourcename
00290     if ($node->analysisname) {
00291       $record->title = $node->analysisname;
00292     } else {
00293       //Construct node title as "program (version)
00294       $record->title = "$node->program ($node->programversion)";
00295     }
00296     $record->nid = $node->nid;
00297     drupal_write_record('node',$record,'nid');
00298     drupal_write_record('node_revisions',$record,'nid');
00299   }
00300 }
00301 
00302 /**
00303  *  When editing or creating a new node of type 'chado_analysis' we need
00304  *  a form.  This function creates the form that will be used for this.
00305  *
00306  * @ingroup tripal_analysis
00307  */
00308 function chado_analysis_form ($node){
00309 
00310    $analysis = $node->analysis;
00311 
00312    // add in the description column. It is a text field and may not be included
00313    // if the text is too big.
00314    $analysis = tripal_core_expand_chado_vars($analysis,'field','analysis.description');
00315 
00316    // get form defaults
00317    $analysis_id = $node->analysis_id;
00318    if(!$analysis_id){
00319       $analysis_id = $analysis->analysis_id;
00320    }
00321    $analysisname = $node->analysisname;
00322    if(!$analysisname){
00323       $analysisname = $analysis->name;
00324    }
00325    $program = $node->program;
00326    if(!$program){
00327       $program = $analysis->program;
00328    }
00329    $programversion = $node->programversion;
00330    if(!$programversion){
00331       $programversion = $analysis->programversion;
00332    }
00333    $algorithm = $node->algorithm;
00334    if(!$algorithm){
00335       $algorithm = $analysis->algorithm;
00336    }
00337    $sourcename = $node->sourcename;
00338    if(!$sourcename){
00339       $sourcename = $analysis->sourcename;
00340    }
00341    $sourceversion = $node->sourceversion;
00342    if(!$sourceversion){
00343       $sourceversion = $analysis->sourceversion;
00344    }
00345    $sourceuri = $node->sourceuri;
00346    if(!$sourceuri){
00347       $sourceuri = $analysis->sourceuri;
00348    }
00349    $timeexecuted = $node->timeexecuted;
00350    if(!$timeexecuted){
00351       $timeexecuted = $analysis->timeexecuted;
00352    }
00353    $description = $node->description;
00354    if(!$description){
00355       $description = $analysis->description;
00356    }
00357   $form = array();
00358   $form['title']= array(
00359       '#type' => 'hidden',
00360       '#default_value' => $node->title,
00361   );
00362   $form['analysis_id']= array(
00363       '#type' => 'hidden',
00364       '#default_value' => $analysis_id,
00365   );
00366   $form['analysisname']= array(
00367       '#type' => 'textfield',
00368       '#title' => t('Analysis Name'),
00369       '#required' => FALSE,
00370       '#default_value' => $analysisname,
00371       '#description' => t("This should be a handy short identifier that 
00372          describes the analysis succintly as possible which helps the user find analyses."),
00373   );
00374   $form['program']= array(
00375       '#type' => 'textfield',
00376       '#title' => t('Program'),
00377       '#required' => TRUE,
00378       '#default_value' => $program,
00379       '#description' => t("Program name, e.g. blastx, blastp, sim4, genscan."),
00380   );
00381   $form['programversion']= array(
00382       '#type' => 'textfield',
00383       '#title' => t('Program Version'),
00384       '#required' => TRUE,
00385       '#default_value' => $programversion,
00386       '#description' => t("Version description, e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]"),
00387   );
00388   $form['algorithm']= array(
00389       '#type' => 'textfield',
00390       '#title' => t('Algorithm'),
00391       '#required' => FALSE,
00392       '#default_value' => $algorithm,
00393       '#description' => t("Algorithm name, e.g. blast."),
00394   );
00395   $form['sourcename']= array(
00396       '#type' => 'textfield',
00397       '#title' => t('Source Name'),
00398       '#required' => TRUE,
00399       '#default_value' => $sourcename,
00400       '#description' => t('The name of the source data.  This could be a file name, data set name or a 
00401            small description for how the data was collected.  For long descriptions use the description field below'),
00402 
00403   );
00404   $form['sourceversion']= array(
00405       '#type' => 'textfield',
00406       '#title' => t('Source Version'),
00407       '#required' => FALSE,
00408       '#default_value' => $sourceversion,
00409       '#description' => t('If the source dataset has a version, include it here'),
00410   );
00411   $form['sourceuri']= array(
00412       '#type' => 'textfield',
00413       '#title' => t('Source URI'),
00414       '#required' => FALSE,
00415       '#default_value' => $sourceuri,
00416       '#description' => t("This is a permanent URL or URI for the source of the analysis. 
00417          Someone could recreate the analysis directly by going to this URI and 
00418          fetching the source data (e.g. the blast database, or the training model)."),
00419   );
00420   // Get time saved in chado
00421   $default_time = $timeexecuted;
00422   $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
00423   $month = preg_replace("/^\d+-0?(\d+)-\d+ .*/", "$1", $default_time);
00424   $day = preg_replace("/^\d+-\d+-0?(\d+) .*/", "$1", $default_time);
00425   // If the time is not set, use current time
00426   if (!$default_time) {
00427     $default_time = time();
00428     $year = format_date($default_time, 'custom', 'Y');
00429     $month = format_date($default_time, 'custom', 'n');
00430     $day = format_date($default_time, 'custom', 'j');
00431   }
00432   $form['timeexecuted']= array(
00433       '#type' => 'date',
00434       '#title' => t('Time Executed'),
00435       '#required' => TRUE,
00436       '#default_value' => array(
00437          'year' => $year,
00438          'month' => $month,
00439          'day' => $day,
00440       ),
00441   );
00442   $form['description']= array(
00443       '#type' => 'textarea',
00444       '#rows' => 15,
00445       '#title' => t('Description and/or Program Settings'),
00446       '#required' => FALSE,
00447       '#default_value' => $description,
00448       '#description' => t('Please provide all necessary information to allow
00449          someone to recreate the analysis, including materials and methods
00450          for collection of the source data and performing the analysis'),
00451   );
00452   
00453   return $form;
00454 }
00455 
00456 /**
00457  *  When a node is requested by the user this function is called to allow us
00458  *  to add auxiliary data to the node object.
00459  *
00460  * @ingroup tripal_analysis
00461  */
00462 function chado_analysis_load($node){
00463 
00464    // get the feature details from chado
00465    $analysis_id = chado_get_id_for_node('analysis',$node);
00466 
00467    $values = array('analysis_id' => $analysis_id);
00468    $analysis = tripal_core_generate_chado_var('analysis',$values);
00469 
00470    $additions->analysis = $analysis;
00471    return $additions;
00472 }
00473 
00474 /**
00475  *  This function customizes the view of the chado_analysis node.  It allows
00476  *  us to generate the markup.
00477  *
00478  * @ingroup tripal_analysis
00479  */
00480 function chado_analysis_view ($node, $teaser = FALSE, $page = FALSE) {
00481   // use drupal's default node view:
00482   if (!$teaser) {
00483     $node = node_prepare($node, $teaser);
00484     // When previewing a node submitting form, it shows 'Array' instead of
00485     // correct date format. We need to format the date here
00486     $time = $node->timeexecuted;
00487     if(is_array($time)){
00488       $month = $time['month'];
00489       $day = $time['day'];
00490       $year = $time['year'];
00491       $timestamp = $year.'-'.$month.'-'.$day;
00492       $node->timeexecuted = $timestamp;
00493     }
00494   }
00495   return $node;
00496 }
00497 
00498 /**
00499  * Synchronize analyses from chado to drupal
00500  *
00501  * @ingroup tripal_analysis
00502  */
00503 function tripal_analysis_sync_analyses ($analysis_id = NULL, $job_id = NULL){
00504   global $user;
00505   $page_content = '';
00506     
00507   if(!$analysis_id){
00508     $sql = "SELECT Analysis_id, name, description, program, ".
00509        "  programversion, algorithm, sourcename, sourceversion, sourceuri, ".
00510           "  timeexecuted ".
00511           "FROM {Analysis} ";
00512     $previous_db = tripal_db_set_active('chado');  // use chado database
00513     $results = db_query($sql);
00514     tripal_db_set_active($previous_db);  // now use drupal database
00515   } else {
00516     $sql = "SELECT Analysis_id, name, description, program, ".
00517         "  programversion, algorithm, sourcename, sourceversion, sourceuri, ".
00518           "  timeexecuted ".
00519           "FROM {Analysis} ".
00520           "WHERE analysis_id = %d";
00521     $previous_db = tripal_db_set_active('chado');  // use chado database
00522     $results = db_query($sql,$analysis_id);
00523     tripal_db_set_active($previous_db);  // now use drupal database
00524   }
00525 
00526 
00527   // We'll use the following SQL statement for checking if the analysis
00528   // already exists as a drupal node.
00529   $sql = "SELECT * FROM {chado_analysis} ".
00530           "WHERE analysis_id = %d";
00531 
00532   while($analysis = db_fetch_object($results)){
00533         print "syncing analysis ";
00534         print $analysis->name;
00535         print ", ";
00536         print $analysis->analysis_id;
00537         print "\n";
00538 
00539     // check if this analysis already exists in the drupal database. if it
00540     // does then skip this analysis and go to the next one.
00541     if(!db_fetch_object(db_query($sql,$analysis->analysis_id))){
00542 
00543       $new_node = new stdClass();
00544         
00545       // try to access analysis type for this analysis
00546       $sql = "SELECT * FROM {analysisprop} 
00547                     WHERE analysis_id = %d 
00548                     AND type_id =  
00549                         (SELECT cvterm_id from {cvterm} where name = '%s')
00550             ";
00551       $previous_db = tripal_db_set_active('chado');
00552       $analysis_type = db_fetch_object(db_query($sql, $analysis->analysis_id, "analysis_type"));
00553       tripal_db_set_active($previous_db);
00554 
00555       // Get the type of analysis using cvterm_id
00556             // Current possibilities: kegg, unigene, interpro, blast
00557       if ($analysis_type) {
00558 
00559         // This is a unigene analysis
00560         if ($analysis_type->value == 'tripal_analysis_unigene') {
00561           $new_node->type = 'chado_analysis_unigene';
00562         // This is a blast analysis
00563         } else if ($analysis_type->value == 'tripal_analysis_blast') {
00564           $new_node->type = 'chado_analysis_blast';
00565          // This is a interpro analysis
00566         } else if ($analysis_type->value == 'tripal_analysis_interpro') {
00567           $new_node->type = 'chado_analysis_interpro';
00568          // This is a kegg analysis
00569         } else if ($analysis_type->value == 'tripal_analysis_kegg' ){
00570            $new_node->type = 'chado_analysis_kegg';
00571         } else {
00572            $new_node->type = 'chado_analysis';
00573         }
00574       // If it doesn't exist, this analysis is generic
00575       } else {
00576         $new_node->type = 'chado_analysis';
00577       }
00578 
00579             print "analysis type is $new_node->type\n";
00580 
00581       $new_node->uid = $user->uid;
00582       $new_node->analysis_id = $analysis->analysis_id;
00583       $new_node->analysisname = $analysis->name;
00584       $new_node->description = $analysis->description;
00585       $new_node->program = $analysis->program;
00586       $new_node->programversion = $analysis->programversion;
00587       $new_node->algorithm = $analysis->algorithm;
00588       $new_node->sourcename = $analysis->sourcename;
00589       $new_node->sourceversion = $analysis->sourceversion;
00590       $new_node->sourceuri = $analysis->sourceuri;
00591       $new_node->timeexecuted = $analysis->timeexecuted;
00592 
00593       // If the analysis has a name, use it as the node title. If not,
00594       // construct the title using program, programversion, and sourcename
00595       if ($new_node->analysisname) {
00596         $new_node->title = $new_node->analysisname;
00597       } else {
00598         //Construct node title as "program (version)"
00599         $new_node->title = "$analysis->program ($analysis->programversion)";
00600       }
00601 
00602       node_validate($new_node);
00603 
00604             $errors = form_get_errors();
00605 
00606             if($errors){
00607                 print_r($errors);
00608             }
00609             else{
00610       // if(!form_get_errors()){
00611         $node = node_submit($new_node);
00612         node_save($node);
00613 
00614         if($node->nid){
00615           $page_content .= "Added $new_node->title<br>";
00616         }
00617       }
00618     } else {
00619       $page_content .= "Skipped $new_node->title<br>";
00620     }
00621   }
00622   return $page_content;
00623 }
00624 
00625 /**
00626  *
00627  * @ingroup tripal_analysis
00628  */
00629 function chado_analysis_validate($node,&$form){
00630    // use the analysis parent to validate the node
00631    tripal_analysis_validate($node, $form); 
00632 }
00633 /**
00634  *
00635  *@ingroup tripal_analysis
00636  */
00637 function tripal_analysis_validate($node, &$form){
00638 ##dprint_r($node);
00639 
00640     // This validation is being used for three activities:
00641     // CASE A: Update a node that exists in both drupal and chado
00642     // CASE B: Synchronizing a node from chado to drupal
00643     // CASE C: Inserting a new node that exists in niether drupal nor chado
00644 
00645     // Only nodes being updated will have an nid already
00646     if($node->nid){
00647         //---------------------------------------------------
00648         // CASE A: We are validating a form for updating an existing node
00649         //---------------------------------------------------
00650 
00651         // TO DO: check that the new fields don't yield a non-unique primary key in chado
00652     }
00653     else{
00654         // To differentiate if we are syncing or creating a new analysis altogther, see if an
00655         // analysis_id already exists
00656 
00657         if($node->analysis_id){
00658 
00659             //---------------------------------------------------
00660             // CASE B: Synchronizing a node from chado to drupal
00661             //---------------------------------------------------
00662 
00663         }
00664         else{
00665 
00666             //---------------------------------------------------
00667             // CASE C: We are validating a form for inserting a new node
00668             //---------------------------------------------------
00669             // The primary key for the chado analysis table is 
00670             // program, programversion, sourcename
00671 
00672 
00673             $values = array(
00674                'program' => $node->program,
00675                'programversion' => $node->programversion,
00676                'sourcename' => $node->sourcename,
00677             );
00678             $analysis = tripal_core_chado_select('analysis', array('analysis_id'),$values);
00679             if(sizeof($analysis) > 0){
00680                form_set_error('program','Cannot add the analysis with this program,  
00681                   program version and source name. An analysis with these values already exists.');
00682                return;
00683             }
00684         }
00685     }
00686 }
00687 /**
00688  * Display help and module information
00689  * @param path which path of the site we're displaying help
00690  * @param arg array that holds the current path as would be returned from arg()
00691  * function
00692  * @return help text for the path
00693  *
00694  * @ingroup tripal_analysis
00695  */
00696 function tripal_analysis_help($path, $arg) {
00697   $output = '';
00698   switch ($path) {
00699     case "admin/help#tripal_analysis":
00700       $output = '<p>'.
00701       t("Displays links to nodes created on this date").
00702                 '</p>';
00703       break;
00704   }
00705   return $output;
00706 }
00707 /**
00708  *  The following function proves access control for users trying to
00709  *  perform actions on data managed by this module
00710  *
00711  * @ingroup tripal_analysis
00712  */
00713 function chado_analysis_access($op, $node, $account){
00714   if ($op == 'create') {
00715     return user_access('create chado_analysis content', $account);
00716   }
00717 
00718   if ($op == 'update') {
00719     if (user_access('edit chado_analysis content', $account)) {
00720       return TRUE;
00721     }
00722   }
00723   if ($op == 'delete') {
00724     if (user_access('delete chado_analysis content', $account)) {
00725       return TRUE;
00726     }
00727   }
00728   if ($op == 'view') {
00729     if (user_access('access chado_analysis content', $account)) {
00730       return TRUE;
00731     }
00732   }
00733   return FALSE;
00734 }
00735 
00736 /**
00737  *  Set the permission types that the chado module uses.  Essentially we
00738  *  want permissionis that protect creation, editing and deleting of chado
00739  #  data objects
00740  *
00741  * @ingroup tripal_analysis
00742  */
00743 function tripal_analysis_perm(){
00744   return array(
00745       'access chado_analysis content',
00746       'create chado_analysis content',
00747       'delete chado_analysis content',
00748       'edit chado_analysis content',
00749   );
00750 }
00751 
00752 /**
00753  *  We need to let drupal know about our theme functions and their arguments.
00754  *  We create theme functions to allow users of the module to customize the
00755  *  look and feel of the output generated in this module
00756  *
00757  * @ingroup tripal_analysis
00758  */
00759 function tripal_analysis_theme () {
00760   return array(
00761       'tripal_analysis_analysis_page' => array (
00762          'arguments' => array('analyses'),
00763       ),
00764   );
00765 }
00766 
00767 /**
00768  * This function uses analysis_id's of all drupal analysis nodes as input and
00769  * pull the analysis information (name, description, program, programversion,
00770  * algorithm, sourcename, sourceversion, sourceuri, timeexecuted) from
00771  * chado database. The return type is an object array that stores $analysis
00772  * objects sorted by program
00773  *
00774  * @ingroup tripal_analysis
00775  */
00776 function get_chado_analyses() {
00777 
00778   $sql_drupal = "SELECT COUNT (analysis_id) FROM {chado_analysis}";
00779   $no_orgs = db_result(db_query($sql_drupal));
00780   if ($no_orgs != 0) {
00781     $sql = "SELECT analysis_id, CA.nid, type FROM {chado_analysis} CA INNER JOIN node ON CA.nid = node.nid";
00782     $result = db_query($sql);
00783     $previous_db = tripal_db_set_active('chado');
00784     $sql = "SELECT Analysis_id, name, description, program, 
00785                    programversion, algorithm, sourcename, sourceversion, 
00786                    sourceuri, timeexecuted
00787             FROM {Analysis} WHERE analysis_id=%d";
00788     $analyses = array();
00789     $count = 0;
00790     while ($data = db_fetch_object($result)) {
00791       $analysis = db_fetch_object(db_query($sql, $data->analysis_id));
00792       $analysis->node_id = $data->nid;
00793       $analysis->node_type = $data->type;
00794       // Use node_type as the key so we can sort by node type
00795       // Since node_type is not unique by itself, we need to add
00796       // $count to the key
00797       $sortedBy = $analysis->timeexecuted;
00798       $analyses ["$sortedBy$count"] = $analysis;
00799       $count ++;
00800     }
00801     tripal_db_set_active($previous_db);
00802 
00803     //Sort analyses by time, descending order
00804     krsort($analyses, SORT_STRING);
00805 
00806     return $analyses;
00807   }
00808 }
00809 /**
00810  *
00811  *
00812  * @ingroup tripal_analysis
00813  */
00814 function theme_tripal_analysis_analysis_page($analyses) {
00815    
00816   $output = "<br>Analyses are listed in the descending order of their execution time.<br><a id=\"tripal_expandableBox_toggle_button\" onClick=\"toggleExpandableBoxes()\">[-] Collapse All</a>";
00817   
00818    foreach($analyses as $analysis){
00819     // Prepare information for html output
00820     $ana_node_url = url("node/$analysis->node_id");
00821      if ($analysis->sourceversion) {
00822          $ver = "($analysis->sourceversion)";
00823       }
00824       $date =  preg_replace("/^(\d+-\d+-\d+) .*/","$1",$analysis->timeexecuted);
00825       
00826       // Generate html output
00827     $output .= "<div class=\"tripal_chado_analysis-info-box\" style=\"padding:5px\">
00828                          <div class=\"tripal_expandableBox\">
00829                            <h3>$analysis->name ($date)</h3>
00830                          </div>
00831                          <div class=\"tripal_expandableBoxContent\">
00832                            <span>
00833                              <table class=\"tripal_chado_analysis_content\">
00834                                <tr><td>
00835                                  Name: <a href=\"$ana_node_url\">$analysis->name</a>
00836                                </td></tr>
00837                                <tr><td>
00838                                  Program: $analysis->program ($analysis->programversion)
00839                                </td></tr>
00840                                <tr><td>
00841                                  Algorithm: $analysis->algorithm
00842                                </td></tr>
00843                              <tr><td>
00844                                  Source: $analysis->sourcename $ver
00845                              </td></tr>
00846                              <tr><td>
00847                                  Source URI: $analysis->sourceuri
00848                                </td></tr>
00849                                <tr><td>
00850                                  Executed Time:$date
00851                                </td></tr>
00852                                <tr><td>
00853                                  Description: $analysis->description
00854                                </td></tr>
00855                              </table>
00856                            </span>
00857                          </div>
00858                        </div>";
00859   }
00860 
00861   return $output;
00862 }
00863 
00864 /**
00865  *
00866  *
00867  * @ingroup tripal_analysis
00868  */
00869 function tripal_analyses_cleanup($dummy = NULL, $job_id = NULL) {
00870 
00871     // select each node from node table with chado_analysis as type
00872     // check to make sure it also exists in chado_analysis table, delete if it doesn't
00873     // (this should never, ever happen, but we'll double check anyway)
00874   $sql_drupal_node = "SELECT * FROM {node} WHERE type LIKE 'chado_analysis%' order by nid";
00875     $sql_drupal_ca = "SELECT * from {chado_analysis} WHERE nid = %d";
00876 
00877   $results = db_query($sql_drupal_node);
00878   while($node = db_fetch_object($results)){
00879         $ca_record = db_fetch_object(db_query($sql_drupal_ca, $node->nid));
00880         if(!$ca_record){ 
00881             node_delete($node->nid); 
00882       $message = "Missing in chado_analysis table.... DELETING node: $nid->nid\n";
00883       watchdog('tripal_analysis',$message,array(),WATCHDOG_WARNING);
00884         }
00885   }
00886 
00887     // get nodes from chado_analysis table and load into array, saving chado analysis_id
00888     // as we iterate through, we'll check that they are actual nodes and
00889     // delete if they aren't
00890     // (this should never, ever happen, but we'll double check anyway)
00891   $sql_drupal_ca2 = "SELECT * FROM {chado_analysis}";
00892   $sql_drupal_node2 = "SELECT * FROM {node} WHERE type LIKE 'chado_analysis%' AND nid = %d";
00893 
00894   $results = db_query($sql_drupal_ca2);
00895   $nid2aid = array();
00896   while($ca_record = db_fetch_object($results)){
00897         $node = db_fetch_object(db_query($sql_drupal_node2, $ca_record->nid));
00898         if(!$node){ 
00899       db_query("DELETE FROM {chado_analysis} WHERE nid = $ca_record->nid");
00900       $message = "chado_analysis missing node.... DELETING chado_analysis record with nid: $ca_record->nid\n";
00901       watchdog('tripal_analysis',$message,array(),WATCHDOG_WARNING);
00902         }
00903         else{
00904         $nid2aid[$ca_record->nid] = $ca_record->analysis_id;
00905         }
00906   }
00907 
00908   // iterate through all of the chado_analysis nodes in drupal
00909     // and delete those that aren't valid in chado
00910     $sql_chado = "SELECT analysis_id from {analysis} WHERE analysis_id = %d";
00911 
00912   foreach($nid2aid as $nid => $aid){
00913         $previous_db = tripal_db_set_active('chado');
00914     $chado_record = db_fetch_object(db_query($sql_chado,$aid));
00915         tripal_db_set_active($previous_db);
00916     if(!$chado_record){
00917             node_delete($nid); 
00918       $message = "Missing in analysis table in chado.... DELETING node: $nid\n";
00919       watchdog('tripal_analysis',$message,array(),WATCHDOG_WARNING);
00920     }
00921   }
00922   return '';
00923 }
00924  
00925 /**
00926 function tripal_analysis_reindex_features ($analysis_id = NULL, $job_id = NULL){
00927   $i = 0;
00928 
00929   // if the caller provided a analysis_id then get all of the features
00930   // associated with the analysis. Otherwise get all sequences associated
00931   // with all libraries.
00932   if(!$analysis_id){
00933     $sql = "SELECT Analysis_id, Feature_id ".
00934           "FROM {Analysisfeature} ".
00935           "ORDER BY analysis_id";
00936     $previous_db = tripal_db_set_active('chado');  // use chado database
00937     $results = db_query($sql);
00938     tripal_db_set_active($previous_db);  // now use drupal database
00939   } else {
00940     $sql = "SELECT Analysis_id, Feature_id ".
00941              "FROM {Analysisfeature} ".
00942              "WHERE analysis_id = %d";
00943              "ORDER BY analysis_id";
00944     $previous_db = tripal_db_set_active('chado');  // use chado database
00945     $results = db_query($sql,$analysis_id);
00946     tripal_db_set_active($previous_db);  // now use drupal database
00947   }
00948 
00949   // load into ids array
00950   $count = 0;
00951   $ids = array();
00952   while($id = db_fetch_object($results)){
00953     $ids[$count] = $id->feature_id;
00954     $count++;
00955   }
00956 
00957   $interval = intval($count * 0.01);
00958   foreach($ids as $feature_id){
00959     // update the job status every 1% features
00960     if($job_id and $i % interval == 0){
00961       tripal_job_set_progress($job_id,intval(($i/$count)*100));
00962     }
00963     tripal_feature_sync_feature ($feature_id);
00964     $i++;
00965   }
00966 } */
00967  
00968 /**
00969 function tripal_analysis_taxonify_features ($analysis_id = NULL, $job_id = NULL){
00970   $i = 0;
00971 
00972   // if the caller provided a analysis_id then get all of the features
00973   // associated with the analysis. Otherwise get all sequences assoicated
00974   // with all libraries.
00975   if(!$analysis_id){
00976     $sql = "SELECT Analysis_id, Feature_id ".
00977           "FROM {Analysisfeature} ".
00978           "ORDER BY analysis_id";
00979     $previous_db = tripal_db_set_active('chado');  // use chado database
00980     $results = db_query($sql);
00981     tripal_db_set_active($previous_db);  // now use drupal database
00982   } else {
00983     $sql = "SELECT Analysis_id, Feature_id ".
00984              "FROM {Analysisfeature} ".
00985              "WHERE analysis_id = %d";
00986              "ORDER BY analysis_id";
00987     $previous_db = tripal_db_set_active('chado');  // use chado database
00988     $results = db_query($sql,$analysis_id);
00989     tripal_db_set_active($previous_db);  // now use drupal database
00990   }
00991 
00992   // load into ids array
00993   $count = 0;
00994   $ids = array();
00995   while($id = db_fetch_object($results)){
00996     $ids[$count] = $id->feature_id;
00997     $count++;
00998   }
00999 
01000   // make sure our vocabularies are set before proceeding
01001   tripal_feature_set_vocabulary();
01002 
01003   // use this SQL for getting the nodes
01004   $nsql =  "SELECT * FROM {chado_feature} CF ".
01005             "  INNER JOIN {node} N ON N.nid = CF.nid ".
01006             "WHERE feature_id = %d";
01007 
01008   // iterate through the features and set the taxonomy
01009   $interval = intval($count * 0.01);
01010   foreach($ids as $feature_id){
01011     // update the job status every 1% features
01012     if($job_id and $i % $interval == 0){
01013       tripal_job_set_progress($job_id,intval(($i/$count)*100));
01014     }
01015     $node = db_fetch_object(db_query($nsql,$feature_id));
01016     tripal_feature_set_taxonomy($node,$feature_id);
01017     $i++;
01018   }
01019 }
01020 */
01021 /**
01022  * Implements hook_views_api()
01023  * Purpose: Essentially this hook tells drupal that there is views support for
01024  *  for this module which then includes tripal_analysis.views.inc where all the
01025  *  views integration code is
01026  *
01027  * @ingroup tripal_analysis
01028  */ 
01029 function tripal_analysis_views_api() {
01030    return array(
01031       'api' => 2.0,
01032    );
01033 }
 All Classes Files Functions Variables