tripal_chado.schema.api.inc 19 KB

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