tripal_chado.mapping.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. *
  4. */
  5. function tripal_chado_tripal_cvterm_mapping_schema() {
  6. $schema = array (
  7. 'table' => 'tripal_cvterm_mapping',
  8. 'fields' => array (
  9. 'mapping_id' => array(
  10. 'type' => 'serial',
  11. 'not null' => TRUE
  12. ),
  13. 'cvterm_id' => array (
  14. 'type' => 'int',
  15. 'not null' => TRUE
  16. ),
  17. 'chado_table' => array (
  18. 'type' => 'varchar',
  19. 'length' => 128,
  20. 'not null' => TRUE
  21. ),
  22. 'chado_field' => array (
  23. 'type' => 'varchar',
  24. 'length' => 128,
  25. 'not null' => TRUE
  26. ),
  27. ),
  28. 'primary key' => array (
  29. 0 => 'mapping_id'
  30. ),
  31. 'indexes' => array(
  32. 'tripal_cvterm2table_idx1' => array('cvterm_id'),
  33. 'tripal_cvterm2table_idx2' => array('chado_table'),
  34. 'tripal_cvterm2table_idx3' => array('chado_table', 'chado_field'),
  35. ),
  36. );
  37. return $schema;
  38. }
  39. /**
  40. * This function populates the Tripal entity tables using existing
  41. * data in the database.
  42. */
  43. function tripal_chado_map_cvterms() {
  44. // Get the cvterm table and look for all of the tables that link to it.
  45. $schema = chado_get_schema('cvterm');
  46. $referring = $schema['referring_tables'];
  47. // Perform this action in a transaction
  48. $transaction = db_transaction();
  49. print "\nNOTE: Populating of tripal entity tables is performed using a database transaction. \n" .
  50. "If the load fails or is terminated prematurely then the entire set of \n" .
  51. "insertions/updates is rolled back and will not be found in the database\n\n";
  52. try {
  53. // Iterate through the referring tables to see what records are there.
  54. foreach ($referring as $tablename) {
  55. // We only want to look at base tables.
  56. if ($tablename == 'cvterm_dbxref' || $tablename == 'cvterm_relationship' ||
  57. $tablename == 'cvtermpath' || $tablename == 'cvtermprop' || $tablename == 'chadoprop' ||
  58. $tablename == 'cvtermsynonym' || preg_match('/_relationship$/', $tablename) ||
  59. preg_match('/_cvterm$/', $tablename)) {
  60. continue;
  61. }
  62. print "Examining $tablename...\n";
  63. $ref_schema = chado_get_schema($tablename);
  64. $fkeys = $ref_schema['foreign keys'];
  65. foreach ($fkeys['cvterm']['columns'] as $local_id => $remote_id) {
  66. // Get the list of cvterm_ids from existing records in the table.
  67. $sql = "
  68. SELECT $local_id
  69. FROM { " . $tablename . "}
  70. GROUP BY $local_id
  71. ";
  72. $results = chado_query($sql);
  73. while ($cvterm_id = $results->fetchField()) {
  74. // Get the CV term details and add it to the tripal_vocabulary table if
  75. // it doesn't already exist.
  76. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
  77. $values = array(
  78. 'cvterm_id' => $cvterm->cvterm_id,
  79. 'chado_table' => $tablename,
  80. 'chado_field' => $remote_id
  81. );
  82. // TODO insert records into the tripal_cvterm_mapping table.
  83. }
  84. }
  85. }
  86. }
  87. catch (Exception $e) {
  88. print "\n"; // make sure we start errors on new line
  89. $transaction->rollback();
  90. watchdog_exception('tripal_chado', $e);
  91. print "FAILED: Rolling back database changes...\n";
  92. }
  93. print "\nDone.\n";
  94. }