Tripal v1.0 (6.x-1.0)
tripal_analysis.module
Go to the documentation of this file.
00001 <?php
00002 
00017 require('api/tripal_analysis.api.inc');
00018 require('includes/tripal_analysis_privacy.inc');
00019 
00020 
00026 function tripal_analysis_init() {
00027   drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_analysis.js');
00028 }
00029 
00036 function tripal_analysis_menu() {
00037   //Sync analysis
00038   $items['chado_sync_analyses'] = array(
00039      'title' => 'Sync Data',
00040      'page callback' => 'tripal_analysis_sync_analyses',
00041      'access arguments' => array('administer tripal analyses'),
00042      'type' => MENU_CALLBACK
00043   );
00044   // Tripal Analysis administrative settings
00045   $items['admin/tripal/tripal_analysis'] = array(
00046       'title' => 'Analyses',
00047       'description' => 'Basic Description of Tripal Analysis Module Functionality.',
00048       'page callback' => 'theme',
00049       'page arguments' => array('tripal_analysis_admin'),
00050       'access arguments' => array('administer tripal analyses'),
00051       'type' => MENU_NORMAL_ITEM,
00052       'file' => 'includes/tripal_analysis.admin.inc',
00053   );
00054 
00055   $items['admin/tripal/tripal_analysis/configuration'] = array(
00056       'title' => 'Configuration',
00057       'description' => 'Settings for the displays of analysis results.',
00058       'page callback' => 'drupal_get_form',
00059       'page arguments' => array('tripal_analysis_admin'),
00060       'access arguments' => array('administer tripal analyses'),
00061       'type' => MENU_NORMAL_ITEM,
00062       'file' => 'includes/tripal_analysis.admin.inc',
00063   );
00064   return $items;
00065 }
00066 
00073 function tripal_analysis_node_info() {
00074   $nodes = array();
00075   $nodes['chado_analysis'] = array(
00076       'name' => t('Analysis'),
00077       'module' => 'chado_analysis',
00078       'description' => t('An analysis from the chado database'),
00079       'has_title' => FALSE,
00080       'title_label' => t('Analysis'),
00081       'has_body' => FALSE,
00082       'body_label' => t('Analysis Description'),
00083       'locked' => TRUE
00084   );
00085   return $nodes;
00086 }
00087 
00095 function chado_analysis_insert($node) {
00096   global $user;
00097 
00098   // Create a timestamp so we can insert it into the chado database
00099   $time = $node->timeexecuted;
00100   $month = $time['month'];
00101   $day = $time['day'];
00102   $year = $time['year'];
00103   $timestamp = $month . '/' . $day . '/' . $year;
00104 
00105   // If this analysis already exists then don't recreate it in chado
00106   $analysis_id = $node->analysis_id;
00107   if ($analysis_id) {
00108     $values = array('analysis_id' => $node->analysis_id);
00109     $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
00110     if($result and count($result) > 0) {
00111       $analysis = $result[0];
00112     }
00113   }
00114   
00115   // If the analysis doesn't exist then let's create it in chado.
00116   if (!$analysis) {
00117       // insert and then get the newly inserted analysis record
00118       $values = array(
00119           'name' => $node->analysisname,
00120           'description' => $node->description,
00121           'program' => $node->program,
00122           'programversion' => $node->programversion,
00123           'algorithm' => $node->algorithm,
00124           'sourcename' => $node->sourcename,
00125           'sourceversion' => $node->sourceversion,
00126           'sourceuri' => $node->sourceuri,
00127           'timeexecuted' => $timestamp
00128       );
00129       if (tripal_core_chado_insert('analysis', $values)) {
00130         $analysis = tripal_core_chado_select('analysis', array('*'), $values);
00131         $analysis_id = $analysis[0]->analysis_id;
00132       }
00133   }
00134 
00135   // Make sure the entry for this analysis doesn't already exist in the
00136   // chado_analysis table if it doesn't exist then we want to add it.
00137   $node_check_sql = "SELECT * FROM {chado_analysis} ".
00138                      "WHERE analysis_id = %d";
00139   $node_check = db_fetch_object(db_query($node_check_sql, $analysis_id));
00140   if (!$node_check) {
00141     // next add the item to the drupal table
00142     $sql = "INSERT INTO {chado_analysis} (nid, vid, analysis_id) ".
00143              "VALUES (%d, %d, %d)";
00144     db_query($sql, $node->nid, $node->vid, $analysis_id);
00145     // Create a title for the analysis node using the unique keys so when the
00146     // node is saved, it will have a title
00147     $record = new stdClass();
00148     // If the analysis has a name, use it as the node title. If not, construct
00149     // the title using program, programversion, and sourcename
00150     if ($node->analysisname) {
00151       $record->title = $node->analysisname;
00152     }
00153     else {
00154       //Construct node title as "program (version)
00155       $record->title = "$node->program ($node->programversion)";
00156     }
00157     $record->nid = $node->nid;
00158     drupal_write_record('node', $record, 'nid');
00159     drupal_write_record('node_revisions', $record, 'nid');
00160   }
00161 
00162    // add the analysis to the node object for
00163    // use by other analysis modules that may be using this function
00164     $node->analysis = $analysis;
00165     $node->analysis_id = $analysis_id; // we need to set this for children
00166 }
00167 
00176 function chado_analysis_delete($node) {
00177   $analysis_id = chado_get_id_for_node('analysis', $node);
00178   
00179   // if we don't have an organism id for this node then this isn't a node of
00180   // type chado_organism or the entry in the chado_organism table was lost.
00181   if (!$analysis_id) {
00182     return;
00183   }
00184 
00185   // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
00186   $sql_del = "DELETE FROM {chado_analysis} ".
00187               "WHERE nid = %d ".
00188               "AND vid = %d";
00189   db_query($sql_del, $node->nid, $node->vid);
00190   $sql_del = "DELETE FROM {node} ".
00191               "WHERE nid = %d ".
00192               "AND vid = %d";
00193   db_query($sql_del, $node->nid, $node->vid);
00194   $sql_del = "DELETE FROM {node_revisions} ".
00195               "WHERE nid = %d ".
00196               "AND vid = %d";
00197   db_query($sql_del, $node->nid, $node->vid);
00198 
00199   //Remove from analysis and analysisprop tables of chado database as well
00200   chado_query("DELETE FROM {analysis} WHERE analysis_id = %d", $analysis_id);
00201 }
00202 
00211 function chado_analysis_update($node) {
00212   global $user;
00213   if ($node->revision) {
00214     // TODO -- decide what to do about revisions
00215   }
00216   // Create a timestamp so we can insert it into the chado database
00217   $time = $node->timeexecuted;
00218   $month = $time['month'];
00219   $day = $time['day'];
00220   $year = $time['year'];
00221   $timestamp = $month . '/' . $day . '/' . $year;
00222 
00223   // get the analysis_id for this node:
00224   $sql = "SELECT analysis_id ".
00225            "FROM {chado_analysis} ".
00226            "WHERE nid = %d";
00227   $analysis_id = db_fetch_object(db_query($sql, $node->nid))->analysis_id;
00228 
00229   $sql = "UPDATE {analysis} ".
00230            "SET name = '%s', ".
00231            "    description = '%s', ".
00232            "    program = '%s', ".
00233            "    programversion = '%s', ".
00234            "    algorithm = '%s', ".
00235            "    sourcename = '%s', ".
00236            "    sourceversion = '%s', ".
00237            "    sourceuri = '%s', ".
00238            "    timeexecuted = '%s' ".
00239            "WHERE analysis_id = %d ";
00240 
00241   chado_query($sql, $node->analysisname, $node->description, $node->program,
00242     $node->programversion, $node->algorithm, $node->sourcename,
00243     $node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
00244 
00245   // Create a title for the analysis node using the unique keys so when the
00246   // node is saved, it will have a title
00247   $record = new stdClass();
00248   // If the analysis has a name, use it as the node title. If not, construct
00249   // the title using program, programversion, and sourcename
00250   if ($node->analysisname) {
00251     $record->title = $node->analysisname;
00252   }
00253   else {
00254     //Construct node title as "program (version)
00255     $record->title = "$node->program ($node->programversion)";
00256   }
00257 
00258   $record->nid = $node->nid;
00259   drupal_write_record('node', $record, 'nid');
00260   drupal_write_record('node_revisions', $record, 'nid');
00261 }
00262 
00269 function chado_analysis_form($node) {
00270 
00271   $analysis = $node->analysis;
00272 
00273   // add in the description column. It is a text field and may not be included
00274   // if the text is too big.
00275   $analysis = tripal_core_expand_chado_vars($analysis, 'field', 'analysis.description');
00276 
00277   // get form defaults
00278   $analysis_id = $node->analysis_id;
00279   if (!$analysis_id) {
00280     $analysis_id = $analysis->analysis_id;
00281   }
00282   $analysisname = $node->analysisname;
00283   if (!$analysisname) {
00284     $analysisname = $analysis->name;
00285   }
00286   $program = $node->program;
00287   if (!$program) {
00288     $program = $analysis->program;
00289   }
00290   $programversion = $node->programversion;
00291   if (!$programversion) {
00292     $programversion = $analysis->programversion;
00293   }
00294   $algorithm = $node->algorithm;
00295   if (!$algorithm) {
00296     $algorithm = $analysis->algorithm;
00297   }
00298   $sourcename = $node->sourcename;
00299   if (!$sourcename) {
00300     $sourcename = $analysis->sourcename;
00301   }
00302   $sourceversion = $node->sourceversion;
00303   if (!$sourceversion) {
00304     $sourceversion = $analysis->sourceversion;
00305   }
00306   $sourceuri = $node->sourceuri;
00307   if (!$sourceuri) {
00308     $sourceuri = $analysis->sourceuri;
00309   }
00310   $timeexecuted = $node->timeexecuted;
00311   if (!$timeexecuted) {
00312     $timeexecuted = $analysis->timeexecuted;
00313   }
00314   $description = $node->description;
00315   if (!$description) {
00316     $description = $analysis->description;
00317   }
00318   $form = array();
00319   $form['title']= array(
00320       '#type' => 'hidden',
00321       '#default_value' => $node->title,
00322   );
00323   $form['analysis_id']= array(
00324       '#type' => 'hidden',
00325       '#default_value' => $analysis_id,
00326   );
00327   $form['analysisname']= array(
00328       '#type' => 'textfield',
00329       '#title' => t('Analysis Name'),
00330       '#required' => TRUE,
00331       '#default_value' => $analysisname,
00332       '#description' => t("This should be a brief name that
00333          describes the analysis succintly. This name will helps the user find analyses."),
00334   );
00335   $form['program']= array(
00336       '#type' => 'textfield',
00337       '#title' => t('Program'),
00338       '#required' => TRUE,
00339       '#default_value' => $program,
00340       '#description' => t("Program name, e.g. blastx, blastp, sim4, genscan."),
00341   );
00342   $form['programversion']= array(
00343       '#type' => 'textfield',
00344       '#title' => t('Program Version'),
00345       '#required' => TRUE,
00346       '#default_value' => $programversion,
00347       '#description' => t("Version description, e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter 'n/a' if no version is available."),
00348   );
00349   $form['algorithm']= array(
00350       '#type' => 'textfield',
00351       '#title' => t('Algorithm'),
00352       '#required' => FALSE,
00353       '#default_value' => $algorithm,
00354       '#description' => t("Algorithm name, e.g. blast."),
00355   );
00356   $form['sourcename']= array(
00357       '#type' => 'textfield',
00358       '#title' => t('Source Name'),
00359       '#required' => TRUE,
00360       '#default_value' => $sourcename,
00361       '#description' => t('The name of the source data.  This could be a file name, data set name or a
00362            small description for how the data was collected.  For long descriptions use the description field below'),
00363 
00364   );
00365   $form['sourceversion']= array(
00366       '#type' => 'textfield',
00367       '#title' => t('Source Version'),
00368       '#required' => FALSE,
00369       '#default_value' => $sourceversion,
00370       '#description' => t('If the source dataset has a version, include it here'),
00371   );
00372   $form['sourceuri']= array(
00373       '#type' => 'textfield',
00374       '#title' => t('Source URI'),
00375       '#required' => FALSE,
00376       '#default_value' => $sourceuri,
00377       '#description' => t("This is a permanent URL or URI for the source of the analysis.
00378          Someone could recreate the analysis directly by going to this URI and
00379          fetching the source data (e.g. the blast database, or the training model)."),
00380   );
00381   // Get time saved in chado
00382   $default_time = $timeexecuted;
00383   $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
00384   $month = preg_replace("/^\d+-0?(\d+)-\d+ .*/", "$1", $default_time);
00385   $day = preg_replace("/^\d+-\d+-0?(\d+) .*/", "$1", $default_time);
00386   // If the time is not set, use current time
00387   if (!$default_time) {
00388     $default_time = time();
00389     $year = format_date($default_time, 'custom', 'Y');
00390     $month = format_date($default_time, 'custom', 'n');
00391     $day = format_date($default_time, 'custom', 'j');
00392   }
00393   $form['timeexecuted']= array(
00394       '#type' => 'date',
00395       '#title' => t('Time Executed'),
00396       '#required' => TRUE,
00397       '#default_value' => array(
00398          'year' => $year,
00399          'month' => $month,
00400          'day' => $day,
00401       ),
00402   );
00403   $form['description']= array(
00404       '#type' => 'textarea',
00405       '#rows' => 15,
00406       '#title' => t('Materials & Methods (Description and/or Program Settings)'),
00407       '#required' => FALSE,
00408       '#default_value' => $description,
00409       '#description' => t('Please provide all necessary information to allow
00410          someone to recreate the analysis, including materials and methods
00411          for collection of the source data and performing the analysis'),
00412   );
00413 
00414   return $form;
00415 }
00416 
00423 function chado_analysis_load($node) {
00424 
00425    // get the feature details from chado
00426   $analysis_id = chado_get_id_for_node('analysis', $node);
00427 
00428   $values = array('analysis_id' => $analysis_id);
00429   $analysis = tripal_core_generate_chado_var('analysis', $values);
00430 
00431   $additions = new stdClass();
00432   $additions->analysis = $analysis;
00433   return $additions;
00434 }
00435 
00442 function chado_analysis_view($node, $teaser = FALSE, $page = FALSE) {
00443   // use drupal's default node view:
00444   if (!$teaser) {
00445     $node = node_prepare($node, $teaser);
00446     // When previewing a node submitting form, it shows 'Array' instead of
00447     // correct date format. We need to format the date here
00448     $time = $node->timeexecuted;
00449     if (is_array($time)) {
00450       $month = $time['month'];
00451       $day = $time['day'];
00452       $year = $time['year'];
00453       $timestamp = $year . '-' . $month . '-' . $day;
00454       $node->timeexecuted = $timestamp;
00455     }
00456   }
00457   return $node;
00458 }
00459 
00460 
00461 
00467 function chado_analysis_validate($node, &$form) {
00468    // use the analysis parent to validate the node
00469   tripal_analysis_validate($node, $form);
00470 }
00471 
00480 function tripal_analysis_validate($node, &$form) {
00481   
00482  
00483   // Only nodes being updated will have an nid already
00484   if (!is_null($node->nid)) {    
00485     // CASE A: We are validating a form for updating an existing node
00486     
00487     // get the existing node    
00488     $values = array('analysis_id' => $node->analysis_id);      
00489     $result = tripal_core_chado_select('analysis', array('*'), $values);
00490     $analysis = $result[0];
00491       
00492     // if the name has changed make sure it doesn't conflict with an existing name
00493     if($analysis->name != $node->analysisname) {
00494       $values = array('name' => $node->analysisname);
00495       $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
00496       if($result and count($result) > 0) {
00497         form_set_error('analysisname', 'Cannot update the analysis with this analysis name. An analysis with this name already exists.');
00498         return;
00499       }  
00500     }
00501     
00502     // if the unique constraint has changed check to make sure it doesn't conflict with an
00503     // existing record
00504     if($analysis->program != $node->program or $analysis->programversion != $node->programversion or 
00505        $analysis->sourcename != $node->sourcename) {
00506       $values = array(
00507         'program' => $node->program,
00508         'programversion' => $node->programversion,
00509         'sourcename' => $node->sourcename,
00510       );
00511       $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
00512       if ($result and count($result) > 0) {
00513         if ($analysis->program != $node->program) {
00514           $field = 'program';  
00515         }
00516         if ($analysis->programversion != $node->programversion) {
00517           $field = 'programversion';  
00518         }
00519         if ($analysis->sourcename != $node->sourcename) {
00520           $field = 'sourcename';  
00521         }
00522         form_set_error($field, 'Cannot update the analysis with this program,
00523           program version and source name. An analysis with these values already exists.');
00524         return;
00525       }  
00526     }
00527   }
00528   else{
00529     // To differentiate if we are syncing or creating a new analysis altogther, see if an
00530     // analysis_id already exists
00531     if ($node->analysis_id and $node->analysis_id != 0) {
00532       // CASE B: Synchronizing a node from chado to drupal
00533       // we don't need to do anything.
00534     }
00535     else {
00536       // CASE C: We are validating a form for inserting a new node
00537       // The unique constraint for the chado analysis table is: program, programversion, sourcename
00538       $values = array(
00539         'program' => $node->program,
00540         'programversion' => $node->programversion,
00541         'sourcename' => $node->sourcename,
00542       );
00543       $analysis = tripal_core_chado_select('analysis', array('analysis_id'), $values);
00544       if ($analysis and count($analysis) > 0) {
00545         form_set_error('program', 'Cannot add the analysis with this program,
00546           program version and source name. An analysis with these values already exists.');
00547         return;
00548       }
00549       
00550       // make sure we have a unique analysis name. This is not a requirement 
00551       // for the analysis table but we use the analysis name for the Drupal node
00552       // title, so it should be unique      
00553       $values = array('name' => $node->analysisname);
00554       $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
00555       if($result and count($result) > 0) {
00556         form_set_error('analysisname', 'Cannot add the analysis with this analysis name. An analysis with this name already exists.');
00557         return;
00558       }
00559     }
00560   }
00561 }
00562 
00572 function tripal_analysis_help($path, $arg) {
00573   $output = '';
00574   switch ($path) {
00575     case "admin/help#tripal_analysis":
00576       $output = '<p>' .
00577       t("Displays links to nodes created on this date") .
00578                 '</p>';
00579       break;
00580   }
00581   return $output;
00582 }
00583 
00590 function chado_analysis_access($op, $node, $account) {
00591 
00592   if ($op == 'create') {
00593     if (!user_access('create chado_analysis content', $account)) {
00594       return FALSE;
00595       }
00596   }
00597   if ($op == 'update') {
00598     if (!user_access('edit chado_analysis content', $account)) {
00599       return FALSE;
00600     }
00601   }
00602   if ($op == 'delete') {
00603     if (!user_access('delete chado_analysis content', $account)) {
00604       return FALSE;
00605     }
00606   }
00607   if ($op == 'view') {
00608     if (!user_access('access chado_analysis content', $account)) {
00609       return FALSE;
00610       }
00611   }
00612     return NULL;
00613 }
00614 
00622 function tripal_analysis_perm() {
00623   return array(
00624     'access chado_analysis content',
00625     'create chado_analysis content',
00626     'delete chado_analysis content',
00627     'edit chado_analysis content',
00628     'administer tripal analyses',
00629   );
00630 }
00631 
00639 function tripal_analysis_theme() {
00640   return array(
00641     'tripal_analysis_base' => array(
00642        'arguments' => array('node' => NULL),
00643        'template' => 'tripal_analysis_base',
00644     ),
00645     'tripal_feature_analyses' => array(
00646       'template' => 'tripal_feature_analyses',  
00647       'arguments' =>  array('node' => NULL),  
00648     ),
00649     'tripal_analysis_admin' => array(
00650       'template' => 'tripal_analysis_admin',  
00651       'arguments' =>  array(NULL),  
00652       'path' => drupal_get_path('module', 'tripal_analysis') . '/theme', 
00653     ),
00654   );
00655 }
00661 function tripal_analysis_block($op = 'list', $delta = 0, $edit=array()) {
00662   switch ($op) {
00663     case 'list':
00664       $blocks['base']['info'] = t('Tripal Analysis Details');
00665       $blocks['base']['cache'] = BLOCK_NO_CACHE;
00666       
00667       $blocks['featureblast']['info'] = t('Tripal Feature Analyses');
00668       $blocks['featureblast']['cache'] = BLOCK_NO_CACHE;
00669       
00670       return $blocks;
00671 
00672   case 'view':
00673     if (user_access('access chado_analysis content') and arg(0) == 'node' and is_numeric(arg(1))) {
00674       $nid = arg(1);
00675       $node = node_load($nid);
00676 
00677       $block = array();
00678       switch ($delta) {       
00679         case 'base':
00680           $block['subject'] = t('Analysis Details');
00681           $block['content'] = theme('tripal_analysis_base', $node);
00682           break;
00683         case 'tripal_feature_analyses':
00684           $block['subject'] = t('Feature Analyses');
00685           $block['content'] = theme('tripal_feature_analyses', $node);
00686           break;
00687         default :
00688       }
00689       return $block;
00690     }
00691   }
00692 }
00693 
00694 /*******************************************************************************
00695  * tripal_analysis_nodeapi()
00696  * HOOK: Implementation of hook_nodeapi()
00697  * Display blast results for allowed node types
00698  */
00699 function tripal_analysis_nodeapi(&$node,  $op, $teaser, $page) {
00700 
00701   switch ($op) {
00702     case 'view':
00703 
00704       if ($teaser) {
00705         return '';
00706       }
00707       // Abort if this node is not one of the types we should show.
00708       if (strcmp($node->type, 'chado_feature') == 0) {
00709         if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
00710            // return results for searching
00711         } 
00712         else {
00713            // return normal results
00714            $node->content['tripal_feature_analyses'] = array(
00715                   '#value' => theme('tripal_feature_analyses',   $node),
00716                   '#weight' => 8
00717            );
00718         }
00719       }
00720       break;
00721   }
00722 }
00723 
00732 function tripal_analysis_views_api() {
00733     return array(
00734     'api' => 2.0,
00735   );
00736 }
00737 
00738 
 All Classes Files Functions Variables