tripal_entities.tables.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * This function populates the Tripal entity tables using existing
  4. * data in the database.
  5. */
  6. function tripal_entities_populate_entity_tables() {
  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 entity tables 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.
  38. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
  39. // First add a record to the tripal_vocabulary table.
  40. $values = array(
  41. 'cv_id' => $cvterm->cv_id->cv_id,
  42. 'db_id' => $cvterm->dbxref_id->db_id->db_id,
  43. 'publish' => 0,
  44. );
  45. $vocabulary_id = 0;
  46. $vocabulary = chado_select_record('tripal_vocabulary', array('vocabulary_id'), $values);
  47. if (count($vocabulary) == 0) {
  48. $vocabulary = chado_insert_record('tripal_vocabulary', $values);
  49. $vocabulary_id = $vocabulary['vocabulary_id'];
  50. }
  51. else {
  52. $vocabulary_id = $vocabulary[0]->vocabulary_id;
  53. }
  54. // Next add a record to the tripal_term table.
  55. $values = array(
  56. 'vocabulary_id' => $vocabulary_id,
  57. 'cvterm_id' => $cvterm_id,
  58. 'publish' => 0
  59. );
  60. $term_id = 0;
  61. $bundle = chado_select_record('tripal_term', array('term_id'), $values);
  62. if (count($bundle) == 0) {
  63. $bundle = chado_insert_record('tripal_term', $values);
  64. $term_id = $bundle['term_id'];
  65. }
  66. else {
  67. $term_id = $bundle[0]->term_id;
  68. }
  69. // Add the table where the records are found.
  70. $values = array(
  71. 'term_id' => $term_id,
  72. 'data_table' => $tablename,
  73. 'type_table' => $tablename,
  74. 'field' => $local_id
  75. );
  76. if (!chado_select_record('tripal_term_usage', array('term_usage_id'), $values, array('has_record' => TRUE))) {
  77. chado_insert_record('tripal_term_usage', $values);
  78. }
  79. // Add the table where the records are found.
  80. $values = array(
  81. 'vocabulary_id' => $vocabulary_id,
  82. 'data_table' => $tablename,
  83. 'type_table' => $tablename,
  84. 'field' => $local_id
  85. );
  86. if (!chado_select_record('tripal_vocabulary_usage', array('vocabulary_id'), $values, array('has_record' => TRUE))) {
  87. chado_insert_record('tripal_vocabulary_usage', $values);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. catch (Exception $e) {
  94. print "\n"; // make sure we start errors on new line
  95. $transaction->rollback();
  96. watchdog_exception('tripal_ws', $e);
  97. print "FAILED: Rolling back database changes...\n";
  98. }
  99. print "\nDone.\n";
  100. }