41 #include "../PatternTools.h" 42 #include "../Model/MixedSubstitutionModel.h" 43 #include "../TreeTools.h" 45 #include <Bpp/Numeric/NumConstants.h> 46 #include <Bpp/Text/TextTools.h> 47 #include <Bpp/App/ApplicationTools.h> 60 DiscreteDistribution* rDist,
67 upperNode_(tree.getRootId()),
70 if (!modelSet->isFullySetUpFor(tree))
71 throw Exception(
"RNonHomogeneousMixedTreeLikelihood(constructor). Model set is not fully specified.");
73 for (
size_t i=0;i<modelSet->getNumberOfHyperNodes();i++){
74 mvTreeLikelihoods_[tree.getRootId()].push_back(
new RNonHomogeneousMixedTreeLikelihood(tree, modelSet, modelSet->getHyperNode(i), upperNode_, rDist,
false, usePatterns));
82 const SiteContainer& data,
84 DiscreteDistribution* rDist,
91 upperNode_(tree.getRootId()),
94 if (!modelSet->isFullySetUpFor(tree))
95 throw Exception(
"RNonHomogeneousMixedTreeLikelihood(constructor). Model set is not fully specified.");
97 for (
size_t i=0;i<modelSet->getNumberOfHyperNodes();i++)
98 mvTreeLikelihoods_[tree.getRootId()].push_back(
new RNonHomogeneousMixedTreeLikelihood(tree, data, modelSet, modelSet->getHyperNode(i), upperNode_, rDist,
false, usePatterns));
107 DiscreteDistribution* rDist,
111 mvTreeLikelihoods_(),
112 hyperNode_(hyperNode),
113 upperNode_(upperNode),
117 throw Exception(
"RNonHomogeneousMixedTreeLikelihood(constructor). Model set is not fully specified.");
125 const SiteContainer& data,
129 DiscreteDistribution* rDist,
133 mvTreeLikelihoods_(),
134 hyperNode_(hyperNode),
135 upperNode_(upperNode),
139 throw Exception(
"RNonHomogeneousMixedTreeLikelihood(constructor). Model set is not fully specified.");
148 std::vector<int> vDesc;
153 const SiteContainer* pdata =
getData();
159 while (vDesc.size() != 0) {
166 std::map<int, vector<int> > mdesc;
168 for (
size_t i = 0; i < vson.size(); i++)
175 for (
size_t i = 0; i < nbmodels; i++)
185 std::map<int, vector<int> >::iterator it;
186 for (it = mdesc.begin(); it != mdesc.end(); it++)
188 for (
size_t j = 0; j < it->second.size(); j++)
190 if (it->second[j] != it->first)
192 if (find(vn.begin(), vn.end(), it->second[j]) != vn.end())
194 flag += (find(vn.begin(), vn.end(), it->first) != vn.end()) ? 2 : 1;
199 else if (find(vn.begin(), vn.end(), it->first) != vn.end())
206 vExpMod.push_back(static_cast<int>(i));
210 if (vExpMod.size() != 0)
212 std::map<int, int> mapmodels;
214 for (vector<int>::iterator it = vExpMod.begin(); it != vExpMod.end(); it++)
217 ttmodels *=
static_cast<size_t>(mapmodels[*it]);
220 for (
size_t i = 0; i < ttmodels; i++)
222 int s =
static_cast<int>(i);
225 for (
size_t j = 0; j < nbmodels; j++)
227 if ((
hyperNode_.
getNode(j).
size() >= 1) && find(vExpMod.begin(), vExpMod.end(),
static_cast<int>(j)) != vExpMod.end())
230 s /= mapmodels[
static_cast<int>(j)];
240 pr->resetParameters_();
245 for (
size_t i = 0; i < vson.size(); i++)
247 vDesc.push_back(vson[i]);
258 mvTreeLikelihoods_(),
259 hyperNode_(lik.hyperNode_),
260 upperNode_(lik.upperNode_),
263 map<int, vector<RNonHomogeneousMixedTreeLikelihood*> >::const_iterator it;
266 for (
size_t i = 0; i < it->second.size(); i++)
285 map<int, vector<RNonHomogeneousMixedTreeLikelihood*> >::const_iterator it;
288 for (
size_t i = 0; i < it->second.size(); i++)
303 map<int, vector<RNonHomogeneousMixedTreeLikelihood*> >::iterator it;
306 for (
size_t i = 0; i < it->second.size(); i++)
308 delete it->second[i];
323 map<int, vector<RNonHomogeneousMixedTreeLikelihood*> >::iterator it;
326 for (
size_t i = 0; i < it->second.size(); i++)
328 it->second[i]->initialize();
342 for (
size_t i = 0; i <
nbNodes_; i++)
344 int id =
nodes_[i]->getId();
347 const Parameter* rootBrLen = &getParameter(
"BrLenRoot");
348 const Parameter* rootPos = &getParameter(
"RootPosition");
349 nodes_[i]->setDistanceToFather(rootBrLen->getValue() * rootPos->getValue());
353 const Parameter* rootBrLen = &getParameter(
"BrLenRoot");
354 const Parameter* rootPos = &getParameter(
"RootPosition");
355 nodes_[i]->setDistanceToFather(rootBrLen->getValue() * (1. - rootPos->getValue()));
359 const Parameter* brLen = &getParameter(
string(
"BrLen") + TextTools::toString(i));
360 if (brLen)
nodes_[i]->setDistanceToFather(brLen->getValue());
365 map<int, vector<RNonHomogeneousMixedTreeLikelihood*> >::const_iterator it2;
367 for (
size_t i = 0; i < it2->second.size(); i++){
368 (it2->second)[i]->
setProbability((dynamic_cast<MixedSubstitutionModelSet*>(
modelSet_))->getHyperNodeProbability((it2->second)[i]->getHyperNode()));
377 if (params.getCommonParametersWith(
rateDistribution_->getIndependentParameters()).size() > 0)
385 for (
size_t i = 0; i < tmp.size(); i++)
388 ids = VectorTools::vectorUnion(ids, tmpv);
391 vector<const Node*> nodes;
392 for (
size_t i = 0; i < ids.size(); i++)
396 vector<const Node*> tmpv;
398 for (
size_t i = 0; i < tmp.size(); i++)
400 if (tmp[i] ==
"BrLenRoot" || tmp[i] ==
"RootPosition")
404 tmpv.push_back(
tree_->getRootNode()->getSon(0));
405 tmpv.push_back(
tree_->getRootNode()->getSon(1));
410 tmpv.push_back(
nodes_[TextTools::to < size_t > (tmp[i].substr(5))]);
412 nodes = VectorTools::vectorUnion(nodes, tmpv);
414 for (
size_t i = 0; i < nodes.size(); i++){
419 map<int, vector<RNonHomogeneousMixedTreeLikelihood*> >::iterator it;
422 for (
size_t i = 0; i < it->second.size(); i++)
424 it->second[i]->matchParametersValues(params);
440 map<int, vector<RNonHomogeneousMixedTreeLikelihood*> >::iterator it;
441 for (it = mvTreeLikelihoods_.begin(); it != mvTreeLikelihoods_.end(); it++)
443 for (
size_t i = 0; i < it->second.size(); i++)
445 it->second[i]->setData(sites);
478 for (
size_t i = 0; i < nbSites; i++)
481 VVdouble* _likelihoods_node_i = &(*_likelihoods_node)[i];
485 Vdouble* _likelihoods_node_i_c = &(*_likelihoods_node_i)[c];
489 (*_likelihoods_node_i_c)[x] = 0.;
498 for (
size_t t = 0; t < vr.size(); t++)
502 for (
size_t t = 0; t < vr.size(); t++)
504 VVVdouble* _vt_likelihoods_node = &vr[t]->likelihoodData_->getLikelihoodArray(nodeId);
505 for (
size_t i = 0; i < nbSites; i++)
508 VVdouble* _likelihoods_node_i = &(*_likelihoods_node)[i];
512 Vdouble* _likelihoods_node_i_c = &(*_likelihoods_node_i)[c];
513 Vdouble* _vt_likelihoods_node_i_c = &(*_vt_likelihoods_node)[i][c];
516 (*_likelihoods_node_i_c)[x] += (*_vt_likelihoods_node_i_c)[x] * vr[t]->getProbability()/
getProbability();
539 const Node* father, father2;
543 father =
tree_->getRootNode();
545 if ((variable ==
"BrLenRoot") || (variable ==
"RootPosition"))
546 father =
tree_->getRootNode();
549 size_t brI = TextTools::to<size_t>(variable.substr(5));
567 int fatherId = father->
getId();
571 size_t nbSites = _dLikelihoods_father->size();
572 for (
size_t i = 0; i < nbSites; i++) {
573 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
575 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
577 (*_dLikelihoods_father_i_c)[s] = 0.;
584 for (
size_t t = 0; t < vr.size(); t++)
589 for (
size_t t = 0; t < vr.size(); t++) {
590 VVVdouble* _vt_dLikelihoods_father = &vr[t]->likelihoodData_->getDLikelihoodArray(fatherId);
591 for (
size_t i = 0; i < nbSites; i++){
593 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
596 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
597 Vdouble* _vt_dLikelihoods_father_i_c = &(*_vt_dLikelihoods_father)[i][c];
599 (*_dLikelihoods_father_i_c)[x] += (*_vt_dLikelihoods_father_i_c)[x] * vr[t]->getProbability()/
getProbability();
630 const Node* father, father2;
633 father =
tree_->getRootNode();
635 if ((variable ==
"BrLenRoot") || (variable ==
"RootPosition"))
636 father =
tree_->getRootNode();
639 size_t brI = TextTools::to<size_t>(variable.substr(5));
659 int fatherId = father->
getId();
663 size_t nbSites = _d2Likelihoods_father->size();
664 for (
size_t i = 0; i < nbSites; i++) {
665 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
667 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
669 (*_d2Likelihoods_father_i_c)[s] = 0.;
677 for (
size_t t = 0; t < vr.size(); t++)
681 for (
size_t t = 0; t < vr.size(); t++) {
682 VVVdouble* _vt_d2Likelihoods_father = &vr[t]->likelihoodData_->getD2LikelihoodArray(fatherId);
683 for (
size_t i = 0; i < nbSites; i++) {
685 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
688 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
689 Vdouble* _vt_d2Likelihoods_father_i_c = &(*_vt_d2Likelihoods_father)[i][c];
691 (*_d2Likelihoods_father_i_c)[x] += (*_vt_d2Likelihoods_father_i_c)[x] * vr[t]->getProbability() /
getProbability();
726 vector<const SubstitutionModel*> vModel;
727 vector<double> vProba;
730 if (nd.
size() == 0) {
731 vModel.push_back(model);
737 for (
size_t i = 0; i < nd.
size(); ++i){
738 vModel.push_back(mmodel->
getNModel(static_cast<size_t>(nd[i])));
743 for (
size_t i = 0; i < nd.
size(); ++i)
749 VVVdouble* pxy__node = &
pxy_[node->
getId()];
751 VVdouble* pxy__node_c = &(*pxy__node)[c];
753 Vdouble* pxy__node_c_x = &(*pxy__node_c)[x];
755 (*pxy__node_c_x)[y] = 0;
759 for (
size_t i=0;i<vModel.size();i++){
760 RowMatrix<double> Q = vModel[i]->getPij_t(l *
rateDistribution_->getCategory(c));
762 Vdouble* pxy__node_c_x = &(*pxy__node_c)[x];
764 (*pxy__node_c_x)[y] += vProba[i] * Q(x, y);
772 VVVdouble* dpxy__node = &
dpxy_[node->
getId()];
775 VVdouble* dpxy__node_c = &(*dpxy__node)[c];
778 Vdouble* dpxy__node_c_x = &(*dpxy__node_c)[x];
780 (*dpxy__node_c_x)[y] = 0;
784 for (
size_t i=0;i<vModel.size();i++){
785 RowMatrix<double> dQ = vModel[i]->getdPij_dt(l * rc);
788 Vdouble* dpxy__node_c_x = &(*dpxy__node_c)[x];
790 (*dpxy__node_c_x)[y] += vProba[i] * rc * dQ(x, y);
801 VVdouble* d2pxy__node_c = &(*d2pxy__node)[c];
803 Vdouble* d2pxy__node_c_x = &(*d2pxy__node_c)[x];
805 (*d2pxy__node_c_x)[y] = 0;
810 for (
size_t i=0;i<vModel.size();i++){
811 RowMatrix<double> d2Q = vModel[i]->getd2Pij_dt2(l * rc);
813 Vdouble* d2pxy__node_c_x = &(*d2pxy__node_c)[x];
815 (*d2pxy__node_c_x)[y] += vProba[i] * rc * rc * d2Q(x, y);
virtual void computeDownSubtreeD2Likelihood(const Node *)
bool isFullySetUpFor(const Tree &tree, bool throwEx=true) const
Check if the model set is fully specified for a given tree.
virtual void computeDownSubtreeDLikelihood(const Node *)
RNonHomogeneousMixedTreeLikelihood(const Tree &tree, MixedSubstitutionModelSet *modelSet, const MixedSubstitutionModelSet::HyperNode &hyperNode, int upperNode, DiscreteDistribution *rDist, bool verbose, bool usePatterns)
Build a new RNonHomogeneousMixeTreeLikelihood object without data.
This class implement the 'traditional' way of computing likelihood for a tree, allowing for non-homog...
void computeTreeD2Likelihood(const string &variable)
void setModel(size_t nM, const Vint &vnS)
sets submodel numbers in the nMth mixed model. Checks if all the numbers are valid.
Interface for all substitution models.
virtual void computeTreeD2Likelihood(const std::string &variable)
const SubstitutionModel * getModelForNode(int nodeId) const
Get the model associated to a particular node id.
bool main_
a flag to say if this object is the head of the hierarchy
ParameterList brLenParameters_
std::vector< Node * > nodes_
Pointer toward all nodes in the tree.
void setProbability(double x)
sets the probability of this object in the hierarchy
virtual void computeDownSubtreeDLikelihood(const Node *)
virtual double getNProbability(size_t i) const =0
Returns the probability of a specific model from the mixture.
RNonHomogeneousMixedTreeLikelihood & operator=(const RNonHomogeneousMixedTreeLikelihood &lik)
size_t getModelIndexForNode(int nodeId) const
Get the index in the set of the model associated to a particular node id.
SubstitutionModelSet * modelSet_
virtual void computeAllTransitionProbabilities()
Fill the pxy_, dpxy_ and d2pxy_ arrays for all nodes.
const SiteContainer * getData() const
Get the dataset for which the likelihood must be evaluated.
void setProbability(double x)
sets the probability
virtual void applyParameters()
All parameters are stored in a parameter list. This function apply these parameters to the substituti...
std::vector< double > getRootFrequencies() const
void init(bool usePatterns)
double getProbability() const
returns the probability of this object in the hierarchy
const Node & getNode(size_t i) const
virtual const Node * getFather() const
Get the father of this node is there is one.
Interface for phylogenetic tree objects.
RNonHomogeneousTreeLikelihood & operator=(const RNonHomogeneousTreeLikelihood &lik)
map< int, vector< RNonHomogeneousMixedTreeLikelihood * > > mvTreeLikelihoods_
the map of the branch numbers to the vectors of the TreeLikelihoods for the expanded model on this br...
std::vector< double > rootFreqs_
virtual bool isLeaf() const
ParameterList getNodeParameters() const
Get the parameters corresponding attached to the nodes of the tree.
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
void computeTreeDLikelihood(const string &variable)
void initialize()
Init the likelihood object.
VVVdouble & getDLikelihoodArray(int nodeId)
virtual void computeTreeLikelihood()
virtual void initParameters()
This builds the parameters list from all parametrizable objects, i.e. substitution model...
virtual int getId() const
Get the node's id.
MixedSubstitutionModelSet::HyperNode hyperNode_
A specific HyperNode in which the computation is processed. If the probability of this HyperNode is -...
virtual void computeDownSubtreeD2Likelihood(const Node *)
void setData(const SiteContainer &sites)
Set the dataset for which the likelihood must be evaluated.
void computeTransitionProbabilitiesForNode(const Node *node)
Fill the pxy_, dpxy_ and d2pxy_ arrays for one node.
double getProbability() const
returns the probability
bool computeFirstOrderDerivatives_
Substitution models manager for Mixed Substitution Models. This class inherits from SubstitutionModel...
std::map< int, VVVdouble > d2pxy_
virtual void computeTreeDLikelihood(const std::string &variable)
void fireParameterChanged(const ParameterList ¶ms)
void setData(const SiteContainer &sites)
Set the dataset for which the likelihood must be evaluated.
virtual void computeSubtreeLikelihood(const Node *node)
Compute the likelihood for a subtree defined by the Tree::Node node.
TreeTemplate< Node > * tree_
virtual void initBranchLengthsParameters()
std::map< int, const Node * > idToNode_
An index linking nodes to their id, for faster access than the getNode() method.
DRASRTreeLikelihoodData * likelihoodData_
The phylogenetic node class.
int upperNode_
the number of the node under which tree the Treelikelihood is computed.
virtual void computeSubtreeLikelihood(const Node *node)
Compute the likelihood for a subtree defined by the Tree::Node node.
const Tree & getTree() const
Get the tree (topology and branch lengths).
double getLogLikelihood() const
Get the logarithm of the likelihood for the whole dataset.
virtual ~RNonHomogeneousMixedTreeLikelihood()
std::map< int, VVVdouble > dpxy_
const std::vector< int > & getNodesWithModel(size_t i) const
Get a list of nodes id for which the given model is associated.
DiscreteDistribution * rateDistribution_
size_t getNumberOfModels() const
virtual std::vector< int > getSonsId(int parentId) const =0
void initialize()
Init the likelihood object.
VVVdouble & getLikelihoodArray(int nodeId)
std::vector< int > getNodesWithParameter(const std::string &name) const
VVVdouble & getD2LikelihoodArray(int nodeId)
virtual const SubstitutionModel * getNModel(size_t i) const =0
Returns a specific model from the mixture.
bool computeSecondOrderDerivatives_
std::map< int, VVVdouble > pxy_
Interface for Substitution models, defined as a mixture of "simple" substitution models.