bpp-phyl3 3.0.0
PhyloStatistics.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
5#include "PhyloStatistics.h"
6#include "Tree/Node.h"
7#include "Tree/TreeTemplate.h"
8
9using namespace bpp;
10using namespace std;
11
12void PhyloStatistics::setTree(const Tree& tree)
13{
14 const Tree* treeP = &tree;
15 const TreeTemplate<Node>* ttreeP = dynamic_cast<const TreeTemplate<Node>*>(treeP);
16 bool copy = false;
17 if (!ttreeP)
18 {
19 ttreeP = new TreeTemplate<Node>(tree);
20 copy = true;
21 }
22
23 // Perform computations:
26 branchLengths_.clear();
27 nodeHeights_.clear();
28 nodeDepths_.clear();
29 nodeNumberOfSons_.clear();
30 nodeIds_.clear();
31 double h;
32 size_t d;
33 computeForSubtree_(ttreeP->getRootNode(), h, d);
34
35 if (copy)
36 delete ttreeP;
37}
38
39void PhyloStatistics::computeForSubtree_(const Node* node, double& height, size_t& depth)
40{
41 if (node->isLeaf())
43 else
45
46 nodeNumberOfSons_.push_back(node->getNumberOfSons());
47
48 height = 0;
49 depth = 0;
50 for (size_t i = 0; i < node->getNumberOfSons(); i++)
51 {
52 const Node* son = (*node)[static_cast<int>(i)];
53 double dist = 0;
54 if (son->hasDistanceToFather())
55 dist = son->getDistanceToFather();
56 else
57 dist = 0;
58 double subHeight;
59 size_t subDepth;
60 computeForSubtree_(son, subHeight, subDepth);
61 subHeight += dist;
62 subDepth++;
63 if (subHeight > height)
64 height = subHeight;
65 if (subDepth > depth)
66 depth = subDepth;
67 }
68
69 if (node->hasDistanceToFather())
70 branchLengths_.push_back(node->getDistanceToFather());
71 else
72 branchLengths_.push_back(log(0)); // -Inf if no branch length.
73
74 nodeHeights_.push_back(height);
75 nodeDepths_.push_back(depth);
76}
The phylogenetic node class.
Definition: Node.h:59
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
Definition: Node.h:288
virtual bool isLeaf() const
Definition: Node.h:679
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
Definition: Node.h:250
virtual size_t getNumberOfSons() const
Definition: Node.h:355
void computeForSubtree_(const Node *node, double &height, size_t &depth)
std::vector< int > nodeIds_
std::vector< double > nodeHeights_
std::vector< size_t > nodeDepths_
std::vector< size_t > nodeNumberOfSons_
std::vector< double > branchLengths_
The phylogenetic tree class.
Definition: TreeTemplate.h:59
virtual N * getRootNode()
Definition: TreeTemplate.h:389
Interface for phylogenetic tree objects.
Definition: Tree.h:115
Defines the basic types of data flow nodes.
double log(const ExtendedFloat &ef)