tripal_chado.vocab_storage.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 [
  9. 'term_chado_storage' => [
  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' => [],
  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 = [];
  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, []);
  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, ['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', ['attributes' => ['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.', ['!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, [':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, ['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 = [];
  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, [':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, [':cv_id' => $cv->cv_id]);
  142. while ($cvterm_id = $results->fetchField()) {
  143. $match = ['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, [':dbname' => $vocabulary], $limit, $element, $csql);
  179. $terms = [];
  180. while ($cvterm_id = $results->fetchField()) {
  181. $match = ['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 = [];
  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 = [
  203. 'dbxref_id' => [
  204. 'db_id' => [
  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 CVTR.subject_id, CVT.name
  218. FROM {cvterm_relationship} CVTR
  219. INNER JOIN {cvterm} CVT on CVTR.subject_id = CVT.cvterm_id
  220. WHERE object_id = :object_id
  221. ORDER BY CVT.name
  222. ";
  223. $results = chado_query($sql, [':object_id' => $cvterm->cvterm_id]);
  224. while ($cvterm = $results->fetchObject()) {
  225. $cvterm_id = $cvterm->subject_id;
  226. $match = ['cvterm_id' => $cvterm_id];
  227. $cvterm = chado_generate_var('cvterm', $match);
  228. $terms[] = _tripal_chado_format_term_description($cvterm);
  229. }
  230. return $terms;
  231. }
  232. /**
  233. * Implements hook_vocab_get_term().
  234. *
  235. * This hook is created by the Tripal module and is not a Drupal hook.
  236. */
  237. function tripal_chado_vocab_get_term($vocabulary, $accession) {
  238. // Cache ID for this term:
  239. $cid = 'tripal_chado:term:' . $vocabulary . ':' . $accession;
  240. // Check the cache first. Get the term from cache if it's available
  241. $cache = cache_get($cid, 'cache');
  242. if (isset($cache->data)) {
  243. return $cache->data;
  244. }
  245. // It's possible that Chado is not available (i.e. it gets renamed
  246. // for copying) but Tripal has already been prepared and the
  247. // entities exist. If this is the case we don't want to run the
  248. // commands below.
  249. if (!chado_table_exists('cvterm')) {
  250. return FALSE;
  251. }
  252. $match = [
  253. 'dbxref_id' => [
  254. 'db_id' => [
  255. 'name' => $vocabulary,
  256. ],
  257. 'accession' => $accession,
  258. ],
  259. ];
  260. $cvterm = chado_generate_var('cvterm', $match);
  261. if (!$cvterm) {
  262. return FALSE;
  263. }
  264. $options = ['return_array' => TRUE];
  265. $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition', $options);
  266. $cvterm = chado_expand_var($cvterm, 'table', 'cvterm_dbxref', $options);
  267. $cvterm = chado_expand_var($cvterm, 'table', 'cvtermsynonym', $options);
  268. $cvterm = chado_expand_var($cvterm, 'table', 'cvterm_relationship', $options);
  269. $cvterm = chado_expand_var($cvterm, 'table', 'cvtermprop', $options);
  270. // Cache the term to reduce the amount of queries sent to the database
  271. $term = _tripal_chado_format_term_description($cvterm);
  272. cache_set($cid, $term, 'cache', CACHE_TEMPORARY);
  273. return $term;
  274. }
  275. /**
  276. * A helper functions for the hook_vocab_xxx functions.
  277. *
  278. * @param $cvterm
  279. * A cvterm object.
  280. */
  281. function _tripal_chado_format_term_description($cvterm) {
  282. $url = $cvterm->dbxref_id->db_id->url;
  283. $urlprefix = $cvterm->dbxref_id->db_id->urlprefix;
  284. // Generate the URL that can be used for semantic web applications.
  285. $sw_url = $urlprefix;
  286. if ($sw_url) {
  287. $sw_url = preg_replace('/{db}/', $cvterm->dbxref_id->db_id->name, $sw_url);
  288. $sw_url = preg_replace('/{accession}/', '', $sw_url);
  289. $sw_url = url($sw_url, ['absolute' => TRUE]);
  290. }
  291. $vocabulary = tripal_chado_vocab_get_vocabulary($cvterm->dbxref_id->db_id->name);
  292. $term = [
  293. 'vocabulary' => $vocabulary,
  294. 'namespace' => $cvterm->cv_id->name,
  295. 'accession' => $cvterm->dbxref_id->accession,
  296. 'name' => $cvterm->name,
  297. 'url' => chado_get_dbxref_url($cvterm->dbxref_id),
  298. 'definition' => (isset($cvterm->definition)) ? $cvterm->definition : '',
  299. ];
  300. // Is this term in any relationships as the subject?
  301. if (property_exists($cvterm, 'cvterm_relationship')) {
  302. $relationships = $cvterm->cvterm_relationship->subject_id;
  303. if ($relationships) {
  304. foreach ($relationships as $relationship) {
  305. $term['relationship'][] = $relationship->type_id->name . ' ' . $relationship->object_id->dbxref_id->db_id->name . ':' . $relationship->object_id->dbxref_id->accession;
  306. }
  307. }
  308. }
  309. // Does this term have synonyms?
  310. if (property_exists($cvterm, 'cvtermsynonym')) {
  311. $synonyms = $cvterm->cvtermsynonym->cvterm_id;
  312. if ($synonyms) {
  313. foreach ($synonyms as $synonym) {
  314. $term['synonym'][] = $synonym->synonym;
  315. }
  316. }
  317. }
  318. // Does this term have any cross refs?
  319. if (property_exists($cvterm, 'cvterm_dbxref')) {
  320. $xrefs = $cvterm->cvterm_dbxref;
  321. if ($xrefs) {
  322. foreach ($xrefs as $xref) {
  323. $term['cross reference'][] = $xref->dbxref_id->db_id->name . ':' . $xref->dbxref_id->accession;
  324. }
  325. }
  326. }
  327. // Does this term have any properties?
  328. if (property_exists($cvterm, 'cvtermprop')) {
  329. $props = $cvterm->cvtermprop->cvterm_id;
  330. if ($props) {
  331. foreach ($props as $prop) {
  332. $term[$prop->type_id->name][] = property_exists($prop, 'value') ? $prop->value : '';
  333. }
  334. }
  335. }
  336. return $term;
  337. }
  338. /**
  339. * Implements hook_vocab_add_term().
  340. *
  341. * This hook is created by the Tripal module and is not a Drupal hook.
  342. */
  343. function tripal_chado_vocab_add_term($details) {
  344. $vocabulary = $details['vocab']['name'];
  345. $accession = $details['accession'];
  346. // First check to make sure the term doesn't already exist
  347. $term = tripal_chado_vocab_get_term($vocabulary, $accession);
  348. if ($term) {
  349. return TRUE;
  350. }
  351. // First make sure the vocabulary is added.
  352. $values = [
  353. 'name' => $vocabulary,
  354. 'description' => $details['vocab']['description'],
  355. 'url' => $details['vocab']['url'],
  356. // TODO: deal with the URL prefix
  357. ];
  358. $options = ['update_existing' => TRUE];
  359. chado_insert_db($values, $options);
  360. // Second make sure the term is added.
  361. $term = chado_insert_cvterm([
  362. 'id' => $vocabulary . ':' . $accession,
  363. 'name' => $details['name'],
  364. 'definition' => $details['definition'],
  365. 'cv_name' => $details['vocab']['name'],
  366. ]);
  367. // Return TRUE on success.
  368. if (!$term) {
  369. return FALSE;
  370. }
  371. return TRUE;
  372. }
  373. /**
  374. * Implements hook_vocab_import_form();
  375. */
  376. function tripal_chado_vocab_import_form($form, &$form_state) {
  377. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  378. return tripal_cv_obo_form($form, $form_state);
  379. }
  380. /**
  381. * Implements hook_vocab_import_form_validate();
  382. */
  383. function tripal_chado_vocab_import_form_validate($form, &$form_state) {
  384. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  385. return tripal_cv_obo_form_validate($form, $form_state);
  386. }
  387. /**
  388. * Implements hook_vocab_import_form_submit();
  389. */
  390. function tripal_chado_vocab_import_form_submit($form, &$form_state) {
  391. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
  392. return tripal_cv_obo_form_submit($form, $form_state);
  393. }