bpp-phyl  2.2.0
Node.cpp
Go to the documentation of this file.
1 //
2 // File: Node.cpp
3 // Created by: Julien Dutheil
4 // Created on: Thu Mar 13 12:03:18 2003
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (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 "Node.h"
41 #include "TreeTools.h"
42 
43 #include <Bpp/Exceptions.h>
44 #include <Bpp/Text/TextTools.h>
45 
46 using namespace bpp;
47 
48 //from the STL:
49 #include <algorithm>
50 #include <iostream>
51 
52 using namespace std;
53 
56 Node::Node(const Node& node):
57  id_(node.id_), name_(0),
58  sons_(), father_(0),
59  //, sons_(node.sons_), father_(node.father_),
60  distanceToFather_(0), nodeProperties_(), branchProperties_()
61 {
62  name_ = node.hasName() ? new string(* node.name_) : 0;
63  distanceToFather_ = node.hasDistanceToFather() ? new double(* node.distanceToFather_) : 0;
64  for (map<string, Clonable *>::iterator i = node.nodeProperties_.begin(); i != node.nodeProperties_.end(); i++)
65  nodeProperties_[i->first] = i->second->clone();
66  for (map<string, Clonable *>::iterator i = node.branchProperties_.begin(); i != node.branchProperties_.end(); i++)
67  branchProperties_[i->first] = i->second->clone();
68 }
69 
72 Node& Node::operator=(const Node & node)
73 {
74  id_ = node.id_;
75  if(name_) delete name_;
76  name_ = node.hasName() ? new string(* node.name_) : 0;
77  //father_ = node.father_;
79  distanceToFather_ = node.hasDistanceToFather() ? new double(* node.distanceToFather_) : 0;
80  //sons_ = node.sons_;
81  for(map<string, Clonable *>::iterator i = node.nodeProperties_.begin(); i != node.nodeProperties_.end(); i++)
82  {
83  Clonable * p = nodeProperties_[i->first];
84  if(p) delete p;
85  nodeProperties_[i->first] = i->second->clone();
86  }
87  for(map<string, Clonable *>::iterator i = node.branchProperties_.begin(); i != node.branchProperties_.end(); i++)
88  {
89  Clonable * p = branchProperties_[i->first];
90  if(p) delete p;
91  branchProperties_[i->first] = i->second->clone();
92  }
93  return * this;
94 }
95 
98 void Node::swap(size_t branch1, size_t branch2) throw (IndexOutOfBoundsException)
99 {
100  if (branch1 > branch2)
101  {
102  size_t tmp = branch1;
103  branch1 = branch2;
104  branch2 = tmp;
105  }
106  Node* node1 = getSon(branch1);
107  Node* node2 = getSon(branch2);
108  removeSon(node1);
109  removeSon(node2);
110  addSon(branch1, node2);
111  addSon(branch2, node1);
112 }
113 
114 vector<const Node *> Node::getNeighbors() const
115 {
116  vector<const Node *> neighbors;
117  if(hasFather()) neighbors.push_back(father_);
118  for(size_t i = 0; i < sons_.size(); i++) neighbors.push_back(sons_[i]);
119  return neighbors;
120 }
121 
122 vector<Node *> Node::getNeighbors()
123 {
124  vector<Node *> neighbors;
125  if(hasFather()) neighbors.push_back(father_);
126  for(size_t i = 0; i < sons_.size(); i++) neighbors.push_back(sons_[i]);
127  return neighbors;
128 }
129 
130 size_t Node::getSonPosition(const Node* son) const throw (NodeNotFoundException, NullPointerException)
131 {
132  if (!son)
133  throw NullPointerException("Node::getSonPosition(). Empty node given as input.");
134  for(size_t i = 0; i < sons_.size(); i++)
135  {
136  if(sons_[i] == son) return i;
137  }
138  throw NodeNotFoundException("Son not found", TextTools::toString(son->getId()));
139 }
140 
142 {
144 }
145 
147 {
149  return dynamic_cast<const Number<double> *>(getBranchProperty(TreeTools::BOOTSTRAP))->getValue();
150  else
152 }
153 
154 /******************************************************************************/
155 
std::string * name_
Definition: Node.h:95
virtual bool hasName() const
Tell is this node has a name.
Definition: Node.h:267
virtual Clonable * getBranchProperty(const std::string &name)
Definition: Node.h:617
std::vector< Node * > sons_
Definition: Node.h:96
STL namespace.
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
Definition: Node.h:321
virtual bool hasBootstrapValue() const
Definition: Node.cpp:141
Node & operator=(const Node &node)
Assignation operator.
Definition: Node.cpp:72
double * distanceToFather_
Definition: Node.h:98
virtual bool hasFather() const
Tell if this node has a father node.
Definition: Node.h:379
std::vector< const Node * > getNeighbors() const
Definition: Node.cpp:114
std::map< std::string, Clonable * > nodeProperties_
Definition: Node.h:99
std::map< std::string, Clonable * > branchProperties_
Definition: Node.h:100
virtual size_t getSonPosition(const Node *son) const
Definition: Node.cpp:130
Exception thrown when something is wrong with a particular node.
The phylogenetic node class.
Definition: Node.h:90
Node * father_
Definition: Node.h:97
virtual void swap(size_t branch1, size_t branch2)
Definition: Node.cpp:98
General exception thrown if a property could not be found.
static const std::string BOOTSTRAP
Bootstrap tag.
Definition: TreeTools.h:723
int id_
Definition: Node.h:94
Node()
Build a new void Node object.
Definition: Node.h:106
virtual bool hasBranchProperty(const std::string &name) const
Definition: Node.h:678
virtual double getBootstrapValue() const
Definition: Node.cpp:146