tripal_chado.vocab_storage.inc 13 KB

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