tripal_db.api.inc 9.9 KB

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