tripal_chado.vocab_storage.inc 8.3 KB

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