trees.php 11 KB

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