|  | @@ -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));
 |