8 #include "../Model/MixedTransitionModel.h"
19 modelSet_(set.modelSet_.size()),
21 nodeToModel_ (set.nodeToModel_),
22 modelToNodes_ (set.modelToNodes_),
23 modelParameters_ (set.modelParameters_)
26 for (
size_t i = 0; i < set.
modelSet_.size(); i++)
32 for (
size_t i = 0; i <
modelSet_.size(); i++)
34 modelScenario_->changeModel(dynamic_pointer_cast<MixedTransitionModelInterface>(set.
modelSet_[i]), dynamic_pointer_cast<MixedTransitionModelInterface>(
modelSet_[i]));
54 for (
size_t i = 0; i < set.
modelSet_.size(); i++)
60 for (
size_t i = 0; i <
modelSet_.size(); i++)
62 modelScenario_->changeModel(dynamic_pointer_cast<MixedTransitionModelInterface>(set.
modelSet_[i]), dynamic_pointer_cast<MixedTransitionModelInterface>(
modelSet_[i]));
84 vector<unsigned int> vNod;
85 vNod.push_back(nodeNumber);
92 throw Exception(
"NonHomogeneousSubstitutionProcess::addModel. A Substitution Model cannot be added to a Substitution Process if it does not have the same alphabet.");
94 throw Exception(
"NonHomogeneousSubstitutionProcess::addModel. A Substitution Model cannot be added to a Substitution Process if it does not have the same number of states.");
97 size_t thisModelIndex =
modelSet_.size() - 1;
102 for (
size_t i = 0; i < nodesId.size(); i++)
112 for (
size_t i = 0; i < pl.
size(); i++)
123 throw Exception(
"NonHomogeneousSubstitutionProcess::setModel. A Substitution Model cannot be added to a Substitution Process if it does not have the same alphabet.");
125 throw Exception(
"NonHomogeneousSubstitutionProcess::setModel. A Substitution Model cannot be added to a Substitution Process if it does not have the same number of states.");
134 for (
size_t i = 0; i < pl1.
size(); i++)
143 for (
size_t i = 0; i < pl.
size(); i++)
153 for (
size_t i = 0; i <
modelSet_.size(); i++)
155 out <<
"Model " << i + 1 <<
": " <<
modelSet_[i]->getName() <<
"\t attached to nodes ";
168 rDist_->matchParametersValues(parameters);
211 throw Exception(
"NonHomogeneousSubstitutionProcess::checkOrphanNodes(). No tree provided.");
217 for (
size_t i = 0; i < ids.size(); i++)
222 throw Exception(
"NonHomogeneousSubstitutionProcess::checkOrphanNodes(). Node '" +
TextTools::toString(ids[i]) +
"' in tree has no model associated.");
234 throw Exception(
"NonHomogeneousSubstitutionProcess::checkUnknownNodes(). No tree provided.");
241 map<size_t, vector<unsigned int>>::const_iterator it;
245 for (
size_t j = 0; j < it->second.size(); j++)
251 throw Exception(
"NonHomogeneousSubstitutionProcess::checkUnknownNodes(). Node '" +
TextTools::toString(
id) +
"' is not found in tree or is the root node.");
263 if (dynamic_pointer_cast<const MixedTransitionModelInterface>(
getModel(i)) !=
nullptr)
272 auto vmod = modelscenario->getModels();
277 throw Exception(
"NonHomogeneousSubstitutionProcess::setModelScenario: unknown model " + mod->getName());
285 shared_ptr<BranchModelInterface> model,
286 shared_ptr<DiscreteDistributionInterface> rdist,
287 shared_ptr<PhyloTree> tree,
288 shared_ptr<FrequencySetInterface> rootFreqs,
289 shared_ptr<ModelScenario> scenario)
292 throw Exception(
"NonHomogeneousSubstitutionProcess::createHomogeneousSubstitutionProcess: missing tree.");
298 unique_ptr<AutonomousSubstitutionProcessInterface> modelSet;
306 modelSet->setRootFrequencySet(rootFreqs);
309 modelSet->setModelScenario(scenario);
315 shared_ptr<BranchModelInterface> model,
316 shared_ptr<DiscreteDistributionInterface> rdist,
317 shared_ptr<PhyloTree> tree,
318 shared_ptr<FrequencySetInterface> rootFreqs,
319 const vector<string>& globalParameterNames,
320 shared_ptr<ModelScenario> scenario)
323 throw Exception(
"NonHomogeneousSubstitutionProcess::createNonHomogeneousSubstitutionProcess: missing tree.");
328 if (dynamic_pointer_cast<MixedTransitionModelInterface>(
model) !=
nullptr)
329 throw Exception(
"createNonHomogeneousSubstitutionProcess not yet programmed for mixture models.");
334 vector<string> globalParameterNames2;
339 for (i = 0; i < globalParameterNames.size(); i++)
341 if (globalParameterNames[i].find(
"*") != string::npos)
343 for (j = 0; j < globalParameters.
size(); j++)
347 string parn = globalParameters[j].getName();
357 pos2 = parn.find(g, pos1);
358 if (pos2 == string::npos)
363 pos1 = pos2 + g.length();
366 ((g.length() == 0) || (pos1 == parn.length()) || (parn.rfind(g) == parn.length() - g.length())))
367 globalParameterNames2.push_back(parn);
370 else if (!globalParameters.
hasParameter(globalParameterNames[i]))
371 throw Exception(
"NonHomogeneousSubstitutionProcess::createNonHomogeneousModelSet. Parameter '" + globalParameterNames[i] +
"' is not valid.");
373 globalParameterNames2.push_back(globalParameterNames[i]);
377 for (i = globalParameters.
size(); i > 0; i--)
379 if (find(globalParameterNames2.begin(), globalParameterNames2.end(), globalParameters[i - 1].getName()) == globalParameterNames2.end())
387 auto modelSet = make_unique<NonHomogeneousSubstitutionProcess>(rdist, tree, rootFreqs);
390 vector<unsigned int> ids = tree->getAllNodesIndexes();
391 unsigned int rootId = tree->getRootIndex();
393 for (i = 0; i < ids.size(); ++i)
395 if (ids[i] == rootId)
402 ids.erase(ids.begin() + (
long)pos);
403 sort(ids.begin(), ids.end());
405 for (i = 0; i < ids.size(); i++)
407 modelSet->addModel(shared_ptr<BranchModelInterface>(
model->
clone()), vector<unsigned int>(1, ids[i]));
411 for (i = 0; i < globalParameters.
size(); i++)
413 string pname = globalParameters[i].getName();
415 for (
size_t nn = 1; nn < ids.size(); nn++)
422 throw Exception(
"NonHomogeneousSubstitutionProcess::createNonHomogeneousModelSet : setModelScenario(scenario) to be implemented.");
A partial implementation of the SubstitutionProcess interface.
std::shared_ptr< ModelScenario > modelScenario_
void fireParameterChanged(const ParameterList &pl)
AbsractParametrizable interface.
std::shared_ptr< const ParametrizablePhyloTree > getParametrizablePhyloTree() const
AbstractAutonomousSubstitutionProcess & operator=(const AbstractAutonomousSubstitutionProcess &asp)
void deleteParameter_(size_t index)
void addParameter_(Parameter *parameter)
bool hasIndependentParameter(const std::string &name) const
AbstractParameterAliasable & operator=(const AbstractParameterAliasable &ap)
double getParameterValue(const std::string &name) const override
size_t getNumberOfStates() const
std::shared_ptr< const Alphabet > getAlphabet() const
virtual std::string getAlphabetType() const=0
BranchModelInterface * clone() const =0
virtual size_t getNumberOfStates() const =0
Get the number of states.
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
virtual const Alphabet & alphabet() const =0
Substitution process manager for non-homogeneous / non-reversible models of evolution.
std::vector< std::shared_ptr< BranchModelInterface > > modelSet_
Contains all models used in this tree.
std::map< unsigned int, size_t > nodeToModel_
Contains for each node in a tree the index of the corresponding model in modelSet_.
std::shared_ptr< DiscreteDistributionInterface > rDist_
Rate Distribution.
void setModel(std::shared_ptr< BranchModelInterface > model, size_t modelIndex)
Change a given model.
void fireParameterChanged(const ParameterList ¶meters) override
void clear()
Resets all the information contained in this object.
const BranchModelInterface & model(size_t n) const override
void setModelScenario(std::shared_ptr< ModelScenario > modelscenario) override
Set the modelPath, after checking it is valid (ie modelpath has only the model of the process).
void listModelNames(std::ostream &out=std::cout) const
list all model names.
std::map< size_t, std::vector< unsigned int > > modelToNodes_
bool checkUnknownNodes(bool throwEx) const
ParameterList getSubstitutionModelParameters(bool independent) const override
Get the INDEPENDENT parameters corresponding to the models.
NonHomogeneousSubstitutionProcess(std::shared_ptr< DiscreteDistributionInterface > rdist, std::shared_ptr< const PhyloTree > tree=0, std::shared_ptr< FrequencySetInterface > rootFreqs=0)
Create a model set according to the specified alphabet and root frequencies. Stationarity is not assu...
bool checkOrphanNodes(bool throwEx) const
void addModel(std::shared_ptr< BranchModelInterface > model, const std::vector< unsigned int > &nodesId)
Add a new model to the set, and set relationships with nodes and params.
size_t getNumberOfModels() const override
static std::unique_ptr< NonHomogeneousSubstitutionProcess > createNonHomogeneousSubstitutionProcess(std::shared_ptr< BranchModelInterface > model, std::shared_ptr< DiscreteDistributionInterface > rdist, std::shared_ptr< PhyloTree > tree, std::shared_ptr< FrequencySetInterface > rootFreqs, const std::vector< std::string > &globalParameterNames, std::shared_ptr< ModelScenario > scenario=0)
Create a NonHomogeneousSubstitutionProcess object, with one model per branch.
static std::unique_ptr< AutonomousSubstitutionProcessInterface > createHomogeneousSubstitutionProcess(std::shared_ptr< BranchModelInterface > model, std::shared_ptr< DiscreteDistributionInterface > rdist, std::shared_ptr< PhyloTree > tree, std::shared_ptr< FrequencySetInterface > rootFreqs=0, std::shared_ptr< ModelScenario > scenario=0)
Create a NonHomogeneousSubstitutionProcess object, corresponding to the homogeneous case.
void setModelToNode(size_t modelIndex, unsigned int nodeNumber)
Associate an existing model with a given node.
std::vector< ParameterList > modelParameters_
Parameters for each model in the set.
NonHomogeneousSubstitutionProcess & operator=(const NonHomogeneousSubstitutionProcess &set)
bool hasMixedTransitionModel() const
std::shared_ptr< const BranchModelInterface > getModel(size_t n) const override
virtual const ParameterList & getIndependentParameters() const=0
virtual bool hasParameter(const std::string &name) const
virtual void addParameter(const Parameter ¶m)
ParameterList * clone() const
virtual void deleteParameter(const std::string &name)
virtual void setName(const std::string &name)
virtual const std::string & getName() const
Space and time homogeneous substitution process, without mixture.
const std::string & nextToken()
bool hasMoreToken() const
std::string toString(T t)
Defines the basic types of data flow nodes.