tripal_chado.vocab_storage.inc 9.4 KB

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