123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981 |
- <?php
- /*******************************************************************************
- *
- ******************************************************************************/
- function tripal_analysis_kegg_init(){
- // add the tripal_analysis_kegg JS and CSS
- drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_analysis_kegg.js');
- drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_analysis_kegg.css');
- // add the jsTree JS and CSS
- drupal_add_css(drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.css');
- drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jsTree/source/_lib.js');
- drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.js');
- }
- /*******************************************************************************
- * Provide information to drupal about the node types that we're creating
- * in this module
- */
- function tripal_analysis_kegg_node_info() {
- $nodes = array();
- $nodes['chado_analysis_kegg'] = array(
- 'name' => t('Analysis: KEGG'),
- 'module' => 'chado_analysis_kegg',
- 'description' => t('Results from a KEGG/KAAS analysis'),
- 'has_title' => FALSE,
- 'title_label' => t('Analysis: KEGG'),
- 'has_body' => FALSE,
- 'body_label' => t('KEGG Analysis Description'),
- 'locked' => TRUE
- );
- return $nodes;
- }
- /*******************************************************************************
- * Menu items are automatically added for the new node types created
- * by this module to the 'Create Content' Navigation menu item. This function
- * adds more menu items needed for this module.
- */
- function tripal_analysis_kegg_menu() {
- $items['brite/%'] = array(
- 'title' => t('KEGG BRITE'),
- 'page callback' => 'tripal_analysis_kegg_brite',
- 'page arguments' => array(1, 2),
- 'access arguments' => array('access content'),
- 'type' => MENU_CALLBACK
- );
- $items['tripal_analysis_kegg_org_report/%'] = array(
- 'path' => 'tripal_analysis_kegg_org_report',
- 'title' => t('Analysis KEGG report'),
- 'page callback' => 'tripal_analysis_kegg_org_report',
- 'page arguments' => array(1),
- 'access arguments' => array('access chado_analysis_kegg content'),
- 'type' => MENU_CALLBACK
- );
- return $items;
- }
- /*******************************************************************************
- * Set the permission types that the chado module uses. Essentially we
- * want permissionis that protect creation, editing and deleting of chado
- * data objects
- */
- function tripal_analysis_kegg_perm(){
- return array(
- 'access chado_analysis_kegg content',
- 'create chado_analysis_kegg content',
- 'delete chado_analysis_kegg content',
- 'edit chado_analysis_kegg content',
- );
- }
- /*******************************************************************************
- * The following function proves access control for users trying to
- * perform actions on data managed by this module
- */
- function chado_analysis_kegg_access($op, $node, $account){
- if ($op == 'create') {
- return user_access('create chado_analysis_kegg content', $account);
- }
- if ($op == 'update') {
- if (user_access('edit chado_analysis_kegg content', $account)) {
- return TRUE;
- }
- }
- if ($op == 'delete') {
- if (user_access('delete chado_analysis_kegg content', $account)) {
- return TRUE;
- }
- }
- if ($op == 'view') {
- if (user_access('access chado_analysis_kegg content', $account)) {
- return TRUE;
- }
- }
- return FALSE;
- }
- /*******************************************************************************
- */
- function tripal_analysis_kegg_brite($analysis_id, $type_id, $ajax){
- // If not called by ajax
- if (!$ajax) {
- $content .=
- "<div id=\"tripal_kegg_brite_results\" class=\"tripal_kegg_brite-info-box\">
- <table>
- <tr>
- <th>KEGG BRITE</th>
- <th id=\"tripal_kegg_brite_header\">Hierarchy:</th>
- </tr>
- <tr>
- <td nowrap valign=\"top\">
- ";
- // List all BRITE terms on the left
- $sql = "SELECT CVT.name, CVT.cvterm_id
- FROM {cvterm} CVT
- INNER JOIN analysisprop AP ON CVT.cvterm_id = AP.type_id
- WHERE AP.analysis_id = %d AND CVT.definition LIKE 'KEGG BRITE term: %'
- ORDER BY CVT.cvterm_id";
- $previous_db = tripal_db_set_active('chado');
- $result = db_query($sql, $analysis_id);
- tripal_db_set_active($previous_db);
- while ($brite_term = db_fetch_object($result)) {
- $url = url("brite/$analysis_id/$brite_term->cvterm_id/1");
- $content .= "<li class=\"tripal_kegg_brite_terms\"><a onclick=\"return tripal_update_brite(".
- "this,$brite_term->cvterm_id)\" href=\"$url\">
- $brite_term->name
- </a></li>";
- }
- // Show the hierarchy tree
- $content .="</td>
- <td nowrap id=\"tripal_kegg_brite_hierarchy\" valign=\"top\">";
- $content .= "<i>Note:</i> Click a BRITE term for its functional hierarchy";
-
- // If called by ajax, generate tree structure
- } else {
- // Get BRITE term from cvterm table
- $previous_db = tripal_db_set_active('chado');
- $sql = 'SELECT name FROM {cvterm} WHERE cvterm_id=%d';
- $brite_term = db_result(db_query($sql, $type_id));
- // Get BRITE hierarchy tree
- $sql = "SELECT value
- FROM {analysisprop} AP
- INNER JOIN CVterm CVT on AP.type_id = CVT.cvterm_id
- INNER JOIN CV on CVT.cv_id = CV.cv_id
- WHERE CV.name = 'tripal' and CVT.name = '%s'
- AND AP.analysis_id = %d";
- $result = db_fetch_object(db_query($sql, $brite_term, $analysis_id));
- tripal_db_set_active($previous_db);
- $content .= "<div class=\"tripal_kegg_brite_tree\" id=\"tripal_kegg_brite_tree_$type_id\">$result->value</div>";
- }
-
- if (!$ajax) {
- $content .= " </td>
- </tr>
- </table>
- </div>";
- }
- // since this function provides output for addition into
- // an analysis page, as well as an AJAX refresh of content
- // within the BRITE hierarchy we need to setup the return
- // different depending on the request type
- if($ajax){
- drupal_json(array('update' => $content,
- 'id' => "tripal_kegg_brite_tree_$type_id",
- 'brite_term' => "Hierarchy: $brite_term"));
- } else {
- return $content;
- }
- }
- /*******************************************************************************
- * Provide a KEGG Analysis form
- */
- function chado_analysis_kegg_form ($node){
- // add in the default fields
- $form = chado_analysis_form($node);
- // set the defaults
- $kegg = $node->analysis->tripal_analysis_kegg;
- $query_re = $kegg->query_re;
- $query_type = $kegg->query_type;
- $query_uniquename = $kegg->query_uniquename;
- $hierfile = $kegg->hierfile;
- $moreSettings ['kegg'] = 'KEGG Analysis Settings';
- $form['kegg'] = array(
- '#title' => t('KEGG Settings'),
- '#type' => 'fieldset',
- '#description' => t('Specific Settings for KEGG Analysis.'),
- '#collapsible' => TRUE,
- '#attributes' => array('id' => 'kegg-extra-settings'),
- '#weight' => 11
- );
- $form['kegg']['hierfile'] = array(
- '#title' => t('KAAS hier.tar.gz Output File'),
- '#type' => 'textfield',
- '#description' => t('The full path to the hier.tar.gz file generated by KAAS.
- Alternatively, you can input the full path to the directory
- that contains decompressed kegg files.'),
- '#default_value' => $hierfile,
- );
- $form['kegg']['query_re'] = array(
- '#title' => t('Query Name RE'),
- '#type' => 'textfield',
- '#description' => t('Enter the regular expression that will extract the '.
- 'feature name from the results line in the KEGG heir results. This will be '.
- 'the same as the definition line in the query FASTA file used for the analysis. This option is '.
- 'is only required when the query does not identically match a feature '.
- 'in the database.'),
- '#default_value' => $query_re,
- );
- $form['kegg']['query_type'] = array(
- '#title' => t('Query Type'),
- '#type' => 'textfield',
- '#description' => t('Please enter the Sequence Ontology term that describes '.
- 'the query sequences used for KEGG. This is only necessary if two '.
- 'or more sequences have the same name.'),
- '#default_value' => $query_type,
- );
- $form['kegg']['query_uniquename'] = array(
- '#title' => t('Use Unique Name'),
- '#type' => 'checkbox',
- '#description' => t('Select this checboxk if the feature name in the KEGG heir file '.
- 'matches the uniquename in the database. By default, the feature will '.
- 'be mapped to the "name" of the feature.'),
- '#default_value' => $query_uniquename,
- );
- $form['kegg']['keggjob'] = array(
- '#type' => 'checkbox',
- '#title' => t('Submit a job to parse the kegg output into analysisfeatureprop table'),
- '#description' => t('Note: features associated with the KAAS results must '.
- 'exist in chado before parsing the file. Otherwise, KEGG '.
- 'results that cannot be linked to a feature will be '.
- 'discarded.'),
- );
- return $form;
- }
- /*******************************************************************************
- *
- */
- function chado_analysis_kegg_insert($node){
- // insert the analysis
- chado_analysis_insert($node);
- // set the type for this analysis
- tripal_analysis_insert_property($node->analysis_id,'analysis_type','tripal_analysis_kegg');
- // now add in the remaining settings as a single property but separated by bars
- tripal_analysis_insert_property($node->analysis_id,'analysis_kegg_settings',$node->hierfile);
- tripal_analysis_insert_property($node->analysis_id,'analysis_kegg_query_re',$node->query_re);
- tripal_analysis_insert_property($node->analysis_id,'analysis_kegg_query_type',$node->query_type);
- tripal_analysis_insert_property($node->analysis_id,'analysis_kegg_query_uniquename',$node->query_uniquename);
-
- // Add a job if the user wants to parse the html output
- chado_analysis_kegg_submit_job($node);
- }
- /**
- *
- */
- function chado_analysis_kegg_submit_job($node){
- global $user;
- if($node->keggjob) {
- $job_args[0] = $node->analysis_id;
- $job_args[1] = $node->hierfile;
- $job_args[2] = base_path();
- $job_args[3] = $node->query_re;
- $job_args[4] = $node->query_type;
- $job_args[5] = $node->query_uniquename;
- if (is_readable($node->hierfile)) {
- $fname = preg_replace("/.*\/(.*)/", "$1", $node->hierfile);
- tripal_add_job("Parse KAAS output: $fname",'tripal_analysis_kegg',
- 'tripal_analysis_kegg_parseHierFile', $job_args, $user->uid);
- } else {
- drupal_set_message("Can not open KAAS hier.tar.gz output file. Job not scheduled.");
- }
- }
- }
- /*******************************************************************************
- * Delete KEGG anlysis
- */
- function chado_analysis_kegg_delete($node){
- chado_analysis_delete($node);
- }
- /*******************************************************************************
- * Update KEGG analysis
- */
- function chado_analysis_kegg_update($node){
- // insert the analysis
- chado_analysis_update($node);
- // set the type for this analysis
- tripal_analysis_update_property($node->analysis_id,'analysis_type','tripal_analysis_kegg',1);
- // now add in the remaining settings as a single property but separated by bars
- tripal_analysis_update_property($node->analysis_id,'analysis_kegg_settings',$node->hierfile,1);
- tripal_analysis_update_property($node->analysis_id,'analysis_kegg_query_re',$node->query_re,1);
- tripal_analysis_update_property($node->analysis_id,'analysis_kegg_query_type',$node->query_type,1);
- tripal_analysis_update_property($node->analysis_id,'analysis_kegg_query_uniquename',$node->query_uniquename,1);
-
- // Add a job if the user wants to parse the html output
- chado_analysis_kegg_submit_job($node);
- }
- /*******************************************************************************
- * When a node is requested by the user this function is called to allow us
- * to add auxiliary data to the node object.
- */
- function chado_analysis_kegg_load($node){
- // load the default set of analysis fields
- $additions = chado_analysis_load($node);
- // create some variables for easier lookup
- $analysis = $additions->analysis;
- $analysis_id = $analysis->analysis_id;
- // get the heirfile name
- $hierfile = tripal_analysis_get_property($analysis_id,'analysis_kegg_settings');
- $query_re = tripal_analysis_get_property($analysis->analysis_id,'analysis_kegg_query_re');
- $query_type = tripal_analysis_get_property($analysis->analysis_id,'analysis_kegg_query_type');
- $query_uniquename= tripal_analysis_get_property($analysis->analysis_id,'analysis_kegg_query_uniquename');
- $analysis->tripal_analysis_kegg->hierfile = $hierfile->value;
- $analysis->tripal_analysis_kegg->query_re = $query_re->value;
- $analysis->tripal_analysis_kegg->query_type = $query_type->value;
- $analysis->tripal_analysis_kegg->query_uniquename= $query_uniquename->value;
- return $additions;
- }
- /**
- *
- */
- function chado_analysis_kegg_view ($node, $teaser = FALSE, $page = FALSE) {
- // use drupal's default node view:
- if (!$teaser) {
- $node = node_prepare($node, $teaser);
- // When previewing a node submitting form, it shows 'Array' instead of
- // correct date format. We need to format the date here
- $time = $node->timeexecuted;
- if(is_array($time)){
- $month = $time['month'];
- $day = $time['day'];
- $year = $time['year'];
- $timestamp = $year.'-'.$month.'-'.$day;
- $node->timeexecuted = $timestamp;
- }
- }
- return $node;
- }
- /********************************************************************************
- */
- function tripal_analysis_kegg_parseHierFile ($analysis_id, $hierfile, $base_path,
- $query_re,$query_type,$query_uniquename,$job_id) {
-
- // If user input a file (e.g. hier.tar.gz), decompress it first
- if (is_file($hierfile)) {
- $data_dir = file_directory_path() . "/tripal/tripal_analysis_kegg";
- $stderr = shell_exec("cd $data_dir; tar -zxf $hierfile;");
- print "$stderr\n";
- $hierdir = $data_dir."/hier";
- // Otherwise, treat it as a directory
- } else {
- $hierdir = $hierfile;
- }
- $dir_handle = @opendir($hierdir) or die("Unable to open $hierdir");
- $total_files = count(glob($hierdir . '/*.*'));
- print "There are $total_files keg file(s).\n";
- $interval = intval($total_files * 0.01);
- $no_file = 0;
- while ($file = readdir($dir_handle)) {
- if(preg_match("/^.*\.keg/",$file)){
- // Update the progress
- if ($no_file % $interval == 0) {
- $percentage = (int) ($no_file / $total_files * 100);
- tripal_job_set_progress($job_id, $percentage);
- print $percentage."% ";
- }
- $no_file ++;
-
- # $type variable will be set in tripal_analysis_kegg_parse_kegg_file()
- $content = tripal_analysis_kegg_parse_kegg_file("$hierdir/$file",$type,$analysis_id, $base_path, $query_re,$query_type,$query_uniquename);
- # add the item to the database
- if($content){
- //------------------------------------------------------
- // Insert into analysisprop table
- //------------------------------------------------------
- // Make sure the same value doesn't exist before inserting into chado
- $sql = "SELECT value
- FROM {analysisprop}
- WHERE analysis_id = %d
- AND type_id = (SELECT cvterm_id
- FROM {cvterm} CVT
- INNER JOIN CV ON CVT.cv_id = CV.cv_id
- WHERE CV.name = 'tripal' AND CVT.name = '%s'
- )
- ";
- $previous_db = tripal_db_set_active('chado');
- $oldvalue = db_result(db_query($sql, $analysis_id, $type));
- tripal_db_set_active($previous_db);
-
- // Insert the content
- if ($oldvalue != $content) {
-
- $previous_db = tripal_db_set_active('chado'); // Use chado database
- // Get type_id for the BRITE term
- $sql = "SELECT cvterm_id
- FROM {cvterm} CVT
- INNER JOIN CV ON CVT.cv_id = CV.cv_id
- WHERE CV.name = 'tripal' AND CVT.name = '%s'";
- $brite_cvterm_id = db_result(db_query($sql, $type));
- // Replace all single quote as HTML code before insert
- $content = preg_replace("/\'/", "'", $content);
-
- // Insert the value
- $sql = "INSERT INTO {analysisprop} (analysis_id, type_id, value)
- VALUES (%d, %d,'$content')";
-
- db_query($sql, $analysis_id, $brite_cvterm_id);
- tripal_db_set_active($previous_db); // Use drupal database
- }
-
- }
- }
- }
- print "Done.\n";
- closedir($dir_handle);
- // If user input a file, remove decompressed files after parsing
- if (is_file($hierfile)) {
- $stderr = shell_exec("rm -r $hierdir;");
- print "$stderr\n";
- }
- return;
- }
- /*******************************************************************************
- * Parse *.keg files.
- * Each file has a definition line. BRITE term is extracted * from this line
- * and added to chado as a cvterm. Tree structure for this cvterm is then
- * generated and saved to analysisfeature and analysisfeatureprop tables.
- */
- function tripal_analysis_kegg_parse_kegg_file ($file,&$type,$analysis_id, $base_path, $query_re,$query_type,$query_uniquename){
- $handle = fopen($file,'r');
- $depth = array();
- $current = '@'; # this is one character below 'A' in the ASCII table
- $prev = '@';
- $id = 0;
- $type_id = 0;
- $no_line = 0;
- while($line = fgets($handle)){
- $no_line ++;
- // Find out what kind of file we're looking at.
- if(preg_match("/#DEFINITION\s+(.*)\s+.+?$/",$line,$matches)){
- // Set type as the matched term in the DEFINITION line and add it as a new cvterm
- $type = $matches[1];
- // Before inserting, make sure this cvterm doesn't exist
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT cvterm_id
- FROM {cvterm} CVT
- INNER JOIN cv ON cv.cv_id = CVT.cv_id
- WHERE cv.name = 'tripal'
- AND CVT.name = '%s'";
- $type_id = db_result(db_query($sql, $type));
- tripal_db_set_active($previous_db);
- if (!$type_id) {
- tripal_add_cvterms($type, "KEGG BRITE term: $type");
- // Get newly added type_id
- $sql = "SELECT cvterm_id
- FROM {cvterm} CVT
- INNER JOIN cv ON cv.cv_id = CVT.cv_id
- WHERE cv.name = 'tripal'
- AND CVT.name = '%s'";
- $previous_db = tripal_db_set_active('chado');
- $type_id = db_result(db_query($sql, $type));
- tripal_db_set_active($previous_db);
- }
- }
- // get the depth of the hierarch (e.g. A,B,C or D);
- preg_match("/^([ABCDEFGHIJKLMNOP])\s*(.*)/",$line,$matches);
- # skip lines that aren't data or are empty
- if(!$matches[1] or !$matches[2]){continue;}
- $prev = $current;
- $current = $matches[1];
- for($i = (ord($current) - ord($prev)); $i < 0; $i++){
- $content .= "</li></ul>\n";
- }
- for($i = 0; $i < (ord($current) - ord($prev)); $i++){
- $content .= "<ul>\n";
- }
-
- // change all relative paths to absolute paths pointing to KEGG (www.genome.jp)
- $matches[2] = preg_replace("/<a href=\"\//i","<a href=\"http://www.genome.jp/",$matches[2]);
-
- // add id to <a> tags so we can link kegg.gif to it in tripal_analysis_kegg.css
- $matches[2] = preg_replace("/<a href=\"/i","<a id=\"tripal_kegg_brite_links\" target=\"_blank\" href=\"",$matches[2]);
- // extract the features that have been mapped to the KEGG IDs
- if(preg_match("/^(.*?);\s*(\<a.+)/",$matches[2],$defline)){
- // Find cvterm_id for 'kegg_brite_data'
- $sql = "SELECT cvterm_id
- FROM {cvterm} CVT
- INNER JOIN cv ON cv.cv_id = CVT.cv_id
- WHERE cv.name = 'tripal'
- AND CVT.name = '%s'";
- $previous_db = tripal_db_set_active('chado');
- $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
- tripal_db_set_active($previous_db);
- // get the feature name using the user's regular expression
- if ($query_re and preg_match("/$query_re/", $defline[1], $parts)) {
- $feature = $parts[1];
- }
- // If not in above format then pull up to the first space
- else {
- if (preg_match('/^(.*?)\s.*$/', $defline[1], $parts)) {
- $feature = $parts[1];
- }
- // if no match up to the first space then just use the entire string
- else {
- $feature = $defline[1];
- }
- }
- // now find the feature in chado
- $select = array();
- if($query_uniquename){
- $select['uniquename'] = $feature;
- } else {
- $select['name'] = $feature;
- }
- if($query_type){
- $select['type_id'] = array(
- 'cv_id' => array(
- 'name' => 'sequence'
- ),
- 'name' => $query_type,
- );
- }
- $feature_arr = tripal_core_chado_select('feature',array('feature_id'),$select);
- if(count($feature_arr) > 1){
- print "Ambiguous: '$feature' matches more than one feature and is being skipped.\n";
- continue;
- }
- if(count($feature_arr) == 0){
- print "Failed: '$feature' cannot find a matching feature in the database.\n";
- continue;
- }
- print "Adding KEGG results for $feature ($feature_id,$analysis_id)\n";
- $feature_id = $feature_arr[0]->feature_id;
- // get the node ID of the feature if one exists
- $sql = "SELECT nid FROM {chado_feture} WHERE feature_id = %d";
- $nid = db_result(db_query($sql, $feature_id));
- // Get the higest rank for this feature_id in analysisfeatureprop table
- $sql = "SELECT MAX(rank) FROM {analysisfeatureprop} AFP ".
- "INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id ".
- "WHERE feature_id=%d ".
- "AND analysis_id=%d ".
- "AND type_id=%d ";
- $previous_db = tripal_db_set_active('chado');
- $afp = db_fetch_object(db_query($sql, $feature_id, $analysis_id, $brite_data_type_id));
- tripal_db_set_active($prevous_db);
- $hi_rank = 0;
- if ($afp) {
- $hi_rank = $afp->max + 1;
- }
- //------------------------------------------------------
- // Insert into analysisfeature table
- //------------------------------------------------------
- $sql = "INSERT INTO {analysisfeature} (feature_id, analysis_id) ".
- "VALUES (%d, %d)";
- $previous_db = tripal_db_set_active('chado');
- db_query ($sql, $feature_id, $analysis_id);
- tripal_db_set_active($previous_db);
- // Get the newly inserted analysisfeature_id
- $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE feature_id = %d AND analysis_id = %d";
- $previous_db = tripal_db_set_active('chado');
- $analysisfeature_id = db_result(db_query($sql, $feature_id, $analysis_id));
- tripal_db_set_active($previous_db);
- //------------------------------------------------------
- // Insert into analysisfeatureprop table
- //------------------------------------------------------
- // Before inserting, make sure it's not a duplicatefs
- $sql = "SELECT value FROM {analysisfeatureprop} WHERE analysisfeature_id = %d AND type_id = %d";
- $previous_db = tripal_db_set_active('chado');
- $result = db_query($sql, $analysisfeature_id, $brite_data_type_id);
- tripal_db_set_active($previous_db);
- $duplicate = 0;
- while ($afp_value = db_fetch_object($result)) {
- preg_match("/<a.+?>(.+)<\/a>/",$afp_value->value,$old_ids);
- preg_match("/<a.+?>(.+)<\/a>/",$mat[2], $new_ids);
- if ($old_ids[1] && $old_ids[1] == $new_ids[1]) {
- $duplicate = 1;
- }
- }
- if (!$duplicate) {
- $sql = "INSERT INTO {analysisfeatureprop} (analysisfeature_id, type_id, value, rank)".
- "VALUES (%d, %d, '%s', %d)";
- $previous_db = tripal_db_set_active('chado');
- db_query($sql, $analysisfeature_id, $brite_data_type_id, $mat[2], $hi_rank);
- tripal_db_set_active($previous_db);
- }
- // Add link to each matched feature
- $matches[2] = preg_replace("/^(.*?)(;\s*\<a)/","<a id=\"tripal_kegg_feature_links\" target=\"_blank\" href=\"".url("/node/$nid")."\">"."$1"."</a>"."$2",$matches[2]);
- }
- $content .= "<li id=\"term_$id\"><a></a>$matches[2]\n";
- $id++;
- }
- $content .= "</ul>";
- fclose($handle);
- return $content;
- }
- /*******************************************************************************
- * HOOK: Implementation of hook_nodeapi()
- * Display library information for associated features or organisms
- * This function also provides contents for indexing
- */
- function tripal_analysis_kegg_nodeapi(&$node, $op, $teaser, $page) {
- switch ($op) {
- case 'view':
-
- // add the library to the organism/feature search indexing
- if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
- $node->content['tripal_analysis_kegg_search_index'] = array(
- '#value' => theme('tripal_analysis_kegg_search_index',$node),
- '#weight' => 6,
- );
- } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
- $node->content['tripal_analysis_kegg_search_result'] = array(
- '#value' => theme('tripal_analysis_kegg_search_result',$node),
- '#weight' => 6,
- );
- } else {
- switch($node->type){
- case 'chado_organism':
- // Show KEGG BRITE on an analysis page OR KEGG info box on a feature page
- $node->content['tripal_organism_kegg_summary'] = array(
- '#value' => theme('tripal_organism_kegg_summary', $node),
- '#weight' => 6
- );
- break;
- }
- }
- break;
- }
- }
- /************************************************************************
- * We need to let drupal know about our theme functions and their arguments.
- * We create theme functions to allow users of the module to customize the
- * look and feel of the output generated in this module
- */
- function tripal_analysis_kegg_theme () {
- return array(
- 'tripal_analysis_kegg_search_index' => array (
- 'arguments' => array('node'),
- ),
- 'tripal_analysis_kegg_search_result' => array (
- 'arguments' => array('node'),
- ),
- 'tripal_organism_kegg_summary' => array (
- 'arguments' => array('node'=> null),
- 'template' => 'tripal_organism_kegg_summary',
- ),
- 'tripal_analysis_kegg_report' => array (
- 'arguments' => array('node'=> null),
- 'template' => 'tripal_analysis_kegg_report',
- ),
- );
- }
- /**
- *
- *
- * @ingroup tripal_analysis_kegg
- */
- function tripal_analysis_kegg_preprocess(&$variables){
- // if the template file is the default node template file then we want
- // to add all of our variables.
- if($variables['template_files'][0] == 'node-chado_analysis_kegg'){
- $analysis = $variables['node']->analysis;
- $report = tripal_analysis_kegg_full_report($analysis->analysis_id);
- $analysis->tripal_analysis_kegg->kegg_report = $report;
- }
- }
- /*******************************************************************************
- *
- */
- function tripal_analysis_kegg_preprocess_tripal_organism_kegg_summary(&$variables){
- $node = $variables['node'];
- $variables['tripal_analysis_kegg']['select_form'] = tripal_analysis_kegg_load_organism_kegg_summary($node);
- }
- /************************************************************************
- */
- function theme_tripal_analysis_kegg_search_index($node){
- if ($node->type == 'chado_feature') {
- // Find cvterm_id for 'kegg_brite_data'
- $sql = "SELECT cvterm_id
- FROM {cvterm} CVT
- INNER JOIN cv ON cv.cv_id = CVT.cv_id
- WHERE cv.name = 'tripal'
- AND CVT.name = '%s'";
- $previous_db = tripal_db_set_active('chado');
- $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
-
- // Get analysis id
- $sql = "SELECT analysis_id AS aid
- FROM {analysisfeature} AF
- INNER JOIN analysisfeatureprop AFP ON AF.analysisfeature_id = AFP.analysisfeature_id
- WHERE feature_id = %d
- AND AFP.type_id = %d
- GROUP BY analysis_id";
- $feature = $node->feature;
- $feature_id = $feature->feature_id;
- $hasResult = db_result(db_query($sql, $feature_id, $brite_data_type_id));
- $result = db_query($sql, $feature->feature_id, $brite_data_type_id);
- // Show kegg result ORDER BY time
- if ($hasResult) { // If there is any result, show expandable box
- $content = "";
- while ($ana = db_fetch_object($result)) {
- // Show analysis date
- $sql = "SELECT name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
- FROM {analysis}
- WHERE analysis_id = %d";
- $ana_details = db_fetch_object(db_query($sql, $ana->aid));
- // Find node id for the analysis
- tripal_db_set_active($previous_db);
- $ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $ana->aid));
- $ana_url = url("node/".$ana_nid);
- $previous_db = tripal_db_set_active('chado');
-
- // Show content
- $content .= "$ana_details->name";
-
- // Show Kegg results
- $sql = "SELECT AFP.value AS afpvalue
- FROM {analysisfeatureprop} AFP
- INNER JOIN analysisfeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
- WHERE AF.analysis_id = %d
- AND AF.feature_id = %d
- ";
- $kegg_results = db_query($sql, $ana->aid, $feature_id);
- while ($afp = db_fetch_object($kegg_results)) {
- $content .= " $afp->afpvalue";
- }
- }
- }
- tripal_db_set_active($previous_db);
- return $content;
- }
- }
- /************************************************************************
- */
- function theme_tripal_analysis_kegg_search_result($node){
- $content = theme_tripal_analysis_kegg_node_add($node);
- return $content;
- }
- /************************************************************************
- */
- function tripal_analysis_kegg_load_organism_kegg_summary($node) {
- $organism = $node->organism;
- // find analyses that have KEGG terms
- $sql = "
- SELECT *
- FROM {kegg_by_organism} KBO
- WHERE organism_id = %d
- ORDER BY analysis_id DESC
- ";
- $previous_db = tripal_db_set_active('chado');
- $results = db_fetch_object(db_query($sql,$organism->organism_id));
- tripal_db_set_active($previous_db);
- $has_results = 0;
- if($results){
- $has_results = 1;
- }
- return array (
- 'has_results' => $has_results,
- 'form' => drupal_get_form('tripal_analysis_kegg_select_form',$node),
- );
- }
- /************************************************************************
-
- // Show Kegg additional information on a KEGG Analysis page
- if ($node->type == 'chado_analysis_kegg') {
- return tripal_analysis_kegg_full_report($node->analysis_id);
- }
-
- // Show Kegg-info-box on a Feature page
- else if ($node->type == 'chado_feature') {
- return tripal_analysis_kegg_feature_add($node);
- }
-
- return $content;
- }
- */
- function tripal_analysis_kegg_org_report($analysis_id){
- $content = tripal_analysis_kegg_full_report($analysis_id);
- $opt = array($content);
- return drupal_json($opt);
- }
- /************************************************************************
- */
- function tripal_analysis_kegg_full_report($analysis_id){
- // Test if brite data have been parsed into the database
- $sql = "SELECT CVT.name, CVT.cvterm_id
- FROM {cvterm} CVT
- INNER JOIN analysisprop AP ON CVT.cvterm_id = AP.type_id
- WHERE AP.analysis_id = %d
- AND CVT.definition LIKE 'KEGG BRITE term: %'
- ORDER BY CVT.cvterm_id";
- $previous_db = tripal_db_set_active('chado');
- $result = db_query($sql, $analysis_id);
- tripal_db_set_active($previous_db);
- if (db_result($result)) {
- $content = tripal_analysis_kegg_brite($analysis_id, $type_id, 0);
- } else {
- $content = "<i>Note:</i> Analysis result is not available. Please schedule and run the job to parse the kegg output.";
- }
- return $content;
- }
- /*******************************************************************************
- * Tripal Kegg administrative setting form. This function is called by
- * tripal_analysis module which asks for an admin form to show on the page
- */
- function tripal_analysis_kegg_get_settings() {
- // Get an array of node types with internal names as keys
- $options = node_get_types('names');
- // Add 'chado_feature' to allowed content types for showing kegg results
- $allowedoptions ['chado_feature'] = "Show KEGG results on feature pages";
- $allowedoptions ['chado_analysis_kegg'] = "Show KEGG BRITE results on the analysis page.";
- $allowedoptions ['chado_organism'] = "Show KEGG BRITE results on the organism pages.";
- $form['description'] = array(
- '#type' => 'item',
- '#value' => t("Some chado features were analyzed by KEGG automatic annotation server (KAAS). This option allows user to display the kegg analysis results. Please read user manual for storage and display of kegg files. Check the box to enable the analysis results. Uncheck to disable it."),
- '#weight' => 0,
- );
- $form['tripal_analysis_kegg_setting'] = array(
- '#type' => 'checkboxes',
- '#options' => $allowedoptions,
- '#default_value' => variable_get('tripal_analysis_kegg_setting',
- array('chado_feature', 'chado_analysis_kegg')),
- );
- $settings->form = $form;
- $settings->title = "Tripal Kegg";
- return $settings;
- }
- /************************************************************************
- */
- function tripal_analysis_kegg_organism_results($node) {
- $node = node_load($node);
- return tripal_analysis_kegg_organism_add($node);
- }
- /************************************************************************
- */
- function tripal_analysis_kegg_feature_add($node) {
- // Find cvterm_id for 'kegg_brite_data'
- $sql = "SELECT cvterm_id
- FROM {cvterm} CVT
- INNER JOIN cv ON cv.cv_id = CVT.cv_id
- WHERE cv.name = 'tripal'
- AND CVT.name = '%s'";
- $previous_db = tripal_db_set_active('chado');
- $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
-
- // Get analysis id
- $sql = "SELECT analysis_id AS aid
- FROM {analysisfeature} AF
- INNER JOIN analysisfeatureprop AFP ON AF.analysisfeature_id = AFP.analysisfeature_id
- WHERE feature_id = %d
- AND AFP.type_id = %d
- GROUP BY analysis_id";
- $feature = $node->feature;
- $feature_id = $feature->feature_id;
- $hasResult = db_result(db_query($sql, $feature_id, $brite_data_type_id));
- $result = db_query($sql, $feature->feature_id, $brite_data_type_id);
- // Show kegg result ORDER BY time
- if ($hasResult) { // If there is any result, show expandable box
- $content .= "<div id=\"tripal_kegg-hits\" class=\"tripal_kegg-info-box\">
- <table class=\"tripal_kegg_results_table\">
- <tr><td>";
- while ($ana = db_fetch_object($result)) {
- // Show analysis date
- $sql = "SELECT name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
- FROM {analysis}
- WHERE analysis_id = %d";
- $ana_details = db_fetch_object(db_query($sql, $ana->aid));
- // Find node id for the analysis
- tripal_db_set_active($previous_db);
- $ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $ana->aid));
- $ana_url = url("node/".$ana_nid);
- $previous_db = tripal_db_set_active('chado');
-
- // Show content
- $content .= "<strong>Analysis Date:</strong> $ana_details->time
- (<a href=$ana_url>$ana_details->name</a>)<br>";
-
- // Show Kegg results
- $sql = "SELECT AFP.value AS afpvalue
- FROM {analysisfeatureprop} AFP
- INNER JOIN analysisfeature AF on AF.analysisfeature_id = AFP.analysisfeature_id
- WHERE AF.analysis_id = %d
- AND AF.feature_id = %d
- ";
- $kegg_results = db_query($sql, $ana->aid, $feature_id);
- while ($afp = db_fetch_object($kegg_results)) {
- $content .= "$afp->afpvalue<br>";
- }
- $content .= "<br>";
- }
- $content .= '</td></tr></table></div>';
- }
- tripal_db_set_active($previous_db);
- return $content;
- }
- /************************************************************************
- */
- function tripal_analysis_kegg_select_form(&$form_state = NULL,$node){
- $form = array();
- // find analyses that have KEGG terms
- $sql = "
- SELECT *
- FROM {kegg_by_organism} KBO
- WHERE organism_id = %d
- ORDER BY analysis_id DESC
- ";
- $previous_db = tripal_db_set_active('chado');
- $results = db_query($sql,$node->organism->organism_id);
- tripal_db_set_active($previous_db);
- $analyses = array();
- $analyses[''] = '';
- while($analysis = db_fetch_object($results)){
- $analyses[$analysis->analysis_id] = "$analysis->analysis_name";
- }
-
- # create the select box
- $form['tripal_analysis_kegg_select'] = array(
- '#title' => t('Select a KEGG report to view'),
- '#description' => t('Any analysis with KEGG results related to this organism are available for viewing. For further information, see the analysis information page.'),
- '#type' => 'select',
- '#options' => $analyses,
- '#attributes' => array (
- 'onchange' => 'tripal_analysis_kegg_org_report(this.options[this.selectedIndex].value)'
- ),
- );
- return $form;
- }
|