bpp-seq3  3.0.0
DefaultNucleotideScore.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 // from the STL:
8 #include <string>
9 
10 using namespace std;
11 using namespace bpp;
12 
13 DefaultNucleotideScore::DefaultNucleotideScore(const NucleicAlphabet* alphabet) :
14  distanceMatrix_(4, 4),
15  alpha_(alphabet)
16 {
17  // Load the matrix:
18  distanceMatrix_(0, 0) = 10;
19  distanceMatrix_(0, 1) = -3;
20  distanceMatrix_(0, 2) = -1;
21  distanceMatrix_(0, 3) = -4;
22 
23  distanceMatrix_(1, 0) = -3;
24  distanceMatrix_(1, 1) = 9;
25  distanceMatrix_(1, 2) = -5;
26  distanceMatrix_(1, 3) = 0;
27 
28  distanceMatrix_(2, 0) = -1;
29  distanceMatrix_(2, 1) = -5;
30  distanceMatrix_(2, 2) = 7;
31  distanceMatrix_(2, 3) = -3;
32 
33  distanceMatrix_(3, 0) = -4;
34  distanceMatrix_(3, 1) = 0;
35  distanceMatrix_(3, 2) = -3;
36  distanceMatrix_(3, 3) = 8;
37 }
38 
39 double DefaultNucleotideScore::getIndex(int state1, int state2) const
40 {
41  if (!alpha_->isUnresolved(state1) && !alpha_->isUnresolved(state2))
42  return distanceMatrix_(
43  alpha_->getStateIndex(state1) - 1,
44  alpha_->getStateIndex(state2) - 1);
45 
46  vector<int> states1 = alpha_->getAlias(state1);
47  vector<int> states2 = alpha_->getAlias(state2);
48  double score = -5;
49  double tmp_score;
50  for (size_t i = 0; i < states1.size(); i++)
51  {
52  for (size_t j = 0; j < states2.size(); j++)
53  {
54  tmp_score = getIndex(states1[i], states2[j]);
55  if (tmp_score > score)
56  score = tmp_score;
57  }
58  }
59  return score / static_cast<double>(states1.size() + states2.size() - 1);
60 }
61 
62 double DefaultNucleotideScore::getIndex(const std::string& state1, const std::string& state2) const
63 {
64  return distanceMatrix_(
65  alpha_->getStateIndex(state1) - 1,
66  alpha_->getStateIndex(state2) - 1);
67 }
68 
70 {
71  return distanceMatrix_;
72 }
double getIndex(int state1, int state2) const override
Get the index associated to a pair of states.
LinearMatrix< double > distanceMatrix_
std::shared_ptr< const NucleicAlphabet > alpha_
const Matrix< double > & getIndexMatrix() const override
The abstract base class for nucleic alphabets.
This alphabet is used to deal NumericAlphabet.