tripal_cv.api.inc 8.4 KB

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