tripal_chado.schema.api.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. // We need to hardcode the schema here rather than rely on the db_query
  365. // function to settle it because the db_query function needs to know the
  366. // Chado version to make the table prefixes set properly (chicken and
  367. // egg problem).
  368. $chado_schema = chado_get_schema_name('chado');
  369. $sql = "
  370. SELECT value
  371. FROM $chado_schema.chadoprop CP
  372. INNER JOIN $chado_schema.cvterm CVT on CVT.cvterm_id = CP.type_id
  373. INNER JOIN $chado_schema.cv CV on CVT.cv_id = CV.cv_id
  374. WHERE CV.name = 'chado_properties' and CVT.name = 'version'
  375. ";
  376. if (!$is_local) {
  377. $previous_db = chado_set_active('chado');
  378. $results = db_query($sql);
  379. chado_set_active($previous_db);
  380. }
  381. else {
  382. $results = chado_query($sql);
  383. }
  384. $v = $results->fetchObject();
  385. // if we don't have a version in the chadoprop table then it must be
  386. // v1.11 or older
  387. if (!$v) {
  388. $version = "1.11 or older";
  389. }
  390. else {
  391. $version = $v->value;
  392. }
  393. }
  394. // next get the exact Chado version that is installed
  395. $exact_version = $version;
  396. // Tripal only supports v1.11 or newer.. really this is the same as v1.1
  397. // but at the time the v1.11 schema API was written we didn't know that so
  398. // we'll return the version 1.11 so the schema API will work.
  399. if (strcmp($exact_version, '1.11 or older') == 0) {
  400. $exact_version = "1.11";
  401. if ($warn_if_unsupported) {
  402. drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.11. If you are certain this is v1.11
  403. or if Chado was installed using an earlier version of Tripal then all is well. If not please upgrade to v1.11 or later"),
  404. 'warning');
  405. }
  406. }
  407. // if not returing an exact version, return the version to the nearest 10th.
  408. // return 1.2 for all versions of 1.2x
  409. $effective_version = $exact_version;
  410. if (preg_match('/^1\.2\d+$/', $effective_version)) {
  411. $effective_version = "1.2";
  412. }
  413. else {
  414. if (preg_match('/^1\.3\d+$/', $effective_version)) {
  415. $effective_version = "1.3";
  416. }
  417. }
  418. if ($warn_if_unsupported and ($effective_version < 1.11 and $effective_version != 'not installed')) {
  419. drupal_set_message(t("WARNING: The currently installed version of Chado, v$exact_version, is not fully compatible with Tripal."), 'warning');
  420. }
  421. // if the callee has requested the exact version then return it
  422. if ($exact) {
  423. return $exact_version;
  424. }
  425. return $effective_version;
  426. }
  427. /**
  428. * Retrieves the list of tables in the Chado schema. By default it only
  429. * returns
  430. * the default Chado tables, but can return custom tables added to the
  431. * Chado schema if requested
  432. *
  433. * @param $include_custom
  434. * Optional. Set as TRUE to include any custom tables created in the
  435. * Chado schema. Custom tables are added to Chado using the
  436. * tripal_chado_chado_create_table() function.
  437. *
  438. * @returns
  439. * An associative array where the key and value pairs are the Chado table
  440. * names.
  441. *
  442. * @ingroup tripal_chado_schema_api
  443. */
  444. function chado_get_table_names($include_custom = NULL) {
  445. // first get the chado version that is installed
  446. $v = array_key_exists('chado_version', $GLOBALS) ? $GLOBALS["chado_version"] : '';
  447. $tables = [];
  448. if ($v == '1.3') {
  449. $tables_v1_3 = tripal_chado_chado_get_v1_3_tables();
  450. foreach ($tables_v1_3 as $table) {
  451. $tables[$table] = $table;
  452. }
  453. }
  454. if ($v == '1.2') {
  455. $tables_v1_2 = tripal_chado_chado_get_v1_2_tables();
  456. foreach ($tables_v1_2 as $table) {
  457. $tables[$table] = $table;
  458. }
  459. }
  460. if ($v == '1.11' or $v == '1.11 or older') {
  461. $tables_v1_11 = tripal_chado_chado_get_v1_11_tables();
  462. foreach ($tables_v1_11 as $table) {
  463. $tables[$table] = $table;
  464. }
  465. }
  466. // Now add in the custom tables too if requested
  467. if ($include_custom) {
  468. $sql = "SELECT table_name FROM {tripal_custom_tables}";
  469. $resource = db_query($sql);
  470. foreach ($resource as $r) {
  471. $tables[$r->table_name] = $r->table_name;
  472. }
  473. }
  474. asort($tables);
  475. return $tables;
  476. }
  477. /**
  478. * Retrieves the chado tables Schema API array.
  479. *
  480. * @param $table
  481. * The name of the table to retrieve. The function will use the appopriate
  482. * Tripal chado schema API hooks (e.g. v1.11 or v1.2).
  483. *
  484. * @returns
  485. * A Drupal Schema API array defining the table.
  486. *
  487. * @ingroup tripal_chado_schema_api
  488. */
  489. function chado_get_schema($table) {
  490. // first get the chado version that is installed
  491. $v = array_key_exists("chado_version", $GLOBALS) ? $GLOBALS["chado_version"] : '';
  492. // get the table array from the proper chado schema
  493. $v = preg_replace("/\./", "_", $v); // reformat version for hook name
  494. // Call the module_invoke_all.
  495. $hook_name = "chado_schema_v" . $v . "_" . $table;
  496. $table_arr = module_invoke_all($hook_name);
  497. // If the module_invoke_all returned nothing then let's make sure there isn't
  498. // An API call we can call directly. The only time this occurs is
  499. // during an upgrade of a major Drupal version and tripal_core is disabled.
  500. if ((!$table_arr or !is_array($table_arr)) and
  501. function_exists('tripal_chado_' . $hook_name)) {
  502. $api_hook = "tripal_chado_" . $hook_name;
  503. $table_arr = $api_hook();
  504. }
  505. // if the table_arr is empty then maybe this is a custom table
  506. if (!is_array($table_arr) or count($table_arr) == 0) {
  507. $table_arr = chado_get_custom_table_schema($table);
  508. }
  509. return $table_arr;
  510. }
  511. /**
  512. * Retrieves the schema in an array for the specified custom table.
  513. *
  514. * @param $table
  515. * The name of the table to create.
  516. *
  517. * @return
  518. * A Drupal-style Schema API array definition of the table. Returns
  519. * FALSE on failure.
  520. *
  521. * @ingroup tripal_chado_schema_api
  522. */
  523. function chado_get_custom_table_schema($table) {
  524. $sql = "SELECT schema FROM {tripal_custom_tables} WHERE table_name = :table_name";
  525. $results = db_query($sql, [':table_name' => $table]);
  526. $custom = $results->fetchObject();
  527. if (!$custom) {
  528. return FALSE;
  529. }
  530. else {
  531. return unserialize($custom->schema);
  532. }
  533. }
  534. /**
  535. * Returns all chado base tables.
  536. *
  537. * Base tables are those that contain the primary record for a data type. For
  538. * example, feature, organism, stock, are all base tables. Other tables
  539. * include linker tables (which link two or more base tables), property tables,
  540. * and relationship tables. These provide additional information about
  541. * primary data records and are therefore not base tables. This function
  542. * retreives only the list of tables that are considered 'base' tables.
  543. *
  544. * @return
  545. * An array of base table names.
  546. *
  547. * @ingroup tripal_chado_schema_api
  548. */
  549. function chado_get_base_tables() {
  550. // Initialize the base tables with those tables that are missing a type.
  551. // Ideally they should have a type, but that's for a future version of Chado.
  552. $base_tables = [
  553. 'organism',
  554. 'project',
  555. 'analysis',
  556. 'biomaterial',
  557. 'eimage',
  558. 'assay',
  559. ];
  560. // We'll use the cvterm table to guide which tables are base tables. Typically
  561. // base tables (with a few exceptions) all have a type. Iterate through the
  562. // referring tables.
  563. $schema = chado_get_schema('cvterm');
  564. $referring = $schema['referring_tables'];
  565. foreach ($referring as $tablename) {
  566. // Ignore the cvterm tables, relationships, chadoprop tables.
  567. if ($tablename == 'cvterm_dbxref' || $tablename == 'cvterm_relationship' ||
  568. $tablename == 'cvtermpath' || $tablename == 'cvtermprop' || $tablename == 'chadoprop' ||
  569. $tablename == 'cvtermsynonym' || preg_match('/_relationship$/', $tablename) ||
  570. preg_match('/_cvterm$/', $tablename) ||
  571. // Ignore prop tables
  572. preg_match('/prop$/', $tablename) || preg_match('/prop_.+$/', $tablename) ||
  573. // Ignore nd_tables
  574. preg_match('/^nd_/', $tablename)) {
  575. continue;
  576. }
  577. else {
  578. array_push($base_tables, $tablename);
  579. }
  580. }
  581. // Remove any linker tables that have snuck in. Linker tables are those
  582. // whose foreign key constraints link to two or more base table.
  583. $final_list = [];
  584. foreach ($base_tables as $i => $tablename) {
  585. // A few tables break our rule and seems to look
  586. // like a linking table, but we want to keep it as a base table.
  587. if ($tablename == 'biomaterial' or $tablename == 'assay' or $tablename == 'arraydesign') {
  588. $final_list[] = $tablename;
  589. continue;
  590. }
  591. // Remove the phenotype table. It really shouldn't be a base table as
  592. // it is meant to store individual phenotype measurements.
  593. if ($tablename == 'phenotype') {
  594. continue;
  595. }
  596. $num_links = 0;
  597. $schema = chado_get_schema($tablename);
  598. $fkeys = $schema['foreign keys'];
  599. foreach ($fkeys as $fkid => $details) {
  600. $fktable = $details['table'];
  601. if (in_array($fktable, $base_tables)) {
  602. $num_links++;
  603. }
  604. }
  605. if ($num_links < 2) {
  606. $final_list[] = $tablename;
  607. }
  608. }
  609. // Now add in the cvterm table to the list.
  610. $final_list[] = 'cvterm';
  611. // Sort the tables and return the list.
  612. sort($final_list);
  613. return $final_list;
  614. }
  615. /**
  616. * Get information about which Chado base table a cvterm is mapped to.
  617. *
  618. * Vocbulary terms that represent content types in Tripal must be mapped to
  619. * Chado tables. A cvterm can only be mapped to one base table in Chado.
  620. * This function will return an object that contains the chado table and
  621. * foreign key field to which the cvterm is mapped. The 'chado_table' property
  622. * of the returned object contains the name of the table, and the 'chado_field'
  623. * property contains the name of the foreign key field (e.g. type_id), and the
  624. * 'cvterm' property contains a cvterm object.
  625. *
  626. * @params
  627. * An associative array that contains the following keys:
  628. * - cvterm_id: the cvterm ID value for the term.
  629. * - vocabulary: the short name for the vocabulary (e.g. SO, GO, PATO)
  630. * - accession: the accession for the term.
  631. * - bundle_id: the ID for the bundle to which a term is associated.
  632. * The 'vocabulary' and 'accession' must be used together, the 'cvterm_id' can
  633. * be used on it's own.
  634. *
  635. * @return
  636. * An object containing the chado_table and chado_field properties or NULL if
  637. * if no mapping was found for the term.
  638. */
  639. function chado_get_cvterm_mapping($params) {
  640. $cvterm_id = array_key_exists('cvterm_id', $params) ? $params['cvterm_id'] : NULL;
  641. $vocabulary = array_key_exists('vocabulary', $params) ? $params['vocabulary'] : NULL;
  642. $accession = array_key_exists('accession', $params) ? $params['accession'] : NULL;
  643. $cvterm = NULL;
  644. if ($cvterm_id) {
  645. $cvterm = chado_generate_var('cvterm', ['cvterm_id' => $cvterm_id]);
  646. }
  647. else {
  648. if ($vocabulary and $accession) {
  649. $match = [
  650. 'dbxref_id' => [
  651. 'db_id' => [
  652. 'name' => $vocabulary,
  653. ],
  654. 'accession' => $accession,
  655. ],
  656. ];
  657. $cvterm = chado_generate_var('cvterm', $match);
  658. }
  659. }
  660. if ($cvterm) {
  661. $result = db_select('chado_cvterm_mapping', 'tcm')
  662. ->fields('tcm')
  663. ->condition('cvterm_id', $cvterm->cvterm_id)
  664. ->execute();
  665. $result = $result->fetchObject();
  666. if ($result) {
  667. $result->cvterm = $cvterm;
  668. }
  669. return $result;
  670. }
  671. return NULL;
  672. }