bpp-phyl3  3.0.0
NaiveSubstitutionCount.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 
10 unique_ptr<Matrix<double>> NaiveSubstitutionCount::getAllNumbersOfSubstitutions(double length, size_t type) const
11 {
12  size_t n = supportedChars_.size();
13  auto mat = make_unique<RowMatrix<double>>(n, n);
14  for (size_t i = 0; i < n; ++i)
15  {
16  for (size_t j = 0; j < n; ++j)
17  {
18  (*mat)(i, j) = (register_->getType(i, j) == type ? (weights_ ? weights_->getIndex(supportedChars_[i], supportedChars_[j]) : 1.) : 0.);
19  }
20  }
21  return mat;
22 }
23 
24 void NaiveSubstitutionCount::storeAllNumbersOfSubstitutions(double length, size_t type, Eigen::MatrixXd& mat) const
25 {
26  auto n = Eigen::Index(supportedChars_.size());
27  mat.resize(n, n);
28 
29  for (auto i = 0; i < n; ++i)
30  {
31  for (auto j = 0; j < n; ++j)
32  {
33  mat(i, j) = (register_->getType(size_t(i), size_t(j)) == type ? (weights_ ? weights_->getIndex(supportedChars_[size_t(i)], supportedChars_[size_t(j)]) : 1.) : 0.);
34  }
35  }
36 }
37 
38 
40  std::shared_ptr<const SubstitutionModelInterface> model) :
42  make_shared<TotalSubstitutionRegister>(model->getStateMap())),
43  label_(model->getNumberOfStates(), model->getNumberOfStates()),
44  supportedChars_(model->getAlphabetStates())
45 {
46  size_t n = supportedChars_.size();
47  double count = 0;
48  for (size_t i = 0; i < n; ++i)
49  {
50  for (size_t j = 0; j < n; ++j)
51  {
52  if (i == j)
53  label_(i, j) = 0;
54  else
55  label_(i, j) = ++count;
56  }
57  }
58 }
59 
61  std::shared_ptr<const StateMapInterface> stateMap) :
63  make_shared<TotalSubstitutionRegister>(stateMap)),
64  label_(stateMap->getNumberOfModelStates(), stateMap->getNumberOfModelStates()),
65  supportedChars_(stateMap->getAlphabetStates())
66 {
67  size_t n = supportedChars_.size();
68  double count = 0;
69  for (size_t i = 0; i < n; ++i)
70  {
71  for (size_t j = 0; j < n; ++j)
72  {
73  if (i == j)
74  label_(i, j) = 0;
75  else
76  label_(i, j) = ++count;
77  }
78  }
79 }
Partial implementation of the SubstitutionCount interface.
LabelSubstitutionCount(std::shared_ptr< const SubstitutionModelInterface > model)
std::unique_ptr< Matrix< double > > getAllNumbersOfSubstitutions(double length, size_t type=1) const override
Get the numbers of susbstitutions on a branch, for each initial and final states, and given the branc...
void storeAllNumbersOfSubstitutions(double length, size_t type, Eigen::MatrixXd &mat) const override
Stores the numbers of susbstitutions on a branch, for each initial and final states,...
std::size_t count(const std::string &s, const std::string &pattern)
Defines the basic types of data flow nodes.