tripal_chado.term_storage.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. 'urlprefix' => '',
  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. return array(
  52. 'namespace' => $cvterm->dbxref_id->db_id->name,
  53. 'accession' => $cvterm->dbxref_id->accession,
  54. 'name' => $cvterm->name,
  55. 'definition' => (isset($cvterm->definition)) ? $cvterm->definition : '',
  56. 'urlprefix' => $cvterm->dbxref_id->db_id->urlprefix,
  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. }
  63. /**
  64. * Implements hook_vocab_import_form();
  65. */
  66. function tripal_chado_vocab_import_form($form, &$form_state) {
  67. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  68. return tripal_cv_obo_form($form, $form_state);
  69. }
  70. /**
  71. * Implements hook_vocab_import_form_validate();
  72. */
  73. function tripal_chado_vocab_import_form_validate($form, &$form_state) {
  74. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  75. return tripal_cv_obo_form_validate($form, $form_state);
  76. }
  77. /**
  78. * Implements hook_vocab_import_form_submit();
  79. */
  80. function tripal_chado_vocab_import_form_submit($form, &$form_state) {
  81. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  82. return tripal_cv_obo_form_submit($form, $form_state);
  83. }
  84. /**
  85. * Implements hook_vocab_select_term_form().
  86. */
  87. function tripal_chado_vocab_select_term_form($form, &$form_state) {
  88. $term_name = '';
  89. $num_terms = 0;
  90. $cv_id = '';
  91. $terms = array();
  92. // Set defaults using the form state.
  93. if (array_key_exists('storage', $form_state)) {
  94. if (array_key_exists('terms', $form_state['storage'])) {
  95. $terms = $form_state['storage']['terms'];
  96. }
  97. }
  98. $num_terms = count($terms);
  99. // If no term has been selected yet then provide the auto complete field.
  100. if ($num_terms == 0) {
  101. $form['term_name'] = array(
  102. '#title' => t('Content Type'),
  103. '#type' => 'textfield',
  104. '#description' => t("The content type must be the name of a term in
  105. a controlled vocabulary and the controlled vocabulary should
  106. already be loaded into Tripal. For example, to create a content
  107. type for storing 'genes', use the 'gene' term from the
  108. Sequence Ontology (SO)."),
  109. '#required' => TRUE,
  110. '#default_value' => $term_name,
  111. '#autocomplete_path' => "admin/tripal/storage/term/$cv_id",
  112. );
  113. }
  114. // If the term belongs to more than one vocabulary then add additional fields
  115. // to let the user select the vocabulary.
  116. if ($num_terms > 1) {
  117. $form['term_name'] = array(
  118. '#type' => 'hidden',
  119. '#value' => $term_name,
  120. );
  121. $cvs = array();
  122. foreach ($terms as $term) {
  123. $cvs[$term->cv_id->cv_id] = 'Vocabulary: <b>' . $term->cv_id->name . '</b> (' . $term->cv_id->definition . ')<br>' . $term->name . ': ' . $term->definition;
  124. }
  125. $form['cv_id'] = array(
  126. '#type' => 'radios',
  127. '#title' => t('Select the appropriate vocabulary'),
  128. '#options' => $cvs,
  129. );
  130. }
  131. // Add in the button for the cases of no terms or too many.
  132. $form['select_button'] = array(
  133. '#type' => 'submit',
  134. '#value' => t('Use this term'),
  135. '#name' => 'select_cvterm'
  136. );
  137. return $form;
  138. }
  139. /**
  140. * Implements hook_vocab_select_term_form_validate().
  141. */
  142. function tripal_chado_vocab_select_term_form_validate($form, &$form_state) {
  143. if (array_key_exists('clicked_button', $form_state) and
  144. $form_state['clicked_button']['#name'] =='select_cvterm') {
  145. // First, make sure the term is unique. If not then we can't check it.
  146. $term_name = NULL;
  147. $cv_id = NULL;
  148. $cvterm = NULL;
  149. if (array_key_exists('term_name', $form_state['values'])) {
  150. $term_name = $form_state['input']['term_name'];
  151. }
  152. if (array_key_exists('cv_id', $form_state['input'])) {
  153. $cv_id = $form_state['input']['cv_id'];
  154. }
  155. // If a term and $cv_id are provided then we can look for the term using
  156. // both and we should find a unique term. If only ther term is provided
  157. // we can still look for a unique term but there must only be one.
  158. if ($term_name and !$cv_id) {
  159. $match = array(
  160. 'name' => $term_name,
  161. );
  162. }
  163. else {
  164. $match = array(
  165. 'name' => $term_name,
  166. 'cv_id' => $cv_id,
  167. );
  168. }
  169. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  170. $form_state['storage']['terms'] = $terms;
  171. // If we do not have any terms then the term provided by the user does not
  172. // exists and we need to provide an error message.
  173. if (count($terms) == 0) {
  174. form_set_error('term_name', t('The term does not exist in this database.'));
  175. }
  176. // If we have more than one term then we need to set an error so that the
  177. // form can provide a list of vocabularies to select from.
  178. if (count($terms) > 1) {
  179. form_set_error('term_name', t('The term is not unique. A list of vocabularies
  180. that contain this term. Please select the most appropriate vocabulary.'));
  181. }
  182. // If we have a unique term then set the namespace, accession and name.
  183. if (count($terms) == 1) {
  184. $form_state['storage']['namespace'] = $terms[0]->dbxref_id->db_id->name;
  185. $form_state['storage']['accession'] = $terms[0]->dbxref_id->accession;
  186. $form_state['storage']['term_name'] = $terms[0]->name;
  187. }
  188. }
  189. }