bpp-phyl3  3.0.0
SingleProcessPhyloLikelihood.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 
7 using namespace bpp;
8 using namespace std;
9 using namespace numeric;
10 
11 
13 {
14  auto rates = getLikelihoodCalculationSingleProcess()->substitutionProcess().getRateDistribution();
15 
16  if (!rates || rates->getNumberOfCategories() == 1)
17  return Vdouble(1, 1);
18  else
19  {
20  auto probas = rates->getProbabilities();
21  std::vector<DataLik> vv(rates->getNumberOfCategories());
22  for (size_t i = 0; i < vv.size(); i++)
23  {
24  vv[i] = probas[i] * (getLikelihoodCalculationSingleProcess()->getSiteLikelihoodsForAClass(i))(Eigen::Index(pos));
25  }
26 
27  auto sv = VectorTools::sum(vv);
28  Vdouble res(rates->getNumberOfCategories());
29 
30  for (size_t i = 0; i < vv.size(); i++)
31  {
32  res[i] = convert(vv[i] / sv);
33  }
34  return res;
35  }
36 }
37 
39 {
40  auto rates = getLikelihoodCalculationSingleProcess()->substitutionProcess().getRateDistribution();
41 
42  auto nbS = getLikelihoodCalculationSingleProcess()->getNumberOfSites();
43  VVdouble vv(nbS);
44 
45  if (!rates || rates->getNumberOfCategories() == 1)
46  {
47  for (auto& v:vv)
48  {
49  v.resize(1, 1);
50  }
51  }
52  else
53  {
54  Eigen::VectorXd probas;
55  copyBppToEigen(rates->getProbabilities(), probas);
56 
57  auto vvLik = getLikelihoodCalculationSingleProcess()->getSiteLikelihoodsForAllClasses();
58  for (size_t i = 0; i < nbS; i++)
59  {
60  vv[i].resize(size_t(vvLik.rows()));
61  VectorLik sv(numeric::cwise(vvLik.col(Eigen::Index(i))) * probas.array());
62  sv /= sv.sum();
63  copyEigenToBpp(sv, vv[i]);
64  }
65  }
66 
67  return vv;
68 }
69 
70 /******************************************************************************/
71 
73 {
74  VVDataLik vd;
75  auto eg = getLikelihoodCalculationSingleProcess()->getSiteLikelihoodsForAllClasses();
76  copyEigenToBpp(eg.transpose(), vd);
77  return vd;
78 }
79 
80 /******************************************************************************/
81 
83 {
84  size_t nbSites = getNumberOfSites();
85  auto l = getLikelihoodPerSitePerClass();
86  vector<size_t> classes(nbSites);
87  for (size_t i = 0; i < nbSites; ++i)
88  {
89  classes[i] = VectorTools::whichMax<DataLik>(l[i]);
90  }
91  return classes;
92 }
93 
94 /******************************************************************************/
95 
97 {
98  auto probas = getLikelihoodCalculationSingleProcess()->substitutionProcess().getRateDistribution()->getProbabilities();
99  auto rates = getLikelihoodCalculationSingleProcess()->substitutionProcess().getRateDistribution()->getCategories();
100 
101  size_t nbSites = getNumberOfSites();
102  size_t nbClasses = getNumberOfClasses();
103  auto pb = getLikelihoodPerSitePerClass();
104  auto l = getLikelihoodPerSite();
105  Vdouble prates(nbSites, 0.);
106  for (size_t i = 0; i < nbSites; i++)
107  {
108  for (size_t j = 0; j < nbClasses; j++)
109  {
110  prates[i] += convert((pb[i][j] / l[i]) * probas[j] * rates[j]);
111  }
112  }
113  return prates;
114 }
115 
116 
117 /******************************************************************************/
118 
120 {
121  auto vv = getLikelihoodCalculationSingleProcess()->getLikelihoodsAtNode(nodeId)->targetValue();
122 
123  size_t nbSites = getNumberOfSites();
124  VVdouble pp;
125  pp.resize(nbSites);
126 
127  for (uint i = 0; i < (uint)nbSites; i++)
128  {
129  copyEigenToBpp(vv.col(i) / vv.col(i).sum(), pp[size_t(i)]);
130  }
131 
132  Vdouble v(nbStates_);
133  for (uint st = 0; st < (uint)nbStates_; st++)
134  {
135  auto s = 0.0;
136  for (size_t i = 0; i < (size_t)nbSites; i++)
137  {
138  s += pp[i][size_t(st)];
139  }
140 
141  v[size_t(st)] = s / (double)nbSites;
142  }
143  return v;
144 }
const ExtendedFloat & sum() const
VVdouble getPosteriorProbabilitiesPerSitePerClass() const
Get the posterior probabilities of each class, for each site.
Vdouble getPosteriorProbabilitiesForSitePerClass(size_t pos) const
std::vector< size_t > getClassWithMaxPostProbPerSite() const
static T sum(const std::vector< T > &v1)
T & cwise(T &t)
Definition: DataFlowCWise.h:29
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
template void copyEigenToBpp(const ExtendedFloatMatrixXd &eigenMatrix, std::vector< std::vector< double >> &bppMatrix)
template void copyBppToEigen(const std::vector< ExtendedFloat > &bppVector, Eigen::RowVectorXd &eigenVector)
std::vector< VDataLik > VVDataLik
Definition: Definitions.h:24
double convert(const bpp::ExtendedFloat &ef)
std::vector< Vdouble > VVdouble