gnuplot.h
Go to the documentation of this file.
1 
9 #pragma once
10 #include <cctype>
16 
17 namespace illumina { namespace interop { namespace io { namespace plot
18 {
19 
23  {
24  public:
31  void write_flowcell(std::ostream &out,
32  const model::plot::flowcell_data &data,
33  const std::string &output_image_path = "")
34  {
35  write_output_format(out, output_image_path);
36  write_title(out, data);
37  out << "set view map\n";
38  out << "unset key\n";
39  out << "unset tics\n";
40  out << "unset border\n";
41  out << "set datafile missing \"nan\"\n";
42  out << "set cbtics border in scale 0,0 mirror norotate autojustify\n";
43  out << "set cbrange [" << data.saxis().min() << ":" << data.saxis().max() << "]\n";
44  out <<
45  "set palette defined (0 \"#8A2BE2\", 0.143 \"blue\", 0.286 \"cyan\", 0.429 \"light-green\", 0.572 \"#32CD32\", 0.715 \"#00FF00\", 0.858 \"yellow\", 1 \"orange\")\n";
46  out << "set autoscale fix\n";
47  out << "set size ratio 2\n";
48  out << "set yrange[:] reverse\n";
49  out << "set datafile separator \",\"\n";
50  out << "plot \"-\" matrix with image" << "\n";
51  const size_t swath_count = data.column_count() / data.tile_count();
52  for (size_t y = 0; y < data.tile_count(); ++y) // Rows
53  {
54  for (size_t x = 0; x < data.row_count(); ++x) // Column groups
55  {
56  for (size_t s = 0; s < swath_count; ++s) // Columns
57  {
58  if(x > 0 || s > 0) out << ",";
59  out << table::handle_nan(data(x, y + s * data.tile_count()));
60  }
61  out << ",nan";
62  }
63  out << "\n";
64  }
65  }
66 
75  void write_flowcell_tile_id(std::ostream &out,
76  const model::plot::flowcell_data &data,
77  const std::string &output_image_path = "")
78  {
79  write_output_format(out, output_image_path);
80  write_title(out, data);
81  out << "set view map\n";
82  out << "unset key\n";
83  out << "set cbrange [" << data.saxis().min() << ":" << data.saxis().max() << "]\n";
84  out << "plot \"-\" matrix with image" << "\n";
85  const size_t swath_count = data.column_count() / data.tile_count();
86  for (size_t y = 0; y < data.tile_count(); ++y)
87  {
88  for (size_t x = 0; x < data.row_count(); ++x)
89  {
90  for (size_t s = 0; s < swath_count; ++s)
91  {
92  out << " " << data.tile_id(x, y + s * data.tile_count());
93  }
94  }
95  out << "\n";
96  }
97  }
98 
105  void write_heatmap(std::ostream &out,
106  const model::plot::heatmap_data &data,
107  const std::string &output_image_path = "")
108  {
109  write_output_format(out, output_image_path);
110  write_title(out, data);
111  write_axes(out, data.xyaxes());
112  out << "set view map\n";
113  out << "set palette defined (0 \"white\", 0.333 \"green\", 0.667 \"yellow\", 1 \"red\")\n";
114  out << "set datafile separator \",\"\n";
115  out << "plot \"-\" matrix with image" << "\n";
116  for (size_t y = 0; y < data.column_count(); ++y)
117  {
118  out << data(0, y);
119  for (size_t x = 1; x < data.row_count(); ++x)
120  {
121  out << "," << table::handle_nan(data(x, y));
122  }
123  out << "\n";
124  }
125  }
126 
133  template<typename Point>
134  void write_chart(std::ostream &out,
135  const model::plot::plot_data<Point> &data,
136  const std::string &output_image_path = "")
137  {
138  write_output_format(out, output_image_path);
139  write_title(out, data);
140  write_axes(out, data.xyaxes());
141  write_plot_description(out, data);
142  write_plot_data(out, data);
143  }
144 
145  protected:
151  template<typename Point>
152  void write_plot_data(std::ostream &out, const model::plot::plot_data<Point> &data)
153  {
154  if (data.size() == 0) return;
155  for (size_t series = 0; series < data.size(); ++series)
156  {
157  write_plot_series(out, data[series]);
158  write_end_of_series(out); // Marker for the end of the series so GNUPlot knows when the next line starts
159  }
160  }
161 
167  template<typename Point>
168  void write_plot_description(std::ostream &out, const model::plot::plot_data<Point> &data)
169  {
170  if (data.size() == 0) return;
171 
172  out << "set datafile separator \",\"\n";
173  switch (data[0].series_type())
174  {
176  out << "set style fill solid border -1\n";
177  out << "unset key\n";
178  break;
180  out << "set boxwidth 0.3\n";
181  break;
182  default:
183  break;
184  };
185 
186  out << "plot ";
187  for (size_t series = 0; series < data.size(); ++series)
188  {
189  out << "\"-\" ";
190  write_type(out, data[series]);
191  write_label(out, data[series]);
192  write_color(out, data[series]);
193  write_additional(out, data[series]);
194  if ((series + 1) < data.size()) out << ",";
195  }
196  out << "\n";
197  }
198 
204  void write_axes(std::ostream &out, const model::plot::axes &axes)
205  {
206  write_axis(out, axes.y(), 'y');
207  write_axis(out, axes.x(), 'x');
208  }
209 
216  void write_axis(std::ostream &out, const model::plot::axis &axis, const char axis_label)
217  {
218  out << "set " << axis_label << "range ["
219  << table::handle_nan(axis.min()) << " : " << table::handle_nan(axis.max()) << " ]\n";
220  if (axis.label() != "") out << "set " << axis_label << "label \"" << axis.label() << "\"\n";
221  }
222 
228  template<typename PlotData>
229  void write_title(std::ostream &out, const PlotData &data)
230  {
231  if (data.title() != "") out << "set title \"" << data.title() << "\"\n";
232  }
233 
241  void write_output_format(std::ostream &out, const std::string &output_image_path)
242  {
243  if (output_image_path == "") return;
244  out << "set terminal png crop" << std::endl;
245  out << "set output \'" << output_image_path << "\'" << std::endl;
246  //out << "set style fill solid noborder" << std::endl;
247  }
248 
249  protected:
256  {
258  {
259  size_t max_num_outliers = 0;
260  for (size_t i = 0; i < series.size(); ++i)
261  {
262  const model::plot::candle_stick_point point = series[i];
263  const std::vector<float> outliers = point.outliers();
264  max_num_outliers = std::max(max_num_outliers, outliers.size());
265  }
266 
267  for (size_t i = 0; i < series.size(); ++i)
268  {
269  const model::plot::candle_stick_point point = series[i];
270  out << table::handle_nan(point.x()) << ",";
271  out << table::handle_nan(point.lower()) << ",";
272  out << table::handle_nan(point.p25()) << ",";
273  out << table::handle_nan(point.p50()) << ",";
274  out << table::handle_nan(point.p75()) << ",";
275  out << table::handle_nan(point.upper());
276  const std::vector<float> outliers = point.outliers();
277  size_t j=0;
278  for(;j<outliers.size();++j)
279  out << "," << table::handle_nan(outliers[j]);
280  for(;j<max_num_outliers;++j)
281  out << "," << table::handle_nan(std::numeric_limits<float>::quiet_NaN());
282  out << std::endl;
283  }
284  }
285  else write_plot_line(out, series);
286  }
287 
293  template<typename P>
294  void write_plot_line(std::ostream &out, const model::plot::series<P> &series)
295  {
296  for (size_t i = 0; i < series.size(); ++i)
297  {
298  out << table::handle_nan(series[i].x()) << "," << table::handle_nan(series[i].y()) << std::endl;
299  }
300  }
301 
307  template<typename X, typename Y>
308  void write_plot_series(std::ostream &out, const model::plot::series<model::plot::data_point<X, Y> > &series)
309  {
310  write_plot_line(out, series);
311  }
312 
318  void write_plot_series(std::ostream &out, const model::plot::series<model::plot::bar_point> &series)
319  {
320  for (size_t i = 0; i < series.size(); ++i)
321  {
322  out << table::handle_nan(series[i].x()) << ","
323  << table::handle_nan(series[i].y()) << ","
324  << table::handle_nan(series[i].width()) << std::endl;
325  }
326  }
327 
328  protected:
333  void write_end_of_series(std::ostream &out)
334  {
335  out << "e\n";
336  }
337 
346  template<typename P>
347  void write_type(std::ostream &out,
348  const model::plot::series<P> &series,
349  const char sep = ' ')
350  {
351  switch (series.series_type())
352  {
354  out << "using 1:2:3 with boxes" << sep;
355  break;
357  out << "using 1:3:2:6:5 with candlesticks" << sep;
358  break;
360  out << "with lines" << sep;
361  break;
362  }
363  }
364 
373  template<typename P>
374  void write_additional(std::ostream &out, const model::plot::series<P> &series, const char sep = ' ')
375  {
376  switch (series.series_type())
377  {
379  out << "whiskerbars" << sep;//, \'\' using 1:4:4:4:4 with candlesticks" << sep;
380  break;
381  default:
382  break;
383  }
384  }
385 
394  template<typename P>
395  void write_label(std::ostream &out, const model::plot::series<P> &series, const char sep = ' ')
396  {
397  if (series.title() == "") out << "notitle";
398  else out << "title \"" << series.title() << "\"";
399  out << sep;
400  }
401 
409  template<typename P>
410  void write_color(std::ostream &out, const model::plot::series<P> &series, const char sep = ' ')
411  {
412  if (series.color() == "") return;
413  out << "lt rgb \"" << normalize_color(series.color()) << "\"";
414  out << sep;
415  }
416 
422  std::string normalize_color(const std::string &color)
423  {
424  std::string color_normalized;
425  if(color.length() > 0 && color[0] == '#')
426  return color;
427  for (size_t i = 0; i < color.length(); ++i)
428  {
429  if (std::isupper(color[i]))
430  {
431  if (i > 0) color_normalized += '-';
432  color_normalized += static_cast<char>(::tolower(color[i]));
433  }
434  else color_normalized += color[i];
435  }
436  return color_normalized;
437  }
438  };
439 
440 
441 }}}}
442 
void write_output_format(std::ostream &out, const std::string &output_image_path)
Definition: gnuplot.h:241
const std::string & title() const
Definition: series.h:72
Definition: enum_description.h:15
size_t tile_count() const
Definition: flowcell_data.h:115
const axis & y() const
Definition: axes.h:221
float upper() const
Definition: candle_stick_point.h:119
Definition: flowcell_data.h:25
void write_plot_series(std::ostream &out, const model::plot::series< model::plot::candle_stick_point > &series)
Definition: gnuplot.h:255
const axis & x() const
Definition: axes.h:213
const std::string & label() const
Definition: axes.h:62
void write_title(std::ostream &out, const PlotData &data)
Definition: gnuplot.h:229
size_t column_count() const
Definition: heatmap_data.h:137
void write_plot_line(std::ostream &out, const model::plot::series< P > &series)
Definition: gnuplot.h:294
series_types series_type() const
Definition: series.h:88
void write_end_of_series(std::ostream &out)
Definition: gnuplot.h:333
const float_vector_t & outliers() const
Definition: candle_stick_point.h:128
float p25() const
Definition: candle_stick_point.h:83
Definition: candle_stick_point.h:19
size_t size() const
Definition: data_point_collection.h:81
void write_axes(std::ostream &out, const model::plot::axes &axes)
Definition: gnuplot.h:204
void write_color(std::ostream &out, const model::plot::series< P > &series, const char sep= ' ')
Definition: gnuplot.h:410
void write_flowcell_tile_id(std::ostream &out, const model::plot::flowcell_data &data, const std::string &output_image_path="")
Definition: gnuplot.h:75
void write_chart(std::ostream &out, const model::plot::plot_data< Point > &data, const std::string &output_image_path="")
Definition: gnuplot.h:134
const T & handle_nan(const T &val)
Definition: csv_format.h:84
X x() const
Definition: data_point.h:50
void write_type(std::ostream &out, const model::plot::series< P > &series, const char sep= ' ')
Definition: gnuplot.h:347
const plot::axis & saxis() const
Definition: flowcell_data.h:79
void write_plot_series(std::ostream &out, const model::plot::series< model::plot::bar_point > &series)
Definition: gnuplot.h:318
::uint32_t tile_id(const size_t lane_idx, const size_t loc) const INTEROP_THROW_SPEC((model
Definition: flowcell_data.h:66
void write_label(std::ostream &out, const model::plot::series< P > &series, const char sep= ' ')
Definition: gnuplot.h:395
const std::string & color() const
Definition: series.h:80
char tolower(const char ch)
Definition: string.h:25
float p50() const
Definition: candle_stick_point.h:92
const axes & xyaxes() const
Definition: chart_data.h:156
float p75() const
Definition: candle_stick_point.h:101
float min() const
Definition: axes.h:70
void write_flowcell(std::ostream &out, const model::plot::flowcell_data &data, const std::string &output_image_path="")
Definition: gnuplot.h:31
void write_plot_description(std::ostream &out, const model::plot::plot_data< Point > &data)
Definition: gnuplot.h:168
std::string normalize_color(const std::string &color)
Definition: gnuplot.h:422
void write_plot_series(std::ostream &out, const model::plot::series< model::plot::data_point< X, Y > > &series)
Definition: gnuplot.h:308
void write_plot_data(std::ostream &out, const model::plot::plot_data< Point > &data)
Definition: gnuplot.h:152
size_t size() const
Definition: plot_data.h:88
size_t row_count() const
Definition: heatmap_data.h:128
void write_axis(std::ostream &out, const model::plot::axis &axis, const char axis_label)
Definition: gnuplot.h:216
float lower() const
Definition: candle_stick_point.h:110
float max() const
Definition: axes.h:78
void write_additional(std::ostream &out, const model::plot::series< P > &series, const char sep= ' ')
Definition: gnuplot.h:374
void write_heatmap(std::ostream &out, const model::plot::heatmap_data &data, const std::string &output_image_path="")
Definition: gnuplot.h:105