A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tree/attribute_tree/tree.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_ATTRIBUTE_TREE_TREE_H_
2 #define GENESIS_TREE_ATTRIBUTE_TREE_TREE_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 
34 #include "genesis/tree/tree.hpp"
37 
38 #include <map>
39 #include <string>
40 
41 namespace genesis {
42 namespace tree {
43 
44 // =================================================================================================
45 // Typedefs
46 // =================================================================================================
47 
52 
58 
64 
70 
81 using AttributeTreeMap = std::map<std::string, std::string>;
82 
83 // =================================================================================================
84 // Attribute Tree Node Data
85 // =================================================================================================
86 
93 {
94 public:
95 
96  // -------------------------------------------------------------------
97  // Constructor and Rule of Five
98  // -------------------------------------------------------------------
99 
100  virtual ~AttributeTreeNodeData() = default;
101 
102  // Move ctor and assignment.
105 
106 protected:
107 
108  AttributeTreeNodeData() = default;
109 
110  // Copy ctor and assignment.
111  AttributeTreeNodeData( AttributeTreeNodeData const& ) = default;
113 
114 public:
115 
116  static std::unique_ptr< AttributeTreeNodeData > create()
117  {
118  return std::unique_ptr< AttributeTreeNodeData >( new AttributeTreeNodeData() );
119  }
120 
121  virtual std::unique_ptr< BaseNodeData > recreate() const override
122  {
123  return std::unique_ptr< AttributeTreeNodeData >( new AttributeTreeNodeData() );
124  }
125 
126  virtual std::unique_ptr< BaseNodeData > clone() const override
127  {
128  return std::unique_ptr< AttributeTreeNodeData >( new AttributeTreeNodeData( *this ));
129  }
130 
131  // -----------------------------------------------------
132  // Data Members
133  // -----------------------------------------------------
134 
139 
140 };
141 
142 // =================================================================================================
143 // Attribute Tree Edge Data
144 // =================================================================================================
145 
152 {
153 public:
154 
155  // -------------------------------------------------------------------
156  // Constructor and Rule of Five
157  // -------------------------------------------------------------------
158 
159  virtual ~AttributeTreeEdgeData() = default;
160 
161  // Move ctor and assignment.
164 
165 protected:
166 
167  AttributeTreeEdgeData() = default;
168 
169  // Copy ctor and assignment.
170  AttributeTreeEdgeData( AttributeTreeEdgeData const& ) = default;
172 
173 public:
174 
175  static std::unique_ptr< AttributeTreeEdgeData > create()
176  {
177  return std::unique_ptr< AttributeTreeEdgeData >( new AttributeTreeEdgeData() );
178  }
179 
180  virtual std::unique_ptr< BaseEdgeData > recreate() const override
181  {
182  return std::unique_ptr< AttributeTreeEdgeData >( new AttributeTreeEdgeData() );
183  }
184 
185  virtual std::unique_ptr< BaseEdgeData > clone() const override
186  {
187  return std::unique_ptr< AttributeTreeEdgeData >( new AttributeTreeEdgeData( *this ));
188  }
189 
190  // -----------------------------------------------------
191  // Data Members
192  // -----------------------------------------------------
193 
198 
199 };
200 
201 // =================================================================================================
202 // Default Converter
203 // =================================================================================================
204 
212 {
213  return convert(
214  source,
215  [] ( tree::BaseNodeData const& node_data ) {
216  auto attr_node = tree::AttributeTreeNodeData::create();
217  auto const& orig_node = dynamic_cast< DefaultNodeData const& >( node_data );
218  attr_node->name = orig_node.name;
219 
220  return attr_node;
221  },
222  [] ( tree::BaseEdgeData const& edge_data ) {
223  auto attr_edge = tree::AttributeTreeEdgeData::create();
224  auto const& orig_edge = dynamic_cast< DefaultEdgeData const& >( edge_data );
225  attr_edge->branch_length = orig_edge.branch_length;
226 
227  return attr_edge;
228  }
229  );
230 }
231 
232 } // namespace tree
233 } // namespace genesis
234 
235 #endif // include guard
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.
virtual std::unique_ptr< BaseNodeData > recreate() const override
Polymorphically create a default-constructed instance of this class with the same derived type as it ...
AttributeTreeEdgeData & operator=(AttributeTreeEdgeData &&)=delete
Data class for AttributeTreeNodes.
Data class for AttributeTreeEdges.
static std::unique_ptr< AttributeTreeEdgeData > create()
Base class for storing data on Nodes of a Tree.
Definition: node_data.hpp:66
Tree operator functions.
Default class containing the commonly needed data for tree nodes.
virtual std::unique_ptr< BaseNodeData > clone() const override
Polymorphically copy an instance of this class. Use instead of copy constructor.
static std::unique_ptr< AttributeTreeNodeData > create()
virtual std::unique_ptr< BaseEdgeData > clone() const override
Polymorphically copy an instance of this class. Use instead of copy constructor.
Default class containing the commonly needed data for tree edges.
AttributeTreeNodeData & operator=(AttributeTreeNodeData &&)=delete
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
AttributeTree convert_default_tree_to_attribute_tree(DefaultTree const &source)
Helper function that takes a DefaultTree (or any Tree with Node and Edge data derived from it) and tu...
AttributeTreeMap attributes
List of attributes stored on this node.
virtual ~AttributeTreeNodeData()=default
virtual ~AttributeTreeEdgeData()=default
double branch_length
Branch length of the edge.
AttributeTreeMap attributes
List of attributes stored on this edge.
std::string name
Name of the node.
std::map< std::string, std::string > AttributeTreeMap
Alias for the map type used by an AttributeTree.
Header of Tree class.
Base class for storing data on Edges of a Tree.
Definition: edge_data.hpp:66
virtual std::unique_ptr< BaseEdgeData > recreate() const override
Polymorphically create a default-constructed instance of this class with the same derived type as it ...