|
@@ -114,7 +114,7 @@ function tripal_cv_load_update_cvtermpath($newcvs, $jobid) {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
+ * Add the obo to the tripal_cv_obo table in the Drupal database
|
|
|
*/
|
|
|
function tripal_cv_load_obo_add_ref($name, $path) {
|
|
|
$isql = "INSERT INTO {tripal_cv_obo} (name,path) VALUES ('%s','%s')";
|
|
@@ -126,29 +126,30 @@ function tripal_cv_load_obo_add_ref($name, $path) {
|
|
|
* @ingroup tripal_obo_loader
|
|
|
*/
|
|
|
function tripal_cv_load_obo_v1_2($file, $jobid = NULL, &$newcvs) {
|
|
|
- global $_tripal_cv_previous_db;
|
|
|
+
|
|
|
+ // we need to get a persistent connection. If one exists this function
|
|
|
+ // will not recreate it, but if not it will create one and store it in
|
|
|
+ // a Drupal variable for reuse later.
|
|
|
+ $connection = tripal_db_persistent_chado();
|
|
|
|
|
|
$header = array();
|
|
|
$obo = array();
|
|
|
|
|
|
- print "Opening File $file\n";
|
|
|
-
|
|
|
- // set the search path
|
|
|
- $_tripal_cv_previous_db = tripal_db_set_active('chado');
|
|
|
+ print "Opening File $file\n";
|
|
|
|
|
|
// make sure we have an 'internal' and a '_global' database
|
|
|
- if (!tripal_cv_obo_add_db('internal')) {
|
|
|
+ if (!tripal_db_add_db('internal')) {
|
|
|
tripal_cv_obo_quiterror("Cannot add 'internal' database");
|
|
|
}
|
|
|
- if (!tripal_cv_obo_add_db('_global')) {
|
|
|
+ if (!tripal_db_add_db('_global')) {
|
|
|
tripal_cv_obo_quiterror("Cannot add '_global' database");
|
|
|
}
|
|
|
|
|
|
// parse the obo file
|
|
|
- tripal_cv_obo_parse($file, $obo, $header);
|
|
|
+ $default_db = tripal_cv_obo_parse($file, $obo, $header);
|
|
|
|
|
|
// add the CV for this ontology to the database
|
|
|
- $defaultcv = tripal_cv_obo_add_cv($header['default-namespace'][0], '');
|
|
|
+ $defaultcv = tripal_cv_add_cv($header['default-namespace'][0], '');
|
|
|
if (!$defaultcv) {
|
|
|
tripal_cv_obo_quiterror('Cannot add namespace ' . $header['default-namespace'][0]);
|
|
|
}
|
|
@@ -157,16 +158,21 @@ function tripal_cv_load_obo_v1_2($file, $jobid = NULL, &$newcvs) {
|
|
|
// add any typedefs to the vocabulary first
|
|
|
$typedefs = $obo['Typedef'];
|
|
|
foreach ($typedefs as $typedef) {
|
|
|
- tripal_cv_obo_process_term($typedef, $defaultcv, $obo, 1, $newcvs);
|
|
|
- }
|
|
|
-
|
|
|
+ $t = array();
|
|
|
+ $t['id'] = $typedef['id'][0];
|
|
|
+ $t['name'] = $typedef['name'][0];
|
|
|
+ $t['def'] = $typedef['def'][0];
|
|
|
+ $t['subset'] = $typedef['subset'][0];
|
|
|
+ tripal_cv_obo_process_term($t, $defaultcv->name, $obo, 1, $newcvs, $default_db);
|
|
|
+ }
|
|
|
+
|
|
|
// next add terms to the vocabulary
|
|
|
$terms = $obo['Term'];
|
|
|
- if (!tripal_cv_obo_process_terms($terms, $defaultcv, $obo, $jobid, $newcvs)) {
|
|
|
+ if (!tripal_cv_obo_process_terms($terms, $defaultcv->name, $obo, $jobid, $newcvs, $default_db)) {
|
|
|
tripal_cv_obo_quiterror('Cannot add terms from this ontology');
|
|
|
}
|
|
|
|
|
|
- return tripal_cv_obo_loader_done();
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -175,8 +181,7 @@ function tripal_cv_load_obo_v1_2($file, $jobid = NULL, &$newcvs) {
|
|
|
*/
|
|
|
function tripal_cv_obo_quiterror($message) {
|
|
|
|
|
|
- print "ERROR: $message\n";
|
|
|
- db_query("set search_path to public");
|
|
|
+ watchdog("T_obo_loader",$message, array(), WATCHDOG_ERROR);;
|
|
|
exit;
|
|
|
|
|
|
}
|
|
@@ -185,20 +190,7 @@ function tripal_cv_obo_quiterror($message) {
|
|
|
*
|
|
|
* @ingroup tripal_obo_loader
|
|
|
*/
|
|
|
-function tripal_cv_obo_loader_done() {
|
|
|
-
|
|
|
- // return the search path to normal
|
|
|
- tripal_db_set_active($_tripal_cv_previous_db);
|
|
|
- db_query("set search_path to public");
|
|
|
-
|
|
|
- return '';
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- *
|
|
|
- * @ingroup tripal_obo_loader
|
|
|
- */
|
|
|
-function tripal_cv_obo_process_terms($terms, $defaultcv, $obo, $jobid = NULL, &$newcvs) {
|
|
|
+function tripal_cv_obo_process_terms($terms, $defaultcv, $obo, $jobid = NULL, &$newcvs, $default_db) {
|
|
|
|
|
|
$i = 0;
|
|
|
$count = sizeof($terms);
|
|
@@ -207,20 +199,39 @@ function tripal_cv_obo_process_terms($terms, $defaultcv, $obo, $jobid = NULL, &$
|
|
|
$interval = 1;
|
|
|
}
|
|
|
|
|
|
+ // iterate through each term from the OBO file and add it
|
|
|
+ print "Loading terms...\n";
|
|
|
foreach ($terms as $term) {
|
|
|
|
|
|
// update the job status every 1% terms
|
|
|
if ($jobid and $i % $interval == 0) {
|
|
|
- tripal_job_set_progress($jobid, intval(($i / $count) * 50)); // we mulitply by 50 because parsing and loacing cvterms
|
|
|
- // is only the first half. The other half is updating
|
|
|
- } // the cvtermpath table.
|
|
|
-
|
|
|
- if (!tripal_cv_obo_process_term($term, $defaultcv, $obo, 0, $newcvs)) {
|
|
|
+ $complete = ($i / $count) * 100;
|
|
|
+ tripal_job_set_progress($jobid, intval($complete));
|
|
|
+ printf("%d of %d records. (%0.2f%%) memory: %d\r", $i, $count, $complete, memory_get_usage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // add/update this term
|
|
|
+ $t = array();
|
|
|
+ $t['id'] = $term['id'][0];
|
|
|
+ $t['name'] = $term['name'][0];
|
|
|
+ $t['def'] = $term['def'][0];
|
|
|
+ $t['subset'] = $term['subset'][0];
|
|
|
+ $t['namespace'] = $term['namespace'][0];
|
|
|
+ $t['is_obsolete'] = $term['is_obsolete'][0];
|
|
|
+ if (!tripal_cv_obo_process_term($t, $defaultcv, $obo, 0, $newcvs, $default_db)) {
|
|
|
tripal_cv_obo_quiterror("Failed to process terms from the ontology");
|
|
|
}
|
|
|
|
|
|
$i++;
|
|
|
}
|
|
|
+
|
|
|
+ // set the final status
|
|
|
+ if ($jobid) {
|
|
|
+ $complete = ($i / $count) * 100;
|
|
|
+ tripal_job_set_progress($jobid, intval($complete));
|
|
|
+ printf("%d of %d records. (%0.2f%%) memory: %d\r", $i, $count, $complete, memory_get_usage());
|
|
|
+ }
|
|
|
+
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
@@ -228,15 +239,15 @@ function tripal_cv_obo_process_terms($terms, $defaultcv, $obo, $jobid = NULL, &$
|
|
|
*
|
|
|
* @ingroup tripal_obo_loader
|
|
|
*/
|
|
|
-function tripal_cv_obo_process_term($term, $defaultcv, $obo, $is_relationship = 0, &$newcvs) {
|
|
|
+function tripal_cv_obo_process_term($term, $defaultcv, $obo, $is_relationship = 0, &$newcvs, $default_db) {
|
|
|
|
|
|
// add the cvterm
|
|
|
- $cvterm = tripal_cv_obo_add_cvterm($term, $defaultcv, $is_relationship, 1);
|
|
|
+ $cvterm = tripal_cv_add_cvterm($term, $defaultcv, $is_relationship, 1, $default_db);
|
|
|
if (!$cvterm) {
|
|
|
- tripal_cv_obo_quiterror("Cannot add the term " . $term['id'][0]);
|
|
|
+ tripal_cv_obo_quiterror("Cannot add the term " . $term['id']);
|
|
|
}
|
|
|
- if ($term['namespace'][0]) {
|
|
|
- $newcvs[$term['namespace'][0]] = $cvterm->cv_id;
|
|
|
+ if ($term['namespace']) {
|
|
|
+ $newcvs[$term['namespace']] = $cvterm->cv_id;
|
|
|
}
|
|
|
|
|
|
// now handle other properites
|
|
@@ -361,55 +372,6 @@ function tripal_cv_obo_process_term($term, $defaultcv, $obo, $is_relationship =
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
- * Add a database record
|
|
|
- * @ingroup tripal_obo_loader
|
|
|
- */
|
|
|
-function tripal_cv_obo_add_db($dbname) {
|
|
|
-
|
|
|
- $db_sql = "SELECT * FROM {db} WHERE name ='%s'";
|
|
|
- $db = db_fetch_object(db_query($db_sql, $dbname));
|
|
|
- if (!$db) {
|
|
|
- if (!db_query("INSERT INTO {db} (name) VALUES ('%s')", $dbname)) {
|
|
|
- tripal_cv_obo_quiterror("Cannot create '$dbname' db in Chado.");
|
|
|
- }
|
|
|
- $db = db_fetch_object(db_query($db_sql, $dbname));
|
|
|
- }
|
|
|
-
|
|
|
- return $db;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Add a controlled vocab record
|
|
|
- * @ingroup tripal_obo_loader
|
|
|
- */
|
|
|
-function tripal_cv_obo_add_cv($name, $comment) {
|
|
|
-
|
|
|
- // see if the CV (default-namespace) exists already in the database
|
|
|
- $vocab = $name;
|
|
|
- $remark = $comment;
|
|
|
- $cv_sql = "SELECT * FROM {cv} WHERE name = '%s'";
|
|
|
- $cv = db_fetch_object(db_query($cv_sql, $vocab));
|
|
|
-
|
|
|
- // if the CV exists then update it, otherwise insert
|
|
|
- if (!$cv) {
|
|
|
- $sql = "INSERT INTO {cv} (name,definition) VALUES ('%s','%s')";
|
|
|
- if (!db_query($sql, $vocab, $remark)) {
|
|
|
- tripal_cv_obo_quiterror("Failed to create the CV record");
|
|
|
- }
|
|
|
- $cv = db_fetch_object(db_query($cv_sql, $vocab));
|
|
|
- }
|
|
|
- else {
|
|
|
- $sql = "UPDATE {cv} SET definition = '%s' WHERE name ='%s'";
|
|
|
- if (!db_query($sql, $remark, $vocab)) {
|
|
|
- tripal_cv_obo_quiterror("Failed to update the CV record");
|
|
|
- }
|
|
|
- $cv = db_fetch_object(db_query($cv_sql, $vocab));
|
|
|
- }
|
|
|
-
|
|
|
- return $cv;
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
* Add a cvterm relationship
|
|
|
*
|
|
@@ -424,7 +386,7 @@ function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, $rel, $objnam
|
|
|
'definition' => array(''),
|
|
|
'is_obsolete' => array(0),
|
|
|
);
|
|
|
- $relcvterm = tripal_cv_obo_add_cvterm($term, $defaultcv, 1, 0);
|
|
|
+ $relcvterm = tripal_cv_add_cvterm($term, $defaultcv, 1, 0);
|
|
|
if (!$relcvterm) {
|
|
|
tripal_cv_obo_quiterror("Cannot find or insert the relationship term: $rel\n");
|
|
|
}
|
|
@@ -434,17 +396,25 @@ function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, $rel, $objnam
|
|
|
if (!$objterm) {
|
|
|
tripal_cv_obo_quiterror("Could not find object term $objname\n");
|
|
|
}
|
|
|
- $objcvterm = tripal_cv_obo_add_cvterm($objterm, $defaultcv, $object_is_relationship, 1);
|
|
|
+ $objcvterm = tripal_cv_add_cvterm($objterm, $defaultcv, $object_is_relationship, 1);
|
|
|
if (!$objcvterm) {
|
|
|
tripal_cv_obo_quiterror("Cannot add/find cvterm");
|
|
|
}
|
|
|
|
|
|
// check to see if the cvterm_relationship already exists, if not add it
|
|
|
- $cvrsql = "SELECT * FROM {cvterm_relationship} WHERE type_id = %d and subject_id = %d and object_id = %d";
|
|
|
- if (!db_fetch_object(db_query($cvrsql, $relcvterm->cvterm_id, $cvterm->cvterm_id, $objcvterm->cvterm_id))) {
|
|
|
+ $values = array(
|
|
|
+ 'type_id' => $relcvterm->cvterm_id,
|
|
|
+ 'subject_id' => $cvterm->cvterm_id,
|
|
|
+ 'object_id' => $objcvterm->cvterm_id
|
|
|
+ );
|
|
|
+ $options = array('statement_name' => 'sel_cvtermrelationship_tysuob');
|
|
|
+ $result = tripal_core_chado_select('cvterm_relationship', array('*'), $values, $options);
|
|
|
+ if (count($result) == 0) {
|
|
|
+ $options = array('statement_name' => 'ins_cvtermrelationship_tysuob');
|
|
|
$sql = "INSERT INTO {cvterm_relationship} ".
|
|
|
"(type_id,subject_id,object_id) VALUES (%d,%d,%d)";
|
|
|
- if (!db_query($sql, $relcvterm->cvterm_id, $cvterm->cvterm_id, $objcvterm->cvterm_id)) {
|
|
|
+ $success = tripal_core_chado_insert('cvterm_relationship', $values, $options);
|
|
|
+ if (!$success) {
|
|
|
tripal_cv_obo_quiterror("Cannot add term relationship: '$cvterm->name' $rel '$objcvterm->name'");
|
|
|
}
|
|
|
}
|
|
@@ -476,32 +446,28 @@ function tripal_cv_obo_get_term($obo, $id) {
|
|
|
function tripal_cv_obo_add_synonyms($term, $cvterm) {
|
|
|
|
|
|
// make sure we have a 'synonym_type' vocabulary
|
|
|
- $sql = "SELECT * FROM {cv} WHERE name='synonym_type'";
|
|
|
- $syncv = db_fetch_object(db_query($sql));
|
|
|
- if (!$syncv) {
|
|
|
- $sql = "INSERT INTO {cv} (name,definition) VALUES ('synonym_type','')";
|
|
|
- if (!db_query($sql)) {
|
|
|
- tripal_cv_obo_quiterror("Failed to add the synonyms type vocabulary");
|
|
|
- }
|
|
|
- $syncv = db_fetch_object(db_query($sql));
|
|
|
- }
|
|
|
+ $syncv = tripal_cv_add_cv('synonym_type');
|
|
|
|
|
|
// now add the synonyms
|
|
|
if (isset($term['synonym'])) {
|
|
|
foreach ($term['synonym'] as $synonym) {
|
|
|
+
|
|
|
// separate out the synonym definition and the synonym type
|
|
|
$def = preg_replace('/^\s*"(.*)"\s*.*$/', '\1', $synonym);
|
|
|
$type = drupal_strtolower(preg_replace('/^.*"\s+(.*?)\s+.*$/', '\1', $synonym));
|
|
|
|
|
|
// make sure the synonym type exists in the 'synonym_type' vocabulary
|
|
|
- $cvtsql = "
|
|
|
- SELECT *
|
|
|
- FROM {cvterm} CVT
|
|
|
- INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
|
|
|
- WHERE CVT.name = '%s' and CV.name = '%s'
|
|
|
- ";
|
|
|
- $syntype = db_fetch_object(db_query($cvtsql, $type, 'synonym_type'));
|
|
|
- if (!$syntype) {
|
|
|
+ $values = array(
|
|
|
+ 'name' => $type,
|
|
|
+ 'cv_id' => array(
|
|
|
+ 'name' => 'synonym_type',
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ $options = array('statement_name' => 'sel_cvterm_nacv');
|
|
|
+ $results = tripal_core_chado_select('cvterm', array('*'), $values, $options);
|
|
|
+
|
|
|
+ // if it doesn't exist then add it
|
|
|
+ if (count($results) == 0) {
|
|
|
// build a 'term' object so we can add the missing term
|
|
|
$term = array(
|
|
|
'name' => array($type),
|
|
@@ -509,23 +475,28 @@ function tripal_cv_obo_add_synonyms($term, $cvterm) {
|
|
|
'definition' => array(''),
|
|
|
'is_obsolete' => array(0),
|
|
|
);
|
|
|
- $syntype = tripal_cv_obo_add_cvterm($term, $syncv, 0, 1);
|
|
|
+ $syntype = tripal_cv_add_cvterm($term, $syncv, 0, 1);
|
|
|
if (!$syntype) {
|
|
|
tripal_cv_obo_quiterror("Cannot add synonym type: internal:$type");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// make sure the synonym doesn't already exists
|
|
|
- $sql = "
|
|
|
- SELECT *
|
|
|
- FROM {cvtermsynonym}
|
|
|
- WHERE cvterm_id = %d and synonym = '%s'
|
|
|
- ";
|
|
|
- $syn = db_fetch_object(db_query($sql, $cvterm->cvterm_id, $def));
|
|
|
- if (!$syn) {
|
|
|
- $sql = "INSERT INTO {cvtermsynonym} (cvterm_id,synonym,type_id)
|
|
|
- VALUES(%d,'%s',%d)";
|
|
|
- if (!db_query($sql, $cvterm->cvterm_id, $def, $syntype->cvterm_id)) {
|
|
|
+ $values = array(
|
|
|
+ 'cvterm_id' => $cvterm->cvterm_id,
|
|
|
+ 'synonym' => $def
|
|
|
+ );
|
|
|
+ $options = array('statement_name' => 'sel_cvtermsynonym_cvsy');
|
|
|
+ $results = tripal_core_chado_select('cvtermsynonym', array('*'), $values, $options);
|
|
|
+ if (count($results) == 0) {
|
|
|
+ $values = array(
|
|
|
+ 'cvterm_id' => $cvterm->cvterm_id,
|
|
|
+ 'synonym' => $def,
|
|
|
+ 'type_id' => $syntype->cvterm_id
|
|
|
+ );
|
|
|
+ $options = array('statement_name' => 'ins_cvtermsynonym_cvsy');
|
|
|
+ $success = tripal_core_chado_insert('cvtermsynonym', $values, $options);
|
|
|
+ if (!$success) {
|
|
|
tripal_cv_obo_quiterror("Failed to insert the synonym for term: $name ($def)");
|
|
|
}
|
|
|
}
|
|
@@ -559,6 +530,7 @@ function tripal_cv_obo_parse($obo_file, &$obo, &$header) {
|
|
|
$i = 0;
|
|
|
$in_header = 1;
|
|
|
$stanza = array();
|
|
|
+ $default_db = '_global';
|
|
|
|
|
|
// iterate through the lines in the OBO file and parse the stanzas
|
|
|
$fh = fopen($obo_file, 'r');
|
|
@@ -605,6 +577,13 @@ function tripal_cv_obo_parse($obo_file, &$obo, &$header) {
|
|
|
$pair = explode(":", $line, 2);
|
|
|
$tag = $pair[0];
|
|
|
$value = ltrim(rtrim($pair[1]));// remove surrounding spaces
|
|
|
+
|
|
|
+ // look for the default DB
|
|
|
+ $matches = array();
|
|
|
+ if ($tag == 'id' and preg_match('/^(.+?):.*$/', $value, $matches)) {
|
|
|
+ $default_db = $matches[1];
|
|
|
+ }
|
|
|
+
|
|
|
$tag = preg_replace("/\|-\|-\|/", "\:", $tag); // return the escaped colon
|
|
|
$value = preg_replace("/\|-\|-\|/", "\:", $value);
|
|
|
if ($in_header) {
|
|
@@ -632,6 +611,7 @@ function tripal_cv_obo_parse($obo_file, &$obo, &$header) {
|
|
|
array_merge($obo[$type][$stanza['id'][0]], $stanza);
|
|
|
}
|
|
|
}
|
|
|
+ return $default_db;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -658,8 +638,8 @@ function tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref) {
|
|
|
$dbname = 'URL';
|
|
|
}
|
|
|
|
|
|
- // check to see if the database exists
|
|
|
- $db = tripal_cv_obo_add_db($dbname);
|
|
|
+ // add the database
|
|
|
+ $db = tripal_db_add_db($dbname);
|
|
|
if (!$db) {
|
|
|
tripal_cv_obo_quiterror("Cannot find database '$dbname' in Chado.");
|
|
|
}
|
|
@@ -690,7 +670,7 @@ function tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref) {
|
|
|
function tripal_cv_obo_add_cvterm_prop($cvterm, $property, $value, $rank) {
|
|
|
|
|
|
// make sure the 'cvterm_property_type' CV exists
|
|
|
- $cv = tripal_cv_obo_add_cv('cvterm_property_type', '');
|
|
|
+ $cv = tripal_cv_add_cv('cvterm_property_type', '');
|
|
|
if (!$cv) {
|
|
|
tripal_cv_obo_quiterror("Cannot add/find cvterm_property_type cvterm");
|
|
|
}
|
|
@@ -709,7 +689,7 @@ function tripal_cv_obo_add_cvterm_prop($cvterm, $property, $value, $rank) {
|
|
|
'definition' => array(''),
|
|
|
'is_obsolete' => array(0),
|
|
|
);
|
|
|
- $cvproptype = tripal_cv_obo_add_cvterm($term, $cv, 0, 0);
|
|
|
+ $cvproptype = tripal_cv_add_cvterm($term, $cv, 0, 0);
|
|
|
if (!$cvproptype) {
|
|
|
tripal_cv_obo_quiterror("Cannot add cvterm property: internal:$property");
|
|
|
}
|
|
@@ -733,163 +713,6 @@ function tripal_cv_obo_add_cvterm_prop($cvterm, $property, $value, $rank) {
|
|
|
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
- *
|
|
|
- * @ingroup tripal_obo_loader
|
|
|
- */
|
|
|
-function tripal_cv_obo_add_cvterm($term, $defaultcv, $is_relationship = 0, $update = 1) {
|
|
|
-
|
|
|
- // get the term properties
|
|
|
- $id = $term['id'][0];
|
|
|
- $name = $term['name'][0];
|
|
|
- $cvname = $term['namespace'][0];
|
|
|
- $definition = preg_replace('/^\"(.*)\"/', '\1', $term['def'][0]);
|
|
|
- $is_obsolete = 0;
|
|
|
- if (isset($term['is_obsolete'][0]) and strcmp($term['is_obsolete'][0], 'true') == 0) {
|
|
|
- $is_obsolete = 1;
|
|
|
- }
|
|
|
- if (!$cvname) {
|
|
|
- $cvname = $defaultcv->name;
|
|
|
- }
|
|
|
-
|
|
|
- // make sure the CV name exists
|
|
|
- $cv = tripal_cv_obo_add_cv($cvname, '');
|
|
|
- if (!$cv) {
|
|
|
- tripal_cv_obo_quiterror("Cannot find namespace '$cvname' when adding/updating $id");
|
|
|
- }
|
|
|
-
|
|
|
- // this SQL statement will be used a lot to find a cvterm so just set it
|
|
|
- // here for easy reference below.
|
|
|
- $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'";
|
|
|
-
|
|
|
- // get the accession and the database from the cvterm
|
|
|
- if (preg_match('/^.+?:.*$/', $id)) {
|
|
|
- $accession = preg_replace('/^.+?:(.*)$/', '\1', $id);
|
|
|
- $dbname = preg_replace('/^(.+?):.*$/', '\1', $id);
|
|
|
- }
|
|
|
- if ($is_relationship and !$dbname) {
|
|
|
- $accession = $id;
|
|
|
- // 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'));
|
|
|
- if ($cvterm) {
|
|
|
- return $cvterm;
|
|
|
- }
|
|
|
- else {
|
|
|
- // next check if this term is in the _global ontology. If it is then
|
|
|
- // return it no matter what the original CV
|
|
|
- $dbname = '_global';
|
|
|
-
|
|
|
- $cvterm = db_fetch_object(db_query($cvtermsql, $name, $dbname));
|
|
|
- if ($cvterm) {
|
|
|
- return $cvterm;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (!$is_relationship and !$dbname) {
|
|
|
- tripal_cv_obo_quiterror("A database identifier is missing from the term: $id");
|
|
|
- }
|
|
|
-
|
|
|
- // check to see if the database exists.
|
|
|
- $db = tripal_cv_obo_add_db($dbname);
|
|
|
- if (!$db) {
|
|
|
- tripal_cv_obo_quiterror("Cannot find database '$dbname' in Chado.");
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // if the cvterm doesn't exist then add it otherwise just update it
|
|
|
- $cvterm = db_fetch_object(db_query($cvtermsql, $name, $dbname));
|
|
|
- if (!$cvterm) {
|
|
|
- // check to see if the dbxref exists if not, add it
|
|
|
- $dbxref = tripal_cv_obo_add_dbxref($db->db_id, $accession);
|
|
|
- if (!$dbxref) {
|
|
|
- tripal_cv_obo_quiterror("Failed to find or insert the dbxref record for cvterm, $name (id: $accession), for database $dbname");
|
|
|
- }
|
|
|
-
|
|
|
- // 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 = "
|
|
|
- INSERT INTO {cvterm} (cv_id, name, definition, dbxref_id,
|
|
|
- is_obsolete, is_relationshiptype)
|
|
|
- VALUES (%d,'%s','%s',%d,%d,%d)
|
|
|
- ";
|
|
|
- if (!db_query($sql, $cv->cv_id, $name, $definition,
|
|
|
- $dbxref->dbxref_id, $is_obsolete, $is_relationship)) {
|
|
|
- if (!$is_relationship) {
|
|
|
- tripal_cv_obo_quiterror("Failed to insert the term: $name ($dbname)");
|
|
|
- }
|
|
|
- else {
|
|
|
- tripal_cv_obo_quiterror("Failed to insert the relationship term: $name (cv: " . $cvname . " db: $dbname)");
|
|
|
- }
|
|
|
- }
|
|
|
- if (!$is_relationship) {
|
|
|
- print "Added CV term: $name ($dbname)\n";
|
|
|
- }
|
|
|
- else {
|
|
|
- print "Added relationship CV term: $name ($dbname)\n";
|
|
|
- }
|
|
|
- }
|
|
|
- elseif ($check and strcmp($check->name, $name)!=0) {
|
|
|
- // this dbxref_id alrady exists in the database but the name is
|
|
|
- // different. We will trust that the OBO is correct and that there
|
|
|
- // has been a name change for this dbxref, so we'll update
|
|
|
- $sql = "
|
|
|
- UPDATE {cvterm} SET name = '%s', definition = '%s',
|
|
|
- is_obsolete = %d, is_relationshiptype = %d
|
|
|
- WHERE dbxref_id = %d
|
|
|
- ";
|
|
|
- if (!db_query($sql, $name, $definition, $is_obsolete, $is_relationship, $dbxref->dbxref_id)) {
|
|
|
- if (!$is_relationship) {
|
|
|
- tripal_cv_obo_quiterror("Failed to update the term: $name ($dbname)");
|
|
|
- }
|
|
|
- else {
|
|
|
- tripal_cv_obo_quiterror("Failed to update the relationship term: $name (cv: " . $cvname . " db: $dbname)");
|
|
|
- }
|
|
|
- }
|
|
|
- if (!$is_relationship) {
|
|
|
- print "Updated CV term: $name ($dbname)\n";
|
|
|
- }
|
|
|
- else {
|
|
|
- print "Updated relationship CV term: $name ($dbname)\n";
|
|
|
- }
|
|
|
- }
|
|
|
- elseif ($check and strcmp($check->name, $name)==0) {
|
|
|
- // this entry already exists. We're good, so do nothing
|
|
|
- }
|
|
|
- $cvterm = db_fetch_object(db_query($cvtermsql, $name, $dbname));
|
|
|
- }
|
|
|
- elseif ($update) { // update the cvterm
|
|
|
- $sql = "
|
|
|
- UPDATE {cvterm} SET name='%s', definition='%s',
|
|
|
- is_obsolete = %d, is_relationshiptype = %d
|
|
|
- WHERE cvterm_id = %d
|
|
|
- ";
|
|
|
- if (!db_query($sql, $term['name'][0], $definition,
|
|
|
- $is_obsolete, $is_relationship, $cvterm->cvterm_id)) {
|
|
|
- tripal_cv_obo_quiterror("Failed to update the term: $name");
|
|
|
- }
|
|
|
- $cvterm = db_fetch_object(db_query($cvtermsql, $name, $dbname));
|
|
|
- if (!$is_relationship) {
|
|
|
- print "Updated CV term: $name ($dbname)\n";
|
|
|
- }
|
|
|
- else {
|
|
|
- print "Updated relationship CV term: $name ($dbname)\n";
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // return the cvterm
|
|
|
- return $cvterm;
|
|
|
-}
|
|
|
|
|
|
/**
|
|
|
* Add Database Reference
|