A library for working with phylogenetic and population genetic data.
v0.32.0
phylo_factor.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_MASS_TREE_PHYLO_FACTOR_H_
2 #define GENESIS_TREE_MASS_TREE_PHYLO_FACTOR_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2024 Lucas Czech
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 
36 
37 #include <functional>
38 #include <unordered_set>
39 #include <vector>
40 
41 namespace genesis {
42 namespace tree {
43 
44 // =================================================================================================
45 // Forward Declarations
46 // =================================================================================================
47 
48 class Subtree;
49 class Tree;
50 using MassTree = Tree;
51 
52 // =================================================================================================
53 // Phylogenetic Factorization
54 // =================================================================================================
55 
59 using PhyloFactorObjectiveFunction = std::function<
60  double( size_t iteration, size_t edge_index, std::vector<double> const& balances )
61 >;
62 
69 {
73  size_t edge_index;
74 
78  std::unordered_set<size_t> edge_indices_primary;
79 
83  std::unordered_set<size_t> edge_indices_secondary;
84 
88  std::vector<double> balances;
89 
94 
105  std::vector<double> all_objective_values;
106 };
107 
118 std::unordered_set<size_t> phylo_factor_subtree_indices(
119  Subtree const& subtree,
120  std::unordered_set<size_t> const& candidate_edges
121 );
122 
128  size_t iteration,
129  BalanceData const& data,
130  std::unordered_set<size_t> const& candidate_edges,
132 );
133 
167 std::vector<PhyloFactor> phylogenetic_factorization(
168  BalanceData const& data,
169  std::function<double( std::vector<double> const& balances )> objective,
170  size_t max_iterations = 0,
171  std::function<void( size_t iteration, size_t max_iterations )> log_progress = {}
172 );
173 
185 std::vector<PhyloFactor> phylogenetic_factorization(
186  BalanceData const& data,
188  size_t max_iterations = 0,
189  std::function<void( size_t iteration, size_t max_iterations )> log_progress = {}
190 );
191 
192 } // namespace tree
193 } // namespace genesis
194 
195 #endif // include guard
genesis::tree::phylo_factor_subtree_indices
std::unordered_set< size_t > phylo_factor_subtree_indices(Subtree const &subtree, std::unordered_set< size_t > const &candidate_edges)
Helper function for phylogenetic_factorization() to find the constrained subtrees that are split by a...
Definition: phylo_factor.cpp:65
genesis::tree::PhyloFactor::balances
std::vector< double > balances
The balances for all Samples calculated on the two sets of edge indices of this factor.
Definition: phylo_factor.hpp:88
balances.hpp
genesis::tree::MassTree
Tree MassTree
Alias for a Tree that stores masses on its TreeEdges.
Definition: placement/function/operators.hpp:54
genesis::tree::PhyloFactor::edge_index
size_t edge_index
The edge that this factor found to be maximizing for the objective function.
Definition: phylo_factor.hpp:73
genesis::tree::PhyloFactor
A single phylogenetic factor.
Definition: phylo_factor.hpp:68
genesis::tree::PhyloFactor::all_objective_values
std::vector< double > all_objective_values
For reference, all other objective values for the other edges of the tree.
Definition: phylo_factor.hpp:105
genesis::tree::BalanceData
Data needed to calculate balances.
Definition: balances.hpp:170
genesis::tree::PhyloFactor::objective_value
double objective_value
The objective value obtained from the objective function using the balances.
Definition: phylo_factor.hpp:93
genesis::tree::PhyloFactorObjectiveFunction
std::function< double(size_t iteration, size_t edge_index, std::vector< double > const &balances) > PhyloFactorObjectiveFunction
Function type used as the objective function in phylogenetic_factorization().
Definition: phylo_factor.hpp:61
genesis::tree::PhyloFactor::edge_indices_secondary
std::unordered_set< size_t > edge_indices_secondary
The set of edges on the non-root (secondary) side of the edge that belongs to this factor.
Definition: phylo_factor.hpp:83
genesis::tree::Subtree
Reference to a subtree of a Tree.
Definition: subtree.hpp:69
matrix.hpp
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::phylogenetic_factorization
std::vector< PhyloFactor > phylogenetic_factorization(BalanceData const &data, std::function< double(std::vector< double > const &balances)> objective, size_t max_iterations, std::function< void(size_t iteration, size_t max_iterations)> log_progress)
Calculate the Phylogenetic Factorization (PhyloFactor) of a set of MassTrees.
Definition: phylo_factor.cpp:268
genesis::tree::phylo_factor_find_best_edge
PhyloFactor phylo_factor_find_best_edge(size_t iteration, BalanceData const &data, std::unordered_set< size_t > const &candidate_edges, PhyloFactorObjectiveFunction objective)
Helper function for phylogenetic_factorization() that tries all candidate edges to find the one that ...
Definition: phylo_factor.cpp:183
genesis::tree::PhyloFactor::edge_indices_primary
std::unordered_set< size_t > edge_indices_primary
The set of edges on the root (primary) side of the edge that belongs to this factor.
Definition: phylo_factor.hpp:78