tripal_chado.schema.api.inc 20 KB

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