charts.inc 9.0 KB

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