tripal_chado.mapping.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 chado_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. // Now we also want to map tripal terms for existing bundles
  37. $sql =
  38. "SELECT
  39. (SELECT vocabulary FROM tripal_vocab TV WHERE id = TM.vocab_id),
  40. accession,
  41. name
  42. FROM tripal_term TM";
  43. $results = db_query($sql);
  44. while ($tripal_term = $results->fetchObject()) {
  45. $voc = $tripal_term->vocabulary;
  46. $accession = $tripal_term->accession;
  47. $name = $tripal_term->name;
  48. $dbxref_sql =
  49. "SELECT dbxref_id
  50. FROM {dbxref}
  51. WHERE
  52. accession = :accession
  53. AND
  54. db_id = (SELECT db_id FROM {db} WHERE name = :voc)";
  55. $dbxref_id = chado_query($dbxref_sql, array(':accession' => $accession, ':voc' => $voc))->fetchField();
  56. if ($dbxref_id) {
  57. $cvterm_sql =
  58. "SELECT cvterm_id
  59. FROM {cvterm}
  60. WHERE
  61. dbxref_id = :dbxref_id
  62. AND name = :name";
  63. $cvterm_id = chado_query($cvterm_sql, array(':dbxref_id' => $dbxref_id, ':name' => $name))->fetchField();
  64. if ($cvterm_id) {
  65. // Check if this term is already mapped in the chado_cvterm_mapping table
  66. $check_sql =
  67. "SELECT mapping_id
  68. FROM chado_cvterm_mapping
  69. WHERE cvterm_id = :cvterm_id";
  70. $mapped = db_query($check_sql, array(':cvterm_id' => $cvterm_id))->fetchField();
  71. // If mapping does not exist and a table name matches the term name, add it
  72. if (!$mapped && chado_table_exists($name)) {
  73. print "Adding mapped tripal term: $name\n";
  74. tripal_chado_add_cvterm_mapping($cvterm_id, $name, NULL);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. catch (Exception $e) {
  81. print "\n"; // make sure we start errors on new line
  82. $transaction->rollback();
  83. watchdog_exception('tripal_chado', $e);
  84. print "FAILED: Rolling back database changes...\n";
  85. }
  86. print "\nDone.\n";
  87. }
  88. /*
  89. * Add a cvterm mapping record
  90. *
  91. * Check if the cvterm mapping record exists. If not, add it to the chado_cvterm_mapping
  92. * table
  93. */
  94. function tripal_chado_add_cvterm_mapping($cvterm_id, $tablename, $chado_field) {
  95. // check if the record exists
  96. $record = db_select('chado_cvterm_mapping', 'tcm')
  97. ->fields('tcm', array('mapping_id'))
  98. ->condition('cvterm_id', $cvterm_id)
  99. ->execute()
  100. ->fetchField();
  101. // insert records into the chado_cvterm_mapping table.
  102. if (!$record) {
  103. db_insert('chado_cvterm_mapping')
  104. ->fields(
  105. array(
  106. 'cvterm_id' => $cvterm_id,
  107. 'chado_table' => $tablename,
  108. 'chado_field' => $chado_field
  109. )
  110. )
  111. ->execute();
  112. }
  113. // if the record exists, update the term mapping
  114. else {
  115. db_update('chado_cvterm_mapping')
  116. ->fields(
  117. array(
  118. 'chado_table' => $tablename,
  119. 'chado_field' => $chado_field
  120. )
  121. )
  122. ->condition('cvterm_id', $cvterm_id)
  123. ->execute()
  124. ;
  125. }
  126. }