tripal_chado.schema.api.inc 22 KB

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