tripal_chado.schema.api.inc 22 KB

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