charts.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. * $previous_db = tripal_db_set_active('chado'); // use chado database
  77. * $analysis = db_fetch_object(db_query($sql,$analysis_id));
  78. * tripal_db_set_active($previous_db); // now use drupal database
  79. *
  80. * if (strcmp($type,'mf')==0) {
  81. * $class = 'molecular_function';
  82. * $title = "Number of Molecular Function Terms From $analysis->name Analysis";
  83. * }
  84. * if (strcmp($type,'cc')==0) {
  85. * $class = 'cellular_component';
  86. * $title = "Number of Cellular Component Terms From $analysis->name Analysis";
  87. * }
  88. * if (strcmp($type,'bp')==0) {
  89. * $class = 'biological_process';
  90. * $title = "Number of Biological Process Terms From $analysis->name Analysis";
  91. * }
  92. * $options = array(
  93. * count_mview => 'go_count_analysis',
  94. * cvterm_id_column => 'cvterm_id',
  95. * count_column => 'feature_count',
  96. * filter => "
  97. * CNT.organism_id = $organism_id AND
  98. * CNT.analysis_id = $analysis_id AND
  99. * CNT.cvterm_id IN (
  100. * SELECT CVTR.subject_id
  101. * FROM {CVTerm_relationship} CVTR
  102. * INNER JOIN CVTerm CVT on CVTR.object_id = CVT.cvterm_id
  103. * INNER JOIN CV on CVT.cv_id = CV.cv_id
  104. * WHERE CVT.name = '$class' AND
  105. * CV.name = '$class'
  106. * )
  107. * ",
  108. * type => 'p',
  109. * size => '550x175',
  110. * title => $title,
  111. * );
  112. * return $options;
  113. * }
  114. *
  115. * @endcode
  116. *
  117. * @param $chart_id
  118. * The unique identifier for the chart
  119. *
  120. * @return
  121. * JSON array needed for the js caller
  122. *
  123. * With these three components (materialized view, a 'div' box with proper CSS class and ID, and a hook_cv_chart)
  124. * a chart will be created on the page. There is no need to call this function directly.
  125. *
  126. * @ingroup tripal_cv
  127. */
  128. function tripal_cv_chart($chart_id) {
  129. // parse out the tripal module name from the chart_id to find out
  130. // which Tripal "hook" to call:
  131. $tripal_mod = preg_replace("/^(tripal_.+?)_cv_chart_(.+)$/", "$1", $chart_id);
  132. $callback = $tripal_mod . "_cv_chart";
  133. // now call the function in the module responsible for the chart to fill out
  134. // an options array needed by the tripal_cv_count_chart call below.
  135. $opt = call_user_func_array($callback, array($chart_id));
  136. // build the JSON array to return to the javascript caller
  137. $json_arr = tripal_cv_count_chart(
  138. $opt['count_mview'],
  139. $opt['cvterm_id_column'],
  140. $opt['count_column'],
  141. $opt['filter'],
  142. $opt['title'],
  143. $opt['type'],
  144. $opt['size']
  145. );
  146. $json_arr[] = $chart_id; // add the chart_id back into the json array
  147. return drupal_json($json_arr);
  148. }
  149. /**
  150. * This function generates an array with fields compatible with Google charts used
  151. * for generating pie charts of counts associated with terms in
  152. * a controlled vocabulary. An example would be counts of features assigned
  153. * to terms in the Sequence Ontology.
  154. *
  155. * @param $cnt_table
  156. * The name of the table (most likely a materialized view) that
  157. * contains count data for each term. The table must have at least
  158. * two columns, one with the cvterm_id for each term, and a second
  159. * column with the count (i.e. features assigned the term).
  160. * @param $fk_column
  161. * This is the name of the column in the $cnt_table that holds the
  162. * cvterm_id for each term.
  163. * @param $cnt_column
  164. * The name of the column in the $cnt_table containing the counts
  165. * @param $filter
  166. * An SQL compatible 'where' clause (without the word 'WHERE') used
  167. * to filter the records in the $cnt_table
  168. * @param $title
  169. * The title of the chart to be rendered.
  170. * @param $type
  171. * The type of chart to be rendered. The value used here is the same as
  172. * the type names for Google charts. Default is p3 (pie chart).
  173. * @param $size
  174. * The size in pixels of the chart to be rendered. Default is 300x75. The
  175. * size of the chart is constrained by Google charts. See the Google
  176. * chart documentation for exact limitations.
  177. *
  178. * @return
  179. * An array that has the settings needed for Google Charts to creat the chart.
  180. *
  181. * @ingroup tripal_cv
  182. */
  183. function tripal_cv_count_chart($cnt_table, $fk_column,
  184. $cnt_column, $filter = NULL, $title = '', $type = 'p3', $size='300x75') {
  185. if (!$type) {
  186. $type = 'p3';
  187. }
  188. if (!$size) {
  189. $size = '300x75';
  190. }
  191. if (!$filter) {
  192. $filter = '(1=1)';
  193. }
  194. $is_pie = 0;
  195. if (strcmp($type, 'p') == 0 or strcmp($type, 'p3') == 0) {
  196. $is_pie = 1;
  197. }
  198. $sql = "
  199. SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
  200. FROM {$cnt_table} CNT
  201. INNER JOIN {cvterm} CVT on CNT.$fk_column = CVT.cvterm_id
  202. WHERE $filter
  203. ";
  204. $features = array();
  205. $previous_db = tripal_db_set_active('chado'); // use chado database
  206. $results = db_query($sql);
  207. tripal_db_set_active($previous_db); // now use drupal database
  208. $data = array();
  209. $axis = array();
  210. $legend = array();
  211. $total = 0;
  212. $max = 0;
  213. $i = 1;
  214. while ($term = db_fetch_object($results)) {
  215. if ($is_pie) {
  216. $axis[] = "$term->name (" . number_format($term->num_items) . ")";
  217. $data[] = array($term->num_items, 0, 0);
  218. }
  219. else {
  220. $axis[] = "$term->name (" . number_format($term->num_items) . ")";
  221. $data[] = array($term->num_items);
  222. //$legend[] = "$term->name (" . number_format($term->num_items) . ")";
  223. }
  224. if ($term->num_items > $max) {
  225. $max = $term->num_items;
  226. }
  227. $total += $term->num_items;
  228. $i++;
  229. }
  230. // convert numerical values into percentages
  231. foreach ($data as &$set) {
  232. $set[0] = ($set[0] / $total) * 100;
  233. }
  234. $opt[] = array(
  235. data => $data,
  236. axis_labels => $axis,
  237. legend => $legend,
  238. size => $size,
  239. type => $type,
  240. bar_width => 10,
  241. bar_spacing => 0,
  242. title => $title
  243. );
  244. // $opt[] = $sql;
  245. return $opt;
  246. }