44 #include <Bpp/Utils/MapTools.h> 50 AbstractParameterAliasable(set),
51 alphabet_ (set.alphabet_),
52 nbStates_ (set.nbStates_),
53 modelSet_(set.modelSet_.size()),
54 rootFrequencies_(set.stationarity_ ? 0 : dynamic_cast<
FrequenciesSet*>(set.rootFrequencies_->clone())),
55 nodeToModel_ (set.nodeToModel_),
56 modelToNodes_ (set.modelToNodes_),
57 modelParameters_ (set.modelParameters_),
58 stationarity_ (set.stationarity_)
61 for (
size_t i = 0; i <
set.modelSet_.size(); i++)
69 AbstractParameterAliasable::operator=(
set);
83 for (
size_t i = 0; i <
set.modelSet_.size(); i++)
94 for (
size_t i = 0; i <
modelSet_.size(); i++)
116 throw (ParameterNotFoundException)
118 if (!(hasParameter(name)))
119 throw ParameterNotFoundException(
"SubstitutionModelSet::getNodesWithParameter.", name);
121 vector<string> nalias=getAlias(name);
122 size_t p=name.rfind(
"_");
123 vector<int> inode = getNodesWithModel(TextTools::to<size_t>(name.substr(p+1,string::npos)) - 1);
125 for (
size_t i = 0; i < nalias.size(); i++)
127 p=nalias[i].rfind(
"_");
128 vector<int> ni = getNodesWithModel(TextTools::to<size_t>(nalias[i].substr(p+1,string::npos))-1);
129 inode.insert(inode.end(),ni.begin(),ni.end());
138 throw Exception(
"SubstitutionModelSet::addModel. A Substitution Model cannot be added to a Model Set if it does not have the same alphabet.");
140 throw Exception(
"SubstitutionModelSet::addModel. A Substitution Model cannot be added to a Model Set if it does not have the same number of states.");
144 size_t thisModelIndex =
modelSet_.size() - 1;
147 for (
size_t i = 0; i < nodesId.size(); i++)
156 vector<string> nplm=model->getParameters().getParameterNames();
160 for (
size_t i = 0; i < nplm.size(); i++)
163 Parameter* p =
new Parameter(model->getParameters().getParameter(pname));
164 p->setName(pname +
"_" + TextTools::toString(thisModelIndex+1));
171 delete modelSet_[modelIndex];
172 modelSet_[modelIndex]=model;
177 ParameterList pl=getNodeParameters();
179 for (
size_t i = pl.size(); i>0; i--)
181 string pn=pl[i-1].getName();
183 size_t pu=pn.rfind(
"_");
184 int nm=TextTools::toInt(pn.substr(pu+1,string::npos));
185 if (nm==(
int)modelIndex+1){
186 vector<string> alpn=getAlias(pn);
187 for (
unsigned j=0; j<alpn.size(); j++)
189 unaliasParameters(alpn[j],pn);
195 deleteParameter_(i-1);
202 vector<string> nplm=model->getParameters().getParameterNames();
204 for (
size_t i = 0; i < nplm.size(); i++)
207 Parameter* p =
new Parameter(model->getParameters().getParameter(pname));
208 p->setName(pname +
"_" + TextTools::toString(modelIndex+1));
214 modelParameters_[modelIndex].reset();
215 modelParameters_[modelIndex]=*model->getParameters().clone();
220 for (
size_t i = 0; i <
modelSet_.size(); i++)
222 out <<
"Model " << i + 1 <<
": " <<
modelSet_[i]->getName() <<
"\t attached to nodes ";
233 AbstractParameterAliasable::fireParameterChanged(parameters);
252 vector<size_t> index = MapTools::getValues(nodeToModel_);
253 for (
size_t i = 0; i < modelSet_.size(); i++)
255 if (!VectorTools::contains(index, i))
257 if (throwEx)
throw Exception(
"SubstitutionModelSet::checkOrphanModels(). Model '" + TextTools::toString(i + 1) +
"' is associated to no node.");
267 vector<int> ids = tree.getNodesId();
268 int rootId = tree.getRootId();
269 for (
size_t i = 0; i < ids.size(); i++)
271 if (ids[i] != rootId && nodeToModel_.find(ids[i]) == nodeToModel_.end())
273 if (throwEx)
throw Exception(
"SubstitutionModelSet::checkOrphanNodes(). Node '" + TextTools::toString(ids[i]) +
"' in tree has no model associated.");
283 vector<int> ids = tree.getNodesId();
285 int rootId = tree.getRootId();
286 for (
size_t i = 0; i < modelToNodes_.size(); i++)
288 for (
size_t j = 0; j < modelToNodes_[i].size(); j++)
290 id = modelToNodes_[i][j];
291 if (
id == rootId || !VectorTools::contains(ids,
id))
293 if (throwEx)
throw Exception(
"SubstitutionModelSet::checkUnknownNodes(). Node '" + TextTools::toString(
id) +
"' is not found in tree or is the root node.");
305 if (dynamic_cast<const MixedSubstitutionModel*>(
getModel(i)) != NULL)
std::map< size_t, std::vector< int > > modelToNodes_
Substitution models manager for non-homogeneous / non-reversible models of evolution.
Interface for all substitution models.
const SubstitutionModel * getModel(size_t i) const
Get one model from the set knowing its index.
void setRootFrequencies(FrequenciesSet *rootFreqs)
Sets a given FrequenciesSet for root frequencies.
virtual const Alphabet * getAlphabet() const =0
void listModelNames(std::ostream &out=std::cout) const
Interface for phylogenetic tree objects.
Parametrize a set of state frequencies.
void addModel(SubstitutionModel *model, const std::vector< int > &nodesId)
Add a new model to the set, and set relationships with nodes and params.
std::map< int, size_t > nodeToModel_
Contains for each node in a tree the index of the corresponding model in modelSet_.
std::vector< ParameterList > modelParameters_
Parameters for each model in the set.
void clear()
Resets all the information contained in this object.
void replaceModel(size_t modelIndex, SubstitutionModel *model)
Replace a model in the set, and all corresponding parameters. The replaced model deleted.
std::auto_ptr< FrequenciesSet > rootFrequencies_
Root frequencies.
std::vector< SubstitutionModel * > modelSet_
Contains all models used in this tree.
bool checkOrphanModels(bool throwEx) const
virtual void fireParameterChanged(const ParameterList ¶meters)
void updateRootFrequencies()
size_t getNumberOfModels() const
SubstitutionModelSet & operator=(const SubstitutionModelSet &set)
virtual size_t getNumberOfStates() const =0
Get the number of states.
SubstitutionModel * clone() const =0
SubstitutionModelSet(const Alphabet *alpha)
Create a model set according to the specified alphabet. Stationarity is assumed.
bool hasMixedSubstitutionModel() const
std::vector< int > getNodesWithParameter(const std::string &name) const
const Alphabet * alphabet_
A pointer toward the common alphabet to all models in the set.
bool checkOrphanNodes(const Tree &tree, bool throwEx) const
bool checkUnknownNodes(const Tree &tree, bool throwEx) const