41 #include <Bpp/Numeric/Matrix/MatrixTools.h> 42 #include <Bpp/Numeric/Matrix/EigenValue.h> 43 #include <Bpp/Numeric/VectorTools.h> 46 #include <Bpp/Seq/Alphabet/WordAlphabet.h> 47 #include <Bpp/Seq/Alphabet/AlphabetTools.h> 48 #include <Bpp/Seq/Container/SequenceContainerTools.h> 50 #include <Bpp/App/ApplicationTools.h> 65 const std::vector<SubstitutionModel*>& modelVector,
66 const std::string& prefix) :
67 AbstractParameterAliasable(prefix),
72 Vrate_ (modelVector.size())
76 size_t n = modelVector.size();
83 while (!flag && i < (n - 1))
85 if (modelVector[i] == modelVector[j])
100 for (i = 0; i < n; i++)
105 addParameters_(
VSubMod_[i]->getParameters());
111 for (i = 0; i < n; i++)
115 t += TextTools::toString(i + 1);
118 addParameters_(
VSubMod_[0]->getParameters());
121 for (i = 0; i < n; i++)
123 Vrate_[i] = 1.0 /
static_cast<double>(n);
128 const Alphabet* alph,
130 const std::string& prefix) :
131 AbstractParameterAliasable(prefix),
133 new_alphabet_ (false),
144 const std::string& prefix) :
145 AbstractParameterAliasable(prefix),
147 new_alphabet_ (true),
156 for (i = 0; i < num; i++)
161 t += TextTools::toString(i + 1);
165 addParameters_(pmodel->getParameters());
170 AbstractParameterAliasable(wrsm),
172 new_alphabet_ (wrsm.new_alphabet_),
174 VnestedPrefix_(wrsm.VnestedPrefix_),
187 for (i = 0; i < num; i++)
196 AbstractParameterAliasable::operator=(model);
212 for (i = 0; i < num; i++)
228 for (
size_t i = 0; i <
VSubMod_.size(); i++)
246 vector<const Alphabet*> vAlph;
248 for (i = 0; i < modelVector.size(); i++)
253 return new WordAlphabet(vAlph);
258 AbstractSubstitutionModel::setNamespace(prefix);
263 for (
size_t i = 0; i <
VSubMod_.size(); i++)
265 t += TextTools::toString(i + 1);
271 for (
size_t i = 0; i <
VSubMod_.size(); i++)
286 VSubMod_[0]->matchParametersValues(getParameters());
288 for (
size_t i = 0; i <
VSubMod_.size(); i++)
290 VSubMod_[i]->matchParametersValues(getParameters());
302 size_t i, j, n, l, k, m;
304 vector<size_t> vsize;
306 for (k = 0; k < nbmod; k++)
311 RowMatrix<double> gk, exch;
315 for (k = nbmod; k > 0; k--)
317 gk =
VSubMod_[k - 1]->getGenerator();
318 for (i = 0; i < vsize[k - 1]; i++)
320 for (j = 0; j < vsize[k - 1]; j++)
327 for (l = 0; l < m; l++)
331 n += m * vsize[k - 1];
347 for (i = 0; i < salph; i++)
350 for (j = 0; j < salph; j++)
365 for (i = 0; i < salph; i++)
368 for (j = 0; j < salph; j++)
370 if ((i != j) && abs(
generator_(i, j)) > NumConstants::TINY())
378 vnull.push_back(flag);
383 size_t gi = 0, gj = 0;
385 RowMatrix<double> gk;
387 gk.resize(salph - nbStop, salph - nbStop);
388 for (i = 0; i < salph; i++)
393 for (j = 0; j < salph; j++)
407 EigenValue<double> ev(gk);
411 for (i = 0; i < nbStop; i++)
417 RowMatrix<double> rev = ev.getV();
420 for (i = 0; i < salph; i++)
425 for (j = 0; j < salph; j++)
434 for (j = 0; j < salph - nbStop; j++)
439 for (j = salph - nbStop; j < salph; j++)
476 size_t nulleigen = 0;
480 while (nulleigen < salph - nbStop)
517 for (i = 0; i < salph; i++)
521 for (i = 0; i < salph; i++)
524 for (i = 0; i < salph; i++)
530 ApplicationTools::displayMessage(
"Unable to find eigenvector for eigenvalue 1. Taylor series used instead.");
537 catch (ZeroDivisionException& e)
539 ApplicationTools::displayMessage(
"Singularity during diagonalization. Taylor series used instead.");
547 for (i = 1; i < salph; i++)
558 MatrixTools::getId(salph,
tmpMat_);
562 for (i = 0; i < salph; i++)
567 MatrixTools::getId(salph,
vPowGen_[0]);
573 for (i = 0; i < salph; i++)
577 for (i = 0; i < salph; i++)
589 for (i = 0; i <
size_; i++)
590 for (j = 0; j <
size_; j++)
597 map<int, double> tmpFreq;
600 size_t i, j, s, k, d, size;
605 s =
VSubMod_[0]->getAlphabet()->getSize();
606 for (j = 0; j < s; j++)
608 tmpFreq[
static_cast<int>(j)] = 0;
611 for (i = 0; i < nbmod; i++)
614 for (k = 0; k < size; k++)
616 tmpFreq[
static_cast<int>((k / d) % s)] += freqs[
static_cast<int>(k)];
620 for (k = 0; k < s; k++)
622 tmpFreq[
static_cast<int>(k)] /= static_cast<double>(nbmod);
626 matchParametersValues(
VSubMod_[0]->getParameters());
629 for (i = 0; i < nbmod; i++)
632 s =
VSubMod_[i]->getAlphabet()->getSize();
634 for (j = 0; j < s; j++)
636 tmpFreq[
static_cast<int>(j)] = 0;
638 for (k = 0; k < size; k++)
640 tmpFreq[
static_cast<int>((k / d) % s)] += freqs[
static_cast<int>(k)];
643 matchParametersValues(
VSubMod_[i]->getParameters());
Interface for all substitution models.
bool enableEigenDecomposition()
Tell if eigenValues and Vectors must be computed.
RowMatrix< double > exchangeability_
The exchangeability matrix of the model, defined as . When the model is reversible, this matrix is symetric.
bool isNonSingular_
boolean value for non-singularity of rightEigenVectors_
RowMatrix< double > rightEigenVectors_
The matrix made of right eigen vectors (by column).
Vdouble eigenValues_
The vector of eigen values.
static Alphabet * extractAlph(const std::vector< SubstitutionModel *> &modelVector)
std::vector< double > Vrate_
Vdouble freq_
The vector of equilibrium frequencies.
virtual size_t getNumberOfStates() const
Get the number of states.
AbstractSubstitutionModel & operator=(const AbstractSubstitutionModel &model)
Partial implementation of the SubstitutionModel interface.
AbstractWordSubstitutionModel(const std::vector< SubstitutionModel *> &modelVector, const std::string &prefix)
Build a new AbstractWordSubstitutionModel object from a vector of pointers to SubstitutionModels.
const Alphabet * alphabet_
The alphabet relevant to this model.
Abstract Basal class for words of substitution models.
RowMatrix< double > leftEigenVectors_
The matrix made of left eigen vectors (by row) if rightEigenVectors_ is non-singular.
AbstractWordSubstitutionModel & operator=(const AbstractWordSubstitutionModel &)
RowMatrix< double > generator_
The generator matrix of the model.
const Alphabet * getAlphabet() const
std::vector< RowMatrix< double > > vPowGen_
vector of the powers of generator_ for Taylor development (if rightEigenVectors_ is singular)...
virtual ~AbstractWordSubstitutionModel()
virtual void completeMatrices()=0
Called by updateMatrices to handle specific modifications for inheriting classes. ...
size_t size_
The size of the generator, i.e. the number of states.
virtual void setFreq(std::map< int, double > &freqs)
Estimation of the parameters of the models so that the equilibrium frequencies match the given ones...
void setNamespace(const std::string &prefix)
void updateMatrices()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
bool new_alphabet_
boolean flag to check if a specific WordAlphabet has been built
Map the states of a given alphabet which have a model state.
RowMatrix< double > tmpMat_
For computational issues.
std::vector< std::string > VnestedPrefix_
bool isDiagonalizable_
boolean value for diagonalizability in R of the generator_
Vdouble iEigenValues_
The vector of the imaginary part of the eigen values.
std::vector< SubstitutionModel * > VSubMod_