-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHirschberg.cpp
More file actions
358 lines (296 loc) · 9.08 KB
/
Hirschberg.cpp
File metadata and controls
358 lines (296 loc) · 9.08 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Hirschberg Algorithm for Sequence Alignment
* Author: Francesco Borando
* Date: May 2022
* University of Milan, Department of Physics
*
* This C++ code implements the Hirschberg algorithm for global sequence alignment.
* The algorithm is an improvement over the Needleman-Wunsch algorithm in terms of space complexity.
* It uses a divide-and-conquer strategy to achieve linear space complexity, making it more memory-efficient.
* The Hirschberg algorithm is particularly suitable for aligning long sequences.
*
* References:
* - Hirschberg, D. S. (1975). A linear space algorithm for computing maximal common subsequences.
* Communications of the ACM, 18(6), 341–343.
*
* Usage:
* - Compile and run the code, providing input sequences as argv[1] and argv[2].
* - Adjust parameter scores as desired.
* - The output will include the aligned sequences.
*
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <cmath>
#define GAP_PENALTY -1
#define MISMATCH_SCORE -1
#define MATCH_SCORE 1
//Useful tools
int max3(int a, int b, int c);
int match_or_mismatch(char c1, char c2);
void printmatrix(int n, int m, int* M);
int score(char c1, char c2);
//overload pair sum
std::pair<std::string, std::string> operator+(std::pair<std::string, std::string> const& one, std::pair<std::string, std::string> const& two);
//sum_vectors: sum vector function
std::vector <int> sum_vectors(const std::vector<int>& v1, const std::vector<int>& v2);
//NWScore: return last line of score matrix
std::vector<int> NWScore(const std::string& X, const std::string& Y);
//NeedlemanWunsch: returns the alignment pair with standard algorithm
std::pair < std::string, std::string > NeedlemanWunsch(const std::string& X, const std::string& Y);
//argmax_element: returns position of max element in the vector argument
int argmax_element(const std::vector<int> score);
//Hirschberg: main algorithm; returns alignments-pair space-efficiently
std::pair< std::string, std::string > Hirschberg(const std::string& X, const std::string& Y);
int main(int argc, char* argv[])
{
if(!argv[1] || !argv[2])
{
std::cerr << "Please, insert sequences to confront:" << std::endl
<<"• Sequence1 as argv[1]" << std::endl
<<"• Sequence1 as argv[2]" << std::endl;
std::exit(EXIT_FAILURE);
}
const std::string s1 = argv[1], s2 = argv[2];
const int n = s1.length(), m = s2.length();
std::pair<std::string, std::string> ZWpair = Hirschberg(s1,s2);
std::cout << ZWpair.first << std::endl << ZWpair.second << std::endl;
return 0;
}
//Functions
//Return maximum of three integers
int max3(int a, int b, int c)
{
if (a >= b && a >= c) return a;
else if (b >= a && b >= c) return b;
else return c;
}
//Evaluate if diagonal outcome of Needleman-Wunsch
int match_or_mismatch(char c1, char c2)
{
return (c1 == c2) ? MATCH_SCORE : MISMATCH_SCORE;
}
std::vector<int> NWScore(const std::string& X, const std::string& Y)
{
const int n = X.length();
const int m = Y.length();
int Score[n+1][m+1];
std::vector<int> Lastline;
//Step 1: start from zero
Score[0][0]=0;
//Step 1.1: first row penalties
for (int j=1;j<=m;j++)
{
Score[0][j] = Score[0][j-1] + GAP_PENALTY;
}
for (int i=1; i<=n;i++)
{
Score[1][0] = Score[0][0] + GAP_PENALTY;
for (int j=1; j<=m;j++)
{
Score[1][j] = max3(
Score[1][j-1] + GAP_PENALTY,
Score[0][j] + GAP_PENALTY,
Score[0][j-1] + match_or_mismatch(X[i-1],Y[j-1])
);
}
//useless, da portare sotto!
for (int j=0;j<=m;j++)
{
Score[0][j] = Score[1][j];
}
}
for (int j=0;j<=m;j++)
{
Lastline.push_back( Score[1][j] );
}
return Lastline;
}
std::pair < std::string, std::string > NeedlemanWunsch (const std::string& X, const std::string& Y)
{
const int n = X.length(), m = Y.length();
int M[n+1][m+1];
//STEP 1: assign first row and column
M[0][0] = 0;
for (int i=1;i<n+1;i++)
{
M[i][0] = M[i-1][0] + GAP_PENALTY;
}
for (int i=1;i<m+1;i++)
{
M[0][i] = M[0][i-1] + GAP_PENALTY;
}
//STEP 2: Needelman-Wunsch
for (int i=1;i<n+1;i++)
{
for (int j=1;j<m+1;j++)
{
M[i][j] = max3(M[i-1][j-1] + match_or_mismatch(X[i-1], Y[j-1]),
M[i][j-1] + GAP_PENALTY,
M[i-1][j] + GAP_PENALTY);
}
}
//STEP 3: Reconstruct alignment
std::string A_1 = "";
std::string A_2 = "";
int i = n, j = m;
while (i>0 || j>0)
{
if (i>0
&& j>0
&& (M[i][j] == M[i-1][j-1] + match_or_mismatch(X[i-1], Y[j-1])))
{
A_1 = X[i-1] + A_1;
A_2 = Y[j-1] + A_2;
i--;
j--;
}
else if (i>0
&& (M[i][j] == M[i-1][j] + GAP_PENALTY))
{
A_1 = X[i-1] + A_1;
A_2 = '-' + A_2;
i--;
}
else
{
A_1 = '-' + A_1;
A_2 = Y[j-1] + A_2;
j--;
}
}
std::pair < std::string, std::string > alignment_pair;
alignment_pair.first = A_1;
alignment_pair.second = A_2;
return alignment_pair;
}
std::vector<int> sum_vectors(const std::vector<int>& v1, const std::vector<int>& v2)
{
std::vector<int> vector_sum;
if(v1.size() != v2.size())
{
std::cerr << "In vector sum: vector dimensions are not equal!" << std::endl;
std::exit(EXIT_FAILURE);
}
for (int i=0; i<v1.size();i++)
{
vector_sum.push_back(v1[i] + v2[i]);
}
return vector_sum;
}
int argmax_element(const std::vector<int> score)
{
int max = score[0];
int max_index=0;
for (int i=1; i<score.size();i++)
{
if(max < score[i])
{
max = score[i];
max_index = i;
}
}
return max_index;
}
//overload pair sum
std::pair<std::string, std::string> operator+(std::pair<std::string, std::string> const& one, std::pair<std::string, std::string> const& two)
{
std::pair<std::string, std::string> pair_sum;
pair_sum.first = one.first + two.first;
pair_sum.second = one.second + two.second;
return pair_sum;
}
std::pair< std::string, std::string > Hirschberg(const std::string& X, const std::string& Y)
{
const int n = X.length();
const int m = Y.length();
std::pair< std::string, std::string > ZWpair;
if (n==0)
{
for (int i=1; i<=m; i++)
{
ZWpair.first += '-';
ZWpair.second += Y[i-1];
}
}
else if (m==0)
{
for (int i=1; i<=n; i++)
{
ZWpair.first += X[i-1];
ZWpair.second += '-';
}
}
else if (n==1 || m ==1)
{
ZWpair = NeedlemanWunsch(X,Y);
}
else
{
const int xmid = n/2; //defect truncation (.5 -> .0)
std::string X_to_xmid="",
X_from_xmid="",
X_from_xmid_rev="",
Y_to_ymid="",
Y_from_ymid="",
Y_rev="";
//generate x[1...xmid]
for (int i=0;i<xmid;i++)
{
X_to_xmid += X[i];
}
//reverse X[xmid+1 ... n] and get normal
for (int i=0;i<(n-xmid);i++)
{
X_from_xmid_rev += X[n-1-i];
X_from_xmid += X[xmid+i];
}
//reverse Y
for (int i=1;i<=m;i++)
{
Y_rev += Y[m-i];
}
std::vector<int> scoreL = NWScore(X_to_xmid,Y);
std::vector<int> scoreR = NWScore(X_from_xmid_rev,Y_rev);
std::vector<int> scoreR_rev;
//DEBUG
#ifdef DEBUG
std::cout << "ScoreL : ";
for (int i=0; i<scoreL.size();i++)
{
std::cout << scoreL[i] << "\t";
}
std::cout << std::endl;
//DEBUG
std::cout << "ScoreR : ";
for (int i=0; i<scoreR.size();i++)
{
std::cout << scoreR[i] << "\t";
}
std::cout << std::endl;
#endif //DEBUG
//reverse ScoreR
for (int i=1;i<=scoreR.size();i++)
{
scoreR_rev.push_back(scoreR[scoreR.size()-i]);
}
const int ymid = argmax_element(sum_vectors(scoreL, scoreR_rev));
//DEBUG
#ifdef DEBUG
std::cout << "ymid : " << ymid << std::endl;
#endif //DEBUG
//generate Y[1...ymid]
for (int i=0;i<ymid;i++)
{
Y_to_ymid += Y[i];
}
//reverse X[xmid+1 ... n]
for (int i=ymid;i<m;i++)
{
Y_from_ymid += Y[i];
}
ZWpair = Hirschberg(X_to_xmid, Y_to_ymid) + Hirschberg(X_from_xmid, Y_from_ymid);
}
return ZWpair;
}