bpp-phyl3  3.0.0
OneProcessSequencePhyloLikelihood.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_LIKELIHOOD_PHYLOLIKELIHOODS_ONEPROCESSSEQUENCEPHYLOLIKELIHOOD_H
6 #define BPP_PHYL_LIKELIHOOD_PHYLOLIKELIHOODS_ONEPROCESSSEQUENCEPHYLOLIKELIHOOD_H
7 
12 
13 #include "../../Tree/PhyloTree.h"
14 #include "../SitePartition.h"
15 
16 // From bpp-seq:
19 
20 #include "../DataFlow/LikelihoodCalculationSingleProcess.h"
21 
23 #include "../SubstitutionProcess.h"
24 #include "../OneProcessSequenceEvolution.h"
25 
26 namespace bpp
27 {
39 {
40 private:
44  std::shared_ptr<OneProcessSequenceEvolution> mSeqEvol_;
45 
49  mutable std::unordered_map<std::string, ValueRef<RowLik>> firstOrderDerivativeVectors_;
50 
51  mutable std::unordered_map<std::pair<std::string, std::string>, ValueRef<RowLik>,
54 
55 protected:
56  mutable std::shared_ptr<LikelihoodCalculationSingleProcess> likCal_;
57 
58 public:
61  std::shared_ptr<OneProcessSequenceEvolution> evol,
62  size_t nSeqEvol = 0);
63 
66  std::shared_ptr<const AlignmentDataInterface> data,
67  std::shared_ptr<OneProcessSequenceEvolution> evol,
68  size_t nSeqEvol = 0,
69  size_t nData = 0);
70 
72  std::shared_ptr<const AlignmentDataInterface> data,
73  std::shared_ptr<OneProcessSequenceEvolution> evol,
74  std::shared_ptr<CollectionNodes> collNodes,
75  size_t nSeqEvol = 0,
76  size_t nData = 0);
77 
78 protected:
86  mSeqEvol_(lik.mSeqEvol_),
87  likCal_(lik.likCal_)
88  {}
89 
91  {
93  mSeqEvol_ = lik.mSeqEvol_;
94  likCal_ = lik.likCal_;
95  return *this;
96  }
97 
99  {
100  return new OneProcessSequencePhyloLikelihood(*this);
101  }
102 
103 public:
105 
106 public:
112  void setData(std::shared_ptr<const AlignmentDataInterface> sites, size_t nData = 0) override
113  {
116  }
117 
118  bool isInitialized() const override
119  {
121  }
122 
126  std::shared_ptr<const AlignmentDataInterface> getData() const override
127  {
128  return getLikelihoodCalculationSingleProcess()->getData();
129  }
130 
131  std::shared_ptr<const Alphabet> getAlphabet() const override
132  {
133  return getLikelihoodCalculationSingleProcess()->stateMap().getAlphabet();
134  }
135 
150  const SubstitutionProcessInterface& substitutionProcess() const { return mSeqEvol_->substitutionProcess(); }
151 
152  std::shared_ptr<const SubstitutionProcessInterface> getSubstitutionProcess() const { return mSeqEvol_->getSubstitutionProcess(); }
153 
157  size_t getNumberOfClasses() const { return mSeqEvol_->substitutionProcess().getNumberOfClasses(); }
158 
166  std::shared_ptr<const ParametrizablePhyloTree> tree() const
167  {
168  return mSeqEvol_->substitutionProcess().getParametrizablePhyloTree();
169  }
170 
173 public:
178  {
179  return *likCal_;
180  }
181 
182  std::shared_ptr<LikelihoodCalculation> getLikelihoodCalculation() const override
183  {
184  return likCal_;
185  }
186 
188  {
189  return *likCal_;
190  }
191 
192  std::shared_ptr<AlignedLikelihoodCalculation> getAlignedLikelihoodCalculation() const override
193  {
194  return likCal_;
195  }
196 
198  {
199  return *likCal_;
200  }
201 
202  std::shared_ptr<LikelihoodCalculationSingleProcess> getLikelihoodCalculationSingleProcess() const
203  {
204  return likCal_;
205  }
206 
207  // Get nodes of derivatives directly
208 
209  ValueRef<RowLik> getFirstOrderDerivativeVector (const std::string& variable) const
210  {
211  return firstOrderDerivativeVector(variable);
212  }
213 
214  ValueRef<RowLik> firstOrderDerivativeVector (const std::string& variable) const
215  {
216  const auto it = firstOrderDerivativeVectors_.find (variable);
217  if (it != firstOrderDerivativeVectors_.end ())
218  {
219  return it->second;
220  }
221  else
222  {
223  auto vector = getLikelihoodCalculationSingleProcess()->getSiteLikelihoods(true)->deriveAsValue (context_, accessVariableNode (variable));
224  firstOrderDerivativeVectors_.emplace (variable, vector);
225  return vector;
226  }
227  }
228 
229  ValueRef<RowLik> getSecondOrderDerivativeVector (const std::string& variable) const
230  {
231  return getSecondOrderDerivativeVector (variable, variable);
232  }
233 
234  ValueRef<RowLik> getSecondOrderDerivativeVector (const std::string& variable1,
235  const std::string& variable2) const
236  {
237  return secondOrderDerivativeVector (variable1, variable2);
238  }
239 
240  ValueRef<RowLik> secondOrderDerivativeVector (const std::string& variable1,
241  const std::string& variable2) const
242  {
243  const auto key = std::make_pair (variable1, variable2);
244  const auto it = secondOrderDerivativeVectors_.find (key);
245  if (it != secondOrderDerivativeVectors_.end ())
246  {
247  return it->second;
248  }
249  else
250  {
251  // Reuse firstOrderDerivative() to generate the first derivative with caching
252  auto vector =
253  firstOrderDerivativeVector (variable1)->deriveAsValue (context_, accessVariableNode (variable2));
254  secondOrderDerivativeVectors_.emplace (key, vector);
255  return vector;
256  }
257  }
258 
259 public:
265  /*
266  *@brief return the posterior probabilities of rate classes on each site.
267  *
268  *@return 2D-vector sites x classes
269  */
270 
272 
274 
275  /*
276  *@brief return the likelihood of rate classes on each site.
277  *
278  *@return 2D-vector sites x classes
279  */
280 
282 
283  std::vector<size_t> getClassWithMaxPostProbPerSite() const;
284 
286 
288 
289  /* @} */
290 };
291 } // end of namespace bpp.
292 #endif // BPP_PHYL_LIKELIHOOD_PHYLOLIKELIHOODS_ONEPROCESSSEQUENCEPHYLOLIKELIHOOD_H
AbstractParametrizableSequencePhyloLikelihood & operator=(const AbstractParametrizableSequencePhyloLikelihood &apspl)
const Context & context() const override
static Node_DF & accessVariableNode(const Parameter &param)
virtual void setData(std::shared_ptr< const AlignmentDataInterface > sites, size_t nData=0)
Set the dataset for which the likelihood must be evaluated.
Context for dataflow node construction.
Definition: DataFlow.h:527
void setData(std::shared_ptr< const AlignmentDataInterface > sites)
virtual bool isInitialized() const
The OneProcessSequencePhyloLikelihood class: phylogenetic likelihood computation with a single proces...
std::shared_ptr< LikelihoodCalculationSingleProcess > getLikelihoodCalculationSingleProcess() const
ValueRef< RowLik > secondOrderDerivativeVector(const std::string &variable1, const std::string &variable2) const
std::shared_ptr< const SubstitutionProcessInterface > getSubstitutionProcess() const
std::unordered_map< std::string, ValueRef< RowLik > > firstOrderDerivativeVectors_
For Dataflow computing.
ValueRef< RowLik > firstOrderDerivativeVector(const std::string &variable) const
LikelihoodCalculation & likelihoodCalculation() const override
ValueRef< RowLik > getSecondOrderDerivativeVector(const std::string &variable) const
std::shared_ptr< AlignedLikelihoodCalculation > getAlignedLikelihoodCalculation() const override
OneProcessSequencePhyloLikelihood & operator=(const OneProcessSequencePhyloLikelihood &lik)
std::shared_ptr< LikelihoodCalculation > getLikelihoodCalculation() const override
std::shared_ptr< LikelihoodCalculationSingleProcess > likCal_
ValueRef< RowLik > getFirstOrderDerivativeVector(const std::string &variable) const
LikelihoodCalculationSingleProcess & likelihoodCalculationSingleProcess() const
OneProcessSequencePhyloLikelihood(const OneProcessSequencePhyloLikelihood &lik)
void setData(std::shared_ptr< const AlignmentDataInterface > sites, size_t nData=0) override
Set the dataset for which the likelihood must be evaluated.
const SubstitutionProcessInterface & substitutionProcess() const
Get the ParametrizablePhyloTree.
ValueRef< RowLik > getSecondOrderDerivativeVector(const std::string &variable1, const std::string &variable2) const
std::shared_ptr< OneProcessSequenceEvolution > mSeqEvol_
to avoid the dynamic casts
bool isInitialized() const override
Sets the computeLikelihoods_ to true.
OneProcessSequencePhyloLikelihood(Context &context, std::shared_ptr< OneProcessSequenceEvolution > evol, size_t nSeqEvol=0)
std::shared_ptr< const ParametrizablePhyloTree > tree() const
Return the ref to the SubstitutionProcess used to build the phylolikelihood.
std::unordered_map< std::pair< std::string, std::string >, ValueRef< RowLik >, StringPairHash > secondOrderDerivativeVectors_
size_t getNumberOfClasses() const
Get the number of model classes.
std::shared_ptr< const Alphabet > getAlphabet() const override
Get the alphabet associated to the dataset.
OneProcessSequencePhyloLikelihood * clone() const override
std::shared_ptr< const AlignmentDataInterface > getData() const override
return a pointer to the compressed data.
AlignedLikelihoodCalculation & alignedLikelihoodCalculation() const override
This interface describes the substitution process along the tree and sites of the alignment.
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
std::shared_ptr< Value< T > > ValueRef
Shared pointer alias for Value<T>.
Definition: DataFlow.h:84
std::vector< Vdouble > VVdouble