tripal_cv.api.inc 8.3 KB

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