A library for working with phylogenetic and population genetic data.
v0.27.0
newick/color_writer_plugin.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_FORMATS_NEWICK_COLOR_WRITER_PLUGIN_H_
2 #define GENESIS_TREE_FORMATS_NEWICK_COLOR_WRITER_PLUGIN_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include "genesis/tree/tree.hpp"
38 
39 #include <cassert>
40 #include <stdexcept>
41 #include <string>
42 
43 namespace genesis {
44 namespace tree {
45 
46 // =================================================================================================
47 // Newick Color Writer Plugin
48 // =================================================================================================
49 
59 {
60 public:
61 
62  // -------------------------------------------------------------------------
63  // Constructor and Rule of Five
64  // -------------------------------------------------------------------------
65 
66  NewickColorWriterPlugin() = default;
67  virtual ~NewickColorWriterPlugin() = default;
68 
71 
74 
75  // -------------------------------------------------------------------------
76  // Properties
77  // -------------------------------------------------------------------------
78 
86  void color_tag_prefix( std::string const& prefix )
87  {
88  color_tag_prefix_ = prefix;
89  }
90 
94  std::string color_tag_prefix() const
95  {
96  return color_tag_prefix_;
97  }
98 
105  void color_tag_suffix( std::string const& suffix )
106  {
107  color_tag_suffix_ = suffix;
108  }
109 
113  std::string color_tag_suffix() const
114  {
115  return color_tag_suffix_;
116  }
117 
118  // -------------------------------------------------------------------------
119  // Plugin Functions
120  // -------------------------------------------------------------------------
121 
122  void prepare_writing( Tree const& tree, NewickBroker& broker ) const
123  {
124  (void) broker;
125 
127  return;
128  }
129 
130  // If an edge color vector was set, it needs to match the tree's edge count.
131  if (
132  ColorWriterPlugin::edge_colors().size() > 0 &&
133  ColorWriterPlugin::edge_colors().size() != tree.edge_count()
134  ) {
135  throw std::length_error(
136  "Color vector does not have as many elements as the tree has edges."
137  );
138  }
139  }
140 
141  void edge_to_element( TreeEdge const& edge, NewickBrokerElement& element ) const
142  {
144  return;
145  }
146 
147  // If an edge color vector was set, use it.
148  if (ColorWriterPlugin::edge_colors().size() > 0) {
149  assert( edge.index() <= ColorWriterPlugin::edge_colors().size() );
150  set_color_(element, ColorWriterPlugin::edge_colors()[edge.index()]);
151  }
152  }
153 
154  void register_with( NewickWriter& writer ) const
155  {
156  writer.prepare_writing_plugins.push_back(
157  [&]( Tree const& tree, NewickBroker& broker ) {
158  prepare_writing( tree, broker );
159  }
160  );
161  writer.edge_to_element_plugins.push_back(
162  [&]( TreeEdge const& edge, NewickBrokerElement& element ) {
163  edge_to_element( edge, element );
164  }
165  );
166  }
167 
168  // -------------------------------------------------------------------------
169  // Member Functions
170  // -------------------------------------------------------------------------
171 
172 private:
173 
174  void set_color_( NewickBrokerElement& element, unsigned char r, unsigned char g, unsigned char b ) const
175  {
176  set_color_( element, utils::Color(r, g, b) );
177  }
178 
179  void set_color_( NewickBrokerElement& element, utils::Color color ) const
180  {
181  if(
184  ) {
185  return;
186  }
187 
188  // TODO do not create new element if there is already one!
189  // TODO add functor for converting color to string. maybe not all want hex format...
190  element.comments.push_back(
191  color_tag_prefix_ + utils::color_to_hex(color) + color_tag_suffix_
192  );
193  }
194 
195  // -------------------------------------------------------------------------
196  // Data Members
197  // -------------------------------------------------------------------------
198 
199 private:
200 
201  std::string color_tag_prefix_ = "&!color=";
202  std::string color_tag_suffix_ = "";
203 
204 };
205 
206 } // namespace tree
207 } // namespace genesis
208 
209 #endif // include guard
genesis::utils::Color
Definition: color.hpp:47
genesis::tree::NewickColorWriterPlugin::NewickColorWriterPlugin
NewickColorWriterPlugin()=default
genesis::tree::NewickWriter
Write a Tree to Newick format.
Definition: tree/formats/newick/writer.hpp:102
genesis::tree::NewickWriter::prepare_writing_plugins
std::vector< prepare_writing_function > prepare_writing_plugins
Collect all functions to be called before starting the actual tree writing.
Definition: tree/formats/newick/writer.hpp:201
tree.hpp
Header of Tree class.
element.hpp
genesis::tree::TreeEdge::index
size_t index() const
Return the index of this Edge.
Definition: edge.hpp:106
genesis::tree::NewickColorWriterPlugin::color_tag_prefix
void color_tag_prefix(std::string const &prefix)
Set the prefix string that is used within the Newick comment before the actual color value.
Definition: newick/color_writer_plugin.hpp:86
genesis::tree::ColorWriterPlugin::ignored_color
utils::Color ignored_color() const
Return the currently set ignored color. See the setter for more information.
Definition: color_writer_plugin.hpp:155
color_writer_plugin.hpp
genesis::tree::NewickColorWriterPlugin::edge_to_element
void edge_to_element(TreeEdge const &edge, NewickBrokerElement &element) const
Definition: newick/color_writer_plugin.hpp:141
genesis::tree::Tree
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
genesis::tree::ColorWriterPlugin::enable_color
bool enable_color() const
Returns whether colors tags are written to the output.
Definition: color_writer_plugin.hpp:131
genesis::tree::NewickColorWriterPlugin::color_tag_suffix
void color_tag_suffix(std::string const &suffix)
Set the suffix string that is used within the Newick comment after the actual color value.
Definition: newick/color_writer_plugin.hpp:105
genesis::tree::NewickWriter::edge_to_element_plugins
std::vector< edge_to_element_function > edge_to_element_plugins
Collect all functions to be called for each TreeEdge in order to translate it to a Newick representat...
Definition: tree/formats/newick/writer.hpp:218
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis::tree::NewickColorWriterPlugin::operator=
NewickColorWriterPlugin & operator=(NewickColorWriterPlugin const &)=default
genesis::tree::NewickColorWriterPlugin
Plugin class for Newick output that allows coloring of edges.
Definition: newick/color_writer_plugin.hpp:58
writer.hpp
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::tree::NewickBroker
Stores a Newick tree in an intermediate format that can be further processed into a Tree.
Definition: broker.hpp:106
genesis::tree::ColorWriterPlugin::use_ignored_color
bool use_ignored_color() const
Return whether currently an ignored color is used.
Definition: color_writer_plugin.hpp:174
genesis::tree::NewickColorWriterPlugin::~NewickColorWriterPlugin
virtual ~NewickColorWriterPlugin()=default
genesis::tree::NewickColorWriterPlugin::color_tag_prefix
std::string color_tag_prefix() const
Get the currently set prefix string. See the setter for more information.
Definition: newick/color_writer_plugin.hpp:94
genesis::tree::ColorWriterPlugin
Base class for creating plugin classes that allow coloring of Tree edges.
Definition: color_writer_plugin.hpp:81
genesis::tree::ColorWriterPlugin::edge_colors
std::vector< utils::Color > const & edge_colors() const
Return the edge colors that are currently set.
Definition: color_writer_plugin.hpp:115
genesis::tree::NewickColorWriterPlugin::color_tag_suffix
std::string color_tag_suffix() const
Get the currently set suffix string. See the setter for more information.
Definition: newick/color_writer_plugin.hpp:113
genesis::tree::NewickColorWriterPlugin::prepare_writing
void prepare_writing(Tree const &tree, NewickBroker &broker) const
Definition: newick/color_writer_plugin.hpp:122
genesis::tree::NewickBrokerElement
Store the information for one element of a Newick tree.
Definition: element.hpp:60
genesis::tree::NewickColorWriterPlugin::register_with
void register_with(NewickWriter &writer) const
Definition: newick/color_writer_plugin.hpp:154
genesis::utils::color_to_hex
std::string color_to_hex(Color const &c, std::string const &prefix, bool uppercase, bool with_alpha)
Return a hex string representation of a Color in the format "#003366[ff]".
Definition: utils/tools/color/functions.cpp:100
genesis::tree::Tree::edge_count
size_t edge_count() const
Return the number of TreeEdges of the Tree.
Definition: tree/tree.hpp:272