tripal_chado.mapping.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * This function populates the Tripal entity tables using existing
  4. * data in the database.
  5. */
  6. function tripal_chado_map_cvterms() {
  7. // Get the cvterm table and look for all of the tables that link to it.
  8. $schema = chado_get_schema('cvterm');
  9. $referring = $schema['referring_tables'];
  10. // Perform this action in a transaction
  11. $transaction = db_transaction();
  12. print "\nNOTE: Populating of tripal_cvterm_mapping table is performed using a database transaction. \n" .
  13. "If the load fails or is terminated prematurely then the entire set of \n" .
  14. "insertions/updates is rolled back and will not be found in the database\n\n";
  15. try {
  16. // Iterate through the referring tables to see what records are there.
  17. foreach ($referring as $tablename) {
  18. // We only want to look at base tables.
  19. if ($tablename == 'cvterm_dbxref' || $tablename == 'cvterm_relationship' ||
  20. $tablename == 'cvtermpath' || $tablename == 'cvtermprop' || $tablename == 'chadoprop' ||
  21. $tablename == 'cvtermsynonym' || preg_match('/_relationship$/', $tablename) ||
  22. preg_match('/_cvterm$/', $tablename)) {
  23. continue;
  24. }
  25. print "Examining $tablename...\n";
  26. $ref_schema = chado_get_schema($tablename);
  27. $fkeys = $ref_schema['foreign keys'];
  28. foreach ($fkeys['cvterm']['columns'] as $local_id => $remote_id) {
  29. // Get the list of cvterm_ids from existing records in the table.
  30. $sql = "
  31. SELECT $local_id
  32. FROM { " . $tablename . "}
  33. GROUP BY $local_id
  34. ";
  35. $results = chado_query($sql);
  36. while ($cvterm_id = $results->fetchField()) {
  37. // Get the CV term details and add it to the tripal_vocabulary table if
  38. // it doesn't already exist.
  39. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
  40. // TODO insert records into the tripal_cvterm_mapping table.
  41. db_insert('tripal_cvterm_mapping')
  42. ->fields(
  43. array(
  44. 'cvterm_id' => $cvterm->cvterm_id,
  45. 'chado_table' => $tablename,
  46. 'chado_field' => $remote_id
  47. )
  48. )
  49. ->execute();
  50. }
  51. }
  52. }
  53. }
  54. catch (Exception $e) {
  55. print "\n"; // make sure we start errors on new line
  56. $transaction->rollback();
  57. watchdog_exception('tripal_chado', $e);
  58. print "FAILED: Rolling back database changes...\n";
  59. }
  60. print "\nDone.\n";
  61. }