A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tree/default/functions.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2017 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
37 
38 namespace genesis {
39 namespace tree {
40 
41 // =================================================================================================
42 // Node Names
43 // =================================================================================================
44 
54 std::unordered_set<std::string> node_names(
55  Tree const& tree,
56  bool leaves_only
57 ) {
58  std::unordered_set<std::string> name_set;
59  for( auto const& node : tree.nodes() ) {
60  if( node->is_inner() && leaves_only ) {
61  continue;
62  }
63  auto const name = node->data<DefaultNodeData>().name;
64  if( name == "" ) {
65  continue;
66  }
67  name_set.insert( std::move( name ));
68  }
69  return name_set;
70 }
71 
82  Tree const& tree,
83  bool leaves_only
84 ) {
86  for( auto const& node : tree.nodes() ) {
87  if( node->is_inner() && leaves_only ) {
88  continue;
89  }
90  auto const name = node->data<DefaultNodeData>().name;
91  if( name == "" ) {
92  continue;
93  }
94  name_set.insert( std::move( name ));
95  }
96  return name_set;
97 }
98 
106 std::unordered_set<std::string> node_names(
107  TreeSet const& tree_set,
108  bool leaves_only
109 ) {
110  // It would be faster to directly insert into the resulting container, but this version
111  // avoids code duplication and is fast enough for now.
112  std::unordered_set<std::string> name_set;
113  for( auto const& tree : tree_set ) {
114  auto tree_name_set = node_names( tree.tree, leaves_only );
115  name_set.insert( tree_name_set.begin(), tree_name_set.end() );
116  }
117  return name_set;
118 }
119 
128  TreeSet const& tree_set,
129  bool leaves_only
130 ) {
131  // It would be faster to directly insert into the resulting container, but this version
132  // avoids code duplication and is fast enough for now.
134  for( auto const& tree : tree_set ) {
135  // We can use the unsorted version here, which should be a bit faster (not tested...).
136  // Sorting is then done when inserting the names into the final set.
137  auto tree_name_set = node_names( tree.tree, leaves_only );
138  name_set.insert( tree_name_set.begin(), tree_name_set.end() );
139  }
140  return name_set;
141 }
142 
147  Tree const& tree,
148  const std::string& name,
149  bool replace_underscores
150 ) {
151  auto clean_name = name;
152  if (replace_underscores) {
153  clean_name = utils::replace_all(name, "_", " ");
154  }
155 
156  for (auto it = tree.begin_nodes(); it != tree.end_nodes(); ++it) {
157  if( it->get()->data<DefaultNodeData>().name == clean_name) {
158  return it->get();
159  }
160  }
161 
162  return nullptr;
163 }
164 
169  Tree& tree,
170  const std::string& name,
171  bool replace_underscores
172 ) {
173  // Avoid code duplication according to Scott Meyers.
174  auto const& ctree = static_cast< Tree const& >( tree );
175  return const_cast< TreeNode* >(
176  find_node( ctree, name, replace_underscores )
177  );
178 }
179 
180 // =================================================================================================
181 // Branch Length
182 // =================================================================================================
183 
187 double length(Tree const& tree)
188 {
189  double len = 0.0;
190  for( auto const& edge : tree.edges() ) {
191  len += edge->data<DefaultEdgeData>().branch_length;
192  }
193  return len;
194 }
195 
200 double height(Tree const& tree)
201 {
202  auto dists = node_branch_length_distance_vector(tree);
203  return *std::max_element(dists.begin(), dists.end());
204 }
205 
210 double diameter( Tree const& tree )
211 {
212  auto dist_mat = node_branch_length_distance_matrix( tree );
213  return *std::max_element( dist_mat.begin(), dist_mat.end() );
214 }
215 
220 std::vector<double> branch_lengths(
221  Tree const& tree
222 ) {
223  std::vector<double> result;
224  result.reserve( tree.edge_count() );
225  for( size_t i = 0; i < tree.edge_count(); ++i ) {
226  result.push_back( tree.edge_at(i).data<DefaultEdgeData>().branch_length );
227  }
228  return result;
229 }
230 
237  Tree& tree,
238  double length
239 ) {
240  for( auto& edge : tree.edges() ) {
241  edge->data<DefaultEdgeData>().branch_length = length;
242  }
243 }
244 
252  Tree& tree,
253  double factor
254 ) {
255  for( auto& edge : tree.edges() ) {
256  edge->data<DefaultEdgeData>().branch_length *= factor;
257  }
258 }
259 
260 } // namespace tree
261 } // namespace genesis
void set_all_branch_lengths(Tree &tree, double length)
Set all branch lengths of a Tree to a given value.
std::unordered_set< std::string > node_names(Tree const &tree, bool leaves_only)
Returns an unordered set of all TreeNode names of a Tree.
double height(Tree const &tree)
Get the height of the tree, i.e., the longest distance from the root to a leaf, measured using the br...
Default class containing the commonly needed data for tree nodes.
TreeNode const * find_node(Tree const &tree, const std::string &name, bool replace_underscores)
Finds a Node, given its name. If not found, nullptr is returned.
void scale_all_branch_lengths(Tree &tree, double factor)
Scale all branch lengths of a Tree by a given factor.
utils::SortedVector< std::string > node_names_sorted(Tree const &tree, bool leaves_only)
Returns a set of all TreeNode names of a Tree.
std::string replace_all(std::string const &text, std::string const &search, std::string const &replace)
Return a copy of a string, where all occurrences of a search string are replaced by a replace string...
Definition: string.cpp:166
utils::Range< IteratorNodes > nodes()
Definition: tree/tree.cpp:484
Sorted vector of unique elements.
IteratorNodes end_nodes()
Definition: tree/tree.cpp:469
Default class containing the commonly needed data for tree edges.
utils::Range< IteratorEdges > edges()
Definition: tree/tree.cpp:518
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
utils::Matrix< double > node_branch_length_distance_matrix(Tree const &tree)
Return a distance matrix containing pairwise distances between all Nodes, using the branch_length of ...
IteratorNodes begin_nodes()
Definition: tree/tree.cpp:464
Provides some commonly used string utility functions.
Header of Default Tree distance methods.
size_t edge_count() const
Return the number of TreeEdges of the Tree.
Definition: tree/tree.cpp:358
Default Tree functions.
TreeEdge & edge_at(size_t index)
Return the TreeEdge at a certain index.
Definition: tree/tree.cpp:324
tree::TreeSet tree_set(SampleSet const &sample_set)
Return a TreeSet containing all the trees of the SampleSet.
std::vector< double > node_branch_length_distance_vector(Tree const &tree, TreeNode const *node)
Return a vector containing the distance of all nodes with respect to the given start node...
void insert(const_reference value)
Insert a value into the container by copying it.
double length(Tree const &tree)
Get the length of the tree, i.e., the sum of all branch lengths.
std::vector< double > branch_lengths(Tree const &tree)
Get a vector of all branch lengths of a Tree, index by the edge index.
EdgeDataType & data()
Definition: edge.hpp:118
double diameter(Tree const &tree)
Get the diameter of the tree, i.e., the longest distance between any two nodes, measured using the br...