A library for working with phylogenetic and population genetic data.
v0.27.0
tree/common_tree/edge_color.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2019 Lucas Czech and HITS gGmbH
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 <cassert>
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 const& edge : tree.edges() ) {
69  min_bl = std::min(min_bl, edge.data<CommonEdgeData>().branch_length);
70  max_bl = std::max(max_bl, edge.data<CommonEdgeData>().branch_length);
71  }
72  assert( min_bl <= max_bl );
73  double dist = max_bl - min_bl;
74 
75  // If all branch lengths are the same, return the vector as it is (initialized to green).
76  if (dist == 0.0) {;
77  return ret;
78  }
79 
80  // Calculate the heat gradient color based on the branch length for each edge.
81  for( auto const& edge : tree.edges() ) {
82  double val = ( edge.data<CommonEdgeData>().branch_length - min_bl) / dist;
83  ret[ edge.index() ] = utils::heat_gradient(val);
84  }
85  return ret;
86 }
87 
88 } // namespace tree
89 } // namespace genesis
genesis::tree::Tree
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
genesis::tree::CommonEdgeData::branch_length
double branch_length
Branch length of the edge.
Definition: tree/common_tree/tree.hpp:193
genesis::tree::edge_color_branch_length_gradient
std::vector< utils::Color > edge_color_branch_length_gradient(Tree const &tree, bool zero_based)
Definition: tree/common_tree/edge_color.cpp:51
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::utils::heat_gradient
Color heat_gradient(double percentage)
Return a Color that represents a heat gradient for a given percentage value.
Definition: utils/tools/color/functions.cpp:203
tree.hpp
genesis::tree::Tree::edges
utils::Range< IteratorEdges > edges()
Definition: tree/tree.hpp:452
functions.hpp
Color operators and functions.
color.hpp
Header of Color class.
edge_color.hpp
genesis::tree::CommonEdgeData
Common class containing the commonly needed data for tree edges.
Definition: tree/common_tree/tree.hpp:144
genesis::tree::Tree::edge_count
size_t edge_count() const
Return the number of TreeEdges of the Tree.
Definition: tree/tree.hpp:272