Tripal 0.3b
tripal_cv/tripal_cv.api.inc
Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * @defgroup tripal_cv_api CV Module API
00005  * @ingroup tripal_api
00006  * @ingroup tripal_cv
00007  * This module provides a set of functions to simplify working with
00008  * controlled vocabularies.  Most of the API functions deal with retrieving
00009  * terms or their parent vocabularies.  
00010  * 
00011  * However, the API also supports
00012  * generation of trees for browsing a vocabulary as well as generation of 
00013  * pie graphs for display of hierarchical counts of terms.  Version 0.3b of
00014  * Tripal provides a feature browser and a feature summary chart uses
00015  * the API functions provided here.  But in general charts and trees can be
00016  * created for any controlled vocabulary.
00017  *
00018  */
00019 
00020 /**
00021  * Purpose: To retrieve a chado controlled vocabulary object
00022  *
00023  * @param $select_values
00024  *   An array meant to uniquely select a given controlled vocabulary
00025  *
00026  * @return
00027  *   Chado controlled vocabulary object
00028  *
00029  * The controlled vocabulary is selected using tripal_core_chado select and as such the
00030  * $select_values array parameter meant to uniquely identify the controlled vocab to be 
00031  * returned follows the same form as when using tripal_core_chado_select directly.
00032  *
00033  * Example Usage:
00034  * @code
00035     $select_values = array(
00036       'name' => 'feature_property'
00037     );
00038     $cv_object = tripal_cv_get_cv($select_values);
00039  * @endcode
00040  *  The above code selects the feature_property cv and returns the following object:
00041  * @code
00042     $cv_object = stdClass Object ( 
00043       [cv_id] => 13
00044       [name] => feature_property
00045       [definition] => 
00046     ); 
00047  * @endcode
00048  *
00049  * @ingroup tripal_cv_api
00050  */
00051 function tripal_cv_get_cv ($select_values) {
00052 
00053   $columns = array(
00054     'cv_id', 
00055     'name', 
00056     'definition', 
00057   );
00058   $results = tripal_core_chado_select('cv', $columns, $select_values);
00059   if (sizeof($results) == 1) {
00060     return $results[0];
00061   } elseif (empty($results)) {
00062     watchdog('tripal_cv', 
00063       'tripal_cv_get_cv: No cv matches criteria values:%values',
00064       array('%values' => print_r($select_values, TRUE)),
00065       WATCHDOG_WARNING
00066     );
00067     return FALSE;
00068   } else {
00069     watchdog('tripal_cv', 
00070       'tripal_cv_get_cv: 2+ cvs match criteria values:%values',
00071       array('%values' => print_r($select_values, TRUE)),
00072       WATCHDOG_WARNING
00073     );
00074   }
00075   
00076 }
00077 
00078 // Purpose: To retrieve a chado cv object
00079 // @param $where_options 
00080 //   @code
00081 //        array(
00082 //                          <column_name> => array(
00083 //                            'type' => <type of column: INT/STRING>,
00084 //                            'value' => <the vlaue you want to filter on>,
00085 //                            'exact' => <if TRUE use =; if FALSE use ~>,
00086 //                          )
00087 //        )
00088 // @endcode
00089 //
00090 // @return 
00091 //   Chado cv object with all fields from the chado cv table
00092 //
00093 // @ingroup tripal_cv_api
00094 //
00095 //function tripal_cv_get_cv ($where_options)
00096 
00097 /**
00098  * Retrieve a cv given the cv name
00099  *
00100  * @param $name
00101  *  The name of the cv to be returned
00102  * @return 
00103  *   The cv object for the specified CV name
00104  *
00105  * @ingroup tripal_cv_api 
00106  */
00107 function tripal_cv_get_cv_by_name ($name) {
00108   $previous_db = tripal_db_set_active('chado'); 
00109    $r = db_fetch_object(db_query("SELECT * FROM cv WHERE name = '%s'",$name));
00110    tripal_db_set_active($previous_db);
00111 
00112   return $r;
00113 }
00114 
00115 /**
00116  * Retrieve the cv object for the specified CV id
00117  *
00118  * NOTE: This function is deprecated.
00119  * @see tripal_core_chado_generate_vars
00120  *
00121  * @param $cv_id
00122  *   The unique identifier for the cv retrieve
00123  *
00124  * @return
00125  *   An object describing the cv
00126  *
00127  * @ingroup tripal_cv_api
00128  */
00129 function tripal_cv_get_cv_by_id ($cv_id) {
00130   $previous_db = tripal_db_set_active('chado'); 
00131    $r = db_fetch_object(db_query("SELECT * FROM cv WHERE cv_id = %d",$cv_id));
00132    tripal_db_set_active($previous_db);
00133 
00134   return $r;
00135 }
00136 
00137 /**
00138  * Create an options array to be used in a form element which provides a list of all chado cvs
00139  *
00140  * @return 
00141  *   An array(cv_id => name) for each cv in the chado cv table
00142  *
00143  * @ingroup tripal_cv_api
00144  */
00145 function tripal_cv_get_cv_options() {
00146 
00147   $previous_db = tripal_db_set_active('chado');
00148   $result = db_query(
00149     "SELECT cv_id, name FROM cv"
00150   );
00151   tripal_db_set_active($previous_db);
00152 
00153   $options = array();
00154   while ( $r = db_fetch_object($result) ) {
00155     $options[$r->cv_id] = $r->name;
00156   }
00157 
00158   return $options;
00159 
00160 }
00161 
00162 /**
00163  * To retrieve a chado controlled vocabulary term object
00164  *
00165  * @param $select_values
00166  *   An array meant to uniquely select a given controlled vocabulary term
00167  *
00168  * @return
00169  *   Chado controlled vocabulary term object
00170  *
00171  * The controlled vocabulary term is selected using tripal_core_chado select and as such the
00172  * $select_values array parameter meant to uniquely identify the controlled vocab term to be 
00173  * returned follows the same form as when using tripal_core_chado_select directly.
00174  *
00175  * Example Usage:
00176  * @code
00177     $select_values = array(
00178       'name' => 'synonym',
00179       'cv_id' => array(
00180         'name' => 'feature_property'
00181       )
00182     );
00183     $cvterm_object = tripal_cv_get_cvterm($select_values);
00184  * @endcode
00185  *  The above code selects the synonym cvterm from the feature_proeprty cv and returns 
00186  *  the following object:
00187  * @code
00188     $cvterm_object = stdClass Object ( 
00189       [cvterm_id] => 2099
00190       [name] => synonym
00191       [definition] => Historic community symbol, may have originally been symbol []
00192       [is_obsolete] => 0
00193       [is_relationshiptype] => 1
00194       [cv_cv_id] => 13
00195       [cv_name] => feature_property
00196       [cv_definition] => 
00197       [dbreference_dbxref_id] => 2581
00198       [dbreference_accession] => synonym
00199       [dbreference_description] => 
00200       [dbreference_version] => 
00201       [dbreference_db_db_id] => 49
00202       [dbreference_db_name] => SOFP
00203       [dbreference_db_description] => 
00204       [dbreference_db_urlprefix] => 
00205       [dbreference_db_url] => 
00206     ); 
00207  * @endcode
00208  *
00209  * @ingroup tripal_cv_api
00210  */
00211 function tripal_cv_get_cvterm ($select_values) {
00212 
00213   $columns = array(
00214     'cvterm_id', 
00215     'cv_id', 
00216     'name', 
00217     'definition', 
00218     'dbxref_id', 
00219     'is_obsolete', 
00220     'is_relationshiptype'
00221   );
00222   $results = tripal_core_chado_select('cvterm', $columns, $select_values);
00223   if (sizeof($results) == 1) {
00224     // Add cv
00225     $cvterm = tripal_cv_add_cv_to_object(array('cv_id'=>$results[0]->cv_id),$results[0],array());
00226     unset($cvterm->cv_id);
00227     
00228     // Add dbxref
00229     $cvterm = tripal_db_add_dbxref_to_object(array('dbxref_id'=>$cvterm->dbxref_id),$cvterm,array());
00230     unset($cvterm->dbxref_id);
00231     
00232     return $cvterm;
00233   } elseif (empty($results)) {
00234     watchdog('tripal_cv', 
00235       'tripal_cv_get_cvterm: No cvterm matches criteria values:%values',
00236       array('%values' => print_r($select_values, TRUE)),
00237       WATCHDOG_WARNING
00238     );
00239     return FALSE;
00240   } else {
00241     watchdog('tripal_cv', 
00242       'tripal_cv_get_cvterm: 2+ cvterms match criteria values:%values',
00243       array('%values' => print_r($select_values, TRUE)),
00244       WATCHDOG_WARNING
00245     );
00246   }
00247  
00248 }
00249 
00250 /**
00251  * Retrieve a chado cvterm object with a given name
00252  *
00253  * @param $name
00254  *   the cvterm.name
00255  * @param $cv_id
00256  *   the cv_id of the term you are looking for
00257  *
00258  * @return 
00259  *   cvterm object
00260  *
00261  * @ingroup tripal_cv_api
00262  */
00263 function tripal_cv_get_cvterm_by_name ($name, $cv_id=0) {
00264 
00265   if (!empty($cv_id)) {
00266     $sql = "SELECT * FROM cvterm WHERE name='%s' AND cv_id=%d";
00267     $previous_db = tripal_db_set_active('chado');
00268     $r = db_fetch_object(db_query($sql, $name, $cv_id));
00269     tripal_db_set_active($previous_db);
00270   } else {
00271     $sql = "SELECT * FROM cvterm WHERE name='%s'";
00272     $previous_db = tripal_db_set_active('chado');
00273     $r = db_fetch_object(db_query($sql, $name));
00274     tripal_db_set_active($previous_db);
00275   }
00276   
00277   return $r;
00278   
00279 }
00280 
00281 /**
00282  * Create an options array to be used in a form element
00283  *   which provides a list of all chado cvterms
00284  * 
00285  * @param $cv_id 
00286  *   The chado cv_id;
00287  *   only cvterms with the supplied cv_id will be returned
00288  * @return 
00289  *   An array(cvterm_id => name) 
00290  *   for each cvterm in the chado cvterm table where cv_id=that supplied
00291  *
00292  * @ingroup tripal_cv_api
00293  */
00294 function tripal_cv_get_cvterm_options($cv_id = 0) {
00295 
00296   $previous_db = tripal_db_set_active('chado');
00297   if ($cv_id > 0) {
00298     $result = db_query(
00299       "SELECT cvterm_id, name FROM cvterm WHERE cv_id=%d", $cv_id
00300     );
00301   } else {
00302     $result = db_query(
00303       "SELECT cvterm_id, name FROM cvterm"
00304     );
00305   }
00306   tripal_db_set_active($previous_db);
00307 
00308   $options = array();
00309   while ( $r = db_fetch_object($result) ) {
00310     $options[$r->cvterm_id] = $r->name;
00311   }
00312 
00313   return $options;
00314 
00315 }
00316 
00317 /**
00318  * Implements hook_chado_cvterm_schema()
00319  * Purpose: To add descriptions and foreign keys to default table description
00320  * Note: This array will be merged with the array from all other implementations
00321  *
00322  * @return
00323  *    Array describing the cvterm table
00324  *
00325  * @ingroup tripal_schema_api 
00326  */
00327 function tripal_cv_chado_cvterm_schema() {
00328   $description = array();
00329 
00330   $description['foreign keys']['cv'] = array(
00331         'table' => 'cv',
00332         'columns' => array(
00333           'cv_id' => 'cv_id',
00334         ),
00335   );
00336       
00337   $description['foreign keys']['dbxref'] = array(
00338         'table' => 'dbxref',
00339         'columns' => array(
00340           'dbxref_id' => 'dbxref_id',
00341         ),
00342   ); 
00343 
00344   return $description;
00345 }
 All Classes Files Functions Variables