Mac binaries
[jabaws.git] / website / archive / binaries / mac / src / mafft / core / Lalign11.c_nostatic
1 #include "mltaln.h"
2
3 #define DEBUG 0
4 #define DEBUG2 0
5 #define XXXXXXX    0
6 #define USE_PENALTY_EX  1
7
8
9 #if 1
10 static void match_calc( float *match, char **s1, char **s2, int i1, int lgth2 ) 
11 {
12         char *seq2 = s2[0];
13         int *intptr;
14
15         intptr = amino_dis[(int)s1[0][i1]];
16         while( lgth2-- )
17                 *match++ = intptr[(int)*seq2++];
18 }
19 #else
20 static void match_calc( float *match, char **s1, char **s2, int i1, int lgth2 )
21 {
22         int j;
23
24         for( j=0; j<lgth2; j++ )
25                 match[j] = amino_dis[(*s1)[i1]][(*s2)[j]];
26 }
27 #endif
28
29 #if 0
30 static void match_calc_bk( float *match, float **cpmx1, float **cpmx2, int i1, int lgth2, float **floatwork, int **intwork, int initialize )
31 {
32         int j, k, l;
33         float scarr[26];
34         float **cpmxpd = floatwork;
35         int **cpmxpdn = intwork;
36         int count = 0;
37
38         if( initialize )
39         {
40                 for( j=0; j<lgth2; j++ )
41                 {
42                         count = 0;
43                         for( l=0; l<26; l++ )
44                         {
45                                 if( cpmx2[l][j] )
46                                 {
47                                         cpmxpd[count][j] = cpmx2[l][j];
48                                         cpmxpdn[count][j] = l;
49                                         count++;
50                                 }
51                         }
52                         cpmxpdn[count][j] = -1;
53                 }
54         }
55
56         for( l=0; l<26; l++ )
57         {
58                 scarr[l] = 0.0;
59                 for( k=0; k<26; k++ )
60                         scarr[l] += n_dis[k][l] * cpmx1[k][i1];
61         }
62 #if 0 /* ¤³¤ì¤ò»È¤¦¤È¤\ad¤Ïfloatwork¤Î¥¢¥í¥±¡¼¥È¤òµÕ¤Ë¤¹¤ë */
63         {
64                 float *fpt, **fptpt, *fpt2;
65                 int *ipt, **iptpt;
66                 fpt2 = match;
67                 iptpt = cpmxpdn;
68                 fptpt = cpmxpd;
69                 while( lgth2-- )
70                 {
71                         *fpt2 = 0.0;
72                         ipt=*iptpt,fpt=*fptpt;
73                         while( *ipt > -1 )
74                                 *fpt2 += scarr[*ipt++] * *fpt++;
75                         fpt2++,iptpt++,fptpt++;
76                 } 
77         }
78 #else
79         for( j=0; j<lgth2; j++ )
80         {
81                 match[j] = 0.0;
82                 for( k=0; cpmxpdn[k][j]>-1; k++ )
83                         match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
84         } 
85 #endif
86 }
87 #endif
88
89 static float Ltracking( float *lasthorizontalw, float *lastverticalw, 
90                                                 char **seq1, char **seq2, 
91                         char **mseq1, char **mseq2, 
92                         int **ijp, int *off1pt, int *off2pt, int endi, int endj, int localstop )
93 {
94         int i, j, l, iin, jin, lgth1, lgth2, k, limk;
95         int ifi=0, jfi=0; // by D.Mathog, a guess
96         char gap[] = "-";
97         lgth1 = strlen( seq1[0] );
98         lgth2 = strlen( seq2[0] );
99
100 #if 0
101         for( i=0; i<lgth1; i++ ) 
102         {
103                 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
104         }
105 #endif
106  
107     for( i=0; i<lgth1+1; i++ ) 
108     {
109         ijp[i][0] = localstop;
110     }
111     for( j=0; j<lgth2+1; j++ ) 
112     {
113         ijp[0][j] = localstop;
114     }
115
116         mseq1[0] += lgth1+lgth2;
117         *mseq1[0] = 0;
118         mseq2[0] += lgth1+lgth2;
119         *mseq2[0] = 0;
120         iin = endi; jin = endj;
121         limk = lgth1+lgth2;
122         for( k=0; k<=limk; k++ ) 
123         {
124                 if( ijp[iin][jin] < 0 ) 
125                 {
126                         ifi = iin-1; jfi = jin+ijp[iin][jin];
127                 }
128                 else if( ijp[iin][jin] > 0 )
129                 {
130                         ifi = iin-ijp[iin][jin]; jfi = jin-1;
131                 }
132                 else
133                 {
134                         ifi = iin-1; jfi = jin-1;
135                 }
136                 l = iin - ifi;
137                 while( --l ) 
138                 {
139                         *--mseq1[0] = seq1[0][ifi+l];
140                         *--mseq2[0] = *gap;
141                         k++;
142                 }
143                 l= jin - jfi;
144                 while( --l )
145                 {
146                         *--mseq1[0] = *gap;
147                         *--mseq2[0] = seq2[0][jfi+l];
148                         k++;
149                 }
150
151                 if( iin <= 0 || jin <= 0 ) break;
152                 *--mseq1[0] = seq1[0][ifi];
153                 *--mseq2[0] = seq2[0][jfi];
154                 if( ijp[ifi][jfi] == localstop ) break;
155                 k++;
156                 iin = ifi; jin = jfi;
157         }
158         if( ifi == -1 ) *off1pt = 0; else *off1pt = ifi;
159         if( jfi == -1 ) *off2pt = 0; else *off2pt = jfi;
160
161 //      fprintf( stderr, "ifn = %d, jfn = %d\n", ifi, jfi );
162
163
164         return( 0.0 );
165 }
166
167
168 float L__align11( char **seq1, char **seq2, int alloclen, int *off1pt, int *off2pt )
169 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
170 {
171 //      int k;
172         register int i, j;
173         int lasti, lastj;                      /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
174         int lgth1, lgth2;
175         int resultlen;
176         float wm = 0.0;   /* int ?????? */
177         float g;
178         float *currentw, *previousw;
179 #if 1
180         float *wtmp;
181         int *ijppt;
182         float *mjpt, *prept, *curpt;
183         int *mpjpt;
184 #endif
185         float mi, *m;
186         int **ijp;
187         int mpi, *mp;
188         float *w1, *w2;
189         float *match;
190         float *initverticalw;    /* kufuu sureba iranai */
191         float *lastverticalw;    /* kufuu sureba iranai */
192         char **mseq1;
193         char **mseq2;
194         char **mseq;
195 //      int **intwork;
196 //      float **floatwork;
197         float maxwm;
198         int endali = 0, endalj = 0; // by D.Mathog, a guess
199 //      int endali, endalj;
200         float localthr = -offset;
201         float localthr2 = -offset;
202 //      float localthr = 100;
203 //      float localthr2 = 100;
204         float fpenalty = (float)penalty;
205         float fpenalty_ex = (float)penalty_ex;
206         int **localIP = NULL;
207         int localAlloc1 = 0, localAlloc2 = 0;
208         int localstop; // 060910
209
210
211         mseq1 = AllocateCharMtx( njob, 0 );
212         mseq2 = AllocateCharMtx( njob, 0 );
213
214
215         lgth1 = strlen( seq1[0] );
216         lgth2 = strlen( seq2[0] );
217
218         {
219                 int ll1, ll2;
220
221
222                 ll1 = (int)(1.3*lgth1) + 100;
223                 ll2 = (int)(1.3*lgth2) + 100;
224
225 #if DEBUG
226                 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
227 #endif
228
229                 w1 = AllocateFloatVec( ll2+2 );
230                 w2 = AllocateFloatVec( ll2+2 );
231                 match = AllocateFloatVec( ll2+2 );
232
233                 initverticalw = AllocateFloatVec( ll1+2 );
234                 lastverticalw = AllocateFloatVec( ll1+2 );
235
236                 m = AllocateFloatVec( ll2+2 );
237                 mp = AllocateIntVec( ll2+2 );
238
239                 mseq = AllocateCharMtx( njob, ll1+ll2 );
240
241
242 //              floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 ); 
243 //              intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 ); 
244
245 #if DEBUG
246                 fprintf( stderr, "succeeded\n" );
247 #endif
248
249         }
250
251
252         mseq1[0] = mseq[0];
253         mseq2[0] = mseq[1];
254
255
256         {
257                 int ll1, ll2;
258
259
260                 ll1 = (int)(1.3*lgth1) + 100;
261                 ll2 = (int)(1.3*lgth2) + 100;
262
263 #if DEBUG
264                 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
265 #endif
266
267                 localIP = AllocateIntMtx( ll1+10, ll2+10 );
268
269 #if DEBUG
270                 fprintf( stderr, "succeeded\n\n" );
271 #endif
272
273                 localAlloc1 = ll1;
274                 localAlloc2 = ll2;
275         }
276         ijp = localIP;
277
278
279 #if 0
280         for( i=0; i<lgth1; i++ ) 
281                 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
282 #endif
283
284         currentw = w1;
285         previousw = w2;
286
287         match_calc( initverticalw, seq2, seq1, 0, lgth1 );
288
289         match_calc( currentw, seq1, seq2, 0, lgth2 );
290
291
292         lasti = lgth2+1;
293         for( j=1; j<lasti; ++j ) 
294         {
295                 m[j] = currentw[j-1]; mp[j] = 0;
296 #if 0
297                 if( m[j] < localthr ) m[j] = localthr2;
298 #endif
299         }
300
301         lastverticalw[0] = currentw[lgth2-1];
302
303         lasti = lgth1+1;
304
305 #if 0
306 fprintf( stderr, "currentw = \n" );
307 for( i=0; i<lgth1+1; i++ )
308 {
309         fprintf( stderr, "%5.2f ", currentw[i] );
310 }
311 fprintf( stderr, "\n" );
312 fprintf( stderr, "initverticalw = \n" );
313 for( i=0; i<lgth2+1; i++ )
314 {
315         fprintf( stderr, "%5.2f ", initverticalw[i] );
316 }
317 fprintf( stderr, "\n" );
318 #endif
319 #if DEBUG2
320         fprintf( stderr, "\n" );
321         fprintf( stderr, "       " );
322         for( j=0; j<lgth2; j++ )
323                 fprintf( stderr, "%c     ", seq2[0][j] );
324         fprintf( stderr, "\n" );
325 #endif
326
327         localstop = lgth1+lgth2+1;
328         maxwm = -999999999.9;
329 #if DEBUG2
330         fprintf( stderr, "\n" );
331         fprintf( stderr, "%c   ", seq1[0][0] );
332
333         for( j=0; j<lgth2+1; j++ )
334                 fprintf( stderr, "%5.0f ", currentw[j] );
335         fprintf( stderr, "\n" );
336 #endif
337
338         for( i=1; i<lasti; i++ )
339         {
340                 wtmp = previousw; 
341                 previousw = currentw;
342                 currentw = wtmp;
343
344                 previousw[0] = initverticalw[i-1];
345
346                 match_calc( currentw, seq1, seq2, i, lgth2 );
347 #if DEBUG2
348                 fprintf( stderr, "%c   ", seq1[0][i] );
349                 fprintf( stderr, "%5.0f ", currentw[0] );
350 #endif
351
352 #if XXXXXXX
353 fprintf( stderr, "\n" );
354 fprintf( stderr, "i=%d\n", i );
355 fprintf( stderr, "currentw = \n" );
356 for( j=0; j<lgth2; j++ )
357 {
358         fprintf( stderr, "%5.2f ", currentw[j] );
359 }
360 fprintf( stderr, "\n" );
361 #endif
362 #if XXXXXXX
363 fprintf( stderr, "\n" );
364 fprintf( stderr, "i=%d\n", i );
365 fprintf( stderr, "currentw = \n" );
366 for( j=0; j<lgth2; j++ )
367 {
368         fprintf( stderr, "%5.2f ", currentw[j] );
369 }
370 fprintf( stderr, "\n" );
371 #endif
372                 currentw[0] = initverticalw[i];
373
374                 mi = previousw[0]; mpi = 0;
375
376 #if 0
377                 if( mi < localthr ) mi = localthr2;
378 #endif
379
380                 ijppt = ijp[i] + 1;
381                 mjpt = m + 1;
382                 prept = previousw;
383                 curpt = currentw + 1;
384                 mpjpt = mp + 1;
385                 lastj = lgth2+1;
386                 for( j=1; j<lastj; j++ )
387                 {
388                         wm = *prept;
389                         *ijppt = 0;
390
391 #if 0
392                         fprintf( stderr, "%5.0f->", wm );
393 #endif
394 #if 0
395                         fprintf( stderr, "%5.0f?", g );
396 #endif
397                         if( (g=mi+fpenalty) > wm )
398                         {
399                                 wm = g;
400                                 *ijppt = -( j - mpi );
401                         }
402                         if( *prept > mi )
403                         {
404                                 mi = *prept;
405                                 mpi = j-1;
406                         }
407
408 #if USE_PENALTY_EX
409                         mi += fpenalty_ex;
410 #endif
411
412 #if 0 
413                         fprintf( stderr, "%5.0f?", g );
414 #endif
415                         if( (g=*mjpt+fpenalty) > wm )
416                         {
417                                 wm = g;
418                                 *ijppt = +( i - *mpjpt );
419                         }
420                         if( *prept > *mjpt )
421                         {
422                                 *mjpt = *prept;
423                                 *mpjpt = i-1;
424                         }
425 #if USE_PENALTY_EX
426                         *mjpt += fpenalty_ex;
427 #endif
428
429                         if( maxwm < wm )
430                         {
431                                 maxwm = wm;
432                                 endali = i;
433                                 endalj = j;
434                         }
435 #if 1
436                         if( wm < localthr )
437                         {
438 //                              fprintf( stderr, "stop i=%d, j=%d, curpt=%f\n", i, j, *curpt );
439                                 *ijppt = localstop;
440                                 wm = localthr2;
441                         }
442 #endif
443 #if 0
444                         fprintf( stderr, "%5.0f ", *curpt );
445 #endif
446 #if DEBUG2
447                         fprintf( stderr, "%5.0f ", wm );
448 //                      fprintf( stderr, "%c-%c *ijppt = %d, localstop = %d\n", seq1[0][i], seq2[0][j], *ijppt, localstop );
449 #endif
450
451                         *curpt++ += wm;
452                         ijppt++;
453                         mjpt++;
454                         prept++;
455                         mpjpt++;
456                 }
457 #if DEBUG2
458                 fprintf( stderr, "\n" );
459 #endif
460
461                 lastverticalw[i] = currentw[lgth2-1];
462         }
463
464
465 #if 0
466         fprintf( stderr, "maxwm = %f\n", maxwm );
467         fprintf( stderr, "endali = %d\n", endali );
468         fprintf( stderr, "endalj = %d\n", endalj );
469 #endif
470
471         if( ijp[endali][endalj] == localstop )
472         {
473                 strcpy( seq1[0], "" );
474                 strcpy( seq2[0], "" );
475                 *off1pt = *off2pt = 0;
476                 return( 0.0 );
477         }
478                 
479         Ltracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, ijp, off1pt, off2pt, endali, endalj, localstop );
480
481
482         resultlen = strlen( mseq1[0] );
483         if( alloclen < resultlen || resultlen > N )
484         {
485                 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
486                 ErrorExit( "LENGTH OVER!\n" );
487         }
488
489
490         strcpy( seq1[0], mseq1[0] );
491         strcpy( seq2[0], mseq2[0] );
492
493 #if 0
494         fprintf( stderr, "wm=%f\n", wm );
495         fprintf( stderr, ">\n%s\n", mseq1[0] );
496         fprintf( stderr, ">\n%s\n", mseq2[0] );
497
498         fprintf( stderr, "maxwm = %f\n", maxwm );
499         fprintf( stderr, "   wm = %f\n",    wm );
500 #endif
501
502         FreeFloatVec( w1 );
503         FreeFloatVec( w2 );
504         FreeFloatVec( match );
505         FreeFloatVec( initverticalw );
506         FreeFloatVec( lastverticalw );
507
508         FreeFloatVec( m );
509         FreeIntVec( mp );
510
511         FreeCharMtx( mseq );
512
513
514
515 //      FreeFloatMtx( floatwork );
516 //      FreeIntMtx( intwork );
517
518         FreeIntMtx( localIP );
519
520         return( maxwm );
521 }
522