Browse Source

Merged in 6.x-0.4-1589084-feature_props

Stephen Ficklin 13 years ago
parent
commit
6d9de99026

+ 0 - 34
tripal_analysis/tripal_analysis.admin.inc

@@ -481,39 +481,5 @@ function tripal_analysis_admin_validate($form, &$form_state) {
 		tripal_add_job('Cleanup orphaned analyses','tripal_analysis',
          'tripal_analyses_cleanup',$job_args,$user->uid);
 	}
-
-	// -------------------------------------
-	// Save blast regular expression settings
-	if ($form_state['values']['op'] == t('Save settings')) {
-		$db_id = $form_state['values']['blastdb'];
-		$name = $form_state['values']['displayname'];
-		$gbstyle = $form_state['values']['gb_style_parser'];
-		$reg1 = $form_state['values']['hit_id'];
-		$reg2 = $form_state['values']['hit_def'];
-		$reg3 = $form_state['values']['hit_accession'];
-		
-		// Check if the blast settings exists
-		
-		$sql = "SELECT db_id FROM {tripal_analysis_blast} WHERE db_id=%d";
-		$check = db_result(db_query($sql, $db_id));
-
-		// If setting exists, update it
-		if ($check) {
-			$sql = "UPDATE {tripal_analysis_blast} ".
-   	          "SET displayname = '%s', ".
-   	          "    regex_hit_id = '%s', ".
-   	          "    regex_hit_def = '%s', ".
-   	          "    regex_hit_accession = '%s', ".
-			       "    genbank_style = %d ".
-   	          "WHERE db_id=%d";
-			db_query($sql, $name, $reg1, $reg2, $reg3, $gbstyle, $db_id);
-			// Otherwise, insert a new setting for the db
-		} else {
-			$sql = "INSERT INTO {tripal_analysis_blast} (db_id, displayname, ".
-   		       "           regex_hit_id, regex_hit_def, regex_hit_accession, genbank_style) ".
-   		       "VALUES (%d, '%s', '%s', '%s', '%s', %d) ";
-			db_query($sql, $db_id, $name, $reg1, $reg2, $reg3, $gbstyle);
-		}
-	}
 }
 

+ 53 - 4
tripal_core/tripal_core.api.inc

