bpp-phyl3  3.0.0
Node.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <Bpp/Exceptions.h>
6 #include <Bpp/Text/TextTools.h>
7 
8 #include "Node.h"
9 #include "TreeTools.h"
10 #include "PhyloTree.h"
11 
12 using namespace bpp;
13 
14 // from the STL:
15 #include <algorithm>
16 #include <iostream>
17 
18 using namespace std;
19 
22 Node::Node(const Node& node) :
23  id_(node.id_), name_(0),
24  sons_(), father_(0),
25  // , sons_(node.sons_), father_(node.father_),
26  distanceToFather_(0), nodeProperties_(), branchProperties_()
27 {
28  name_ = node.hasName() ? new string(*node.name_) : 0;
29  distanceToFather_ = node.hasDistanceToFather() ? new double(*node.distanceToFather_) : 0;
30  for (map<string, Clonable*>::iterator i = node.nodeProperties_.begin(); i != node.nodeProperties_.end(); i++)
31  {
32  nodeProperties_[i->first] = i->second->clone();
33  }
34  for (map<string, Clonable*>::iterator i = node.branchProperties_.begin(); i != node.branchProperties_.end(); i++)
35  {
36  branchProperties_[i->first] = i->second->clone();
37  }
38 }
39 
42 Node& Node::operator=(const Node& node)
43 {
44  id_ = node.id_;
45  if (name_)
46  delete name_;
47  name_ = node.hasName() ? new string(*node.name_) : 0;
48  // father_ = node.father_;
50  delete distanceToFather_;
51  distanceToFather_ = node.hasDistanceToFather() ? new double(*node.distanceToFather_) : 0;
52  // sons_ = node.sons_;
53  for (map<string, Clonable*>::iterator i = node.nodeProperties_.begin(); i != node.nodeProperties_.end(); i++)
54  {
55  Clonable* p = nodeProperties_[i->first];
56  if (p)
57  delete p;
58  nodeProperties_[i->first] = i->second->clone();
59  }
60  for (map<string, Clonable*>::iterator i = node.branchProperties_.begin(); i != node.branchProperties_.end(); i++)
61  {
62  Clonable* p = branchProperties_[i->first];
63  if (p)
64  delete p;
65  branchProperties_[i->first] = i->second->clone();
66  }
67  return *this;
68 }
69 
72 void Node::addSubTree(const PhyloTree& tree, std::shared_ptr<PhyloNode> phyloNode)
73 {
74  // set Id
75  setId(tree.getNodeIndex(phyloNode));
76 
77  // features of the node
78  auto vnames = phyloNode->getPropertyNames();
79  for (const auto& name:vnames)
80  setNodeProperty(name, *phyloNode->getProperty(name));
81 
82  if (phyloNode->hasName())
83  setName(phyloNode->getName());
84 
85  // look at sons
86  vector<shared_ptr<PhyloNode>> sons = tree.getSons(phyloNode);
87  for (auto& son : sons)
88  {
89  Node* s1 = new Node();
90 
91  // features of the branch
92  const auto phylobranch = tree.getIncomingEdges(son)[0]; // Tree -> one incoming edge
93 
94  if (phylobranch->hasLength())
95  s1->setDistanceToFather(phylobranch->getLength());
96 
97  auto vnames2 = phylobranch->getPropertyNames();
98  for (const auto& name:vnames2)
99  {
100  s1->setBranchProperty(name, *phylobranch->getProperty(name));
101  }
102 
103  // recursive
104  s1->addSubTree(tree, son);
105 
106  // addSon
107  addSon(s1);
108  }
109 }
110 
111 void Node::swap(size_t branch1, size_t branch2)
112 {
113  if (branch1 > branch2)
114  {
115  size_t tmp = branch1;
116  branch1 = branch2;
117  branch2 = tmp;
118  }
119  Node* node1 = getSon(branch1);
120  Node* node2 = getSon(branch2);
121  removeSon(node1);
122  removeSon(node2);
123  addSon(branch1, node2);
124  addSon(branch2, node1);
125 }
126 
127 vector<const Node*> Node::getNeighbors() const
128 {
129  vector<const Node*> neighbors;
130  if (hasFather())
131  neighbors.push_back(father_);
132  for (size_t i = 0; i < sons_.size(); i++)
133  {
134  neighbors.push_back(sons_[i]);
135  }
136  return neighbors;
137 }
138 
139 vector<Node*> Node::getNeighbors()
140 {
141  vector<Node*> neighbors;
142  if (hasFather())
143  neighbors.push_back(father_);
144  for (size_t i = 0; i < sons_.size(); i++)
145  {
146  neighbors.push_back(sons_[i]);
147  }
148  return neighbors;
149 }
150 
151 size_t Node::getSonPosition(const Node* son) const
152 {
153  if (!son)
154  throw NullPointerException("Node::getSonPosition(). Empty node given as input.");
155  for (size_t i = 0; i < sons_.size(); i++)
156  {
157  if (sons_[i] == son)
158  return i;
159  }
160  throw NodeNotFoundException("Son not found", TextTools::toString(son->getId()));
161 }
162 
164 {
166 }
167 
169 {
171  return dynamic_cast<const Number<double>*>(getBranchProperty(TreeTools::BOOTSTRAP))->getValue();
172  else
174 }
175 
176 /******************************************************************************/
virtual std::vector< std::shared_ptr< E > > getIncomingEdges(const std::shared_ptr< N > node) const=0
virtual NodeIndex getNodeIndex(const std::shared_ptr< N > nodeObject) const=0
std::vector< std::shared_ptr< N > > getSons(const std::shared_ptr< N > node) const
Exception thrown when something is wrong with a particular node.
The phylogenetic node class.
Definition: Node.h:59
Node * father_
Definition: Node.h:64
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
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 void addSon(size_t pos, Node *node)
Definition: Node.h:374
Node & operator=(const Node &node)
Assignation operator.
Definition: Node.cpp:42
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 bool hasDistanceToFather() const
Tell is this node has a distance to the father.
Definition: Node.h:288
Node()
Build a new void Node object.
Definition: Node.h:73
std::vector< Node * > sons_
Definition: Node.h:63
virtual void setNodeProperty(const std::string &name, const Clonable &property)
Set/add a node property.
Definition: Node.h:495
std::string * name_
Definition: Node.h:62
virtual size_t getSonPosition(const Node *son) const
Definition: Node.cpp:151
void addSubTree(const PhyloTree &tree, std::shared_ptr< PhyloNode > phyloNode)
Definition: Node.cpp:72
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 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
virtual double getBootstrapValue() const
Definition: Node.cpp:168
virtual bool hasName() const
Tell is this node has a name.
Definition: Node.h:234
std::map< std::string, Clonable * > nodeProperties_
Definition: Node.h:66
General exception thrown if a property could not be found.
static const std::string BOOTSTRAP
Bootstrap tag.
Definition: TreeTools.h:684
std::string toString(T t)
Defines the basic types of data flow nodes.