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>
7
8#include "Node.h"
9#include "TreeTools.h"
10#include "PhyloTree.h"
11
12using namespace bpp;
13
14// from the STL:
15#include <algorithm>
16#include <iostream>
17
18using namespace std;
19
22Node::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
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
72void Node::addSubTree(const PhyloTree& tree, std::shared_ptr<PhyloNode> phyloNode)
73{
74 // set Id
75 setId(static_cast<int>(tree.getNodeIndex(phyloNode)));
76
77 // features of the node
78 auto vnames = phyloNode->getPropertyNames();
79 for (const auto& name:vnames)
80 {
81 setNodeProperty(name, *phyloNode->getProperty(name));
82 }
83
84 if (phyloNode->hasName())
85 setName(phyloNode->getName());
86
87 // look at sons
88 vector<shared_ptr<PhyloNode>> sons = tree.getSons(phyloNode);
89 for (auto& son : sons)
90 {
91 Node* s1 = new Node();
92
93 // features of the branch
94 const auto phylobranch = tree.getIncomingEdges(son)[0]; // Tree -> one incoming edge
95
96 if (phylobranch->hasLength())
97 s1->setDistanceToFather(phylobranch->getLength());
98
99 auto vnames2 = phylobranch->getPropertyNames();
100 for (const auto& name:vnames2)
101 {
102 s1->setBranchProperty(name, *phylobranch->getProperty(name));
103 }
104
105 // recursive
106 s1->addSubTree(tree, son);
107
108 // addSon
109 addSon(s1);
110 }
111}
112
113void Node::swap(size_t branch1, size_t branch2)
114{
115 if (branch1 > branch2)
116 {
117 size_t tmp = branch1;
118 branch1 = branch2;
119 branch2 = tmp;
120 }
121 Node* node1 = getSon(branch1);
122 Node* node2 = getSon(branch2);
123 removeSon(node1);
124 removeSon(node2);
125 addSon(branch1, node2);
126 addSon(branch2, node1);
127}
128
129vector<const Node*> Node::getNeighbors() const
130{
131 vector<const Node*> neighbors;
132 if (hasFather())
133 neighbors.push_back(father_);
134 for (size_t i = 0; i < sons_.size(); i++)
135 {
136 neighbors.push_back(sons_[i]);
137 }
138 return neighbors;
139}
140
141vector<Node*> Node::getNeighbors()
142{
143 vector<Node*> neighbors;
144 if (hasFather())
145 neighbors.push_back(father_);
146 for (size_t i = 0; i < sons_.size(); i++)
147 {
148 neighbors.push_back(sons_[i]);
149 }
150 return neighbors;
151}
152
153size_t Node::getSonPosition(const Node* son) const
154{
155 if (!son)
156 throw NullPointerException("Node::getSonPosition(). Empty node given as input.");
157 for (size_t i = 0; i < sons_.size(); i++)
158 {
159 if (sons_[i] == son)
160 return i;
161 }
162 throw NodeNotFoundException("Son not found", TextTools::toString(son->getId()));
163}
164
166{
168}
169
171{
173 return dynamic_cast<const Number<double>*>(getBranchProperty(TreeTools::BOOTSTRAP))->getValue();
174 else
176}
177
178/******************************************************************************/
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:423
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:665
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 Clonable * getBranchProperty(const std::string &name)
Definition: Node.h:604
virtual void addSon(size_t pos, Node *node)
Definition: Node.h:386
virtual const Node & son(size_t pos) const
Definition: Node.h:368
Node & operator=(const Node &node)
Assignation operator.
Definition: Node.cpp:42
virtual bool hasBootstrapValue() const
Definition: Node.cpp:165
virtual void setBranchProperty(const std::string &name, const Clonable &property)
Set/add a branch property.
Definition: Node.h:597
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:507
virtual const Node * getSon(size_t pos) const
Definition: Node.h:362
std::string * name_
Definition: Node.h:62
virtual size_t getSonPosition(const Node *son) const
Definition: Node.cpp:153
void addSubTree(const PhyloTree &tree, std::shared_ptr< PhyloNode > phyloNode)
Definition: Node.cpp:72
std::vector< const Node * > getNeighbors() const
Definition: Node.cpp:129
virtual void swap(size_t branch1, size_t branch2)
Definition: Node.cpp:113
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:170
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.