|
@@ -1,1302 +0,0 @@
|
|
|
-<?php
|
|
|
-
|
|
|
-
|
|
|
-/*******************************************************************************
|
|
|
- *
|
|
|
- ******************************************************************************/
|
|
|
-
|
|
|
-function tripal_analysis_kegg_init(){
|
|
|
- // add the tripal_analysis_kegg JS and CSS
|
|
|
- drupal_add_js(drupal_get_path('module', 'tripal_analysis_kegg').'/theme/js/tripal_analysis_kegg.js');
|
|
|
- drupal_add_css(drupal_get_path('module', 'tripal_analysis_kegg').'/theme/css/tripal_analysis_kegg.css', 'theme');
|
|
|
- // add the jsTree JS and CSS
|
|
|
- drupal_add_css(drupal_get_path('theme', 'tripal').'/js/jsTree/source/tree_component.css', 'theme');
|
|
|
- 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') {
|
|
|
- if(!user_access('create chado_analysis_kegg content', $account)){
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
- }
|
|
|
- if ($op == 'update') {
|
|
|
- if (!user_access('edit chado_analysis_kegg content', $account)) {
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
- }
|
|
|
- if ($op == 'delete') {
|
|
|
- if (!user_access('delete chado_analysis_kegg content', $account)) {
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
- }
|
|
|
- if ($op == 'view') {
|
|
|
- if(!user_access('access chado_analysis_kegg content', $account)){
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
- }
|
|
|
- return NULL;
|
|
|
-}
|
|
|
-
|
|
|
-/*******************************************************************************
|
|
|
- */
|
|
|
-function tripal_analysis_kegg_brite($analysis_id, $type_id, $ajax){
|
|
|
- global $base_url;
|
|
|
- $theme_dir = drupal_get_path('theme','tripal');
|
|
|
- // 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 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,'$base_url','$theme_dir')\" 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 Chado'),
|
|
|
- '#description' => t('Note: features used in the KAAS analysis 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.'),
|
|
|
- );
|
|
|
-*/
|
|
|
- return $form;
|
|
|
-}
|
|
|
-/**
|
|
|
-*
|
|
|
-*/
|
|
|
-function chado_analysis_kegg_validate($node, &$form){
|
|
|
- // use the analysis parent to validate the node
|
|
|
- tripal_analysis_validate($node, $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;
|
|
|
- global $base_url;
|
|
|
-
|
|
|
- if($node->keggjob) {
|
|
|
- $job_args[0] = $node->analysis_id;
|
|
|
- $job_args[1] = $node->hierfile;
|
|
|
- $job_args[2] = $base_url;
|
|
|
- $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.");
|
|
|
- }
|
|
|
- }
|
|
|
- // Add a job if the user wants to the keywords from the HTML output
|
|
|
- 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);
|
|
|
- }
|
|
|
-}
|
|
|
-/*******************************************************************************
|
|
|
- * 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 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)) {
|
|
|
- // generate a unique directory name for extracting and parsing the file
|
|
|
- $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";
|
|
|
- }
|
|
|
- // 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);
|
|
|
- if($interval > 1){
|
|
|
- $interval = 1;
|
|
|
- }
|
|
|
- $no_file = 0;
|
|
|
-
|
|
|
- // Remove the analysis features for this analysis
|
|
|
- // we will rebuild them from just this parsing
|
|
|
- $select = array('analysis_id' => $analysis_id);
|
|
|
- if(!tripal_core_chado_delete('analysisfeature',$select)){
|
|
|
- print "ERROR: Unable to clear previous results.\n";
|
|
|
- exit;
|
|
|
- }
|
|
|
-
|
|
|
- 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."%\r";
|
|
|
- }
|
|
|
- $no_file ++;
|
|
|
-
|
|
|
- # $heirarchy variable will be set in tripal_analysis_kegg_parse_kegg_file()
|
|
|
- $results = tripal_analysis_kegg_parse_kegg_file("$hierdir/$file",$heirarchy,
|
|
|
- $analysis_id, $base_path, $query_re,$query_type,$query_uniquename);
|
|
|
-
|
|
|
- # add the item to the database
|
|
|
- if(count($results) > 0){
|
|
|
- print "Loading results for '$heirarchy'\n";
|
|
|
- // We want to insert the KEGG heirarchy results into the
|
|
|
- // analysisprop table. We insert a separate record for each
|
|
|
- // heirarchy. But we need to clear out any prevous results first.
|
|
|
- $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'); // 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, $heirarchy));
|
|
|
- if(!$brite_cvterm_id){
|
|
|
- print "ERROR: Cannot find cvterm for '$heirarchy'.\n";
|
|
|
- exit;
|
|
|
- }
|
|
|
-
|
|
|
- // convert the array to text for saving in the database
|
|
|
- // Replace all single quote as HTML code before insert
|
|
|
- $i = 0;
|
|
|
- $content = '<ul>';
|
|
|
- tripal_analysis_kegg_array2list($results,$content,$i);
|
|
|
- $content .= '</ul>';
|
|
|
- $content = preg_replace("/\'/", "'", $content);
|
|
|
-
|
|
|
- // Insert the value
|
|
|
- $sql = "INSERT INTO {analysisprop} (analysis_id, type_id, value)
|
|
|
- VALUES (%d, %d,'$content')";
|
|
|
-
|
|
|
- if(!db_query($sql, $analysis_id, $brite_cvterm_id)){
|
|
|
- print "ERROR: Cannot add '$heirarchy' results to the database.\n";
|
|
|
- exit;
|
|
|
- }
|
|
|
- tripal_db_set_active($previous_db); // Use drupal database
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- print "Done.\n";
|
|
|
- tripal_job_set_progress($job_id,100);
|
|
|
- closedir($dir_handle);
|
|
|
-
|
|
|
- // If user input a file, remove decompressed files after parsing
|
|
|
- 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++;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-/*******************************************************************************
|
|
|
- * 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, &$heirarchy, $analysis_id,
|
|
|
- $base_path, $query_re, $query_type, $query_uniquename)
|
|
|
-{
|
|
|
-
|
|
|
- print "Parsing $file\n";
|
|
|
-
|
|
|
- // get the 'kegg_brite_data' cvterm
|
|
|
- $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;
|
|
|
-
|
|
|
- // iterate through the lines of the file and recurse through the various levels
|
|
|
- $handle = fopen($file,'r');
|
|
|
- while($line = fgets($handle)){
|
|
|
- $filepos += strlen($line);
|
|
|
- $line = trim($line);
|
|
|
-
|
|
|
- // the first line of the file provides the BRITE heirarchy name
|
|
|
- if(preg_match("/#.*nbsp;\s(.*)<\/h2>$/",$line,$matches)){
|
|
|
- $heirarchy = $matches[1];
|
|
|
- // get the CVterm for the heirarchy. If it doesn't exist then add it
|
|
|
- $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($heirarchy, "KEGG BRITE term: $heirarchy");
|
|
|
- $cvt_arr = tripal_core_chado_select('cvterm',array('cvterm_id'),$select);
|
|
|
- }
|
|
|
- $heirarchy_id = $cvt_arr[0]->cvterm_id;
|
|
|
-
|
|
|
- // now that we have the file type we can recurse
|
|
|
- $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();
|
|
|
-
|
|
|
- // get the current level and the value
|
|
|
- $level = $line[0];
|
|
|
- $value = $line[1];
|
|
|
-
|
|
|
- // now get the next line to see what is coming next. If the
|
|
|
- // next level is greater then recurse immediately.
|
|
|
- $prevpos = $filepos;
|
|
|
- while($next = tripal_analysis_kegg_get_next_line($handle,$filepos)){
|
|
|
- $next_level = $next[0];
|
|
|
- $next_value = $next[1];
|
|
|
-
|
|
|
- // check this line to see if it has a feature we need to keep
|
|
|
- $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 we're going up a level then recurse immediately and add results to our array
|
|
|
- if(ord($next_level) > ord($level)){
|
|
|
- // now recurse
|
|
|
- $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;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // if we go down a level on the next line then reset the
|
|
|
- // filepointer and return
|
|
|
- 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;
|
|
|
-
|
|
|
- // get the next line in the file
|
|
|
- $line = fgets($handle);
|
|
|
- $filepos += strlen($line);
|
|
|
-
|
|
|
- // we hit the end of the file, so exit with a null
|
|
|
- if(!$line){
|
|
|
- return null;
|
|
|
- }
|
|
|
- while(!$good){
|
|
|
- $line = trim($line);
|
|
|
- preg_match("/^([ABCDEFGHIJKLMNOP])\s*(.*)/",$line,$matches);
|
|
|
- $level = $matches[1];
|
|
|
- $value = $matches[2];
|
|
|
-
|
|
|
- // skip lines that aren't data or are empty
|
|
|
- if($level and $value) {
|
|
|
-
|
|
|
- // change all relative paths to absolute paths pointing to KEGG (www.genome.jp)
|
|
|
- // add id to <a> tags so we can link kegg.gif to it in tripal_analysis_kegg.css
|
|
|
- $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);
|
|
|
-
|
|
|
- // this line is good so let's exit out
|
|
|
- $good = 1;
|
|
|
- } else {
|
|
|
- $line = fgets($handle);
|
|
|
- $filepos += strlen($line);
|
|
|
- // we hit the end of the file, so exit with a null
|
|
|
- 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)
|
|
|
-{
|
|
|
-
|
|
|
- // extract the features that have been mapped to the KEGG IDs
|
|
|
- if(preg_match("/^(.*?);\s*(\<a.+)/",$value,$matches)){
|
|
|
- $has_feature = 1;
|
|
|
- $fname = $matches[1];
|
|
|
- $keggterm = $matches[2];
|
|
|
-
|
|
|
- // get the feature name using the user's regular expression
|
|
|
- if ($query_re and preg_match("/$query_re/", $fname, $matches)) {
|
|
|
- $feature = $matches[1];
|
|
|
- }
|
|
|
- // If not in above format then pull up to the first space
|
|
|
- else {
|
|
|
- if (preg_match('/^(.*?)\s.*$/', $fname, $matches)) {
|
|
|
- $feature = $matches[1];
|
|
|
- }
|
|
|
- // if no match up to the first space then just use the entire string
|
|
|
- else {
|
|
|
- $feature = $fname;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 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";
|
|
|
- return;
|
|
|
- }
|
|
|
- if(count($feature_arr) == 0){
|
|
|
- print "Failed: '$feature' cannot find a matching feature in the databasef. RE: $query_re; LINE: $fname\n";
|
|
|
- return;
|
|
|
- }
|
|
|
- $feature_id = $feature_arr[0]->feature_id;
|
|
|
-
|
|
|
- if($feature_id){
|
|
|
- print "Adding KEGG term for $feature ($feature_id,$analysis_id). $heirarchy\n";
|
|
|
-
|
|
|
- // add this term to the analysis feature properties
|
|
|
- tripal_analysis_kegg_insert_featureprop($feature_id,$analysis_id,
|
|
|
- $brite_id,$keggterm);
|
|
|
-
|
|
|
- // get the node ID of the feature if one exists
|
|
|
- $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
|
|
|
- $nid = db_result(db_query($sql, $feature_id));
|
|
|
-
|
|
|
- // Add link to each matched feature
|
|
|
- if($nid){
|
|
|
- $value = preg_replace("/^(.*?)(;\s*\<a)/","<a id=\"tripal_kegg_feature_links\" target=\"_blank\" href=\"$base_path/node/$nid\">"."$1"."</a>"."$2",$value);
|
|
|
- }
|
|
|
- // if we have a feature match then add this to our results array
|
|
|
- return $value;
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
-}
|
|
|
-/**
|
|
|
-*
|
|
|
-*/
|
|
|
-function tripal_analysis_kegg_insert_featureprop ($feature_id, $analysis_id,
|
|
|
- $brite_id,$keggterm)
|
|
|
-{
|
|
|
-
|
|
|
- // add the analysisfeature record if it doesn't already exist.
|
|
|
- $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;
|
|
|
- // Insert into analysisfeatureprop if the value doesn't already exist
|
|
|
- // KEGG heir results sometimes have the same record more than once.
|
|
|
- if($analysisfeature_id){
|
|
|
- // Get the highest rank for this feature_id in analysisfeatureprop table
|
|
|
- $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;
|
|
|
- }
|
|
|
-}
|
|
|
-/**
|
|
|
- *
|
|
|
- *
|
|
|
- * @ingroup tripal_analysis_kegg
|
|
|
- */
|
|
|
-function tripal_analysis_kegg_block($op = 'list', $delta = 0, $edit=array()){
|
|
|
- switch($op) {
|
|
|
- case 'list':
|
|
|
- $blocks['featurekegg']['info'] = t('Tripal Feature KEGG Terms');
|
|
|
- $blocks['featurekegg']['cache'] = BLOCK_NO_CACHE;
|
|
|
-
|
|
|
- $blocks['orgkegg']['info'] = t('Organism KEGG Summary');
|
|
|
- $blocks['orgkegg']['cache'] = BLOCK_NO_CACHE;
|
|
|
-
|
|
|
- $blocks['kegg_base']['info'] = t('Analsyis: KEGG Details');
|
|
|
- $blocks['kegg_base']['cache'] = BLOCK_NO_CACHE;
|
|
|
-
|
|
|
-
|
|
|
- return $blocks;
|
|
|
-
|
|
|
-
|
|
|
- case 'view':
|
|
|
- if(user_access('access chado_analysis_kegg content') and arg(0) == 'node' and is_numeric(arg(1))) {
|
|
|
- $nid = arg(1);
|
|
|
- $node = node_load($nid);
|
|
|
-
|
|
|
- $block = array();
|
|
|
- switch($delta){
|
|
|
- case 'featurekegg':
|
|
|
- $block['subject'] = t('KEGG Terms');
|
|
|
- $block['content'] = theme('tripal_feature_kegg_terms',$node);
|
|
|
- break;
|
|
|
-
|
|
|
- case 'orgkegg':
|
|
|
- $block['subject'] = t('KEGG Terms');
|
|
|
- $block['content'] = theme('tripal_organism_kegg_summary',$node);
|
|
|
- break;
|
|
|
-
|
|
|
- case 'kegg_base':
|
|
|
- $block['subject'] = t('KEGG Terms');
|
|
|
- $block['content'] = theme('tripal_analysis_kegg_report',$node);
|
|
|
- break;
|
|
|
-
|
|
|
- default :
|
|
|
- }
|
|
|
- return $block;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-/*******************************************************************************
|
|
|
- * 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 report on the analysis page
|
|
|
- $node->content['tripal_organism_kegg_summary'] = array(
|
|
|
- '#value' => theme('tripal_organism_kegg_summary', $node),
|
|
|
- );
|
|
|
- break;
|
|
|
- case 'chado_feature':
|
|
|
- // Show KEGG terms on a feature page
|
|
|
- $node->content['tripal_feature_kegg_terms'] = array(
|
|
|
- '#value' => theme('tripal_feature_kegg_terms', $node),
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Implements hook_theme_registry_alter().
|
|
|
- */
|
|
|
-function tripal_analysis_kegg_theme_registry_alter(&$info) {
|
|
|
- // Inject our module into the node theme registry as being an available theme
|
|
|
- // path so that we can override the node template for our content type.
|
|
|
- array_splice($info['node']['theme paths'], 1, 0, array(drupal_get_path('module', 'tripal_analysis_kegg')));
|
|
|
-}
|
|
|
-
|
|
|
-/************************************************************************
|
|
|
- * 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 () {
|
|
|
- $path = drupal_get_path('module', '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',
|
|
|
- 'path' => $path . '/tripal_organism',
|
|
|
- ),
|
|
|
- 'tripal_feature_kegg_terms' => array (
|
|
|
- 'arguments' => array('node'=> null),
|
|
|
- 'template' => 'tripal_feature_kegg_terms',
|
|
|
- 'path' => $path . '/tripal_feature',
|
|
|
- ),
|
|
|
- 'tripal_analysis_kegg_report' => array (
|
|
|
- 'arguments' => array('node'=> null),
|
|
|
- 'template' => 'tripal_analysis_kegg_report',
|
|
|
- 'path' => $path . '/tripal_analysis_kegg',
|
|
|
- ),
|
|
|
- );
|
|
|
-}
|
|
|
-/**
|
|
|
- *
|
|
|
- *
|
|
|
- * @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'];
|
|
|
- $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') {
|
|
|
- // 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_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_id));
|
|
|
- $result = db_query($sql, $feature->feature_id, $brite_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 analysis_id
|
|
|
- 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,$organism->organism_id);
|
|
|
- tripal_db_set_active($previous_db);
|
|
|
-
|
|
|
- # check to ensure we have access to at least one of the GO analyses
|
|
|
- $has_results = 0;
|
|
|
- while($analysis = db_fetch_object($results)){
|
|
|
- $anode = tripal_analysis_get_node($analysis->analysis_id);
|
|
|
- if(node_access("view",$anode)){
|
|
|
- $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_load_feature_terms($feature) {
|
|
|
-
|
|
|
- $feature_id = $feature->feature_id;
|
|
|
-
|
|
|
- // Get the KEGG results stored using the term 'kegg_brite_data'
|
|
|
- $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);
|
|
|
- if(!$afeatureprops){
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // iterate through all of the KEGG properties for this feature
|
|
|
- $results = array();
|
|
|
- foreach ($afeatureprops as $index => $afeatureprop) {
|
|
|
-
|
|
|
- // get the analysis feature record
|
|
|
- $analysisfeature_arr = tripal_core_chado_select('analysisfeature',array('analysis_id'),
|
|
|
- array('analysisfeature_id' => $afeatureprop->analysisfeature_id));
|
|
|
- $analysisfeature = $analysisfeature_arr[0];
|
|
|
-
|
|
|
- // get the analysis record and the analysis_id
|
|
|
- $analysis = tripal_core_generate_chado_var('analysis',
|
|
|
- array('analysis_id' => $analysisfeature->analysis_id));
|
|
|
- $analysis_id = $analysis->analysis_id;
|
|
|
-
|
|
|
- $results[$analysis_id]['analysis'] = $analysis;
|
|
|
- }
|
|
|
-
|
|
|
- // now get all the terms for each 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();
|
|
|
- // 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)){
|
|
|
- # check to see if the user has permission to see this analysis
|
|
|
- $anode = tripal_analysis_get_node($analysis->analysis_id);
|
|
|
- if(node_access("view",$anode)){
|
|
|
- $analyses[$analysis->analysis_id] = "$analysis->analysis_name";
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- global $base_url;
|
|
|
- // we need to set some hidden values for the javascript to use
|
|
|
- $form['theme_dir'] = array(
|
|
|
- '#type' => 'hidden',
|
|
|
- '#value' => drupal_get_path('theme', 'tripal'),
|
|
|
- );
|
|
|
- $form['base_url'] = array(
|
|
|
- '#type' => 'hidden',
|
|
|
- '#value' => $base_url,
|
|
|
- );
|
|
|
- // now generate the select box
|
|
|
- global $base_url;
|
|
|
- $theme_dir = drupal_get_path('theme', 'tripal');
|
|
|
- $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,'".$base_url. "','".$theme_dir."')"
|
|
|
- ),
|
|
|
- );
|
|
|
- return $form;
|
|
|
-}
|
|
|
-
|
|
|
-/*******************************************************************************
|
|
|
- * Parsing KEGG HTML results that are stored in analysisfeatureprop for
|
|
|
- * searching
|
|
|
- * */
|
|
|
-function tripal_analysis_kegg_extract_keywords ($analysis_id) {
|
|
|
-
|
|
|
- // Remove kegg keywords previously stored
|
|
|
- print "Remove keywords previously stored...\n";
|
|
|
- $output_type_id = tripal_get_cvterm_id('kegg_brite_data');
|
|
|
- $sql = "SELECT distinct AFP.analysisfeature_id 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_remove = chado_query($sql);
|
|
|
- $keyword_type_id = tripal_get_cvterm_id('analysis_kegg_output_keywords');
|
|
|
- while ($record = db_fetch_object($results_remove)) {
|
|
|
- $af_id = $record->analysisfeature_id;
|
|
|
- $sql = "DELETE FROM {analysisfeatureprop} WHERE analysisfeature_id =$af_id AND type_id = $keyword_type_id";
|
|
|
- chado_query($sql);
|
|
|
- }
|
|
|
-
|
|
|
- print "Extracting keywords...\n";
|
|
|
- $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);
|
|
|
- // Define what to be extracted in the array
|
|
|
- $search = array (
|
|
|
- "'<[/!]*?[^<>]*?>'si", // replace HTML tags with a space
|
|
|
- "'\n'", // replace newlines with a space
|
|
|
- );
|
|
|
- $replace = array (
|
|
|
- " ",
|
|
|
- " ",
|
|
|
- );
|
|
|
- while ($record = db_fetch_object($results)) {
|
|
|
- $af_id = $record->analysisfeature_id;
|
|
|
- $value = $record->value;
|
|
|
-
|
|
|
- // Retrive keywords for this analysisfeature_id if there is any
|
|
|
- $sql = "SELECT value FROM {analysisfeatureprop} WHERE analysisfeature_id =$af_id AND type_id = $keyword_type_id";
|
|
|
- $keywords = db_result(chado_query($sql));
|
|
|
-
|
|
|
- // Extract new keywords from the kegg html output
|
|
|
- $text = preg_replace($search, $replace, $value);
|
|
|
- $new_keywords = trim(ereg_replace(' +', ' ', $text)); // remove extra spaces
|
|
|
-
|
|
|
- // Append the new keywords
|
|
|
- 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 {
|
|
|
- // Insert the keyword into the analysisfeatureprop table
|
|
|
- $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";
|
|
|
-
|
|
|
-}
|
|
|
-
|