bpp-phyl3  3.0.0
Node.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_TREE_NODE_H
6 #define BPP_PHYL_TREE_NODE_H
7 
8 #include <Bpp/BppString.h>
9 #include <Bpp/Clonable.h>
10 #include <Bpp/Numeric/Number.h>
11 #include <Bpp/Utils/MapTools.h>
12 
13 #include "TreeExceptions.h"
14 
15 // From the STL:
16 #include <string>
17 #include <vector>
18 #include <map>
19 #include <iostream>
20 #include <algorithm>
21 #include <cstddef>
22 #include <memory>
23 
24 namespace bpp
25 {
26  class PhyloTree;
27  class PhyloNode;
58 class Node
59 {
60 protected:
61  int id_;
62  std::string* name_;
63  std::vector<Node*> sons_;
66  mutable std::map<std::string, Clonable*> nodeProperties_;
67  mutable std::map<std::string, Clonable*> branchProperties_;
68 
69 public:
73  Node() :
74  id_(0),
75  name_(0),
76  sons_(),
77  father_(0),
81  {}
82 
86  Node(int id) :
87  id_(id),
88  name_(0),
89  sons_(),
90  father_(0),
94  {}
95 
99  Node(const std::string& name) :
100  id_(0),
101  name_(new std::string(name)),
102  sons_(),
103  father_(0),
105  nodeProperties_(),
107  {}
108 
112  Node(int id, const std::string& name) :
113  id_(id),
114  name_(new std::string(name)),
115  sons_(),
116  father_(0),
118  nodeProperties_(),
120  {}
121 
129  Node(const Node& node);
130 
139  Node& operator=(const Node& node);
140 
141  Node* clone() const { return new Node(*this); }
142 
143 public:
144  virtual ~Node()
145  {
146  if (name_) delete name_;
148  for (std::map<std::string, Clonable*>::iterator i = nodeProperties_.begin(); i != nodeProperties_.end(); i++)
149  {
150  delete i->second;
151  }
152  for (std::map<std::string, Clonable*>::iterator i = branchProperties_.begin(); i != branchProperties_.end(); i++)
153  {
154  delete i->second;
155  }
156  }
157 
158 public:
170  virtual int getId() const { return id_; }
171 
177  virtual void setId(int id) { id_ = id; }
178 
179  virtual std::vector<int> getSonsId() const
180  {
181  std::vector<int> sonsId(sons_.size());
182  for (size_t i = 0; i < sons_.size(); i++)
183  {
184  sonsId[i] = sons_[i]->getId();
185  }
186  return sonsId;
187  }
188 
203  virtual std::string getName() const
204  {
205  if (!hasName()) throw NodePException("Node::getName: no name associated to this node.", this);
206  return *name_;
207  }
208 
214  virtual void setName(const std::string& name)
215  {
216  if (name_) delete name_;
217  name_ = new std::string(name);
218  }
219 
223  virtual void deleteName()
224  {
225  if (name_) delete name_;
226  name_ = 0;
227  }
228 
234  virtual bool hasName() const { return name_ != 0; }
235 
250  virtual double getDistanceToFather() const
251  {
252  if (!hasDistanceToFather())
253  throw NodePException("Node::getDistanceToFather: Node has no distance.", this);
254  return *distanceToFather_;
255  }
256 
266  virtual void setDistanceToFather(double distance)
267  {
268  if (distanceToFather_)
269  delete distanceToFather_;
270  distanceToFather_ = new double(distance);
271  }
272 
276  virtual void deleteDistanceToFather()
277  {
278  if (distanceToFather_)
279  delete distanceToFather_;
280  distanceToFather_ = 0;
281  }
282 
288  virtual bool hasDistanceToFather() const
289  {
290  return distanceToFather_ != 0;
291  }
292 
306  virtual const Node* getFather() const { return father_; }
307 
313  virtual Node* getFather() { return father_; }
314 
315  virtual int getFatherId() const { return father_->getId(); }
316 
322  virtual void setFather(Node* node)
323  {
324  if (!node)
325  throw NullPointerException("Node::setFather(). Empty node given as input.");
326  father_ = node;
327  if (find(node->sons_.begin(), node->sons_.end(), this) == node->sons_.end())
328  node->sons_.push_back(this);
329  else // Otherwise node is already present.
330  std::cerr << "DEVEL warning: Node::setFather. Son node already registered! No pb here, but could be a bug in your implementation..." << std::endl;
331  }
332 
336  virtual Node* removeFather()
337  {
338  Node* f = father_;
339  father_ = 0;
340  return f;
341  }
342 
346  virtual bool hasFather() const { return father_ != 0; }
347 
355  virtual size_t getNumberOfSons() const { return sons_.size(); }
356 
357  virtual std::vector<Node*>& getSons()
358  {
359  return sons_;
360  }
361 
362  virtual const Node* getSon(size_t pos) const
363  {
364  if (pos >= sons_.size()) throw IndexOutOfBoundsException("Node::getSon().", pos, 0, sons_.size() - 1);
365  return sons_[pos];
366  }
367 
368  virtual Node* getSon(size_t pos)
369  {
370  if (pos >= sons_.size()) throw IndexOutOfBoundsException("Node::getSon().", pos, 0, sons_.size() - 1);
371  return sons_[pos];
372  }
373 
374  virtual void addSon(size_t pos, Node* node)
375  {
376  if (!node)
377  throw NullPointerException("Node::addSon(). Empty node given as input.");
378  if (find(sons_.begin(), sons_.end(), node) == sons_.end())
379  sons_.insert(sons_.begin() + static_cast<ptrdiff_t>(pos), node);
380  else // Otherwise node is already present.
381  std::cerr << "DEVEL warning: Node::addSon. Son node already registered! No pb here, but could be a bug in your implementation..." << std::endl;
382 
383  node->father_ = this;
384  }
385 
386  virtual void addSon(Node* node)
387  {
388  if (!node)
389  throw NullPointerException("Node::addSon(). Empty node given as input.");
390  if (find(sons_.begin(), sons_.end(), node) == sons_.end())
391  sons_.push_back(node);
392  else // Otherwise node is already present.
393  throw NodePException("Node::addSon. Trying to add a node which is already present.");
394  node->father_ = this;
395  }
396 
397  virtual void setSon(size_t pos, Node* node)
398  {
399  if (!node)
400  throw NullPointerException("Node::setSon(). Empty node given as input.");
401  if (pos >= sons_.size())
402  throw IndexOutOfBoundsException("Node::setSon(). Invalid node position.", pos, 0, sons_.size() - 1);
403  std::vector<Node*>::iterator search = find(sons_.begin(), sons_.end(), node);
404  if (search == sons_.end() || search == sons_.begin() + static_cast<ptrdiff_t>(pos))
405  sons_[pos] = node;
406  else
407  throw NodePException("Node::setSon. Trying to set a node which is already present.");
408  node->father_ = this;
409  }
410 
411  virtual Node* removeSon(size_t pos)
412  {
413  if (pos >= sons_.size())
414  throw IndexOutOfBoundsException("Node::removeSon(). Invalid node position.", pos, 0, sons_.size() - 1);
415  Node* node = sons_[pos];
416  sons_.erase(sons_.begin() + static_cast<ptrdiff_t>(pos));
417  node->removeFather();
418  return node;
419  }
420 
421  virtual void removeSon(Node* node)
422  {
423  if (!node)
424  throw NullPointerException("Node::removeSon(). Empty node given as input.");
425  for (size_t i = 0; i < sons_.size(); i++)
426  {
427  if (sons_[i] == node)
428  {
429  sons_.erase(sons_.begin() + static_cast<ptrdiff_t>(i));
430  node->removeFather();
431  return;
432  }
433  }
434  throw NodeNotFoundException("Node::removeSon.", node->getId());
435  }
436 
437  virtual void removeSons()
438  {
439  while (sons_.size() != 0)
440  removeSon(static_cast<size_t>(0));
441  }
442 
443  virtual void swap(size_t branch1, size_t branch2);
444 
445  virtual size_t getSonPosition(const Node* son) const;
446 
449  // From PhyloNode
450 
451  /*
452  * Add PhyloNode tree as Son of this Node
453  */
454 
455  void addSubTree(const PhyloTree& tree, std::shared_ptr<PhyloNode> phyloNode);
456 
457  // These functions must not be declared as virtual!!
458 
459  std::vector<const Node*> getNeighbors() const;
460 
461  std::vector<Node*> getNeighbors();
462 
463  virtual size_t degree() const { return getNumberOfSons() + (hasFather() ? 1 : 0); }
464 
473  Node* operator[](int i) { return (i < 0) ? father_ : sons_[static_cast<size_t>(i)]; }
474 
475  const Node* operator[](int i) const { return (i < 0) ? father_ : sons_[static_cast<size_t>(i)]; }
476 
495  virtual void setNodeProperty(const std::string& name, const Clonable& property)
496  {
497  if (hasNodeProperty(name))
498  delete nodeProperties_[name];
499  nodeProperties_[name] = property.clone();
500  }
501 
502  virtual Clonable* getNodeProperty(const std::string& name)
503  {
504  if (hasNodeProperty(name))
505  return nodeProperties_[name];
506  else
507  throw PropertyNotFoundException("", name, this);
508  }
509 
510  virtual const Clonable* getNodeProperty(const std::string& name) const
511  {
512  if (hasNodeProperty(name))
513  return const_cast<const Clonable*>(nodeProperties_[name]);
514  else
515  throw PropertyNotFoundException("", name, this);
516  }
517 
518  virtual Clonable* removeNodeProperty(const std::string& name)
519  {
520  if (hasNodeProperty(name))
521  {
522  Clonable* removed = nodeProperties_[name];
523  nodeProperties_.erase(name);
524  return removed;
525  }
526  else
527  throw PropertyNotFoundException("", name, this);
528  }
529 
530  virtual void deleteNodeProperty(const std::string& name)
531  {
532  if (hasNodeProperty(name))
533  {
534  delete nodeProperties_[name];
535  nodeProperties_.erase(name);
536  }
537  else
538  throw PropertyNotFoundException("", name, this);
539  }
540 
546  virtual void removeNodeProperties()
547  {
548  nodeProperties_.clear();
549  }
550 
554  virtual void deleteNodeProperties()
555  {
556  for (std::map<std::string, Clonable*>::iterator i = nodeProperties_.begin(); i != nodeProperties_.end(); i++)
557  {
558  delete i->second;
559  }
560  nodeProperties_.clear();
561  }
562 
563  virtual bool hasNodeProperty(const std::string& name) const { return nodeProperties_.find(name) != nodeProperties_.end(); }
564 
565  virtual std::vector<std::string> getNodePropertyNames() const { return MapTools::getKeys(nodeProperties_); }
566 
585  virtual void setBranchProperty(const std::string& name, const Clonable& property)
586  {
587  if (hasBranchProperty(name))
588  delete branchProperties_[name];
589  branchProperties_[name] = property.clone();
590  }
591 
592  virtual Clonable* getBranchProperty(const std::string& name)
593  {
594  if (hasBranchProperty(name))
595  return branchProperties_[name];
596  else
597  throw PropertyNotFoundException("", name, this);
598  }
599 
600  virtual const Clonable* getBranchProperty(const std::string& name) const
601  {
602  if (hasBranchProperty(name))
603  return const_cast<const Clonable*>(branchProperties_[name]);
604  else
605  throw PropertyNotFoundException("", name, this);
606  }
607 
608  virtual Clonable* removeBranchProperty(const std::string& name)
609  {
610  if (hasBranchProperty(name))
611  {
612  Clonable* removed = branchProperties_[name];
613  branchProperties_.erase(name);
614  return removed;
615  }
616  else
617  throw PropertyNotFoundException("", name, this);
618  }
619 
620  virtual void deleteBranchProperty(const std::string& name)
621  {
622  if (hasBranchProperty(name))
623  {
624  delete branchProperties_[name];
625  branchProperties_.erase(name);
626  }
627  else
628  throw PropertyNotFoundException("", name, this);
629  }
630 
636  virtual void removeBranchProperties()
637  {
638  branchProperties_.clear();
639  }
640 
644  virtual void deleteBranchProperties()
645  {
646  for (std::map<std::string, Clonable*>::iterator i = branchProperties_.begin(); i != branchProperties_.end(); i++)
647  {
648  delete i->second;
649  }
650  branchProperties_.clear();
651  }
652 
653  virtual bool hasBranchProperty(const std::string& name) const { return branchProperties_.find(name) != branchProperties_.end(); }
654 
655  virtual std::vector<std::string> getBranchPropertyNames() const { return MapTools::getKeys(branchProperties_); }
656 
657  virtual bool hasBootstrapValue() const;
658 
659  virtual double getBootstrapValue() const;
661  // Equality operator:
662 
663  virtual bool operator==(const Node& node) const { return id_ == node.id_; }
664 
665  // Tests:
666 
667  virtual bool isLeaf() const { return degree() <= 1; }
668 
669  virtual bool hasNoSon() const { return getNumberOfSons() == 0; }
670 };
671 } // end of namespace bpp.
672 #endif // BPP_PHYL_TREE_NODE_H
static std::vector< Key > getKeys(const std::map< Key, T, Cmp > &myMap)
Exception thrown when something is wrong with a particular node.
General exception thrown when something is wrong with a particular node.
The phylogenetic node class.
Definition: Node.h:59
Node(int id)
Build a new Node with specified id.
Definition: Node.h:86
virtual void deleteNodeProperties()
Delete all node properties.
Definition: Node.h:554
virtual void setSon(size_t pos, Node *node)
Definition: Node.h:397
virtual void deleteName()
Delete the name associated to this node (do nothing if there is no name).
Definition: Node.h:223
Node * father_
Definition: Node.h:64
virtual std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
Definition: Node.h:203
Node(const std::string &name)
Build a new Node with specified name.
Definition: Node.h:99
virtual Clonable * getNodeProperty(const std::string &name)
Definition: Node.h:502
Node * clone() const
Definition: Node.h:141
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
Definition: Node.h:266
virtual Node * removeSon(size_t pos)
Definition: Node.h:411
virtual void deleteDistanceToFather()
Delete the distance to the father node.
Definition: Node.h:276
int id_
Definition: Node.h:61
virtual int getId() const
Get the node's id.
Definition: Node.h:170
virtual bool hasBranchProperty(const std::string &name) const
Definition: Node.h:653
virtual void setId(int id)
Set this node's id.
Definition: Node.h:177
std::map< std::string, Clonable * > branchProperties_
Definition: Node.h:67
virtual const Clonable * getNodeProperty(const std::string &name) const
Definition: Node.h:510
virtual void addSon(size_t pos, Node *node)
Definition: Node.h:374
Node(int id, const std::string &name)
Build a new Node with specified id and name.
Definition: Node.h:112
virtual std::vector< Node * > & getSons()
Definition: Node.h:357
Node & operator=(const Node &node)
Assignation operator.
Definition: Node.cpp:42
virtual ~Node()
Definition: Node.h:144
virtual bool hasBootstrapValue() const
Definition: Node.cpp:163
virtual const Node * getSon(size_t pos) const
Definition: Node.h:362
virtual void setBranchProperty(const std::string &name, const Clonable &property)
Set/add a branch property.
Definition: Node.h:585
virtual const Node * getFather() const
Get the father of this node is there is one.
Definition: Node.h:306
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
Definition: Node.h:288
virtual Node * getFather()
Get the father of this node is there is one.
Definition: Node.h:313
Node()
Build a new void Node object.
Definition: Node.h:73
std::vector< Node * > sons_
Definition: Node.h:63
virtual Clonable * removeBranchProperty(const std::string &name)
Definition: Node.h:608
virtual void setNodeProperty(const std::string &name, const Clonable &property)
Set/add a node property.
Definition: Node.h:495
virtual Clonable * removeNodeProperty(const std::string &name)
Definition: Node.h:518
virtual void removeSons()
Definition: Node.h:437
virtual bool hasNoSon() const
Definition: Node.h:669
virtual const Clonable * getBranchProperty(const std::string &name) const
Definition: Node.h:600
virtual void removeSon(Node *node)
Definition: Node.h:421
Node * operator[](int i)
Definition: Node.h:473
std::string * name_
Definition: Node.h:62
virtual Node * getSon(size_t pos)
Definition: Node.h:368
virtual bool isLeaf() const
Definition: Node.h:667
virtual size_t getSonPosition(const Node *son) const
Definition: Node.cpp:151
virtual void deleteNodeProperty(const std::string &name)
Definition: Node.h:530
void addSubTree(const PhyloTree &tree, std::shared_ptr< PhyloNode > phyloNode)
Definition: Node.cpp:72
virtual bool operator==(const Node &node) const
Definition: Node.h:663
virtual std::vector< int > getSonsId() const
Definition: Node.h:179
std::vector< const Node * > getNeighbors() const
Definition: Node.cpp:127
virtual Clonable * getBranchProperty(const std::string &name)
Definition: Node.h:592
virtual void swap(size_t branch1, size_t branch2)
Definition: Node.cpp:111
virtual void removeNodeProperties()
Remove all node properties.
Definition: Node.h:546
virtual Node * removeFather()
Remove the father of this node.
Definition: Node.h:336
virtual void deleteBranchProperty(const std::string &name)
Definition: Node.h:620
virtual bool hasFather() const
Tell if this node has a father node.
Definition: Node.h:346
virtual void setName(const std::string &name)
Give a name or update the name associated to the node.
Definition: Node.h:214
double * distanceToFather_
Definition: Node.h:65
const Node * operator[](int i) const
Definition: Node.h:475
virtual double getBootstrapValue() const
Definition: Node.cpp:168
virtual void deleteBranchProperties()
Delete all branch properties.
Definition: Node.h:644
virtual std::vector< std::string > getBranchPropertyNames() const
Definition: Node.h:655
virtual void addSon(Node *node)
Definition: Node.h:386
virtual bool hasName() const
Tell is this node has a name.
Definition: Node.h:234
virtual std::vector< std::string > getNodePropertyNames() const
Definition: Node.h:565
virtual void setFather(Node *node)
Set the father node of this node.
Definition: Node.h:322
virtual bool hasNodeProperty(const std::string &name) const
Definition: Node.h:563
std::map< std::string, Clonable * > nodeProperties_
Definition: Node.h:66
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
Definition: Node.h:250
virtual void removeBranchProperties()
Remove all branch properties.
Definition: Node.h:636
virtual int getFatherId() const
Definition: Node.h:315
virtual size_t getNumberOfSons() const
Definition: Node.h:355
virtual size_t degree() const
Definition: Node.h:463
General exception thrown if a property could not be found.
Defines the basic types of data flow nodes.