bpp-phyl3  3.0.0
HmmLikelihood_DF.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 #include "HmmLikelihood_DF.h"
7 
8 // from the STL:
9 #include <iostream>
10 #include <algorithm>
11 using namespace bpp;
12 using namespace std;
13 
15  Context& context,
16  std::shared_ptr<HmmStateAlphabet> hiddenAlphabet,
17  std::shared_ptr<HmmTransitionMatrix> transitionMatrix,
18  std::shared_ptr<HmmPhyloEmissionProbabilities> emissionProbabilities,
19  const std::string& prefix) :
21  context_(context),
22  hiddenAlphabet_(hiddenAlphabet),
23  emissionProbabilities_(emissionProbabilities),
24  matrix_(),
25  hmmEq_(),
26  hmmTrans_(),
27  hmmEmis_(),
28  forwardLik_(),
29  backwardLik_(),
30  hiddenPostProb_(),
31  nbStates_(),
32  nbSites_()
33 {
34  if (!hiddenAlphabet)
35  throw Exception("HmmLikelihood_DF: null pointer passed for HmmStateAlphabet.");
36  if (!transitionMatrix)
37  throw Exception("HmmLikelihood_DF: null pointer passed for HmmTransitionMatrix.");
38  if (!emissionProbabilities)
39  throw Exception("HmmLikelihood_DF: null pointer passed for HmmEmissionProbabilities.");
40 
41  nbStates_ = Eigen::Index(hiddenAlphabet_->getNumberOfStates());
42  nbSites_ = Eigen::Index(emissionProbabilities_->getNumberOfPositions());
43 
44  if (emissionProbabilities_->getNumberOfStates() != size_t(nbStates_))
45  throw BadSizeException("HmmLikelihood_DF: HmmStateAlphabet and HmmEmissionProbabilities do not have the same number of states.", emissionProbabilities_->getNumberOfStates(), size_t(nbStates_));
46 
47  if (transitionMatrix->getNumberOfStates() != size_t(nbStates_))
48  throw BadSizeException("HmmLikelihood_DF: HmmStateAlphabet and HmmTransitionMatrix do not have the same number of states.", transitionMatrix->getNumberOfStates(), size_t(nbStates_));
49 
50 
52  // Adding transition matrix
53 
54  // parameters of the transition matrix
55  const auto& param = transitionMatrix->getParameters();
56  ParameterList paramList;
57 
58  for (size_t i = 0; i < param.size(); i++)
59  {
61  }
62 
63  shareParameters_(paramList);
64 
65  // make TransitionMatrix DF
66  matrix_ = ConfiguredParametrizable::createConfigured<HmmTransitionMatrix, ConfiguredTransitionMatrix>(context_, *transitionMatrix, paramList, "");
67 
68  // for derivates
69  auto deltaNode = NumericMutable<double>::create(context_, 0.001);
71 
72  matrix_->config.delta = deltaNode;
73  matrix_->config.type = config;
74 
75  // equilibrium
76 
77  auto eqdim = VectorDimension(Eigen::Index(nbStates_));
78 
79  hmmEq_ = ConfiguredParametrizable::createVector<ConfiguredTransitionMatrix, EquilibriumFrequenciesFromTransitionMatrix, Eigen::VectorXd>(context_, {matrix_}, eqdim);
80 
81  // transition
82 
83  auto transdim = MatrixDimension(nbStates_, nbStates_);
84  hmmTrans_ = ConfiguredParametrizable::createMatrix<ConfiguredTransitionMatrix, TransitionMatrixFromTransitionMatrix, Eigen::MatrixXd>(context_, {matrix_}, transdim);
85 
86  // emission
87 
88  hmmEmis_ = emissionProbabilities_->getEmissionProbabilities();
89 
90  // Manage parameters:
91  addParameters_(hiddenAlphabet_->getParameters());
92  addParameters_(emissionProbabilities_->getParameters());
93 
94  // forward computation
96 
98 
99  // backward computation
101 
102 
103  // Then Hidden Posterior Probabilities
104 
105  auto forwardNode = dynamic_pointer_cast<ForwardHmmLikelihood_DF>(forwardLik_);
106 
108 
109  // and site likelihoods
110 
112 
114 }
115 
116 void HmmLikelihood_DF::setNamespace(const std::string& nameSpace)
117 {
119 
120  hiddenAlphabet_->setNamespace(nameSpace);
121  matrix_->setNamespace(nameSpace);
122  emissionProbabilities_->setNamespace(nameSpace);
123 }
void addParameters_(const ParameterList &parameters)
void shareParameters_(const ParameterList &parameters)
void setNamespace(const std::string &prefix) override
void setSiteLikelihoods(SiteLikelihoodsRef ll, bool shrunk=false)
static ValueRef< Eigen::MatrixXd > create(Context &c, NodeRefVec &&deps, const Dimension< Eigen::MatrixXd > &dim)
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps, const Parameter &param)
Build a new ConfiguredParameter node.
Definition: Parameter.h:36
Context for dataflow node construction.
Definition: DataFlow.h:527
static ValueRef< RowLik > create(Context &c, NodeRefVec &&deps, const Dimension< Eigen::MatrixXd > &dim)
ValueRef< Eigen::MatrixXd > hmmTrans_
void setNamespace(const std::string &nameSpace)
HmmLikelihood_DF(Context &context, std::shared_ptr< HmmStateAlphabet > hiddenAlphabet, std::shared_ptr< HmmTransitionMatrix > transitionMatrix, std::shared_ptr< HmmPhyloEmissionProbabilities > emissionProbabilities, const std::string &prefix="")
Build a new HmmLikelihood_DF object.
ValueRef< RowLik > forwardLik_
std::shared_ptr< HmmStateAlphabet > hiddenAlphabet_
The alphabet describing the hidden states.
std::shared_ptr< HmmPhyloEmissionProbabilities > emissionProbabilities_
ValueRef< MatrixLik > hmmEmis_
ValueRef< Eigen::MatrixXd > hiddenPostProb_
ValueRef< Eigen::MatrixXd > backwardLik_
std::shared_ptr< ConfiguredTransitionMatrix > matrix_
ValueRef< Eigen::VectorXd > hmmEq_
void setLikelihoodNode(ValueRef< DataLik > ll)
static std::shared_ptr< Self > create(Context &, Args &&... args)
Build a new NumericMutable node with T(args...) value.
virtual void shareParameter(const std::shared_ptr< Parameter > &param)
Defines the basic types of data flow nodes.
Basic matrix dimension type.