A library for working with phylogenetic and population genetic data.
v0.32.0
simple_tree.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_FORMATS_NEWICK_SIMPLE_TREE_H_
2 #define GENESIS_TREE_FORMATS_NEWICK_SIMPLE_TREE_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 
34 #include "genesis/tree/tree.hpp"
37 
38 #include <string>
39 #include <vector>
40 
41 namespace genesis {
42 namespace tree {
43 
44 // =================================================================================================
45 // Typedefs
46 // =================================================================================================
47 
71 
77 
83 
89 
90 // =================================================================================================
91 // Simple Newick Tree Node Data
92 // =================================================================================================
93 
100 {
101 public:
102 
103  // -------------------------------------------------------------------
104  // Constructor and Rule of Five
105  // -------------------------------------------------------------------
106 
107  virtual ~SimpleNewickNodeData() override = default;
108 
109  // Move ctor and assignment.
112 
113 protected:
114 
115  SimpleNewickNodeData() = default;
116 
117  // Copy ctor and assignment.
118  SimpleNewickNodeData( SimpleNewickNodeData const& ) = default;
120 
121 public:
122 
123  static std::unique_ptr< SimpleNewickNodeData > create()
124  {
125  return std::unique_ptr< SimpleNewickNodeData >( new SimpleNewickNodeData() );
126  }
127 
128  virtual std::unique_ptr< BaseNodeData > recreate() const override
129  {
130  return std::unique_ptr< SimpleNewickNodeData >( new SimpleNewickNodeData() );
131  }
132 
133  virtual std::unique_ptr< BaseNodeData > clone() const override
134  {
135  return std::unique_ptr< SimpleNewickNodeData >( new SimpleNewickNodeData( *this ));
136  }
137 
138  // -----------------------------------------------------
139  // Data Members
140  // -----------------------------------------------------
141 
145  std::vector<std::string> comments;
146 
147 };
148 
149 // =================================================================================================
150 // SimpleNewick Tree Edge Data
151 // =================================================================================================
152 
159 {
160 public:
161 
162  // -------------------------------------------------------------------
163  // Constructor and Rule of Five
164  // -------------------------------------------------------------------
165 
166  virtual ~SimpleNewickEdgeData() override = default;
167 
168  // Move ctor and assignment.
171 
172 protected:
173 
174  SimpleNewickEdgeData() = default;
175 
176  // Copy ctor and assignment.
177  SimpleNewickEdgeData( SimpleNewickEdgeData const& ) = default;
179 
180 public:
181 
182  static std::unique_ptr< SimpleNewickEdgeData > create()
183  {
184  return std::unique_ptr< SimpleNewickEdgeData >( new SimpleNewickEdgeData() );
185  }
186 
187  virtual std::unique_ptr< BaseEdgeData > recreate() const override
188  {
189  return std::unique_ptr< SimpleNewickEdgeData >( new SimpleNewickEdgeData() );
190  }
191 
192  virtual std::unique_ptr< BaseEdgeData > clone() const override
193  {
194  return std::unique_ptr< SimpleNewickEdgeData >( new SimpleNewickEdgeData( *this ));
195  }
196 
197  // -----------------------------------------------------
198  // Data Members
199  // -----------------------------------------------------
200 
207  std::vector<std::string> values;
208 
215  std::vector<std::string> tags;
216 
217 };
218 
219 // =================================================================================================
220 // Common Converter
221 // =================================================================================================
222 
230 {
231  return convert(
232  source,
233  [] ( tree::BaseNodeData const& node_data ) {
234  auto attr_node = tree::SimpleNewickNodeData::create();
235  auto const& orig_node = dynamic_cast< CommonNodeData const& >( node_data );
236  attr_node->name = orig_node.name;
237 
238  return attr_node;
239  },
240  [] ( tree::BaseEdgeData const& edge_data ) {
241  auto attr_edge = tree::SimpleNewickEdgeData::create();
242  auto const& orig_edge = dynamic_cast< CommonEdgeData const& >( edge_data );
243  attr_edge->branch_length = orig_edge.branch_length;
244 
245  return attr_edge;
246  }
247  );
248 }
249 
250 } // namespace tree
251 } // namespace genesis
252 
253 #endif // include guard
genesis::tree::convert
Tree convert(Tree const &source, std::function< std::unique_ptr< BaseNodeData >(BaseNodeData const &node_data)> node_data_converter, std::function< std::unique_ptr< BaseEdgeData >(BaseEdgeData const &edge_data)> edge_data_converter)
Create a tree with the same topology as the source tree, while converting its data.
Definition: tree/function/operators.cpp:53
genesis::tree::SimpleNewickNodeData::~SimpleNewickNodeData
virtual ~SimpleNewickNodeData() override=default
genesis::tree::SimpleNewickNodeData::recreate
virtual std::unique_ptr< BaseNodeData > recreate() const override
Polymorphically create a default-constructed instance of this class with the same derived type as it ...
Definition: simple_tree.hpp:128
genesis::tree::convert_common_tree_to_simple_newick_tree
SimpleNewickTree convert_common_tree_to_simple_newick_tree(CommonTree const &source)
Helper function that takes a CommonTree (or any Tree with Node and Edge data derived from it) and tur...
Definition: simple_tree.hpp:229
tree.hpp
Header of Tree class.
genesis::tree::BaseEdgeData
Base class for storing data on Edges of a Tree.
Definition: edge_data.hpp:66
genesis::tree::SimpleNewickNodeData::clone
virtual std::unique_ptr< BaseNodeData > clone() const override
Polymorphically copy an instance of this class. Use instead of copy constructor.
Definition: simple_tree.hpp:133
genesis::tree::SimpleNewickNodeData::operator=
SimpleNewickNodeData & operator=(SimpleNewickNodeData &&)=delete
genesis::tree::Tree
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
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::CommonEdgeData::branch_length
double branch_length
Branch length of the edge.
Definition: tree/common_tree/tree.hpp:193
genesis::tree::SimpleNewickEdgeData::create
static std::unique_ptr< SimpleNewickEdgeData > create()
Definition: simple_tree.hpp:182
genesis::tree::CommonNodeData::name
std::string name
Name of the node.
Definition: tree/common_tree/tree.hpp:127
genesis::tree::SimpleNewickEdgeData::recreate
virtual std::unique_ptr< BaseEdgeData > recreate() const override
Polymorphically create a default-constructed instance of this class with the same derived type as it ...
Definition: simple_tree.hpp:187
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis::tree::TreeNode
Definition: tree/tree/node.hpp:58
genesis::tree::SimpleNewickEdgeData::clone
virtual std::unique_ptr< BaseEdgeData > clone() const override
Polymorphically copy an instance of this class. Use instead of copy constructor.
Definition: simple_tree.hpp:192
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::SimpleNewickEdgeData::~SimpleNewickEdgeData
virtual ~SimpleNewickEdgeData() override=default
genesis::tree::BaseNodeData
Base class for storing data on Nodes of a Tree.
Definition: node_data.hpp:66
operators.hpp
Tree operator functions.
tree.hpp
genesis::tree::SimpleNewickNodeData::comments
std::vector< std::string > comments
List of comments such as NHX elements.
Definition: simple_tree.hpp:145
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::SimpleNewickEdgeData::SimpleNewickEdgeData
SimpleNewickEdgeData()=default
genesis::tree::SimpleNewickNodeData::SimpleNewickNodeData
SimpleNewickNodeData()=default
genesis::tree::SimpleNewickEdgeData
Data class for SimpleNewickTreeEdges.
Definition: simple_tree.hpp:158
genesis::tree::SimpleNewickEdgeData::tags
std::vector< std::string > tags
Arbitrary strings that can be attached to a node via curly brackets "{}".
Definition: simple_tree.hpp:215
genesis::tree::SimpleNewickEdgeData::operator=
SimpleNewickEdgeData & operator=(SimpleNewickEdgeData &&)=delete