A library for working with phylogenetic and population genetic data.
v0.27.0
phylo_factor_colors.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2020 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 
33 #include <cassert>
34 #include <stdexcept>
35 
36 namespace genesis {
37 namespace tree {
38 
39 // =================================================================================================
40 // Helper Functions
41 // =================================================================================================
42 
43 std::vector<size_t> phylo_factor_edge_indices(
44  std::vector<PhyloFactor> const& factors,
45  size_t max_factor
46 ) {
47  // Find as many as we need.
48  max_factor = std::min( max_factor, factors.size() );
49  std::vector<size_t> result;
50  for( size_t i = 0; i < max_factor; ++i ) {
51  result.push_back( factors[i].edge_index );
52  }
53  return result;
54 }
55 
56 // =================================================================================================
57 // Single Factor Coloring
58 // =================================================================================================
59 
60 std::vector<utils::Color> phylo_factor_single_factor_colors(
61  Tree const& tree,
62  std::vector<PhyloFactor> const& factors,
63  size_t factor_index,
65 ) {
66  if( factor_index >= factors.size() ) {
67  throw std::invalid_argument( "Invalid index for a phylo factor." );
68  }
69  auto const& factor = factors[ factor_index ];
70 
71  // Prepare all edges in neutral color.
72  auto edge_cols = std::vector<utils::Color>( tree.edge_count(), colors.neutral_edges );
73 
74  // Helper to set the color of one edge to a value, unless it already has a value,
75  // in which case we have an error.
76  auto set_color_ = [&]( size_t index, utils::Color color ){
77  if( index >= edge_cols.size() ) {
78  throw std::runtime_error( "Invalid edge index in a phylo factor." );
79  }
80  if( edge_cols[ index ] == colors.neutral_edges ) {
81  edge_cols[ index ] = color;
82  } else {
83  throw std::runtime_error(
84  "Edge at index " + std::to_string( index ) + " is in multiple edge sets of the "
85  "phylo factor."
86  );
87  }
88  };
89 
90  // Set the edges of the factor and its subtrees.
91  set_color_( factor.edge_index, colors.factor_edge );
92  for( auto const e : factor.edge_indices_primary ) {
93  set_color_( e, colors.primary_edges );
94  }
95  for( auto const e : factor.edge_indices_secondary ) {
96  set_color_( e, colors.secondary_edges );
97  }
98 
99  // Get all previous factor edges and colorize them.
100  auto const prev_factors = phylo_factor_edge_indices( factors, factor_index );
101  for( auto const e : prev_factors ) {
102  set_color_( e, colors.previous_factors );
103  }
104 
105  return edge_cols;
106 }
107 
108 // =================================================================================================
109 // Factor Clade Coloring
110 // =================================================================================================
111 
112 std::vector<utils::Color> phylo_factor_clade_colors(
113  Tree const& tree,
114  std::vector<PhyloFactor> const& factors,
115  size_t num_factors,
117 ) {
118  // Input checks.
119  if( num_factors > factors.size() ) {
120  throw std::invalid_argument( "Invalid number of factors requested." );
121  }
122  if( num_factors == 0 ) {
123  num_factors = factors.size();
124  }
125  if( num_factors > colors.clade_colors.size() ) {
126  throw std::invalid_argument(
127  "Not enough clade colors provided for the requested number of factors."
128  );
129  }
130 
131  // Prepare all edges in base color.
132  auto edge_cols = std::vector<utils::Color>( tree.edge_count(), colors.base_edges );
133 
134  // Helper to set the color of one edge to a value.
135  auto set_color_ = [&]( size_t index, utils::Color color ){
136  if( index >= edge_cols.size() ) {
137  throw std::runtime_error( "Invalid edge index in a phylo factor." );
138  }
139  edge_cols[ index ] = color;
140  };
141 
142  // Color each factor in a color of the clade color set.
143  for( size_t i = 0; i < num_factors; ++i ) {
144  assert( i < factors.size() );
145  assert( i < colors.clade_colors.size() );
146 
147  auto const& factor = factors[i];
148  auto const& clade_col = colors.clade_colors[i];
149 
150  set_color_( factor.edge_index, colors.factor_edges );
151  for( auto const e : factor.edge_indices_secondary ) {
152  set_color_( e, clade_col );
153  }
154  }
155 
156  return edge_cols;
157 }
158 
159 } // namespace tree
160 } // namespace genesis
genesis::utils::Color
Definition: color.hpp:47
genesis::tree::phylo_factor_single_factor_colors
std::vector< utils::Color > phylo_factor_single_factor_colors(Tree const &tree, std::vector< PhyloFactor > const &factors, size_t factor_index, PhyloFactorSingleColors colors)
Return a color for each edge indicating its role in a single phylogenetic factor.
Definition: phylo_factor_colors.cpp:60
genesis::tree::PhyloFactorSingleColors::previous_factors
utils::Color previous_factors
Color for the edges that have been factored out before (earlier factors in the greedy search).
Definition: phylo_factor_colors.hpp:111
genesis::tree::PhyloFactorCladeColors::base_edges
utils::Color base_edges
Color for the edges towards the root of the first phylo factor.
Definition: phylo_factor_colors.hpp:171
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: functions/genome_locus.hpp:48
genesis::tree::Tree
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
genesis::tree::PhyloFactorSingleColors::primary_edges
utils::Color primary_edges
Color for the edges towards the root that have been used in this phylo factor.
Definition: phylo_factor_colors.hpp:96
genesis::tree::phylo_factor_clade_colors
std::vector< utils::Color > phylo_factor_clade_colors(Tree const &tree, std::vector< PhyloFactor > const &factors, size_t num_factors, PhyloFactorCladeColors colors)
Return a color for each edge, indicating which factor (phylogenetic unit, clade) it belongs to.
Definition: phylo_factor_colors.cpp:112
genesis::tree::PhyloFactorSingleColors::factor_edge
utils::Color factor_edge
Color for the edge of that phylo factor.
Definition: phylo_factor_colors.hpp:89
genesis::tree::PhyloFactorCladeColors::factor_edges
utils::Color factor_edges
Color for the "winning" edges of phylo factors.
Definition: phylo_factor_colors.hpp:161
phylo_factor_colors.hpp
genesis::tree::phylo_factor_edge_indices
std::vector< size_t > phylo_factor_edge_indices(std::vector< PhyloFactor > const &factors, size_t max_factor)
Get a list of all edges that have factored out by phylogenetic_factorization().
Definition: phylo_factor_colors.cpp:43
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::tree::PhyloFactorCladeColors
Store a set of colors for making visualizations of the clades of all phylo factors.
Definition: phylo_factor_colors.hpp:154
genesis::tree::PhyloFactorSingleColors::neutral_edges
utils::Color neutral_edges
Color for the edges that have not been used in this phylo factor.
Definition: phylo_factor_colors.hpp:118
genesis::tree::PhyloFactorSingleColors
Store a set of colors for making visualizations of individual phylo factors.
Definition: phylo_factor_colors.hpp:82
genesis::tree::PhyloFactorSingleColors::secondary_edges
utils::Color secondary_edges
Color for the edges away from the root that have been used in this phylo factor.
Definition: phylo_factor_colors.hpp:103
genesis::tree::PhyloFactorCladeColors::clade_colors
std::vector< utils::Color > clade_colors
Colors for the sets of edges away from the root that have been split by the phylo factors.
Definition: phylo_factor_colors.hpp:179
genesis::tree::Tree::edge_count
size_t edge_count() const
Return the number of TreeEdges of the Tree.
Definition: tree/tree.hpp:272