ソースを参照

Merge branch '7.x-2.x' of git.drupal.org:sandbox/spficklin/1337878 into 7.x-2.x

Stephen Ficklin 11 年 前
コミット
f76f9bd48e

+ 60 - 17
tripal_cv/api/tripal_cv.DEPRECATED.inc

@@ -23,7 +23,7 @@ function tripal_cv_get_cv($select_values) {
     )
   );
 
-  return FALSE;
+  return chado_get_cv($select_values);
 }
 
 /**
@@ -45,7 +45,7 @@ function tripal_cv_get_cv_by_name($name) {
     )
   );
 
-  return FALSE;
+  return chado_get_cv(array('name' => $name));
 }
 
 /**
@@ -67,7 +67,7 @@ function tripal_cv_get_cv_by_id($cv_id) {
     )
   );
 
-  return FALSE;
+  return chado_get_cv(array('cv_id' => $id));
 }
 
 /**
@@ -89,7 +89,13 @@ function tripal_cv_get_cv_id($cv_name) {
     )
   );
 
-  return FALSE;
+  $cv = chado_get_cv(array('name' => $cv_name));
+  if (isset($cv->cv_id)) {
+    return $cv->cv_id;
+  }
+  else {
+    return FALSE;
+  }
 }
 
 /**
@@ -111,7 +117,7 @@ function tripal_cv_get_cv_options() {
     )
   );
 
-  return FALSE;
+  return cv_get_select_options();
 }
 
 /**
@@ -133,7 +139,7 @@ function tripal_cv_get_cvterm_by_id($cvterm_id) {
     )
   );
 
-  return FALSE;
+  return chado_get_cvterm(array('cvterm_id' => $cvterm_id));
 }
 
 /**
@@ -155,7 +161,17 @@ function tripal_cv_get_cvterm_by_name($name, $cv_id = NULL, $cv_name = 'tripal')
     )
   );
 
-  return FALSE;
+  $identifiers = array('name' => $name);
+  if (isset($cv_id)) {
+    $identifiers['cv_id'] = $cv_id;
+  }
+  if (isset($cv_name)) {
+    $identifiers['cv'] = array(
+      'name' => $cv_name
+    );
+  }
+
+  return chado_get_cvterm($identifiers);
 }
 
 /**
@@ -177,7 +193,13 @@ function tripal_cv_get_cvterm_by_synonym($synonym, $cv_id = NULL, $cv_name = 'tr
     )
   );
 
-  return FALSE;
+  return chado_get_cvterm(array(
+    'synonym' => array(
+      'name' => $synonym,
+      'cv_id' => $cv_id,
+      'cv_name' => $cv_name
+    )
+  ));
 }
 
 /**
@@ -199,7 +221,7 @@ function tripal_cv_get_cvterm_options($cv_id = 0) {
     )
   );
 
-  return FALSE;
+  return cvterm_get_select_options($cv_id);
 }
 
 /**
@@ -221,7 +243,7 @@ function tripal_cv_update_cvtermpath($cvid, $job_id = NULL) {
     )
   );
 
-  return FALSE;
+  return chado_update_cvtermpath($cvid, $job_id);
 }
 
 /**
@@ -243,7 +265,7 @@ function tripal_cv_add_cv($name, $definition) {
     )
   );
 
-  return FALSE;
+  return chado_insert_cv($name, $definition);
 }
 
 /**
@@ -265,7 +287,21 @@ function tripal_cv_add_cvterm($term, $defaultcv = '_global', $is_relationship =
     )
   );
 
-  return FALSE;
+  $term['cv_name'] = $defaultcv;
+  $term['db_name'] = $dbname;
+  $term['is_relationship'] = $is_relationship;
+
+  if (isset($term['def'])) {
+    $term['definition'] = $term['def'];
+    unset($term['def']);
+  }
+
+  return chado_insert_cvterm(
+    $term,
+    array(
+      'update_existing' => $update
+    )
+  );
 }
 
 /**
@@ -287,15 +323,22 @@ function tripal_cv_submit_obo_job($obo_id = NULL, $obo_name = NULL, $obo_url = N
     )
   );
 
-  return FALSE;
+  return tripal_submit_obo_job(
+    array(
+      'obo_id' => $obo_id,
+      'name' => $obo_name,
+      'url' => $obo_url,
+      'file' => $obo_file
+    )
+  );
 }
 
 /**
  * @deprecated Restructured API to make naming more readable and consistent.
  * Function was deprecated in Tripal 2.0 and will be removed 2 releases from now.
- * This function has been replaced by chado_insert_obo().
+ * This function has been replaced by tripal_insert_obo().
  *
- * @see chado_insert_obo().
+ * @see tripal_insert_obo().
  */
 function tripal_cv_add_obo_ref($name, $path) {
 
@@ -309,7 +352,7 @@ function tripal_cv_add_obo_ref($name, $path) {
     )
   );
 
