5 #include "../../PatternTools.h"
27 std::shared_ptr<TransitionModelInterface> model,
28 std::shared_ptr<DiscreteDistributionInterface> rDist,
43 std::shared_ptr<TransitionModelInterface> model,
44 std::shared_ptr<DiscreteDistributionInterface> rDist,
117 l *=
std::pow((*lik)[i], (
int)(*w)[i]);
133 la[i] = (*w)[i] *
log((*lik)[i]);
136 sort(la.begin(), la.end());
198 if (
rateDistribution_->getParameters().getCommonParametersWith(params).size() > 0
199 ||
model_->getParameters().getCommonParametersWith(params).size() > 0)
204 else if (params.
size() > 0)
207 for (
size_t i = 0; i < params.
size(); i++)
209 string s = params[i].getName();
210 if (s.substr(0, 5) ==
"BrLen")
236 throw Exception(
"DRHomogeneousTreeLikelihood::getValue(). Instance is not initialized.");
254 double dLi, dLic, dLicx;
258 VVdouble* likelihoods_father_node_i = &(*likelihoods_father_node)[i];
263 Vdouble* likelihoods_father_node_i_c = &(*likelihoods_father_node_i)[c];
264 Vdouble* larray_i_c = &(*larray_i)[c];
265 VVdouble* dpxy_node_c = &(*dpxy_node)[c];
269 Vdouble* dpxy_node_c_x = &(*dpxy_node_c)[x];
273 dLicx += (*dpxy_node_c_x)[y] * (*likelihoods_father_node_i_c)[y];
275 dLicx *= (*larray_i_c)[x];
281 (*dLikelihoods_node)[i] = dLi / (*rootLikelihoodsSR)[i];
290 for (
size_t k = 0; k <
nbNodes_; k++)
304 throw Exception(
"Derivatives respective to rate distribution parameters are not implemented.");
308 throw Exception(
"Derivatives respective to substitution model parameters are not implemented.");
316 size_t brI = TextTools::to<size_t>(variable.substr(5));
323 d += (*w)[i] * (*dLikelihoods_branch)[i];
342 double d2Li, d2Lic, d2Licx;
346 VVdouble* likelihoods_father_node_i = &(*likelihoods_father_node)[i];
351 Vdouble* likelihoods_father_node_i_c = &(*likelihoods_father_node_i)[c];
352 Vdouble* larray_i_c = &(*larray_i)[c];
353 VVdouble* d2pxy_node_c = &(*d2pxy_node)[c];
357 Vdouble* d2pxy_node_c_x = &(*d2pxy_node_c)[x];
361 d2Licx += (*d2pxy_node_c_x)[y] * (*likelihoods_father_node_i_c)[y];
363 d2Licx *= (*larray_i_c)[x];
368 (*d2Likelihoods_node)[i] = d2Li / (*rootLikelihoodsSR)[i];
376 for (
size_t k = 0; k <
nbNodes_; k++)
390 throw Exception(
"Derivatives respective to rate distribution parameters are not implemented.");
394 throw Exception(
"Derivatives respective to substitution model parameters are not implemented.");
402 size_t brI = TextTools::to<size_t>(variable.substr(5));
410 d2 += (*w)[i] * ((*_d2Likelihoods_branch)[i] -
pow((*_dLikelihoods_branch)[i], 2));
454 for (
size_t l = 0; l < nbNodes; l++)
459 VVVdouble* _likelihoods_node_son = &(*_likelihoods_node)[son->
getId()];
467 Vdouble* _likelihoods_leaf_i = &(*_likelihoods_leaf)[i];
468 VVdouble* _likelihoods_node_son_i = &(*_likelihoods_node_son)[i];
472 Vdouble* _likelihoods_node_son_i_c = &(*_likelihoods_node_son_i)[c];
476 (*_likelihoods_node_son_i_c)[x] = (*_likelihoods_leaf_i)[x];
487 vector<const VVVdouble*> iLik(nbSons);
488 vector<const VVVdouble*> tProb(nbSons);
489 for (
size_t n = 0; n < nbSons; n++)
493 iLik[n] = &(*_likelihoods_son)[sonSon->
getId()];
509 for (
size_t n = 0; n < nbSons; n++)
519 map<int, VVVdouble>* _likelihoods_father = &
likelihoodData_->getLikelihoodArrays(father->
getId());
520 VVVdouble* _likelihoods_node_father = &(*_likelihoods_node)[father->
getId()];
533 Vdouble* _likelihoods_leaf_i = &(*_likelihoods_leaf)[i];
534 VVdouble* _likelihoods_node_father_i = &(*_likelihoods_node_father)[i];
538 Vdouble* _likelihoods_node_father_i_c = &(*_likelihoods_node_father_i)[c];
542 (*_likelihoods_node_father_i_c)[x] = (*_likelihoods_leaf_i)[x];
549 vector<const Node*> nodes;
552 for (
size_t n = 0; n < nbFatherSons; n++)
556 nodes.push_back(son);
562 size_t nbSons = nodes.size();
564 vector<const VVVdouble*> iLik(nbSons);
565 vector<const VVVdouble*> tProb(nbSons);
566 for (
size_t n = 0; n < nbSons; n++)
568 const Node* fatherSon = nodes[n];
570 iLik[n] = &(*_likelihoods_father)[fatherSon->
getId()];
589 VVdouble* _likelihoods_node_father_i = &(*_likelihoods_node_father)[i];
592 Vdouble* _likelihoods_node_father_i_c = &(*_likelihoods_node_father_i)[c];
595 (*_likelihoods_node_father_i_c)[x] *=
rootFreqs_[x];
603 for (
size_t i = 0; i < nbNodeSons; i++)
622 VVdouble* rootLikelihoods_i = &(*rootLikelihoods)[i];
623 Vdouble* leavesLikelihoods_root_i = &(*leavesLikelihoods_root)[i];
626 Vdouble* rootLikelihoods_i_c = &(*rootLikelihoods_i)[c];
629 (*rootLikelihoods_i_c)[x] = (*leavesLikelihoods_root_i)[x];
641 vector<const VVVdouble*> iLik(nbNodes);
642 vector<const VVVdouble*> tProb(nbNodes);
643 for (
size_t n = 0; n < nbNodes; n++)
647 iLik[n] = &(*likelihoods_root)[son->
getId()];
657 VVdouble* rootLikelihoods_i = &(*rootLikelihoods)[i];
658 Vdouble* rootLikelihoodsS_i = &(*rootLikelihoodsS)[i];
659 (*rootLikelihoodsSR)[i] = 0;
663 Vdouble* rootLikelihoods_i_c = &(*rootLikelihoods_i)[c];
664 double* rootLikelihoodsS_i_c = &(*rootLikelihoodsS_i)[c];
665 (*rootLikelihoodsS_i_c) = 0;
669 (*rootLikelihoodsS_i_c) +=
rootFreqs_[x] * (*rootLikelihoods_i_c)[x];
671 (*rootLikelihoodsSR)[i] += p[c] * (*rootLikelihoodsS_i_c);
675 if ((*rootLikelihoodsSR)[i] < 0)
676 (*rootLikelihoodsSR)[i] = 0.;
685 int nodeId = node->
getId();
687 map<int, VVVdouble>* likelihoods_node = &
likelihoodData_->getLikelihoodArrays(nodeId);
695 VVdouble* likelihoodArray_i = &likelihoodArray[i];
696 Vdouble* leavesLikelihoods_node_i = &(*leavesLikelihoods_node)[i];
700 Vdouble* likelihoodArray_i_c = &(*likelihoodArray_i)[c];
704 (*likelihoodArray_i_c)[x] = (*leavesLikelihoods_node_i)[x];
715 VVdouble* likelihoodArray_i = &likelihoodArray[i];
719 Vdouble* likelihoodArray_i_c = &(*likelihoodArray_i)[c];
723 (*likelihoodArray_i_c)[x] = 1.;
731 vector<const VVVdouble*> iLik;
732 vector<const VVVdouble*> tProb;
734 for (
size_t n = 0; n < nbNodes; n++)
740 iLik.push_back(&(*likelihoods_node)[son->
getId()]);
752 throw Exception(
"DRHomogeneousTreeLikelihood::computeLikelihoodAtNode_(...). 'sonNode' not found as a son of 'node'.");
767 VVdouble* likelihoodArray_i = &likelihoodArray[i];
770 Vdouble* likelihoodArray_i_c = &(*likelihoodArray_i)[c];
783 const vector<const VVVdouble*>& iLik,
784 const vector<const VVVdouble*>& tProb,
787 size_t nbDistinctSites,
795 for (
size_t n = 0; n < nbNodes; n++)
800 for (
size_t i = 0; i < nbDistinctSites; i++)
803 const VVdouble* iLik_n_i = &(*iLik_n)[i];
806 for (
size_t c = 0; c < nbClasses; c++)
809 const Vdouble* iLik_n_i_c = &(*iLik_n_i)[c];
810 Vdouble* oLik_i_c = &(*oLik_i)[c];
811 const VVdouble* pxy_n_c = &(*pxy_n)[c];
812 for (
size_t x = 0; x < nbStates; x++)
815 const Vdouble* pxy_n_c_x = &(*pxy_n_c)[x];
816 double likelihood = 0;
817 for (
size_t y = 0; y < nbStates; y++)
819 likelihood += (*pxy_n_c_x)[y] * (*iLik_n_i_c)[y];
822 (*oLik_i_c)[x] *= likelihood;
832 const vector<const VVVdouble*>& iLik,
833 const vector<const VVVdouble*>& tProb,
838 size_t nbDistinctSites,
846 for (
size_t n = 0; n < nbNodes; n++)
851 for (
size_t i = 0; i < nbDistinctSites; i++)
854 const VVdouble* iLik_n_i = &(*iLik_n)[i];
857 for (
size_t c = 0; c < nbClasses; c++)
860 const Vdouble* iLik_n_i_c = &(*iLik_n_i)[c];
861 Vdouble* oLik_i_c = &(*oLik_i)[c];
862 const VVdouble* pxy_n_c = &(*pxy_n)[c];
863 for (
size_t x = 0; x < nbStates; x++)
866 const Vdouble* pxy_n_c_x = &(*pxy_n_c)[x];
867 double likelihood = 0;
868 for (
size_t y = 0; y < nbStates; y++)
872 likelihood += (*pxy_n_c_x)[y] * (*iLik_n_i_c)[y];
876 (*oLik_i_c)[x] *= likelihood;
883 for (
size_t i = 0; i < nbDistinctSites; i++)
886 const VVdouble* iLikR_i = &(*iLikR)[i];
889 for (
size_t c = 0; c < nbClasses; c++)
892 const Vdouble* iLikR_i_c = &(*iLikR_i)[c];
893 Vdouble* oLik_i_c = &(*oLik_i)[c];
894 const VVdouble* pxyR_c = &(*tProbR)[c];
895 for (
size_t x = 0; x < nbStates; x++)
897 double likelihood = 0;
898 for (
size_t y = 0; y < nbStates; y++)
901 const Vdouble* pxyR_c_y = &(*pxyR_c)[y];
902 likelihood += (*pxyR_c_y)[x] * (*iLikR_i_c)[y];
905 (*oLik_i_c)[x] *= likelihood;
915 cout <<
"Likelihoods at node " << node->
getId() <<
": " << endl;
919 cout <<
"Array for sub-node " << subNode->
getId() << endl;
925 cout <<
"Array for father node " << father->
getId() << endl;
928 cout <<
" ***" << endl;
std::shared_ptr< DiscreteDistributionInterface > rateDistribution_
static void displayLikelihoodArray(const VVVdouble &likelihoodArray)
Print the likelihood array to terminal (debugging tool).
static void resetLikelihoodArray(VVVdouble &likelihoodArray)
Set all conditional likelihoods to 1.
Partial implementation for homogeneous model of the TreeLikelihood interface.
std::vector< Node * > nodes_
Pointer toward all nodes in the tree.
virtual void applyParameters()
All parameters are stored in a parameter list. This function apply these parameters to the substituti...
std::shared_ptr< TransitionModelInterface > model_
virtual void computeTransitionProbabilitiesForNode(const Node *node)
Fill the pxy_, dpxy_ and d2pxy_ arrays for one node.
ParameterList getRateDistributionParameters() const
Get the parameters associated to the rate distribution.
std::vector< double > rootFreqs_
ParameterList getSubstitutionModelParameters() const
Get the parameters associated to substitution model(s).
std::map< int, VVVdouble > dpxy_
std::map< int, VVVdouble > pxy_
std::map< int, VVVdouble > d2pxy_
virtual void computeAllTransitionProbabilities()
Fill the pxy_, dpxy_ and d2pxy_ arrays for all nodes.
AbstractHomogeneousTreeLikelihood & operator=(const AbstractHomogeneousTreeLikelihood &lik)
Assignation operator.
void setParametersValues(const ParameterList ¶meters) override
bool hasParameter(const std::string &name) const override
const AlignmentDataInterface & data() const
Get the dataset for which the likelihood must be evaluated.
std::unique_ptr< const AlignmentDataInterface > data_
std::shared_ptr< TreeTemplate< Node > > tree_
bool isInitialized() const
bool computeFirstOrderDerivatives_
bool computeSecondOrderDerivatives_
This class implements the likelihood computation for a tree using the double-recursive algorithm.
double getLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const override
Get the likelihood for a site knowing its rate class and its ancestral state.
virtual void fireParameterChanged(const ParameterList ¶ms) override
double getLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const override
Get the likelihood for a site knowing its rate class.
double getLikelihood() const override
Get the likelihood for the whole dataset.
static void computeLikelihoodFromArrays(const std::vector< const VVVdouble * > &iLik, const std::vector< const VVVdouble * > &tProb, VVVdouble &oLik, size_t nbNodes, size_t nbDistinctSites, size_t nbClasses, size_t nbStates, bool reset=true)
Compute conditional likelihoods.
double getSecondOrderDerivative(const std::string &variable) const override
virtual void resetLikelihoodArrays(const Node *node)
double getFirstOrderDerivative(const std::string &variable) const override
double getValue() const override
Function and NNISearchable interface.
double getLikelihoodForASite(size_t site) const override
Get the likelihood for a site.
void init_()
Method called by constructors.
void computeTreeLikelihood()
double getLogLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const override
Get the logarithm of the likelihood for a site knowing its rate class and its ancestral state.
virtual void computeTreeDLikelihoodAtNode(const Node *node)
void setParameters(const ParameterList ¶meters) override
Implements the Function interface.
virtual void displayLikelihood(const Node *node)
This method is mainly for debugging purpose.
virtual void computeTreeDLikelihoods()
virtual void computeTreeD2LikelihoodAtNode(const Node *node)
virtual void computeTreeD2Likelihoods()
virtual void computeLikelihoodAtNode_(const Node *node, VVVdouble &likelihoodArray, const Node *sonNode=0) const
double getLogLikelihood() const override
Get the logarithm of the likelihood for the whole dataset.
DRHomogeneousTreeLikelihood(const Tree &tree, std::shared_ptr< TransitionModelInterface > model, std::shared_ptr< DiscreteDistributionInterface > rDist, bool checkRooted=true, bool verbose=true)
Build a new DRHomogeneousTreeLikelihood object without data.
virtual void computeSubtreeLikelihoodPrefix(const Node *node)
virtual void computeRootLikelihood()
double getLogLikelihoodForASite(size_t site) const override
Get the logarithm of the likelihood for a site.
void setData(const AlignmentDataInterface &sites) override
Set the dataset for which the likelihood must be evaluated.
std::unique_ptr< DRASDRTreeLikelihoodData > likelihoodData_
DRHomogeneousTreeLikelihood & operator=(const DRHomogeneousTreeLikelihood &lik)
double getLogLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const override
Get the logarithm of the likelihood for a site knowing its rate class.
virtual void computeSubtreeLikelihoodPostfix(const Node *node)
The phylogenetic node class.
virtual int getId() const
Get the node's id.
virtual const Node * getSon(size_t pos) const
virtual const Node * getFather() const
Get the father of this node is there is one.
virtual bool isLeaf() const
virtual bool hasFather() const
Tell if this node has a father node.
virtual size_t getNumberOfSons() const
Interface for phylogenetic tree objects.
std::string toString(T t)
Defines the basic types of data flow nodes.
double log(const ExtendedFloat &ef)
std::vector< double > Vdouble
ExtendedFloat pow(const ExtendedFloat &ef, double exp)
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble