A library for working with phylogenetic and population genetic data.
v0.32.0
tree/formats/newick/simple_reader.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_FORMATS_NEWICK_SIMPLE_READER_H_
2 #define GENESIS_TREE_FORMATS_NEWICK_SIMPLE_READER_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 Reader Plugin
43 // =================================================================================================
44 
49 {
50 public:
51 
52  // -------------------------------------------------------------------------
53  // Constructor and Rule of Five
54  // -------------------------------------------------------------------------
55 
57  virtual ~SimpleNewickTreeNewickReaderPlugin() = default;
58 
61 
64  ) = default;
67  ) = default;
68 
69  // -------------------------------------------------------------------------
70  // Plugin Functions
71  // -------------------------------------------------------------------------
72 
73  void element_to_node( NewickBrokerElement const& element, TreeNode& node ) const
74  {
75  // Get the node comments and store them in our tree
76  auto& node_data = node.data<SimpleNewickNodeData>();
77  node_data.comments = element.comments;
78  }
79 
80  void element_to_edge( NewickBrokerElement const& element, TreeEdge& edge ) const
81  {
82  // Get the edge values (including branch lenght, but then remove it), and tags.
83  auto& edge_data = edge.data<SimpleNewickEdgeData>();
84  edge_data.values = element.values;
85  if( edge_data.values.size() > 0 ) {
86  edge_data.values.erase( edge_data.values.begin() );
87  }
88  edge_data.tags = element.tags;
89  }
90 
91  void register_with( NewickReader& reader ) const
92  {
93  // Set node data creation function.
94  reader.create_node_data_plugin = []( TreeNode& node ){
95  node.reset_data( SimpleNewickNodeData::create() );
96  };
97 
98  // Set edge data creation function.
99  reader.create_edge_data_plugin = []( TreeEdge& edge ){
100  edge.reset_data( SimpleNewickEdgeData::create() );
101  };
102 
103  // Add node manipulation functions.
104  reader.element_to_node_plugins.push_back(
105  [&]( NewickBrokerElement const& element, TreeNode& node ) {
106  element_to_node( element, node );
107  }
108  );
109 
110  // Add edge manipulation functions.
111  reader.element_to_edge_plugins.push_back(
112  [&]( NewickBrokerElement const& element, TreeEdge& edge ) {
113  element_to_edge( element, edge );
114  }
115  );
116  }
117 };
118 
119 // =================================================================================================
120 // Simple Newick Tree Newick Reader
121 // =================================================================================================
122 
124  : public NewickReader
127 {
128 public:
129 
130  // -------------------------------------------------------------------------
131  // Constructor and Rule of Five
132  // -------------------------------------------------------------------------
133 
135  {
136  // We explicitly use tags here. Activate them!
137  enable_tags( true );
138 
139  // We first register the default reader, then the tree reader, because the latter
140  // overwrites the data creation functions.
143  }
144 };
145 
146 } // namespace tree
147 } // namespace genesis
148 
149 #endif // include guard
genesis::tree::NewickReader::create_node_data_plugin
create_node_data_function create_node_data_plugin
Definition: tree/formats/newick/reader.hpp:313
newick_reader.hpp
genesis::tree::NewickReader
Definition: tree/formats/newick/reader.hpp:67
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::SimpleNewickTreeNewickReaderPlugin::operator=
SimpleNewickTreeNewickReaderPlugin & operator=(SimpleNewickTreeNewickReaderPlugin const &)=default
genesis::tree::CommonTreeNewickReaderPlugin
Provide a set of plugin functions for NewickReader to read a CommonTree.
Definition: tree/common_tree/newick_reader.hpp:50
genesis::tree::NewickReader::create_edge_data_plugin
create_edge_data_function create_edge_data_plugin
Definition: tree/formats/newick/reader.hpp:314
genesis::tree::SimpleNewickTreeNewickReaderPlugin
Definition: tree/formats/newick/simple_reader.hpp:48
genesis::tree::SimpleNewickTreeNewickReaderPlugin::element_to_edge
void element_to_edge(NewickBrokerElement const &element, TreeEdge &edge) const
Definition: tree/formats/newick/simple_reader.hpp:80
genesis::tree::CommonTreeNewickReaderPlugin::register_with
void register_with(NewickReader &reader) const
Definition: tree/common_tree/newick_reader.hpp:254
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::NewickReader::element_to_edge_plugins
std::vector< element_to_edge_function > element_to_edge_plugins
Definition: tree/formats/newick/reader.hpp:317
genesis::tree::SimpleNewickNodeData
Data class for SimpleNewickTreeNodes.
Definition: simple_tree.hpp:99
genesis::tree::SimpleNewickEdgeData::values
std::vector< std::string > values
Numerical values associated with the node, excluding branch lengths.
Definition: simple_tree.hpp:207
genesis::tree::SimpleNewickNodeData::create
static std::unique_ptr< SimpleNewickNodeData > create()
Definition: simple_tree.hpp:123
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::SimpleNewickEdgeData::create
static std::unique_ptr< SimpleNewickEdgeData > create()
Definition: simple_tree.hpp:182
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis::tree::TreeNode
Definition: tree/tree/node.hpp:58
genesis::tree::SimpleNewickTreeNewickReaderPlugin::SimpleNewickTreeNewickReaderPlugin
SimpleNewickTreeNewickReaderPlugin()=default
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::NewickReader::enable_tags
bool enable_tags() const
Return whether currently Newick tags are enabled.
Definition: tree/formats/newick/reader.cpp:839
genesis::tree::SimpleNewickNodeData::comments
std::vector< std::string > comments
List of comments such as NHX elements.
Definition: simple_tree.hpp:145
genesis::tree::SimpleNewickTreeNewickReader
Definition: tree/formats/newick/simple_reader.hpp:123
reader.hpp
genesis::tree::NewickReader::element_to_node_plugins
std::vector< element_to_node_function > element_to_node_plugins
Definition: tree/formats/newick/reader.hpp:316
genesis::tree::SimpleNewickTreeNewickReaderPlugin::register_with
void register_with(NewickReader &reader) const
Definition: tree/formats/newick/simple_reader.hpp:91
genesis::tree::SimpleNewickTreeNewickReaderPlugin::element_to_node
void element_to_node(NewickBrokerElement const &element, TreeNode &node) const
Definition: tree/formats/newick/simple_reader.hpp:73
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::SimpleNewickTreeNewickReader::SimpleNewickTreeNewickReader
SimpleNewickTreeNewickReader()
Definition: tree/formats/newick/simple_reader.hpp:134
genesis::tree::SimpleNewickTreeNewickReaderPlugin::~SimpleNewickTreeNewickReaderPlugin
virtual ~SimpleNewickTreeNewickReaderPlugin()=default