bpp-seq  2.2.0
DefaultNucleotideScore.cpp
Go to the documentation of this file.
1 //
2 // File: DefaultNucleotideScore.cpp
3 // Created by: Julien Dutheil
4 // Created on: Fri Jan 19 10:30 2007
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for sequences analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #include "DefaultNucleotideScore.h"
41 
42 // from the STL:
43 #include <string>
44 
45 using namespace std;
46 using namespace bpp;
47 
48 DefaultNucleotideScore::DefaultNucleotideScore(const NucleicAlphabet* alphabet) :
49  distanceMatrix_(4, 4),
50  alpha_(alphabet)
51 {
52  // Load the matrix:
53  distanceMatrix_(0, 0) = 10;
54  distanceMatrix_(0, 1) = -3;
55  distanceMatrix_(0, 2) = -1;
56  distanceMatrix_(0, 3) = -4;
57 
58  distanceMatrix_(1, 0) = -3;
59  distanceMatrix_(1, 1) = 9;
60  distanceMatrix_(1, 2) = -5;
61  distanceMatrix_(1, 3) = 0;
62 
63  distanceMatrix_(2, 0) = -1;
64  distanceMatrix_(2, 1) = -5;
65  distanceMatrix_(2, 2) = 7;
66  distanceMatrix_(2, 3) = -3;
67 
68  distanceMatrix_(3, 0) = -4;
69  distanceMatrix_(3, 1) = 0;
70  distanceMatrix_(3, 2) = -3;
71  distanceMatrix_(3, 3) = 8;
72 }
73 
74 double DefaultNucleotideScore::getIndex(int state1, int state2) const
75 throw (BadIntException)
76 {
77  if (alpha_->isGap(state1) || !alpha_->isIntInAlphabet(state1))
78  throw BadIntException(state1, "DefaultNucleotideScore::getIndex(). Invalid state1.", alpha_);
79  if (alpha_->isGap(state2) || !alpha_->isIntInAlphabet(state2))
80  throw BadIntException(state2, "DefaultNucleotideScore::getIndex(). Invalid state1.", alpha_);
81  if (!alpha_->isUnresolved(state1) && !alpha_->isUnresolved(state2))
82  return distanceMatrix_(
83  static_cast<size_t>(state1),
84  static_cast<size_t>(state2));
85  vector<int> states1 = alpha_->getAlias(state1);
86  vector<int> states2 = alpha_->getAlias(state2);
87  double score = -5;
88  double tmp_score;
89  for (size_t i = 0; i < states1.size(); i++)
90  {
91  for (size_t j = 0; j < states2.size(); j++)
92  {
93  tmp_score = getIndex(states1[i], states2[j]);
94  if (tmp_score > score)
95  score = tmp_score;
96  }
97  }
98  return score / static_cast<double>(states1.size() + states2.size() - 1);
99 }
100 
101 double DefaultNucleotideScore::getIndex(const std::string& state1, const std::string& state2) const
102 throw (BadCharException)
103 {
104  return distanceMatrix_(
105  static_cast<size_t>(alpha_->charToInt(state1)),
106  static_cast<size_t>(alpha_->charToInt(state2)));
107 }
108 
109 LinearMatrix<double>* DefaultNucleotideScore::getIndexMatrix() const
110 {
111  return new LinearMatrix<double>(distanceMatrix_);
112 }
113 
An alphabet exception thrown when trying to specify a bad char to the alphabet.
This alphabet is used to deal NumericAlphabet.
STL namespace.
LinearMatrix< double > * getIndexMatrix() const
double getIndex(int state1, int state2) const
Get the index associated to a pair of states.
LinearMatrix< double > distanceMatrix_
An alphabet exception thrown when trying to specify a bad int to the alphabet.
The abstract base class for nucleic alphabets.