bpp-phyl3  3.0.0
DistanceEstimation.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_DISTANCE_DISTANCEESTIMATION_H
6 #define BPP_PHYL_DISTANCE_DISTANCEESTIMATION_H
7 
8 #include <Bpp/Clonable.h>
14 
15 #include "../Likelihood/SubstitutionProcess.h"
16 #include "../PseudoNewtonOptimizer.h"
17 #include "../Likelihood/PhyloLikelihoods/SingleProcessPhyloLikelihood.h"
18 #include "../Likelihood/RateAcrossSitesSubstitutionProcess.h"
19 
20 // From bpp-seq:
22 
23 // From the STL:
24 #include <memory>
25 
26 namespace bpp
27 {
29  public virtual Clonable
30 {
31 private:
32  std::shared_ptr<SubstitutionProcessInterface> process_;
33  size_t numProc_; // needed if in Collection Process
34  std::shared_ptr<const AlignmentDataInterface> sites_;
35  std::shared_ptr<DistanceMatrix> dist_;
36  std::shared_ptr<OptimizerInterface> optimizer_;
37  std::shared_ptr<MetaOptimizer> defaultOptimizer_;
38  size_t verbose_;
40 
41 public:
54  std::shared_ptr<SubstitutionProcessInterface> process,
55  size_t verbose = 1) :
57  numProc_(0),
58  sites_(0),
59  dist_(0),
60  optimizer_(0),
62  verbose_(verbose),
63  parameters_()
64  {
65  init_();
66  }
67 
81  std::shared_ptr<BranchModelInterface> model,
82  std::shared_ptr<DiscreteDistributionInterface> rateDist,
83  size_t verbose = 1) :
84  process_(),
85  numProc_(0),
86  sites_(0),
87  dist_(0),
88  optimizer_(0),
90  verbose_(verbose),
91  parameters_()
92  {
93  process_ = std::make_shared<RateAcrossSitesSubstitutionProcess>(model, rateDist);
94  init_();
95  }
96 
112  std::shared_ptr<SubstitutionProcessInterface> process,
113  std::shared_ptr<const AlignmentDataInterface> sites,
114  size_t verbose = 1,
115  bool computeMat = true) :
116  process_(process),
117  numProc_(0),
118  sites_(sites),
119  dist_(0),
120  optimizer_(0),
122  verbose_(verbose),
123  parameters_()
124  {
125  init_();
126  if (computeMat) computeMatrix();
127  }
128 
145  std::shared_ptr<BranchModelInterface> model,
146  std::shared_ptr<DiscreteDistributionInterface> rateDist,
147  std::shared_ptr<const AlignmentDataInterface> sites,
148  size_t verbose = 1,
149  bool computeMat = true) :
150  process_(),
151  numProc_(0),
152  sites_(sites),
153  dist_(0),
154  optimizer_(0),
156  verbose_(verbose),
157  parameters_()
158  {
159  process_ = std::make_shared<RateAcrossSitesSubstitutionProcess>(model, rateDist);
160  init_();
161  if (computeMat) computeMatrix();
162  }
163 
171  DistanceEstimation(const DistanceEstimation& distanceEstimation) :
172  process_(distanceEstimation.process_),
173  numProc_(distanceEstimation.numProc_),
174  sites_(distanceEstimation.sites_),
175  dist_(0),
176  optimizer_(distanceEstimation.optimizer_),
177  defaultOptimizer_(distanceEstimation.defaultOptimizer_),
178  verbose_(distanceEstimation.verbose_),
179  parameters_(distanceEstimation.parameters_)
180  {
181  if (distanceEstimation.dist_ != 0)
182  dist_ = std::make_shared<DistanceMatrix>(*distanceEstimation.dist_);
183  else
184  dist_ = 0;
185  }
186 
195  DistanceEstimation& operator=(const DistanceEstimation& distanceEstimation)
196  {
197  process_ = distanceEstimation.process_;
198  numProc_ = distanceEstimation.numProc_;
199  sites_ = distanceEstimation.sites_;
200  if (distanceEstimation.dist_)
201  dist_ = std::make_shared<DistanceMatrix>(*distanceEstimation.dist_);
202  else
203  dist_ = 0;
204  optimizer_ = distanceEstimation.optimizer_;
205  // _defaultOptimizer has already been initialized since the default constructor has been called.
206  verbose_ = distanceEstimation.verbose_;
207  parameters_ = distanceEstimation.parameters_;
208  return *this;
209  }
210 
211  virtual ~DistanceEstimation() {}
212 
213  DistanceEstimation* clone() const override { return new DistanceEstimation(*this); }
214 
215 private:
216  void init_();
217 
218 public:
227  void computeMatrix();
228 
234  std::unique_ptr<DistanceMatrix> getMatrix() const
235  {
236  return dist_ == nullptr ? nullptr : std::make_unique<DistanceMatrix>(*dist_);
237  }
238 
239  bool hasProcess() const { return process_.get(); }
240 
242  {
243  if (hasProcess())
244  return *process_;
245  else
246  throw Exception("DistanceEstimation::getSubstitutionModel(). No process associated to this instance.");
247  }
248 
249  std::shared_ptr<const SubstitutionProcessInterface> getProcess() const
250  {
251  return process_;
252  }
253 
254  void setProcess(std::shared_ptr<SubstitutionProcessInterface> process = nullptr) { process_ = process; }
255 
256  void setData(std::shared_ptr<const AlignmentDataInterface> sites = nullptr) { sites_ = sites; }
257 
258  std::shared_ptr<const AlignmentDataInterface> getData() const { return sites_; }
259 
260  const AlignmentDataInterface& data() const { return *sites_; }
261 
262  void setOptimizer(std::shared_ptr<OptimizerInterface> optimizer)
263  {
265  }
266 
267  std::shared_ptr<const OptimizerInterface> getOptimizer() const { return optimizer_; }
268 
269  std::shared_ptr<OptimizerInterface> getOptimizer() { return optimizer_; }
270 
271  const OptimizerInterface& optimizer() const { return *optimizer_; }
272 
274 
275  void resetOptimizer() { optimizer_ = dynamic_pointer_cast<OptimizerInterface>(defaultOptimizer_); }
276 
277  bool matchParametersValues(const ParameterList& parameters)
278  {
279  if (hasProcess())
280  return process_->matchParametersValues(parameters);
281  return false;
282  }
283 
291  void setAdditionalParameters(const ParameterList& parameters)
292  {
293  parameters_ = parameters;
294  }
295 
300  {
301  parameters_.reset();
302  }
303 
307  void setVerbose(size_t verbose) { verbose_ = verbose; }
311  size_t getVerbose() const { return verbose_; }
312 };
313 } // end of namespace bpp.
314 #endif // BPP_PHYL_DISTANCE_DISTANCEESTIMATION_H
std::shared_ptr< DistanceMatrix > dist_
std::unique_ptr< DistanceMatrix > getMatrix() const
Get the distance matrix.
void resetAdditionalParameters()
Reset all additional parameters.
const SubstitutionProcessInterface & process() const
std::shared_ptr< const AlignmentDataInterface > sites_
DistanceEstimation & operator=(const DistanceEstimation &distanceEstimation)
Assignment operator.
void setProcess(std::shared_ptr< SubstitutionProcessInterface > process=nullptr)
DistanceEstimation(std::shared_ptr< BranchModelInterface > model, std::shared_ptr< DiscreteDistributionInterface > rateDist, size_t verbose=1)
Create a new DistanceEstimation object according to a given substitution model and a rate distributio...
void computeMatrix()
Perform the distance computation.
const AlignmentDataInterface & data() const
bool matchParametersValues(const ParameterList &parameters)
void setOptimizer(std::shared_ptr< OptimizerInterface > optimizer)
const OptimizerInterface & optimizer() const
DistanceEstimation(std::shared_ptr< BranchModelInterface > model, std::shared_ptr< DiscreteDistributionInterface > rateDist, std::shared_ptr< const AlignmentDataInterface > sites, size_t verbose=1, bool computeMat=true)
Create a new DistanceEstimation object and compute distances according to a given substitution model ...
OptimizerInterface & optimizer()
std::shared_ptr< OptimizerInterface > getOptimizer()
std::shared_ptr< const OptimizerInterface > getOptimizer() const
void setData(std::shared_ptr< const AlignmentDataInterface > sites=nullptr)
std::shared_ptr< MetaOptimizer > defaultOptimizer_
DistanceEstimation(std::shared_ptr< SubstitutionProcessInterface > process, std::shared_ptr< const AlignmentDataInterface > sites, size_t verbose=1, bool computeMat=true)
Create a new DistanceEstimation object and compute distances according to a given substitution proces...
std::shared_ptr< SubstitutionProcessInterface > process_
std::shared_ptr< const SubstitutionProcessInterface > getProcess() const
void setVerbose(size_t verbose)
DistanceEstimation(const DistanceEstimation &distanceEstimation)
Copy constructor.
DistanceEstimation(std::shared_ptr< SubstitutionProcessInterface > process, size_t verbose=1)
Create a new DistanceEstimation object according to a given substitution process.
DistanceEstimation * clone() const override
std::shared_ptr< OptimizerInterface > optimizer_
std::shared_ptr< const AlignmentDataInterface > getData() const
void setAdditionalParameters(const ParameterList &parameters)
Specify a list of parameters to be estimated.
virtual void reset()
This interface describes the substitution process along the tree and sites of the alignment.
Defines the basic types of data flow nodes.