bpp-phyl  2.2.0
PhyloStatistics.cpp
Go to the documentation of this file.
1 //
2 // File: PHyloStatistics.cpp
3 // Created by: Julien Dutheil
4 // Created on: Sat Aug 08 07:29 2009
5 //
6 
7 /*
8 Copyright or © or Copr. CNRS, (November 16, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for phylogenetic data analysis.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #include "PhyloStatistics.h"
41 #include "TreeTemplate.h"
42 #include "Node.h"
43 
44 using namespace bpp;
45 using namespace std;
46 
47 void PhyloStatistics::setTree(const Tree& tree)
48 {
49  const Tree* treeP = &tree;
50  const TreeTemplate<Node>* ttreeP = dynamic_cast<const TreeTemplate<Node>*>(treeP);
51  bool copy = false;
52  if (!ttreeP)
53  {
54  ttreeP = new TreeTemplate<Node>(tree);
55  copy = true;
56  }
57 
58  //Perform computations:
59  numberOfLeaves_ = 0;
60  numberOfAncestors_ = 0;
61  branchLengths_.clear();
62  nodeHeights_.clear();
63  nodeDepths_.clear();
64  nodeNumberOfSons_.clear();
65  nodeIds_.clear();
66  double h;
67  size_t d;
68  computeForSubtree_(ttreeP->getRootNode(), h, d);
69 
70  if (copy) delete ttreeP;
71 }
72 
73 void PhyloStatistics::computeForSubtree_(const Node* node, double& height, size_t& depth)
74 {
75  if (node->isLeaf())
76  numberOfLeaves_++;
77  else
78  numberOfAncestors_++;
79 
80  nodeNumberOfSons_.push_back(node->getNumberOfSons());
81 
82  height = 0;
83  depth = 0;
84  for (size_t i = 0; i < node->getNumberOfSons(); i++)
85  {
86  const Node* son = (*node)[static_cast<int>(i)];
87  double dist = 0;
88  if (son->hasDistanceToFather()) dist = son->getDistanceToFather();
89  else dist = 0;
90  double subHeight;
91  size_t subDepth;
92  computeForSubtree_(son, subHeight, subDepth);
93  subHeight += dist;
94  subDepth++;
95  if (subHeight > height) height = subHeight;
96  if (subDepth > depth ) depth = subDepth ;
97  }
98 
99  if (node->hasDistanceToFather())
100  branchLengths_.push_back(node->getDistanceToFather());
101  else
102  branchLengths_.push_back(log(0)); //-Inf if no branch length.
103 
104  nodeHeights_.push_back(height);
105  nodeDepths_.push_back(depth);
106 }
107 
108 
virtual N * getRootNode()
Definition: TreeTemplate.h:403
STL namespace.
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
Definition: Node.h:321
The phylogenetic tree class.
Interface for phylogenetic tree objects.
Definition: Tree.h:148
void setTree(const Tree &tree)
Compute statistics for a given input tree.
virtual bool isLeaf() const
Definition: Node.h:692
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
Definition: Node.h:283
The phylogenetic node class.
Definition: Node.h:90
virtual size_t getNumberOfSons() const
Definition: Node.h:388
void computeForSubtree_(const Node *node, double &height, size_t &depth)