tripal_chado.schema.api.inc 22 KB

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