tripal_chado.vocab_storage.inc

File

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