@@ -1359,7 +1359,7 @@ function chado_get_node_id ($table, $id) {
  *   The base table for which the property should be retrieved. Thus to retrieve a property
  *   for a feature the basetable=feature and property is retrieved from featureprop
  * @param $record_id
- *   The primary key of the basetable to retrieve properties for. This should be in integer.
+ *   The foriegn key field of the base table. This should be in integer.
  * @param $property
  *   The cvterm name describing the type of properties to be retrieved
  * @param $cv_name
@@ -1371,7 +1371,7 @@ function chado_get_node_id ($table, $id) {
  * @ingroup tripal_chado_api
  */
 function tripal_core_get_property($basetable, $record_id, $property, $cv_name){
-
+  
    // get the foreign key for this property table
    $table_desc = module_invoke_all('chado_'.$basetable.'prop_schema');
    $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
@@ -1387,6 +1387,7 @@ function tripal_core_get_property($basetable, $record_id, $property, $cv_name){
          'is_obsolete' => 0
       ),
    );
+
    $results = tripal_core_generate_chado_var($basetable.'prop',$values);
    $results = tripal_core_expand_chado_vars($results,'field',$basetable.'prop.value');
    return $results;
@@ -1401,7 +1402,7 @@ function tripal_core_get_property($basetable, $record_id, $property, $cv_name){
  *   The base table for which the property should be inserted. Thus to insert a property
  *   for a feature the basetable=feature and property is inserted into featureprop
  * @param $record_id
- *   The primary key of the basetable to insert a property for. This should be in integer.
+ *   The foriegn key field of the base table. This should be in integer. 
  * @param $property
  *   The cvterm name describing the type of properties to be inserted
  * @param $cv_name
@@ -1426,21 +1427,32 @@ function tripal_core_insert_property($basetable, $record_id, $property,
    // insert
    $prop = tripal_core_get_property($basetable,$record_id,$property,$cv_name);
    $rank = 0;
+   $exists = 0;
    if(count($prop)>0){ 
       if($update_if_present){
          return tripal_core_update_property($basetable,$record_id,$property,$cv_name,$value) ;
       } else {
-         // iterate through the properties returned and get the largest rank
+         // iterate through the properties returned and check to see if the
+         // property with this value already exists if not, get the largest rank
+         // and insert the same property but with this new value
          foreach($prop as $p){
             if($p->rank > $rank){
                $rank = $p->rank;
             }
+            if(strcmp($p->value,$value)==0){
+               $exists = 1;
+            }
          }
          // now add 1 to the rank
          $rank++;
       }
    }
 
+   // if the property with this value exists then we return TRUE as if the
+   // insert was successful.
+   if($exists){
+      return TRUE;
+   }
    // get the foreign key for this property table
    $table_desc = module_invoke_all('chado_'.$basetable.'prop_schema');
    $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
@@ -1783,3 +1795,40 @@ function tripal_get_max_chado_rank ($tablename, $where_options) {
 		return -1;
 	}
 }
+/**
+ * Add a new table to the Chado schema. This function is simply a wrapper for
+ * the db_create_table() function of Drupal, but ensures the table is created
+ * inside the Chado schema rather than the Drupal schema.
+ *
+ * @param $ret
+ *   Array to which query results will be added.
+ * @param $table
+ *   The name of the table to create.
+ * @param $schema 
+ *   A Drupal-style Schema API definition of the table
+ *
+ * @return 
+ *   A database query result resource for the new table, or FALSE if table was not constructed.
+ *
+ * @ingroup tripal_core_api
+ */
+function tripal_create_chado_table (&$ret,$table,$schema) {
+   $ret = array();
+   
+   $previous_db = tripal_db_set_active('chado');  // use chado database
+   db_create_table($ret,$table,$schema);
+   tripal_db_set_active($previous_db);  // now use drupal database
+   
+   // if the table creation was succesful then add an entry
+   // in the tripal_custom_table
+   if($ret){
+      $record = new stdClass();
+      $record->table_name = $table;
+      $record->schema = serialize($schema);
+      $success = drupal_write_record('tripal_custom_tables',$record);
+      if(!$success){
+         drupal_set_message($message,"Error adding custom table: $table");   
+      }
+   }
+   return $ret;
+}

+ 6 - 1
tripal_core/tripal_core.drush.inc

@@ -76,9 +76,14 @@ function tripal_core_drush_command() {
  */
 function drush_tripal_core_tripal_launch_jobs () {
   $parallel = drush_get_option('parallel');   
+  
   if($parallel){
-    tripal_jobs_launch($do_parallel);
+    print "Tripal Job Launcher (in parallel)\n";
+    print "-------------------\n";
+    tripal_jobs_launch($parallel);
   } else {
+    print "Tripal Job Launcher\n";
+    print "-------------------\n";
     tripal_jobs_launch();  
   }
 }

+ 32 - 2
tripal_core/tripal_core.install

@@ -31,12 +31,19 @@ function tripal_core_update_6000(){
    // recreate the materialized view
    db_add_field($ret, 'tripal_mviews', 'status', array('type' => 'text', 'size' => 'normal', 'not null' => FALSE));
    db_add_field($ret, 'tripal_mviews', 'comment', array('type' => 'text', 'size' => 'normal', 'not null' => FALSE));
+   
+   // create the custom tables table
+   $ret = array();
+   $schema = tripal_core_custom_tables_schema();
+   db_create_table($ret,'tripal_custom_tables',$schema['tripal_custom_tables']);
+   
    $ret = array(
       '#finished' => 1,
    );
-   
+
    return $ret;
 }
+
 /************************************************************************
 * Implementation of hook_schema().
 *
@@ -75,7 +82,10 @@ function tripal_core_get_schemas (){
    foreach ($temp as $table => $arr){ 
       $schema[$table] = $arr; 
    }
-
+   $temp = tripal_core_custom_tables_schema();
+   foreach ($temp as $table => $arr){ 
+      $schema[$table] = $arr; 
+   }
 	return $schema;
 }
 /************************************************************************
@@ -144,4 +154,24 @@ function tripal_core_jobs_schema(){
    );
    return $schema;
 }
+/************************************************************************
+* 
+*
+* @ingroup tripal_core
+*/
+function tripal_core_custom_tables_schema(){
+   $schema = array();
+   $schema['tripal_custom_tables'] = array(
+      'fields' => array(
+         'table_id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
+         'table_name' => array ('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
+         'schema' => array('type' => 'text','not null' => TRUE),
+      ),
+      'indexes' => array(
+         'table_id' => array('table_id'),
+      ),
+      'primary key' => array('table_id'),
+   );
+   return $schema;
+}
 ?>

+ 23 - 2
tripal_core/tripal_core.schema.api.inc

@@ -37,10 +37,20 @@
  */
 
 /**
+ * Retrieves the list tables in the Chado schema.  By default it only retursn
+ * the default Chado tables, but may also return custom tables added to the 
+ * Chado schema as well.
  *
+ * @param $include_custom
+ *   Optional.  Set as TRUE to include any custom tables created in the
+ *   Chado schema. Custom tables are added to Chado using the
+ *   tripal_core_chado_create_table() function.
+ *
+ * @returns
+ *   An associative array where the key and value pairs are the Chado table names.
  * @ingroup tripal_schema_api
  */
-function tripal_core_get_chado_tables() {
+function tripal_core_get_chado_tables($include_custom = NULL) {
   if(is_array($db_url) and array_key_exists('chado',$db_url)){
     $previous_db = tripal_db_set_active('chado');
     $sql = 'SELECT tablename FROM pg_tables';
@@ -50,11 +60,22 @@ function tripal_core_get_chado_tables() {
     $sql = "SELECT tablename FROM pg_tables WHERE schemaname='chado'";
     $resource = db_query($sql);
   }
-  
   $tables = array();
   while ($r = db_fetch_object($resource)) {
     $tables[$r->tablename] = $r->tablename;
+  }  
+  
+  
+  // now add in the custom tables too
+  if($include_custom){
+    $sql = "SELECT table_name FROM tripal_custom_tables";
+    $resource = db_query($sql);
   }
+  while ($r = db_fetch_object($resource)) {
+    $tables[$r->table_name] = $r->table_name;
+  } 
+  
+
   asort($tables);
   return $tables;
 }

+ 46 - 8
tripal_cv/tripal_cv.api.inc

@@ -375,6 +375,39 @@ function tripal_cv_add_cv($name,$comment){
    return $cv;
 }
 /**
+*  Add's a CV term to the cvterm table.  If the parent CV does not exist then
+*  that too is added to the CV table.  If the cvterm is a relationship term
+*  then the $is_relationship argument should be set.  The function will try
+*  to first find the relationship in the relationship ontology for updating and
+*  if it can't be found will add the relationship to the __global CV.  All terms
+*  must also have a corresponding database.  This is specified in the term's
+*  ID just before the colon (e.g. GO:003824).  If the database does not exist
+*  in the DB table then it will be added automatically.  The accession (the 
+*  value just after the colon in the term's ID) will be added to the dbxref 
+*  table.  If the CVterm already exists and $update is set (default) then the
+*  cvterm is updated.  If the CVTerm already exists and $update is not set, then
+*  no changes are made and the CVTerm object is returned.
+*
+* @param $term
+*   An associative array with the following keys: 'id', 'name' and 'namespace',
+*   'is_obsolete', and 'def'.  Where 'id' is the term accession, 'name' is the 
+*   term name, 'namespace' is the CV name for the term, 'def' is the term 
+*   definition and 'is_obsolete' is present and set to 1 if the term is defunct.
+*   The 'id' must be of the form <DB>:<ACCESSION>, where <DB> is the name of
+*   the database to which the cvterm belongs and the <ACCESSION> is the 
+*   term's accession number in the database.
+* @param $defaultcv
+*   Optional. A CV object populated with the fields of the CV to which the term 
+*   belongs.  If this arugment is null or not provided then the function tries
+*   to find a record in the CV table with the same name provided in the 
+*   $term[namespace]. 
+* @param $is_relationship
+*   If this term is a relationship term then this value should be 1.
+* @param $update
+*   By default this is set to 1.  If the term exists it is automatically updated.
+*
+* @return
+*   A CVTerm object  
 *
 * @ingroup tripal_cv_api
 */
@@ -400,13 +433,15 @@ function tripal_cv_add_cvterm($term,$defaultcv,$is_relationship = 0,$update = 1)
    }
 
    // this SQL statement will be used a lot to find a cvterm so just set it
-   // here for easy reference below.
+   // here for easy reference below.  Because CV terms can change their names
+   // but accessions don't change, the following SQL finds cvterms based on
+   // their accession rather than the name
    $cvtermsql = "SELECT CVT.name, CVT.cvterm_id, DB.name as dbname, DB.db_id 
                   FROM {cvterm} CVT
                     INNER JOIN {dbxref} DBX on CVT.dbxref_id = DBX.dbxref_id
                     INNER JOIN {db} DB on DBX.db_id = DB.db_id
                     INNER JOIN {cv} CV on CV.cv_id = CVT.cv_id
-                  WHERE CVT.name = '%s' and DB.name = '%s'";  
+                  WHERE DBX.accession = '%s' and DB.name = '%s'";  
 
    // get the accession and the database from the cvterm
    if(preg_match('/^.+?:.*$/',$id)){
@@ -418,7 +453,7 @@ function tripal_cv_add_cvterm($term,$defaultcv,$is_relationship = 0,$update = 1)
       // because this is a relationship cvterm first check to see if it 
       // exists in the relationship ontology. If it does then return the cvterm.
       //  If not then set the dbname to _global and we'll add it or find it there
-      $cvterm = db_fetch_object(db_query($cvtermsql,$name,'OBO_REL'));
+      $cvterm = db_fetch_object(db_query($cvtermsql,$accession,'OBO_REL'));
       if($cvterm){
          return $cvterm;
       } else {
@@ -426,7 +461,7 @@ function tripal_cv_add_cvterm($term,$defaultcv,$is_relationship = 0,$update = 1)
          // return it no matter what the original CV
          $dbname = '_global';
 
-         $cvterm = db_fetch_object(db_query($cvtermsql,$name,$dbname));
+         $cvterm = db_fetch_object(db_query($cvtermsql,$accesion,$dbname));
          if($cvterm){
             return $cvterm;
          }
@@ -437,7 +472,8 @@ function tripal_cv_add_cvterm($term,$defaultcv,$is_relationship = 0,$update = 1)
       return 0;
    }
 
-   // check to see if the database exists. 
+   // add the database. The function will just return the DB object if the
+   // database already exists. 
    $db = tripal_db_add_db($dbname);
    if(!$db){
       watchdog('tripal_cv', "Cannot find database '$dbname' in Chado.",NULL,WATCHDOG_WARNING);
@@ -446,7 +482,7 @@ function tripal_cv_add_cvterm($term,$defaultcv,$is_relationship = 0,$update = 1)
 
 
    // if the cvterm doesn't exist then add it otherwise just update it
-   $cvterm = db_fetch_object(db_query($cvtermsql,$name,$dbname));
+   $cvterm = db_fetch_object(db_query($cvtermsql,$accession,$dbname));
    if(!$cvterm){
       // check to see if the dbxref exists if not, add it
       $dbxref =  tripal_db_add_dbxref($db->db_id,$accession);
@@ -458,7 +494,6 @@ function tripal_cv_add_cvterm($term,$defaultcv,$is_relationship = 0,$update = 1)
       // check to see if the dbxref already has an entry in the cvterm table
       $sql = "SELECT * FROM {cvterm} WHERE dbxref_id = %d";
       $check = db_fetch_object(db_query($sql,$dbxref->dbxref_id));
-
       if(!$check){
          // now add the cvterm
          $sql = "
@@ -477,11 +512,14 @@ function tripal_cv_add_cvterm($term,$defaultcv,$is_relationship = 0,$update = 1)
             }
          }  
       }
+      // if the dbxref already exists check to make sure it exists for the correct databaes name
+      // if it does then we're good and we don't need to do anything
       elseif($check and strcmp($check->name,$name)==0){
          // this entry already exists. We're good, so do nothing
       }
+      // if the dbxref exists but does not map to the same database name
       elseif($check and strcmp($check->name,$name)!=0){
-         watchdog('tripal_cv', "The dbxref already exists in the cvterm table: $dbxref->dbxref_id ($$accession) for term $name",NULL,WATCHDOG_WARNING);
+         watchdog('tripal_cv', "The dbxref already exists in the cvterm table. DBXREF ID: $dbxref->dbxref_id, ACCESSION: $accession. DB:  '".$dbxref->db_name ."'. term '$name'. The requested db was '$dbname'",NULL,WATCHDOG_WARNING);
          return 0;
       }
       $cvterm = db_fetch_object(db_query($cvtermsql,$name,$dbname));

+ 7 - 1
tripal_db/tripal_db.api.inc

@@ -314,7 +314,13 @@ function tripal_db_add_db($dbname){
 function tripal_db_add_dbxref($db_id,$accession,$version='',$description=''){
 
    // check to see if the dbxref exists if not, add it
-   $dbxsql = "SELECT dbxref_id FROM {dbxref} WHERE db_id = %d and accession = '%s'";
+   $dbxsql = "
+      SELECT DBX.dbxref_id, DBX.db_id, DBX.description, DBX.version, DBX.accession,
+         DB.name as db_name
+      FROM {dbxref} DBX
+         INNER JOIN db DB on DB.db_id = DBX.db_id
+      WHERE DBX.db_id = %d and DBX.accession = '%s'
+   ";
    $dbxref = db_fetch_object(db_query($dbxsql,$db_id,$accession));
    if(!$dbxref){
       $sql = "

+ 1 - 2
tripal_feature/tripal_feature.api.inc

@@ -255,7 +255,7 @@ function tripal_feature_chado_feature_synonym_schema() {
 }
 
 /**
- * Retrieve properties of a given type for a given analysis featire
+ * Retrieve properties from the analysisfeatureprop table for a feature.
  *
  * @param $analysis_id
  *    The analysis ID for the analysis feature. This argument is optional but
@@ -481,7 +481,6 @@ function tripal_feature_analysis_update_property_by_id($analysisfeatureprop_id,
 function tripal_feature_analysis_delete_property($analysis_id = NULL, $feature_id = NUll, 
    $analysisfeature_id = NULL, $property, $cv_name = 'tripal')
 {
-     
    // check that the incoming arguments are correct
    if(($analysis_id and !$feature_id) or
        (!$analysis_id and $feature_id)){