tripal_db.api.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. $previous_db = tripal_db_set_active('chado');
  83. $r = db_fetch_object(db_query(
  84. "SELECT * FROM {db} WHERE db_id=%d", $db_id
  85. ));
  86. tripal_db_set_active($previous_db);
  87. return $r;
  88. }
  89. /**
  90. * Purpose: To retrieve a chado db object
  91. *
  92. * @param $name
  93. * db.name
  94. * @return
  95. * chado db object with all fields from the chado db table
  96. *
  97. * @ingroup tripal_db_api
  98. */
  99. function tripal_db_get_db_by_name($name) {
  100. $previous_db = tripal_db_set_active('chado');
  101. $r = db_fetch_object(db_query(
  102. "SELECT * FROM {db} WHERE name='%s'", $name
  103. ));
  104. tripal_db_set_active($previous_db);
  105. return $r;
  106. }
  107. // Purpose: To retrieve a chado db object
  108. //
  109. // @params where_options: array(
  110. // <column_name> => array(
  111. // 'type' => <type of column: INT/**STRING>,
  112. // 'value' => <the vlaue you want to filter on>,
  113. // 'exact' => <if TRUE use =; if FALSE use ~>,
  114. // )
  115. // )
  116. // @return chado db object with all fields from the chado db table
  117. //
  118. //function tripal_db_get_db ($where_options) {
  119. /**
  120. * Purpose: Create an options array to be used in a form element
  121. * which provides a list of all chado dbs
  122. *
  123. * @return
  124. * An array(db_id => name) for each db in the chado db table
  125. *
  126. * @ingroup tripal_db_api
  127. */
  128. function tripal_db_get_db_options() {
  129. $previous_db = tripal_db_set_active('chado');
  130. $result = db_query(
  131. "SELECT db_id, name FROM {db}"
  132. );
  133. tripal_db_set_active($previous_db);
  134. $options = array();
  135. while ( $r = db_fetch_object($result) ) {
  136. $options[$r->db_id] = $r->name;
  137. }
  138. return $options;
  139. }
  140. // Purpose: To retrieve a chado dbxref object
  141. //
  142. // @param where_options: array(
  143. // <column_name> => array(
  144. // 'type' => <type of column: INT/**STRING>,
  145. // 'value' => <the vlaue you want to filter on>,
  146. // 'exact' => <if TRUE use =; if FALSE use ~>,
  147. // )
  148. // )
  149. // @return chado dbxref object with all fields from the chado dbxref table
  150. //
  151. //function tripal_db_get_dbxref ($where_options) {
  152. /**
  153. * Purpose: To retrieve a chado database reference object
  154. *
  155. * @param $select_values
  156. * An array meant to uniquely select a given database reference
  157. *
  158. * @return
  159. * Chado database reference object
  160. *
  161. * The database reference is selected using tripal_core_chado select and as such the
  162. * $select_values array parameter meant to uniquely identify the database reference to be
  163. * returned follows the same form as when using tripal_core_chado_select directly.
  164. *
  165. * Example Usage:
  166. * @code
  167. $select_values = array(
  168. 'accession' => 'synonym',
  169. 'db_id' => array(
  170. 'name' => 'SOFP'
  171. )
  172. );
  173. $dbxref_object = tripal_db_get_dbxref($select_values);
  174. * @endcode
  175. * The above code selects the synonym database reference and returns the following object:
  176. * @code
  177. $dbxref_object = stdClass Object (
  178. [dbxref_id] => 2581
  179. [accession] => synonym
  180. [description] =>
  181. [version] =>
  182. [db_db_id] => 49
  183. [db_name] => SOFP
  184. [db_description] =>
  185. [db_urlprefix] =>
  186. [db_url] =>
  187. );
  188. * @endcode
  189. *
  190. * @ingroup tripal_db_api
  191. */
  192. function tripal_db_get_dbxref($select_values) {
  193. $columns = array(
  194. 'dbxref_id',
  195. 'db_id',
  196. 'accession',
  197. 'description',
  198. 'version'
  199. );
  200. $results = tripal_core_chado_select('dbxref', $columns, $select_values);
  201. if (sizeof($results) == 1) {
  202. $dbxref = tripal_db_add_db_to_object(array('db_id' => $results[0]->db_id), $results[0], array());
  203. unset($dbxref->db_id);
  204. return $dbxref;
  205. }
  206. elseif (empty($results)) {
  207. watchdog('tripal_db',
  208. 'tripal_db_get_dbxref: No dbxref matches criteria values:%values',
  209. array('%values' => print_r($select_values, TRUE)),
  210. WATCHDOG_WARNING
  211. );
  212. return FALSE;
  213. }
  214. else {
  215. watchdog('tripal_db',
  216. 'tripal_db_get_dbxref: 2+ dbxrefs match criteria values:%values',
  217. array('%values' => print_r($select_values, TRUE)),
  218. WATCHDOG_WARNING
  219. );
  220. }
  221. }
  222. /**
  223. * Purpose: To retrieve a chado dbxref object with a given accession
  224. *
  225. * @param $accession
  226. * dbxref.accession
  227. * @param $db_id
  228. * dbxref.db_id
  229. * @return
  230. * chado dbxref object with all fields from the chado dbxref table
  231. *
  232. * @ingroup tripal_db_api
  233. */
  234. function tripal_db_get_dbxref_by_accession($accession, $db_id=0) {
  235. if (!empty($db_id)) {
  236. $previous_db = tripal_db_set_active('chado');
  237. $r = db_fetch_object(db_query(
  238. "SELECT * FROM {dbxref} WHERE accession='%s' AND db_id=%d",
  239. $accession, $db_id
  240. ));
  241. tripal_db_set_active($previous_db);
  242. }
  243. else {
  244. $previous_db = tripal_db_set_active('chado');
  245. $r = db_fetch_object(db_query(
  246. "SELECT * FROM {dbxref} WHERE accession='%s'",
  247. $accession
  248. ));
  249. tripal_db_set_active($previous_db);
  250. }
  251. return $r;
  252. }
  253. /**
  254. * Adds a new database to the Chado DB table and returns the DB object.
  255. *
  256. * @param $dbname
  257. * The name of the database. This name is usually used as the prefix for
  258. * CV term accessions
  259. * @param $description
  260. * Optional. A description of the database. By default no description is required.
  261. * @param $url
  262. * Optional. The URL for the database
  263. * @param $urlprefix
  264. * Optional. The URL that is to be used as a prefix when constructing a link to
  265. * a database term
  266. * @param $update
  267. * Optional. Set this to '1' to force an update of the database if it
  268. * already exists. The default is to not update. If the database exists
  269. * then nothing is added.
  270. *
  271. * @return
  272. * An object populated with fields from the newly added database. If the
  273. * database already exists it returns the values in the current entry.
  274. *
  275. * @ingroup tripal_db_api
  276. */
  277. function tripal_db_add_db($dbname, $description = '', $url = '',
  278. $urlprefix = '', $update = 0) {
  279. // build the values array for inserting/updating
  280. $ins_values = array(
  281. 'name' => $dbname,
  282. 'description' => $description,
  283. 'url' => $url,
  284. 'urlprefix' => $urlprefix
  285. );
  286. // get the database record if it already exists
  287. $sel_values = array('name' => $dbname);
  288. $sel_options = array('statement_name' => 'sel_db_na');
  289. $result = tripal_core_chado_select('db', array('*'), $sel_values, $sel_options);
  290. // if it does not exists then add it
  291. if (count($result) == 0) {
  292. $ins_options = array('statement_name' => 'ins_db_nadeurur');
  293. $success = tripal_core_chado_insert('db', $ins_values, $ins_options);
  294. if (!$success) {
  295. watchdog('tripal_db', "Cannot create db '$dbname'.", NULL, WATCHDOG_WARNING);
  296. return 0;
  297. }
  298. $result = tripal_core_chado_select('db', array('*'), $sel_values, $sel_options);
  299. }
  300. // if it exists and update is enabled the do the update
  301. elseif ($update) {
  302. $upd_options = array('statement_name' => 'upd_db_nadeurur');
  303. $success = tripal_core_chado_update('db', $sel_values, $ins_values, $upd_options);
  304. if (!$success) {
  305. watchdog('tripal_db', "Cannot update db '$dbname'.", NULL, WATCHDOG_WARNING);
  306. return 0;
  307. }
  308. $result = tripal_core_chado_select('db', array('*'), $sel_values, $sel_options);
  309. }
  310. // return the database object
  311. return $result[0];
  312. }
  313. /**
  314. *
  315. * @ingroup tripal_db_api
  316. */
  317. function tripal_db_add_dbxref($db_id, $accession, $version = '', $description = '') {
  318. $ins_values = array(
  319. 'db_id' => $db_id,
  320. 'accession' => $accession,
  321. 'version' => $version,
  322. 'description' => $description
  323. );
  324. // check to see if the dbxref exists
  325. $sel_values = array(
  326. 'db_id' => $db_id,
  327. 'accession' => $accession,
  328. );
  329. $sel_options = array('statement_name' => 'sel_dbxref_dbacve');
  330. $result = tripal_core_chado_select('dbxref', array('*'), $sel_values, $sel_options);
  331. // if it doesn't already exist then add it
  332. if (count($result) == 0) {
  333. $ins_options = array('statement_name' => 'ins_dbxref_dbacvede');
  334. $success = tripal_core_chado_insert('dbxref', $ins_values, $ins_options);
  335. if (!$success) {
  336. watchdog('tripal_cv', "Failed to insert the dbxref record $accession", NULL, WATCHDOG_WARNING);
  337. return 0;
  338. }
  339. $result = tripal_core_chado_select('dbxref', array('*'), $sel_values, $sel_options);
  340. }
  341. return $result[0];
  342. }