42 #include <Bpp/Text/TextTools.h> 43 #include <Bpp/App/ApplicationTools.h> 44 #include <Bpp/Numeric/AutoParameter.h> 59 nbClasses_ = rDist->getNumberOfCategories();
60 pxy_.resize(nbClasses_);
61 for (
size_t i = 0; i < nbClasses_; i++)
63 pxy_[i].resize(nbStates_);
64 for (
size_t j = 0; j < nbStates_; j++)
66 pxy_[i][j].resize(nbStates_);
74 double l = getParameterValue(
"BrLen");
77 for (
size_t c = 0; c < nbClasses_; c++)
79 VVdouble* pxy__c = &pxy_[c];
80 RowMatrix<double> Q = model_->getPij_t(l * rDist_->getCategory(c));
81 for (
size_t x = 0; x < nbStates_; x++)
83 Vdouble* pxy__c_x = &(*pxy__c)[x];
84 for (
size_t y = 0; y < nbStates_; y++)
86 (*pxy__c_x)[y] = Q(x, y);
97 vector<double> la(array1_->size());
98 for (
size_t i = 0; i < array1_->size(); i++)
101 for (
size_t c = 0; c < nbClasses_; c++)
103 double rc = rDist_->getProbability(c);
104 for (
size_t x = 0; x < nbStates_; x++)
106 for (
size_t y = 0; y < nbStates_; y++)
108 Li += rc * (*array1_)[i][c][x] * pxy_[c][x][y] * (*array2_)[i][c][y];
112 la[i] = weights_[i] * log(Li);
115 sort(la.begin(), la.end());
116 for (
size_t i = array1_->size(); i > 0; i--)
127 DiscreteDistribution* rDist,
137 brentOptimizer_ =
new BrentOneDimension();
138 brentOptimizer_->setConstraintPolicy(AutoParameter::CONSTRAINTS_AUTO);
139 brentOptimizer_->setProfiler(0);
140 brentOptimizer_->setMessageHandler(0);
141 brentOptimizer_->setVerbose(0);
148 const SiteContainer& data,
150 DiscreteDistribution* rDist,
160 brentOptimizer_ =
new BrentOneDimension();
161 brentOptimizer_->setConstraintPolicy(AutoParameter::CONSTRAINTS_AUTO);
162 brentOptimizer_->setProfiler(0);
163 brentOptimizer_->setMessageHandler(0);
164 brentOptimizer_->setVerbose(0);
209 const Node* son = tree_->getNode(nodeId);
210 if (!son->
hasFather())
throw NodePException(
"DRHomogeneousTreeLikelihood::testNNI(). Node 'son' must not be the root node.", son);
212 if (!parent->
hasFather())
throw NodePException(
"DRHomogeneousTreeLikelihood::testNNI(). Node 'parent' must not be the root node.", parent);
219 const Node* uncle = grandFather->
getSon(parentPosition > 1 ? 0 : 1 - parentPosition);
225 size_t nbParentNeighbors = parentNeighbors.size();
226 vector<const VVVdouble*> parentArrays(nbParentNeighbors);
227 vector<const VVVdouble*> parentTProbs(nbParentNeighbors);
228 for (
size_t k = 0; k < nbParentNeighbors; k++)
230 const Node* n = parentNeighbors[k];
234 parentTProbs[k] = &pxy_[n->
getId()];
240 size_t nbGrandFatherNeighbors = grandFatherNeighbors.size();
241 vector<const VVVdouble*> grandFatherArrays;
242 vector<const VVVdouble*> grandFatherTProbs;
243 for (
size_t k = 0; k < nbGrandFatherNeighbors; k++)
245 const Node* n = grandFatherNeighbors[k];
249 grandFatherTProbs.push_back(&pxy_[n->
getId()]);
254 VVVdouble array1 = *sonArray;
255 resetLikelihoodArray(array1);
256 grandFatherArrays.push_back(sonArray);
257 grandFatherTProbs.push_back(&pxy_[son->
getId()]);
260 computeLikelihoodFromArrays(grandFatherArrays, grandFatherTProbs, &grandFatherData->
getLikelihoodArrayForNeighbor(grandFather->
getFather()->
getId()), &pxy_[grandFather->
getId()], array1, nbGrandFatherNeighbors, nbDistinctSites_, nbClasses_, nbStates_,
false);
264 computeLikelihoodFromArrays(grandFatherArrays, grandFatherTProbs, array1, nbGrandFatherNeighbors + 1, nbDistinctSites_, nbClasses_, nbStates_,
false);
267 for (
size_t i = 0; i < nbDistinctSites_; i++)
269 for (
size_t j = 0; j < nbClasses_; j++)
271 for (
size_t x = 0; x < nbStates_; x++)
273 array1[i][j][x] *= rootFreqs_[x];
280 VVVdouble array2 = *uncleArray;
281 resetLikelihoodArray(array2);
282 parentArrays.push_back(uncleArray);
283 parentTProbs.push_back(&pxy_[uncle->
getId()]);
284 computeLikelihoodFromArrays(parentArrays, parentTProbs, array2, nbParentNeighbors + 1, nbDistinctSites_, nbClasses_, nbStates_,
false);
287 brLikFunction_->initModel(model_, rateDistribution_);
288 brLikFunction_->initLikelihoods(&array1, &array2);
289 ParameterList parameters;
291 while (pos < nodes_.size() && nodes_[pos]->getId() != parent->
getId()) pos++;
292 if (pos == nodes_.size())
throw Exception(
"NNIHomogeneousTreeLikelihood::testNNI. Unvalid node id.");
293 Parameter brLen = getParameter(
"BrLen" + TextTools::toString(pos));
294 brLen.setName(
"BrLen");
295 parameters.addParameter(brLen);
296 brLikFunction_->setParameters(parameters);
299 brentOptimizer_->setFunction(brLikFunction_);
300 brentOptimizer_->getStopCondition()->setTolerance(0.1);
301 brentOptimizer_->setInitialInterval(brLen.getValue(), brLen.getValue() + 0.01);
302 brentOptimizer_->init(parameters);
303 brentOptimizer_->optimize();
305 brLenNNIValues_[nodeId] = brentOptimizer_->getParameters().getParameter(
"BrLen").getValue();
306 brLikFunction_->resetLikelihoods();
310 return brLikFunction_->getValue() - getValue();
317 Node* son = tree_->getNode(nodeId);
318 if (!son->
hasFather())
throw NodePException(
"DRHomogeneousTreeLikelihood::testNNI(). Node 'son' must not be the root node.", son);
320 if (!parent->
hasFather())
throw NodePException(
"DRHomogeneousTreeLikelihood::testNNI(). Node 'parent' must not be the root node.", parent);
326 Node* uncle = grandFather->
getSon(parentPosition > 1 ? 0 : 1 - parentPosition);
333 while (pos < nodes_.size() && nodes_[pos]->getId() != parent->
getId()) pos++;
334 if (pos == nodes_.size())
throw Exception(
"NNIHomogeneousTreeLikelihood::doNNI. Unvalid node id.");
336 string name =
"BrLen" + TextTools::toString(pos);
337 if (brLenNNIValues_.find(nodeId) != brLenNNIValues_.end())
339 double length = brLenNNIValues_[nodeId];
340 brLenParameters_.setParameterValue(name, length);
341 getParameter_(name).setValue(length);
344 else cerr <<
"ERROR, branch not found: " << nodeId << endl;
347 brLenNNIParams_.addParameter(brLenParameters_.getParameter(name));
349 catch (ParameterException& ex)
352 cerr <<
"DEBUG:" << endl;
353 brLenNNIParams_.printParameters(errout);
354 cerr <<
"DEBUG:" << name << endl;
359 brLenNNIParams_[brLenNNIParams_.size() - 1].removeConstraint();
NNIHomogeneousTreeLikelihood & operator=(const NNIHomogeneousTreeLikelihood &lik)
DRHomogeneousTreeLikelihood & operator=(const DRHomogeneousTreeLikelihood &lik)
virtual void addSon(size_t pos, Node *node)
Interface for all substitution models.
Likelihood data structure for a node.
virtual const Node * getSon(size_t pos) const
VVVdouble & getLikelihoodArrayForNeighbor(int neighborId)
void initModel(const SubstitutionModel *model, const DiscreteDistribution *rDist)
virtual const Node * getFather() const
Get the father of this node is there is one.
This class adds support for NNI topology estimation to the DRHomogeneousTreeLikelihood class...
Interface for phylogenetic tree objects.
virtual bool hasFather() const
Tell if this node has a father node.
BrentOneDimension * brentOptimizer_
Optimizer used for testing NNI.
virtual ~NNIHomogeneousTreeLikelihood()
BranchLikelihood * clone() const
NNIHomogeneousTreeLikelihood(const Tree &tree, SubstitutionModel *model, DiscreteDistribution *rDist, bool checkRooted=true, bool verbose=true)
Build a new NNIHomogeneousTreeLikelihood object.
virtual int getId() const
Get the node's id.
std::map< int, double > brLenNNIValues_
Hash used for backing up branch lengths when testing NNIs.
This class implements the likelihood computation for a tree using the double-recursive algorithm...
virtual Node * removeSon(size_t pos)
virtual size_t getSonPosition(const Node *son) const
ParameterList brLenNNIParams_
void computeLogLikelihood()
The phylogenetic node class.
BranchLikelihood * brLikFunction_
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
Compute likelihood for a 4-tree.
void doNNI(int nodeId)
Perform a NNI movement.
virtual size_t getNumberOfStates() const =0
Get the number of states.
General exception thrown when something is wrong with a particular node.
General exception thrown when something is wrong with a particular node.
double testNNI(int nodeId) const
Send the score of a NNI movement, without performing it.
void computeAllTransitionProbabilities()