trees.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. //
  3. // Copyright 2009 Clemson University
  4. //
  5. /*************************************************************************
  6. *
  7. */
  8. function tripal_cv_show_browser() {
  9. $content = drupal_get_form('tripal_cv_list_form');
  10. $content .= "
  11. <div id=\"cv_browser\"></div>
  12. ";
  13. return $content;
  14. }
  15. /*************************************************************************
  16. *
  17. */
  18. function tripal_cv_tree($tree_id){
  19. // parse out the tripal module name from the chart_id to find out
  20. // which Tripal "hook" to call:
  21. $tripal_mod = preg_replace("/^(tripal_.+?)_cv_tree_(.+)$/","$1",$tree_id);
  22. if($tripal_mod){
  23. $callback = $tripal_mod . "_cv_tree";
  24. // now call the function in the module responsible for the tree. This
  25. // should call the tripal_cv_init_cv with the proper parameters set for
  26. // getting the cv_id of the vocabulary to use
  27. $opt = call_user_func_array($callback,array($tree_id));
  28. // we only need to return the cv_id for this function call.
  29. $json_array[] = $opt[cv_id];
  30. }
  31. $json_array[] = $tree_id;
  32. return drupal_json($json_array);
  33. }
  34. /*************************************************************************
  35. *
  36. */
  37. function tripal_cv_update_tree() {
  38. $content = array();
  39. $ontology = 'sequence';
  40. # get the id of the term to look up
  41. $cv = check_plain($_REQUEST['cv']);
  42. $term = check_plain($_REQUEST['term']);
  43. $tree_id = check_plain($_REQUEST['tree_id']);
  44. # get the options needed for this tree from the tripal module that
  45. # wants to create the tree
  46. $tripal_mod = preg_replace("/^(tripal_.+?)_cv_tree_(.+)$/","$1",$tree_id);
  47. if($tripal_mod){
  48. $callback = $tripal_mod . "_cv_tree";
  49. $opt = call_user_func_array($callback,array($tree_id));
  50. }
  51. # get the CV root terms
  52. if(strcmp($term,'root')==0){
  53. if(!$cv){
  54. $cv = $opt[cv_id];
  55. }
  56. $content = tripal_cv_init_tree($cv,$opt[count_mview],
  57. $opt[cvterm_id_column],$opt[count_column],$opt[filter],$opt[label]);
  58. }
  59. # get the children terms
  60. else {
  61. $content = tripal_cv_get_term_children($term,$opt[count_mview],
  62. $opt[cvterm_id_column],$opt[count_column],$opt[filter],$opt[label]);
  63. }
  64. drupal_json($content);
  65. }
  66. /*************************************************************************
  67. * name: tripal_cv_init_cv
  68. * description: This function returns the JSON array for the jsTree
  69. * jQuery code that builds a tree for browsing the ontology. This function
  70. * should be called to generate the root level branches of the tree.
  71. */
  72. function tripal_cv_init_tree($cv_id,$cnt_table = null, $fk_column = null,
  73. $cnt_column = null, $filter = null, $label = null) {
  74. // get the list of root terms for the provided CV
  75. $sql = "
  76. SELECT *
  77. FROM {cv_root_mview} CRM
  78. WHERE cv_id = %d
  79. ";
  80. $previous_db = db_set_active('chado');
  81. $results = db_query($sql,$cv_id);
  82. db_set_active($previous_db);
  83. // prepare the SQL statement that will allow us to pull out count
  84. // information for each term in the tree.
  85. if($cnt_table){
  86. if(!$filter){
  87. $filter = '(1=1)';
  88. }
  89. $cnt_sql = "
  90. SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
  91. FROM {$cnt_table} CNT
  92. INNER JOIN cvterm CVT on CNT.$fk_column = CVT.cvterm_id
  93. WHERE $filter AND CVT.cvterm_id = %d
  94. ORDER BY $cnt_column desc
  95. ";
  96. }
  97. while ($term = db_fetch_object($results)) {
  98. $name = $term->name;
  99. $count = 0;
  100. if($cnt_table){
  101. $previous_db = db_set_active('chado');
  102. $cnt_results = db_query($cnt_sql,$term->cvterm_id);
  103. db_set_active($previous_db);
  104. while($cnt = db_fetch_object($cnt_results)){
  105. $count += $cnt->cnt;
  106. }
  107. if($count > 0){
  108. $name .= " ($count $label(s))";
  109. }
  110. }
  111. $content[] = array(
  112. 'attributes' => array (
  113. 'id' => $term->cvterm_id,
  114. ),
  115. state => 'closed',
  116. data => $name,
  117. children => array (),
  118. );
  119. }
  120. return $content;
  121. }
  122. /*************************************************************************
  123. * name: tripal_cv_get_term_children
  124. * description: This function returns the JSON array for the jsTree
  125. * jQuery code when expanding a term to view it's children.
  126. */
  127. function tripal_cv_get_term_children($cvterm_id,$cnt_table = null,
  128. $fk_column = null,$cnt_column = null, $filter = null, $label = null) {
  129. # get the children for the term provided
  130. $sql = "
  131. SELECT CVTR.cvterm_relationship_id,CVTR.subject_id,
  132. CVT1.name as subject_name, CVT3.name as type_name, CVTR.type_id,
  133. CVT2.name as object_name,CVTR.object_id
  134. FROM {cvterm_relationship} CVTR
  135. INNER JOIN CVTerm CVT1 on CVTR.subject_id = CVT1.cvterm_id
  136. INNER JOIN CVTerm CVT2 on CVTR.object_id = CVT2.cvterm_id
  137. INNER JOIN CVTerm CVT3 on CVTR.type_id = CVT3.cvterm_id
  138. INNER JOIN CV on CV.cv_id = CVT1.cv_id
  139. WHERE CVTR.object_id = %d
  140. ORDER BY CVT1.name
  141. ";
  142. $previous_db = db_set_active('chado');
  143. $results = db_query($sql,$cvterm_id);
  144. db_set_active($previous_db);
  145. // prepare the SQL statement that will allow us to pull out count
  146. // information for each term in the tree.
  147. if($cnt_table){
  148. if(!$filter){
  149. $filter = '(1=1)';
  150. }
  151. $cnt_sql = "
  152. SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
  153. FROM {$cnt_table} CNT
  154. INNER JOIN cvterm CVT on CNT.$fk_column = CVT.cvterm_id
  155. WHERE $filter AND CVT.cvterm_id = %d
  156. ORDER BY $cnt_column desc
  157. ";
  158. }
  159. // populate the JSON content array
  160. while ($term = db_fetch_object($results)) {
  161. // count the number of items per term if requested
  162. $name = $term->subject_name;
  163. $count = 0;
  164. if($cnt_table){
  165. $previous_db = db_set_active('chado');
  166. $cnt_results = db_query($cnt_sql,$term->subject_id);
  167. db_set_active($previous_db);
  168. while($cnt = db_fetch_object($cnt_results)){
  169. $count += $cnt->num_items;
  170. }
  171. if($count > 0){
  172. $name .= " (".number_format($count)." $label)";
  173. }
  174. }
  175. // check if we have any children if so then set the value
  176. $previous_db = db_set_active('chado');
  177. $children = db_fetch_object(db_query($sql,$term->subject_id));
  178. db_set_active($previous_db);
  179. $state = 'leaf';
  180. if($children){
  181. $state = 'closed';
  182. }
  183. $content[] = array(
  184. 'attributes' => array (
  185. 'id' => $term->subject_id,
  186. ),
  187. state => $state,
  188. data => $name,
  189. children => array(),
  190. );
  191. }
  192. $content[] = $cnt_sql;
  193. return $content;
  194. }
  195. /*************************************************************************
  196. *
  197. */
  198. function tripal_cv_init_browser($cv_id) {
  199. $content = "<div id=\"tripal_cv_term_box\" class=\"feature-info-box\">";
  200. $content .= "<div class=\"tripal_expandableBox\">";
  201. $content .= "<h3>Term Information</h3>";
  202. $content .= "</div>";
  203. $content .= "<div class=\"tripal_expandableBoxContent\">";
  204. $content .= "<div id=\"cvterm_info\"></div></div>";
  205. $content .= "<h3>Tree Browser</h3>";
  206. $content .= "<div id=\"browser\"</div></div>";
  207. drupal_json(array('update' => "$content"));
  208. }
  209. /*************************************************************************
  210. *
  211. */
  212. function tripal_cv_cvterm_info($cvterm_id){
  213. # get the id of the term to look up
  214. $cv = check_plain($_REQUEST['cv']);
  215. $tree_id = check_plain($_REQUEST['tree_id']);
  216. // first get any additional information to add to the cvterm
  217. $tripal_mod = preg_replace("/^(tripal_.+?)_cv_tree_(.+)$/","$1",$tree_id);
  218. if($tripal_mod){
  219. $callback = $tripal_mod . "_cvterm_add";
  220. $opt = call_user_func_array($callback,array($cvterm_id,$tree_id));
  221. }
  222. $sql = "
  223. SELECT CVT.name as cvtermname, CVT.definition, CV.name as cvname,
  224. DBX.accession,DB.urlprefix,DB.db_id,DB.name as dbname
  225. FROM {CVTerm} CVT
  226. INNER JOIN CV on CVT.cv_id = CV.cv_id
  227. INNER JOIN dbxref DBX on CVT.dbxref_id = DBX.dbxref_id
  228. INNER JOIN DB on DBX.db_id = DB.db_id
  229. WHERE CVT.cvterm_id = %d
  230. ";
  231. $previous_db = db_set_active('chado');
  232. $cvterm = db_fetch_object(db_query($sql,$cvterm_id));
  233. db_set_active($previous_db);
  234. $sql = "
  235. SELECT CVTS.synonym, CVT.name as cvname
  236. FROM {cvtermsynonym} CVTS
  237. INNER JOIN cvterm CVT on CVTS.type_id = CVT.cvterm_id
  238. WHERE CVTS.cvterm_id = %d
  239. ";
  240. $previous_db = db_set_active('chado');
  241. $results = db_query($sql,$cvterm_id);
  242. db_set_active($previous_db);
  243. while($synonym = db_fetch_object($results)){
  244. $synonym_rows .= "<b>$synonym->cvname:</b> $synonym->synonym<br>";
  245. }
  246. $accession = $cvterm->accession;
  247. if($cvterm->urlprefix){
  248. $accession = "<a href=\"$cvterm->urlprefix$cvterm->accession\">$cvterm->accession</a>";
  249. }
  250. $content = "
  251. <div id=\"cvterm\">
  252. <table>
  253. <tr><th>Term</th><td>$cvterm->cvtermname</td></tr>
  254. <tr><th>Accession</th><td>$accession</td></tr>
  255. <tr><th>Ontology</th><td>$cvterm->cvname</td></tr>
  256. <tr><th>Definition</th><td>$cvterm->definition</td></tr>
  257. <tr><th>Synonyms</th><td>$synonym_rows</td></tr>
  258. <tr><th>Internal ID</th><td>$cvterm_id</td></tr>
  259. ";
  260. // now add in any additional options from a hook
  261. foreach ($opt as $key=>$value){
  262. $content .= "<tr><th>$key</th><td>$value</td>";
  263. }
  264. // close out the information table
  265. $content .= "
  266. </table>
  267. </div>
  268. ";
  269. drupal_json(array('update' => $content));
  270. }
  271. /*******************************************************************************
  272. */
  273. function tripal_cv_list_form($form_state) {
  274. // get a list of db from chado for user to choose
  275. $sql = "
  276. SELECT DISTINCT CV.name,CV.cv_id
  277. FROM {cvterm_relationship} CVTR
  278. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  279. INNER JOIN CV on CV.cv_id = CVT.cv_id
  280. ";
  281. $previous_db = db_set_active('chado'); // use chado database
  282. $results = db_query ($sql);
  283. db_set_active($previous_db);
  284. $blastdbs = array();
  285. $cvs[''] = '';
  286. while ($cv = db_fetch_object($results)){
  287. $cvs[$cv->cv_id] = $cv->name;
  288. }
  289. $form['db_options'] = array(
  290. '#type' => 'value',
  291. '#value' => $cvs
  292. );
  293. $form['cv_list'] = array(
  294. '#title' => t('CVs with relationships'),
  295. '#type' => 'select',
  296. '#description' => t('Choose the controlled vocabulary to browse'),
  297. '#options' => $form['db_options']['#value'],
  298. '#attributes' => array(
  299. 'onChange' => "return tripal_cv_init_browser(this)",
  300. )
  301. );
  302. return $form;
  303. }