bpp-phyl3 3.0.0
PhyloNode.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_PHYLONODE_H
6#define BPP_PHYL_TREE_PHYLONODE_H
7
8#include <Bpp/Clonable.h>
10#include <memory>
11
12#include "PhyloTreeExceptions.h"
13
14namespace bpp
15{
16/***********
17 * For each PhyloNode of the tree, a description of the event at the node.
18 *
19 * It will determine the choice of the computing operator on this node.
20 *
21 */
22
23class NodeEvent : public Clonable
24{
25 enum class NodeType
26 {
27 Speciation = 0,
28 Mixture = 1,
29 Hybridization = 2
30 };
31
33
34public:
35 NodeEvent(NodeType type) { nodeType_ = type;}
36
37 // NodeEvent(const NodeEvent& event) :
38 // nodeType_(event.nodeType_) {}
39
40 NodeEvent* clone() const { return new NodeEvent(*this);}
41
42 bool isSpeciation() const { return nodeType_ == NodeType::Speciation;}
43
44 bool isMixture() const {return nodeType_ == NodeType::Mixture;}
45
47
48public:
50 static const NodeEvent mixtureEvent;
52
53 std::string toString() const
54 {
55 if (isSpeciation())
56 return "speciation";
57 else if (isMixture())
58 return "mixture";
59 else
60 return "hybridization";
61 }
62};
63
64
66{
67private:
68 // a name, if specified
69 std::string name_;
70
71 // Node properties
72 mutable std::map<std::string, Clonable*> properties_;
73
74public:
79 name_(""),
81 {}
82
88 PhyloNode(const std::string& name) :
89 name_(name),
91 {}
92
98 PhyloNode(const PhyloNode& node);
99
107 PhyloNode& operator=(const PhyloNode& node);
108
109 PhyloNode* clone() const { return new PhyloNode(*this); }
110
115 virtual ~PhyloNode()
116 {
118 }
119
120public:
133 std::string getName() const
134 {
135 if (!hasName()) throw PhyloNodePException("Node::getName: no name associated to this node.", this);
136 return name_;
137 }
138
144 void setName(const std::string& name)
145 {
146 name_ = name;
147 }
148
153 {
154 name_ = "";
155 }
156
162 bool hasName() const { return name_ != ""; }
163
182 void setProperty(const std::string& name, const Clonable& property)
183 {
184 if (hasProperty(name))
185 delete properties_[name];
186 properties_[name] = property.clone();
187 }
188
189 Clonable* getProperty(const std::string& name)
190 {
191 if (hasProperty(name))
192 return properties_[name];
193 else
194 throw PhyloNodePropertyNotFoundException("", name, this);
195 }
196
197 const Clonable* getProperty(const std::string& name) const
198 {
199 if (hasProperty(name))
200 return const_cast<const Clonable*>(properties_[name]);
201 else
202 throw PhyloNodePropertyNotFoundException("", name, this);
203 }
204
205 Clonable* removeProperty(const std::string& name)
206 {
207 if (hasProperty(name))
208 {
209 Clonable* removed = properties_[name];
210 properties_.erase(name);
211 return removed;
212 }
213 else
214 throw PhyloNodePropertyNotFoundException("", name, this);
215 }
216
217 void deleteProperty(const std::string& name)
218 {
219 if (hasProperty(name))
220 {
221 delete properties_[name];
222 properties_.erase(name);
223 }
224 else
225 throw PhyloNodePropertyNotFoundException("", name, this);
226 }
227
234 {
235 properties_.clear();
236 }
237
242 {
243 for (std::map<std::string, Clonable*>::iterator i = properties_.begin(); i != properties_.end(); i++)
244 {
245 delete i->second;
246 }
247 properties_.clear();
248 }
249
250 bool hasProperty(const std::string& name) const { return properties_.find(name) != properties_.end(); }
251
252 std::vector<std::string> getPropertyNames() const { return MapTools::getKeys(properties_); }
254}; // end of class node
255} // end of namespace bpp.
256
257#else
258namespace bpp { class PhyloNode; }
259#endif // BPP_PHYL_TREE_PHYLONODE_H
static std::vector< Key > getKeys(const std::map< Key, T, Cmp > &myMap)
NodeEvent(NodeType type)
Definition: PhyloNode.h:35
NodeEvent * clone() const
Definition: PhyloNode.h:40
std::string toString() const
Definition: PhyloNode.h:53
bool isSpeciation() const
Definition: PhyloNode.h:42
static const NodeEvent speciationEvent
Definition: PhyloNode.h:49
NodeType nodeType_
Definition: PhyloNode.h:32
static const NodeEvent mixtureEvent
Definition: PhyloNode.h:50
bool isHybridization() const
Definition: PhyloNode.h:46
static const NodeEvent hybridizationEvent
Definition: PhyloNode.h:51
bool isMixture() const
Definition: PhyloNode.h:44
General exception thrown when something is wrong with a particular node.
General exception thrown if a property could not be found.
const Clonable * getProperty(const std::string &name) const
Definition: PhyloNode.h:197
void setName(const std::string &name)
Give a name or update the name associated to the node.
Definition: PhyloNode.h:144
bool hasName() const
Tell is this node has a name.
Definition: PhyloNode.h:162
Clonable * removeProperty(const std::string &name)
Definition: PhyloNode.h:205
PhyloNode()
Build a new void Node object.
Definition: PhyloNode.h:78
void deleteProperty(const std::string &name)
Definition: PhyloNode.h:217
std::vector< std::string > getPropertyNames() const
Definition: PhyloNode.h:252
Clonable * getProperty(const std::string &name)
Definition: PhyloNode.h:189
PhyloNode * clone() const
Definition: PhyloNode.h:109
void deleteProperties()
Delete all node properties.
Definition: PhyloNode.h:241
std::string name_
Definition: PhyloNode.h:69
void deleteName()
Delete the name associated to this node (do nothing if there is no name).
Definition: PhyloNode.h:152
void setProperty(const std::string &name, const Clonable &property)
Set/add a node property.
Definition: PhyloNode.h:182
std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
Definition: PhyloNode.h:133
std::map< std::string, Clonable * > properties_
Definition: PhyloNode.h:72
PhyloNode(const std::string &name)
Build a new Node with specified name.
Definition: PhyloNode.h:88
PhyloNode & operator=(const PhyloNode &node)
Assignation operator.
Definition: PhyloNode.cpp:30
bool hasProperty(const std::string &name) const
Definition: PhyloNode.h:250
virtual ~PhyloNode()
destructor.
Definition: PhyloNode.h:115
void removeProperties()
Remove all node properties.
Definition: PhyloNode.h:233
Defines the basic types of data flow nodes.