7 #include "../Model/MixedTransitionModel.h"
17 for (
const auto& it : modelToNodes_)
19 vMN.push_back(it.first);
27 return collection().model(n);
32 return collection().getModel(n);
37 return collection().getModel(n);
42 return collection().getModelScenario(nPath_);
47 return collection().getModelScenario(nPath_);
52 return collection().getRateDistribution(nDist_);
57 return collection().getRateDistribution(nDist_);
62 return collection().rateDistribution(nDist_);
67 return collection().rateDistribution(nDist_);
72 return collection().getRateDistributionParameters(nDist_, independent);
78 return collection().getBranchLengthParameters(nTree_, independent);
86 return collection().getRootFrequenciesParameters(nRoot_, independent);
94 addParameters_(getSubstitutionModelParameters(
true));
95 addParameters_(getRootFrequenciesParameters(
true));
96 addParameters_(getRateDistributionParameters(
true));
97 addParameters_(getBranchLengthParameters(
true));
118 std::map<size_t, std::vector<unsigned int>>::const_iterator it;
119 for (it = modelToNodes_.begin(); it != modelToNodes_.end(); it++)
121 pl.
includeParameters(getCollection()->getSubstitutionModelParameters(it->first, independent));
133 return collection().getFrequencySet(nRoot_);
141 return collection().getFrequencySet(nRoot_);
147 throw NullPointerException(
"SubstitutionProcessCollectionMember::rootFrequencySet(). No root frequency parameters, this is a stationary model.");
149 return collection().frequencySet(nRoot_);
155 throw NullPointerException(
"SubstitutionProcessCollectionMember::rootFrequencySet(). No root frequency parameters, this is a stationary model.");
157 return collection().frequencySet(nRoot_);
162 auto model = dynamic_pointer_cast<const TransitionModelInterface>(getCollection()->getModel(modelToNodes_.begin()->first));
164 if (isStationary() && model)
165 return model->getFrequencies();
167 return collection().frequencySet(nRoot_).getFrequencies();
172 if (!getCollection()->hasModelScenario(numPath))
173 throw Exception(
"SubstitutionProcessCollectionMember::setModelScenario: Collection does not have ModelScenario number");
175 auto modelScenario = getCollection()->getModelScenario(numPath);
179 auto models = modelScenario->getModels();
181 auto modnum = getModelNumbers();
183 for (
const auto& model:models)
186 for (
auto num:modnum)
188 if (getModel(num) == model)
205 if (collection().hasTreeNumber(nTree_))
206 return collection().tree(nTree_);
208 throw Exception(
"SubstitutionProcessCollectionMember::parametrizablePhyloTree(). No associated tree.");
213 return collection().hasTreeNumber(nTree_) ? collection().getTree(nTree_) :
nullptr;
218 if (collection().hasTreeNumber(nTree_))
219 return collection().tree(nTree_);
221 throw Exception(
"SubstitutionProcessCollectionMember::parametrizablePhyloTree(). No associated tree.");
226 return collection().hasTreeNumber(nTree_) ? collection().getTree(nTree_) :
nullptr;
231 if (!collection().hasTreeNumber(nTree))
232 throw BadIntException((
int)nTree,
"SubstitutionProcessCollectionMember::setTreeNumber(). No associated tree.", getAlphabet());
234 deleteParameters_(getBranchLengthParameters(
true).getParameterNames());
238 addParameters_(getBranchLengthParameters(
true));
246 auto& nmod = getCollection()->model(numModel);
248 if (modelToNodes_.size() > 0)
250 auto& modi = getCollection()->model(modelToNodes_.begin()->first);
251 if (nmod.getAlphabet()->getAlphabetType() != modi.getAlphabet()->getAlphabetType())
252 throw Exception(
"SubstitutionProcessCollectionMember::addModel. A Substitution Model cannot be added to a Model Set if it does not have the same alphabet.");
253 if (nmod.getNumberOfStates() != modi.getNumberOfStates())
254 throw Exception(
"SubstitutionProcessCollectionMember::addModel. A Substitution Model cannot be added to a Model Set if it does not have the same number of states.");
256 else if (!isStationary())
258 auto& freq = getCollection()->frequencySet(nRoot_);
259 if (freq.getAlphabet()->getAlphabetType() != nmod.getAlphabet()->getAlphabetType())
260 throw Exception(
"SubstitutionProcessCollectionMember::addModel. A Substitution Model cannot be added to a Model Set if it does not have the same alphabet as the root frequencies.");
261 if (freq.getFrequencies().size() != nmod.getNumberOfStates())
262 throw Exception(
"SubstitutionProcessCollectionMember::addModel. A Substitution Model cannot be added to a Model Set if it does not have the same number of states as the root frequencies.");
266 for (
size_t i = 0; i < nodesId.size(); i++)
268 nodeToModel_[nodesId[i]] = numModel;
269 modelToNodes_[numModel].push_back(nodesId[i]);
278 auto& freq = getCollection()->frequencySet(numFreq);
279 if (modelToNodes_.size() > 0)
281 auto& modi = getCollection()->model(modelToNodes_.begin()->first);
283 if (freq.getAlphabet()->getAlphabetType() != modi.getAlphabet()->getAlphabetType())
284 throw Exception(
"SubstitutionProcessCollectionMember::setRootFrequencies. A Frequencies Set cannot be added to a Model Set if it does not have the same alphabet as the models.");
285 if (freq.getFrequencies().size() != modi.getNumberOfStates())
286 throw Exception(
"SubstitutionProcessCollectionMember::setRootFrequencies. A Frequencies Set cannot be added to a Model Set if it does not have the same number of states as the models.");
296 if (!getParametrizablePhyloTree())
299 throw Exception(
"SubstitutionProcessCollectionMember::checkOrphanNodes(). No Tree");
304 vector<unsigned int> ids = getParametrizablePhyloTree()->getAllNodesIndexes();
305 unsigned int rootId = getParametrizablePhyloTree()->getNodeIndex(getParametrizablePhyloTree()->getRoot());
306 for (
size_t i = 0; i < ids.size(); i++)
308 if (ids[i] != rootId && nodeToModel_.find(ids[i]) == nodeToModel_.end())
311 throw Exception(
"SubstitutionProcessCollectionMember::checkOrphanNodes(). Node '" +
TextTools::toString(ids[i]) +
"' in tree has no model associated.");
320 if (!getParametrizablePhyloTree())
323 throw Exception(
"SubstitutionProcessCollectionMember::checkUnknownNodes(). No Tree");
328 vector<unsigned int> ids = getParametrizablePhyloTree()->getAllNodesIndexes();
330 unsigned int rootId = getParametrizablePhyloTree()->getNodeIndex(getParametrizablePhyloTree()->getRoot());
332 for (
auto& it : modelToNodes_)
334 for (
auto id : it.second)
339 throw Exception(
"SubstitutionProcessCollectionMember::checkUnknownNodes(). Node '" +
TextTools::toString(
id) +
"' is not found in tree or is the root node.");
349 for (
auto& it : modelToNodes_)
351 if (dynamic_pointer_cast<const MixedTransitionModelInterface>(collection().getModel(it.first)))
361 collection().matchParametersValues(parameters);
370 if (classIndex >= rateDistribution().getNumberOfCategories())
371 throw IndexOutOfBoundsException(
"SubstitutionProcessCollectionMember::getProbabilityForModel.", classIndex, 0, getRateDistribution()->getNumberOfCategories());
372 return rateDistribution().getProbability(classIndex);
379 for (
size_t i = 0; i < rateDistribution().getNumberOfCategories(); i++)
381 vProb.push_back(rateDistribution().getProbability(i));
389 if (classIndex >= getRateDistribution()->getNumberOfCategories())
390 throw IndexOutOfBoundsException(
"SubstitutionProcessCollectionMember::getRateForModel.", classIndex, 0, getRateDistribution()->getNumberOfCategories());
391 return rateDistribution().getCategory(classIndex);
bool matchParametersValues(const ParameterList ¶meters) override
Interface for all Branch models.
Parametrize a set of state frequencies.
virtual void includeParameters(const ParameterList ¶ms)
PhyloTree with Parametrizable Phylo Branches. They SHARE their branch length parameters.
void updateParameters()
sets the parameters as the independent parameters on the objects
std::vector< size_t > getModelNumbers() const override
std::shared_ptr< const DiscreteDistributionInterface > getRateDistribution() const override
Get a pointer to the rate distribution (or null if there is no rate distribution).
const DiscreteDistributionInterface & rateDistribution() const override
Get the rate distribution.
const FrequencySetInterface & rootFrequencySet() const override
bool hasMixedTransitionModel() const
ParameterList getBranchLengthParameters(bool independent) const override
Get the parameters of the tree.
void setTreeNumber(size_t nTree, bool check=true)
double getRateForModel(size_t classIndex) const override
bool matchParametersValues(const ParameterList ¶meters) override
AbsractParametrizable interface.
const BranchModelInterface & model(size_t n) const override
std::shared_ptr< const ModelScenario > getModelScenario() const override
Get the Model Scenario associated with this process, in case there are mixture models involved.
void addModel(size_t numModel, const std::vector< unsigned int > &nodesId)
Add a new model to the set, and set relationships with nodes.
std::shared_ptr< const FrequencySetInterface > getRootFrequencySet() const override
std::shared_ptr< const BranchModelInterface > getModel(size_t n) const override
ParameterList getSubstitutionModelParameters(bool independent) const override
Get the parameters of the substitution models.
const std::vector< double > & getRootFrequencies() const override
ParameterList getRateDistributionParameters(bool independent) const override
Get the parameters of the rate distribution.
std::shared_ptr< const ParametrizablePhyloTree > getParametrizablePhyloTree() const override
void setModelScenario(size_t numPath)
Vdouble getClassProbabilities() const override
bool checkUnknownNodes(bool throwEx) const
const ParametrizablePhyloTree & parametrizablePhyloTree() const override
bool checkOrphanNodes(bool throwEx) const
void setRootFrequencies(size_t numFreq)
Set the root Frequencies Set.
ParameterList getNonDerivableParameters() const override
get all NonDerivable parameters.
ParameterList getRootFrequenciesParameters(bool independent) const override
Get the parameters of the root frequencies set.
double getProbabilityForModel(size_t classIndex) const override
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble