tripal_chado.vocab_storage.inc 5.2 KB

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