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>
9 #include <Bpp/Numeric/Number.h>
10 #include <Bpp/Utils/MapTools.h>
11 
12 #include "PhyloTreeExceptions.h"
13 
14 #include <iostream>
15 
16 namespace bpp
17 {
18 class PhyloBranchParam;
19 
21 {
22 protected:
24  double length_;
25  mutable std::map<std::string, Clonable*> properties_;
26 
27 public:
36  isLengthDefined_(false),
37  length_(0),
38  properties_()
39  {
40  }
41 
42  PhyloBranch(double length) :
43  isLengthDefined_(true),
44  length_(length),
45  properties_()
46  {
47  }
48 
49  PhyloBranch(const PhyloBranchParam& branch);
50 
56  PhyloBranch(const PhyloBranch& branch);
57 
64  PhyloBranch& operator=(const PhyloBranch& branch);
65 
66  PhyloBranch* clone() const { return new PhyloBranch(*this); }
67 
72  virtual ~PhyloBranch()
73  {
75  }
76 
81  bool hasLength() const
82  {
83  return isLengthDefined_;
84  }
85 
86 
90  void deleteLength()
91  {
92  isLengthDefined_ = false;
93  }
94 
101  double getLength() const
102  {
103  if (!isLengthDefined_)
104  throw PhyloBranchPException("PhyloBranch::getLength: length is not defined.", this);
105 
106  return length_;
107  }
108 
109 
114  void setLength(double newLength)
115  {
116  length_ = newLength;
117  isLengthDefined_ = true;
118  }
119 
130  void setProperty(const std::string& name, const Clonable& property)
131  {
132  if (hasProperty(name))
133  delete properties_[name];
134  properties_[name] = property.clone();
135  }
136 
137  Clonable* getProperty(const std::string& name)
138  {
139  if (hasProperty(name))
140  return properties_[name];
141  else
142  throw PhyloBranchPropertyNotFoundException("", name, this);
143  }
144 
145  const Clonable* getProperty(const std::string& name) const
146  {
147  if (hasProperty(name))
148  return const_cast<const Clonable*>(properties_[name]);
149  else
150  throw PhyloBranchPropertyNotFoundException("", name, this);
151  }
152 
153  Clonable* removeProperty(const std::string& name)
154  {
155  if (hasProperty(name))
156  {
157  Clonable* removed = properties_[name];
158  properties_.erase(name);
159  return removed;
160  }
161  else
162  throw PhyloBranchPropertyNotFoundException("", name, this);
163  }
164 
165  void deleteProperty(const std::string& name)
166  {
167  if (hasProperty(name))
168  {
169  delete properties_[name];
170  properties_.erase(name);
171  }
172  else
173  throw PhyloBranchPropertyNotFoundException("", name, this);
174  }
175 
182  {
183  properties_.clear();
184  }
185 
190  {
191  for (std::map<std::string, Clonable*>::iterator i = properties_.begin(); i != properties_.end(); i++)
192  {
193  delete i->second;
194  }
195  properties_.clear();
196  }
197 
198  bool hasProperty(const std::string& name) const { return properties_.find(name) != properties_.end(); }
199 
200  std::vector<std::string> getPropertyNames() const { return MapTools::getKeys(properties_); }
201 
202  bool hasBootstrapValue() const
203  {
204  return properties_.find("bootstrap") != properties_.end();
205  }
206 
207  double getBootstrapValue() const
208  {
209  if (!hasBootstrapValue())
210  throw PhyloBranchPropertyNotFoundException("", "bootstrap", this);
211 
212  return (dynamic_cast<const Number<double>*>(properties_.find("bootstrap")->second))->getValue();
213  }
214 }; // end of class PhyloBranch
215 } // end of namespace bpp.
216 #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 * clone() const
Definition: PhyloBranch.h:66
PhyloBranch()
Constructors.
Definition: PhyloBranch.h:35
PhyloBranch & operator=(const PhyloBranch &branch)
Assignation operator.
Definition: PhyloBranch.cpp:34
Clonable * removeProperty(const std::string &name)
Definition: PhyloBranch.h:153
void setProperty(const std::string &name, const Clonable &property)
Set/add a branch property.
Definition: PhyloBranch.h:130
bool hasLength() const
Has the length been set?
Definition: PhyloBranch.h:81
bool hasProperty(const std::string &name) const
Definition: PhyloBranch.h:198
Clonable * getProperty(const std::string &name)
Definition: PhyloBranch.h:137
virtual ~PhyloBranch()
destructor. In Graph, nothing is changed.
Definition: PhyloBranch.h:72
void deleteLength()
Delete length.
Definition: PhyloBranch.h:90
PhyloBranch(double length)
Definition: PhyloBranch.h:42
double getLength() const
What is the branch length?
Definition: PhyloBranch.h:101
void setLength(double newLength)
Definition: PhyloBranch.h:114
std::map< std::string, Clonable * > properties_
Definition: PhyloBranch.h:25
void deleteProperties()
Delete all branch properties.
Definition: PhyloBranch.h:189
std::vector< std::string > getPropertyNames() const
Definition: PhyloBranch.h:200
const Clonable * getProperty(const std::string &name) const
Definition: PhyloBranch.h:145
bool hasBootstrapValue() const
Definition: PhyloBranch.h:202
void deleteProperty(const std::string &name)
Definition: PhyloBranch.h:165
void removeProperties()
Remove all branch properties.
Definition: PhyloBranch.h:181
double getBootstrapValue() const
Definition: PhyloBranch.h:207
Defines the basic types of data flow nodes.