tripal_chado.mapping.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. try {
  12. // Iterate through the referring tables to see what records are there.
  13. foreach ($base_tables as $tablename) {
  14. $ref_schema = chado_get_schema($tablename);
  15. $fkeys = $ref_schema['foreign keys'];
  16. if (!isset($fkeys['cvterm']['columns'])) {
  17. continue;
  18. }
  19. foreach ($fkeys['cvterm']['columns'] as $local_id => $remote_id) {
  20. // Get the list of cvterm_ids from existing records in the table.
  21. $sql = "
  22. SELECT $local_id
  23. FROM { " . $tablename . "}
  24. GROUP BY $local_id
  25. ";
  26. $results = chado_query($sql);
  27. while ($cvterm_id = $results->fetchField()) {
  28. tripal_chado_add_cvterm_mapping($cvterm_id, $tablename, $local_id);
  29. }
  30. }
  31. }
  32. // Now we also want to map tripal terms for existing bundles
  33. $sql =
  34. "SELECT
  35. (SELECT vocabulary FROM tripal_vocab TV WHERE id = TM.vocab_id),
  36. accession,
  37. name
  38. FROM tripal_term TM";
  39. $results = db_query($sql);
  40. while ($tripal_term = $results->fetchObject()) {
  41. $voc = $tripal_term->vocabulary;
  42. $accession = $tripal_term->accession;
  43. $name = $tripal_term->name;
  44. $dbxref_sql =
  45. "SELECT dbxref_id
  46. FROM {dbxref}
  47. WHERE
  48. accession = :accession
  49. AND
  50. db_id = (SELECT db_id FROM {db} WHERE name = :voc)";
  51. $dbxref_id = chado_query($dbxref_sql, [
  52. ':accession' => $accession,
  53. ':voc' => $voc,
  54. ])->fetchField();
  55. if ($dbxref_id) {
  56. $cvterm_sql =
  57. "SELECT cvterm_id
  58. FROM {cvterm}
  59. WHERE
  60. dbxref_id = :dbxref_id
  61. AND name = :name";
  62. $cvterm_id = chado_query($cvterm_sql, [
  63. ':dbxref_id' => $dbxref_id,
  64. ':name' => $name,
  65. ])->fetchField();
  66. if ($cvterm_id) {
  67. // Check if this term is already mapped in the chado_cvterm_mapping table
  68. $check_sql =
  69. "SELECT mapping_id
  70. FROM chado_cvterm_mapping
  71. WHERE cvterm_id = :cvterm_id";
  72. $mapped = db_query($check_sql, [':cvterm_id' => $cvterm_id])->fetchField();
  73. // If mapping does not exist and a table name matches the term name, add it
  74. if (!$mapped && chado_table_exists($name)) {
  75. print "Adding mapped tripal term: $name\n";
  76. tripal_chado_add_cvterm_mapping($cvterm_id, $name, NULL);
  77. }
  78. }
  79. }
  80. }
  81. } catch (Exception $e) {
  82. print "\n"; // make sure we start errors on new line
  83. $transaction->rollback();
  84. watchdog_exception('tripal_chado', $e);
  85. print "FAILED: Rolling back database changes...\n";
  86. }
  87. print "\nDone.\n";
  88. }
  89. /**
  90. * Add a cvterm mapping record
  91. *
  92. * Check if the cvterm mapping record exists. If not, add it to the
  93. * chado_cvterm_mapping table
  94. *
  95. * @param $cvterm_id
  96. * The cvterm_id to map.
  97. * @param $tablename
  98. * The name of the table that the term maps to.
  99. * @param $chado_field
  100. * The field that the term maps to.
  101. */
  102. function tripal_chado_add_cvterm_mapping($cvterm_id, $tablename, $chado_field) {
  103. // check if the record exists
  104. $record = db_select('chado_cvterm_mapping', 'tcm')
  105. ->fields('tcm', ['mapping_id'])
  106. ->condition('cvterm_id', $cvterm_id)
  107. ->execute()
  108. ->fetchField();
  109. // insert records into the chado_cvterm_mapping table.
  110. if (!$record) {
  111. db_insert('chado_cvterm_mapping')
  112. ->fields(
  113. [
  114. 'cvterm_id' => $cvterm_id,
  115. 'chado_table' => $tablename,
  116. 'chado_field' => $chado_field,
  117. ]
  118. )
  119. ->execute();
  120. }
  121. // if the record exists, update the term mapping
  122. else {
  123. db_update('chado_cvterm_mapping')
  124. ->fields(
  125. [
  126. 'chado_table' => $tablename,
  127. 'chado_field' => $chado_field,
  128. ]
  129. )
  130. ->condition('cvterm_id', $cvterm_id)
  131. ->execute();
  132. }
  133. }