-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScoreMatrix.cpp
136 lines (116 loc) · 3.29 KB
/
ScoreMatrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "ScoreMatrix.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
double ScoreMatrix::getScore(char a, char b) const {
a = tolower(a);
b = tolower(b);
// Treat spaces and dashes as gaps
if (a == ' ' || a == '-') {
a = '*';
}
if (b == '-' || b == ' ') {
b = '*';
}
if (matrix.find(a) != matrix.end()) {
auto score = matrix.at(a).find(b);
if (score != matrix.at(a).end()) {
return score->second;
}
}
// This is usually caused by frameshifts. Returning zero works because the remainder
// of the alignment after frameshift also results in mismatches and thus zeros.
// Still, frameshifts should be handled in a better way.
// cerr << "Warning: score for (" << a << "," << b << ") is not defined in the "
// "matrix. Returning 0 instead." << endl;
return 0;
}
double ScoreMatrix::getMaxScore() const {
double max = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
double score = matrix.at(columnHeaders[i]).at(columnHeaders[j]);
if (score > max) {
max = score;
}
}
}
return max;
}
bool ScoreMatrix::loadFromFile(string filename) {
size = 0;
inputStream.open(filename.c_str());
if (!inputStream) {
cerr << "error: Failed to open matrix file \"" << filename << "\"" << endl;
return false;
}
if (!readColumnHeaders()) {
return false;
}
for (int i = 0; i < size; i++) {
if (!readRow()) {
cerr << "error: Could not read matrix file" << endl;
return false;
}
}
inputStream.close();
return true;
}
bool ScoreMatrix::readRow() {
string line;
if (!getline(inputStream, line)) {
return false;
}
processLine(line);
stringstream ss(line);
char rowHeader;
ss >> rowHeader;
rowHeader = tolower(rowHeader);
for (int i = 0; i < size; i++) {
double score;
if (!(ss >> score)) {
return false;
}
matrix[rowHeader][columnHeaders[i]] = score;
}
return true;
}
bool ScoreMatrix::readColumnHeaders() {
string line;
if (!getline(inputStream, line)) {
cerr << "error: Could not read matrix file" << endl;
return false;
}
// Skip initial comments
while (line[0] == '#') {
if (!getline(inputStream, line)) {
cerr << "error: Could not read matrix file" << endl;
return false;
}
}
processLine(line);
stringstream ss(line);
char columnHeader;
while (ss >> columnHeader) {
columnHeader = tolower(columnHeader);
size++;
columnHeaders.push_back(columnHeader);
}
return true;
}
void ScoreMatrix::processLine(string & line) {
std::replace(line.begin(), line.end(), '"', ' ');
std::replace(line.begin(), line.end(), ',', ' ');
std::replace(line.begin(), line.end(), ';', ' ');
std::replace(line.begin(), line.end(), '|', ' ');
}
void ScoreMatrix::print() const {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cout << matrix.at(columnHeaders[i]).at(columnHeaders[j]) << " ";
}
cout << endl;
}
}