-  return FALSE;
+  return tripal_insert_obo($name, $ref);
 }
 
 /**
@@ -331,5 +374,5 @@ function tripal_cv_cvterm_name_autocomplete($cv_id, $string = '') {
     )
   );
 
-  return FALSE;
+  return chado_cvterm_autocomplete($cv_id, $string);
 }

+ 245 - 248
tripal_cv/api/tripal_cv.api.inc

@@ -24,117 +24,90 @@
 /**
  * Retrieves a chado controlled vocabulary object
  *
- * @param $select_values
- *   An array meant to uniquely select a given controlled vocabulary
+ * @param $identifier
+ *   An array with the key stating what the identifier is. Supported keys (only on of the
+ *   following unique keys is required):
+ *    - cv_id: the chado cv.cv_id primary key
+ *    - name: the chado cv.name field (assume unique)
+ * @param $options
+ *   An array of options. Supported keys include:
+ *     - Any keys supported by chado_generate_var(). See that function definition for
+ *       additional details.
+ *
+ * NOTE: the $identifier parameter can really be any array similar to $values passed into
+ *   chado_select_record(). It should fully specify the cv record to be returned.
  *
  * @return
- *   Chado controlled vocabulary object
- *
- * The controlled vocabulary is selected using tripal_core_chado select and as such the
- * $select_values array parameter meant to uniquely identify the controlled vocab to be
- * returned follows the same form as when using tripal_core_chado_select directly.
- *
- * Example Usage:
- * @code
-    $select_values = array(
-      'name' => 'feature_property'
-    );
-    $cv_object = tripal_cv_get_cv($select_values);
- * @endcode
- *  The above code selects the feature_property cv and returns the following object:
- * @code
-    $cv_object = stdClass Object (
-      [cv_id] => 13
-      [name] => feature_property
-      [definition] =>
-    );
- * @endcode
+ *   If unique values were passed in as an identifier then an object describing the cv
+ *   will be returned (will be a chado variable from chado_generate_var()). Otherwise,
+ *   FALSE will be returned.
  *
  * @ingroup tripal_cv_api
  */
