tripal_chado.vocab_storage.inc 11 KB

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