-
Notifications
You must be signed in to change notification settings - Fork 0
/
scorematrix.cpp
82 lines (65 loc) · 1.77 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
#include "scorematrix.h"
#include "codeTable.h"
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
ScoreMatrix::ScoreMatrix(const string & filename, int nbrCols) {
ifstream file;
file.open(filename);
this->nbrCols = nbrCols; //default number of column of a BLOSUM matrix is 27
matrix.assign(nbrCols * nbrCols, -10000); //fill the matrix with -10000
string line;
CodeTable coder = CodeTable();
if (file.is_open()) {
int value;
string residues = "";
unsigned int y = 0;
while (getline(file, line)) {
if (line[0] == '#') { //ignore lines beginning with #
continue;
}
else if (line[0] == ' ') {
//loading residues in the order of the score matrix file
stringstream converter;
converter << &line[1];
char residue;
while (converter >> residue) { //get all possible residues in the matrix : AGTFCBNDSKE...
residues += residue;
}
continue;
}
unsigned int x = 0;
stringstream converter;
converter << &line[1];
while (converter >> value){
if (x < residues.size() && y < residues.size()) {
int residue_x = coder.encode(residues[x]);
int residue_y = coder.encode(residues[y]);
this->operator()(residue_x, residue_y) = (int) value;
x++;
}
}
++y;
}
}
else {
cout << "Unable to load score matrix file : " << filename << endl;
throw string("Unable to load score matrix file");
}
}
int & ScoreMatrix::operator()(char i, char j) {
//return element (i,j) of the matrix
return matrix[nbrCols * i + j];
}
void ScoreMatrix::print() {
string s;
for (unsigned int i = 0; i< matrix.size(); i++) {
if (i % nbrCols == 0) {
cout << endl;
}
cout << matrix[i] << "|";
}
//cout << "matrix : " << s << "\n";
cout << "\n";
}