Stephen Ficklin 9 年之前
父节点
当前提交
1dd0273d3b
共有 1 个文件被更改,包括 64 次插入70 次删除
  1. 64 70
      tripal_cv/api/tripal_cv.api.inc

+ 64 - 70
tripal_cv/api/tripal_cv.api.inc

@@ -422,7 +422,7 @@ function tripal_insert_cv($name, $definition) {
  */
  */
 function tripal_insert_cvterm($term, $options = array()) {
 function tripal_insert_cvterm($term, $options = array()) {
 
 
-  // Set Defaults
+  // Set Defaults.
   if (isset($term['cv_name'])) {
   if (isset($term['cv_name'])) {
     $defaultcv = $term['cv_name'];
     $defaultcv = $term['cv_name'];
   }
   }
@@ -451,7 +451,7 @@ function tripal_insert_cvterm($term, $options = array()) {
     $update = 1;
     $update = 1;
   }
   }
 
 
-  // get the term properties
+  // Get the term properties.
   $id = (isset($term['id'])) ? $term['id'] : '';
   $id = (isset($term['id'])) ? $term['id'] : '';
   $name = '';
   $name = '';
   $cvname = '';
   $cvname = '';
@@ -492,7 +492,7 @@ function tripal_insert_cvterm($term, $options = array()) {
     $id = $name;
     $id = $name;
   }
   }
 
 
-  // get the accession and the database from the cvterm id
+  // Get the accession and the database from the cvterm id.
   if ($dbname) {
   if ($dbname) {
     $accession = $id;
     $accession = $id;
   }
   }
@@ -502,7 +502,8 @@ function tripal_insert_cvterm($term, $options = array()) {
     $dbname = preg_replace('/^(.+?):.*$/', '\1', $id);
     $dbname = preg_replace('/^(.+?):.*$/', '\1', $id);
   }
   }
 
 
