/usr/include/Bpp/Phyl/Model/AbstractSubstitutionModel.h is in libbpp-phyl-dev 2.0.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | //
// File: AbstractSubstitutionModel.h
// Created by: Julien Dutheil
// Created on: Tue May 27 10:31:49 2003
//
/*
Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
This software is a computer program whose purpose is to provide classes
for phylogenetic data analysis.
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
*/
#ifndef _ABSTRACTSUBSTITUTIONMODEL_H_
#define _ABSTRACTSUBSTITUTIONMODEL_H_
#include "SubstitutionModel.h"
#include <Bpp/Numeric/AbstractParameterAliasable.h>
#include <Bpp/Numeric/VectorTools.h>
namespace bpp
{
/**
* @brief Partial implementation of the SubstitutionModel interface.
*
* This abstract class provides some fields, namely:
* - alphabet_: a pointer toward the alphabet,
* - size_: the size of the alphabet, a parameter frequently called during various computations,
* - rate_: the rate of the model
* - generator_, leftEigenVectors_, rightEigenVectors_: useful matrices,
* - eigenValues_, iEigenValues_, freq_: useful vectors.
* - isDiagonalizable_ : boolean value useful for computation of the exponential
*
* Access methods for these fields are implemented.
*
* This class also provides the updateMatrices() method, which computes eigen values and vectors and fills the corresponding vector (eigenValues_)
* and matrices (leftEigenVectors_ and rightEigenVectors_) from the generator.
*
* The freq_ vector and generator_ matrices are hence the only things to provide to
* create a substitution model.
* It is also possible to redefine one of these methods for better efficiency.
* The Pij_t, dPij_dt and d2Pij_dt2 are particularly inefficient since the matrix formula
* is used to compute all probabilities, and then the result for the initial and final state
* of interest is retrieved.
*
* @note This class is dedicated to "simple" substitution models, for which the number of states is equivalent to the number of characters in the alphabet.
* Consider using the MarkovModulatedSubstitutionModel for more complexe cases.
*/
class AbstractSubstitutionModel :
public virtual SubstitutionModel,
public virtual AbstractParameterAliasable
{
protected:
/**
* @brief The alphabet relevant to this model.
*/
const Alphabet* alphabet_;
/**
* @brief The size of the generator, i.e. the number of states.
*/
unsigned int size_;
/**
* @brief The rate of the model (default: 1). The generator (and all
* its vectorial components) is independent of the rate, since it
* should be normalized.
*/
double rate_;
/**
* @brief The list of supported chars.
*/
std::vector<int> chars_;
/**
* @brief The generator matrix \f$Q\f$ of the model.
*/
RowMatrix<double> generator_;
/**
* @brief These ones are for bookkeeping:
*/
mutable RowMatrix<double> pijt_;
mutable RowMatrix<double> dpijt_;
mutable RowMatrix<double> d2pijt_;
/**
* @brief The \f$U\f$ matrix made of left eigen vectors (by row).
*/
RowMatrix<double> leftEigenVectors_;
/**
* @brief The \f$U^-1\f$ matrix made of right eigen vectors (by column).
*/
RowMatrix<double> rightEigenVectors_;
/**
* @brief The vector of eigen values.
*/
Vdouble eigenValues_;
/**
* @brief The vector of the imaginary part of the eigen values.
*/
Vdouble iEigenValues_;
/**
* @brief boolean value for diagonalizability in R of the generator_
*/
bool isDiagonalizable_;
/**
* @brief The vector of equilibrium frequencies.
*/
Vdouble freq_;
/**
* @brief Tell if the eigen decomposition should be performed.
*/
bool eigenDecompose_;
public:
AbstractSubstitutionModel(const Alphabet* alpha, const std::string& prefix);
AbstractSubstitutionModel(const AbstractSubstitutionModel& model) :
AbstractParameterAliasable(model),
alphabet_(model.alphabet_),
size_(model.size_),
rate_(model.rate_),
chars_(model.chars_),
generator_(model.generator_),
pijt_(model.pijt_),
dpijt_(model.dpijt_),
d2pijt_(model.d2pijt_),
leftEigenVectors_(model.leftEigenVectors_),
rightEigenVectors_(model.rightEigenVectors_),
eigenValues_(model.eigenValues_),
iEigenValues_(model.iEigenValues_),
isDiagonalizable_(model.isDiagonalizable_),
freq_(model.freq_),
eigenDecompose_(model.eigenDecompose_)
{}
AbstractSubstitutionModel& operator=(const AbstractSubstitutionModel& model)
{
AbstractParameterAliasable::operator=(model);
alphabet_ = model.alphabet_;
size_ = model.size_;
rate_ = model.rate_;
chars_ = model.chars_;
generator_ = model.generator_;
pijt_ = model.pijt_;
dpijt_ = model.dpijt_;
d2pijt_ = model.d2pijt_;
leftEigenVectors_ = model.leftEigenVectors_;
rightEigenVectors_ = model.rightEigenVectors_;
eigenValues_ = model.eigenValues_;
iEigenValues_ = model.iEigenValues_;
isDiagonalizable_ = model.isDiagonalizable_;
freq_ = model.freq_;
eigenDecompose_ = model.eigenDecompose_;
return *this;
}
virtual ~AbstractSubstitutionModel() {}
#ifndef NO_VIRTUAL_COV
virtual AbstractSubstitutionModel* clone() const = 0;
#endif
public:
const Alphabet* getAlphabet() const { return alphabet_; }
const std::vector<int>& getAlphabetChars() const { return chars_; }
int getAlphabetChar(unsigned int i) const { return chars_[i]; }
std::vector<unsigned int> getModelStates(int i) const { return VectorTools::whichAll(chars_, i); }
virtual const Vdouble& getFrequencies() const { return freq_; }
const Matrix<double>& getGenerator() const { return generator_; }
virtual const Matrix<double>& getPij_t(double t) const;
virtual const Matrix<double>& getdPij_dt(double t) const;
virtual const Matrix<double>& getd2Pij_dt2(double t) const;
const Vdouble& getEigenValues() const { return eigenValues_; }
const Vdouble& getIEigenValues() const { return iEigenValues_; }
bool isDiagonalizable() const { return isDiagonalizable_;}
const Matrix<double>& getRowLeftEigenVectors() const { return leftEigenVectors_; }
const Matrix<double>& getColumnRightEigenVectors() const { return rightEigenVectors_; }
virtual double freq(unsigned int i) const { return freq_[i]; }
virtual double Qij(unsigned int i, unsigned int j) const { return generator_(i, j); }
virtual double Pij_t (unsigned int i, unsigned int j, double t) const { return getPij_t(t) (i, j); }
virtual double dPij_dt (unsigned int i, unsigned int j, double t) const { return getdPij_dt(t) (i, j); }
virtual double d2Pij_dt2(unsigned int i, unsigned int j, double t) const { return getd2Pij_dt2(t) (i, j); }
double getInitValue(unsigned int i, int state) const throw (BadIntException);
void setFreqFromData(const SequenceContainer& data, unsigned int pseudoCount = 0);
virtual void setFreq(std::map<int, double>&);
void enableEigenDecomposition(bool yn) { eigenDecompose_ = yn; }
bool enableEigenDecomposition() { return eigenDecompose_; }
/**
* @brief Tells the model that a parameter value has changed.
*
* This updates the matrices consequently.
*/
virtual void fireParameterChanged(const ParameterList& parameters)
{
AbstractParameterAliasable::fireParameterChanged(parameters);
if ((parameters.size()!=1) || (parameters[0].getName()!=getNamespace()+"rate"))
updateMatrices();
}
void addRateParameter();
protected:
/**
* @brief Diagonalize the \f$Q\f$ matrix, and fill the eigenValues_, iEigenValues_,
* leftEigenVectors_ and rightEigenVectors_ matrices.
*
* The generator_ matrix and freq_ vector must be initialized.
*
* Eigen values and vectors are computed from the generator and
* assigned to the eigenValues_ for the real part, iEigenValues_ for
* the imaginary part, rightEigenVectors_ and leftEigenVectors_
* variables. isDiagonalizable_ checks if the generator_ is
* diagonalizable in R.
*
* The optional rate parameter is not taken into account in this
* method to prevent unnecessary computation.
*/
virtual void updateMatrices();
public:
/**
* @brief Get the scalar product of diagonal elements of the generator
* and the frequencies vector.
* If the generator is normalized, then scale=1. Otherwise each element
* must be multiplied by 1/scale.
*
* @return Minus the scalar product of diagonal elements and the frequencies vector.
*/
double getScale() const;
/**
* @brief Set the rate of the generator, defined as the scalar
* product of diagonal elements of the generator and the frequencies
* vector.
*
* When the generator is normalized, scale=1. Otherwise each element
* is multiplied such that the correct scale is set.
*
*/
void setScale(double scale);
/**
* @brief Get the rate of the model
*/
virtual double getRate() const;
/**
* @brief Set the rate of the model (must be positive).
* @param rate must be positive.
*/
virtual void setRate(double rate);
friend class AbstractBiblioSubstitutionModel;
};
/**
* @brief Partial implementation of the ReversibleSubstitutionModel interface.
*
* This abstract class adds the exchangeability_ fields to the AbstractSubstitutionModel class.
* Access methods for this field is implemented.
*
* This class also overrides the updateMatrices() method, which
* updates the generator_ matrix from the exchangeability_ matrix and
* freq_ vector. It then computes eigen values and vectors and fills
* the corresponding vector (eigenValues_) and matrices
* (leftEigenVectors_ and rightEigenVectors_). Because of
* reversibility, isDiagonalizable_ is set to true.
*
* The freq_ vector and exchangeability_ matrices are hence the only things to provide to
* create a substitution model.
* It is also possible to redefine one of these methods for better efficiency.
* The Pij_t, dPij_dt and d2Pij_dt2 are particularly inefficient since the matrix formula
* is used to compute all probabilities, and then the result for the initial and final state
* of interest is retrieved.
*
* @note This class is dedicated to "simple" substitution models, for which the number of states is equivalent to the number of characters in the alphabet.
* Consider using the MarkovModulatedSubstitutionModel for more complexe cases.
*/
class AbstractReversibleSubstitutionModel :
public virtual AbstractSubstitutionModel,
public virtual ReversibleSubstitutionModel
{
protected:
/**
* @brief The exchangeability matrix \f$S\f$ of the model.
*/
RowMatrix<double> exchangeability_;
public:
AbstractReversibleSubstitutionModel(const Alphabet* alpha, const std::string& prefix) :
AbstractParameterAliasable(prefix),
AbstractSubstitutionModel(alpha, prefix),
exchangeability_(size_, size_) {
isDiagonalizable_=true;
}
virtual ~AbstractReversibleSubstitutionModel() {}
#ifndef NO_VIRTUAL_COV
virtual AbstractReversibleSubstitutionModel* clone() const = 0;
#endif
public:
const Matrix<double>& getExchangeabilityMatrix() const { return exchangeability_; }
double Sij(unsigned int i, unsigned int j) const { return exchangeability_(i, j); }
protected:
/**
* @brief Compute and diagonalize the \f$Q\f$ matrix, and fill the eigenValues_,
* leftEigenVectors_ and rightEigenVectors_ matrices.
*
* The exchangeability_ matrix and freq_ vector must be initialized.
* This function computes the generator_ matrix with the formula
* \f[
* Q = S \times \pi
* \f]
* where \f$Q\f$ is the generator matrix, \f$S\f$ is the exchangeability matrix and
* \f$Pi\f$ the diagonal matrix with frequencies.
*
* The generator is then scaled so that
* \f[
* \sum_i Q_{i,i} \times \pi_i = -1
* \f]
* (\f$\pi_i\f$ are the equilibrium frequencies).
*
* Eigen values and vectors are computed from the scaled generator and assigned to the
* eigenValues_, rightEigenVectors_ and leftEigenVectors_ variables.
*/
virtual void updateMatrices();
};
} //end of namespace bpp.
#endif //_ABSTRACTSUBSTITUTIONMODEL_H_
|