Mac binaries
[jabaws.git] / website / archive / binaries / mac / src / mafft / core / Galign11.c
1 #include "mltaln.h"
2 #include "dp.h"
3
4 #define DEBUG 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 = amino_dis[(int)s1[0][i1]];
14
15         while( lgth2-- )
16                 *match++ = intptr[(int)*seq2++];
17 }
18 static void match_calc_mtx( int mtx[0x80][0x80], float *match, char **s1, char **s2, int i1, int lgth2 ) 
19 {
20         char *seq2 = s2[0];
21         int *intptr = mtx[(int)s1[0][i1]];
22
23         while( lgth2-- )
24                 *match++ = intptr[(int)*seq2++];
25 }
26 #else
27 static void match_calc( float *match, char **s1, char **s2, int i1, int lgth2 )
28 {
29         int j;
30
31         for( j=0; j<lgth2; j++ )
32                 match[j] = amino_dis[(*s1)[i1]][(*s2)[j]];
33 }
34 #endif
35
36 static float Atracking( float *lasthorizontalw, float *lastverticalw, 
37                                                 char **seq1, char **seq2, 
38                         char **mseq1, char **mseq2, 
39                         int **ijp,
40                                                 int tailgp )
41 {
42         int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k, limk;
43 //      char gap[] = "-";
44         char *gap;
45         gap = newgapstr;
46         lgth1 = strlen( seq1[0] );
47         lgth2 = strlen( seq2[0] );
48         float wm;
49
50
51 #if 0
52         for( i=0; i<lgth1; i++ ) 
53         {
54                 fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
55         }
56 #endif
57  
58     for( i=0; i<lgth1+1; i++ ) 
59     {
60         ijp[i][0] = i + 1;
61     }
62     for( j=0; j<lgth2+1; j++ ) 
63     {
64         ijp[0][j] = -( j + 1 );
65     }
66
67         if( tailgp == 1 )
68                 ;
69         else
70         {
71                 wm = lastverticalw[0];
72                 for( i=0; i<lgth1; i++ )
73                 {
74                         if( lastverticalw[i] >= wm )
75                         {
76                                 wm = lastverticalw[i];
77                                 iin = i; jin = lgth2-1;
78                                 ijp[lgth1][lgth2] = +( lgth1 - i );
79                         }
80                 }
81                 for( j=0; j<lgth2; j++ )
82                 {
83                         if( lasthorizontalw[j] >= wm )
84                         {
85                                 wm = lasthorizontalw[j];
86                                 iin = lgth1-1; jin = j;
87                                 ijp[lgth1][lgth2] = -( lgth2 - j );
88                         }
89                 }
90         }
91
92
93
94         mseq1[0] += lgth1+lgth2;
95         *mseq1[0] = 0;
96         mseq2[0] += lgth1+lgth2;
97         *mseq2[0] = 0;
98
99
100         iin = lgth1; jin = lgth2;
101         limk = lgth1+lgth2 + 1;
102         for( k=0; k<limk; k++ ) 
103         {
104                 if( ijp[iin][jin] < 0 ) 
105                 {
106                         ifi = iin-1; jfi = jin+ijp[iin][jin];
107                 }
108                 else if( ijp[iin][jin] > 0 )
109                 {
110                         ifi = iin-ijp[iin][jin]; jfi = jin-1;
111                 }
112                 else
113                 {
114                         ifi = iin-1; jfi = jin-1;
115                 }
116                 l = iin - ifi;
117                 while( --l ) 
118                 {
119                         *--mseq1[0] = seq1[0][ifi+l];
120                         *--mseq2[0] = *gap;
121                         k++;
122                 }
123                 l= jin - jfi;
124                 while( --l )
125                 {
126                         *--mseq1[0] = *gap;
127                         *--mseq2[0] = seq2[0][jfi+l];
128                         k++;
129                 }
130                 if( iin <= 0 || jin <= 0 ) break;
131                 *--mseq1[0] = seq1[0][ifi];
132                 *--mseq2[0] = seq2[0][jfi];
133                 k++;
134                 iin = ifi; jin = jfi;
135         }
136         return( 0.0 );
137 }
138
139
140 float G__align11( char **seq1, char **seq2, int alloclen, int headgp, int tailgp )
141 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
142 {
143 //      int k;
144         register int i, j;
145         int lasti;                      /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
146         int lgth1, lgth2;
147         int resultlen;
148         float wm;   /* int ?????? */
149         float g;
150         float *currentw, *previousw;
151         float fpenalty = (float)penalty;
152 #if USE_PENALTY_EX
153         float fpenalty_ex = (float)penalty_ex;
154 #endif
155 #if 1
156         float *wtmp;
157         int *ijppt;
158         float *mjpt, *prept, *curpt;
159         int *mpjpt;
160 #endif
161         static TLS float mi, *m;
162         static TLS int **ijp;
163         static TLS int mpi, *mp;
164         static TLS float *w1, *w2;
165         static TLS float *match;
166         static TLS float *initverticalw;    /* kufuu sureba iranai */
167         static TLS float *lastverticalw;    /* kufuu sureba iranai */
168         static TLS char **mseq1;
169         static TLS char **mseq2;
170         static TLS char **mseq;
171         static TLS int **intwork;
172         static TLS float **floatwork;
173         static TLS int orlgth1 = 0, orlgth2 = 0;
174
175         if( seq1 == NULL )
176         {
177                 if( orlgth1 > 0 && orlgth2 > 0 )
178                 {
179                         orlgth1 = 0;
180                         orlgth2 = 0;
181                         free( mseq1 );
182                         free( mseq2 );
183                         FreeFloatVec( w1 );
184                         FreeFloatVec( w2 );
185                         FreeFloatVec( match );
186                         FreeFloatVec( initverticalw );
187                         FreeFloatVec( lastverticalw );
188
189                         FreeFloatVec( m );
190                         FreeIntVec( mp );
191
192                         FreeCharMtx( mseq );
193
194
195
196                         FreeFloatMtx( floatwork );
197                         FreeIntMtx( intwork );
198                 }
199                 return( 0.0 );
200         }
201
202         lgth1 = strlen( seq1[0] );
203         lgth2 = strlen( seq2[0] );
204
205         if( lgth1 <= 0 || lgth2 <= 0 )
206         {
207                 fprintf( stderr, "WARNING (g11): lgth1=%d, lgth2=%d\n", lgth1, lgth2 );
208         }
209
210 #if 1
211         if( lgth1 == 0 && lgth2 == 0 )
212                 return( 0.0 );
213
214         if( lgth1 == 0 )
215         {
216                 seq1[0][lgth2] = 0;
217                 while( lgth2 ) seq1[0][--lgth2] = '-';
218 //              fprintf( stderr, "seq1[0] = %s\n", seq1[0] );
219                 return( 0.0 );
220         }
221
222         if( lgth2 == 0 )
223         {
224                 seq2[0][lgth1] = 0;
225                 while( lgth1 ) seq2[0][--lgth1] = '-';
226 //              fprintf( stderr, "seq2[0] = %s\n", seq2[0] );
227                 return( 0.0 );
228         }
229 #endif
230
231
232         wm = 0.0;
233
234         if( orlgth1 == 0 )
235         {
236                 mseq1 = AllocateCharMtx( njob, 0 );
237                 mseq2 = AllocateCharMtx( njob, 0 );
238         }
239
240
241
242         if( lgth1 > orlgth1 || lgth2 > orlgth2 )
243         {
244                 int ll1, ll2;
245
246                 if( orlgth1 > 0 && orlgth2 > 0 )
247                 {
248                         FreeFloatVec( w1 );
249                         FreeFloatVec( w2 );
250                         FreeFloatVec( match );
251                         FreeFloatVec( initverticalw );
252                         FreeFloatVec( lastverticalw );
253
254                         FreeFloatVec( m );
255                         FreeIntVec( mp );
256
257                         FreeCharMtx( mseq );
258
259
260
261                         FreeFloatMtx( floatwork );
262                         FreeIntMtx( intwork );
263                 }
264
265                 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
266                 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
267
268 #if DEBUG
269                 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
270 #endif
271
272                 w1 = AllocateFloatVec( ll2+2 );
273                 w2 = AllocateFloatVec( ll2+2 );
274                 match = AllocateFloatVec( ll2+2 );
275
276                 initverticalw = AllocateFloatVec( ll1+2 );
277                 lastverticalw = AllocateFloatVec( ll1+2 );
278
279                 m = AllocateFloatVec( ll2+2 );
280                 mp = AllocateIntVec( ll2+2 );
281
282                 mseq = AllocateCharMtx( njob, ll1+ll2 );
283
284
285                 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 ); 
286                 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 ); 
287
288 #if DEBUG
289                 fprintf( stderr, "succeeded\n" );
290 #endif
291
292                 orlgth1 = ll1 - 100;
293                 orlgth2 = ll2 - 100;
294         }
295
296
297         mseq1[0] = mseq[0];
298         mseq2[0] = mseq[1];
299
300
301         if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
302         {
303                 int ll1, ll2;
304
305                 if( commonAlloc1 && commonAlloc2 )
306                 {
307                         FreeIntMtx( commonIP );
308                 }
309
310                 ll1 = MAX( orlgth1, commonAlloc1 );
311                 ll2 = MAX( orlgth2, commonAlloc2 );
312
313 #if DEBUG
314                 fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
315 #endif
316
317                 commonIP = AllocateIntMtx( ll1+10, ll2+10 );
318
319 #if DEBUG
320                 fprintf( stderr, "succeeded\n\n" );
321 #endif
322
323                 commonAlloc1 = ll1;
324                 commonAlloc2 = ll2;
325         }
326         ijp = commonIP;
327
328
329 #if 0
330         for( i=0; i<lgth1; i++ ) 
331                 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
332 #endif
333
334         currentw = w1;
335         previousw = w2;
336
337
338         match_calc( initverticalw, seq2, seq1, 0, lgth1 );
339
340
341         match_calc( currentw, seq1, seq2, 0, lgth2 );
342
343         if( headgp == 1 )
344         {
345                 for( i=1; i<lgth1+1; i++ )
346                 {
347                         initverticalw[i] += fpenalty;
348                 }
349                 for( j=1; j<lgth2+1; j++ )
350                 {
351                         currentw[j] += fpenalty;
352                 }
353         }
354
355         for( j=1; j<lgth2+1; ++j ) 
356         {
357                 m[j] = currentw[j-1]; mp[j] = 0;
358         }
359
360         if( lgth2 == 0 )
361                 lastverticalw[0] = 0.0;               // lgth2==0 no toki error
362         else
363                 lastverticalw[0] = currentw[lgth2-1]; // lgth2==0 no toki error
364
365         if( tailgp ) lasti = lgth1+1; else lasti = lgth1;
366
367 #if XXXXXXX
368 fprintf( stderr, "currentw = \n" );
369 for( i=0; i<lgth1+1; i++ )
370 {
371         fprintf( stderr, "%5.2f ", currentw[i] );
372 }
373 fprintf( stderr, "\n" );
374 fprintf( stderr, "initverticalw = \n" );
375 for( i=0; i<lgth2+1; i++ )
376 {
377         fprintf( stderr, "%5.2f ", initverticalw[i] );
378 }
379 fprintf( stderr, "\n" );
380 #endif
381
382         for( i=1; i<lasti; i++ )
383         {
384                 wtmp = previousw; 
385                 previousw = currentw;
386                 currentw = wtmp;
387
388                 previousw[0] = initverticalw[i-1];
389
390                 match_calc( currentw, seq1, seq2, i, lgth2 );
391 #if XXXXXXX
392 fprintf( stderr, "\n" );
393 fprintf( stderr, "i=%d\n", i );
394 fprintf( stderr, "currentw = \n" );
395 for( j=0; j<lgth2; j++ )
396 {
397         fprintf( stderr, "%5.2f ", currentw[j] );
398 }
399 fprintf( stderr, "\n" );
400 #endif
401 #if XXXXXXX
402 fprintf( stderr, "\n" );
403 fprintf( stderr, "i=%d\n", i );
404 fprintf( stderr, "currentw = \n" );
405 for( j=0; j<lgth2; j++ )
406 {
407         fprintf( stderr, "%5.2f ", currentw[j] );
408 }
409 fprintf( stderr, "\n" );
410 #endif
411                 currentw[0] = initverticalw[i];
412
413                 mi = previousw[0]; mpi = 0;
414
415                 ijppt = ijp[i] + 1;
416                 mjpt = m + 1;
417                 prept = previousw;
418                 curpt = currentw + 1;
419                 mpjpt = mp + 1;
420                 for( j=1; j<lgth2+1; j++ )
421                 {
422                         wm = *prept;
423                         *ijppt = 0;
424
425 #if 0
426                         fprintf( stderr, "%5.0f->", wm );
427 #endif
428 #if 0
429                         fprintf( stderr, "%5.0f?", g );
430 #endif
431                         if( (g=mi+fpenalty) > wm )
432                         {
433                                 wm = g;
434                                 *ijppt = -( j - mpi );
435                         }
436                         if( (g=*prept) >= mi )
437                         {
438                                 mi = g;
439                                 mpi = j-1;
440                         }
441 #if USE_PENALTY_EX
442                         mi += fpenalty_ex;
443 #endif
444
445 #if 0 
446                         fprintf( stderr, "%5.0f?", g );
447 #endif
448                         if( (g=*mjpt + fpenalty) > wm )
449                         {
450                                 wm = g;
451                                 *ijppt = +( i - *mpjpt );
452                         }
453                         if( (g=*prept) >= *mjpt )
454                         {
455                                 *mjpt = g;
456                                 *mpjpt = i-1;
457                         }
458 #if USE_PENALTY_EX
459                         m[j] += fpenalty_ex;
460 #endif
461
462 #if 0
463                         fprintf( stderr, "%5.0f ", wm );
464 #endif
465                         *curpt++ += wm;
466                         ijppt++;
467                         mjpt++;
468                         prept++;
469                         mpjpt++;
470                 }
471                 lastverticalw[i] = currentw[lgth2-1]; // lgth2==0 no toki error
472         }
473
474         Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, ijp, tailgp );
475
476         resultlen = strlen( mseq1[0] );
477         if( alloclen < resultlen || resultlen > N )
478         {
479                 fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
480                 ErrorExit( "LENGTH OVER!\n" );
481         }
482
483
484         strcpy( seq1[0], mseq1[0] );
485         strcpy( seq2[0], mseq2[0] );
486 #if 0
487         fprintf( stderr, "\n" );
488         fprintf( stderr, ">\n%s\n", mseq1[0] );
489         fprintf( stderr, ">\n%s\n", mseq2[0] );
490         fprintf( stderr, "wm = %f\n", wm );
491 #endif
492
493         return( wm );
494 }
495
496 float G__align11_noalign( int scoremtx[0x80][0x80], int penal, int penal_ex, char **seq1, char **seq2, int alloclen )
497 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
498 {
499 //      int k;
500         register int i, j;
501         int lasti;                      /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
502         int lgth1, lgth2;
503 //      int resultlen;
504         float wm;   /* int ?????? */
505         float g;
506         float *currentw, *previousw;
507         float fpenalty = (float)penal;
508 #if USE_PENALTY_EX
509         float fpenalty_ex = (float)penal_ex;
510 #endif
511 #if 1
512         float *wtmp;
513         float *mjpt, *prept, *curpt;
514 //      int *mpjpt;
515 #endif
516         static TLS float mi, *m;
517         static TLS float *w1, *w2;
518         static TLS float *match;
519         static TLS float *initverticalw;    /* kufuu sureba iranai */
520         static TLS float *lastverticalw;    /* kufuu sureba iranai */
521         static TLS int **intwork;
522         static TLS float **floatwork;
523         static TLS int orlgth1 = 0, orlgth2 = 0;
524
525         if( seq1 == NULL )
526         {
527                 if( orlgth1 > 0 && orlgth2 > 0 )
528                 {
529                         orlgth1 = 0;
530                         orlgth2 = 0;
531                         FreeFloatVec( w1 );
532                         FreeFloatVec( w2 );
533                         FreeFloatVec( match );
534                         FreeFloatVec( initverticalw );
535                         FreeFloatVec( lastverticalw );
536                         free( m );
537
538
539                         FreeFloatMtx( floatwork );
540                         FreeIntMtx( intwork );
541                 }
542                 return( 0.0 );
543         }
544
545         wm = 0.0;
546
547
548
549         lgth1 = strlen( seq1[0] );
550         lgth2 = strlen( seq2[0] );
551
552
553
554         if( lgth1 <= 0 || lgth2 <= 0 )
555         {
556                 fprintf( stderr, "WARNING (g11): lgth1=%d, lgth2=%d\n", lgth1, lgth2 );
557         }
558
559         if( lgth1 > orlgth1 || lgth2 > orlgth2 )
560         {
561                 int ll1, ll2;
562
563                 if( orlgth1 > 0 && orlgth2 > 0 )
564                 {
565                         FreeFloatVec( w1 );
566                         FreeFloatVec( w2 );
567                         FreeFloatVec( match );
568                         FreeFloatVec( initverticalw );
569                         FreeFloatVec( lastverticalw );
570
571                         FreeFloatVec( m );
572
573
574
575
576                         FreeFloatMtx( floatwork );
577                         FreeIntMtx( intwork );
578                 }
579
580                 ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
581                 ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
582
583 #if DEBUG
584                 fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
585 #endif
586
587                 w1 = AllocateFloatVec( ll2+2 );
588                 w2 = AllocateFloatVec( ll2+2 );
589                 match = AllocateFloatVec( ll2+2 );
590
591                 initverticalw = AllocateFloatVec( ll1+2 );
592                 lastverticalw = AllocateFloatVec( ll1+2 );
593
594                 m = AllocateFloatVec( ll2+2 );
595
596
597
598                 floatwork = AllocateFloatMtx( 26, MAX( ll1, ll2 )+2 ); 
599                 intwork = AllocateIntMtx( 26, MAX( ll1, ll2 )+2 ); 
600
601 #if DEBUG
602                 fprintf( stderr, "succeeded\n" );
603 #endif
604
605                 orlgth1 = ll1 - 100;
606                 orlgth2 = ll2 - 100;
607         }
608
609
610
611
612
613
614 #if 0
615         for( i=0; i<lgth1; i++ ) 
616                 fprintf( stderr, "ogcp1[%d]=%f\n", i, ogcp1[i] );
617 #endif
618
619         currentw = w1;
620         previousw = w2;
621
622
623         match_calc_mtx( scoremtx, initverticalw, seq2, seq1, 0, lgth1 );
624
625
626         match_calc_mtx( scoremtx, currentw, seq1, seq2, 0, lgth2 );
627
628         if( 1 ) // tsuneni outgap-1
629         {
630                 for( i=1; i<lgth1+1; i++ )
631                 {
632                         initverticalw[i] += fpenalty;
633                 }
634                 for( j=1; j<lgth2+1; j++ )
635                 {
636                         currentw[j] += fpenalty;
637                 }
638         }
639
640         for( j=1; j<lgth2+1; ++j ) 
641         {
642                 m[j] = currentw[j-1];
643         }
644
645         if( lgth2 == 0 )
646                 lastverticalw[0] = 0.0;               // lgth2==0 no toki error
647         else
648                 lastverticalw[0] = currentw[lgth2-1]; // lgth2==0 no toki error
649
650         if( 1 ) lasti = lgth1+1; else lasti = lgth1; // tsuneni outgap-1
651
652 #if XXXXXXX
653 fprintf( stderr, "currentw = \n" );
654 for( i=0; i<lgth1+1; i++ )
655 {
656         fprintf( stderr, "%5.2f ", currentw[i] );
657 }
658 fprintf( stderr, "\n" );
659 fprintf( stderr, "initverticalw = \n" );
660 for( i=0; i<lgth2+1; i++ )
661 {
662         fprintf( stderr, "%5.2f ", initverticalw[i] );
663 }
664 fprintf( stderr, "\n" );
665 #endif
666
667         for( i=1; i<lasti; i++ )
668         {
669                 wtmp = previousw; 
670                 previousw = currentw;
671                 currentw = wtmp;
672
673                 previousw[0] = initverticalw[i-1];
674
675                 match_calc_mtx( scoremtx, currentw, seq1, seq2, i, lgth2 );
676 #if XXXXXXX
677 fprintf( stderr, "\n" );
678 fprintf( stderr, "i=%d\n", i );
679 fprintf( stderr, "currentw = \n" );
680 for( j=0; j<lgth2; j++ )
681 {
682         fprintf( stderr, "%5.2f ", currentw[j] );
683 }
684 fprintf( stderr, "\n" );
685 #endif
686 #if XXXXXXX
687 fprintf( stderr, "\n" );
688 fprintf( stderr, "i=%d\n", i );
689 fprintf( stderr, "currentw = \n" );
690 for( j=0; j<lgth2; j++ )
691 {
692         fprintf( stderr, "%5.2f ", currentw[j] );
693 }
694 fprintf( stderr, "\n" );
695 #endif
696                 currentw[0] = initverticalw[i];
697
698                 mi = previousw[0];
699
700                 mjpt = m + 1;
701                 prept = previousw;
702                 curpt = currentw + 1;
703                 for( j=1; j<lgth2+1; j++ )
704                 {
705                         wm = *prept;
706
707 #if 0
708                         fprintf( stderr, "%5.0f->", wm );
709 #endif
710 #if 0
711                         fprintf( stderr, "%5.0f?", g );
712 #endif
713                         if( (g=mi+fpenalty) > wm )
714                         {
715                                 wm = g;
716                         }
717                         if( (g=*prept) >= mi )
718                         {
719                                 mi = g;
720                         }
721 #if USE_PENALTY_EX
722                         mi += fpenalty_ex;
723 #endif
724
725 #if 0 
726                         fprintf( stderr, "%5.0f?", g );
727 #endif
728                         if( (g=*mjpt + fpenalty) > wm )
729                         {
730                                 wm = g;
731                         }
732                         if( (g=*prept) >= *mjpt )
733                         {
734                                 *mjpt = g;
735                         }
736 #if USE_PENALTY_EX
737                         m[j] += fpenalty_ex;
738 #endif
739
740 #if 0
741                         fprintf( stderr, "%5.0f ", wm );
742 #endif
743                         *curpt++ += wm;
744                         mjpt++;
745                         prept++;
746                 }
747                 lastverticalw[i] = currentw[lgth2-1]; // lgth2==0 no toki error
748         }
749
750 #if 0
751         fprintf( stderr, "\n" );
752         fprintf( stderr, ">\n%s\n", mseq1[0] );
753         fprintf( stderr, ">\n%s\n", mseq2[0] );
754         fprintf( stderr, "wm = %f\n", wm );
755 #endif
756
757         return( wm );
758 }