A library for working with phylogenetic and population genetic data.
v0.32.0
phyloxml_writer.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_COMMON_TREE_PHYLOXML_WRITER_H_
2 #define GENESIS_TREE_COMMON_TREE_PHYLOXML_WRITER_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 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 
37 
38 namespace genesis {
39 namespace tree {
40 
41 // =================================================================================================
42 // Common Tree Phyloxml Writer Plugin
43 // =================================================================================================
44 
49 {
50 public:
51 
52  // -------------------------------------------------------------------------
53  // Constructor and Rule of Five
54  // -------------------------------------------------------------------------
55 
57  virtual ~CommonTreePhyloxmlWriterPlugin() = 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<CommonNodeData>().name);
72  }
73 
74  void edge_to_element( TreeEdge const& edge, utils::XmlElement& element ) const
75  {
76  set_branch_length_(element, edge.data<CommonEdgeData>().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 // Common 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
genesis::tree::CommonTreePhyloxmlWriter
Definition: phyloxml_writer.hpp:121
writer.hpp
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::tree::PhyloxmlWriter::node_to_element_plugins
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...
Definition: tree/formats/phyloxml/writer.hpp:200
genesis::utils::XmlElement
Definition: xml/document.hpp:149
genesis::tree::length
double length(Tree const &tree)
Get the length of the tree, i.e., the sum of all branch lengths.
Definition: tree/common_tree/functions.cpp:160
genesis::tree::CommonTreePhyloxmlWriterPlugin
Definition: phyloxml_writer.hpp:48
genesis::tree::CommonTreePhyloxmlWriterPlugin::CommonTreePhyloxmlWriterPlugin
CommonTreePhyloxmlWriterPlugin()=default
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: function/genome_locus.hpp:52
genesis::tree::CommonTreePhyloxmlWriterPlugin::operator=
CommonTreePhyloxmlWriterPlugin & operator=(CommonTreePhyloxmlWriterPlugin const &)=default
genesis::tree::CommonTreePhyloxmlWriterPlugin::~CommonTreePhyloxmlWriterPlugin
virtual ~CommonTreePhyloxmlWriterPlugin()=default
genesis::utils::XmlElement::content
std::vector< std::unique_ptr< XmlValue > > content
Definition: xml/document.hpp:209
genesis::tree::CommonEdgeData::branch_length
double branch_length
Branch length of the edge.
Definition: tree/common_tree/tree.hpp:193
genesis::tree::CommonTreePhyloxmlWriterPlugin::register_with
void register_with(PhyloxmlWriter &writer) const
Definition: phyloxml_writer.hpp:79
genesis::tree::CommonNodeData::name
std::string name
Name of the node.
Definition: tree/common_tree/tree.hpp:127
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis::tree::TreeNode
Definition: tree/tree/node.hpp:58
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
tree.hpp
genesis::tree::CommonEdgeData
Common class containing the commonly needed data for tree edges.
Definition: tree/common_tree/tree.hpp:144
genesis::tree::CommonNodeData
Common class containing the commonly needed data for tree nodes.
Definition: tree/common_tree/tree.hpp:79
genesis::tree::TreeNode::data
NodeDataType & data()
Definition: tree/tree/node.hpp:203
genesis::tree::TreeEdge::data
EdgeDataType & data()
Definition: edge.hpp:217
document.hpp
A collection of classes for working with XML documents. See XmlDocument for more.
genesis::tree::CommonTreePhyloxmlWriter::CommonTreePhyloxmlWriter
CommonTreePhyloxmlWriter()
Definition: phyloxml_writer.hpp:131
genesis::tree::CommonTreePhyloxmlWriterPlugin::edge_to_element
void edge_to_element(TreeEdge const &edge, utils::XmlElement &element) const
Definition: phyloxml_writer.hpp:74
genesis::tree::CommonTreePhyloxmlWriterPlugin::node_to_element
void node_to_element(TreeNode const &node, utils::XmlElement &element) const
Definition: phyloxml_writer.hpp:69