bpp-phyl3 3.0.0
PhyloLikelihoodFormula.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
10
12
13using namespace bpp;
14using namespace std;
15
16PhyloLikelihoodFormula::PhyloLikelihoodFormula(
17 Context& context,
18 std::shared_ptr<PhyloLikelihoodContainer> pC,
19 const std::string& formula,
20 bool inCollection) :
23 AbstractPhyloLikelihoodSet(context, pC, {}, inCollection),
24 compTree_(),
25 likCal_(make_shared<LikelihoodCalculation>(context))
26{
27 readFormula(formula, inCollection);
28 likCal_->setLikelihoodNode(makeLikelihoods());
29
30 // using bpp::DotOptions;
31 // writeGraphToDot(
32 // "formula.dot", {likCal_->getLikelihoodNode().get()});//, DotOptions::DetailedNodeInfo | DotOp
33}
34
35
36void PhyloLikelihoodFormula::readFormula(const std::string& formula, bool inCollection)
37{
38 map<string, shared_ptr<FunctionInterface>> functionNames;
39
40 const vector<size_t>& nPhyl = getPhyloContainer()->getNumbersOfPhyloLikelihoods();
41
42 for (size_t i = 0; i < nPhyl.size(); i++)
43 {
44 functionNames["phylo" + TextTools::toString(nPhyl[i])] = getPhyloLikelihood(nPhyl[i]);
45 }
46
47 compTree_ = make_unique<ComputationTree>(formula, functionNames);
48
49 // add used Phylolikelihoods
50
51 string popout = output();
52
53 StringTokenizer st(popout, "phylo", true, true);
54 st.nextToken();
55
56 vector<size_t> phyldep;
57
58 while (st.hasMoreToken())
59 {
60 string ex = st.nextToken();
61 size_t np = (size_t)(atoi(ex.c_str()));
62 if (!hasPhyloLikelihood(np))
63 addPhyloLikelihood(np, inCollection ? "" : "_" + ex);
64 }
65}
66
68{
69 return compTree_->output();
70}
71
73{
74 auto cst = dynamic_pointer_cast<ConstantOperator>(op);
75 if (cst)
76 return NumericConstant<DataLik>::create(context_, cst->getValue());
77
78 auto neg = dynamic_pointer_cast<NegativeOperator>(op);
79 if (neg)
80 {
81 auto sonDF = makeLikelihoodsFromOperator(neg->getSon());
83 }
84
85 auto bin = dynamic_pointer_cast<BinaryOperator>(op);
86 if (bin)
87 {
88 auto left = makeLikelihoodsFromOperator(bin->getLeftSon());
89 auto right = makeLikelihoodsFromOperator(bin->getRightSon());
90
91 switch (bin->getSymbol())
92 {
93 case '+':
95 case '-':
97 case '/':
98 {
101 }
102 case '*':
104 default:
106 }
107 }
108
109 auto mat = dynamic_pointer_cast<MathOperator>(op);
110 if (mat)
111 {
112 auto sonDF = makeLikelihoodsFromOperator(mat->getSon());
113 auto name = mat->getName();
114 if (name == "exp")
116 else if (name == "log")
118 else
119 throw Exception("PhyloLikelihoodFormula::Makelikelihoodsfromoperator : unknown function " + name + ". Ask developers.");
120 }
121
122
123 auto func = dynamic_pointer_cast<FunctionOperator<SecondOrderDerivable>>(op);
124 if (func)
125 {
126 auto name = func->getName();
127 if (name.substr(0, 5) == "phylo")
128 {
129 auto phyl = getPhyloLikelihood(TextTools::to<size_t>(name.substr(5, string::npos)));
130 shareParameters_(phyl->getParameters());
131
132 return phyl->getLikelihoodNode();
133 }
134 }
135
136 throw Exception("PhyloLikelihoodFormula::makeLikelihoodsFromOperator : Unknown operator: " + op->output());
137}
virtual void shareParameters_(const ParameterList &parameters)
The PhyloLikelihoodSet class, to manage a subset of PhyloLikelihoods from a given PhyloLikelihoodCont...
bool hasPhyloLikelihood(size_t nPhyl) override
std::shared_ptr< const PhyloLikelihoodInterface > getPhyloLikelihood(size_t nPhyl) const override
virtual bool addPhyloLikelihood(size_t nPhyl, const std::string &suff="") override
adds a PhyloLikelihood already stored in the PhyloLikelihoodContainer, iff it is an AbstractPhyloLike...
std::shared_ptr< PhyloLikelihoodContainer > getPhyloContainer() override
static ValueRef< T > create(Context &c, NodeRefVec &&deps, const Dimension< T > &dim)
Build a new CWiseExp node with the given output dimensions.
static ValueRef< T > create(Context &c, NodeRefVec &&deps, const Dimension< T > &dim)
Build a new CWiseInverse node with the given output dimensions.
static ValueRef< T > create(Context &c, NodeRefVec &&deps, const Dimension< T > &dim)
Build a new CWiseLog node with the given output dimensions.
static ValueRef< T > create(Context &c, NodeRefVec &&deps, const Dimension< T > &dim)
Build a new CWiseNegate node with the given output dimensions.
Context for dataflow node construction.
Definition: DataFlow.h:527
static std::shared_ptr< Self > create(Context &c, Args &&... args)
Build a new NumericConstant node with T(args...) value.
std::unique_ptr< ComputationTree > compTree_
void readFormula(const std::string &formula, bool inCollection=true)
ValueRef< DataLik > makeLikelihoodsFromOperator(std::shared_ptr< Operator > op)
Build the LikelihoodNode from a node of the computation Tree.
const std::string & nextToken()
bool hasMoreToken() const
std::string toString(T 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
Store a dimension for type T.