trees.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /**
  3. *
  4. * @ingroup tripal_cv
  5. */
  6. function tripal_cv_show_browser() {
  7. $content = drupal_get_form('tripal_cv_list_form');
  8. $content .= "
  9. <div id=\"cv_browser\"></div>
  10. ";
  11. return $content;
  12. }
  13. /**
  14. * Generates JSON used for generating an exapandable tree of terms from
  15. * a controlled vocabulary that has associated counts.
  16. *
  17. * The progammer must first create a materialized view that
  18. * will generate count data for a given controlled vocabulary. For example, the Tripal
  19. * Analysis GO creates a materialized view for counting Gene Ontology assignments
  20. * to features. This view is created on install of the module and is named
  21. * 'go_count_analysis'.
  22. *
  23. * Second, the progammer must add an HTML 'div' box to the desired page
  24. * with a class name of 'tripal_cv_tree', and an id of the following format:
  25. *
  26. * tripal_[module_name]_cv_tree_[unique id]
  27. *
  28. * where [module_name] is the name of the tripal module (e.g. tripal_analyisis_go)
  29. * and [unique id] is some unique identifier that the contolling module
  30. * recognizes. This string is the $tree_id variable passed as the first argument
  31. * to the function. For example, the Tripal GO Analysis module generates
  32. * tree ids of the form:
  33. *
  34. * tripal_analysis_go_cv_tree_10_2_bp
  35. *
  36. * In this case the module that will manage this tree is identified as 'tripal_analysis_go' and within
  37. * the [unique id] portion contains the
  38. * organism_id (e.g. 10), analysis_id (e.g. 2) and tree type (bp = biological process).
  39. *
  40. * Second, the programmer must then define a hook in the controlling module for setting
  41. * some options used to build the chart. The hook has the form: hook_cv_tree($tree_id).
  42. * This hook should accept the full $tree_id as the single parameter. For the Tripal
  43. * Analysis GO module the hook is named: tripal_analysis_go_cv_tree.
  44. *
  45. * The array returned by this hook must have the following fields:
  46. * - cv_id
  47. * the cv_id for the controlled vocabulary
  48. * - count_mview
  49. * the name of the materialized view that contains the count data
  50. * this materialized view must have at least two columns, one with the cvterm_id
  51. * for each term and a second column containing the counts. The tree will only
  52. * be expanded to include child terms that have counts.
  53. * - cvterm_id_column
  54. * the column name in the materialized view that contains
  55. * the cvterm_ids
  56. * - count_column
  57. * the column name in the materialized view that contains the
  58. * counts
  59. * - filter
  60. * an SQL compatible WHERE clause string (whithout the word 'WHERE')
  61. * that can be used for filtering the records in the materialized view.
  62. * - label
  63. * the title for the tree
  64. *
  65. * Example from the tripal_analysis_go module:
  66. * @code
  67. * function tripal_analysis_go_cv_tree($tree_id){
  68. *
  69. * $organism_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$tree_id);
  70. * $analysis_id = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$tree_id);
  71. * $type = preg_replace("/^tripal_analysis_go_cv_tree_(\d+)-(\d+)_(bp|cc|mf)$/","$3",$tree_id);
  72. *
  73. * if(strcmp($type,'mf')==0){
  74. * $class = 'molecular_function';
  75. * }
  76. * if(strcmp($type,'cc')==0){
  77. * $class = 'cellular_component';
  78. * }
  79. * if(strcmp($type,'bp')==0){
  80. * $class = 'biological_process';
  81. * }
  82. *
  83. * $options = array(
  84. * cv_id => tripal_cv_get_cv_id($class),
  85. * count_mview => 'go_count_analysis',
  86. * cvterm_id_column => 'cvterm_id',
  87. * count_column => 'feature_count',
  88. * filter => "CNT.organism_id = $organism_id AND CNT.analysis_id = $analysis_id",
  89. * label => 'Features',
  90. * );
  91. * return $options;
  92. * }
  93. *
  94. * @endcode
  95. *
  96. * @param $tree_id
  97. * The unique identifier for the tree
  98. *
  99. * @return
  100. * JSON array needed for the jsTree package for generating expandable trees.
  101. *
  102. * With these three components (materialized view, a 'div' box with proper CSS class and ID, and a hook_cv_tree)
  103. * a tree will be created on the page. There is no need to call this function directly.
  104. *
  105. * @ingroup tripal_cv
  106. */
  107. function tripal_cv_tree($tree_id){
  108. // parse out the tripal module name from the chart_id to find out
  109. // which Tripal "hook" to call:
  110. $tripal_mod = preg_replace("/^(tripal_.+?)_cv_tree_(.+)$/","$1",$tree_id);
  111. if($tripal_mod){
  112. $callback = $tripal_mod . "_cv_tree";
  113. // now call the function in the module responsible for the tree. This
  114. // should call the tripal_cv_init_cv with the proper parameters set for
  115. // getting the cv_id of the vocabulary to use
  116. $opt = call_user_func_array($callback,array($tree_id));
  117. // we only need to return the cv_id for this function call.
  118. $json_array[] = $opt[cv_id];
  119. }
  120. $json_array[] = $tree_id;
  121. return drupal_json($json_array);
  122. }
  123. /**
  124. *
  125. * @ingroup tripal_cv
  126. */
  127. function tripal_cv_update_tree() {
  128. $content = array();
  129. $ontology = 'sequence';
  130. # get the id of the term to look up
  131. $cv = check_plain($_REQUEST['cv']);
  132. $term = check_plain($_REQUEST['term']);
  133. $tree_id = check_plain($_REQUEST['tree_id']);
  134. # get the options needed for this tree from the tripal module that
  135. # wants to create the tree
  136. $tripal_mod = preg_replace("/^(tripal_.+?)_cv_tree_(.+)$/","$1",$tree_id);
  137. if($tripal_mod){
  138. $callback = $tripal_mod . "_cv_tree";
  139. $opt = call_user_func_array($callback,array($tree_id));
  140. }
  141. # get the CV root terms
  142. if(strcmp($term,'root')==0){
  143. if(!$cv){
  144. $cv = $opt[cv_id];
  145. }
  146. $content = tripal_cv_init_tree($cv,$opt[count_mview],
  147. $opt[cvterm_id_column],$opt[count_column],$opt[filter],$opt[label]);
  148. }
  149. # get the children terms
  150. else {
  151. $content = tripal_cv_get_term_children($term,$opt[count_mview],
  152. $opt[cvterm_id_column],$opt[count_column],$opt[filter],$opt[label]);
  153. }
  154. drupal_json($content);
  155. }
  156. /**
  157. * Generates JSON needed for jsTree Root-level Branches
  158. *
  159. * This function returns the JSON array for the jsTree
  160. * jQuery code that builds a tree for browsing the ontology. This function
  161. * should be called to generate the root level branches of the tree.
  162. *
  163. * @ingroup tripal_cv
  164. */
  165. function tripal_cv_init_tree($cv_id,$cnt_table = null, $fk_column = null,
  166. $cnt_column = null, $filter = null, $label = null) {
  167. // get the list of root terms for the provided CV
  168. $sql = "
  169. SELECT *
  170. FROM {cv_root_mview} CRM
  171. WHERE cv_id = %d
  172. ";
  173. $previous_db = tripal_db_set_active('chado');
  174. $results = db_query($sql,$cv_id);
  175. tripal_db_set_active($previous_db);
  176. // prepare the SQL statement that will allow us to pull out count
  177. // information for each term in the tree.
  178. if($cnt_table){
  179. if(!$filter){
  180. $filter = '(1=1)';
  181. }
  182. $cnt_sql = "
  183. SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
  184. FROM {$cnt_table} CNT
  185. INNER JOIN cvterm CVT on CNT.$fk_column = CVT.cvterm_id
  186. WHERE $filter AND CVT.cvterm_id = %d
  187. ORDER BY $cnt_column desc
  188. ";
  189. }
  190. while ($term = db_fetch_object($results)) {
  191. $name = $term->name;
  192. $count = 0;
  193. if($cnt_table){
  194. $previous_db = tripal_db_set_active('chado');
  195. $cnt_results = db_query($cnt_sql,$term->cvterm_id);
  196. tripal_db_set_active($previous_db);
  197. while($cnt = db_fetch_object($cnt_results)){
  198. $count += $cnt->cnt;
  199. }
  200. if($count > 0){
  201. $name .= " ($count $label(s))";
  202. }
  203. }
  204. $content[] = array(
  205. 'attributes' => array (
  206. 'id' => $term->cvterm_id,
  207. ),
  208. state => 'closed',
  209. data => $name,
  210. children => array (),
  211. );
  212. }
  213. return $content;
  214. }
  215. /**
  216. * Generates SON needed for jsTree -expanding a term to view children
  217. *
  218. * This function returns the JSON array for the jsTree
  219. * jQuery code when expanding a term to view it's children.
  220. *
  221. * @ingroup tripal_cv_api
  222. */
  223. function tripal_cv_get_term_children($cvterm_id,$cnt_table = null,
  224. $fk_column = null,$cnt_column = null, $filter = null, $label = null) {
  225. # get the children for the term provided
  226. $sql = "
  227. SELECT CVTR.cvterm_relationship_id,CVTR.subject_id,
  228. CVT1.name as subject_name, CVT3.name as type_name, CVTR.type_id,
  229. CVT2.name as object_name,CVTR.object_id
  230. FROM {cvterm_relationship} CVTR
  231. INNER JOIN CVTerm CVT1 on CVTR.subject_id = CVT1.cvterm_id
  232. INNER JOIN CVTerm CVT2 on CVTR.object_id = CVT2.cvterm_id
  233. INNER JOIN CVTerm CVT3 on CVTR.type_id = CVT3.cvterm_id
  234. INNER JOIN CV on CV.cv_id = CVT1.cv_id
  235. WHERE CVTR.object_id = %d
  236. ORDER BY CVT1.name
  237. ";
  238. $previous_db = tripal_db_set_active('chado');
  239. $results = db_query($sql,$cvterm_id);
  240. tripal_db_set_active($previous_db);
  241. // prepare the SQL statement that will allow us to pull out count
  242. // information for each term in the tree.
  243. if($cnt_table){
  244. if(!$filter){
  245. $filter = '(1=1)';
  246. }
  247. $cnt_sql = "
  248. SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
  249. FROM {$cnt_table} CNT
  250. INNER JOIN cvterm CVT on CNT.$fk_column = CVT.cvterm_id
  251. WHERE $filter AND CVT.cvterm_id = %d
  252. ORDER BY $cnt_column desc
  253. ";
  254. }
  255. // populate the JSON content array
  256. while ($term = db_fetch_object($results)) {
  257. // count the number of items per term if requested
  258. $name = $term->subject_name;
  259. $count = 0;
  260. if($cnt_table){
  261. $previous_db = tripal_db_set_active('chado');
  262. $cnt_results = db_query($cnt_sql,$term->subject_id);
  263. tripal_db_set_active($previous_db);
  264. while($cnt = db_fetch_object($cnt_results)){
  265. $count += $cnt->num_items;
  266. }
  267. if($count > 0){
  268. $name .= " (".number_format($count)." $label)";
  269. // check if we have any children if so then set the value
  270. $previous_db = tripal_db_set_active('chado');
  271. $children = db_fetch_object(db_query($sql,$term->subject_id));
  272. tripal_db_set_active($previous_db);
  273. $state = 'leaf';
  274. if($children){
  275. $state = 'closed';
  276. }
  277. $content[] = array(
  278. 'attributes' => array (
  279. 'id' => $term->subject_id,
  280. ),
  281. state => $state,
  282. data => $name,
  283. children => array(),
  284. );
  285. }
  286. } else {
  287. // check if we have any children if so then set the value
  288. $previous_db = tripal_db_set_active('chado');
  289. $children = db_fetch_object(db_query($sql,$term->subject_id));
  290. tripal_db_set_active($previous_db);
  291. $state = 'leaf';
  292. if($children){
  293. $state = 'closed';
  294. }
  295. $content[] = array(
  296. 'attributes' => array (
  297. 'id' => $term->subject_id,
  298. ),
  299. state => $state,
  300. data => $name,
  301. children => array(),
  302. );
  303. }
  304. }
  305. $content[] = $cnt_sql;
  306. return $content;
  307. }
  308. /**
  309. *
  310. * @ingroup tripal_cv
  311. */
  312. function tripal_cv_init_browser($cv_id) {
  313. $content = "
  314. <div id=\"tripal_cv_cvterm_info_box\">
  315. <a href=\"#\" onclick=\"$('#tripal_cv_cvterm_info_box').hide()\" style=\"float: right\">Close [X]</a>
  316. <h3>Term Information</h3>
  317. <div id=\"tripal_cv_cvterm_info\"></div>
  318. </div>
  319. <div id=\"tripal_ajaxLoading\" style=\"display:none\">
  320. <div id=\"loadingText\">Loading...</div>
  321. <img src=\"$url\">
  322. </div>
  323. <h3>Tree Browser</h3>
  324. <div id=\"browser\"</div></div>
  325. ";
  326. drupal_json(array('update' => "$content"));
  327. }
  328. /**
  329. *
  330. * @ingroup tripal_cv
  331. */
  332. function tripal_cv_cvterm_info($cvterm_id){
  333. # get the id of the term to look up
  334. $cv = check_plain($_REQUEST['cv']);
  335. $tree_id = check_plain($_REQUEST['tree_id']);
  336. // first get any additional information to add to the cvterm
  337. if(strcmp($tree_id,'undefined')!=0){
  338. $tripal_mod = preg_replace("/^(tripal_.+?)_cv_tree_(.+)$/","$1",$tree_id);
  339. if($tripal_mod){
  340. $callback = $tripal_mod . "_cvterm_add";
  341. $opt = call_user_func_array($callback,array($cvterm_id,$tree_id));
  342. }
  343. }
  344. $sql = "
  345. SELECT CVT.name as cvtermname, CVT.definition, CV.name as cvname,
  346. DBX.accession,DB.urlprefix,DB.db_id,DB.name as dbname
  347. FROM {CVTerm} CVT
  348. INNER JOIN CV on CVT.cv_id = CV.cv_id
  349. INNER JOIN dbxref DBX on CVT.dbxref_id = DBX.dbxref_id
  350. INNER JOIN DB on DBX.db_id = DB.db_id
  351. WHERE CVT.cvterm_id = %d
  352. ";
  353. $previous_db = tripal_db_set_active('chado');
  354. $cvterm = db_fetch_object(db_query($sql,$cvterm_id));
  355. tripal_db_set_active($previous_db);
  356. $sql = "
  357. SELECT CVTS.synonym, CVT.name as cvname
  358. FROM {cvtermsynonym} CVTS
  359. INNER JOIN cvterm CVT on CVTS.type_id = CVT.cvterm_id
  360. WHERE CVTS.cvterm_id = %d
  361. ";
  362. $previous_db = tripal_db_set_active('chado');
  363. $results = db_query($sql,$cvterm_id);
  364. tripal_db_set_active($previous_db);
  365. while($synonym = db_fetch_object($results)){
  366. $synonym_rows .= "<b>$synonym->cvname:</b> $synonym->synonym<br>";
  367. }
  368. $accession = $cvterm->accession;
  369. if($cvterm->urlprefix){
  370. $accession = "<a href=\"$cvterm->urlprefix$cvterm->accession\">$cvterm->accession</a>";
  371. }
  372. $content = "
  373. <div id=\"cvterm\">
  374. <table>
  375. <tr><th>Term</th><td>$cvterm->cvtermname</td></tr>
  376. <tr><th>Accession</th><td>$accession</td></tr>
  377. <tr><th>Ontology</th><td>$cvterm->cvname</td></tr>
  378. <tr><th>Definition</th><td>$cvterm->definition</td></tr>
  379. <tr><th>Synonyms</th><td>$synonym_rows</td></tr>
  380. <tr><th>Internal ID</th><td>$cvterm_id</td></tr>
  381. ";
  382. // now add in any additional options from a hook
  383. if($opt){
  384. foreach ($opt as $key=>$value){
  385. $content .= "<tr><th>$key</th><td>$value</td>";
  386. }
  387. }
  388. // close out the information table
  389. $content .= "
  390. </table>
  391. </div>
  392. ";
  393. drupal_json(array('update' => $content));
  394. }
  395. /**
  396. *
  397. * @ingroup tripal_cv
  398. */
  399. function tripal_cv_list_form($form_state) {
  400. // get a list of db from chado for user to choose
  401. $sql = "
  402. SELECT DISTINCT CV.name,CV.cv_id
  403. FROM {cvterm_relationship} CVTR
  404. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  405. INNER JOIN CV on CV.cv_id = CVT.cv_id
  406. ";
  407. $previous_db = tripal_db_set_active('chado'); // use chado database
  408. $results = db_query ($sql);
  409. tripal_db_set_active($previous_db);
  410. $blastdbs = array();
  411. $cvs[''] = '';
  412. while ($cv = db_fetch_object($results)){
  413. $cvs[$cv->cv_id] = $cv->name;
  414. }
  415. $form['db_options'] = array(
  416. '#type' => 'value',
  417. '#value' => $cvs
  418. );
  419. $form['cv_list'] = array(
  420. '#title' => t('CVs with relationships'),
  421. '#type' => 'select',
  422. '#description' => t('Choose the controlled vocabulary to browse'),
  423. '#options' => $form['db_options']['#value'],
  424. '#attributes' => array(
  425. 'onChange' => "return tripal_cv_init_browser(this)",
  426. )
  427. );
  428. return $form;
  429. }