1 /**
2 * UGENE - Integrated Bioinformatics Tools.
3 * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4 * http://ugene.net
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #ifndef _U2_SUBST_MATRIX_H
23 #define _U2_SUBST_MATRIX_H
24
25 #include <QVarLengthArray>
26
27 #include <U2Core/DNAAlphabet.h>
28 #include <U2Core/global.h>
29
30 namespace U2 {
31
32 class U2CORE_EXPORT SScore {
33 public:
34 char c1;
35 char c2;
36 float score;
SScore(char _c1,char _c2,float _score)37 SScore(char _c1, char _c2, float _score)
38 : c1(_c1), c2(_c2), score(_score) {
39 }
40 };
41
42 // Substitution, Scoring or Weight matrix model.
43 // Example: Blosum70, PAM200, VTML200
44 class U2CORE_EXPORT SMatrix {
45 public:
46 SMatrix(const QString &name, const DNAAlphabet *alphabet, const QList<SScore> &rawMatrix, const QString &description = QString());
47
48 // constructs empty anonymous matrix
SMatrix()49 SMatrix() {
50 alphabet = nullptr, minChar = 0;
51 maxChar = 0;
52 charsInRow = 0;
53 }
54
isEmpty()55 bool isEmpty() const {
56 return scores.size() == 0;
57 }
58
59 float getScore(char c1, char c2) const;
60
61 void setScore(char c1, char c2, float score);
62
getName()63 const QString &getName() const {
64 return name;
65 }
66
getDescription()67 const QString &getDescription() const {
68 return description;
69 }
70
getAlphabet()71 const DNAAlphabet *getAlphabet() const {
72 return alphabet;
73 }
74
getMinScore()75 float getMinScore() const {
76 return minScore;
77 }
78
getMaxScore()79 float getMaxScore() const {
80 return maxScore;
81 }
82
83 // TODO: make this class serializable
84 QVariant toQVariant() const;
85
86 static SMatrix fromQVariant(const QVariant &v);
87
88 private:
89 int getScoreIdx(char c1, char c2) const;
90 void copyCharValues(char src, char dst);
91
92 QString name;
93 QString description;
94
95 const DNAAlphabet *alphabet;
96 QVarLengthArray<float> scores; // TODO: make scores integer ?
97 char minChar; // used for optimization of scores size. Minimal character in the alphabet.
98 char maxChar; // used for optimization of scores size. Maximum character in the alphabet.
99 int charsInRow;
100 float minScore;
101 float maxScore;
102 QByteArray validCharacters; // used only for debugging now. Use array but not Set since number of characters is low
103 };
104
getScore(char c1,char c2)105 inline float SMatrix::getScore(char c1, char c2) const {
106 int idx = getScoreIdx(c1, c2);
107 return scores[idx];
108 }
109
setScore(char c1,char c2,float score)110 inline void SMatrix::setScore(char c1, char c2, float score) {
111 int idx = getScoreIdx(c1, c2);
112 scores[idx] = score;
113 }
114
getScoreIdx(char c1,char c2)115 inline int SMatrix::getScoreIdx(char c1, char c2) const {
116 assert(validCharacters.contains(c1));
117 assert(validCharacters.contains(c2));
118 int d1 = c1 - minChar;
119 int d2 = c2 - minChar;
120 assert(d1 < charsInRow && d2 < charsInRow && d1 >= 0 && d2 >= 0);
121 return d1 * charsInRow + d2;
122 }
123
124 } // namespace U2
125
126 #endif
127