tripal_chado.term_storage.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Implements hook_vocab_storage_info().
  4. */
  5. function tripal_chado_vocab_storage_info() {
  6. return array(
  7. 'term_chado_storage' => array(
  8. 'label' => t('Chado storage'),
  9. 'module' => 'tripal_chado',
  10. 'description' => t('Integrates terms stored in the local Chado database with Tripal entities.'),
  11. 'settings' => array(),
  12. ),
  13. );
  14. }
  15. /**
  16. * Implements hook_vocab_get_term().
  17. */
  18. function tripal_chado_vocab_get_term($namespace, $accession) {
  19. // Create an empty term array for returning if there is a problem.
  20. $empty_term = array(
  21. 'namespace' => $namespace,
  22. 'accession' => $accession,
  23. 'name' => '',
  24. 'definition' => 'Term is undefined.',
  25. 'url' => '',
  26. // The following are not required for the returned array but we'll
  27. // add these for convenience later when we look at the TripalTerm
  28. // objects and these will be there.
  29. 'cvterm' => NULL,
  30. );
  31. // It's possible that Chado is not available (i.e. it gets renamed
  32. // for copying) but Tripal has already been prepared and the
  33. // entities exist. If this is the case we don't want to run the
  34. // commands below.
  35. if (!chado_table_exists('cvterm')) {
  36. return $empty_term;
  37. }
  38. $match = array(
  39. 'dbxref_id' => array(
  40. 'db_id' => array(
  41. 'name' => $namespace,
  42. ),
  43. 'accession' => $accession,
  44. ),
  45. );
  46. $cvterm = chado_generate_var('cvterm', $match);
  47. if (!$cvterm) {
  48. return $empty_term;
  49. }
  50. $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
  51. $term = array(
  52. 'namespace' => $cvterm->dbxref_id->db_id->name,
  53. 'accession' => $cvterm->dbxref_id->accession,
  54. 'name' => $cvterm->name,
  55. 'url' => '',
  56. 'definition' => (isset($cvterm->definition)) ? $cvterm->definition : '',
  57. // The following are not required for the returned array but we'll
  58. // add these for convenience later when we look at the TripalTerm
  59. // objects and these will be there.
  60. 'cvterm' => $cvterm,
  61. );
  62. if ($cvterm->dbxref_id->db_id->urlprefix) {
  63. $term['url'] = $cvterm->dbxref_id->db_id->urlprefix .
  64. $cvterm->dbxref_id->db_id->name . ':' . $cvterm->dbxref_id->accession;
  65. }
  66. return $term;
  67. }
  68. /**
  69. * Implements hook_vocab_import_form();
  70. */
  71. function tripal_chado_vocab_import_form($form, &$form_state) {
  72. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  73. return tripal_cv_obo_form($form, $form_state);
  74. }
  75. /**
  76. * Implements hook_vocab_import_form_validate();
  77. */
  78. function tripal_chado_vocab_import_form_validate($form, &$form_state) {
  79. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  80. return tripal_cv_obo_form_validate($form, $form_state);
  81. }
  82. /**
  83. * Implements hook_vocab_import_form_submit();
  84. */
  85. function tripal_chado_vocab_import_form_submit($form, &$form_state) {
  86. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  87. return tripal_cv_obo_form_submit($form, $form_state);
  88. }
  89. /**
  90. * Implements hook_vocab_select_term_form().
  91. */
  92. function tripal_chado_vocab_select_term_form($form, &$form_state) {
  93. $term_name = array_key_exists('values', $form_state) ? $form_state['values']['term_name'] : '';
  94. // If no term has been selected yet then provide the auto complete field.
  95. $form['term_name'] = array(
  96. '#title' => t('Content Type'),
  97. '#type' => 'textfield',
  98. '#description' => t("The content type must be the name of a term in
  99. a controlled vocabulary and the controlled vocabulary should
  100. already be loaded into Tripal. For example, to create a content
  101. type for storing 'genes', use the 'gene' term from the
  102. Sequence Ontology (SO)."),
  103. '#required' => TRUE,
  104. '#default_value' => $term_name,
  105. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  106. );
  107. $form['select_button'] = array(
  108. '#type' => 'submit',
  109. '#value' => t('Lookup Term'),
  110. '#name' => 'select_cvterm',
  111. '#ajax' => array(
  112. 'callback' => "tripal_chado_vocab_select_term_form_ajax_callback",
  113. 'wrapper' => "tripal-chado-vocab-select-form",
  114. 'effect' => 'fade',
  115. 'method' => 'replace'
  116. ),
  117. );
  118. if ($term_name) {
  119. $form['terms_list'] = array(
  120. '#type' => 'fieldset',
  121. '#title' => t('Matching Terms'),
  122. '#description' => t('Please select the term the best matches the
  123. content type you want to create. If the same term exists in
  124. multiple vocabularies you will see more than one option below.')
  125. );
  126. $match = array(
  127. 'name' => $term_name,
  128. );
  129. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  130. $terms = chado_expand_var($terms, 'field', 'cvterm.definition');
  131. $num_terms = 0;
  132. foreach ($terms as $term) {
  133. $form['terms_list']['term-' . $term->cvterm_id] = array(
  134. '#type' => 'checkbox',
  135. '#title' => $term->name,
  136. '#description' => '<b>Vocabulary:</b> ' . $term->cv_id->name .
  137. '<br><b>Term: </b> ' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession . '. ' .
  138. '<br><b>Definition:</b> ' . $term->definition,
  139. );
  140. $num_terms++;
  141. }
  142. if ($num_terms == 0) {
  143. $form['terms_list']['none'] = array(
  144. '#type' => 'item',
  145. '#markup' => '<i>' . t('There is no term that matches the entered text.') . '</i>'
  146. );
  147. }
  148. // Add in the button for the cases of no terms or too many.
  149. $form['submit_button'] = array(
  150. '#type' => 'submit',
  151. '#value' => t('Use this term'),
  152. '#name' => 'use_cvterm'
  153. );
  154. }
  155. $form['#prefix'] = '<div id = "tripal-chado-vocab-select-form">';
  156. $form['#suffix'] = '</div>';
  157. return $form;
  158. }
  159. /**
  160. * Implements an AJAX callback for the tripal_chado_vocab_select_term_form.
  161. */
  162. function tripal_chado_vocab_select_term_form_ajax_callback($form, $form_state) {
  163. return $form;
  164. }
  165. /**
  166. * Implements hook_vocab_select_term_form_validate().
  167. */
  168. function tripal_chado_vocab_select_term_form_validate($form, &$form_state) {
  169. if (array_key_exists('clicked_button', $form_state) and
  170. $form_state['clicked_button']['#name'] =='use_cvterm') {
  171. $cvterm_id = NULL;
  172. // Make sure we have a cvterm selected
  173. $num_selected = 0;
  174. foreach ($form_state['values'] as $key => $value) {
  175. $matches = array();
  176. if (preg_match("/^term-(\d+)$/", $key, $matches) and
  177. $form_state['values']['term-' . $matches[1]]) {
  178. $cvterm_id = $matches[1];
  179. $term = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
  180. $num_selected++;
  181. }
  182. }
  183. if ($num_selected == 0) {
  184. form_set_error('', 'Please select at least one term.');
  185. }
  186. else if ($num_selected > 1) {
  187. form_set_error('term-' . $cvterm_id, 'Please select only one term from the list below.');
  188. }
  189. else {
  190. $form_state['storage']['namespace'] = $term->dbxref_id->db_id->name;
  191. $form_state['storage']['accession'] = $term->dbxref_id->accession;
  192. $form_state['storage']['term_name'] = $term->name;
  193. }
  194. }
  195. // For any other button click it's an AJAX call and we just want to reubild
  196. // the form.
  197. else {
  198. $form_state['rebuild'] = TRUE;
  199. }
  200. }