7 #include <unordered_map>
18 using namespace numeric;
19 using namespace Eigen;
21 LikelihoodCalculationSingleProcess::LikelihoodCalculationSingleProcess(
23 std::shared_ptr<const AlignmentDataInterface> sites,
24 std::shared_ptr<const SubstitutionProcessInterface> process) :
26 rootPatternLinks_(), rootWeights_(), shrunkData_(),
27 processNodes_(), rFreqs_(),
28 vRateCatTrees_(), catProb_(), condLikelihoodTree_(0)
30 if (!
process_->getParametrizablePhyloTree())
31 throw Exception(
"LikelihoodCalculationSingleProcess::LikelihoodCalculationSingleProcess: missing tree in SubstitutionProcess.");
41 std::shared_ptr<const SubstitutionProcessInterface> process) :
43 process_(process), psites_(),
44 rootPatternLinks_(), rootWeights_(), shrunkData_(),
45 processNodes_(), rFreqs_(),
46 vRateCatTrees_(), catProb_(), condLikelihoodTree_(0)
48 if (!
process_->getParametrizablePhyloTree())
49 throw Exception(
"LikelihoodCalculationSingleProcess::LikelihoodCalculationSingleProcess: missing tree in SubstitutionProcess.");
58 std::shared_ptr<CollectionNodes> collection,
59 std::shared_ptr<const AlignmentDataInterface> sites,
62 process_(collection->collection().getSubstitutionProcess(nProcess)),
64 rootPatternLinks_(), rootWeights_(), shrunkData_(),
65 processNodes_(), rFreqs_(),
66 vRateCatTrees_(), catProb_(), condLikelihoodTree_(0)
68 if (!
process_->getParametrizablePhyloTree())
69 throw Exception(
"LikelihoodCalculationSingleProcess::LikelihoodCalculationSingleProcess: missing tree in SubstitutionProcess.");
80 std::shared_ptr<CollectionNodes> collection,
83 process_(collection->collection().getSubstitutionProcess(nProcess)),
85 rootPatternLinks_(), rootWeights_(), shrunkData_(),
86 processNodes_(), rFreqs_(),
87 vRateCatTrees_(), catProb_(), condLikelihoodTree_(0)
98 process_(lik.process_), psites_(lik.psites_),
99 rootPatternLinks_(lik.rootPatternLinks_), rootWeights_(), shrunkData_(lik.shrunkData_),
100 processNodes_(), rFreqs_(),
101 vRateCatTrees_(), catProb_(), condLikelihoodTree_(0)
116 Eigen::RowVectorXi weights(nbSites);
117 for (std::size_t i = 0; i < nbSites; i++)
119 weights(Eigen::Index(i)) = int(patterns.
getWeights()[i]);
127 cerr <<
"LikelihoodCalculationSingleProcess::makeProcessNodes_(){" << endl;
130 const auto& paramProc =
process_->getIndependentParameters();
132 for (
size_t i = 0; i < paramProc.size(); ++i)
138 for (
size_t i = 0; i < paramProc.size(); i++)
140 auto name = paramProc[i].getName();
144 for (
const auto& s:vs)
152 auto spcm = dynamic_pointer_cast<const SubstitutionProcessCollectionMember>(
process_);
158 std::string suff = spcm ? (
"_" +
TextTools::toString(spcm->getRateDistributionNumber())) :
"";
160 auto rates =
process_->getRateDistribution();
161 if (rates && dynamic_pointer_cast<const ConstantRateDistribution>(rates) ==
nullptr)
172 auto root =
process_->getRootFrequencySet();
181 for (itE->start(); !itE->end(); itE->next())
183 if ((*(*itE))->getModel() != 0)
190 throw Exception(
"LikelihoodCalculationSingleProcess::makeProcessNodes_: null modelNode_");
193 cerr <<
"likelihoodcalculationsingleprocess::makeprocessnodes_()}" << endl;
204 for (
size_t i = 0; i < paramProc.size(); i++)
206 auto name = paramProc[i].getName();
208 throw Exception(
"LikelihoodCalculationSingleProcess::makeProcessNodes_ : CollectionNodes does not have parameter " + name);
215 for (
size_t i = 0; i < paramProc.size(); i++)
217 auto name = paramProc[i].getName();
218 auto vs = spcm.getAlias(name);
221 for (
const auto& s:vs)
231 auto rates = spcm.getRateDistribution();
233 if (dynamic_pointer_cast<const ConstantRateDistribution>(rates) ==
nullptr)
244 if (!spcm.isStationary())
251 for (itE->start(); !itE->end(); itE->next())
253 if ((*(*itE))->getModel() != 0)
260 throw Exception(
"LikelihoodCalculationSingleProcess::makeProcessNodes_: null modelNode_");
281 auto mN = it->getModel();
284 mN->config.delta = deltaNode;
285 mN->config.type = config;
320 auto cp = it->getBrLen();
335 for (
auto& name:parNames)
337 if (name.substr(0, 5) ==
"BrLen")
357 for (
size_t nCat = 0; nCat < nbCat; nCat++)
376 ConfiguredParametrizable::createRowVector<ConfiguredModel, EquilibriumFrequenciesFromModel, Eigen::RowVectorXd>(
386 throw Exception (
"LikelihoodCalculationSingleProcess::makeForwardLikelihoodTree_ : PhyloTree must be rooted");
395 for (uint nCat = 0; nCat < nbCat; nCat++)
442 std::vector<std::shared_ptr<Node_DF>> vLikRoot;
463 vLikRoot.push_back(catProb);
510 auto nbState = Eigen::Index(
stateMap().getNumberOfModelStates());
513 const auto phylotree =
process_->getParametrizablePhyloTree();
517 std::shared_ptr<ConditionalLikelihood> cond(0);
519 std::vector<NodeRef> vCondRate;
524 std::vector<std::shared_ptr<Node_DF>> vRoot;
534 rateCat.blt = std::make_shared<BackwardLikelihoodTree>(
getContext_(), rateCat.flt, rateCat.phyloTree,
rFreqs_,
stateMap(), nbDistSite);
537 rateCat.clt = std::make_shared<ConditionalLikelihoodDAG>(rateCat.flt->getGraph());
540 rateCat.lt = std::make_shared<SiteLikelihoodsDAG>(rateCat.flt->getGraph());
543 if (!rateCat.speciesLt)
544 rateCat.speciesLt = std::make_shared<SiteLikelihoodsTree>(phylotree->getGraph());
546 auto& dagIndexes = rateCat.flt->getDAGNodesIndexes(speciesId);
548 std::vector<std::shared_ptr<Node_DF>> vCond;
550 for (
const auto& index : dagIndexes)
552 if (rateCat.clt->hasNode(index))
554 cond = rateCat.clt->getNode(index);
555 if (dagIndexes.size() > 1)
556 vCond.push_back(cond);
560 auto condAbove = rateCat.blt->getBackwardLikelihoodArray(index);
561 auto condBelow = rateCat.flt->getForwardLikelihoodArray(index);
563 cond = BuildConditionalLikelihood::create (
564 getContext_(), {condAbove, condBelow}, likelihoodMatrixDim);
566 if (dagIndexes.size() > 1)
567 vCond.push_back(cond);
569 rateCat.clt->associateNode(cond, rateCat.flt->getNodeGraphid(rateCat.flt->getNode(index)));
570 rateCat.clt->setNodeIndex(cond, index);
576 rateCat.lt->associateNode(lt, rateCat.flt->getNodeGraphid(rateCat.flt->getNode(index)));
577 rateCat.lt->setNodeIndex(lt, index);
586 if (dagIndexes.size() > 1)
593 if (!rateCat.speciesLt->hasNode(speciesId))
595 rateCat.speciesLt->associateNode(siteLikelihoodsCat, phylotree->getNodeGraphid(phylotree->getNode(speciesId)));
596 rateCat.speciesLt->setNodeIndex(siteLikelihoodsCat, speciesId);
601 distinctSiteLikelihoodsNode = siteLikelihoodsCat;
602 conditionalLikelihoodsNode = cond;
608 vCondRate.push_back(cond);
609 vRoot.push_back(siteLikelihoodsCat);
617 vRoot.push_back(catProb);
618 vCondRate.push_back(catProb);
625 condLikelihoodTree_->associateNode(conditionalLikelihoodsNode, phylotree->getNodeGraphid(phylotree->getNode(speciesId)));
643 auto nbState = Eigen::Index(
stateMap().getNumberOfModelStates());
653 rateCat.clt = std::make_shared<ConditionalLikelihoodDAG>(rateCat.flt->getGraph());
655 if (rateCat.clt->hasNode(nodeId))
659 rateCat.blt = std::make_shared<BackwardLikelihoodTree>(
getContext_(), rateCat.flt, rateCat.phyloTree,
rFreqs_,
stateMap(), nbDistSite);
662 rateCat.lt = std::make_shared<SiteLikelihoodsDAG>(rateCat.flt->getGraph());
666 auto condAbove = rateCat.blt->getBackwardLikelihoodArray(nodeId);
667 auto condBelow = rateCat.flt->getForwardLikelihoodArray(nodeId);
669 auto cond = BuildConditionalLikelihood::create (
670 getContext_(), {condAbove, condBelow}, likelihoodMatrixDim);
672 rateCat.clt->associateNode(cond, rateCat.flt->getNodeGraphid(rateCat.flt->getNode(nodeId)));
673 rateCat.clt->setNodeIndex(cond, nodeId);
679 rateCat.lt->associateNode(lt, rateCat.flt->getNodeGraphid(rateCat.flt->getNode(nodeId)));
680 rateCat.lt->setNodeIndex(lt, nodeId);
691 throw Exception(
"LikelihoodCalculationSingleProcess::getSiteLikelihoodsTree : data not set.");
712 throw Exception(
"LikelihoodCalculationSingleProcess::getForwardLikelihoodsAtNodeForClass : bad class number " +
TextTools::toString(nCat));
725 throw Exception(
"LikelihoodCalculationSingleProcess::getConditionalLikelihoodsAtNodeForClass : bad class number " +
TextTools::toString(nCat));
738 throw Exception(
"LikelihoodCalculationSingleProcess::getConditionalLikelihoodsAtNodeForClass : bad class number " +
TextTools::toString(nCat));
749 auto spId =
getTreeNode(nCat)->getEdge(edgeId)->getSpeciesIndex();
755 throw Exception(
"LikelihoodCalculationSingleProcess::getForwardLikelihoodsAtNodeForClass : bad class number " +
TextTools::toString(nCat));
768 throw Exception(
"LikelihoodCalculationSingleProcess::getForwardLikelihoodsAtNodeForClass : bad class number " +
TextTools::toString(nCat));
777 throw Exception(
"LikelihoodCalculationSingleProcess::getNodeIds. ForwardLikelihoodTree not computed.");
823 auto& c =
flt->getContext();
824 const auto& lroot =
flt->getForwardLikelihoodArrayAtRoot();
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 > ¶meter)
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 cleanAllLikelihoods()
SiteLikelihoodsRef patternedSiteLikelihoods_
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
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.
static ValueRef< R > create(Context &c, NodeRefVec &&deps, const Dimension< R > &dim)
Build a new Convert node with the given output dimensions.
std::shared_ptr< ConfiguredFrequencySet > rootFreqsNode_
std::shared_ptr< ProcessTree > treeNode_
std::shared_ptr< ConfiguredDistribution > ratesNode_
std::shared_ptr< ConfiguredModel > modelNode_
std::shared_ptr< ForwardLikelihoodTree > flt
std::shared_ptr< ProcessTree > phyloTree
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)
ValueRef< PatternType > rootPatternLinks_
ValueRef< Eigen::RowVectorXd > rFreqs_
std::shared_ptr< ConditionalLikelihoodTree > condLikelihoodTree_
std::shared_ptr< ForwardLikelihoodTree > getForwardLikelihoodTree(size_t nCat)
std::shared_ptr< const SubstitutionProcessInterface > process_
ProcessNodes processNodes_
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)
size_t getNumberOfSites() const
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 cleanAllLikelihoods()
void makeLikelihoodsAtDAGNode_(uint nodeId)
Compute the likelihood at a given node in the DAG,.
std::shared_ptr< const AlignmentDataInterface > getShrunkData() const
ValueRef< Eigen::RowVectorXd > catProb_
size_t getNumberOfDistinctSites() const
ValueRef< RowLik > expandVector(ValueRef< RowLik > vector)
const StateMapInterface & stateMap() const
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)
void makeForwardLikelihoodTree_()
void makeLikelihoodsAtRoot_()
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)
std::vector< RateCategoryTrees > vRateCatTrees_
void setClockLike(double rate=1)
ValueRef< DataLik > likelihood_
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.
const std::vector< unsigned int > & getWeights() const
const IndicesType & getIndices() const
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.
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>.
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
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.
ValueRef< RowLik > SiteLikelihoodsRef
Specialisation of Dimension<T> for floating point types.
Basic matrix dimension type.