41 #ifndef _DISTANCEESTIMATION_H_ 42 #define _DISTANCEESTIMATION_H_ 44 #include "../Model/SubstitutionModel.h" 45 #include "../Likelihood/AbstractTreeLikelihood.h" 46 #include "../Likelihood/DRHomogeneousTreeLikelihood.h" 47 #include "../Likelihood/PseudoNewtonOptimizer.h" 49 #include <Bpp/Clonable.h> 50 #include <Bpp/Numeric/ParameterList.h> 51 #include <Bpp/Numeric/Prob/DiscreteDistribution.h> 52 #include <Bpp/Numeric/Function/Optimizer.h> 53 #include <Bpp/Numeric/Function/SimpleMultiDimensions.h> 54 #include <Bpp/Numeric/Function/MetaOptimizer.h> 57 #include <Bpp/Seq/Container/SiteContainer.h> 117 const std::string& seq1,
const std::string& seq2,
118 const SiteContainer& data,
120 DiscreteDistribution* rDist,
121 bool verbose)
throw (Exception);
150 throw NotImplementedException(
"TwoTreeLikelihood::getLikelihoodData.");
154 throw NotImplementedException(
"TwoTreeLikelihood::getLikelihoodData.");
170 void setData(
const SiteContainer& sites)
throw (Exception) {}
201 throw NotImplementedException(
"TwoTreeLikelihood::getNewBranchSiteModelIterator. This class does not (yet) provide support for partition models.");
206 throw NotImplementedException(
"TwoTreeLikelihood::getNewSiteModelIterator. This class is for inner use only and does not provide site model iterators.");
224 void setParameters(
const ParameterList& parameters)
throw (ParameterNotFoundException, ConstraintException);
225 double getValue()
const throw(Exception);
307 public virtual Clonable
337 DiscreteDistribution* rateDist,
338 size_t verbose = 1) :
344 defaultOptimizer_(0),
370 DiscreteDistribution* rateDist,
371 const SiteContainer* sites,
373 bool computeMat =
true) :
379 defaultOptimizer_(0),
384 if(computeMat) computeMatrix();
396 rateDist_(distanceEstimation.rateDist_->
clone()),
397 sites_(distanceEstimation.sites_),
399 optimizer_(dynamic_cast<Optimizer *>(distanceEstimation.optimizer_->
clone())),
400 defaultOptimizer_(dynamic_cast<MetaOptimizer *>(distanceEstimation.defaultOptimizer_->
clone())),
401 verbose_(distanceEstimation.verbose_),
402 parameters_(distanceEstimation.parameters_)
404 if(distanceEstimation.
dist_ != 0)
405 dist_ =
new DistanceMatrix(*distanceEstimation.
dist_);
421 rateDist_.reset(distanceEstimation.
rateDist_->clone());
422 sites_ = distanceEstimation.
sites_;
423 if (distanceEstimation.
dist_ != 0)
424 dist_ =
new DistanceMatrix(*distanceEstimation.
dist_);
427 optimizer_ =
dynamic_cast<Optimizer *
>(distanceEstimation.
optimizer_->clone());
429 verbose_ = distanceEstimation.
verbose_;
436 if (dist_)
delete dist_;
437 delete defaultOptimizer_;
441 #ifndef NO_VIRTUAL_COV 451 MetaOptimizerInfos* desc =
new MetaOptimizerInfos();
452 std::vector<std::string> name;
453 name.push_back(
"BrLen");
454 desc->addOptimizer(
"Branch length",
new PseudoNewtonOptimizer(0), name, 2, MetaOptimizerInfos::IT_TYPE_FULL);
455 ParameterList tmp =
model_->getParameters();
456 tmp.addParameters(rateDist_->getParameters());
457 desc->addOptimizer(
"substitution model and rate distribution",
new SimpleMultiDimensions(0), tmp.getParameterNames(), 0, MetaOptimizerInfos::IT_TYPE_STEP);
458 defaultOptimizer_ =
new MetaOptimizer(0, desc);
459 defaultOptimizer_->setMessageHandler(0);
460 defaultOptimizer_->setProfiler(0);
461 defaultOptimizer_->getStopCondition()->setTolerance(0.0001);
462 optimizer_ =
dynamic_cast<Optimizer*
>(defaultOptimizer_->clone());
475 void computeMatrix() throw (NullPointerException);
482 DistanceMatrix* getMatrix()
const {
return dist_ == 0 ? 0 :
new DistanceMatrix(*dist_); }
487 if (hasSubstitutionModel())
490 throw Exception(
"DistanceEstimation::getSubstitutionModel(). No model assciated to this instance.");
498 if (hasRateDistribution())
501 throw Exception(
"DistanceEstimation::getRateDistribution(). No rate distribution assciated to this instance.");
506 void setData(
const SiteContainer* sites) { sites_ = sites; }
507 const SiteContainer*
getData()
const {
return sites_; }
512 if (optimizer_)
delete optimizer_;
513 optimizer_ =
dynamic_cast<Optimizer *
>(optimizer->clone());
517 void resetOptimizer() { optimizer_ =
dynamic_cast<Optimizer*
>(defaultOptimizer_->clone()); }
528 parameters_ = parameters;
551 #endif //_DISTANCEESTIMATION_H_ VVdouble leafLikelihoods1_
Optimizer * getOptimizer()
Interface for all substitution models.
DistanceEstimation(const DistanceEstimation &distanceEstimation)
Copy constructor.
bool hasSubstitutionModel() const
virtual int getAlphabetStateAsInt(size_t index) const =0
Constraint * brLenConstraint_
VVVdouble getTransitionProbabilitiesPerRateClass(int nodeId, size_t siteIndex) const
This method is not applicable for this object.
VVdouble rootLikelihoodsS_
auto_ptr< DiscreteDistribution > rateDist_
VVdouble leafLikelihoods2_
Vdouble rootLikelihoodsSR_
const DiscreteDistribution & getRateDistribution() const
const TreeLikelihoodData * getLikelihoodData() const
void resetSubstitutionModel(SubstitutionModel *model=0)
virtual const std::vector< int > & getAlphabetStates() const =0
Partial implementation of the DiscreteRatesAcrossSitesTreeLikelihood interface.
TwoTreeLikelihood(const std::string &seq1, const std::string &seq2, const SiteContainer &data, SubstitutionModel *model, DiscreteDistribution *rDist, bool verbose)
ParameterList getBranchLengthsParameters() const
Get the branch lengths parameters.
This class is a simplified version of DRHomogeneousTreeLikelihood for 2-Trees.
DistanceEstimation & operator=(const DistanceEstimation &distanceEstimation)
Assigment operator.
void setParameters(const ParameterList ¶meters)
Implements the Function interface.
double getLikelihoodForASite(size_t site) const
Get the likelihood for a site.
Iterates through all models used for all sites on a given branch.
virtual void initParameters()
This builds the parameters list from all parametrizable objects, i.e. substitution model...
TwoTreeLikelihood & operator=(const TwoTreeLikelihood &lik)
const SubstitutionModel & getSubstitutionModel() const
SubstitutionModel * model_
virtual ~TwoTreeLikelihood()
const SiteContainer * sites_
SubstitutionModel * getSubstitutionModel()
Get the substitution model used for the computation.
ConstBranchModelIterator * getNewBranchModelIterator(int nodeId) const
TreeLikelihood data structure.
virtual void initBranchLengthsParameters()
virtual double getMinimumBranchLength() const
double getLogLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
Get the logarithm of the likelihood for a site knowing its rate class.
size_t getNumberOfStates() const
std::string getAlphabetStateAsChar(size_t i) const
void fireParameterChanged(const ParameterList ¶ms)
void setVerbose(size_t verbose)
void setData(const SiteContainer &sites)
Set the dataset for which the likelihood must be evaluated.
virtual ~DistanceEstimation()
void setOptimizer(const Optimizer *optimizer)
virtual const Vdouble & getFrequencies() const =0
const std::vector< int > & getAlphabetStates() const
std::vector< unsigned int > rootWeights_
The frequency of each site.
const std::vector< double > & getRootFrequencies(size_t siteIndex) const
Get the values of the frequencies for each state in the alphabet at the root node.
void setData(const SiteContainer *sites)
Iterates through all models used for all branches on a given site.
double getLogLikelihood() const
Get the logarithm of the likelihood for the whole dataset.
SubstitutionModel * getSubstitutionModel(int nodeId, size_t siteIndex)
Get the substitution model associated to a given node and alignment column.
const SubstitutionModel * getSubstitutionModel(int nodeId, size_t siteIndex) const
Get the substitution model associated to a given node and alignment column.
void initialize()
Init the likelihood object.
size_t getVerbose() const
virtual void initTreeLikelihoods(const SequenceContainer &sequences)
This method initializes the leaves according to a sequence container.
const SubstitutionModel * getSubstitutionModel() const
Get the substitution model used for the computation.
virtual void computeTreeDLikelihood()
This Optimizer implements Newton's algorithm for finding a minimum of a function. This is in fact a m...
Exception thrown when something is wrong with a particular node.
std::vector< std::string > seqnames_
std::vector< size_t > rootPatternLinks_
As previous, but for the global container.
virtual void computeTreeLikelihood()
const SiteContainer * getData() const
size_t getSiteIndex(size_t site) const
Get the index (used for inner computations) of a given site (original alignment column).
virtual void computeTreeD2Likelihood()
int getAlphabetStateAsInt(size_t i) const
double getLogLikelihoodForASite(size_t site) const
Get the logarithm of the likelihood for a site.
virtual std::string getAlphabetStateAsChar(size_t index) const =0
double getSecondOrderDerivative(const std::string &variable) const
double getFirstOrderDerivative(const std::string &variable) const
TreeLikelihoodData * getLikelihoodData()
double getLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const
Get the likelihood for a site knowing its rate class.
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.
const Optimizer * getOptimizer() const
SiteContainer * shrunkData_
DistanceEstimation * clone() const
ConstSiteModelIterator * getNewSiteModelIterator(size_t siteIndex) const
void setAdditionalParameters(const ParameterList ¶meters)
Specify a list of parameters to be estimated.
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 getLikelihood() const
Get the likelihood for the whole dataset.
TwoTreeLikelihood * clone() const
DistanceEstimation(SubstitutionModel *model, DiscreteDistribution *rateDist, size_t verbose=1)
Create a new DistanceEstimation object according to a given substitution model and a rate distributio...
Estimate a distance matrix from sequence data, according to a given model.
void resetAdditionalParameters()
Reset all additional parameters.
virtual size_t getNumberOfStates() const =0
Get the number of states.
DistanceEstimation(SubstitutionModel *model, DiscreteDistribution *rateDist, const SiteContainer *sites, size_t verbose=1, bool computeMat=true)
Create a new DistanceEstimation object and compute distances according to a given substitution model ...
ParameterList brLenParameters_
void resetRateDistribution(DiscreteDistribution *rateDist=0)
VVVdouble rootLikelihoods_
ParameterList parameters_
virtual void applyParameters()
All parameters are stores in a parameter list.
ParameterList getSubstitutionModelParameters() const
Get the parameters associated to substitution model(s).
auto_ptr< SubstitutionModel > model_
MetaOptimizer * defaultOptimizer_
virtual void setMinimumBranchLength(double minimum)
bool hasRateDistribution() const