41 #include <Bpp/Numeric/NumConstants.h> 42 #include <Bpp/Numeric/Prob/ConstantDistribution.h> 43 #include <Bpp/Exceptions.h> 52 const Alphabet* alpha,
54 std::map<std::string, DiscreteDistribution*> parametersDistributionsList,
56 int tto)
throw (Exception) :
57 AbstractParameterAliasable(model->getNamespace()),
63 if (to_ >=
int(alpha->getSize()))
64 throw BadIntegerException(
"Bad state in alphabet", to_);
65 if (from_ >=
int(alpha->getSize()))
66 throw BadIntegerException(
"Bad state in alphabet", from_);
70 map<string, DiscreteDistribution*>::iterator it;
74 vector<string> parnames = model->getParameters().getParameterNames();
76 for (i = 0; i < model->getNumberOfParameters(); i++)
79 s2 = model->getParameterNameWithoutNamespace(s1);
81 if (parametersDistributionsList.find(s2) != parametersDistributionsList.end())
82 distributionMap_[s1] = parametersDistributionsList.find(s2)->second->clone();
84 distributionMap_[ s1] =
new ConstantDistribution(model->getParameterValue(s2));
87 if (dynamic_cast<ConstantDistribution*>(distributionMap_[s1]) == 0)
88 distributionMap_[s1]->setNamespace(s1 +
"_" + distributionMap_[s1]->getNamespace());
90 distributionMap_[s1]->setNamespace(s1 +
"_");
97 for (it = distributionMap_.begin(); it != distributionMap_.end(); it++)
99 c *= it->second->getNumberOfCategories();
102 for (i = 0; i < c; i++)
104 modelsContainer_.push_back(model->clone());
105 modelsContainer_[i]->addRateParameter();
106 modelsContainer_[i]->setNamespace(model->getNamespace());
108 vProbas_.push_back(1.0 / static_cast<double>(c));
109 vRates_.push_back(1.0);
115 DiscreteDistribution* pd;
117 for (it = distributionMap_.begin(); it != distributionMap_.end(); it++)
119 pm = model->getParameter(model->getParameterNameWithoutNamespace(getParameterNameWithoutNamespace(it->first)));
122 if (pm.hasConstraint())
123 pd->restrictToConstraint(*pm.getConstraint());
125 if (!dynamic_cast<ConstantDistribution*>(it->second))
127 const ParameterList pl = pd->getParameters();
128 for (i = 0; i != it->second->getNumberOfParameters(); i++)
130 addParameter_(pl[i].clone());
134 addParameter_(
new Parameter(it->first, pd->getCategory(0), (pd->getParameter(
"value").getConstraint()) ? pd->getParameter(
"value").getConstraint()->clone() : 0,
true));
140 AbstractParameterAliasable(msm),
146 map<string, DiscreteDistribution*>::const_iterator it;
156 AbstractParameterAliasable::operator=(msm);
165 map<string, DiscreteDistribution*>::const_iterator it;
176 map<string, DiscreteDistribution*>::iterator it;
198 map<string, DiscreteDistribution*>::iterator it;
207 if (dynamic_cast<ConstantDistribution*>(it->second) == NULL)
209 vector<string> vDistnames = it->second->getParameters().getParameterNames();
210 for (i = 0; i < it->second->getNumberOfParameters(); i++)
212 d = getParameterValue(getParameterNameWithoutNamespace(vDistnames[i]));
213 pl.addParameter(Parameter(vDistnames[i], d));
215 it->second->matchParametersValues(pl);
220 t = it->second->getNamespace();
221 d = getParameter(getParameterNameWithoutNamespace(t.substr(0, t.length() - 1))).getValue();
222 it->second->setParameterValue(
"value", d);
233 l = j % it->second->getNumberOfCategories();
235 d = it->second->getCategory(l);
236 vProbas_[i] *= it->second->getProbability(l);
237 if (pl.hasParameter(s))
238 pl.setParameterValue(s, d);
240 pl.addParameter(Parameter(s, d));
242 j = j / it->second->getNumberOfCategories();
281 vector<string> parnames =
modelsContainer_[0]->getParameters().getParameterNames();
282 std::map<std::string, size_t> msubn;
283 map<string, DiscreteDistribution*>::const_iterator it;
285 StringTokenizer st(desc,
",");
286 while (st.hasMoreToken())
288 string param = st.nextToken();
289 string::size_type index = param.rfind(
"_");
290 if (index == string::npos)
291 throw Exception(
"MixtureOfASubstitutionModel::getSubmodelNumbers parameter description should contain a number" + param);
292 msubn[param.substr(0, index)] = TextTools::to<size_t>(param.substr(index + 1, 4)) - 1;
307 l = j % it->second->getNumberOfCategories();
309 if (msubn.find(s) != msubn.end())
316 j = j / it->second->getNumberOfCategories();
319 submodnb.push_back(static_cast<int>(i));
Vint getSubmodelNumbers(std::string &desc) const
Interface for all substitution models.
MixtureOfASubstitutionModel & operator=(const MixtureOfASubstitutionModel &)
std::map< std::string, DiscreteDistribution * > distributionMap_
virtual void setVRates(const Vdouble &vd)
Sets the rates of the submodels to be proportional to a given vector, with the constraint that the me...
Vdouble freq_
The vector of equilibrium frequencies.
virtual ~MixtureOfASubstitutionModel()
Partial implementation for Mixed Substitution models, defined as a mixture of "simple" substitution m...
void updateMatrices()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
AbstractMixedSubstitutionModel & operator=(const AbstractMixedSubstitutionModel &)
Substitution models defined as a mixture of nested substitution models.
const DiscreteDistribution * getDistribution(std::string &parName) const
returns the DiscreteDistribution associated with a given parameter name.
double Qij(size_t i, size_t j) const
This function can not be applied here, so it is defined to prevent wrong usage.
virtual const SubstitutionModel * getNModel(size_t i) const
Returns a specific model from the mixture.
MixtureOfASubstitutionModel(const Alphabet *alpha, SubstitutionModel *model, std::map< std::string, DiscreteDistribution *> parametersDistributionsList, int ffrom=-1, int tto=-1)
void setFreq(std::map< int, double > &)
sets the eq frequencies of the first nested model, and adapts the parameters at best to it (surely th...
virtual size_t getNumberOfStates() const
From SubstitutionModel interface.
std::vector< SubstitutionModel * > modelsContainer_
vector of pointers to SubstitutionModels.
std::vector< double > vProbas_
vector of the probabilities of the models