-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment1.c
290 lines (276 loc) · 7.41 KB
/
assignment1.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
// Cost matrix elements
struct score
{
int result;
int diagonal;
int upper;
int side;
};
// Function to calculate minimum value between three integers
int minimum(int diagonal, int upper, int side)
{
if (diagonal <= upper)
{
if (diagonal <= side)
{
return diagonal;
}
else
{
return side;
}
}
else
{
if (upper <= side)
{
return upper;
}
else
{
return side;
}
}
}
// Function to calculate minimum value between three integers, but only if selected as possible moves and non-negative
int allowed_minimum(int diagonal_cost, int diagonal, int upper_cost, int upper, int side_cost, int side)
{
if ((diagonal == 0) || (diagonal_cost < 0))
{
diagonal_cost = INT_MAX;
}
if ((upper == 0) || (upper_cost < 0))
{
upper_cost = INT_MAX;
}
if ((side == 0) || (side_cost < 0))
{
side_cost = INT_MAX;
}
if (diagonal_cost <= upper_cost)
{
if (diagonal_cost <= side_cost)
{
return diagonal_cost;
}
else
{
return side_cost;
}
}
else
{
if (upper_cost <= side_cost)
{
return upper_cost;
}
else
{
return side_cost;
}
}
}
int compute_alignment(char *X, int X_length, char *Y, int Y_length)
{
// Cost matrix
struct score cost_matrix[X_length + 1][Y_length + 1];
int gap_cost;
int mismatch_cost;
printf("Insert the gap cost: ");
scanf("%d", &gap_cost);
printf("Insert the mismatch cost: ");
scanf("%d", &mismatch_cost);
// Initialize Cost matrix
for (int i = 0; i < X_length + 1; i++)
{
for (int j = 0; j < Y_length + 1; j++)
{
if (i == 0)
{
cost_matrix[i][j].result = j * gap_cost;
cost_matrix[i][j].diagonal = 0;
cost_matrix[i][j].upper = 0;
cost_matrix[i][j].side = 1;
}
else if (j == 0)
{
cost_matrix[i][j].result = i * gap_cost;
cost_matrix[i][j].diagonal = 0;
cost_matrix[i][j].upper = 1;
cost_matrix[i][j].side = 0;
}
else
{
cost_matrix[i][j].result = 0;
cost_matrix[i][j].diagonal = 0;
cost_matrix[i][j].upper = 0;
cost_matrix[i][j].side = 0;
}
}
}
// Calculate Cost matrix
int diagonal_cost = 0;
int upper_cost = 0;
int side_cost = 0;
int is_minimum = 0;
for (int i = 1; i < X_length + 1; i++)
{
for (int j = 1; j < Y_length + 1; j++)
{
if (X[i - 1] != Y[j - 1])
{
diagonal_cost = cost_matrix[i - 1][j - 1].result + (mismatch_cost * 2);
}
else
{
diagonal_cost = cost_matrix[i - 1][j - 1].result;
}
upper_cost = cost_matrix[i - 1][j].result + gap_cost;
side_cost = cost_matrix[i][j - 1].result + gap_cost;
is_minimum = minimum(diagonal_cost, upper_cost, side_cost);
cost_matrix[i][j].result = is_minimum;
if (is_minimum == diagonal_cost)
{
cost_matrix[i][j].diagonal = 1;
}
if (is_minimum == upper_cost)
{
cost_matrix[i][j].upper = 1;
}
if (is_minimum == side_cost)
{
cost_matrix[i][j].side = 1;
}
}
}
// Print Cost matrix
printf("+\t_\t");
for (int i = 0; i < Y_length; i++)
{
printf("%c\t", Y[i]);
if (i == Y_length - 1)
{
printf("\n");
}
}
for (int i = 0; i < X_length + 1; i++)
{
if (i == 0)
{
printf("_\t");
}
else
{
printf("%c\t", X[i - 1]);
}
for (int j = 0; j < Y_length + 1; j++)
{
printf("%d\t", cost_matrix[i][j].result);
if (j == Y_length)
{
printf("\n");
}
}
}
// Calculate minimum cost
int minimum_cost = 0;
int x_position = X_length;
int y_position = Y_length;
int partial_cost = cost_matrix[X_length][Y_length].result;
int move_cost = 0;
int diagonal = 0;
int upper = 0;
int side = 0;
char aligned_X[100] = {'\0'};
char aligned_Y[100] = {'\0'};
int position = 0;
while ((partial_cost != 0))
{
diagonal_cost = cost_matrix[x_position - 1][y_position - 1].result;
upper_cost = cost_matrix[x_position - 1][y_position].result;
side_cost = cost_matrix[x_position][y_position - 1].result;
diagonal = cost_matrix[x_position][y_position].diagonal;
upper = cost_matrix[x_position][y_position].upper;
side = cost_matrix[x_position][y_position].side;
move_cost = allowed_minimum(diagonal_cost, diagonal, upper_cost, upper, side_cost, side);
if ((move_cost == diagonal_cost) && (diagonal == 1))
{
x_position--;
y_position--;
if ((partial_cost - move_cost) == (mismatch_cost * 2))
{
// Mismatch
aligned_X[position] = '*';
aligned_Y[position] = '*';
}
else
{
// Match
aligned_X[position] = X[x_position];
aligned_Y[position] = Y[y_position];
}
}
else if ((move_cost == upper_cost) && (upper == 1))
{
// Gap in Y
x_position--;
aligned_X[position] = X[x_position];
aligned_Y[position] = '_';
}
else
{
// Gap in X
y_position--;
aligned_X[position] = '_';
aligned_Y[position] = Y[y_position];
}
minimum_cost += (partial_cost - move_cost);
partial_cost = move_cost;
position++;
}
while ((x_position) && (y_position) && (partial_cost == 0))
{
x_position--;
y_position--;
aligned_X[position] = X[x_position];
aligned_Y[position] = Y[y_position];
position++;
}
// Reverse aligned strings
int len_X = strlen(aligned_X);
int len_Y = strlen(aligned_Y);
for (int i = 0; i < len_X / 2; i++)
{
char temp = aligned_X[i];
aligned_X[i] = aligned_X[len_X - 1 - i];
aligned_X[len_X - 1 - i] = temp;
}
for (int i = 0; i < len_Y / 2; i++)
{
char temp = aligned_Y[i];
aligned_Y[i] = aligned_Y[len_Y - 1 - i];
aligned_Y[len_Y - 1 - i] = temp;
}
printf("The aligned strings are:\nX -->\t%s\nY -->\t%s\n", aligned_X, aligned_Y);
return minimum_cost;
}
int main()
{
char X[] = "GATTACA";
char Y[] = "GTCGACGCA";
printf("Assignment 1\nThe predefined strings are:\nX -->\t%s\nY -->\t%s\nTo change them, modify the code.\n", X, Y);
int minimum_cost = 0;
if (strlen(X) < strlen(Y))
{
minimum_cost = compute_alignment(X, strlen(X), Y, strlen(Y));
}
else
{
minimum_cost = compute_alignment(Y, strlen(Y), X, strlen(X));
}
printf("Cost: %d\n", minimum_cost);
}