tripal_chado.vocab_storage.inc 5.2 KB

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