trees.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 = tripal_db_set_active('chado');
  81. $results = db_query($sql,$cv_id);
  82. tripal_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 = tripal_db_set_active('chado');
  102. $cnt_results = db_query($cnt_sql,$term->cvterm_id);
  103. tripal_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 = tripal_db_set_active('chado');
  143. $results = db_query($sql,$cvterm_id);
  144. tripal_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 = tripal_db_set_active('chado');
  166. $cnt_results = db_query($cnt_sql,$term->subject_id);
  167. tripal_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 = tripal_db_set_active('chado');
  177. $children = db_fetch_object(db_query($sql,$term->subject_id));
  178. tripal_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 = "
  200. <div id=\"tripal_cv_cvterm_info_box\">
  201. <a href=\"#\" onclick=\"$('#tripal_cv_cvterm_info_box').hide()\" style=\"float: right\">Close [X]</a>
  202. <h3>Term Information</h3>
  203. <div id=\"tripal_cv_cvterm_info\"></div>
  204. </div>
  205. <div id=\"tripal_ajaxLoading\" style=\"display:none\">
  206. <div id=\"loadingText\">Loading...</div>
  207. <img src=\"$url\">
  208. </div>
  209. <h3>Tree Browser</h3>
  210. <div id=\"browser\"</div></div>
  211. ";
  212. drupal_json(array('update' => "$content"));
  213. }
  214. /*************************************************************************
  215. *
  216. */
  217. function tripal_cv_cvterm_info($cvterm_id){
  218. # get the id of the term to look up
  219. $cv = check_plain($_REQUEST['cv']);
  220. $tree_id = check_plain($_REQUEST['tree_id']);
  221. // first get any additional information to add to the cvterm
  222. if(strcmp($tree_id,'undefined')!=0){
  223. $tripal_mod = preg_replace("/^(tripal_.+?)_cv_tree_(.+)$/","$1",$tree_id);
  224. if($tripal_mod){
  225. $callback = $tripal_mod . "_cvterm_add";
  226. $opt = call_user_func_array($callback,array($cvterm_id,$tree_id));
  227. }
  228. }
  229. $sql = "
  230. SELECT CVT.name as cvtermname, CVT.definition, CV.name as cvname,
  231. DBX.accession,DB.urlprefix,DB.db_id,DB.name as dbname
  232. FROM {CVTerm} CVT
  233. INNER JOIN CV on CVT.cv_id = CV.cv_id
  234. INNER JOIN dbxref DBX on CVT.dbxref_id = DBX.dbxref_id
  235. INNER JOIN DB on DBX.db_id = DB.db_id
  236. WHERE CVT.cvterm_id = %d
  237. ";
  238. $previous_db = tripal_db_set_active('chado');
  239. $cvterm = db_fetch_object(db_query($sql,$cvterm_id));
  240. tripal_db_set_active($previous_db);
  241. $sql = "
  242. SELECT CVTS.synonym, CVT.name as cvname
  243. FROM {cvtermsynonym} CVTS
  244. INNER JOIN cvterm CVT on CVTS.type_id = CVT.cvterm_id
  245. WHERE CVTS.cvterm_id = %d
  246. ";
  247. $previous_db = tripal_db_set_active('chado');
  248. $results = db_query($sql,$cvterm_id);
  249. tripal_db_set_active($previous_db);
  250. while($synonym = db_fetch_object($results)){
  251. $synonym_rows .= "<b>$synonym->cvname:</b> $synonym->synonym<br>";
  252. }
  253. $accession = $cvterm->accession;
  254. if($cvterm->urlprefix){
  255. $accession = "<a href=\"$cvterm->urlprefix$cvterm->accession\">$cvterm->accession</a>";
  256. }
  257. $content = "
  258. <div id=\"cvterm\">
  259. <table>
  260. <tr><th>Term</th><td>$cvterm->cvtermname</td></tr>
  261. <tr><th>Accession</th><td>$accession</td></tr>
  262. <tr><th>Ontology</th><td>$cvterm->cvname</td></tr>
  263. <tr><th>Definition</th><td>$cvterm->definition</td></tr>
  264. <tr><th>Synonyms</th><td>$synonym_rows</td></tr>
  265. <tr><th>Internal ID</th><td>$cvterm_id</td></tr>
  266. ";
  267. // now add in any additional options from a hook
  268. if($opt){
  269. foreach ($opt as $key=>$value){
  270. $content .= "<tr><th>$key</th><td>$value</td>";
  271. }
  272. }
  273. // close out the information table
  274. $content .= "
  275. </table>
  276. </div>
  277. ";
  278. drupal_json(array('update' => $content));
  279. }
  280. /*******************************************************************************
  281. */
  282. function tripal_cv_list_form($form_state) {
  283. // get a list of db from chado for user to choose
  284. $sql = "
  285. SELECT DISTINCT CV.name,CV.cv_id
  286. FROM {cvterm_relationship} CVTR
  287. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  288. INNER JOIN CV on CV.cv_id = CVT.cv_id
  289. ";
  290. $previous_db = tripal_db_set_active('chado'); // use chado database
  291. $results = db_query ($sql);
  292. tripal_db_set_active($previous_db);
  293. $blastdbs = array();
  294. $cvs[''] = '';
  295. while ($cv = db_fetch_object($results)){
  296. $cvs[$cv->cv_id] = $cv->name;
  297. }
  298. $form['db_options'] = array(
  299. '#type' => 'value',
  300. '#value' => $cvs
  301. );
  302. $form['cv_list'] = array(
  303. '#title' => t('CVs with relationships'),
  304. '#type' => 'select',
  305. '#description' => t('Choose the controlled vocabulary to browse'),
  306. '#options' => $form['db_options']['#value'],
  307. '#attributes' => array(
  308. 'onChange' => "return tripal_cv_init_browser(this)",
  309. )
  310. );
  311. return $form;
  312. }