tripal_db.api.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * @defgroup tripal_db_api Database Module API
  4. * @ingroup tripal_api
  5. * @ingroup tripal_db
  6. * Provides an application programming interface (API) to manage references to external databases
  7. */
  8. /**
  9. * Purpose: To retrieve a chado database object
  10. *
  11. * @param $select_values
  12. * An array meant to uniquely select a given database
  13. *
  14. * @return
  15. * Chado database object
  16. *
  17. * The database is selected using tripal_core_chado select and as such the
  18. * $select_values array parameter meant to uniquely identify the database 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' => 'SOFP'
  25. );
  26. $db_object = tripal_db_get_db($select_values);
  27. * @endcode
  28. * The above code selects the SOFP db and returns the following object:
  29. * @code
  30. $db_object = stdClass Object (
  31. [db_id] => 49
  32. [name] => SOFP
  33. [description] =>
  34. [urlprefix] =>
  35. [url] =>
  36. );
  37. * @endcode
  38. *
  39. * @ingroup tripal_db_api
  40. */
  41. function tripal_db_get_db ($select_values) {
  42. $columns = array(
  43. 'db_id',
  44. 'name',
  45. 'description',
  46. 'urlprefix',
  47. 'url'
  48. );
  49. $results = tripal_core_chado_select('db', $columns, $select_values);
  50. if (sizeof($results) == 1) {
  51. return $results[0];
  52. } elseif (empty($results)) {
  53. watchdog('tripal_cdb',
  54. 'tripal_db_get_db: No db matches criteria values:%values',
  55. array('%values' => print_r($select_values, TRUE)),
  56. WATCHDOG_WARNING
  57. );
  58. return FALSE;
  59. } else {
  60. watchdog('tripal_db',
  61. 'tripal_db_get_db: 2+ dbs match criteria values:%values',
  62. array('%values' => print_r($select_values, TRUE)),
  63. WATCHDOG_WARNING
  64. );
  65. }
  66. }
  67. /**
  68. * Purpose: To retrieve a chado db object
  69. *
  70. * @param $db_id
  71. * db.db_id
  72. * @return
  73. * Chado db object with all fields from the chado db table
  74. *
  75. * @ingroup tripal_db_api
  76. */
  77. function tripal_db_get_db_by_db_id ($db_id) {
  78. $previous_db = tripal_db_set_active('chado');
  79. $r = db_fetch_object(db_query(
  80. "SELECT * FROM db WHERE db_id=%d", $db_id
  81. ));
  82. tripal_db_set_active($previous_db);
  83. return $r;
  84. }
  85. /**
  86. * Purpose: To retrieve a chado db object
  87. *
  88. * @param $name
  89. * db.name
  90. * @return
  91. * chado db object with all fields from the chado db table
  92. *
  93. * @ingroup tripal_db_api
  94. */
  95. function tripal_db_get_db_by_name ($name) {
  96. $previous_db = tripal_db_set_active('chado');
  97. $r = db_fetch_object(db_query(
  98. "SELECT * FROM db WHERE name='%s'", $name
  99. ));
  100. tripal_db_set_active($previous_db);
  101. return $r;
  102. }
  103. // Purpose: To retrieve a chado db object
  104. //
  105. // @params where_options: array(
  106. // <column_name> => array(
  107. // 'type' => <type of column: INT/**STRING>,
  108. // 'value' => <the vlaue you want to filter on>,
  109. // 'exact' => <if TRUE use =; if FALSE use ~>,
  110. // )
  111. // )
  112. // @return chado db object with all fields from the chado db table
  113. //
  114. //function tripal_db_get_db ($where_options) {
  115. /**
  116. * Purpose: Create an options array to be used in a form element
  117. * which provides a list of all chado dbs
  118. *
  119. * @return
  120. * An array(db_id => name) for each db in the chado db table
  121. *
  122. * @ingroup tripal_db_api
  123. */
  124. function tripal_db_get_db_options() {
  125. $previous_db = tripal_db_set_active('chado');
  126. $result = db_query(
  127. "SELECT db_id, name FROM db"
  128. );
  129. tripal_db_set_active($previous_db);
  130. $options = array();
  131. while ( $r = db_fetch_object($result) ) {
  132. $options[$r->db_id] = $r->name;
  133. }
  134. return $options;
  135. }
  136. // Purpose: To retrieve a chado dbxref object
  137. //
  138. // @param where_options: array(
  139. // <column_name> => array(
  140. // 'type' => <type of column: INT/**STRING>,
  141. // 'value' => <the vlaue you want to filter on>,
  142. // 'exact' => <if TRUE use =; if FALSE use ~>,
  143. // )
  144. // )
  145. // @return chado dbxref object with all fields from the chado dbxref table
  146. //
  147. //function tripal_db_get_dbxref ($where_options) {
  148. /**
  149. * Purpose: To retrieve a chado database reference object
  150. *
  151. * @param $select_values
  152. * An array meant to uniquely select a given database reference
  153. *
  154. * @return
  155. * Chado database reference object
  156. *
  157. * The database reference is selected using tripal_core_chado select and as such the
  158. * $select_values array parameter meant to uniquely identify the database reference to be
  159. * returned follows the same form as when using tripal_core_chado_select directly.
  160. *
  161. * Example Usage:
  162. * @code
  163. $select_values = array(
  164. 'accession' => 'synonym',
  165. 'db_id' => array(
  166. 'name' => 'SOFP'
  167. )
  168. );
  169. $dbxref_object = tripal_db_get_dbxref($select_values);
  170. * @endcode
  171. * The above code selects the synonym database reference and returns the following object:
  172. * @code
  173. $dbxref_object = stdClass Object (
  174. [dbxref_id] => 2581
  175. [accession] => synonym
  176. [description] =>
  177. [version] =>
  178. [db_db_id] => 49
  179. [db_name] => SOFP
  180. [db_description] =>
  181. [db_urlprefix] =>
  182. [db_url] =>
  183. );
  184. * @endcode
  185. *
  186. * @ingroup tripal_db_api
  187. */
  188. function tripal_db_get_dbxref ($select_values) {
  189. $columns = array(
  190. 'dbxref_id',
  191. 'db_id',
  192. 'accession',
  193. 'description',
  194. 'version'
  195. );
  196. $results = tripal_core_chado_select('dbxref', $columns, $select_values);
  197. if (sizeof($results) == 1) {
  198. $dbxref = tripal_db_add_db_to_object(array('db_id' => $results[0]->db_id), $results[0], array());
  199. unset($dbxref->db_id);
  200. return $dbxref;
  201. } elseif (empty($results)) {
  202. watchdog('tripal_db',
  203. 'tripal_db_get_dbxref: No dbxref matches criteria values:%values',
  204. array('%values' => print_r($select_values, TRUE)),
  205. WATCHDOG_WARNING
  206. );
  207. return FALSE;
  208. } else {
  209. watchdog('tripal_db',
  210. 'tripal_db_get_dbxref: 2+ dbxrefs match criteria values:%values',
  211. array('%values' => print_r($select_values, TRUE)),
  212. WATCHDOG_WARNING
  213. );
  214. }
  215. }
  216. /**
  217. * Purpose: To retrieve a chado dbxref object with a given accession
  218. *
  219. * @param $accession
  220. * dbxref.accession
  221. * @param $db_id
  222. * dbxref.db_id
  223. * @return
  224. * chado dbxref object with all fields from the chado dbxref table
  225. *
  226. * @ingroup tripal_db_api
  227. */
  228. function tripal_db_get_dbxref_by_accession ($accession, $db_id=0) {
  229. if (!empty($db_id)) {
  230. $previous_db = tripal_db_set_active('chado');
  231. $r = db_fetch_object(db_query(
  232. "SELECT * FROM dbxref WHERE accession='%s' AND db_id=%d",
  233. $accession, $db_id
  234. ));
  235. tripal_db_set_active($previous_db);
  236. } else {
  237. $previous_db = tripal_db_set_active('chado');
  238. $r = db_fetch_object(db_query(
  239. "SELECT * FROM dbxref WHERE accession='%s'",
  240. $accession
  241. ));
  242. tripal_db_set_active($previous_db);
  243. }
  244. return $r;
  245. }
  246. /**
  247. * Implements hook_chado_dbxref_schema()
  248. * Purpose: To add descriptions and foreign keys to default table description
  249. * Note: This array will be merged with the array from all other implementations
  250. *
  251. * @return
  252. * Array describing the dbxref table
  253. *
  254. * @ingroup tripal_schema_api
  255. */
  256. function tripal_db_chado_dbxref_schema() {
  257. $description = array();
  258. $description['foreign keys']['db'] = array(
  259. 'table' => 'db',
  260. 'columns' => array(
  261. 'db_id' => 'db_id',
  262. ),
  263. );
  264. return $description;
  265. }