|
A library for working with phylogenetic and population genetic data.
v0.32.0
|
|
Go to the documentation of this file.
48 #include <unordered_set>
73 throw std::invalid_argument(
74 "Tree is not rooted. Cannot calculate its Phylogenetic ILR tranform."
78 throw std::invalid_argument(
79 "Tree is not bifurcating. Cannot calculate its Phylogenetic ILR tranform."
86 auto get_subtree_indices_ = [](
Subtree const& subtree ){
87 std::unordered_set<size_t> sub_indices;
88 for(
auto it :
preorder( subtree )) {
91 assert( sub_indices.count( it.edge().index() ) == 0 );
95 sub_indices.insert( it.edge().index() );
104 #pragma omp parallel for
105 for(
size_t node_idx = 0; node_idx < data.
tree.
node_count(); ++node_idx ) {
107 assert( node.index() == node_idx );
110 auto const deg =
degree( node );
118 std::unordered_set<size_t> lhs_indices;
119 std::unordered_set<size_t> rhs_indices;
125 lhs_indices = get_subtree_indices_(
Subtree{ node.
link().outer() });
126 rhs_indices = get_subtree_indices_(
Subtree{ node.
link().next().outer() });
129 assert( lhs_indices.size() + rhs_indices.size() == data.
tree.
edge_count() );
134 lhs_indices = get_subtree_indices_(
Subtree{ node.
link().next().outer() });
135 rhs_indices = get_subtree_indices_(
Subtree{ node.
link().next().next().outer() });
138 assert( lhs_indices.size() + rhs_indices.size() < data.
tree.
edge_count() );
142 if( reverse_signs ) {
147 result.col( node_idx ) =
mass_balance( data, lhs_indices, rhs_indices );
165 auto get_subtree_indices_ = [](
Subtree const& subtree ){
166 std::unordered_set<size_t> sub_indices;
167 for(
auto it :
preorder( subtree )) {
170 assert( sub_indices.count( it.edge().index() ) == 0 );
173 if( it.is_first_iteration() ) {
177 sub_indices.insert( it.edge().index() );
186 #pragma omp parallel for
187 for(
size_t edge_idx = 0; edge_idx < data.
tree.
edge_count(); ++edge_idx ) {
189 assert( edge.index() == edge_idx );
197 auto p_indices = get_subtree_indices_(
Subtree{ edge.primary_link() });
198 auto s_indices = get_subtree_indices_(
Subtree{ edge.secondary_link() });
201 assert( p_indices.size() + s_indices.size() == data.
tree.
edge_count() - 1 );
204 if( reverse_signs ) {
209 result.col( edge_idx ) =
mass_balance( data, s_indices, p_indices );
void swap(Sample &lhs, Sample &rhs)
TreeLink const & link() const
Get the TreeLink that separates the subtree from the rest of the tree.
size_t node_count() const
Return the number of TreeNodes of the Tree.
std::vector< double > mass_balance(BalanceData const &data, std::unordered_set< size_t > const &numerator_edge_indices, std::unordered_set< size_t > const &denominator_edge_indices)
Calculate the balance of edge masses between two sets of edges.
utils::Matrix< double > edge_masses
The relative per-edge masses per original input Tree.
TreeNode & node_at(size_t index)
Return the TreeNode at a certain index.
TreeEdge & edge_at(size_t index)
Return the TreeEdge at a certain index.
utils::Range< IteratorPreorder< true > > preorder(ElementType const &element)
size_t degree(TreeLink const &link)
Return the degree of the node for a given TreeLink, i.e. how many neighbouring nodes it has.
Data needed to calculate balances.
std::vector< double > taxon_weights
The taxon/edge weights calculated from mulitple trees.
bool empty() const
Return whether the Tree is empty (i.e., has no nodes, edges and links).
bool is_root(TreeLink const &link)
Return whether the link belongs to the root node of its Tree.
Reference to a subtree of a Tree.
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Tree tree
The Tree on which to calculate balances.
bool is_bifurcating(Tree const &tree, bool loose)
Return whether the Tree is bifurcating.
bool is_rooted(Tree const &tree)
Return whether the Tree is rooted, that is, whether the root node has two neighbors.
utils::Matrix< double > edge_balances(BalanceData const &data, bool reverse_signs)
Calculate edge balances using the Isometric Log Ratio transformation.
utils::Matrix< double > phylogenetic_ilr_transform(BalanceData const &data, bool reverse_signs)
Calculate the Phylogenetic Isometric Log Ratio transformation.
bool is_leaf(TreeLink const &link)
Return true iff the node of the given link is a leaf node.
size_t edge_count() const
Return the number of TreeEdges of the Tree.