tripal_chado.vocab_storage.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_vocabulary().
  20. *
  21. * This hook is created by the Tripal module and is not a Drupal hook.
  22. */
  23. function tripal_chado_vocab_get_vocabulary($vocabulary) {
  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('cv')) {
  29. return FALSE;
  30. }
  31. $sql = "
  32. SELECT DB.name as short_name, DB.description, DB.url, DB.urlprefix,
  33. string_agg(DBCVM.cvname, ', ') as name
  34. FROM {db} DB
  35. INNER JOIN {db2cv_mview} DBCVM ON DBCVM.db_id = DB.db_id
  36. WHERE DB.name = :name
  37. GROUP BY DB.name, DB.description, DB.url, DB.urlprefix
  38. ";
  39. $result = chado_query($sql, array(':name' => $vocabulary));
  40. $result = $result->fetchAssoc();
  41. if (!$result['name']) {
  42. $result['name'] = $result['short_name'];
  43. }
  44. $sw_url = $result['urlprefix'];
  45. if ($sw_url) {
  46. $sw_url = preg_replace('/\{db\}/', $result['short_name'], $sw_url);
  47. $sw_url = preg_replace('/\{accession\}/', '', $sw_url);
  48. $sw_url = url($sw_url, array('absolute' => TRUE));
  49. }
  50. $result['sw_url'] = $sw_url;
  51. return $result;
  52. }
  53. /**
  54. * Implements hook_vocab_get_root_terms().
  55. *
  56. * This hook is created by the Tripal module and is not a Drupal hook.
  57. */
  58. function tripal_chado_vocab_get_root_terms($vocabulary) {
  59. $terms = array();
  60. // It's possible that Chado is not available (i.e. it gets renamed
  61. // for copying) but Tripal has already been prepared and the
  62. // entities exist. If this is the case we don't want to run the
  63. // commands below.
  64. if (!chado_table_exists('db')) {
  65. return FALSE;
  66. }
  67. // Get the list of CV's that belong to this vocabulary and get their
  68. // roots.
  69. $sql = "
  70. SELECT *
  71. FROM {db2cv_mview} WHERE dbname = :dbname
  72. ";
  73. $cvs = chado_query($sql, array(':dbname' => $vocabulary));
  74. while ($cv = $cvs->fetchObject()) {
  75. $sql = "
  76. SELECT cvterm_id
  77. FROM {cv_root_mview} CRM
  78. WHERE CRM.cv_id = :cv_id
  79. ";
  80. $results = chado_query($sql, array(':cv_id' => $cv->cv_id));
  81. while($cvterm_id = $results->fetchField()) {
  82. $match = array('cvterm_id' => $cvterm_id);
  83. $cvterm = chado_generate_var('cvterm', $match);
  84. $terms[] = _tripal_chado_format_term_description($cvterm);
  85. }
  86. }
  87. return $terms;
  88. }
  89. /**
  90. * Implements hook_vocab_get_term_children().
  91. *
  92. * This hook is created by the Tripal module and is not a Drupal hook.
  93. */
  94. function tripal_chado_vocab_get_term_children($vocabulary, $accession) {
  95. $terms = array();
  96. // It's possible that Chado is not available (i.e. it gets renamed
  97. // for copying) but Tripal has already been prepared and the
  98. // entities exist. If this is the case we don't want to run the
  99. // commands below.
  100. if (!chado_table_exists('cvtermpath')) {
  101. return FALSE;
  102. }
  103. // Get the parent cvterm.
  104. $match = array(
  105. 'dbxref_id' => array(
  106. 'db_id' => array(
  107. 'name' => $vocabulary,
  108. ),
  109. 'accession' => $accession,
  110. ),
  111. );
  112. $cvterm = chado_generate_var('cvterm', $match);
  113. if (!$cvterm) {
  114. return FALSE;
  115. }
  116. $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
  117. // Get the children
  118. $sql = "
  119. SELECT subject_id
  120. FROM {cvterm_relationship} CVTR
  121. WHERE object_id = :object_id
  122. ";
  123. $results = chado_query($sql, array(':object_id' => $cvterm->cvterm_id));
  124. while($cvterm_id = $results->fetchField()) {
  125. $match = array('cvterm_id' => $cvterm_id);
  126. $cvterm = chado_generate_var('cvterm', $match);
  127. $terms[] = _tripal_chado_format_term_description($cvterm);
  128. }
  129. return $terms;
  130. }
  131. /**
  132. * Implements hook_vocab_get_term().
  133. *
  134. * This hook is created by the Tripal module and is not a Drupal hook.
  135. */
  136. function tripal_chado_vocab_get_term($vocabulary, $accession) {
  137. // It's possible that Chado is not available (i.e. it gets renamed
  138. // for copying) but Tripal has already been prepared and the
  139. // entities exist. If this is the case we don't want to run the
  140. // commands below.
  141. if (!chado_table_exists('cvterm')) {
  142. return FALSE;
  143. }
  144. $match = array(
  145. 'dbxref_id' => array(
  146. 'db_id' => array(
  147. 'name' => $vocabulary,
  148. ),
  149. 'accession' => $accession,
  150. ),
  151. );
  152. $cvterm = chado_generate_var('cvterm', $match);
  153. if (!$cvterm) {
  154. return FALSE;
  155. }
  156. $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
  157. return _tripal_chado_format_term_description($cvterm);
  158. }
  159. /**
  160. * A helper functions for the hook_vocab_xxx functions.
  161. *
  162. * @param $cvterm
  163. * A cvterm object.
  164. */
  165. function _tripal_chado_format_term_description($cvterm) {
  166. $url = $cvterm->dbxref_id->db_id->url;
  167. $urlprefix = $cvterm->dbxref_id->db_id->urlprefix;
  168. // Generate the URL that can be used for semantic web applications.
  169. $sw_url = $urlprefix;
  170. if ($sw_url) {
  171. $sw_url = preg_replace('/{db}/', $cvterm->dbxref_id->db_id->name, $sw_url);
  172. $sw_url = preg_replace('/{accession}/', '', $sw_url);
  173. $sw_url = url($sw_url, array('absolute' => TRUE));
  174. }
  175. $term = array(
  176. 'vocabulary' => array(
  177. 'name' => $cvterm->cv_id->name,
  178. 'short_name' => $cvterm->dbxref_id->db_id->name,
  179. 'description' => $cvterm->dbxref_id->db_id->description,
  180. 'url' => $url,
  181. 'urlprefix' => $urlprefix,
  182. 'sw_url' => $sw_url,
  183. ),
  184. 'accession' => $cvterm->dbxref_id->accession,
  185. 'name' => $cvterm->name,
  186. 'url' => tripal_get_dbxref_url($cvterm->dbxref_id),
  187. 'definition' => (isset($cvterm->definition)) ? $cvterm->definition : '',
  188. );
  189. return $term;
  190. }
  191. /**
  192. * Implements hook_vocab_add_term().
  193. *
  194. * This hook is created by the Tripal module and is not a Drupal hook.
  195. */
  196. function tripal_chado_vocab_add_term($details) {
  197. $vocabulary = $details['vocab']['name'];
  198. $accession = $details['accession'];
  199. // First check to make sure the term doesn't already exist
  200. $term = tripal_chado_vocab_get_term($vocabulary, $accession);
  201. if ($term) {
  202. return TRUE;
  203. }
  204. // First make sure the vocabulary is added.
  205. $values = array(
  206. 'name' => $vocabulary,
  207. 'description' => $details['vocab']['description'],
  208. 'url' => $details['vocab']['url'],
  209. // TODO: deal with the URL prefix
  210. );
  211. $options = array('update_existing' => TRUE);
  212. tripal_insert_db($values, $options);
  213. // Second make sure the term is added.
  214. $term = tripal_insert_cvterm(array(
  215. 'id' => $vocabulary . ':' . $accession,
  216. 'name' => $details['name'],
  217. 'definition' => $details['definition'],
  218. 'cv_name' => $details['vocab']['name'],
  219. ));
  220. // Return TRUE on success.
  221. if (!$term) {
  222. return FALSE;
  223. }
  224. return TRUE;
  225. }
  226. /**
  227. * Implements hook_vocab_import_form();
  228. */
  229. function tripal_chado_vocab_import_form($form, &$form_state) {
  230. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  231. return tripal_cv_obo_form($form, $form_state);
  232. }
  233. /**
  234. * Implements hook_vocab_import_form_validate();
  235. */
  236. function tripal_chado_vocab_import_form_validate($form, &$form_state) {
  237. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  238. return tripal_cv_obo_form_validate($form, $form_state);
  239. }
  240. /**
  241. * Implements hook_vocab_import_form_submit();
  242. */
  243. function tripal_chado_vocab_import_form_submit($form, &$form_state) {
  244. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  245. return tripal_cv_obo_form_submit($form, $form_state);
  246. }