-  // check that we have a database name, give a different message if it's a relationship
+  // Check that we have a database name, give a different message if it's a
+  // relationship.
   if ($is_relationship and !$dbname) {
   if ($is_relationship and !$dbname) {
     tripal_report_error('tripal_cv', TRIPAL_WARNING, "A database name is not provided for this relationship term: $id", NULL);
     tripal_report_error('tripal_cv', TRIPAL_WARNING, "A database name is not provided for this relationship term: $id", NULL);
     return 0;
     return 0;
@@ -512,7 +513,7 @@ function tripal_insert_cvterm($term, $options = array()) {
     return 0;
     return 0;
   }
   }
 
 
-  // make sure the CV name exists
+  // Make sure the CV name exists
   $cv = tripal_get_cv(array('name' => $cvname));
   $cv = tripal_get_cv(array('name' => $cvname));
   if (!$cv) {
   if (!$cv) {
     $cv = tripal_insert_cv($cvname, '');
     $cv = tripal_insert_cv($cvname, '');
@@ -522,10 +523,10 @@ function tripal_insert_cvterm($term, $options = array()) {
     return 0;
     return 0;
   }
   }
 
 
-  // this SQL statement will be used a lot to find a cvterm so just set it
+  // 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
   // here for easy reference below.  Because CV terms can change their names
   // but accessions don't change, the following SQL finds cvterms based on
   // but accessions don't change, the following SQL finds cvterms based on
-  // their accession rather than the name
+  // their accession rather than the name.
   $cvtermsql = "
   $cvtermsql = "
     SELECT CVT.name, CVT.cvterm_id, CV.cv_id, CV.name as cvname,
     SELECT CVT.name, CVT.cvterm_id, CV.cv_id, CV.name as cvname,
       DB.name as dbname, DB.db_id, DBX.accession
       DB.name as dbname, DB.db_id, DBX.accession
@@ -536,18 +537,19 @@ function tripal_insert_cvterm($term, $options = array()) {
     WHERE DBX.accession = :accession and DB.name = :name
     WHERE DBX.accession = :accession and DB.name = :name
   ";
   ";
 
 
-  // add the database. The function will just return the DB object if the
+  // Add the database. The function will just return the DB object if the
   // database already exists.
   // database already exists.
   $db = tripal_get_db(array('name' => $dbname));
   $db = tripal_get_db(array('name' => $dbname));
   if (!$db) {
   if (!$db) {
     $db = tripal_insert_db(array('name' => $dbname));
     $db = tripal_insert_db(array('name' => $dbname));
   }
   }
   if (!$db) {
   if (!$db) {
-    tripal_report_error('tripal_cv', TRIPAL_WARNING, "Cannot find database '$dbname' in Chado.", NULL);
+    tripal_report_error('tripal_cv', TRIPAL_WARNING,
+      "Cannot find database '$dbname' in Chado.", NULL);
     return 0;
     return 0;
   }
   }
 
 
-  // the cvterm table has two unique dependencies. We need to check both.
+  // The cvterm table has two unique dependencies. We need to check both.
   // first check the (name, cv_id, is_obsolete) constraint
   // first check the (name, cv_id, is_obsolete) constraint
   $values = array(
   $values = array(
     'name' => $name,
     'name' => $name,
@@ -556,70 +558,63 @@ function tripal_insert_cvterm($term, $options = array()) {
       'name' => $cvname,
       'name' => $cvname,
     ),
     ),
   );
   );
-  $options = array('statement_name' => 'sel_cvterm_c1');
-  $result = chado_select_record('cvterm', array('*'), $values, $options);
-
-  // if the constraint is met then let's check it to see if
-  // the database name matches the one we have been provided
+  $result = chado_select_record('cvterm', array('*'), $values);
   if (count($result) == 1) {
   if (count($result) == 1) {
     $cvterm = $result[0];
     $cvterm = $result[0];
 
 
-    // get the dbxref record
+    // Get the dbxref record.
     $values = array('dbxref_id' => $cvterm->dbxref_id);
     $values = array('dbxref_id' => $cvterm->dbxref_id);
-    $options = array('statement_name' => 'sel_dbxref_id');
-    $result = chado_select_record('dbxref', array('*'), $values, $options);
+    $result = chado_select_record('dbxref', array('*'), $values);
     $dbxref = $result[0];
     $dbxref = $result[0];
 
 
-    // get the db
+    // Get the db.
     $values = array('db_id' => $dbxref->db_id);
     $values = array('db_id' => $dbxref->db_id);
-    $options = array('statement_name' => 'sel_db_id');
-    $result = chado_select_record('db', array('*'), $values, $options);
+    $result = chado_select_record('db', array('*'), $values);
     $db_check = $result[0];
     $db_check = $result[0];
 
 
-    // the database name for this existing term does not match that of the
-    // one provided to this function.  The CV name matches otherwise we
-    // wouldn't have made it this far. So, let's swap the database for
-    // this term
-    if ($db_check->name != $db->name) {
-
-      // look to see if the correct dbxref record already exists for this database
-      $values = array(
-        'db_id' => $db->db_id,
-        'accession' => $accession,
-      );
-      $options = array('statement_name' => 'sel_dbxref_idac');
-      $result = chado_select_record('dbxref', array('*'), $values, $options);
-
-      // if we already have a good dbxref then we want to update our cvterm
-      // to use this dbxref
-      if (count($result) > 0) {
-        $dbxref = $result[0];
-        $match = array('cvterm_id' => $cvterm->cvterm_id);
-        $values = array('dbxref_id' => $dbxref->dbxref_id);
-        $options = array('statement_name' => 'upd_cvterm_db');
-        $success = chado_update_record('cvterm', $match, $values, $options);
-        if (!$success) {
-          tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to correct the dbxref id for the cvterm " .
-            "'$name' (id: $accession), for database $dbname", NULL);
-          return 0;
-        }
-      }
-      // if we don't have the record then we want to delete our cvterm and let the code
-      // below recreate it with the correct info
-      else {
-        $match = array('cvterm_id' => $cvterm->cvterm_id);
-        $options = array('statement_name' => 'del_cvterm_cv');
-        chado_delete_record('cvterm', $match, $options);
-      }
-    }
-
-    // check that the accession matches.  Sometimes an OBO can define the same term
+//     // The database name for this existing term does not match that of the
+//     // one provided to this function.  The CV name matches otherwise we
+//     // wouldn't have made it this far. So, let's swap the database for
+//     // this term
+//     if ($db_check->name != $db->name) {
+
+//       // Look to see if the correct dbxref record already exists for this
+//       // database.
+//       $values = array(
+//         'db_id' => $db->db_id,
+//         'accession' => $accession,
+//       );
+//       $result = chado_select_record('dbxref', array('*'), $values);
+
+//       // If we already have a good dbxref then we want to update our cvterm
+//       // to use this dbxref.
+//       if (count($result) > 0) {
+//         $dbxref = $result[0];
+//         $match = array('cvterm_id' => $cvterm->cvterm_id);
+//         $values = array('dbxref_id' => $dbxref->dbxref_id);
+//         $success = chado_update_record('cvterm', $match, $values);
+//         if (!$success) {
+//           tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to correct the dbxref id for the cvterm " .
+//             "'$name' (id: $accession), for database $dbname", NULL);
+//           return 0;
+//         }
+//       }
+//       // If we don't have the record then we want to delete our cvterm and let
+//       // the code below recreate it with the correct info.
+//       else {
+//         $match = array('cvterm_id' => $cvterm->cvterm_id);
+//         $options = array('statement_name' => 'del_cvterm_cv');
+//         chado_delete_record('cvterm', $match, $options);
+//       }
+//     }
+
+    // Check that the accession matches.  Sometimes an OBO can define a term
     // multiple times but with different accessions.  If this is the case we
     // multiple times but with different accessions.  If this is the case we
     // can't do an insert or it will violate the constraint in the cvterm table.
     // can't do an insert or it will violate the constraint in the cvterm table.
-    // so we'll need to add the record to the cvterm_dbxref table instead
+    // So we'll need to add the record to the cvterm_dbxref table instead.
     if ($dbxref->accession != $accession) {
     if ($dbxref->accession != $accession) {
 
 
-      // get/add the dbxref fort his term
+      // Get/add the dbxref for his term.
       $dbxref_new =  tripal_insert_dbxref(array(
       $dbxref_new =  tripal_insert_dbxref(array(
         'db_id' => $db->db_id,
         'db_id' => $db->db_id,
         'accession' => $accession
         'accession' => $accession
@@ -630,7 +625,7 @@ function tripal_insert_cvterm($term, $options = array()) {
         return 0;
         return 0;
       }
       }
 
 
-      // check to see if the cvterm_dbxref record already exists
+      // Check to see if the cvterm_dbxref record already exists.
       $values = array(
       $values = array(
         'cvterm_id' => $cvterm->cvterm_id,
         'cvterm_id' => $cvterm->cvterm_id,
         'dbxref_id' => $dbxref_new->dbxref_id,
         'dbxref_id' => $dbxref_new->dbxref_id,
@@ -658,16 +653,16 @@ function tripal_insert_cvterm($term, $options = array()) {
       return $cvterm;
       return $cvterm;
     }
     }
 
 
-    // continue on, we've fixed the record if the db_id did not match,
-    // we can now perform and updated if we need to.
+    // Continue on, we've fixed the record if the db_id did not match.
+    // We can now perform and updated if we need to.
   }
   }
 
 
-  // get the CVterm record
+  // 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' => $accession, ':name' => $dbname));
   $cvterm = $result->fetchObject();
   $cvterm = $result->fetchObject();
   if (!$cvterm) {
   if (!$cvterm) {
 
 
-    // check to see if the dbxref exists if not, add it
+    // check to see if the dbxref exists if not, add it.
     $dbxref =  tripal_insert_dbxref(array(
     $dbxref =  tripal_insert_dbxref(array(
       'db_id' => $db->db_id,
       'db_id' => $db->db_id,
       'accession' => $accession
       'accession' => $accession
@@ -678,8 +673,7 @@ function tripal_insert_cvterm($term, $options = array()) {
       return 0;
       return 0;
     }
     }
 
 
-    // check to see if the dbxref already has an entry in the cvterm table
-    // this is the second constraint in the cvterm table
+    // Check to see if the dbxref already has an entry in the cvterm table.
     $values = array('dbxref_id' => $dbxref->dbxref_id);
     $values = array('dbxref_id' => $dbxref->dbxref_id);
     $options = array('statement_name' => 'sel_cvterm_db');
     $options = array('statement_name' => 'sel_cvterm_db');
     $check = chado_select_record('cvterm', array('cvterm_id'), $values, $options);
     $check = chado_select_record('cvterm', array('cvterm_id'), $values, $options);
@@ -706,7 +700,7 @@ function tripal_insert_cvterm($term, $options = array()) {
         }
         }
       }
       }
     }
     }
-    // this dbxref already exists in the cvterm table
+    // This dbxref already exists in the cvterm table.
     else {
     else {
       tripal_report_error('tripal_cv', TRIPAL_WARNING, "The dbxref already exists for another cvterm record: $name (cv: " . $cvname . " db: $dbname)", NULL);
       tripal_report_error('tripal_cv', TRIPAL_WARNING, "The dbxref already exists for another cvterm record: $name (cv: " . $cvname . " db: $dbname)", NULL);
       return 0;
       return 0;
@@ -714,7 +708,7 @@ function tripal_insert_cvterm($term, $options = array()) {
     $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
     $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
     $cvterm = $result->fetchObject();
     $cvterm = $result->fetchObject();
   }
   }
-  // upate the cvterm
+  // Update the cvterm.
   elseif ($update) {
   elseif ($update) {
     $match = array('cvterm_id' => $cvterm->cvterm_id);
     $match = array('cvterm_id' => $cvterm->cvterm_id);
     $upd_values = array(
     $upd_values = array(