charts.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Generates JSON used for generating a Google chart of count data associated
  4. * with a controlled vocabulary. An example would be features assigned to
  5. * Gene Ontology terms.
  6. *
  7. * To generate a chart, the progammer must first create a materialized view that
  8. * will generate count data for a given controlled vocabulary. For example, the Tripal
  9. * Analysis GO creates a materialized view for counting Gene Ontology assignments
  10. * to features. This view is created on install of the module and is named
  11. * 'go_count_analysis'.
  12. *
  13. * Next, an HTML 'div' box must be added to the desired page
  14. * with a class name of 'tripal_cv_chart', and an id of the following format:
  15. *
  16. * tripal_[module_name]_cv_chart_[unique id]
  17. *
  18. * where [module_name] is the name of the tripal module (e.g. tripal_analyisis_go)
  19. * and [unique id] is some unique identifier that the contolling module
  20. * recognizes. This string is the $chart_id variable passed as the first argument
  21. * to the function. For example, the Tripal GO Analysis module generates
  22. * chart ids of the form:
  23. *
  24. * tripal_analysis_go_cv_chart_10_2_bp
  25. *
  26. * In this case the module that will manage this chart is identified as 'tripal_analysis_go' and within
  27. * the [unique id] portion contains the
  28. * organism_id (e.g. 10), analysis_id (e.g. 2) and chart type (bp = biological process).
  29. *
  30. * Second, the programmer must then define a hook in the controlling module for setting
  31. * some options used to build the chart. The hook has the form: hook_cv_chart($chart_id).
  32. * This hook should accept the full $chart_id as the single parameter. For the Tripal
  33. * Analysis GO module the hook is named: tripal_analysis_go_cv_chart.
  34. *
  35. * The array returned by this hook must have the following fields:
  36. * - count_mview
  37. * the name of the materialized view that contains the count data
  38. * this materialized view must have at least two columns, one with the cvterm_id
  39. * for each term and a second column containing the counts
  40. * - cvterm_id_column
  41. * the column name in the materialized view that contains
  42. * the cvterm_ids
  43. * - count_column
  44. * the column name in the materialized view that contains the
  45. * counts
  46. * - filter
  47. * an SQL compatible WHERE clause string (whithout the word 'WHERE')
  48. * that can be used for filtering the records in the materialized view.
  49. * - title
  50. * the title for the chart
  51. * - type
  52. * the type of chart to create (see Google Charts documenation). Leave
  53. * blank for a pie chart.
  54. * - size
  55. * the dimensions of the chart in pixels (see Google Charts documenations
  56. * for exact size limitations).
  57. *
  58. * Example from the tripal_analysis_go module:
  59. * @code
  60. * function tripal_analysis_go_cv_chart($chart_id){
  61. *
  62. * // The CV module will create the JSON array necessary for buillding a
  63. * // pie chart using jgChart and Google Charts. We have to pass to it
  64. * // a table that contains count information, tell it which column
  65. * // contains the cvterm_id and provide a filter for getting the
  66. * // results we want from the table.
  67. * $organism_id = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$1",$chart_id);
  68. * $analysis_id = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$2",$chart_id);
  69. * $type = preg_replace("/^tripal_analysis_go_cv_chart_(\d+)-(\d+)_(bp|cc|mf)$/","$3",$chart_id);
  70. *
  71. * $sql = "SELECT * FROM {Analysis} WHERE analysis_id = %d";
  72. * $previous_db = tripal_db_set_active('chado'); // use chado database
  73. * $analysis = db_fetch_object(db_query($sql,$analysis_id));
  74. * tripal_db_set_active($previous_db); // now use drupal database
  75. *
  76. * if(strcmp($type,'mf')==0){
  77. * $class = 'molecular_function';
  78. * $title = "Number of Molecular Function Terms From $analysis->name Analysis";
  79. * }
  80. * if(strcmp($type,'cc')==0){
  81. * $class = 'cellular_component';
  82. * $title = "Number of Cellular Component Terms From $analysis->name Analysis";
  83. * }
  84. * if(strcmp($type,'bp')==0){
  85. * $class = 'biological_process';
  86. * $title = "Number of Biological Process Terms From $analysis->name Analysis";
  87. * }
  88. * $options = array(
  89. * count_mview => 'go_count_analysis',
  90. * cvterm_id_column => 'cvterm_id',
  91. * count_column => 'feature_count',
  92. * filter => "
  93. * CNT.organism_id = $organism_id AND
  94. * CNT.analysis_id = $analysis_id AND
  95. * CNT.cvterm_id IN (
  96. * SELECT CVTR.subject_id
  97. * FROM {CVTerm_relationship} CVTR
  98. * INNER JOIN CVTerm CVT on CVTR.object_id = CVT.cvterm_id
  99. * INNER JOIN CV on CVT.cv_id = CV.cv_id
  100. * WHERE CVT.name = '$class' AND
  101. * CV.name = '$class'
  102. * )
  103. * ",
  104. * type => 'p',
  105. * size => '550x175',
  106. * title => $title,
  107. * );
  108. * return $options;
  109. * }
  110. *
  111. * @endcode
  112. *
  113. * @param $chart_id
  114. * The unique identifier for the chart
  115. *
  116. * @return
  117. * JSON array needed for the js caller
  118. *
  119. * With these three components (materialized view, a 'div' box with proper CSS class and ID, and a hook_cv_chart)
  120. * a chart will be created on the page. There is no need to call this function directly.
  121. *
  122. * @ingroup tripal_cv
  123. */
  124. function tripal_cv_chart($chart_id){
  125. // parse out the tripal module name from the chart_id to find out
  126. // which Tripal "hook" to call:
  127. $tripal_mod = preg_replace("/^(tripal_.+?)_cv_chart_(.+)$/","$1",$chart_id);
  128. $callback = $tripal_mod . "_cv_chart";
  129. // now call the function in the module responsible for the chart to fill out
  130. // an options array needed by the tripal_cv_count_chart call below.
  131. $opt = call_user_func_array($callback,array($chart_id));
  132. // build the JSON array to return to the javascript caller
  133. $json_arr = tripal_cv_count_chart($opt[count_mview],$opt[cvterm_id_column],
  134. $opt[count_column],$opt[filter],$opt[title], $opt[type],$opt[size]);
  135. $json_arr[] = $chart_id; // add the chart_id back into the json array
  136. return drupal_json($json_arr);
  137. }
  138. /**
  139. * This function generates an array with fields compatible with Google charts used
  140. * for generating pie charts of counts associated with terms in
  141. * a controlled vocabulary. An example would be counts of features assigned
  142. * to terms in the Sequence Ontology.
  143. *
  144. * @param $cnt_table
  145. * The name of the table (most likely a materialized view) that
  146. * contains count data for each term. The table must have at least
  147. * two columns, one with the cvterm_id for each term, and a second
  148. * column with the count (i.e. features assigned the term).
  149. * @param $fk_column
  150. * This is the name of the column in the $cnt_table that holds the
  151. * cvterm_id for each term.
  152. * @param $cnt_column
  153. * The name of the column in the $cnt_table containing the counts
  154. * @param $filter
  155. * An SQL compatible 'where' clause (without the word 'WHERE') used
  156. * to filter the records in the $cnt_table
  157. * @param $title
  158. * The title of the chart to be rendered.
  159. * @param $type
  160. * The type of chart to be rendered. The value used here is the same as
  161. * the type names for Google charts. Default is p3 (pie chart).
  162. * @param $size
  163. * The size in pixels of the chart to be rendered. Default is 300x75. The
  164. * size of the chart is constrained by Google charts. See the Google
  165. * chart documentation for exact limitations.
  166. *
  167. * @return
  168. * An array that has the settings needed for Google Charts to creat the chart.
  169. *
  170. * @ingroup tripal_cv
  171. */
  172. function tripal_cv_count_chart($cnt_table, $fk_column,
  173. $cnt_column, $filter = null, $title = '', $type = 'p3', $size='300x75') {
  174. if(!$type){
  175. $type = 'p3';
  176. }
  177. if(!$size){
  178. $size = '300x75';
  179. }
  180. if(!$filter){
  181. $filter = '(1=1)';
  182. }
  183. $isPie = 0;
  184. if(strcmp($type,'p')==0 or strcmp($type,'p3')==0){
  185. $isPie = 1;
  186. }
  187. $sql = "
  188. SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
  189. FROM {$cnt_table} CNT
  190. INNER JOIN {cvterm} CVT on CNT.$fk_column = CVT.cvterm_id
  191. WHERE $filter
  192. ";
  193. $features = array();
  194. $previous_db = tripal_db_set_active('chado'); // use chado database
  195. $results = db_query($sql);
  196. tripal_db_set_active($previous_db); // now use drupal database
  197. $data = array();
  198. $axis = array();
  199. $legend = array();
  200. $total = 0;
  201. $max = 0;
  202. $i = 1;
  203. while($term = db_fetch_object($results)){
  204. if($isPie){
  205. $axis[] = "$term->name (".number_format($term->num_items).")";
  206. $data[] = array($term->num_items,0,0);
  207. } else {
  208. $axis[] = "$term->name (".number_format($term->num_items).")";
  209. $data[] = array($term->num_items);
  210. // $legend[] = "$term->name (".number_format($term->num_items).")";
  211. }
  212. if($term->num_items > $max){
  213. $max = $term->num_items;
  214. }
  215. $total += $term->num_items;
  216. $i++;
  217. }
  218. // convert numerical values into percentages
  219. foreach($data as &$set){
  220. $set[0] = ($set[0] / $total) * 100;
  221. }
  222. $opt[] = array(
  223. data => $data,
  224. axis_labels => $axis,
  225. legend => $legend,
  226. size => $size,
  227. type => $type,
  228. bar_width => 10,
  229. bar_spacing => 0,
  230. title => $title
  231. );
  232. // $opt[] = $sql;
  233. return $opt;
  234. }