-
Notifications
You must be signed in to change notification settings - Fork 0
/
swalign.c
422 lines (319 loc) · 8.71 KB
/
swalign.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* Smith-Waterman alignment
*
* Wang, Qinhu
* Northwest A&F University
* wangqinhu@nwafu.edu.cn
*
* Feb 28, 2014
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <ctype.h>
#define MAXCOL 120 // max column each line
#define MAXSEQ 1000 // max sequene length
#define MAXFNL 256 // max filename length
#define MATCH 4 // match score
#define MISMATCH -3 // mismatch score
#define GAP -4 // gap score
void usage() {
printf("\nSimith-Waterman Alignment\n\n");
printf("usage:\n\n");
printf("swalign <options>\n\n");
printf(" -h help\n");
printf(" -i input sequence file 1\n");
printf(" -j input sequence file 2\n");
printf(" -a input sequence a, directly in command line\n");
printf(" -b input sequence b, directly in command line\n");
printf(" -s output the raw alignment\n\n");
printf("________________________________________________\n");
printf("Wang, Qinhu\n");
printf("wangqinhu@nwafu.edu.cn\n");
printf("https://github.com/wangqinhu/alignments\n");
exit(1);
}
/* define nucleotude */
int isnuc (char nuc) {
switch ( nuc ) {
case 'A': return 1;
case 'C': return 1;
case 'G': return 1;
case 'T': return 1;
case 'U': return 1;
case 'a': return 1;
case 'c': return 1;
case 'g': return 1;
case 't': return 1;
case 'u': return 1;
default: return 0;
}
}
/* convert seq to UPPER case */
void upper (char seq[]) {
int i;
i = 0;
while ( seq[i] != '\0' ) {
seq[i] = toupper( seq[i]);
i++;
}
}
/* read a fasta file and pass the sequence to an array */
void readseq (char filename[], char seq[]) {
FILE *file; // hold fasta file
int i; // index for seq[]
char line[ MAXCOL ]; // store each line
int j; // index for line[]
file = fopen( filename, "r" );
if ( file == NULL ) {
printf("Error: file %s does not exist!\n", filename);
exit(1);
}
i = 0;
while ( fgets( line, sizeof line, file ) ) {
/* if a fasta header line was found */
if ( line[0] == '>' ) continue;
/* now try to parse the fasta body */
for ( j = 0; j < strlen(line); j++ ) {
if ( isnuc( line[j] ) ) seq[i++] = line[j];
}
} // end of while
seq[i] = '\0';
fclose( file );
} // end readseq
/* scoring scheme */
int score (char a, char b) {
int score; // final score
if ( a == '-' || b == '-' )
score = GAP;
else
score = ( a == b ) ? MATCH : MISMATCH;
return score;
} // end score
/* max3: return the max score */
int max3 (int d, int l, int u) {
int max; // max score
max = 0;
max = ( d > max ) ? d : max;
max = ( l > max ) ? l : max;
max = ( u > max ) ? u : max;
return max;
} // end max3
/* mss: return the max score source */
char mss (int d, int l, int u) {
int max; // max score
char mss; // max score source
max = max3( d, l, u );
mss = ' ';
if (max == d) mss = '\\';
if (max == l) mss = '|';
if (max == u) mss = '_';
return mss;
} // end of mss
/* revers a string: used for the last step of back tracing */
void revseq (char seq[]) {
int i; // index for seq and revseq
int l; // seq length
l = strlen( seq );
char hold[ l ]; // hold seq
strcpy(hold, seq);
for ( i = 0; i < l; i++ ) {
seq[i] = hold[l-1-i];
}
} // end of revseq
/* get the alignment string */
void align_str (char aln1[], char aln2[], char aln[]) {
int i, l; // index and length for alignment
l = strlen( aln1 );
if ( l != strlen( aln2 ) ) {
printf("Error, seq1 and seq2 are not equal in length!\n");
exit(1);
}
// get the alignment string
for ( i = 0; i < l; i++ ) {
if ( aln1[i] == aln2[i] ) {
aln[i] = ':';
} else if ( aln1[i] == '-' || aln2[i] == '-' ) {
aln[i] = ' ';
} else {
aln[i] = '.';
}
}
aln[i] = '\0';
} // end of align_str
/* smith-waterman alignment function */
int swalign (char seq1[], char seq2[], char bts1[], char bts2[], char aln[]) {
int mat[ MAXSEQ + 1 ][ MAXSEQ + 1 ]; // score matrix
int i, j; // matrix indices
int m, n; // sequences length
char btm[ MAXSEQ + 1 ][ MAXSEQ + 1 ]; // back trace mark
int d, u, l; // source score
int me, mi, mj; // max element and its indices
int bi; // back trace index, forward
/* initialization the matrix */
m = strlen( seq1 );
n = strlen( seq2 );
for ( i = 0; i <= m; i++ ) mat[i][0] = 0;
for ( j = 0; j <= n; j++ ) mat[0][j] = 0;
/* filling the matrix */
for ( i = 1; i <= m; i ++ )
for ( j = 1; j <= n; j++ ) {
// diagonal
d = mat[i-1][j-1] + score( seq1[i-1], seq2[j-1] );
// up
u = mat[i-1][j] + score( seq1[i-1], '-' );
// left
l = mat[i][j-1] + score( '-', seq2[j-1] );
// find the max score
mat[i][j] = max3( d, u, l );
// mark the trace path
btm[i][j] = mss( d, u, l );
}
/* back trace */
/*
// print the back trace matrix
for ( i = 0; i <= m; i++ ) {
for ( j = 0; j <= n; j++ ) printf("%2d %c\t", mat[i][j], btm[i][j]);
printf("\n");
}
*/
// find max element and its indices
me = 0;
for ( i = 1; i <= m; i++ )
for ( j = 1; j <= n; j++ ) {
if ( mat[i][j] > me ) {
me = mat[i][j];
mi = i;
mj = j;
}
}
// back trace
for ( i = mi, j = mj, bi = 0 ; i >= 0; bi++ )
if ( btm[i][j] == '\\' ) {
bts1[bi] = seq1[i - 1];
bts2[bi] = seq2[j - 1];
i--;
j--;
} else if ( btm[i][j] == '_') {
bts1[bi] = '-';
bts2[bi] = seq2[j - 1];
j--;
} else if ( btm[i][j] == '|' ) {
bts1[bi] = seq1[i - 1] ;
bts2[bi] = '-';
i--;
} else {
bts1[bi] = '\0';
bts2[bi] = '\0';
revseq(bts1);
revseq(bts2);
break;
}
// get aligment string
align_str( bts1, bts2, aln );
return me;
} // end of swalign
/* format alignment output */
void print_align (char aln1[], char aln2[], char aln[]) {
int i,l; // index and length for alignment
int j, k, m, n; // vars for format control
l = strlen( aln1 );
// output alignment header
printf("1 10 20 30 40 50\n");
printf("....:....|....:....|....:....|....:....|....:....|\n\n");
// format output
m = l;
n = 0;
for ( i = 0; i < l; i = i + 50 ) {
// decide the length of the char to print
if ( m > 50 ) {
k = 50;
} else {
k = m % 50;
}
// print seqs/alignment
// seq1
for ( j = 0; j < k; j++ ) {
printf("%c", aln1[i+j]);
}
printf("\n");
// alingment
for ( j = 0; j < k; j++ ) {
printf("%c", aln[i+j]);
}
printf("\n");
// seq2
for ( j = 0; j < k; j++ ) {
printf("%c", aln2[i+j]);
}
printf("\n\n");
// recal controls
m = m - 50;
n++;
}
} // end of print_align
int main (int argc, char *argv[]) {
char seq1[ MAXSEQ ]; // input seq1
char seq2[ MAXSEQ ]; // input seq2
char bts1[ MAXSEQ ]; // back trace seq1
char bts2[ MAXSEQ ]; // back trace seq2
char aln[ MAXSEQ ]; // the one line alignment string
int swas; // smith-waterman alginment score
int c; // options
char infile1[ MAXFNL ]; // input file1
char infile2[ MAXFNL ]; // input file2
int simple; // output controls
// options
while ( ( c = getopt( argc, argv, "ha:b:i:j:s" ) ) != EOF )
switch (c) {
case 'h':
usage();
break;
case 'a':
strcpy(seq1, optarg);
break;
case 'b':
strcpy(seq2, optarg);
break;
case 'i':
strcpy(infile1, optarg);
break;
case 'j':
strcpy(infile2, optarg);
break;
case 's':
simple = 1;
break;
}
if ( argc == 1 ) {
usage();
}
// check if sequences were passed by file
if ( ( infile1[0] != '\0' ) && ( infile2[0] != '\0' ) ) {
readseq( infile1, seq1 );
readseq( infile2, seq2 );
}
// check if sequences are ready
if ( ( seq1[0] == '\0' ) || ( seq2[0] == '\0' ) ) {
printf("Error: insufficent input sequences found!\n");
usage();
}
// covert sequences to UPPER case
upper( seq1 );
upper( seq2 );
// do smith-waterman alignment
swas = swalign( seq1, seq2, bts1, bts2, aln );
// output alignment
if ( simple == 1 ) {
printf("%d\n", swas);
puts(bts1);
puts(aln);
puts(bts2);
} else {
printf("Smith-Waterman Alignment:\n\n");
printf("Score:\t%d\n\n", swas);
print_align( bts1, bts2, aln );
}
}