tripal_chado.vocab_storage.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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'),
  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. }