bpp-phyl3 3.0.0
PhyloBranch.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_PHYLOBRANCH_H
6#define BPP_PHYL_TREE_PHYLOBRANCH_H
7
8#include <Bpp/Clonable.h>
10#include <Bpp/Utils/MapTools.h>
11
12#include "PhyloTreeExceptions.h"
13
14#include <iostream>
15
16namespace bpp
17{
18class PhyloBranchParam;
19
21{
22protected:
24 double length_;
25 mutable std::map<std::string, Clonable*> properties_;
26
27public:
36 isLengthDefined_(false),
37 length_(0),
39 {}
40
41 PhyloBranch(double length) :
42 isLengthDefined_(true),
43 length_(length),
45 {}
46
47 PhyloBranch(const PhyloBranchParam& branch);
48
54 PhyloBranch(const PhyloBranch& branch);
55
62 PhyloBranch& operator=(const PhyloBranch& branch);
63
64 PhyloBranch* clone() const { return new PhyloBranch(*this); }
65
70 virtual ~PhyloBranch()
71 {
73 }
74
79 bool hasLength() const
80 {
81 return isLengthDefined_;
82 }
83
84
89 {
90 isLengthDefined_ = false;
91 }
92
99 double getLength() const
100 {
101 if (!isLengthDefined_)
102 throw PhyloBranchPException("PhyloBranch::getLength: length is not defined.", this);
103
104 return length_;
105 }
106
107
112 void setLength(double newLength)
113 {
114 length_ = newLength;
115 isLengthDefined_ = true;
116 }
117
128 void setProperty(const std::string& name, const Clonable& property)
129 {
130 if (hasProperty(name))
131 delete properties_[name];
132 properties_[name] = property.clone();
133 }
134
135 Clonable* getProperty(const std::string& name)
136 {
137 if (hasProperty(name))
138 return properties_[name];
139 else
140 throw PhyloBranchPropertyNotFoundException("", name, this);
141 }
142
143 const Clonable* getProperty(const std::string& name) const
144 {
145 if (hasProperty(name))
146 return const_cast<const Clonable*>(properties_[name]);
147 else
148 throw PhyloBranchPropertyNotFoundException("", name, this);
149 }
150
151 Clonable* removeProperty(const std::string& name)
152 {
153 if (hasProperty(name))
154 {
155 Clonable* removed = properties_[name];
156 properties_.erase(name);
157 return removed;
158 }
159 else
160 throw PhyloBranchPropertyNotFoundException("", name, this);
161 }
162
163 void deleteProperty(const std::string& name)
164 {
165 if (hasProperty(name))
166 {
167 delete properties_[name];
168 properties_.erase(name);
169 }
170 else
171 throw PhyloBranchPropertyNotFoundException("", name, this);
172 }
173
180 {
181 properties_.clear();
182 }
183
188 {
189 for (std::map<std::string, Clonable*>::iterator i = properties_.begin(); i != properties_.end(); i++)
190 {
191 delete i->second;
192 }
193 properties_.clear();
194 }
195
196 bool hasProperty(const std::string& name) const { return properties_.find(name) != properties_.end(); }
197
198 std::vector<std::string> getPropertyNames() const { return MapTools::getKeys(properties_); }
199
200 bool hasBootstrapValue() const
201 {
202 return properties_.find("bootstrap") != properties_.end();
203 }
204
205 double getBootstrapValue() const
206 {
207 if (!hasBootstrapValue())
208 throw PhyloBranchPropertyNotFoundException("", "bootstrap", this);
209
210 return (dynamic_cast<const Number<double>*>(properties_.find("bootstrap")->second))->getValue();
211 }
212}; // end of class PhyloBranch
213} // end of namespace bpp.
214#endif // BPP_PHYL_TREE_PHYLOBRANCH_H
static std::vector< Key > getKeys(const std::map< Key, T, Cmp > &myMap)
General exception thrown when something is wrong with a particular branch.
General exception thrown if a property could not be found.
PhyloBranch()
Constructors.
Definition: PhyloBranch.h:35
Clonable * getProperty(const std::string &name)
Definition: PhyloBranch.h:135
PhyloBranch & operator=(const PhyloBranch &branch)
Assignation operator.
Definition: PhyloBranch.cpp:33
void setProperty(const std::string &name, const Clonable &property)
Set/add a branch property.
Definition: PhyloBranch.h:128
Clonable * removeProperty(const std::string &name)
Definition: PhyloBranch.h:151
bool hasLength() const
Has the length been set?
Definition: PhyloBranch.h:79
bool hasProperty(const std::string &name) const
Definition: PhyloBranch.h:196
virtual ~PhyloBranch()
destructor. In Graph, nothing is changed.
Definition: PhyloBranch.h:70
void deleteLength()
Delete length.
Definition: PhyloBranch.h:88
PhyloBranch(double length)
Definition: PhyloBranch.h:41
double getLength() const
What is the branch length?
Definition: PhyloBranch.h:99
void setLength(double newLength)
Definition: PhyloBranch.h:112
std::map< std::string, Clonable * > properties_
Definition: PhyloBranch.h:25
void deleteProperties()
Delete all branch properties.
Definition: PhyloBranch.h:187
bool hasBootstrapValue() const
Definition: PhyloBranch.h:200
void deleteProperty(const std::string &name)
Definition: PhyloBranch.h:163
PhyloBranch * clone() const
Definition: PhyloBranch.h:64
void removeProperties()
Remove all branch properties.
Definition: PhyloBranch.h:179
const Clonable * getProperty(const std::string &name) const
Definition: PhyloBranch.h:143
double getBootstrapValue() const
Definition: PhyloBranch.h:205
std::vector< std::string > getPropertyNames() const
Definition: PhyloBranch.h:198
Defines the basic types of data flow nodes.