12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211 |
- <?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['node/%/kegg'] = array(
- 'title' => t('KEGG'),
- 'page callback' => 'tripal_analysis_kegg_organism_results',
- 'page arguments' => array(1),
- 'access callback' => 'tripal_analysis_kegg_node_has_menu',
- 'access arguments' => array('access chado_analysis_kegg content',1),
- 'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM
- );
- $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_node_has_menu($type,$vid){
-
- $sql = 'SELECT organism_id FROM {chado_organism} WHERE vid = %d';
- $result = db_query($sql, $vid);
-
- $box_status =variable_get("tripal_analysis_kegg-box-results","menu_off");
-
-
- if($org_id = db_fetch_object($result)){
- if(strcmp($box_status,"menu_on")==0){
- return user_access($type);
- }
- } else {
- return FALSE;
- }
- }
- function tripal_analysis_kegg_brite($analysis_id, $type_id, $ajax){
-
- if (!$ajax) {
-
- $url_ajax = url("sites/all/themes/theme_tripal/images/ajax-loader.gif");
- $content .= "<div id=\"tripal_ajaxLoading\" style=\"display:none\">".
- "<div id=\"loadingText\">Loading...</div>".
- "<img src=\"$url_ajax\"></div>";
-
- $content .= "<div id=\"tripal_kegg_brite_results\" class=\"tripal_kegg_brite-info-box\">
- <div class=\"tripal_expandableBox\">
- <h3>Analysis Results</h3>
- </div>
- <div class=\"tripal_expandableBoxContent\">
- <table>
- <tr>
- <th>KEGG BRITE</th>
- <th id=\"tripal_kegg_brite_header\">Hierarchy:</th>
- </tr>
- <tr>
- <td nowrap valign=\"top\">";
-
- $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 = db_set_active('chado');
- $result = db_query($sql, $analysis_id);
- 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 = 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));
- 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>
- </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){
- $type = node_get_types('type', $node);
- $form = array();
- $form['title']= array(
- '#type' => 'hidden',
- '#default_value' => $node->title,
- );
- $form['analysisname']= array(
- '#type' => 'textfield',
- '#title' => t('Analysis Name'),
- '#required' => FALSE,
- '#default_value' => $node->analysisname,
- '#weight' => 1
- );
- $form['program']= array(
- '#type' => 'textfield',
- '#title' => t('Program'),
- '#required' => TRUE,
- '#default_value' => $node->program,
- '#weight' => 2
- );
- $form['programversion']= array(
- '#type' => 'textfield',
- '#title' => t('Program Version'),
- '#required' => TRUE,
- '#default_value' => $node->programversion,
- '#weight' => 3
- );
- $form['algorithm']= array(
- '#type' => 'textfield',
- '#title' => t('Algorithm'),
- '#required' => FALSE,
- '#default_value' => $node->algorithm,
- '#weight' => 4
- );
- $form['sourcename']= array(
- '#type' => 'textfield',
- '#title' => t('Source Name'),
- '#required' => FALSE,
- '#default_value' => $node->sourcename,
- '#weight' => 5
- );
- $form['sourceversion']= array(
- '#type' => 'textfield',
- '#title' => t('Source Version'),
- '#required' => FALSE,
- '#default_value' => $node->sourceversion,
- '#weight' => 6
- );
- $form['sourceuri']= array(
- '#type' => 'textfield',
- '#title' => t('Source URI'),
- '#required' => FALSE,
- '#default_value' => $node->sourceuri,
- '#weight' => 7
- );
-
- $default_time = $node->timeexecuted;
- $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
- $month = preg_replace("/^\d+-0?(\d+)-\d+ .*/", "$1", $default_time);
- $day = preg_replace("/^\d+-\d+-0?(\d+) .*/", "$1", $default_time);
-
- if (!$default_time) {
- $default_time = time();
- $year = format_date($default_time, 'custom', 'Y');
- $month = format_date($default_time, 'custom', 'n');
- $day = format_date($default_time, 'custom', 'j');
- }
- $form['timeexecuted']= array(
- '#type' => 'date',
- '#title' => t('Time Executed'),
- '#required' => TRUE,
- '#default_value' => array(
- 'year' => $year,
- 'month' => $month,
- 'day' => $day,
- ),
- '#weight' => 8
- );
- $form['description']= array(
- '#type' => 'textarea',
- '#rows' => 15,
- '#title' => t('Description and/or Program Settings'),
- '#required' => FALSE,
- '#default_value' => check_plain($node->description),
- '#weight' => 9
- );
-
- $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' => $node->hierfile,
- );
- $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.'),
- '#default_value' => $node->keggjob,
- );
- return $form;
- }
- function chado_analysis_kegg_insert($node){
- global $user;
-
- $time = $node->timeexecuted;
- $month = $time['month'];
- $day = $time['day'];
- $year = $time['year'];
- $timestamp = $month.'/'.$day.'/'.$year;
-
- $analysis_id = $node->analysis_id;
- if ($analysis_id) {
- $sql = "SELECT analysis_id ".
- "FROM {Analysis} ".
- "WHERE analysis_id = %d ";
- $previous_db = db_set_active('chado');
- $analysis = db_fetch_object(db_query($sql, $node->analysis_id));
- db_set_active($previous_db);
- }
-
- if(!$analysis){
-
- $sql = "INSERT INTO {analysis} ".
- " (name, description, program, programversion, algorithm, ".
- " sourcename, sourceversion, sourceuri, timeexecuted) ".
- "VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s')";
- $previous_db = db_set_active('chado');
- db_query($sql,$node->analysisname, $node->description,
- $node->program,$node->programversion,$node->algorithm,
- $node->sourcename, $node->sourceversion, $node->sourceuri,
- $timestamp);
-
- $sql = "SELECT analysis_id ".
- "FROM {Analysis} ".
- "WHERE program='%s'".
- "AND programversion='%s'".
- "AND sourcename='%s'";
- $analysis_id = db_result(db_query($sql, $node->program,
- $node->programversion, $node->sourcename));
-
- $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT ".
- "INNER JOIN cv ON cv.cv_id = CVT.cv_id ".
- "WHERE CVT.name = 'analysis_kegg_settings' ".
- "AND CV.name = 'tripal'";
- $type_id = db_result(db_query($sql));
-
-
- $sql = "
- INSERT INTO {analysisprop} (analysis_id, type_id, value)
- VALUES (%d, %d, '%s')
- ";
- $keggsettings = $node->hierfile;
- db_query($sql, $analysis_id, $type_id, $keggsettings);
- db_set_active($previous_db);
-
- if($node->keggjob) {
- $job_args[0] = $analysis_id;
- $job_args[1] = $node->hierfile;
- 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.");
- }
- }
- }
-
-
- $node_check_sql = "SELECT * FROM {chado_analysis} ".
- "WHERE analysis_id = %d";
- $node_check = db_fetch_object(db_query($node_check_sql, $analysis_id));
- if(!$node_check){
-
- $sql = "INSERT INTO {chado_analysis} (nid, vid, analysis_id) ".
- "VALUES (%d, %d, %d)";
- db_query($sql,$node->nid,$node->vid,$analysis_id);
-
-
- $record = new stdClass();
-
-
- if ($node->analysisname) {
- $record->title = $node->analysisname;
- } else {
-
- $record->title = "$node->program ($node->programversion)";
- }
- $record->nid = $node->nid;
- drupal_write_record('node',$record,'nid');
- drupal_write_record('node_revisions',$record,'nid');
- }
- }
- function chado_analysis_kegg_delete($node){
-
-
- $sql_drupal = "SELECT analysis_id ".
- "FROM {chado_analysis} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- $analysis_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
-
- $sql_del = "DELETE FROM {chado_analysis} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- db_query($sql_del, $node->nid, $node->vid);
- $sql_del = "DELETE FROM {node} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- db_query($sql_del, $node->nid, $node->vid);
- $sql_del = "DELETE FROM {node_revisions} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- db_query($sql_del, $node->nid, $node->vid);
-
- $previous_db = db_set_active('chado');
- $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE analysis_id=%d";
- $results = db_query($sql, $analysis_id);
- while ($af = db_fetch_object($results)) {
- db_query("DELETE FROM {analysisfeatureprop} WHERE analysisfeature_id = %d", $af->analysisfeature_id);
- }
- db_query("DELETE FROM {analysisfeature} WHERE analysis_id = %d", $analysis_id);
- db_query("DELETE FROM {analysisprop} WHERE analysis_id = %d", $analysis_id);
- db_query("DELETE FROM {analysis} WHERE analysis_id = %d", $analysis_id);
- db_set_active($previous_db);
- }
- function chado_analysis_kegg_update($node){
- global $user;
- if($node->revision){
-
- } else {
-
- $time = $node->timeexecuted;
- $month = $time['month'];
- $day = $time['day'];
- $year = $time['year'];
- $timestamp = $month.'/'.$day.'/'.$year;
-
- $sql = "SELECT analysis_id ".
- "FROM {chado_analysis} ".
- "WHERE vid = %d";
- $analysis_id = db_fetch_object(db_query($sql, $node->vid))->analysis_id;
- $sql = "UPDATE {analysis} ".
- "SET name = '%s', ".
- " description = '%s', ".
- " program = '%s', ".
- " programversion = '%s', ".
- " algorithm = '%s', ".
- " sourcename = '%s', ".
- " sourceversion = '%s', ".
- " sourceuri = '%s', ".
- " timeexecuted = '%s' ".
- "WHERE analysis_id = %d ";
- $previous_db = db_set_active('chado');
- db_query($sql, $node->analysisname, $node->description, $node->program,
- $node->programversion,$node->algorithm,$node->sourcename,
- $node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
-
- $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT ".
- "INNER JOIN cv CV ON CV.cv_id = CVT.cv_id ".
- "WHERE CVT.name = 'analysis_kegg_settings' ".
- "AND CV.name = 'tripal'";
- $type_id = db_result(db_query($sql));
-
- $sql = "UPDATE {analysisprop} ".
- "SET value = '%s' ".
- "WHERE analysis_id = %d AND type_id = %d";
- $keggsettings = $node->hierfile;
- db_query($sql, $keggsettings, $analysis_id, $type_id);
- db_set_active($previous_db);
-
- if($node->keggjob) {
- $job_args[0] = $analysis_id;
- $job_args[1] = $node->hierfile;
- 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.");
- }
- }
-
-
- $record = new stdClass();
-
-
- if ($node->analysisname) {
- $record->title = $node->analysisname;
- } else {
-
- $record->title = "$node->program ($node->programversion)";
- }
- $record->nid = $node->nid;
- drupal_write_record('node',$record,'nid');
- drupal_write_record('node_revisions',$record,'nid');
- }
- }
- function chado_analysis_kegg_load($node){
-
- $sql = "SELECT analysis_id FROM {chado_analysis} WHERE vid = %d";
- $ana_node = db_fetch_object(db_query($sql, $node->vid));
- $additions = new stdClass();
- if ($ana_node) {
-
- $sql = "SELECT Analysis_id, name AS analysisname, description, program, ".
- " programversion, algorithm, sourcename, sourceversion, ".
- " sourceuri, timeexecuted ".
- "FROM {Analysis} ".
- "WHERE Analysis_id = $ana_node->analysis_id";
- $previous_db = db_set_active('chado');
- $additions = db_fetch_object(db_query($sql));
-
- $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT ".
- "INNER JOIN cv ON cv.cv_id = CVT.cv_id ".
- "WHERE CVT.name = 'analysis_kegg_settings' ".
- "AND CV.name = 'tripal'";
- $type_id = db_result(db_query($sql));
-
- $sql = "SELECT value FROM {analysisprop} ".
- "WHERE analysis_id = %d ".
- "AND type_id = %d";
- $analysisprop = db_result(db_query($sql, $ana_node->analysis_id, $type_id));
-
- $additions->hierfile = $analysisprop;
- db_set_active($previous_db);
- }
-
-
- if ($additions->analysisname) {
- $additions->title = $additions->analysisname;
- } else {
-
- $additions->title = "$additions->program ($additions->programversion)";
- }
- 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, $job_id) {
-
-
- 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";
-
- } else {
- $hierdir = $hierfile;
- }
- $dir_handle = @opendir($hierdir) or die("Unable to open $hierdir");
- $total_files = count(glob($hierdir . '/*.*'));
- $interval = intval($total_files * 0.01);
- $no_file = 0;
- 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 ++;
-
-
- $content = tripal_analysis_kegg_parse_keg_file("$hierdir/$file",$type,$analysis_id);
-
- if($content){
-
-
-
-
- $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 = db_set_active('chado');
- $oldvalue = db_result(db_query($sql, $analysis_id, $type));
- db_set_active($previous_db);
- if ($oldvalue != $content) {
- $sql = "
- INSERT INTO {analysisprop} (analysis_id, type_id, value) VALUES
- (%d,
- (SELECT cvterm_id
- FROM {cvterm} CVT
- INNER JOIN CV ON CVT.cv_id = CV.cv_id
- WHERE CV.name = 'tripal' AND CVT.name = '%s'),
- '%s')";
- $previous_db = db_set_active('chado');
- db_query($sql,$analysis_id,$type,$content);
- db_set_active($previous_db);
- }
-
- }
- }
- }
- print "Done.\n";
- closedir($dir_handle);
-
- if (is_file($hierfile)) {
- $stderr = shell_exec("rm -r $hierdir;");
- print "$stderr\n";
- }
- return;
- }
- function tripal_analysis_kegg_parse_keg_file ($file,&$type,$analysis_id){
- $handle = fopen($file,'r');
- $depth = array();
- $current = '@';
- $prev = '@';
- $id = 0;
- $type_id = 0;
- $no_line = 0;
- $prefix = variable_get('chado_feature_accession_prefix','ID');
- while($line = fgets($handle)){
- $no_line ++;
-
- if(preg_match("/#DEFINITION\s+(.*)\s+.+?$/",$line,$matches)){
-
- $type = $matches[1];
-
- $previous_db = 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));
- db_set_active($previous_db);
- if (!$type_id) {
- tripal_add_cvterms($type, "KEGG BRITE term: $type");
-
- $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 = db_set_active('chado');
- $type_id = db_result(db_query($sql, $type));
- db_set_active($previous_db);
- }
- }
-
- preg_match("/^([ABCDEFGHIJKLMNOP])\s*(.*)/",$line,$matches);
-
- 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";
- }
-
-
- $matches[2] = preg_replace("/<a href=\"\//i","<a href=\"http://www.genome.jp/",$matches[2]);
-
-
- $matches[2] = preg_replace("/<a href=\"/i","<a id=\"tripal_kegg_brite_links\" target=\"_blank\" href=\"",$matches[2]);
-
- if(preg_match("/^(.*?);\s*(\<a.+)/",$matches[2],$mat)){
-
- $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 = db_set_active('chado');
- $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
- db_set_active($previous_db);
-
- $uniquename = $mat[1];
-
- $sql = "SELECT feature_id FROM {feature} WHERE uniquename = '%s'";
- $previous_db = db_set_active('chado');
- $feature_id = db_result(db_query($sql, $uniquename));
- db_set_active($previous_db);
-
- $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 = db_set_active('chado');
- $afp = db_fetch_object(db_query($sql, $feature_id, $analysis_id, $brite_data_type_id));
- db_set_active($prevous_db);
- $hi_rank = 0;
- if ($afp) {
- $hi_rank = $afp->max + 1;
- }
-
-
-
- $sql = "INSERT INTO {analysisfeature} (feature_id, analysis_id) ".
- "VALUES (%d, %d)";
- $previous_db = db_set_active('chado');
- db_query ($sql, $feature_id, $analysis_id);
- db_set_active($previous_db);
-
- $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE feature_id = %d AND analysis_id = %d";
- $previous_db = db_set_active('chado');
- $analysisfeature_id = db_result(db_query($sql, $feature_id, $analysis_id));
- db_set_active($previous_db);
-
-
-
-
- $sql = "SELECT value FROM {analysisfeatureprop} WHERE analysisfeature_id = %d AND type_id = %d";
- $previous_db = db_set_active('chado');
- $result = db_query($sql, $analysisfeature_id, $brite_data_type_id);
- 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 = db_set_active('chado');
- db_query($sql, $analysisfeature_id, $brite_data_type_id, $mat[2], $hi_rank);
- db_set_active($previous_db);
- }
-
- $feature_url = url("$prefix$feature_id");
- $matches[2] = preg_replace("/^(.*?)(;\s*\<a)/","<a id=\"tripal_kegg_feature_links\" target=\"_blank\" href=\"/$prefix$feature_id\">"."$1"."</a>"."$2",$matches[2]);
- }
- $content .= "<li id=\"term_$id\"><a></a>$matches[2]\n";
- $id++;
- }
- $content .= "</ul>";
- fclose($handle);
- return $content;
- }
- function tripal_analysis_kegg_nodeapi(&$node, $op, $teaser, $page) {
- switch ($op) {
-
-
- case 'view':
-
- $types_to_show = variable_get('tripal_analysis_kegg_setting',
- array('chado_feature','chado_analysis_kegg','chado_organism'));
-
- if (!in_array($node->type, $types_to_show, TRUE)) {
-
- $box_status = variable_get("tripal_analysis_kegg-box-results","menu_off");
- if (strcmp($box_status,"menu_on")==0 && $node->type =='chado_organism'){
- variable_set("tripal_analysis_kegg-box-results","menu_off");
- }
- break;
- }
-
-
- if (!$teaser) {
-
- 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 {
-
- $node->content['tripal_analysis_kegg_node_add'] = array(
- '#value' => theme('tripal_analysis_kegg_node_add', $node),
- '#weight' => 6
- );
- }
- }
- }
- }
- 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_analysis_kegg_node_add' => array (
- 'arguments' => array('node'),
- )
- );
- }
- 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 = db_set_active('chado');
- $brite_data_type_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_data_type_id));
- $result = db_query($sql, $feature->feature_id, $brite_data_type_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));
-
- 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 = 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";
- }
- }
- }
- 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 theme_tripal_analysis_kegg_node_add($node) {
-
- if ($node->type == 'chado_analysis_kegg') {
- return tripal_analysis_kegg_full_report($node->analysis_id);
- }
-
-
- else if ($node->type == 'chado_feature') {
- return tripal_analysis_kegg_feature_add($node);
- }
-
-
- else if ($node->type == 'chado_organism') {
- $box_status = variable_get("tripal_analysis_kegg-box-results","menu_off");
-
- if (strcmp($box_status,"menu_off")==0){
- $content .= tripal_analysis_kegg_organism_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){
-
- $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 = db_set_active('chado');
- $result = db_query($sql, $analysis_id);
- 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_feature_add($node) {
-
- $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 = db_set_active('chado');
- $brite_data_type_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_data_type_id));
- $result = db_query($sql, $feature->feature_id, $brite_data_type_id);
-
- if ($hasResult) {
- $content .= "<div id=\"tripal_kegg-hits\" class=\"tripal_kegg-info-box\">
- <div class=\"tripal_expandableBox\">
- <h3>KEGG Analysis</h3>
- </div>
- <div class=\"tripal_expandableBoxContent\">
- <span>
- <table class=\"tripal_kegg_results_table\">
- <tr><td>";
- 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));
-
- 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 = db_set_active('chado');
-
-
- $content .= "<strong>Analysis Date:</strong> $ana_details->time
- (<a href=$ana_url>$ana_details->name</a>)<br>";
-
-
- $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></span></div></div>';
- }
- db_set_active($previous_db);
- return $content;
- }
- function tripal_analysis_kegg_organism_add($node) {
-
-
-
-
- $box_status = variable_get("tripal_analysis_kegg-box-results","menu_off");
-
- if(strcmp($box_status,"menu_off")==0){
- $content .= "
- <div class=\"tripal_kegg_summary-info-box\">
- <div class=\"tripal_expandableBox\">
- <h3>KEGG Analysis Results</h3>
- </div>
- <div class=\"tripal_expandableBoxContent\">
- ";
- }
- $select_analysis = drupal_get_form('tripal_analysis_kegg_select_form',$node);
- $url = url("sites/all/themes/theme_tripal/images/ajax-loader.gif");
- $content .= "
- $select_analysis
- <div id=\"tripal_ajaxLoading\" style=\"display:none\">
- <div id=\"loadingText\">Loading...</div>
- <img src=\"$url\"></div>
- <div id=\"tripal_analysis_kegg_org_report\"></div>
- ";
- if(user_access('access administrative pages')){
- $link = url("tripal_toggle_box_menu/tripal_analysis_kegg/results/$node->nid");
- if(strcmp($box_status,"menu_off")==0){
- $content .= "<br><a href=\"$link\">Show on menu</a>";
- } else {
- $content .= "<br><a href=\"$link\">Remove from menu</a>";
- }
- }
- if(strcmp($box_status,"menu_off")==0){
- $content .= "</div></div>";
- }
- return $content;
- }
- 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 = db_set_active('chado');
- $results = db_query($sql,$node->organism_id);
- db_set_active($previous_db);
- $analyses = array();
- $analyses[''] = '';
- while($analysis = db_fetch_object($results)){
- $analyses[$analysis->analysis_id] = "$analysis->analysis_name";
- }
-
-
- $form['analysis_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;
- }
|