tripal_chado.vocab_storage.inc 9.5 KB

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