A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tree/default/edge_color.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 
36 
37 #include <assert.h>
38 #include <algorithm>
39 #include <limits>
40 
41 namespace genesis {
42 namespace tree {
43 
44 // =================================================================================================
45 // Edge Color Functions
46 // =================================================================================================
47 
51 std::vector<utils::Color> edge_color_branch_length_gradient( Tree const& tree, bool zero_based )
52 {
53  // Init the result vector with the min head color (green) for each edge.
54  auto ret = std::vector<utils::Color>( tree.edge_count(), utils::heat_gradient(0.0) );
55  if (tree.edge_count() == 0) {
56  // If we are here, this is an empty vector.
57  return ret;
58  }
59 
60  // Perpare min and max.
61  double min_bl = std::numeric_limits<double>::max();
62  double max_bl = std::numeric_limits<double>::lowest();
63  if (zero_based) {
64  min_bl = 0.0;
65  }
66 
67  // Find min and max branch lengths.
68  for (auto it = tree.begin_edges(); it != tree.end_edges(); ++it) {
69  auto const& edge = **it;
70  min_bl = std::min(min_bl, edge.data<DefaultEdgeData>().branch_length);
71  max_bl = std::max(max_bl, edge.data<DefaultEdgeData>().branch_length);
72  }
73  assert( min_bl <= max_bl );
74  double dist = max_bl - min_bl;
75 
76  // If all branch lengths are the same, return the vector as it is (initialized to green).
77  if (dist == 0.0) {;
78  return ret;
79  }
80 
81  // Calculate the heat gradient color based on the branch length for each edge.
82  for (auto it = tree.begin_edges(); it != tree.end_edges(); ++it) {
83  auto const& edge = **it;
84  double val = ( edge.data<DefaultEdgeData>().branch_length - min_bl) / dist;
85  ret[edge.index()] = utils::heat_gradient(val);
86  }
87  return ret;
88 }
89 
90 } // namespace tree
91 } // namespace genesis
IteratorEdges begin_edges()
Definition: tree/tree.cpp:498
IteratorEdges end_edges()
Definition: tree/tree.cpp:503
std::vector< utils::Color > edge_color_branch_length_gradient(Tree const &tree, bool zero_based)
Header of Color class.
Default class containing the commonly needed data for tree edges.
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
Color heat_gradient(double percentage)
Return a Color that represents a heat gradient for a given percentage value.
size_t edge_count() const
Return the number of TreeEdges of the Tree.
Definition: tree/tree.cpp:358
double branch_length
Branch length of the edge.
Color operators and functions.