bpp-phyl3  3.0.0
LikelihoodCalculationSingleProcess.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <list>
6 #include <numeric>
7 #include <unordered_map>
8 
15 
16 using namespace std;
17 using namespace bpp;
18 using namespace numeric;
19 using namespace Eigen;
20 
21 LikelihoodCalculationSingleProcess::LikelihoodCalculationSingleProcess(
22  Context& context,
23  std::shared_ptr<const AlignmentDataInterface> sites,
24  std::shared_ptr<const SubstitutionProcessInterface> process) :
25  AlignedLikelihoodCalculation(context), process_(process), psites_(sites),
26  rootPatternLinks_(), rootWeights_(), shrunkData_(),
27  processNodes_(), rFreqs_(),
28  vRateCatTrees_(), catProb_(), condLikelihoodTree_(0)
29 {
30  if (!process_->getParametrizablePhyloTree())
31  throw Exception("LikelihoodCalculationSingleProcess::LikelihoodCalculationSingleProcess: missing tree in SubstitutionProcess.");
32  setPatterns_();
34 
35  // Default Derivate
37 }
38 
40  Context& context,
41  std::shared_ptr<const SubstitutionProcessInterface> process) :
43  process_(process), psites_(),
44  rootPatternLinks_(), rootWeights_(), shrunkData_(),
45  processNodes_(), rFreqs_(),
46  vRateCatTrees_(), catProb_(), condLikelihoodTree_(0)
47 {
48  if (!process_->getParametrizablePhyloTree())
49  throw Exception("LikelihoodCalculationSingleProcess::LikelihoodCalculationSingleProcess: missing tree in SubstitutionProcess.");
50 
52 
53  // Default Derivate
55 }
56 
58  std::shared_ptr<CollectionNodes> collection,
59  std::shared_ptr<const AlignmentDataInterface> sites,
60  size_t nProcess) :
61  AlignedLikelihoodCalculation(collection->context()),
62  process_(collection->collection().getSubstitutionProcess(nProcess)),
63  psites_(sites),
64  rootPatternLinks_(), rootWeights_(), shrunkData_(),
65  processNodes_(), rFreqs_(),
66  vRateCatTrees_(), catProb_(), condLikelihoodTree_(0)
67 {
68  if (!process_->getParametrizablePhyloTree())
69  throw Exception("LikelihoodCalculationSingleProcess::LikelihoodCalculationSingleProcess: missing tree in SubstitutionProcess.");
70 
71  setPatterns_();
72  makeProcessNodes_(*collection, nProcess);
73 
74  // Default Derivate
76 }
77 
78 
80  std::shared_ptr<CollectionNodes> collection,
81  size_t nProcess) :
82  AlignedLikelihoodCalculation(collection->context()),
83  process_(collection->collection().getSubstitutionProcess(nProcess)),
84  psites_(),
85  rootPatternLinks_(), rootWeights_(), shrunkData_(),
86  processNodes_(), rFreqs_(),
87  vRateCatTrees_(), catProb_(), condLikelihoodTree_(0)
88 {
89  makeProcessNodes_(*collection, nProcess);
90 
91  // Default Derivate
93 }
94 
95 
98  process_(lik.process_), psites_(lik.psites_),
99  rootPatternLinks_(lik.rootPatternLinks_), rootWeights_(), shrunkData_(lik.shrunkData_),
100  processNodes_(), rFreqs_(),
101  vRateCatTrees_(), catProb_(), condLikelihoodTree_(0)
102 {
103  setPatterns_();
105 
106  // Default Derivate
108 }
109 
111 {
112  SitePatterns patterns(*psites_, process_->getParametrizablePhyloTree()->getAllLeavesNames());
113  shrunkData_ = patterns.getSites();
115  size_t nbSites = shrunkData_->getNumberOfSites();
116  Eigen::RowVectorXi weights(nbSites);
117  for (std::size_t i = 0; i < nbSites; i++)
118  {
119  weights(Eigen::Index(i)) = int(patterns.getWeights()[i]);
120  }
121  rootWeights_ = SiteWeights::create(getContext_(), std::move(weights));
122 }
123 
125 {
126 #ifdef DEBUG
127  cerr << "LikelihoodCalculationSingleProcess::makeProcessNodes_(){" << endl;
128 #endif
129  // add Independent Parameters
130  const auto& paramProc = process_->getIndependentParameters();
131 
132  for (size_t i = 0; i < paramProc.size(); ++i)
133  {
135  }
136 
137  // Share dependencies with aliased parameters
138  for (size_t i = 0; i < paramProc.size(); i++)
139  {
140  auto name = paramProc[i].getName();
141  auto vs = process_->getAlias(name);
142 
143  auto dep = dynamic_cast<const ConfiguredParameter*>(&getParameters_()[i])->dependency(0);
144  for (const auto& s:vs)
145  {
146  auto newacp = ConfiguredParameter::create(getContext_(), {dep}, process_->parameter(s));
147  shareParameter_(newacp);
148  aliasParameters(name, s);
149  }
150  }
151 
152  auto spcm = dynamic_pointer_cast<const SubstitutionProcessCollectionMember>(process_);
153 
154 
155  auto& pl2(getParameters_());
156 
157  // rates node
158  std::string suff = spcm ? ("_" + TextTools::toString(spcm->getRateDistributionNumber())) : "";
159 
160  auto rates = process_->getRateDistribution();
161  if (rates && dynamic_pointer_cast<const ConstantRateDistribution>(rates) == nullptr)
162  processNodes_.ratesNode_ = ConfiguredParametrizable::createConfigured<DiscreteDistributionInterface, ConfiguredDistribution>(getContext_(), *rates, pl2, suff);
163 
165  // tree node
166  suff = spcm ? ("_" + TextTools::toString(spcm->getTreeNumber())) : "";
168 
170  // rootFrequencies node
171 
172  auto root = process_->getRootFrequencySet();
173  if (root)
174  {
175  suff = spcm ? ("_" + TextTools::toString(spcm->getRootFrequenciesNumber())) : "";
176  processNodes_.rootFreqsNode_ = ConfiguredParametrizable::createConfigured<FrequencySetInterface, ConfiguredFrequencySet>(getContext_(), *root, pl2, suff);
177  }
178 
179  auto itE = processNodes_.treeNode_->allEdgesIterator();
180  // get any modelNode from the map (only for StateMap)
181  for (itE->start(); !itE->end(); itE->next())
182  {
183  if ((*(*itE))->getModel() != 0)
184  {
185  processNodes_.modelNode_ = (*(*itE))->getModel();
186  break;
187  }
188  }
190  throw Exception("LikelihoodCalculationSingleProcess::makeProcessNodes_: null modelNode_");
191 
192 #ifdef DEBUG
193  cerr << "likelihoodcalculationsingleprocess::makeprocessnodes_()}" << endl;
194 #endif
195 }
196 
198 {
199  auto& spcm = collection.collection().substitutionProcess(nProc);
200 
201  // share process parameters with those of the collection
202  const auto& paramProc = spcm.getIndependentParameters();
203 
204  for (size_t i = 0; i < paramProc.size(); i++)
205  {
206  auto name = paramProc[i].getName();
207  if (!collection.hasParameter(name))
208  throw Exception("LikelihoodCalculationSingleProcess::makeProcessNodes_ : CollectionNodes does not have parameter " + name);
209 
210  shareParameter_(collection.getParameter(name));
211  }
212 
213  // share dependencies
214 
215  for (size_t i = 0; i < paramProc.size(); i++)
216  {
217  auto name = paramProc[i].getName();
218  auto vs = spcm.getAlias(name);
219 
220  auto dep = dynamic_cast<const ConfiguredParameter*>(&getParameters_()[i])->dependency(0);
221  for (const auto& s:vs)
222  {
223  auto newacp = ConfiguredParameter::create(getContext_(), {dep}, spcm.parameter(s));
224  shareParameter_(newacp);
225  aliasParameters(name, s);
226  }
227  }
228 
229  // rates node
230 
231  auto rates = spcm.getRateDistribution();
232 
233  if (dynamic_pointer_cast<const ConstantRateDistribution>(rates) == nullptr)
234  processNodes_.ratesNode_ = collection.getRateDistribution(spcm.getRateDistributionNumber());
235 
237  // tree node
238 
240 
242  // rootFrequencies node
243 
244  if (!spcm.isStationary())
245  processNodes_.rootFreqsNode_ = collection.getFrequencies(spcm.getRootFrequenciesNumber());
246 
248  // get any modelNode from the map (only for StateMap)
249 
250  auto itE = processNodes_.treeNode_->allEdgesIterator();
251  for (itE->start(); !itE->end(); itE->next())
252  {
253  if ((*(*itE))->getModel() != 0)
254  {
255  processNodes_.modelNode_ = (*(*itE))->getModel();
256  break;
257  }
258  }
260  throw Exception("LikelihoodCalculationSingleProcess::makeProcessNodes_: null modelNode_");
261 }
262 
263 
265 {
266  auto deltaNode = NumericMutable<double>::create(getContext_(), delta);
267 
269  {
270  processNodes_.ratesNode_->config.delta = deltaNode;
271  processNodes_.ratesNode_->config.type = config;
272  }
273 
275  // model nodes
276 
277  vector<shared_ptr<ProcessEdge>> vpn = processNodes_.treeNode_->getAllEdges();
278 
279  for (auto& it: vpn)
280  {
281  auto mN = it->getModel();
282  if (mN)
283  {
284  mN->config.delta = deltaNode;
285  mN->config.type = config;
286  }
287 
288  // auto pN=it->getProba();
289  // {
290  // pN->config.delta = deltaNode;
291  // pN->config.type = config;
292  // }
293  }
294 
296  // rootFrequencies node
297 
299  {
300  processNodes_.rootFreqsNode_->config.delta = deltaNode;
301  processNodes_.rootFreqsNode_->config.type = config;
302  }
303 }
304 
306 {
307  Parameter pRate("BrLen_rate", rate, Parameter::R_PLUS_STAR);
308 
309  auto rateNode = ConfiguredParameter::create(getContext_(), pRate);
310 
311  auto rateRef = ValueFromConfiguredParameter::create(getContext_(), {rateNode});
312 
314  // brlen nodes
315 
316  vector<shared_ptr<ProcessEdge>> vpn = processNodes_.treeNode_->getAllEdges();
317 
318  for (auto& it: vpn)
319  {
320  auto cp = it->getBrLen();
321 
322  if (cp)
323  {
324  auto mulref = CWiseMul<double, std::tuple<double, double>>::create (getContext_(), {cp->dependency(0), rateRef}, Dimension<double>());
325 
326  auto cp2 = ConfiguredParameter::resetDependencies(getContext_(), cp, {mulref});
327 
328  it->setBrLen(cp2);
329  }
330  }
331 
332  // Remove all BrLen parameters
333  auto parNames = getParameters().getParameterNames();
334 
335  for (auto& name:parNames)
336  {
337  if (name.substr(0, 5) == "BrLen")
338  deleteParameter_(name);
339  }
340 
341  shareParameter_(rateNode);
342 }
343 
345 {
346  if (shrunk)
347  return getSiteLikelihoodsTree_(nCat)->getRoot()->targetValue();
348  else
349  return expandVector(getSiteLikelihoodsTree_(nCat)->getRoot())->targetValue();
350 }
351 
353 {
354  auto nbCat = vRateCatTrees_.size();
355  auto allLk = std::make_shared<AllRatesSiteLikelihoods>(nbCat, shrunk ? getNumberOfDistinctSites() : getNumberOfSites());
356 
357  for (size_t nCat = 0; nCat < nbCat; nCat++)
358  {
359  allLk->row(Eigen::Index(nCat)) = getSiteLikelihoodsForAClass(nCat, shrunk);
360  }
361 
362  return *allLk;
363 }
364 
365 
366 /****************************************
367 * Construction methods
368 ****************************************/
370 {
371 // Set root frequencies
372 
373  size_t nbState = stateMap().getNumberOfModelStates();
374  rFreqs_ = processNodes_.rootFreqsNode_ ? ConfiguredParametrizable::createRowVector<ConfiguredFrequencySet, FrequenciesFromFrequencySet, Eigen::RowVectorXd>(
375  getContext_(), {processNodes_.rootFreqsNode_}, RowVectorDimension (Eigen::Index (nbState))) :
376  ConfiguredParametrizable::createRowVector<ConfiguredModel, EquilibriumFrequenciesFromModel, Eigen::RowVectorXd>(
377  getContext_(), {processNodes_.modelNode_}, RowVectorDimension (Eigen::Index (nbState)));
378 }
379 
380 
382 {
383  // Build conditional likelihoods up to root recursively.
384  if (!processNodes_.treeNode_->isRooted ())
385  {
386  throw Exception ("LikelihoodCalculationSingleProcess::makeForwardLikelihoodTree_ : PhyloTree must be rooted");
387  }
388 
390  {
391  uint nbCat = (uint)processNodes_.ratesNode_->targetValue()->getNumberOfCategories();
392 
393  vRateCatTrees_.resize(nbCat);
394 
395  for (uint nCat = 0; nCat < nbCat; nCat++)
396  {
398 
399  auto treeCat = std::make_shared<ProcessTree>(*processNodes_.treeNode_, catRef);
400 
401  vRateCatTrees_[nCat].phyloTree = treeCat;
402 
403  auto flt = std::make_shared<ForwardLikelihoodTree>(getContext_(), treeCat, stateMap());
404 
405  if (getShrunkData())
406  flt->initialize(*getShrunkData());
407  else
408  flt->initialize(*psites_);
409  vRateCatTrees_[nCat].flt = flt;
410  }
411  }
412  else
413  {
414  vRateCatTrees_.resize(1);
415  vRateCatTrees_[0].phyloTree = processNodes_.treeNode_;
416 
417  auto flt = std::make_shared<ForwardLikelihoodTree >(getContext_(), processNodes_.treeNode_, processNodes_.modelNode_->targetValue()->stateMap());
418 
419  if (getShrunkData())
420  flt->initialize(*getShrunkData());
421  else
422  flt->initialize(*psites_);
423  vRateCatTrees_[0].flt = flt;
424  }
425 }
426 
427 
429 {
430  if (vRateCatTrees_.size() == 0)
432 
433  size_t nbDistSite = getNumberOfDistinctSites();
434  // Set root frequencies
435  if (rFreqs_ == 0)
436  makeRootFreqs_();
437 
438  ValueRef<RowLik> sL;
439 
441  {
442  std::vector<std::shared_ptr<Node_DF>> vLikRoot;
443 
444  auto zero = getContext_().getZero();
445 
446  for (auto& rateCat: vRateCatTrees_)
447  {
449  getContext_(), {rFreqs_, rateCat.flt->getForwardLikelihoodArrayAtRoot()},
450  RowVectorDimension (Eigen::Index (nbDistSite))));
451  }
452 
453  // for (size_t nCat = 0; nCat < vRateCatTrees_.size(); nCat++)
454  // {
455  // vLikRoot.push_back(ProbabilityFromDiscreteDistribution::create(getContext_(), {processNodes_.ratesNode_}, (uint)nCat));
456  // }
457 
458 
459 // sL = CWiseMean<RowLik, ReductionOf<RowLik>, ReductionOf<double> >::create(getContext_(), std::move(vLikRoot), RowVectorDimension (Eigen::Index(nbDistSite)));
460 
463  vLikRoot.push_back(catProb);
464 
465  sL = CWiseMean<RowLik, ReductionOf<RowLik>, RowLik>::create(getContext_(), std::move(vLikRoot), RowVectorDimension (Eigen::Index(nbDistSite)));
466  }
467  else
468  {
470  getContext_(), {rFreqs_, vRateCatTrees_[0].flt->getForwardLikelihoodArrayAtRoot()}, RowVectorDimension (Eigen::Index (nbDistSite)));
471  }
472 
473 
474  // likelihoods per distinct site
475  setSiteLikelihoods(sL, true);
476 
477  // likelihoods per site
479 
480  // global likelihood
481  ValueRef<DataLik> val;
482  if (rootPatternLinks_)
483  val = SumOfLogarithms<RowLik>::create (getContext_(), {sL, rootWeights_}, RowVectorDimension (Eigen::Index (nbDistSite)));
484  else
485  val = SumOfLogarithms<RowLik>::create (getContext_(), {sL}, RowVectorDimension (Eigen::Index (nbDistSite)));
486 
487  setLikelihoodNode(val);
488 
489 #ifdef DEBUG
490  using bpp::DotOptions;
492  "debug_lik.dot", {likelihood_.get()}, DotOptions::DetailedNodeInfo);
493 #endif
494 }
495 
496 
498 {
499  // Already built
500  if (condLikelihoodTree_ && condLikelihoodTree_->hasNode(speciesId))
501  return;
502 
503  if (vRateCatTrees_.size() == 0)
505 
506  if (rFreqs_ == 0)
507  makeRootFreqs_();
508 
509  auto nbDistSite = Eigen::Index(getNumberOfDistinctSites());
510  auto nbState = Eigen::Index(stateMap().getNumberOfModelStates());
511  MatrixDimension likelihoodMatrixDim = conditionalLikelihoodDimension (nbState, nbDistSite);
512 
513  const auto phylotree = process_->getParametrizablePhyloTree();
514 
515  ValueRef<RowLik> siteLikelihoodsNode;
516 
517  std::shared_ptr<ConditionalLikelihood> cond(0);
518 
519  std::vector<NodeRef> vCondRate;
520 
521  SiteLikelihoodsRef distinctSiteLikelihoodsNode;
522  ConditionalLikelihoodRef conditionalLikelihoodsNode;
523 
524  std::vector<std::shared_ptr<Node_DF>> vRoot; // if several rates
525 
526  if (!condLikelihoodTree_)
527  condLikelihoodTree_ = std::make_shared<ConditionalLikelihoodTree>(phylotree->getGraph());
528 
530 
531  for (auto& rateCat: vRateCatTrees_)
532  {
533  if (!rateCat.blt)
534  rateCat.blt = std::make_shared<BackwardLikelihoodTree>(getContext_(), rateCat.flt, rateCat.phyloTree, rFreqs_, stateMap(), nbDistSite);
535 
536  if (!rateCat.clt)
537  rateCat.clt = std::make_shared<ConditionalLikelihoodDAG>(rateCat.flt->getGraph());
538 
539  if (!rateCat.lt)
540  rateCat.lt = std::make_shared<SiteLikelihoodsDAG>(rateCat.flt->getGraph());
541 
542 
543  if (!rateCat.speciesLt)
544  rateCat.speciesLt = std::make_shared<SiteLikelihoodsTree>(phylotree->getGraph());
545 
546  auto& dagIndexes = rateCat.flt->getDAGNodesIndexes(speciesId);
547 
548  std::vector<std::shared_ptr<Node_DF>> vCond;
549 
550  for (const auto& index : dagIndexes)
551  {
552  if (rateCat.clt->hasNode(index))
553  {
554  cond = rateCat.clt->getNode(index);
555  if (dagIndexes.size() > 1) // for sum
556  vCond.push_back(cond);
557  continue;
558  }
559 
560  auto condAbove = rateCat.blt->getBackwardLikelihoodArray(index);
561  auto condBelow = rateCat.flt->getForwardLikelihoodArray(index);
562 
563  cond = BuildConditionalLikelihood::create (
564  getContext_(), {condAbove, condBelow}, likelihoodMatrixDim);
565 
566  if (dagIndexes.size() > 1) // for sum
567  vCond.push_back(cond);
568 
569  rateCat.clt->associateNode(cond, rateCat.flt->getNodeGraphid(rateCat.flt->getNode(index)));
570  rateCat.clt->setNodeIndex(cond, index);
571 
572  // Site Likelihoods on this point
574  getContext_(), {one, cond}, RowVectorDimension (nbDistSite));
575 
576  rateCat.lt->associateNode(lt, rateCat.flt->getNodeGraphid(rateCat.flt->getNode(index)));
577  rateCat.lt->setNodeIndex(lt, index);
578  }
579 
580  /*
581  * If several DAG nodes related with this species node, sum the
582  * likelihoods of all (already multiplied by their probability).
583  *
584  */
585 
586  if (dagIndexes.size() > 1)
587  cond = CWiseAdd<MatrixLik, ReductionOf<MatrixLik>>::create(getContext_(), std::move(vCond), likelihoodMatrixDim);
588 
589  // for Lik at Node
590  auto siteLikelihoodsCat = LikelihoodFromRootConditionalAtRoot::create (
591  getContext_(), {one, cond}, RowVectorDimension (nbDistSite));
592 
593  if (!rateCat.speciesLt->hasNode(speciesId))
594  {
595  rateCat.speciesLt->associateNode(siteLikelihoodsCat, phylotree->getNodeGraphid(phylotree->getNode(speciesId)));
596  rateCat.speciesLt->setNodeIndex(siteLikelihoodsCat, speciesId);
597  }
598 
600  {
601  distinctSiteLikelihoodsNode = siteLikelihoodsCat;
602  conditionalLikelihoodsNode = cond;
603  break;
604  }
605  else
606  {
607  // For Conditional at Node
608  vCondRate.push_back(cond);
609  vRoot.push_back(siteLikelihoodsCat);
610  }
611  }
612 
613  if (catProb_) // Should have been computed before
614  {
616 
617  vRoot.push_back(catProb);
618  vCondRate.push_back(catProb);
619 
620  distinctSiteLikelihoodsNode = CWiseMean<RowLik, ReductionOf<RowLik>, RowLik>::create(getContext_(), std::move(vRoot), RowVectorDimension (nbDistSite));
621 
622  conditionalLikelihoodsNode = CWiseMean<MatrixLik, ReductionOf<MatrixLik>, RowLik>::create(getContext_(), std::move(vCondRate), MatrixDimension (nbState, nbDistSite));
623  }
624 
625  condLikelihoodTree_->associateNode(conditionalLikelihoodsNode, phylotree->getNodeGraphid(phylotree->getNode(speciesId)));
626  condLikelihoodTree_->setNodeIndex(conditionalLikelihoodsNode, speciesId);
627 
628  // We want -log(likelihood)
629  // auto totalNegLogLikelihood =
630  // CWiseNegate<double>::create (getContext_(), {totalLogLikelihood}, Dimension<double> ());
631  // return totalNegLogLikelihood;
632 }
633 
635 {
636  if (vRateCatTrees_.size() == 0)
638 
639  if (rFreqs_ == 0)
640  makeRootFreqs_();
641 
642  auto nbDistSite = Eigen::Index(getNumberOfDistinctSites());
643  auto nbState = Eigen::Index(stateMap().getNumberOfModelStates());
644  MatrixDimension likelihoodMatrixDim = conditionalLikelihoodDimension (nbState, nbDistSite);
645 
646  ValueRef<RowLik> siteLikelihoodsNode;
647 
649 
650  for (auto& rateCat: vRateCatTrees_)
651  {
652  if (!rateCat.clt)
653  rateCat.clt = std::make_shared<ConditionalLikelihoodDAG>(rateCat.flt->getGraph());
654 
655  if (rateCat.clt->hasNode(nodeId)) // already computed
656  continue;
657 
658  if (!rateCat.blt)
659  rateCat.blt = std::make_shared<BackwardLikelihoodTree>(getContext_(), rateCat.flt, rateCat.phyloTree, rFreqs_, stateMap(), nbDistSite);
660 
661  if (!rateCat.lt)
662  rateCat.lt = std::make_shared<SiteLikelihoodsDAG>(rateCat.flt->getGraph());
663 
664  // Conditional Likelihoods on this node
665 
666  auto condAbove = rateCat.blt->getBackwardLikelihoodArray(nodeId);
667  auto condBelow = rateCat.flt->getForwardLikelihoodArray(nodeId);
668 
669  auto cond = BuildConditionalLikelihood::create (
670  getContext_(), {condAbove, condBelow}, likelihoodMatrixDim);
671 
672  rateCat.clt->associateNode(cond, rateCat.flt->getNodeGraphid(rateCat.flt->getNode(nodeId)));
673  rateCat.clt->setNodeIndex(cond, nodeId);
674 
675  // Site Likelihoods on this node
677  getContext_(), {one, cond}, RowVectorDimension (Eigen::Index (nbDistSite)));
678 
679  rateCat.lt->associateNode(lt, rateCat.flt->getNodeGraphid(rateCat.flt->getNode(nodeId)));
680  rateCat.lt->setNodeIndex(lt, nodeId);
681  }
682 }
683 
684 
685 std::shared_ptr<SiteLikelihoodsTree> LikelihoodCalculationSingleProcess::getSiteLikelihoodsTree_(size_t nCat)
686 {
687  if (nCat >= vRateCatTrees_.size())
688  throw Exception("LikelihoodCalculationSingleProcess::getSiteLikelihoodsTree : Bad Class number " + TextTools::toString(nCat));
689 
690  if (!shrunkData_)
691  throw Exception("LikelihoodCalculationSingleProcess::getSiteLikelihoodsTree : data not set.");
692 
693  if (!getLikelihoodNode_())
695 
696  if (vRateCatTrees_[nCat].speciesLt == 0)
697  makeLikelihoodsAtNode_(getTreeNode(nCat)->getRoot()->getSpeciesIndex());
698 
699  return vRateCatTrees_[nCat].speciesLt;
700 }
701 
702 
704 {
705  // compute forward likelihoods for all nodes (not the quickest, but
706  // in practice they are all needed)
707 
708  if (!getLikelihoodNode_())
709  makeLikelihoods();
710 
711  if (nCat >= vRateCatTrees_.size())
712  throw Exception("LikelihoodCalculationSingleProcess::getForwardLikelihoodsAtNodeForClass : bad class number " + TextTools::toString(nCat));
713 
714  return vRateCatTrees_[nCat].flt->getNode(nodeId);
715 }
716 
718 {
719  // compute likelihoods for all edges with similar species index
720  // (not the quickest, but in practice they are all needed)
721 
723 
724  if (nCat >= vRateCatTrees_.size())
725  throw Exception("LikelihoodCalculationSingleProcess::getConditionalLikelihoodsAtNodeForClass : bad class number " + TextTools::toString(nCat));
726 
727  return vRateCatTrees_[nCat].clt->getNode(nodeId);
728 }
729 
731 {
732  // compute likelihoods for all edges with similar species index
733  // (not the quickest, but in practice they are all needed)
734 
736 
737  if (nCat >= vRateCatTrees_.size())
738  throw Exception("LikelihoodCalculationSingleProcess::getConditionalLikelihoodsAtNodeForClass : bad class number " + TextTools::toString(nCat));
739 
740  return vRateCatTrees_[nCat].lt->getNode(nodeId);
741 }
742 
743 
745 {
746  // compute likelihoods for all edges with similar species index
747  // (not the quickest, but in practice they are all needed)
748 
749  auto spId = getTreeNode(nCat)->getEdge(edgeId)->getSpeciesIndex();
750 
751  if (!(condLikelihoodTree_ && condLikelihoodTree_->hasNode(spId)))
753 
754  if (nCat >= vRateCatTrees_.size())
755  throw Exception("LikelihoodCalculationSingleProcess::getForwardLikelihoodsAtNodeForClass : bad class number " + TextTools::toString(nCat));
756 
757  return vRateCatTrees_[nCat].blt->getEdge(edgeId);
758 }
759 
761 {
762  // compute likelihoods for all nodes with similar species index
763  // (not the quickest, but in practice they are all needed)
764 
766 
767  if (nCat >= vRateCatTrees_.size())
768  throw Exception("LikelihoodCalculationSingleProcess::getForwardLikelihoodsAtNodeForClass : bad class number " + TextTools::toString(nCat));
769 
770  return vRateCatTrees_[nCat].blt->getNode(nodeId);
771 }
772 
773 
775 {
776  if (vRateCatTrees_.size() == 0)
777  throw Exception("LikelihoodCalculationSingleProcess::getNodeIds. ForwardLikelihoodTree not computed.");
778 
779  return vRateCatTrees_[0].flt->getDAGNodesIndexes(speciesId);
780 }
781 
782 const DAGindexes& LikelihoodCalculationSingleProcess::getEdgesIds(uint speciesId, size_t nCat) const
783 {
784  if (nCat >= vRateCatTrees_.size())
785  throw Exception("LikelihoodCalculationSingleProcess::getEdgesIds : bad class number " + TextTools::toString(nCat));
786 
787  return vRateCatTrees_[nCat].flt->getDAGEdgesIndexes(speciesId);
788 }
789 
790 std::shared_ptr<ForwardLikelihoodTree> LikelihoodCalculationSingleProcess::getForwardLikelihoodTree(size_t nCat)
791 {
792  if (nCat >= vRateCatTrees_.size())
793  throw Exception("LikelihoodCalculationSingleProcess::getForwardTree : bad class number " + TextTools::toString(nCat));
794 
795  return vRateCatTrees_[nCat].flt;
796 }
797 
798 std::shared_ptr<BackwardLikelihoodTree> LikelihoodCalculationSingleProcess::getBackwardLikelihoodTree(size_t nCat)
799 {
800  if (nCat >= vRateCatTrees_.size())
801  throw Exception("LikelihoodCalculationSingleProcess::getBackwardTree : bad class number " + TextTools::toString(nCat));
802 
803  return vRateCatTrees_[nCat].blt;
804 }
805 
807 {
808  psites_.reset();
809  rootPatternLinks_.reset();
810  rootWeights_.reset();
811  shrunkData_.reset();
812  condLikelihoodTree_.reset();
813 
814  vRateCatTrees_.clear();
815 
817 }
818 
820 {
821  if (flt)
822  {
823  auto& c = flt->getContext();
824  const auto& lroot = flt->getForwardLikelihoodArrayAtRoot();
825  c.erase(lroot);
826  }
827 
828  phyloTree.reset();
829 }
const ParameterList & getIndependentParameters() const
void deleteParameter_(size_t index)
void aliasParameters(const std::string &p1, const std::string &p2)
void shareParameter_(const std::shared_ptr< Parameter > &parameter)
bool hasParameter(const std::string &name) const override
const std::shared_ptr< Parameter > & getParameter(const std::string &name) const
ParameterList & getParameters_() override
const ParameterList & getParameters() const override
void setSiteLikelihoods(SiteLikelihoodsRef ll, bool shrunk=false)
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps, uint nCat)
std::shared_ptr< ConfiguredFrequencySet > getFrequencies(size_t freqIndex)
std::shared_ptr< ConfiguredDistribution > getRateDistribution(size_t distIndex)
const SubstitutionProcessCollection & collection() const
Data flow node representing a parameter.
Definition: Parameter.h:27
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps, const Parameter &param)
Build a new ConfiguredParameter node.
Definition: Parameter.h:36
static std::shared_ptr< Self > resetDependencies(Context &c, std::shared_ptr< Self > self, NodeRefVec &&deps)
Definition: Parameter.h:51
static std::shared_ptr< Self > create(Context &c, const Dimension< T > &dim)
Build a new ConstantOne node of the given dimension.
Context for dataflow node construction.
Definition: DataFlow.h:527
NodeRef getZero()
Definition: DataFlow.h:568
static ValueRef< R > create(Context &c, NodeRefVec &&deps, const Dimension< R > &dim)
Build a new Convert node with the given output dimensions.
SiteLikelihoodsRef getLikelihoodsAtNodeForClass(uint nodeId, size_t nCat)
Get shrunked conditional likelihood matrix at Node (ie just above the node), for a given rate class.
std::shared_ptr< SiteLikelihoodsTree > getSiteLikelihoodsTree_(size_t nCat)
std::shared_ptr< ConditionalLikelihoodTree > condLikelihoodTree_
std::shared_ptr< ForwardLikelihoodTree > getForwardLikelihoodTree(size_t nCat)
std::shared_ptr< const SubstitutionProcessInterface > process_
AllRatesSiteLikelihoods getSiteLikelihoodsForAllClasses(bool shrunk=false)
Output array (Classes X Sites) of likelihoods for all sites & classes.
std::shared_ptr< const AlignmentDataInterface > psites_
LikelihoodCalculationSingleProcess(Context &context, std::shared_ptr< const AlignmentDataInterface > sites, std::shared_ptr< const SubstitutionProcessInterface > process)
std::shared_ptr< SiteWeights > rootWeights_
The frequency of each site.
ConditionalLikelihoodRef getBackwardLikelihoodsAtEdgeForClass(uint edgeId, size_t nCat)
ConditionalLikelihoodRef getForwardLikelihoodsAtNodeForClass(uint nodeId, size_t nCat)
std::shared_ptr< BackwardLikelihoodTree > getBackwardLikelihoodTree(size_t nCat)
void makeLikelihoodsAtNode_(uint nodeId)
Compute the likelihood at a given node in the tree, which number may not be the same number in the DA...
void makeLikelihoodsAtDAGNode_(uint nodeId)
Compute the likelihood at a given node in the DAG,.
std::shared_ptr< const AlignmentDataInterface > getShrunkData() const
ValueRef< RowLik > expandVector(ValueRef< RowLik > vector)
std::shared_ptr< AlignmentDataInterface > shrunkData_
const DAGindexes & getEdgesIds(uint speciesId, size_t nCat) const
Get indexes of the non-empty edges in the Likelihood DAG that have a given species index for a given ...
ConditionalLikelihoodRef getBackwardLikelihoodsAtNodeForClass(uint nodeId, size_t nCat)
const DAGindexes & getNodesIds(uint speciesId) const
Get indexes of the nodes in the Likelihood DAG that have a given species index.
ConditionalLikelihoodRef getConditionalLikelihoodsAtNodeForClass(uint nodeId, size_t nCat)
RowLik getSiteLikelihoodsForAClass(size_t nCat, bool shrunk=false)
Get site likelihoods for a rate category.
std::shared_ptr< ProcessTree > getTreeNode(size_t nCat)
Get process tree for a rate category.
void setNumericalDerivateConfiguration(double delta, const NumericalDerivativeType &config)
Set derivation procedure (see DataFlowNumeric.h)
ValueRef< DataLik > getLikelihoodNode_()
void setLikelihoodNode(ValueRef< DataLik > ll)
static ValueRef< R > create(Context &c, NodeRefVec &&deps, const Dimension< R > &dim)
Build a new MatrixProduct node with the given output dimensions.
static std::shared_ptr< Self > create(Context &c, Args &&... args)
Build a new NumericConstant node with T(args...) value.
static std::shared_ptr< Self > create(Context &, Args &&... args)
Build a new NumericMutable node with T(args...) value.
virtual std::vector< std::string > getParameterNames() const
static const std::shared_ptr< IntervalConstraint > R_PLUS_STAR
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps)
static std::shared_ptr< ProcessTree > makeProcessTree(Context &context, std::shared_ptr< const SubstitutionProcessInterface > process, ParameterList &parList, const std::string &suff="")
Create a Process Tree following a Substitution Process. Tree Node parameters are got from ConfiguredP...
Data structure for site patterns.
Definition: SitePatterns.h:35
const std::vector< unsigned int > & getWeights() const
Definition: SitePatterns.h:99
const IndicesType & getIndices() const
Definition: SitePatterns.h:103
std::unique_ptr< AlignmentDataInterface > getSites() const
virtual size_t getNumberOfModelStates() const =0
SubstitutionProcessCollectionMember & substitutionProcess(size_t i)
static ValueRef< DataLik > create(Context &c, NodeRefVec &&deps, const Dimension< F > &mDim)
Build a new SumOfLogarithms node with the given input matrix dimensions.
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps)
Definition: Parameter.cpp:117
std::string toString(T t)
T one(const Dimension< T > &)
T zero(const Dimension< T > &)
Defines the basic types of data flow nodes.
std::shared_ptr< Value< T > > ValueRef
Shared pointer alias for Value<T>.
Definition: DataFlow.h:84
std::vector< uint > DAGindexes
Helper: create a map with mutable dataflow nodes for each branch of the tree. The map is indexed by b...
ValueRef< MatrixLik > ConditionalLikelihoodRef
DotOptions
Definition: DataFlow.h:47
MatrixDimension conditionalLikelihoodDimension(Eigen::Index nbState, Eigen::Index nbSite)
void writeGraphToDot(std::ostream &os, const std::vector< const Node_DF * > &nodes, DotOptions opt)
Write dataflow graph starting at nodes to output stream.
Definition: DataFlow.cpp:564
ValueRef< RowLik > SiteLikelihoodsRef
Specialisation of Dimension<T> for floating point types.
Basic matrix dimension type.