tripal.term_lookup.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * Provides the content for the Controlled vocabulary home page.
  4. */
  5. function tripal_vocabulary_lookup_page() {
  6. // set the breadcrumb
  7. $breadcrumb = array();
  8. $breadcrumb[] = l('Home', '<front>');
  9. drupal_set_breadcrumb($breadcrumb);
  10. $vocabs = tripal_get_vocabularies();
  11. $rows = array();
  12. foreach ($vocabs as $vocabulary) {
  13. $rows[] = array(
  14. l($vocabulary['short_name'], 'cv/lookup/' . $vocabulary['short_name']),
  15. $vocabulary['name'],
  16. $vocabulary['description'],
  17. array(
  18. 'data' => number_format($vocabulary['num_terms']),
  19. 'style' => 'text-align: right;'
  20. ),
  21. );
  22. }
  23. $headers = array('Short Name', 'Vocabulary Name(s)', 'Description', 'Loaded Terms');
  24. $table = array(
  25. 'header' => $headers,
  26. 'rows' => $rows,
  27. 'attributes' => array(),
  28. 'sticky' => FALSE,
  29. 'caption' => '',
  30. 'colgroups' => array(),
  31. 'empty' => t('There are no controlled vocabularies'),
  32. );
  33. $content = array(
  34. 'description' => array(
  35. '#type' => 'markup',
  36. '#markup' => '<p>The following controlled vocabularies are used on this site. Click a vocabulary short name for more details.</p>',
  37. ),
  38. 'vocab_table' => array(
  39. '#type' => 'item',
  40. '#markup' => theme_table($table),
  41. ),
  42. );
  43. return $content;
  44. }
  45. /**
  46. * Provides the content for a single controlled vocabulary.
  47. */
  48. function tripal_vocabulary_lookup_vocab_page($vocabulary) {
  49. // set the breadcrumb
  50. $breadcrumb = array();
  51. $breadcrumb[] = l('Home', '<front>');
  52. $breadcrumb[] = l('Controlled Vocabularies', 'cv/lookup');
  53. drupal_set_breadcrumb($breadcrumb);
  54. $vocab = tripal_get_vocabulary_details($vocabulary);
  55. $vocab_table = tripal_vocabulary_get_vocab_details($vocab);
  56. if ($vocab['description']) {
  57. drupal_set_title($vocabulary . ': ' . $vocab['description']);
  58. }
  59. else {
  60. drupal_set_title($vocabulary);
  61. }
  62. // If we can't find the term then just return a message.
  63. if (!$vocab) {
  64. drupal_set_message('The vocabulary cannot be found on this site', 'error');
  65. return '';
  66. }
  67. $has_root = TRUE;
  68. $root_terms = tripal_get_vocabulary_root_terms($vocabulary);
  69. // If this vocabulary doesn't have root terms then it's either not an
  70. // ontology or not all of the terms are loaded. In this case, let's get
  71. // a paged list of all terms
  72. if (count($root_terms) == 0) {
  73. $root_terms = tripal_get_vocabulary_terms($vocabulary, 25);
  74. $has_root = FALSE;
  75. }
  76. $items = tripal_vocabulary_lookup_term_children_format($root_terms);
  77. if (count($root_terms) == 0) {
  78. $items = '<p>This vocabulary has no terms loaded</p>';
  79. }
  80. else {
  81. $items = '<p>Click the + icon (if present) to expand the tree. If ' .
  82. 'the full ontology or the term heirarchy is not loaded into this site, ' .
  83. 'then the tree will consist of all terms at the same level. ' .
  84. 'For some vocabularies, only a subset of terms are loaded</p>' . $items;
  85. }
  86. drupal_add_js(array(
  87. 'tripal' => array(
  88. 'cv_lookup' => array(
  89. 'vocabulary' => $vocabulary,
  90. ),
  91. ),
  92. ), 'setting');
  93. $content = array(
  94. 'vocab_table' => array(
  95. '#type' => 'item',
  96. '#title' => 'Details',
  97. '#markup' => '<p>A vocabulary is always identified by its short name ' .
  98. 'and sometimes it may offer multiple sub-vocabularies with different ' .
  99. 'names. Both are listed below.</p>' . $vocab_table,
  100. ),
  101. 'vocab_browser' => array(
  102. '#type' => 'item',
  103. '#title' => 'Term Browser',
  104. '#markup' => $items,
  105. ),
  106. );
  107. if (!$has_root) {
  108. $content['pager']= array(
  109. '#type' => 'markup',
  110. '#markup' => theme('pager'),
  111. );
  112. }
  113. // Add support for our custom tree viewer
  114. drupal_add_css(drupal_get_path('module', 'tripal') . '/theme/css/tripal.cv_lookup.css');
  115. drupal_add_js(drupal_get_path('module', 'tripal') . '/theme/js/tripal.cv_lookup.js', 'file');
  116. return $content;
  117. }
  118. /**
  119. * Generates a table view of the vocabulary.
  120. *
  121. * @param $vocab
  122. * The vocabulary array.
  123. * @return
  124. * An HTML rendered table describing the vocabulary.
  125. */
  126. function tripal_vocabulary_get_vocab_details($vocab) {
  127. $headers = array();
  128. $rows = array();
  129. $vocab_name = $vocab['name'];
  130. $short_name = $vocab['short_name'];
  131. if ($vocab['url']) {
  132. $short_name = l($vocab['short_name'], $vocab['url'], array('attributes' => array('target' => '_blank')));
  133. }
  134. $vocab_desc = $vocab['description'];
  135. $rows[] = array(
  136. array(
  137. 'data' => 'Short Name',
  138. 'header' => TRUE,
  139. 'width' => '20%',
  140. ),
  141. $short_name,
  142. );
  143. $rows[] = array(
  144. array(
  145. 'data' => 'Vocabulary Name(s)',
  146. 'header' => TRUE,
  147. 'width' => '20%',
  148. ),
  149. $vocab_name,
  150. );
  151. $rows[] = array(
  152. array(
  153. 'data' => 'Description',
  154. 'header' => TRUE,
  155. 'width' => '20%',
  156. ),
  157. $vocab_desc,
  158. );
  159. $rows[] = array(
  160. array(
  161. 'data' => 'Number of Terms Loaded on This Site',
  162. 'header' => TRUE,
  163. 'width' => '20%',
  164. ),
  165. number_format($vocab['num_terms']),
  166. );
  167. $table = array(
  168. 'header' => $headers,
  169. 'rows' => $rows,
  170. 'attributes' => array(),
  171. 'sticky' => FALSE,
  172. 'caption' => '',
  173. 'colgroups' => array(),
  174. 'empty' => '',
  175. );
  176. return theme_table($table);
  177. }
  178. /**
  179. * A helper function to format an array of terms into a list for the web page.
  180. *
  181. * @param $children
  182. * A list of children terms.
  183. */
  184. function tripal_vocabulary_lookup_term_children_format($children) {
  185. $items = '<ul id="tripal-cv-lookup-tree">';
  186. foreach ($children as $child) {
  187. $grand = tripal_get_term_children($child['vocabulary']['short_name'], $child['accession']);
  188. $num_grand = count($grand);
  189. $items .= '<li vocabulary = "' . $child['vocabulary']['short_name'] . '" ' .
  190. 'accession = "' . $child['accession'] . '" ' .
  191. 'children = "' . $num_grand . '" ' .
  192. 'state = "closed" '.
  193. 'class = "cv-lookup-tree-node">';
  194. $class = 'tree-node-closed';
  195. if ($num_grand == 0) {
  196. $class = 'tree-node-single';
  197. }
  198. $items .= '<i class = "tree-node-icon ' . $class . '"></i>';
  199. $items .= l($child['name'], 'cv/lookup/' . $child['vocabulary']['short_name'] . '/' . $child['accession'], array('attributes' => array('target' => '_blank')));
  200. if ($child['accession'] != $child['name']) {
  201. $items .= ' [' . $child['vocabulary']['short_name'] . ':' . $child['accession'] . '] ';
  202. }
  203. $items .= '</li>';
  204. }
  205. $items .= '</ul>';
  206. if (count($children)== 0) {
  207. $items ='';
  208. }
  209. return $items;
  210. }
  211. /**
  212. * An ajax callback to get the children of a term.
  213. *
  214. * @param $vocabulary
  215. * The short name of the vocabulary (e.g. SO, GO, etc.)
  216. * @param $accession
  217. * The term accession.
  218. *
  219. * @return
  220. * A JSON array compatible with the JSTree library.
  221. * https://www.jstree.com/docs/json/
  222. */
  223. function tripal_vocabulary_lookup_term_children_ajax($vocabulary, $accession) {
  224. $term = tripal_get_term_details($vocabulary, $accession);
  225. $children = tripal_get_term_children($vocabulary, $accession);
  226. $response = array(
  227. 'vocabulary' => $vocabulary,
  228. 'accession' => $accession,
  229. 'content' => tripal_vocabulary_lookup_term_children_format($children)
  230. );
  231. drupal_json_output($response);
  232. }
  233. /**
  234. *
  235. * @param $vocabulary
  236. * @param $accession
  237. *
  238. * @return
  239. */
  240. function tripal_vocabulary_lookup_term_page($vocabulary, $accession) {
  241. // set the breadcrumb
  242. $breadcrumb = array();
  243. $breadcrumb[] = l('Home', '<front>');
  244. $breadcrumb[] = l('Controlled Vocabularies', 'cv/lookup');
  245. $breadcrumb[] = l($vocabulary, 'cv/lookup/' . $vocabulary);
  246. drupal_set_breadcrumb($breadcrumb);
  247. $vocab = tripal_get_vocabulary_details($vocabulary);
  248. $vocab_table = tripal_vocabulary_get_vocab_details($vocab);
  249. $term = tripal_get_term_details($vocabulary, $accession);
  250. // If we can't find the term then just return a message.
  251. if (!$term) {
  252. drupal_set_message('The term cannot be found on this site', 'error');
  253. return '';
  254. }
  255. // Build the Term table.
  256. $headers = array();
  257. $rows = array();
  258. $term_name = $term['name'];
  259. $accession = $term['vocabulary']['short_name'] . ':' . $term['accession'];
  260. drupal_set_title($accession);
  261. // Create a URL to point this term to it's source page, but only if the
  262. // source is not this site.
  263. if ($term['url'] and !preg_match('/cv\/lookup/', $term['url'])) {
  264. $accession = l($accession, $term['url'], array('attributes' => array('target' => '_blank')));
  265. }
  266. $rows[] = array(
  267. array(
  268. 'data' => 'Term',
  269. 'header' => TRUE,
  270. 'width' => '20%',
  271. ),
  272. $accession,
  273. );
  274. $rows[] = array(
  275. array(
  276. 'data' => 'Name',
  277. 'header' => TRUE,
  278. 'width' => '20%',
  279. ),
  280. $term_name,
  281. );
  282. $rows[] = array(
  283. array(
  284. 'data' => 'Definition',
  285. 'header' => TRUE,
  286. 'width' => '20%',
  287. ),
  288. $term['definition'],
  289. );
  290. // Now iterate through any other columns in the term array and add those
  291. // details.
  292. foreach ($term as $key => $value) {
  293. if (in_array($key, ['name', 'definition', 'vocabulary', 'accession', 'url'])) {
  294. continue;
  295. }
  296. // Convert thisto an array so we can alter it.
  297. if (!is_array($value)) {
  298. $new_values[] = $value;
  299. $value = $new_values;
  300. }
  301. // If this is a relationship key then let's try to rewrite the GO
  302. // term in the relationship as a link.
  303. if ($key == 'relationship') {
  304. foreach ($value as $index => $v) {
  305. $matches = [];
  306. if (preg_match('/^(.+)\s(.+?):(.+?)$/', $v, $matches)) {
  307. $rel = $matches[1];
  308. $voc = $matches[2];
  309. $acc = $matches[3];
  310. $v = $rel . ' ' . l($voc . ':' . $acc, 'cv/lookup/' . $voc . '/' . $acc, ['attributes' => ['target' => '_blank']]);
  311. $t = tripal_get_term_details($voc, $acc);
  312. if ($t) {
  313. $v .= ' (' . $t['name'] . ')';
  314. }
  315. $value[$index] = $v;
  316. }
  317. }
  318. }
  319. if (count($value) > 1) {
  320. $value_str = theme_item_list([
  321. 'items' => $value,
  322. 'type' => 'ul',
  323. 'attributes' => [],
  324. 'title' => '',
  325. ]);
  326. }
  327. else {
  328. $value_str = $value[0];
  329. }
  330. $rows[] = array(
  331. array(
  332. 'data' => ucfirst($key),
  333. 'header' => TRUE,
  334. 'width' => '20%',
  335. ),
  336. $value_str,
  337. );
  338. }
  339. $table = array(
  340. 'header' => $headers,
  341. 'rows' => $rows,
  342. 'attributes' => array(),
  343. 'sticky' => FALSE,
  344. 'caption' => '',
  345. 'colgroups' => array(),
  346. 'empty' => '',
  347. );
  348. $content['cvterm'] = [
  349. '#type' => 'item',
  350. '#title' => 'Term Details',
  351. '#markup' => theme_table($table),
  352. ];
  353. $content['vocabulary'] = [
  354. '#type' => 'item',
  355. '#title' => 'Vocabulary Details',
  356. '#markup' => $vocab_table,
  357. ];
  358. drupal_add_js(array(
  359. 'tripal' => array(
  360. 'cv_lookup' => array(
  361. 'vocabulary' => $vocabulary,
  362. 'accession' => $accession,
  363. ),
  364. ),
  365. ), 'setting');
  366. return $content;
  367. }