A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2017 Lucas Czech
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 <assert.h>
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 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 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( color_tag_prefix_ + color_to_hex(color) + color_tag_suffix_ );
191  }
192 
193  // -------------------------------------------------------------------------
194  // Data Members
195  // -------------------------------------------------------------------------
196 
197 private:
198 
199  std::string color_tag_prefix_ = "&!color=";
200  std::string color_tag_suffix_ = "";
201 
202 };
203 
204 } // namespace tree
205 } // namespace genesis
206 
207 #endif // include guard
NewickColorWriterPlugin & operator=(NewickColorWriterPlugin const &)=default
bool use_ignored_color() const
Return whether currently an ignored color is used.
bool enable_color() const
Returns whether colors tags are written to the output.
size_t index() const
Return the index of this Edge.
Definition: edge.cpp:45
std::vector< utils::Color > const & edge_colors() const
Return the edge colors that are currently set.
void register_with(NewickWriter &writer) const
Write a Tree to Newick format.
utils::Color ignored_color() const
Return the currently set ignored color. See the setter for more information.
void prepare_writing(Tree const &tree, NewickBroker &broker) const
void color_tag_prefix(std::string prefix)
Set the prefix string that is used within the Newick comment before the actual color value...
Stores a Newick tree in an intermediate format that can be further processed into a Tree...
Definition: broker.hpp:106
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
Plugin class for Newick output that allows coloring of edges.
Base class for creating plugin classes that allow coloring of Tree edges.
std::string color_tag_suffix() const
Get the currently set suffix string. See the setter for more information.
std::vector< prepare_writing_function > prepare_writing_plugins
Collect all functions to be called before starting the actual tree writing.
void color_tag_suffix(std::string suffix)
Set the suffix string that is used within the Newick comment after the actual color value...
void edge_to_element(TreeEdge const &edge, NewickBrokerElement &element) const
size_t edge_count() const
Return the number of TreeEdges of the Tree.
Definition: tree/tree.cpp:358
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...
std::string color_tag_prefix() const
Get the currently set prefix string. See the setter for more information.
Header of Tree class.
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]".
Store the information for one element of a Newick tree.
Definition: element.hpp:60