tripal_chado.db.api.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage references to
  5. * external databases
  6. */
  7. /**
  8. * @defgroup tripal_chado_database_api Chado DB
  9. * @ingroup tripal_chado_api
  10. * @{
  11. * @}
  12. */
  13. /**
  14. * Retrieves a chado db variable.
  15. *
  16. * Example Usage:
  17. * @code
  18. * $select_values = array(
  19. * 'name' => 'SOFP'
  20. * );
  21. * $db_object = chado_get_db($select_values);
  22. * @endcode
  23. * The above code selects the SOFP db and returns the following object:
  24. * @code
  25. * $db_object = stdClass Object (
  26. * [db_id] => 49
  27. * [name] => SOFP
  28. * [description] =>
  29. * [urlprefix] =>
  30. * [url] =>
  31. * );
  32. * @endcode
  33. *
  34. * @param $identifier
  35. * An array with the key stating what the identifier is. Supported keys (only
  36. * on of the following unique keys is required):
  37. * - db_id: the chado db.db_id primary key.
  38. * - name: the chado db.name field (assume unique).
  39. * @param $options
  40. * An array of options. Supported keys include:
  41. * - Any keys supported by chado_generate_var(). See that function
  42. * definition for additional details.
  43. *
  44. * NOTE: the $identifier parameter can really be any array similar to $values
  45. * passed into chado_select_record(). It should fully specify the db record to
  46. * be returned.
  47. *
  48. * @return
  49. * If unique values were passed in as an identifier then an object describing
  50. * the cv will be returned (will be a chado variable from
  51. * chado_generate_var()). Otherwise, an array of objects will be returned.
  52. *
  53. * @ingroup tripal_chado_database_api
  54. */
  55. function chado_get_db($identifiers, $options = array()) {
  56. // Set Defaults.
  57. if (!isset($options['include_fk'])) {
  58. // Tells chado_generate_var not to follow any foreign keys.
  59. $options['include_fk'] = array();
  60. }
  61. // Error Checking of parameters.
  62. if (!is_array($identifiers)) {
  63. tripal_report_error(
  64. 'tripal_chado_database_api',
  65. TRIPAL_ERROR,
  66. "chado_get_db: The identifier passed in is expected to be an array with the key
  67. matching a column name in the db table (ie: db_id or name). You passed in %identifier.",
  68. array(
  69. '%identifier'=> print_r($identifiers, TRUE)
  70. )
  71. );
  72. }
  73. elseif (empty($identifiers)) {
  74. tripal_report_error(
  75. 'tripal_chado_database_api',
  76. TRIPAL_ERROR,
  77. "chado_get_db: You did not pass in anything to identify the db you want. The identifier
  78. is expected to be an array with the key matching a column name in the db table
  79. (ie: db_id or name). You passed in %identifier.",
  80. array(
  81. '%identifier'=> print_r($identifiers, TRUE)
  82. )
  83. );
  84. }
  85. // Try to get the db.
  86. $db = chado_generate_var(
  87. 'db',
  88. $identifiers,
  89. $options
  90. );
  91. // Ensure the db is singular. If it's an array then it is not singular.
  92. if (is_array($db)) {
  93. tripal_report_error(
  94. 'tripal_chado_database_api',
  95. TRIPAL_ERROR,
  96. "chado_get_db: The identifiers you passed in were not unique. You passed in %identifier.",
  97. array(
  98. '%identifier'=> print_r($identifiers, TRUE)
  99. )
  100. );
  101. }
  102. // Report an error if $db is FALSE since then chado_generate_var has failed.
  103. elseif ($db === FALSE) {
  104. tripal_report_error(
  105. 'tripal_chado_database_api',
  106. TRIPAL_ERROR,
  107. "chado_get_db: chado_generate_var() failed to return a db based on the identifiers
  108. you passed in. You should check that your identifiers are correct, as well as, look
  109. for a chado_generate_var error for additional clues. You passed in %identifier.",
  110. array(
  111. '%identifier'=> print_r($identifiers, TRUE)
  112. )
  113. );
  114. }
  115. // Else, as far we know, everything is fine so give them their db :)
  116. else {
  117. return $db;
  118. }
  119. }
  120. /**
  121. * 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_chado_database_api
  128. */
  129. function chado_get_db_select_options() {
  130. $dbs = chado_query("SELECT db_id, name FROM {db} ORDER BY name");
  131. $options = array();
  132. $options[] = 'Select a Database';
  133. foreach ($dbs as $db) {
  134. $options[$db->db_id] = $db->name;
  135. }
  136. return $options;
  137. }
  138. /**
  139. * Retrieves a chado database reference variable.
  140. *
  141. * Example Usage:
  142. * @code
  143. * $identifiers = array(
  144. * 'accession' => 'synonym',
  145. * 'db_id' => array(
  146. * 'name' => 'SOFP'
  147. * )
  148. * );
  149. * $dbxref_object = chado_get_dbxref($identifiers);
  150. * @endcode
  151. * The above code selects the synonym database reference and returns the
  152. * following object:
  153. * @code
  154. * $dbxref_object = stdClass Object (
  155. * [dbxref_id] => 2581
  156. * [accession] => synonym
  157. * [description] =>
  158. * [version] =>
  159. * [db_db_id] => 49
  160. * [db_name] => SOFP
  161. * [db_description] =>
  162. * [db_urlprefix] =>
  163. * [db_url] =>
  164. * );
  165. * @endcode
  166. *
  167. * @param $identifier
  168. * An array apropriate for use with the chado_generate_var for uniquely
  169. * identifying a dbxref record. Alternatively, there are also some specially
  170. * handled keys. They are:
  171. * - property: An array/object describing the property to select records for.
  172. * It should at least have either a type_name (if unique across cvs) or
  173. * type_id. Other supported keys include: cv_id/cv_name (of the type),
  174. * value and rank.
  175. * @param $options
  176. * An array of options. Supported keys include:
  177. * - Any keys supported by chado_generate_var(). See that function
  178. * definition for additional details.
  179. *
  180. * NOTE: the $identifier parameter can really be any array similar to $values
  181. * passed into chado_select_record(). It should fully specify the dbxref record
  182. * to be returned.
  183. *
  184. * @return
  185. * If unique values were passed in as an identifier then an object describing
  186. * the dbxref will be returned (will be a chado variable from
  187. * chado_generate_var()). Otherwise, FALSE will be returned.
  188. *
  189. * @ingroup tripal_chado_database_api
  190. */
  191. function chado_get_dbxref($identifiers, $options = array()) {
  192. // Set Defaults.
  193. if (!isset($options['include_fk'])) {
  194. // Tells chado_generate_var not only expand the db.
  195. $options['include_fk'] = array('db_id' => TRUE);
  196. }
  197. // Error Checking of parameters.
  198. if (!is_array($identifiers)) {
  199. tripal_report_error('tripal_db_api', TRIPAL_ERROR,
  200. "chado_get_dbxref: The identifier passed in is expected to be an array with the key
  201. matching a column name in the dbxref table (ie: dbxref_id or name). You passed in %identifier.",
  202. array('%identifier'=> print_r($identifiers, TRUE))
  203. );
  204. }
  205. elseif (empty($identifiers)) {
  206. tripal_report_error('tripal_db_api', TRIPAL_ERROR,
  207. "chado_get_dbxref: You did not pass in anything to identify the dbxref you want. The identifier
  208. is expected to be an array with the key matching a column name in the dbxref table
  209. (ie: dbxref_id or name). You passed in %identifier.",
  210. array('%identifier'=> print_r($identifiers, TRUE))
  211. );
  212. }
  213. // If one of the identifiers is property then use chado_get_record_with_property().
  214. if (isset($identifiers['property'])) {
  215. $property = $identifiers['property'];
  216. unset($identifiers['property']);
  217. $dbxref = chado_get_record_with_property(
  218. array('table' => 'dbxref', 'base_records' => $identifiers),
  219. array('type_name' => $property),
  220. $options
  221. );
  222. }
  223. // Else we have a simple case and we can just use chado_generate_var to get
  224. // the analysis.
  225. else {
  226. $dbxref = chado_generate_var('dbxref', $identifiers, $options);
  227. }
  228. // Ensure the dbxref is singular. If it's an array then it is not singular.
  229. if (is_array($dbxref)) {
  230. tripal_report_error('tripal_db_api', TRIPAL_ERROR,
  231. "chado_get_dbxref: The identifiers you passed in were not unique. You passed in %identifier.",
  232. array('%identifier'=> print_r($identifiers, TRUE))
  233. );
  234. }
  235. // Report an error if $dbxref is FALSE since then chado_generate_var has
  236. // failed.
  237. elseif ($dbxref === FALSE) {
  238. tripal_report_error(
  239. 'tripal_db_api',
  240. TRIPAL_ERROR,
  241. "chado_get_dbxref: chado_generate_var() failed to return a dbxref based on the identifiers
  242. you passed in. You should check that your identifiers are correct, as well as, look
  243. for a chado_generate_var error for additional clues. You passed in %identifier.",
  244. array(
  245. '%identifier'=> print_r($identifiers, TRUE)
  246. )
  247. );
  248. }
  249. // Else, as far we know, everything is fine so give them their dbxref :)
  250. else {
  251. return $dbxref;
  252. }
  253. }
  254. /**
  255. * Generates a URL for the controlled vocabulary term.
  256. *
  257. * If the URL and URL prefix are provided for the database record of a cvterm
  258. * then a URL can be created for the term. By default, the db.name and
  259. * dbxref.accession are concatenated and appended to the end of the db.urlprefix.
  260. * But Tripal supports the use of {db} and {accession} tokens when if present
  261. * in the db.urlprefix string will be replaced with the db.name and
  262. * dbxref.accession respectively.
  263. *
  264. * @param $dbxref
  265. * A dbxref object as created by the chado_generate_var() function.
  266. *
  267. * @return
  268. * A string containing the URL.
  269. *
  270. * @ingroup tripal_chado_database_api
  271. */
  272. function chado_get_dbxref_url($dbxref) {
  273. $final_url = '';
  274. // Create the URL for the term.
  275. if ($dbxref->db_id->urlprefix) {
  276. $db_count = 0;
  277. $acc_count = 0;
  278. $url = $dbxref->db_id->urlprefix;
  279. // If the URL prefix has replacement tokens then use those.
  280. $url = preg_replace('/\{db\}/', $dbxref->db_id->name, $url, -1, $db_count);
  281. $url = preg_replace('/\{accession\}/', $dbxref->accession, $url, -1, $acc_count);
  282. $final_url = $url;
  283. // If no replacements were made above then tokens weren't used and we can
  284. // default to just appending the db name and accession to the end.
  285. if (!$db_count and !$acc_count) {
  286. $final_url = $dbxref->db_id->urlprefix . $dbxref->db_id->name . ':' . $dbxref->accession;
  287. }
  288. // If the URL prefix is relative then convert it to a full URL.
  289. if (!preg_match('/^(http|https)/', $final_url)) {
  290. $final_url = url($final_url, array('absolute' => TRUE));
  291. }
  292. }
  293. return $final_url;
  294. }
  295. /**
  296. * Adds a new database to the Chado DB table and returns the DB object.
  297. *
  298. * @param $values
  299. * An associative array of the values of the db (those to be inserted):
  300. * - name: The name of the database. This name is usually used as the prefix
  301. * for CV term accessions.
  302. * - description: (Optional) A description of the database. By default no
  303. * description is required.
  304. * - url: (Optional) The URL for the database.
  305. * - urlprefix: (Optional) The URL that is to be used as a prefix when
  306. * constructing a link to a database term.
  307. * @param $options
  308. * Optional. An associative array of options that can include:
  309. * - update_existing: Set this to '1' to force an update of the database if it
  310. * already exists. The default is to not update. If the database exists
  311. * then nothing is added.
  312. *
  313. * @return
  314. * An object populated with fields from the newly added database. If the
  315. * database already exists it returns the values in the current entry.
  316. *
  317. * @ingroup tripal_chado_database_api
  318. */
  319. function chado_insert_db($values, $options = array()) {
  320. // Default Values.
  321. $dbname = $values['name'];
  322. $description = (isset($values['description'])) ? $values['description'] : '';
  323. $url = (isset($values['url'])) ? $values['url'] : '';
  324. $urlprefix = (isset($values['urlprefix'])) ? $values['urlprefix'] : '';
  325. $update = (isset($options['update_existing'])) ? $options['update_existing'] : TRUE;
  326. // Build the values array for inserting/updating.
  327. $ins_values = array(
  328. 'name' => $dbname,
  329. 'description' => $description,
  330. 'url' => $url,
  331. 'urlprefix' => $urlprefix
  332. );
  333. // Get the database record if it already exists.
  334. $sel_values = array('name' => $dbname);
  335. $result = chado_select_record('db', array('*'), $sel_values);
  336. // If it does not exists then add it.
  337. if (count($result) == 0) {
  338. $ins_options = array('statement_name' => 'ins_db_nadeurur');
  339. $success = chado_insert_record('db', $ins_values, $ins_options);
  340. if (!$success) {
  341. tripal_report_error('tripal_chado', TRIPAL_WARNING, "Cannot create db '$dbname'.", NULL);
  342. return 0;
  343. }
  344. $result = chado_select_record('db', array('*'), $sel_values);
  345. }
  346. // If it exists and update is enabled the do the update.
  347. elseif ($update) {
  348. $upd_options = array('statement_name' => 'upd_db_nadeurur');
  349. $success = chado_update_record('db', $sel_values, $ins_values, $upd_options);
  350. if (!$success) {
  351. tripal_report_error('tripal_chado', TRIPAL_WARNING, "Cannot update db '$dbname'.", NULL);
  352. return 0;
  353. }
  354. $result = chado_select_record('db', array('*'), $sel_values);
  355. }
  356. // Return the database object.
  357. return $result[0];
  358. }
  359. /**
  360. * Add a database reference.
  361. *
  362. * @param $values
  363. * An associative array of the values to be inserted including:
  364. * - db_id: the database_id of the database the reference is from.
  365. * - accession: the accession.
  366. * - version: (Optional) The version of the database reference.
  367. * - description: (Optional) A description of the database reference.
  368. *
  369. * @return
  370. * The newly inserted dbxref as an object, similar to that returned by
  371. * the chado_select_record() function.
  372. *
  373. * @ingroup tripal_chado_database_api
  374. */
  375. function chado_insert_dbxref($values) {
  376. $db_id = $values['db_id'];
  377. $accession = $values['accession'];
  378. $version = (isset($values['version'])) ? $values['version'] : '';
  379. $description = (isset($values['description'])) ? $values['description'] : '';
  380. $ins_values = array(
  381. 'db_id' => $db_id,
  382. 'accession' => $accession,
  383. 'version' => $version,
  384. 'description' => $description
  385. );
  386. // Check to see if the dbxref exists.
  387. $sel_values = array(
  388. 'db_id' => $db_id,
  389. 'accession' => $accession,
  390. 'version' => $version
  391. );
  392. $result = chado_select_record('dbxref', array('*'), $sel_values);
  393. // If it doesn't already exist then add it.
  394. if (!$result) {
  395. $success = chado_insert_record('dbxref', $ins_values);
  396. if (!$success) {
  397. tripal_report_error('tripal_chado', TRIPAL_WARNING, "Failed to insert the dbxref record $accession", NULL);
  398. return 0;
  399. }
  400. $result = chado_select_record('dbxref', array('*'), $sel_values);
  401. }
  402. if (isset($result[0])) {
  403. return $result[0];
  404. }
  405. else {
  406. return FALSE;
  407. }
  408. }
  409. /**
  410. * Add a record to a database reference linking table (ie: feature_dbxref).
  411. *
  412. * @param $basetable
  413. * The base table for which the dbxref should be associated. Thus to associate
  414. * a dbxref with a feature the basetable=feature and dbxref_id is added to the
  415. * feature_dbxref table.
  416. * @param $record_id
  417. * The primary key of the basetable to associate the dbxref with. This should
  418. * be in integer.
  419. * @param $dbxref
  420. * An associative array describing the dbxref. Valid keys include:
  421. * 'accession' => the accession for the dbxref, 'db_name' => the name of the
  422. * database the dbxref belongs to.
  423. * 'db_id' => the primary key of the database the dbxref belongs to.
  424. * @param $options
  425. * An associative array of options. Valid keys include:
  426. * - insert_dbxref: Insert the dbxref if it doesn't already exist. TRUE is
  427. * the default.
  428. *
  429. * @ingroup tripal_chado_database_api
  430. */
  431. function chado_associate_dbxref($basetable, $record_id, $dbxref, $options = array()) {
  432. $linking_table = $basetable . '_dbxref';
  433. $foreignkey_name = $basetable . '_id';
  434. // Default Values.
  435. $options['insert_dbxref'] = (isset($options['insert_dbxref'])) ? $options['insert_dbxref'] : TRUE;
  436. // If the dbxref_id is set then we know it already exists.
  437. // Otherwise, select to check.
  438. if (!isset($dbxref['dbxref_id'])) {
  439. $values = array(
  440. 'accession' => $dbxref['accession'],
  441. );
  442. if (isset($dbxref['db_id'])) {
  443. $values['db_id'] = $dbxref['db_id'];
  444. } elseif (isset($dbxref['db_name'])) {
  445. $values['db_id'] = array(
  446. 'name' => $dbxref['db_name']
  447. );
  448. }
  449. else {
  450. tripal_report_error(
  451. 'tripal_chado_database_api',
  452. TRIPAL_WARNING,
  453. "chado_associate_dbxref: The dbxref needs to have either the db_name or db_id
  454. supplied. You were trying to associate a dbxref with the %base %record_id
  455. and supplied the dbxref values: %dbxref.",
  456. array('%base' => $basetable, '%record_id' => $record_id, '%dbxref' => print_r($dbxref,TRUE))
  457. );
  458. return FALSE;
  459. }
  460. $select = chado_select_record('dbxref',array('*'), $values);
  461. if ($select) {
  462. $dbxref['dbxref_id'] = $select[0]->dbxref_id;
  463. }
  464. elseif ($options['insert_dbxref']) {
  465. // Insert the dbxref.
  466. $insert = chado_insert_dbxref($values);
  467. if (isset($insert->dbxref_id)) {
  468. $dbxref['dbxref_id'] = $insert->dbxref_id;
  469. }
  470. else {
  471. tripal_report_error(
  472. 'tripal_chado_database_api',
  473. TRIPAL_WARNING,
  474. "chado_associate_dbxref: Unable to insert the dbxref using the dbxref values: %dbxref.",
  475. array('%dbxref' => print_r($dbxref,TRUE))
  476. );
  477. return FALSE;
  478. }
  479. }
  480. else {
  481. tripal_report_error(
  482. 'tripal_api',
  483. TRIPAL_WARNING,
  484. "chado_associate_dbxref: The dbxref doesn't already exist. You supplied the dbxref values: %dbxref.",
  485. array('%dbxref' => print_r($dbxref,TRUE))
  486. );
  487. return FALSE;
  488. }
  489. }
  490. // Now add the link between the record & dbxref.
  491. if ($dbxref['dbxref_id'] > 0) {
  492. $values = array(
  493. 'dbxref_id' => $dbxref['dbxref_id'],
  494. $foreignkey_name => $record_id
  495. );
  496. $result = chado_select_record($linking_table, array('*'), $values);
  497. // If it doesn't already exist then add it.
  498. if (!$result) {
  499. $success = chado_insert_record($linking_table, $values);
  500. if (!$success) {
  501. tripal_report_error(
  502. 'tripal_api',
  503. TRIPAL_WARNING,
  504. "Failed to insert the %base record %accession",
  505. array('%base' => $linking_table, '%accession' => $dbxref['accession'])
  506. );
  507. return FALSE;
  508. }
  509. $result = chado_select_record($linking_table, array('*'), $values);
  510. }
  511. if (isset($result[0])) {
  512. return $result[0];
  513. }
  514. else {
  515. return FALSE;
  516. }
  517. }
  518. return FALSE;
  519. }
  520. /**
  521. * This function is intended to be used in autocomplete forms
  522. * for searching for accession that begin with the provided string.
  523. *
  524. * @param $db_id
  525. * The DB ID in which to search for the term.
  526. * @param $string
  527. * The string to search for.
  528. *
  529. * @return
  530. * A json array of terms that begin with the provided string.
  531. *
  532. * @ingroup tripal_chado_database_api
  533. */
  534. function chado_autocomplete_dbxref($db_id, $string = '') {
  535. if (!$db_id) {
  536. return drupal_json_output(array());
  537. }
  538. $sql = "
  539. SELECT dbxref_id, accession
  540. FROM {dbxref}
  541. WHERE db_id = :db_id and lower(accession) like lower(:accession)
  542. ORDER by accession
  543. LIMIT 25 OFFSET 0
  544. ";
  545. $results = chado_query($sql, array(':db_id' => $db_id, ':accession' => $string . '%'));
  546. $items = array();
  547. foreach ($results as $ref) {
  548. $items[$ref->accession] = $ref->accession;
  549. }
  550. drupal_json_output($items);
  551. }