tripal_cv.api.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * @defgroup tripal_cv_api CV Module API
  4. * @ingroup tripal_api
  5. * @ingroup tripal_cv
  6. * This module provides a set of functions to simplify working with
  7. * controlled vocabularies. Most of the API functions deal with retrieving
  8. * terms or their parent vocabularies.
  9. *
  10. * However, the API also supports
  11. * generation of trees for browsing a vocabulary as well as generation of
  12. * pie graphs for display of hierarchical counts of terms. Version 0.3b of
  13. * Tripal provides a feature browser and a feature summary chart uses
  14. * the API functions provided here. But in general charts and trees can be
  15. * created for any controlled vocabulary.
  16. *
  17. */
  18. /**
  19. * Purpose: To retrieve a chado controlled vocabulary object
  20. *
  21. * @param $select_values
  22. * An array meant to uniquely select a given controlled vocabulary
  23. *
  24. * @return
  25. * Chado controlled vocabulary object
  26. *
  27. * The controlled vocabulary is selected using tripal_core_chado select and as such the
  28. * $select_values array parameter meant to uniquely identify the controlled vocab to be
  29. * returned follows the same form as when using tripal_core_chado_select directly.
  30. *
  31. * Example Usage:
  32. * @code
  33. $select_values = array(
  34. 'name' => 'feature_property'
  35. );
  36. $cv_object = tripal_cv_get_cv($select_values);
  37. * @endcode
  38. * The above code selects the feature_property cv and returns the following object:
  39. * @code
  40. $cv_object = stdClass Object (
  41. [cv_id] => 13
  42. [name] => feature_property
  43. [definition] =>
  44. );
  45. * @endcode
  46. *
  47. * @ingroup tripal_cv_api
  48. */
  49. function tripal_cv_get_cv ($select_values) {
  50. $columns = array(
  51. 'cv_id',
  52. 'name',
  53. 'definition',
  54. );
  55. $results = tripal_core_chado_select('cv', $columns, $select_values);
  56. if (sizeof($results) == 1) {
  57. return $results[0];
  58. } elseif (empty($results)) {
  59. watchdog('tripal_cv',
  60. 'tripal_cv_get_cv: No cv matches criteria values:%values',
  61. array('%values' => print_r($select_values, TRUE)),
  62. WATCHDOG_WARNING
  63. );
  64. return FALSE;
  65. } else {
  66. watchdog('tripal_cv',
  67. 'tripal_cv_get_cv: 2+ cvs match criteria values:%values',
  68. array('%values' => print_r($select_values, TRUE)),
  69. WATCHDOG_WARNING
  70. );
  71. }
  72. }
  73. // Purpose: To retrieve a chado cv object
  74. // @param $where_options
  75. // @code
  76. // array(
  77. // <column_name> => array(
  78. // 'type' => <type of column: INT/STRING>,
  79. // 'value' => <the vlaue you want to filter on>,
  80. // 'exact' => <if TRUE use =; if FALSE use ~>,
  81. // )
  82. // )
  83. // @endcode
  84. //
  85. // @return
  86. // Chado cv object with all fields from the chado cv table
  87. //
  88. // @ingroup tripal_cv_api
  89. //
  90. //function tripal_cv_get_cv ($where_options)
  91. /**
  92. * Retrieve a cv given the cv name
  93. *
  94. * @param $name
  95. * The name of the cv to be returned
  96. * @return
  97. * The cv object for the specified CV name
  98. *
  99. * @ingroup tripal_cv_api
  100. */
  101. function tripal_cv_get_cv_by_name ($name) {
  102. $previous_db = tripal_db_set_active('chado');
  103. $r = db_fetch_object(db_query("SELECT * FROM cv WHERE name = '%s'",$name));
  104. tripal_db_set_active($previous_db);
  105. return $r;
  106. }
  107. /**
  108. * Retrieve the cv object for the specified CV id
  109. *
  110. * NOTE: This function is deprecated.
  111. * @see tripal_core_chado_generate_vars
  112. *
  113. * @param $cv_id
  114. * The unique identifier for the cv retrieve
  115. *
  116. * @return
  117. * An object describing the cv
  118. *
  119. * @ingroup tripal_cv_api
  120. */
  121. function tripal_cv_get_cv_by_id ($cv_id) {
  122. $previous_db = tripal_db_set_active('chado');
  123. $r = db_fetch_object(db_query("SELECT * FROM cv WHERE cv_id = %d",$cv_id));
  124. tripal_db_set_active($previous_db);
  125. return $r;
  126. }
  127. /**
  128. * Create an options array to be used in a form element which provides a list of all chado cvs
  129. *
  130. * @return
  131. * An array(cv_id => name) for each cv in the chado cv table
  132. *
  133. * @ingroup tripal_cv_api
  134. */
  135. function tripal_cv_get_cv_options() {
  136. $previous_db = tripal_db_set_active('chado');
  137. $result = db_query(
  138. "SELECT cv_id, name FROM cv"
  139. );
  140. tripal_db_set_active($previous_db);
  141. $options = array();
  142. while ( $r = db_fetch_object($result) ) {
  143. $options[$r->cv_id] = $r->name;
  144. }
  145. return $options;
  146. }
  147. /**
  148. * To retrieve a chado controlled vocabulary term object
  149. *
  150. * @param $select_values
  151. * An array meant to uniquely select a given controlled vocabulary term
  152. *
  153. * @return
  154. * Chado controlled vocabulary term object
  155. *
  156. * The controlled vocabulary term is selected using tripal_core_chado select and as such the
  157. * $select_values array parameter meant to uniquely identify the controlled vocab term to be
  158. * returned follows the same form as when using tripal_core_chado_select directly.
  159. *
  160. * Example Usage:
  161. * @code
  162. $select_values = array(
  163. 'name' => 'synonym',
  164. 'cv_id' => array(
  165. 'name' => 'feature_property'
  166. )
  167. );
  168. $cvterm_object = tripal_cv_get_cvterm($select_values);
  169. * @endcode
  170. * The above code selects the synonym cvterm from the feature_proeprty cv and returns
  171. * the following object:
  172. * @code
  173. $cvterm_object = stdClass Object (
  174. [cvterm_id] => 2099
  175. [name] => synonym
  176. [definition] => Historic community symbol, may have originally been symbol []
  177. [is_obsolete] => 0
  178. [is_relationshiptype] => 1
  179. [cv_cv_id] => 13
  180. [cv_name] => feature_property
  181. [cv_definition] =>
  182. [dbreference_dbxref_id] => 2581
  183. [dbreference_accession] => synonym
  184. [dbreference_description] =>
  185. [dbreference_version] =>
  186. [dbreference_db_db_id] => 49
  187. [dbreference_db_name] => SOFP
  188. [dbreference_db_description] =>
  189. [dbreference_db_urlprefix] =>
  190. [dbreference_db_url] =>
  191. );
  192. * @endcode
  193. *
  194. * @ingroup tripal_cv_api
  195. */
  196. function tripal_cv_get_cvterm ($select_values) {
  197. $columns = array(
  198. 'cvterm_id',
  199. 'cv_id',
  200. 'name',
  201. 'definition',
  202. 'dbxref_id',
  203. 'is_obsolete',
  204. 'is_relationshiptype'
  205. );
  206. $results = tripal_core_chado_select('cvterm', $columns, $select_values);
  207. if (sizeof($results) == 1) {
  208. // Add cv
  209. $cvterm = tripal_cv_add_cv_to_object(array('cv_id'=>$results[0]->cv_id),$results[0],array());
  210. unset($cvterm->cv_id);
  211. // Add dbxref
  212. $cvterm = tripal_db_add_dbxref_to_object(array('dbxref_id'=>$cvterm->dbxref_id),$cvterm,array());
  213. unset($cvterm->dbxref_id);
  214. return $cvterm;
  215. } elseif (empty($results)) {
  216. watchdog('tripal_cv',
  217. 'tripal_cv_get_cvterm: No cvterm matches criteria values:%values',
  218. array('%values' => print_r($select_values, TRUE)),
  219. WATCHDOG_WARNING
  220. );
  221. return FALSE;
  222. } else {
  223. watchdog('tripal_cv',
  224. 'tripal_cv_get_cvterm: 2+ cvterms match criteria values:%values',
  225. array('%values' => print_r($select_values, TRUE)),
  226. WATCHDOG_WARNING
  227. );
  228. }
  229. }
  230. /**
  231. * Retrieve a chado cvterm object with a given name
  232. *
  233. * @param $name
  234. * the cvterm.name
  235. * @param $cv_id
  236. * the cv_id of the term you are looking for
  237. *
  238. * @return
  239. * cvterm object
  240. *
  241. * @ingroup tripal_cv_api
  242. */
  243. function tripal_cv_get_cvterm_by_name ($name, $cv_id=0) {
  244. if (!empty($cv_id)) {
  245. $sql = "SELECT * FROM cvterm WHERE name='%s' AND cv_id=%d";
  246. $previous_db = tripal_db_set_active('chado');
  247. $r = db_fetch_object(db_query($sql, $name, $cv_id));
  248. tripal_db_set_active($previous_db);
  249. } else {
  250. $sql = "SELECT * FROM cvterm WHERE name='%s'";
  251. $previous_db = tripal_db_set_active('chado');
  252. $r = db_fetch_object(db_query($sql, $name));
  253. tripal_db_set_active($previous_db);
  254. }
  255. return $r;
  256. }
  257. /**
  258. * Create an options array to be used in a form element
  259. * which provides a list of all chado cvterms
  260. *
  261. * @param $cv_id
  262. * The chado cv_id;
  263. * only cvterms with the supplied cv_id will be returned
  264. * @return
  265. * An array(cvterm_id => name)
  266. * for each cvterm in the chado cvterm table where cv_id=that supplied
  267. *
  268. * @ingroup tripal_cv_api
  269. */
  270. function tripal_cv_get_cvterm_options($cv_id = 0) {
  271. $previous_db = tripal_db_set_active('chado');
  272. if ($cv_id > 0) {
  273. $result = db_query(
  274. "SELECT cvterm_id, name FROM cvterm WHERE cv_id=%d", $cv_id
  275. );
  276. } else {
  277. $result = db_query(
  278. "SELECT cvterm_id, name FROM cvterm"
  279. );
  280. }
  281. tripal_db_set_active($previous_db);
  282. $options = array();
  283. while ( $r = db_fetch_object($result) ) {
  284. $options[$r->cvterm_id] = $r->name;
  285. }
  286. return $options;
  287. }
  288. /**
  289. * Implements hook_chado_cvterm_schema()
  290. * Purpose: To add descriptions and foreign keys to default table description
  291. * Note: This array will be merged with the array from all other implementations
  292. *
  293. * @return
  294. * Array describing the cvterm table
  295. *
  296. * @ingroup tripal_schema_api
  297. */
  298. function tripal_cv_chado_cvterm_schema() {
  299. $description = array();
  300. $description['foreign keys']['cv'] = array(
  301. 'table' => 'cv',
  302. 'columns' => array(
  303. 'cv_id' => 'cv_id',
  304. ),
  305. );
  306. $description['foreign keys']['dbxref'] = array(
  307. 'table' => 'dbxref',
  308. 'columns' => array(
  309. 'dbxref_id' => 'dbxref_id',
  310. ),
  311. );
  312. return $description;
  313. }