A library for working with phylogenetic and population genetic data.
v0.32.0
simple_writer.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_FORMATS_NEWICK_SIMPLE_WRITER_H_
2 #define GENESIS_TREE_FORMATS_NEWICK_SIMPLE_WRITER_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2022 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 <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
25 */
26 
37 
38 namespace genesis {
39 namespace tree {
40 
41 // =================================================================================================
42 // Simple Newick Tree Newick Writer Plugin
43 // =================================================================================================
44 
49 {
50 public:
51 
52  // -------------------------------------------------------------------------
53  // Constructor and Rule of Five
54  // -------------------------------------------------------------------------
55 
57  virtual ~SimpleNewickTreeNewickWriterPlugin() = default;
58 
61 
64  ) = default;
67  ) = default;
68 
69  // -------------------------------------------------------------------------
70  // Plugin Functions
71  // -------------------------------------------------------------------------
72 
73  void node_to_element( TreeNode const& node, NewickBrokerElement& element ) const
74  {
75  // Add all comments stored in the node to the element.
76  // We push them back, in case that some other plugin also wants to add data.
77  auto& node_data = node.data<SimpleNewickNodeData>();
78  for( auto const& comment : node_data.comments ) {
79  element.comments.push_back( comment );
80  }
81  }
82 
83  void edge_to_element( TreeEdge const& edge, NewickBrokerElement& element ) const
84  {
85  // Add all additiona values and tags to the element.
86  // We push them back, in case that some other plugin also wants to add data.
87  auto& edge_data = edge.data<SimpleNewickEdgeData>();
88  for( auto const& value : edge_data.values ) {
89  element.values.push_back( value );
90  }
91  for( auto const& tag : edge_data.tags ) {
92  element.tags.push_back( tag );
93  }
94  }
95 
96  void register_with( NewickWriter& writer ) const
97  {
98  writer.node_to_element_plugins.push_back(
99  [&]( TreeNode const& node, NewickBrokerElement& element ) {
100  node_to_element( node, element );
101  }
102  );
103 
104  writer.edge_to_element_plugins.push_back(
105  [&]( TreeEdge const& edge, NewickBrokerElement& element ) {
106  edge_to_element( edge, element );
107  }
108  );
109  }
110 };
111 
112 // =================================================================================================
113 // SimpleNewick Tree Newick Writer
114 // =================================================================================================
115 
117  : public NewickWriter
120 {
121 public:
122 
123  // -------------------------------------------------------------------------
124  // Constructor and Rule of Five
125  // -------------------------------------------------------------------------
126 
128  {
131  }
132 };
133 
134 } // namespace tree
135 } // namespace genesis
136 
137 #endif // include guard
genesis::tree::SimpleNewickTreeNewickWriterPlugin::~SimpleNewickTreeNewickWriterPlugin
virtual ~SimpleNewickTreeNewickWriterPlugin()=default
genesis::tree::SimpleNewickTreeNewickWriterPlugin::register_with
void register_with(NewickWriter &writer) const
Definition: simple_writer.hpp:96
genesis::tree::SimpleNewickTreeNewickWriter
Definition: simple_writer.hpp:116
genesis::tree::NewickBrokerElement::comments
std::vector< std::string > comments
Arbitrary strings that can be attached to a node, e.g. in Newick format via "[]".
Definition: element.hpp:132
simple_tree.hpp
genesis::tree::NewickWriter
Write a Tree to Newick format.
Definition: tree/formats/newick/writer.hpp:102
genesis::tree::SimpleNewickTreeNewickWriterPlugin::node_to_element
void node_to_element(TreeNode const &node, NewickBrokerElement &element) const
Definition: simple_writer.hpp:73
genesis::tree::NewickBrokerElement::values
std::vector< std::string > values
Numerical values associated with the node, i.e. branch lengths.
Definition: element.hpp:122
genesis::tree::SimpleNewickNodeData
Data class for SimpleNewickTreeNodes.
Definition: simple_tree.hpp:99
newick_writer.hpp
genesis::tree::NewickWriter::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 Newick representat...
Definition: tree/formats/newick/writer.hpp:222
genesis::tree::NewickBrokerElement::tags
std::vector< std::string > tags
Arbitrary strings that can be attached to a node, e.g. in Newick format via "{}".
Definition: element.hpp:127
genesis::tree::SimpleNewickTreeNewickWriterPlugin
Definition: simple_writer.hpp:48
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis::tree::TreeNode
Definition: tree/tree/node.hpp:58
writer.hpp
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::SimpleNewickTreeNewickWriterPlugin::SimpleNewickTreeNewickWriterPlugin
SimpleNewickTreeNewickWriterPlugin()=default
genesis::tree::CommonTreeNewickWriterPlugin
Provide a set of plugin functions for NewickWriter to write a CommonTree.
Definition: tree/common_tree/newick_writer.hpp:51
genesis::tree::SimpleNewickTreeNewickWriter::SimpleNewickTreeNewickWriter
SimpleNewickTreeNewickWriter()
Definition: simple_writer.hpp:127
genesis::tree::CommonTreeNewickWriterPlugin::register_with
void register_with(NewickWriter &writer) const
Definition: tree/common_tree/newick_writer.hpp:312
genesis::tree::SimpleNewickTreeNewickWriterPlugin::edge_to_element
void edge_to_element(TreeEdge const &edge, NewickBrokerElement &element) const
Definition: simple_writer.hpp:83
genesis::tree::TreeNode::data
NodeDataType & data()
Definition: tree/tree/node.hpp:203
genesis::tree::TreeEdge::data
EdgeDataType & data()
Definition: edge.hpp:217
genesis::tree::NewickBrokerElement
Store the information for one element of a Newick tree.
Definition: element.hpp:60
genesis::tree::SimpleNewickEdgeData
Data class for SimpleNewickTreeEdges.
Definition: simple_tree.hpp:158
genesis::tree::SimpleNewickTreeNewickWriterPlugin::operator=
SimpleNewickTreeNewickWriterPlugin & operator=(SimpleNewickTreeNewickWriterPlugin const &)=default
genesis::tree::NewickWriter::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 Newick representat...
Definition: tree/formats/newick/writer.hpp:216