A library for working with phylogenetic and population genetic data.
v0.27.0
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-2019 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 
36 #include "genesis/tree/tree.hpp"
38 
39 #include <cassert>
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
genesis::utils::Color
Definition: color.hpp:47
writer.hpp
genesis::tree::PhyloxmlColorWriterPlugin::prepare_writing
void prepare_writing(Tree const &tree, utils::XmlDocument &xml) const
Definition: phyloxml/color_writer_plugin.hpp:87
genesis::tree::PhyloxmlColorWriterPlugin::~PhyloxmlColorWriterPlugin
virtual ~PhyloxmlColorWriterPlugin()=default
genesis::tree::PhyloxmlWriter::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 Phyloxml represent...
Definition: tree/formats/phyloxml/writer.hpp:206
genesis::utils::XmlElement
Definition: xml/document.hpp:149
tree.hpp
Header of Tree class.
genesis::tree::TreeEdge::index
size_t index() const
Return the index of this Edge.
Definition: edge.hpp:106
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
genesis::tree::PhyloxmlColorWriterPlugin::operator=
PhyloxmlColorWriterPlugin & operator=(PhyloxmlColorWriterPlugin const &)=default
color_writer_plugin.hpp
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: functions/genome_locus.hpp:48
genesis::tree::PhyloxmlColorWriterPlugin::register_with
void register_with(PhyloxmlWriter &writer) const
Definition: phyloxml/color_writer_plugin.hpp:119
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::utils::XmlElement::content
std::vector< std::unique_ptr< XmlValue > > content
Definition: xml/document.hpp:209
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis::tree::PhyloxmlWriter
Write a Tree to Phyloxml format.
Definition: tree/formats/phyloxml/writer.hpp:101
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::PhyloxmlColorWriterPlugin::edge_to_element
void edge_to_element(TreeEdge const &edge, utils::XmlElement &element) const
Definition: phyloxml/color_writer_plugin.hpp:106
genesis::tree::PhyloxmlWriter::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/phyloxml/writer.hpp:189
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::PhyloxmlColorWriterPlugin::PhyloxmlColorWriterPlugin
PhyloxmlColorWriterPlugin()=default
genesis::tree::PhyloxmlColorWriterPlugin
Plugin class for PhyloXML output that allows coloring of edges.
Definition: phyloxml/color_writer_plugin.hpp:66
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
document.hpp
A collection of classes for working with XML documents. See XmlDocument for more.
genesis::utils::XmlDocument
Definition: xml/document.hpp:219
genesis::tree::Tree::edge_count
size_t edge_count() const
Return the number of TreeEdges of the Tree.
Definition: tree/tree.hpp:272