A library for working with phylogenetic data.
v0.25.0
genome_heatmap.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2020 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
34 
35 #include <cassert>
36 
37 namespace genesis {
38 namespace population {
39 
40 // =================================================================================================
41 // Genome Heatmap
42 // =================================================================================================
43 
44 void GenomeHeatmap::add( std::string const& label, utils::Matrix<utils::Color> const& heatmap )
45 {
46  using namespace utils;
47 
48  // Add the label, unless it's supposed to be at the bottom, in which case we add it later.
49  switch( text_position_ ) {
50  case TextPosition::kTop: {
51  document_ << SvgText( label, SvgPoint{ 0.0, current_y_ });
52  current_y_ += text_template_.font.size + 5.0;
53  break;
54  }
55  case TextPosition::kLeft: {
56  document_ << SvgText( label, SvgPoint{ -100.0, current_y_ });
57  break;
58  }
59  case TextPosition::kRight: {
60  auto const r = static_cast<double>( heatmap.cols() ) * h_scaling_;
61  document_ << SvgText( label, SvgPoint{ r + 10.0, current_y_ });
62  break;
63  }
64  case TextPosition::kBottom: {
65  break;
66  }
67  default: {
68  assert( false );
69  }
70  }
71 
72  // Add the heatmap as a bitmap, embedded by encoding it in base64, and using pixelated rendering
73  // (which is not really well defined in svg, but we try hard).
74  std::string matrix_bmp;
75  BmpWriter().write( heatmap, to_string( matrix_bmp ));
76  auto img = SvgImage(
77  svg_data_uri( "image/bmp", matrix_bmp, true ),
78  SvgPoint{ 0.0, current_y_ },
79  SvgSize{
80  static_cast<double>( heatmap.cols() ) * h_scaling_,
81  static_cast<double>( heatmap.rows() ) * v_scaling_
82  }
83  );
84  img.rendering = SvgImage::ImageRendering::kPixelated;
85  document_ << std::move( img );
86 
87  // Add the label if its supposed to be at the bottom, or simply add vertical space as needed.
88  current_y_ += static_cast<double>( heatmap.rows() ) * v_scaling_;
89  switch( text_position_ ) {
90  case TextPosition::kBottom: {
91  auto txt = text_template_;
92  txt.text = label;
93  current_y_ += text_template_.font.size + 5.0;
94  txt.position = SvgPoint{ 0.0, current_y_ };
95  document_ << txt;
96  current_y_ += 20.0;
97  break;
98  }
99  default: {
100  current_y_ += 20.0;
101  break;
102  }
103  }
104 }
105 
106 void GenomeHeatmap::write( std::shared_ptr<utils::BaseOutputTarget> target ) const
107 {
108  document_.write( target->ostream() );
109 }
110 
111 } // namespace population
112 } // namespace genesis
genesis::utils::svg_data_uri
std::string svg_data_uri(std::string const &media_type, std::string const &content, bool encode_base64=false)
Definition: utils/formats/svg/helper.hpp:262
genesis::utils::Matrix::cols
size_t cols() const
Definition: containers/matrix.hpp:167
genesis::population::GenomeHeatmap::TextPosition::kBottom
@ kBottom
genesis::utils::SvgDocument::write
void write(std::ostream &out) const
Write the SvgDocument to an output stream.
Definition: svg/document.cpp:70
genome_heatmap.hpp
genesis::utils::Matrix
Definition: placement/function/emd.hpp:53
genesis::population::GenomeHeatmap::write
void write(std::shared_ptr< utils::BaseOutputTarget > target) const
Definition: genome_heatmap.cpp:106
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::population::to_string
std::string to_string(GenomeRegion const &region)
Definition: functions/genome_region.cpp:55
writer.hpp
genesis::utils::Matrix::rows
size_t rows() const
Definition: containers/matrix.hpp:162
genesis::population::GenomeHeatmap::add
void add(std::string const &label, utils::Matrix< utils::Color > const &heatmap)
Definition: genome_heatmap.cpp:44
genesis::population::GenomeHeatmap::TextPosition::kRight
@ kRight
genesis::population::GenomeHeatmap::TextPosition::kTop
@ kTop
genesis::population::GenomeHeatmap::TextPosition::kLeft
@ kLeft