tripal_chado.vocab_storage.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 short_name, CV.name as name, DB.description, DB.url, DB.urlprefix
  33. FROM {db} DB
  34. LEFT JOIN {dbxref} DBX on DBX.db_id = DB.db_id
  35. LEFT JOIN {cvterm} CVT on CVT.dbxref_id = DBX.dbxref_id
  36. LEFT 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. if (!$result['name']) {
  44. $result['name'] = $result['short_name'];
  45. }
  46. return $result;
  47. }
  48. /**
  49. * Implements hook_vocab_get_term().
  50. *
  51. * This hook is created by the Tripal module and is not a Drupal hook.
  52. */
  53. function tripal_chado_vocab_get_term($vocabulary, $accession) {
  54. // It's possible that Chado is not available (i.e. it gets renamed
  55. // for copying) but Tripal has already been prepared and the
  56. // entities exist. If this is the case we don't want to run the
  57. // commands below.
  58. if (!chado_table_exists('cvterm')) {
  59. return FALSE;
  60. }
  61. $match = array(
  62. 'dbxref_id' => array(
  63. 'db_id' => array(
  64. 'name' => $vocabulary,
  65. ),
  66. 'accession' => $accession,
  67. ),
  68. );
  69. $cvterm = chado_generate_var('cvterm', $match);
  70. if (!$cvterm) {
  71. return FALSE;
  72. }
  73. $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
  74. $term = array(
  75. 'vocabulary' => array(
  76. 'name' => $cvterm->cv_id->name,
  77. 'short_name' => $cvterm->dbxref_id->db_id->name,
  78. 'description' => $cvterm->dbxref_id->db_id->description,
  79. 'url' => $cvterm->dbxref_id->db_id->url,
  80. 'urlprefix' => $cvterm->dbxref_id->db_id->urlprefix
  81. ),
  82. 'accession' => $cvterm->dbxref_id->accession,
  83. 'name' => $cvterm->name,
  84. 'url' => tripal_get_dbxref_url($cvterm->dbxref_id),
  85. 'definition' => (isset($cvterm->definition)) ? $cvterm->definition : '',
  86. );
  87. return $term;
  88. }
  89. /**
  90. * Implements hook_vocab_add_term().
  91. *
  92. * This hook is created by the Tripal module and is not a Drupal hook.
  93. */
  94. function tripal_chado_vocab_add_term($details) {
  95. $vocabulary = $details['vocab']['name'];
  96. $accession = $details['accession'];
  97. // First check to make sure the term doesn't already exist
  98. $term = tripal_chado_vocab_get_term($vocabulary, $accession);
  99. if ($term) {
  100. return TRUE;
  101. }
  102. // First make sure the vocabulary is added.
  103. $values = array(
  104. 'name' => $vocabulary,
  105. 'description' => $details['vocab']['description'],
  106. 'url' => $details['vocab']['url'],
  107. // TODO: deal with the URL prefix
  108. );
  109. $options = array('update_existing' => TRUE);
  110. tripal_insert_db($values, $options);
  111. // Second make sure the term is added.
  112. $term = tripal_insert_cvterm(array(
  113. 'id' => $vocabulary . ':' . $accession,
  114. 'name' => $details['name'],
  115. 'definition' => $details['definition'],
  116. 'cv_name' => $details['vocab']['name'],
  117. ));
  118. // Return TRUE on success.
  119. if (!$term) {
  120. return FALSE;
  121. }
  122. return TRUE;
  123. }
  124. /**
  125. * Implements hook_vocab_import_form();
  126. */
  127. function tripal_chado_vocab_import_form($form, &$form_state) {
  128. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  129. return tripal_cv_obo_form($form, $form_state);
  130. }
  131. /**
  132. * Implements hook_vocab_import_form_validate();
  133. */
  134. function tripal_chado_vocab_import_form_validate($form, &$form_state) {
  135. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  136. return tripal_cv_obo_form_validate($form, $form_state);
  137. }
  138. /**
  139. * Implements hook_vocab_import_form_submit();
  140. */
  141. function tripal_chado_vocab_import_form_submit($form, &$form_state) {
  142. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  143. return tripal_cv_obo_form_submit($form, $form_state);
  144. }