bpp-phyl  2.2.0
AbstractAgglomerativeDistanceMethod.cpp
Go to the documentation of this file.
1 //
2 // File: AbstractAgglomerativeDistanceMethod.cpp
3 // Created by: Julien Dutheil
4 // Vincent Ranwez
5 // Created on: Wed jun 22 10:00 2005
6 //
7 
8 /*
9 Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
10 
11 This software is a computer program whose purpose is to provide classes
12 for phylogenetic data analysis.
13 
14 This software is governed by the CeCILL license under French law and
15 abiding by the rules of distribution of free software. You can use,
16 modify and/ or redistribute the software under the terms of the CeCILL
17 license as circulated by CEA, CNRS and INRIA at the following URL
18 "http://www.cecill.info".
19 
20 As a counterpart to the access to the source code and rights to copy,
21 modify and redistribute granted by the license, users are provided only
22 with a limited warranty and the software's author, the holder of the
23 economic rights, and the successive licensors have only limited
24 liability.
25 
26 In this respect, the user's attention is drawn to the risks associated
27 with loading, using, modifying and/or developing or reproducing the
28 software by the user in light of its specific status of free software,
29 that may mean that it is complicated to manipulate, and that also
30 therefore means that it is reserved for developers and experienced
31 professionals having in-depth computer knowledge. Users are therefore
32 encouraged to load and test the software's suitability as regards their
33 requirements in conditions enabling the security of their systems and/or
34 data to be ensured and, more generally, to use and operate it in the
35 same conditions as regards security.
36 
37 The fact that you are presently reading this means that you have had
38 knowledge of the CeCILL license and that you accept its terms.
39 */
40 
42 #include "../Node.h"
43 
44 #include <Bpp/App/ApplicationTools.h>
45 
46 using namespace bpp;
47 
48 // From the STL:
49 #include <iostream>
50 
51 using namespace std;
52 
53 void AbstractAgglomerativeDistanceMethod::setDistanceMatrix(const DistanceMatrix& matrix) throw (Exception)
54 {
55  if (matrix.size() <= 3)
56  throw Exception("AbstractAgglomerativeDistanceMethod::setDistanceMatrix(): matrix must be at least of dimension 3.");
57  matrix_ = matrix;
58  currentNodes_.clear();
59  if (tree_) delete tree_;
60 }
61 
63 {
64  // Initialization:
65  for (size_t i = 0; i < matrix_.size(); ++i)
66  {
67  currentNodes_[i] = getLeafNode(static_cast<int>(i), matrix_.getName(i));
68  }
69  int idNextNode = static_cast<int>(matrix_.size());
70  vector<double> newDist(matrix_.size());
71 
72  // Build tree:
73  while (currentNodes_.size() > (rootTree_ ? 2 : 3))
74  {
75  if (verbose_)
76  ApplicationTools::displayGauge(matrix_.size() - currentNodes_.size(), matrix_.size() - (rootTree_ ? 2 : 3) - 1);
77  vector<size_t> bestPair = getBestPair();
78  vector<double> distances = computeBranchLengthsForPair(bestPair);
79  Node* best1 = currentNodes_[bestPair[0]];
80  Node* best2 = currentNodes_[bestPair[1]];
81  // Distances may be used by getParentNodes (PGMA for instance).
82  best1->setDistanceToFather(distances[0]);
83  best2->setDistanceToFather(distances[1]);
84  Node* parent = getParentNode(idNextNode, best1, best2);
85  idNextNode++;
86  for (map<size_t, Node *>::iterator i = currentNodes_.begin(); i != currentNodes_.end(); i++)
87  {
88  size_t id = i->first;
89  if (id != bestPair[0] && id != bestPair[1])
90  {
91  assert (id < newDist.size()); //DEBUG
92  newDist[id] = computeDistancesFromPair(bestPair, distances, id);
93  }
94  else
95  {
96  newDist[id] = 0;
97  }
98  }
99  // Actualize currentNodes_:
100  currentNodes_[bestPair[0]] = parent;
101  currentNodes_.erase(bestPair[1]);
102  for (map<size_t, Node *>::iterator i = currentNodes_.begin(); i != currentNodes_.end(); i++)
103  {
104  size_t id = i->first;
105  matrix_(bestPair[0], id) = matrix_(id, bestPair[0]) = newDist[id];
106  }
107  }
108  finalStep(idNextNode);
109 }
110 
112 {
113  return new Node(id, name);
114 }
115 
117 {
118  Node* parent = new Node(id);
119  parent->addSon(son1);
120  parent->addSon(son2);
121  return parent;
122 }
123 
virtual void addSon(size_t pos, Node *node)
Definition: Node.h:407
virtual Node * getLeafNode(int id, const std::string &name)
Get a leaf node.
virtual void setDistanceMatrix(const DistanceMatrix &matrix)
Set the distance matrix to use.
STL namespace.
virtual void computeTree()
Compute the tree corresponding to the distance matrix.
The phylogenetic node class.
Definition: Node.h:90
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
Definition: Node.h:299
virtual Node * getParentNode(int id, Node *son1, Node *son2)
Get an inner node.