9 #include "../../Model/MixedTransitionModel.h"
10 #include "../../Tree/TreeTools.h"
11 #include "../../PatternTools.h"
25 std::shared_ptr<MixedSubstitutionModelSet> modelSet,
26 std::shared_ptr<DiscreteDistributionInterface> rDist,
32 upperNode_(tree.getRootId()),
35 if (!modelSet->isFullySetUpFor(
tree))
36 throw Exception(
"RNonHomogeneousMixedTreeLikelihood(constructor). Model set is not fully specified.");
38 for (
size_t i = 0; i < modelSet->getNumberOfHyperNodes(); ++i)
41 shared_ptr<RNonHomogeneousMixedTreeLikelihood>(
43 tree, modelSet, modelSet->getHyperNode(i),
55 std::shared_ptr<MixedSubstitutionModelSet> modelSet,
56 std::shared_ptr<DiscreteDistributionInterface> rDist,
62 upperNode_(tree.getRootId()),
65 if (!modelSet->isFullySetUpFor(
tree))
66 throw Exception(
"RNonHomogeneousMixedTreeLikelihood(constructor). Model set is not fully specified.");
68 for (
size_t i = 0; i < modelSet->getNumberOfHyperNodes(); ++i)
71 shared_ptr<RNonHomogeneousMixedTreeLikelihood>(
73 tree,
data, modelSet, modelSet->getHyperNode(i),
84 std::shared_ptr<MixedSubstitutionModelSet> modelSet,
87 std::shared_ptr<DiscreteDistributionInterface> rDist,
92 hyperNode_(hyperNode),
93 upperNode_(upperNode),
96 if (!modelSet->isFullySetUpFor(
tree))
97 throw Exception(
"RNonHomogeneousMixedTreeLikelihood(constructor). Model set is not fully specified.");
107 std::shared_ptr<MixedSubstitutionModelSet> modelSet,
110 std::shared_ptr<DiscreteDistributionInterface> rDist,
114 mvTreeLikelihoods_(),
115 hyperNode_(hyperNode),
116 upperNode_(upperNode),
119 if (!modelSet->isFullySetUpFor(
tree))
120 throw Exception(
"RNonHomogeneousMixedTreeLikelihood(constructor). Model set is not fully specified.");
129 std::vector<int> vDesc;
132 size_t nbmodels =
modelSet_->getNumberOfModels();
138 while (vDesc.size() != 0)
146 std::map<int, vector<int>> mdesc;
148 for (
size_t i = 0; i < vson.size(); i++)
155 for (
size_t i = 0; i < nbmodels; i++)
165 std::map<int, vector<int>>::iterator it;
166 for (it = mdesc.begin(); it != mdesc.end(); it++)
168 for (
size_t j = 0; j < it->second.size(); j++)
170 if (it->second[j] != it->first)
172 if (find(vn.begin(), vn.end(), it->second[j]) != vn.end())
174 flag += (find(vn.begin(), vn.end(), it->first) != vn.end()) ? 2 : 1;
179 else if (find(vn.begin(), vn.end(), it->first) != vn.end())
186 vExpMod.push_back(
static_cast<int>(i));
190 if (vExpMod.size() != 0)
192 std::map<int, int> mapmodels;
194 for (vector<int>::iterator it = vExpMod.begin(); it != vExpMod.end(); it++)
196 mapmodels[*it] =
static_cast<int>(
hyperNode_.
getNode(
static_cast<size_t>(*it)).size());
197 ttmodels *=
static_cast<size_t>(mapmodels[*it]);
200 for (
size_t i = 0; i < ttmodels; i++)
202 int s =
static_cast<int>(i);
205 for (
size_t j = 0; j < nbmodels; j++)
207 if ((
hyperNode_.
getNode(j).
size() >= 1) && find(vExpMod.begin(), vExpMod.end(),
static_cast<int>(j)) != vExpMod.end())
210 s /= mapmodels[
static_cast<int>(j)];
214 shared_ptr<RNonHomogeneousMixedTreeLikelihood> pr;
218 pr = shared_ptr<RNonHomogeneousMixedTreeLikelihood>(
222 dynamic_pointer_cast<MixedSubstitutionModelSet>(
modelSet_),
229 pr = shared_ptr<RNonHomogeneousMixedTreeLikelihood>(
232 dynamic_pointer_cast<MixedSubstitutionModelSet>(
modelSet_),
237 pr->resetParameters_();
242 for (
size_t i = 0; i < vson.size(); ++i)
244 vDesc.push_back(vson[i]);
255 mvTreeLikelihoods_(),
256 hyperNode_(lik.hyperNode_),
257 upperNode_(lik.upperNode_),
262 for (
size_t i = 0; i < it.second.size(); ++i)
265 make_shared<RNonHomogeneousMixedTreeLikelihood>(*it.second[i])
285 for (
size_t i = 0; i < it.second.size(); ++i)
288 make_shared<RNonHomogeneousMixedTreeLikelihood>(*it.second[i])
315 for (
size_t i = 0; i < it.second.size(); ++i)
317 it.second[i]->initialize();
332 for (
size_t i = 0; i <
nbNodes_; i++)
334 int id =
nodes_[i]->getId();
358 for (
size_t i = 0; i < it2.second.size(); ++i)
361 dynamic_pointer_cast<MixedSubstitutionModelSet>(
modelSet_)->getHyperNodeProbability(
362 (it2.second)[i]->getHyperNode()
386 for (
size_t i = 0; i < tmp.size(); i++)
388 vector<int> tmpv =
modelSet_->getNodesWithParameter(tmp[i]);
392 vector<const Node*> nodes;
393 for (
size_t i = 0; i < ids.size(); ++i)
397 vector<const Node*> tmpv;
399 for (
size_t i = 0; i < tmp.size(); ++i)
401 if (tmp[i] ==
"BrLenRoot" || tmp[i] ==
"RootPosition")
405 tmpv.push_back(
tree_->getRootNode()->getSon(0));
406 tmpv.push_back(
tree_->getRootNode()->getSon(1));
411 tmpv.push_back(
nodes_[TextTools::to< size_t >(tmp[i].substr(5))]);
415 for (
size_t i = 0; i < nodes.size(); ++i)
423 for (
size_t i = 0; i < it.second.size(); ++i)
425 it.second[i]->matchParametersValues(params);
444 for (
size_t i = 0; i < it.second.size(); ++i)
446 it.second[i]->setData(sites);
481 for (
size_t i = 0; i < nbSites; ++i)
484 VVdouble* _likelihoods_node_i = &(*_likelihoods_node)[i];
488 Vdouble* _likelihoods_node_i_c = &(*_likelihoods_node_i)[c];
492 (*_likelihoods_node_i_c)[x] = 0.;
501 for (
size_t t = 0; t < vr.size(); t++)
503 vr[t]->computeSubtreeLikelihood(node);
507 for (
size_t t = 0; t < vr.size(); t++)
509 VVVdouble* _vt_likelihoods_node = &vr[t]->likelihoodData_->getLikelihoodArray(nodeId);
510 for (
size_t i = 0; i < nbSites; i++)
513 VVdouble* _likelihoods_node_i = &(*_likelihoods_node)[i];
517 Vdouble* _likelihoods_node_i_c = &(*_likelihoods_node_i)[c];
518 Vdouble* _vt_likelihoods_node_i_c = &(*_vt_likelihoods_node)[i][c];
521 (*_likelihoods_node_i_c)[x] += (*_vt_likelihoods_node_i_c)[x] * vr[t]->getProbability() /
getProbability();
544 const Node* father, father2;
548 father =
tree_->getRootNode();
551 if ((variable ==
"BrLenRoot") || (variable ==
"RootPosition"))
552 father =
tree_->getRootNode();
555 size_t brI = TextTools::to<size_t>(variable.substr(5));
576 int fatherId = father->
getId();
580 size_t nbSites = _dLikelihoods_father->size();
581 for (
size_t i = 0; i < nbSites; i++)
583 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
586 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
589 (*_dLikelihoods_father_i_c)[s] = 0.;
597 for (
size_t t = 0; t < vr.size(); t++)
599 vr[t]->computeTreeDLikelihood(variable);
604 for (
size_t t = 0; t < vr.size(); t++)
606 VVVdouble* _vt_dLikelihoods_father = &vr[t]->likelihoodData_->getDLikelihoodArray(fatherId);
607 for (
size_t i = 0; i < nbSites; i++)
610 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
614 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
615 Vdouble* _vt_dLikelihoods_father_i_c = &(*_vt_dLikelihoods_father)[i][c];
618 (*_dLikelihoods_father_i_c)[x] += (*_vt_dLikelihoods_father_i_c)[x] * vr[t]->getProbability() /
getProbability();
649 const Node* father, father2;
652 father =
tree_->getRootNode();
655 if ((variable ==
"BrLenRoot") || (variable ==
"RootPosition"))
656 father =
tree_->getRootNode();
659 size_t brI = TextTools::to<size_t>(variable.substr(5));
680 int fatherId = father->
getId();
684 size_t nbSites = _d2Likelihoods_father->size();
685 for (
size_t i = 0; i < nbSites; i++)
687 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
690 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
693 (*_d2Likelihoods_father_i_c)[s] = 0.;
701 for (
size_t t = 0; t < vr.size(); t++)
703 vr[t]->computeTreeD2Likelihood(variable);
707 for (
size_t t = 0; t < vr.size(); t++)
709 VVVdouble* _vt_d2Likelihoods_father = &vr[t]->likelihoodData_->getD2LikelihoodArray(fatherId);
710 for (
size_t i = 0; i < nbSites; i++)
713 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
717 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
718 Vdouble* _vt_d2Likelihoods_father_i_c = &(*_vt_d2Likelihoods_father)[i][c];
721 (*_d2Likelihoods_father_i_c)[x] += (*_vt_d2Likelihoods_father_i_c)[x] * vr[t]->getProbability() /
getProbability();
756 vector< shared_ptr<const TransitionModelInterface>> vModel;
757 vector<double> vProba;
762 vModel.push_back(model);
767 auto mmodel = dynamic_pointer_cast<const MixedTransitionModelInterface>(model);
769 for (
size_t i = 0; i < nd.
size(); ++i)
771 vModel.push_back(shared_ptr<TransitionModelInterface>(mmodel->nModel(
static_cast<size_t>(nd[i])).clone()));
772 vProba.push_back(mmodel->getNProbability(
static_cast<size_t>(nd[i])));
776 for (
size_t i = 0; i < nd.
size(); ++i)
787 VVdouble* pxy__node_c = &(*pxy__node)[c];
790 Vdouble* pxy__node_c_x = &(*pxy__node_c)[x];
793 (*pxy__node_c_x)[y] = 0;
797 for (
size_t i = 0; i < vModel.size(); i++)
802 Vdouble* pxy__node_c_x = &(*pxy__node_c)[x];
805 (*pxy__node_c_x)[y] += vProba[i] * Q(x, y);
818 VVdouble* dpxy__node_c = &(*dpxy__node)[c];
822 Vdouble* dpxy__node_c_x = &(*dpxy__node_c)[x];
825 (*dpxy__node_c_x)[y] = 0;
829 for (
size_t i = 0; i < vModel.size(); i++)
835 Vdouble* dpxy__node_c_x = &(*dpxy__node_c)[x];
838 (*dpxy__node_c_x)[y] += vProba[i] * rc* dQ(x, y);
851 VVdouble* d2pxy__node_c = &(*d2pxy__node)[c];
854 Vdouble* d2pxy__node_c_x = &(*d2pxy__node_c)[x];
857 (*d2pxy__node_c_x)[y] = 0;
862 for (
size_t i = 0; i < vModel.size(); i++)
867 Vdouble* d2pxy__node_c_x = &(*d2pxy__node_c)[x];
870 (*d2pxy__node_c_x)[y] += vProba[i] * rc* rc* d2Q(x, y);
std::shared_ptr< DiscreteDistributionInterface > rateDistribution_
std::map< int, VVVdouble > d2pxy_
virtual void initParameters()
This builds the parameters list from all parametrizable objects, i.e. substitution model,...
std::shared_ptr< SubstitutionModelSet > modelSet_
virtual void initBranchLengthsParameters(bool verbose=true)
void initialize() override
Init the likelihood object.
virtual void computeAllTransitionProbabilities()
Fill the pxy_, dpxy_ and d2pxy_ arrays for all nodes.
std::map< int, VVVdouble > dpxy_
std::map< int, const Node * > idToNode_
An index linking nodes to their id, for faster access than the getNode() method.
std::vector< Node * > nodes_
Pointer toward all nodes in the tree.
ParameterList brLenParameters_
virtual void applyParameters()
All parameters are stored in a parameter list. This function apply these parameters to the substituti...
std::map< int, VVVdouble > pxy_
std::vector< double > rootFreqs_
const Parameter & parameter(const std::string &name) const override
virtual void includeParameters_(const ParameterList ¶meters)
const AlignmentDataInterface & data() const
Get the dataset for which the likelihood must be evaluated.
const Tree & tree() const
Get the tree (topology and branch lengths).
std::shared_ptr< TreeTemplate< Node > > tree_
bool hasLikelihoodData() const
bool computeFirstOrderDerivatives_
bool computeSecondOrderDerivatives_
void setProbability(double x)
sets the probability
double getProbability() const
returns the probability
void setModel(size_t nM, const Vuint &vnS)
sets submodel numbers in the nMth mixed model. Checks if all the numbers are valid.
const Node & getNode(size_t i) const
The phylogenetic node class.
virtual int getId() const
Get the node's id.
virtual const Node * getFather() const
Get the father of this node is there is one.
virtual bool isLeaf() const
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
virtual ParameterList getCommonParametersWith(const ParameterList ¶ms) const
virtual std::vector< std::string > getParameterNames() const
virtual double getValue() const
void computeTransitionProbabilitiesForNode(const Node *node)
Fill the pxy_, dpxy_ and d2pxy_ arrays for one node.
void computeTreeD2Likelihood(const std::string &variable)
virtual void computeSubtreeLikelihood(const Node *node)
Compute the likelihood for a subtree defined by the Tree::Node node.
void init(bool usePatterns)
RNonHomogeneousMixedTreeLikelihood & operator=(const RNonHomogeneousMixedTreeLikelihood &lik)
std::map< int, std::vector< std::shared_ptr< RNonHomogeneousMixedTreeLikelihood > > > mvTreeLikelihoods_
the map of the branch numbers to the vectors of the TreeLikelihoods for the expanded model on this br...
void setData(const AlignmentDataInterface &sites)
Set the dataset for which the likelihood must be evaluated.
virtual void computeDownSubtreeD2Likelihood(const Node *)
void computeTreeDLikelihood(const std::string &variable)
void fireParameterChanged(const ParameterList ¶ms)
double getProbability() const
returns the probability of this object in the hierarchy
void setProbability(double x)
sets the probability of this object in the hierarchy
MixedSubstitutionModelSet::HyperNode hyperNode_
A specific HyperNode in which the computation is processed. If the probability of this HyperNode is -...
bool main_
a flag to say if this object is the head of the hierarchy
virtual ~RNonHomogeneousMixedTreeLikelihood()
virtual void computeDownSubtreeDLikelihood(const Node *)
int upperNode_
the number of the node under which tree the Treelikelihood is computed.
void initialize()
Init the likelihood object.
This class implement the 'traditional' way of computing likelihood for a tree, allowing for non-homog...
virtual void computeDownSubtreeD2Likelihood(const Node *)
virtual void computeTreeDLikelihood(const std::string &variable)
RNonHomogeneousTreeLikelihood & operator=(const RNonHomogeneousTreeLikelihood &lik)
double getLogLikelihood() const
Get the logarithm of the likelihood for the whole dataset.
void setData(const AlignmentDataInterface &sites)
Set the dataset for which the likelihood must be evaluated.
std::shared_ptr< DRASRTreeLikelihoodData > likelihoodData_
virtual void computeDownSubtreeDLikelihood(const Node *)
friend class RNonHomogeneousMixedTreeLikelihood
virtual void computeTreeD2Likelihood(const std::string &variable)
virtual void computeSubtreeLikelihood(const Node *node)
Compute the likelihood for a subtree defined by the Tree::Node node.
virtual void computeTreeLikelihood()
Interface for phylogenetic tree objects.
virtual std::vector< int > getSonsId(int parentId) const =0
virtual int getRootId() const =0
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble
std::vector< unsigned int > Vuint