bpp-phyl  2.2.0
Node.h
Go to the documentation of this file.
1 //
2 // File: Node.h
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 #ifndef _NODE_H_
41 #define _NODE_H_
42 
43 #include "TreeExceptions.h"
44 
45 #include <Bpp/Clonable.h>
46 #include <Bpp/Utils/MapTools.h>
47 #include <Bpp/BppString.h>
48 #include <Bpp/Numeric/Number.h>
49 
50 // From the STL:
51 #include <string>
52 #include <vector>
53 #include <map>
54 #include <iostream>
55 #include <algorithm>
56 #include <cstddef>
57 
58 namespace bpp
59 {
90 class Node
91 {
92 
93 protected:
94  int id_;
95  std::string* name_;
96  std::vector<Node*> sons_;
99  mutable std::map<std::string, Clonable*> nodeProperties_;
100  mutable std::map<std::string, Clonable*> branchProperties_;
101 
102 public:
106  Node() :
107  id_(0),
108  name_(0),
109  sons_(),
110  father_(0),
112  nodeProperties_(),
114  {}
115 
119  Node(int id) :
120  id_(id),
121  name_(0),
122  sons_(),
123  father_(0),
125  nodeProperties_(),
127  {}
128 
132  Node(const std::string& name) :
133  id_(0),
134  name_(new std::string(name)),
135  sons_(),
136  father_(0),
138  nodeProperties_(),
140  {}
141 
145  Node(int id, const std::string& name) :
146  id_(id),
147  name_(new std::string(name)),
148  sons_(),
149  father_(0),
151  nodeProperties_(),
153  {}
154 
162  Node(const Node& node);
163 
172  Node& operator=(const Node& node);
173 
174  Node* clone() const { return new Node(*this); }
175 
176 public:
177  virtual ~Node()
178  {
179  if (name_) delete name_;
181  for (std::map<std::string, Clonable*>::iterator i = nodeProperties_.begin(); i != nodeProperties_.end(); i++)
182  {
183  delete i->second;
184  }
185  for (std::map<std::string, Clonable*>::iterator i = branchProperties_.begin(); i != branchProperties_.end(); i++)
186  {
187  delete i->second;
188  }
189  }
190 
191 public:
203  virtual int getId() const { return id_; }
204 
210  virtual void setId(int id) { id_ = id; }
211 
212  virtual std::vector<int> getSonsId() const
213  {
214  std::vector<int> sonsId(sons_.size());
215  for (size_t i = 0; i < sons_.size(); i++)
216  {
217  sonsId[i] = sons_[i]->getId();
218  }
219  return sonsId;
220  }
221 
236  virtual std::string getName() const throw (NodePException)
237  {
238  if (!hasName()) throw NodePException("Node::getName: no name associated to this node.", this);
239  return *name_;
240  }
241 
247  virtual void setName(const std::string& name)
248  {
249  if (name_) delete name_;
250  name_ = new std::string(name);
251  }
252 
256  virtual void deleteName()
257  {
258  if (name_) delete name_;
259  name_ = 0;
260  }
261 
267  virtual bool hasName() const { return name_ != 0; }
268 
283  virtual double getDistanceToFather() const
284  {
285  if (!hasDistanceToFather())
286  throw NodePException("Node::getDistanceToFather: Node has no distance.", this);
287  return *distanceToFather_;
288  }
289 
299  virtual void setDistanceToFather(double distance)
300  {
301  if (distanceToFather_)
302  delete distanceToFather_;
303  distanceToFather_ = new double(distance);
304  }
305 
309  virtual void deleteDistanceToFather()
310  {
311  if (distanceToFather_)
312  delete distanceToFather_;
313  distanceToFather_ = 0;
314  }
315 
321  virtual bool hasDistanceToFather() const
322  {
323  return distanceToFather_ != 0;
324  }
325 
339  virtual const Node* getFather() const { return father_; }
340 
346  virtual Node* getFather() { return father_; }
347 
348  virtual int getFatherId() const { return father_->getId(); }
349 
355  virtual void setFather(Node* node) throw (NullPointerException)
356  {
357  if (!node)
358  throw NullPointerException("Node::setFather(). Empty node given as input.");
359  father_ = node;
360  if (find(node->sons_.begin(), node->sons_.end(), this) == node->sons_.end())
361  node->sons_.push_back(this);
362  else // Otherwise node is already present.
363  std::cerr << "DEVEL warning: Node::setFather. Son node already registered! No pb here, but could be a bug in your implementation..." << std::endl;
364  }
365 
369  virtual Node* removeFather()
370  {
371  Node* f = father_;
372  father_ = 0;
373  return f;
374  }
375 
379  virtual bool hasFather() const { return father_ != 0; }
380 
388  virtual size_t getNumberOfSons() const { return sons_.size(); }
389 
390  virtual std::vector<Node*>& getSons()
391  {
392  return sons_;
393  }
394 
395  virtual const Node* getSon(size_t pos) const throw (IndexOutOfBoundsException)
396  {
397  if (pos >= sons_.size()) throw IndexOutOfBoundsException("Node::getSon().", pos, 0, sons_.size() - 1);
398  return sons_[pos];
399  }
400 
401  virtual Node* getSon(size_t pos) throw (IndexOutOfBoundsException)
402  {
403  if (pos >= sons_.size()) throw IndexOutOfBoundsException("Node::getSon().", pos, 0, sons_.size() - 1);
404  return sons_[pos];
405  }
406 
407  virtual void addSon(size_t pos, Node* node) throw (NullPointerException, NodePException)
408  {
409  if (!node)
410  throw NullPointerException("Node::addSon(). Empty node given as input.");
411  if (find(sons_.begin(), sons_.end(), node) == sons_.end())
412  sons_.insert(sons_.begin() + static_cast<ptrdiff_t>(pos), node);
413  else // Otherwise node is already present.
414  std::cerr << "DEVEL warning: Node::addSon. Son node already registered! No pb here, but could be a bug in your implementation..." << std::endl;
415 
416  node->father_ = this;
417  }
418 
419  virtual void addSon(Node* node) throw (NullPointerException, NodePException)
420  {
421  if (!node)
422  throw NullPointerException("Node::addSon(). Empty node given as input.");
423  if (find(sons_.begin(), sons_.end(), node) == sons_.end())
424  sons_.push_back(node);
425  else // Otherwise node is already present.
426  throw NodePException("Node::addSon. Trying to add a node which is already present.");
427  node->father_ = this;
428  }
429 
430  virtual void setSon(size_t pos, Node* node) throw (IndexOutOfBoundsException, NullPointerException, NodePException)
431  {
432  if (!node)
433  throw NullPointerException("Node::setSon(). Empty node given as input.");
434  if (pos >= sons_.size())
435  throw IndexOutOfBoundsException("Node::setSon(). Invalid node position.", pos, 0, sons_.size() - 1);
436  std::vector<Node*>::iterator search = find(sons_.begin(), sons_.end(), node);
437  if (search == sons_.end() || search == sons_.begin() + static_cast<ptrdiff_t>(pos))
438  sons_[pos] = node;
439  else
440  throw NodePException("Node::setSon. Trying to set a node which is already present.");
441  node->father_ = this;
442  }
443 
444  virtual Node* removeSon(size_t pos) throw (IndexOutOfBoundsException)
445  {
446  if (pos >= sons_.size())
447  throw IndexOutOfBoundsException("Node::removeSon(). Invalid node position.", pos, 0, sons_.size() - 1);
448  Node* node = sons_[pos];
449  sons_.erase(sons_.begin() + static_cast<ptrdiff_t>(pos));
450  node->removeFather();
451  return node;
452  }
453 
454  virtual void removeSon(Node* node) throw (NodeNotFoundException, NullPointerException)
455  {
456  if (!node)
457  throw NullPointerException("Node::removeSon(). Empty node given as input.");
458  for (size_t i = 0; i < sons_.size(); i++)
459  {
460  if (sons_[i] == node)
461  {
462  sons_.erase(sons_.begin() + static_cast<ptrdiff_t>(i));
463  node->removeFather();
464  return;
465  }
466  }
467  throw NodeNotFoundException("Node::removeSon.", node->getId());
468  }
469 
470  virtual void removeSons()
471  {
472  while (sons_.size() != 0)
473  removeSon(static_cast<size_t>(0));
474  }
475 
476  virtual void swap(size_t branch1, size_t branch2) throw (IndexOutOfBoundsException);
477 
478  virtual size_t getSonPosition(const Node* son) const throw (NodeNotFoundException, NullPointerException);
479 
482  // These functions must not be declared as virtual!!
483 
484  std::vector<const Node*> getNeighbors() const;
485 
486  std::vector<Node*> getNeighbors();
487 
488  virtual size_t degree() const { return getNumberOfSons() + (hasFather() ? 1 : 0); }
489 
498  Node* operator[](int i) { return (i < 0) ? father_ : sons_[static_cast<size_t>(i)]; }
499 
500  const Node* operator[](int i) const { return (i < 0) ? father_ : sons_[static_cast<size_t>(i)]; }
501 
520  virtual void setNodeProperty(const std::string& name, const Clonable& property)
521  {
522  if (hasNodeProperty(name))
523  delete nodeProperties_[name];
524  nodeProperties_[name] = property.clone();
525  }
526 
527  virtual Clonable* getNodeProperty(const std::string& name) throw (PropertyNotFoundException)
528  {
529  if (hasNodeProperty(name))
530  return nodeProperties_[name];
531  else
532  throw PropertyNotFoundException("", name, this);
533  }
534 
535  virtual const Clonable* getNodeProperty(const std::string& name) const throw (PropertyNotFoundException)
536  {
537  if (hasNodeProperty(name))
538  return const_cast<const Clonable*>(nodeProperties_[name]);
539  else
540  throw PropertyNotFoundException("", name, this);
541  }
542 
543  virtual Clonable* removeNodeProperty(const std::string& name) throw (PropertyNotFoundException)
544  {
545  if (hasNodeProperty(name))
546  {
547  Clonable* removed = nodeProperties_[name];
548  nodeProperties_.erase(name);
549  return removed;
550  }
551  else
552  throw PropertyNotFoundException("", name, this);
553  }
554 
555  virtual void deleteNodeProperty(const std::string& name) throw (PropertyNotFoundException)
556  {
557  if (hasNodeProperty(name))
558  {
559  delete nodeProperties_[name];
560  nodeProperties_.erase(name);
561  }
562  else
563  throw PropertyNotFoundException("", name, this);
564  }
565 
571  virtual void removeNodeProperties()
572  {
573  nodeProperties_.clear();
574  }
575 
579  virtual void deleteNodeProperties()
580  {
581  for (std::map<std::string, Clonable*>::iterator i = nodeProperties_.begin(); i != nodeProperties_.end(); i++)
582  {
583  delete i->second;
584  }
585  nodeProperties_.clear();
586  }
587 
588  virtual bool hasNodeProperty(const std::string& name) const { return nodeProperties_.find(name) != nodeProperties_.end(); }
589 
590  virtual std::vector<std::string> getNodePropertyNames() const { return MapTools::getKeys(nodeProperties_); }
591 
610  virtual void setBranchProperty(const std::string& name, const Clonable& property)
611  {
612  if (hasBranchProperty(name))
613  delete branchProperties_[name];
614  branchProperties_[name] = property.clone();
615  }
616 
617  virtual Clonable* getBranchProperty(const std::string& name) throw (PropertyNotFoundException)
618  {
619  if (hasBranchProperty(name))
620  return branchProperties_[name];
621  else
622  throw PropertyNotFoundException("", name, this);
623  }
624 
625  virtual const Clonable* getBranchProperty(const std::string& name) const throw (PropertyNotFoundException)
626  {
627  if (hasBranchProperty(name))
628  return const_cast<const Clonable*>(branchProperties_[name]);
629  else
630  throw PropertyNotFoundException("", name, this);
631  }
632 
633  virtual Clonable* removeBranchProperty(const std::string& name) throw (PropertyNotFoundException)
634  {
635  if (hasBranchProperty(name))
636  {
637  Clonable* removed = branchProperties_[name];
638  branchProperties_.erase(name);
639  return removed;
640  }
641  else
642  throw PropertyNotFoundException("", name, this);
643  }
644 
645  virtual void deleteBranchProperty(const std::string& name) throw (PropertyNotFoundException)
646  {
647  if (hasBranchProperty(name))
648  {
649  delete branchProperties_[name];
650  branchProperties_.erase(name);
651  }
652  else
653  throw PropertyNotFoundException("", name, this);
654  }
655 
661  virtual void removeBranchProperties()
662  {
663  branchProperties_.clear();
664  }
665 
669  virtual void deleteBranchProperties()
670  {
671  for (std::map<std::string, Clonable*>::iterator i = branchProperties_.begin(); i != branchProperties_.end(); i++)
672  {
673  delete i->second;
674  }
675  branchProperties_.clear();
676  }
677 
678  virtual bool hasBranchProperty(const std::string& name) const { return branchProperties_.find(name) != branchProperties_.end(); }
679 
680  virtual std::vector<std::string> getBranchPropertyNames() const { return MapTools::getKeys(branchProperties_); }
681 
682  virtual bool hasBootstrapValue() const;
683 
684  virtual double getBootstrapValue() const throw (PropertyNotFoundException);
686  // Equality operator:
687 
688  virtual bool operator==(const Node& node) const { return id_ == node.id_; }
689 
690  // Tests:
691 
692  virtual bool isLeaf() const { return degree() <= 1; }
693 
694 };
695 } // end of namespace bpp.
696 
697 #endif // _NODE_H_
698 
virtual void addSon(size_t pos, Node *node)
Definition: Node.h:407
virtual std::vector< std::string > getBranchPropertyNames() const
Definition: Node.h:680
std::string * name_
Definition: Node.h:95
virtual bool hasNodeProperty(const std::string &name) const
Definition: Node.h:588
virtual std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
Definition: Node.h:236
virtual void removeSons()
Definition: Node.h:470
virtual bool hasName() const
Tell is this node has a name.
Definition: Node.h:267
virtual void deleteBranchProperties()
Delete all branch properties.
Definition: Node.h:669
virtual Clonable * removeBranchProperty(const std::string &name)
Definition: Node.h:633
virtual std::vector< int > getSonsId() const
Definition: Node.h:212
virtual void deleteNodeProperty(const std::string &name)
Definition: Node.h:555
virtual void setSon(size_t pos, Node *node)
Definition: Node.h:430
virtual Clonable * getBranchProperty(const std::string &name)
Definition: Node.h:617
std::vector< Node * > sons_
Definition: Node.h:96
virtual const Node * getSon(size_t pos) const
Definition: Node.h:395
Node * operator[](int i)
Definition: Node.h:498
STL namespace.
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
Definition: Node.h:321
virtual const Clonable * getBranchProperty(const std::string &name) const
Definition: Node.h:625
Node(const std::string &name)
Build a new Node with specified name.
Definition: Node.h:132
virtual const Node * getFather() const
Get the father of this node is there is one.
Definition: Node.h:339
virtual int getFatherId() const
Definition: Node.h:348
Node * clone() const
Definition: Node.h:174
virtual void removeNodeProperties()
Remove all node properties.
Definition: Node.h:571
virtual ~Node()
Definition: Node.h:177
virtual bool hasBootstrapValue() const
Definition: Node.cpp:141
virtual void deleteBranchProperty(const std::string &name)
Definition: Node.h:645
virtual void setName(const std::string &name)
Give a name or update the name associated to the node.
Definition: Node.h:247
virtual void deleteNodeProperties()
Delete all node properties.
Definition: Node.h:579
Node & operator=(const Node &node)
Assignation operator.
Definition: Node.cpp:72
double * distanceToFather_
Definition: Node.h:98
virtual void addSon(Node *node)
Definition: Node.h:419
virtual bool hasFather() const
Tell if this node has a father node.
Definition: Node.h:379
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
Node(int id)
Build a new Node with specified id.
Definition: Node.h:119
virtual int getId() const
Get the node&#39;s id.
Definition: Node.h:203
virtual Node * removeFather()
Remove the father of this node.
Definition: Node.h:369
std::vector< const Node * > getNeighbors() const
Definition: Node.cpp:114
virtual Node * removeSon(size_t pos)
Definition: Node.h:444
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.
virtual void removeSon(Node *node)
Definition: Node.h:454
The phylogenetic node class.
Definition: Node.h:90
virtual void setId(int id)
Set this node&#39;s id.
Definition: Node.h:210
virtual Clonable * getNodeProperty(const std::string &name)
Definition: Node.h:527
Node * father_
Definition: Node.h:97
virtual void swap(size_t branch1, size_t branch2)
Definition: Node.cpp:98
Node(int id, const std::string &name)
Build a new Node with specified id and name.
Definition: Node.h:145
General exception thrown if a property could not be found.
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
Definition: Node.h:299
virtual size_t getNumberOfSons() const
Definition: Node.h:388
virtual const Clonable * getNodeProperty(const std::string &name) const
Definition: Node.h:535
virtual void setFather(Node *node)
Set the father node of this node.
Definition: Node.h:355
virtual Node * getSon(size_t pos)
Definition: Node.h:401
General exception thrown when something is wrong with a particular node.
virtual std::vector< Node * > & getSons()
Definition: Node.h:390
int id_
Definition: Node.h:94
const Node * operator[](int i) const
Definition: Node.h:500
virtual void deleteName()
Delete the name associated to this node (do nothing if there is no name).
Definition: Node.h:256
virtual size_t degree() const
Definition: Node.h:488
virtual void removeBranchProperties()
Remove all branch properties.
Definition: Node.h:661
Node()
Build a new void Node object.
Definition: Node.h:106
virtual Clonable * removeNodeProperty(const std::string &name)
Definition: Node.h:543
virtual void deleteDistanceToFather()
Delete the distance to the father node.
Definition: Node.h:309
virtual Node * getFather()
Get the father of this node is there is one.
Definition: Node.h:346
virtual std::vector< std::string > getNodePropertyNames() const
Definition: Node.h:590
virtual bool hasBranchProperty(const std::string &name) const
Definition: Node.h:678
virtual void setBranchProperty(const std::string &name, const Clonable &property)
Set/add a branch property.
Definition: Node.h:610
virtual void setNodeProperty(const std::string &name, const Clonable &property)
Set/add a node property.
Definition: Node.h:520
virtual double getBootstrapValue() const
Definition: Node.cpp:146