8 #include "../../PatternTools.h"
22 std::shared_ptr<TransitionModelInterface> model,
23 std::shared_ptr<DiscreteDistributionInterface> rDist,
39 std::shared_ptr<TransitionModelInterface> model,
40 std::shared_ptr<DiscreteDistributionInterface> rDist,
68 minusLogLik_(lik.minusLogLik_)
121 for (
size_t i = 0; i <
nbSites_; i++)
134 for (
size_t i = 0; i <
nbSites_; i++)
138 sort(la.begin(), la.end());
139 for (
size_t i =
nbSites_; i > 0; i--)
229 if (
rateDistribution_->getParameters().getCommonParametersWith(params).size() > 0
230 ||
model_->getParameters().getCommonParametersWith(params).size() > 0)
235 else if (params.
size() > 0)
238 for (
size_t i = 0; i < params.
size(); i++)
240 string s = params[i].getName();
241 if (s.substr(0, 5) ==
"BrLen")
260 throw Exception(
"RHomogeneousTreeLikelihood::getValue(). Instance is not initialized.");
269 size_t rateClass)
const
307 for (
size_t i = 0; i <
nbSites_; i++)
323 throw Exception(
"Derivatives respective to rate distribution parameter are not implemented.");
327 throw Exception(
"Derivatives respective to substitution model parameters are not implemented.");
340 size_t brI = TextTools::to<size_t>(variable.substr(5));
347 size_t nbSites = _dLikelihoods_father->size();
348 for (
size_t i = 0; i < nbSites; i++)
350 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
353 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
356 (*_dLikelihoods_father_i_c)[s] = 1.;
362 for (
size_t l = 0; l < nbNodes; l++)
373 for (
size_t i = 0; i < nbSites; i++)
375 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
376 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
379 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
380 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
381 VVdouble* dpxy__son_c = &(*dpxy__son)[c];
385 Vdouble* dpxy__son_c_x = &(*dpxy__son_c)[x];
388 dl += (*dpxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
390 (*_dLikelihoods_father_i_c)[x] *= dl;
398 for (
size_t i = 0; i < nbSites; i++)
400 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
401 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
404 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
405 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
406 VVdouble* pxy__son_c = &(*pxy__son)[c];
410 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
413 dl += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
415 (*_dLikelihoods_father_i_c)[x] *= dl;
439 size_t nbSites = _dLikelihoods_father->size();
440 for (
size_t i = 0; i < nbSites; i++)
442 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
445 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
448 (*_dLikelihoods_father_i_c)[s] = 1.;
454 for (
size_t l = 0; l < nbNodes; l++)
464 for (
size_t i = 0; i < nbSites; i++)
466 VVdouble* _dLikelihoods_son_i = &(*_dLikelihoods_son)[(*_patternLinks_father_son)[i]];
467 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
470 Vdouble* _dLikelihoods_son_i_c = &(*_dLikelihoods_son_i)[c];
471 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
472 VVdouble* pxy__son_c = &(*pxy__son)[c];
476 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
479 dl += (*pxy__son_c_x)[y] * (*_dLikelihoods_son_i_c)[y];
481 (*_dLikelihoods_father_i_c)[x] *= dl;
489 for (
size_t i = 0; i < nbSites; i++)
491 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
492 VVdouble* _dLikelihoods_father_i = &(*_dLikelihoods_father)[i];
495 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
496 Vdouble* _dLikelihoods_father_i_c = &(*_dLikelihoods_father_i)[c];
497 VVdouble* pxy__son_c = &(*pxy__son)[c];
501 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
504 dl += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
506 (*_dLikelihoods_father_i_c)[x] *= dl;
522 size_t rateClass)
const
560 for (
size_t i = 0; i <
nbSites_; i++)
575 throw Exception(
"Derivatives respective to rate distribution parameter are not implemented.");
579 throw Exception(
"Derivatives respective to substitution model parameters are not implemented.");
592 size_t brI = TextTools::to<size_t>(variable.substr(5));
599 size_t nbSites = _d2Likelihoods_father->size();
600 for (
size_t i = 0; i < nbSites; i++)
602 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
605 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
608 (*_d2Likelihoods_father_i_c)[s] = 1.;
614 for (
size_t l = 0; l < nbNodes; l++)
624 for (
size_t i = 0; i < nbSites; i++)
626 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
627 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
630 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
631 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
632 VVdouble* d2pxy__son_c = &(*d2pxy__son)[c];
636 Vdouble* d2pxy__son_c_x = &(*d2pxy__son_c)[x];
639 d2l += (*d2pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
641 (*_d2Likelihoods_father_i_c)[x] *= d2l;
649 for (
size_t i = 0; i < nbSites; i++)
651 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
652 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
655 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
656 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
657 VVdouble* pxy__son_c = &(*pxy__son)[c];
661 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
664 d2l += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
666 (*_d2Likelihoods_father_i_c)[x] *= d2l;
690 size_t nbSites = _d2Likelihoods_father->size();
691 for (
size_t i = 0; i < nbSites; i++)
693 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
696 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
699 (*_d2Likelihoods_father_i_c)[s] = 1.;
705 for (
size_t l = 0; l < nbNodes; l++)
715 for (
size_t i = 0; i < nbSites; i++)
717 VVdouble* _d2Likelihoods_son_i = &(*_d2Likelihoods_son)[(*_patternLinks_father_son)[i]];
718 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
721 Vdouble* _d2Likelihoods_son_i_c = &(*_d2Likelihoods_son_i)[c];
722 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
723 VVdouble* pxy__son_c = &(*pxy__son)[c];
727 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
730 d2l += (*pxy__son_c_x)[y] * (*_d2Likelihoods_son_i_c)[y];
732 (*_d2Likelihoods_father_i_c)[x] *= d2l;
740 for (
size_t i = 0; i < nbSites; i++)
742 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_father_son)[i]];
743 VVdouble* _d2Likelihoods_father_i = &(*_d2Likelihoods_father)[i];
746 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
747 Vdouble* _d2Likelihoods_father_i_c = &(*_d2Likelihoods_father_i)[c];
748 VVdouble* pxy__son_c = &(*pxy__son)[c];
752 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
755 dl += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
757 (*_d2Likelihoods_father_i_c)[x] *= dl;
787 for (
size_t i = 0; i < nbSites; i++)
790 VVdouble* _likelihoods_node_i = &(*_likelihoods_node)[i];
794 Vdouble* _likelihoods_node_i_c = &(*_likelihoods_node_i)[c];
798 (*_likelihoods_node_i_c)[x] = 1.;
803 for (
size_t l = 0; l < nbNodes; l++)
815 for (
size_t i = 0; i < nbSites; i++)
818 VVdouble* _likelihoods_son_i = &(*_likelihoods_son)[(*_patternLinks_node_son)[i]];
819 VVdouble* _likelihoods_node_i = &(*_likelihoods_node)[i];
823 Vdouble* _likelihoods_son_i_c = &(*_likelihoods_son_i)[c];
824 Vdouble* _likelihoods_node_i_c = &(*_likelihoods_node_i)[c];
825 VVdouble* pxy__son_c = &(*pxy__son)[c];
829 Vdouble* pxy__son_c_x = &(*pxy__son_c)[x];
830 double likelihood = 0;
833 likelihood += (*pxy__son_c_x)[y] * (*_likelihoods_son_i_c)[y];
836 (*_likelihoods_node_i_c)[x] *= likelihood;
847 cout <<
"Likelihoods at node " << node->
getName() <<
": " << endl;
849 cout <<
" ***" << endl;
std::shared_ptr< DiscreteDistributionInterface > rateDistribution_
static void displayLikelihoodArray(const VVVdouble &likelihoodArray)
Print the likelihood array to terminal (debugging tool).
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
discrete Rate Across Sites, (simple) Recursive likelihood data structure.
size_t getNumberOfSites() const
VVVdouble & getDLikelihoodArray(int nodeId)
void initLikelihoods(const AlignmentDataInterface &sites, const TransitionModelInterface &model)
size_t getRootArrayPosition(size_t currentPosition) const
const std::vector< size_t > & getArrayPositions(int parentId, int sonId) const
size_t getNumberOfDistinctSites() const
size_t getNumberOfStates() const
DRASRTreeLikelihoodData * clone() const
VVVdouble & getLikelihoodArray(int nodeId)
void setTree(std::shared_ptr< const TreeTemplate< Node >> tree)
Set the tree associated to the data.
VVVdouble & getD2LikelihoodArray(int nodeId)
The phylogenetic node class.
virtual std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
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 size_t getNumberOfSons() const
This class implement the 'traditional' way of computing likelihood for a tree.
virtual double getD2LikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
virtual double getDLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
void setParameters(const ParameterList ¶meters)
Implements the Function interface.
double getLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
Get the likelihood for a site knowing its rate class and its ancestral state.
double getLogLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const
Get the logarithm of the likelihood for a site knowing its rate class and its ancestral state.
double getLogLikelihoodForASite(size_t site) const
Get the logarithm of the likelihood for a site.
RHomogeneousTreeLikelihood & operator=(const RHomogeneousTreeLikelihood &lik)
virtual void displayLikelihood(const Node *node)
This method is mainly for debugging purpose.
virtual ~RHomogeneousTreeLikelihood()
virtual void computeSubtreeLikelihood(const Node *node)
Compute the likelihood for a subtree defined by the Tree::Node node.
double getSecondOrderDerivative(const std::string &variable) const
double getLikelihoodForASite(size_t site) const
Get the likelihood for a site.
double getLogLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
Get the logarithm of the likelihood for a site knowing its rate class.
void setData(const AlignmentDataInterface &sites)
Set the dataset for which the likelihood must be evaluated.
virtual double getD2LikelihoodForASite(size_t site) const
virtual void computeTreeDLikelihood(const std::string &variable)
virtual void computeTreeD2Likelihood(const std::string &variable)
RHomogeneousTreeLikelihood(const Tree &tree, std::shared_ptr< TransitionModelInterface > model, std::shared_ptr< DiscreteDistributionInterface > rDist, bool checkRooted=true, bool verbose=true, bool usePatterns=true)
Build a new RHomogeneousTreeLikelihood object without data.
virtual double getDLikelihoodForASite(size_t site) const
double getLikelihood() const
Get the likelihood for the whole dataset.
double getFirstOrderDerivative(const std::string &variable) const
virtual double getD2LogLikelihood() const
virtual double getD2LogLikelihoodForASite(size_t site) const
double getLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
Get the likelihood for a site knowing its rate class.
virtual double getDLogLikelihoodForASite(size_t site) const
double getLogLikelihood() const
Get the logarithm of the likelihood for the whole dataset.
void computeTreeLikelihood()
void init_(bool usePatterns)
Method called by constructors.
DRASRTreeLikelihoodData * likelihoodData_
virtual double getDLogLikelihood() const
virtual void computeDownSubtreeD2Likelihood(const Node *)
void fireParameterChanged(const ParameterList ¶ms)
virtual void computeDownSubtreeDLikelihood(const Node *)
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