A toolkit for working with phylogenetic data.
v0.24.0
tree/function/manipulation.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_FUNCTION_MANIPULATION_H_
2 #define GENESIS_TREE_FUNCTION_MANIPULATION_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2019 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 
34 #include <cstddef> // size_t
35 #include <functional>
36 
37 #include "genesis/tree/tree.hpp"
39 
40 namespace genesis {
41 namespace tree {
42 
43 // =================================================================================================
44 // Forward Declarations
45 // =================================================================================================
46 
47 class Tree;
48 class TreeNode;
49 class TreeEdge;
50 class TreeLink;
51 class Subtree;
52 
53 // =================================================================================================
54 // Minimal Tree
55 // =================================================================================================
56 
68 
80 template< class NodeDataType = CommonNodeData, class EdgeDataType = CommonEdgeData >
82 {
83  auto tree = minimal_tree_topology();
84  for( size_t i = 0; i < tree.node_count(); ++i ) {
85  tree.node_at(i).reset_data( NodeDataType::create() );
86  }
87  for( size_t i = 0; i < tree.edge_count(); ++i ) {
88  tree.edge_at(i).reset_data( EdgeDataType::create() );
89  }
90  return tree;
91 }
92 
93 // =================================================================================================
94 // Add Nodes
95 // =================================================================================================
96 
131 TreeNode& add_new_node( Tree& tree, TreeNode& target_node );
132 
174  Tree& tree,
175  TreeEdge& target_edge,
176  std::function<void( TreeEdge& target_edge, TreeEdge& new_edge )> adjust_edges = {}
177 );
178 
213  Tree& tree,
214  TreeEdge& target_edge,
215  std::function<void( TreeEdge& target_edge, TreeEdge& new_edge )> adjust_edges = {}
216 );
217 
218 // =================================================================================================
219 // Delete Nodes
220 // =================================================================================================
221 
235 void delete_node( Tree& tree, TreeNode& target_node );
236 
242 void delete_leaf_node( Tree& tree, TreeNode& target_node );
243 
259 void delete_linear_node(
260  Tree& tree,
261  TreeNode& target_node,
262  std::function<void( TreeEdge& remaining_edge, TreeEdge& deleted_edge )> adjust_edges = {}
263 );
264 
276 void delete_subtree( Tree& tree, Subtree const& subtree );
277 
278 // void delete_edge(
279 // Tree& tree,
280 // TreeEdge& target_edge,
281 // std::function<void( TreeEdge& remaining_node, TreeEdge& deleted_node )> adjust_nodes = {}
282 // );
283 
284 // =================================================================================================
285 // Rooting, Rerooting, Unrooting
286 // =================================================================================================
287 
306  Tree& tree,
307  TreeEdge& target_edge,
308  std::function<void( TreeEdge& target_edge, TreeEdge& new_edge )> adjust_edges = {}
309 );
310 
325  Tree& tree,
326  std::function<void( TreeEdge& target_edge, TreeEdge& new_edge )> adjust_edges = {}
327 );
328 
338 void make_unrooted(
339  Tree& tree,
340  std::function<void( TreeEdge& remaining_edge, TreeEdge& deleted_edge )> adjust_edges = {}
341 );
342 
371 void change_rooting( Tree& tree, TreeLink& at_link );
372 
380 void change_rooting( Tree& tree, TreeNode& at_node );
381 
382 // =================================================================================================
383 // Ladderize
384 // =================================================================================================
385 
386 enum class LadderizeOrder
387 {
388  kSmallFirst,
390 };
391 
400 
401 } // namespace tree
402 } // namespace genesis
403 
404 #endif // include guard
void delete_node(Tree &tree, TreeNode &target_node)
Delete a TreeNode from a Tree.
Tree minimal_tree()
Create a minimal Tree that can be used with manipulation functions such as add_new_node() or add_new_...
void ladderize(Tree &tree, LadderizeOrder order)
Ladderize a Tree, that is, order its subtrees by size.
void make_unrooted(Tree &tree, std::function< void(TreeEdge &remaining_edge, TreeEdge &deleted_edge)> adjust_edges)
Unroot a Tree.
void delete_subtree(Tree &tree, Subtree const &subtree)
Delete a complete Subtree from a Tree.
Tree minimal_tree_topology()
Create a minimal Tree that can be used with manipulation functions such as add_new_node() or add_new_...
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
Reference to a subtree of a Tree.
Definition: subtree.hpp:69
TreeNode & make_rooted(Tree &tree, TreeEdge &target_edge, std::function< void(TreeEdge &target_edge, TreeEdge &new_edge)> adjust_edges)
Root a Tree at a given TreeEdge.
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
TreeNode & add_new_leaf_node(Tree &tree, TreeEdge &target_edge, std::function< void(TreeEdge &target_edge, TreeEdge &new_edge)> adjust_edges)
Add a new Node as a leaf to an existing Edge, by also adding a new Node in the middle of that Edge...
TreeNode & add_new_node(Tree &tree, TreeNode &target_node)
Add a new Node as a leaf to an existing Node.
void delete_leaf_node(Tree &tree, TreeNode &target_node)
Delete a leaf TreeNode.
Header of Tree class.
void change_rooting(Tree &tree, TreeLink &at_link)
"Reroot" the Tree at the given TreeLink.
void delete_linear_node(Tree &tree, TreeNode &target_node, std::function< void(TreeEdge &remaining_edge, TreeEdge &deleted_edge)> adjust_edges)
Delete a "linear" TreeNode from a Tree, that is, a node with two neighbours.