bpp-phyl3  3.0.0
MarkovModulatedSubstitutionModel.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_MODEL_MARKOVMODULATEDSUBSTITUTIONMODEL_H
6 #define BPP_PHYL_MODEL_MARKOVMODULATEDSUBSTITUTIONMODEL_H
7 
10 
11 #include "SubstitutionModel.h"
13 
14 namespace bpp
15 {
50 {
51 protected:
52  std::unique_ptr<ReversibleSubstitutionModelInterface> model_;
53  std::shared_ptr<const MarkovModulatedStateMap> stateMap_;
54  size_t nbStates_; // Number of states in model
55  size_t nbRates_; // Number of rate classes
56 
63  RowMatrix<double> rates_; // All rates values
64  RowMatrix<double> ratesExchangeability_; // All rates transitions
65  Vdouble ratesFreq_; // All rates equilibrium frequencies
67  RowMatrix<double> ratesGenerator_; // All rates transitions
68 
73 
78 
83 
88 
93 
99 
104 
110  bool compFreq_;
111 
118 
123 
125 
126  std::string nestedPrefix_;
127 
128 public:
140  std::unique_ptr<ReversibleSubstitutionModelInterface> model,
141  unsigned int nbRates,
142  bool normalizeRateChanges,
143  const std::string& prefix) :
145  model_(std::move(model)),
146  stateMap_(std::make_shared<MarkovModulatedStateMap>(model_->stateMap(), nbRates)),
148  nbRates_(nbRates),
149  rates_(nbRates, nbRates),
150  ratesExchangeability_(nbRates, nbRates),
151  ratesFreq_(nbRates),
152  ratesGenerator_(nbRates, nbRates),
153  generator_(),
157  eigenValues_(),
158  iEigenValues_(),
159  eigenDecompose_(true),
160  compFreq_(false),
161  pijt_(), dpijt_(), d2pijt_(), freq_(),
162  normalizeRateChanges_(normalizeRateChanges),
163  nestedPrefix_("model_" + model_->getNamespace())
164  {
165  model_->setNamespace(prefix + nestedPrefix_);
166  addParameters_(model_->getIndependentParameters());
167  }
168 
171 
173 
175 
176 public:
177  const Alphabet& alphabet() const override { return model_->alphabet(); }
178 
179  std::shared_ptr<const Alphabet> getAlphabet() const override { return model_->getAlphabet(); }
180 
181  size_t getNumberOfStates() const override { return stateMap_->getNumberOfModelStates(); }
182 
183  const StateMapInterface& stateMap() const override { return *stateMap_; }
184 
185  std::shared_ptr<const StateMapInterface> getStateMap() const override { return stateMap_; }
186 
187  const std::vector<int>& getAlphabetStates() const override { return stateMap_->getAlphabetStates(); }
188 
189  std::string getAlphabetStateAsChar(size_t index) const override { return stateMap_->getAlphabetStateAsChar(index); }
190 
191  int getAlphabetStateAsInt(size_t index) const override { return stateMap_->getAlphabetStateAsInt(index); }
192 
193  std::vector<size_t> getModelStates(int code) const override { return stateMap_->getModelStates(code); }
194 
195  std::vector<size_t> getModelStates(const std::string& code) const override { return stateMap_->getModelStates(code); }
196 
197  const Vdouble& getFrequencies() const override { return freq_; }
198 
199  const Matrix<double>& exchangeabilityMatrix() const override { return exchangeability_; }
200 
201  const Matrix<double>& generator() const override { return generator_; }
202 
203  const Matrix<double>& getPij_t(double t) const override;
204  const Matrix<double>& getdPij_dt(double t) const override;
205  const Matrix<double>& getd2Pij_dt2(double t) const override;
206 
207  const Vdouble& getEigenValues() const override { return eigenValues_; }
208  const Vdouble& getIEigenValues() const override { return iEigenValues_; }
209 
210  bool isDiagonalizable() const override { return true; }
211  bool isNonSingular() const override { return true; }
212 
213  const Matrix<double>& getRowLeftEigenVectors() const override { return leftEigenVectors_; }
215 
216  double freq(size_t i) const override { return freq_[i]; }
217  double Sij(size_t i, size_t j) const override { return exchangeability_(i, j); }
218  double Qij(size_t i, size_t j) const override { return generator_(i, j); }
219 
220  double Pij_t (size_t i, size_t j, double t) const override { return getPij_t(t)(i, j); }
221  double dPij_dt (size_t i, size_t j, double t) const override { return getdPij_dt(t)(i, j); }
222  double d2Pij_dt2(size_t i, size_t j, double t) const override { return getd2Pij_dt2(t)(i, j); }
223 
224  double getInitValue(size_t i, int state) const override;
225 
226  void setFreqFromData(const SequenceDataInterface& data, double pseudoCount = 0) override
227  {
228  model_->setFreqFromData(data, pseudoCount);
229  updateMatrices_();
230  }
231 
232  void setFreq(std::map<int, double>& frequencies) override
233  {
234  model_->setFreq(frequencies);
235  updateMatrices_();
236  }
237 
238  const FrequencySetInterface& frequencySet() const override
239  {
240  throw NullPointerException("MarkovModulatedSubstitutionModel::frequencySet. No FrequencySet associated to this model. Frequencies are computed from the FrequencySet of the modulated model.");
241  }
242 
244  {
245  return *model_;
246  }
247 
255  size_t getRate(size_t i) const
256  {
257  return i / nbStates_;
258  }
259 
260  double getRate() const override { return model_->getRate(); }
261 
262  void setRate(double rate) override { model_->setRate(rate); }
263 
264  bool isScalable() const override
265  {
266  return model_->isScalable();
267  }
268 
269  void setScalable(bool scalable) override
270  {
271  model_->setScalable(scalable);
272  }
273 
274  void normalize() override
275  {
276  model_->normalize();
277  updateMatrices_();
278  }
279 
280  void setDiagonal() override;
281 
282  double getScale() const override
283  {
284  std::vector<double> v;
286  return -VectorTools::scalar<double, double>(v, freq_);
287  }
288 
289  void setScale(double scale) override
290  {
291  model_->setScale(scale);
292  updateMatrices_();
293  }
294 
295  void enableEigenDecomposition(bool yn) override { eigenDecompose_ = yn; }
296 
297  bool enableEigenDecomposition() override { return eigenDecompose_; }
298 
299  bool computeFrequencies() const override { return compFreq_; }
300 
301  void computeFrequencies(bool yn) override { compFreq_ = yn; }
302 
303 
309  virtual void fireParameterChanged(const ParameterList& parameters) override
310  {
311  model_->matchParametersValues(parameters);
313  updateMatrices_();
314  }
315 
316  void setNamespace(const std::string& prefix) override;
317 
318 protected:
319  virtual void updateMatrices_();
320 
327  virtual void updateRatesModel_() = 0;
328 
330  {
331  return freq_;
332  }
333 };
334 } // end of namespace bpp.
335 #endif // BPP_PHYL_MODEL_MARKOVMODULATEDSUBSTITUTIONMODEL_H
Partial implementation of the TransitionModel interface, with function for likelihood computations.
void addParameters_(const ParameterList &parameters)
std::string getNamespace() const override
Parametrize a set of state frequencies.
Definition: FrequencySet.h:29
This class implements a state map for Markov modulated models.
Definition: StateMap.h:192
Partial implementation of the Markov-modulated class of substitution models.
double Qij(size_t i, size_t j) const override
A method for computing all necessary matrices.
std::shared_ptr< const StateMapInterface > getStateMap() const override
const Matrix< double > & generator() const override
bool compFreq_
Tell if the equilibrium frequencies should be computed from the generator.
std::vector< size_t > getModelStates(const std::string &code) const override
Get the state in the model corresponding to a particular state in the alphabet.
double Sij(size_t i, size_t j) const override
RowMatrix< double > rightEigenVectors_
The matrix made of right eigen vectors (by column).
std::vector< size_t > getModelStates(int code) const override
Get the state in the model corresponding to a particular state in the alphabet.
bool enableEigenDecomposition() override
Tell if eigenValues and Vectors must be computed.
MarkovModulatedSubstitutionModel * clone() const override=0
double getInitValue(size_t i, int state) const override
bool isScalable() const override
returns if model is scalable
const Matrix< double > & getColumnRightEigenVectors() const override
std::shared_ptr< const MarkovModulatedStateMap > stateMap_
const FrequencySetInterface & frequencySet() const override
std::unique_ptr< ReversibleSubstitutionModelInterface > model_
void setScale(double scale) override
Multiplies the current generator by the given scale.
void setRate(double rate) override
Set the rate of the model (must be positive).
const Matrix< double > & getdPij_dt(double t) const override
void enableEigenDecomposition(bool yn) override
Set if eigenValues and Vectors must be computed.
void setScalable(bool scalable) override
sets if model is scalable, ie scale can be changed. Default : true, set to false to avoid normalizati...
const std::vector< int > & getAlphabetStates() const override
MarkovModulatedSubstitutionModel & operator=(const MarkovModulatedSubstitutionModel &model)
virtual void fireParameterChanged(const ParameterList &parameters) override
Tells the model that a parameter value has changed.
const Matrix< double > & getPij_t(double t) const override
void normalize() override
Normalize the generator.
int getAlphabetStateAsInt(size_t index) const override
Vdouble iEigenValues_
The vector of imaginary parts of the eigen values (zero in case of reversible pmodel).
std::shared_ptr< const Alphabet > getAlphabet() const override
MarkovModulatedSubstitutionModel(std::unique_ptr< ReversibleSubstitutionModelInterface > model, unsigned int nbRates, bool normalizeRateChanges, const std::string &prefix)
Build a new MarkovModulatedSubstitutionModel object.
RowMatrix< double > pijt_
These ones are for bookkeeping:
const Matrix< double > & getd2Pij_dt2(double t) const override
double Pij_t(size_t i, size_t j, double t) const override
const Matrix< double > & exchangeabilityMatrix() const override
size_t getRate(size_t i) const
Get the rate category corresponding to a particular state in the compound model.
double getScale() const override
Get the scalar product of diagonal elements of the generator and the frequencies vector....
void setFreqFromData(const SequenceDataInterface &data, double pseudoCount=0) override
Set equilibrium frequencies equal to the frequencies estimated from the data.
const StateMapInterface & stateMap() const override
RowMatrix< double > exchangeability_
The exchangeability matrix of the model.
Vdouble freq_
The vector of equilibrium frequencies.
RowMatrix< double > generator_
The generator matrix of the model.
std::string getAlphabetStateAsChar(size_t index) const override
void setDiagonal() override
set the diagonal of the generator such that sum on each line equals 0.
bool eigenDecompose_
Tell if the eigen decomposition should be performed.
double dPij_dt(size_t i, size_t j, double t) const override
Vdouble eigenValues_
The vector of real parts of eigen values.
virtual void updateRatesModel_()=0
Update the rates vector, generator and equilibrium frequencies.
double d2Pij_dt2(size_t i, size_t j, double t) const override
const Matrix< double > & getRowLeftEigenVectors() const override
RowMatrix< double > leftEigenVectors_
The matrix made of left eigen vectors (by row).
double getRate() const override
Get the rate.
void setNamespace(const std::string &prefix) override
void setFreq(std::map< int, double > &frequencies) override
Set equilibrium frequencies.
size_t getNumberOfStates() const override
Get the number of states.
const ReversibleSubstitutionModelInterface & nestedModel() const
static void diag(const std::vector< Scalar > &D, Matrix< Scalar > &O)
Interface for reversible substitution models.
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:25
Defines the basic types of data flow nodes.
std::vector< double > Vdouble