A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
phyloxml_writer.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_DEFAULT_PHYLOXML_WRITER_H_
2 #define GENESIS_TREE_DEFAULT_PHYLOXML_WRITER_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 
37 
38 namespace genesis {
39 namespace tree {
40 
41 // =================================================================================================
42 // Default Tree Phyloxml Writer Plugin
43 // =================================================================================================
44 
49 {
50 public:
51 
52  // -------------------------------------------------------------------------
53  // Constructor and Rule of Five
54  // -------------------------------------------------------------------------
55 
57  virtual ~DefaultTreePhyloxmlWriterPlugin() = default;
58 
61 
64 
65  // -------------------------------------------------------------------------
66  // Plugin Functions
67  // -------------------------------------------------------------------------
68 
69  void node_to_element( TreeNode const& node, utils::XmlElement& element ) const
70  {
71  set_name_(element, node.data<DefaultNodeData>().name);
72  }
73 
74  void edge_to_element( TreeEdge const& edge, utils::XmlElement& element ) const
75  {
76  set_branch_length_(element, edge.data<DefaultEdgeData>().branch_length);
77  }
78 
79  void register_with( PhyloxmlWriter& writer ) const
80  {
81  writer.node_to_element_plugins.push_back(
82  [&]( TreeNode const& node, utils::XmlElement& element ) {
83  node_to_element( node, element );
84  }
85  );
86  writer.edge_to_element_plugins.push_back(
87  [&]( TreeEdge const& edge, utils::XmlElement& element ) {
88  edge_to_element( edge, element );
89  }
90  );
91  }
92 
93  // -------------------------------------------------------------------------
94  // Member Functions
95  // -------------------------------------------------------------------------
96 
97 private:
98 
99  void set_name_( utils::XmlElement& element, const std::string& name ) const
100  {
101  // TODO do not create new element if there is already one!
102  auto name_e = utils::make_unique< utils::XmlElement >( "name" );
103  name_e->append_markup(name);
104  element.content.push_back(std::move(name_e));
105  }
106 
107  void set_branch_length_( utils::XmlElement& element, double length ) const
108  {
109  // TODO do not create new element if there is already one!
110  auto bl_e = utils::make_unique< utils::XmlElement >( "branch_length" );
111  bl_e->append_markup(std::to_string(length));
112  element.content.push_back(std::move(bl_e));
113  }
114 
115 };
116 
117 // =================================================================================================
118 // Default Tree Phyloxml Writer
119 // =================================================================================================
120 
122  : public PhyloxmlWriter
124 {
125 public:
126 
127  // -------------------------------------------------------------------------
128  // Constructor and Rule of Five
129  // -------------------------------------------------------------------------
130 
132  {
133  register_with( *this );
134  }
135 };
136 
137 } // namespace tree
138 } // namespace genesis
139 
140 #endif // include guard
void node_to_element(TreeNode const &node, utils::XmlElement &element) const
std::vector< node_to_element_function > node_to_element_plugins
Collect all functions to be called for each TreeNode in order to translate it to a Phyloxml represent...
Write a Tree to Phyloxml format.
Default class containing the commonly needed data for tree nodes.
void edge_to_element(TreeEdge const &edge, utils::XmlElement &element) const
std::string to_string(T const &v)
Return a string representation of a given value.
Definition: string.hpp:373
Default class containing the commonly needed data for tree edges.
A collection of classes for working with XML documents. See XmlDocument for more. ...
void register_with(PhyloxmlWriter &writer) const
std::vector< std::unique_ptr< XmlValue > > content
double branch_length
Branch length of the edge.
std::string name
Name of the node.
NodeDataType & data()
Definition: node.hpp:108
DefaultTreePhyloxmlWriterPlugin & operator=(DefaultTreePhyloxmlWriterPlugin const &)=default
double length(Tree const &tree)
Get the length of the tree, i.e., the sum of all branch lengths.
EdgeDataType & data()
Definition: edge.hpp:118
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...