tripal_chado.vocab_storage.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 storage'),
  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_term().
  20. *
  21. * This hook is created by the Tripal module and is not a Drupal hook.
  22. */
  23. function tripal_chado_vocab_get_term($vocabulary, $accession) {
  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('cvterm')) {
  29. return FALSE;
  30. }
  31. $match = array(
  32. 'dbxref_id' => array(
  33. 'db_id' => array(
  34. 'name' => $vocabulary,
  35. ),
  36. 'accession' => $accession,
  37. ),
  38. );
  39. $cvterm = chado_generate_var('cvterm', $match);
  40. if (!$cvterm) {
  41. return FALSE;
  42. }
  43. $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
  44. $term = array(
  45. 'vocabulary' => array(
  46. 'name' => $cvterm->cv_id->name,
  47. 'short_name' => $cvterm->dbxref_id->db_id->name,
  48. 'description' => $cvterm->dbxref_id->db_id->description,
  49. 'url' => $cvterm->dbxref_id->db_id->url
  50. ),
  51. 'accession' => $cvterm->dbxref_id->accession,
  52. 'name' => $cvterm->name,
  53. 'url' => tripal_get_dbxref_url($cvterm->dbxref_id),
  54. 'definition' => (isset($cvterm->definition)) ? $cvterm->definition : '',
  55. );
  56. return $term;
  57. }
  58. /**
  59. * Implements hook_vocab_import_form();
  60. */
  61. function tripal_chado_vocab_import_form($form, &$form_state) {
  62. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  63. return tripal_cv_obo_form($form, $form_state);
  64. }
  65. /**
  66. * Implements hook_vocab_import_form_validate();
  67. */
  68. function tripal_chado_vocab_import_form_validate($form, &$form_state) {
  69. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  70. return tripal_cv_obo_form_validate($form, $form_state);
  71. }
  72. /**
  73. * Implements hook_vocab_import_form_submit();
  74. */
  75. function tripal_chado_vocab_import_form_submit($form, &$form_state) {
  76. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  77. return tripal_cv_obo_form_submit($form, $form_state);
  78. }
  79. /**
  80. * Implements hook_vocab_select_term_form().
  81. *
  82. * TODO: can't this be moved over to the tripal module and remove the
  83. * Chado specific parts???
  84. */
  85. function tripal_chado_vocab_select_term_form($form, &$form_state) {
  86. $term_name = array_key_exists('values', $form_state) ? $form_state['values']['term_name'] : '';
  87. // If no term has been selected yet then provide the auto complete field.
  88. $form['term_name'] = array(
  89. '#title' => t('Content Type'),
  90. '#type' => 'textfield',
  91. '#description' => t("The content type must be the name of a term in
  92. a controlled vocabulary and the controlled vocabulary should
  93. already be loaded into Tripal. For example, to create a content
  94. type for storing 'genes', use the 'gene' term from the
  95. Sequence Ontology (SO)."),
  96. '#required' => TRUE,
  97. '#default_value' => $term_name,
  98. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  99. );
  100. $form['select_button'] = array(
  101. '#type' => 'submit',
  102. '#value' => t('Lookup Term'),
  103. '#name' => 'select_cvterm',
  104. '#ajax' => array(
  105. 'callback' => "tripal_chado_vocab_select_term_form_ajax_callback",
  106. 'wrapper' => "tripal-chado-vocab-select-form",
  107. 'effect' => 'fade',
  108. 'method' => 'replace'
  109. ),
  110. );
  111. if ($term_name) {
  112. $form['terms_list'] = array(
  113. '#type' => 'fieldset',
  114. '#title' => t('Matching Terms'),
  115. '#description' => t('Please select the term the best matches the
  116. content type you want to create. If the same term exists in
  117. multiple vocabularies you will see more than one option below.')
  118. );
  119. $match = array(
  120. 'name' => $term_name,
  121. );
  122. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  123. $terms = chado_expand_var($terms, 'field', 'cvterm.definition');
  124. $num_terms = 0;
  125. foreach ($terms as $term) {
  126. // Save the user a click by setting the default value as 1 if there's
  127. // only one matching term.
  128. $default = FALSE;
  129. $attrs = array();
  130. if ($num_terms == 0 and count($terms) == 1) {
  131. $default = TRUE;
  132. $attrs = array('checked' => 'checked');
  133. }
  134. $form['terms_list']['term-' . $term->cvterm_id] = array(
  135. '#type' => 'checkbox',
  136. '#title' => $term->name,
  137. '#default_value' => $default,
  138. '#attributes' => $attrs,
  139. '#description' => '<b>Vocabulary:</b> ' . $term->cv_id->name . ' (' . $term->dbxref_id->db_id->name . ') ' . $term->cv_id->definition .
  140. '<br><b>Term: </b> ' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession . '. ' .
  141. '<br><b>Definition:</b> ' . $term->definition,
  142. );
  143. $base_tables = chado_get_base_tables();
  144. $default_table = 0;
  145. $options = array(0 => '-- Select table --');
  146. foreach ($base_tables AS $tablename) {
  147. $options[$tablename] = $tablename;
  148. }
  149. $mapped_table = chado_get_cvterm_mapping(array('cvterm_id' => $term->cvterm_id));
  150. if ($mapped_table) {
  151. $default_table = $mapped_table->chado_table;
  152. }
  153. $form['terms_list']['default_table-' . $term->cvterm_id] = array(
  154. '#type' => 'select',
  155. '#title' => 'Chado table',
  156. '#options' => $options,
  157. '#description' => 'Select the table in Chado where data of this type
  158. will be stored.',
  159. '#default_value' => $default_table
  160. );
  161. $num_terms++;
  162. }
  163. if ($num_terms == 0) {
  164. $form['terms_list']['none'] = array(
  165. '#type' => 'item',
  166. '#markup' => '<i>' . t('There is no term that matches the entered text.') . '</i>'
  167. );
  168. }
  169. // Add in the button for the cases of no terms or too many.
  170. $form['submit_button'] = array(
  171. '#type' => 'submit',
  172. '#value' => t('Use this term'),
  173. '#name' => 'use_cvterm'
  174. );
  175. }
  176. $form['#prefix'] = '<div id = "tripal-chado-vocab-select-form">';
  177. $form['#suffix'] = '</div>';
  178. return $form;
  179. }
  180. /**
  181. * Implements an AJAX callback for the tripal_chado_vocab_select_term_form.
  182. */
  183. function tripal_chado_vocab_select_term_form_ajax_callback($form, $form_state) {
  184. return $form;
  185. }
  186. /**
  187. * Implements hook_vocab_select_term_form_validate().
  188. */
  189. function tripal_chado_vocab_select_term_form_validate($form, &$form_state) {
  190. if (array_key_exists('clicked_button', $form_state) and
  191. $form_state['clicked_button']['#name'] =='use_cvterm') {
  192. $cvterm_id = NULL;
  193. // Make sure we have a cvterm selected
  194. $num_selected = 0;
  195. foreach ($form_state['values'] as $key => $value) {
  196. $matches = array();
  197. if (preg_match("/^term-(\d+)$/", $key, $matches) and
  198. $form_state['values']['term-' . $matches[1]]) {
  199. $cvterm_id = $matches[1];
  200. $term = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
  201. $num_selected++;
  202. }
  203. }
  204. if ($num_selected == 0) {
  205. form_set_error('', 'Please select at least one term.');
  206. }
  207. else if ($num_selected > 1) {
  208. form_set_error('term-' . $cvterm_id, 'Please select only one term from the list below.');
  209. }
  210. else {
  211. $form_state['storage']['vocabulary'] = $term->dbxref_id->db_id->name;
  212. $form_state['storage']['accession'] = $term->dbxref_id->accession;
  213. $form_state['storage']['term_name'] = $term->name;
  214. // Make sure a default table is selected
  215. $default_table = $form_state['values']['default_table-' . $cvterm_id];
  216. if (!$default_table) {
  217. form_set_error('default_table-' . $cvterm_id, 'Please select a default table.');
  218. }
  219. else {
  220. $chado_field = NULL;
  221. $schema = chado_get_schema($default_table);
  222. if (isset($schema['foreign keys']['cvterm']['columns'])) {
  223. $fkey = array_keys($schema['foreign keys']['cvterm']['columns']);
  224. $chado_field = $fkey[0];
  225. }
  226. tripal_chado_add_cvterm_mapping($cvterm_id, $default_table, $chado_field);
  227. }
  228. }
  229. }
  230. // For any other button click it's an AJAX call and we just want to reubild
  231. // the form.
  232. else {
  233. $form_state['rebuild'] = TRUE;
  234. }
  235. }