tripal_chado.vocab_storage.inc 11 KB

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