bpp-phyl3  3.0.0
NaiveSubstitutionCount.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_MAPPING_NAIVESUBSTITUTIONCOUNT_H
6 #define BPP_PHYL_MAPPING_NAIVESUBSTITUTIONCOUNT_H
7 
9 
11 
12 namespace bpp
13 {
31 {
32 private:
33  bool allowSelf_;
34  std::vector<int> supportedChars_;
35 
36 public:
48  std::shared_ptr<const SubstitutionModelInterface> model,
49  std::shared_ptr<const SubstitutionRegisterInterface> reg,
50  bool allowSelf = false,
51  std::shared_ptr<const AlphabetIndex2> weights = nullptr) :
54  allowSelf_(allowSelf),
55  supportedChars_(model->getAlphabetStates()) {}
56 
58  std::shared_ptr<const StateMapInterface> stateMap,
59  std::shared_ptr<const SubstitutionRegisterInterface> reg,
60  bool allowSelf = false,
61  std::shared_ptr<const AlphabetIndex2> weights = nullptr) :
64  allowSelf_(allowSelf),
65  supportedChars_(stateMap->getAlphabetStates()) {}
66 
68 
69  NaiveSubstitutionCount* clone() const override
70  {
71  return new NaiveSubstitutionCount(*this);
72  }
73 
74 public:
75  double getNumberOfSubstitutions(size_t initialState, size_t finalState, double length, size_t type = 1) const override
76  {
77  if (initialState == finalState && !allowSelf_)
78  return 0;
79  else
80  {
81  int alphabetState1 = supportedChars_[initialState];
82  int alphabetState2 = supportedChars_[finalState];
83  return register_->getType(initialState, finalState) == type ? (weights_ ? weights_->getIndex(alphabetState1, alphabetState2) : 1.) : 0.;
84  }
85  }
86 
87  std::unique_ptr< Matrix<double>> getAllNumbersOfSubstitutions(double length, size_t type = 1) const override;
88 
89  void storeAllNumbersOfSubstitutions(double length, size_t type, Eigen::MatrixXd& mat) const override;
90 
91  std::vector<double> getNumberOfSubstitutionsPerType(size_t initialState, size_t finalState, double length) const override
92  {
93  std::vector<double> v(getNumberOfSubstitutionTypes());
94  for (size_t t = 1; t <= getNumberOfSubstitutionTypes(); ++t)
95  {
96  v[t - 1] = getNumberOfSubstitutions(initialState, finalState, length, t);
97  }
98  return v;
99  }
100 
101  void setSubstitutionModel(std::shared_ptr<const SubstitutionModelInterface> model) override
102  {
103  if (model)
104  supportedChars_ = model->getAlphabetStates();
105  }
106 
107 private:
109  void weightsHaveChanged() override {}
110 };
111 
121 {
122 private:
124  std::vector<int> supportedChars_;
125 
126 public:
127  LabelSubstitutionCount(std::shared_ptr<const SubstitutionModelInterface> model);
128 
129  LabelSubstitutionCount(std::shared_ptr<const StateMapInterface> statemap);
130 
132 
133  LabelSubstitutionCount* clone() const override { return new LabelSubstitutionCount(*this); }
134 
135 public:
136  double getNumberOfSubstitutions(size_t initialState, size_t finalState, double length, size_t type = 1) const override
137  {
138  return label_(initialState, finalState);
139  }
140 
141  std::unique_ptr< Matrix<double>> getAllNumbersOfSubstitutions(double length, size_t type = 1) const override
142  {
143  return std::unique_ptr< Matrix<double>>(label_.clone());
144  }
145 
146  void storeAllNumbersOfSubstitutions(double length, size_t type, Eigen::MatrixXd& mat) const override
147  {
148  auto nbStates = supportedChars_.size();
149 
150  mat.resize(Eigen::Index(nbStates), Eigen::Index(nbStates));
151 
152  for (size_t i = 0; i < nbStates; i++)
153  {
154  for (size_t j = 0; j < nbStates; j++)
155  {
156  mat(Eigen::Index(i), Eigen::Index(j)) = label_(i, j);
157  }
158  }
159  }
160 
161 
162  std::vector<double> getNumberOfSubstitutionsPerType(size_t initialState, size_t finalState, double length) const override
163  {
164  std::vector<double> v(1);
165  v[0] = label_(initialState, finalState);
166  return v;
167  }
168 
169  void setSubstitutionModel(std::shared_ptr<const SubstitutionModelInterface> model) override
170  {
171  if (model)
172  supportedChars_ = model->getAlphabetStates();
173  }
174 
175  void setSubstitutionRegister(std::shared_ptr<const SubstitutionRegisterInterface> reg) override
176  {
177  throw Exception("OneJumpSubstitutionCount::setSubstitutionRegister. This SubstitutionsCount only works with a TotalSubstitutionRegister.");
178  }
179 
180 private:
182 };
183 } // end of namespace bpp.
184 #endif // BPP_PHYL_MAPPING_NAIVESUBSTITUTIONCOUNT_H
Partial implementation of the SubstitutionCount interface.
std::shared_ptr< const SubstitutionRegisterInterface > register_
Partial implementation of the WeightedSubstitutionCount interface.
std::shared_ptr< const AlphabetIndex2 > weights_
Labelling substitution count.
void setSubstitutionRegister(std::shared_ptr< const SubstitutionRegisterInterface > reg) override
std::vector< double > getNumberOfSubstitutionsPerType(size_t initialState, size_t finalState, double length) const override
Get the numbers of susbstitutions on a branch for all types, for an initial and final states,...
void setSubstitutionModel(std::shared_ptr< const SubstitutionModelInterface > model) override
Set the substitution model associated with this count, if relevant.
double getNumberOfSubstitutions(size_t initialState, size_t finalState, double length, size_t type=1) const override
Get the number of susbstitutions on a branch, given the initial and final states, and the branch leng...
LabelSubstitutionCount(std::shared_ptr< const SubstitutionModelInterface > model)
void substitutionRegisterHasChanged() override
LabelSubstitutionCount * clone() const override
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::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...
LinearMatrix * clone() const
Naive substitution count.
NaiveSubstitutionCount * clone() const override
void setSubstitutionModel(std::shared_ptr< const SubstitutionModelInterface > model) override
Set the substitution model associated with this count, if relevant.
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 substitutionRegisterHasChanged() override
double getNumberOfSubstitutions(size_t initialState, size_t finalState, double length, size_t type=1) const override
Get the number of susbstitutions on a branch, given the initial and final states, and the branch leng...
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::vector< double > getNumberOfSubstitutionsPerType(size_t initialState, size_t finalState, double length) const override
Get the numbers of susbstitutions on a branch for all types, for an initial and final states,...
NaiveSubstitutionCount(std::shared_ptr< const StateMapInterface > stateMap, std::shared_ptr< const SubstitutionRegisterInterface > reg, bool allowSelf=false, std::shared_ptr< const AlphabetIndex2 > weights=nullptr)
NaiveSubstitutionCount(std::shared_ptr< const SubstitutionModelInterface > model, std::shared_ptr< const SubstitutionRegisterInterface > reg, bool allowSelf=false, std::shared_ptr< const AlphabetIndex2 > weights=nullptr)
Build a new simple substitution count.
virtual size_t getNumberOfSubstitutionTypes() const
Short cut function, equivalent to getSubstitutionRegister().getNumberOfSubstitutionTypes().
Defines the basic types of data flow nodes.