tripal_chado.mapping.inc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 chado base tables
  8. $base_tables = chado_get_base_tables();
  9. // Perform this action in a transaction
  10. $transaction = db_transaction();
  11. print "\nNOTE: Populating of tripal_cvterm_mapping table is performed using a database transaction. \n" .
  12. "If the load fails or is terminated prematurely then the entire set of \n" .
  13. "insertions/updates is rolled back and will not be found in the database\n\n";
  14. try {
  15. // Iterate through the referring tables to see what records are there.
  16. foreach ($base_tables as $tablename) {
  17. print "Examining $tablename...\n";
  18. $ref_schema = chado_get_schema($tablename);
  19. $fkeys = $ref_schema['foreign keys'];
  20. if (!isset($fkeys['cvterm']['columns'])) {
  21. continue;
  22. }
  23. foreach ($fkeys['cvterm']['columns'] as $local_id => $remote_id) {
  24. // Get the list of cvterm_ids from existing records in the table.
  25. $sql = "
  26. SELECT $local_id
  27. FROM { " . $tablename . "}
  28. GROUP BY $local_id
  29. ";
  30. $results = chado_query($sql);
  31. while ($cvterm_id = $results->fetchField()) {
  32. tripal_chado_add_cvterm_mapping($cvterm_id, $tablename, $local_id);
  33. }
  34. }
  35. }
  36. }
  37. catch (Exception $e) {
  38. print "\n"; // make sure we start errors on new line
  39. $transaction->rollback();
  40. watchdog_exception('tripal_chado', $e);
  41. print "FAILED: Rolling back database changes...\n";
  42. }
  43. print "\nDone.\n";
  44. }
  45. /*
  46. * Return mapped tables for a cvterm
  47. *
  48. * Query the tripal_cvterm_mapping table and return tables used by a cvterm
  49. */
  50. function tripal_chado_get_cvterm_mapped_tables($cvterm_id) {
  51. $tables = array();
  52. $results = db_select('tripal_cvterm_mapping', 'tcm')
  53. ->fields('tcm')
  54. ->condition('cvterm_id', $cvterm_id)
  55. ->execute();
  56. while($table = $results->fetchObject()) {
  57. array_push($tables, $table);
  58. }
  59. return $tables;
  60. }
  61. /*
  62. * Add a cvterm mapping record
  63. *
  64. * Check if the cvterm mapping record exists. If not, add it to the tripal_cvterm_mapping
  65. * table
  66. */
  67. function tripal_chado_add_cvterm_mapping($cvterm_id, $tablename, $chado_field) {
  68. // check if the record exists
  69. $record = db_select('tripal_cvterm_mapping', 'tcm')
  70. ->fields('tcm', array('mapping_id'))
  71. ->condition('cvterm_id', $cvterm_id)
  72. ->condition('chado_table', $tablename)
  73. ->condition('chado_field', $chado_field)
  74. ->execute()
  75. ->fetchField();
  76. // insert records into the tripal_cvterm_mapping table.
  77. if (!$record) {
  78. db_insert('tripal_cvterm_mapping')
  79. ->fields(
  80. array(
  81. 'cvterm_id' => $cvterm_id,
  82. 'chado_table' => $tablename,
  83. 'chado_field' => $chado_field
  84. )
  85. )
  86. ->execute();
  87. }
  88. }