charts.php 9.2 KB

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