tripal_entities.tables.inc 4.4 KB

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