trees.php 11 KB

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