Bläddra i källkod

API Update: updated all modules to use the db API

Lacey Sanderson 10 år sedan
förälder
incheckning
c39b529262

+ 26 - 23
tripal_core/api/tripal_core.chado_nodes.dbxrefs.api.inc

@@ -128,7 +128,7 @@ function chado_add_node_form_dbxrefs(&$form, &$form_state, $details) {
   $details['fieldset_title'] = 'Additional Database References';
   $details['additional_instructions'] = '';
 
-  // Get the list of databases to display in the dropdown. 
+  // Get the list of databases to display in the dropdown.
   if (isset($details['select_options'])) {
     // the callee has provided a list
     $db_options = $details['select_options'];
@@ -199,9 +199,9 @@ function chado_add_node_form_dbxrefs(&$form, &$form_state, $details) {
   }
   else {
     $existing_dbxrefs = chado_query(
-      "SELECT 
+      "SELECT
          db.name as db_name, db.db_id as db_id,
-         dbxref.dbxref_id, dbxref.accession as accession, 
+         dbxref.dbxref_id, dbxref.accession as accession,
          dbxref.description as description, dbxref.version
        FROM {dbxref} dbxref
          LEFT JOIN {db} db ON db.db_id = dbxref.db_id
@@ -243,14 +243,14 @@ function chado_add_node_form_dbxrefs(&$form, &$form_state, $details) {
    * an element to the form for each one.
    */
   foreach ($existing_dbxrefs as $dbxref) {
-    
+
     if (array_key_exists($dbxref->db_id, $db_options)) {
 
-      /* Since the dbxref version is part of the unique constraint, when it is 
-       * empty we need to use something in the key to indicate this case. Otherwise, 
-       * you wouldn't be able to select those elements from the array 
-       * (ie: $form['addtl_dbxrefs']['dbxref_table'][9999][''] doesn't work as 
-       * expected whereas $form['addtl_dbxrefs']['dbxref_table'][9999][NONE] 
+      /* Since the dbxref version is part of the unique constraint, when it is
+       * empty we need to use something in the key to indicate this case. Otherwise,
+       * you wouldn't be able to select those elements from the array
+       * (ie: $form['addtl_dbxrefs']['dbxref_table'][9999][''] doesn't work as
+       * expected whereas $form['addtl_dbxrefs']['dbxref_table'][9999][NONE]
        * is much better)
        */
       $version = (!empty($dbxref->version)) ? $dbxref->version : 'NONE';
@@ -434,13 +434,13 @@ function chado_add_node_form_dbxrefs_add_button_submit(&$form, &$form_state) {
   $key = $dbxref['db_id'] . '-' . $version;
   $form_state['chado_additional_dbxrefs'][$key] = (object) $dbxref;
 
-  
+
   // we don't want the new element to pick up the values from the previous element so wipe them out
   unset($form_state['input']['dbxref_table']['new']['db']);
   unset($form_state['input']['dbxref_table']['new']['db_name']);
   unset($form_state['input']['dbxref_table']['new']['dbxref_version']);
   unset($form_state['input']['dbxref_table']['new']['dbxref_accession']);
-  
+
   $form_state['rebuild'] = TRUE;
 }
 
@@ -649,12 +649,12 @@ function chado_update_node_form_dbxrefs($node, $details, $retrieved_dbxrefs = FA
           // If there is no dbxref then we have to create that first
           if ($dbxref_id == 'NONE') {
             $version = ($version == 'NONE') ? '' : $version;
-            $success = tripal_db_add_dbxref(
-              $db_id,
-              $accession,
-              $version,
-              NULL
-            );
+            $success = tripal_insert_dbxref(array(
+              'db_id' => $db_id,
+              'accession' => $accession,
+              'version' => $version,
+              'description' => NULL
+            ));
             if ($success) {
               $dbxref_id = $success->dbxref_id;
             }
@@ -665,12 +665,15 @@ function chado_update_node_form_dbxrefs($node, $details, $retrieved_dbxrefs = FA
 
           // add _dbxref linker
           if ($dbxref_id) {
-            $success_link = tripal_db_add_dbxref_link(
-              $linking_table,
-              $dbxref_id,
-              $foreignkey_name,
-              $foreignkey_value
-            );
+            if (preg_match('/(\w+)_dbxref/',$linking_table,$matches)) {
+              $base_table = $matches[1];
+
+              $success_link = tripal_associate_dbxref(
+                $base_table,
+                $foreignkey_value,
+                array('dbxref_id' => $dbxref_id)
+              );
+            }
           }
         }
       }

+ 9 - 3
tripal_cv/api/tripal_cv.api.inc

@@ -550,7 +550,7 @@ function tripal_insert_cvterm($term, $options = array()) {
   // database already exists.
   $db = tripal_get_db(array('name' => $dbname));
   if (!$db) {
-    $db = tripal_db_add_db($dbname);
+    $db = tripal_insert_db(array('name' => $dbname));
   }
   if (!$db) {
     tripal_report_error('tripal_cv', TRIPAL_WARNING, "Cannot find database '$dbname' in Chado.", NULL);
@@ -630,7 +630,10 @@ function tripal_insert_cvterm($term, $options = array()) {
     if ($dbxref->accession != $accession) {
 
       // get/add the dbxref fort his term
-      $dbxref_new =  tripal_db_add_dbxref($db->db_id, $accession);
+      $dbxref_new =  tripal_insert_dbxref(array(
+        'db_id' => $db->db_id,
+        'accession' => $accession
+      ));
       if (!$dbxref_new) {
         tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to find or insert the dbxref record for cvterm, " .
           "$name (id: $accession), for database $dbname", NULL);
@@ -675,7 +678,10 @@ function tripal_insert_cvterm($term, $options = array()) {
   if (!$cvterm) {
 
     // check to see if the dbxref exists if not, add it
-    $dbxref =  tripal_db_add_dbxref($db->db_id, $accession);
+    $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);

+ 69 - 69
tripal_cv/includes/tripal_cv.obo_loader.inc

@@ -15,12 +15,12 @@
 /**
  * Provides the form to load an already existing controlled
  *  Vocabulary into chado
- *  
+ *
  * @param $form
- *   The form array 
+ *   The form array
  * @param $form_state
  *   The form state array
- *   
+ *
  * @return
  *   The form array with new additions
  *
@@ -104,13 +104,13 @@ function tripal_cv_obo_form($form, &$form_state) {
 /**
  * The submit function for the load ontology form. It registers a
  *   tripal job to import the user specified ontology file
- *   
+ *
  * @param $form
- *   The form array 
+ *   The form array
  * @param $form_state
  *   The form state array
- *   
- * 
+ *
+ *
  * @ingroup tripal_obo_loader
  */
 function tripal_cv_obo_form_submit($form, &$form_state) {
@@ -123,15 +123,15 @@ function tripal_cv_obo_form_submit($form, &$form_state) {
 }
 
 /**
- * A wrapper function for importing the user specified OBO file into Chado by 
- * specifying the obo_id of the OBO. It requires that the file be in OBO v1.2 
- * compatible format.  This function is typically executed via the Tripal jobs 
+ * A wrapper function for importing the user specified OBO file into Chado by
+ * specifying the obo_id of the OBO. It requires that the file be in OBO v1.2
+ * compatible format.  This function is typically executed via the Tripal jobs
  * management after a user submits a job via the Load Onotloies form.
- * 
+ *
  * @param $obo_id
  *   An obo_id from the tripal_cv_obo file that specifies which OBO file to import
  * @param $job_id
- *   The job_id of the job from the Tripal jobs management system. 
+ *   The job_id of the job from the Tripal jobs management system.
  *
  * @ingroup tripal_obo_loader
  */
@@ -166,19 +166,19 @@ function tripal_cv_load_obo_v1_2_id($obo_id, $jobid = NULL) {
 }
 
 /**
- * A wrapper function for importing the user specified OBO file into Chado by 
- * specifying the filename and path of the OBO. It requires that the file be in OBO v1.2 
- * compatible format.  This function is typically executed via the Tripal jobs 
+ * A wrapper function for importing the user specified OBO file into Chado by
+ * specifying the filename and path of the OBO. It requires that the file be in OBO v1.2
+ * compatible format.  This function is typically executed via the Tripal jobs
  * management after a user submits a job via the Load Onotloies form.
- * 
+ *
  * @param $obo_name
  *   The name of the OBO (typially the ontology or controlled vocabulary name)
  * @param $file
  *   The path on the file system where the ontology can be found
  * @param $job_id
- *   The job_id of the job from the Tripal jobs management system. 
+ *   The job_id of the job from the Tripal jobs management system.
  * @param $is_new
- *   Set to TRUE if this is a new ontology that does not yet exist in the 
+ *   Set to TRUE if this is a new ontology that does not yet exist in the
  *   tripal_cv_obo table.  If TRUE the OBO will be added to the table.
  *
  * @ingroup tripal_obo_loader
@@ -199,19 +199,19 @@ function tripal_cv_load_obo_v1_2_file($obo_name, $file, $jobid = NULL, $is_new =
 }
 
 /**
- * A wrapper function for importing the user specified OBO file into Chado by 
- * specifying the remote URL of the OBO. It requires that the file be in OBO v1.2 
- * compatible format.  This function is typically executed via the Tripal jobs 
+ * A wrapper function for importing the user specified OBO file into Chado by
+ * specifying the remote URL of the OBO. It requires that the file be in OBO v1.2
+ * compatible format.  This function is typically executed via the Tripal jobs
  * management after a user submits a job via the Load Onotloies form.
- * 
+ *
  * @param $obo_name
  *   The name of the OBO (typially the ontology or controlled vocabulary name)
  * @param $url
  *   The remote URL of the OBO file.
  * @param $job_id
- *   The job_id of the job from the Tripal jobs management system. 
+ *   The job_id of the job from the Tripal jobs management system.
  * @param $is_new
- *   Set to TRUE if this is a new ontology that does not yet exist in the 
+ *   Set to TRUE if this is a new ontology that does not yet exist in the
  *   tripal_cv_obo table.  If TRUE the OBO will be added to the table.
  *
  * @ingroup tripal_obo_loader
@@ -236,7 +236,7 @@ function tripal_cv_load_obo_v1_2_url($obo_name, $url, $jobid = NULL, $is_new = T
   }
   fclose($url_fh);
   fclose($obo_fh);
-  
+
   if ($is_new) {
     tripal_cv_add_obo_ref($obo_name, $url);
   }
@@ -244,7 +244,7 @@ function tripal_cv_load_obo_v1_2_url($obo_name, $url, $jobid = NULL, $is_new = T
   // second, parse the OBO
   $success = tripal_cv_load_obo_v1_2($temp, $jobid, $newcvs);
   if ($success) {
-    
+
     // update the cvtermpath table
     tripal_cv_load_update_cvtermpath($newcvs, $jobid);
     print "Done\n";
@@ -255,15 +255,15 @@ function tripal_cv_load_obo_v1_2_url($obo_name, $url, $jobid = NULL, $is_new = T
 
 /**
  * A function for executing the cvtermpath function of Chado.  This function
- * populates the cvtermpath table of Chado for quick lookup of term 
+ * populates the cvtermpath table of Chado for quick lookup of term
  * relationships
- * 
+ *
  * @param $newcvs
  *   An associative array of controlled vocabularies to update.  The key must be
  *   the name of the vocabulary and the value the cv_id from the cv table of chado.
  * @param $jobid
- *   The job_id of the job from the Tripal jobs management system. 
- * 
+ *   The job_id of the job from the Tripal jobs management system.
+ *
  * @ingroup tripal_obo_loader
  */
 function tripal_cv_load_update_cvtermpath($newcvs, $jobid) {
@@ -276,21 +276,21 @@ function tripal_cv_load_update_cvtermpath($newcvs, $jobid) {
 
 /**
  * Imports a given OBO file into Chado.  This function is usually called by
- * one of three wrapper functions:  tripal_cv_load_obo_v1_2_id, 
+ * one of three wrapper functions:  tripal_cv_load_obo_v1_2_id,
  * tripal_cv_load_obo_v1_2_file or tirpal_cv_load_obo_v1_2_url. But, it can
- * be called directly if the full path to an OBO file is available on the 
+ * be called directly if the full path to an OBO file is available on the
  * file system.
- * 
+ *
  * @param $flie
  *   The full path to the OBO file on the file system
  * @param $jobid
  *   The job_id of the job from the Tripal jobs management system.
  * @param $newcvs
- *   An empty array passed by reference that upon return will contain the list 
+ *   An empty array passed by reference that upon return will contain the list
  *   of newly added vocabularies.  The key will contain the CV name and the
  *   value the new cv_id
- * 
- * 
+ *
+ *
  * @ingroup tripal_obo_loader
  */
 function tripal_cv_load_obo_v1_2($file, $jobid = NULL, &$newcvs) {
@@ -312,10 +312,10 @@ function tripal_cv_load_obo_v1_2($file, $jobid = NULL, &$newcvs) {
     print "Step 1: Preloading File $file\n";
 
     // make sure we have an 'internal' and a '_global' database
-    if (!tripal_db_add_db('internal')) {
+    if (!tripal_insert_db(array('name' => 'internal'))) {
       tripal_cv_obo_quiterror("Cannot add 'internal' database");
     }
-    if (!tripal_db_add_db('_global')) {
+    if (!tripal_insert_db(array('name' => '_global'))) {
       tripal_cv_obo_quiterror("Cannot add '_global' database");
     }
 
@@ -335,8 +335,8 @@ function tripal_cv_load_obo_v1_2($file, $jobid = NULL, &$newcvs) {
     }
     // if the 'default-namespace' is missing
     else {
-      
-      // look to see if an 'ontology' key is present.  It is part of the v1.4 
+
+      // look to see if an 'ontology' key is present.  It is part of the v1.4
       // specification so it shouldn't be in the file, but just in case
       if (array_key_exists('ontology', $header)) {
         $defaultcv = tripal_cv_add_cv(strtoupper($header['ontology'][0]), '');
@@ -349,7 +349,7 @@ function tripal_cv_load_obo_v1_2($file, $jobid = NULL, &$newcvs) {
         $defaultcv = tripal_cv_add_cv('_global', '');
         $newcvs['_global'] = $defaultcv->cv_id;
       }
-      watchdog('t_obo_loader', "This OBO is missing the 'default-namespace' header. It is not possible to determine which vocabulary terms without a 'namespace' key should go.  Instead, those terms will be placed in the '%vocab' vocabulary.", 
+      watchdog('t_obo_loader', "This OBO is missing the 'default-namespace' header. It is not possible to determine which vocabulary terms without a 'namespace' key should go.  Instead, those terms will be placed in the '%vocab' vocabulary.",
         array('%vocab' => $defaultcv->name), WATCHDOG_WARNING);
     }
 
@@ -376,10 +376,10 @@ function tripal_cv_load_obo_v1_2($file, $jobid = NULL, &$newcvs) {
 
 /**
  * Immediately terminates loading of the OBO file.
- * 
+ *
  * @param $message
  *   The error message to present to the user
- *   
+ *
  * @ingroup tripal_obo_loader
  */
 function tripal_cv_obo_quiterror($message) {
@@ -390,11 +390,11 @@ function tripal_cv_obo_quiterror($message) {
 }
 
 /**
- * OBO files are divided into a typedefs terms section and vocabulary terms section.  
+ * OBO files are divided into a typedefs terms section and vocabulary terms section.
  * This function loads the typedef terms from the OBO.
- * 
+ *
  * @param $defaultcv
- *   A database object containing a record from the cv table for the 
+ *   A database object containing a record from the cv table for the
  *   default controlled vocabulary
  * @param $newcvs
  *   An associative array of controlled vocabularies for this OBO.  The key must be
@@ -403,7 +403,7 @@ function tripal_cv_obo_quiterror($message) {
  *   The name of the default database.
  * @param $jobid
  *   The job_id of the job from the Tripal jobs management system.
- *   
+ *
  * @ingroup tripal_obo_loader
  */
 function tripal_cv_obo_load_typedefs($defaultcv, $newcvs, $default_db, $jobid) {
@@ -453,11 +453,11 @@ function tripal_cv_obo_load_typedefs($defaultcv, $newcvs, $default_db, $jobid) {
 }
 
 /**
- * OBO files are divided into a typedefs section and a terms section.  This 
+ * OBO files are divided into a typedefs section and a terms section.  This
  * function loads the typedef terms from the OBO.
- * 
+ *
  * @param $defaultcv
- *   A database object containing a record from the cv table for the 
+ *   A database object containing a record from the cv table for the
  *   default controlled vocabulary
  * @param $jobid
  *  The job_id of the job from the Tripal jobs management system.
@@ -527,18 +527,18 @@ function tripal_cv_obo_process_terms($defaultcv, $jobid = NULL, &$newcvs, $defau
 }
 
 /**
- * Uses the provided term array to add/update information to Chado about the 
+ * Uses the provided term array to add/update information to Chado about the
  * term including the term, dbxref, synonyms, properties, and relationships.
- * 
+ *
  * @param $term
- *   An array representing the cvterm. 
+ *   An array representing the cvterm.
  * @param $defaultcv
  *   The name of the default controlled vocabulary
  * @is_relationship
  *   Set to 1 if this term is a relationship term
  * @default_db
  *   The name of the default database.
- * 
+ *
  * @ingroup tripal_obo_loader
  */
 function tripal_cv_obo_process_term($term, $defaultcv, $is_relationship = 0, &$newcvs, $default_db) {
@@ -547,7 +547,7 @@ function tripal_cv_obo_process_term($term, $defaultcv, $is_relationship = 0, &$n
   if (!array_key_exists('namespace', $term) and !($defaultcv or $defaultcv == '')) {
     tripal_cv_obo_quiterror("Cannot add the term: no namespace defined. " . $term['id'][0]);
   }
-  
+
   // construct the term array for sending to the tripal_cv_add_cvterm function
   // for adding a new cvterm
   $t = array();
@@ -705,15 +705,15 @@ function tripal_cv_obo_process_term($term, $defaultcv, $is_relationship = 0, &$n
 
 /**
  * Adds a cvterm relationship
- * 
+ *
  * @param $cvterm
- *   A database object for the cvterm 
+ *   A database object for the cvterm
  * @param $rel
  *   The relationship name
  * @param $objname
  *   The relationship term name
  * @param $defaultcv
- *   A database object containing a record from the cv table for the 
+ *   A database object containing a record from the cv table for the
  *   default controlled vocabulary
  * @object_is_relationship
  *   Set to 1 if this term is a relationship term
@@ -799,10 +799,10 @@ function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $rel,
 
 /**
  * Retreives the term array from the temp loading table for a given term id.
- * 
+ *
  * @param id
  *   The id of the term to retrieve
- *   
+ *
  * @ingroup tripal_obo_loader
  */
 function tripal_cv_obo_get_term($id) {
@@ -817,12 +817,12 @@ function tripal_cv_obo_get_term($id) {
 
 /**
  * Adds the synonyms to a term
- * 
+ *
  * @param term
  *   An array representing the cvterm. It must have a 'synonym' key/value pair.
  * @param cvterm
  *   The database object of the cvterm to which the synonym will be added.
- * 
+ *
  * @ingroup tripal_obo_loader
  */
 function tripal_cv_obo_add_synonyms($term, $cvterm) {
@@ -917,13 +917,13 @@ function tripal_cv_obo_add_synonyms($term, $cvterm) {
  * Parse the OBO file and populate the templ loading table
  *
  * @param $file
- *   The path on the file system where the ontology can be found 
+ *   The path on the file system where the ontology can be found
  * @param $header
  *   An array passed by reference that will be populated with the header
  *   information from the OBO file
  * @param $jobid
  *   The job_id of the job from the Tripal jobs management system.
- * 
+ *
  * @ingroup tripal_obo_loader
  */
 function tripal_cv_obo_parse($obo_file, &$header, $jobid) {
@@ -1048,7 +1048,7 @@ function tripal_cv_obo_parse($obo_file, &$header, $jobid) {
 
 /**
  * Adds a database reference to a cvterm
- * 
+ *
  * @param cvterm
  *   The database object of the cvterm to which the synonym will be added.
  * @param xref
@@ -1076,7 +1076,7 @@ function tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref) {
   }
 
   // add the database
-  $db = tripal_db_add_db($dbname);
+  $db = tripal_insert_db(array('name' => $dbname));
   if (!$db) {
     tripal_cv_obo_quiterror("Cannot find database '$dbname' in Chado.");
   }
@@ -1111,12 +1111,12 @@ function tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref) {
 
 /**
  * Adds a property to a cvterm
- * 
+ *
  * @param cvterm
  *   A database object for the cvterm to which properties will be added
  * @param $property
  *   The name of the property to add
- * @param $value 
+ * @param $value
  *   The value of the property
  * @param rank
  *   The rank of the property
@@ -1187,7 +1187,7 @@ function tripal_cv_obo_add_cvterm_prop($cvterm, $property, $value, $rank) {
 
 /**
  * Adds a database cross reference to a cvterm
- * 
+ *
  * @param db_id
  *   The database ID of the cross reference
  * @param accession

+ 2 - 2
tripal_db/api/tripal_db.api.inc

@@ -21,7 +21,7 @@
     $select_values = array(
       'name' => 'SOFP'
     );
-    $db_object = tripal_db_get_db($select_values);
+    $db_object = tripal_get_db($select_values);
  * @endcode
  *  The above code selects the SOFP db and returns the following object:
  * @code
@@ -159,7 +159,7 @@ function tripal_get_db_select_options() {
         'name' => 'SOFP'
       )
     );
-    $dbxref_object = tripal_db_get_dbxref($identifiers);
+    $dbxref_object = tripal_get_dbxref($identifiers);
  * @endcode
  *  The above code selects the synonym database reference and returns the following object:
  * @code

+ 5 - 5
tripal_example/tripal_example.install

@@ -208,14 +208,14 @@ function tripal_example_add_mviews() {
  * @ingroup tripal_example
  */
 function tripal_example_add_dbs() {
-  // EXPLANATION: use the tripal_db_add_db() function to add any
+  // EXPLANATION: use the tripal_insert_db() function to add any
   // external databases needed by your module.  If the database already
   // exists then the function will gracefully return.
 
-  tripal_db_add_db(
-    'example_db',
-    'An example database.'
-  );
+  tripal_insert_db(array(
+    'name' => 'example_db',
+    'description' => 'An example database.'
+  ));
 }
 /**
  * Add cvs related to publications

+ 17 - 2
tripal_pub/tripal_pub.install

@@ -86,8 +86,23 @@ function tripal_pub_uninstall() {
  */
 function tripal_pub_enable() {
   // make sure we have our supported databases
-  tripal_db_add_db('PMID', 'PubMed', 'http://www.ncbi.nlm.nih.gov/pubmed', 'http://www.ncbi.nlm.nih.gov/pubmed/', TRUE);
-  tripal_db_add_db('AGL', 'USDA National Agricultural Library', 'http://agricola.nal.usda.gov/', '', TRUE);
+  tripal_insert_db(
+    array(
+      'name' => 'PMID',
+      'description' => 'PubMed',
+      'url' => 'http://www.ncbi.nlm.nih.gov/pubmed',
+      'urlprefix' => 'http://www.ncbi.nlm.nih.gov/pubmed/'
+    ),
+    array('update_existing' => TRUE)
+  );
+  tripal_insert_db(
+    array(
+      'name' => 'AGL',
+      'description' => 'USDA National Agricultural Library',
+      'url' => 'http://agricola.nal.usda.gov/'
+    ),
+    array('update_existing' => TRUE)
+  );
 
   variable_set('tripal_pub_supported_dbs', array('PMID', 'AGL'));
 }

+ 1 - 1
tripal_stock/includes/tripal_stock.chado_node.inc

@@ -277,7 +277,7 @@ function chado_stock_form($node, $form_state) {
     '#description' => t('Optionally enter a description about the database accession.')
   );
 
-  $db_options = tripal_db_get_db_options();
+  $db_options = tripal_get_db_select_options();
   $db_options[0] = 'Select a Database';
   $form['database_reference']['database'] = array(
     '#type' => 'select',