-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem.cpp
201 lines (161 loc) · 4.99 KB
/
Problem.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// Created by osmangokalp on 5/4/2020.
//
#include "Problem.h"
#include <string>
#include <iostream>
#include <fstream>
int Problem::getT() const {
return t;
}
int Problem::getN() const {
return n;
}
std::string *Problem::getSequences() const {
return sequences;
}
const std::string &Problem::getFileName() const {
return fileName;
}
Problem::~Problem() {
delete[] sequences;
}
Problem::Problem(const std::string &fileName) : fileName(fileName) {
std::ifstream file("../TRANSFAC_Dataset/Real/" + fileName);
std::string line;
//find t, the number of sequences
int counter = 0;
while (getline(file, line)) {
if (line[0] == '>') { // >seq_x line
counter++;
}
}
this->t = counter;
this->sequences = new std::string[t];
file.clear();
file.seekg(0, std::ios::beg);
counter = 0;
while (getline(file, line)) {
if (line[0] == '>') { //sequence label line
getline(file, line); //read real sequence
sequences[counter++] = line;
}
}
file.close();
//find n, the sequence length
this->n = sequences[0].size();
}
void Problem::evaluateSolution(Solution *sol, int numRow, int l) const {
double sim = -1;
char **alignmentMatrix = constructAlignmentMatrix(sol->startIndices, numRow, l);
double **profileMatrix = constructProfileMatrix(alignmentMatrix, numRow, l);
ConsensusString consensusString = constructConsensusString(profileMatrix, l);
//free memory
for (int i = 0; i < numRow; ++i) {
delete[] alignmentMatrix[i];
}
delete[] alignmentMatrix;
for (int i = 0; i < 4; ++i) {
delete[] profileMatrix[i];
}
delete[] profileMatrix;
sol->consensusString = consensusString.getSequence();
sol->similarityScore = consensusString.getSimilarity();
}
char **Problem::constructAlignmentMatrix(int *positionVector, int numRow, int l) const {
try {
if (l <= 0) {
throw std::string("Motif length must be positive.");
}
char **alignmentMatrix = new char *[numRow];
for (int i = 0; i < numRow; ++i) {
alignmentMatrix[i] = new char[l];
}
for (int i = 0; i < l; ++i) {
int rowIndex = 0;
for (int j = 0; j < this->t; ++j) {
int startIndex = positionVector[j];
if (startIndex < 0) { //negative index means that the sequence will not be used
continue;
}
if (startIndex + l > this->n) {
throw std::string("Too long motif. Start index of the motif must be <= n - l.");
}
char gene = sequences[j][startIndex + i];
alignmentMatrix[rowIndex++][i] = gene;
}
}
return alignmentMatrix;
}
catch (std::string msg) {
std::cerr << msg << std::endl;
std::exit(1);
}
}
ConsensusString Problem::constructConsensusString(double **profileMatrix, int l) const {
std::string conSeq(l, '-');
double maxTotal = 0;
for (int i = 0; i < l; ++i) {
int maxIndex = 0;
double maxVal = profileMatrix[0][i];
for (int j = 1; j < 4; ++j) {
if (profileMatrix[j][i] > maxVal) {
maxVal = profileMatrix[j][i];
maxIndex = j;
}
}
switch (maxIndex) {
case 0:
conSeq[i] = 'A';
break;
case 1:
conSeq[i] = 'T';
break;
case 2:
conSeq[i] = 'G';
break;
case 3:
conSeq[i] = 'C';
break;
}
maxTotal += maxVal;
}
double sim = maxTotal / l;
return ConsensusString(conSeq, sim);
}
double **Problem::constructProfileMatrix(char **alignmentMatrix, int numRow, int l) const {
double **profileMatrix = new double *[4]; //row order: A, T, G, C
for (int k = 0; k < 4; ++k) {
profileMatrix[k] = new double[l];
}
for (int i = 0; i < l; ++i) {
int countA = 0;
int countT = 0;
int countG = 0;
int countC = 0;
for (int j = 0; j < numRow; ++j) {
char gene = alignmentMatrix[j][i];
switch (gene) {
case 'A':
countA++;
break;
case 'T':
countT++;
break;
case 'G':
countG++;
break;
case 'C':
countC++;
break;
default:
throw std::string("Unexpected gene label: ") + gene + ". It should be A, C, G or T.";
}
}
profileMatrix[0][i] = (1.0 / numRow) * countA;
profileMatrix[1][i] = (1.0 / numRow) * countT;
profileMatrix[2][i] = (1.0 / numRow) * countG;
profileMatrix[3][i] = (1.0 / numRow) * countC;
}
return profileMatrix;
}