12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184 |
- <?php
- function tripal_analysis_kegg_init(){
-
- 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');
-
- 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');
- }
- 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;
- }
- 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;
- }
- 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',
- );
- }
- 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 (!$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\">
- ";
-
- $sql = "SELECT DISTINCT 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>";
- }
-
- $content .="</td>
- <td nowrap id=\"tripal_kegg_brite_hierarchy\" valign=\"top\">";
- $content .= "<i>Note:</i> Click a BRITE term for its functional hierarchy";
-
-
- } else {
-
- $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));
-
- $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>";
- }
-
-
-
-
- if($ajax){
- drupal_json(array('update' => $content,
- 'id' => "tripal_kegg_brite_tree_$type_id",
- 'brite_term' => "Hierarchy: $brite_term"));
- } else {
- return $content;
- }
- }
- function chado_analysis_kegg_form ($node){
-
- $form = chado_analysis_form($node);
-
- $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.'),
- );
- $form['kegg']['keggkeywordjob'] = array(
- '#type' => 'checkbox',
- '#title' => t('Submit a job to extract keywords from the KEGG html output'),
- '#description' => t('Note: KEGG results are only searchable after keywords are extracted. Do not run this twice if you have already done so.'),
- );
- return $form;
- }
- function chado_analysis_kegg_insert($node){
-
- chado_analysis_insert($node);
-
- tripal_analysis_insert_property($node->analysis_id,'analysis_type','tripal_analysis_kegg');
-
- 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);
-
-
- 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.");
- }
- }
-
- if ($node->keggkeywordjob) {
- $analysis_id =chado_get_id_for_node('analysis', $node);
- $job_args[0] = $analysis_id;
- tripal_add_job("Extract keywords for search: $node->analysisname",'tripal_analysis_kegg',
- 'tripal_analysis_kegg_extract_keywords', $job_args, $user->uid);
- }
- }
- function chado_analysis_kegg_delete($node){
- chado_analysis_delete($node);
- }
- function chado_analysis_kegg_update($node){
-
- chado_analysis_update($node);
-
- tripal_analysis_update_property($node->analysis_id,'analysis_type','tripal_analysis_kegg',1);
-
- 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);
-
-
- chado_analysis_kegg_submit_job($node);
- }
- function chado_analysis_kegg_load($node){
-
- $additions = chado_analysis_load($node);
-
- $analysis = $additions->analysis;
- $analysis_id = $analysis->analysis_id;
-
- $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) {
-
- if (!$teaser) {
- $node = node_prepare($node, $teaser);
-
-
- $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 (is_file($hierfile)) {
-
- $data_dir = sys_get_temp_dir() . "/" . uniqid();
- mkdir($data_dir);
- $stderr = shell_exec("cd $data_dir; tar -zxf $hierfile;");
- print "$stderr\n";
- $hierdir = $data_dir . "/hier";
- }
-
- 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;
-
-
- $select = array('analysis_id' => $analysis_id);
- if(!tripal_core_chado_delete('analysisfeature',$select)){
- print "ERROR: Cannot prepare the analysis for adding features\n";
- exit;
- }
- while ($file = readdir($dir_handle)) {
- if(preg_match("/^.*\.keg/",$file)){
-
- if ($no_file % $interval == 0) {
- $percentage = (int) ($no_file / $total_files * 100);
- tripal_job_set_progress($job_id, $percentage);
- print $percentage."% ";
- }
- $no_file ++;
-
-
- $results = tripal_analysis_kegg_parse_kegg_file("$hierdir/$file",$heirarchy,
- $analysis_id, $base_path, $query_re,$query_type,$query_uniquename);
-
- if(count($results) > 0){
-
-
-
-
- $sql = "DELETE
- 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');
- db_result(db_query($sql, $analysis_id, $heirarchy));
- tripal_db_set_active($previous_db);
-
-
- $previous_db = tripal_db_set_active('chado');
-
- $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, $heirarchy));
-
-
- $i = 0;
- $content = '<ul>';
- tripal_analysis_kegg_array2list($results,$content,$i);
- $content .= '</ul>';
- $content = preg_replace("/\'/", "'", $content);
-
-
- $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);
-
- }
- }
- }
- print "Done.\n";
- tripal_job_set_progress($job_id,100);
- closedir($dir_handle);
-
- if (is_file($hierfile)) {
- $stderr = shell_exec("rm -r $data_dir;");
- print "$stderr\n";
- }
- return;
- }
- function tripal_analysis_kegg_array2list($array,&$content,&$i){
- foreach($array as $index => $item){
- if(is_array($item)){
- if(is_numeric($index)){
- tripal_analysis_kegg_array2list($item,$content,$i);
- } else {
- $content .= "<li id=\"term_$i\"><a></a>$index\n<ul>";
- $i++;
- tripal_analysis_kegg_array2list($item,$content,$i);
- $content .= "</ul>\n</li>\n";
- }
- } else {
- $content .= "<li id=\"term_$i\"><a></a>$item</li>\n";
- $i++;
- }
- }
- }
- function tripal_analysis_kegg_parse_kegg_file ($file, &$heirarchy, $analysis_id,
- $base_path, $query_re, $query_type, $query_uniquename)
- {
- print "Parsing $file\n";
-
- $select = array('name' => 'kegg_brite_data',
- 'cv_id' => array('name' => 'tripal'));
- $bdt_arr = tripal_core_chado_select('cvterm',array('cvterm_id'),$select);
- $brite_id = $bdt_arr[0]->cvterm_id;
- $filepos = 0;
-
- $handle = fopen($file,'r');
- while($line = fgets($handle)){
- $filepos += strlen($line);
- $line = trim($line);
-
- if(preg_match("/#.*nbsp;\s(.*)<\/h2>$/",$line,$matches)){
-
-
- $heirarchy = $matches[1];
- $select = array('name' => $heirarchy,'cv_id' => array('name' => 'tripal'));
- $cvt_arr = tripal_core_chado_select('cvterm',array('cvterm_id'),$select);
- if (count($cvt_arr) == 0) {
- tripal_add_cvterms($type, "KEGG BRITE term: $type");
- $cvt_arr = tripal_core_chado_select('cvterm',array('cvterm_id'),$select);
- }
- $heirarchy_id = $cvt_arr[0]->cvterm_id;
-
- $next = tripal_analysis_kegg_get_next_line($handle,$filepos);
- $results = tripal_analysis_kegg_recurse_heir($handle,$next,$query_re,
- $query_type,$query_uniquename,$base_path,$analysis_id,
- $brite_id,$heirarchy,$filepos);
- }
- }
- fclose($handle);
- return $results;
- }
- function tripal_analysis_kegg_recurse_heir($handle, $line,$query_re,
- $query_type, $query_uniquename, $base_path, $analysis_id, $brite_id,
- $heirarchy,&$filepos)
- {
- $results = array();
-
- $level = $line[0];
- $value = $line[1];
-
-
- $prevpos = $filepos;
- while($next = tripal_analysis_kegg_get_next_line($handle,$filepos)){
- $next_level = $next[0];
- $next_value = $next[1];
-
- $ret = tripal_analysis_kegg_check_line_handle_feature($query_re,
- $query_type, $query_uniquename, $base_path, $analysis_id, $brite_id,
- $heirarchy,$value);
- if($ret){
- $results[] = $ret;
- }
-
- if(ord($next_level) > ord($level)){
-
- $ret = tripal_analysis_kegg_recurse_heir($handle,$next,
- $query_re,$query_type, $query_uniquename, $base_path, $analysis_id,
- $brite_id,$heirarchy,$filepos);
- if(count($ret) > 0){
- $results[][$value] = $ret;
- }
- }
-
-
- elseif(ord($next_level) < ord($level)){
- fseek($handle,$prevpos);
- $filepos = $prevpos;
- return $results;
- }
- else {
- $line = $next;
- $level = $next[0];
- $value = $next[1];
- }
- $prevpos = $filepos;
- }
- return $results;
- }
- function tripal_analysis_kegg_get_next_line($handle,&$filepos){
- $good = 0;
-
- $line = fgets($handle);
- $filepos += strlen($line);
-
- if(!$line){
- return null;
- }
- while(!$good){
- $line = trim($line);
- preg_match("/^([ABCDEFGHIJKLMNOP])\s*(.*)/",$line,$matches);
- $level = $matches[1];
- $value = $matches[2];
-
- if($level and $value) {
-
-
- $value = preg_replace("/<a href=\"\//i","<a href=\"http://www.genome.jp/",$value);
- $value = preg_replace("/<a href=\"/i","<a id=\"tripal_kegg_brite_links\" target=\"_blank\" href=\"",$value);
-
- $good = 1;
- } else {
- $line = fgets($handle);
- $filepos += strlen($line);
-
- if(!$line){
- return null;
- }
- }
- }
- return array($level,$value);
- }
- function tripal_analysis_kegg_check_line_handle_feature($query_re,
- $query_type, $query_uniquename, $base_path, $analysis_id, $brite_id,
- $heirarchy,$value)
- {
-
- if(preg_match("/^(.*?);\s*(\<a.+)/",$value,$matches)){
- $has_feature = 1;
- $fname = $matches[1];
- $keggterm = $matches[2];
-
- if ($query_re and preg_match("/$query_re/", $fname, $matches)) {
- $feature = $matches[1];
- }
-
- else {
- if (preg_match('/^(.*?)\s.*$/', $fname, $matches)) {
- $feature = $matches[1];
- }
-
- else {
- $feature = $fname;
- }
- }
-
- $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;
- }
- $feature_id = $feature_arr[0]->feature_id;
- if($feature_id){
- print "Adding KEGG term for $feature ($feature_id,$analysis_id). $heirarchy\n";
-
- tripal_analysis_kegg_insert_featureprop($feature_id,$analysis_id,
- $brite_id,$keggterm);
-
- $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
- $nid = db_result(db_query($sql, $feature_id));
-
- if($nid){
- $value = preg_replace("/^(.*?)(;\s*\<a)/","<a id=\"tripal_kegg_feature_links\" target=\"_blank\" href=\"".url("node/$nid")."\">"."$1"."</a>"."$2",$value);
- }
-
- return $value;
- }
- }
- return null;
- }
- function tripal_analysis_kegg_insert_featureprop ($feature_id, $analysis_id,
- $brite_id,$keggterm)
- {
-
- $values = array('feature_id' => $feature_id,'analysis_id' => $analysis_id);
- $analysisfeature_arr = tripal_core_chado_select('analysisfeature',
- array('analysisfeature_id'),$values);
- if(count($analysisfeature_arr) == 0){
- tripal_core_chado_insert('analysisfeature',$values);
- $analysisfeature_arr = tripal_core_chado_select('analysisfeature',
- array('analysisfeature_id'),$values);
- }
- $analysisfeature_id = $analysisfeature_arr[0]->analysisfeature_id;
-
-
- if($analysisfeature_id){
-
- $sql = "SELECT rank FROM analysisfeatureprop WHERE analysisfeature_id = %d and type_id = %d ORDER BY rank DESC";
- $previous_db = tripal_db_set_active('chado');
- $result = db_fetch_object(db_query($sql,$analysisfeature_id,$brite_id));
- tripal_db_set_active($previous);
- $rank = 0;
- if ($result and $result->rank > 0) {
- $rank = $result->rank + 1;
- }
- $values = array(
- 'analysisfeature_id' => $analysisfeature_id,
- 'type_id' => $brite_id,
- 'value' => $keggterm,
- 'rank' => $rank,
- );
- return tripal_core_chado_insert('analysisfeatureprop',$values);
- }
- else {
- return 0;
- }
- }
- function tripal_analysis_kegg_nodeapi(&$node, $op, $teaser, $page) {
- switch ($op) {
- case 'view':
-
-
- 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':
-
- $node->content['tripal_organism_kegg_summary'] = array(
- '#value' => theme('tripal_organism_kegg_summary', $node),
- );
- break;
- case 'chado_feature':
-
- $node->content['tripal_feature_kegg_terms'] = array(
- '#value' => theme('tripal_feature_kegg_terms', $node),
- );
- }
- }
- break;
- }
- }
- 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_feature_kegg_terms' => array (
- 'arguments' => array('node'=> null),
- 'template' => 'tripal_feature_kegg_terms',
- ),
- 'tripal_analysis_kegg_report' => array (
- 'arguments' => array('node'=> null),
- 'template' => 'tripal_analysis_kegg_report',
- ),
- );
- }
- function tripal_analysis_kegg_preprocess(&$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'];
- $organism = $node->organism;
- $organism->tripal_analysis_kegg->select_form = tripal_analysis_kegg_load_organism_kegg_summary($node);
- }
- function tripal_analysis_kegg_preprocess_tripal_feature_kegg_terms(&$variables){
- $node = $variables['node'];
- $feature = $node->feature;
- $feature->tripal_analysis_kegg->results = tripal_analysis_kegg_load_feature_terms($feature);
- }
- function theme_tripal_analysis_kegg_search_index($node){
- if ($node->type == 'chado_feature') {
-
- $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_id = db_result(db_query($sql, 'kegg_brite_data'));
-
-
- $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_id));
- $result = db_query($sql, $feature->feature_id, $brite_id);
-
- if ($hasResult) {
- $content = "";
- while ($ana = db_fetch_object($result)) {
-
- $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));
-
- 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');
-
-
- $content .= "$ana_details->name";
-
-
- $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;
-
- $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),
- );
- }
- 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){
-
- $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;
- }
- function tripal_analysis_kegg_get_settings() {
-
- $options = node_get_types('names');
-
- $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_load_feature_terms($feature) {
-
- $feature_id = $feature->feature_id;
-
- $select = array(
- 'analysisfeature_id' => array(
- 'feature_id' => $feature_id,
- ),
- 'type_id' => array(
- 'name' => 'kegg_brite_data',
- 'cv_id' => array(
- 'name' => 'tripal'
- ),
- ),
- );
- $afeatureprops = tripal_core_chado_select('analysisfeatureprop',array('*'),$select);
-
- $results = array();
- foreach ($afeatureprops as $index => $afeatureprop) {
-
- $analysisfeature_arr = tripal_core_chado_select('analysisfeature',array('analysis_id'),
- array('analysisfeature_id' => $afeatureprop->analysisfeature_id));
- $analysisfeature = $analysisfeature_arr[0];
-
- $analysis = tripal_core_generate_chado_var('analysis',
- array('analysis_id' => $analysisfeature->analysis_id));
- $analysis_id = $analysis->analysis_id;
-
- $results[$analysis_id]['analysis'] = $analysis;
- }
-
- foreach($results as $analysis_id => $arr){
- $select = array(
- 'analysisfeature_id' => array(
- 'analysis_id' => $analysis_id,
- 'feature_id' => $feature_id,
- ),
- 'type_id' => array(
- 'name' => 'kegg_brite_data',
- 'cv_id' => array(
- 'name' => 'tripal',
- ),
- ),
- );
- $terms = tripal_core_chado_select('analysisfeatureprop',array('*'),$select);
- foreach ($terms as $term){
- $results[$analysis_id]['terms'][] = $term->value;
- }
- }
- return $results;
- }
- function tripal_analysis_kegg_select_form(&$form_state = NULL,$node){
- $form = array();
-
- $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";
- }
-
-
- $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;
- }
- function tripal_analysis_kegg_extract_keywords ($analysis_id) {
-
- print "Extracting keywords...\n";
-
- $output_type_id = tripal_get_cvterm_id('kegg_brite_data');
- $sql = "SELECT AFP.analysisfeature_id, AFP.value FROM {analysisfeatureprop} AFP
- INNER JOIN {analysisfeature} AF ON AF.analysisfeature_id = AFP.analysisfeature_id
- WHERE type_id = $output_type_id
- AND AF.analysis_id = $analysis_id";
- $results = chado_query($sql);
- $keyword_type_id = tripal_get_cvterm_id('analysis_kegg_output_keywords');
-
- $search = array (
- "'<[/!]*?[^<>]*?>'si",
- "'\n'",
- );
- $replace = array (
- " ",
- " ",
- );
- while ($record = db_fetch_object($results)) {
- $af_id = $record->analysisfeature_id;
- $value = $record->value;
-
- $sql = "SELECT value FROM {analysisfeatureprop} WHERE analysisfeature_id =$af_id AND type_id = $keyword_type_id";
- $keywords = db_result(chado_query($sql));
-
-
- $text = preg_replace($search, $replace, $value);
- $new_keywords = trim(ereg_replace(' +', ' ', $text));
-
-
- if ($keywords) {
- $new_keywords = "$keywords $new_keywords";
- $sql = "UPDATE {analysisfeatureprop} SET value = '$new_keywords' WHERE analysisfeature_id =$af_id AND type_id = $keyword_type_id ";
- } else {
-
- $sql = "INSERT INTO {analysisfeatureprop} (analysisfeature_id, type_id, value, rank) VALUES ($af_id, $keyword_type_id, '$new_keywords', 0)";
- }
- chado_query($sql);
- }
-
- print "Finished.\n";
-
- }
|