tripal_chado.term_storage.inc 6.9 KB

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