tripal_chado.vocab_storage.inc 11 KB

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