-function tripal_cv_get_cv($select_values) {
-
-  $columns = array(
-    'cv_id',
-    'name',
-    'definition',
-  );
-  $results = chado_select_record('cv', $columns, $select_values);
-  if (sizeof($results) == 1) {
-    return $results[0];
-  }
-  elseif (empty($results)) {
-    tripal_report_error('tripal_cv', TRIPAL_WARNING,
-      'tripal_cv_get_cv: No cv matches criteria values:%values',
-      array('%values' => print_r($select_values, TRUE)));
-    return FALSE;
+function chado_get_cv($identifiers, $options = array()) {
+
+  // Error Checking of parameters
+  if (!is_array($identifiers)) {
+    tripal_report_error(
+      'tripal_cv_api',
+      TRIPAL_ERROR,
+      "chado_get_cv: The identifier passed in is expected to be an array with the key
+        matching a column name in the cv table (ie: cv_id or name). You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
   }
-  else {
-    tripal_report_error('tripal_cv', TRIPAL_WARNING,
-      'tripal_cv_get_cv: 2+ cvs match criteria values:%values',
-      array('%values' => print_r($select_values, TRUE)));
+  elseif (empty($identifiers)) {
+    tripal_report_error(
+      'tripal_cv_api',
+      TRIPAL_ERROR,
+      "chado_get_cv: You did not pass in anything to identify the cv you want. The identifier
+        is expected to be an array with the key matching a column name in the cv table
+        (ie: cv_id or name). You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
   }
 
-}
-
-/**
- * Retrieve a cv given the cv name
- *
- * @param $name
- *  The name of the cv to be returned
- * @return
- *   The cv object for the specified CV name
- *
- * @ingroup tripal_cv_api
- */
-function tripal_cv_get_cv_by_name($name) {
-
-  $r = chado_select_record('cv', array('*'), array('name' => $name));
-
-  return $r[0];
-}
-
-/**
- * Retrieve the cv object for the specified CV id
- *
- * NOTE: This function is deprecated.
- * @see tripal_core_chado_generate_vars()
- *
- * @param $cv_id
- *   The unique identifier for the cv to retrieve
- *
- * @return
- *   An object describing the cv
- *
- * @ingroup tripal_cv_api
- */
-function tripal_cv_get_cv_by_id($cv_id) {
-
-  $r = chado_select_record('cv', array('*'), array('cv_id' => $cv_id));
-
-  return $r;
-}
+  // Try to get the cv
+  $cv = chado_generate_var(
+    'cv',
+    $identifiers,
+    $options
+  );
 
-/**
- * Retrieve the cv id for the specified CV by name
- *
- * NOTE: This function is deprecated.
- * @see tripal_core_chado_generate_vars()
- *
- * @param $cv_name
- *   The unique name for the cv to retrieve
- *
- * @return
- *   The numeric cv ID
- *
- * @ingroup tripal_cv_api
- */
-function tripal_cv_get_cv_id($cv_name) {
+  // Ensure the cv is singular. If it's an array then it is not singular
+  if (is_array($cv)) {
+    tripal_report_error(
+      'tripal_cv_api',
+      TRIPAL_ERROR,
+      "chado_get_cv: The identifiers you passed in were not unique. You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
+  }
 
-  $sql = "SELECT cv_id FROM {cv} WHERE name = :name";
-  $cv = chado_query($sql, array(':name' => $cv_name))->fetchObject();
+  // Report an error if $cv is FALSE since then chado_generate_var has failed
+  elseif ($cv === FALSE) {
+    tripal_report_error(
+      'tripal_cv_api',
+      TRIPAL_ERROR,
+      "chado_get_cv: chado_generate_var() failed to return a cv based on the identifiers
+        you passed in. You should check that your identifiers are correct, as well as, look
+        for a chado_generate_var error for additional clues. You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
+  }
 
-  return $cv->cv_id;
+  // Else, as far we know, everything is fine so give them their cv :)
+  else {
+    return $cv;
+  }
 }
 
 /**
@@ -146,7 +119,7 @@ function tripal_cv_get_cv_id($cv_name) {
  *
  * @ingroup tripal_cv_api
  */
-function tripal_cv_get_cv_options() {
+function cv_get_select_options() {
 
   $results = chado_select_record('cv', array('cv_id', 'name'), array());
 
@@ -160,136 +133,129 @@ function tripal_cv_get_cv_options() {
 }
 
 /**
- * Retrieve a chado cvterm object with a given name
- *
- * @param $cvterm_id
- *   the cvterm.cvterm_id
+ * Retrieves a chado controlled vocabulary term object
+ *
+ * @param $identifier
+ *   An array with the key stating what the identifier is. Supported keys (only on of the
+ *   following unique keys is required):
+ *    - cvterm_id: the chado cv.cvterm_id primary key
+ *    - name: the chado cv.name field (assume unique)
+ *    - synonym: an array with 'name' => the name of the synonym of the cvterm you want
+ *        returned; 'cv_id' => the cv_id of the synonym; 'cv_name' => the name of the cv
+ *        of the synonym
+ * @param $options
+ *   An array of options. Supported keys include:
+ *     - Any keys supported by chado_generate_var(). See that function definition for
+ *       additional details.
+ *
+ * NOTE: the $identifier parameter can really be any array similar to $values passed into
+ *   chado_select_record(). It should fully specify the cvterm record to be returned.
  *
  * @return
- *   cvterm array or FALSE on error
+ *   If unique values were passed in as an identifier then an object describing the cvterm
+ *   will be returned (will be a chado variable from chado_generate_var()). Otherwise,
+ *   FALSE will be returned.
  *
  * @ingroup tripal_cv_api
  */
-function tripal_cv_get_cvterm_by_id($cvterm_id) {
-  if (!is_numeric($cvterm_id)) {
-    return FALSE;
+function chado_get_cvterm($identifiers, $options = array()) {
+
+  // Error Checking of parameters
+  if (!is_array($identifiers)) {
+    tripal_report_error(
+      'tripal_cv_api',
+      TRIPAL_ERROR,
+      "chado_get_cvterm: The identifier passed in is expected to be an array with the key
+        matching a column name in the cvterm table (ie: cvterm_id or name). You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
   }
-  $values = array('cvterm_id' => $cvterm_id);
-  $options = array('statement_name' => 'sel_cvterm_id');
-  $r = chado_select_record('cvterm', array('*'), $values, $options);
-  if (!$r) {
-    return FALSE;
+  elseif (empty($identifiers)) {
+    tripal_report_error(
+      'tripal_cv_api',
+      TRIPAL_ERROR,
+      "chado_get_cvterm: You did not pass in anything to identify the cvterm you want. The identifier
+        is expected to be an array with the key matching a column name in the cvterm table
+        (ie: cvterm_id or name). You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
   }
-  return $r[0];
-}
 
-/**
- * Retrieve a chado cvterm object with a given name
- *
- * @param $name
- *   the cvterm.name
- * @param $cv_id
- *   the cv_id of the term you are looking for
- * @param $cv_name
- *   the name of the CV
- *
- * @return
- *   cvterm array or FALSE on error
- *
- * @ingroup tripal_cv_api
- */
-function tripal_cv_get_cvterm_by_name($name, $cv_id = NULL, $cv_name = 'tripal') {
+  // If synonym was passed in, then process this first before calling chado_generate_var()
+  if (isset($identifier['synonym'])) {
+    $synonym = $identifier['synonym']['name'];
 
-  if ($cv_id) {
     $values = array(
-       'name' => $name,
-       'cv_id' => $cv_id,
+       'synonym' => $synonym,
     );
+    $statement = "sel_cvtermsynonym_sy";
+    if (isset($identifier['synonym']['cv_id'])) {
+      $values['cvterm_id'] = array('cv_id' => $identifier['synonym']['cv_id']);
+      $statement = "sel_cvtermsynonym_sycv";
+    }
+    if (isset($identifier['synonym']['cv_name'])) {
+      $values['cvterm_id'] = array('cv_id' => array('name' => $identifier['synonym']['cv_name']));
+      $statement = "sel_cvtermsynonym_sycv";
+    }
     $options = array(
+      'statement_name' => $statement,
       'case_insensitive_columns' => array('name')
     );
-    $r = chado_select_record('cvterm', array('*'), $values, $options);
-  }
-  elseif ($cv_name) {
-    $values = array(
-      'name' => $name,
-      'cv_id' => array(
-        'name' => $cv_name,
-      ),
-    );
-    $options = array('case_insensitive_columns' => array('name'));
-    $r = chado_select_record('cvterm', array('*'), $values, $options);
-  }
-  else {
-    $values = array('name' => $name);
-    $options = array('case_insensitive_columns' => array('name'));
-    $r = chado_select_record('cvterm', array('*'), $values, $options);
-  }
+    $synonym = chado_select_record('cvtermsynonym', array('cvterm_id'), $values, $options);
 
-  if (!$r) {
-    return FALSE;
-  }
-  if (count($r) > 1) {
-    tripal_report_error('tripal_cv', TRIPAL_ERROR,
-      "Cannot find a unique term for the term '%name' in the vocabulary '%cv'. Multiple entries exist for this name",
-      array('%name' => $name, '%cv' => $cv_name ? $cv_name : $cv_id));
-    return FALSE;
-  }
-  if (count($r) == 0) {
-    return FALSE;
-  }
-  return $r[0];
-}
+    // if the synonym doens't exist or more than one record is returned then return false
+    if (count($synonym) == 0) {
+      return FALSE;
+    }
+    if (count($synonym) > 1) {
+      return FALSE;
+    }
 
-/**
- * Retrieve a chado cvterm object with a given name
- *
- * @param $synonym
- *   the synonym of the term
- * @param $cv_id
- *   the cv_id of the term you are looking for
- * @param $cv_name
- *   the name of the CV
- *
- * @return
- *   cvterm object
- *
- * @ingroup tripal_cv_api
- */
-function tripal_cv_get_cvterm_by_synonym($synonym, $cv_id = NULL, $cv_name = 'tripal') {
+    $identifiers = array('cvterm_id' => $synonym[0]->cvterm_id);
+  }
 
-  // first find the CVTerm synonym
-  $values = array(
-     'synonym' => $synonym,
+  // Try to get the cvterm
+  $cvterm = chado_generate_var(
+    'cvterm',
+    $identifiers,
+    $options
   );
-  $statement = "sel_cvtermsynonym_sy";
-  if ($cv_id) {
-    $values['cvterm_id'] = array('cv_id' => $cv_id);
-    $statement = "sel_cvtermsynonym_sycv";
-  }
-  if ($cv_name) {
-    $values['cvterm_id'] = array('cv_id' => array('name' => $cv_name));
-    $statement = "sel_cvtermsynonym_sycv";
-  }
-  $options = array(
-    'statement_name' => $statement,
-    'case_insensitive_columns' => array('name')
-  );
-  $synonym = chado_select_record('cvtermsynonym', array('cvterm_id'), $values, $options);
 
-  // if the synonym doens't exist or more than one record is returned then return false
-  if (count($synonym) == 0) {
-    return FALSE;
+  // Ensure the cvterm is singular. If it's an array then it is not singular
+  if (is_array($cvterm)) {
+    tripal_report_error(
+      'tripal_cv_api',
+      TRIPAL_ERROR,
+      "chado_get_cvterm: The identifiers you passed in were not unique. You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
   }
-  if (count($synonym) > 1) {
-    return FALSE;
+
+  // Report an error if $cvterm is FALSE since then chado_generate_var has failed
+  elseif ($cvterm === FALSE) {
+    tripal_report_error(
+      'tripal_cv_api',
+      TRIPAL_ERROR,
+      "chado_get_cvterm: chado_generate_var() failed to return a cvterm based on the identifiers
+        you passed in. You should check that your identifiers are correct, as well as, look
+        for a chado_generate_var error for additional clues. You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
+  }
+
+  // Else, as far we know, everything is fine so give them their cvterm :)
+  else {
+    return $cvterm;
   }
 
-  // get the cvterm
-  $values = array('cvterm_id' => $synonym[0]->cvterm_id);
-  $options = array('statement_name' => 'sel_cvterm_id');
-  $cvterm = chado_select_record('cvterm', array('*'), $values, $options);
-  return $cvterm[0];
 }
 
 /**
@@ -305,7 +271,7 @@ function tripal_cv_get_cvterm_by_synonym($synonym, $cv_id = NULL, $cv_name = 'tr
  *
  * @ingroup tripal_cv_api
  */
-function tripal_cv_get_cvterm_options($cv_id = 0) {
+function cvterm_get_select_options($cv_id = 0) {
 
   if ($cv_id > 0) {
     $results = chado_select_record('cvterm', array('cvterm_id', 'name'), array('cv_id' => $cv_id));
@@ -337,7 +303,7 @@ function tripal_cv_get_cvterm_options($cv_id = 0) {
  *
  * @ingroup tripal_cv_api
  */
-function tripal_cv_update_cvtermpath($cvid, $job_id = NULL) {
+function chado_update_cvtermpath($cvid, $job_id = NULL) {
   // TODO: need better error checking in this function
 
   // first get the controlled vocabulary name:
@@ -376,7 +342,7 @@ function tripal_cv_update_cvtermpath($cvid, $job_id = NULL) {
  *
  * @ingroup tripal_cv_api
  */
-function tripal_cv_add_cv($name, $definition) {
+function chado_insert_cv($name, $definition) {
 
   // insert/update values
   $ins_values = array(
@@ -415,7 +381,7 @@ function tripal_cv_add_cv($name, $definition) {
 }
 
 /**
- *  Add's a CV term to the cvterm table.
+ *  Add's a controlled vocabulary 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
@@ -431,35 +397,60 @@ function tripal_cv_add_cv($name, $definition) {
  *  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. The CV name 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].  If this field is provided then it overrides what the
- *   value in $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.
- * @param $dbname
- *   In some cases the database name will not be part of the $term['id'] and it
- *   needs to be explicitly set.  Use this argument only if the database name
- *   cannot be specififed in the term ID (e.g. <DB>:<ACCESSION>).
+ *   An associative array with the following keys:
+ *    - id: the term accession. 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.
+ *    - name: the name of the term. usually meant to be human-readable.
+ *    - namespace: the CV name for the term. DEPRECATED. Please use cv_name instead.
+ *    - is_obsolete: is present and set to 1 if the term is defunct
+ *    - definition: the definition of the term
+ *    - cv_name: The CV name 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].  If this field is provided then it
+ *        overrides what the value in $term[namespace]
+ *    - is_relationship: If this term is a relationship term then this value should be 1.
+ *    _ db_name: In some cases the database name will not be part of the $term['id'] and it
+ *        needs to be explicitly set.  Use this argument only if the database name
+ *        cannot be specififed in the term ID (e.g. <DB>:<ACCESSION>).
+ * @param $options
+ *    - update_existing: By default this is TRUE.  If the term exists it is automatically updated.
  *
  * @return
  *   A CVTerm object
  *
  * @ingroup tripal_cv_api
  */
-function tripal_cv_add_cvterm($term, $defaultcv = '_global', $is_relationship = 0,
-  $update = 1, $dbname = 'internal') {
+function chado_insert_cvterm($term, $options) {
+
+  // Set Defaults
+  if (isset($term['cv_name'])) {
+    $defaultcv = $term['cv_name'];
+  }
+  else {
+    $defaultcv = '_global';
+  }
+
+  if (isset($term['is_relationship'])) {
+    $is_relationship = $term['is_relationship'];
+  }
+  else {
+    $is_relationship = 0;
+  }
+
+  if (isset($term['db_name'])) {
+    $dbname = $term['db_name'];
+  }
+  else {
+    $dbname = 'internal';
+  }
+
+  if (isset($options['update_existing'])) {
+    $update = $options['update_existing'];
+  }
+  else {
+    $update = 1;
+  }
 
   // get the term properties
   $id = $term['id'];
@@ -482,8 +473,8 @@ function tripal_cv_add_cvterm($term, $defaultcv = '_global', $is_relationship =
   else {
     $cvname = $defaultcv;
   }
-  if (array_key_exists('def', $term)) {
-    $definition = preg_replace('/^\"(.*)\"/', '\1', $term['def']);
+  if (array_key_exists('definition', $term)) {
+    $definition = preg_replace('/^\"(.*)\"/', '\1', $term['definition']);
   }
   else {
     $definition = '';
@@ -774,25 +765,31 @@ function tripal_cv_add_cvterm($term, $defaultcv = '_global', $is_relationship =
  *
  * @ingroup tripal_cv_api
  */
-function tripal_cv_submit_obo_job($obo_id = NULL, $obo_name = NULL, $obo_url = NULL, $obo_file = NULL) {
+function tripal_submit_obo_job($obo) {
   global $user;
 
-  if ($obo_id) {
+  // Set Defaults
+  $obo['obo_id'] = (isset($obo['obo_id'])) ? $obo['obo_id'] : NULL;
+  $obo['name'] = (isset($obo['name'])) ? $obo['name'] : NULL;
+  $obo['url'] = (isset($obo['url'])) ? $obo['url'] : NULL;
+  $obo['file'] = (isset($obo['file'])) ? $obo['file'] : NULL;
+
+  if ($obo['obo_id']) {
     $sql = "SELECT * FROM {tripal_cv_obo} WHERE obo_id = :obo_id";
-    $obo = db_query($sql, array(':obo_id' => $obo_id))->fetchObject();
+    $obo = db_query($sql, array(':obo_id' => $obo['obo_id']))->fetchObject();
 
-    $args = array($obo_id);
+    $args = array($obo['obo_id']);
     return tripal_add_job("Load OBO $obo->name", 'tripal_cv',
        "tripal_cv_load_obo_v1_2_id", $args, $user->uid);
   }
   else {
-    if ($obo_url) {
-      $args = array($obo_name, $obo_url);
+    if ($obo['url']) {
+      $args = array($obo['name'], $obo['url']);
       return tripal_add_job("Load OBO $obo_name", 'tripal_cv',
         "tripal_cv_load_obo_v1_2_url", $args, $user->uid);
     }
-    elseif ($obo_file) {
-      $args = array($obo_name, $obo_file);
+    elseif ($obo['file']) {
+      $args = array($obo['name'], $obo['file']);
       return tripal_add_job("Load OBO $obo_name", 'tripal_cv',
         "tripal_cv_load_obo_v1_2_file", $args, $user->uid);
     }
@@ -813,7 +810,7 @@ function tripal_cv_submit_obo_job($obo_id = NULL, $obo_name = NULL, $obo_url = N
  *
  * @ingroup tripal_cv_api
  */
-function tripal_cv_add_obo_ref($name, $path) {
+function tripal_insert_obo($name, $path) {
   $record = new stdClass;
   $record->name = $name;
   $record->path = $path;
@@ -835,7 +832,7 @@ function tripal_cv_add_obo_ref($name, $path) {
  *
  * @ingroup tripal_cv_api
  */
-function tripal_cv_cvterm_name_autocomplete($cv_id, $string = '') {
+function chado_cvterm_autocomplete($cv_id, $string = '') {
   $sql = "
     SELECT cvterm_id, name
     FROM {cvterm}

+ 1 - 1
tripal_cv/tripal_cv.module

@@ -16,7 +16,7 @@
 
 require_once 'api/tripal_cv.api.inc';
 require_once 'api/tripal_cv.schema.api.inc';
-//require_once 'apitripal_cv.DEPRECATED.inc';
+require_once 'api/tripal_cv.DEPRECATED.inc';
 
 require_once 'includes/tripal_cv.admin.inc';
 require_once 'includes/tripal_cv.charts.inc';

+ 37 - 9
tripal_db/api/tripal_db.DEPRECATED.inc

@@ -23,7 +23,7 @@ function tripal_db_get_db($select_values) {
     )
   );
 
-  return FALSE;
+  return chado_get_db($select_values);
 }
 
 /**
@@ -45,7 +45,7 @@ function tripal_db_get_db_by_db_id($db_id) {
     )
   );
 
-  return FALSE;
+  return chado_get_db(array('db_id' => $db_id));
 }
 
 /**
@@ -67,7 +67,7 @@ function tripal_db_get_db_by_name($name) {
     )
   );
 
-  return FALSE;
+  return chado_get_db(array('name' => $name));
 }
 
 /**
@@ -89,7 +89,7 @@ function tripal_db_get_db_options() {
     )
   );
 
-  return FALSE;
+  return db_get_select_options();
 }
 
 /**
@@ -111,7 +111,7 @@ function tripal_db_get_dbxref($select_values) {
     )
   );
 
-  return FALSE;
+  return chado_get_dbxref($select_values);
 }
 
 /**
@@ -133,7 +133,15 @@ function tripal_db_get_dbxref_by_accession($accession, $db_id=0) {
     )
   );
 
-  return FALSE;
+  $identifiers = array(
+    'accession' => $accession
+  );
+  if ($db_id > 0) {
+    $identifiers['db'] = array(
+      'db_id' => $db_id
+    );
+  }
+  return chado_get_dbxref($identifiers);
 }
 
 /**
@@ -155,7 +163,17 @@ function tripal_db_add_db($dbname, $description = '', $url = '', $urlprefix = ''
     )
   );
 
-  return FALSE;
+  return chado_insert_db(
+    array(
+      'name' => $dbname,
+      'description' => $description,
+      'url' => $url,
+      'urlprefix' => $urlprefix
+    ),
+    array(
+      'update_existing' => $update
+    )
+  );
 }
 
 /**
@@ -177,7 +195,12 @@ function tripal_db_add_dbxref($db_id, $accession, $version = '', $description =
     )
   );
 
-  return FALSE;
+  return chado_insert_dbxref(array(
+    'db_id' => $db_id,
+    'accession' => $accession,
+    'version' => $version,
+    'description' => $description
+  ));
 }
 
 /**
@@ -199,5 +222,10 @@ function tripal_db_add_dbxref_link($linking_table, $dbxref_id, $foreignkey_name,
     )
   );
 
-  return FALSE;
+  return chado_associate_dbxref(array(
+    'linking_table' => $linking_table,
+    'dbxref_id' => $dbxref_id,
+    'foreignkey_name' => $foreignkey_name,
+    'foreignkey_id' => $foreignkey_id
+  ));
 }

+ 199 - 148
tripal_db/api/tripal_db.api.inc

@@ -14,17 +14,7 @@
  */
 
 /**
- * To retrieve a chado database object
- *
- * @param $select_values
- *   An array meant to uniquely select a given database
- *
- * @return
- *   Chado database object
- *
- * The database is selected using tripal_core_chado select and as such the
- * $select_values array parameter meant to uniquely identify the database to be
- * returned follows the same form as when using tripal_core_chado_select directly.
+ * Retrieves a chado db object
  *
  * Example Usage:
  * @code
@@ -44,75 +34,89 @@
     );
  * @endcode
  *
- * @ingroup tripal_db_api
- */
-function tripal_db_get_db($select_values) {
-
-  $columns = array(
-    'db_id',
-    'name',
-    'description',
-    'urlprefix',
-    'url'
-  );
-  $results = chado_select_record('db', $columns, $select_values);
-  if (sizeof($results) == 1) {
-    return $results[0];
-  }
-  elseif (empty($results)) {
-    tripal_report_error('tripal_db', TRIPAL_WARNING,
-      'tripal_db_get_db: No db matches criteria values:%values',
-      array('%values' => print_r($select_values, TRUE)));
-    return FALSE;
-  }
-  else {
-    tripal_report_error('tripal_db', TRIPAL_WARNING,
-      'tripal_db_get_db: 2+ dbs match criteria values:%values',
-      array('%values' => print_r($select_values, TRUE)));
-  }
-
-}
-
-/**
- * To retrieve a chado db object
- *
- * @param $db_id
- *   db.db_id
- * @return
- *   Chado db object with all fields from the chado db table
+ * @param $identifier
+ *   An array with the key stating what the identifier is. Supported keys (only on of the
+ *   following unique keys is required):
+ *    - db_id: the chado cv.cv_id primary key
+ *    - name: the chado cv.name field (assume unique)
+ * @param $options
+ *   An array of options. Supported keys include:
+ *     - Any keys supported by chado_generate_var(). See that function definition for
+ *       additional details.
  *
- * @ingroup tripal_db_api
- */
-function tripal_db_get_db_by_db_id($db_id) {
-
-  $r = chado_query("SELECT * FROM {db} WHERE db_id = :db_id", array(':db_id' => $db_id ));
-  return $r->fetchObject();
-}
-
-/**
- * To retrieve a chado db object
+ * NOTE: the $identifier parameter can really be any array similar to $values passed into
+ *   chado_select_record(). It should fully specify the db record to be returned.
  *
- * @param $name
- *   db.name
  * @return
- *   chado db object with all fields from the chado db table
+ *   If unique values were passed in as an identifier then an object describing the cv
+ *   will be returned (will be a chado variable from chado_generate_var()). Otherwise,
+ *   an array of objects will be returned.
  *
  * @ingroup tripal_db_api
  */
-function tripal_db_get_db_by_name($name) {
+function chado_get_db($identifiers, $options = array()) {
+
+  // Error Checking of parameters
+  if (!is_array($identifiers)) {
+    tripal_report_error(
+      'tripal_db_api',
+      TRIPAL_ERROR,
+      "chado_get_db: The identifier passed in is expected to be an array with the key
+        matching a column name in the db table (ie: db_id or name). You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
+  }
+  elseif (empty($identifiers)) {
+    tripal_report_error(
+      'tripal_db_api',
+      TRIPAL_ERROR,
+      "chado_get_db: You did not pass in anything to identify the db you want. The identifier
+        is expected to be an array with the key matching a column name in the db table
+        (ie: db_id or name). You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
+  }
 
-  $values = array('name' => $name);
-  $options = array('statement_name' => 'sel_db_na');
+  // Try to get the db
+  $db = chado_generate_var(
+    'db',
+    $identifiers,
+    $options
+  );
 
-  $db = chado_select_record('db', array('*'), $values, $options);
-  if (count($db) == 1) {
-    return $db[0];
+  // Ensure the db is singular. If it's an array then it is not singular
+  if (is_array($db)) {
+    tripal_report_error(
+      'tripal_db_api',
+      TRIPAL_ERROR,
+      "chado_get_db: The identifiers you passed in were not unique. You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
   }
-  if (count($db) == 0) {
-    return FALSE;
+
+  // Report an error if $db is FALSE since then chado_generate_var has failed
+  elseif ($db === FALSE) {
+    tripal_report_error(
+      'tripal_db_api',
+      TRIPAL_ERROR,
+      "chado_get_db: chado_generate_var() failed to return a db based on the identifiers
+        you passed in. You should check that your identifiers are correct, as well as, look
+        for a chado_generate_var error for additional clues. You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
   }
-  if (count($db) > 1) {
-    return FALSE;
+
+  // Else, as far we know, everything is fine so give them their db :)
+  else {
+    return $db;
   }
 }
 
@@ -125,7 +129,7 @@ function tripal_db_get_db_by_name($name) {
  *
  * @ingroup tripal_db_api
  */
-function tripal_db_get_db_options() {
+function db_get_select_options() {
 
   $result = chado_query("SELECT db_id, name FROM {db}");
 
@@ -139,27 +143,17 @@ function tripal_db_get_db_options() {
 }
 
 /**
- * To retrieve a chado database reference object
- *
- * @param $select_values
- *   An array meant to uniquely select a given database reference
- *
- * @return
- *   Chado database reference object
- *
- * The database reference is selected using tripal_core_chado select and as such the
- * $select_values array parameter meant to uniquely identify the database reference to be
- * returned follows the same form as when using tripal_core_chado_select directly.
+ * Retrieves a chado database reference object
  *
  * Example Usage:
  * @code
-    $select_values = array(
+    $identifiers = array(
       'accession' => 'synonym',
       'db_id' => array(
         'name' => 'SOFP'
       )
     );
-    $dbxref_object = tripal_db_get_dbxref($select_values);
+    $dbxref_object = tripal_db_get_dbxref($identifiers);
  * @endcode
  *  The above code selects the synonym database reference and returns the following object:
  * @code
@@ -176,81 +170,108 @@ function tripal_db_get_db_options() {
     );
  * @endcode
  *
+ * @param $identifier
+ *   An array with the key stating what the identifier is. Supported keys (only on of the
+ *   following unique keys is required):
+ *    - dbxref_id: the chado dbxref.dbxref_id primary key
+ *    - accession: the chado dbxref.accession field (assume unique)
+ * @param $options
+ *   An array of options. Supported keys include:
+ *     - Any keys supported by chado_generate_var(). See that function definition for
+ *       additional details.
+ *
+ * NOTE: the $identifier parameter can really be any array similar to $values passed into
+ *   chado_select_record(). It should fully specify the dbxref record to be returned.
+ *
+ * @return
+ *   If unique values were passed in as an identifier then an object describing the dbxref
+ *   will be returned (will be a chado variable from chado_generate_var()). Otherwise,
+ *   FALSE will be returned.
+ *
  * @ingroup tripal_db_api
  */
-function tripal_db_get_dbxref($select_values) {
-
-  $columns = array(
-    'dbxref_id',
-    'db_id',
-    'accession',
-    'description',
-    'version'
-  );
-  $results = chado_select_record('dbxref', $columns, $select_values);
-  if (sizeof($results) == 1) {
-    $dbxref = tripal_db_add_db_to_object(array('db_id' => $results[0]->db_id), $results[0], array());
-    unset($dbxref->db_id);
-
-    return $dbxref;
-  }
-  elseif (empty($results)) {
-    tripal_report_error('tripal_db', TRIPAL_WARNING,
-      'tripal_db_get_dbxref: No dbxref matches criteria values:%values',
-      array('%values' => print_r($select_values, TRUE)));
-    return FALSE;
+function chado_get_dbxref($identifiers, $options = array()) {
+
+  // Error Checking of parameters
+  if (!is_array($identifiers)) {
+    tripal_report_error(
+      'tripal_db_api',
+      TRIPAL_ERROR,
+      "chado_get_dbxref: The identifier passed in is expected to be an array with the key
+        matching a column name in the dbxref table (ie: dbxref_id or name). You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
   }
-  else {
-    tripal_report_error('tripal_db', TRIPAL_WARNING,
-      'tripal_db_get_dbxref: 2+ dbxrefs match criteria values:%values',
-      array('%values' => print_r($select_values, TRUE)));
+  elseif (empty($identifiers)) {
+    tripal_report_error(
+      'tripal_db_api',
+      TRIPAL_ERROR,
+      "chado_get_dbxref: You did not pass in anything to identify the dbxref you want. The identifier
+        is expected to be an array with the key matching a column name in the dbxref table
+        (ie: dbxref_id or name). You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
   }
 
-}
+  // Try to get the dbxref
+  $dbxref = chado_generate_var(
+    'dbxref',
+    $identifiers,
+    $options
+  );
 
-/**
- * Purpose: To retrieve a chado dbxref object with a given accession
- *
- * @param $accession
- *   dbxref.accession
- * @param $db_id
- *   dbxref.db_id
- * @return
- *   chado dbxref object with all fields from the chado dbxref table
- *
- * @ingroup tripal_db_api
- */
-function tripal_db_get_dbxref_by_accession($accession, $db_id=0) {
+  // Ensure the dbxref is singular. If it's an array then it is not singular
+  if (is_array($dbxref)) {
+    tripal_report_error(
+      'tripal_db_api',
+      TRIPAL_ERROR,
+      "chado_get_dbxref: The identifiers you passed in were not unique. You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
+  }
 
-  if (!empty($db_id)) {
-    $sql = "SELECT * FROM {dbxref} WHERE accession = :accession AND db_id = :db_id";
-    $r = chado_query($sql, array(':accession' => $accession, ':db_id' => $db_id));
+  // Report an error if $dbxref is FALSE since then chado_generate_var has failed
+  elseif ($dbxref === FALSE) {
+    tripal_report_error(
+      'tripal_db_api',
+      TRIPAL_ERROR,
+      "chado_get_dbxref: chado_generate_var() failed to return a dbxref based on the identifiers
+        you passed in. You should check that your identifiers are correct, as well as, look
+        for a chado_generate_var error for additional clues. You passed in %identifier.",
+      array(
+        '%identifier'=> print_r($identifiers, TRUE)
+      )
+    );
   }
+
+  // Else, as far we know, everything is fine so give them their dbxref :)
   else {
-    $sql = "SELECT * FROM {dbxref} WHERE accession = :accession";
-    $r = chado_query($sql, array(':accession' => $accession));
+    return $dbxref;
   }
-
-  return $r->fetchObject();
 }
 
 /**
  * Adds a new database to the Chado DB table and returns the DB object.
  *
- * @param $dbname
- *   The name of the database. This name is usually used as the prefix for
- *   CV term accessions
- * @param $description
- *   Optional. A description of the database.  By default no description is required.
- * @param $url
- *   Optional. The URL for the database
- * @param $urlprefix
- *   Optional. The URL that is to be used as a prefix when constructing a link to
- *   a database term
- * @param $update
- *   Optional. Set this to '1' to force an update of the database if it
- *   already exists. The default is to not update. If the database exists
- *   then nothing is added.
+ * @param $values
+ *   An associative array of the values of the db (those to be inserted)
+ *   - name: The name of the database. This name is usually used as the prefix for
+ *     CV term accessions
+ *   - description: (Optional) A description of the database.  By default no description is required.
+ *   - url: (Optional) The URL for the database
+ *   - urlprefix: (Optional) The URL that is to be used as a prefix when constructing a
+ *     link to a database term
+ * @param $options
+ *   An associative array of options including:
+ *   - update_existing: (Optional) Set this to '1' to force an update of the database if it
+ *     already exists. The default is to not update. If the database exists
+ *     then nothing is added.
  *
  * @return
  *   An object populated with fields from the newly added database.  If the
@@ -258,8 +279,14 @@ function tripal_db_get_dbxref_by_accession($accession, $db_id=0) {
  *
  * @ingroup tripal_db_api
  */
-function tripal_db_add_db($dbname, $description = '', $url = '',
-  $urlprefix = '', $update = 0) {
+function chado_insert_db($values, $options) {
+
+  // Default Values
+  $dbname = $values['name'];
+  $description = (isset($values['description'])) ? $values['description'] : '';
+  $url = (isset($values['url'])) ? $values['url'] : '';
+  $urlprefix = (isset($values['urlprefix'])) ? $values['urlprefix'] : '';
+  $update = (isset($options['update_existing'])) ? $options['update_existing'] : TRUE;
 
   // build the values array for inserting/updating
   $ins_values = array(
@@ -303,9 +330,21 @@ function tripal_db_add_db($dbname, $description = '', $url = '',
 /**
  * Add a database reference
  *
+ * @param $values
+ *   An associative array of the values to be inserted including:
+ *    - db_id: the database_id of the database the reference is from
+ *    - accession: the accession
+ *    - version: (Optional) The version of the database reference
+ *    - description: (Optional) A description of the database reference
+ *
  * @ingroup tripal_db_api
  */
-function tripal_db_add_dbxref($db_id, $accession, $version = '', $description = '') {
+function chado_insert_dbxref($values) {
+
+  $db_id = $values['db_id'];
+  $accession = $values['accession'];
+  $version = (isset($values['version'])) ? $values['version'] : '';
+  $description = (isset($values['description'])) ? $values['description'] : '';
 
   $ins_values = array(
     'db_id'       => $db_id,
@@ -343,9 +382,21 @@ function tripal_db_add_dbxref($db_id, $accession, $version = '', $description =
 /**
  * Add a record to a database reference linking table (ie: feature_dbxref)
  *
+ * @param: $values
+ *   An associative array describing the link
+ *    - linking_table:
+ *    - dbxref_id:
+ *    - foreignkey_name:
+ *    - foreignkey_id:
+ *
  * @ingroup tripal_db_api
  */
-function tripal_db_add_dbxref_link($linking_table, $dbxref_id, $foreignkey_name, $foreignkey_id) {
+function chado_associate_dbxref($values) {
+
+  $linking_table = $values['linking_table'];
+  $dbxref_id = $values['dbxref_id'];
+  $foreignkey_name = $values['foreignkey_name'];
+  $foreignkey_id = $values['foreignkey_id'];
 
   $values = array(
     'dbxref_id' => $dbxref_id,

+ 1 - 1
tripal_db/tripal_db.module

@@ -5,7 +5,7 @@
  */
 
 require_once 'api/tripal_db.api.inc';
-//require_once 'api/tripal_db.DEPRECATED.inc';
+require_once 'api/tripal_db.DEPRECATED.inc';
 
 require_once 'includes/tripal_db.admin.inc';