tripal_db.api.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage references to external databases
  5. *
  6. * @defgroup tripal_db_api DB Module API
  7. * @ingroup tripal_api
  8. * @ingroup tripal_db
  9. */
  10. /**
  11. * Purpose: To retrieve a chado database object
  12. *
  13. * @param $select_values
  14. * An array meant to uniquely select a given database
  15. *
  16. * @return
  17. * Chado database object
  18. *
  19. * The database is selected using tripal_core_chado select and as such the
  20. * $select_values array parameter meant to uniquely identify the database to be
  21. * returned follows the same form as when using tripal_core_chado_select directly.
  22. *
  23. * Example Usage:
  24. * @code
  25. $select_values = array(
  26. 'name' => 'SOFP'
  27. );
  28. $db_object = tripal_db_get_db($select_values);
  29. * @endcode
  30. * The above code selects the SOFP db and returns the following object:
  31. * @code
  32. $db_object = stdClass Object (
  33. [db_id] => 49
  34. [name] => SOFP
  35. [description] =>
  36. [urlprefix] =>
  37. [url] =>
  38. );
  39. * @endcode
  40. *
  41. * @ingroup tripal_db_api
  42. */
  43. function tripal_db_get_db($select_values) {
  44. $columns = array(
  45. 'db_id',
  46. 'name',
  47. 'description',
  48. 'urlprefix',
  49. 'url'
  50. );
  51. $results = tripal_core_chado_select('db', $columns, $select_values);
  52. if (sizeof($results) == 1) {
  53. return $results[0];
  54. }
  55. elseif (empty($results)) {
  56. watchdog('tripal_cdb',
  57. 'tripal_db_get_db: No db matches criteria values:%values',
  58. array('%values' => print_r($select_values, TRUE)),
  59. WATCHDOG_WARNING
  60. );
  61. return FALSE;
  62. }
  63. else {
  64. watchdog('tripal_db',
  65. 'tripal_db_get_db: 2+ dbs match criteria values:%values',
  66. array('%values' => print_r($select_values, TRUE)),
  67. WATCHDOG_WARNING
  68. );
  69. }
  70. }
  71. /**
  72. * Purpose: To retrieve a chado db object
  73. *
  74. * @param $db_id
  75. * db.db_id
  76. * @return
  77. * Chado db object with all fields from the chado db table
  78. *
  79. * @ingroup tripal_db_api
  80. */
  81. function tripal_db_get_db_by_db_id($db_id) {
  82. $r = db_fetch_object(chado_query(
  83. "SELECT * FROM {db} WHERE db_id=%d", $db_id
  84. ));
  85. return $r;
  86. }
  87. /**
  88. * Purpose: To retrieve a chado db object
  89. *
  90. * @param $name
  91. * db.name
  92. * @return
  93. * chado db object with all fields from the chado db table
  94. *
  95. * @ingroup tripal_db_api
  96. */
  97. function tripal_db_get_db_by_name($name) {
  98. $r = db_fetch_object(chado_query(
  99. "SELECT * FROM {db} WHERE name='%s'", $name
  100. ));
  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. $result = chado_query(
  126. "SELECT db_id, name FROM {db}"
  127. );
  128. $options = array();
  129. while ( $r = db_fetch_object($result) ) {
  130. $options[$r->db_id] = $r->name;
  131. }
  132. return $options;
  133. }
  134. // Purpose: To retrieve a chado dbxref object
  135. //
  136. // @param where_options: array(
  137. // <column_name> => array(
  138. // 'type' => <type of column: INT/**STRING>,
  139. // 'value' => <the vlaue you want to filter on>,
  140. // 'exact' => <if TRUE use =; if FALSE use ~>,
  141. // )
  142. // )
  143. // @return chado dbxref object with all fields from the chado dbxref table
  144. //
  145. //function tripal_db_get_dbxref ($where_options) {
  146. /**
  147. * Purpose: To retrieve a chado database reference object
  148. *
  149. * @param $select_values
  150. * An array meant to uniquely select a given database reference
  151. *
  152. * @return
  153. * Chado database reference object
  154. *
  155. * The database reference is selected using tripal_core_chado select and as such the
  156. * $select_values array parameter meant to uniquely identify the database reference to be
  157. * returned follows the same form as when using tripal_core_chado_select directly.
  158. *
  159. * Example Usage:
  160. * @code
  161. $select_values = array(
  162. 'accession' => 'synonym',
  163. 'db_id' => array(
  164. 'name' => 'SOFP'
  165. )
  166. );
  167. $dbxref_object = tripal_db_get_dbxref($select_values);
  168. * @endcode
  169. * The above code selects the synonym database reference and returns the following object:
  170. * @code
  171. $dbxref_object = stdClass Object (
  172. [dbxref_id] => 2581
  173. [accession] => synonym
  174. [description] =>
  175. [version] =>
  176. [db_db_id] => 49
  177. [db_name] => SOFP
  178. [db_description] =>
  179. [db_urlprefix] =>
  180. [db_url] =>
  181. );
  182. * @endcode
  183. *
  184. * @ingroup tripal_db_api
  185. */
  186. function tripal_db_get_dbxref($select_values) {
  187. $columns = array(
  188. 'dbxref_id',
  189. 'db_id',
  190. 'accession',
  191. 'description',
  192. 'version'
  193. );
  194. $results = tripal_core_chado_select('dbxref', $columns, $select_values);
  195. if (sizeof($results) == 1) {
  196. $dbxref = tripal_db_add_db_to_object(array('db_id' => $results[0]->db_id), $results[0], array());
  197. unset($dbxref->db_id);
  198. return $dbxref;
  199. }
  200. elseif (empty($results)) {
  201. watchdog('tripal_db',
  202. 'tripal_db_get_dbxref: No dbxref matches criteria values:%values',
  203. array('%values' => print_r($select_values, TRUE)),
  204. WATCHDOG_WARNING
  205. );
  206. return FALSE;
  207. }
  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. $r = db_fetch_object(chado_query(
  231. "SELECT * FROM {dbxref} WHERE accession='%s' AND db_id=%d",
  232. $accession, $db_id
  233. ));
  234. }
  235. else {
  236. $r = db_fetch_object(chado_query(
  237. "SELECT * FROM {dbxref} WHERE accession='%s'",
  238. $accession
  239. ));
  240. }
  241. return $r;
  242. }
  243. /**
  244. * Adds a new database to the Chado DB table and returns the DB object.
  245. *
  246. * @param $dbname
  247. * The name of the database. This name is usually used as the prefix for
  248. * CV term accessions
  249. * @param $description
  250. * Optional. A description of the database. By default no description is required.
  251. * @param $url
  252. * Optional. The URL for the database
  253. * @param $urlprefix
  254. * Optional. The URL that is to be used as a prefix when constructing a link to
  255. * a database term
  256. * @param $update
  257. * Optional. Set this to '1' to force an update of the database if it
  258. * already exists. The default is to not update. If the database exists
  259. * then nothing is added.
  260. *
  261. * @return
  262. * An object populated with fields from the newly added database. If the
  263. * database already exists it returns the values in the current entry.
  264. *
  265. * @ingroup tripal_db_api
  266. */
  267. function tripal_db_add_db($dbname, $description = '', $url = '',
  268. $urlprefix = '', $update = 0) {
  269. // build the values array for inserting/updating
  270. $ins_values = array(
  271. 'name' => $dbname,
  272. 'description' => $description,
  273. 'url' => $url,
  274. 'urlprefix' => $urlprefix
  275. );
  276. // get the database record if it already exists
  277. $sel_values = array('name' => $dbname);
  278. $sel_options = array('statement_name' => 'sel_db_na');
  279. $result = tripal_core_chado_select('db', array('*'), $sel_values, $sel_options);
  280. // if it does not exists then add it
  281. if (count($result) == 0) {
  282. $ins_options = array('statement_name' => 'ins_db_nadeurur');
  283. $success = tripal_core_chado_insert('db', $ins_values, $ins_options);
  284. if (!$success) {
  285. watchdog('tripal_db', "Cannot create db '$dbname'.", NULL, WATCHDOG_WARNING);
  286. return 0;
  287. }
  288. $result = tripal_core_chado_select('db', array('*'), $sel_values, $sel_options);
  289. }
  290. // if it exists and update is enabled the do the update
  291. elseif ($update) {
  292. $upd_options = array('statement_name' => 'upd_db_nadeurur');
  293. $success = tripal_core_chado_update('db', $sel_values, $ins_values, $upd_options);
  294. if (!$success) {
  295. watchdog('tripal_db', "Cannot update db '$dbname'.", NULL, WATCHDOG_WARNING);
  296. return 0;
  297. }
  298. $result = tripal_core_chado_select('db', array('*'), $sel_values, $sel_options);
  299. }
  300. // return the database object
  301. return $result[0];
  302. }
  303. /**
  304. *
  305. * @ingroup tripal_db_api
  306. */
  307. function tripal_db_add_dbxref($db_id, $accession, $version = '', $description = '') {
  308. $ins_values = array(
  309. 'db_id' => $db_id,
  310. 'accession' => $accession,
  311. 'version' => $version,
  312. 'description' => $description
  313. );
  314. // check to see if the dbxref exists
  315. $sel_values = array(
  316. 'db_id' => $db_id,
  317. 'accession' => $accession,
  318. );
  319. $sel_options = array('statement_name' => 'sel_dbxref_dbacve', 'is_duplicate' => 1);
  320. $result = tripal_core_chado_select('dbxref', array('*'), $sel_values, $sel_options);
  321. // if it doesn't already exist then add it
  322. if (!$result) {
  323. $ins_options = array('statement_name' => 'ins_dbxref_dbacvede');
  324. $success = tripal_core_chado_insert('dbxref', $ins_values, $ins_options);
  325. if (!$success) {
  326. watchdog('tripal_cv', "Failed to insert the dbxref record $accession", NULL, WATCHDOG_WARNING);
  327. return 0;
  328. }
  329. $result = tripal_core_chado_select('dbxref', array('*'), $sel_values, $sel_options);
  330. }
  331. return $result[0];
  332. }