17 std::shared_ptr<const SubstitutionModelInterface> model,
18 std::shared_ptr<const SubstitutionRegisterInterface> reg,
19 std::shared_ptr<const AlphabetIndex2> weights,
20 std::shared_ptr<const AlphabetIndex2> distances) :
25 counts_(reg->getNumberOfSubstitutionTypes()),
29 if (
typeid(model->getAlphabet()) !=
typeid(reg->getAlphabet()))
30 throw Exception(
"DecompositionSubstitutionCount (constructor): alphabets do not match between register and model.");
40 std::shared_ptr<const SubstitutionRegisterInterface> reg,
41 std::shared_ptr<const AlphabetIndex2> weights,
42 std::shared_ptr<const AlphabetIndex2> distances) :
47 counts_(reg->getNumberOfSubstitutionTypes()),
58 for (
size_t i = 0; i <
register_->getNumberOfSubstitutionTypes(); ++i)
69 throw Exception(
"DecompositionSubstitutionCount::fillBMatrices_: model not defined.");
71 vector<int> supportedStates =
model_->getAlphabetStates();
91 throw Exception(
"DecompositionSubstitutionCount::setDistanceBMatrices_: model not defined.");
93 vector<int> supportedStates =
model_->getAlphabetStates();
114 vector<int> supportedStates =
model_->getAlphabetStates();
116 for (
size_t i = 0; i <
nbTypes_; i++)
123 if (!std::isnormal(
counts_[i](j, k)))
126 counts_[i](j, k) *=
weights_->getIndex(supportedStates[j], supportedStates[k]);
135 double length,
size_t type)
const
138 throw Exception(
"DecompositionSubstitutionCount::getAllNumbersOfSubstitutions: model not defined.");
141 throw Exception(
"DecompositionSubstitutionCount::getAllNumbersOfSubstitutions. Negative branch length: " +
TextTools::toString(length) +
".");
147 return make_unique<RowMatrix<double>>(
counts_[type - 1]);
155 throw Exception(
"DecompositionSubstitutionCount::storeAllNumbersOfSubstitutions: model not defined.");
158 throw Exception(
"DecompositionSubstitutionCount::getAllNumbersOfSubstitutions. Negative branch length: " +
TextTools::toString(length) +
".");
167 const auto& ct =
counts_[type - 1];
172 mat(Eigen::Index(i), Eigen::Index(j)) = isnan(ct(i, j)) ? 0 : ct(i, j);
182 throw Exception(
"DecompositionSubstitutionCount::getNumberOfSubstitutions: model not defined.");
185 throw Exception(
"DecompositionSubstitutionCount::getNumbersOfSubstitutions. Negative branch length: " +
TextTools::toString(length) +
".");
191 return counts_[type - 1](initialState, finalState);
199 throw Exception(
"DecompositionSubstitutionCount::getNumberOfSubstitutionsPerType: model not defined.");
202 throw Exception(
"DecompositionSubstitutionCount::getNumbersOfSubstitutions. Negative branch length: " +
TextTools::toString(length) +
".");
212 v[t] =
counts_[t](initialState, finalState);
220 std::shared_ptr<const SubstitutionModelInterface> model)
223 if (
typeid(model->getAlphabet()) !=
typeid(
register_->getAlphabet()))
224 throw Exception(
"DecompositionMethods::setSubstitutionModel: alphabets do not match between register and model.");
246 if (
model_->getAlphabet()->getAlphabetType() !=
register_->getAlphabet()->getAlphabetType())
247 throw Exception(
"DecompositionMethods::substitutionRegisterHasChanged: alphabets do not match between register and model.");
267 throw Exception(
"DecompositionSubstitutionCount::weightsHaveChanged. Incorrect alphabet type.");
280 if (
distances_->getAlphabet()->getAlphabetType() !=
register_->getAlphabet()->getAlphabetType())
281 throw Exception(
"DecompositionSubstitutionCount::distancesHaveChanged. Incorrect alphabet type.");
Partial implementation of the SubstitutionCount interface.
std::shared_ptr< const SubstitutionRegisterInterface > register_
Partial implementation of the SubstitutionDistance interface.
std::shared_ptr< const AlphabetIndex2 > distances_
Partial implementation of the WeightedSubstitutionCount interface.
std::shared_ptr< const AlphabetIndex2 > weights_
Methods useful for analytical substitution count and rewards using the eigen decomposition method.
void computeExpectations(RowMatrix< double > &mapping, double length) const
void setSubstitutionModel(std::shared_ptr< const SubstitutionModelInterface > model)
Set the substitution model.
std::shared_ptr< const SubstitutionModelInterface > model_
std::vector< RowMatrix< double > > bMatrices_
computation matrices
void distancesHaveChanged() override
void setDistanceBMatrices_()
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...
DecompositionSubstitutionCount(std::shared_ptr< const SubstitutionModelInterface > model, std::shared_ptr< const SubstitutionRegisterInterface > reg, std::shared_ptr< const AlphabetIndex2 > weights=nullptr, std::shared_ptr< const AlphabetIndex2 > distances=nullptr)
std::vector< RowMatrix< double > > counts_
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
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 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,...
void weightsHaveChanged() override
void computeCounts_(double length) const
void setSubstitutionModel(std::shared_ptr< const SubstitutionModelInterface > model) override
Set the substitution model.
virtual size_t getNumberOfSubstitutionTypes() const
Short cut function, equivalent to getSubstitutionRegister().getNumberOfSubstitutionTypes().
std::string toString(T t)
Defines the basic types of data flow nodes.