tripal_chado.vocab_storage.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Implements hook_vocab_storage_info().
  4. *
  5. * This hook is created by the Tripal module and is not a Drupal hook.
  6. */
  7. function tripal_chado_vocab_storage_info() {
  8. return array(
  9. 'term_chado_storage' => array(
  10. 'label' => t('Chado'),
  11. 'module' => 'tripal_chado',
  12. 'description' => t('Integrates terms stored in the local Chado database
  13. with Tripal entities.'),
  14. 'settings' => array(),
  15. ),
  16. );
  17. }
  18. /**
  19. * Implements hook_vocab_get_vocabulary().
  20. *
  21. * This hook is created by the Tripal module and is not a Drupal hook.
  22. */
  23. function tripal_chado_vocab_get_vocabulary($vocabulary) {
  24. // It's possible that Chado is not available (i.e. it gets renamed
  25. // for copying) but Tripal has already been prepared and the
  26. // entities exist. If this is the case we don't want to run the
  27. // commands below.
  28. if (!chado_table_exists('cv')) {
  29. return FALSE;
  30. }
  31. $sql = "
  32. SELECT DB.name as name, CV.name as short_name, DB.description, DB.url
  33. FROM {db} DB
  34. INNER JOIN {dbxref} DBX on DBX.db_id = DB.db_id
  35. INNER JOIN {cvterm} CVT on CVT.dbxref_id = DBX.dbxref_id
  36. INNER JOIN {cv} CV on CV.cv_id = CVT.cv_id
  37. WHERE
  38. DB.name = :name
  39. LIMIT 1 OFFSET 0
  40. ";
  41. $result = chado_query($sql, array(':name' => $vocabulary));
  42. $result = $result->fetchAssoc();
  43. return $result;
  44. }
  45. /**
  46. * Implements hook_vocab_get_term().
  47. *
  48. * This hook is created by the Tripal module and is not a Drupal hook.
  49. */
  50. function tripal_chado_vocab_get_term($vocabulary, $accession) {
  51. // It's possible that Chado is not available (i.e. it gets renamed
  52. // for copying) but Tripal has already been prepared and the
  53. // entities exist. If this is the case we don't want to run the
  54. // commands below.
  55. if (!chado_table_exists('cvterm')) {
  56. return FALSE;
  57. }
  58. $match = array(
  59. 'dbxref_id' => array(
  60. 'db_id' => array(
  61. 'name' => $vocabulary,
  62. ),
  63. 'accession' => $accession,
  64. ),
  65. );
  66. $cvterm = chado_generate_var('cvterm', $match);
  67. if (!$cvterm) {
  68. return FALSE;
  69. }
  70. $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
  71. $term = array(
  72. 'vocabulary' => array(
  73. 'name' => $cvterm->cv_id->name,
  74. 'short_name' => $cvterm->dbxref_id->db_id->name,
  75. 'description' => $cvterm->dbxref_id->db_id->description,
  76. 'url' => $cvterm->dbxref_id->db_id->url
  77. ),
  78. 'accession' => $cvterm->dbxref_id->accession,
  79. 'name' => $cvterm->name,
  80. 'url' => tripal_get_dbxref_url($cvterm->dbxref_id),
  81. 'definition' => (isset($cvterm->definition)) ? $cvterm->definition : '',
  82. );
  83. return $term;
  84. }
  85. /**
  86. * Implements hook_vocab_add_term().
  87. *
  88. * This hook is created by the Tripal module and is not a Drupal hook.
  89. */
  90. function tripal_chado_vocab_add_term($details) {
  91. $vocabulary = $details['vocab']['name'];
  92. $accession = $details['accession'];
  93. // First check to make sure the term doesn't already exist
  94. $term = tripal_chado_vocab_get_term($vocabulary, $accession);
  95. if ($term) {
  96. return TRUE;
  97. }
  98. // First make sure the vocabulary is added.
  99. $values = array(
  100. 'name' => $vocabulary,
  101. 'description' => $details['vocab']['description'],
  102. 'url' => $details['vocab']['url'],
  103. // TODO: deal with the URL prefix
  104. );
  105. $options = array('update_existing' => TRUE);
  106. tripal_insert_db($values, $options);
  107. // Second make sure the term is added.
  108. $term = tripal_insert_cvterm(array(
  109. 'id' => $vocabulary . ':' . $accession,
  110. 'name' => $details['name'],
  111. 'definition' => $details['definition'],
  112. 'cv_name' => $details['vocab']['name'],
  113. ));
  114. // Return TRUE on success.
  115. if (!$term) {
  116. return FALSE;
  117. }
  118. return TRUE;
  119. }
  120. /**
  121. * Implements hook_vocab_import_form();
  122. */
  123. function tripal_chado_vocab_import_form($form, &$form_state) {
  124. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  125. return tripal_cv_obo_form($form, $form_state);
  126. }
  127. /**
  128. * Implements hook_vocab_import_form_validate();
  129. */
  130. function tripal_chado_vocab_import_form_validate($form, &$form_state) {
  131. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  132. return tripal_cv_obo_form_validate($form, $form_state);
  133. }
  134. /**
  135. * Implements hook_vocab_import_form_submit();
  136. */
  137. function tripal_chado_vocab_import_form_submit($form, &$form_state) {
  138. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  139. return tripal_cv_obo_form_submit($form, $form_state);
  140. }