tripal_db.api.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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.
  273. *
  274. * @ingroup tripal_db_api
  275. */
  276. function tripal_db_add_db($dbname, $description='', $url='', $urlprefix='', $update=0) {
  277. $values = array(
  278. 'name' => $dbname,
  279. 'description' => $description,
  280. 'url' => $url,
  281. 'urlprefix' => $urlprefix
  282. );
  283. $db_sql = "SELECT * FROM {db} WHERE name ='%s'";
  284. $db = db_fetch_object(db_query($db_sql, $dbname));
  285. if (!$db) {
  286. if (!tripal_core_chado_insert('db', $values)) {
  287. watchdog('tripal_db', "Cannot create db '$dbname'.", NULL, WATCHDOG_WARNING);
  288. return 0;
  289. }
  290. $db = tripal_core_chado_select('db', array('*'), $values);
  291. }
  292. elseif ($update) {
  293. $match = array('db_id' => $db->db_id);
  294. if (!tripal_core_chado_update('db', $match, $values)) {
  295. watchdog('tripal_db', "Cannot update db '$dbname'.", NULL, WATCHDOG_WARNING);
  296. return 0;
  297. }
  298. $db = tripal_core_chado_select('db', array('*'), $values);
  299. }
  300. else {
  301. return $db;
  302. }
  303. }
  304. /**
  305. *
  306. * @ingroup tripal_db_api
  307. */
  308. function tripal_db_add_dbxref($db_id, $accession, $version='', $description='') {
  309. // check to see if the dbxref exists if not, add it
  310. $dbxsql = "
  311. SELECT DBX.dbxref_id, DBX.db_id, DBX.description, DBX.version, DBX.accession,
  312. DB.name as db_name
  313. FROM {dbxref} DBX
  314. INNER JOIN db DB on DB.db_id = DBX.db_id
  315. WHERE DBX.db_id = %d and DBX.accession = '%s'
  316. ";
  317. $dbxref = db_fetch_object(db_query($dbxsql, $db_id, $accession));
  318. if (!$dbxref) {
  319. $sql = "
  320. INSERT INTO {dbxref} (db_id, accession, version, description)
  321. VALUES (%d,'%s','%s','%s')
  322. ";
  323. if (!db_query($sql, $db_id, $accession, $version, $description)) {
  324. watchdog('tripal_cv', "Failed to insert the dbxref record $accession", NULL, WATCHDOG_WARNING);
  325. return 0;
  326. }
  327. print "Added Dbxref accession: $accession\n";
  328. $dbxref = db_fetch_object(db_query($dbxsql, $db_id, $accession));
  329. }
  330. return $dbxref;
  331. }