A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
phyloxml/color_writer_plugin.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_FORMATS_PHYLOXML_COLOR_WRITER_PLUGIN_H_
2 #define GENESIS_TREE_FORMATS_PHYLOXML_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 
36 #include "genesis/tree/tree.hpp"
38 
39 #include <assert.h>
40 #include <stdexcept>
41 
42 namespace genesis {
43 namespace tree {
44 
45 // =================================================================================================
46 // Phyloxml Color Writer Plugin
47 // =================================================================================================
48 
67 {
68 public:
69 
70  // -------------------------------------------------------------------------
71  // Constructor and Rule of Five
72  // -------------------------------------------------------------------------
73 
74  PhyloxmlColorWriterPlugin() = default;
75  virtual ~PhyloxmlColorWriterPlugin() = default;
76 
79 
82 
83  // -------------------------------------------------------------------------
84  // Plugin Functions
85  // -------------------------------------------------------------------------
86 
87  void prepare_writing( Tree const& tree, utils::XmlDocument& xml ) const
88  {
89  (void) xml;
90 
92  return;
93  }
94 
95  // If an edge color vector was set, it needs to match the tree's edge count.
96  if (
97  ColorWriterPlugin::edge_colors().size() > 0 &&
99  ) {
100  throw std::length_error(
101  "Color vector does not have as many elements as the tree has edges."
102  );
103  }
104  }
105 
106  void edge_to_element( TreeEdge const& edge, utils::XmlElement& element ) const
107  {
109  return;
110  }
111 
112  // If an edge color vector was set, use it.
113  if (ColorWriterPlugin::edge_colors().size() > 0) {
114  assert( edge.index() <= ColorWriterPlugin::edge_colors().size() );
115  set_color_(element, ColorWriterPlugin::edge_colors()[edge.index()]);
116  }
117  }
118 
119  void register_with( PhyloxmlWriter& writer ) const
120  {
121  writer.prepare_writing_plugins.push_back(
122  [&]( Tree const& tree, utils::XmlDocument& xml ) {
123  prepare_writing( tree, xml );
124  }
125  );
126  writer.edge_to_element_plugins.push_back(
127  [&]( TreeEdge const& edge, utils::XmlElement& element ) {
128  edge_to_element( edge, element );
129  }
130  );
131  }
132 
133  // -------------------------------------------------------------------------
134  // Member Functions
135  // -------------------------------------------------------------------------
136 
137 private:
138 
139  void set_color_( utils::XmlElement& element, unsigned char r, unsigned char g, unsigned char b ) const
140  {
141  if(
144  ) {
145  return;
146  }
147 
148  // TODO do not create new element if there is already one!
149  auto re = utils::make_unique< utils::XmlElement >("red");
150  re->append_markup(std::to_string(r));
151 
152  auto ge = utils::make_unique< utils::XmlElement >("green");
153  ge->append_markup(std::to_string(g));
154 
155  auto be = utils::make_unique< utils::XmlElement >("blue");
156  be->append_markup(std::to_string(b));
157 
158  auto color = utils::make_unique< utils::XmlElement >("color");
159  color->content.push_back(std::move(re));
160  color->content.push_back(std::move(ge));
161  color->content.push_back(std::move(be));
162 
163  element.content.push_back(std::move(color));
164  }
165 
166  void set_color_( utils::XmlElement& element, utils::Color color ) const
167  {
168  set_color_( element, color.r_byte(), color.g_byte(), color.b_byte() );
169  }
170 
171 };
172 
173 } // namespace tree
174 } // namespace genesis
175 
176 #endif // include guard
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.hpp:105
std::vector< utils::Color > const & edge_colors() const
Return the edge colors that are currently set.
void register_with(PhyloxmlWriter &writer) const
Plugin class for PhyloXML output that allows coloring of edges.
Write a Tree to Phyloxml format.
std::string to_string(T const &v)
Return a string representation of a given value.
Definition: string.hpp:381
utils::Color ignored_color() const
Return the currently set ignored color. 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.
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
Base class for creating plugin classes that allow coloring of Tree edges.
void edge_to_element(TreeEdge const &edge, utils::XmlElement &element) const
A collection of classes for working with XML documents. See XmlDocument for more. ...
PhyloxmlColorWriterPlugin & operator=(PhyloxmlColorWriterPlugin const &)=default
size_t edge_count() const
Return the number of TreeEdges of the Tree.
Definition: tree/tree.cpp:358
std::vector< std::unique_ptr< XmlValue > > content
void prepare_writing(Tree const &tree, utils::XmlDocument &xml) const
Header of Tree class.
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 Phyloxml represent...