PandA-2024.02
nw.c
Go to the documentation of this file.
1 #include "nw.h"
2 
3 #define MATCH_SCORE 1
4 #define MISMATCH_SCORE -1
5 #define GAP_SCORE -1
6 
7 #define ALIGN '\\'
8 #define SKIPA '^'
9 #define SKIPB '<'
10 
11 #define MAX(A,B) ( ((A)>(B))?(A):(B) )
12 
13 void needwun(char SEQA[ALEN], char SEQB[BLEN],
14  char alignedA[ALEN+BLEN], char alignedB[ALEN+BLEN],
15  int M[(ALEN+1)*(BLEN+1)], char ptr[(ALEN+1)*(BLEN+1)]){
16 
17  int score, up_left, up, left, max;
18  int row, row_up, r;
19  int a_idx, b_idx;
20  int a_str_idx, b_str_idx;
21 
22  init_row: for(a_idx=0; a_idx<(ALEN+1); a_idx++){
23  M[a_idx] = a_idx * GAP_SCORE;
24  }
25  init_col: for(b_idx=0; b_idx<(BLEN+1); b_idx++){
26  M[b_idx*(ALEN+1)] = b_idx * GAP_SCORE;
27  }
28 
29  // Matrix filling loop
30  fill_out: for(b_idx=1; b_idx<(BLEN+1); b_idx++){
31  fill_in: for(a_idx=1; a_idx<(ALEN+1); a_idx++){
32  if(SEQA[a_idx-1] == SEQB[b_idx-1]){
33  score = MATCH_SCORE;
34  } else {
35  score = MISMATCH_SCORE;
36  }
37 
38  row_up = (b_idx-1)*(ALEN+1);
39  row = (b_idx)*(ALEN+1);
40 
41  up_left = M[row_up + (a_idx-1)] + score;
42  up = M[row_up + (a_idx )] + GAP_SCORE;
43  left = M[row + (a_idx-1)] + GAP_SCORE;
44 
45  max = MAX(up_left, MAX(up, left));
46 
47  M[row + a_idx] = max;
48  if(max == left){
49  ptr[row + a_idx] = SKIPB;
50  } else if(max == up){
51  ptr[row + a_idx] = SKIPA;
52  } else{
53  ptr[row + a_idx] = ALIGN;
54  }
55  }
56  }
57 
58  // TraceBack (n.b. aligned sequences are backwards to avoid string appending)
59  a_idx = ALEN;
60  b_idx = BLEN;
61  a_str_idx = 0;
62  b_str_idx = 0;
63 
64  trace: while(a_idx>0 || b_idx>0) {
65  r = b_idx*(ALEN+1);
66  if (ptr[r + a_idx] == ALIGN){
67  alignedA[a_str_idx++] = SEQA[a_idx-1];
68  alignedB[b_str_idx++] = SEQB[b_idx-1];
69  a_idx--;
70  b_idx--;
71  }
72  else if (ptr[r + a_idx] == SKIPB){
73  alignedA[a_str_idx++] = SEQA[a_idx-1];
74  alignedB[b_str_idx++] = '-';
75  a_idx--;
76  }
77  else{ // SKIPA
78  alignedA[a_str_idx++] = '-';
79  alignedB[b_str_idx++] = SEQB[b_idx-1];
80  b_idx--;
81  }
82  }
83 
84  // Pad the result
85  pad_a: for( ; a_str_idx<ALEN+BLEN; a_str_idx++ ) {
86  alignedA[a_str_idx] = '_';
87  }
88  pad_b: for( ; b_str_idx<ALEN+BLEN; b_str_idx++ ) {
89  alignedB[b_str_idx] = '_';
90  }
91 }
int score
#define GAP_SCORE
Definition: nw.c:5
void needwun(char SEQA[ALEN], char SEQB[BLEN], char alignedA[ALEN+BLEN], char alignedB[ALEN+BLEN], int M[(ALEN+1) *(BLEN+1)], char ptr[(ALEN+1) *(BLEN+1)])
Definition: nw.c:13
#define BLEN
Definition: nw.h:6
#define MISMATCH_SCORE
Definition: nw.c:4
#define max
Definition: backprop.h:17
#define ALEN
Definition: nw.h:5
#define MAX(A, B)
Definition: nw.c:11
#define M
Definition: gsm.c:30
#define SKIPA
Definition: nw.c:8
#define MATCH_SCORE
Definition: nw.c:3
#define SKIPB
Definition: nw.c:9
#define ALIGN
Definition: nw.c:7

Generated on Mon Feb 12 2024 13:02:50 for PandA-2024.02 by doxygen 1.8.13