Преглед изворни кода

Bug fixes for various modules and themes

spficklin пре 14 година
родитељ
комит
47dcfaa04c

+ 1 - 3
theme_tripal/js/tripal.js

@@ -1,6 +1,4 @@
-//
-// Copyright 2009 Clemson University
-//
+
 
 if (Drupal.jsEnabled) {
    //------------------------------------------------------------

+ 1 - 1
theme_tripal/js/tripal_analysis_go.js

@@ -31,7 +31,7 @@ if (Drupal.jsEnabled) {
       }
       if (!baseurl) {
          // The last possibility is we've assigned an alias path, get base_url till the last /
-         var baseurl = location.href.substring(0,location.href.lastIndexOf('/'));
+         var baseurl = location.href.substring(0,location.href.indexOf('/',10));
       }
       
       // Form the link for the following ajax call       

+ 2 - 2
theme_tripal/js/tripal_analysis_kegg.js

@@ -29,12 +29,12 @@ if (Drupal.jsEnabled) {
   	     var baseurl = location.href.substring(0,location.href.lastIndexOf('/node'));
       }
       if (!baseurl) {
-         // This base_url is obtained when Clena URL function is off
+         // This base_url is obtained when Clean URL function is off
          var baseurl = location.href.substring(0,location.href.lastIndexOf('/?q=node'));
       }
       if (!baseurl) {
          // The last possibility is we've assigned an alias path, get base_url till the last /
-         var baseurl = location.href.substring(0,location.href.lastIndexOf('/'));
+         var baseurl = location.href.substring(0,location.href.indexOf('/',10));
       }
      
       // Form the link for the following ajax call  

+ 1 - 1
theme_tripal/js/tripal_cv.js

@@ -20,7 +20,7 @@ if (Drupal.jsEnabled) {
        }
        if (!baseurl) {
           // The last possibility is we've assigned an alias path, get base_url till the last /
-          baseurl = location.href.substring(0,location.href.lastIndexOf('/'));
+          baseurl = location.href.substring(0,location.href.indexOf('/',10));
           isClean = 0;
        }
 

+ 3 - 3
theme_tripal/tripal_feature/tripal_feature_blast_results.tpl.php

@@ -19,7 +19,7 @@ if(count($blast_results_list) > 0){
 <div id="tripal_analysis_blast-results-<?php print "$db->db_id" ?>-box" class="tripal_analysis_blast-box tripal-info-box">
 	<div class="tripal-info-box-title tripal_analysis_blast-info-box-title">Blast vs. <?php print $db->name ?></div>
 	<div class="tripal-info-box-desc tripal_analysis_blast-info-box-desc">
-	<strong>Analysis Date: </strong><?php print "$blast_result->ana_time (<a href=".url("node/$blast_result->ana_nid").">$blast_result->ana_name</a>)"?><br>
+	<strong>Analysis Date: </strong><?php print preg_replace("/^(\d+-\d+-\d+) .*/","$1",$analysis->timeexecuted) . " (<a href=".url("node/$analysis->nid").">$analysis->name</a>)"?><br>
 	<!--Query: <?php print "$blast_result->xml_tag"?><br>-->
 	
 	<?php 
@@ -79,7 +79,7 @@ if(count($blast_results_list) > 0){
 		   </tr>
 	      <tr class="<?php print $class ?>">
 		      <td colspan=5>
-			      <a class="blast-hit-arrow-icon" onclick="return tripal_blast_toggle_alignment(<?php print $analysis->aid ?>,<?php print $i?>)"><img id="tripal_analysis_blast-info-toggle-image-<?php print $analysis->aid ?>-<?php print $i?>" src=<?php print $hit['arrowr_url']?> align="top"> View Alignment</a>
+			      <a class="blast-hit-arrow-icon" onclick="return tripal_blast_toggle_alignment(<?php print $analysis->analysis_id ?>,<?php print $i?>)"><img id="tripal_analysis_blast-info-toggle-image-<?php print $analysis->analysis_id ?>-<?php print $i?>" src=<?php print $hit['arrowr_url']?> align="top"> View Alignment</a>
 			      <div class="tripal_analysis_blast-info-hsp-title"></div>
 		      </td>
 	      </tr>
@@ -88,7 +88,7 @@ if(count($blast_results_list) > 0){
 		      <?php 
 		      $hsps_array = $hit['hsp'];
 		      foreach ($hsps_array AS $hsp) { ?>
-			      <div class="tripal_analysis_blast-info-hsp-desc" id="tripal_analysis_blast-info-hsp-desc-<?php print $analysis->aid ?>-<?php print $i?>">
+			      <div class="tripal_analysis_blast-info-hsp-desc" id="tripal_analysis_blast-info-hsp-desc-<?php print $analysis->analysis_id ?>-<?php print $i?>">
 				      &nbsp;HSP <?php  print $hsp['hsp_num'] ?>
 				      <pre>Score: <?php print $hsp['bit_score'] ?> bits (<?php print $hsp['score'] ?>), Expect = <?php print $hsp['evalue'] ?><br>Identity = <?php print sprintf("%d/%d (%.2f%%)", $hsp['identity'], $hsp['align_len'], $hsp['identity']/$hsp['align_len']*100) ?>, Postives = <?php print sprintf("%d/%d (%.2f%%)", $hsp['positive'], $hsp['align_len'], $hsp['positive']/$hsp['align_len']*100)?>, Query Frame = <?php print $hsp['query_frame']?></a><br><br></a>Query: <?php print sprintf("%4d", $hsp['query_from'])?> <?php print $hsp['qseq'] ?> <?php print sprintf("%d", $hsp['query_to']); ?><br>            <?php print $hsp['midline'] ?><br>Sbjct: <?php print sprintf("%4d", $hsp['hit_from']) ?> <?php print $hsp['hseq']?> <?php print sprintf("%d",$hsp['hit_to']) ?></pre><br>
 			      </div>

+ 5 - 1
theme_tripal/tripal_feature/tripal_feature_interpro_results.tpl.php

@@ -5,5 +5,9 @@ $results = $variables['tripal_analysis_interpro']['results'];
 <div id="tripal_feature-interpro_results-box" class="tripal_analysis_interpro-box tripal-info-box">
   <div class="tripal_feature-info-box-title tripal-info-box-title">InterPro Report</div>
   <div class="tripal_feature-info-box-desc tripal-info-box-desc"></div>
-  <?php print $results ?>
+  <?php if($results){?>
+     <?php print $results ?>
+  <?php } else { ?>
+    <div class="tripal-no-results">There is no InterPro report for this feature</div> 
+  <?php }?>
 </div>

+ 1 - 1
theme_tripal/tripal_feature/tripal_feature_references.tpl.php

@@ -44,7 +44,7 @@ $feature = $variables['node']->feature;
         <td><?php print $result->db_name?></td>
         <td><?php 
            if($result->urlprefix){ 
-              ?><a href="<?php print $result->urlprefix.$result->accession?>"><?php print $result->accession?></a><?php 
+              ?><a href="<?php print $result->urlprefix.$result->accession?>" target="_blank"><?php print $result->accession?></a><?php 
            } else { 
              print $result->accession; 
            } 

+ 0 - 7
theme_tripal/tripal_organism/tripal_organism_feature_counts.tpl.php

@@ -6,13 +6,6 @@ $enabled = $variables['tripal_feature']['feature_counts']['enabled'];
 
 if($enabled){
 ?>
-<script type="text/javascript">
-if (Drupal.jsEnabled) {
-   $(document).ready(function() {
-      $("[title]").tooltip();
-   });
-}
-</script>
 <div id="tripal_organism-feature_counts-box" class="tripal_organism-info-box tripal-info-box">
   <div class="tripal_organism-info-box-title tripal-info-box-title">Feature Type Summary</div>
   <div class="tripal_organism-info-box-desc tripal-info-box-desc">The following types of features are currently present in this database. Hold your mouse over the feature type for a popup with a description.</div>

+ 29 - 1
tripal_analysis/tripal_analysis.api.inc

@@ -32,6 +32,34 @@ function tripal_analysis_chado_analysisfeature_schema() {
   return $description;
 }
 
+ /****************************************************************************
+ * Implements hook_chado_analysisfeatureprop_schema()
+ * Purpose: To add descriptions and foreign keys to default table description
+ * Note: This array will be merged with the array from all other implementations
+ *
+ * @return
+ *    Array describing the analysisfeatureprop table
+ */
+function tripal_analysis_chado_analysisfeatureprop_schema() {
+  $description = array();
+
+  $description['foreign keys']['analysisfeature_id'] = array(
+        'table' => 'analysisfeature',
+        'columns' => array(
+          'analysisfeature_id' => 'analysisfeature_id',
+        ),
+  );
+
+  $description['foreign keys']['cvterm'] = array(
+        'table' => 'cvterm',
+        'columns' => array(
+          'type_id' => 'cvterm_id',
+        ),
+  );
+  return $description;
+}
+
+
  /****************************************************************************
  * Implements hook_chado_analysisprop_schema()
  * Purpose: To add descriptions and foreign keys to default table description
@@ -85,7 +113,7 @@ function tripal_analysis_get_property($analysis_id,$property){
 * @ingroup tripal_api
 */
 function tripal_analysis_insert_property($analysis_id,$property,$value,$update_if_present = 0){
-   // first see if the property already exists, if so we can't 
+   // first see if the property already exists, if so we can't insert
    $prop = tripal_analysis_get_property($analysis_id,$property);
    if(count($prop)>0){
       if($update_if_present){

+ 90 - 3
tripal_analysis/tripal_analysis.module

@@ -202,6 +202,7 @@ function chado_analysis_insert($node){
    // add the analysis to the node object for
    // use by other analysis modules that may be using this function
    $node->analysis = $analysis;
+   $node->analysis_id = $analysis_id; // we need to set this for children
 }
 /*******************************************************************************
  *
@@ -601,6 +602,92 @@ function tripal_analysis_sync_analyses ($analysis_id = NULL, $job_id = NULL){
 	}
 	return $page_content;
 }
+/**
+*
+*/
+function tripal_analysis_validate($node, &$form){
+##dprint_r($node);
+
+    // This validation is being used for three activities:
+    // CASE A: Update a node that exists in both drupal and chado
+    // CASE B: Synchronizing a node from chado to drupal
+    // CASE C: Inserting a new node that exists in niether drupal nor chado
+
+    // Only nodes being updated will have an nid already
+    if($node->nid){
+        //---------------------------------------------------
+        // CASE A: We are validating a form for updating an existing node
+        //---------------------------------------------------
+
+        // TO DO: check that the new fields don't yield a non-unique primary key in chado
+    }
+    else{
+        // To differentiate if we are syncing or creating a new analysis altogther, see if an
+        // analysis_id already exists
+
+        if($node->analysis_id){
+
+            //---------------------------------------------------
+            // CASE B: Synchronizing a node from chado to drupal
+            //---------------------------------------------------
+
+        }
+        else{
+
+            //---------------------------------------------------
+            // CASE C: We are validating a form for inserting a new node
+            //---------------------------------------------------
+            // The primary key for the chado analysis table is 
+            // program, programversion, sourcename
+            // Check to see if this analysis really is new -ie, it doesn't have the same
+            // primary key as any other analysis 
+	        $sql = "SELECT analysis_id ".
+                "FROM {analysis} ".
+                "WHERE program='%s'".
+                "AND programversion='%s'".
+                "AND sourcename='%s'";
+	        $previous_db = tripal_db_set_active('chado');
+	        $analysis_id = db_result(db_query($sql, $node->program, $node->programversion, $node->sourcename));
+            tripal_db_set_active($previous_db);
+        
+            if($analysis_id){
+                //---------------------------------------------------
+                // this primary key already exists in chado analysis table!
+                //---------------------------------------------------
+        
+                // check to see if it has also been synced with drupal
+	            $sql = "SELECT nid FROM {chado_analysis} ".
+                                "WHERE analysis_id = %d";
+	            $node_id = db_result(db_query($sql, $analysis_id));
+	            if($node_id){
+                    //---------------------------------------------------
+                    // the analysis has already been synced with drupal, redirect the user
+                    // to modify that node or start over
+                    //---------------------------------------------------
+                    $error = 'This analysis already exists in the chado database (analysis id ';
+                    $error .= $analysis_id.') and has been synchronized ';
+                        $error .= 'with drupal. See node '.$node_id.' if you wish to update that analysis.  ';
+                    $error .= ' For a new analysis, please select a unique primary key ';
+                    $error .= '(primary key consists of sourcename, program and programversion).';
+                    form_set_error('sourcename', t($error));
+                }
+        
+                else{
+                    //---------------------------------------------------
+                    // the analysis does not exist in drupal - tell the user 
+                    // to sync from chado or create a new unique primary key
+                    //---------------------------------------------------
+                    $error = 'This analysis already exists in the chado database (analysis id ';
+                    $error .= $analysis_id.') but has not been synchronized ';
+                    $error .= 'with drupal. See the tripal admin pages to synchronize. ';
+                    $error .= ' For a new analysis, please select a unique primary key ';
+                    $error .= '(primary key consists of sourcename, program and programversion).';
+                    form_set_error('sourcename', t($error));
+                }
+            }
+        }
+    }
+}
 /*******************************************************************************
  * Display help and module information
  * @param path which path of the site we're displaying help
@@ -699,9 +786,9 @@ function get_chado_analyses() {
 		$result = db_query($sql);
 		$previous_db = tripal_db_set_active('chado');
 		$sql = "SELECT Analysis_id, name, description, program, 
-		                       programversion, algorithm, sourcename, sourceversion, 
-		                       sourceuri, timeexecuted
-		           FROM {Analysis} 		           WHERE analysis_id=%d";
+		               programversion, algorithm, sourcename, sourceversion, 
+		               sourceuri, timeexecuted
+		        FROM {Analysis} WHERE analysis_id=%d";
 		$analyses = array();
 		$count = 0;
 		while ($data = db_fetch_object($result)) {

+ 4 - 4
tripal_analysis_blast/parse_blast_XML.inc

@@ -338,6 +338,7 @@ function tripal_analysis_blast_parseXML($analysis_id, $blastdb, $blastfile,
  */
 function tripal_analysis_blast_get_result_object($xml_string,$db,$max,$feature_id, $analysis) {
 	$blast_object = new stdClass();
+
 	// Get the parser using db_id
 	$sql = "SELECT * FROM {tripal_analysis_blast} WHERE db_id = %d";
 	$parser = db_fetch_object(db_query($sql, $db->db_id));
@@ -374,10 +375,9 @@ function tripal_analysis_blast_get_result_object($xml_string,$db,$max,$feature_i
 	}
 
 	// Find node id for the analysis
-	$ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $analysis->aid));
-	$blast_object->ana_nid = $ana_nid;
-	$blast_object->ana_time = $analysis->time;
-	$blast_object->ana_name = $analysis->name;
+	$ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $analysis->analysis_id));
+   $analysis->nid = $ana_nid;
+
 
 	// Load the file.  This XML file should be an extract
 	// of the original XML file with only a single iteration.

+ 81 - 141
tripal_analysis_blast/tripal_analysis_blast.module

@@ -57,6 +57,10 @@ function tripal_analysis_blast_nodeapi(&$node, $op, $teaser, $page) {
 
 	switch ($op) {
 		case 'view':
+
+         if($teaser){
+            return '';
+         }
 			// Find out which node types for showing the blast
 			$types_to_show = variable_get('tripal_analysis_blast_setting',
 			array('chado_feature'));
@@ -65,23 +69,22 @@ function tripal_analysis_blast_nodeapi(&$node, $op, $teaser, $page) {
 			if (!in_array($node->type, $types_to_show, TRUE)) {
 				break;
 			}
-			// Add blast to the content item if it's not a teaser
-			if (!$teaser && $node->feature->feature_id) {
+
+         if(strcmp($node->type,'chado_feature')==0){
 				if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
 					$node->content['tripal_analysis_blast_index_version'] = array(
                   '#value' => theme('tripal_analysis_blast_results_index_version',$node),
                   '#weight' => 8,
 					);
 				} else {
-               if(strcmp($node->type,'chado_feature')==0){
-					   // Show blast result if not at teaser view
-					   $node->content['tripal_feature_blast_results'] = array(
-                     '#value' => theme('tripal_feature_blast_results', $node),
-                     '#weight' => 8
-					   );
-               }
+				   // Show blast result if not at teaser view
+				   $node->content['tripal_feature_blast_results'] = array(
+                  '#value' => theme('tripal_feature_blast_results', $node),
+                  '#weight' => 8
+				   );
 				}
 			}
+         break;
 	}
 }
 /************************************************************************
@@ -119,12 +122,13 @@ function tripal_get_feature_blast_results_ajax($feature_id, $db_id, $max){
  */
 function tripal_analysis_blast_preprocess_tripal_feature_blast_results(&$variables){
    $feature = $variables['node']->feature;
-   $db_id = $variables['node']->db_id;
+   $db_id = $variables['node']->db_id; // this value only gets set on an ajax call
   
    $max = 10;
    if(isset($variables['node']->max)){
       $max = $variables['node']->max;
    }
+
    $blast_results = tripal_get_feature_blast_results($feature->feature_id, $db_id, $max);
    $variables['tripal_analysis_blast']['blast_results_list'] = $blast_results;
 }
@@ -147,61 +151,74 @@ function tripal_get_feature_blast_results($feature_id, $db_id, $max){
    // the type for the property is named 'analysis_blast_output_iteration_hits'
    // and is found in the 'tripal' controlled vocabulary.  This CV term was
    // added by this module.
-	$sql = "SELECT AP.value AS apvalue, AFP.value AS afpvalue, AF.analysis_id AS aid
-	        FROM {analysisfeatureprop} AFP
-	          INNER JOIN {analysisfeature} AF ON AF.analysisfeature_id = AFP.analysisfeature_id
-	          INNER JOIN {analysisprop} AP ON AP.analysis_id = AF.analysis_id
-             INNER JOIN {cvterm} CVT on AFP.type_id = CVT.cvterm_id
-             INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
-	        WHERE AF.feature_id = %d AND CV.name = '%s' AND
-                 CVT.name = '%s' AND AP.value like '%|%' ";
-	$previous_db = tripal_db_set_active('chado');
-	$result = db_query($sql, $feature_id,'tripal','analysis_blast_output_iteration_hits');
-	tripal_db_set_active($previous_db);
+   $select = array(
+      'analysisfeature_id' => array(
+         'feature_id' => $feature_id,
+      ), 
+      'type_id' => array(
+         'name' => 'analysis_blast_output_iteration_hits',
+         'cv_id' => array(
+            'name' => 'tripal'
+         ),
+      ),         
+   );
+   $blast_results = tripal_core_chado_select('analysisfeatureprop',array('*'),$select);
 
 	// get the HTML content for viewing each of the XML file
 	$blast_obj_array = array ();
 	$blast_obj_counter = 0;
-	while ($analysisfeatureprop = db_fetch_object($result)) {
-
-      // the database db_id is stored in the value field of the table
-      // along with the XML.  The two are separated by a bar.  We
-      // will separate these two:
-		$blastsettings = explode("|", $analysisfeatureprop->apvalue);
-
-      // if we don't have the proper number of fields in the value column then
-      // skip this entry
-      if(count($blastsettings) != 3){
-        continue;
+	foreach ($blast_results as $index => $analysisfeatureprop) {
+      // get the blast XML for this feature
+      $blast_xml = $analysisfeatureprop->value;
+
+      // get the analysis record
+      $analysisfeature_arr = tripal_core_chado_select('analysisfeature',array('analysis_id'),
+         array('analysisfeature_id' => $analysisfeatureprop->analysisfeature_id));
+      $analysis_arr = tripal_core_chado_select('analysis',array('*'),
+         array('analysis_id' => $analysisfeature_arr[0]->analysis_id));
+      $analysis = $analysis_arr[0];
+      $analysis_id = $analysis->analysis_id;
+
+      // the old style was to store all parameters in a single CV term in the analysisprop 
+      // table. However now each property has it's own CV term in that table. But,
+      // we still need to support the old method for backwards compatibility.
+      // so, first get the old style variable and see if it has values. In
+      // particular we need the database setting
+      $blast_settings  = tripal_analysis_get_property($analysis_id,'analysis_blast_settings');	
+      if($blast_settings){
+		   $blastsettings = explode("|", $blast_settings[0]->value);
+         // if we don't have the proper number of fields in the value column then
+         // skip this entry
+         if(count($blastsettings) != 3){
+           continue;
+         }
+		   $adb_id = $blastsettings[0];
       }
-      if(!$blastsettings[0]){
-        continue;
+      // if we're not using the old style then try the new method to get the 
+      // database id
+      else {
+         $blastdb = tripal_analysis_get_property($analysis_id,'analysis_blast_blastdb');
+         $adb_id = $blastdb->value;
       }
-		$att_db_id = $blastsettings[0];
-
-		// get analysis name and date
-		$previous_db = tripal_db_set_active('chado');
-		$sql = "SELECT analysis_id AS aid, name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
-		        FROM {analysis}
- 		        WHERE analysis_id = %d";
-		$analysis = db_fetch_object(db_query($sql, $analysisfeatureprop->aid));
-		tripal_db_set_active($previous_db);
-
-		// Get db object using the db_id
-		$previous_db = tripal_db_set_active('chado');
-		$sql = "SELECT * FROM {db} WHERE db_id=%d";
-		$db = db_fetch_object(db_query($sql, $att_db_id));
-		tripal_db_set_active($previous_db);
-
-      // we want to add this blast result to our list if a database id has
-      // not been specified or if it has been specified and the database id
-      // for this analysis matches that of the one requested
-      if(!$db_id or ($db_id and $att_db_id == $db_id)) {
-			$blast_obj = tripal_analysis_blast_get_result_object($analysisfeatureprop->afpvalue,$db,$max,$feature_id, $analysis);
-         $blast_obj->analysis = $analysis;
-			$blast_obj_array [$blast_obj_counter] = $blast_obj;
-			$blast_obj_counter ++;
+      // if the callee specified a database to show then we want to check that
+      // with the database id of the analysis we're looking at.  If they
+      // don't match then skip this blast.  If a database id was not specified
+      // then continue
+      if($db_id and $adb_id != $db_id){
+         continue;
+      } 
+
+      // get the database
+      if($adb_id){
+        $db_arr = tripal_core_chado_select('db',array('*'),array('db_id' => $adb_id)); 
+        $db = $db_arr[0];
       }
+
+      // parse the XML and add it to the array of blast results to be returned
+		$blast_obj = tripal_analysis_blast_get_result_object($blast_xml,$db,$max,$feature_id, $analysis);
+      $blast_obj->analysis = $analysis;
+		$blast_obj_array [$blast_obj_counter] = $blast_obj;
+		$blast_obj_counter ++;
 	}
    return $blast_obj_array;
 }
@@ -505,89 +522,12 @@ function chado_analysis_blast_form ($node){
 	
 	return $form;
 }
-
+/**
+*
+*/
 function chado_analysis_blast_validate($node, &$form){
-    ##dprint_r($node);
-
-    // This validation is being used for three activities:
-    // CASE A: Update a node that exists in both drupal and chado
-    // CASE B: Synchronizing a node from chado to drupal
-    // CASE C: Inserting a new node that exists in niether drupal nor chado
-
-    // Only nodes being updated will have an nid already
-    if($node->nid){
-        //---------------------------------------------------
-        // CASE A: We are validating a form for updating an existing node
-        //---------------------------------------------------
-
-        // TO DO: check that the new fields don't yield a non-unique primary key in chado
-    }
-    else{
-        // To differentiate if we are syncing or creating a new analysis altogther, see if an
-        // analysis_id already exists
-
-        if($node->analysis_id){
-
-            //---------------------------------------------------
-            // CASE B: Synchronizing a node from chado to drupal
-            //---------------------------------------------------
-
-        }
-        else{
-
-            //---------------------------------------------------
-            // CASE C: We are validating a form for inserting a new node
-            //---------------------------------------------------
-            // The primary key for the chado analysis table is 
-            // program, programversion, sourcename
-            // Check to see if this analysis really is new -ie, it doesn't have the same
-            // primary key as any other analysis 
-	        $sql = "SELECT analysis_id ".
-                "FROM {analysis} ".
-                "WHERE program='%s'".
-                "AND programversion='%s'".
-                "AND sourcename='%s'";
-	        $previous_db = tripal_db_set_active('chado');
-	        $analysis_id = db_result(db_query($sql, $node->program, $node->programversion, $node->sourcename));
-            tripal_db_set_active($previous_db);
-        
-            if($analysis_id){
-                //---------------------------------------------------
-                // this primary key already exists in chado analysis table!
-                //---------------------------------------------------
-        
-                // check to see if it has also been synced with drupal
-	            $sql = "SELECT nid FROM {chado_analysis} ".
-                                "WHERE analysis_id = %d";
-	            $node_id = db_result(db_query($sql, $analysis_id));
-	            if($node_id){
-                    //---------------------------------------------------
-                    // the analysis has already been synced with drupal, redirect the user
-                    // to modify that node or start over
-                    //---------------------------------------------------
-                    $error = 'This analysis already exists in the chado database (analysis id ';
-                    $error .= $analysis_id.') and has been synchronized ';
-                        $error .= 'with drupal. See node '.$node_id.' if you wish to update that analysis.  ';
-                    $error .= ' For a new analysis, please select a unique primary key ';
-                    $error .= '(primary key consists of sourcename, program and programversion).';
-                    form_set_error('sourcename', t($error));
-                }
-        
-                else{
-                    //---------------------------------------------------
-                    // the analysis does not exist in drupal - tell the user 
-                    // to sync from chado or create a new unique primary key
-                    //---------------------------------------------------
-                    $error = 'This analysis already exists in the chado database (analysis id ';
-                    $error .= $analysis_id.') but has not been synchronized ';
-                    $error .= 'with drupal. See the tripal admin pages to synchronize. ';
-                    $error .= ' For a new analysis, please select a unique primary key ';
-                    $error .= '(primary key consists of sourcename, program and programversion).';
-                    form_set_error('sourcename', t($error));
-                }
-            }
-        }
-    }
+   // use the analysis parent to validate the node
+   tripal_analysis_validate($node, $form); 
 }
 
 /*******************************************************************************
@@ -643,7 +583,7 @@ function chado_analysis_blast_load($node){
 */
 function chado_analysis_blast_insert($node){
 
-   // insert the analysis
+   // insert the analysistripal_core_generate_chado_var
    chado_analysis_insert($node);
 
    // set the type for this analysis

+ 26 - 6
tripal_analysis_kegg/tripal_analysis_kegg.install

@@ -15,13 +15,10 @@ function tripal_analysis_kegg_install(){
    
    // Inert into drupal's {tripal_analysis}
    tripal_analysis_register_child('tripal_analysis_kegg');
-
-   // Add cvterm 'analysis_interpro_settings' for inserting into analysisprop table
-   tripal_add_cvterms('analysis_kegg_settings', 'Settings of a KEGG analysis, '.
-      'Currently include only the heir.tar.gz file name & path.');
-   tripal_add_cvterms('kegg_brite_data', 'This term is intended for use '.
-      'in the analysisfeatureprop table to store the KAAS results data.');
    
+   // add the cvterms needed for this module
+   tripal_analysis_kegg_add_cvterms ();
+
    // add the db and the cv for the KEGG terms
    tripal_add_db('KEGG','KEGG: Kyoto Encyclopedia of Genes and Genomes.',
       'http://www.genome.jp',
@@ -52,7 +49,30 @@ function tripal_analysis_kegg_install(){
       ''
    );
 }
+/*******************************************************************************
+ * Provide update script for adding new cvterms
+ */
+function tripal_analysis_kegg_update_6000(){
+
+   // we have some new cvterms to add
+   tripal_analysis_kegg_add_cvterms();
 
+   $ret = array(
+      '#finished' => 1,
+   );
+   
+   return $ret;
+}
+/**
+*
+*/
+function tripal_analysis_kegg_add_cvterms () {
+   tripal_add_cvterms('analysis_kegg_settings', 'Settings of a KEGG analysis, Currently include only the heir.tar.gz file name & path.');
+   tripal_add_cvterms('kegg_brite_data', 'This term is intended for use in the analysisfeatureprop table to store the KAAS results data.');
+   tripal_add_cvterms('analysis_kegg_query_re','The regular expression for finding the feature name in the query definition line of the blast results');
+   tripal_add_cvterms('analysis_kegg_query_type','The feature type (e.g. mRNA, polypeptide) of the query input file. This type is used to identify the query feature when multiple features have the same name');
+   tripal_add_cvterms('analysis_kegg_query_uniquename','Indicates if the matched name in the query definition line of the blast results is feature uniquename');
+}
 /*******************************************************************************
 * Implementation of hook_uninstall()
 */

+ 117 - 26
tripal_analysis_kegg/tripal_analysis_kegg.module

@@ -46,10 +46,10 @@ function tripal_analysis_kegg_menu() {
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK
    );
-   $items['tripal_analysis_kegg_org_report'] = array(
+   $items['tripal_analysis_kegg_org_report/%'] = array(
       'path' => 'tripal_analysis_kegg_org_report',
       'title' => t('Analysis KEGG report'),
-      'page callback' => 'f',
+      'page callback' => 'tripal_analysis_kegg_org_report',
       'page arguments' => array(1),
       'access arguments' => array('access chado_analysis_kegg content'),
       'type' => MENU_CALLBACK
@@ -181,6 +181,10 @@ function chado_analysis_kegg_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';
@@ -200,6 +204,34 @@ function chado_analysis_kegg_form ($node){
                            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'),
@@ -218,10 +250,13 @@ function chado_analysis_kegg_insert($node){
    chado_analysis_insert($node);
 
    // set the type for this analysis
-   tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_type','tripal_analysis_kegg');
+   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);
@@ -233,9 +268,13 @@ function chado_analysis_kegg_submit_job($node){
    global $user;
 
    if($node->keggjob) {
-      $job_args[0] = $analysis_id;
+      $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',
@@ -260,10 +299,14 @@ function chado_analysis_kegg_update($node){
    chado_analysis_update($node);
 
    // set the type for this analysis
-   tripal_analysis_update_property($node->analysis->analysis_id,'analysis_type','tripal_analysis_kegg',1);
+   tripal_analysis_update_property($node->analysis_id,'analysis_type','tripal_analysis_kegg',1);
 
    // now add in the remaining settings as a single property but separated by bars
    tripal_analysis_update_property($node->analysis_id,'analysis_kegg_settings',$node->hierfile,1);
+   tripal_analysis_update_property($node->analysis_id,'analysis_kegg_query_re',$node->query_re,1);	
+   tripal_analysis_update_property($node->analysis_id,'analysis_kegg_query_type',$node->query_type,1);	
+   tripal_analysis_update_property($node->analysis_id,'analysis_kegg_query_uniquename',$node->query_uniquename,1);	
+
      
    // Add a job if the user wants to parse the html output
    chado_analysis_kegg_submit_job($node);
@@ -282,16 +325,22 @@ function chado_analysis_kegg_load($node){
    $analysis_id = $analysis->analysis_id;
 
    // get the heirfile name
-   $hierfile  = tripal_analysis_get_property($analysis_id,'analysis_kegg_settings');	
+   $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;
 }
 
-/*******************************************************************************
- *  This function customizes the view of the chado_analysis node.  It allows
- *  us to generate the markup.
- */
+/**
+*
+*/
 function chado_analysis_kegg_view ($node, $teaser = FALSE, $page = FALSE) {
    // use drupal's default node view:
    if (!$teaser) {
@@ -311,8 +360,9 @@ function chado_analysis_kegg_view ($node, $teaser = FALSE, $page = FALSE) {
 }
 /********************************************************************************
  */
-function tripal_analysis_kegg_parseHierFile ($analysis_id, $hierfile, $base_path, $job_id) {
-   
+function tripal_analysis_kegg_parseHierFile ($analysis_id, $hierfile, $base_path, 
+   $query_re,$query_type,$query_uniquename,$job_id) {
+ 
    // If user input a file (e.g. hier.tar.gz), decompress it first
    if (is_file($hierfile)) {
       $data_dir = file_directory_path() . "/tripal/tripal_analysis_kegg";
@@ -340,7 +390,7 @@ function tripal_analysis_kegg_parseHierFile ($analysis_id, $hierfile, $base_path
          $no_file ++;
          
          # $type variable will be set in tripal_analysis_kegg_parse_kegg_file()
-         $content = tripal_analysis_kegg_parse_kegg_file("$hierdir/$file",$type,$analysis_id, $base_path);
+         $content = tripal_analysis_kegg_parse_kegg_file("$hierdir/$file",$type,$analysis_id, $base_path, $query_re,$query_type,$query_uniquename);
 
          # add the item to the database
          if($content){
@@ -402,7 +452,7 @@ function tripal_analysis_kegg_parseHierFile ($analysis_id, $hierfile, $base_path
  * and added to chado as a cvterm. Tree structure for this cvterm is then 
  * generated and saved to analysisfeature and analysisfeatureprop tables.
  */
-function tripal_analysis_kegg_parse_kegg_file ($file,&$type,$analysis_id, $base_path){
+function tripal_analysis_kegg_parse_kegg_file ($file,&$type,$analysis_id, $base_path, $query_re,$query_type,$query_uniquename){
    $handle = fopen($file,'r');
    $depth = array();
    $current = '@';  # this is one character below 'A' in the ASCII table
@@ -410,7 +460,8 @@ function tripal_analysis_kegg_parse_kegg_file ($file,&$type,$analysis_id, $base_
    $id = 0;
    $type_id = 0;
    $no_line = 0;
-   $prefix = variable_get('chado_feature_accession_prefix','ID');
+
+
    while($line = fgets($handle)){
       $no_line ++;
       // Find out what kind of file we're looking at.
@@ -460,7 +511,7 @@ function tripal_analysis_kegg_parse_kegg_file ($file,&$type,$analysis_id, $base_
       $matches[2] = preg_replace("/<a href=\"/i","<a id=\"tripal_kegg_brite_links\" target=\"_blank\" href=\"",$matches[2]);
 
       // extract the features that have been mapped to the KEGG IDs
-      if(preg_match("/^(.*?);\s*(\<a.+)/",$matches[2],$mat)){
+      if(preg_match("/^(.*?);\s*(\<a.+)/",$matches[2],$defline)){
          // Find cvterm_id for 'kegg_brite_data'
          $sql = "SELECT cvterm_id 
                  FROM {cvterm} CVT
@@ -470,13 +521,54 @@ function tripal_analysis_kegg_parse_kegg_file ($file,&$type,$analysis_id, $base_
          $previous_db = tripal_db_set_active('chado');
          $brite_data_type_id = db_result(db_query($sql, 'kegg_brite_data'));
          tripal_db_set_active($previous_db);
-         
-         $uniquename = $mat[1];
-         // Find feature_id using uniquename
-         $sql = "SELECT feature_id FROM {feature} WHERE uniquename = '%s'";
-         $previous_db = tripal_db_set_active('chado');
-         $feature_id = db_result(db_query($sql, $uniquename)); // retrive first returned feature_id (assuming uniquename is unique)
-         tripal_db_set_active($previous_db);
+
+			// get the feature name using the user's regular expression
+			if ($query_re and preg_match("/$query_re/", $defline[1], $parts)) {
+				$feature = $parts[1];
+			} 
+         // If not in above format then pull up to the first space
+         else {
+				if (preg_match('/^(.*?)\s.*$/', $defline[1], $parts)) {
+					$feature = $parts[1];
+            } 
+            // if no match up to the first space then just use the entire string
+            else {
+               $feature = $defline[1];
+            }
+         } 
+
+         // now find the feature in chado
+         $select = array();
+         if($query_uniquename){
+            $select['uniquename'] = $feature;
+         } else {
+            $select['name'] = $feature;
+         }
+         if($query_type){
+            $select['type_id'] = array(
+              'cv_id' => array(
+                 'name' => 'sequence'
+              ),
+              'name' => $query_type,
+            );
+         }
+         $feature_arr = tripal_core_chado_select('feature',array('feature_id'),$select);
+
+         if(count($feature_arr) > 1){
+			   print "Ambiguous: '$feature' matches more than one feature and is being skipped.\n";
+				continue;
+         }
+         if(count($feature_arr) == 0){
+				print "Failed: '$feature' cannot find a matching feature in the database.\n";
+            continue;
+         }
+         print "Adding KEGG results for $feature ($feature_id,$analysis_id)\n";
+         $feature_id = $feature_arr[0]->feature_id;
+
+         // get the node ID of the feature if one exists
+         $sql = "SELECT nid FROM {chado_feture} WHERE feature_id = %d";
+         $nid = db_result(db_query($sql, $feature_id)); 
+
          // Get the higest rank for this feature_id in analysisfeatureprop table
          $sql = "SELECT MAX(rank) FROM {analysisfeatureprop} AFP ".
                 "INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id ".       
@@ -506,7 +598,7 @@ function tripal_analysis_kegg_parse_kegg_file ($file,&$type,$analysis_id, $base_
          //------------------------------------------------------
          // Insert into analysisfeatureprop table
          //------------------------------------------------------
-         // Before inserting, make sure it's not a duplicate
+         // Before inserting, make sure it's not a duplicatefs
          $sql = "SELECT value FROM {analysisfeatureprop} WHERE analysisfeature_id = %d AND type_id = %d";
          $previous_db = tripal_db_set_active('chado');
          $result = db_query($sql, $analysisfeature_id, $brite_data_type_id);
@@ -527,8 +619,7 @@ function tripal_analysis_kegg_parse_kegg_file ($file,&$type,$analysis_id, $base_
             tripal_db_set_active($previous_db);
          }
          // Add link to each matched feature
-         $feature_url = url("$prefix$feature_id");
-         $matches[2] = preg_replace("/^(.*?)(;\s*\<a)/","<a id=\"tripal_kegg_feature_links\" target=\"_blank\" href=\"$base_path$prefix$feature_id\">"."$1"."</a>"."$2",$matches[2]);
+         $matches[2] = preg_replace("/^(.*?)(;\s*\<a)/","<a id=\"tripal_kegg_feature_links\" target=\"_blank\" href=\"".url("/node/$nid")."\">"."$1"."</a>"."$2",$matches[2]);
       }
       $content .= "<li id=\"term_$id\"><a></a>$matches[2]\n";
       $id++;

+ 15 - 15
tripal_core/tripal_core.api.inc

@@ -747,11 +747,11 @@ function tripal_core_generate_chado_var($table, $values) {
 
     //if criteria then remove from query
     $success = drupal_eval('<?php return '.$criteria.'; ?>');
-    watchdog('tripal_core', 
-      'Evaluating criteria (%criteria) for field %field in tripal_core_generate_chado_var for %table evaluated to %success',
-      array('%table' => $table, '%criteria'=>$criteria, '%field' => $field_name, '%success'=>$success),
-      WATCHDOG_NOTICE
-    );
+//    watchdog('tripal_core', 
+//      'Evaluating criteria (%criteria) for field %field in tripal_core_generate_chado_var for %table evaluated to %success',
+//      array('%table' => $table, '%criteria'=>$criteria, '%field' => $field_name, '%success'=>$success),
+//      WATCHDOG_NOTICE
+//    );
     if ($success) {
       unset($table_columns[array_search($field_name, $table_columns)]);
       unset($fields_to_remove[$field_name]);
@@ -783,11 +783,11 @@ function tripal_core_generate_chado_var($table, $values) {
 
         //if criteria then remove from query
         $success = drupal_eval('<?php return '.$criteria.'; ?>');
-        watchdog('tripal_core', 
-          'Evaluating criteria (%criteria) for field %field of $type in tripal_core_generate_chado_var for %table evaluated to %success',
-          array('%table'=>$table, '%criteria'=>$criteria, '%field'=>$field_name, '%type'=>$field_type, '%success'=>$success),
-          WATCHDOG_NOTICE
-        );
+//        watchdog('tripal_core', 
+//          'Evaluating criteria (%criteria) for field %field of $type in tripal_core_generate_chado_var for %table evaluated to %success',
+//          array('%table'=>$table, '%criteria'=>$criteria, '%field'=>$field_name, '%type'=>$field_type, '%success'=>$success),
+//          WATCHDOG_NOTICE
+//        );
         if ($success) {
           unset($table_columns[array_search($field_name, $table_columns)]);
           $all->expandable_fields[] = $table . '.' . $field_name;
@@ -833,11 +833,11 @@ function tripal_core_generate_chado_var($table, $values) {
       $criteria = preg_replace('/&gt;field_value&lt; /', $object->{$field_name}, $criteria);
       //if criteria then remove from query
       $success = drupal_eval('<?php return '.$criteria.'; ?>');
-      watchdog('tripal_core', 
-        'Evaluating criteria (%criteria) for field %field in tripal_core_generate_chado_var for   %table evaluated to %success',
-        array('%table' => $table, '%criteria'=>$criteria, '%field' => $field_name, '%success'=>$success),
-        WATCHDOG_NOTICE
-      );
+//      watchdog('tripal_core', 
+//        'Evaluating criteria (%criteria) for field %field in tripal_core_generate_chado_var for   %table evaluated to %success',
+//        array('%table' => $table, '%criteria'=>$criteria, '%field' => $field_name, '%success'=>$success),
+//        WATCHDOG_NOTICE
+//      );
       if ($success) {
         unset($object->{$field_name});
         $object->expandable_fields[] = $table . '.' . $field_name;