tripal_chado.mapping.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, array(':accession' => $accession, ':voc' => $voc))->fetchField();
  52. if ($dbxref_id) {
  53. $cvterm_sql =
  54. "SELECT cvterm_id
  55. FROM {cvterm}
  56. WHERE
  57. dbxref_id = :dbxref_id
  58. AND name = :name";
  59. $cvterm_id = chado_query($cvterm_sql, array(':dbxref_id' => $dbxref_id, ':name' => $name))->fetchField();
  60. if ($cvterm_id) {
  61. // Check if this term is already mapped in the chado_cvterm_mapping table
  62. $check_sql =
  63. "SELECT mapping_id
  64. FROM chado_cvterm_mapping
  65. WHERE cvterm_id = :cvterm_id";
  66. $mapped = db_query($check_sql, array(':cvterm_id' => $cvterm_id))->fetchField();
  67. // If mapping does not exist and a table name matches the term name, add it
  68. if (!$mapped && chado_table_exists($name)) {
  69. print "Adding mapped tripal term: $name\n";
  70. tripal_chado_add_cvterm_mapping($cvterm_id, $name, NULL);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. catch (Exception $e) {
  77. print "\n"; // make sure we start errors on new line
  78. $transaction->rollback();
  79. watchdog_exception('tripal_chado', $e);
  80. print "FAILED: Rolling back database changes...\n";
  81. }
  82. print "\nDone.\n";
  83. }
  84. /**
  85. * Add a cvterm mapping record
  86. *
  87. * Check if the cvterm mapping record exists. If not, add it to the chado_cvterm_mapping
  88. * table
  89. *
  90. * @param $cvterm_id
  91. * The cvterm_id to map.
  92. * @param $tablename
  93. * The name of the table that the term maps to.
  94. * @param $chado_field
  95. * The field that the term maps to.
  96. */
  97. function tripal_chado_add_cvterm_mapping($cvterm_id, $tablename, $chado_field) {
  98. // check if the record exists
  99. $record = db_select('chado_cvterm_mapping', 'tcm')
  100. ->fields('tcm', array('mapping_id'))
  101. ->condition('cvterm_id', $cvterm_id)
  102. ->execute()
  103. ->fetchField();
  104. // insert records into the chado_cvterm_mapping table.
  105. if (!$record) {
  106. db_insert('chado_cvterm_mapping')
  107. ->fields(
  108. array(
  109. 'cvterm_id' => $cvterm_id,
  110. 'chado_table' => $tablename,
  111. 'chado_field' => $chado_field
  112. )
  113. )
  114. ->execute();
  115. }
  116. // if the record exists, update the term mapping
  117. else {
  118. db_update('chado_cvterm_mapping')
  119. ->fields(
  120. array(
  121. 'chado_table' => $tablename,
  122. 'chado_field' => $chado_field
  123. )
  124. )
  125. ->condition('cvterm_id', $cvterm_id)
  126. ->execute()
  127. ;
  128. }
  129. }