tripal_chado.vocab_storage.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. drupal_set_message("Could not find details about the vocabulary: $vocabulary");
  98. return FALSE;
  99. }
  100. if (!$result['name']) {
  101. $result['name'] = $result['short_name'];
  102. }
  103. $sw_url = $result['urlprefix'];
  104. if ($sw_url) {
  105. $sw_url = preg_replace('/\{db\}/', $result['short_name'], $sw_url);
  106. $sw_url = preg_replace('/\{accession\}/', '', $sw_url);
  107. $sw_url = url($sw_url, array('absolute' => TRUE));
  108. }
  109. $result['sw_url'] = $sw_url;
  110. return $result;
  111. }
  112. /**
  113. * Implements hook_vocab_get_root_terms().
  114. *
  115. * This hook is created by the Tripal module and is not a Drupal hook.
  116. */
  117. function tripal_chado_vocab_get_root_terms($vocabulary) {
  118. $terms = array();
  119. // It's possible that Chado is not available (i.e. it gets renamed
  120. // for copying) but Tripal has already been prepared and the
  121. // entities exist. If this is the case we don't want to run the
  122. // commands below.
  123. if (!chado_table_exists('db')) {
  124. return FALSE;
  125. }
  126. // Get the list of CV's that belong to this vocabulary and get their
  127. // roots.
  128. $sql = "
  129. SELECT *
  130. FROM {db2cv_mview} WHERE dbname = :dbname
  131. ";
  132. $cvs = chado_query($sql, array(':dbname' => $vocabulary));
  133. while ($cv = $cvs->fetchObject()) {
  134. $sql = "
  135. SELECT cvterm_id
  136. FROM {cv_root_mview} CRM
  137. WHERE CRM.cv_id = :cv_id
  138. ";
  139. $results = chado_query($sql, array(':cv_id' => $cv->cv_id));
  140. while($cvterm_id = $results->fetchField()) {
  141. $match = array('cvterm_id' => $cvterm_id);
  142. $cvterm = chado_generate_var('cvterm', $match);
  143. $terms[] = _tripal_chado_format_term_description($cvterm);
  144. }
  145. }
  146. return $terms;
  147. }
  148. /**
  149. * Implements hook_vocab_get_terms().
  150. *
  151. * This hook is created by the Tripal module and is not a Drupal hook.
  152. */
  153. function tripal_chado_vocab_get_terms($vocabulary, $limit = 25, $element = 0) {
  154. // It's possible that Chado is not available (i.e. it gets renamed
  155. // for copying) but Tripal has already been prepared and the
  156. // entities exist. If this is the case we don't want to run the
  157. // commands below.
  158. if (!chado_table_exists('cvterm')) {
  159. return FALSE;
  160. }
  161. $sql = "
  162. SELECT CVT.cvterm_id
  163. FROM {cvterm} CVT
  164. INNER JOIN {dbxref} DBX on DBX.dbxref_id = CVT.dbxref_id
  165. INNER JOIN {db} DB on DB.db_id = DBX.db_id
  166. WHERE db.name = :dbname
  167. ORDER BY CVT.name
  168. ";
  169. $csql = "
  170. SELECT COUNT(CVT.cvterm_id)
  171. FROM {cvterm} CVT
  172. INNER JOIN {dbxref} DBX on DBX.dbxref_id = CVT.dbxref_id
  173. INNER JOIN {db} DB on DB.db_id = DBX.db_id
  174. WHERE db.name = :dbname
  175. ";
  176. $results = chado_pager_query($sql, array(':dbname' => $vocabulary), $limit, $element, $csql);
  177. $terms = array();
  178. while($cvterm_id = $results->fetchField()) {
  179. $match = array('cvterm_id' => $cvterm_id);
  180. $cvterm = chado_generate_var('cvterm', $match);
  181. $terms[] = _tripal_chado_format_term_description($cvterm);
  182. }
  183. return $terms;
  184. }
  185. /**
  186. * Implements hook_vocab_get_term_children().
  187. *
  188. * This hook is created by the Tripal module and is not a Drupal hook.
  189. */
  190. function tripal_chado_vocab_get_term_children($vocabulary, $accession) {
  191. $terms = array();
  192. // It's possible that Chado is not available (i.e. it gets renamed
  193. // for copying) but Tripal has already been prepared and the
  194. // entities exist. If this is the case we don't want to run the
  195. // commands below.
  196. if (!chado_table_exists('cvtermpath')) {
  197. return FALSE;
  198. }
  199. // Get the parent cvterm.
  200. $match = array(
  201. 'dbxref_id' => array(
  202. 'db_id' => array(
  203. 'name' => $vocabulary,
  204. ),
  205. 'accession' => $accession,
  206. ),
  207. );
  208. $cvterm = chado_generate_var('cvterm', $match);
  209. if (!$cvterm) {
  210. return FALSE;
  211. }
  212. $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
  213. // Get the children.
  214. $sql = "
  215. SELECT DISTINCT subject_id
  216. FROM {cvterm_relationship} CVTR
  217. WHERE object_id = :object_id
  218. ";
  219. $results = chado_query($sql, array(':object_id' => $cvterm->cvterm_id));
  220. while($cvterm_id = $results->fetchField()) {
  221. $match = array('cvterm_id' => $cvterm_id);
  222. $cvterm = chado_generate_var('cvterm', $match);
  223. $terms[] = _tripal_chado_format_term_description($cvterm);
  224. }
  225. return $terms;
  226. }
  227. /**
  228. * Implements hook_vocab_get_term().
  229. *
  230. * This hook is created by the Tripal module and is not a Drupal hook.
  231. */
  232. function tripal_chado_vocab_get_term($vocabulary, $accession) {
  233. // It's possible that Chado is not available (i.e. it gets renamed
  234. // for copying) but Tripal has already been prepared and the
  235. // entities exist. If this is the case we don't want to run the
  236. // commands below.
  237. if (!chado_table_exists('cvterm')) {
  238. return FALSE;
  239. }
  240. $match = array(
  241. 'dbxref_id' => array(
  242. 'db_id' => array(
  243. 'name' => $vocabulary,
  244. ),
  245. 'accession' => $accession,
  246. ),
  247. );
  248. $cvterm = chado_generate_var('cvterm', $match);
  249. if (!$cvterm) {
  250. return FALSE;
  251. }
  252. $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
  253. return _tripal_chado_format_term_description($cvterm);
  254. }
  255. /**
  256. * A helper functions for the hook_vocab_xxx functions.
  257. *
  258. * @param $cvterm
  259. * A cvterm object.
  260. */
  261. function _tripal_chado_format_term_description($cvterm) {
  262. $url = $cvterm->dbxref_id->db_id->url;
  263. $urlprefix = $cvterm->dbxref_id->db_id->urlprefix;
  264. // Generate the URL that can be used for semantic web applications.
  265. $sw_url = $urlprefix;
  266. if ($sw_url) {
  267. $sw_url = preg_replace('/{db}/', $cvterm->dbxref_id->db_id->name, $sw_url);
  268. $sw_url = preg_replace('/{accession}/', '', $sw_url);
  269. $sw_url = url($sw_url, array('absolute' => TRUE));
  270. }
  271. $vocabulary = tripal_chado_vocab_get_vocabulary($cvterm->dbxref_id->db_id->name);
  272. $term = array(
  273. 'vocabulary' => $vocabulary,
  274. 'accession' => $cvterm->dbxref_id->accession,
  275. 'name' => $cvterm->name,
  276. 'url' => chado_get_dbxref_url($cvterm->dbxref_id),
  277. 'definition' => (isset($cvterm->definition)) ? $cvterm->definition : '',
  278. );
  279. return $term;
  280. }
  281. /**
  282. * Implements hook_vocab_add_term().
  283. *
  284. * This hook is created by the Tripal module and is not a Drupal hook.
  285. */
  286. function tripal_chado_vocab_add_term($details) {
  287. $vocabulary = $details['vocab']['name'];
  288. $accession = $details['accession'];
  289. // First check to make sure the term doesn't already exist
  290. $term = tripal_chado_vocab_get_term($vocabulary, $accession);
  291. if ($term) {
  292. return TRUE;
  293. }
  294. // First make sure the vocabulary is added.
  295. $values = array(
  296. 'name' => $vocabulary,
  297. 'description' => $details['vocab']['description'],
  298. 'url' => $details['vocab']['url'],
  299. // TODO: deal with the URL prefix
  300. );
  301. $options = array('update_existing' => TRUE);
  302. chado_insert_db($values, $options);
  303. // Second make sure the term is added.
  304. $term = chado_insert_cvterm(array(
  305. 'id' => $vocabulary . ':' . $accession,
  306. 'name' => $details['name'],
  307. 'definition' => $details['definition'],
  308. 'cv_name' => $details['vocab']['name'],
  309. ));
  310. // Return TRUE on success.
  311. if (!$term) {
  312. return FALSE;
  313. }
  314. return TRUE;
  315. }
  316. /**
  317. * Implements hook_vocab_import_form();
  318. */
  319. function tripal_chado_vocab_import_form($form, &$form_state) {
  320. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  321. return tripal_cv_obo_form($form, $form_state);
  322. }
  323. /**
  324. * Implements hook_vocab_import_form_validate();
  325. */
  326. function tripal_chado_vocab_import_form_validate($form, &$form_state) {
  327. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  328. return tripal_cv_obo_form_validate($form, $form_state);
  329. }
  330. /**
  331. * Implements hook_vocab_import_form_submit();
  332. */
  333. function tripal_chado_vocab_import_form_submit($form, &$form_state) {
  334. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  335. return tripal_cv_obo_form_submit($form, $form_state);
  336. }