tripal_chado.schema.api.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <?php
  2. /**
  3. * @defgroup tripal_chado_schema_api Chado Schema API
  4. * @ingroup tripal_chado_api
  5. * @{
  6. * Provides an application programming interface (API) for describing Chado tables.
  7. * This API consists of a set of functions, one for each table in Chado. Each
  8. * function simply returns a Drupal style array that defines the table.
  9. *
  10. * Because Drupal 6 does not handle foreign key (FK) relationships, however FK
  11. * relationships are needed to for Tripal Views. Therefore, FK relationships
  12. * have been added to the schema defintitions below.
  13. *
  14. * The functions provided in this documentation should not be called as is, but if you need
  15. * the Drupal-style array definition for any table, use the following function
  16. * call:
  17. *
  18. * $table_desc = chado_get_schema($table)
  19. *
  20. * where the variable $table contains the name of the table you want to
  21. * retireve. The chado_get_schema function determines the appropriate version of
  22. * Chado and uses the Drupal hook infrastructure to call the appropriate
  23. * hook function to retrieve the table schema.
  24. * @}
  25. */
  26. /**
  27. * Check that any given Chado table exists.
  28. *
  29. * This function is necessary because Drupal's db_table_exists() function will
  30. * not look in any other schema but the one were Drupal is installed
  31. *
  32. * @return
  33. * TRUE/FALSE depending upon whether it exists
  34. *
  35. * @ingroup tripal_chado_schema_api
  36. */
  37. function chado_table_exists($table) {
  38. global $databases;
  39. global $databases;
  40. $default_db = $databases['default']['default']['database'];
  41. $sql = "
  42. SELECT 1
  43. FROM information_schema.tables
  44. WHERE
  45. table_name = :table_name AND
  46. table_schema = :chado AND
  47. table_catalog = :default_db
  48. ";
  49. $args = array(
  50. ':table_name' => $table,
  51. ':chado' => tripal_get_schema_name('chado'),
  52. ':default_db' => $default_db
  53. );
  54. $results = db_query($sql, $args);
  55. $exists = $results->fetchObject();
  56. if (!$exists) {
  57. return FALSE;
  58. }
  59. return TRUE;
  60. }
  61. /**
  62. * Check that any given column in a Chado table exists.
  63. *
  64. * This function is necessary because Drupal's db_field_exists() will not
  65. * look in any other schema but the one were Drupal is installed
  66. *
  67. * @param $table
  68. * The name of the chado table.
  69. * @param $column
  70. * The name of the column in the chado table.
  71. * @return
  72. * TRUE if the column exists for the table in the chado schema and
  73. * FALSE if it does not.
  74. *
  75. * @ingroup tripal_chado_schema_api
  76. */
  77. function chado_column_exists($table, $column) {
  78. global $databases;
  79. $default_db = $databases['default']['default']['database'];
  80. $cached_obj = cache_get('chado_table_columns', 'cache');
  81. $cached_cols = $cached_obj->data;
  82. if (is_array($cached_cols) and
  83. array_key_exists($table, $cached_cols) and
  84. array_key_Exists($column, $cached_cols[$table])) {
  85. return $cached_cols[$table][$column]['exists'];
  86. }
  87. $sql = "
  88. SELECT 1
  89. FROM information_schema.columns
  90. WHERE
  91. table_name = :table_name AND
  92. column_name = :column_name AND
  93. table_schema = :chado AND
  94. table_catalog = :default_db
  95. ";
  96. $args = array(
  97. ':table_name' => $table,
  98. ':column_name' => $column,
  99. ':chado' => tripal_get_schema_name('chado'),
  100. ':default_db' => $default_db
  101. );
  102. $results = db_query($sql, $args);
  103. $exists = $results->fetchField();
  104. if (!$exists) {
  105. $cached_cols[$table][$column]['exists'] = FALSE;
  106. cache_set('chado_table_columns', $cached_cols, 'cache', CACHE_TEMPORARY);
  107. return FALSE;
  108. }
  109. $cached_cols[$table][$column]['exists'] = TRUE;
  110. cache_set('chado_table_columns', $cached_cols, 'cache', CACHE_TEMPORARY);
  111. return TRUE;
  112. }
  113. /**
  114. * Check that any given column in a Chado table exists.
  115. *
  116. * This function is necessary because Drupal's db_field_exists() will not
  117. * look in any other schema but the one were Drupal is installed
  118. *
  119. * @param sequence
  120. * The name of the sequence
  121. * @return
  122. * TRUE if the seqeuence exists in the chado schema and FALSE if it does not.
  123. *
  124. * @ingroup tripal_chado_schema_api
  125. */
  126. function chado_sequence_exists($sequence) {
  127. global $databases;
  128. $default_db = $databases['default']['default']['database'];
  129. $cached_obj = cache_get('chado_sequences', 'cache');
  130. $cached_seqs = $cached_obj->data;
  131. if (is_array($cached_seqs) and array_key_exists($sequence, $cached_seqs)) {
  132. return $cached_seqs[$sequence]['exists'];
  133. }
  134. $sql = "
  135. SELECT 1
  136. FROM information_schema.sequences
  137. WHERE
  138. sequence_name = :sequence_name AND
  139. sequence_schema = :sequence_schema AND
  140. sequence_catalog = :sequence_catalog
  141. ";
  142. $args = array(
  143. ':sequence_name' => $sequence,
  144. ':sequence_schema' => tripal_get_schema_name('chado'),
  145. ':sequence_catalog' => $default_db
  146. );
  147. $results = db_query($sql, $args);
  148. $exists = $results->fetchField();
  149. if (!$exists) {
  150. $cached_seqs[$sequence]['exists'] = FALSE;
  151. cache_set('chado_sequences', $cached_seqs, 'cache', CACHE_TEMPORARY);
  152. return FALSE;
  153. }
  154. $cached_seqs[$sequence]['exists'] = FALSE;
  155. cache_set('chado_sequences', $cached_seqs, 'cache', CACHE_TEMPORARY);
  156. return TRUE;
  157. }
  158. /**
  159. * A Chado-aware replacement for the db_index_exists() function.
  160. *
  161. * @param $table
  162. * The table to be altered.
  163. * @param $name
  164. * The name of the index.
  165. */
  166. function chado_index_exists($table, $name) {
  167. global $databases;
  168. $indexname = $table . '_' . $name . '_idx';
  169. $default_db = $databases['default']['default']['database'];
  170. $sql = "
  171. SELECT 1 as exists
  172. FROM pg_indexes
  173. WHERE indexname = :indexname
  174. ";
  175. $result = db_query($sql, array(':indexname' => $indexname));
  176. $exists = $result->fetchObject();
  177. return $exists->exists;
  178. }
  179. /**
  180. * A Chado-aware wrapper for the db_add_index() function.
  181. *
  182. * @param $table
  183. * The table to be altered.
  184. * @param $name
  185. * The name of the index.
  186. * @param $fields
  187. * An array of field names.
  188. */
  189. function chado_add_index($table, $name, $fields) {
  190. $indexname = $table . '_' . $name . '_idx';
  191. $query = 'CREATE INDEX "' . $indexname . '" ON {' . $table . '} ';
  192. $query .= '(';
  193. $temp = array();
  194. foreach ($fields as $field) {
  195. if (is_array($field)) {
  196. $temp[] = 'substr(' . $field[0] . ', 1, ' . $field[1] . ')';
  197. }
  198. else {
  199. $temp[] = '"' . $field . '"';
  200. }
  201. }
  202. $query .= implode(', ', $temp);
  203. $query .= ')';
  204. return chado_query($query);
  205. }
  206. /**
  207. * Check that any given schema exists.
  208. *
  209. * @param $schema
  210. * The name of the schema to check the existence of
  211. *
  212. * @return
  213. * TRUE/FALSE depending upon whether or not the schema exists
  214. *
  215. * @ingroup tripal_chado_schema_api
  216. */
  217. function chado_dbschema_exists($schema) {
  218. $sql = "
  219. SELECT nspname
  220. FROM pg_namespace
  221. WHERE
  222. has_schema_privilege(nspname, 'USAGE') AND
  223. nspname = :nspname
  224. ORDER BY nspname
  225. ";
  226. $schema = db_query($sql, array(':nspname' => $schema))->fetchField();
  227. if ($schema) {
  228. return TRUE;
  229. }
  230. return FALSE;
  231. }
  232. /**
  233. * Check that the Chado schema exists within the local database
  234. *
  235. * @return
  236. * TRUE/FALSE depending upon whether it exists
  237. *
  238. * @ingroup tripal_chado_schema_api
  239. */
  240. function chado_is_local() {
  241. // This is postgresql-specific code to check the existence of the chado schema
  242. // @coder-ignore: acting on pg_catalog schema rather then drupal schema therefore, table prefixing does not apply
  243. $sql = "
  244. SELECT nspname
  245. FROM pg_namespace
  246. WHERE
  247. has_schema_privilege(nspname, 'USAGE') AND
  248. nspname = 'chado'
  249. ";
  250. $results = db_query($sql);
  251. $name = $results->fetchObject();
  252. if ($name) {
  253. variable_set('chado_schema_exists', FALSE);
  254. return TRUE;
  255. }
  256. else {
  257. variable_set('chado_schema_exists', TRUE);
  258. return FALSE;
  259. }
  260. }
  261. /**
  262. * Check whether chado is installed (either in the same or a different database)
  263. *
  264. * @return
  265. * TRUE/FALSE depending upon whether chado is installed.
  266. *
  267. * @ingroup tripal_chado_schema_api
  268. */
  269. function chado_is_installed() {
  270. global $databases;
  271. // first check if chado is in the $databases variable of the settings.php file
  272. if (array_key_exists('chado', $databases)) {
  273. return TRUE;
  274. }
  275. // check to make sure the chado schema exists
  276. return chado_is_local();
  277. }
  278. /**
  279. * Returns the version number of the currently installed Chado instance.
  280. * It can return the real or effective version. Note, this function
  281. * is executed in the hook_init() of the tripal_chado module which then
  282. * sets the $GLOBAL['exact_chado_version'] and $GLOBAL['chado_version']
  283. * variable. You can access these variables rather than calling this function.
  284. *
  285. * @param $exact
  286. * Set this argument to 1 to retrieve the exact version that is installed.
  287. * Otherwise, this function will set the version to the nearest 'tenth'.
  288. * Chado versioning numbers in the hundreds represent changes to the
  289. * software and not the schema. Changes in the tenth's represent changes
  290. * in the schema.
  291. *
  292. * @param $warn_if_unsupported
  293. * If the currently installed version of Chado is not supported by Tripal
  294. * this generates a Drupal warning.
  295. *
  296. * @returns
  297. * The version of Chado
  298. *
  299. * @ingroup tripal_chado_schema_api
  300. */
  301. function chado_get_version($exact = FALSE, $warn_if_unsupported = FALSE) {
  302. global $databases;
  303. $version = '';
  304. $is_local = 0;
  305. // check that Chado is installed if not return 'uninstalled as the version'
  306. $chado_exists = chado_is_local();
  307. if (!$chado_exists) {
  308. // if it's not in the drupal database check to see if it's specified in the $db_url
  309. // in the settings.php
  310. if (!array_key_exists('chado', $databases)) {
  311. // if it's not in the drupal database or specified in the $db_url then
  312. // return uninstalled as the version
  313. return 'not installed';
  314. }
  315. $is_local = 0;
  316. $previous_db = chado_set_active('chado');
  317. $prop_exists = chado_table_exists('chadoprop');
  318. chado_set_active($previous_db);
  319. }
  320. else {
  321. $is_local = 1;
  322. $prop_exists = chado_table_exists('chadoprop');
  323. }
  324. // if the table doesn't exist then we don't know what version but we know
  325. // it must be 1.11 or older.
  326. if (!$prop_exists) {
  327. $version = "1.11 or older";
  328. }
  329. else {
  330. $sql = "
  331. SELECT value
  332. FROM {chadoprop} CP
  333. INNER JOIN {cvterm} CVT on CVT.cvterm_id = CP.type_id
  334. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  335. WHERE CV.name = 'chado_properties' and CVT.name = 'version'
  336. ";
  337. if (!$is_local) {
  338. $previous_db = chado_set_active('chado');
  339. $results = db_query($sql);
  340. chado_set_active($previous_db);
  341. }
  342. else {
  343. $results = chado_query($sql);
  344. }
  345. $v = $results->fetchObject();
  346. // if we don't have a version in the chadoprop table then it must be
  347. // v1.11 or older
  348. if (!$v) {
  349. $version = "1.11 or older";
  350. }
  351. else {
  352. $version = $v->value;
  353. }
  354. }
  355. // next get the exact Chado version that is installed
  356. $exact_version = $version;
  357. // Tripal only supports v1.11 or newer.. really this is the same as v1.1
  358. // but at the time the v1.11 schema API was written we didn't know that so
  359. // we'll return the version 1.11 so the schema API will work.
  360. if (strcmp($exact_version, '1.11 or older') == 0) {
  361. $exact_version = "1.11";
  362. if ($warn_if_unsupported) {
  363. drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.11. If you are certain this is v1.11
  364. or if Chado was installed using an earlier version of Tripal then all is well. If not please upgrade to v1.11 or later"),
  365. 'warning');
  366. }
  367. }
  368. // if not returing an exact version, return the version to the nearest 10th.
  369. // return 1.2 for all versions of 1.2x
  370. $effective_version = $exact_version;
  371. if (preg_match('/^1\.2\d+$/', $effective_version)) {
  372. $effective_version = "1.2";
  373. }
  374. if ($warn_if_unsupported and ($effective_version < 1.11 and $effective_version != 'not installed')) {
  375. drupal_set_message(t("WARNING: The currently installed version of Chado, v$exact_version, is not fully compatible with Tripal."), 'warning');
  376. }
  377. // if the callee has requested the exact version then return it
  378. if ($exact) {
  379. return $exact_version;
  380. }
  381. return $effective_version;
  382. }
  383. /**
  384. * Retrieves the list of tables in the Chado schema. By default it only returns
  385. * the default Chado tables, but can return custom tables added to the
  386. * Chado schema if requested
  387. *
  388. * @param $include_custom
  389. * Optional. Set as TRUE to include any custom tables created in the
  390. * Chado schema. Custom tables are added to Chado using the
  391. * tripal_chado_chado_create_table() function.
  392. *
  393. * @returns
  394. * An associative array where the key and value pairs are the Chado table names.
  395. *
  396. * @ingroup tripal_chado_schema_api
  397. */
  398. function chado_get_table_names($include_custom = NULL) {
  399. // first get the chado version that is installed
  400. $v = array_key_exists('chado_version', $GLOBALS) ? $GLOBALS["chado_version"] : '';
  401. $tables = array();
  402. if ($v == '1.3') {
  403. $tables_v1_3 = tripal_chado_chado_get_v1_3_tables();
  404. foreach ($tables_v1_3 as $table) {
  405. $tables[$table] = $table;
  406. }
  407. }
  408. if ($v == '1.2') {
  409. $tables_v1_2 = tripal_chado_chado_get_v1_2_tables();
  410. foreach ($tables_v1_2 as $table) {
  411. $tables[$table] = $table;
  412. }
  413. }
  414. if ($v == '1.11' or $v == '1.11 or older') {
  415. $tables_v1_11 = tripal_chado_chado_get_v1_11_tables();
  416. foreach ($tables_v1_11 as $table) {
  417. $tables[$table] = $table;
  418. }
  419. }
  420. // now add in the custom tables too if requested
  421. if ($include_custom) {
  422. $sql = "SELECT table_name FROM {tripal_custom_tables}";
  423. $resource = db_query($sql);
  424. foreach ($resource as $r) {
  425. $tables[$r->table_name] = $r->table_name;
  426. }
  427. }
  428. asort($tables);
  429. return $tables;
  430. }
  431. /**
  432. * Retrieves the chado tables Schema API array.
  433. *
  434. * @param $table
  435. * The name of the table to retrieve. The function will use the appopriate
  436. * Tripal chado schema API hooks (e.g. v1.11 or v1.2).
  437. *
  438. * @returns
  439. * A Drupal Schema API array defining the table.
  440. *
  441. * @ingroup tripal_chado_schema_api
  442. */
  443. function chado_get_schema($table) {
  444. // first get the chado version that is installed
  445. $v = array_key_exists("chado_version", $GLOBALS) ? $GLOBALS["chado_version"] : '';
  446. // get the table array from the proper chado schema
  447. $v = preg_replace("/\./", "_", $v); // reformat version for hook name
  448. // Call the module_invoke_all.
  449. $hook_name = "chado_schema_v" . $v . "_" . $table;
  450. $table_arr = module_invoke_all($hook_name);
  451. // If the module_invoke_all returned nothing then let's make sure there isn't
  452. // An API call we can call directly. The only time this occurs is
  453. // during an upgrade of a major Drupal version and tripal_core is disabled.
  454. if ((!$table_arr or !is_array($table_arr)) and
  455. function_exists('tripal_core_' . $hook_name)) {
  456. $api_hook = "tripal_core_" . $hook_name;
  457. $table_arr = $api_hook();
  458. }
  459. // if the table_arr is empty then maybe this is a custom table
  460. if (!is_array($table_arr) or count($table_arr) == 0) {
  461. $table_arr = chado_get_custom_table_schema($table);
  462. }
  463. return $table_arr;
  464. }
  465. /**
  466. * Retrieves the schema in an array for the specified custom table.
  467. *
  468. * @param $table
  469. * The name of the table to create.
  470. *
  471. * @return
  472. * A Drupal-style Schema API array definition of the table. Returns
  473. * FALSE on failure.
  474. *
  475. * @ingroup tripal_chado_schema_api
  476. */
  477. function chado_get_custom_table_schema($table) {
  478. $sql = "SELECT schema FROM {tripal_custom_tables} WHERE table_name = :table_name";
  479. $results = db_query($sql, array(':table_name' => $table));
  480. $custom = $results->fetchObject();
  481. if (!$custom) {
  482. return FALSE;
  483. }
  484. else {
  485. return unserialize($custom->schema);
  486. }
  487. }
  488. /**
  489. * Returns all chado base tables.
  490. *
  491. * Base tables are those that contain the primary record for a data type. For
  492. * example, feature, organism, stock, are all base tables. Other tables
  493. * include linker tables (which link two or more base tables), property tables,
  494. * and relationship tables. These provide additional information about
  495. * primary data records and are therefore not base tables. This function
  496. * retreives only the list of tables that are considered 'base' tables.
  497. *
  498. * @return
  499. * An array of base table names.
  500. *
  501. * @ingroup tripal_chado_schema_api
  502. */
  503. function chado_get_base_tables() {
  504. // Initialize the base tables with those tables that are missing a type.
  505. // Ideally they should have a type, but that's for a future version of Chado.
  506. $base_tables = array('organism', 'project', 'analysis', 'biomaterial');
  507. // We'll use the cvterm table to guide which tables are base tables. Typically
  508. // base tables (with a few exceptions) all have a type. Iterate through the
  509. // referring tables.
  510. $schema = chado_get_schema('cvterm');
  511. $referring = $schema['referring_tables'];
  512. foreach ($referring as $tablename) {
  513. // Ignore the cvterm tables, relationships, chadoprop tables.
  514. if ($tablename == 'cvterm_dbxref' || $tablename == 'cvterm_relationship' ||
  515. $tablename == 'cvtermpath' || $tablename == 'cvtermprop' || $tablename == 'chadoprop' ||
  516. $tablename == 'cvtermsynonym' || preg_match('/_relationship$/', $tablename) ||
  517. preg_match('/_cvterm$/', $tablename) ||
  518. // Ignore prop tables
  519. preg_match('/prop$/', $tablename) || preg_match('/prop_.+$/', $tablename) ||
  520. // Ignore nd_tables
  521. preg_match('/^nd_/', $tablename)) {
  522. continue;
  523. }
  524. else {
  525. array_push($base_tables, $tablename);
  526. }
  527. }
  528. // Remove any linker tables that have snuck in. Linker tables are those
  529. // whose foreign key constraints link to two or more base table.
  530. $final_list = array();
  531. foreach ($base_tables as $i => $tablename) {
  532. $num_links = 0;
  533. $schema = chado_get_schema($tablename);
  534. $fkeys = $schema['foreign keys'];
  535. foreach ($fkeys as $fkid => $details) {
  536. $fktable = $details['table'];
  537. if (in_array($fktable, $base_tables)) {
  538. $num_links++;
  539. }
  540. }
  541. if ($num_links < 2) {
  542. $final_list[] = $tablename;
  543. }
  544. }
  545. // Sort the tables and return the list.
  546. sort($final_list);
  547. return $final_list;
  548. }
  549. /**
  550. * Get information about which Chado base table a cvterm is mapped to.
  551. *
  552. * Vocbulary terms that represent content types in Tripal must be mapped to
  553. * Chado tables. A cvterm can only be mapped to one base table in Chado.
  554. * This function will return an object that contains the chado table and
  555. * foreign key field to which the cvterm is mapped. The 'chado_table' property
  556. * of the returned object contains the name of the table, and the 'chado_field'
  557. * property contains the name of the foreign key field (e.g. type_id), and the
  558. * 'cvterm' property contains a cvterm object.
  559. *
  560. * @params
  561. * An associative array that contains the following keys:
  562. * - cvterm_id: the cvterm ID value for the term.
  563. * - vocabulary: the short name for the vocabulary (e.g. SO, GO, PATO)
  564. * - accession: the accession for the term.
  565. * - bundle_id: the ID for the bundle to which a term is associated.
  566. * The 'vocabulary' and 'accession' must be used together, the 'cvterm_id' can
  567. * be used on it's own.
  568. * @return
  569. * An object containing the chado_table and chado_field properties or NULL if
  570. * if no mapping was found for the term.
  571. */
  572. function chado_get_cvterm_mapping($params) {
  573. $cvterm_id = array_key_exists('cvterm_id', $params) ? $params['cvterm_id'] : NULL;
  574. $vocabulary = array_key_exists('vocabulary', $params) ? $params['vocabulary'] : NULL;
  575. $accession = array_key_exists('accession', $params) ? $params['accession'] : NULL;
  576. if ($cvterm_id) {
  577. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
  578. }
  579. else if ($vocabulary and $accession) {
  580. $match = array(
  581. 'dbxref_id' => array(
  582. 'db_id' => array(
  583. 'name' => $vocabulary,
  584. ),
  585. 'accession' => $accession,
  586. ),
  587. );
  588. $cvterm = chado_generate_var('cvterm', $match);
  589. }
  590. $result = db_select('chado_cvterm_mapping', 'tcm')
  591. ->fields('tcm')
  592. ->condition('cvterm_id', $cvterm->cvterm_id)
  593. ->execute();
  594. $result = $result->fetchObject();
  595. if ($result) {
  596. $result->cvterm = $cvterm;
  597. }
  598. return $result;
  599. }