|
@@ -524,9 +524,9 @@ function tripal_insert_cvterm($term, $options = array()) {
|
|
|
}
|
|
|
|
|
|
// This SQL statement will be used a lot to find a cvterm so just set it
|
|
|
- // 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.
|
|
|
+ // here for easy reference below. The following SQL tries to cover both the following:
|
|
|
+ // 1) The cvterm name may have changed but the accession remains constant
|
|
|
+ // 2) The cvterm name remains constant but the daccession/db may have changed (rare).
|
|
|
$cvtermsql = "
|
|
|
SELECT CVT.name, CVT.cvterm_id, CV.cv_id, CV.name as cvname,
|
|
|
DB.name as dbname, DB.db_id, DBX.accession
|
|
@@ -534,7 +534,7 @@ function tripal_insert_cvterm($term, $options = array()) {
|
|
|
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 DBX.accession = :accession and DB.name = :name
|
|
|
+ WHERE (DBX.accession = :accession and DB.name = :dbname) OR (CVT.name = :term and CV.name = :cvname)
|
|
|
";
|
|
|
|
|
|
// Add the database. The function will just return the DB object if the
|
|
@@ -648,7 +648,7 @@ function tripal_insert_cvterm($term, $options = array()) {
|
|
|
}
|
|
|
}
|
|
|
// get the original cvterm with the same name and return that.
|
|
|
- $result = chado_query($cvtermsql, array(':accession' => $dbxref->accession, ':name' => $dbname));
|
|
|
+ $result = chado_query($cvtermsql, array(':accession' => $dbxref->accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname));
|
|
|
$cvterm = $result->fetchObject();
|
|
|
return $cvterm;
|
|
|
}
|
|
@@ -658,7 +658,7 @@ function tripal_insert_cvterm($term, $options = array()) {
|
|
|
}
|
|
|
|
|
|
// Get the CVterm record. If it doesn't exist then create it.
|
|
|
- $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
|
|
|
+ $result = chado_query($cvtermsql, array(':accession' => $dbxref->accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname));
|
|
|
$cvterm = $result->fetchObject();
|
|
|
if (!$cvterm) {
|
|
|
|
|
@@ -705,11 +705,13 @@ function tripal_insert_cvterm($term, $options = array()) {
|
|
|
tripal_report_error('tripal_cv', TRIPAL_WARNING, "The dbxref already exists for another cvterm record: $name (cv: " . $cvname . " db: $dbname)", NULL);
|
|
|
return 0;
|
|
|
}
|
|
|
- $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
|
|
|
+ $result = chado_query($cvtermsql, array(':accession' => $dbxref->accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname));
|
|
|
$cvterm = $result->fetchObject();
|
|
|
}
|
|
|
// Update the cvterm.
|
|
|
elseif ($update) {
|
|
|
+
|
|
|
+ // First, basic update of the term.
|
|
|
$match = array('cvterm_id' => $cvterm->cvterm_id);
|
|
|
$upd_values = array(
|
|
|
'name' => $name,
|
|
@@ -723,7 +725,43 @@ function tripal_insert_cvterm($term, $options = array()) {
|
|
|
tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to update the term: $name", NULL);
|
|
|
return 0;
|
|
|
}
|
|
|
- $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
|
|
|
+
|
|
|
+ // Second, check that the dbxref has not changed and if it has then update it.
|
|
|
+ $checksql = "
|
|
|
+ SELECT cvterm_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 DBX.accession = :accession and DB.name = :dbname and CVT.name = :term and CV.name = :cvname
|
|
|
+ ";
|
|
|
+ $check = chado_query($checksql, array(':accession' => $dbxref->accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname))->fetchObject();
|
|
|
+ if (!$check) {
|
|
|
+
|
|
|
+ // check to see if the dbxref exists if not, add it.
|
|
|
+ $dbxref = tripal_insert_dbxref(array(
|
|
|
+ 'db_id' => $db->db_id,
|
|
|
+ 'accession' => $accession
|
|
|
+ ));
|
|
|
+ if (!$dbxref) {
|
|
|
+ tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to find or insert the dbxref record for cvterm, " .
|
|
|
+ "$name (id: $accession), for database $dbname", NULL);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $match = array('cvterm_id' => $cvterm->cvterm_id);
|
|
|
+ $upd_values = array(
|
|
|
+ 'dbxref_id' => $dbxref->dbxref_id,
|
|
|
+ );
|
|
|
+ $success = chado_update_record('cvterm', $match, $upd_values);
|
|
|
+ if (!$success) {
|
|
|
+ tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to update the term $name with new accession $db:$accession", NULL);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Finally grab the updated details.
|
|
|
+ $result = chado_query($cvtermsql, array(':accession' => $dbxref->accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname));
|
|
|
$cvterm = $result->fetchObject();
|
|
|
}
|
|
|
else {
|
|
@@ -1111,4 +1149,4 @@ function tripal_get_cvterm_default_select_options($table, $field, $field_desc) {
|
|
|
}
|
|
|
|
|
|
return $options;
|
|
|
-}
|
|
|
+}
|