6bb6416579f0041805d1e7c72e90b873f3c6ba46
[jabaws.git] / binaries / src / mafft / core / mltaln9.c
1 #include "mltaln.h"
2
3 #define DEBUG 0
4 #define CANONICALTREEFORMAT 1
5 #define MEMSAVE 1
6
7
8
9 #if 0
10 int seqlen( char *seq )
11 {
12         int val = 0;
13         while( *seq )
14                 if( *seq++ != '-' ) val++;
15         return( val );
16 }
17 #else
18 int seqlen( char *seq )
19 {
20         int val = 0;
21         if( *newgapstr == '-' )
22         {
23                 while( *seq )
24                         if( *seq++ != '-' ) val++;
25         }
26         else
27         {
28                 while( *seq )
29                 {
30                         if( *seq != '-' && *seq != *newgapstr ) val++;
31                         seq++;
32                 }
33         }
34         return( val );
35 }
36 #endif
37
38 int intlen( int *num )
39 {
40         int value = 0;
41         while( *num++ != -1 ) value++;
42         return( value );
43 }
44
45 char seqcheck( char **seq )
46 {
47         int i, len;
48         char **seqbk = seq;
49         while( *seq )   
50         {
51                 len = strlen( *seq );
52                 for( i=0; i<len; i++ ) 
53                 {
54                         if( amino_n[(int)(*seq)[i]] == -1 ) 
55                         {
56
57                                 reporterr(       "========================================================================= \n" );
58                                 reporterr(       "========================================================================= \n" );
59                                 reporterr(       "=== \n" );
60                                 reporterr(       "=== Alphabet '%c' is unknown.\n", (*seq)[i] );
61                                 reporterr(       "=== Please check site %d in sequence %d.\n", i+1, (int)(seq-seqbk+1) );
62                                 reporterr(       "=== \n" );
63                                 reporterr(       "=== To make an alignment having unusual characters (U, @, #, etc), try\n" );
64                                 reporterr(       "=== %% mafft --anysymbol input > output\n" );
65                                 reporterr(       "=== \n" );
66                                 reporterr(       "========================================================================= \n" );
67                                 reporterr(       "========================================================================= \n" );
68                                 return( (int)(*seq)[i] );
69                         }
70                 }
71                 seq++;
72         }
73         return( 0 );
74 }
75
76 void intcat( int *s1, int *s2 )
77 {
78         while( *s1 != -1 ) s1++;
79         while( *s2 != -1 ) 
80         {
81 //              reporterr(       "copying %d\n", *s2 );
82                 *s1++ = *s2++;
83         }
84         *s1 = -1;
85 }
86
87 void intcpy( int *s1, int *s2 )
88 {
89         while( *s2 != -1 ) 
90         {
91 //              reporterr(       "copying %d\n", *s2 );
92                 *s1++ = *s2++;
93         }
94         *s1 = -1;
95 }
96
97 void intncpy( int *s1, int *s2, int n )
98 {
99         while( n-- ) *s1++ = *s2++;
100 }
101
102 void fltncpy( double *s1, double *s2, int n )
103 {
104         while( n-- ) *s1++ = *s2++;
105 }
106
107 static int countmem( int *s )
108 {
109         int v = 0;
110         while( *s++ != -1 ) v++;
111         return( v );
112 }
113
114 static int lastmem( int *s )
115 {
116         while( *s++ != -1 ) 
117                 ;
118         return( *(s-2) );
119 }
120
121         
122 void scmx_calc( int icyc, char **aseq, double *effarr, double **scmx )
123 {
124         int  i, j, lgth;
125          
126         lgth = strlen( aseq[0] );
127         for( j=0; j<lgth; j++ )
128         {
129                 for( i=0; i<nalphabets; i++ )
130                 {
131                         scmx[i][j] = 0;
132                 }
133         }
134         for( i=0; i<icyc+1; i++ )
135         {
136                 int id;
137                 id = amino_n[(unsigned char)aseq[i][0]];
138                 scmx[id][0] += (double)effarr[i];
139         }
140         for( j=1; j<lgth-1; j++ )
141         {
142                 for( i=0; i<icyc+1; i++ )
143                 {
144                         int id;
145                         id = amino_n[(unsigned char)aseq[i][j]];
146                         scmx[id][j] += (double)effarr[i];
147                 }
148         }
149         for( i=0; i<icyc+1; i++ )
150         {
151                 int id;
152                 id = amino_n[(unsigned char)aseq[i][lgth-1]];
153                 scmx[id][lgth-1] += (double)effarr[i];
154         }
155 }
156
157 void exitall( char arr[] )
158 {
159         reporterr(       "%s\n", arr );
160         exit( 1 );
161 }
162
163 void display( char **seq, int nseq )
164 {
165         int i, imax;
166         char b[121];
167
168         if( !disp ) return;
169                 if( nseq > DISPSEQF ) imax = DISPSEQF;
170                 else                  imax = nseq;
171                 reporterr(       "    ....,....+....,....+....,....+....,....+....,....+....,....+....,....+....,....+....,....+....,....+....,....+....,....+\n" );
172                 for( i=0; i<+imax; i++ )
173                 {
174                         strncpy( b, seq[i]+DISPSITEI, 120 );
175                         b[120] = 0;
176                         reporterr(       "%3d %s\n", i+1, b );
177                 }
178 }
179
180 void intergroup_score_consweight( char **seq1, char **seq2, double *eff1, double *eff2, int clus1, int clus2, int len, double *value )
181 {
182         int i, j, k;
183         int len2 = len - 2;
184         unsigned char ms1, ms2;
185         double tmpscore;
186         char *mseq1, *mseq2;
187         double efficient;
188
189 //      totaleff1 = 0.0; for( i=0; i<clus1; i++ ) totaleff1 += eff1[i];
190 //      totaleff2 = 0.0; for( i=0; i<clus2; i++ ) totaleff2 += eff2[i];
191
192
193
194         *value = 0.0;
195         for( i=0; i<clus1; i++ ) 
196         {
197                 for( j=0; j<clus2; j++ ) 
198                 {
199                         efficient = eff1[i] * eff2[j]; /* \e$B$J$<$+G[Ns$r;H$o$J$$$H$*$+$7$/$J$k\e(B, \e$BB?J,%P%0\e(B */
200                         mseq1 = seq1[i];
201                         mseq2 = seq2[j];
202                         tmpscore = 0.0;
203                         for( k=0; k<len; k++ ) 
204                         {
205                                 ms1 = (unsigned char)mseq1[k];
206                                 ms2 = (unsigned char)mseq2[k];
207                                 if( ms1 == '-' && ms2 == '-' ) continue;
208                                 tmpscore += (double)amino_dis_consweight_multi[ms1][ms2];
209         
210                                 if( ms1 == '-' ) 
211                                 {
212                                         tmpscore += (double)penalty;
213                                         tmpscore += (double)amino_dis_consweight_multi[ms1][ms2];
214                                         while( (ms1=(unsigned char)mseq1[++k]) == '-' )
215                                                 ;
216 //                                              tmpscore += (double)amino_dis_consweight_multi[ms1][ms2];
217                                         k--;
218                                         if( k >len2 ) break;
219                                         continue;
220                                 }
221                                 if( ms2 == '-' )
222                                 {
223                                         tmpscore += (double)penalty;
224                                         tmpscore += (double)amino_dis_consweight_multi[ms1][ms2];
225                                         while( (ms2=(unsigned char)mseq2[++k]) == '-' )
226                                                 ;
227 //                                              tmpscore += (double)amino_dis_consweight_multi[ms1][ms2];
228                                         k--;
229                                         if( k > len2 ) break;
230                                         continue;
231                                 }
232                         }
233                         *value += (double)tmpscore * (double)efficient;
234 //                      reporterr(       "val in _gapnomi = %f\n", *value );
235                 }
236         }
237 #if 0
238         fprintf( stdout, "###score = %f\n", score );
239 #endif
240 #if DEBUG
241         reporterr(       "score in intergroup_score = %f\n", score );
242 #endif
243 //      return( score );
244 }
245 void intergroup_score_gapnomi( char **seq1, char **seq2, double *eff1, double *eff2, int clus1, int clus2, int len, double *value )
246 {
247         int i, j, k;
248         int len2 = len - 2;
249         int ms1, ms2;
250         double tmpscore;
251         char *mseq1, *mseq2;
252         double efficient;
253
254 //      totaleff1 = 0.0; for( i=0; i<clus1; i++ ) totaleff1 += eff1[i];
255 //      totaleff2 = 0.0; for( i=0; i<clus2; i++ ) totaleff2 += eff2[i];
256
257
258
259         *value = 0.0;
260         for( i=0; i<clus1; i++ ) 
261         {
262                 for( j=0; j<clus2; j++ ) 
263                 {
264                         efficient = eff1[i] * eff2[j]; /* \e$B$J$<$+G[Ns$r;H$o$J$$$H$*$+$7$/$J$k\e(B, \e$BB?J,%P%0\e(B */
265                         mseq1 = seq1[i];
266                         mseq2 = seq2[j];
267                         tmpscore = 0.0;
268                         for( k=0; k<len; k++ ) 
269                         {
270                                 ms1 = (int)mseq1[k];
271                                 ms2 = (int)mseq2[k];
272                                 if( ms1 == (int)'-' && ms2 == (int)'-' ) continue;
273 //                              tmpscore += (double)amino_dis[ms1][ms2];
274         
275                                 if( ms1 == (int)'-' ) 
276                                 {
277                                         tmpscore += (double)penalty;
278 //                                      tmpscore += (double)amino_dis[ms1][ms2];
279                                         while( (ms1=(int)mseq1[++k]) == (int)'-' )
280                                                 ;
281 //                                              tmpscore += (double)amino_dis[ms1][ms2];
282                                         k--;
283                                         if( k >len2 ) break;
284                                         continue;
285                                 }
286                                 if( ms2 == (int)'-' )
287                                 {
288                                         tmpscore += (double)penalty;
289 //                                      tmpscore += (double)amino_dis[ms1][ms2];
290                                         while( (ms2=(int)mseq2[++k]) == (int)'-' )
291                                                 ;
292 //                                              tmpscore += (double)amino_dis[ms1][ms2];
293                                         k--;
294                                         if( k > len2 ) break;
295                                         continue;
296                                 }
297                         }
298                         *value += (double)tmpscore * (double)efficient;
299 //                      reporterr(       "val in _gapnomi = %f\n", *value );
300                 }
301         }
302 #if 0
303         fprintf( stdout, "###score = %f\n", score );
304 #endif
305 #if DEBUG
306         reporterr(       "score in intergroup_score = %f\n", score );
307 #endif
308 //      return( score );
309 }
310
311 void intergroup_score_multimtx( int **whichmtx, double ***scoringmatrices, char **seq1, char **seq2, double *eff1, double *eff2, int clus1, int clus2, int len, double *value )
312 {
313         int i, j, k, c;
314         int len2 = len - 2;
315         int mn1, mn2;
316         double tmpscore;
317         char *mseq1, *mseq2;
318         double efficient;
319         int gapnum = amino_n['-'];
320
321         double gaptmpscore;
322         double gapscore = 0.0;
323
324 //      reporterr(       "#### in intergroup_score\n" );
325
326 //      totaleff1 = 0.0; for( i=0; i<clus1; i++ ) totaleff1 += eff1[i];
327 //      totaleff2 = 0.0; for( i=0; i<clus2; i++ ) totaleff2 += eff2[i];
328
329 //      reporterr(       "\n intergroup_score_multimtx ..." );
330         *value = 0.0;
331         for( i=0; i<clus1; i++ ) 
332         {
333                 for( j=0; j<clus2; j++ ) 
334                 {
335                         efficient = eff1[i] * eff2[j]; /* \e$B$J$<$+G[Ns$r;H$o$J$$$H$*$+$7$/$J$k\e(B, \e$BB?J,%P%0\e(B */
336                         c = whichmtx[i][j];
337                         mseq1 = seq1[i];
338                         mseq2 = seq2[j];
339                         tmpscore = 0.0;
340                         gaptmpscore = 0.0;
341                         for( k=0; k<len; k++ ) 
342                         {
343                                 mn1 = amino_n[(unsigned char)(mseq1[k])];
344                                 mn2 = amino_n[(unsigned char)(mseq2[k])];
345                                 if( mn1 == gapnum && mn2 == gapnum ) continue;
346                                 tmpscore += (double)scoringmatrices[c][mn1][mn2];
347 //                              tmpscore += (double)scoringmtx[mn1][mn2];
348         
349                                 if( mn1 == gapnum ) 
350                                 {
351                                         tmpscore += (double)penalty;
352                                         gaptmpscore += (double)penalty;
353 //                                      tmpscore += (double)scoringmtx[mn1][mn2];
354                                         tmpscore += (double)scoringmatrices[c][mn1][mn2];
355                                         while( (mn1=amino_n[(unsigned char)mseq1[++k]]) == gapnum )
356                                                 tmpscore += (double)scoringmatrices[c][mn1][mn2];
357 //                                              tmpscore += (double)scoringmtx[mn1][mn2];
358                                         k--;
359                                         if( k >len2 ) break;
360                                         continue;
361                                 }
362                                 if( mn2 == gapnum )
363                                 {
364                                         tmpscore += (double)penalty;
365                                         gaptmpscore += (double)penalty;
366                                         tmpscore += (double)scoringmatrices[c][mn1][mn2];
367 //                                      tmpscore += (double)scoringmtx[mn1][mn2];
368                                         while( (mn2=amino_n[(unsigned char)mseq2[++k]]) == gapnum )
369                                                 tmpscore += (double)scoringmatrices[c][mn1][mn2];
370 //                                              tmpscore += (double)scoringmtx[mn1][mn2];
371                                         k--;
372                                         if( k > len2 ) break;
373                                         continue;
374                                 }
375                         }
376                         *value += (double)tmpscore * (double)efficient;
377                         gapscore += (double)gaptmpscore * (double)efficient;
378                 }
379         }
380 //      reporterr(       "done." );
381 #if 0
382         reporterr(       "###gapscore = %f\n", gapscore );
383 #endif
384 #if DEBUG
385         reporterr(       "score in intergroup_score = %f\n", score );
386 #endif
387 //      return( score );
388 }
389 void intergroup_score_dynmtx( double **offsetmtx, int scoringmtx[0x80][0x80], char **seq1, char **seq2, double *eff1, double *eff2, int clus1, int clus2, int len, double *value )
390 {
391         int i, j, k;
392         int len2 = len - 2;
393         int ms1, ms2;
394         double tmpscore;
395         char *mseq1, *mseq2;
396         double efficient;
397
398         double gaptmpscore;
399         double gapscore = 0.0;
400
401 //      reporterr(       "#### in intergroup_score\n" );
402
403 //      totaleff1 = 0.0; for( i=0; i<clus1; i++ ) totaleff1 += eff1[i];
404 //      totaleff2 = 0.0; for( i=0; i<clus2; i++ ) totaleff2 += eff2[i];
405
406         reporterr(       "\n intergroup_score_dynmtx ..." );
407         *value = 0.0;
408         for( i=0; i<clus1; i++ ) 
409         {
410                 for( j=0; j<clus2; j++ ) 
411                 {
412                         efficient = eff1[i] * eff2[j]; /* \e$B$J$<$+G[Ns$r;H$o$J$$$H$*$+$7$/$J$k\e(B, \e$BB?J,%P%0\e(B */
413                         mseq1 = seq1[i];
414                         mseq2 = seq2[j];
415                         tmpscore = 0.0;
416                         gaptmpscore = 0.0;
417                         for( k=0; k<len; k++ ) 
418                         {
419                                 ms1 = (int)mseq1[k];
420                                 ms2 = (int)mseq2[k];
421                                 if( ms1 == (int)'-' && ms2 == (int)'-' ) continue;
422                                 tmpscore += (double)scoringmtx[ms1][ms2] + offsetmtx[i][j] * 600;
423 //                              tmpscore += (double)scoringmtx[ms1][ms2];
424         
425                                 if( ms1 == (int)'-' ) 
426                                 {
427                                         tmpscore += (double)penalty;
428                                         gaptmpscore += (double)penalty;
429 //                                      tmpscore += (double)scoringmtx[ms1][ms2];
430                                         tmpscore += (double)scoringmtx[ms1][ms2] + offsetmtx[i][j] * 600;;
431                                         while( (ms1=(int)mseq1[++k]) == (int)'-' )
432                                                 tmpscore += (double)scoringmtx[ms1][ms2] + offsetmtx[i][j] * 600;
433 //                                              tmpscore += (double)scoringmtx[ms1][ms2];
434                                         k--;
435                                         if( k >len2 ) break;
436                                         continue;
437                                 }
438                                 if( ms2 == (int)'-' )
439                                 {
440                                         tmpscore += (double)penalty;
441                                         gaptmpscore += (double)penalty;
442                                         tmpscore += (double)scoringmtx[ms1][ms2] + offsetmtx[i][j] * 600;
443 //                                      tmpscore += (double)scoringmtx[ms1][ms2];
444                                         while( (ms2=(int)mseq2[++k]) == (int)'-' )
445                                                 tmpscore += (double)scoringmtx[ms1][ms2] + offsetmtx[i][j] * 600;
446 //                                              tmpscore += (double)scoringmtx[ms1][ms2];
447                                         k--;
448                                         if( k > len2 ) break;
449                                         continue;
450                                 }
451                         }
452                         *value += (double)tmpscore * (double)efficient;
453                         gapscore += (double)gaptmpscore * (double)efficient;
454                 }
455         }
456         reporterr(       "done." );
457 #if 0
458         reporterr(       "###gapscore = %f\n", gapscore );
459 #endif
460 #if DEBUG
461         reporterr(       "score in intergroup_score = %f\n", score );
462 #endif
463 //      return( score );
464 }
465 void intergroup_score( char **seq1, char **seq2, double *eff1, double *eff2, int clus1, int clus2, int len, double *value )
466 {
467         int i, j, k;
468         int len2 = len - 2;
469         unsigned char ms1, ms2;
470         double tmpscore;
471         char *mseq1, *mseq2;
472         double efficient;
473
474         double gaptmpscore;
475         double gapscore = 0.0;
476
477 //      reporterr(       "#### in intergroup_score\n" );
478
479 //      totaleff1 = 0.0; for( i=0; i<clus1; i++ ) totaleff1 += eff1[i];
480 //      totaleff2 = 0.0; for( i=0; i<clus2; i++ ) totaleff2 += eff2[i];
481
482         *value = 0.0;
483         for( i=0; i<clus1; i++ ) 
484         {
485                 for( j=0; j<clus2; j++ ) 
486                 {
487                         efficient = eff1[i] * eff2[j]; /* \e$B$J$<$+G[Ns$r;H$o$J$$$H$*$+$7$/$J$k\e(B, \e$BB?J,%P%0\e(B */
488                         mseq1 = seq1[i];
489                         mseq2 = seq2[j];
490                         tmpscore = 0.0;
491                         gaptmpscore = 0.0;
492                         for( k=0; k<len; k++ ) 
493                         {
494                                 ms1 = (unsigned char)mseq1[k];
495                                 ms2 = (unsigned char)mseq2[k];
496                                 if( ms1 == '-' && ms2 == '-' ) continue;
497 //                              tmpscore += (double)amino_dis[ms1][ms2];
498                                 tmpscore += (double)amino_dis_consweight_multi[ms1][ms2];
499         
500                                 if( ms1 == '-' ) 
501                                 {
502                                         tmpscore += (double)penalty;
503                                         gaptmpscore += (double)penalty;
504 //                                      tmpscore += (double)amino_dis[ms1][ms2];
505                                         tmpscore += (double)amino_dis_consweight_multi[ms1][ms2];
506                                         while( (ms1=(unsigned char)mseq1[++k]) == '-' )
507 //                                              tmpscore += (double)amino_dis[ms1][ms2];
508                                                 tmpscore += (double)amino_dis_consweight_multi[ms1][ms2];
509                                         k--;
510                                         if( k >len2 ) break;
511                                         continue;
512                                 }
513                                 if( ms2 == '-' )
514                                 {
515                                         tmpscore += (double)penalty;
516                                         gaptmpscore += (double)penalty;
517 //                                      tmpscore += (double)amino_dis[ms1][ms2];
518                                         tmpscore += (double)amino_dis_consweight_multi[ms1][ms2];
519                                         while( (ms2=(unsigned char)mseq2[++k]) == '-' )
520 //                                              tmpscore += (double)amino_dis[ms1][ms2];
521                                                 tmpscore += (double)amino_dis_consweight_multi[ms1][ms2];
522                                         k--;
523                                         if( k > len2 ) break;
524                                         continue;
525                                 }
526                         }
527                         *value += (double)tmpscore * (double)efficient;
528                         gapscore += (double)gaptmpscore * (double)efficient;
529                 }
530         }
531 #if 0
532         reporterr(       "###gapscore = %f\n", gapscore );
533 #endif
534 #if DEBUG
535         reporterr(       "score in intergroup_score = %f\n", score );
536 #endif
537 //      return( score );
538 }
539
540 double score_calc5( char **seq, int s, double **eff, int ex )  /* method 3 deha nai */
541 {
542     int i, j, k;
543     double c;
544     int len = strlen( seq[0] );
545     double score;
546     double tmpscore;
547     char *mseq1, *mseq2;
548     double efficient;
549 #if DEBUG
550         FILE *fp;
551 #endif
552
553     score = 0.0;
554     c = 0.0;
555
556         for( i=0; i<s; i++ ) 
557         {
558                 
559                         if( i == ex ) continue;
560             efficient = eff[i][ex];
561             mseq1 = seq[i];
562             mseq2 = seq[ex];
563             tmpscore = 0.0;
564             for( k=0; k<len; k++ )
565             {
566                 if( mseq1[k] == '-' && mseq2[k] == '-' ) continue;
567                 tmpscore += amino_dis[(unsigned char)mseq1[k]][(unsigned char)mseq2[k]];
568
569                 if( mseq1[k] == '-' )
570                 {
571                     tmpscore += penalty;
572                     while( mseq1[++k] == '-' )
573                         tmpscore += amino_dis[(unsigned char)mseq1[k]][(unsigned char)mseq2[k]];
574                     k--;
575                     if( k > len-2 ) break;
576                     continue;
577                 }
578                 if( mseq2[k] == '-' )
579                 {
580                     tmpscore += penalty;
581                     while( mseq2[++k] == '-' )
582                         tmpscore += amino_dis[(unsigned char)mseq1[k]][(unsigned char)mseq2[k]];
583                     k--;
584                     if( k > len-2 ) break;
585                     continue;
586                 }
587             }
588             score += (double)tmpscore * efficient;
589 /*
590                         fprintf( stdout, "%d-%d tmpscore = %f, eff = %f, tmpscore*eff = %f\n", i, ex, tmpscore, efficient, tmpscore*efficient );
591 */
592         }
593         /*
594         fprintf( stdout, "total score = %f\n", score );
595         */
596
597     for( i=0; i<s-1; i++ )
598     {
599         for( j=i+1; j<s; j++ )
600         {
601                         if( i == ex || j == ex ) continue;
602
603             efficient = eff[i][j];
604             mseq1 = seq[i];
605             mseq2 = seq[j];
606             tmpscore = 0.0;
607             for( k=0; k<len; k++ )
608             {
609                 if( mseq1[k] == '-' && mseq2[k] == '-' ) continue;
610                 tmpscore += amino_dis[(unsigned char)mseq1[k]][(unsigned char)mseq2[k]];
611
612                 if( mseq1[k] == '-' )
613                 {
614                     tmpscore += penalty;
615                     while( mseq1[++k] == '-' )
616                         tmpscore += amino_dis[(unsigned char)mseq1[k]][(unsigned char)mseq2[k]];
617                     k--;
618                     if( k > len-2 ) break;
619                     continue;
620                 }
621                 if( mseq2[k] == '-' )
622                 {
623                     tmpscore += penalty;
624                     while( mseq2[++k] == '-' )
625                         tmpscore += amino_dis[(unsigned char)mseq1[k]][(unsigned char)mseq2[k]];
626                     k--;
627                     if( k > len-2 ) break;
628                     continue;
629                 }
630             }
631             score += (double)tmpscore * efficient;
632         }
633     }
634 /*
635         reporterr(       "score in score_calc5 = %f\n", score );
636 */
637     return( (double)score );
638 /*
639
640 fprintf( trap_g, "score by fast = %f\n", (double)score );
641
642 tmpscore = score = 0.0;
643         for( i=0; i<s; i++ ) 
644         {
645                 if( i == ex ) continue;
646                 tmpscore = Cscore_m_1( seq, i, eff );
647                 fprintf( stdout, "%d %f\n", i, tmpscore );
648
649                 score += tmpscore;
650         }
651         tmpscore = Cscore_m_1( seq, ex, eff );
652         fprintf( stdout, "ex%d %f\n", i, tmpscore );
653         score += tmpscore;
654
655         return( score );
656 */
657 }
658
659
660         
661 double score_calc4( char **seq, int s, double **eff, int ex )  /* method 3 deha nai */
662 {
663     int i, j, k;
664         double c;
665     int len = strlen( seq[0] );
666     double score;
667     long tmpscore;
668         char *mseq1, *mseq2;
669         double efficient;
670
671     score = 0.0;
672         c = 0.0;
673 /*
674         printf( "in score_calc4\n" );
675         for( i=0; i<s; i++ )
676         {
677                 for( j=0; j<s; j++ ) 
678                 {
679                         printf( "% 5.3f", eff[i][j] ); 
680                 }
681                 printf( "\n" );
682                 
683         }
684 */
685     for( i=0; i<s-1; i++ )
686     {
687         for( j=i+1; j<s; j++ )
688         {
689                         efficient = eff[i][j];
690                         if( mix == 1 ) efficient = 1.0;
691                         /*
692                         printf( "weight for %d v.s. %d = %f\n", i, j, efficient );
693                         */
694             mseq1 = seq[i];
695             mseq2 = seq[j];
696             tmpscore = 0;
697             for( k=0; k<len; k++ )
698             {
699                 if( mseq1[k] == '-' && mseq2[k] == '-' ) continue;
700                 tmpscore += amino_dis[(unsigned char)mseq1[k]][(unsigned char)mseq2[k]] + 400 * !scoremtx ;
701
702                                 c += efficient;
703
704                 if( mseq1[k] == '-' )
705                 {
706                     tmpscore += penalty - n_dis[24][0];
707                     while( mseq1[++k] == '-' )
708                                                 ;
709                     k--;
710                     if( k > len-2 ) break;
711                     continue;
712                 }
713                 if( mseq2[k] == '-' )
714                 {
715                     tmpscore += penalty - n_dis[24][0];
716                     while( mseq2[++k] == '-' )
717                                                 ;
718                     k--;
719                     if( k > len-2 ) break;
720                     continue;
721                 }
722             }
723                         /*
724                         if( x == 65 ) printf( "i=%d j=%d tmpscore=%d l=%d\n", i, j, tmpscore, len );
725                         */
726             score += (double)tmpscore * efficient;
727         }
728     }
729     score /= c;
730     return( (double)score );
731 }
732
733
734
735 void upg2( int nseq, double **eff, int ***topol, double **len )
736 {
737     int i, j, k;
738         double tmplen[M];
739
740     static char **pair = NULL;
741
742         if( !pair )
743         {
744                 pair = AllocateCharMtx( njob, njob );
745         }
746
747         for( i=0; i<nseq; i++ ) tmplen[i] = 0.0;
748     for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ ) pair[i][j] = 0;
749     for( i=0; i<nseq; i++ ) pair[i][i] = 1;
750
751     for( k=0; k<nseq-1; k++ )
752     {
753         double minscore = 9999.0;
754         int im = -1, jm = -1;
755         int count;
756
757         for( i=0; i<nseq-1; i++ ) for( j=i+1; j<nseq; j++ )
758         {
759             if( eff[i][j] < minscore )
760             {
761                 minscore = eff[i][j];
762                 im = i; jm = j;
763             }
764         }
765         for( i=0, count=0; i<nseq; i++ )
766             if( pair[im][i] > 0 )
767             {
768                 topol[k][0][count] = i;
769                 count++;
770             }
771         topol[k][0][count] = -1;
772         for( i=0, count=0; i<nseq; i++ )
773             if( pair[jm][i] > 0 )
774             {
775                 topol[k][1][count] = i;
776                 count++;
777             }
778         topol[k][1][count] = -1;
779
780                 len[k][0] = minscore / 2.0 - tmplen[im];
781                 len[k][1] = minscore / 2.0 - tmplen[jm];
782
783                 tmplen[im] = minscore / 2.0;
784
785         for( i=0; i<nseq; i++ ) pair[im][i] += ( pair[jm][i] > 0 );
786         for( i=0; i<nseq; i++ ) pair[jm][i] = 0;
787
788         for( i=0; i<nseq; i++ )
789         {
790             if( i != im && i != jm )
791             {
792                 eff[MIN(i,im)][MAX(i,im)] =
793                 ( eff[MIN(i,im)][MAX(i,im)] + eff[MIN(i,jm)][MAX(i,jm)] ) / 2.0;
794                 eff[MIN(i,jm)][MAX(i,jm)] = 9999.0;
795             }
796             eff[im][jm] = 9999.0;
797         }
798 #if DEBUG
799         printf( "STEP-%03d:\n", k+1 );
800                 printf( "len0 = %f\n", len[k][0] );
801         for( i=0; topol[k][0][i]>-1; i++ ) printf( " %03d", topol[k][0][i] );
802         printf( "\n" );
803                 printf( "len1 = %f\n", len[k][1] );
804         for( i=0; topol[k][1][i]>-1; i++ ) printf( " %03d", topol[k][1][i] );
805         printf( "\n" );
806 #endif
807     }
808 }
809
810 #define BLOCKSIZE 100
811 #define LARGEBLOCKSIZE 100
812
813 typedef struct _generaltdistarrthread_arg
814 {
815         int para;
816         int njob;
817 //      int thread_no;
818         int m;
819         int *nlen;
820         char **seq;
821         int **skiptable;
822         int **pointt;
823         int *ttable;
824         int *tselfscore;
825         int *posshared;
826         int *joblist;
827         double *result;
828 #ifdef enablemultithread
829         pthread_mutex_t *mutex;
830 #endif
831 } generaldistarrthread_arg_t;
832
833 static void *generalkmerdistarrthread( void *arg ) // enablemultithread == 0 demo tsukau
834 {
835         generaldistarrthread_arg_t *targ = (generaldistarrthread_arg_t *)arg;
836         int njob = targ->njob;
837         int para = targ->para;
838         int m = targ->m;
839         int *nlen = targ->nlen;
840         int **pointt = targ->pointt;
841         int *ttable = targ->ttable;
842         int *tselfscore = targ->tselfscore;
843         int *joblist = targ->joblist;
844         int *posshared = targ->posshared;
845         double *result = targ->result;
846 //      double **partmtx = targ->partmtx;
847         int i, posinjoblist, n;
848
849 //                      for( acpti=ac; acpti!=NULL; acpti=acpti->next )
850
851                 while( 1 )
852                 {
853 #ifdef enablemultithread
854                         if( para ) pthread_mutex_lock( targ->mutex );
855 #endif 
856                         if( *posshared >= njob ) // block no toki >=
857                         {
858 #ifdef enablemultithread
859                                 if( para ) pthread_mutex_unlock( targ->mutex );
860 #endif
861                                 commonsextet_p( NULL, NULL );
862                                 return( NULL );
863                         }
864                         posinjoblist = *posshared;
865                         *posshared += LARGEBLOCKSIZE;
866 #ifdef enablemultithread
867                         if( para ) pthread_mutex_unlock( targ->mutex );
868 #endif
869
870                         for( n=0; n<LARGEBLOCKSIZE&&posinjoblist<njob; n++ )
871                         {
872                                 i = joblist[posinjoblist++];
873         
874 //                              if( i == m ) continue; // iranai
875                 
876                                 result[i] = distcompact( nlen[m], nlen[i], ttable, pointt[i], tselfscore[m], tselfscore[i] );
877                 
878                         }
879                 }
880 }
881
882 static void *generalmsadistarrthread( void *arg ) // enablemultithread == 0 demo tsukau
883 {
884         generaldistarrthread_arg_t *targ = (generaldistarrthread_arg_t *)arg;
885         int njob = targ->njob;
886         int para = targ->para;
887         int m = targ->m;
888         int *tselfscore = targ->tselfscore;
889         char **seq = targ->seq;
890         int **skiptable = targ->skiptable;
891         int *joblist = targ->joblist;
892         int *posshared = targ->posshared;
893         double *result = targ->result;
894 //      double **partmtx = targ->partmtx;
895         int i, posinjoblist, n;
896
897 //                      for( acpti=ac; acpti!=NULL; acpti=acpti->next )
898
899                 while( 1 )
900                 {
901 #ifdef enablemultithread
902                         if( para ) pthread_mutex_lock( targ->mutex );
903 #endif 
904                         if( *posshared >= njob ) // block no toki >=
905                         {
906 #ifdef enablemultithread
907                                 if( para ) pthread_mutex_unlock( targ->mutex );
908 #endif
909                                 return( NULL );
910                         }
911                         posinjoblist = *posshared;
912                         *posshared += LARGEBLOCKSIZE;
913 #ifdef enablemultithread
914                         if( para ) pthread_mutex_unlock( targ->mutex );
915 #endif
916
917                         for( n=0; n<LARGEBLOCKSIZE&&posinjoblist<njob; n++ )
918                         {
919                                 i = joblist[posinjoblist++];
920         
921 //                              if( i == m ) continue; // iranai
922                 
923                                 result[i] = distcompact_msa( seq[m], seq[i], skiptable[m], skiptable[i], tselfscore[m], tselfscore[i] );
924                 
925                         }
926                 }
927 }
928
929 #if 1
930 static void kmerresetnearest( int nseq, Bchain *acpt, double **distfrompt, double *mindisfrompt, int *nearestpt, int pos, int *tselfscore, int **pointt, int *nlen, int *singlettable1, double *result, int *joblist )
931 {
932         int i, j;
933         double tmpdouble;
934         double mindisfrom;
935         int nearest;
936 //      double **effptpt;
937         Bchain *acptj;
938 //      double *result;
939 //      int *joblist;
940
941         mindisfrom = 999.9;
942         nearest = -1;
943
944
945 //      reporterr( "resetnearest..\r" );
946 //      printf( "[%d], %f, dist=%d ->", pos, *distfrompt, *nearestpt );
947
948 //      mindisfrom = 999.9;
949 //      nearest = -1;
950
951
952 //      result = calloc( nseq, sizeof( double ) );
953 //      joblist = calloc( nseq, sizeof( int ) );
954
955
956         for( acptj=(acpt+pos)->next,j=0; acptj!=NULL; acptj=acptj->next ) // setnearest ni awaseru
957         {
958                 i = acptj->pos;
959 //              if( i == pos ) continue;
960
961                 if( distfrompt[pos] )
962                 {
963                         tmpdouble = result[i] = distfrompt[pos][i];
964                         if( tmpdouble < mindisfrom )
965                         {
966                                 mindisfrom = tmpdouble;
967                                 nearest = i;
968                         }
969                 }
970                 else if( distfrompt[i] )
971                 {
972                         tmpdouble = result[i] = distfrompt[i][pos];
973                         if( tmpdouble < mindisfrom )
974                         {
975                                 mindisfrom = tmpdouble;
976                                 nearest = i;
977                         }
978                 }
979                 else
980                         joblist[j++] = i;
981         }
982
983         for( acptj=acpt; (acptj&&acptj->pos!=pos); acptj=acptj->next ) // setnearest ni awaseru
984         {
985                 i = acptj->pos;
986 //              if( i == pos ) continue;
987
988                 if( distfrompt[pos] )
989                 {
990                         tmpdouble = result[i] = distfrompt[pos][i];
991                         if( tmpdouble < mindisfrom )
992                         {
993                                 mindisfrom = tmpdouble;
994                                 nearest = i;
995                         }
996                 }
997                 else if( distfrompt[i] )
998                 {
999                         tmpdouble = result[i] = distfrompt[i][pos];
1000                         if( tmpdouble < mindisfrom )
1001                         {
1002                                 mindisfrom = tmpdouble;
1003                                 nearest = i;
1004                         }
1005                 }
1006                 else
1007                         joblist[j++] = i;
1008         }
1009
1010
1011         if( j )
1012         {
1013 //              reporterr( "resetting in parallel!! j=%d\n", j );
1014 //              exit( 1 );
1015                 int posshared;
1016                 generaldistarrthread_arg_t *targ;
1017
1018 #ifdef enablemultithread
1019                 if( nthread )
1020                 {
1021                         pthread_t *handle;
1022                         pthread_mutex_t mutex;
1023
1024                         targ = calloc( nthread, sizeof( generaldistarrthread_arg_t ) );
1025                         handle = calloc( nthread, sizeof( pthread_t ) );
1026                         posshared = 0;
1027                         pthread_mutex_init( &mutex, NULL );
1028                         for( i=0; i<nthread; i++ )
1029                         {
1030                                 targ[i].para = 1;
1031                                 targ[i].njob = j;
1032                                 targ[i].m = pos;
1033                                 targ[i].tselfscore = tselfscore;
1034                                 targ[i].nlen = nlen;
1035                                 targ[i].pointt = pointt;
1036                                 targ[i].ttable = singlettable1;
1037                                 targ[i].joblist = joblist;
1038                                 targ[i].result = result;
1039                                 targ[i].posshared = &posshared;
1040                                 targ[i].mutex = &mutex;
1041         
1042                                 pthread_create( handle+i, NULL, generalkmerdistarrthread, (void *)(targ+i) );
1043                         }
1044                                 
1045                         for( j=0; j<nthread; j++ ) pthread_join( handle[j], NULL );
1046                         pthread_mutex_destroy( &mutex );
1047                         free( handle );
1048                 }
1049                 else
1050 #endif
1051                 {
1052                         targ = calloc( 1, sizeof( generaldistarrthread_arg_t ) );
1053                         posshared = 0;
1054                         {
1055                                 targ[0].para = 0;
1056                                 targ[0].njob = j;
1057                                 targ[0].m = pos;
1058                                 targ[0].tselfscore = tselfscore;
1059                                 targ[0].nlen = nlen;
1060                                 targ[0].pointt = pointt;
1061                                 targ[0].ttable = singlettable1;
1062                                 targ[0].joblist = joblist;
1063                                 targ[0].result = result;
1064                                 targ[0].posshared = &posshared;
1065         
1066                                 generalkmerdistarrthread( targ );
1067                         }
1068                 }
1069                 free( targ );
1070 // sukoshi muda
1071                 for( acptj=(acpt+pos)->next; acptj!=NULL; acptj=acptj->next ) // setnearest ni awaseru
1072                 {
1073                         j = acptj->pos;
1074                         tmpdouble = result[j];
1075                         if( tmpdouble < mindisfrom )
1076                         {
1077                                 mindisfrom = tmpdouble;
1078                                 nearest = j;
1079                         }
1080                 }
1081         
1082                 for( acptj=acpt; (acptj&&acptj->pos!=pos); acptj=acptj->next ) // setnearest ni awaseru
1083                 {
1084                         j = acptj->pos;
1085                         tmpdouble = result[j];
1086                         if( tmpdouble < mindisfrom )
1087                         {
1088                                 mindisfrom = tmpdouble;
1089                                 nearest = j;
1090                         }
1091                 }
1092         }
1093
1094
1095         *mindisfrompt = mindisfrom;
1096         *nearestpt = nearest;
1097
1098 //      free( joblist );
1099 //      free( result );
1100 }
1101
1102 #else
1103 static void kmerresetnearest( int nseq, Bchain *acpt, double **distfrompt, double *mindisfrompt, int *nearestpt, int pos, int *tselfscore, int **pointt, int *nlen, int *singlettable1, double *resultnotused, int *joblistnotused )
1104 {
1105         int j;
1106         double tmpdouble;
1107         double mindisfrom;
1108         int nearest;
1109 //      double **effptpt;
1110         Bchain *acptj;
1111
1112         mindisfrom = 999.9;
1113         nearest = -1;
1114
1115
1116 //      reporterr( "resetnearest..\r" );
1117 //      printf( "[%d], %f, dist=%d ->", pos, *distfrompt, *nearestpt );
1118
1119 //      mindisfrom = 999.9;
1120 //      nearest = -1;
1121
1122
1123         for( acptj=(acpt+pos)->next; acptj!=NULL; acptj=acptj->next ) // setnearest ni awaseru
1124         {
1125                 j = acptj->pos;
1126
1127                 if( distfrompt[pos] )
1128                         tmpdouble=distfrompt[pos][j];
1129                 else if( distfrompt[j] )
1130                         tmpdouble=distfrompt[j][pos];
1131 //              else if( seq )
1132 //                      tmpdouble=distcompact_msa( seq[pos], seq[j], skiptable[pos], skiptable[j], tselfscore[pos], tselfscore[j] );
1133                 else
1134                         tmpdouble=distcompact( nlen[pos], nlen[j], singlettable1, pointt[j], tselfscore[pos], tselfscore[j] );
1135                                 
1136
1137                 if( tmpdouble < mindisfrom )
1138                 {
1139                         mindisfrom = tmpdouble;
1140                         nearest = j;
1141                 }
1142         }
1143
1144         for( acptj=acpt; (acptj&&acptj->pos!=pos); acptj=acptj->next ) // setnearest ni awaseru
1145         {
1146                 j = acptj->pos;
1147
1148                 if( distfrompt[pos] )
1149                         tmpdouble=distfrompt[pos][j];
1150                 else if( distfrompt[j] )
1151                         tmpdouble=distfrompt[j][pos];
1152 //              else if( seq )
1153 //                      tmpdouble=distcompact_msa( seq[pos], seq[j], skiptable[pos], skiptable[j], tselfscore[pos], tselfscore[j] );
1154                 else
1155                         tmpdouble=distcompact( nlen[pos], nlen[j], singlettable1, pointt[j], tselfscore[pos], tselfscore[j] );
1156                                 
1157
1158
1159                 if( tmpdouble < mindisfrom )
1160                 {
1161                         mindisfrom = tmpdouble;
1162                         nearest = j;
1163                 }
1164         }
1165 //      printf( "mindisfrom = %f\n", mindisfrom );
1166
1167         *mindisfrompt = mindisfrom;
1168         *nearestpt = nearest;
1169 }
1170 #endif
1171
1172 #if 1
1173 static void msaresetnearest( int nseq, Bchain *acpt, double **distfrompt, double *mindisfrompt, int *nearestpt, int pos, char **seq, int **skiptable, int *tselfscore, double *result, int *joblist )
1174 {
1175         int i, j;
1176         double tmpdouble;
1177         double mindisfrom;
1178         int nearest;
1179 //      double **effptpt;
1180         Bchain *acptj;
1181 //      double *result;
1182 //      int *joblist;
1183
1184         mindisfrom = 999.9;
1185         nearest = -1;
1186
1187
1188 //      reporterr( "resetnearest..\r" );
1189 //      printf( "[%d], %f, dist=%d ->", pos, *distfrompt, *nearestpt );
1190
1191 //      mindisfrom = 999.9;
1192 //      nearest = -1;
1193
1194
1195 //      result = calloc( nseq, sizeof( double ) );
1196 //      joblist = calloc( nseq, sizeof( int ) );
1197
1198 //      for( acptj=acpt,j=0; acptj!=NULL; acptj=acptj->next ) // setnearest ni awaseru
1199         for( acptj=(acpt+pos)->next,j=0; acptj!=NULL; acptj=acptj->next ) // setnearest ni awaseru
1200         {
1201                 i = acptj->pos;
1202 //              if( i == pos ) continue;
1203
1204                 if( distfrompt[pos] )
1205                 {
1206                         tmpdouble = result[i] = distfrompt[pos][i];
1207                         if( tmpdouble < mindisfrom )
1208                         {
1209                                 mindisfrom = tmpdouble;
1210                                 nearest = i;
1211                         }
1212                 }
1213                 else if( distfrompt[i] )
1214                 {
1215                         tmpdouble = result[i] = distfrompt[i][pos];
1216                         if( tmpdouble < mindisfrom )
1217                         {
1218                                 mindisfrom = tmpdouble;
1219                                 nearest = i;
1220                         }
1221                 }
1222                 else
1223                         joblist[j++] = i;
1224         }
1225
1226         for( acptj=acpt; (acptj&&acptj->pos!=pos); acptj=acptj->next ) // setnearest ni awaseru
1227         {
1228                 i = acptj->pos;
1229 //              if( i == pos ) continue;
1230
1231                 if( distfrompt[pos] )
1232                 {
1233                         tmpdouble = result[i] = distfrompt[pos][i];
1234                         if( tmpdouble < mindisfrom )
1235                         {
1236                                 mindisfrom = tmpdouble;
1237                                 nearest = i;
1238                         }
1239                 }
1240                 else if( distfrompt[i] )
1241                 {
1242                         tmpdouble = result[i] = distfrompt[i][pos];
1243                         if( tmpdouble < mindisfrom )
1244                         {
1245                                 mindisfrom = tmpdouble;
1246                                 nearest = i;
1247                         }
1248                 }
1249                 else
1250                         joblist[j++] = i;
1251         }
1252
1253
1254         if( j )
1255         {
1256 //              reporterr( "resetting in parallel!! j=%d\r", j );
1257 //              exit( 1 );
1258                 int posshared;
1259                 generaldistarrthread_arg_t *targ;
1260                 posshared = 0;
1261
1262 #ifdef enablemultithread
1263                 if( nthread )
1264                 {
1265                         pthread_t *handle;
1266                         pthread_mutex_t mutex;
1267                         targ = calloc( nthread, sizeof( generaldistarrthread_arg_t ) );
1268                         handle = calloc( nthread, sizeof( pthread_t ) );
1269                         pthread_mutex_init( &mutex, NULL );
1270                         for( i=0; i<nthread; i++ )
1271                         {
1272                                 targ[i].para = 1;
1273                                 targ[i].njob = j;
1274                                 targ[i].m = pos;
1275                                 targ[i].tselfscore = tselfscore;
1276                                 targ[i].seq = seq;
1277                                 targ[i].skiptable = skiptable;
1278                                 targ[i].joblist = joblist;
1279                                 targ[i].result = result;
1280                                 targ[i].posshared = &posshared;
1281                                 targ[i].mutex = &mutex;
1282         
1283                                 pthread_create( handle+i, NULL, generalmsadistarrthread, (void *)(targ+i) );
1284                         }
1285                         for( j=0; j<nthread; j++ ) pthread_join( handle[j], NULL );
1286                         pthread_mutex_destroy( &mutex );
1287                         free( handle );
1288                 }
1289                 else
1290 #endif
1291                 {
1292                         targ = calloc( 1, sizeof( generaldistarrthread_arg_t ) );
1293                         {
1294                                 targ[0].para = 0;
1295                                 targ[0].njob = j;
1296                                 targ[0].m = pos;
1297                                 targ[0].tselfscore = tselfscore;
1298                                 targ[0].seq = seq;
1299                                 targ[0].skiptable = skiptable;
1300                                 targ[0].joblist = joblist;
1301                                 targ[0].result = result;
1302                                 targ[0].posshared = &posshared;
1303         
1304                                 generalmsadistarrthread( targ );
1305                         }
1306                 }
1307                 free( targ );
1308 // sukoshi muda
1309                 for( acptj=(acpt+pos)->next; acptj!=NULL; acptj=acptj->next ) // setnearest ni awaseru
1310                 {
1311                         j = acptj->pos;
1312                         tmpdouble = result[j];
1313                         if( tmpdouble < mindisfrom )
1314                         {
1315                                 mindisfrom = tmpdouble;
1316                                 nearest = j;
1317                         }
1318                 }
1319         
1320                 for( acptj=acpt; (acptj&&acptj->pos!=pos); acptj=acptj->next ) // setnearest ni awaseru
1321                 {
1322                         j = acptj->pos;
1323                         tmpdouble = result[j];
1324                         if( tmpdouble < mindisfrom )
1325                         {
1326                                 mindisfrom = tmpdouble;
1327                                 nearest = j;
1328                         }
1329                 }
1330
1331         }
1332
1333 //      printf( "mindisfrom = %f\n", mindisfrom );
1334
1335         *mindisfrompt = mindisfrom;
1336         *nearestpt = nearest;
1337
1338 //      free( joblist );
1339 //      free( result );
1340 }
1341 #else
1342 static void msaresetnearest( int nseq, Bchain *acpt, double **distfrompt, double *mindisfrompt, int *nearestpt, int pos, char **seq, int **skiptable, int *tselfscore, double *resultnotused, int *joblistnotused )
1343 {
1344         int j;
1345         double tmpdouble;
1346         double mindisfrom;
1347         int nearest;
1348 //      double **effptpt;
1349         Bchain *acptj;
1350
1351         mindisfrom = 999.9;
1352         nearest = -1;
1353
1354
1355 //      reporterr( "resetnearest..\r" );
1356 //      printf( "[%d], %f, dist=%d ->", pos, *distfrompt, *nearestpt );
1357
1358 //      mindisfrom = 999.9;
1359 //      nearest = -1;
1360
1361
1362         for( acptj=(acpt+pos)->next; acptj!=NULL; acptj=acptj->next ) // setnearest ni awaseru
1363         {
1364                 j = acptj->pos;
1365
1366                 if( distfrompt[pos] )
1367                         tmpdouble=distfrompt[pos][j];
1368                 else if( distfrompt[j] )
1369                         tmpdouble=distfrompt[j][pos];
1370                 else
1371                         tmpdouble=distcompact_msa( seq[pos], seq[j], skiptable[pos], skiptable[j], tselfscore[pos], tselfscore[j] );
1372 //              else
1373 //                      tmpdouble=distcompact( nlen[pos], nlen[j], singlettable1, pointt[j], tselfscore[pos], tselfscore[j] );
1374                                 
1375
1376                 if( tmpdouble < mindisfrom )
1377                 {
1378                         mindisfrom = tmpdouble;
1379                         nearest = j;
1380                 }
1381         }
1382
1383         for( acptj=acpt; (acptj&&acptj->pos!=pos); acptj=acptj->next ) // setnearest ni awaseru
1384         {
1385                 j = acptj->pos;
1386
1387                 if( distfrompt[pos] )
1388                         tmpdouble=distfrompt[pos][j];
1389                 else if( distfrompt[j] )
1390                         tmpdouble=distfrompt[j][pos];
1391                 else
1392                         tmpdouble=distcompact_msa( seq[pos], seq[j], skiptable[pos], skiptable[j], tselfscore[pos], tselfscore[j] );
1393 //              else
1394 //                      tmpdouble=distcompact( nlen[pos], nlen[j], singlettable1, pointt[j], tselfscore[pos], tselfscore[j] );
1395                                 
1396
1397
1398                 if( tmpdouble < mindisfrom )
1399                 {
1400                         mindisfrom = tmpdouble;
1401                         nearest = j;
1402                 }
1403         }
1404 //      printf( "mindisfrom = %f\n", mindisfrom );
1405
1406         *mindisfrompt = mindisfrom;
1407         *nearestpt = nearest;
1408 }
1409 #endif
1410
1411 static void setnearest( int nseq, Bchain *acpt, double **eff, double *mindisfrompt, int *nearestpt, int pos )
1412 {
1413         int j;
1414         double tmpdouble;
1415         double mindisfrom;
1416         int nearest;
1417 //      double **effptpt;
1418         Bchain *acptj;
1419
1420         mindisfrom = 999.9;
1421         nearest = -1;
1422
1423 //      printf( "[%d], %f, dist=%d ->", pos, *mindisfrompt, *nearestpt );
1424
1425 //      if( (acpt+pos)->next ) effpt = eff[pos]+(acpt+pos)->next->pos-pos;
1426
1427 //      for( j=pos+1; j<nseq; j++ )
1428         for( acptj=(acpt+pos)->next; acptj!=NULL; acptj=acptj->next )
1429         {
1430                 j = acptj->pos;
1431 //              if( (tmpdouble=*effpt++) < *mindisfrompt )
1432                 if( (tmpdouble=eff[pos][j-pos]) < mindisfrom )
1433                 {
1434                         mindisfrom = tmpdouble;
1435                         nearest = j;
1436                 }
1437         }
1438 //      effptpt = eff;
1439 //      for( j=0; j<pos; j++ )
1440         for( acptj=acpt; (acptj&&acptj->pos!=pos); acptj=acptj->next )
1441         {
1442                 j = acptj->pos;
1443 //              if( (tmpdouble=(*effptpt++)[pos-j]) < *mindisfrompt )
1444                 if( (tmpdouble=eff[j][pos-j]) < mindisfrom )
1445                 {
1446                         mindisfrom = tmpdouble;
1447                         nearest = j;
1448                 }
1449         }
1450
1451         *mindisfrompt = mindisfrom;
1452         *nearestpt = nearest;
1453 //      printf( "%f, %d \n", pos, *mindisfrompt, *nearestpt );
1454 }
1455
1456 static void setnearest_double_fullmtx( int nseq, Bchain *acpt, double **eff, double *mindisfrompt, int *nearestpt, int pos )
1457 {
1458         int j;
1459         double tmpdouble;
1460         double **effptpt;
1461         Bchain *acptj;
1462
1463         *mindisfrompt = 999.9;
1464         *nearestpt = -1;
1465
1466 //      if( (acpt+pos)->next ) effpt = eff[pos]+(acpt+pos)->next->pos-pos;
1467
1468 //      for( j=pos+1; j<nseq; j++ )
1469         for( acptj=(acpt+pos)->next; acptj!=NULL; acptj=acptj->next )
1470         {
1471                 j = acptj->pos;
1472 //              if( (tmpdouble=*effpt++) < *mindisfrompt )
1473                 if( (tmpdouble=eff[pos][j]) < *mindisfrompt )
1474                 {
1475                         *mindisfrompt = tmpdouble;
1476                         *nearestpt = j;
1477                 }
1478         }
1479         effptpt = eff;
1480 //      for( j=0; j<pos; j++ )
1481         for( acptj=acpt; (acptj&&acptj->pos!=pos); acptj=acptj->next )
1482         {
1483                 j = acptj->pos;
1484 //              if( (tmpdouble=(*effptpt++)[pos-j]) < *mindisfrompt )
1485                 if( (tmpdouble=eff[j][pos]) < *mindisfrompt )
1486                 {
1487                         *mindisfrompt = tmpdouble;
1488                         *nearestpt = j;
1489                 }
1490         }
1491 }
1492
1493
1494
1495 static void loadtreeoneline( int *ar, double *len, FILE *fp )
1496 {
1497         static char gett[1000];
1498         int res;
1499         char *p;
1500
1501         p = fgets( gett, 999, fp );
1502         if( p == NULL )
1503         {
1504                 reporterr(       "\n\nFormat error (1) in the tree?  It has to be a bifurcated and rooted tree.\n" );
1505                 reporterr(       "Please use newick2mafft.rb to generate a tree file from a newick tree.\n\n" );
1506                 exit( 1 );
1507         }
1508
1509
1510         res = sscanf( gett, "%d %d %lf %lf", ar, ar+1, len, len+1 );
1511         if( res != 4 )
1512         {
1513                 reporterr(       "\n\nFormat error (2) in the tree?  It has to be a bifurcated and rooted tree.\n" );
1514                 reporterr(       "Please use newick2mafft.rb to generate a tree file from a newick tree.\n\n" );
1515                 exit( 1 );
1516         }
1517
1518         ar[0]--;
1519         ar[1]--;
1520
1521         if( ar[0] >= ar[1] )
1522         {
1523                 reporterr(       "\n\nIncorrect guide tree\n" );
1524                 reporterr(       "Please use newick2mafft.rb to generate a tree file from a newick tree.\n\n" );
1525                 exit( 1 );
1526         }
1527
1528
1529 //      reporterr(       "ar[0] = %d, ar[1] = %d\n", ar[0], ar[1] );
1530 //      reporterr(       "len[0] = %f, len[1] = %f\n", len[0], len[1] );
1531 }
1532
1533 void loadtop( int nseq, double **mtx, int ***topol, double **len, char **name, int *nlen, Treedep *dep )
1534 {
1535         int i, j, k, minijm, maxijm;
1536         int *intpt, *intpt2;
1537         int *hist = NULL;
1538         Bchain *ac = NULL;
1539         int im = -1, jm = -1;
1540         Bchain *acjmnext, *acjmprev;
1541         int prevnode;
1542         int *pt1, *pt2, *pt11, *pt22;
1543         int *nmemar;
1544         int nmemim, nmemjm;
1545         char **tree;
1546         char *treetmp;
1547         char *nametmp, *nameptr, *tmpptr; 
1548         char namec;
1549         FILE *fp;
1550         int node[2];
1551         double *height;
1552         double clusterdist;
1553         int mpair, mi, mj;
1554
1555         fp = fopen( "_guidetree", "r" );
1556         if( !fp )
1557         {
1558                 reporterr(       "cannot open _guidetree\n" );
1559                 exit( 1 );
1560         }
1561
1562         if( !hist )
1563         {
1564                 hist = AllocateIntVec( nseq );
1565                 ac = (Bchain *)malloc( nseq * sizeof( Bchain ) );
1566                 nmemar = AllocateIntVec( nseq );
1567 //              treetmp = AllocateCharVec( nseq*50 );
1568                 treetmp = NULL;
1569                 nametmp = AllocateCharVec( 1000 ); // nagasugi
1570 //              tree = AllocateCharMtx( nseq, nseq*50 );
1571                 tree = AllocateCharMtx( nseq, 0 );
1572                 height = AllocateFloatVec( nseq );
1573         }
1574
1575         for( i=0; i<nseq; i++ )
1576         {
1577                 for( j=0; j<999; j++ ) nametmp[j] = 0;
1578                 for( j=0; j<999; j++ ) 
1579                 {
1580                         namec = name[i][j];
1581                         if( namec == 0 )
1582                                 break;
1583                         else if( isalnum( namec ) || namec == '/' || namec == '=' || namec == '-' || namec == '{' || namec == '}' )
1584                                 nametmp[j] = namec;
1585                         else
1586                                 nametmp[j] = '_';
1587                 }
1588                 nametmp[j] = 0;
1589 //              sprintf( tree[i], "%d_l=%d_%.20s", i+1, nlen[i], nametmp+1 );
1590                 if( outnumber )
1591                         nameptr = strstr( nametmp, "_numo_e" ) + 8;
1592                 else
1593                         nameptr = nametmp + 1;
1594
1595                 if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
1596
1597                 tree[i] = calloc( strlen( nametmp )+100, sizeof( char ) ); // suuji no bun de +100
1598                 if( tree[i] == NULL )
1599                 {
1600                         reporterr(       "Cannot allocate tree!\n" );
1601                         exit( 1 );
1602                 }
1603                 sprintf( tree[i], "\n%d_%.900s\n", i+1, nameptr );
1604         }
1605
1606
1607         for( i=0; i<nseq; i++ )
1608         {
1609                 ac[i].next = ac+i+1;
1610                 ac[i].prev = ac+i-1;
1611                 ac[i].pos = i;
1612         }
1613         ac[nseq-1].next = NULL;
1614
1615
1616         for( i=0; i<nseq; i++ ) 
1617         {
1618                 hist[i] = -1;
1619                 nmemar[i] = 1;
1620         }
1621
1622         reporterr(       "\n" );
1623         for( k=0; k<nseq-1; k++ )
1624         {
1625                 if( k % 10 == 0 ) reporterr(       "\r% 5d / %d", k, nseq );
1626 #if 0
1627                 minscore = 999.9;
1628                 for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
1629                 {
1630                         i = acpti->pos;
1631 //                      reporterr(       "k=%d i=%d\n", k, i );
1632                         if( mindisfrom[i] < minscore ) // muscle
1633                         {
1634                                 im = i;
1635                                 minscore = mindisfrom[i];
1636                         }
1637                 }
1638                 jm = nearest[im];
1639                 if( jm < im ) 
1640                 {
1641                         j=jm; jm=im; im=j;
1642                 }
1643 #else
1644                 len[k][0] = len[k][1] = -1.0;
1645                 loadtreeoneline( node, len[k], fp );
1646                 im = node[0];
1647                 jm = node[1];
1648
1649                 if( im > nseq-1 || jm > nseq-1 || tree[im] == NULL || tree[jm] == NULL )
1650                 {
1651                         reporterr(       "\n\nCheck the guide tree.\n" );
1652                         reporterr(       "im=%d, jm=%d\n", im+1, jm+1 );
1653                         reporterr(       "Please use newick2mafft.rb to generate a tree file from a newick tree.\n\n" );
1654                         exit( 1 );
1655                 }
1656
1657 #endif
1658
1659                 prevnode = hist[im];
1660                 if( dep ) dep[k].child0 = prevnode;
1661                 nmemim = nmemar[im];
1662
1663 //              reporterr(       "prevnode = %d, nmemim = %d\n", prevnode, nmemim );
1664
1665                 intpt = topol[k][0] = (int *)realloc( topol[k][0], ( nmemim + 1 ) * sizeof( int ) );
1666                 if( prevnode == -1 )
1667                 {
1668                         *intpt++ = im;
1669                         *intpt = -1;
1670                 }
1671                 else
1672                 {
1673                         pt1 = topol[prevnode][0];
1674                         pt2 = topol[prevnode][1];
1675                         if( *pt1 > *pt2 )
1676                         {
1677                                 pt11 = pt2;
1678                                 pt22 = pt1;
1679                         }
1680                         else
1681                         {
1682                                 pt11 = pt1;
1683                                 pt22 = pt2;
1684                         }
1685                         for( intpt2=pt11; *intpt2!=-1; )
1686                                 *intpt++ = *intpt2++;
1687                         for( intpt2=pt22; *intpt2!=-1; )
1688                                 *intpt++ = *intpt2++;
1689                         *intpt = -1;
1690                 }
1691
1692
1693                 nmemjm = nmemar[jm];
1694                 prevnode = hist[jm];
1695                 if( dep ) dep[k].child1 = prevnode;
1696
1697 //              reporterr(       "prevnode = %d, nmemjm = %d\n", prevnode, nmemjm );
1698
1699                 intpt = topol[k][1] = (int *)realloc( topol[k][1], ( nmemjm + 1 ) * sizeof( int ) );
1700                 if( !intpt )
1701                 {
1702                         reporterr(       "Cannot reallocate topol\n" );
1703                         exit( 1 );
1704                 }
1705                 if( prevnode == -1 )
1706                 {
1707                         *intpt++ = jm;
1708                         *intpt = -1;
1709                 }
1710                 else
1711                 {
1712                         pt1 = topol[prevnode][0];
1713                         pt2 = topol[prevnode][1];
1714                         if( *pt1 > *pt2 )
1715                         {
1716                                 pt11 = pt2;
1717                                 pt22 = pt1;
1718                         }
1719                         else
1720                         {
1721                                 pt11 = pt1;
1722                                 pt22 = pt2;
1723                         }
1724                         for( intpt2=pt11; *intpt2!=-1; )
1725                                 *intpt++ = *intpt2++;
1726                         for( intpt2=pt22; *intpt2!=-1; )
1727                                 *intpt++ = *intpt2++;
1728                         *intpt = -1;
1729                 }
1730
1731
1732 //              len[k][0] = ( minscore - tmptmplen[im] );
1733 //              len[k][1] = ( minscore - tmptmplen[jm] );
1734 //              len[k][0] = -1;
1735 //              len[k][1] = -1;
1736
1737
1738                 hist[im] = k;
1739                 nmemar[im] = nmemim + nmemjm;
1740
1741
1742                 if( len[k][0] == -1 || len[k][1] == -1 )
1743                 {
1744                         reporterr( "Re-computing the length of branch %d..\n", k );
1745                         clusterdist = 0.0;
1746                         mpair = 0;
1747                         for( i=0; (mi=topol[k][0][i])>-1; i++ ) for( j=0; (mj=topol[k][1][j])>-1; j++ ) 
1748                         {
1749                                 minijm = MIN(mi,mj);
1750                                 maxijm = MAX(mi,mj);
1751                                 clusterdist += mtx[minijm][maxijm-minijm];
1752                                 mpair += 1;
1753                         }
1754                         clusterdist /= (double)mpair;
1755                         reporterr( "clusterdist = %f\n", clusterdist );
1756                         if( len[k][0] == -1 ) len[k][0] = clusterdist/2.0 - height[im];
1757                         if( len[k][1] == -1 ) len[k][1] = clusterdist/2.0 - height[im];
1758         
1759                         fprintf( stderr, "len0 = %f\n", len[k][0] );
1760                         fprintf( stderr, "len1 = %f\n\n", len[k][1] );
1761                 }
1762
1763 #if 0
1764         fprintf( stderr, "vSTEP-%03d:\n", k+1 );
1765                 fprintf( stderr, "len0 = %f\n", len[k][0] );
1766         for( i=0; topol[k][0][i]>-1; i++ ) fprintf( stderr, " %03d", topol[k][0][i]+1 );
1767         fprintf( stderr, "\n" );
1768                 fprintf( stderr, "len1 = %f\n", len[k][1] );
1769         for( i=0; topol[k][1][i]>-1; i++ ) fprintf( stderr, " %03d", topol[k][1][i]+1 );
1770         fprintf( stderr, "\n" );
1771
1772 #endif
1773                 height[im] += len[k][0]; // for ig tree, 2015/Dec/25
1774                 dep[k].distfromtip = height[im]; // for ig tree, 2015/Dec/25
1775 //              reporterr( "##### dep[%d].distfromtip = %f\n", k, height[im] );
1776
1777
1778
1779                 treetmp = realloc( treetmp, strlen( tree[im] ) + strlen( tree[jm] ) + 100 ); // 22 de juubunn (:%7,:%7) %7 ha minus kamo
1780                 if( !treetmp )
1781                 {
1782                         reporterr(       "Cannot allocate treetmp\n" );
1783                         exit( 1 );
1784                 }
1785                 sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
1786                 free( tree[im] );
1787                 free( tree[jm] );
1788                 tree[im] = calloc( strlen( treetmp )+1, sizeof( char ) );
1789                 tree[jm] = NULL;
1790                 if( tree[im] == NULL )
1791                 {
1792                         reporterr(       "Cannot reallocate tree!\n" );
1793                         exit( 1 );
1794                 }
1795                 strcpy( tree[im], treetmp );
1796
1797 //              reporterr(       "im,jm=%d,%d\n", im, jm );
1798                 acjmprev = ac[jm].prev; 
1799                 acjmnext = ac[jm].next; 
1800                 acjmprev->next = acjmnext;
1801                 if( acjmnext != NULL )
1802                         acjmnext->prev = acjmprev;
1803 //              free( (void *)eff[jm] ); eff[jm] = NULL;
1804
1805 #if 0 // muscle seems to miss this.
1806                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
1807                 {
1808                         i = acpti->pos;
1809                         if( nearest[i] == im ) 
1810                         {
1811 //                              reporterr(       "calling setnearest\n" );
1812 //                              setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i );
1813                         }
1814                 }
1815 #endif
1816
1817
1818     }
1819         fclose( fp );
1820         fp = fopen( "infile.tree", "w" );
1821                 fprintf( fp, "%s\n", treetmp );
1822                 fprintf( fp, "#by loadtop\n" );
1823         fclose( fp );
1824
1825         FreeCharMtx( tree );
1826         free( treetmp );
1827         free( nametmp );
1828         free( hist );
1829         free( (char *)ac );
1830         free( (void *)nmemar );
1831         free( height );
1832
1833 }
1834
1835
1836 void stringshuffle( int *ary, int size )
1837 {
1838         int i;
1839     for(i=0;i<size;i++)
1840     {
1841         int j = rand()%size;
1842         int t = ary[i];
1843         ary[i] = ary[j];
1844         ary[j] = t;
1845     }
1846 }
1847
1848 void topolorder( int nseq, int *order, int *posinorder, int ***topol, Treedep *dep, int pos, int nchild )
1849 {
1850         int *str;
1851         int child0, child1;
1852         child0 = dep[pos].child0;
1853         child1 = dep[pos].child1;
1854
1855 //      int i;
1856
1857
1858         if( nchild == 0 || nchild == 2 )
1859         {
1860                 if( child0 == -1 )
1861                 {
1862                         str = calloc( 2, sizeof( int ) );
1863                         str[0] = topol[pos][0][0]; // kanarazu memsave format nara, tanjunka dekiru.
1864                         str[1] = -1;
1865
1866
1867 //                      for( i=0; order[i]!=-1; i++ )
1868 //                              ;
1869 //                      reporterr( "0: i=%d, *posinorder=%d\n", i, *posinorder );
1870
1871                         intcpy( order+*posinorder, str );
1872 //                      intcat( order, str );
1873
1874
1875                         *posinorder += 1;
1876
1877                         free( str );
1878                 }
1879                 else
1880                 {
1881                         topolorder( nseq, order, posinorder, topol, dep, child0, 2 );
1882                 }
1883         }
1884
1885
1886         if( nchild == 1 || nchild == 2 )
1887         {
1888                 if( child1 == -1 )
1889                 {
1890                         str = calloc( 2, sizeof( int ) );
1891                         str[0] = topol[pos][1][0]; // kanarazu memsave format nara, tanjunka dekiru.
1892                         str[1] = -1;
1893
1894
1895 //                      for( i=0; order[i]!=-1; i++ )
1896 //                              ;
1897 //                      reporterr( "1: i=%d, *posinorder=%d\n", i, *posinorder );
1898
1899                         intcpy( order+*posinorder, str );
1900 //                      intcat( order, str );
1901
1902
1903                         *posinorder += 1;
1904                         free( str );
1905                 }
1906                 else
1907                 {
1908                         topolorder( nseq, order, posinorder, topol, dep, child1, 2 );
1909                 }
1910         }
1911 //      return( posinorder );
1912 }
1913
1914 #if CANONICALTREEFORMAT
1915 void createchain( int nseq, int ***topol, double **len, char **name, int *nlen, Treedep *dep, int treeout, int shuffle, int seed )
1916 {
1917         FILE *fp;
1918         int i, j;
1919         double l, ll;
1920         int treelen;
1921         char **tree;
1922         char *instanttree;
1923         int posinit;
1924 //      char *treetmp, *tt;
1925         char *nametmp, *nameptr, *tmpptr; 
1926         char namec;
1927         int *order;
1928         int im, jm, mm;
1929
1930         if( treeout )
1931         {
1932 //              treetmp = NULL;
1933                 nametmp = AllocateCharVec( 1000 ); // nagasugi
1934                 tree = AllocateCharMtx( nseq, 0 );
1935
1936                 treelen = nseq;
1937                 for( i=0; i<nseq; i++ )
1938                 {
1939         
1940                         for( j=0; j<999; j++ ) nametmp[j] = 0;
1941                         for( j=0; j<999; j++ ) 
1942                         {
1943                                 namec = name[i][j];
1944                                 if( namec == 0 )
1945                                         break;
1946                                 else if( isalnum( namec ) || namec == '/' || namec == '=' || namec == '-' || namec == '{' || namec == '}' )
1947                                         nametmp[j] = namec;
1948                                 else
1949                                         nametmp[j] = '_';
1950                         }
1951                         nametmp[j] = 0;
1952 //                      sprintf( tree[i], "%d_l=%d_%.20s", i+1, nlen[i], nametmp+1 );
1953                         if( outnumber )
1954                                 nameptr = strstr( nametmp, "_numo_e" ) + 8;
1955                         else
1956                                 nameptr = nametmp + 1;
1957         
1958                         if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
1959         
1960                         tree[i] = calloc( strlen( nametmp )+100, sizeof( char ) ); // suuji no bun de +100
1961                         if( tree[i] == NULL )
1962                         {
1963                                 reporterr(       "Cannot allocate tree!\n" );
1964                                 exit( 1 );
1965                         }
1966                         sprintf( tree[i], "\n%d_%.900s\n", i+1, nameptr );
1967                         treelen += strlen( tree[i] ) + 20;
1968
1969                 }
1970
1971                 instanttree = calloc( treelen, sizeof( char ) );
1972                 posinit = 0;
1973                 for( i=0; i<nseq-1; i++ )
1974                 {
1975                         instanttree[i] = '(';
1976                         posinit++;
1977                 }
1978
1979         }
1980
1981
1982         order = calloc( nseq, sizeof( int ) );
1983         for( i=0; i<nseq; i++ ) order[i] = i;
1984
1985         srand( seed );
1986         if( shuffle ) stringshuffle( order, nseq );
1987
1988         ll = l = 2.0 / nseq;
1989
1990         im = order[0];
1991         jm = order[1];
1992
1993         topol[0][0] = (int *)realloc( topol[0][0], ( 2 ) * sizeof( int ) );
1994         topol[0][1] = (int *)realloc( topol[0][1], ( 2 ) * sizeof( int ) );
1995         if( im < jm )
1996         {
1997                 topol[0][0][0] = im;
1998                 topol[0][0][1] = -1;
1999                 topol[0][1][0] = jm;
2000                 topol[0][1][1] = -1;
2001                 mm = im;
2002         }       
2003         else
2004         {
2005                 topol[0][0][0] = jm;
2006                 topol[0][0][1] = -1;
2007                 topol[0][1][0] = im;
2008                 topol[0][1][1] = -1;
2009                 mm = jm;
2010         }
2011         len[0][0] = len[0][1] = l;
2012         dep[0].child1 = -1;
2013         dep[0].child0 = -1;
2014         dep[0].distfromtip = l;
2015         ll += l;
2016
2017         if( treeout )
2018         {
2019                 posinit += sprintf( instanttree+posinit, "%s:%7.5f,", tree[im], len[0][0] );
2020 //              reporterr( "instanttree = %s\n", instanttree );
2021         }
2022
2023         for( i=1; i<nseq-1; i++ )
2024         {
2025                 im = order[i];
2026                 jm = order[i+1];
2027
2028                 if( mm < jm )
2029                 {
2030 #if MEMSAVE
2031                         topol[i][0] = (int *)realloc( topol[i][0], ( 2 ) * sizeof( int ) );
2032                         topol[i][0][0] = mm;
2033                         topol[i][0][1] = -1;
2034 #else
2035                         topol[i][0] = (int *)realloc( topol[i][0], ( i + 2 ) * sizeof( int ) );
2036                         intcpy( topol[i][0], topol[i-1][0] );
2037                         intcat( topol[i][0], topol[i-1][1] );
2038 #endif
2039                         topol[i][1] = (int *)realloc( topol[i][1], ( 2 ) * sizeof( int ) );
2040                         topol[i][1][0] = jm;
2041                         topol[i][1][1] = -1;
2042         
2043 //                      reporterr( "step %d\n", i );
2044 //                      for( j=0; topol[i][0][j]!=-1; j++ ) reporterr( "%5d ", topol[i][0][j] );
2045 //                      reporterr( "\n", i );
2046 //                      for( j=0; topol[i][1][j]!=-1; j++ ) reporterr( "%5d ", topol[i][1][j] );
2047 //                      reporterr( "\n\n", i );
2048 //                      
2049                         len[i][0] = l;
2050                         len[i][1] = ll;
2051         
2052                         if( dep ) 
2053                         {
2054                                 dep[i].child0 = i-1;
2055                                 dep[i].child1 = -1;
2056                                 dep[i].distfromtip = ll;
2057                         }
2058                 }
2059                 else
2060                 {
2061
2062 #if MEMSAVE
2063                         topol[i][1] = (int *)realloc( topol[i][1], ( 2 ) * sizeof( int ) );
2064                         topol[i][1][0] = mm;
2065                         topol[i][1][1] = -1;
2066 #else
2067                         topol[i][1] = (int *)realloc( topol[i][1], ( i + 2 ) * sizeof( int ) );
2068                         intcpy( topol[i][1], topol[i-1][0] );
2069                         intcat( topol[i][1], topol[i-1][1] );
2070 #endif
2071                         topol[i][0] = (int *)realloc( topol[i][0], ( 2 ) * sizeof( int ) );
2072                         topol[i][0][0] = jm;
2073                         topol[i][0][1] = -1;
2074
2075                         mm = jm;
2076         
2077 //                      reporterr( "step %d\n", i );
2078 //                      for( j=0; topol[i][0][j]!=-1; j++ ) reporterr( "%5d ", topol[i][0][j] );
2079 //                      reporterr( "\n", i );
2080 //                      for( j=0; topol[i][1][j]!=-1; j++ ) reporterr( "%5d ", topol[i][1][j] );
2081 //                      reporterr( "\n\n", i );
2082 //                      
2083         
2084                         len[i][1] = l;
2085                         len[i][0] = ll;
2086         
2087                         if( dep ) 
2088                         {
2089                                 dep[i].child1 = i-1;
2090                                 dep[i].child0 = -1;
2091                                 dep[i].distfromtip = ll;
2092                         }
2093                 }
2094
2095                 if( treeout ) 
2096                 {
2097                         posinit += sprintf( instanttree+posinit, "%s:%7.5f):%7.5f,", tree[im], ll-l, l );
2098 //                      reporterr( "instanttree (in loop) = %s\n", instanttree );
2099 #if 0
2100                         if( i % 1000 == 0 ) reporterr( "\r%d/%d", i, nseq );
2101 //                      reporterr( "size = %d\n", ( strlen( tree[im] ) + strlen( tree[jm] ) + 100 ) * sizeof( char ) );
2102 //                      reporterr( "size = %d\n", ( strlen( tree[im] ) + strlen( tree[jm] ) + 100 ) );
2103 //                      reporterr( "treetmp = %p\n", treetmp  );
2104                         tt = realloc( treetmp, ( strlen( tree[im] ) + strlen( tree[jm] ) + 100 ) * sizeof( char ) ); // 22 de juubunn (:%7,:%7) %7 ha minus kamo
2105                         if( tt == NULL )
2106                         {
2107                                 reporterr(       "Cannot allocate treetmp\n" );
2108                                 exit( 1 );
2109                         }
2110                         treetmp = tt;
2111 //                      reporterr( "i=%d\n", i );
2112 //                      reporterr( "part1=%s\n", tree[0] );
2113 //                      reporterr( "part2=%s\n", tree[i+1] );
2114 //                      reporterr( "size = %d, %d\n", strlen( tree[0] ), strlen( tree[i+1] )  );
2115                         sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[i][0], tree[jm], len[i][1] );
2116                         free( tree[im] );
2117                         free( tree[jm] );
2118                         tree[jm] = calloc( strlen( treetmp )+1, sizeof( char ) );
2119                         tree[im] = NULL;
2120                         if( tree[jm] == NULL )
2121                         {
2122                                 reporterr(       "Cannot reallocate tree!\n" );
2123                                 exit( 1 );
2124                         }
2125                         strcpy( tree[jm], treetmp );
2126 #endif
2127                 }
2128                 ll += l;
2129         }
2130         if( treeout ) 
2131         {
2132                 posinit += sprintf( instanttree+posinit, "%s:%7.5f)", tree[jm], ll-l );
2133                 fp = fopen( "infile.tree", "w" );
2134 //              fprintf( fp, "%s;\n", treetmp );
2135 //              fprintf( fp, "#by createchain\n" );
2136                 fprintf( fp, "%s;\n", instanttree );
2137                 fclose( fp );
2138                 FreeCharMtx( tree );
2139                 free( nametmp );
2140                 free( instanttree );
2141         }
2142
2143         fp = fopen( "_guidetree", "w" );
2144         if( !fp )
2145         {
2146                 reporterr(       "cannot open _guidetree\n" );
2147                 exit( 1 );
2148         }
2149         for( i=0; i<nseq-1; i++ )
2150                 fprintf( fp, "%d %d %f %f\n", topol[i][0][0]+1, topol[i][1][0]+1, len[i][0], len[i][1] );
2151         fclose( fp );
2152
2153         free( order );
2154
2155 }
2156 #else
2157 void createchain( int nseq, int ***topol, double **len, char **name, int *nlen, Treedep *dep, int treeout, int shuffle, int seed )
2158 {
2159         FILE *fp;
2160         int i, j;
2161         double l, ll;
2162         int treelen;
2163         char **tree;
2164         char *instanttree;
2165         int posinit;
2166 //      char *treetmp, *tt;
2167         char *nametmp, *nameptr, *tmpptr; 
2168         char namec;
2169         int *order;
2170         int im, jm;
2171
2172         if( treeout )
2173         {
2174 //              treetmp = NULL;
2175                 nametmp = AllocateCharVec( 1000 ); // nagasugi
2176                 tree = AllocateCharMtx( nseq, 0 );
2177
2178                 treelen = nseq;
2179                 for( i=0; i<nseq; i++ )
2180                 {
2181         
2182                         for( j=0; j<999; j++ ) nametmp[j] = 0;
2183                         for( j=0; j<999; j++ ) 
2184                         {
2185                                 namec = name[i][j];
2186                                 if( namec == 0 )
2187                                         break;
2188                                 else if( isalnum( namec ) || namec == '/' || namec == '=' || namec == '-' || namec == '{' || namec == '}' )
2189                                         nametmp[j] = namec;
2190                                 else
2191                                         nametmp[j] = '_';
2192                         }
2193                         nametmp[j] = 0;
2194 //                      sprintf( tree[i], "%d_l=%d_%.20s", i+1, nlen[i], nametmp+1 );
2195                         if( outnumber )
2196                                 nameptr = strstr( nametmp, "_numo_e" ) + 8;
2197                         else
2198                                 nameptr = nametmp + 1;
2199         
2200                         if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
2201         
2202                         tree[i] = calloc( strlen( nametmp )+100, sizeof( char ) ); // suuji no bun de +100
2203                         if( tree[i] == NULL )
2204                         {
2205                                 reporterr(       "Cannot allocate tree!\n" );
2206                                 exit( 1 );
2207                         }
2208                         sprintf( tree[i], "\n%d_%.900s\n", i+1, nameptr );
2209                         treelen += strlen( tree[i] ) + 20;
2210
2211                 }
2212
2213                 instanttree = calloc( treelen, sizeof( char ) );
2214                 posinit = 0;
2215                 for( i=0; i<nseq-1; i++ )
2216                 {
2217                         instanttree[i] = '(';
2218                         posinit++;
2219                 }
2220
2221         }
2222
2223
2224         order = calloc( nseq, sizeof( int ) );
2225         for( i=0; i<nseq; i++ ) order[i] = i;
2226
2227         srand( seed );
2228         if( shuffle ) stringshuffle( order, nseq );
2229
2230
2231         ll = l = 2.0 / nseq;
2232
2233         for( i=0; i<nseq-1; i++ )
2234         {
2235                 im = order[i];
2236                 jm = order[i+1];
2237
2238                 topol[i][0] = (int *)realloc( topol[i][0], ( i + 2 ) * sizeof( int ) );
2239                 topol[i][1] = (int *)realloc( topol[i][1], ( 2 ) * sizeof( int ) );
2240
2241                 for( j=0; j<i; j++ ) 
2242                         topol[i][0][j] = order[j];
2243                 topol[i][0][i] = im;
2244                 topol[i][0][i+1] = -1;
2245
2246                 topol[i][1][0] = jm;
2247                 topol[i][1][1] = -1;
2248
2249                 len[i][0] = l;
2250                 len[i][1] = ll;
2251                 ll += l;
2252
2253                 if( dep ) 
2254                 {
2255                         dep[i].child0 = i-1;
2256                         dep[i].child1 = -1;
2257                         dep[i].distfromtip = ll;
2258                 }
2259
2260                 if( treeout ) 
2261                 {
2262                         if( i == 0 )
2263                         {
2264                                 posinit += sprintf( instanttree+posinit, "%s:%7.5f,", tree[im], len[i][0] );
2265 //                              reporterr( "instanttree = %s\n", instanttree );
2266                         }
2267                         else if ( i == nseq-2 )
2268                         {
2269                                 posinit += sprintf( instanttree+posinit, "%s:%7.5f):%7.5f,", tree[im], len[i-1][1], len[i-1][0] );
2270                                 posinit += sprintf( instanttree+posinit, "%s:%7.5f)", tree[jm], len[i][1] );
2271                         }
2272                         else
2273                         {
2274                                 posinit += sprintf( instanttree+posinit, "%s:%7.5f):%7.5f,", tree[im], len[i-1][1], len[i-1][0] );
2275 //                              reporterr( "instanttree (in loop) = %s\n", instanttree );
2276                         }
2277 #if 0
2278                         if( i % 1000 == 0 ) reporterr( "\r%d/%d", i, nseq );
2279 //                      reporterr( "size = %d\n", ( strlen( tree[im] ) + strlen( tree[jm] ) + 100 ) * sizeof( char ) );
2280 //                      reporterr( "size = %d\n", ( strlen( tree[im] ) + strlen( tree[jm] ) + 100 ) );
2281 //                      reporterr( "treetmp = %p\n", treetmp  );
2282                         tt = realloc( treetmp, ( strlen( tree[im] ) + strlen( tree[jm] ) + 100 ) * sizeof( char ) ); // 22 de juubunn (:%7,:%7) %7 ha minus kamo
2283                         if( tt == NULL )
2284                         {
2285                                 reporterr(       "Cannot allocate treetmp\n" );
2286                                 exit( 1 );
2287                         }
2288                         treetmp = tt;
2289 //                      reporterr( "i=%d\n", i );
2290 //                      reporterr( "part1=%s\n", tree[0] );
2291 //                      reporterr( "part2=%s\n", tree[i+1] );
2292 //                      reporterr( "size = %d, %d\n", strlen( tree[0] ), strlen( tree[i+1] )  );
2293                         sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[i][0], tree[jm], len[i][1] );
2294                         free( tree[im] );
2295                         free( tree[jm] );
2296                         tree[jm] = calloc( strlen( treetmp )+1, sizeof( char ) );
2297                         tree[im] = NULL;
2298                         if( tree[jm] == NULL )
2299                         {
2300                                 reporterr(       "Cannot reallocate tree!\n" );
2301                                 exit( 1 );
2302                         }
2303                         strcpy( tree[jm], treetmp );
2304 #endif
2305                 }
2306         }
2307         if( treeout ) 
2308         {
2309                 fp = fopen( "infile.tree", "w" );
2310 //              fprintf( fp, "%s;\n", treetmp );
2311 //              fprintf( fp, "#by createchain\n" );
2312                 fprintf( fp, "%s;\n", instanttree );
2313                 fclose( fp );
2314                 FreeCharMtx( tree );
2315                 free( nametmp );
2316                 free( instanttree );
2317         }
2318
2319         fp = fopen( "_guidetree", "w" );
2320         if( !fp )
2321         {
2322                 reporterr(       "cannot open _guidetree\n" );
2323                 exit( 1 );
2324         }
2325 #if CANONICALTREEFORMAT
2326         for( i=0; i<nseq-1; i++ )
2327                 fprintf( fp, "%d %d %f %f\n", topol[i][0][0]+1, topol[i][1][0]+1, len[i][0], len[i][1] );
2328 #else
2329         k = topol[0][0][0];
2330         for( i=0; i<nseq-1; i++ )
2331         {
2332                 jm = topol[i][1][0];
2333
2334                 if( jm > k )
2335                 {
2336                         fprintf( fp, "%d %d %f %f\n", k+1, jm+1, len[i][0], len[i][1] );
2337                 }
2338                 else
2339                 {
2340                         fprintf( fp, "%d %d %f %f\n", jm+1, k+1, len[i][1], len[i][0] );
2341                         k = jm;
2342                 }
2343         }
2344 #endif
2345         fclose( fp );
2346         free( order );
2347 }
2348 #endif
2349
2350 void loadtree( int nseq, int ***topol, double **len, char **name, int *nlen, Treedep *dep, int treeout )
2351 {
2352         int i, j, k, miniim, maxiim, minijm, maxijm;
2353         int *intpt, *intpt2;
2354         int *hist = NULL;
2355         Bchain *ac = NULL;
2356         int im = -1, jm = -1;
2357         Bchain *acjmnext, *acjmprev;
2358         int prevnode;
2359         Bchain *acpti;
2360         int *pt1, *pt2, *pt11, *pt22;
2361         int *nmemar;
2362         int nmemim, nmemjm;
2363         char **tree;
2364         char *treetmp;
2365         char *nametmp, *nameptr, *tmpptr; 
2366         char namec;
2367         FILE *fp;
2368         int node[2];
2369         double *height;
2370
2371         fp = fopen( "_guidetree", "r" );
2372         if( !fp )
2373         {
2374                 reporterr(       "cannot open _guidetree\n" );
2375                 exit( 1 );
2376         }
2377
2378
2379         reporterr( "Loading a tree\n" );
2380
2381         if( !hist )
2382         {
2383                 hist = AllocateIntVec( nseq );
2384                 ac = (Bchain *)malloc( nseq * sizeof( Bchain ) );
2385                 nmemar = AllocateIntVec( nseq );
2386 //              treetmp = AllocateCharVec( nseq*50 );
2387                 if( dep ) height = AllocateFloatVec( nseq );
2388         }
2389
2390         if( treeout )
2391         {
2392                 treetmp = NULL;
2393                 nametmp = AllocateCharVec( 1000 ); // nagasugi
2394 //              tree = AllocateCharMtx( nseq, nseq*50 );
2395                 tree = AllocateCharMtx( nseq, 0 );
2396
2397                 for( i=0; i<nseq; i++ )
2398                 {
2399                         for( j=0; j<999; j++ ) nametmp[j] = 0;
2400                         for( j=0; j<999; j++ ) 
2401                         {
2402                                 namec = name[i][j];
2403                                 if( namec == 0 )
2404                                         break;
2405                                 else if( isalnum( namec ) || namec == '/' || namec == '=' || namec == '-' || namec == '{' || namec == '}' )
2406                                         nametmp[j] = namec;
2407                                 else
2408                                         nametmp[j] = '_';
2409                         }
2410                         nametmp[j] = 0;
2411 //                      sprintf( tree[i], "%d_l=%d_%.20s", i+1, nlen[i], nametmp+1 );
2412                         if( outnumber )
2413                                 nameptr = strstr( nametmp, "_numo_e" ) + 8;
2414                         else
2415                                 nameptr = nametmp + 1;
2416         
2417                         if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
2418         
2419                         tree[i] = calloc( strlen( nametmp )+100, sizeof( char ) ); // suuji no bun de +100
2420                         if( tree[i] == NULL )
2421                         {
2422                                 reporterr(       "Cannot allocate tree!\n" );
2423                                 exit( 1 );
2424                         }
2425                         sprintf( tree[i], "\n%d_%.900s\n", i+1, nameptr );
2426                 }
2427
2428         }
2429
2430         for( i=0; i<nseq; i++ )
2431         {
2432                 ac[i].next = ac+i+1;
2433                 ac[i].prev = ac+i-1;
2434                 ac[i].pos = i;
2435         }
2436         ac[nseq-1].next = NULL;
2437
2438
2439         for( i=0; i<nseq; i++ ) 
2440         {
2441                 hist[i] = -1;
2442                 nmemar[i] = 1;
2443         }
2444
2445         reporterr(       "\n" );
2446         for( k=0; k<nseq-1; k++ )
2447         {
2448                 if( k % 10 == 0 ) reporterr(       "\r% 5d / %d", k, nseq );
2449 #if 0
2450                 minscore = 999.9;
2451                 for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
2452                 {
2453                         i = acpti->pos;
2454 //                      reporterr(       "k=%d i=%d\n", k, i );
2455                         if( mindisfrom[i] < minscore ) // muscle
2456                         {
2457                                 im = i;
2458                                 minscore = mindisfrom[i];
2459                         }
2460                 }
2461                 jm = nearest[im];
2462                 if( jm < im ) 
2463                 {
2464                         j=jm; jm=im; im=j;
2465                 }
2466 #else
2467                 len[k][0] = len[k][1] = -1.0;
2468                 loadtreeoneline( node, len[k], fp );
2469                 im = node[0];
2470                 jm = node[1];
2471
2472 //              if( im > nseq-1 || jm > nseq-1 || tree[im] == NULL || tree[jm] == NULL )
2473                 if( im > nseq-1 || jm > nseq-1 )
2474                 {
2475                         reporterr(       "\n\nCheck the guide tree.\n" );
2476                         reporterr(       "im=%d, jm=%d\n", im+1, jm+1 );
2477                         reporterr(       "Please use newick2mafft.rb to generate a tree file from a newick tree.\n\n" );
2478                         exit( 1 );
2479                 }
2480
2481
2482                 if( len[k][0] == -1.0 || len[k][1] == -1.0 )
2483                 {
2484                         reporterr(       "\n\nERROR: Branch length is not given.\n" );
2485                         exit( 1 );
2486                 }
2487
2488                 if( len[k][0] < 0.0 ) len[k][0] = 0.0;
2489                 if( len[k][1] < 0.0 ) len[k][1] = 0.0;
2490
2491
2492 #endif
2493
2494                 prevnode = hist[im];
2495                 if( dep ) dep[k].child0 = prevnode;
2496                 nmemim = nmemar[im];
2497
2498 //              reporterr(       "prevnode = %d, nmemim = %d\n", prevnode, nmemim );
2499
2500                 intpt = topol[k][0] = (int *)realloc( topol[k][0], ( nmemim + 1 ) * sizeof( int ) );
2501                 if( prevnode == -1 )
2502                 {
2503                         *intpt++ = im;
2504                         *intpt = -1;
2505                 }
2506                 else
2507                 {
2508                         pt1 = topol[prevnode][0];
2509                         pt2 = topol[prevnode][1];
2510                         if( *pt1 > *pt2 )
2511                         {
2512                                 pt11 = pt2;
2513                                 pt22 = pt1;
2514                         }
2515                         else
2516                         {
2517                                 pt11 = pt1;
2518                                 pt22 = pt2;
2519                         }
2520                         for( intpt2=pt11; *intpt2!=-1; )
2521                                 *intpt++ = *intpt2++;
2522                         for( intpt2=pt22; *intpt2!=-1; )
2523                                 *intpt++ = *intpt2++;
2524                         *intpt = -1;
2525                 }
2526
2527
2528                 nmemjm = nmemar[jm];
2529                 prevnode = hist[jm];
2530                 if( dep ) dep[k].child1 = prevnode;
2531
2532 //              reporterr(       "prevnode = %d, nmemjm = %d\n", prevnode, nmemjm );
2533
2534                 intpt = topol[k][1] = (int *)realloc( topol[k][1], ( nmemjm + 1 ) * sizeof( int ) );
2535                 if( !intpt )
2536                 {
2537                         reporterr(       "Cannot reallocate topol\n" );
2538                         exit( 1 );
2539                 }
2540                 if( prevnode == -1 )
2541                 {
2542                         *intpt++ = jm;
2543                         *intpt = -1;
2544                 }
2545                 else
2546                 {
2547                         pt1 = topol[prevnode][0];
2548                         pt2 = topol[prevnode][1];
2549                         if( *pt1 > *pt2 )
2550                         {
2551                                 pt11 = pt2;
2552                                 pt22 = pt1;
2553                         }
2554                         else
2555                         {
2556                                 pt11 = pt1;
2557                                 pt22 = pt2;
2558                         }
2559                         for( intpt2=pt11; *intpt2!=-1; )
2560                                 *intpt++ = *intpt2++;
2561                         for( intpt2=pt22; *intpt2!=-1; )
2562                                 *intpt++ = *intpt2++;
2563                         *intpt = -1;
2564                 }
2565
2566
2567 //              len[k][0] = ( minscore - tmptmplen[im] );
2568 //              len[k][1] = ( minscore - tmptmplen[jm] );
2569 //              len[k][0] = -1;
2570 //              len[k][1] = -1;
2571
2572
2573                 hist[im] = k;
2574                 nmemar[im] = nmemim + nmemjm;
2575
2576 //              mindisfrom[im] = 999.9;
2577                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
2578         {
2579                         i = acpti->pos;
2580             if( i != im && i != jm )
2581             {
2582                                 if( i < im )
2583                                 {
2584                                          miniim = i;
2585                                          maxiim = im;
2586                                          minijm = i;
2587                                          maxijm = jm;
2588                                 }
2589                                 else if( i < jm )
2590                                 {
2591                                          miniim = im;
2592                                          maxiim = i;
2593                                          minijm = i;
2594                                          maxijm = jm;
2595                                 }
2596                                 else
2597                                 {
2598                                          miniim = im;
2599                                          maxiim = i;
2600                                          minijm = jm;
2601                                          maxijm = i;
2602                                 }
2603             }
2604         }
2605
2606
2607                 if( treeout )
2608                 {
2609                         treetmp = realloc( treetmp, strlen( tree[im] ) + strlen( tree[jm] ) + 100 ); // 22 de juubunn (:%7,:%7) %7 ha minus kamo
2610                         if( !treetmp )
2611                         {
2612                                 reporterr(       "Cannot allocate treetmp\n" );
2613                                 exit( 1 );
2614                         }
2615                         sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
2616                         free( tree[im] );
2617                         free( tree[jm] );
2618                         tree[im] = calloc( strlen( treetmp )+1, sizeof( char ) );
2619                         tree[jm] = NULL;
2620                         if( tree[im] == NULL )
2621                         {
2622                                 reporterr(       "Cannot reallocate tree!\n" );
2623                                 exit( 1 );
2624                         }
2625                         strcpy( tree[im], treetmp );
2626                 }
2627
2628 //              reporterr(       "im,jm=%d,%d\n", im, jm );
2629                 acjmprev = ac[jm].prev; 
2630                 acjmnext = ac[jm].next; 
2631                 acjmprev->next = acjmnext;
2632                 if( acjmnext != NULL )
2633                         acjmnext->prev = acjmprev;
2634 //              free( (void *)eff[jm] ); eff[jm] = NULL;
2635
2636 #if 0 // muscle seems to miss this.
2637                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
2638                 {
2639                         i = acpti->pos;
2640                         if( nearest[i] == im ) 
2641                         {
2642 //                              reporterr(       "calling setnearest\n" );
2643 //                              setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i );
2644                         }
2645                 }
2646 #endif
2647
2648
2649 #if 0
2650         fprintf( stderr, "vSTEP-%03d:\n", k+1 );
2651                 fprintf( stderr, "len0 = %f\n", len[k][0] );
2652         for( i=0; topol[k][0][i]>-1; i++ ) fprintf( stderr, " %03d", topol[k][0][i]+1 );
2653         fprintf( stderr, "\n" );
2654                 fprintf( stderr, "len1 = %f\n", len[k][1] );
2655         for( i=0; topol[k][1][i]>-1; i++ ) fprintf( stderr, " %03d", topol[k][1][i]+1 );
2656         fprintf( stderr, "\n" );
2657 #endif
2658
2659                 if( dep ) 
2660                 {
2661                         height[im] += len[k][0]; // for ig tree, 2015/Dec/25
2662                         dep[k].distfromtip = height[im]; // for ig tree, 2015/Dec/25
2663 //                      reporterr(       "##### dep[%d].distfromtip = %f\n\n", k, height[im] );
2664                 }
2665
2666 //              reporterr( "dep[%d].child0 = %d\n", k, dep[k].child0 );
2667 //              reporterr( "dep[%d].child1 = %d\n", k, dep[k].child1 );
2668 //              reporterr( "dep[%d].distfromtip = %f\n", k, dep[k].distfromtip );
2669     }
2670         fclose( fp );
2671
2672         if( treeout )
2673         {
2674                 fp = fopen( "infile.tree", "w" );
2675                 fprintf( fp, "%s;\n", treetmp );
2676                 fprintf( fp, "#by loadtree\n" );
2677                 fclose( fp );
2678                 FreeCharMtx( tree );
2679                 free( treetmp );
2680                 free( nametmp );
2681         }
2682
2683         free( hist );
2684         free( (char *)ac );
2685         free( (void *)nmemar );
2686         if( dep ) free( height );
2687
2688 }
2689
2690 int check_guidetreefile( int *seed, int *npick, double *limitram )
2691 {
2692         char string[100];
2693         char *sizestring;
2694         FILE *fp;
2695         double tanni;
2696         double tmpd;
2697
2698         *seed = 0;
2699         *npick = 200;
2700         *limitram = 10.0 * 1000 * 1000 * 1000; // 10GB
2701         fp = fopen( "_guidetree", "r" );
2702         if( !fp )
2703         {
2704                 reporterr(       "cannot open _guidetree\n" );
2705                 exit( 1 );
2706         }
2707
2708         fgets( string, 999, fp );
2709         fclose( fp );
2710
2711         if( !strncmp( string, "shuffle", 7 ) )
2712         {
2713                 sscanf( string+7, "%d", seed );
2714                 reporterr( "shuffle, seed=%d\n", *seed );
2715                 return( 's' );
2716         }
2717         else if( !strncmp( string, "pileup", 6 ) )
2718         {
2719                 reporterr( "pileup.\n" );
2720                 return( 'p' );
2721         }
2722         else if( !strncmp( string, "auto", 4 ) )
2723         {
2724                 sscanf( string+4, "%d %d", seed, npick );
2725                 reporterr( "auto, seed=%d, npick=%d\n", *seed, *npick );
2726                 if( *npick < 2 )
2727                 {
2728                         reporterr( "Check npick\n" );
2729                         exit( 1 );
2730                 }
2731                 return( 'a' );
2732         }
2733         else if( !strncmp( string, "test", 4 ) )
2734         {
2735                 sscanf( string+4, "%d %d", seed, npick );
2736                 reporterr( "calc, seed=%d, npick=%d\n", *seed, *npick );
2737                 if( *npick < 2 )
2738                 {
2739                         reporterr( "Check npick\n" );
2740                         exit( 1 );
2741                 }
2742                 return( 't' );
2743         }
2744         else if( !strncmp( string, "compact", 7 ) )
2745         {
2746                 sizestring = string + 7;
2747                 reporterr( "sizestring = %s\n", sizestring );
2748                 if( strchr( sizestring, 'k' ) || strchr( sizestring, 'k' ) ) tanni = 1.0 * 1000; // kB
2749                 else if( strchr( sizestring, 'M' ) || strchr( sizestring, 'm' ) ) tanni = 1.0 * 1000 * 1000; // GB
2750                 else if( strchr( sizestring, 'G' ) || strchr( sizestring, 'g' ) ) tanni = 1.0 * 1000 * 1000 * 1000; // GB
2751                 else if( strchr( sizestring, 'T' ) || strchr( sizestring, 't' ) ) tanni = 1.0 * 1000 * 1000 * 1000 * 1000; // TB
2752                 else
2753                 {
2754                         reporterr( "\nSpecify initial ram usage by '--initialramusage xGB'\n\n\n" );
2755                         exit( 1 );
2756                 }
2757                 sscanf( sizestring, "%lf", &tmpd );
2758                 *limitram = tmpd * tanni;
2759                 reporterr( "Initial RAM usage = %10.3fGB\n", *limitram/1000/1000/1000 );
2760                 return( 'c' );
2761         }
2762         else if( !strncmp( string, "very compact", 12 ) )
2763         {
2764                 reporterr( "very compact.\n" );
2765                 return( 'C' );
2766         }
2767         else
2768         {
2769                 reporterr( "loadtree.\n" );
2770                 return( 'l' );
2771         }
2772 }
2773
2774
2775 static double sueff1, sueff05;
2776 //static double sueff1_double, sueff05_double;
2777
2778 static double cluster_mix_double( double d1, double d2 )
2779 {
2780         return( MIN( d1, d2 ) * sueff1 + ( d1 + d2 ) * sueff05 ); 
2781 }
2782 static double cluster_average_double( double d1, double d2 )
2783 {
2784         return( ( d1 + d2 ) * 0.5 ); 
2785 }
2786 static double cluster_minimum_double( double d1, double d2 )
2787 {
2788         return( MIN( d1, d2 ) ); 
2789 }
2790 #if 0
2791 static double cluster_mix_double( double d1, double d2 )
2792 {
2793         return( MIN( d1, d2 ) * sueff1_double + ( d1 + d2 ) * sueff05_double ); 
2794 }
2795 static double cluster_average_double( double d1, double d2 )
2796 {
2797         return( ( d1 + d2 ) * 0.5 ); 
2798 }
2799 static double cluster_minimum_double( double d1, double d2 )
2800 {
2801         return( MIN( d1, d2 ) ); 
2802 }
2803 #endif
2804
2805 static void increaseintergroupdistanceshalfmtx( double **eff, int ngroup, int **groups, int nseq )
2806 {
2807         int nwarned = 0;
2808         int i, k, m, s1, s2, sl, ss;
2809         int *others, *tft;
2810         double maxdist, *dptr, dtmp;
2811         tft = calloc( nseq, sizeof( int * ) );
2812         others = calloc( nseq, sizeof( int * ) );
2813
2814 //      for( m=0; m<nseq-1; m++ ) for( k=m+1; k<nseq; k++ )
2815 //              reporterr( "mtx[%d][%d] originally = %f (maxdist=%f)\n", m, k, eff[m][k-m], maxdist );
2816
2817         reporterr( "\n" ); // Hitsuyou desu.
2818         for( i=0; i<ngroup; i++ )
2819         {
2820                 if( groups[i][1] == -1 ) continue;
2821
2822                 for( m=0; m<nseq; m++ ) tft[m] = 0;
2823                 for( m=0; (s1=groups[i][m])>-1; m++ ) tft[s1] = 1;
2824                 for( m=0,k=0; m<nseq; m++ ) if( tft[m] == 0 ) others[k++] = m;
2825                 others[k] = -1;
2826
2827                 maxdist = 0.0;
2828                 for( m=1; (s2=groups[i][m])>-1; m++ ) for( k=0; (s1=groups[i][k])>-1&&k<m; k++ ) 
2829                 {
2830 //                      reporterr( "m=%d, k=%d, s2=%d, s1=%d\n", m, k, s2, s1 );
2831
2832                         if( s2 > s1 )
2833                         {
2834                                 sl = s2; ss = s1;
2835                         }
2836                         else
2837                         {
2838                                 sl = s1; ss = s2;
2839                         }
2840                         dtmp = eff[ss][sl-ss];
2841                         if( dtmp > maxdist ) maxdist = dtmp;
2842                 }
2843 //              reporterr( "maxdist = %f\n", maxdist );
2844
2845                 for( m=0; (s2=groups[i][m])>-1; m++ ) for( k=0; (s1=others[k])>-1; k++ ) 
2846                 {
2847                         if( s2 > s1 )
2848                         {
2849                                 sl = s2; ss = s1;
2850                         }
2851                         else
2852                         {
2853                                 sl = s1; ss = s2;
2854                         }
2855                         dptr = eff[ss] + sl-ss;
2856                         if( *dptr < maxdist )
2857                         {
2858                                 if( *dptr < 0.5 && nwarned++ < 100 ) reporterr( "# Sequences %d and %d seem to be closely related, but are not in the same sub MSA (%d) in your setting.\n", s2+1, s1+1, i+1 );
2859                                 *dptr = maxdist;
2860                         }
2861                 }
2862 //              for( m=0; m<nseq-1; m++ ) for( k=m+1; k<nseq; k++ )
2863 //                      reporterr( "mtx[%d][%d] after modification%d = %f (maxdist=%f)\n", m, k, i, eff[m][k-m], maxdist );
2864         }
2865         if( nwarned > 100 ) reporterr( "# Sequenc.... (more pairs)\n" );
2866
2867         free( tft );
2868         free( others );
2869 }
2870
2871 static void increaseintergroupdistancesfullmtx( double **eff, int ngroup, int **groups, int nseq )
2872 {
2873         int nwarned = 0;
2874         int i, k, m, s1, s2, sl, ss;
2875         int *others, *tft;
2876         double maxdist, *dptr, dtmp;
2877         tft = calloc( nseq, sizeof( int * ) );
2878         others = calloc( nseq, sizeof( int * ) );
2879
2880         reporterr( "\n" ); // Hitsuyou desu.
2881         for( i=0; i<ngroup; i++ )
2882         {
2883                 if( groups[i][1] == -1 ) continue;
2884
2885                 for( m=0; m<nseq; m++ ) tft[m] = 0;
2886                 for( m=0; (s1=groups[i][m])>-1; m++ ) tft[s1] = 1;
2887                 for( m=0,k=0; m<nseq; m++ ) if( tft[m] == 0 ) others[k++] = m;
2888                 others[k] = -1;
2889
2890                 maxdist = 0.0;
2891                 for( m=1; (s2=groups[i][m])>-1; m++ ) for( k=0; (s1=groups[i][k])>-1&&k<m; k++ ) 
2892                 {
2893                         if( s2 > s1 )
2894                         {
2895                                 sl = s2; ss = s1;
2896                         }
2897                         else
2898                         {
2899                                 sl = s1; ss = s2;
2900                         }
2901                         dtmp = eff[ss][sl];
2902                         if( dtmp > maxdist ) maxdist = dtmp;
2903                 }
2904
2905 //              reporterr( "maxdist = %f\n", maxdist );
2906
2907                 for( m=0; (s2=groups[i][m])>-1; m++ ) for( k=0; (s1=others[k])>-1; k++ ) 
2908                 {
2909                         if( s2 > s1 )
2910                         {
2911                                 sl = s2; ss = s1;
2912                         }
2913                         else
2914                         {
2915                                 sl = s1; ss = s2;
2916                         }
2917                         dptr = eff[ss] + sl;
2918                         if( *dptr < maxdist )
2919                         {
2920                                 if( *dptr < 0.5 && nwarned++ < 100 ) reporterr( "# Sequences %d and %d seem to be closely related, but are not in the same sub MSA (%d) in your setting.\n", s2+1, s1+1, i+1 );
2921                                 *dptr = maxdist;
2922                         }
2923                 }
2924         }
2925         if( nwarned > 100 ) reporterr( "# Sequenc.... (more pairs)\n" );
2926
2927 //      for( m=0; m<nseq-1; m++ ) for( k=m+1; k<nseq; k++ )
2928 //              reporterr( "mtx[%d][%d] after modification = %f (maxdist=%f)\n", m, k, eff[m][k], maxdist );
2929         free( tft );
2930         free( others );
2931 }
2932
2933 void fixed_supg_double_realloc_nobk_halfmtx_treeout_constrained( int nseq, double **eff, int ***topol, double **len, char **name, int *nlen, Treedep *dep, int ngroup, int **groups, int efffree )
2934 {
2935         int i, j, k, miniim, maxiim, minijm, maxijm;
2936         int *intpt, *intpt2;
2937         double tmpdouble;
2938         double eff1, eff0;
2939         double *tmptmplen = NULL; //static?
2940         int *hist = NULL; //static?
2941         Bchain *ac = NULL; //static?
2942         int im = -1, jm = -1;
2943         Bchain *acjmnext, *acjmprev;
2944         int prevnode;
2945         Bchain *acpti, *acptj;
2946         int *pt1, *pt2, *pt11, *pt22;
2947         int *nmemar; //static?
2948         int nmemim, nmemjm;
2949         double minscore;
2950         int *nearest = NULL; // by D.Mathog, a guess
2951         double *mindisfrom = NULL; // by D.Mathog, a guess
2952         char **tree; //static?
2953         char *treetmp; //static?
2954         char *nametmp, *nameptr, *tmpptr; //static?
2955         FILE *fp;
2956         double (*clusterfuncpt[1])(double,double);
2957         char namec;
2958         int *testtopol, **inconsistent;
2959         int **inconsistentpairlist;
2960         int ninconsistentpairs;
2961         int *warned;
2962         int allinconsistent;
2963         int firsttime;
2964
2965         increaseintergroupdistanceshalfmtx( eff, ngroup, groups, nseq );
2966
2967         sueff1 = 1 - (double)sueff_global;
2968         sueff05 = (double)sueff_global * 0.5;
2969         if ( treemethod == 'X' )
2970                 clusterfuncpt[0] = cluster_mix_double;
2971         else if ( treemethod == 'E' )
2972                 clusterfuncpt[0] = cluster_average_double;
2973         else if ( treemethod == 'q' )
2974                 clusterfuncpt[0] = cluster_minimum_double;
2975         else
2976         {
2977                 reporterr(       "Unknown treemethod, %c\n", treemethod );
2978                 exit( 1 );
2979         }
2980
2981         if( !hist )
2982         {
2983                 hist = AllocateIntVec( njob );
2984                 tmptmplen = AllocateFloatVec( njob );
2985                 ac = (Bchain *)malloc( njob * sizeof( Bchain ) );
2986                 nmemar = AllocateIntVec( njob );
2987                 mindisfrom = AllocateFloatVec( njob );
2988                 nearest = AllocateIntVec( njob );
2989 //              treetmp = AllocateCharVec( njob * ( B + 100 ) ); // nagasugi?
2990                 treetmp = NULL; // kentou 2013/06/12
2991                 nametmp = AllocateCharVec( 1000 ); // nagasugi
2992 //              tree = AllocateCharMtx( njob, njob*600 );
2993                 tree = AllocateCharMtx( njob, 0 );
2994                 testtopol = AllocateIntVec( njob + 1 );
2995                 inconsistent = AllocateIntMtx( njob, njob ); // muda
2996 //              inconsistentpairlist = AllocateIntMtx( njob*(njob-1)/2+1, 2 ); // muda!
2997                 inconsistentpairlist = AllocateIntMtx( 1, 2 );
2998 //              reporterr( "allocating inconsistentpairlist\n" );
2999                 warned = AllocateIntVec( ngroup );
3000         }
3001
3002         
3003         for( i=0; i<nseq; i++ )
3004         {
3005                 for( j=0; j<999; j++ ) nametmp[j] = 0;
3006                 for( j=0; j<999; j++ ) 
3007                 {
3008                         namec = name[i][j];
3009                         if( namec == 0 )
3010                                 break;
3011                         else if( isalnum( namec ) || namec == '/' || namec == '=' || namec == '-' || namec == '{' || namec == '}' )
3012                                 nametmp[j] = namec;
3013                         else
3014                                 nametmp[j] = '_';
3015                 }
3016                 nametmp[j] = 0;
3017 //              sprintf( tree[i], "%d_l=%d_%.20s", i+1, nlen[i], nametmp+1 );
3018                 if( outnumber )
3019                         nameptr = strstr( nametmp, "_numo_e" ) + 8;
3020                 else
3021                         nameptr = nametmp + 1;
3022
3023                 if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
3024
3025                 tree[i] = calloc( strlen( nametmp )+100, sizeof( char ) ); // suuji no bun de +100
3026                 if( tree[i] == NULL )
3027                 {
3028                         reporterr(       "Cannot allocate tree!\n" );
3029                         exit( 1 );
3030                 }
3031                 sprintf( tree[i], "\n%d_%.900s\n", i+1, nameptr );
3032         }
3033         for( i=0; i<nseq; i++ )
3034         {
3035                 ac[i].next = ac+i+1;
3036                 ac[i].prev = ac+i-1;
3037                 ac[i].pos = i;
3038         }
3039         ac[nseq-1].next = NULL;
3040
3041         for( i=0; i<nseq; i++ ) setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i ); // muscle
3042
3043         for( i=0; i<nseq; i++ ) tmptmplen[i] = 0.0;
3044         for( i=0; i<nseq; i++ ) 
3045         {
3046                 hist[i] = -1;
3047                 nmemar[i] = 1;
3048         }
3049
3050         reporterr(       "\n" );
3051         ninconsistentpairs = 0;
3052         for( k=0; k<nseq-1; k++ )
3053         {
3054                 if( k % 10 == 0 ) reporterr(       "\r% 5d / %d", k, nseq );
3055
3056                 for( i=0; i<ninconsistentpairs; i++ ) inconsistent[inconsistentpairlist[i][0]][inconsistentpairlist[i][1]] = 0;
3057 //              for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) for( acptj=acpti->next; acptj!=NULL; acptj=acptj->next ) inconsistent[acpti->pos][acptj->pos] = 0; // osoi!!!
3058                 ninconsistentpairs = 0;
3059                 firsttime = 1;
3060                 while( 1 )
3061                 {
3062                         if( firsttime )
3063                         {
3064                                 firsttime = 0;
3065                                 minscore = 999.9;
3066                                 for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
3067                                 {
3068                                         i = acpti->pos;
3069 //                                      reporterr(       "k=%d i=%d\n", k, i );
3070                                         if( mindisfrom[i] < minscore ) // muscle
3071                                         {
3072                                                 im = i;
3073                                                 minscore = mindisfrom[i];
3074                                         }
3075                                 }
3076                                 jm = nearest[im];
3077                                 if( jm < im ) 
3078                                 {
3079                                         j=jm; jm=im; im=j;
3080                                 }
3081                         }
3082                         else
3083                         {
3084                                 minscore = 999.9;
3085                                 for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
3086                                 {
3087                                         i = acpti->pos;
3088 //                                      reporterr(       "k=%d i=%d\n", k, i );
3089                                         for( acptj=acpti->next; acptj!=NULL; acptj=acptj->next )
3090                                         {
3091                                                 j = acptj->pos;
3092                                                 if( !inconsistent[i][j] && (tmpdouble=eff[i][j-i]) < minscore )
3093                                                 {
3094                                                         minscore = tmpdouble;
3095                                                         im = i; jm = j;
3096                                                 }
3097                                         }
3098                                         for( acptj=ac; (acptj&&acptj->pos!=i); acptj=acptj->next )
3099                                         {
3100                                                 j = acptj->pos;
3101                                                 if( !inconsistent[j][i] && (tmpdouble=eff[j][i-j]) < minscore )
3102                                                 {
3103                                                         minscore = tmpdouble;
3104                                                         im = j; jm = i;
3105                                                 }
3106                                         }
3107                                 }
3108                         }
3109
3110
3111                         allinconsistent = 1;
3112                         for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
3113                         {
3114                                 for( acptj=acpti->next; acptj!=NULL; acptj=acptj->next )
3115                                 {
3116                                         if( inconsistent[acpti->pos][acptj->pos] == 0 )
3117                                         {
3118                                                 allinconsistent = 0;
3119                                                 goto exitloop_f;
3120                                         }
3121                                 }
3122                         }
3123                         exitloop_f:
3124
3125                         if( allinconsistent )
3126                         {
3127                                 reporterr(       "\n\n\nPlease check whether the grouping is possible.\n\n\n" );
3128                                 exit( 1 );
3129                         }
3130 #if 1
3131                         intpt = testtopol;
3132                         prevnode = hist[im];
3133                         if( prevnode == -1 )
3134                         {
3135                                 *intpt++ = im;
3136                         }
3137                         else
3138                         {
3139                                 for( intpt2=topol[prevnode][0]; *intpt2!=-1; )
3140                                         *intpt++ = *intpt2++;
3141                                 for( intpt2=topol[prevnode][1]; *intpt2!=-1; )
3142                                         *intpt++ = *intpt2++;
3143                         }
3144                         
3145                         prevnode = hist[jm];
3146                         if( prevnode == -1 )
3147                         {
3148                                 *intpt++ = jm;
3149                         }
3150                         else
3151                         {
3152                                 for( intpt2=topol[prevnode][0]; *intpt2!=-1; )
3153                                         *intpt++ = *intpt2++;
3154                                 for( intpt2=topol[prevnode][1]; *intpt2!=-1; )
3155                                         *intpt++ = *intpt2++;
3156                         }
3157                         *intpt = -1;
3158 //                      reporterr(       "testtopol = \n" );
3159 //              for( i=0; testtopol[i]>-1; i++ ) reporterr(       " %03d", testtopol[i]+1 );
3160 //                      reporterr(       "\n" );
3161 #endif
3162                         for( i=0; i<ngroup; i++ )
3163                         {
3164 //                              reporterr(       "groups[%d] = \n", i );
3165 //                              for( j=0; groups[i][j]>-1; j++ ) reporterr(       " %03d", groups[i][j]+1 );
3166 //                              reporterr(       "\n" );
3167                                 if( overlapmember( groups[i], testtopol ) )
3168                                 {
3169                                         if( !includemember( testtopol, groups[i] ) && !includemember( groups[i], testtopol ) )
3170                                         {
3171                                                 if( !warned[i] )
3172                                                 {
3173                                                         warned[i] = 1;
3174                                                         reporterr(       "\n###################################################################\n" );
3175                                                         reporterr(       "# WARNING: Group %d is forced to be a monophyletic cluster.\n", i+1 );
3176                                                         reporterr(       "###################################################################\n" );
3177                                                 }
3178                                                 inconsistent[im][jm] = 1;
3179
3180                                                 inconsistentpairlist = realloc( inconsistentpairlist, (ninconsistentpairs+1)*sizeof( int * ) );
3181                                                 inconsistentpairlist[ninconsistentpairs] = malloc( sizeof( int ) * 2 );
3182                                                 reporterr( "reallocating inconsistentpairlist, size=%d\n", ninconsistentpairs+1 );
3183                                                 inconsistentpairlist[ninconsistentpairs][0] = im;
3184                                                 inconsistentpairlist[ninconsistentpairs][1] = jm;
3185                                                 ninconsistentpairs++;
3186                                                 break;
3187                                         }
3188                                 }
3189                         }
3190                         if( i == ngroup )
3191                         {
3192 //                              reporterr(       "OK\n" );
3193                                 break;
3194                         }
3195                 }
3196
3197
3198                 prevnode = hist[im];
3199                 if( dep ) dep[k].child0 = prevnode;
3200                 nmemim = nmemar[im];
3201                 intpt = topol[k][0] = (int *)realloc( topol[k][0], ( nmemim + 1 ) * sizeof( int ) );
3202                 if( prevnode == -1 )
3203                 {
3204                         *intpt++ = im;
3205                         *intpt = -1;
3206                 }
3207                 else
3208                 {
3209                         pt1 = topol[prevnode][0];
3210                         pt2 = topol[prevnode][1];
3211                         if( *pt1 > *pt2 )
3212                         {
3213                                 pt11 = pt2;
3214                                 pt22 = pt1;
3215                         }
3216                         else
3217                         {
3218                                 pt11 = pt1;
3219                                 pt22 = pt2;
3220                         }
3221                         for( intpt2=pt11; *intpt2!=-1; )
3222                                 *intpt++ = *intpt2++;
3223                         for( intpt2=pt22; *intpt2!=-1; )
3224                                 *intpt++ = *intpt2++;
3225                         *intpt = -1;
3226                 }
3227
3228                 prevnode = hist[jm];
3229                 if( dep ) dep[k].child1 = prevnode;
3230                 nmemjm = nmemar[jm];
3231                 intpt = topol[k][1] = (int *)realloc( topol[k][1], ( nmemjm + 1 ) * sizeof( int ) );
3232                 if( !intpt )
3233                 {
3234                         reporterr(       "Cannot reallocate topol\n" );
3235                         exit( 1 );
3236                 }
3237                 if( prevnode == -1 )
3238                 {
3239                         *intpt++ = jm;
3240                         *intpt = -1;
3241                 }
3242                 else
3243                 {
3244                         pt1 = topol[prevnode][0];
3245                         pt2 = topol[prevnode][1];
3246                         if( *pt1 > *pt2 )
3247                         {
3248                                 pt11 = pt2;
3249                                 pt22 = pt1;
3250                         }
3251                         else
3252                         {
3253                                 pt11 = pt1;
3254                                 pt22 = pt2;
3255                         }
3256                         for( intpt2=pt11; *intpt2!=-1; )
3257                                 *intpt++ = *intpt2++;
3258                         for( intpt2=pt22; *intpt2!=-1; )
3259                                 *intpt++ = *intpt2++;
3260                         *intpt = -1;
3261                 }
3262
3263                 minscore *= 0.5;
3264
3265                 len[k][0] = ( minscore - tmptmplen[im] );
3266                 len[k][1] = ( minscore - tmptmplen[jm] );
3267                 if( len[k][0] < 0.0 ) len[k][0] = 0.0;
3268                 if( len[k][1] < 0.0 ) len[k][1] = 0.0;
3269
3270                 if( dep ) dep[k].distfromtip = minscore;
3271 //              reporterr(       "\n##### dep[%d].distfromtip = %f\n", k, minscore );
3272
3273                 tmptmplen[im] = minscore;
3274
3275                 hist[im] = k;
3276                 nmemar[im] = nmemim + nmemjm;
3277
3278                 mindisfrom[im] = 999.9;
3279                 eff[im][jm-im] = 999.9;
3280                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
3281         {
3282                         i = acpti->pos;
3283             if( i != im && i != jm )
3284             {
3285                                 if( i < im )
3286                                 {
3287                                          miniim = i;
3288                                          maxiim = im;
3289                                          minijm = i;
3290                                          maxijm = jm;
3291                                 }
3292                                 else if( i < jm )
3293                                 {
3294                                          miniim = im;
3295                                          maxiim = i;
3296                                          minijm = i;
3297                                          maxijm = jm;
3298                                 }
3299                                 else
3300                                 {
3301                                          miniim = im;
3302                                          maxiim = i;
3303                                          minijm = jm;
3304                                          maxijm = i;
3305                                 }
3306                                 eff0 = eff[miniim][maxiim-miniim];
3307                                 eff1 = eff[minijm][maxijm-minijm];
3308 #if 0
3309                                 tmpdouble = eff[miniim][maxiim-miniim] =
3310                                 MIN( eff0, eff1 ) * sueff1 + ( eff0 + eff1 ) * sueff05; 
3311 #else
3312                 tmpdouble = eff[miniim][maxiim-miniim] =
3313                                 (clusterfuncpt[0])( eff0, eff1 );
3314 #endif
3315 #if 1
3316                                 if( tmpdouble < mindisfrom[i]  )
3317                                 {
3318                                         mindisfrom[i] = tmpdouble;
3319                                         nearest[i] = im;
3320                                 }
3321                                 if( tmpdouble < mindisfrom[im]  )
3322                                 {
3323                                         mindisfrom[im] = tmpdouble;
3324                                         nearest[im] = i;
3325                                 }
3326                                 if( nearest[i] == jm )
3327                                 {
3328                                         nearest[i] = im;
3329                                 }
3330 #endif
3331             }
3332         }
3333
3334                 treetmp = realloc( treetmp, strlen( tree[im] ) + strlen( tree[jm] ) + 100 ); // 22 de juubunn (:%7,:%7) %7 ha minus kamo
3335                 if( !treetmp )
3336                 {
3337                         reporterr(       "Cannot allocate treetmp\n" );
3338                         exit( 1 );
3339                 }
3340                 sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
3341                 free( tree[im] );
3342                 free( tree[jm] );
3343                 tree[im] = calloc( strlen( treetmp )+1, sizeof( char ) );
3344                 tree[jm] = NULL;
3345                 if( tree[im] == NULL )
3346                 {
3347                         reporterr(       "Cannot reallocate tree!\n" );
3348                         exit( 1 );
3349                 }
3350                 strcpy( tree[im], treetmp );
3351
3352                 acjmprev = ac[jm].prev; 
3353                 acjmnext = ac[jm].next; 
3354                 acjmprev->next = acjmnext;
3355                 if( acjmnext != NULL )
3356                         acjmnext->prev = acjmprev;
3357                 if( efffree )
3358                 {
3359                         free( (void *)eff[jm] ); eff[jm] = NULL;
3360                 }
3361
3362 #if 1 // muscle seems to miss this.
3363                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
3364                 {
3365                         i = acpti->pos;
3366                         if( nearest[i] == im ) 
3367                         {
3368                                 if( i < im )
3369                                 {
3370                                          miniim = i;
3371                                          maxiim = im;
3372                                 }
3373                                 else
3374                                 {
3375                                          miniim = im;
3376                                          maxiim = i;
3377                                 }
3378                                 if( eff[miniim][maxiim-miniim] > mindisfrom[i] )
3379                                         setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i );
3380                         }
3381                 }
3382 #endif
3383
3384
3385 #if 0
3386         reporterr(       "\noSTEP-%03d:\n", k+1 );
3387                 reporterr(       "len0 = %f\n", len[k][0] );
3388         for( i=0; topol[k][0][i]>-1; i++ ) reporterr(       " %03d", topol[k][0][i]+1 );
3389         reporterr(       "\n" );
3390                 reporterr(       "len1 = %f\n", len[k][1] );
3391         for( i=0; topol[k][1][i]>-1; i++ ) reporterr(       " %03d", topol[k][1][i]+1 );
3392         reporterr(       "\n\n" );
3393 #endif
3394     }
3395         fp = fopen( "infile.tree", "w" );
3396                 fprintf( fp, "%s\n", treetmp );
3397         fclose( fp );
3398
3399         free( tree[0] );
3400         free( tree );
3401         free( treetmp );
3402         free( nametmp );
3403         free( (void *)tmptmplen ); tmptmplen = NULL;
3404         free( hist ); hist = NULL;
3405         free( (char *)ac ); ac = NULL;
3406         free( (void *)nmemar ); nmemar = NULL;
3407         free( mindisfrom );
3408         free( nearest );
3409         free( testtopol );
3410         FreeIntMtx( inconsistent );
3411         FreeIntMtx( inconsistentpairlist );
3412         free( warned );
3413 }
3414
3415 void makecompositiontable_global( int *table, int *pointt )
3416 {
3417         int point;
3418
3419         while( ( point = *pointt++ ) != END_OF_VEC )
3420                 table[point]++;
3421 }
3422
3423 typedef struct _resetnearestthread_arg
3424 {
3425         int para;
3426 //      int thread_no;
3427         int im;
3428         int nseq;
3429         double **partmtx;
3430         double *mindist;
3431         int *nearest;
3432         char **seq;
3433         int **skiptable;
3434         int *tselfscore;
3435         int **pointt;
3436         int *nlen;
3437         double *result;
3438         int *joblist;
3439         Bchain **acpt;
3440         Bchain *ac;
3441 #ifdef enablemultithread
3442         pthread_mutex_t *mutex;
3443 #endif
3444 } resetnearestthread_arg_t;
3445
3446 static void *msaresetnearestthread( void *arg )
3447 {
3448         resetnearestthread_arg_t *targ = (resetnearestthread_arg_t *)arg;
3449 //      int thread_no = targ->thread_no;
3450         int para = targ->para;
3451         int im = targ->im;
3452         int nseq = targ->nseq;
3453         double **partmtx = targ->partmtx;
3454         double *mindist = targ->mindist;
3455         int *nearest = targ->nearest;
3456         char **seq = targ->seq;
3457         int **skiptable = targ->skiptable;
3458         int *tselfscore = targ->tselfscore;
3459         double *result = targ->result;
3460         int *joblist = targ->joblist;
3461         Bchain **acpt = targ->acpt;
3462         Bchain *ac = targ->ac;
3463
3464         Bchain *acptbk;
3465         Bchain *acptinit;
3466
3467         int i;
3468
3469         acptinit = *acpt;
3470         while( 1 )
3471         {
3472 #ifdef enablemultithread
3473                 if( para ) pthread_mutex_lock( targ->mutex );
3474 #endif 
3475                 if( *acpt == NULL )
3476                 {
3477 #ifdef enablemultithread
3478                         if( para ) pthread_mutex_unlock( targ->mutex );
3479 #endif
3480                         commonsextet_p( NULL, NULL );
3481                         return( NULL );
3482                 }
3483                 acptbk = *acpt;
3484                 *acpt = (*acpt)->next;
3485
3486 #ifdef enablemultithread
3487                 if( para ) pthread_mutex_unlock( targ->mutex );
3488 #endif
3489                 i = acptbk->pos;
3490                 if( nearest[i] == im ) 
3491                 {
3492                         if( partmtx[im][i] > mindist[i] )
3493                         {
3494                                 msaresetnearest( nseq, ac, partmtx, mindist+i, nearest+i, i, seq, skiptable, tselfscore, result, joblist );
3495                         }
3496                 }
3497         }
3498 }
3499
3500 static void *kmerresetnearestthread( void *arg )
3501 {
3502         resetnearestthread_arg_t *targ = (resetnearestthread_arg_t *)arg;
3503 //      int thread_no = targ->thread_no;
3504         int para = targ->para;
3505         int im = targ->im;
3506         int nseq = targ->nseq;
3507         double **partmtx = targ->partmtx;
3508         double *mindist = targ->mindist;
3509         int *nearest = targ->nearest;
3510         int *tselfscore = targ->tselfscore;
3511         int **pointt = targ->pointt;
3512         int *nlen = targ->nlen;
3513         double *result = targ->result;
3514         int *joblist = targ->joblist;
3515         Bchain **acpt = targ->acpt;
3516         Bchain *ac = targ->ac;
3517
3518         int *singlettable1;
3519
3520         Bchain *acptbk;
3521         Bchain *acptinit;
3522
3523         int i;
3524
3525         acptinit = *acpt;
3526         while( 1 )
3527         {
3528 #ifdef enablemultithread
3529                 if( para ) pthread_mutex_lock( targ->mutex );
3530 #endif 
3531                 if( *acpt == NULL )
3532                 {
3533 #ifdef enablemultithread
3534                         if( para ) pthread_mutex_unlock( targ->mutex );
3535 #endif
3536                         commonsextet_p( NULL, NULL );
3537                         return( NULL );
3538                 }
3539                 acptbk = *acpt;
3540                 *acpt = (*acpt)->next;
3541
3542 #ifdef enablemultithread
3543                 if( para ) pthread_mutex_unlock( targ->mutex );
3544 #endif
3545                 i = acptbk->pos;
3546                 if( nearest[i] == im ) 
3547                 {
3548                         if( partmtx[im][i] > mindist[i] )
3549                         {
3550                                 if( pointt ) // kmer
3551                                 {
3552                                         singlettable1 = (int *)calloc( tsize, sizeof( int ) );
3553                                         makecompositiontable_global( singlettable1, pointt[i] );
3554                                 }
3555                                 kmerresetnearest( nseq, ac, partmtx, mindist+i, nearest+i, i, tselfscore, pointt, nlen, singlettable1, result, joblist );
3556                                 if( pointt ) free( singlettable1 ); singlettable1 = NULL;// kmer
3557                                 if( pointt ) commonsextet_p( NULL, NULL );
3558                         }
3559                 }
3560         }
3561 }
3562
3563
3564 typedef struct _compactdistarrthread_arg
3565 {
3566         int para;
3567         int njob;
3568 //      int thread_no;
3569         int im;
3570         int jm;
3571         int *nlen;
3572         char **seq;
3573         int **skiptable;
3574         int **pointt;
3575         int *table1;
3576         int *table2;
3577         int *tselfscore;
3578         Bchain **acpt;
3579         int *posshared;
3580         double *mindist;
3581         double *newarr;
3582         double **partmtx;
3583         int *nearest;
3584         int *joblist;
3585 #ifdef enablemultithread
3586         pthread_mutex_t *mutex;
3587 #endif
3588 } compactdistarrthread_arg_t;
3589
3590 static void *verycompactkmerdistarrthreadjoblist( void *arg ) // enablemultithread == 0 demo tsukau
3591 {
3592         compactdistarrthread_arg_t *targ = (compactdistarrthread_arg_t *)arg;
3593         int njob = targ->njob;
3594         int para = targ->para;
3595         int im = targ->im;
3596         int jm = targ->jm;
3597 //      int thread_no = targ->thread_no;
3598         int *nlen = targ->nlen;
3599         int **pointt = targ->pointt;
3600         int *table1 = targ->table1;
3601         int *table2 = targ->table2;
3602         int *tselfscore = targ->tselfscore;
3603         int *joblist = targ->joblist;
3604         int *posshared = targ->posshared;
3605         double *mindist = targ->mindist;
3606         int *nearest = targ->nearest;
3607 //      double **partmtx = targ->partmtx;
3608         double *newarr = targ->newarr;
3609         int i, posinjoblist, n;
3610
3611         double tmpdist1;
3612         double tmpdist2;
3613         double tmpdouble;
3614
3615 //                      for( acpti=ac; acpti!=NULL; acpti=acpti->next )
3616
3617         while( 1 )
3618         {
3619 #ifdef enablemultithread
3620                 if( para ) pthread_mutex_lock( targ->mutex );
3621 #endif 
3622                 if( *posshared >= njob ) // block no toki >=
3623                 {
3624 #ifdef enablemultithread
3625                         if( para ) pthread_mutex_unlock( targ->mutex );
3626 #endif
3627                         commonsextet_p( NULL, NULL );
3628                         return( NULL );
3629                 }
3630                 posinjoblist = *posshared;
3631                 *posshared += BLOCKSIZE;
3632 #ifdef enablemultithread
3633                 if( para ) pthread_mutex_unlock( targ->mutex );
3634 #endif
3635
3636                 for( n=0; n<BLOCKSIZE&&posinjoblist<njob; n++ )
3637                 {
3638                         i = joblist[posinjoblist++];
3639
3640                         if( i == im ) continue;
3641                         if( i == jm ) continue;
3642         
3643 //                      if( partmtx[im] )
3644 //                              tmpdist1 = partmtx[im][i];
3645 //                      else if( partmtx[i] )
3646 //                              tmpdist1 = partmtx[i][im];
3647 //                      else
3648                                 tmpdist1 = distcompact( nlen[im], nlen[i], table1, pointt[i], tselfscore[im], tselfscore[i] );
3649                                         
3650 //                      if( partmtx[jm] )
3651 //                              tmpdist2 = partmtx[jm][i];
3652 //                      else if( partmtx[i] )
3653 //                              tmpdist2 = partmtx[i][jm];
3654 //                      else
3655                                 tmpdist2 = distcompact( nlen[jm], nlen[i], table2, pointt[i], tselfscore[jm], tselfscore[i] );
3656         
3657 //                      if( seq )
3658 //                      {
3659 //                              tmpdist1 = distcompact_msa( seq[im], seq[i], skiptable[im], skiptable[i], tselfscore[im], tselfscore[i] );
3660 //                              tmpdist2 = distcompact_msa( seq[jm], seq[i], skiptable[jm], skiptable[i], tselfscore[jm], tselfscore[i] );
3661 //                      }
3662 //                      else
3663 //                      {
3664 //                              tmpdist1 = distcompact( nlen[im], nlen[i], table1, pointt[i], tselfscore[im], tselfscore[i] );
3665 //                              tmpdist2 = distcompact( nlen[jm], nlen[i], table2, pointt[i], tselfscore[jm], tselfscore[i] );
3666 //                      }
3667                         tmpdouble = cluster_mix_double( tmpdist1, tmpdist2 );
3668                         newarr[i] = tmpdouble;
3669         
3670 //                      if( partmtx[i] ) partmtx[i][im] = partmtx[i][jm] = newarr[i];
3671         
3672                         if( tmpdouble < mindist[i]  )
3673                         {
3674                                 mindist[i] = tmpdouble;
3675                                 nearest[i] = im;
3676                         }
3677         
3678 //                      if( tmpdouble < mindist[im]  ) // koko deha muri
3679 //                      {
3680 //                              mindist[im] = tmpdouble;
3681 //                              nearest[im] = i;
3682 //                      }
3683         
3684                         if( nearest[i] == jm )
3685                         {
3686                                 nearest[i] = im;
3687                         }
3688                 }
3689         }
3690 }
3691
3692 static void *kmerdistarrthreadjoblist( void *arg ) // enablemultithread == 0 demo tsukau
3693 {
3694         compactdistarrthread_arg_t *targ = (compactdistarrthread_arg_t *)arg;
3695         int njob = targ->njob;
3696         int para = targ->para;
3697         int im = targ->im;
3698         int jm = targ->jm;
3699 //      int thread_no = targ->thread_no;
3700         int *nlen = targ->nlen;
3701         int **pointt = targ->pointt;
3702         int *table1 = targ->table1;
3703         int *table2 = targ->table2;
3704         int *tselfscore = targ->tselfscore;
3705         int *joblist = targ->joblist;
3706         int *posshared = targ->posshared;
3707         double *mindist = targ->mindist;
3708         int *nearest = targ->nearest;
3709         double **partmtx = targ->partmtx;
3710         double *newarr = targ->newarr;
3711         int i, posinjoblist, n;
3712
3713         double tmpdist1;
3714         double tmpdist2;
3715         double tmpdouble;
3716
3717 //                      for( acpti=ac; acpti!=NULL; acpti=acpti->next )
3718
3719         while( 1 )
3720         {
3721 #ifdef enablemultithread
3722                 if( para ) pthread_mutex_lock( targ->mutex );
3723 #endif 
3724                 if( *posshared >= njob ) // block no toki >=
3725                 {
3726 #ifdef enablemultithread
3727                         if( para ) pthread_mutex_unlock( targ->mutex );
3728 #endif
3729                         commonsextet_p( NULL, NULL );
3730                         return( NULL );
3731                 }
3732                 posinjoblist = *posshared;
3733                 *posshared += BLOCKSIZE;
3734 #ifdef enablemultithread
3735                 if( para ) pthread_mutex_unlock( targ->mutex );
3736 #endif
3737
3738                 for( n=0; n<BLOCKSIZE&&posinjoblist<njob; n++ )
3739                 {
3740                         i = joblist[posinjoblist++];
3741
3742                         if( i == im ) continue;
3743                         if( i == jm ) continue;
3744         
3745                         if( partmtx[im] )
3746                                 tmpdist1 = partmtx[im][i];
3747                         else if( partmtx[i] )
3748                                 tmpdist1 = partmtx[i][im];
3749                         else
3750                                 tmpdist1 = distcompact( nlen[im], nlen[i], table1, pointt[i], tselfscore[im], tselfscore[i] );
3751                                         
3752                         if( partmtx[jm] )
3753                                 tmpdist2 = partmtx[jm][i];
3754                         else if( partmtx[i] )
3755                                 tmpdist2 = partmtx[i][jm];
3756                         else
3757                                 tmpdist2 = distcompact( nlen[jm], nlen[i], table2, pointt[i], tselfscore[jm], tselfscore[i] );
3758         
3759 //                      if( seq )
3760 //                      {
3761 //                              tmpdist1 = distcompact_msa( seq[im], seq[i], skiptable[im], skiptable[i], tselfscore[im], tselfscore[i] );
3762 //                              tmpdist2 = distcompact_msa( seq[jm], seq[i], skiptable[jm], skiptable[i], tselfscore[jm], tselfscore[i] );
3763 //                      }
3764 //                      else
3765 //                      {
3766 //                              tmpdist1 = distcompact( nlen[im], nlen[i], table1, pointt[i], tselfscore[im], tselfscore[i] );
3767 //                              tmpdist2 = distcompact( nlen[jm], nlen[i], table2, pointt[i], tselfscore[jm], tselfscore[i] );
3768 //                      }
3769                         tmpdouble = cluster_mix_double( tmpdist1, tmpdist2 );
3770                         newarr[i] = tmpdouble;
3771         
3772                         if( partmtx[i] ) partmtx[i][im] = partmtx[i][jm] = newarr[i];
3773         
3774                         if( tmpdouble < mindist[i]  )
3775                         {
3776                                 mindist[i] = tmpdouble;
3777                                 nearest[i] = im;
3778                         }
3779         
3780 //                      if( tmpdouble < mindist[im]  ) // koko deha muri
3781 //                      {
3782 //                              mindist[im] = tmpdouble;
3783 //                              nearest[im] = i;
3784 //                      }
3785         
3786                         if( nearest[i] == jm )
3787                         {
3788                                 nearest[i] = im;
3789                         }
3790                 }
3791         }
3792 }
3793
3794 static void *verycompactmsadistarrthreadjoblist( void *arg ) // enablemultithread == 0 demo tsukau
3795 {
3796         compactdistarrthread_arg_t *targ = (compactdistarrthread_arg_t *)arg;
3797         int njob = targ->njob;
3798         int para = targ->para;
3799         int im = targ->im;
3800         int jm = targ->jm;
3801 //      int thread_no = targ->thread_no;
3802         int *tselfscore = targ->tselfscore;
3803         char **seq = targ->seq;
3804         int **skiptable = targ->skiptable;
3805         int *joblist = targ->joblist;
3806         int *posshared = targ->posshared;
3807         double *mindist = targ->mindist;
3808         int *nearest = targ->nearest;
3809 //      double **partmtx = targ->partmtx;
3810         double *newarr = targ->newarr;
3811         int i, posinjoblist, n;
3812
3813         double tmpdist1;
3814         double tmpdist2;
3815         double tmpdouble;
3816
3817 //                      for( acpti=ac; acpti!=NULL; acpti=acpti->next )
3818
3819
3820         while( 1 )
3821         {
3822 #ifdef enablemultithread
3823                 if( para ) pthread_mutex_lock( targ->mutex );
3824 #endif 
3825                 if( *posshared >= njob ) // block no toki >=
3826                 {
3827 #ifdef enablemultithread
3828                         if( para ) pthread_mutex_unlock( targ->mutex );
3829 #endif
3830                         commonsextet_p( NULL, NULL );
3831                         return( NULL );
3832                 }
3833                 posinjoblist = *posshared;
3834                 *posshared += BLOCKSIZE;
3835 #ifdef enablemultithread
3836                 if( para ) pthread_mutex_unlock( targ->mutex );
3837 #endif
3838
3839                 for( n=0; n<BLOCKSIZE&&posinjoblist<njob; n++ )
3840                 {
3841                         i = joblist[posinjoblist++];
3842
3843                         if( i == im ) continue;
3844                         if( i == jm ) continue;
3845
3846 //                      if( partmtx[im] )
3847 //                              tmpdist1 = partmtx[im][i];
3848 //                      else if( partmtx[i] )
3849 //                              tmpdist1 = partmtx[i][im];
3850 //                      else
3851                                 tmpdist1 = distcompact_msa( seq[im], seq[i], skiptable[im], skiptable[i], tselfscore[im], tselfscore[i] );
3852                                         
3853 //                      if( partmtx[jm] )
3854 //                              tmpdist2 = partmtx[jm][i];
3855 //                      else if( partmtx[i] )
3856 //                              tmpdist2 = partmtx[i][jm];
3857 //                      else
3858                                 tmpdist2 = distcompact_msa( seq[jm], seq[i], skiptable[jm], skiptable[i], tselfscore[jm], tselfscore[i] );
3859         
3860                         tmpdouble = cluster_mix_double( tmpdist1, tmpdist2 );
3861                         newarr[i] = tmpdouble;
3862         
3863 //                      if( partmtx[i] ) partmtx[i][im] = partmtx[i][jm] = newarr[i];
3864         
3865                         if( tmpdouble < mindist[i]  )
3866                         {
3867                                 mindist[i] = tmpdouble;
3868                                 nearest[i] = im;
3869                         }
3870         
3871 //                      if( tmpdouble < mindist[im]  ) // koko deha muri
3872 //                      {
3873 //                              mindist[im] = tmpdouble;
3874 //                              nearest[im] = i;
3875 //                      }
3876         
3877                         if( nearest[i] == jm )
3878                         {
3879                                 nearest[i] = im;
3880                         }
3881                 }
3882         }
3883 }
3884
3885 static void *msadistarrthreadjoblist( void *arg ) // enablemultithread == 0 demo tsukau
3886 {
3887         compactdistarrthread_arg_t *targ = (compactdistarrthread_arg_t *)arg;
3888         int njob = targ->njob;
3889         int para = targ->para;
3890         int im = targ->im;
3891         int jm = targ->jm;
3892 //      int thread_no = targ->thread_no;
3893         int *tselfscore = targ->tselfscore;
3894         char **seq = targ->seq;
3895         int **skiptable = targ->skiptable;
3896         int *joblist = targ->joblist;
3897         int *posshared = targ->posshared;
3898         double *mindist = targ->mindist;
3899         int *nearest = targ->nearest;
3900         double **partmtx = targ->partmtx;
3901         double *newarr = targ->newarr;
3902         int i, posinjoblist, n;
3903
3904         double tmpdist1;
3905         double tmpdist2;
3906         double tmpdouble;
3907
3908 //                      for( acpti=ac; acpti!=NULL; acpti=acpti->next )
3909
3910
3911         while( 1 )
3912         {
3913 #ifdef enablemultithread
3914                 if( para ) pthread_mutex_lock( targ->mutex );
3915 #endif 
3916                 if( *posshared >= njob ) // block no toki >=
3917                 {
3918 #ifdef enablemultithread
3919                         if( para ) pthread_mutex_unlock( targ->mutex );
3920 #endif
3921                         commonsextet_p( NULL, NULL );
3922                         return( NULL );
3923                 }
3924                 posinjoblist = *posshared;
3925                 *posshared += BLOCKSIZE;
3926 #ifdef enablemultithread
3927                 if( para ) pthread_mutex_unlock( targ->mutex );
3928 #endif
3929
3930                 for( n=0; n<BLOCKSIZE&&posinjoblist<njob; n++ )
3931                 {
3932                         i = joblist[posinjoblist++];
3933
3934                         if( i == im ) continue;
3935                         if( i == jm ) continue;
3936
3937                         if( partmtx[im] )
3938                                 tmpdist1 = partmtx[im][i];
3939                         else if( partmtx[i] )
3940                                 tmpdist1 = partmtx[i][im];
3941                         else
3942                                 tmpdist1 = distcompact_msa( seq[im], seq[i], skiptable[im], skiptable[i], tselfscore[im], tselfscore[i] );
3943                                         
3944                         if( partmtx[jm] )
3945                                 tmpdist2 = partmtx[jm][i];
3946                         else if( partmtx[i] )
3947                                 tmpdist2 = partmtx[i][jm];
3948                         else
3949                                 tmpdist2 = distcompact_msa( seq[jm], seq[i], skiptable[jm], skiptable[i], tselfscore[jm], tselfscore[i] );
3950         
3951                         tmpdouble = cluster_mix_double( tmpdist1, tmpdist2 );
3952                         newarr[i] = tmpdouble;
3953         
3954                         if( partmtx[i] ) partmtx[i][im] = partmtx[i][jm] = newarr[i];
3955         
3956                         if( tmpdouble < mindist[i]  )
3957                         {
3958                                 mindist[i] = tmpdouble;
3959                                 nearest[i] = im;
3960                         }
3961         
3962 //                      if( tmpdouble < mindist[im]  ) // koko deha muri
3963 //                      {
3964 //                              mindist[im] = tmpdouble;
3965 //                              nearest[im] = i;
3966 //                      }
3967         
3968                         if( nearest[i] == jm )
3969                         {
3970                                 nearest[i] = im;
3971                         }
3972                 }
3973         }
3974 }
3975
3976 void compacttree_memsaveselectable( int nseq, double **partmtx, int *nearest, double *mindist, int **pointt, int *tselfscore, char **seq, int **skiptable, int ***topol, double **len, char **name, int *nlen, Treedep *dep, int treeout, int howcompact, int memsave )
3977 {
3978         int i, j, k;
3979 //      int miniim, maxiim, minijm, maxijm;
3980         int *intpt, *intpt2;
3981 //      double tmpdouble;
3982 //      double eff1, eff0;
3983         double *tmptmplen = NULL; //static?
3984         int *hist = NULL; //static?
3985         Bchain *ac = NULL; //static?
3986         int im = -1, jm = -1;
3987         Bchain *acjmnext, *acjmprev;
3988         int prevnode;
3989         Bchain *acpti;
3990         int *pt1, *pt2, *pt11, *pt22;
3991         int *nmemar; //static?
3992         int nmemim, nmemjm;
3993         double minscore;
3994         char **tree; //static?
3995         char *treetmp; //static?
3996         char *nametmp, *nameptr, *tmpptr; //static?
3997         FILE *fp;
3998         double (*clusterfuncpt[1])(double,double);
3999         char namec;
4000         int *singlettable1 = NULL;
4001         int *singlettable2 = NULL;
4002         double *newarr;
4003         void *(*distarrfunc)( void * );
4004         void *(*resetnearestfunc)( void * );
4005         int numfilled;
4006         int nthreadtree;
4007         compactdistarrthread_arg_t *distarrarg;
4008         resetnearestthread_arg_t *resetarg;
4009         int *joblist, nactive, posshared;
4010         double *result;
4011
4012
4013         sueff1 = 1 - (double)sueff_global;
4014         sueff05 = (double)sueff_global * 0.5;
4015         if ( treemethod == 'X' )
4016                 clusterfuncpt[0] = cluster_mix_double;
4017         else
4018         {
4019                 reporterr(       "Unknown treemethod, %c\n", treemethod );
4020                 exit( 1 );
4021         }
4022
4023         if( howcompact == 2 )
4024         {
4025                 if( seq )
4026                 {
4027 //                      distarrfunc = verycompactmsadistarrthread;
4028                         distarrfunc = verycompactmsadistarrthreadjoblist;
4029                         resetnearestfunc = NULL;
4030                 }
4031                 else
4032                 {
4033 //                      distarrfunc = verycompactkmerdistarrthread;
4034                         distarrfunc = verycompactkmerdistarrthreadjoblist;
4035                         resetnearestfunc = NULL;
4036                 }
4037         }
4038         else
4039         {
4040                 if( seq )
4041                 {
4042                         distarrfunc = msadistarrthreadjoblist;
4043                         resetnearestfunc = msaresetnearestthread;
4044                 }       
4045                 else
4046                 {
4047                         distarrfunc = kmerdistarrthreadjoblist;
4048                         resetnearestfunc = kmerresetnearestthread;
4049                 }
4050         }
4051         distarrarg = calloc( MAX( nthread, 1 ), sizeof( compactdistarrthread_arg_t ) );
4052         resetarg = calloc( MAX( nthread, 1 ), sizeof( resetnearestthread_arg_t ) );
4053         joblist = calloc( njob, sizeof( int ) );
4054         if( howcompact != 2 ) result = calloc( njob, sizeof( double ) );
4055         else result = NULL;
4056
4057         if( !hist )
4058         {
4059                 hist = AllocateIntVec( njob );
4060                 tmptmplen = AllocateFloatVec( njob );
4061                 ac = (Bchain *)malloc( njob * sizeof( Bchain ) );
4062                 nmemar = AllocateIntVec( njob );
4063                 if( treeout )
4064                 {
4065                         treetmp = NULL; // kentou 2013/06/12
4066                         nametmp = AllocateCharVec( 1000 ); // nagasugi
4067                         tree = AllocateCharMtx( njob, 0 );
4068                 }
4069         }
4070
4071         
4072         if( treeout )
4073         {
4074             for( i=0; i<nseq; i++ )
4075                 {
4076                         for( j=0; j<999; j++ ) nametmp[j] = 0;
4077                         for( j=0; j<999; j++ ) 
4078                         {
4079                                 namec = name[i][j];
4080                                 if( namec == 0 )
4081                                         break;
4082                                 else if( isalnum( namec ) || namec == '/' || namec == '=' || namec == '-' || namec == '{' || namec == '}' )
4083                                         nametmp[j] = namec;
4084                                 else
4085                                         nametmp[j] = '_';
4086                         }
4087                         nametmp[j] = 0;
4088 //                      sprintf( tree[i], "%d_l=%d_%.20s", i+1, nlen[i], nametmp+1 );
4089                         if( outnumber )
4090                                 nameptr = strstr( nametmp, "_numo_e" ) + 8;
4091                         else
4092                                 nameptr = nametmp + 1;
4093         
4094                         if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
4095         
4096                         tree[i] = calloc( strlen( nametmp )+100, sizeof( char ) ); // suuji no bun de +100
4097                         if( tree[i] == NULL )
4098                         {
4099                                 reporterr(       "Cannot allocate tree!\n" );
4100                                 exit( 1 );
4101                         }
4102                         sprintf( tree[i], "\n%d_%.900s\n", i+1, nameptr );
4103                 }
4104         }
4105
4106         for( i=0; i<nseq; i++ )
4107         {
4108                 ac[i].next = ac+i+1;
4109                 ac[i].prev = ac+i-1;
4110                 ac[i].pos = i;
4111         }
4112         ac[nseq-1].next = NULL;
4113
4114 //      for( i=0; i<nseq; i++ ) setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i ); // muscle
4115
4116         for( i=0; i<nseq; i++ ) tmptmplen[i] = 0.0;
4117         for( i=0; i<nseq; i++ ) 
4118         {
4119                 hist[i] = -1;
4120                 nmemar[i] = 1;
4121         }
4122
4123         for( i=0,numfilled=0; i<nseq; i++ ) if( partmtx[i] ) numfilled++;
4124         reporterr(       "\n" );
4125         for( k=0; k<nseq-1; k++ )
4126         {
4127                 if( k % 100 == 0 ) reporterr(       "\r% 5d / %d", k, nseq );
4128
4129 //              for( i=0,j=0; i<nseq; i++ ) if( partmtx[i] ) j++;
4130 //              if( k% 100 == 0 ) reporterr( "numfilled=%d, filledinpartmtx=%d, numempty=%d\n", numfilled, j, nseq-k-numfilled );
4131
4132                 minscore = 999.9;
4133                 for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
4134                 {
4135                         i = acpti->pos;
4136 //                      printf(       "k=%d i=%d, mindist[i]=%f\n", k, i, mindist[i] );
4137                         if( mindist[i] < minscore ) // muscle
4138                         {
4139                                 im = i;
4140                                 minscore = mindist[i];
4141                         }
4142                 }
4143 //              printf(       "minscore=%f\n", minscore );
4144                 jm = nearest[im];
4145 //              printf(       "im=%d\n", im );
4146 //              printf(       "jm=%d\n", jm );
4147
4148
4149                 if( jm < im ) 
4150                 {
4151                         j=jm; jm=im; im=j;
4152                 }
4153
4154                 if( partmtx[im] == NULL && howcompact != 2 ) numfilled++;
4155                 if( partmtx[jm] != NULL ) numfilled--;
4156
4157                 prevnode = hist[im];
4158                 if( dep ) dep[k].child0 = prevnode;
4159                 nmemim = nmemar[im];
4160                 if( memsave )
4161                         intpt = topol[k][0] = (int *)realloc( topol[k][0], ( 2 ) * sizeof( int ) ); // memsave
4162                 else
4163                         intpt = topol[k][0] = (int *)realloc( topol[k][0], ( nmemim + 1 ) * sizeof( int ) ); // memsave
4164                 if( prevnode == -1 )
4165                 {
4166                         *intpt++ = im;
4167                         *intpt = -1;
4168                 }
4169                 else
4170                 {
4171                         pt1 = topol[prevnode][0];
4172                         pt2 = topol[prevnode][1];
4173                         if( *pt1 > *pt2 )
4174                         {
4175                                 pt11 = pt2;
4176 //                              pt22 = pt1;
4177                         }
4178                         else
4179                         {
4180                                 pt11 = pt1;
4181 //                              pt22 = pt2;
4182                         }
4183                         if( memsave )
4184                         {
4185                                 *intpt++ = *pt11;
4186                                 *intpt = -1;
4187                         }
4188                         else
4189                         {
4190                                 for( intpt2=pt11; *intpt2!=-1; )
4191                                         *intpt++ = *intpt2++;
4192                                 for( intpt2=pt22; *intpt2!=-1; )
4193                                         *intpt++ = *intpt2++;
4194                                 *intpt = -1;
4195                         }
4196                 }
4197
4198                 prevnode = hist[jm];
4199                 if( dep ) dep[k].child1 = prevnode;
4200                 nmemjm = nmemar[jm];
4201                 if( memsave )
4202                         intpt = topol[k][1] = (int *)realloc( topol[k][1], ( 2 ) * sizeof( int ) ); // memsave
4203                 else
4204                         intpt = topol[k][1] = (int *)realloc( topol[k][1], ( nmemjm + 1 ) * sizeof( int ) ); // memsave
4205                 if( !intpt )
4206                 {
4207                         reporterr(       "Cannot reallocate topol\n" );
4208                         exit( 1 );
4209                 }
4210                 if( prevnode == -1 )
4211                 {
4212                         *intpt++ = jm;
4213                         *intpt = -1;
4214                 }
4215                 else
4216                 {
4217                         pt1 = topol[prevnode][0];
4218                         pt2 = topol[prevnode][1];
4219                         if( *pt1 > *pt2 )
4220                         {
4221                                 pt11 = pt2;
4222 //                              pt22 = pt1;
4223                         }
4224                         else
4225                         {
4226                                 pt11 = pt1;
4227 //                              pt22 = pt2;
4228                         }
4229                         if( memsave )
4230                         {
4231                                 *intpt++ = *pt11;
4232                                 *intpt = -1;
4233                         }
4234                         else
4235                         {
4236                                 for( intpt2=pt11; *intpt2!=-1; )
4237                                         *intpt++ = *intpt2++;
4238                                 for( intpt2=pt22; *intpt2!=-1; )
4239                                         *intpt++ = *intpt2++;
4240                                 *intpt = -1;
4241                         }
4242                 }
4243
4244                 minscore *= 0.5;
4245
4246 //              printf( "minscore = %f, tmptmplen[im] = %f, tmptmplen[jm] = %f\n", minscore, tmptmplen[im], tmptmplen[jm] );
4247
4248                 len[k][0] = ( minscore - tmptmplen[im] );
4249                 len[k][1] = ( minscore - tmptmplen[jm] );
4250
4251                 if( dep ) dep[k].distfromtip = minscore;
4252 //              reporterr(       "\n##### dep[%d].distfromtip = %f\n", k, minscore );
4253
4254                 tmptmplen[im] = minscore;
4255
4256                 hist[im] = k;
4257                 nmemar[im] = nmemim + nmemjm;
4258                 mindist[im] = 999.9;
4259
4260                 if( pointt ) // kmer
4261                 {
4262                         singlettable1 = (int *)calloc( tsize, sizeof( int ) );
4263                         singlettable2 = (int *)calloc( tsize, sizeof( int ) );
4264                         makecompositiontable_global( singlettable1, pointt[im] );
4265                         makecompositiontable_global( singlettable2, pointt[jm] );
4266                 }
4267
4268                 newarr = calloc( nseq, sizeof( double ) );
4269
4270 //              nthreadtree = MAX( 1, nthread );
4271                 nthreadtree = nthread;
4272
4273
4274                 for( acpti=ac,nactive=0; acpti!=NULL; acpti=acpti->next ) joblist[nactive++] = acpti->pos; // sukoshi muda...
4275
4276
4277 #ifdef enablemultithread
4278                 if( nthreadtree > 0 )
4279                 {
4280                         compactdistarrthread_arg_t *targ;
4281                         pthread_t *handle;
4282                         pthread_mutex_t mutex;
4283                 
4284                         posshared = 0;
4285 //                      targ = calloc( nthreadtree, sizeof( compactdistarrthread_arg_t ) );
4286                         targ = distarrarg;
4287                         handle = calloc( nthreadtree, sizeof( pthread_t ) );
4288                         pthread_mutex_init( &mutex, NULL );
4289                 
4290                         if( k % 100 == 0 ) reporterr( " (%d threads, nactive=%d, nfilled=%d)     \r", nthreadtree, nactive, numfilled );
4291                         for( i=0; i<nthreadtree; i++ )
4292                         {
4293                                 targ[i].para = 1;
4294                                 targ[i].njob = nactive;
4295 //                              targ[i].thread_no = i;
4296                                 targ[i].im = im;
4297                                 targ[i].jm = jm;
4298                                 targ[i].tselfscore = tselfscore;
4299                                 targ[i].nlen = nlen;
4300                                 targ[i].seq = seq;
4301                                 targ[i].skiptable = skiptable;
4302                                 targ[i].pointt = pointt;
4303                                 targ[i].table1 = singlettable1;
4304                                 targ[i].table2 = singlettable2;
4305                                 targ[i].joblist = joblist;
4306                                 targ[i].posshared = &posshared;
4307                                 targ[i].mindist = mindist;
4308                                 targ[i].nearest = nearest;
4309                                 targ[i].newarr = newarr;
4310                                 targ[i].partmtx = partmtx;
4311                                 targ[i].mutex = &mutex;
4312
4313                                 pthread_create( handle+i, NULL, distarrfunc, (void *)(targ+i) );
4314                         }
4315                 
4316                         for( j=0; j<nthreadtree; j++ ) pthread_join( handle[j], NULL );
4317                         pthread_mutex_destroy( &mutex );
4318                         free( handle );
4319 //                      free( targ );
4320         
4321 #if 0
4322                         for( acpti=ac; acpti!=NULL; acpti=acpti->next ) // antei sei no tame
4323                         {
4324                                 i = acpti->pos;
4325                                 if( i != im && i != jm )
4326                                 {
4327 //                                      if( partmtx[i] ) partmtx[i][im] = partmtx[i][jm] = newarr[i]; // heiretsu demo ii.
4328 //                                      if( newarr[i] < mindist[i]  )
4329 //                                      {
4330 //                                              mindist[i] = newarr[i];
4331 //                                              nearest[i] = im;
4332 //                                      }
4333                                         if( newarr[i] < mindist[im]  )
4334                                         {
4335                                                 mindist[im] = newarr[i];
4336                                                 nearest[im] = i;
4337                                         }
4338 //                                      if( nearest[i] == jm )
4339 //                                      {
4340 //                                              nearest[i] = im;
4341 //                                      }
4342                                 }
4343                         }
4344 #endif
4345                 }
4346                 else
4347 #endif
4348                 {
4349                         if( k % 100 == 0 ) reporterr( " (serial, nactive=%d, nfilled=%d)             \r", nactive, numfilled );
4350                         compactdistarrthread_arg_t *targ;
4351                 
4352                         posshared = 0;
4353 //                      targ = calloc( 1, sizeof( compactdistarrthread_arg_t ) );
4354                         targ = distarrarg;
4355                 
4356                         for( i=0; i<1; i++ )
4357                         {
4358                                 targ[i].para = 0;
4359                                 targ[i].njob = nactive;
4360 //                              targ[i].thread_no = i;
4361                                 targ[i].im = im;
4362                                 targ[i].jm = jm;
4363                                 targ[i].tselfscore = tselfscore;
4364                                 targ[i].nlen = nlen;
4365                                 targ[i].seq = seq;
4366                                 targ[i].skiptable = skiptable;
4367                                 targ[i].pointt = pointt;
4368                                 targ[i].table1 = singlettable1;
4369                                 targ[i].table2 = singlettable2;
4370                                 targ[i].joblist = joblist;
4371                                 targ[i].posshared = &posshared;
4372                                 targ[i].mindist = mindist;
4373                                 targ[i].nearest = nearest;
4374                                 targ[i].newarr = newarr;
4375                                 targ[i].partmtx = partmtx;
4376
4377                                 distarrfunc( targ+i );
4378 //                              pthread_create( handle, NULL, distarrfunc, (void *)(targ) );
4379                         }
4380
4381 //                      free( targ );
4382         
4383                 }
4384
4385                 for( acpti=ac; acpti!=NULL; acpti=acpti->next ) // antei sei no tame
4386                 {
4387                         i = acpti->pos;
4388                         if( i != im && i != jm )
4389                         {
4390 //                              if( partmtx[i] ) partmtx[i][im] = partmtx[i][jm] = newarr[i]; // heiretsu demo ii.
4391 //                              if( newarr[i] < mindist[i]  )
4392 //                              {
4393 //                                      mindist[i] = newarr[i];
4394 //                                      nearest[i] = im;
4395 //                              }
4396                                 if( newarr[i] < mindist[im]  )
4397                                 {
4398                                         mindist[im] = newarr[i];
4399                                         nearest[im] = i;
4400                                 }
4401 //                              if( nearest[i] == jm )
4402 //                              {
4403 //                                      nearest[i] = im;
4404 //                              }
4405                         }
4406                 }
4407
4408 //              printf( "im=%d, jm=%d\n", im, jm );
4409 #if 0
4410                 printf( "matrix = \n" );
4411                 for( i=0; i<njob; i++ )
4412                 {
4413                         if( partmtx[i] ) for( j=0; j<njob; j++ ) printf( "%f ", partmtx[i][j] );
4414                         else printf( "nai" );
4415                         printf( "\n" );
4416                         
4417                 }
4418 #endif
4419 //              if( k%500 == 0 )
4420 //              {
4421 //                      reporterr( "at step %d,", k );
4422 //                      use_getrusage();
4423 //              }
4424
4425                 if( partmtx[im] ) free( partmtx[im] ); partmtx[im] = NULL;
4426                 if( partmtx[jm] ) free( partmtx[jm] ); partmtx[jm] = NULL;
4427                 if( howcompact == 2 )
4428                 {
4429                         free( newarr );
4430                         newarr = NULL;
4431                 }
4432                 else
4433                 {
4434                         partmtx[im] = newarr;
4435                 }
4436
4437
4438                 if( pointt )
4439                 {
4440                         free( singlettable1 );
4441                         free( singlettable2 );
4442                         singlettable1 = NULL;
4443                         singlettable2 = NULL;
4444                 }
4445         
4446                 if( treeout )
4447                 {
4448                         treetmp = realloc( treetmp, strlen( tree[im] ) + strlen( tree[jm] ) + 100 ); // 22 de juubunn (:%7,:%7) %7 ha minus kamo
4449                         if( !treetmp )
4450                         {
4451                                 reporterr(       "Cannot allocate treetmp\n" );
4452                                 exit( 1 );
4453                         }
4454                         sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
4455                         free( tree[im] );
4456                         free( tree[jm] );
4457                         tree[im] = calloc( strlen( treetmp )+1, sizeof( char ) );
4458                         tree[jm] = NULL;
4459                         if( tree[im] == NULL )
4460                         {
4461                                 reporterr(       "Cannot reallocate tree!\n" );
4462                                 exit( 1 );
4463                         }
4464                         strcpy( tree[im], treetmp );
4465                 }
4466
4467                 acjmprev = ac[jm].prev; 
4468                 acjmnext = ac[jm].next; 
4469                 acjmprev->next = acjmnext;
4470                 if( acjmnext != NULL )
4471                         acjmnext->prev = acjmprev;
4472
4473 #if 0 // muscle seems to miss this.
4474 //              int nwork = 0;
4475                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
4476                 {
4477                         i = acpti->pos;
4478 //                      printf( "reset nearest? i=%d, k=%d, nearest[i]=%d, im=%d, mindist=%f\n", i, k, nearest[i], im, mindist[i] );
4479                         if( nearest[i] == im ) 
4480                         {
4481 //                              printf( "reset nearest, i=%d, k=%d\n", i, k );
4482                                 if( partmtx[im][i] > mindist[i] )
4483                                 {
4484 //                                      nwork++;
4485 //                                      printf( "go\n" );
4486                                         if( pointt ) // kmer
4487                                         {
4488                                                 singlettable1 = (int *)calloc( tsize, sizeof( int ) );
4489                                                 makecompositiontable_global( singlettable1, pointt[i] );
4490                                         }
4491                                         resetnearest( nseq, ac, partmtx, mindist+i, nearest+i, i, seq, skiptable, tselfscore, pointt, nlen, singlettable1 );
4492                                         if( pointt ) free( singlettable1 ); singlettable1 = NULL;// kmer
4493                                         if( pointt ) commonsextet_p( NULL, NULL );
4494                                 }
4495                         }
4496                 }
4497 //              reporterr( "nwork = %d\n", nwork );
4498 #else
4499
4500                 if( howcompact == 2 ) continue;
4501
4502 #if 0
4503                 if( 0 && nthreadtree > 0  )
4504                 {
4505                         resetnearestthread_arg_t *targ;
4506                         pthread_t *handle;
4507                         pthread_mutex_t mutex;
4508                         Bchain *acshared;
4509                 
4510                         acshared = ac;
4511 //                      targ = calloc( nthreadtree, sizeof( resetnearestthread_arg_t ) );
4512                         targ = resetarg;
4513                         handle = calloc( nthreadtree, sizeof( pthread_t ) );
4514                         pthread_mutex_init( &mutex, NULL );
4515                 
4516                         for( i=0; i<nthreadtree; i++ )
4517                         {
4518                                 targ[i].para = 1;
4519                                 targ[i].nseq = nseq;
4520                                 targ[i].im = im;
4521                                 targ[i].partmtx = partmtx;
4522                                 targ[i].mindist = mindist;
4523                                 targ[i].nearest = nearest;
4524                                 targ[i].seq = seq;
4525                                 targ[i].skiptable = skiptable;
4526                                 targ[i].tselfscore = tselfscore;
4527                                 targ[i].pointt = pointt;
4528                                 targ[i].nlen = nlen;
4529                                 targ[i].acpt = &acshared;
4530                                 targ[i].ac = ac;
4531                                 targ[i].mutex = &mutex;
4532
4533                                 pthread_create( handle+i, NULL, resetnearestfunc, (void *)(targ+i) );
4534                         }
4535                 
4536                         for( j=0; j<nthreadtree; j++ ) pthread_join( handle[j], NULL );
4537                         pthread_mutex_destroy( &mutex );
4538                         free( handle );
4539 //                      free( targ );
4540                 }
4541                 else
4542 #endif
4543                 {
4544                         Bchain *acshared;
4545                         acshared = ac;
4546                         resetnearestthread_arg_t *targ;
4547 //                      targ = calloc( 1, sizeof( resetnearestthread_arg_t ) );
4548                         targ = resetarg;
4549                         {
4550                                 targ[0].para = 0;
4551                                 targ[0].nseq = nseq;
4552                                 targ[0].im = im;
4553                                 targ[0].partmtx = partmtx;
4554                                 targ[0].mindist = mindist;
4555                                 targ[0].nearest = nearest;
4556                                 targ[0].seq = seq;
4557                                 targ[0].skiptable = skiptable;
4558                                 targ[0].tselfscore = tselfscore;
4559                                 targ[0].pointt = pointt;
4560                                 targ[0].nlen = nlen;
4561                                 targ[0].result = result;
4562                                 targ[0].joblist = joblist;
4563                                 targ[0].acpt = &acshared;
4564                                 targ[0].ac = ac;
4565
4566                                 resetnearestfunc( targ );
4567                         }
4568 //                      free( targ );
4569                 }
4570 #endif
4571
4572
4573 #if 0
4574         printf(       "\nooSTEP-%03d:\n", k+1 );
4575                 printf(       "len0 = %f\n", len[k][0] );
4576         for( i=0; topol[k][0][i]>-1; i++ ) printf(       " %03d", topol[k][0][i]+1 );
4577         printf(       "\n" );
4578                 printf(       "len1 = %f\n", len[k][1] );
4579         for( i=0; topol[k][1][i]>-1; i++ ) printf(       " %03d", topol[k][1][i]+1 );
4580         printf(       "\n" );
4581 #endif
4582     }
4583         if( treeout )
4584         {
4585                 fp = fopen( "infile.tree", "w" );
4586                 fprintf( fp, "%s\n", treetmp );
4587                 fclose( fp );
4588         }
4589
4590         for( im=0; im<nseq; im++ ) // im wo ugokasu hituyouha nai.
4591         {
4592                 if( partmtx[im] ) free( partmtx[im] ); partmtx[im] = NULL;
4593         }
4594 //      if( partmtx ) free( partmtx ); partmtx = NULL; // oya ga free
4595         if( treeout )
4596         {
4597                 free( tree[0] );
4598                 free( tree );
4599                 free( treetmp );
4600                 free( nametmp );
4601         }
4602         free( (void *)tmptmplen ); tmptmplen = NULL;
4603         free( hist ); hist = NULL;
4604         free( (char *)ac ); ac = NULL;
4605         free( (void *)nmemar ); nmemar = NULL;
4606         if( singlettable1 ) free( singlettable1 );
4607         if( singlettable2 ) free( singlettable2 );
4608         free( distarrarg );
4609         free( resetarg );
4610         free( joblist );
4611         if( result ) free( result );
4612 }
4613
4614 void fixed_musclesupg_double_realloc_nobk_halfmtx_treeout_memsave( int nseq, double **eff, int ***topol, double **len, char **name, int *nlen, Treedep *dep, int efffree )
4615 {
4616
4617         int i, j, k, miniim, maxiim, minijm, maxijm;
4618         int *intpt;
4619         double tmpdouble;
4620         double eff1, eff0;
4621         double *tmptmplen = NULL; //static?
4622         int *hist = NULL; //static?
4623         Bchain *ac = NULL; //static?
4624         int im = 1, jm = -1;
4625         Bchain *acjmnext, *acjmprev;
4626         int prevnode;
4627         Bchain *acpti;
4628         int *pt1, *pt2, *pt11;
4629         int *nmemar; //static?
4630         int nmemim, nmemjm;
4631         double minscore;
4632         int *nearest = NULL; // by D.Mathog, a guess
4633         double *mindisfrom = NULL; // by D.Mathog, a guess
4634         char **tree; //static?
4635         char *treetmp; //static?
4636         char *nametmp, *nameptr, *tmpptr; //static?
4637         FILE *fp;
4638         double (*clusterfuncpt[1])(double,double);
4639         char namec;
4640
4641
4642         sueff1 = 1 - (double)sueff_global;
4643         sueff05 = (double)sueff_global * 0.5;
4644         if ( treemethod == 'X' )
4645                 clusterfuncpt[0] = cluster_mix_double;
4646         else if ( treemethod == 'E' )
4647                 clusterfuncpt[0] = cluster_average_double;
4648         else if ( treemethod == 'q' )
4649                 clusterfuncpt[0] = cluster_minimum_double;
4650         else
4651         {
4652                 reporterr(       "Unknown treemethod, %c\n", treemethod );
4653                 exit( 1 );
4654         }
4655
4656         if( !hist )
4657         {
4658                 hist = AllocateIntVec( njob );
4659                 tmptmplen = AllocateFloatVec( njob );
4660                 ac = (Bchain *)malloc( njob * sizeof( Bchain ) );
4661                 nmemar = AllocateIntVec( njob );
4662                 mindisfrom = AllocateFloatVec( njob );
4663                 nearest = AllocateIntVec( njob );
4664 //              treetmp = AllocateCharVec( njob * ( B + 100 ) ); // nagasugi?
4665                 treetmp = NULL; // kentou 2013/06/12
4666                 nametmp = AllocateCharVec( 1000 ); // nagasugi
4667 //              tree = AllocateCharMtx( njob, njob*600 );
4668                 tree = AllocateCharMtx( njob, 0 );
4669         }
4670
4671         
4672     for( i=0; i<nseq; i++ )
4673         {
4674                 for( j=0; j<999; j++ ) nametmp[j] = 0;
4675                 for( j=0; j<999; j++ ) 
4676                 {
4677                         namec = name[i][j];
4678                         if( namec == 0 )
4679                                 break;
4680                         else if( isalnum( namec ) || namec == '/' || namec == '=' || namec == '-' || namec == '{' || namec == '}' )
4681                                 nametmp[j] = namec;
4682                         else
4683                                 nametmp[j] = '_';
4684                 }
4685                 nametmp[j] = 0;
4686 //              sprintf( tree[i], "%d_l=%d_%.20s", i+1, nlen[i], nametmp+1 );
4687                 if( outnumber )
4688                         nameptr = strstr( nametmp, "_numo_e" ) + 8;
4689                 else
4690                         nameptr = nametmp + 1;
4691
4692                 if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
4693
4694                 tree[i] = calloc( strlen( nametmp )+100, sizeof( char ) ); // suuji no bun de +100
4695                 if( tree[i] == NULL )
4696                 {
4697                         reporterr(       "Cannot allocate tree!\n" );
4698                         exit( 1 );
4699                 }
4700                 sprintf( tree[i], "\n%d_%.900s\n", i+1, nameptr );
4701         }
4702         for( i=0; i<nseq; i++ )
4703         {
4704                 ac[i].next = ac+i+1;
4705                 ac[i].prev = ac+i-1;
4706                 ac[i].pos = i;
4707         }
4708         ac[nseq-1].next = NULL;
4709
4710         for( i=0; i<nseq; i++ ) setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i ); // muscle
4711
4712
4713         for( i=0; i<nseq; i++ ) tmptmplen[i] = 0.0;
4714         for( i=0; i<nseq; i++ ) 
4715         {
4716                 hist[i] = -1;
4717                 nmemar[i] = 1;
4718         }
4719
4720         reporterr(       "\n" );
4721         for( k=0; k<nseq-1; k++ )
4722         {
4723                 if( k % 10 == 0 ) reporterr(       "\r% 5d / %d", k, nseq );
4724
4725                 minscore = 999.9;
4726                 for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
4727                 {
4728                         i = acpti->pos;
4729 //                      printf(       "k=%d i=%d, mindist[i]=%f\n", k, i, mindisfrom[i] );
4730                         if( mindisfrom[i] < minscore ) // muscle
4731                         {
4732                                 im = i;
4733                                 minscore = mindisfrom[i];
4734                         }
4735                 }
4736
4737 //              printf(       "minscore=%f\n", minscore );
4738                 jm = nearest[im];
4739 //              printf(       "im=%d\n", im );
4740 //              printf(       "jm=%d\n", jm );
4741                 if( jm < im ) 
4742                 {
4743                         j=jm; jm=im; im=j;
4744                 }
4745
4746
4747                 prevnode = hist[im];
4748                 if( dep ) dep[k].child0 = prevnode;
4749                 nmemim = nmemar[im];
4750                 intpt = topol[k][0] = (int *)realloc( topol[k][0], ( 2 ) * sizeof( int ) ); // memsave
4751                 if( prevnode == -1 )
4752                 {
4753                         *intpt++ = im;
4754                         *intpt = -1;
4755                 }
4756                 else
4757                 {
4758                         pt1 = topol[prevnode][0];
4759                         pt2 = topol[prevnode][1];
4760                         if( *pt1 > *pt2 )
4761                         {
4762                                 pt11 = pt2;
4763 //                              pt22 = pt1;
4764                         }
4765                         else
4766                         {
4767                                 pt11 = pt1;
4768 //                              pt22 = pt2;
4769                         }
4770 #if 1 // memsave
4771                         *intpt++ = *pt11;
4772                         *intpt = -1;
4773 #else
4774                         for( intpt2=pt11; *intpt2!=-1; )
4775                                 *intpt++ = *intpt2++;
4776                         for( intpt2=pt22; *intpt2!=-1; )
4777                                 *intpt++ = *intpt2++;
4778                         *intpt = -1;
4779 #endif
4780                 }
4781
4782                 prevnode = hist[jm];
4783                 if( dep ) dep[k].child1 = prevnode;
4784                 nmemjm = nmemar[jm];
4785                 intpt = topol[k][1] = (int *)realloc( topol[k][1], ( 2 ) * sizeof( int ) ); // memsave
4786                 if( !intpt )
4787                 {
4788                         reporterr(       "Cannot reallocate topol\n" );
4789                         exit( 1 );
4790                 }
4791                 if( prevnode == -1 )
4792                 {
4793                         *intpt++ = jm;
4794                         *intpt = -1;
4795                 }
4796                 else
4797                 {
4798                         pt1 = topol[prevnode][0];
4799                         pt2 = topol[prevnode][1];
4800                         if( *pt1 > *pt2 )
4801                         {
4802                                 pt11 = pt2;
4803 //                              pt22 = pt1;
4804                         }
4805                         else
4806                         {
4807                                 pt11 = pt1;
4808 //                              pt22 = pt2;
4809                         }
4810 #if 1 // memsave
4811                         *intpt++ = *pt11;
4812                         *intpt = -1;
4813 #else
4814                         for( intpt2=pt11; *intpt2!=-1; )
4815                                 *intpt++ = *intpt2++;
4816                         for( intpt2=pt22; *intpt2!=-1; )
4817                                 *intpt++ = *intpt2++;
4818                         *intpt = -1;
4819 #endif
4820                 }
4821
4822                 minscore *= 0.5;
4823
4824 //              printf( "minscore = %f, tmptmplen[im] = %f, tmptmplen[jm] = %f\n", minscore, tmptmplen[im], tmptmplen[jm] );
4825                 len[k][0] = ( minscore - tmptmplen[im] );
4826                 len[k][1] = ( minscore - tmptmplen[jm] );
4827
4828                 if( dep ) dep[k].distfromtip = minscore;
4829 //              reporterr(       "\n##### dep[%d].distfromtip = %f\n", k, minscore );
4830
4831                 tmptmplen[im] = minscore;
4832
4833                 hist[im] = k;
4834                 nmemar[im] = nmemim + nmemjm;
4835
4836                 mindisfrom[im] = 999.9;
4837                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
4838         {
4839                         i = acpti->pos;
4840             if( i != im && i != jm )
4841             {
4842                                 if( i < im )
4843                                 {
4844                                          miniim = i;
4845                                          maxiim = im;
4846                                          minijm = i;
4847                                          maxijm = jm;
4848                                 }
4849                                 else if( i < jm )
4850                                 {
4851                                          miniim = im;
4852                                          maxiim = i;
4853                                          minijm = i;
4854                                          maxijm = jm;
4855                                 }
4856                                 else
4857                                 {
4858                                          miniim = im;
4859                                          maxiim = i;
4860                                          minijm = jm;
4861                                          maxijm = i;
4862                                 }
4863                                 eff0 = eff[miniim][maxiim-miniim];
4864                                 eff1 = eff[minijm][maxijm-minijm];
4865 #if 0
4866                                 tmpdouble = eff[miniim][maxiim-miniim] =
4867                                 MIN( eff0, eff1 ) * sueff1 + ( eff0 + eff1 ) * sueff05; 
4868 #else
4869                 tmpdouble = eff[miniim][maxiim-miniim] =
4870                                 (clusterfuncpt[0])( eff0, eff1 );
4871 //                              printf( "tmpdouble=%f, eff0=%f, eff1=%f\n", tmpdouble, eff0, eff1 );
4872 #endif
4873                                 if( tmpdouble < mindisfrom[i]  )
4874                                 {
4875                                         mindisfrom[i] = tmpdouble;
4876                                         nearest[i] = im;
4877                                 }
4878                                 if( tmpdouble < mindisfrom[im]  )
4879                                 {
4880                                         mindisfrom[im] = tmpdouble;
4881                                         nearest[im] = i;
4882                                 }
4883                                 if( nearest[i] == jm )
4884                                 {
4885                                         nearest[i] = im;
4886                                 }
4887             }
4888         }
4889 //              printf( "im=%d, jm=%d\n", im, jm );
4890 #if 0
4891                 printf( "matrix = \n" );
4892                 for( i=0; i<njob; i++ )
4893                 {
4894                         for( j=0; j<njob; j++ ) 
4895                         {
4896                                 if( i>j )
4897                                 {
4898                                         minijm=j;
4899                                         maxijm=i;
4900                                 }
4901                                 else
4902                                 {
4903                                         minijm=i;
4904                                         maxijm=j;
4905                                 }
4906                                 printf( "%f ", eff[minijm][maxijm-minijm] );
4907                         }
4908                         printf( "\n" );
4909                 }
4910 #endif
4911
4912                 treetmp = realloc( treetmp, strlen( tree[im] ) + strlen( tree[jm] ) + 100 ); // 22 de juubunn (:%7,:%7) %7 ha minus kamo
4913                 if( !treetmp )
4914                 {
4915                         reporterr(       "Cannot allocate treetmp\n" );
4916                         exit( 1 );
4917                 }
4918                 sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
4919                 free( tree[im] );
4920                 free( tree[jm] );
4921                 tree[im] = calloc( strlen( treetmp )+1, sizeof( char ) );
4922                 tree[jm] = NULL;
4923                 if( tree[im] == NULL )
4924                 {
4925                         reporterr(       "Cannot reallocate tree!\n" );
4926                         exit( 1 );
4927                 }
4928                 strcpy( tree[im], treetmp );
4929
4930                 acjmprev = ac[jm].prev; 
4931                 acjmnext = ac[jm].next; 
4932                 acjmprev->next = acjmnext;
4933                 if( acjmnext != NULL )
4934                         acjmnext->prev = acjmprev;
4935                 if( efffree )
4936                 {
4937                         free( (void *)eff[jm] ); eff[jm] = NULL; // Ato de fukkatsu
4938                 }
4939
4940 #if 1 // muscle seems to miss this.
4941                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
4942                 {
4943                         i = acpti->pos;
4944 //                      printf( "reset nearest? i=%d, k=%d, nearest[i]=%d, im=%d, mindist=%f\n", i, k, nearest[i], im, mindisfrom[i] );
4945                         if( nearest[i] == im ) 
4946                         {
4947 //                              printf( "reset nearest, i=%d, k=%d\n", i, k );
4948                                 if( i < im )
4949                                 {
4950                                          miniim = i;
4951                                          maxiim = im;
4952                                 }
4953                                 else
4954                                 {
4955                                          miniim = im;
4956                                          maxiim = i;
4957                                 }
4958                                 if( eff[miniim][maxiim-miniim] > mindisfrom[i] )
4959                                 {
4960 //                                      printf( "go\n" );
4961                                         setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i );
4962                                 }
4963                         }
4964                 }
4965 #else
4966                 reporterr( "CHUUI!\n" );
4967 #endif
4968
4969
4970 #if 0
4971         printf(       "\nooSTEP-%03d:\n", k+1 );
4972                 printf(       "len0 = %f\n", len[k][0] );
4973         for( i=0; topol[k][0][i]>-1; i++ ) printf(       " %03d", topol[k][0][i]+1 );
4974         printf(       "\n" );
4975                 printf(       "len1 = %f\n", len[k][1] );
4976         for( i=0; topol[k][1][i]>-1; i++ ) printf(       " %03d", topol[k][1][i]+1 );
4977         printf(       "\n" );
4978 #endif
4979     }
4980         fp = fopen( "infile.tree", "w" );
4981                 fprintf( fp, "%s\n", treetmp );
4982         fclose( fp );
4983
4984         free( tree[0] );
4985         free( tree );
4986         free( treetmp );
4987         free( nametmp );
4988         free( (void *)tmptmplen ); tmptmplen = NULL;
4989         free( hist ); hist = NULL;
4990         free( (char *)ac ); ac = NULL;
4991         free( (void *)nmemar ); nmemar = NULL;
4992         free( mindisfrom );
4993         free( nearest );
4994 }
4995
4996 void fixed_musclesupg_double_realloc_nobk_halfmtx_treeout( int nseq, double **eff, int ***topol, double **len, char **name, int *nlen, Treedep *dep, int efffree )
4997 {
4998         int i, j, k, miniim, maxiim, minijm, maxijm;
4999         int *intpt, *intpt2;
5000         double tmpdouble;
5001         double eff1, eff0;
5002         double *tmptmplen = NULL; //static?
5003         int *hist = NULL; //static?
5004         Bchain *ac = NULL; //static?
5005         int im = -1, jm = -1;
5006         Bchain *acjmnext, *acjmprev;
5007         int prevnode;
5008         Bchain *acpti;
5009         int *pt1, *pt2, *pt11, *pt22;
5010         int *nmemar; //static?
5011         int nmemim, nmemjm;
5012         double minscore;
5013         int *nearest = NULL; // by D.Mathog, a guess
5014         double *mindisfrom = NULL; // by D.Mathog, a guess
5015         char **tree; //static?
5016         char *treetmp; //static?
5017         char *nametmp, *nameptr, *tmpptr; //static?
5018         FILE *fp;
5019         double (*clusterfuncpt[1])(double,double);
5020         char namec;
5021
5022
5023         sueff1 = 1 - (double)sueff_global;
5024         sueff05 = (double)sueff_global * 0.5;
5025         if ( treemethod == 'X' )
5026                 clusterfuncpt[0] = cluster_mix_double;
5027         else if ( treemethod == 'E' )
5028                 clusterfuncpt[0] = cluster_average_double;
5029         else if ( treemethod == 'q' )
5030                 clusterfuncpt[0] = cluster_minimum_double;
5031         else
5032         {
5033                 reporterr(       "Unknown treemethod, %c\n", treemethod );
5034                 exit( 1 );
5035         }
5036
5037         if( !hist )
5038         {
5039                 hist = AllocateIntVec( njob );
5040                 tmptmplen = AllocateFloatVec( njob );
5041                 ac = (Bchain *)malloc( njob * sizeof( Bchain ) );
5042                 nmemar = AllocateIntVec( njob );
5043                 mindisfrom = AllocateFloatVec( njob );
5044                 nearest = AllocateIntVec( njob );
5045 //              treetmp = AllocateCharVec( njob * ( B + 100 ) ); // nagasugi?
5046                 treetmp = NULL; // kentou 2013/06/12
5047                 nametmp = AllocateCharVec( 1000 ); // nagasugi
5048 //              tree = AllocateCharMtx( njob, njob*600 );
5049                 tree = AllocateCharMtx( njob, 0 );
5050         }
5051
5052         
5053     for( i=0; i<nseq; i++ )
5054         {
5055                 for( j=0; j<999; j++ ) nametmp[j] = 0;
5056                 for( j=0; j<999; j++ ) 
5057                 {
5058                         namec = name[i][j];
5059                         if( namec == 0 )
5060                                 break;
5061                         else if( isalnum( namec ) || namec == '/' || namec == '=' || namec == '-' || namec == '{' || namec == '}' )
5062                                 nametmp[j] = namec;
5063                         else
5064                                 nametmp[j] = '_';
5065                 }
5066                 nametmp[j] = 0;
5067 //              sprintf( tree[i], "%d_l=%d_%.20s", i+1, nlen[i], nametmp+1 );
5068                 if( outnumber )
5069                         nameptr = strstr( nametmp, "_numo_e" ) + 8;
5070                 else
5071                         nameptr = nametmp + 1;
5072
5073                 if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
5074
5075                 tree[i] = calloc( strlen( nametmp )+100, sizeof( char ) ); // suuji no bun de +100
5076                 if( tree[i] == NULL )
5077                 {
5078                         reporterr(       "Cannot allocate tree!\n" );
5079                         exit( 1 );
5080                 }
5081                 sprintf( tree[i], "\n%d_%.900s\n", i+1, nameptr );
5082         }
5083         for( i=0; i<nseq; i++ )
5084         {
5085                 ac[i].next = ac+i+1;
5086                 ac[i].prev = ac+i-1;
5087                 ac[i].pos = i;
5088         }
5089         ac[nseq-1].next = NULL;
5090
5091         for( i=0; i<nseq; i++ ) setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i ); // muscle
5092
5093         for( i=0; i<nseq; i++ ) tmptmplen[i] = 0.0;
5094         for( i=0; i<nseq; i++ ) 
5095         {
5096                 hist[i] = -1;
5097                 nmemar[i] = 1;
5098         }
5099
5100         reporterr(       "\n" );
5101         for( k=0; k<nseq-1; k++ )
5102         {
5103                 if( k % 10 == 0 ) reporterr(       "\r% 5d / %d", k, nseq );
5104
5105                 minscore = 999.9;
5106                 for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
5107                 {
5108                         i = acpti->pos;
5109 //                      reporterr(       "k=%d i=%d\n", k, i );
5110                         if( mindisfrom[i] < minscore ) // muscle
5111                         {
5112                                 im = i;
5113                                 minscore = mindisfrom[i];
5114                         }
5115                 }
5116                 jm = nearest[im];
5117                 if( jm < im ) 
5118                 {
5119                         j=jm; jm=im; im=j;
5120                 }
5121
5122
5123                 prevnode = hist[im];
5124                 if( dep ) dep[k].child0 = prevnode;
5125                 nmemim = nmemar[im];
5126                 intpt = topol[k][0] = (int *)realloc( topol[k][0], ( nmemim + 1 ) * sizeof( int ) );
5127                 if( prevnode == -1 )
5128                 {
5129                         *intpt++ = im;
5130                         *intpt = -1;
5131                 }
5132                 else
5133                 {
5134                         pt1 = topol[prevnode][0];
5135                         pt2 = topol[prevnode][1];
5136                         if( *pt1 > *pt2 )
5137                         {
5138                                 pt11 = pt2;
5139                                 pt22 = pt1;
5140                         }
5141                         else
5142                         {
5143                                 pt11 = pt1;
5144                                 pt22 = pt2;
5145                         }
5146                         for( intpt2=pt11; *intpt2!=-1; )
5147                                 *intpt++ = *intpt2++;
5148                         for( intpt2=pt22; *intpt2!=-1; )
5149                                 *intpt++ = *intpt2++;
5150                         *intpt = -1;
5151                 }
5152
5153                 prevnode = hist[jm];
5154                 if( dep ) dep[k].child1 = prevnode;
5155                 nmemjm = nmemar[jm];
5156                 intpt = topol[k][1] = (int *)realloc( topol[k][1], ( nmemjm + 1 ) * sizeof( int ) );
5157                 if( !intpt )
5158                 {
5159                         reporterr(       "Cannot reallocate topol\n" );
5160                         exit( 1 );
5161                 }
5162                 if( prevnode == -1 )
5163                 {
5164                         *intpt++ = jm;
5165                         *intpt = -1;
5166                 }
5167                 else
5168                 {
5169                         pt1 = topol[prevnode][0];
5170                         pt2 = topol[prevnode][1];
5171                         if( *pt1 > *pt2 )
5172                         {
5173                                 pt11 = pt2;
5174                                 pt22 = pt1;
5175                         }
5176                         else
5177                         {
5178                                 pt11 = pt1;
5179                                 pt22 = pt2;
5180                         }
5181                         for( intpt2=pt11; *intpt2!=-1; )
5182                                 *intpt++ = *intpt2++;
5183                         for( intpt2=pt22; *intpt2!=-1; )
5184                                 *intpt++ = *intpt2++;
5185                         *intpt = -1;
5186                 }
5187
5188                 minscore *= 0.5;
5189
5190                 len[k][0] = ( minscore - tmptmplen[im] );
5191                 len[k][1] = ( minscore - tmptmplen[jm] );
5192
5193                 if( dep ) dep[k].distfromtip = minscore;
5194 //              reporterr(       "\n##### dep[%d].distfromtip = %f\n", k, minscore );
5195
5196                 tmptmplen[im] = minscore;
5197
5198                 hist[im] = k;
5199                 nmemar[im] = nmemim + nmemjm;
5200
5201                 mindisfrom[im] = 999.9;
5202                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
5203         {
5204                         i = acpti->pos;
5205             if( i != im && i != jm )
5206             {
5207                                 if( i < im )
5208                                 {
5209                                          miniim = i;
5210                                          maxiim = im;
5211                                          minijm = i;
5212                                          maxijm = jm;
5213                                 }
5214                                 else if( i < jm )
5215                                 {
5216                                          miniim = im;
5217                                          maxiim = i;
5218                                          minijm = i;
5219                                          maxijm = jm;
5220                                 }
5221                                 else
5222                                 {
5223                                          miniim = im;
5224                                          maxiim = i;
5225                                          minijm = jm;
5226                                          maxijm = i;
5227                                 }
5228                                 eff0 = eff[miniim][maxiim-miniim];
5229                                 eff1 = eff[minijm][maxijm-minijm];
5230 #if 0
5231                                 tmpdouble = eff[miniim][maxiim-miniim] =
5232                                 MIN( eff0, eff1 ) * sueff1 + ( eff0 + eff1 ) * sueff05; 
5233 #else
5234                 tmpdouble = eff[miniim][maxiim-miniim] =
5235                                 (clusterfuncpt[0])( eff0, eff1 );
5236
5237
5238 #endif
5239                                 if( tmpdouble < mindisfrom[i]  )
5240                                 {
5241                                         mindisfrom[i] = tmpdouble;
5242                                         nearest[i] = im;
5243                                 }
5244                                 if( tmpdouble < mindisfrom[im]  )
5245                                 {
5246                                         mindisfrom[im] = tmpdouble;
5247                                         nearest[im] = i;
5248                                 }
5249                                 if( nearest[i] == jm )
5250                                 {
5251                                         nearest[i] = im;
5252                                 }
5253             }
5254         }
5255
5256                 treetmp = realloc( treetmp, strlen( tree[im] ) + strlen( tree[jm] ) + 100 ); // 22 de juubunn (:%7,:%7) %7 ha minus kamo
5257                 if( !treetmp )
5258                 {
5259                         reporterr(       "Cannot allocate treetmp\n" );
5260                         exit( 1 );
5261                 }
5262                 sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
5263                 free( tree[im] );
5264                 free( tree[jm] );
5265                 tree[im] = calloc( strlen( treetmp )+1, sizeof( char ) );
5266                 tree[jm] = NULL;
5267                 if( tree[im] == NULL )
5268                 {
5269                         reporterr(       "Cannot reallocate tree!\n" );
5270                         exit( 1 );
5271                 }
5272                 strcpy( tree[im], treetmp );
5273
5274                 acjmprev = ac[jm].prev; 
5275                 acjmnext = ac[jm].next; 
5276                 acjmprev->next = acjmnext;
5277                 if( acjmnext != NULL )
5278                         acjmnext->prev = acjmprev;
5279                 if( efffree )
5280                 {
5281                         free( (void *)eff[jm] ); eff[jm] = NULL;
5282                 }
5283
5284 #if 1 // muscle seems to miss this.
5285                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
5286                 {
5287                         i = acpti->pos;
5288                         if( nearest[i] == im ) 
5289                         {
5290                                 if( i < im )
5291                                 {
5292                                          miniim = i;
5293                                          maxiim = im;
5294                                 }
5295                                 else
5296                                 {
5297                                          miniim = im;
5298                                          maxiim = i;
5299                                 }
5300                                 if( eff[miniim][maxiim-miniim] > mindisfrom[i] )
5301                                         setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i );
5302                         }
5303                 }
5304 #else
5305                 reporterr( "chuui!\n" );
5306 #endif
5307
5308
5309 #if 0
5310         printf(       "\nooSTEP-%03d:\n", k+1 );
5311                 printf(       "len0 = %f\n", len[k][0] );
5312         for( i=0; topol[k][0][i]>-1; i++ ) printf(       " %03d", topol[k][0][i]+1 );
5313         printf(       "\n" );
5314                 printf(       "len1 = %f\n", len[k][1] );
5315         for( i=0; topol[k][1][i]>-1; i++ ) printf(       " %03d", topol[k][1][i]+1 );
5316         printf(       "\n" );
5317 #endif
5318     }
5319         fp = fopen( "infile.tree", "w" );
5320                 fprintf( fp, "%s\n", treetmp );
5321         fclose( fp );
5322
5323         free( tree[0] );
5324         free( tree );
5325         free( treetmp );
5326         free( nametmp );
5327         free( (void *)tmptmplen ); tmptmplen = NULL;
5328         free( hist ); hist = NULL;
5329         free( (char *)ac ); ac = NULL;
5330         free( (void *)nmemar ); nmemar = NULL;
5331         free( mindisfrom );
5332         free( nearest );
5333 }
5334
5335 void fixed_musclesupg_double_treeout( int nseq, double **eff, int ***topol, double **len, char **name )
5336 {
5337         int i, j, k, miniim, maxiim, minijm, maxijm;
5338         int *intpt, *intpt2;
5339         double tmpdouble;
5340         double eff1, eff0;
5341         static double *tmptmplen = NULL;
5342         static int *hist = NULL;
5343         static Bchain *ac = NULL;
5344         int im = -1, jm = -1;
5345         Bchain *acjmnext, *acjmprev;
5346         int prevnode;
5347         Bchain *acpti;
5348         int *pt1, *pt2, *pt11, *pt22;
5349         static int *nmemar;
5350         int nmemim, nmemjm;
5351         double minscore;
5352         int *nearest = NULL; // by D.Mathog, a guess
5353         double *mindisfrom = NULL; // by D.Mathog, a guess
5354         static char **tree;
5355         static char *treetmp;
5356         static char *nametmp, *nameptr, *tmpptr;
5357         FILE *fp;
5358         double (*clusterfuncpt[1])(double,double);
5359         char namec;
5360
5361
5362         sueff1 = 1.0 - sueff_global;
5363         sueff05 = sueff_global * 0.5;
5364         if ( treemethod == 'X' )
5365                 clusterfuncpt[0] = cluster_mix_double;
5366         else if ( treemethod == 'E' )
5367                 clusterfuncpt[0] = cluster_average_double;
5368         else if ( treemethod == 'q' )
5369                 clusterfuncpt[0] = cluster_minimum_double;
5370         else
5371         {
5372                 reporterr(       "Unknown treemethod, %c\n", treemethod );
5373                 exit( 1 );
5374         }
5375
5376
5377
5378
5379
5380 #if 0
5381         if( !hist )
5382         {
5383                 hist = AllocateIntVec( njob );
5384                 tmptmplen = AllocateDoubleVec( njob );
5385                 ac = (Bchain *)malloc( njob * sizeof( Bchain ) );
5386                 nmemar = AllocateIntVec( njob );
5387                 mindisfrom = AllocateDoubleVec( njob );
5388                 nearest = AllocateIntVec( njob );
5389                 treetmp = AllocateCharVec( njob*150 );
5390                 nametmp = AllocateCharVec( 91 );
5391                 tree = AllocateCharMtx( njob, njob*150 );
5392         }
5393     for( i=0; i<nseq; i++ )
5394         {
5395                 for( j=0; j<90; j++ ) nametmp[j] = 0;
5396                 for( j=0; j<90; j++ ) 
5397                 {
5398                         if( name[i][j] == 0 )
5399                                 break;
5400                         else if( isalnum( name[i][j] ) )
5401                                 nametmp[j] = name[i][j];
5402                         else
5403                                 nametmp[j] = '_';
5404                 }
5405                 nametmp[90] = 0;
5406 //              sprintf( tree[i], "%d_%.60s", i+1, nametmp+1 );
5407                 if( outnumber )
5408                         nameptr = strstr( nametmp, "_numo_e" ) + 8;
5409                 else
5410                         nameptr = nametmp + 1;
5411
5412                 if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
5413
5414                 sprintf( tree[i], "\n%d_%.60s\n", i+1, nameptr );
5415         }
5416
5417 #else
5418
5419         if( !hist )
5420         {
5421                 hist = AllocateIntVec( njob );
5422                 tmptmplen = AllocateDoubleVec( njob );
5423                 ac = (Bchain *)malloc( njob * sizeof( Bchain ) );
5424                 nmemar = AllocateIntVec( njob );
5425                 mindisfrom = AllocateDoubleVec( njob );
5426                 nearest = AllocateIntVec( njob );
5427 //              treetmp = AllocateCharVec( njob * ( B + 100 ) ); // nagasugi?
5428                 treetmp = NULL; // kentou 2013/06/12
5429                 nametmp = AllocateCharVec( 1000 ); // nagasugi
5430 //              tree = AllocateCharMtx( njob, njob*600 );
5431                 tree = AllocateCharMtx( njob, 0 );
5432         }
5433
5434         
5435     for( i=0; i<nseq; i++ )
5436         {
5437                 for( j=0; j<999; j++ ) nametmp[j] = 0;
5438                 for( j=0; j<999; j++ ) 
5439                 {
5440                         namec = name[i][j];
5441                         if( namec == 0 )
5442                                 break;
5443                         else if( isalnum( namec ) || namec == '/' || namec == '=' || namec == '-' || namec == '{' || namec == '}' )
5444                                 nametmp[j] = namec;
5445                         else
5446                                 nametmp[j] = '_';
5447                 }
5448                 nametmp[j] = 0;
5449 //              sprintf( tree[i], "%d_l=%d_%.20s", i+1, nlen[i], nametmp+1 );
5450                 if( outnumber )
5451                         nameptr = strstr( nametmp, "_numo_e" ) + 8;
5452                 else
5453                         nameptr = nametmp + 1;
5454
5455                 if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
5456
5457                 tree[i] = calloc( strlen( nametmp )+100, sizeof( char ) ); // suuji no bun de +100
5458                 if( tree[i] == NULL )
5459                 {
5460                         reporterr(       "Cannot allocate tree!\n" );
5461                         exit( 1 );
5462                 }
5463                 sprintf( tree[i], "\n%d_%.900s\n", i+1, nameptr );
5464         }
5465
5466 #endif
5467
5468
5469
5470
5471
5472
5473
5474
5475         for( i=0; i<nseq; i++ )
5476         {
5477                 ac[i].next = ac+i+1;
5478                 ac[i].prev = ac+i-1;
5479                 ac[i].pos = i;
5480         }
5481         ac[nseq-1].next = NULL;
5482
5483         for( i=0; i<nseq; i++ ) setnearest_double_fullmtx( nseq, ac, eff, mindisfrom+i, nearest+i, i ); // muscle
5484
5485         for( i=0; i<nseq; i++ ) tmptmplen[i] = 0.0;
5486         for( i=0; i<nseq; i++ ) 
5487         {
5488                 hist[i] = -1;
5489                 nmemar[i] = 1;
5490         }
5491
5492         reporterr(       "\n" );
5493         for( k=0; k<nseq-1; k++ )
5494         {
5495                 if( k % 10 == 0 ) reporterr(       "\r% 5d / %d", k, nseq );
5496
5497                 minscore = 999.9;
5498                 for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
5499                 {
5500                         i = acpti->pos;
5501 //                      reporterr(       "k=%d i=%d\n", k, i );
5502                         if( mindisfrom[i] < minscore ) // muscle
5503                         {
5504                                 im = i;
5505                                 minscore = mindisfrom[i];
5506                         }
5507                 }
5508                 jm = nearest[im];
5509                 if( jm < im ) 
5510                 {
5511                         j=jm; jm=im; im=j;
5512                 }
5513
5514
5515                 prevnode = hist[im];
5516                 nmemim = nmemar[im];
5517 //              intpt = topol[k][0] = (int *)realloc( topol[k][0], ( nmemim + 1 ) * sizeof( int ) );
5518                 intpt = topol[k][0];
5519                 if( prevnode == -1 )
5520                 {
5521                         *intpt++ = im;
5522                         *intpt = -1;
5523                 }
5524                 else
5525                 {
5526                         pt1 = topol[prevnode][0];
5527                         pt2 = topol[prevnode][1];
5528                         if( *pt1 > *pt2 )
5529                         {
5530                                 pt11 = pt2;
5531                                 pt22 = pt1;
5532                         }
5533                         else
5534                         {
5535                                 pt11 = pt1;
5536                                 pt22 = pt2;
5537                         }
5538                         for( intpt2=pt11; *intpt2!=-1; )
5539                                 *intpt++ = *intpt2++;
5540                         for( intpt2=pt22; *intpt2!=-1; )
5541                                 *intpt++ = *intpt2++;
5542                         *intpt = -1;
5543                 }
5544
5545                 prevnode = hist[jm];
5546                 nmemjm = nmemar[jm];
5547 //              intpt = topol[k][1] = (int *)realloc( topol[k][1], ( nmemjm + 1 ) * sizeof( int ) );
5548                 intpt = topol[k][1];
5549                 if( prevnode == -1 )
5550                 {
5551                         *intpt++ = jm;
5552                         *intpt = -1;
5553                 }
5554                 else
5555                 {
5556                         pt1 = topol[prevnode][0];
5557                         pt2 = topol[prevnode][1];
5558                         if( *pt1 > *pt2 )
5559                         {
5560                                 pt11 = pt2;
5561                                 pt22 = pt1;
5562                         }
5563                         else
5564                         {
5565                                 pt11 = pt1;
5566                                 pt22 = pt2;
5567                         }
5568                         for( intpt2=pt11; *intpt2!=-1; )
5569                                 *intpt++ = *intpt2++;
5570                         for( intpt2=pt22; *intpt2!=-1; )
5571                                 *intpt++ = *intpt2++;
5572                         *intpt = -1;
5573                 }
5574
5575                 minscore *= 0.5;
5576
5577                 len[k][0] = ( minscore - tmptmplen[im] );
5578                 len[k][1] = ( minscore - tmptmplen[jm] );
5579
5580
5581                 tmptmplen[im] = minscore;
5582
5583                 hist[im] = k;
5584                 nmemar[im] = nmemim + nmemjm;
5585
5586                 mindisfrom[im] = 999.9;
5587                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
5588         {
5589                         i = acpti->pos;
5590             if( i != im && i != jm )
5591             {
5592                                 if( i < im )
5593                                 {
5594                                          miniim = i;
5595                                          maxiim = im;
5596                                          minijm = i;
5597                                          maxijm = jm;
5598                                 }
5599                                 else if( i < jm )
5600                                 {
5601                                          miniim = im;
5602                                          maxiim = i;
5603                                          minijm = i;
5604                                          maxijm = jm;
5605                                 }
5606                                 else
5607                                 {
5608                                          miniim = im;
5609                                          maxiim = i;
5610                                          minijm = jm;
5611                                          maxijm = i;
5612                                 }
5613                                 eff0 = eff[miniim][maxiim];
5614                                 eff1 = eff[minijm][maxijm];
5615 #if 0
5616                                 tmpdouble = eff[miniim][maxiim] =
5617                                 MIN( eff0, eff1 ) * sueff1 + ( eff0 + eff1 ) * sueff05; 
5618 #else
5619                 tmpdouble = eff[miniim][maxiim] =
5620                                 (clusterfuncpt[0])( eff0, eff1 );
5621 #endif
5622                                 if( tmpdouble < mindisfrom[i]  )
5623                                 {
5624                                         mindisfrom[i] = tmpdouble;
5625                                         nearest[i] = im;
5626                                 }
5627                                 if( tmpdouble < mindisfrom[im]  )
5628                                 {
5629                                         mindisfrom[im] = tmpdouble;
5630                                         nearest[im] = i;
5631                                 }
5632                                 if( nearest[i] == jm )
5633                                 {
5634                                         nearest[i] = im;
5635                                 }
5636             }
5637         }
5638 #if 0
5639                 sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
5640                 strcpy( tree[im], treetmp );
5641 #else
5642                 treetmp = realloc( treetmp, strlen( tree[im] ) + strlen( tree[jm] ) + 100 ); // 22 de juubunn (:%7,:%7) %7 ha minus kamo
5643                 if( !treetmp )
5644                 {
5645                         reporterr(       "Cannot allocate treetmp\n" );
5646                         exit( 1 );
5647                 }
5648                 sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
5649                 free( tree[im] );
5650                 free( tree[jm] );
5651                 tree[im] = calloc( strlen( treetmp )+1, sizeof( char ) );
5652                 tree[jm] = NULL;
5653                 if( tree[im] == NULL )
5654                 {
5655                         reporterr(       "Cannot reallocate tree!\n" );
5656                         exit( 1 );
5657                 }
5658                 strcpy( tree[im], treetmp );
5659 #endif
5660
5661                 acjmprev = ac[jm].prev; 
5662                 acjmnext = ac[jm].next; 
5663                 acjmprev->next = acjmnext;
5664                 if( acjmnext != NULL )
5665                         acjmnext->prev = acjmprev;
5666 //              free( (void *)eff[jm] ); eff[jm] = NULL;
5667
5668 #if 1 // muscle seems to miss this.
5669                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
5670                 {
5671                         i = acpti->pos;
5672                         if( nearest[i] == im ) 
5673                         {
5674                                 if( i < im )
5675                                 {
5676                                          miniim = i;
5677                                          maxiim = im;
5678                                 }
5679                                 else
5680                                 {
5681                                          miniim = im;
5682                                          maxiim = i;
5683                                 }
5684                                 if( eff[miniim][maxiim] > mindisfrom[i] )
5685                                         setnearest_double_fullmtx( nseq, ac, eff, mindisfrom+i, nearest+i, i );
5686                         }
5687                 }
5688 #endif
5689
5690
5691 #if 0
5692         fprintf( stdout, "\nvSTEP-%03d:\n", k+1 );
5693                 fprintf( stdout, "len0 = %f\n", len[k][0] );
5694         for( i=0; topol[k][0][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][0][i]+1 );
5695         fprintf( stdout, "\n" );
5696                 fprintf( stdout, "len1 = %f\n", len[k][1] );
5697         for( i=0; topol[k][1][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][1][i]+1 );
5698         fprintf( stdout, "\n" );
5699 #endif
5700     }
5701         fp = fopen( "infile.tree", "w" );
5702                 fprintf( fp, "%s\n", treetmp );
5703         fclose( fp );
5704 #if 0
5705         FreeCharMtx( tree );
5706 #else
5707         free( tree[0] );
5708         free( tree );
5709 #endif
5710         free( treetmp );
5711         free( nametmp );
5712         free( (void *)tmptmplen ); tmptmplen = NULL;
5713         free( hist ); hist = NULL;
5714         free( (char *)ac ); ac = NULL;
5715         free( (void *)nmemar ); nmemar = NULL;
5716         free( mindisfrom );
5717         free( nearest );
5718 }
5719
5720 void fixed_supg_double_treeout_constrained( int nseq, double **eff, int ***topol, double **len, char **name, int ngroup, int **groups )
5721 {
5722         int i, j, k, miniim, maxiim, minijm, maxijm;
5723         int *intpt, *intpt2;
5724         double tmpdouble;
5725         double eff1, eff0;
5726         static double *tmptmplen = NULL;
5727         static int *hist = NULL;
5728         static Bchain *ac = NULL;
5729         int im = -1, jm = -1;
5730         Bchain *acjmnext, *acjmprev;
5731         int prevnode;
5732         Bchain *acpti, *acptj;
5733         int *pt1, *pt2, *pt11, *pt22;
5734         static int *nmemar;
5735         int nmemim, nmemjm;
5736         double minscore;
5737         int *nearest = NULL; // by D.Mathog, a guess
5738         double *mindisfrom = NULL; // by D.Mathog, a guess
5739         static char **tree;
5740         static char *treetmp;
5741         static char *nametmp, *nameptr, *tmpptr;
5742         FILE *fp;
5743         double (*clusterfuncpt[1])(double,double);
5744         char namec;
5745         int *testtopol, **inconsistent;
5746         int **inconsistentpairlist;
5747         int ninconsistentpairs;
5748         int *warned;
5749         int allinconsistent;
5750         int firsttime;
5751
5752         increaseintergroupdistancesfullmtx( eff, ngroup, groups, nseq );
5753
5754         sueff1 = 1 - sueff_global;
5755         sueff05 = sueff_global * 0.5;
5756         if ( treemethod == 'X' )
5757                 clusterfuncpt[0] = cluster_mix_double;
5758         else if ( treemethod == 'E' )
5759                 clusterfuncpt[0] = cluster_average_double;
5760         else if ( treemethod == 'q' )
5761                 clusterfuncpt[0] = cluster_minimum_double;
5762         else
5763         {
5764                 reporterr(       "Unknown treemethod, %c\n", treemethod );
5765                 exit( 1 );
5766         }
5767
5768
5769
5770
5771
5772 #if 0
5773         if( !hist )
5774         {
5775                 hist = AllocateIntVec( njob );
5776                 tmptmplen = AllocateDoubleVec( njob );
5777                 ac = (Bchain *)malloc( njob * sizeof( Bchain ) );
5778                 nmemar = AllocateIntVec( njob );
5779                 mindisfrom = AllocateDoubleVec( njob );
5780                 nearest = AllocateIntVec( njob );
5781                 treetmp = AllocateCharVec( njob*150 );
5782                 nametmp = AllocateCharVec( 91 );
5783                 tree = AllocateCharMtx( njob, njob*150 );
5784         }
5785         for( i=0; i<nseq; i++ )
5786         {
5787                 for( j=0; j<90; j++ ) nametmp[j] = 0;
5788                 for( j=0; j<90; j++ ) 
5789                 {
5790                         if( name[i][j] == 0 )
5791                                 break;
5792                         else if( isalnum( name[i][j] ) )
5793                                 nametmp[j] = name[i][j];
5794                         else
5795                                 nametmp[j] = '_';
5796                 }
5797                 nametmp[90] = 0;
5798 //              sprintf( tree[i], "%d_%.60s", i+1, nametmp+1 );
5799                 if( outnumber )
5800                         nameptr = strstr( nametmp, "_numo_e" ) + 8;
5801                 else
5802                         nameptr = nametmp + 1;
5803
5804                 if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
5805
5806                 sprintf( tree[i], "\n%d_%.60s\n", i+1, nameptr );
5807         }
5808
5809 #else
5810
5811         if( !hist )
5812         {
5813                 hist = AllocateIntVec( njob );
5814                 tmptmplen = AllocateDoubleVec( njob );
5815                 ac = (Bchain *)malloc( njob * sizeof( Bchain ) );
5816                 nmemar = AllocateIntVec( njob );
5817                 mindisfrom = AllocateDoubleVec( njob );
5818                 nearest = AllocateIntVec( njob );
5819 //              treetmp = AllocateCharVec( njob * ( B + 100 ) ); // nagasugi?
5820                 treetmp = NULL; // kentou 2013/06/12
5821                 nametmp = AllocateCharVec( 1000 ); // nagasugi
5822 //              tree = AllocateCharMtx( njob, njob*600 );
5823                 tree = AllocateCharMtx( njob, 0 );
5824                 testtopol = AllocateIntVec( njob + 1 );
5825                 inconsistent = AllocateIntMtx( njob, njob ); // muda
5826 //              inconsistentpairlist = AllocateIntMtx( njob*(njob-1)/2+1, 2 ); // muda
5827                 inconsistentpairlist = AllocateIntMtx( 1, 2 );
5828                 warned = AllocateIntVec( ngroup );
5829         }
5830
5831         
5832         for( i=0; i<nseq; i++ )
5833         {
5834                 for( j=0; j<999; j++ ) nametmp[j] = 0;
5835                 for( j=0; j<999; j++ ) 
5836                 {
5837                         namec = name[i][j];
5838                         if( namec == 0 )
5839                                 break;
5840                         else if( isalnum( namec ) || namec == '/' || namec == '=' || namec == '-' || namec == '{' || namec == '}' )
5841                                 nametmp[j] = namec;
5842                         else
5843                                 nametmp[j] = '_';
5844                 }
5845                 nametmp[j] = 0;
5846 //              sprintf( tree[i], "%d_l=%d_%.20s", i+1, nlen[i], nametmp+1 );
5847                 if( outnumber )
5848                         nameptr = strstr( nametmp, "_numo_e" ) + 8;
5849                 else
5850                         nameptr = nametmp + 1;
5851
5852                 if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
5853
5854                 tree[i] = calloc( strlen( nametmp )+100, sizeof( char ) ); // suuji no bun de +100
5855                 if( tree[i] == NULL )
5856                 {
5857                         reporterr(       "Cannot allocate tree!\n" );
5858                         exit( 1 );
5859                 }
5860                 sprintf( tree[i], "\n%d_%.900s\n", i+1, nameptr );
5861         }
5862
5863 #endif
5864
5865
5866
5867
5868
5869
5870
5871
5872         for( i=0; i<nseq; i++ )
5873         {
5874                 ac[i].next = ac+i+1;
5875                 ac[i].prev = ac+i-1;
5876                 ac[i].pos = i;
5877         }
5878         ac[nseq-1].next = NULL;
5879
5880         for( i=0; i<nseq; i++ ) setnearest_double_fullmtx( nseq, ac, eff, mindisfrom+i, nearest+i, i ); // muscle
5881
5882         for( i=0; i<nseq; i++ ) tmptmplen[i] = 0.0;
5883         for( i=0; i<nseq; i++ ) 
5884         {
5885                 hist[i] = -1;
5886                 nmemar[i] = 1;
5887         }
5888
5889         reporterr(       "\n" );
5890         ninconsistentpairs = 0;
5891         for( k=0; k<nseq-1; k++ )
5892         {
5893                 if( k % 10 == 0 ) reporterr(       "\r% 5d / %d", k, nseq );
5894
5895
5896
5897 //              for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) for( acptj=acpti->next; acptj!=NULL; acptj=acptj->next ) inconsistent[acpti->pos][acptj->pos] = 0;
5898                 for( i=0; i<ninconsistentpairs; i++ ) inconsistent[inconsistentpairlist[i][0]][inconsistentpairlist[i][1]] = 0;
5899                 ninconsistentpairs = 0;
5900                 firsttime = 1;
5901                 while( 1 )
5902                 {
5903                         if( firsttime )
5904                         {
5905                                 firsttime = 0;
5906                                 minscore = 999.9;
5907                                 for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
5908                                 {
5909                                         i = acpti->pos;
5910 //                                      reporterr(       "k=%d i=%d\n", k, i );
5911                                         if( mindisfrom[i] < minscore ) // muscle
5912                                         {
5913                                                 im = i;
5914                                                 minscore = mindisfrom[i];
5915                                         }
5916                                 }
5917                                 jm = nearest[im];
5918                                 if( jm < im ) 
5919                                 {
5920                                         j=jm; jm=im; im=j;
5921                                 }
5922                         }
5923                         else
5924                         {
5925                                 minscore = 999.9;
5926                                 for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
5927                                 {
5928                                         i = acpti->pos;
5929 //                                      reporterr(       "k=%d i=%d\n", k, i );
5930                                         for( acptj=acpti->next; acptj!=NULL; acptj=acptj->next )
5931                                         {
5932                                                 j = acptj->pos;
5933                                                 if( !inconsistent[i][j] && (tmpdouble=eff[i][j]) < minscore )
5934                                                 {
5935                                                         minscore = tmpdouble;
5936                                                         im = i; jm = j;
5937                                                 }
5938                                         }
5939                                         for( acptj=ac; (acptj&&acptj->pos!=i); acptj=acptj->next )
5940                                         {
5941                                                 j = acptj->pos;
5942                                                 if( !inconsistent[j][i] && (tmpdouble=eff[j][i]) < minscore )
5943                                                 {
5944                                                         minscore = tmpdouble;
5945                                                         im = j; jm = i;
5946                                                 }
5947                                         }
5948                                 }
5949                         }
5950
5951                         allinconsistent = 1;
5952                         for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
5953                         {
5954                                 for( acptj=acpti->next; acptj!=NULL; acptj=acptj->next )
5955                                 {
5956                                         if( inconsistent[acpti->pos][acptj->pos] == 0 )
5957                                         {
5958                                                 allinconsistent = 0;
5959                                                 goto exitloop_d;
5960                                         }
5961                                 }
5962                         }
5963                         exitloop_d:
5964
5965                         if( allinconsistent )
5966                         {
5967                                 reporterr(       "\n\n\nPlease check whether the grouping is possible.\n\n\n" );
5968                                 exit( 1 );
5969                         }
5970 #if 1
5971                         intpt = testtopol;
5972                         prevnode = hist[im];
5973                         if( prevnode == -1 )
5974                         {
5975                                 *intpt++ = im;
5976                         }
5977                         else
5978                         {
5979                                 for( intpt2=topol[prevnode][0]; *intpt2!=-1; )
5980                                         *intpt++ = *intpt2++;
5981                                 for( intpt2=topol[prevnode][1]; *intpt2!=-1; )
5982                                         *intpt++ = *intpt2++;
5983                         }
5984                         
5985                         prevnode = hist[jm];
5986                         if( prevnode == -1 )
5987                         {
5988                                 *intpt++ = jm;
5989                         }
5990                         else
5991                         {
5992                                 for( intpt2=topol[prevnode][0]; *intpt2!=-1; )
5993                                         *intpt++ = *intpt2++;
5994                                 for( intpt2=topol[prevnode][1]; *intpt2!=-1; )
5995                                         *intpt++ = *intpt2++;
5996                         }
5997                         *intpt = -1;
5998 //                      reporterr(       "testtopol = \n" );
5999 //              for( i=0; testtopol[i]>-1; i++ ) reporterr(       " %03d", testtopol[i]+1 );
6000 //                      reporterr(       "\n" );
6001 #endif
6002                         for( i=0; i<ngroup; i++ )
6003                         {
6004 //                              reporterr(       "groups[%d] = \n", i );
6005 //                      for( j=0; groups[i][j]>-1; j++ ) reporterr(       " %03d", groups[i][j]+1 );
6006 //                              reporterr(       "\n" );
6007                                 if( overlapmember( testtopol, groups[i] ) )
6008                                 {
6009                                         if( !includemember( testtopol, groups[i] ) && !includemember( groups[i], testtopol ) )
6010                                         {
6011                                                 if( !warned[i] )
6012                                                 {
6013                                                         warned[i] = 1;
6014                                                         reporterr(       "\n###################################################################\n" );
6015                                                         reporterr(       "# WARNING: Group %d is forced to be a monophyletic cluster.\n", i+1 );
6016                                                         reporterr(       "###################################################################\n" );
6017                                                 }
6018                                                 inconsistent[im][jm] = 1;
6019
6020                                                 inconsistentpairlist = realloc( inconsistentpairlist, (ninconsistentpairs+1)*sizeof( int * ) );
6021                                                 inconsistentpairlist[ninconsistentpairs] = malloc( sizeof( int ) * 2 );
6022                                                 inconsistentpairlist[ninconsistentpairs][0] = im;
6023                                                 inconsistentpairlist[ninconsistentpairs][1] = jm;
6024                                                 ninconsistentpairs++;
6025                                                 break;
6026                                         }
6027                                 }
6028                         }
6029                         if( i == ngroup )
6030                         {
6031 //                              reporterr(       "OK\n" );
6032                                 break;
6033                         }
6034                 }
6035
6036
6037
6038
6039
6040
6041                 prevnode = hist[im];
6042                 nmemim = nmemar[im];
6043 //              intpt = topol[k][0] = (int *)realloc( topol[k][0], ( nmemim + 1 ) * sizeof( int ) );
6044                 intpt = topol[k][0];
6045                 if( prevnode == -1 )
6046                 {
6047                         *intpt++ = im;
6048                         *intpt = -1;
6049                 }
6050                 else
6051                 {
6052                         pt1 = topol[prevnode][0];
6053                         pt2 = topol[prevnode][1];
6054                         if( *pt1 > *pt2 )
6055                         {
6056                                 pt11 = pt2;
6057                                 pt22 = pt1;
6058                         }
6059                         else
6060                         {
6061                                 pt11 = pt1;
6062                                 pt22 = pt2;
6063                         }
6064                         for( intpt2=pt11; *intpt2!=-1; )
6065                                 *intpt++ = *intpt2++;
6066                         for( intpt2=pt22; *intpt2!=-1; )
6067                                 *intpt++ = *intpt2++;
6068                         *intpt = -1;
6069                 }
6070
6071                 prevnode = hist[jm];
6072                 nmemjm = nmemar[jm];
6073 //              intpt = topol[k][1] = (int *)realloc( topol[k][1], ( nmemjm + 1 ) * sizeof( int ) );
6074                 intpt = topol[k][1];
6075                 if( prevnode == -1 )
6076                 {
6077                         *intpt++ = jm;
6078                         *intpt = -1;
6079                 }
6080                 else
6081                 {
6082                         pt1 = topol[prevnode][0];
6083                         pt2 = topol[prevnode][1];
6084                         if( *pt1 > *pt2 )
6085                         {
6086                                 pt11 = pt2;
6087                                 pt22 = pt1;
6088                         }
6089                         else
6090                         {
6091                                 pt11 = pt1;
6092                                 pt22 = pt2;
6093                         }
6094                         for( intpt2=pt11; *intpt2!=-1; )
6095                                 *intpt++ = *intpt2++;
6096                         for( intpt2=pt22; *intpt2!=-1; )
6097                                 *intpt++ = *intpt2++;
6098                         *intpt = -1;
6099                 }
6100
6101                 minscore *= 0.5;
6102
6103                 len[k][0] = ( minscore - tmptmplen[im] );
6104                 len[k][1] = ( minscore - tmptmplen[jm] );
6105                 if( len[k][0] < 0.0 ) len[k][0] = 0.0;
6106                 if( len[k][1] < 0.0 ) len[k][1] = 0.0;
6107
6108
6109                 tmptmplen[im] = minscore;
6110
6111                 hist[im] = k;
6112                 nmemar[im] = nmemim + nmemjm;
6113
6114                 mindisfrom[im] = 999.9;
6115                 eff[im][jm] = 999.9;
6116 //              eff[im][jm-im] = 999.9; // bug??
6117
6118                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
6119         {
6120                         i = acpti->pos;
6121             if( i != im && i != jm )
6122             {
6123                                 if( i < im )
6124                                 {
6125                                          miniim = i;
6126                                          maxiim = im;
6127                                          minijm = i;
6128                                          maxijm = jm;
6129                                 }
6130                                 else if( i < jm )
6131                                 {
6132                                          miniim = im;
6133                                          maxiim = i;
6134                                          minijm = i;
6135                                          maxijm = jm;
6136                                 }
6137                                 else
6138                                 {
6139                                          miniim = im;
6140                                          maxiim = i;
6141                                          minijm = jm;
6142                                          maxijm = i;
6143                                 }
6144                                 eff0 = eff[miniim][maxiim];
6145                                 eff1 = eff[minijm][maxijm];
6146 #if 0
6147                                 tmpdouble = eff[miniim][maxiim] =
6148                                 MIN( eff0, eff1 ) * sueff1 + ( eff0 + eff1 ) * sueff05; 
6149 #else
6150                 tmpdouble = eff[miniim][maxiim] =
6151                                 (clusterfuncpt[0])( eff0, eff1 );
6152 #endif
6153
6154 #if 1
6155                                 if( tmpdouble < mindisfrom[i]  )
6156                                 {
6157                                         mindisfrom[i] = tmpdouble;
6158                                         nearest[i] = im;
6159                                 }
6160                                 if( tmpdouble < mindisfrom[im]  )
6161                                 {
6162                                         mindisfrom[im] = tmpdouble;
6163                                         nearest[im] = i;
6164                                 }
6165                                 if( nearest[i] == jm )
6166                                 {
6167                                         nearest[i] = im;
6168                                 }
6169 #endif
6170             }
6171         }
6172 #if 0
6173                 sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
6174                 strcpy( tree[im], treetmp );
6175 #else
6176                 treetmp = realloc( treetmp, strlen( tree[im] ) + strlen( tree[jm] ) + 100 ); // 22 de juubunn (:%7,:%7) %7 ha minus kamo
6177                 if( !treetmp )
6178                 {
6179                         reporterr(       "Cannot allocate treetmp\n" );
6180                         exit( 1 );
6181                 }
6182                 sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
6183                 free( tree[im] );
6184                 free( tree[jm] );
6185                 tree[im] = calloc( strlen( treetmp )+1, sizeof( char ) );
6186                 tree[jm] = NULL;
6187                 if( tree[im] == NULL )
6188                 {
6189                         reporterr(       "Cannot reallocate tree!\n" );
6190                         exit( 1 );
6191                 }
6192                 strcpy( tree[im], treetmp );
6193 #endif
6194
6195                 acjmprev = ac[jm].prev; 
6196                 acjmnext = ac[jm].next; 
6197                 acjmprev->next = acjmnext;
6198                 if( acjmnext != NULL )
6199                         acjmnext->prev = acjmprev;
6200 //              free( (void *)eff[jm] ); eff[jm] = NULL;
6201
6202 #if 1 // muscle seems to miss this.
6203                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
6204                 {
6205                         i = acpti->pos;
6206                         if( nearest[i] == im ) 
6207                         {
6208                                 if( i < im )
6209                                 {
6210                                          miniim = i;
6211                                          maxiim = im;
6212                                 }
6213                                 else
6214                                 {
6215                                          miniim = im;
6216                                          maxiim = i;
6217                                 }
6218                                 if( eff[miniim][maxiim] > mindisfrom[i] )
6219                                         setnearest_double_fullmtx( nseq, ac, eff, mindisfrom+i, nearest+i, i );
6220                         }
6221                 }
6222 #endif
6223
6224
6225 #if 0
6226         fprintf( stdout, "\ncSTEP-%03d:\n", k+1 );
6227                 fprintf( stdout, "len0 = %f\n", len[k][0] );
6228         for( i=0; topol[k][0][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][0][i]+1 );
6229         fprintf( stdout, "\n" );
6230                 fprintf( stdout, "len1 = %f\n", len[k][1] );
6231         for( i=0; topol[k][1][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][1][i]+1 );
6232         fprintf( stdout, "\n" );
6233 #endif
6234     }
6235         fp = fopen( "infile.tree", "w" );
6236                 fprintf( fp, "%s\n", treetmp );
6237         fclose( fp );
6238 #if 0
6239         FreeCharMtx( tree );
6240 #else
6241         free( tree[0] );
6242         free( tree );
6243 #endif
6244         free( treetmp );
6245         free( nametmp );
6246         free( (void *)tmptmplen ); tmptmplen = NULL;
6247         free( hist ); hist = NULL;
6248         free( (char *)ac ); ac = NULL;
6249         free( (void *)nmemar ); nmemar = NULL;
6250         free( mindisfrom );
6251         free( nearest );
6252         free( testtopol );
6253         FreeIntMtx( inconsistent );
6254         FreeIntMtx( inconsistentpairlist );
6255         free( warned );
6256 }
6257
6258 void fixed_musclesupg_double_realloc_nobk_halfmtx_memsave( int nseq, double **eff, int ***topol, double **len, Treedep *dep, int progressout, int efffree )
6259 {
6260         int i, j, k, miniim, maxiim, minijm, maxijm;
6261         int *intpt;
6262         double tmpdouble;
6263         double eff1, eff0;
6264         double *tmptmplen = NULL; // static TLS -> local, 2012/02/25
6265         int *hist = NULL; // static TLS -> local, 2012/02/25 
6266         Bchain *ac = NULL; // static TLS -> local, 2012/02/25
6267         int im = -1, jm = -1;
6268         Bchain *acjmnext, *acjmprev;
6269         int prevnode;
6270         Bchain *acpti;
6271         int *pt1, *pt2, *pt11;
6272         int *nmemar; // static TLS -> local, 2012/02/25
6273         int nmemim, nmemjm;
6274         double minscore;
6275         int *nearest = NULL; // by Mathog, a guess
6276         double *mindisfrom = NULL; // by Mathog, a guess
6277         double (*clusterfuncpt[1])(double,double);
6278
6279
6280         sueff1 = 1 - (double)sueff_global;
6281         sueff05 = (double)sueff_global * 0.5;
6282         if ( treemethod == 'X' )
6283                 clusterfuncpt[0] = cluster_mix_double;
6284         else if ( treemethod == 'E' )
6285                 clusterfuncpt[0] = cluster_average_double;
6286         else if ( treemethod == 'q' )
6287                 clusterfuncpt[0] = cluster_minimum_double;
6288         else
6289         {
6290                 reporterr(       "Unknown treemethod, %c\n", treemethod );
6291                 exit( 1 );
6292         }
6293
6294         if( !hist )
6295         {
6296                 hist = AllocateIntVec( njob );
6297                 tmptmplen = AllocateFloatVec( njob );
6298                 ac = (Bchain *)malloc( njob * sizeof( Bchain ) );
6299                 nmemar = AllocateIntVec( njob );
6300                 mindisfrom = AllocateFloatVec( njob );
6301                 nearest = AllocateIntVec( njob );
6302         }
6303
6304         
6305         for( i=0; i<nseq; i++ )
6306         {
6307                 ac[i].next = ac+i+1;
6308                 ac[i].prev = ac+i-1;
6309                 ac[i].pos = i;
6310         }
6311         ac[nseq-1].next = NULL;
6312
6313         for( i=0; i<nseq; i++ ) setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i ); // muscle
6314
6315         for( i=0; i<nseq; i++ ) tmptmplen[i] = 0.0;
6316         for( i=0; i<nseq; i++ ) 
6317         {
6318                 hist[i] = -1;
6319                 nmemar[i] = 1;
6320         }
6321
6322         if( progressout ) reporterr(       "\n" );
6323         for( k=0; k<nseq-1; k++ )
6324         {
6325                 if( progressout && k % 10 == 0 ) reporterr(       "\r% 5d / %d", k, nseq );
6326
6327                 minscore = 999.9;
6328                 for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
6329                 {
6330                         i = acpti->pos;
6331 //                      reporterr(       "k=%d i=%d\n", k, i );
6332                         if( mindisfrom[i] < minscore ) // muscle
6333                         {
6334                                 im = i;
6335                                 minscore = mindisfrom[i];
6336                         }
6337                 }
6338                 jm = nearest[im];
6339                 if( jm < im ) 
6340                 {
6341                         j=jm; jm=im; im=j;
6342                 }
6343
6344
6345                 prevnode = hist[im];
6346                 if( dep ) dep[k].child0 = prevnode;
6347                 nmemim = nmemar[im];
6348                 intpt = topol[k][0] = (int *)realloc( topol[k][0], ( 2 ) * sizeof( int ) ); // memsave
6349 //              intpt = topol[k][0] = (int *)realloc( topol[k][0], ( nmemim + 1 ) * sizeof( int ) );
6350                 if( prevnode == -1 )
6351                 {
6352                         *intpt++ = im;
6353                         *intpt = -1;
6354                 }
6355                 else
6356                 {
6357                         pt1 = topol[prevnode][0];
6358                         pt2 = topol[prevnode][1];
6359                         if( *pt1 > *pt2 )
6360                         {
6361                                 pt11 = pt2;
6362 //                              pt22 = pt1;
6363                         }
6364                         else
6365                         {
6366                                 pt11 = pt1;
6367 //                              pt22 = pt2;
6368                         }
6369 #if 1
6370                         *intpt++ = *pt11;
6371                         *intpt = -1;
6372 #else
6373                         for( intpt2=pt11; *intpt2!=-1; )
6374                                 *intpt++ = *intpt2++;
6375                         for( intpt2=pt22; *intpt2!=-1; )
6376                                 *intpt++ = *intpt2++;
6377                         *intpt = -1;
6378 #endif
6379                 }
6380
6381                 prevnode = hist[jm];
6382                 if( dep ) dep[k].child1 = prevnode;
6383                 nmemjm = nmemar[jm];
6384                 intpt = topol[k][1] = (int *)realloc( topol[k][1], ( 2 ) * sizeof( int ) );
6385 //              intpt = topol[k][1] = (int *)realloc( topol[k][1], ( nmemjm + 1 ) * sizeof( int ) );
6386                 if( !intpt )
6387                 {
6388                         reporterr(       "Cannot reallocate topol\n" );
6389                         exit( 1 );
6390                 }
6391                 if( prevnode == -1 )
6392                 {
6393                         *intpt++ = jm;
6394                         *intpt = -1;
6395                 }
6396                 else
6397                 {
6398                         pt1 = topol[prevnode][0];
6399                         pt2 = topol[prevnode][1];
6400                         if( *pt1 > *pt2 )
6401                         {
6402                                 pt11 = pt2;
6403 //                              pt22 = pt1;
6404                         }
6405                         else
6406                         {
6407                                 pt11 = pt1;
6408 //                              pt22 = pt2;
6409                         }
6410 #if 1
6411                         *intpt++ = *pt11;
6412                         *intpt = -1;
6413 #else
6414                         for( intpt2=pt11; *intpt2!=-1; )
6415                                 *intpt++ = *intpt2++;
6416                         for( intpt2=pt22; *intpt2!=-1; )
6417                                 *intpt++ = *intpt2++;
6418                         *intpt = -1;
6419 #endif
6420                 }
6421
6422                 minscore *= 0.5;
6423
6424                 len[k][0] = ( minscore - tmptmplen[im] );
6425                 len[k][1] = ( minscore - tmptmplen[jm] );
6426
6427                 if( dep ) dep[k].distfromtip = minscore;
6428
6429                 tmptmplen[im] = minscore;
6430
6431                 hist[im] = k;
6432                 nmemar[im] = nmemim + nmemjm;
6433
6434                 mindisfrom[im] = 999.9;
6435                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
6436         {
6437                         i = acpti->pos;
6438             if( i != im && i != jm )
6439             {
6440                                 if( i < im )
6441                                 {
6442                                          miniim = i;
6443                                          maxiim = im;
6444                                          minijm = i;
6445                                          maxijm = jm;
6446                                 }
6447                                 else if( i < jm )
6448                                 {
6449                                          miniim = im;
6450                                          maxiim = i;
6451                                          minijm = i;
6452                                          maxijm = jm;
6453                                 }
6454                                 else
6455                                 {
6456                                          miniim = im;
6457                                          maxiim = i;
6458                                          minijm = jm;
6459                                          maxijm = i;
6460                                 }
6461                                 eff0 = eff[miniim][maxiim-miniim];
6462                                 eff1 = eff[minijm][maxijm-minijm];
6463                                 tmpdouble = eff[miniim][maxiim-miniim] =
6464 #if 0
6465                                 MIN( eff0, eff1 ) * sueff1 + ( eff0 + eff1 ) * sueff05; 
6466 #else
6467                                 (clusterfuncpt[0])( eff0, eff1 );
6468 #endif
6469                                 if( tmpdouble < mindisfrom[i]  )
6470                                 {
6471                                         mindisfrom[i] = tmpdouble;
6472                                         nearest[i] = im;
6473                                 }
6474                                 if( tmpdouble < mindisfrom[im]  )
6475                                 {
6476                                         mindisfrom[im] = tmpdouble;
6477                                         nearest[im] = i;
6478                                 }
6479                                 if( nearest[i] == jm )
6480                                 {
6481                                         nearest[i] = im;
6482                                 }
6483             }
6484         }
6485
6486 //              reporterr(       "im,jm=%d,%d\n", im, jm );
6487                 acjmprev = ac[jm].prev; 
6488                 acjmnext = ac[jm].next; 
6489                 acjmprev->next = acjmnext;
6490                 if( acjmnext != NULL )
6491                         acjmnext->prev = acjmprev;
6492                 if( efffree )
6493                 {
6494                         free( (void *)eff[jm] ); eff[jm] = NULL;
6495                 }
6496
6497 #if 1 // muscle seems to miss this.
6498                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
6499                 {
6500                         i = acpti->pos;
6501                         if( nearest[i] == im ) 
6502                         {
6503                                 if( i < im )
6504                                 {
6505                                          miniim = i;
6506                                          maxiim = im;
6507                                 }
6508                                 else
6509                                 {
6510                                          miniim = im;
6511                                          maxiim = i;
6512                                 }
6513                                 if( eff[miniim][maxiim-miniim] > mindisfrom[i] )
6514                                         setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i );
6515                         }
6516                 }
6517 #endif
6518
6519
6520 #if 0
6521         fprintf( stdout, "vSTEP-%03d:\n", k+1 );
6522                 fprintf( stdout, "len0 = %f\n", len[k][0] );
6523         for( i=0; topol[k][0][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][0][i]+1 );
6524         fprintf( stdout, "\n" );
6525                 fprintf( stdout, "len1 = %f\n", len[k][1] );
6526         for( i=0; topol[k][1][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][1][i]+1 );
6527         fprintf( stdout, "\n" );
6528 #endif
6529     }
6530         free( (void *)tmptmplen ); tmptmplen = NULL;
6531         free( hist ); hist = NULL;
6532         free( (char *)ac ); ac = NULL;
6533         free( (void *)nmemar ); nmemar = NULL;
6534         free( mindisfrom );
6535         free( nearest );
6536 }
6537 void fixed_musclesupg_double_realloc_nobk_halfmtx( int nseq, double **eff, int ***topol, double **len, Treedep *dep, int progressout, int efffree )
6538 {
6539         int i, j, k, miniim, maxiim, minijm, maxijm;
6540         int *intpt, *intpt2;
6541         double tmpdouble;
6542         double eff1, eff0;
6543         double *tmptmplen = NULL; // static TLS -> local, 2012/02/25
6544         int *hist = NULL; // static TLS -> local, 2012/02/25 
6545         Bchain *ac = NULL; // static TLS -> local, 2012/02/25
6546         int im = -1, jm = -1;
6547         Bchain *acjmnext, *acjmprev;
6548         int prevnode;
6549         Bchain *acpti;
6550         int *pt1, *pt2, *pt11, *pt22;
6551         int *nmemar; // static TLS -> local, 2012/02/25
6552         int nmemim, nmemjm;
6553         double minscore;
6554         int *nearest = NULL; // by Mathog, a guess
6555         double *mindisfrom = NULL; // by Mathog, a guess
6556         double (*clusterfuncpt[1])(double,double);
6557
6558
6559         sueff1 = 1 - (double)sueff_global;
6560         sueff05 = (double)sueff_global * 0.5;
6561         if ( treemethod == 'X' )
6562                 clusterfuncpt[0] = cluster_mix_double;
6563         else if ( treemethod == 'E' )
6564                 clusterfuncpt[0] = cluster_average_double;
6565         else if ( treemethod == 'q' )
6566                 clusterfuncpt[0] = cluster_minimum_double;
6567         else
6568         {
6569                 reporterr(       "Unknown treemethod, %c\n", treemethod );
6570                 exit( 1 );
6571         }
6572
6573         if( !hist )
6574         {
6575                 hist = AllocateIntVec( njob );
6576                 tmptmplen = AllocateFloatVec( njob );
6577                 ac = (Bchain *)malloc( njob * sizeof( Bchain ) );
6578                 nmemar = AllocateIntVec( njob );
6579                 mindisfrom = AllocateFloatVec( njob );
6580                 nearest = AllocateIntVec( njob );
6581         }
6582
6583         
6584         for( i=0; i<nseq; i++ )
6585         {
6586                 ac[i].next = ac+i+1;
6587                 ac[i].prev = ac+i-1;
6588                 ac[i].pos = i;
6589         }
6590         ac[nseq-1].next = NULL;
6591
6592         for( i=0; i<nseq; i++ ) setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i ); // muscle
6593
6594         for( i=0; i<nseq; i++ ) tmptmplen[i] = 0.0;
6595         for( i=0; i<nseq; i++ ) 
6596         {
6597                 hist[i] = -1;
6598                 nmemar[i] = 1;
6599         }
6600
6601         if( progressout ) reporterr(       "\n" );
6602         for( k=0; k<nseq-1; k++ )
6603         {
6604                 if( progressout && k % 10 == 0 ) reporterr(       "\r% 5d / %d", k, nseq );
6605
6606                 minscore = 999.9;
6607                 for( acpti=ac; acpti->next!=NULL; acpti=acpti->next ) 
6608                 {
6609                         i = acpti->pos;
6610 //                      reporterr(       "k=%d i=%d\n", k, i );
6611                         if( mindisfrom[i] < minscore ) // muscle
6612                         {
6613                                 im = i;
6614                                 minscore = mindisfrom[i];
6615                         }
6616                 }
6617                 jm = nearest[im];
6618                 if( jm < im ) 
6619                 {
6620                         j=jm; jm=im; im=j;
6621                 }
6622
6623
6624                 prevnode = hist[im];
6625                 if( dep ) dep[k].child0 = prevnode;
6626                 nmemim = nmemar[im];
6627                 intpt = topol[k][0] = (int *)realloc( topol[k][0], ( nmemim + 1 ) * sizeof( int ) );
6628                 if( prevnode == -1 )
6629                 {
6630                         *intpt++ = im;
6631                         *intpt = -1;
6632                 }
6633                 else
6634                 {
6635                         pt1 = topol[prevnode][0];
6636                         pt2 = topol[prevnode][1];
6637                         if( *pt1 > *pt2 )
6638                         {
6639                                 pt11 = pt2;
6640                                 pt22 = pt1;
6641                         }
6642                         else
6643                         {
6644                                 pt11 = pt1;
6645                                 pt22 = pt2;
6646                         }
6647                         for( intpt2=pt11; *intpt2!=-1; )
6648                                 *intpt++ = *intpt2++;
6649                         for( intpt2=pt22; *intpt2!=-1; )
6650                                 *intpt++ = *intpt2++;
6651                         *intpt = -1;
6652                 }
6653
6654                 prevnode = hist[jm];
6655                 if( dep ) dep[k].child1 = prevnode;
6656                 nmemjm = nmemar[jm];
6657                 intpt = topol[k][1] = (int *)realloc( topol[k][1], ( nmemjm + 1 ) * sizeof( int ) );
6658                 if( !intpt )
6659                 {
6660                         reporterr(       "Cannot reallocate topol\n" );
6661                         exit( 1 );
6662                 }
6663                 if( prevnode == -1 )
6664                 {
6665                         *intpt++ = jm;
6666                         *intpt = -1;
6667                 }
6668                 else
6669                 {
6670                         pt1 = topol[prevnode][0];
6671                         pt2 = topol[prevnode][1];
6672                         if( *pt1 > *pt2 )
6673                         {
6674                                 pt11 = pt2;
6675                                 pt22 = pt1;
6676                         }
6677                         else
6678                         {
6679                                 pt11 = pt1;
6680                                 pt22 = pt2;
6681                         }
6682                         for( intpt2=pt11; *intpt2!=-1; )
6683                                 *intpt++ = *intpt2++;
6684                         for( intpt2=pt22; *intpt2!=-1; )
6685                                 *intpt++ = *intpt2++;
6686                         *intpt = -1;
6687                 }
6688
6689                 minscore *= 0.5;
6690
6691                 len[k][0] = ( minscore - tmptmplen[im] );
6692                 len[k][1] = ( minscore - tmptmplen[jm] );
6693
6694                 if( dep ) dep[k].distfromtip = minscore;
6695
6696                 tmptmplen[im] = minscore;
6697
6698                 hist[im] = k;
6699                 nmemar[im] = nmemim + nmemjm;
6700
6701                 mindisfrom[im] = 999.9;
6702                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
6703         {
6704                         i = acpti->pos;
6705             if( i != im && i != jm )
6706             {
6707                                 if( i < im )
6708                                 {
6709                                          miniim = i;
6710                                          maxiim = im;
6711                                          minijm = i;
6712                                          maxijm = jm;
6713                                 }
6714                                 else if( i < jm )
6715                                 {
6716                                          miniim = im;
6717                                          maxiim = i;
6718                                          minijm = i;
6719                                          maxijm = jm;
6720                                 }
6721                                 else
6722                                 {
6723                                          miniim = im;
6724                                          maxiim = i;
6725                                          minijm = jm;
6726                                          maxijm = i;
6727                                 }
6728                                 eff0 = eff[miniim][maxiim-miniim];
6729                                 eff1 = eff[minijm][maxijm-minijm];
6730                                 tmpdouble = eff[miniim][maxiim-miniim] =
6731 #if 0
6732                                 MIN( eff0, eff1 ) * sueff1 + ( eff0 + eff1 ) * sueff05; 
6733 #else
6734                                 (clusterfuncpt[0])( eff0, eff1 );
6735 #endif
6736                                 if( tmpdouble < mindisfrom[i]  )
6737                                 {
6738                                         mindisfrom[i] = tmpdouble;
6739                                         nearest[i] = im;
6740                                 }
6741                                 if( tmpdouble < mindisfrom[im]  )
6742                                 {
6743                                         mindisfrom[im] = tmpdouble;
6744                                         nearest[im] = i;
6745                                 }
6746                                 if( nearest[i] == jm )
6747                                 {
6748                                         nearest[i] = im;
6749                                 }
6750             }
6751         }
6752
6753 //              reporterr(       "im,jm=%d,%d\n", im, jm );
6754                 acjmprev = ac[jm].prev; 
6755                 acjmnext = ac[jm].next; 
6756                 acjmprev->next = acjmnext;
6757                 if( acjmnext != NULL )
6758                         acjmnext->prev = acjmprev;
6759                 if( efffree )
6760                 {
6761                         free( (void *)eff[jm] ); eff[jm] = NULL;
6762                 }
6763
6764 #if 1 // muscle seems to miss this.
6765                 for( acpti=ac; acpti!=NULL; acpti=acpti->next )
6766                 {
6767                         i = acpti->pos;
6768                         if( nearest[i] == im ) 
6769                         {
6770                                 if( i < im )
6771                                 {
6772                                          miniim = i;
6773                                          maxiim = im;
6774                                 }
6775                                 else
6776                                 {
6777                                          miniim = im;
6778                                          maxiim = i;
6779                                 }
6780                                 if( eff[miniim][maxiim-miniim] > mindisfrom[i] )
6781                                         setnearest( nseq, ac, eff, mindisfrom+i, nearest+i, i );
6782                         }
6783                 }
6784 #endif
6785
6786
6787 #if 0
6788         fprintf( stdout, "vSTEP-%03d:\n", k+1 );
6789                 fprintf( stdout, "len0 = %f\n", len[k][0] );
6790         for( i=0; topol[k][0][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][0][i]+1 );
6791         fprintf( stdout, "\n" );
6792                 fprintf( stdout, "len1 = %f\n", len[k][1] );
6793         for( i=0; topol[k][1][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][1][i]+1 );
6794         fprintf( stdout, "\n" );
6795 #endif
6796     }
6797         free( (void *)tmptmplen ); tmptmplen = NULL;
6798         free( hist ); hist = NULL;
6799         free( (char *)ac ); ac = NULL;
6800         free( (void *)nmemar ); nmemar = NULL;
6801         free( mindisfrom );
6802         free( nearest );
6803 }
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813 void veryfastsupg_double_loadtree( int nseq, double **eff, int ***topol, double **len, char **name )
6814 {
6815     int i, j, k, miniim, maxiim, minijm, maxijm;
6816         int *intpt, *intpt2;
6817         double eff1, eff0;
6818     int *hist = NULL;
6819         Achain *ac = NULL;
6820         double minscore;
6821         char **tree;
6822         char *treetmp;
6823         int im = -1, jm = -1;
6824         int prevnode, acjmnext, acjmprev;
6825         int *pt1, *pt2, *pt11, *pt22;
6826         FILE *fp;
6827         int node[2];
6828         double lenfl[2];
6829         char *nametmp, *nameptr, *tmpptr; //static?
6830         char namec;
6831
6832         fp = fopen( "_guidetree", "r" );
6833         if( !fp )
6834         {
6835                 reporterr(       "cannot open _guidetree\n" );
6836                 exit( 1 );
6837         }
6838
6839
6840         if( !hist )
6841         {
6842 //              treetmp = AllocateCharVec( njob*50 );
6843                 treetmp = NULL;
6844 //              tree = AllocateCharMtx( njob, njob*50 );
6845                 tree = AllocateCharMtx( njob, 0 );
6846                 nametmp = AllocateCharVec( 1000 ); // nagasugi
6847                 hist = AllocateIntVec( njob );
6848                 ac = (Achain *)malloc( njob * sizeof( Achain ) );
6849         }
6850
6851         for( i=0; i<nseq; i++ )
6852         {
6853                 for( j=0; j<999; j++ ) nametmp[j] = 0;
6854                 for( j=0; j<999; j++ ) 
6855                 {
6856                         namec = name[i][j];
6857                         if( namec == 0 )
6858                                 break;
6859                         else if( isalnum( namec ) || namec == '/' || namec == '=' || namec == '-' || namec == '{' || namec == '}' )
6860                                 nametmp[j] = namec;
6861                         else
6862                                 nametmp[j] = '_';
6863                 }
6864                 nametmp[j] = 0;
6865 //              sprintf( tree[i], "%d_l=%d_%.20s", i+1, nlen[i], nametmp+1 );
6866                 if( outnumber )
6867                         nameptr = strstr( nametmp, "_numo_e" ) + 8;
6868                 else
6869                         nameptr = nametmp + 1;
6870
6871                 if( (tmpptr=strstr( nameptr, "_oe_" )) ) nameptr = tmpptr + 4; // = -> _ no tame
6872
6873                 tree[i] = calloc( strlen( nametmp )+100, sizeof( char ) ); // suuji no bun de +100
6874                 if( tree[i] == NULL )
6875                 {
6876                         reporterr(       "Cannot allocate tree!\n" );
6877                         exit( 1 );
6878                 }
6879                 sprintf( tree[i], "\n%d_%.900s\n", i+1, nameptr );
6880         }
6881         
6882         for( i=0; i<nseq; i++ )
6883         {
6884                 ac[i].next = i+1;
6885                 ac[i].prev = i-1;
6886 //              ac[i].curr = i;
6887         }
6888         ac[nseq-1].next = -1;
6889
6890     for( i=0; i<nseq; i++ ) hist[i] = -1;
6891
6892         reporterr(       "\n" );
6893     for( k=0; k<nseq-1; k++ )
6894     {
6895                 if( k % 10 == 0 ) reporterr(       "%d / %d\r", k, nseq );
6896
6897 #if 0
6898                 minscore = 99999.9;
6899                 for( i=0; ac[i].next!=-1; i=ac[i].next ) 
6900                 {
6901                         for( j=ac[i].next; j!=-1; j=ac[j].next )
6902                 {
6903                                 tmpdouble = eff[i][j];
6904                                 if( tmpdouble < minscore )
6905                                 {
6906                                         minscore = tmpdouble;
6907                                         im = i; jm = j;
6908                                 }
6909                         }
6910                 }
6911 #else
6912                 lenfl[0] = lenfl[1] = -1.0;
6913                 loadtreeoneline( node, lenfl, fp );
6914                 im = node[0];
6915                 jm = node[1];
6916                 minscore = eff[im][jm];
6917
6918                 if( im > nseq-1 || jm > nseq-1 || tree[im] == NULL || tree[jm] == NULL )
6919                 {
6920                         reporterr(       "\n\nCheck the guide tree.\n" );
6921                         reporterr(       "im=%d, jm=%d\n", im+1, jm+1 );
6922                         reporterr(       "Please use newick2mafft.rb to generate a tree file from a newick tree.\n\n" );
6923                         exit( 1 );
6924                 }
6925
6926
6927 //              reporterr(       "im=%d, jm=%d, minscore = %f\n", im, jm, minscore );
6928
6929
6930                 if( lenfl[0] == -1.0 || lenfl[1] == -1.0 )
6931                 {
6932                         reporterr(       "\n\nWARNING: Branch length is not given.\n" );
6933                         exit( 1 );
6934                 }
6935
6936                 if( lenfl[0] < 0.0 ) lenfl[0] = 0.0;
6937                 if( lenfl[1] < 0.0 ) lenfl[1] = 0.0;
6938 #endif
6939
6940 //              reporterr(       "im=%d, jm=%d\n", im, jm );
6941
6942                 intpt = topol[k][0];
6943                 prevnode = hist[im];
6944                 if( prevnode == -1 )
6945                 {
6946                         *intpt++ = im;
6947                         *intpt = -1;
6948                 }
6949                 else
6950                 {
6951                         pt1 = topol[prevnode][0];
6952                         pt2 = topol[prevnode][1];
6953                         if( *pt1 > *pt2 )
6954                         {
6955                                 pt11 = pt2;
6956                                 pt22 = pt1;
6957                         }
6958                         else
6959                         {
6960                                 pt11 = pt1;
6961                                 pt22 = pt2;
6962                         }
6963                         for( intpt2=pt11; *intpt2!=-1; )
6964                                 *intpt++ = *intpt2++;
6965                         for( intpt2=pt22; *intpt2!=-1; )
6966                                 *intpt++ = *intpt2++;
6967                         *intpt = -1;
6968                 }
6969
6970                 intpt = topol[k][1];
6971                 prevnode = hist[jm];
6972                 if( prevnode == -1 )
6973                 {
6974                         *intpt++ = jm;
6975                         *intpt = -1;
6976                 }
6977                 else
6978                 {
6979                         pt1 = topol[prevnode][0];
6980                         pt2 = topol[prevnode][1];
6981                         if( *pt1 > *pt2 )
6982                         {
6983                                 pt11 = pt2;
6984                                 pt22 = pt1;
6985                         }
6986                         else
6987                         {
6988                                 pt11 = pt1;
6989                                 pt22 = pt2;
6990                         }
6991                         for( intpt2=pt11; *intpt2!=-1; )
6992                                 *intpt++ = *intpt2++;
6993                         for( intpt2=pt22; *intpt2!=-1; )
6994                                 *intpt++ = *intpt2++;
6995                         *intpt = -1;
6996                 }
6997
6998                 minscore *= 0.5;
6999
7000 #if 0
7001                 len[k][0] = minscore - tmptmplen[im];
7002                 len[k][1] = minscore - tmptmplen[jm];
7003 #else
7004                 len[k][0] = lenfl[0];
7005                 len[k][1] = lenfl[1];
7006 #endif
7007
7008
7009                 hist[im] = k;
7010
7011                 for( i=0; i!=-1; i=ac[i].next )
7012         {
7013             if( i != im && i != jm )
7014             {
7015                                 if( i < im )
7016                                 {
7017                                          miniim = i;
7018                                          maxiim = im;
7019                                          minijm = i;
7020                                          maxijm = jm;
7021                                 }
7022                                 else if( i < jm )
7023                                 {
7024                                          miniim = im;
7025                                          maxiim = i;
7026                                          minijm = i;
7027                                          maxijm = jm;
7028                                 }
7029                                 else
7030                                 {
7031                                          miniim = im;
7032                                          maxiim = i;
7033                                          minijm = jm;
7034                                          maxijm = i;
7035                                 }
7036                                 eff0 = eff[miniim][maxiim];
7037                                 eff1 = eff[minijm][maxijm];
7038                 eff[miniim][maxiim] =
7039                 MIN( eff0, eff1 ) * ( 1.0 - sueff_global ) +
7040                                 ( eff0 + eff1 ) * 0.5 * sueff_global;
7041             }
7042         }
7043                 acjmprev = ac[jm].prev; 
7044                 acjmnext = ac[jm].next; 
7045                 ac[acjmprev].next = acjmnext;
7046                 if( acjmnext != -1 )
7047                         ac[acjmnext].prev = acjmprev;
7048
7049
7050                 treetmp = realloc( treetmp, strlen( tree[im] ) + strlen( tree[jm] ) + 100 ); // 22 de juubunn (:%7,:%7) %7 ha minus kamo
7051                 if( !treetmp )
7052                 {
7053                         reporterr(       "Cannot allocate treetmp\n" );
7054                         exit( 1 );
7055                 }
7056                 sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
7057                 free( tree[im] );
7058                 free( tree[jm] );
7059                 tree[im] = calloc( strlen( treetmp )+1, sizeof( char ) );
7060                 tree[jm] = NULL;
7061                 if( tree[im] == NULL )
7062                 {
7063                         reporterr(       "Cannot reallocate tree!\n" );
7064                         exit( 1 );
7065                 }
7066                 strcpy( tree[im], treetmp );
7067
7068 //              sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
7069 //              strcpy( tree[im], treetmp );
7070
7071 #if 0
7072         fprintf( stdout, "STEP-%03d:\n", k+1 );
7073                 fprintf( stdout, "len0 = %f\n", len[k][0] );
7074         for( i=0; topol[k][0][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][0][i] );
7075         fprintf( stdout, "\n" );
7076                 fprintf( stdout, "len1 = %f\n", len[k][1] );
7077         for( i=0; topol[k][1][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][1][i] );
7078         fprintf( stdout, "\n" );
7079 #endif
7080     }
7081         fclose( fp );
7082
7083
7084         fp = fopen( "infile.tree", "w" );
7085         fprintf( fp, "%s\n", treetmp );
7086 //      fprintf( fp, "by veryfastsupg_double_loadtree\n" );
7087         fclose( fp );
7088
7089 #if 1
7090         reporterr(       "\n" );
7091         free( hist );
7092         free( (char *)ac );
7093         FreeCharMtx( tree );
7094         free( treetmp );
7095         free( nametmp );
7096 #endif
7097
7098 #if 0
7099 //      reporterr(       "reconstructing eff[][]\n" ); // Tsune ni hat2 ha aru node koreha iranai.
7100     for( k=0; k<nseq; k++ ) for( i=0; i<nseq; i++ ) eff[i][k] = 0.0;
7101     for( k=0; k<nseq-1; k++ )
7102         {
7103                 reporterr(       "len[k][0], len[k][1] = %f, %f\n", len[k][0], len[k][1] );
7104         for( i=0; (im=topol[k][0][i])>-1; i++ )
7105                 {
7106                         reporterr(       " %03d", im );
7107                 }
7108                 fprintf( stdout, "\n" );
7109         for( i=0; (jm=topol[k][1][i])>-1; i++ ) 
7110                 {
7111                         reporterr(       " %03d", jm );
7112                 }
7113         for( i=0; (im=topol[k][0][i])>-1; i++ ) for( j=0; (jm=topol[k][1][j])>-1; j++ ) 
7114                 {
7115                         eff[im][jm] += len[k][0] + len[k][1];
7116                         eff[jm][im] += len[k][0] + len[k][1];
7117                 }
7118         }
7119 #endif
7120 }
7121
7122 #if 0
7123 void veryfastsupg_double( int nseq, double **eff, int ***topol, double **len )
7124 {
7125     int i, j, k, miniim, maxiim, minijm, maxijm;
7126         int *intpt, *intpt2;
7127         double tmpdouble;
7128         double eff1, eff0;
7129         static double *tmptmplen = NULL;
7130     static int *hist = NULL;
7131         static Achain *ac = NULL;
7132         double minscore;
7133         int im = -1, jm = -1;
7134         int prevnode, acjmnext, acjmprev;
7135         int *pt1, *pt2, *pt11, *pt22;
7136         if( !hist )
7137         {
7138                 hist = AllocateIntVec( njob );
7139                 tmptmplen = (double *)malloc( njob * sizeof( double ) );
7140                 ac = (Achain *)malloc( njob * sizeof( Achain ) );
7141         }
7142         
7143         for( i=0; i<nseq; i++ )
7144         {
7145                 ac[i].next = i+1;
7146                 ac[i].prev = i-1;
7147 //              ac[i].curr = i;
7148         }
7149         ac[nseq-1].next = -1;
7150
7151         for( i=0; i<nseq; i++ ) tmptmplen[i] = 0.0;
7152     for( i=0; i<nseq; i++ ) hist[i] = -1;
7153
7154         reporterr(       "\n" );
7155     for( k=0; k<nseq-1; k++ )
7156     {
7157                 if( k % 10 == 0 ) reporterr(       "%d / %d\r", k, nseq );
7158
7159                 minscore = 99999.9;
7160                 for( i=0; ac[i].next!=-1; i=ac[i].next ) 
7161                 {
7162                         for( j=ac[i].next; j!=-1; j=ac[j].next )
7163                 {
7164                                 tmpdouble = eff[i][j];
7165                                 if( tmpdouble < minscore )
7166                                 {
7167                                         minscore = tmpdouble;
7168                                         im = i; jm = j;
7169                                 }
7170                         }
7171                 }
7172
7173 //              reporterr(       "im=%d, jm=%d\n", im, jm );
7174
7175                 intpt = topol[k][0];
7176                 prevnode = hist[im];
7177                 if( prevnode == -1 )
7178                 {
7179                         *intpt++ = im;
7180                         *intpt = -1;
7181                 }
7182                 else
7183                 {
7184                         pt1 = topol[prevnode][0];
7185                         pt2 = topol[prevnode][1];
7186                         if( *pt1 > *pt2 )
7187                         {
7188                                 pt11 = pt2;
7189                                 pt22 = pt1;
7190                         }
7191                         else
7192                         {
7193                                 pt11 = pt1;
7194                                 pt22 = pt2;
7195                         }
7196                         for( intpt2=pt11; *intpt2!=-1; )
7197                                 *intpt++ = *intpt2++;
7198                         for( intpt2=pt22; *intpt2!=-1; )
7199                                 *intpt++ = *intpt2++;
7200                         *intpt = -1;
7201                 }
7202
7203                 intpt = topol[k][1];
7204                 prevnode = hist[jm];
7205                 if( prevnode == -1 )
7206                 {
7207                         *intpt++ = jm;
7208                         *intpt = -1;
7209                 }
7210                 else
7211                 {
7212                         pt1 = topol[prevnode][0];
7213                         pt2 = topol[prevnode][1];
7214                         if( *pt1 > *pt2 )
7215                         {
7216                                 pt11 = pt2;
7217                                 pt22 = pt1;
7218                         }
7219                         else
7220                         {
7221                                 pt11 = pt1;
7222                                 pt22 = pt2;
7223                         }
7224                         for( intpt2=pt11; *intpt2!=-1; )
7225                                 *intpt++ = *intpt2++;
7226                         for( intpt2=pt22; *intpt2!=-1; )
7227                                 *intpt++ = *intpt2++;
7228                         *intpt = -1;
7229                 }
7230
7231                 minscore *= 0.5;
7232
7233                 len[k][0] = minscore - tmptmplen[im];
7234                 len[k][1] = minscore - tmptmplen[jm];
7235
7236                 tmptmplen[im] = minscore;
7237
7238                 hist[im] = k;
7239
7240                 for( i=0; i!=-1; i=ac[i].next )
7241         {
7242             if( i != im && i != jm )
7243             {
7244                                 if( i < im )
7245                                 {
7246                                          miniim = i;
7247                                          maxiim = im;
7248                                          minijm = i;
7249                                          maxijm = jm;
7250                                 }
7251                                 else if( i < jm )
7252                                 {
7253                                          miniim = im;
7254                                          maxiim = i;
7255                                          minijm = i;
7256                                          maxijm = jm;
7257                                 }
7258                                 else
7259                                 {
7260                                          miniim = im;
7261                                          maxiim = i;
7262                                          minijm = jm;
7263                                          maxijm = i;
7264                                 }
7265                                 eff0 = eff[miniim][maxiim];
7266                                 eff1 = eff[minijm][maxijm];
7267                 eff[miniim][maxiim] =
7268                 MIN( eff0, eff1 ) * ( 1.0 - sueff_global ) +
7269                                 ( eff0 + eff1 ) * 0.5 * sueff_global;
7270             }
7271         }
7272                 acjmprev = ac[jm].prev; 
7273                 acjmnext = ac[jm].next; 
7274                 ac[acjmprev].next = acjmnext;
7275                 if( acjmnext != -1 )
7276                         ac[acjmnext].prev = acjmprev;
7277 #if 0
7278         fprintf( stdout, "STEP-%03d:\n", k+1 );
7279                 fprintf( stdout, "len0 = %f\n", len[k][0] );
7280         for( i=0; topol[k][0][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][0][i] );
7281         fprintf( stdout, "\n" );
7282                 fprintf( stdout, "len1 = %f\n", len[k][1] );
7283         for( i=0; topol[k][1][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][1][i] );
7284         fprintf( stdout, "\n" );
7285 #endif
7286     }
7287 #if 1
7288         reporterr(       "\n" );
7289         free( (void *)tmptmplen ); tmptmplen = NULL;
7290         free( hist ); hist = NULL;
7291         free( (char *)ac ); ac = NULL;
7292 #endif
7293 }
7294 #endif
7295
7296 void veryfastsupg_double_outtree( int nseq, double **eff, int ***topol, double **len, char **name ) // not used
7297 {
7298     int i, j, k, miniim, maxiim, minijm, maxijm;
7299         int *intpt, *intpt2;
7300         double tmpdouble;
7301         double eff1, eff0;
7302         static double *tmptmplen = NULL;
7303     static int *hist = NULL;
7304         static Achain *ac = NULL;
7305         double minscore;
7306         static char **tree;
7307         static char *treetmp;
7308         static char *nametmp;
7309         FILE *fpout;
7310         int im = -1, jm = -1;
7311         int prevnode, acjmnext, acjmprev;
7312         int *pt1, *pt2, *pt11, *pt22;
7313         double (*clusterfuncpt[1])(double,double);
7314
7315
7316         sueff1 = 1 - sueff_global;
7317         sueff05 = sueff_global * 0.5;
7318         if ( treemethod == 'X' )
7319                 clusterfuncpt[0] = cluster_mix_double;
7320         else if ( treemethod == 'E' )
7321                 clusterfuncpt[0] = cluster_average_double;
7322         else if ( treemethod == 'q' )
7323                 clusterfuncpt[0] = cluster_minimum_double;
7324         else
7325         {
7326                 reporterr(       "Unknown treemethod, %c\n", treemethod );
7327                 exit( 1 );
7328         }
7329
7330         if( !hist )
7331         {
7332                 treetmp = AllocateCharVec( njob*50 );
7333                 tree = AllocateCharMtx( njob, njob*50 );
7334                 hist = AllocateIntVec( njob );
7335                 tmptmplen = (double *)malloc( njob * sizeof( double ) );
7336                 ac = (Achain *)malloc( njob * sizeof( Achain ) );
7337                 nametmp = AllocateCharVec( 31 );
7338         }
7339
7340 //      for( i=0; i<nseq; i++ ) sprintf( tree[i], "%d", i+1 );
7341     for( i=0; i<nseq; i++ )
7342         {
7343                 for( j=0; j<30; j++ ) nametmp[j] = 0;
7344                 for( j=0; j<30; j++ ) 
7345                 {
7346                         if( isalnum( name[i][j] ) )
7347                                 nametmp[j] = name[i][j];
7348                         else
7349                                 nametmp[j] = '_';
7350                 }
7351                 nametmp[30] = 0;
7352                 sprintf( tree[i], "%d_%.20s", i+1, nametmp+1 );
7353         }
7354         
7355         for( i=0; i<nseq; i++ )
7356         {
7357                 ac[i].next = i+1;
7358                 ac[i].prev = i-1;
7359 //              ac[i].curr = i;
7360         }
7361         ac[nseq-1].next = -1;
7362
7363         for( i=0; i<nseq; i++ ) tmptmplen[i] = 0.0;
7364     for( i=0; i<nseq; i++ ) hist[i] = -1;
7365
7366         reporterr(       "\n" );
7367     for( k=0; k<nseq-1; k++ )
7368     {
7369                 if( k % 10 == 0 ) reporterr(       "%d / %d\r", k, nseq );
7370
7371                 minscore = 99999.9;
7372                 for( i=0; ac[i].next!=-1; i=ac[i].next ) 
7373                 {
7374                         for( j=ac[i].next; j!=-1; j=ac[j].next )
7375                 {
7376                                 tmpdouble = eff[i][j];
7377                                 if( tmpdouble < minscore )
7378                                 {
7379                                         minscore = tmpdouble;
7380                                         im = i; jm = j;
7381                                 }
7382                         }
7383                 }
7384
7385 //              reporterr(       "im=%d, jm=%d\n", im, jm );
7386
7387                 intpt = topol[k][0];
7388                 prevnode = hist[im];
7389                 if( prevnode == -1 )
7390                 {
7391                         *intpt++ = im;
7392                         *intpt = -1;
7393                 }
7394                 else
7395                 {
7396                         pt1 = topol[prevnode][0];
7397                         pt2 = topol[prevnode][1];
7398                         if( *pt1 > *pt2 )
7399                         {
7400                                 pt11 = pt2;
7401                                 pt22 = pt1;
7402                         }
7403                         else
7404                         {
7405                                 pt11 = pt1;
7406                                 pt22 = pt2;
7407                         }
7408                         for( intpt2=pt11; *intpt2!=-1; )
7409                                 *intpt++ = *intpt2++;
7410                         for( intpt2=pt22; *intpt2!=-1; )
7411                                 *intpt++ = *intpt2++;
7412                         *intpt = -1;
7413                 }
7414
7415                 intpt = topol[k][1];
7416                 prevnode = hist[jm];
7417                 if( prevnode == -1 )
7418                 {
7419                         *intpt++ = jm;
7420                         *intpt = -1;
7421                 }
7422                 else
7423                 {
7424                         pt1 = topol[prevnode][0];
7425                         pt2 = topol[prevnode][1];
7426                         if( *pt1 > *pt2 )
7427                         {
7428                                 pt11 = pt2;
7429                                 pt22 = pt1;
7430                         }
7431                         else
7432                         {
7433                                 pt11 = pt1;
7434                                 pt22 = pt2;
7435                         }
7436                         for( intpt2=pt11; *intpt2!=-1; )
7437                                 *intpt++ = *intpt2++;
7438                         for( intpt2=pt22; *intpt2!=-1; )
7439                                 *intpt++ = *intpt2++;
7440                         *intpt = -1;
7441                 }
7442
7443                 minscore *= 0.5;
7444
7445                 len[k][0] = minscore - tmptmplen[im];
7446                 len[k][1] = minscore - tmptmplen[jm];
7447
7448                 tmptmplen[im] = minscore;
7449
7450                 hist[im] = k;
7451
7452                 for( i=0; i!=-1; i=ac[i].next )
7453         {
7454             if( i != im && i != jm )
7455             {
7456                                 if( i < im )
7457                                 {
7458                                          miniim = i;
7459                                          maxiim = im;
7460                                          minijm = i;
7461                                          maxijm = jm;
7462                                 }
7463                                 else if( i < jm )
7464                                 {
7465                                          miniim = im;
7466                                          maxiim = i;
7467                                          minijm = i;
7468                                          maxijm = jm;
7469                                 }
7470                                 else
7471                                 {
7472                                          miniim = im;
7473                                          maxiim = i;
7474                                          minijm = jm;
7475                                          maxijm = i;
7476                                 }
7477                                 eff0 = eff[miniim][maxiim];
7478                                 eff1 = eff[minijm][maxijm];
7479                 eff[miniim][maxiim] =
7480                                 (clusterfuncpt[0])( eff0, eff1 );
7481             }
7482         }
7483                 acjmprev = ac[jm].prev; 
7484                 acjmnext = ac[jm].next; 
7485                 ac[acjmprev].next = acjmnext;
7486                 if( acjmnext != -1 )
7487                         ac[acjmnext].prev = acjmprev;
7488
7489                 sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], len[k][0], tree[jm], len[k][1] );
7490                 strcpy( tree[im], treetmp );
7491 #if 0
7492         fprintf( stdout, "STEP-%03d:\n", k+1 );
7493                 fprintf( stdout, "len0 = %f\n", len[k][0] );
7494         for( i=0; topol[k][0][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][0][i] );
7495         fprintf( stdout, "\n" );
7496                 fprintf( stdout, "len1 = %f\n", len[k][1] );
7497         for( i=0; topol[k][1][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][1][i] );
7498         fprintf( stdout, "\n" );
7499 #endif
7500     }
7501         fpout = fopen( "infile.tree", "w" );
7502         fprintf( fpout, "%s\n", treetmp );
7503 //      fprintf( fpout, "by veryfastsupg_double_outtree\n" );
7504         fclose( fpout );
7505 #if 1
7506         reporterr(       "\n" );
7507         free( (void *)tmptmplen ); tmptmplen = NULL;
7508         free( hist ); hist = NULL;
7509         free( (char *)ac ); ac = NULL;
7510         FreeCharMtx( tree );
7511         free( treetmp );
7512         free( nametmp );
7513 #endif
7514 }
7515
7516 void veryfastsupg( int nseq, double **oeff, int ***topol, double **len )
7517 {
7518     int i, j, k, miniim, maxiim, minijm, maxijm;
7519         int *intpt, *intpt2;
7520         int tmpint;
7521         int eff1, eff0;
7522         static double *tmptmplen = NULL;
7523         static int **eff = NULL;
7524     static int *hist = NULL;
7525         static Achain *ac = NULL;
7526         int minscore;
7527         double minscoref;
7528         int im = -1, jm = -1;
7529         int prevnode, acjmnext, acjmprev;
7530         int *pt1, *pt2, *pt11, *pt22;
7531         if( !eff )
7532         {
7533                 eff = AllocateIntMtx( njob, njob );
7534                 hist = AllocateIntVec( njob );
7535                 tmptmplen = (double *)malloc( njob * sizeof( double ) );
7536                 ac = (Achain *)malloc( njob * sizeof( Achain ) );
7537         }
7538         
7539         for( i=0; i<nseq; i++ ) 
7540         {
7541                 for( j=0; j<nseq; j++ ) 
7542                 {
7543                         eff[i][j] = (int)( oeff[i][j] * INTMTXSCALE + 0.5 );
7544                 }
7545         }
7546
7547         for( i=0; i<nseq; i++ )
7548         {
7549                 ac[i].next = i+1;
7550                 ac[i].prev = i-1;
7551 //              ac[i].curr = i;
7552         }
7553         ac[nseq-1].next = -1;
7554
7555         for( i=0; i<nseq; i++ ) tmptmplen[i] = 0.0;
7556     for( i=0; i<nseq; i++ ) hist[i] = -1;
7557
7558         reporterr(       "\n" );
7559     for( k=0; k<nseq-1; k++ )
7560     {
7561                 if( k % 10 == 0 ) reporterr(       "%d / %d\r", k, nseq );
7562
7563                 minscore = INTMTXSCALE*4;
7564                 for( i=0; ac[i].next!=-1; i=ac[i].next ) 
7565                 {
7566                         for( j=ac[i].next; j!=-1; j=ac[j].next )
7567                 {
7568                                 tmpint = eff[i][j];
7569                                 if( tmpint < minscore )
7570                                 {
7571                                         minscore = tmpint;
7572                                         im = i; jm = j;
7573                                 }
7574                         }
7575                 }
7576                 minscoref = (double)minscore * 0.5 / ( INTMTXSCALE );
7577
7578 //              reporterr(       "im=%d, jm=%d\n", im, jm );
7579
7580 #if 1
7581                 intpt = topol[k][0];
7582                 prevnode = hist[im];
7583                 if( prevnode == -1 )
7584                 {
7585                         *intpt++ = im;
7586                         *intpt = -1;
7587                 }
7588                 else
7589                 {
7590                         pt1 = topol[prevnode][0];
7591                         pt2 = topol[prevnode][1];
7592                         if( *pt1 > *pt2 )
7593                         {
7594                                 pt11 = pt2;
7595                                 pt22 = pt1;
7596                         }
7597                         else
7598                         {
7599                                 pt11 = pt1;
7600                                 pt22 = pt2;
7601                         }
7602                         for( intpt2=pt11; *intpt2!=-1; )
7603                                 *intpt++ = *intpt2++;
7604                         for( intpt2=pt22; *intpt2!=-1; )
7605                                 *intpt++ = *intpt2++;
7606                         *intpt = -1;
7607                 }
7608
7609                 intpt = topol[k][1];
7610                 prevnode = hist[jm];
7611                 if( prevnode == -1 )
7612                 {
7613                         *intpt++ = jm;
7614                         *intpt = -1;
7615                 }
7616                 else
7617                 {
7618                         pt1 = topol[prevnode][0];
7619                         pt2 = topol[prevnode][1];
7620                         if( *pt1 > *pt2 )
7621                         {
7622                                 pt11 = pt2;
7623                                 pt22 = pt1;
7624                         }
7625                         else
7626                         {
7627                                 pt11 = pt1;
7628                                 pt22 = pt2;
7629                         }
7630                         for( intpt2=pt11; *intpt2!=-1; )
7631                                 *intpt++ = *intpt2++;
7632                         for( intpt2=pt22; *intpt2!=-1; )
7633                                 *intpt++ = *intpt2++;
7634                         *intpt = -1;
7635                 }
7636 #else
7637                 intpt = topol[k][0];
7638         for( i=0; i<nseq; i++ )
7639             if( pair[im][i] > -2 )
7640                                 *intpt++ = i;
7641                 *intpt = -1;
7642
7643                 intpt = topol[k][1];
7644         for( i=0; i<nseq; i++ )
7645             if( pair[jm][i] > -2 )
7646                                 *intpt++ = i;
7647                 *intpt = -1;
7648 #endif
7649
7650                 len[k][0] = minscoref - tmptmplen[im];
7651                 len[k][1] = minscoref - tmptmplen[jm];
7652
7653                 tmptmplen[im] = minscoref;
7654
7655                 hist[im] = k;
7656
7657                 for( i=0; i!=-1; i=ac[i].next )
7658         {
7659             if( i != im && i != jm )
7660             {
7661                                 if( i < im )
7662                                 {
7663                                          miniim = i;
7664                                          maxiim = im;
7665                                          minijm = i;
7666                                          maxijm = jm;
7667                                 }
7668                                 else if( i < jm )
7669                                 {
7670                                          miniim = im;
7671                                          maxiim = i;
7672                                          minijm = i;
7673                                          maxijm = jm;
7674                                 }
7675                                 else
7676                                 {
7677                                          miniim = im;
7678                                          maxiim = i;
7679                                          minijm = jm;
7680                                          maxijm = i;
7681                                 }
7682                                 eff0 = eff[miniim][maxiim];
7683                                 eff1 = eff[minijm][maxijm];
7684                 eff[miniim][maxiim] =
7685                 MIN( eff0, eff1 ) * ( 1.0 - sueff_global ) + // int??
7686                                 ( eff0 + eff1 ) * 0.5 * sueff_global;        // int??
7687             }
7688         }
7689                 acjmprev = ac[jm].prev; 
7690                 acjmnext = ac[jm].next; 
7691                 ac[acjmprev].next = acjmnext;
7692                 if( acjmnext != -1 )
7693                         ac[acjmnext].prev = acjmprev;
7694 #if 0
7695         fprintf( stdout, "STEP-%03d:\n", k+1 );
7696                 fprintf( stdout, "len0 = %f\n", len[k][0] );
7697         for( i=0; topol[k][0][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][0][i] );
7698         fprintf( stdout, "\n" );
7699                 fprintf( stdout, "len1 = %f\n", len[k][1] );
7700         for( i=0; topol[k][1][i]>-1; i++ ) fprintf( stdout, " %03d", topol[k][1][i] );
7701         fprintf( stdout, "\n" );
7702 #endif
7703     }
7704 #if 1
7705         FreeIntMtx( eff ); eff = NULL;
7706         free( (void *)tmptmplen ); tmptmplen = NULL;
7707         free( hist ); hist = NULL;
7708         free( (char *)ac ); ac = NULL;
7709 #endif
7710 }
7711
7712 void fastsupg( int nseq, double **oeff, int ***topol, double **len )
7713 {
7714     int i, j, k, miniim, maxiim, minijm, maxijm;
7715 #if 0
7716         double eff[nseq][nseq];
7717     char pair[njob][njob];
7718 #else
7719         static double *tmplen;
7720         int *intpt;
7721         double tmpdouble;
7722         double eff1, eff0;
7723         static double **eff = NULL;
7724     static char **pair = NULL;
7725         static Achain *ac;
7726         double minscore;
7727         int im = -1, jm = -1;
7728         if( !eff )
7729         {
7730                 eff = AllocateFloatMtx( njob, njob );
7731                 pair = AllocateCharMtx( njob, njob );
7732                 tmplen = AllocateFloatVec( njob );
7733                 ac = (Achain *)calloc( njob, sizeof( Achain ) );
7734         }
7735 #endif
7736         
7737         for( i=0; i<nseq; i++ ) 
7738         {
7739                 for( j=0; j<nseq; j++ ) 
7740                 {
7741                         eff[i][j] = (double)oeff[i][j];
7742                 }
7743         }
7744
7745         for( i=0; i<nseq; i++ )
7746         {
7747                 ac[i].next = i+1;
7748                 ac[i].prev = i-1;
7749 //              ac[i].curr = i;
7750         }
7751         ac[nseq-1].next = -1;
7752
7753         for( i=0; i<nseq; i++ ) tmplen[i] = 0.0;
7754     for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ ) pair[i][j] = 0;
7755     for( i=0; i<nseq; i++ ) pair[i][i] = 1;
7756
7757         reporterr(       "\n" );
7758     for( k=0; k<nseq-1; k++ )
7759     {
7760                 if( k % 10 == 0 ) reporterr(       "%d / %d\r", k, nseq );
7761
7762                 minscore = 9999.0;
7763                 for( i=0; ac[i].next!=-1; i=ac[i].next ) 
7764 //              for( i=0; i<nseq-1; i++ ) 
7765                 {
7766                         for( j=ac[i].next; j!=-1; j=ac[j].next )
7767 //                      for( j=i+1; j<nseq; j++ ) 
7768                 {
7769                                 tmpdouble = eff[i][j];
7770                                 if( tmpdouble < minscore )
7771                                 {
7772                                         minscore = tmpdouble;
7773                                         im = i; jm = j;
7774                                 }
7775                         }
7776                 }
7777
7778 //              reporterr(       "im=%d, jm=%d\n", im, jm );
7779
7780                 intpt = topol[k][0];
7781         for( i=0; i<nseq; i++ )
7782             if( pair[im][i] > 0 )
7783                                 *intpt++ = i;
7784                 *intpt = -1;
7785
7786                 intpt = topol[k][1];
7787         for( i=0; i<nseq; i++ )
7788             if( pair[jm][i] > 0 )
7789                                 *intpt++ = i;
7790                 *intpt = -1;
7791
7792                 minscore /= 2.0;
7793
7794                 len[k][0] = (double)minscore - tmplen[im];
7795                 len[k][1] = (double)minscore - tmplen[jm];
7796
7797                 tmplen[im] = (double)minscore;
7798
7799         for( i=0; i<nseq; i++ ) pair[im][i] += ( pair[jm][i] > 0 );
7800         for( i=0; i<nseq; i++ ) pair[jm][i] = 0;
7801
7802 //              for( i=0; i<nseq; i++ )
7803                 for( i=0; i!=-1; i=ac[i].next )
7804         {
7805             if( i != im && i != jm )
7806             {
7807                                 if( i < im )
7808                                 {
7809                                          miniim = i;
7810                                          maxiim = im;
7811                                          minijm = i;
7812                                          maxijm = jm;
7813                                 }
7814                                 else if( i < jm )
7815                                 {
7816                                          miniim = im;
7817                                          maxiim = i;
7818                                          minijm = i;
7819                                          maxijm = jm;
7820                                 }
7821                                 else
7822                                 {
7823                                          miniim = im;
7824                                          maxiim = i;
7825                                          minijm = jm;
7826                                          maxijm = i;
7827                                 }
7828                                 eff0 = eff[miniim][maxiim];
7829                                 eff1 = eff[minijm][maxijm];
7830                 eff[miniim][maxiim] =
7831                 MIN( eff0, eff1 ) * ( 1.0 - sueff_global ) +
7832                                 ( eff0 + eff1 ) * 0.5 * sueff_global;
7833 //                      eff[minijm][maxijm] = 9999.0;
7834             }
7835         }
7836                 ac[ac[jm].prev].next = ac[jm].next;
7837                 ac[ac[jm].next].prev = ac[jm].prev;
7838 //              eff[im][jm] = 9999.0;
7839 #if 0
7840         reporterr(       "STEP-%03d:\n", k+1 );
7841                 reporterr(       "len0 = %f\n", len[k][0] );
7842         for( i=0; topol[k][0][i]>-1; i++ ) reporterr(       " %03d", topol[k][0][i] );
7843         reporterr(       "\n" );
7844                 reporterr(       "len1 = %f\n", len[k][1] );
7845         for( i=0; topol[k][1][i]>-1; i++ ) reporterr(       " %03d", topol[k][1][i] );
7846         reporterr(       "\n" );
7847 #endif
7848     }
7849         reporterr(       "\n" );
7850
7851 //      FreeFloatMtx( eff );
7852 //      FreeCharMtx( pair );
7853 //      FreeFloatVec( tmplen );
7854 //      free( ac );
7855 }
7856 void supg( int nseq, double **oeff, int ***topol, double **len )
7857 {
7858     int i, j, k, miniim, maxiim, minijm, maxijm;
7859 #if 0
7860         double eff[nseq][nseq];
7861     char pair[njob][njob];
7862 #else
7863         static double *tmplen;
7864         int *intpt;
7865         double **doubleptpt;
7866         double *doublept;
7867         double tmpdouble;
7868         double eff1, eff0;
7869         static double **eff = NULL;
7870     static char **pair = NULL;
7871         if( !eff )
7872         {
7873                 eff = AllocateFloatMtx( njob, njob );
7874                 pair = AllocateCharMtx( njob, njob );
7875                 tmplen = AllocateFloatVec( njob );
7876         }
7877 #endif
7878
7879         
7880         for( i=0; i<nseq; i++ ) 
7881         {
7882                 for( j=0; j<nseq; j++ ) 
7883                 {
7884                         eff[i][j] = (double)oeff[i][j];
7885                 }
7886         }
7887         for( i=0; i<nseq; i++ ) tmplen[i] = 0.0;
7888     for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ ) pair[i][j] = 0;
7889     for( i=0; i<nseq; i++ ) pair[i][i] = 1;
7890
7891     for( k=0; k<nseq-1; k++ )
7892     {
7893         double minscore = 9999.0;
7894         int im = -1, jm = -1;
7895
7896
7897                 doubleptpt = eff;
7898         for( i=0; i<nseq-1; i++ ) 
7899                 {
7900                         doublept = *doubleptpt++ + i + 1;
7901                         for( j=i+1; j<nseq; j++ )
7902                 {
7903                                 tmpdouble = *doublept++;
7904                                 if( tmpdouble < minscore )
7905                                 {
7906                                         minscore = tmpdouble;
7907                                         im = i; jm = j;
7908                                 }
7909                         }
7910                 }
7911                 intpt = topol[k][0];
7912         for( i=0; i<nseq; i++ )
7913             if( pair[im][i] > 0 )
7914                                 *intpt++ = i;
7915                 *intpt = -1;
7916
7917                 intpt = topol[k][1];
7918         for( i=0; i<nseq; i++ )
7919             if( pair[jm][i] > 0 )
7920                                 *intpt++ = i;
7921                 *intpt = -1;
7922
7923                 len[k][0] = (double)minscore / 2.0 - tmplen[im];
7924                 len[k][1] = (double)minscore / 2.0 - tmplen[jm];
7925
7926                 tmplen[im] = (double)minscore / 2.0;
7927
7928         for( i=0; i<nseq; i++ ) pair[im][i] += ( pair[jm][i] > 0 );
7929         for( i=0; i<nseq; i++ ) pair[jm][i] = 0;
7930
7931         for( i=0; i<nseq; i++ )
7932         {
7933             if( i != im && i != jm )
7934             {
7935 #if 1
7936                                 if( i < im )
7937                                 {
7938                                          miniim = i;
7939                                          maxiim = im;
7940                                          minijm = i;
7941                                          maxijm = jm;
7942                                 }
7943                                 else if( i < jm )
7944                                 {
7945                                          miniim = im;
7946                                          maxiim = i;
7947                                          minijm = i;
7948                                          maxijm = jm;
7949                                 }
7950                                 else
7951                                 {
7952                                          miniim = im;
7953                                          maxiim = i;
7954                                          minijm = jm;
7955                                          maxijm = i;
7956                                 }
7957 #else
7958                                 miniim = MIN( i, im );
7959                                 maxiim = MAX( i, im );
7960                                 minijm = MIN( i, jm );
7961                                 maxijm = MAX( i, jm );
7962 #endif
7963 #if 1
7964                                 eff0 = eff[miniim][maxiim];
7965                                 eff1 = eff[minijm][maxijm];
7966                 eff[miniim][maxiim] =
7967                 MIN( eff0, eff1 ) * ( 1.0 - sueff_global ) +
7968                                 ( eff0 + eff1 ) * 0.5 * sueff_global;
7969 #else
7970                 MIN( eff[miniim][maxiim], eff[minijm][maxijm] ) * ( 1.0 - sueff_global ) +
7971                                 ( eff[miniim][maxiim] + eff[minijm][maxijm] ) * 0.5 * sueff_global;
7972 #endif
7973                 eff[minijm][maxijm] = 9999.0;
7974                 eff[im][jm] = 9999.0;
7975             }
7976         }
7977 #if DEBUG
7978         printf( "STEP-%03d:\n", k+1 );
7979                 printf( "len0 = %f\n", len[k][0] );
7980         for( i=0; topol[k][0][i]>-1; i++ ) printf( " %03d", topol[k][0][i] );
7981         printf( "\n" );
7982                 printf( "len1 = %f\n", len[k][1] );
7983         for( i=0; topol[k][1][i]>-1; i++ ) printf( " %03d", topol[k][1][i] );
7984         printf( "\n" );
7985 #endif
7986     }
7987 }
7988
7989 void spg( int nseq, double **oeff, int ***topol, double **len )
7990 {
7991     int i, j, k;
7992         double tmplen[M];
7993 #if 0
7994         double eff[nseq][nseq];
7995     char pair[njob][njob];
7996 #else
7997         double **eff = NULL;
7998     char **pair = NULL;
7999         if( !eff )
8000         {
8001                 eff = AllocateDoubleMtx( njob, njob );
8002                 pair = AllocateCharMtx( njob, njob );
8003         }
8004 #endif
8005         
8006         for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ ) eff[i][j] = oeff[i][j];
8007         for( i=0; i<nseq; i++ ) tmplen[i] = 0.0;
8008     for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ ) pair[i][j] = 0;
8009     for( i=0; i<nseq; i++ ) pair[i][i] = 1;
8010
8011     for( k=0; k<nseq-1; k++ )
8012     {
8013         double minscore = 9999.0;
8014         int im = -1, jm = -1;
8015         int count;
8016
8017         for( i=0; i<nseq-1; i++ ) for( j=i+1; j<nseq; j++ )
8018         {
8019             if( eff[i][j] < minscore )
8020             {
8021                 minscore = eff[i][j];
8022                 im = i; jm = j;
8023             }
8024         }
8025         for( i=0, count=0; i<nseq; i++ )
8026             if( pair[im][i] > 0 )
8027             {
8028                 topol[k][0][count] = i;
8029                 count++;
8030             }
8031         topol[k][0][count] = -1;
8032         for( i=0, count=0; i<nseq; i++ )
8033             if( pair[jm][i] > 0 )
8034             {
8035                 topol[k][1][count] = i;
8036                 count++;
8037             }
8038         topol[k][1][count] = -1;
8039
8040                 len[k][0] = minscore / 2.0 - tmplen[im];
8041                 len[k][1] = minscore / 2.0 - tmplen[jm];
8042
8043                 tmplen[im] = minscore / 2.0;
8044
8045         for( i=0; i<nseq; i++ ) pair[im][i] += ( pair[jm][i] > 0 );
8046         for( i=0; i<nseq; i++ ) pair[jm][i] = 0;
8047
8048         for( i=0; i<nseq; i++ )
8049         {
8050             if( i != im && i != jm )
8051             {
8052                 eff[MIN(i,im)][MAX(i,im)] =
8053                 MIN( eff[MIN(i,im)][MAX(i,im)], eff[MIN(i,jm)][MAX(i,jm)] );
8054                 eff[MIN(i,jm)][MAX(i,jm)] = 9999.0;
8055             }
8056             eff[im][jm] = 9999.0;
8057         }
8058 #if DEBUG
8059         printf( "STEP-%03d:\n", k+1 );
8060                 printf( "len0 = %f\n", len[k][0] );
8061         for( i=0; topol[k][0][i]>-1; i++ ) printf( " %03d", topol[k][0][i] );
8062         printf( "\n" );
8063                 printf( "len1 = %f\n", len[k][1] );
8064         for( i=0; topol[k][1][i]>-1; i++ ) printf( " %03d", topol[k][1][i] );
8065         printf( "\n" );
8066 #endif
8067     }
8068 }
8069
8070 double ipower( double x, int n )    /* n > 0  */
8071 {
8072     double r;
8073
8074     r = 1;
8075     while( n != 0 )
8076     {
8077         if( n & 1 ) r *= x;
8078         x *= x; n >>= 1;
8079     }
8080     return( r );
8081 }
8082
8083 void countnode( int nseq, int ***topol, double **node ) /* node[j][i] != node[i][j] */
8084 {
8085     int i, j, k, s1, s2;
8086     static double rootnode[M];
8087
8088     if( nseq-2 < 0 )
8089         {
8090                 reporterr(       "Too few sequence for countnode: nseq = %d\n", nseq );
8091                 exit( 1 );
8092     }
8093
8094     for( i=0; i<nseq; i++ ) rootnode[i] = 0;
8095     for( i=0; i<nseq-2; i++ )
8096     {
8097         for( j=0; topol[i][0][j]>-1; j++ )
8098             rootnode[topol[i][0][j]]++;
8099         for( j=0; topol[i][1][j]>-1; j++ )
8100             rootnode[topol[i][1][j]]++;
8101         for( j=0; topol[i][0][j]>-1; j++ )
8102         {
8103             s1 = topol[i][0][j];
8104             for( k=0; topol[i][1][k]>-1; k++ )
8105             {
8106                 s2 = topol[i][1][k];
8107                 node[MIN(s1,s2)][MAX(s1,s2)] = rootnode[s1] + rootnode[s2] - 1;
8108             }
8109         }
8110     }
8111     for( j=0; topol[nseq-2][0][j]>-1; j++ )
8112     {
8113         s1 = topol[nseq-2][0][j];
8114         for( k=0; topol[nseq-2][1][k]>-1; k++ )
8115         {
8116             s2 = topol[nseq-2][1][k];
8117             node[MIN(s1,s2)][MAX(s1,s2)] = rootnode[s1] + rootnode[s2];
8118         }
8119     }
8120 }
8121
8122 void countnode_int( int nseq, int ***topol, int **node )  /* node[i][j] == node[j][i] */
8123 {
8124     int i, j, k, s1, s2;
8125     int rootnode[M];
8126
8127     for( i=0; i<nseq; i++ ) rootnode[i] = 0;
8128     for( i=0; i<nseq-2; i++ )
8129     {
8130         for( j=0; topol[i][0][j]>-1; j++ )
8131             rootnode[topol[i][0][j]]++;
8132         for( j=0; topol[i][1][j]>-1; j++ )
8133             rootnode[topol[i][1][j]]++;
8134         for( j=0; topol[i][0][j]>-1; j++ )
8135         {
8136             s1 = topol[i][0][j];
8137             for( k=0; topol[i][1][k]>-1; k++ )
8138             {
8139                 s2 = topol[i][1][k];
8140                 node[MIN(s1,s2)][MAX(s1,s2)] = rootnode[s1] + rootnode[s2] - 1;
8141             }
8142         }
8143     }
8144     for( j=0; topol[nseq-2][0][j]>-1; j++ )
8145     {
8146         s1 = topol[nseq-2][0][j];
8147         for( k=0; topol[nseq-2][1][k]>-1; k++ )
8148         {
8149             s2 = topol[nseq-2][1][k];
8150             node[MIN(s1,s2)][MAX(s1,s2)] = rootnode[s1] + rootnode[s2];
8151         }
8152     }
8153         for( i=0; i<nseq-1; i++ ) for( j=i+1; j<nseq; j++ ) 
8154                 node[j][i] = node[i][j];
8155 #if DEBUG
8156         reporterr(       "node[][] in countnode_int" );
8157         for( i=0; i<nseq; i++ ) 
8158         {
8159                 for( j=0; j<nseq; j++ ) 
8160                 {
8161                         reporterr(       "%#3d", node[i][j] );
8162                 }
8163                 reporterr(       "\n" );
8164         }
8165 #endif
8166 }
8167
8168 void counteff_simple_double( int nseq, int ***topol, double **len, double *node )
8169 {
8170     int i, j, s1, s2;
8171         double total;
8172         static double rootnode[M];
8173         static double eff[M];
8174
8175 #if DEBUG
8176         for( i=0; i<nseq; i++ ){
8177                 reporterr(       "len0 = %f\n", len[i][0] );
8178                 reporterr(       "len1 = %f\n", len[i][1] );
8179         }
8180 #endif
8181     for( i=0; i<nseq; i++ )
8182         {
8183                 rootnode[i] = 0.0;
8184                 eff[i] = 1.0;
8185 /*
8186                 rootnode[i] = 1.0;
8187 */
8188         }
8189         for( i=0; i<nseq-1; i++ )
8190         {
8191         for( j=0; (s1=topol[i][0][j]) > -1; j++ )
8192                 {
8193                 rootnode[s1] += (double)len[i][0] * eff[s1];
8194                         eff[s1] *= 0.5;
8195 /*
8196                 rootnode[s1] *= 0.5;
8197 */
8198                         
8199                 }
8200         for( j=0; (s2=topol[i][1][j]) > -1; j++ )
8201                 {
8202                 rootnode[s2] +=  (double)len[i][1] * eff[s2];
8203                         eff[s2] *= 0.5;
8204 /*
8205                 rootnode[s2] *= 0.5;
8206 */
8207                                 
8208                 }
8209         }
8210         for( i=0; i<nseq; i++ ) 
8211         {
8212 #if 1 /* 97.9.29 */
8213                 rootnode[i] += GETA3;
8214 #endif
8215 #if 0
8216                 reporterr(       "### rootnode for %d = %f\n", i, rootnode[i] );
8217 #endif
8218         }
8219 #if 1
8220         total = 0.0;
8221         for( i=0; i<nseq; i++ ) 
8222         {
8223                 total += rootnode[i];
8224         }
8225 #else
8226         total = 1.0;
8227 #endif
8228                 
8229         for( i=0; i<nseq; i++ ) 
8230         {
8231                 node[i] = rootnode[i] / total;
8232         }
8233
8234 #if 0
8235         reporterr(       "weight array in counteff_simple\n" );
8236         for( i=0; i<nseq; i++ )
8237                 reporterr(       "%f\n", node[i] );
8238         printf( "\n" );
8239         exit( 1 );
8240 #endif
8241 }
8242
8243 void counteff_simple_double_nostatic_memsave( int nseq, int ***topol, double **len, Treedep *dep, double *node )
8244 {
8245     int i, j, s1, s2;
8246         double total;
8247         double *rootnode;
8248         double *eff;
8249         int **localmem;
8250         int posinmem;
8251
8252         rootnode = AllocateDoubleVec( nseq );
8253         eff = AllocateDoubleVec( nseq );
8254         localmem = AllocateIntMtx( 2, nseq+1 );
8255
8256         for( i=0; i<nseq; i++ ) // 2014/06/07, fu no eff wo sakeru.
8257         {
8258                 if( len[i][0] < 0.0 ) 
8259                 {
8260                         reporterr( "WARNING: negative branch length %f, step %d-0\n", len[i][0], i );
8261                         len[i][0] = 0.0;
8262                 }
8263                 if( len[i][1] < 0.0 ) 
8264                 {
8265                         reporterr( "WARNING: negative branch length %f, step %d-1\n", len[i][1], i );
8266                         len[i][1] = 0.0;
8267                 }
8268         }
8269 #if DEBUG
8270         for( i=0; i<nseq-1; i++ )
8271         {
8272                 reporterr( "\nstep %d, group 0\n", i );
8273                 for( j=0; topol[i][0][j]!=-1; j++) reporterr( "%3d ", topol[i][0][j] );
8274                 reporterr( "\n", i );
8275                 reporterr( "step %d, group 1\n", i );
8276                 for( j=0; topol[i][1][j]!=-1; j++) reporterr( "%3d ", topol[i][1][j] );
8277                 reporterr( "\n", i );
8278                 reporterr(       "len0 = %f\n", len[i][0] );
8279                 reporterr(       "len1 = %f\n", len[i][1] );
8280         }
8281 #endif
8282     for( i=0; i<nseq; i++ )
8283         {
8284                 rootnode[i] = 0.0;
8285                 eff[i] = 1.0;
8286 /*
8287                 rootnode[i] = 1.0;
8288 */
8289         }
8290         for( i=0; i<nseq-1; i++ )
8291         {
8292                 localmem[0][0] = -1;
8293                 posinmem = 0;
8294                 topolorder( njob, localmem[0], &posinmem, topol, dep, i, 0 );
8295                 localmem[1][0] = -1;
8296                 posinmem = 0;
8297                 topolorder( njob, localmem[1], &posinmem, topol, dep, i, 1 );
8298
8299         for( j=0; (s1=localmem[0][j]) > -1; j++ )
8300                 {
8301                 rootnode[s1] += (double)len[i][0] * eff[s1];
8302                         eff[s1] *= 0.5;
8303 /*
8304                 rootnode[s1] *= 0.5;
8305 */
8306                         
8307                 }
8308         for( j=0; (s2=localmem[1][j]) > -1; j++ )
8309                 {
8310                 rootnode[s2] +=  (double)len[i][1] * eff[s2];
8311                         eff[s2] *= 0.5;
8312 /*
8313                 rootnode[s2] *= 0.5;
8314 */
8315                                 
8316                 }
8317         }
8318         for( i=0; i<nseq; i++ ) 
8319         {
8320 #if 1 /* 97.9.29 */
8321                 rootnode[i] += GETA3;
8322 #endif
8323 #if 0
8324                 reporterr(       "### rootnode for %d = %f\n", i, rootnode[i] );
8325 #endif
8326         }
8327 #if 1
8328         total = 0.0;
8329         for( i=0; i<nseq; i++ ) 
8330         {
8331                 total += rootnode[i];
8332         }
8333 #else
8334         total = 1.0;
8335 #endif
8336                 
8337         for( i=0; i<nseq; i++ ) 
8338         {
8339                 node[i] = rootnode[i] / total;
8340         }
8341
8342 #if 0
8343         reporterr(       "weight array in counteff_simple\n" );
8344         for( i=0; i<nseq; i++ )
8345                 reporterr(       "%f\n", node[i] );
8346         printf( "\n" );
8347         exit( 1 );
8348 #endif
8349         free( rootnode );
8350         free( eff );
8351         FreeIntMtx( localmem );
8352 }
8353
8354 void counteff_simple_double_nostatic( int nseq, int ***topol, double **len, double *node )
8355 {
8356     int i, j, s1, s2;
8357         double total;
8358         double *rootnode;
8359         double *eff;
8360
8361         rootnode = AllocateDoubleVec( nseq );
8362         eff = AllocateDoubleVec( nseq );
8363
8364         for( i=0; i<nseq; i++ ) // 2014/06/07, fu no eff wo sakeru.
8365         {
8366                 if( len[i][0] < 0.0 ) 
8367                 {
8368                         reporterr( "WARNING: negative branch length %f, step %d-0\n", len[i][0], i );
8369                         len[i][0] = 0.0;
8370                 }
8371                 if( len[i][1] < 0.0 ) 
8372                 {
8373                         reporterr( "WARNING: negative branch length %f, step %d-1\n", len[i][1], i );
8374                         len[i][1] = 0.0;
8375                 }
8376         }
8377 #if DEBUG
8378         for( i=0; i<nseq-1; i++ )
8379         {
8380                 reporterr( "\nstep %d, group 0\n", i );
8381                 for( j=0; topol[i][0][j]!=-1; j++) reporterr( "%3d ", topol[i][0][j] );
8382                 reporterr( "\n", i );
8383                 reporterr( "step %d, group 1\n", i );
8384                 for( j=0; topol[i][1][j]!=-1; j++) reporterr( "%3d ", topol[i][1][j] );
8385                 reporterr( "\n", i );
8386                 reporterr(       "len0 = %f\n", len[i][0] );
8387                 reporterr(       "len1 = %f\n", len[i][1] );
8388         }
8389 #endif
8390     for( i=0; i<nseq; i++ )
8391         {
8392                 rootnode[i] = 0.0;
8393                 eff[i] = 1.0;
8394 /*
8395                 rootnode[i] = 1.0;
8396 */
8397         }
8398         for( i=0; i<nseq-1; i++ )
8399         {
8400         for( j=0; (s1=topol[i][0][j]) > -1; j++ )
8401                 {
8402                 rootnode[s1] += (double)len[i][0] * eff[s1];
8403                         eff[s1] *= 0.5;
8404 /*
8405                 rootnode[s1] *= 0.5;
8406 */
8407                         
8408                 }
8409         for( j=0; (s2=topol[i][1][j]) > -1; j++ )
8410                 {
8411                 rootnode[s2] +=  (double)len[i][1] * eff[s2];
8412                         eff[s2] *= 0.5;
8413 /*
8414                 rootnode[s2] *= 0.5;
8415 */
8416                                 
8417                 }
8418         }
8419         for( i=0; i<nseq; i++ ) 
8420         {
8421 #if 1 /* 97.9.29 */
8422                 rootnode[i] += GETA3;
8423 #endif
8424 #if 0
8425                 reporterr(       "### rootnode for %d = %f\n", i, rootnode[i] );
8426 #endif
8427         }
8428 #if 1
8429         total = 0.0;
8430         for( i=0; i<nseq; i++ ) 
8431         {
8432                 total += rootnode[i];
8433         }
8434 #else
8435         total = 1.0;
8436 #endif
8437                 
8438         for( i=0; i<nseq; i++ ) 
8439         {
8440                 node[i] = rootnode[i] / total;
8441         }
8442
8443 #if 0
8444         reporterr(       "weight array in counteff_simple\n" );
8445         for( i=0; i<nseq; i++ )
8446                 reporterr(       "%f\n", node[i] );
8447         printf( "\n" );
8448         exit( 1 );
8449 #endif
8450         free( rootnode );
8451         free( eff );
8452 }
8453
8454 void counteff_simple( int nseq, int ***topol, double **len, double *node )
8455 {
8456     int i, j, s1, s2;
8457         double total;
8458 #if 0
8459         static double rootnode[M];
8460         static double eff[M];
8461 #else
8462         double *rootnode;
8463         double *eff;
8464         rootnode = AllocateDoubleVec( nseq );
8465         eff = AllocateDoubleVec( nseq );
8466 #endif
8467
8468 #if DEBUG
8469         for( i=0; i<nseq; i++ ){
8470                 reporterr(       "len0 = %f\n", len[i][0] );
8471                 reporterr(       "len1 = %f\n", len[i][1] );
8472         }
8473 #endif
8474     for( i=0; i<nseq; i++ )
8475         {
8476                 rootnode[i] = 0.0;
8477                 eff[i] = 1.0;
8478 /*
8479                 rootnode[i] = 1.0;
8480 */
8481         }
8482         for( i=0; i<nseq-1; i++ )
8483         {
8484         for( j=0; (s1=topol[i][0][j]) > -1; j++ )
8485                 {
8486                 rootnode[s1] += len[i][0] * eff[s1];
8487                         eff[s1] *= 0.5;
8488 /*
8489                 rootnode[s1] *= 0.5;
8490 */
8491                         
8492                 }
8493         for( j=0; (s2=topol[i][1][j]) > -1; j++ )
8494                 {
8495                 rootnode[s2] +=  len[i][1] * eff[s2];
8496                         eff[s2] *= 0.5;
8497 /*
8498                 rootnode[s2] *= 0.5;
8499 */
8500                                 
8501                 }
8502         }
8503         for( i=0; i<nseq; i++ ) 
8504         {
8505 #if 1 /* 97.9.29 */
8506                 rootnode[i] += GETA3;
8507 #endif
8508 #if 0
8509                 reporterr(       "### rootnode for %d = %f\n", i, rootnode[i] );
8510 #endif
8511         }
8512 #if 1
8513         total = 0.0;
8514         for( i=0; i<nseq; i++ ) 
8515         {
8516                 total += rootnode[i];
8517         }
8518 #else
8519         total = 1.0;
8520 #endif
8521                 
8522         for( i=0; i<nseq; i++ ) 
8523         {
8524                 node[i] = rootnode[i] / total;
8525         }
8526
8527 #if 0
8528         reporterr(       "weight array in counteff_simple\n" );
8529         for( i=0; i<nseq; i++ )
8530                 reporterr(       "%f\n", node[i] );
8531         printf( "\n" );
8532         exit( 1 );
8533 #endif
8534 #if 1
8535         free( rootnode );
8536         free( eff );
8537 #endif
8538 }
8539
8540
8541 void counteff( int nseq, int ***topol, double **len, double **node )
8542 {
8543     int i, j, k, s1, s2;
8544         double rootnode[M];
8545         double eff[M];
8546
8547         if( mix ) 
8548         {
8549                 switch( weight )
8550                 {
8551                         case( 2 ): 
8552                                 weight = 3;
8553                                 break;
8554                         case( 3 ): 
8555                                 weight = 2;
8556                                 break;
8557                         default: 
8558                                 ErrorExit( "mix error" );
8559                                 break;
8560                 }
8561         }
8562
8563         if( weight == 2 )
8564         {
8565             for( i=0; i<nseq; i++ ) rootnode[i] = 0;
8566         for( i=0; i<nseq-2; i++ )
8567         {
8568                 for( j=0; topol[i][0][j]>-1; j++ )
8569                 rootnode[topol[i][0][j]]++;
8570                 for( j=0; topol[i][1][j]>-1; j++ )
8571                 rootnode[topol[i][1][j]]++;
8572                 for( j=0; topol[i][0][j]>-1; j++ )
8573                 {
8574                 s1 = topol[i][0][j];
8575                 for( k=0; topol[i][1][k]>-1; k++ )
8576                 {
8577                         s2 = topol[i][1][k];
8578                         node[MIN(s1,s2)][MAX(s1,s2)] = rootnode[s1] + rootnode[s2] - 1;
8579                 }
8580                 }
8581         }
8582         for( j=0; topol[nseq-2][0][j]>-1; j++ )
8583         {
8584                 s1 = topol[nseq-2][0][j];
8585                 for( k=0; topol[nseq-2][1][k]>-1; k++ )
8586                 {
8587                 s2 = topol[nseq-2][1][k];
8588                 node[MIN(s1,s2)][MAX(s1,s2)] = rootnode[s1] + rootnode[s2];
8589                 }
8590         }
8591                 for( i=0; i<nseq-1; i++ ) for( j=i+1; j<nseq; j++ )
8592                         node[i][j] = ipower( 0.5, (int)node[i][j] ) + geta2;
8593                 for( i=0; i<nseq-1; i++ ) for( j=i+1; j<nseq; j++ ) 
8594                         node[j][i] = node[i][j];
8595         }
8596
8597         if( weight == 3 )
8598         {
8599 #if DEBUG
8600                 for( i=0; i<nseq; i++ ){
8601                         reporterr(       "len0 = %f\n", len[i][0] );
8602                         reporterr(       "len1 = %f\n", len[i][1] );
8603                 }
8604 #endif
8605             for( i=0; i<nseq; i++ )
8606                 {
8607                         rootnode[i] = 0.0;
8608                         eff[i] = 1.0;
8609 /*
8610                         rootnode[i] = 1.0;
8611 */
8612                 }
8613         for( i=0; i<nseq-1; i++ )
8614         {
8615                 for( j=0; (s1=topol[i][0][j]) > -1; j++ )
8616                         {
8617                         rootnode[s1] += len[i][0] * eff[s1];
8618                                 eff[s1] *= 0.5;
8619 /*
8620                         rootnode[s1] *= 0.5;
8621 */
8622                                 
8623                         }
8624                 for( j=0; (s2=topol[i][1][j]) > -1; j++ )
8625                         {
8626                         rootnode[s2] +=  len[i][1] * eff[s2];
8627                                 eff[s2] *= 0.5;
8628 /*
8629                         rootnode[s2] *= 0.5;
8630 */
8631                                 
8632                         }
8633                 }
8634                 for( i=0; i<nseq; i++ ) 
8635                 {
8636 #if 1 /* 97.9.29 */
8637                         rootnode[i] += GETA3;
8638 #endif
8639 #if DEBUG
8640                         reporterr(       "rootnode for %d = %f\n", i, rootnode[i] );
8641 #endif
8642                 }
8643                 for( i=0; i<nseq; i++ ) 
8644                 {
8645                         for( j=0; j<nseq; j++ ) 
8646                                 if( j != i )
8647                                         node[i][j] = (double)rootnode[i] * rootnode[j];
8648                                 else node[i][i] = rootnode[i];
8649                 }
8650         }
8651
8652 #if 0
8653         printf( "weight matrix in counteff\n" );
8654         for( i=0; i<nseq; i++ )
8655         {
8656                 for( j=0; j<nseq; j++ ) 
8657                 {
8658                         printf( "%f ", node[i][j] );
8659                 }
8660                 printf( "\n" );
8661         }
8662 #endif
8663 }
8664
8665 double score_calcp( char *seq1, char *seq2, int len )
8666 {
8667         int k;
8668         unsigned char ms1, ms2;
8669         double tmpscore;
8670         int len2 = len - 2;
8671
8672         tmpscore = 0.0;
8673         for( k=0; k<len; k++ )
8674         {
8675                 ms1 = (unsigned char)seq1[k];
8676                 ms2 = (unsigned char)seq2[k];
8677                 if( ms1 == '-' && ms2 == '-' ) continue;
8678                 tmpscore += (double)amino_dis[ms1][ms2];
8679         
8680                 if( ms1 == (int)'-' ) 
8681                 {
8682                         tmpscore += (double)penalty;
8683                         tmpscore += (double)amino_dis[ms1][ms2];
8684                         while( (ms1=(unsigned char)seq1[++k]) == '-' )
8685                                 tmpscore += (double)amino_dis[ms1][ms2];
8686                         k--;
8687                         if( k >len2 ) break;
8688                         continue;
8689                 }
8690                 if( ms2 == (int)'-' )
8691                 {
8692                         tmpscore += (double)penalty;
8693                         tmpscore += (double)amino_dis[ms1][ms2];
8694                         while( (ms2=(unsigned char)seq2[++k]) == '-' )
8695                                 tmpscore += (double)amino_dis[ms1][ms2];
8696                         k--;
8697                         if( k > len2 ) break;
8698                         continue;
8699                 }
8700         }
8701         return( tmpscore );
8702 }
8703
8704 double score_calc1( char *seq1, char *seq2 )   /* method 1 */
8705 {
8706         int k;
8707         double score = 0.0;
8708         int count = 0;
8709         int len = strlen( seq1 );
8710
8711         for( k=0; k<len; k++ )
8712         {       
8713                 if( seq1[k] != '-' && seq2[k] != '-' )
8714                 {
8715                         score += (double)amino_dis[(unsigned char)seq1[k]][(unsigned char)seq2[k]];
8716                         count++;
8717                 }
8718         }
8719         if( count ) score /= (double)count;
8720         else score = 1.0;
8721         return( score );
8722 }
8723
8724 double substitution_nid( char *seq1, char *seq2 )
8725 {
8726         int k;
8727         double s12;
8728         int len = strlen( seq1 );
8729         
8730         s12 = 0.0;
8731         for( k=0; k<len; k++ )
8732                 if( seq1[k] != '-' && seq2[k] != '-' )
8733                         s12 += ( seq1[k] == seq2[k] );
8734
8735 //      fprintf( stdout, "s12 = %f\n", s12 );
8736         return( s12 );
8737 }
8738
8739 double substitution_score( char *seq1, char *seq2 )
8740 {
8741         int k;
8742         double s12;
8743         int len = strlen( seq1 );
8744         
8745         s12 = 0.0;
8746         for( k=0; k<len; k++ )
8747                 if( seq1[k] != '-' && seq2[k] != '-' )
8748                         s12 += amino_dis[(unsigned char)seq1[k]][(unsigned char)seq2[k]];
8749
8750 //      fprintf( stdout, "s12 = %f\n", s12 );
8751         return( s12 );
8752 }
8753
8754 double substitution_hosei( char *seq1, char *seq2 )   /* method 1 */
8755 #if 0
8756 {
8757         int k;
8758         double score = 0.0;
8759         int count = 0;
8760         int len = strlen( seq1 );
8761
8762         for( k=0; k<len; k++ )
8763         {       
8764                 if( seq1[k] != '-' && seq2[k] != '-' )
8765                 {
8766                         score += (double)( seq1[k] != seq2[k] );
8767                         count++;
8768                 }
8769         }
8770         if( count ) score /= (double)count;
8771         else score = 1.0;
8772         if( score < 0.95 ) score = - log( 1.0 - score );
8773         else score = 3.0;
8774         return( score );
8775 }
8776 #else
8777 {
8778         int count = 0;
8779         double score;
8780         int iscore = 0;
8781         char s1, s2;
8782
8783         while( (s1=*seq1++) )
8784         {
8785                 s2 = *seq2++;
8786                 if( s1 == '-' ) continue;
8787                 if( s2 == '-' ) continue;
8788                 iscore += ( s1 != s2 );
8789                 count++;
8790         }
8791         if( count ) score = (double)iscore / count;
8792         else score = 1.0;
8793         if( score < 0.95 ) score = - log( 1.0 - score );
8794         else score = 3.0;
8795         return( score );
8796 }
8797 #endif
8798
8799 double substitution( char *seq1, char *seq2 )   /* method 1 */
8800 {
8801         int k;
8802         double score = 0.0;
8803         int count = 0;
8804         int len = strlen( seq1 );
8805
8806         for( k=0; k<len; k++ )
8807         {       
8808                 if( seq1[k] != '-' && seq2[k] != '-' )
8809                 {
8810                         score += (double)( seq1[k] != seq2[k] );
8811                         count++;
8812                 }
8813         }
8814         if( count ) score /= (double)count;
8815         else score = 1.0;
8816         return( score );
8817 }
8818
8819
8820 void treeconstruction( char **seq, int nseq, int ***topol, double **len, double **eff )
8821 {
8822     int i, j;
8823
8824         if( weight > 1 )
8825         {
8826                 if( utree == 0 )
8827                 {
8828                 for( i=0; i<nseq-1; i++ ) for( j=i+1; j<nseq; j++ )
8829                         {
8830 /*
8831                          eff[i][j] = (double)score_calc1( seq[i], seq[j] );
8832 */
8833                          eff[i][j] = (double)substitution_hosei( seq[i], seq[j] );
8834  /*
8835                                  reporterr(       "%f\n", eff[i][j] );
8836  */
8837                         }
8838 /*
8839                         reporterr(       "distance matrix\n" );
8840                         for( i=0; i<nseq; i++ )
8841                         {
8842                                 for( j=0; j<nseq; j++ ) 
8843                                 {
8844                                         reporterr(       "%f ", eff[i][j] );
8845                                 }
8846                                 reporterr(       "\n" );
8847                         }
8848 */
8849 /*
8850                         upg( nseq, eff, topol, len );
8851                         upg2( nseq, eff, topol, len );
8852 */
8853                         spg( nseq, eff, topol, len );
8854                         counteff( nseq, topol, len, eff );
8855                 }
8856         }
8857         else
8858         {
8859                 for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ ) 
8860                         eff[i][j] = 1.0;
8861         }
8862 /*
8863 reporterr(       "weight matrix\n" );
8864 for( i=0; i<nseq; i++ )
8865 {
8866         for( j=0; j<nseq; j++ ) 
8867         {
8868                 reporterr(       "%f ", eff[i][j] );
8869         }
8870         reporterr(       "\n" );
8871 }
8872 */
8873 }
8874
8875 double bscore_calc( char **seq, int s, double **eff )  /* algorithm B */
8876 {
8877         int i, j, k;
8878         int gb1, gb2, gc1, gc2;
8879         int cob;
8880         int nglen;
8881     int len = strlen( seq[0] );
8882     long score;
8883
8884         score = 0;
8885         nglen = 0;
8886         for( i=0; i<s-1; i++ ) for( j=i+1; j<s; j++ )
8887         {
8888                 double efficient = eff[i][j];
8889
8890                 gc1 = 0;
8891                 gc2 = 0;
8892                 for( k=0; k<len; k++ )
8893                 {
8894                         gb1 = gc1;
8895                         gb2 = gc2;
8896
8897                         gc1 = ( seq[i][k] == '-' );
8898                         gc2 = ( seq[j][k] == '-' );
8899                         
8900             cob = 
8901                        !gb1  *  gc1
8902                          * !gb2  * !gc2
8903
8904                  + !gb1  * !gc1 
8905                  * !gb2  *  gc2
8906
8907                  + !gb1  *  gc1
8908                  *  gb2  * !gc2
8909
8910                  +  gb1  * !gc1
8911                  * !gb2  *  gc2
8912       
8913                                  + gb1   * !gc1
8914                                  * gb2   *  gc2      *BEFF
8915
8916                                  + gb1   *  gc1
8917                                  * gb2   * !gc2      *BEFF
8918                  ;
8919                         score += (long)cob * penalty * efficient;
8920                         score += (long)amino_dis[(unsigned char)seq[i][k]][(unsigned char)seq[j][k]] * efficient;
8921                         nglen += ( !gc1 * !gc2 );
8922                 }
8923         }
8924         return( (double)score / nglen + 400.0 * !scoremtx );
8925 }
8926
8927 void AllocateTmpSeqs( char ***mseq2pt, char **mseq1pt, int locnlenmax )
8928 {
8929         *mseq2pt = AllocateCharMtx( njob, locnlenmax+1 );
8930         *mseq1pt = AllocateCharVec( locnlenmax+1 );
8931 }
8932
8933 void FreeTmpSeqs( char **mseq2, char *mseq1 )
8934 {
8935         FreeCharMtx( mseq2 );
8936         free( (char *)mseq1 );
8937 }
8938
8939
8940 void gappick0( char *aseq, char *seq )
8941 {
8942         for( ; *seq != 0; seq++ )
8943         {
8944                 if( *seq != '-' )
8945                         *aseq++ = *seq;
8946         }
8947         *aseq = 0;
8948
8949 }
8950
8951 int isallgap( char *seq )
8952 {
8953         for( ; *seq != 0; seq++ )
8954         {
8955                 if( *seq != '-' )
8956                         return( 0 );
8957         }
8958         return( 1 );
8959 }
8960
8961 void gappick( int nseq, int s, char **aseq, char **mseq2, 
8962                           double **eff, double *effarr )
8963 {
8964         int i, j, count, countjob, len, allgap;
8965         len = strlen( aseq[0] );
8966         for( i=0, count=0; i<len; i++ ) 
8967         {
8968                 allgap = 1;
8969                 for( j=0; j<nseq; j++ ) if( j != s ) allgap *= ( aseq[j][i] == '-' );
8970         if( allgap == 0 )
8971                 {
8972                         for( j=0, countjob=0; j<nseq; j++ ) 
8973                         {
8974                                 if( j != s )
8975                                 {
8976                                         mseq2[countjob][count] = aseq[j][i];
8977                                         countjob++;
8978                                 }
8979                         }
8980                         count++;
8981                 }
8982         }
8983         for( i=0; i<nseq-1; i++ ) mseq2[i][count] = 0;
8984
8985         for( i=0, countjob=0; i<nseq; i++ ) 
8986         {
8987                 if( i != s )
8988                 {
8989                         effarr[countjob] = eff[s][i];
8990                         countjob++;
8991                 }
8992         }
8993 /*
8994 fprintf( stdout, "effarr in gappick s = %d\n", s+1 );
8995 for( i=0; i<countjob; i++ ) 
8996         fprintf( stdout, " %f", effarr[i] );
8997 printf( "\n" );
8998 */
8999 }
9000
9001 void commongappick_record( int nseq, char **seq, int *map )
9002 {
9003         int i, j, count;
9004         int len = strlen( seq[0] );
9005
9006
9007         for( i=0, count=0; i<=len; i++ ) 
9008         {
9009         /*
9010                 allgap = 1;
9011                 for( j=0; j<nseq; j++ ) 
9012                         allgap *= ( seq[j][i] == '-' );
9013                 if( !allgap )
9014         */
9015                 for( j=0; j<nseq; j++ )
9016                         if( seq[j][i] != '-' ) break;
9017                 if( j != nseq )
9018                 {
9019                         for( j=0; j<nseq; j++ )
9020                         {
9021                                 seq[j][count] = seq[j][i];
9022                         }
9023                         map[count] = i;
9024                         count++;
9025                 }
9026         }
9027 }
9028
9029
9030 void commongappick( int nseq, char **seq )
9031 {
9032         int i, j, count;
9033         int len = strlen( seq[0] );
9034 #if 1
9035
9036         int *mapfromnewtoold;
9037
9038         mapfromnewtoold = calloc( len+1, sizeof( int ) );
9039
9040         for( i=0, count=0; i<=len; i++ ) 
9041         {
9042                 for( j=0; j<nseq; j++ )
9043                         if( seq[j][i] != '-' ) break;
9044                 if( j != nseq )
9045                 {
9046                         mapfromnewtoold[count++] = i;
9047                 }
9048         }
9049 //      mapfromnewtoold[count] = -1; // iranai
9050         for( j=0; j<nseq; j++ )
9051         {
9052                 for( i=0; i<count; i++ )
9053                 {
9054                         seq[j][i] = seq[j][mapfromnewtoold[i]];
9055                 }
9056         }
9057         free( mapfromnewtoold );
9058 #else
9059 --------------------------
9060
9061         int *mapfromoldtonew;
9062         int pos;
9063
9064         mapfromoldtonew = calloc( len+1, sizeof( int ) );
9065         for( i=0; i<=len; i++ ) mapfromoldtonew[i] = -1;
9066
9067         for( i=0, count=0; i<=len; i++ ) 
9068         {
9069                 for( j=0; j<nseq; j++ )
9070                         if( seq[j][i] != '-' ) break;
9071                 if( j != nseq )
9072                 {
9073                         mapfromoldtonew[i] = count;
9074                         count++;
9075                 }
9076         }
9077         for( j=0; j<nseq; j++ )
9078         {
9079                 for( i=0; i<=len; i++ ) 
9080                 {
9081                         if( (pos=mapfromoldtonew[i]) != -1 )
9082                                 seq[j][pos] = seq[j][i];
9083                 }
9084         }
9085         free( mapfromoldtonew );
9086 --------------------------
9087
9088         for( i=0, count=0; i<=len; i++ ) 
9089         {
9090         /*
9091                 allgap = 1;
9092                 for( j=0; j<nseq; j++ ) 
9093                         allgap *= ( seq[j][i] == '-' );
9094                 if( !allgap )
9095         */
9096                 for( j=0; j<nseq; j++ )
9097                         if( seq[j][i] != '-' ) break;
9098                 if( j != nseq )
9099                 {
9100                         for( j=0; j<nseq; j++ )
9101                         {
9102                                 seq[j][count] = seq[j][i];
9103                         }
9104                         count++;
9105                 }
9106         }
9107
9108 #endif
9109 }
9110
9111 #if 0
9112 void commongaprecord( int nseq, char **seq, char *originallygapped )
9113 {
9114         int i, j;
9115         int len = strlen( seq[0] );
9116
9117         for( i=0; i<len; i++ ) 
9118         {
9119                 for( j=0; j<nseq; j++ )
9120                         if( seq[j][i] != '-' ) break;
9121                 if( j == nseq )
9122                         originallygapped[i] = '-';
9123                 else
9124                         originallygapped[i] = 'o';
9125         }
9126         originallygapped[len] = 0;
9127 }
9128 #endif
9129                 
9130 double score_calc0( char **seq, int s, double **eff, int ex )
9131 {
9132         double tmp;
9133
9134         if( scmtd == 4 ) tmp = score_calc4( seq, s, eff, ex );
9135         if( scmtd == 5 ) tmp = score_calc5( seq, s, eff, ex );
9136         else             tmp = score_calc5( seq, s, eff, ex );
9137
9138         return( tmp );
9139
9140 }
9141
9142 /*
9143 double score_m_1( char **seq, int ex, double **eff )
9144 {
9145         int i, j, k;
9146         int len = strlen( seq[0] );
9147         int gb1, gb2, gc1, gc2;
9148         int cob;
9149         int nglen;
9150         double score;
9151
9152         score = 0.0;
9153         nglen = 0;
9154         for( i=0; i<njob; i++ ) 
9155         {
9156                 double efficient = eff[MIN(i,ex)][MAX(i,ex)];
9157                 if( i == ex ) continue;
9158
9159                 gc1 = 0; 
9160                 gc2 = 0;
9161                 for( k=0; k<len; k++ ) 
9162                 {
9163                         gb1 = gc1;
9164                         gb2 = gc2;
9165
9166                         gc1 = ( seq[i][k] == '-' );
9167                         gc2 = ( seq[ex][k] == '-' );
9168       
9169             cob = 
9170                    !gb1  *  gc1
9171                  * !gb2  * !gc2
9172
9173                  + !gb1  * !gc1
9174                  * !gb2  *  gc2
9175
9176                  + !gb1  *  gc1
9177                  *  gb2  * !gc2
9178
9179                  +  gb1  * !gc1
9180                  * !gb2  *  gc2
9181       
9182                  +  gb1  * !gc1
9183                  *  gb2  *  gc2      *BEFF
9184
9185                  +  gb1  *  gc1
9186                  *  gb2  * !gc2      *BEFF
9187                  ;
9188                         score += (double)cob * penalty * efficient;
9189                         score += (double)amino_dis[seq[i][k]][seq[ex][k]] * efficient;
9190                         *
9191                         nglen += ( !gc1 * !gc2 );
9192                         *
9193                         if( !gc1 && !gc2 ) fprintf( stdout, "%f\n", score );
9194                 }
9195         }
9196         return( (double)score / nglen + 400.0 * !scoremtx );
9197 }
9198 */
9199
9200 #if 0
9201 void sitescore( char **seq, double **eff, char sco1[], char sco2[], char sco3[] )
9202 {
9203         int i, j, k;
9204         int len = strlen( seq[0] );
9205         double tmp;
9206         double count;
9207         int ch;
9208         double sco[N];
9209
9210         for( i=0; i<len; i++ ) 
9211         {
9212                 tmp = 0.0; count = 0;
9213                 for( j=0; j<njob-1; j++ ) for( k=j+1; k<njob; k++ ) 
9214                 {
9215                 /*
9216                         if( seq[j][i] != '-' && seq[k][i] != '-' )
9217                 */
9218                         {
9219                                 tmp += amino_dis[seq[j][i]][seq[k][i]] + 400 * !scoremtx;
9220                                 count++; 
9221                         }
9222                 }
9223                 if( count > 0.0 ) tmp /= count;
9224                 else( tmp = 0.0 );
9225                 ch = (int)( tmp/100.0 - 0.000001 );
9226                 sprintf( sco1+i, "%c", ch+0x61 );
9227         }
9228         sco1[len] = 0;
9229
9230     for( i=0; i<len; i++ ) 
9231     {
9232         tmp = 0.0; count = 0;
9233         for( j=0; j<njob-1; j++ ) for( k=j+1; k<njob; k++ ) 
9234         {
9235                 /*
9236             if( seq[j][i] != '-' && seq[k][i] != '-' )
9237                 */
9238             {
9239                 tmp += eff[j][k] * ( amino_dis[seq[j][i]][seq[k][i]] + 400 * !scoremtx );
9240                 count += eff[j][k]; 
9241             }
9242         }
9243                 if( count > 0.0 ) tmp /= count;
9244                 else( tmp = 0.0 );
9245                 tmp = ( tmp - 400 * !scoremtx ) * 2;
9246                 if( tmp < 0 ) tmp = 0;
9247         ch = (int)( tmp/100.0 - 0.000001 );
9248         sprintf( sco2+i, "%c", ch+0x61 );
9249                 sco[i] = tmp;
9250     }
9251     sco2[len] = 0;
9252
9253         for( i=WIN; i<len-WIN; i++ )
9254         {
9255                 tmp = 0.0;
9256                 for( j=i-WIN; j<=i+WIN; j++ )
9257                 {
9258                         tmp += sco[j];
9259                 }
9260                 for( j=0; j<njob; j++ ) 
9261                 {
9262                         if( seq[j][i] == '-' )
9263                         {
9264                                 tmp = 0.0;
9265                                 break;
9266                         }
9267                 }
9268                 tmp /= WIN * 2 + 1;
9269                 ch = (int)( tmp/100.0 - 0.0000001 );
9270                 sprintf( sco3+i, "%c", ch+0x61 );
9271         }
9272         for( i=0; i<WIN; i++ ) sco3[i] = '-';
9273         for( i=len-WIN; i<len; i++ ) sco3[i] = '-';
9274         sco3[len] = 0;
9275 }
9276 #endif
9277
9278 void strins( char *str1, char *str2 )
9279 {
9280         char *bk;
9281         int len1 = strlen( str1 );
9282         int len2 = strlen( str2 );
9283
9284         bk = str2;
9285         str2 += len1+len2;
9286         str1 += len1-1;
9287
9288         while( str2 >= bk+len1 ) { *str2 = *(str2-len1); str2--;} // by D.Mathog
9289         while( str2 >= bk ) { *str2-- = *str1--; }
9290 }
9291
9292 int isaligned( int nseq, char **seq )
9293 {
9294         int i;
9295         int len = strlen( seq[0] );
9296         for( i=1; i<nseq; i++ ) 
9297         {
9298                 if( strlen( seq[i] ) != len ) return( 0 );
9299         }
9300         return( 1 );
9301 }
9302
9303 double score_calc_for_score( int nseq, char **seq )
9304 {
9305     int i, j, k, c;
9306     int len = strlen( seq[0] );
9307     double score;
9308     double tmpscore;
9309     char *mseq1, *mseq2;
9310
9311     score = 0.0;
9312     for( i=0; i<nseq-1; i++ )
9313     {
9314         for( j=i+1; j<nseq; j++ )
9315         {
9316             mseq1 = seq[i];
9317             mseq2 = seq[j];
9318             tmpscore = 0.0;
9319             c = 0;
9320             for( k=0; k<len; k++ )
9321             {
9322                 if( mseq1[k] == '-' && mseq2[k] == '-' ) continue;
9323                 tmpscore += amino_dis[(unsigned char)mseq1[k]][(unsigned char)mseq2[k]];
9324                 c++;
9325                 if( mseq1[k] == '-' )
9326                 {
9327                     tmpscore += penalty - n_dis[0][24];
9328                     while( mseq1[++k] == '-' )
9329                         ;
9330                     k--;
9331                     if( k > len-2 ) break;
9332                     continue;
9333                 }
9334                 if( mseq2[k] == '-' )
9335                 {
9336                     tmpscore += penalty - n_dis[0][24];
9337                     while( mseq2[++k] == '-' )
9338                         ;
9339                     k--;
9340                     if( k > len-2 ) break;
9341                     continue;
9342                 }
9343             }
9344             score += (double)tmpscore / (double)c;
9345 #if DEBUG
9346                         printf( "tmpscore in mltaln9.c = %f\n", tmpscore );
9347                         printf( "tmpscore / c          = %f\n", tmpscore/(double)c );
9348 #endif
9349         }
9350     }
9351         reporterr(       "raw score = %f\n", score );
9352         score /= (double)nseq * ( nseq-1.0 ) / 2.0;
9353         score += 400.0;
9354 #if DEBUG
9355         printf( "score in mltaln9.c = %f\n", score );
9356 #endif
9357     return( (double)score );
9358 }
9359
9360 void doublencpy( double *vec1, double *vec2, int len )
9361 {
9362         while( len-- )
9363                 *vec1++ = *vec2++;
9364 }
9365
9366 double score_calc_a( char **seq, int s, double **eff )  /* algorithm A+ */
9367 {
9368         int i, j, k;
9369         int gb1, gb2, gc1, gc2;
9370         int cob;
9371         int nglen;
9372     int len = strlen( seq[0] );
9373     double score;
9374
9375         score = 0;
9376         nglen = 0;
9377         for( i=0; i<s-1; i++ ) for( j=i+1; j<s; j++ )
9378         {
9379                 double efficient = eff[i][j];
9380
9381                 gc1 = 0;
9382                 gc2 = 0;
9383                 for( k=0; k<len; k++ )
9384                 {
9385                         gb1 = gc1;
9386                         gb2 = gc2;
9387
9388                         gc1 = ( seq[i][k] == '-' );
9389                         gc2 = ( seq[j][k] == '-' );
9390                         
9391             cob = 
9392                        !gb1  *  gc1
9393                          * !gb2  * !gc2
9394
9395                  +  gb1  * !gc1 
9396                  * !gb2  * !gc2
9397
9398                      + !gb1  * !gc1
9399                          * !gb2  *  gc2
9400
9401                  + !gb1  * !gc1 
9402                  *  gb2  * !gc2
9403
9404                  + !gb1  *  gc1
9405                  *  gb2  * !gc2
9406
9407                  +  gb1  * !gc1
9408                  * !gb2  *  gc2
9409       
9410                                  +  gb1  * !gc1
9411                                  *  gb2  *  gc2
9412
9413                                  +  gb1  *  gc1
9414                                  *  gb2  * !gc2
9415       
9416                                  + !gb1  *  gc1
9417                                  *  gb2  *  gc2
9418
9419                                  +  gb1  *  gc1
9420                                  * !gb2  *  gc2
9421                  ;
9422                         score += 0.5 * (double)cob * penalty * efficient;
9423                         score += (double)amino_dis[(unsigned char)seq[i][k]][(unsigned char)seq[j][k]] * (double)efficient;
9424                         nglen += ( !gc1 * !gc2 );
9425                 }
9426         }
9427         return( (double)score / nglen + 400.0 * !scoremtx );
9428 }
9429
9430
9431 double score_calc_s( char **seq, int s, double **eff )  /* algorithm S, not used */
9432 {
9433         int i, j, k;
9434         int gb1, gb2, gc1, gc2;
9435         int cob;
9436         int nglen;
9437     int len = strlen( seq[0] );
9438     double score;
9439
9440         score = 0;
9441         nglen = 0;
9442         for( i=0; i<s-1; i++ ) for( j=i+1; j<s; j++ )
9443         {
9444                 double efficient = eff[i][j];
9445
9446                 gc1 = 0;
9447                 gc2 = 0;
9448                 for( k=0; k<len; k++ )
9449                 {
9450                         gb1 = gc1;
9451                         gb2 = gc2;
9452
9453                         gc1 = ( seq[i][k] == '-' );
9454                         gc2 = ( seq[j][k] == '-' );
9455                         
9456             cob = 
9457                        !gb1  *  gc1
9458                          * !gb2  * !gc2
9459
9460                  +  gb1  * !gc1 
9461                  * !gb2  * !gc2
9462
9463                      + !gb1  * !gc1
9464                          * !gb2  *  gc2
9465
9466                  + !gb1  * !gc1 
9467                  *  gb2  * !gc2
9468
9469                  + !gb1  *  gc1
9470                  *  gb2  * !gc2
9471
9472                  +  gb1  * !gc1
9473                  * !gb2  *  gc2
9474       
9475 #if 0
9476                                  +  gb1  * !gc1
9477                                  *  gb2  *  gc2
9478
9479                                  +  gb1  *  gc1
9480                                  *  gb2  * !gc2
9481       
9482                                  + !gb1  *  gc1
9483                                  *  gb2  *  gc2
9484
9485                                  +  gb1  *  gc1
9486                                  * !gb2  *  gc2
9487 #endif
9488                  ;
9489                         score += 0.5 * (double)cob * penalty * efficient;
9490                         score += (double)amino_dis[(unsigned char)seq[i][k]][(int)seq[j][k]] * (double)efficient;
9491                         nglen += ( !gc1 * !gc2 );
9492                 }
9493         }
9494         return( (double)score / nglen + 400.0 );
9495 }
9496
9497 double score_calc_for_score_s( int s, char **seq )  /* algorithm S */
9498 {
9499         int i, j, k;
9500         int gb1, gb2, gc1, gc2;
9501         int cob;
9502         int nglen;
9503     int len = strlen( seq[0] );
9504     double score;
9505
9506         score = 0;
9507         nglen = 0;
9508         for( i=0; i<s-1; i++ ) for( j=i+1; j<s; j++ )
9509         {
9510
9511                 gc1 = 0;
9512                 gc2 = 0;
9513                 for( k=0; k<len; k++ )
9514                 {
9515                         gb1 = gc1;
9516                         gb2 = gc2;
9517
9518                         gc1 = ( seq[i][k] == '-' );
9519                         gc2 = ( seq[j][k] == '-' );
9520                         
9521             cob = 
9522                        !gb1  *  gc1
9523                          * !gb2  * !gc2
9524
9525                  +  gb1  * !gc1 
9526                  * !gb2  * !gc2
9527
9528                      + !gb1  * !gc1
9529                          * !gb2  *  gc2
9530
9531                  + !gb1  * !gc1 
9532                  *  gb2  * !gc2
9533
9534                  + !gb1  *  gc1
9535                  *  gb2  * !gc2
9536
9537                  +  gb1  * !gc1
9538                  * !gb2  *  gc2
9539       
9540 #if 0
9541                                  +  gb1  * !gc1
9542                                  *  gb2  *  gc2
9543
9544                                  +  gb1  *  gc1
9545                                  *  gb2  * !gc2
9546       
9547                                  + !gb1  *  gc1
9548                                  *  gb2  *  gc2
9549
9550                                  +  gb1  *  gc1
9551                                  * !gb2  *  gc2
9552 #endif
9553                  ;
9554                         score += 0.5 * (double)cob * penalty;
9555                         score += (double)amino_dis[(int)seq[i][k]][(unsigned char)seq[j][k]];
9556                         nglen += ( !gc1 * !gc2 );
9557                 }
9558 #if 0
9559                 reporterr(       "i = %d, j=%d\n", i+1, j+1 );
9560                 reporterr(       "score = %f\n", score );
9561 #endif
9562         }
9563         return( (double)score / nglen + 400.0 );
9564 }
9565
9566 double SSPscore___( int s, char **seq, int ex )  /* algorithm S */
9567 {
9568         int i, j, k;
9569         int gb1, gb2, gc1, gc2;
9570         int cob;
9571         int nglen;
9572     int len = strlen( seq[0] );
9573     double score;
9574
9575         score = 0;
9576         nglen = 0;
9577         i=ex; for( j=0; j<s; j++ )
9578         {
9579
9580                 if( j == ex ) continue;
9581
9582                 gc1 = 0;
9583                 gc2 = 0;
9584                 for( k=0; k<len; k++ )
9585                 {
9586                         gb1 = gc1;
9587                         gb2 = gc2;
9588
9589                         gc1 = ( seq[i][k] == '-' );
9590                         gc2 = ( seq[j][k] == '-' );
9591                         
9592             cob = 
9593                        !gb1  *  gc1
9594                          * !gb2  * !gc2
9595
9596                  +  gb1  * !gc1 
9597                  * !gb2  * !gc2
9598
9599                      + !gb1  * !gc1
9600                          * !gb2  *  gc2
9601
9602                  + !gb1  * !gc1 
9603                  *  gb2  * !gc2
9604
9605                  + !gb1  *  gc1
9606                  *  gb2  * !gc2 * 2.0 
9607
9608                  +  gb1  * !gc1
9609                  * !gb2  *  gc2 * 2.0 
9610       
9611 #if 0
9612                                  +  gb1  * !gc1
9613                                  *  gb2  *  gc2
9614
9615                                  +  gb1  *  gc1
9616                                  *  gb2  * !gc2
9617       
9618                                  + !gb1  *  gc1
9619                                  *  gb2  *  gc2
9620
9621                                  +  gb1  *  gc1
9622                                  * !gb2  *  gc2
9623 #endif
9624                  ;
9625                         score += 0.5 * (double)cob * penalty;
9626                         score += (double)amino_dis[(unsigned char)seq[i][k]][(unsigned char)seq[j][k]];
9627                         nglen += ( !gc1 * !gc2 ); /* tsukawanai */
9628                 }
9629 #if 0
9630                 reporterr(       "i = %d, j=%d\n", i+1, j+1 );
9631                 reporterr(       "score = %f\n", score );
9632 #endif
9633         }
9634         return( (double)score );
9635 }
9636
9637 double SSPscore( int s, char **seq )  /* algorithm S */
9638 {
9639         int i, j, k;
9640         int gb1, gb2, gc1, gc2;
9641         int cob;
9642         int nglen;
9643     int len = strlen( seq[0] );
9644     double score;
9645
9646         score = 0;
9647         nglen = 0;
9648         for( i=0; i<s-1; i++ ) for( j=i+1; j<s; j++ )
9649         {
9650
9651                 gc1 = 0;
9652                 gc2 = 0;
9653                 for( k=0; k<len; k++ )
9654                 {
9655                         gb1 = gc1;
9656                         gb2 = gc2;
9657
9658                         gc1 = ( seq[i][k] == '-' );
9659                         gc2 = ( seq[j][k] == '-' );
9660                         
9661             cob = 
9662                        !gb1  *  gc1
9663                          * !gb2  * !gc2
9664
9665                  +  gb1  * !gc1 
9666                  * !gb2  * !gc2
9667
9668                      + !gb1  * !gc1
9669                          * !gb2  *  gc2
9670
9671                  + !gb1  * !gc1 
9672                  *  gb2  * !gc2
9673
9674                  + !gb1  *  gc1
9675                  *  gb2  * !gc2
9676
9677                  +  gb1  * !gc1
9678                  * !gb2  *  gc2
9679       
9680 #if 0
9681                                  +  gb1  * !gc1
9682                                  *  gb2  *  gc2
9683
9684                                  +  gb1  *  gc1
9685                                  *  gb2  * !gc2
9686       
9687                                  + !gb1  *  gc1
9688                                  *  gb2  *  gc2
9689
9690                                  +  gb1  *  gc1
9691                                  * !gb2  *  gc2
9692 #endif
9693                  ;
9694                         score += 0.5 * (double)cob * penalty;
9695                         score += (double)amino_dis[(unsigned char)seq[i][k]][(unsigned char)seq[j][k]];
9696                         nglen += ( !gc1 * !gc2 ); /* tsukawanai */
9697                 }
9698 #if 0
9699                 reporterr(       "i = %d, j=%d\n", i+1, j+1 );
9700                 reporterr(       "score = %f\n", score );
9701 #endif
9702         }
9703         return( (double)score );
9704 }
9705
9706
9707
9708 double DSPscore( int s, char **seq )  /* method 3 deha nai */
9709 {
9710     int i, j, k;
9711     double c;
9712     int len = strlen( seq[0] );
9713     double score;
9714     double tmpscore;
9715     char *mseq1, *mseq2;
9716 #if DEBUG
9717         FILE *fp;
9718 #endif
9719
9720     score = 0.0;
9721     c = 0.0;
9722
9723     for( i=0; i<s-1; i++ )
9724     {
9725         for( j=i+1; j<s; j++ )
9726         {
9727             mseq1 = seq[i];
9728             mseq2 = seq[j];
9729             tmpscore = 0.0;
9730             for( k=0; k<len; k++ )
9731             {
9732                 if( mseq1[k] == '-' && mseq2[k] == '-' ) continue;
9733                 tmpscore += amino_dis[(unsigned char)mseq1[k]][(unsigned char)mseq2[k]];
9734
9735                 if( mseq1[k] == '-' )
9736                 {
9737                     tmpscore += penalty;
9738                     while( mseq1[++k] == '-' )
9739                         tmpscore += amino_dis[(unsigned char)mseq1[k]][(unsigned char)mseq2[k]];
9740                     k--;
9741                     if( k > len-2 ) break;
9742                     continue;
9743                 }
9744                 if( mseq2[k] == '-' )
9745                 {
9746                     tmpscore += penalty;
9747                     while( mseq2[++k] == '-' )
9748                         tmpscore += amino_dis[(unsigned char)mseq1[k]][(unsigned char)mseq2[k]];
9749                     k--;
9750                     if( k > len-2 ) break;
9751                     continue;
9752                 }
9753             }
9754             score += (double)tmpscore;
9755         }
9756     }
9757
9758         return( score );
9759 }
9760
9761
9762 #define SEGMENTSIZE 150
9763
9764 int searchAnchors( int nseq, char **seq, Segment *seg )
9765 {
9766         int i, j, k, kcyc;
9767         int status;
9768         double score;
9769         int value = 0;
9770         int len;
9771         int length;
9772         static double *stra = NULL;
9773         static int alloclen = 0;
9774         double cumscore;
9775         static double threshold;
9776
9777         len = strlen( seq[0] );
9778         if( alloclen < len )
9779         {
9780                 if( alloclen )
9781                 {
9782                         FreeDoubleVec( stra );
9783                 }
9784                 else
9785                 {
9786                         threshold = (int)divThreshold / 100.0 * 600.0 * divWinSize;
9787                 }
9788                 stra = AllocateDoubleVec( len );
9789                 alloclen = len;
9790         }
9791
9792         for( i=0; i<len; i++ )
9793         {
9794                 stra[i] = 0.0;
9795                 kcyc = nseq-1;
9796                 for( k=0; k<kcyc; k++ ) for( j=k+1; j<nseq; j++ )
9797                         stra[i] += n_dis[(int)amino_n[(unsigned char)seq[k][i]]][(int)amino_n[(unsigned char)seq[j][i]]];
9798                 stra[i] /= (double)nseq * ( nseq-1 ) / 2;
9799         }
9800
9801         (seg+0)->skipForeward = 0;
9802         (seg+1)->skipBackward = 0;
9803         status = 0;
9804         cumscore = 0.0;
9805         score = 0.0;
9806         length = 0; /* modified at 01/09/11 */
9807         for( j=0; j<divWinSize; j++ ) score += stra[j];
9808         for( i=1; i<len-divWinSize; i++ )
9809         {
9810                 score = score - stra[i-1] + stra[i+divWinSize-1];
9811 #if DEBUG
9812                 reporterr(       "%d %f   ? %f", i, score, threshold );
9813                 if( score > threshold ) reporterr(       "YES\n" );
9814                 else                    reporterr(       "NO\n" );
9815 #endif
9816
9817                 if( score > threshold )
9818                 {
9819                         if( !status )
9820                         {
9821                                 status = 1;
9822                                 seg->start = i;
9823                                 length = 0;
9824                                 cumscore = 0.0;
9825                         }
9826                         length++;
9827                         cumscore += score;
9828                 }
9829                 if( score <= threshold || length > SEGMENTSIZE )
9830                 {
9831                         if( status )
9832                         {
9833                                 seg->end = i;
9834                                 seg->center = ( seg->start + seg->end + divWinSize ) / 2 ;
9835                                 seg->score = cumscore;
9836 #if DEBUG
9837                                 reporterr(       "%d-%d length = %d\n", seg->start, seg->end, length );
9838 #endif
9839                                 if( length > SEGMENTSIZE )
9840                                 {
9841                                         (seg+0)->skipForeward = 1;
9842                                         (seg+1)->skipBackward = 1;
9843                                 }
9844                                 else
9845                                 {
9846                                         (seg+0)->skipForeward = 0;
9847                                         (seg+1)->skipBackward = 0;
9848                                 }
9849                                 length = 0;
9850                                 cumscore = 0.0;
9851                                 status = 0;
9852                                 value++;
9853                                 seg++;
9854                                 if( value > MAXSEG - 3 ) ErrorExit( "TOO MANY SEGMENTS!");
9855                         }
9856                 }
9857         }
9858         if( status )
9859         {
9860                 seg->end = i;
9861                 seg->center = ( seg->start + seg->end + divWinSize ) / 2 ;
9862                 seg->score = cumscore;
9863 #if DEBUG
9864 reporterr(       "%d-%d length = %d\n", seg->start, seg->end, length );
9865 #endif
9866                 value++;
9867         }
9868         return( value );
9869 }
9870
9871 void dontcalcimportance_target( int nseq, double *eff, char **seq, LocalHom **localhom, int ntarget )
9872 {
9873         int i, j;
9874         LocalHom *ptr;
9875         int *nogaplen;
9876
9877         nogaplen = AllocateIntVec( nseq );
9878
9879         for( i=0; i<nseq; i++ )
9880         {
9881                 nogaplen[i] = seqlen( seq[i] );
9882 //              reporterr(       "nogaplen[%d] = %d\n", i, nogaplen[i] );
9883         }
9884
9885         for( i=0; i<ntarget; i++ )
9886         {
9887                 for( j=0; j<nseq; j++ )
9888                 {
9889                         for( ptr=localhom[i]+j; ptr; ptr=ptr->next )
9890                         {
9891 //                              reporterr(       "i,j=%d,%d,ptr=%p\n", i, j, ptr );
9892 #if 1
9893                                 ptr->importance = ptr->opt / ptr->overlapaa;
9894 //                              ptr->fimportance = (double)ptr->importance;
9895 #else
9896                                 ptr->importance = ptr->opt / MIN( nogaplen[i], nogaplen[j] );
9897 #endif
9898                         }
9899                 }
9900         }
9901         free( nogaplen );
9902 }
9903 void dontcalcimportance( int nseq, double *eff, char **seq, LocalHom **localhom )
9904 {
9905         int i, j;
9906         LocalHom *ptr;
9907         int *nogaplen;
9908
9909         nogaplen = AllocateIntVec( nseq );
9910
9911         for( i=0; i<nseq; i++ )
9912         {
9913                 nogaplen[i] = seqlen( seq[i] );
9914 //              reporterr(       "nogaplen[%d] = %d\n", i, nogaplen[i] );
9915         }
9916
9917         for( i=0; i<nseq; i++ )
9918         {
9919                 for( j=0; j<nseq; j++ )
9920                 {
9921                         for( ptr=localhom[i]+j; ptr; ptr=ptr->next )
9922                         {
9923 //                              reporterr(       "i,j=%d,%d,ptr=%p\n", i, j, ptr );
9924 #if 1
9925                                 ptr->importance = ptr->opt / ptr->overlapaa;
9926 //                              ptr->fimportance = (double)ptr->importance;
9927 #else
9928                                 ptr->importance = ptr->opt / MIN( nogaplen[i], nogaplen[j] );
9929 #endif
9930                         }
9931                 }
9932         }
9933         free( nogaplen );
9934 }
9935
9936 void dontcalcimportance_firstone( int nseq, double *eff, char **seq, LocalHom **localhom )
9937 {
9938         int i, j, nseq1;
9939         LocalHom *ptr;
9940 #if 1
9941 #else
9942         int *nogaplen;
9943         nogaplen = AllocateIntVec( nseq );
9944         for( i=0; i<nseq; i++ )
9945         {
9946                 nogaplen[i] = seqlen( seq[i] );
9947 //              reporterr(       "nogaplen[%d] = %d\n", i, nogaplen[i] );
9948         }
9949 #endif
9950
9951         nseq1 = nseq - 1;
9952         for( i=0; i<nseq1; i++ )
9953         {
9954                 j=0;
9955                 {
9956                         for( ptr=localhom[i]+j; ptr; ptr=ptr->next )
9957                         {
9958 //                              reporterr(       "i,j=%d,%d,ptr=%p\n", i, j, ptr );
9959 #if 1
9960 //                              ptr->importance = ptr->opt / ptr->overlapaa;
9961                                 ptr->importance = ptr->opt * 0.5; // tekitou
9962 //                              ptr->fimportance = (double)ptr->importance;
9963 //                              reporterr(       "i=%d, j=%d, importance = %f, opt=%f\n", i, j, ptr->fimportance, ptr->opt  );
9964 #else
9965                                 ptr->importance = ptr->opt / MIN( nogaplen[i], nogaplen[j] );
9966 #endif
9967                         }
9968                 }
9969         }
9970 #if 1
9971 #else
9972         free( nogaplen );
9973 #endif
9974 }
9975
9976 void calcimportance_target( int nseq, int ntarget, double *eff, char **seq, LocalHom **localhom, int *targetmap, int *targetmapr )
9977 {
9978         int i, j, pos, len, ti, tj;
9979         double *importance; // static -> local, 2012/02/25
9980         double tmpdouble;
9981         double *ieff, totaleff; // counteff_simple_double ni utsusu kamo
9982         int *nogaplen; // static -> local, 2012/02/25
9983         LocalHom *tmpptr;
9984
9985         importance = AllocateDoubleVec( nlenmax );
9986         nogaplen = AllocateIntVec( nseq );
9987         ieff = AllocateDoubleVec( nseq );
9988
9989         totaleff = 0.0;
9990         for( i=0; i<nseq; i++ )
9991         {
9992                 nogaplen[i] = seqlen( seq[i] );
9993 //              reporterr(       "nogaplen[] = %d\n", nogaplen[i] );
9994                 if( nogaplen[i] == 0 ) ieff[i] = 0.0;
9995                 else ieff[i] = eff[i];
9996                 totaleff += ieff[i];
9997         }
9998         for( i=0; i<nseq; i++ ) ieff[i] /= totaleff;
9999         for( i=0; i<nseq; i++ ) printf(       "eff[%d] = %30.25f\n", i, ieff[i] );
10000
10001 #if 0
10002         for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ )
10003         {
10004                 tmpptr = localhom[i]+j;
10005                 reporterr(       "%d-%d\n", i, j );
10006                 do
10007                 {
10008                         reporterr(       "reg1=%d-%d, reg2=%d-%d, opt=%f\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->opt );
10009                 } while( tmpptr=tmpptr->next );
10010         }
10011 #endif
10012
10013
10014 //      for( i=0; i<nseq; i++ )
10015         for( ti=0; ti<ntarget; ti++ )
10016         {
10017                 i = targetmapr[ti];
10018 //              reporterr(       "i = %d\n", i );
10019                 for( pos=0; pos<nlenmax; pos++ )
10020                         importance[pos] = 0.0;
10021                 for( j=0; j<nseq; j++ )
10022                 {
10023                         if( i == j ) continue;
10024 //                      tmpptr = localhom[ti]+j;
10025                         for( tmpptr = localhom[ti]+j; tmpptr; tmpptr=tmpptr->next )
10026                         {
10027                                 if( tmpptr->opt == -1 ) continue;
10028                                 for( pos=tmpptr->start1; pos<=tmpptr->end1; pos++ )
10029                                 {
10030 #if 1
10031 //                                      if( pos == 0 ) reporterr( "hit! i=%d, j=%d, pos=%d\n", i, j, pos );
10032                                         importance[pos] += ieff[j];
10033 #else
10034                                         importance[pos] += ieff[j] * tmpptr->opt / MIN( nogaplen[i], nogaplen[j] );
10035                                         importance[pos] += ieff[j] * tmpptr->opt / tmpptr->overlapaa;
10036 #endif
10037                                 }
10038                         }
10039                 }
10040 #if 0
10041                 reporterr(       "position specific importance of seq %d:\n", i );
10042                 for( pos=0; pos<nlenmax; pos++ )
10043                         reporterr(       "%d: %f\n", pos, importance[pos] );
10044                 reporterr(       "\n" );
10045 #endif
10046                 for( j=0; j<nseq; j++ )
10047                 {
10048 //                      reporterr(       "i=%d, j=%d\n", i, j );
10049                         if( i == j ) continue;
10050                         if( localhom[ti][j].opt == -1.0 ) continue;
10051 #if 1
10052                         for( tmpptr = localhom[ti]+j; tmpptr; tmpptr=tmpptr->next )
10053                         {
10054                                 if( tmpptr->opt == -1.0 ) continue;
10055                                 tmpdouble = 0.0;
10056                                 len = 0;
10057                                 for( pos=tmpptr->start1; pos<=tmpptr->end1; pos++ )
10058                                 {
10059                                         tmpdouble += importance[pos];
10060                                         len++;
10061                                 }
10062
10063                                 tmpdouble /= (double)len;
10064
10065                                 tmpptr->importance = tmpdouble * tmpptr->opt;
10066 //                              tmpptr->fimportance = (double)tmpptr->importance;
10067                         }
10068 #else
10069                         tmpdouble = 0.0;
10070                         len = 0;
10071                         for( tmpptr = localhom[ti]+j; tmpptr; tmpptr=tmpptr->next )
10072                         {
10073                                 if( tmpptr->opt == -1.0 ) continue;
10074                                 for( pos=tmpptr->start1; pos<=tmpptr->end1; pos++ )
10075                                 {
10076                                         tmpdouble += importance[pos];
10077                                         len++;
10078                                 }
10079                         }
10080
10081                         tmpdouble /= (double)len;
10082
10083                         for( tmpptr = localhom[ti]+j; tmpptr; tmpptr=tmpptr->next )
10084                         {
10085                                 if( tmpptr->opt == -1.0 ) continue;
10086                                 tmpptr->importance = tmpdouble * tmpptr->opt;
10087 //                              tmpptr->importance = tmpptr->opt / tmpptr->overlapaa; //\e$B$J$+$C$?$3$H$K$9$k\e(B
10088                         }
10089 #endif
10090
10091 //                      reporterr(       "importance of match between %d - %d = %f\n", i, j, tmpdouble );
10092                 }
10093         }
10094
10095 #if 0
10096         printf(       "before averaging:\n" );
10097
10098         for( ti=0; ti<ntarget; ti++ ) for( j=0; j<nseq; j++ )
10099         {
10100                 i = targetmapr[ti];
10101                 if( i == j ) continue;
10102                 printf(       "%d-%d\n", i, j );
10103                 for( tmpptr = localhom[ti]+j; tmpptr; tmpptr=tmpptr->next )
10104                 {
10105                         printf(       "reg1=%d-%d, reg2=%d-%d, imp=%f -> %f opt=%30.25f\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->opt / tmpptr->overlapaa, eff[i] * tmpptr->importance, tmpptr->opt );
10106                 }
10107         }
10108 #endif
10109
10110 #if 1
10111 //      reporterr(       "average?\n" );
10112 //      for( i=0; i<nseq-1; i++ ) for( j=i+1; j<nseq; j++ )
10113         for( ti=0; ti<ntarget; ti++ ) for( tj=ti+1; tj<ntarget; tj++ )
10114         {
10115                 double imp;
10116                 LocalHom *tmpptr1, *tmpptr2;
10117
10118                 i = targetmapr[ti];
10119                 j = targetmapr[tj];
10120 //              if( i == j ) continue;
10121
10122 //              reporterr(       "i=%d, j=%d\n", i, j );
10123
10124                 tmpptr1 = localhom[ti]+j; tmpptr2 = localhom[tj]+i;
10125                 for( ; tmpptr1 && tmpptr2; tmpptr1 = tmpptr1->next, tmpptr2 = tmpptr2->next)
10126                 {
10127                         if( tmpptr1->opt == -1.0 || tmpptr2->opt == -1.0 ) 
10128                         {
10129 //                              reporterr(       "WARNING: i=%d, j=%d, tmpptr1->opt=%f, tmpptr2->opt=%f\n", i, j, tmpptr1->opt, tmpptr2->opt );
10130                                 continue;
10131                         }
10132 //                      reporterr(       "## importances = %f, %f\n", tmpptr1->importance, tmpptr2->importance );
10133                         imp = 0.5 * ( tmpptr1->importance + tmpptr2->importance );
10134                         tmpptr1->importance = tmpptr2->importance = imp;
10135 //                      tmpptr1->fimportance = tmpptr2->fimportance = (double)imp;
10136
10137 //                      reporterr(       "## importance = %f\n", tmpptr1->importance );
10138
10139                 }
10140
10141 #if 0 // commented out, 2012/02/10
10142                 if( ( tmpptr1 && !tmpptr2 ) || ( !tmpptr1 && tmpptr2 ) )
10143                 {
10144                         reporterr(       "ERROR: i=%d, j=%d\n", i, j );
10145                         exit( 1 );
10146                 }
10147 #endif
10148         }
10149
10150         for( ti=0; ti<ntarget; ti++ ) for( j=0; j<nseq; j++ )
10151         {
10152                 double imp;
10153                 LocalHom *tmpptr1;
10154
10155                 i = targetmapr[ti];
10156                 if( i == j ) continue;
10157                 if( targetmap[j] != -1 ) continue;
10158
10159 //              reporterr(       "i=%d, j=%d\n", i, j );
10160
10161                 tmpptr1 = localhom[ti]+j;
10162                 for( ; tmpptr1; tmpptr1 = tmpptr1->next )
10163                 {
10164                         if( tmpptr1->opt == -1.0  ) 
10165                         {
10166 //                              reporterr(       "WARNING: i=%d, j=%d, tmpptr1->opt=%f, tmpptr2->opt=%f\n", i, j, tmpptr1->opt, tmpptr2->opt );
10167                                 continue;
10168                         }
10169 //                      reporterr(       "## importances = %f, %f\n", tmpptr1->importance, tmpptr2->importance );
10170                         imp = 0.5 * ( tmpptr1->importance );
10171 //                      imp = 1.0 * ( tmpptr1->importance );
10172                         tmpptr1->importance = imp;
10173 //                      tmpptr1->fimportance = (double)imp;
10174
10175 //                      reporterr(       "## importance = %f\n", tmpptr1->importance );
10176
10177                 }
10178
10179 #if 0 // commented out, 2012/02/10
10180                 if( ( tmpptr1 && !tmpptr2 ) || ( !tmpptr1 && tmpptr2 ) )
10181                 {
10182                         reporterr(       "ERROR: i=%d, j=%d\n", i, j );
10183                         exit( 1 );
10184                 }
10185 #endif
10186         }
10187 #endif
10188 #if 0
10189         printf(       "after averaging:\n" );
10190
10191         for( ti=0; ti<ntarget; ti++ ) for( j=0; j<nseq; j++ )
10192         {
10193                 i = targetmapr[ti];
10194                 if( i == j ) continue;
10195                 printf(       "%d-%d\n", i, j );
10196                 for( tmpptr = localhom[ti]+j; tmpptr; tmpptr=tmpptr->next )
10197                 {
10198                         if( tmpptr->end1 )
10199                                 printf(       "%d-%d, reg1=%d-%d, reg2=%d-%d, imp=%f -> %f opt=%f\n", i, j, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->opt / tmpptr->overlapaa, tmpptr->importance, tmpptr->opt );
10200                 }
10201         }
10202 //exit( 1 );
10203 #endif
10204         free( importance );
10205         free( nogaplen );
10206         free( ieff );
10207 }
10208
10209 void calcimportance_half( int nseq, double *eff, char **seq, LocalHom **localhom )
10210 {
10211         int i, j, pos, len;
10212         double *importance; // static -> local, 2012/02/25
10213         double tmpdouble;
10214         double *ieff, totaleff; // counteff_simple_double ni utsusu kamo
10215         int *nogaplen; // static -> local, 2012/02/25
10216         LocalHom *tmpptr;
10217
10218         importance = AllocateDoubleVec( nlenmax );
10219         nogaplen = AllocateIntVec( nseq );
10220         ieff = AllocateDoubleVec( nseq );
10221
10222         totaleff = 0.0;
10223         for( i=0; i<nseq; i++ )
10224         {
10225                 nogaplen[i] = seqlen( seq[i] );
10226 //              reporterr(       "nogaplen[] = %d\n", nogaplen[i] );
10227                 if( nogaplen[i] == 0 ) ieff[i] = 0.0;
10228                 else ieff[i] = eff[i];
10229                 totaleff += ieff[i];
10230         }
10231         for( i=0; i<nseq; i++ ) ieff[i] /= totaleff;
10232 //      for( i=0; i<nseq; i++ ) reporterr(       "eff[%d] = %f\n", i, ieff[i] );
10233
10234 #if 0
10235         for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ )
10236         {
10237                 tmpptr = localhom[i]+j;
10238                 reporterr(       "%d-%d\n", i, j );
10239                 do
10240                 {
10241                         reporterr(       "reg1=%d-%d, reg2=%d-%d, opt=%f\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->opt );
10242                 } while( tmpptr=tmpptr->next );
10243         }
10244 #endif
10245
10246
10247         for( i=0; i<nseq; i++ )
10248         {
10249 //              reporterr(       "i = %d\n", i );
10250                 for( pos=0; pos<nlenmax; pos++ )
10251                         importance[pos] = 0.0;
10252                 for( j=0; j<nseq; j++ )
10253                 {
10254                         if( i == j ) continue;
10255
10256                         else if( i < j )
10257                         {
10258                                 for( tmpptr = localhom[i]+j-i; tmpptr; tmpptr=tmpptr->next )
10259                                 {
10260                                         if( tmpptr->opt == -1 ) continue;
10261                                         for( pos=tmpptr->start1; pos<=tmpptr->end1; pos++ )
10262                                         {
10263 #if 1
10264 //                                              if( pos == 0 ) reporterr( "hit! i=%d, j=%d, pos=%d\n", i, j, pos );
10265                                                 importance[pos] += ieff[j];
10266 #else
10267                                                 importance[pos] += ieff[j] * tmpptr->opt / MIN( nogaplen[i], nogaplen[j] );
10268                                                 importance[pos] += ieff[j] * tmpptr->opt / tmpptr->overlapaa;
10269 #endif
10270                                         }
10271                                 }
10272                         }
10273                         else
10274                         {
10275                                 for( tmpptr = localhom[j]+i-j; tmpptr; tmpptr=tmpptr->next )
10276                                 {
10277                                         if( tmpptr->opt == -1 ) continue;
10278                                         for( pos=tmpptr->start2; pos<=tmpptr->end2; pos++ )
10279                                         {
10280 #if 1
10281 //                                              if( pos == 0 ) reporterr( "hit! i=%d, j=%d, pos=%d\n", i, j, pos );
10282                                                 importance[pos] += ieff[j];
10283 #else
10284                                                 importance[pos] += ieff[j] * tmpptr->opt / MIN( nogaplen[i], nogaplen[j] );
10285                                                 importance[pos] += ieff[j] * tmpptr->opt / tmpptr->overlapaa;
10286 #endif
10287                                         }
10288                                 }
10289                         }
10290                 }
10291 #if 0
10292                 reporterr(       "position specific importance of seq %d:\n", i );
10293                 for( pos=0; pos<nlenmax; pos++ )
10294                         reporterr(       "%d: %f\n", pos, importance[pos] );
10295                 reporterr(       "\n" );
10296 #endif
10297                 for( j=0; j<nseq; j++ )
10298                 {
10299 //                      reporterr(       "i=%d, j=%d\n", i, j );
10300                         if( i == j ) continue;
10301
10302                         else if( i < j )
10303                         {
10304                                 if( localhom[i][j-i].opt == -1.0 ) continue;
10305         
10306                                 for( tmpptr = localhom[i]+j-i; tmpptr; tmpptr=tmpptr->next )
10307                                 {
10308                                         if( tmpptr->opt == -1.0 ) continue;
10309                                         tmpdouble = 0.0;
10310                                         len = 0;
10311                                         for( pos=tmpptr->start1; pos<=tmpptr->end1; pos++ )
10312                                         {
10313                                                 tmpdouble += importance[pos];
10314                                                 len++;
10315                                         }
10316         
10317                                         tmpdouble /= (double)len;
10318         
10319                                         tmpptr->importance = tmpdouble * tmpptr->opt;
10320 //                                      tmpptr->fimportance = (double)tmpptr->importance;
10321                                 }
10322                         }
10323                         else
10324                         {
10325                                 if( localhom[j][i-j].opt == -1.0 ) continue;
10326         
10327                                 for( tmpptr = localhom[j]+i-j; tmpptr; tmpptr=tmpptr->next )
10328                                 {
10329                                         if( tmpptr->opt == -1.0 ) continue;
10330                                         tmpdouble = 0.0;
10331                                         len = 0;
10332                                         for( pos=tmpptr->start2; pos<=tmpptr->end2; pos++ )
10333                                         {
10334                                                 tmpdouble += importance[pos];
10335                                                 len++;
10336                                         }
10337         
10338                                         tmpdouble /= (double)len;
10339         
10340                                         tmpptr->rimportance = tmpdouble * tmpptr->opt;
10341 //                                      tmpptr->fimportance = (double)tmpptr->importance;
10342                                 }
10343                         }
10344
10345 //                      reporterr(       "importance of match between %d - %d = %f\n", i, j, tmpdouble );
10346                 }
10347         }
10348
10349 #if 0
10350         printf(       "before averaging:\n" );
10351
10352         for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ )
10353         {
10354                 if( i == j ) continue;
10355
10356                 else if( i < j ) 
10357                 {
10358                         printf(       "%d-%d\n", i, j );
10359                         for( tmpptr = localhom[i]+j-i; tmpptr; tmpptr=tmpptr->next )
10360                         {
10361                                 printf(       "reg1=%d-%d, reg2=%d-%d, imp=%f -> %f opt=%f\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->opt / tmpptr->overlapaa, eff[i] * tmpptr->importance, tmpptr->opt );
10362                         }
10363                 }
10364                 else
10365                 {
10366                         printf(       "%d-%d\n", i, j );
10367                         for( tmpptr = localhom[j]+i-j; tmpptr; tmpptr=tmpptr->next )
10368                         {
10369                                 printf(       "reg1=%d-%d, reg2=%d-%d, imp=%f -> %f opt=%f\n", tmpptr->start2, tmpptr->end2, tmpptr->start1, tmpptr->end1, tmpptr->opt / tmpptr->overlapaa, eff[i] * tmpptr->rimportance, tmpptr->opt );
10370                         }
10371                 }
10372         }
10373 #endif
10374
10375 #if 1
10376 //      reporterr(       "average?\n" );
10377         for( i=0; i<nseq-1; i++ ) for( j=i+1; j<nseq; j++ )
10378         {
10379                 double imp;
10380                 LocalHom *tmpptr1;
10381
10382 //              reporterr(       "i=%d, j=%d\n", i, j );
10383
10384                 tmpptr1 = localhom[i]+j-i;
10385                 for( ; tmpptr1; tmpptr1 = tmpptr1->next)
10386                 {
10387                         if( tmpptr1->opt == -1.0 )
10388                         {
10389 //                              reporterr(       "WARNING: i=%d, j=%d, tmpptr1->opt=%f, tmpptr2->opt=%f\n", i, j, tmpptr1->opt, tmpptr2->opt );
10390                                 continue;
10391                         }
10392 //                      reporterr(       "## importances = %f, %f\n", tmpptr1->importance, tmpptr2->importance );
10393                         imp = 0.5 * ( tmpptr1->importance + tmpptr1->rimportance );
10394                         tmpptr1->importance = tmpptr1->rimportance = imp;
10395 //                      tmpptr1->fimportance = tmpptr2->fimportance = (double)imp;
10396
10397 //                      reporterr(       "## importance = %f\n", tmpptr1->importance );
10398
10399                 }
10400
10401 #if 0 // commented out, 2012/02/10
10402                 if( ( tmpptr1 && !tmpptr2 ) || ( !tmpptr1 && tmpptr2 ) )
10403                 {
10404                         reporterr(       "ERROR: i=%d, j=%d\n", i, j );
10405                         exit( 1 );
10406                 }
10407 #endif
10408         }
10409 #endif
10410 #if 0
10411         printf(       "after averaging:\n" );
10412
10413         for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ )
10414         {
10415                 if( i < j ) for( tmpptr = localhom[i]+j-i; tmpptr; tmpptr=tmpptr->next )
10416                 {
10417                         if( tmpptr->end1 && tmpptr->start1 != -1 )
10418                                 printf(       "%d-%d, reg1=%d-%d, reg2=%d-%d, imp=%f -> %f opt=%f\n", i, j, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->opt / tmpptr->overlapaa, tmpptr->importance, tmpptr->opt );
10419                 }
10420                 else for( tmpptr = localhom[j]+i-j; tmpptr; tmpptr=tmpptr->next )
10421                 {
10422                         if( tmpptr->end2 && tmpptr->start2 != -1 )
10423                                 printf(       "%d-%d, reg1=%d-%d, reg2=%d-%d, imp=%f -> %f opt=%f\n", i, j, tmpptr->start2, tmpptr->end2, tmpptr->start1, tmpptr->end1, tmpptr->opt / tmpptr->overlapaa, tmpptr->importance, tmpptr->opt );
10424                 }
10425         }
10426 exit( 1 );
10427 #endif
10428         free( importance );
10429         free( nogaplen );
10430         free( ieff );
10431 }
10432
10433 void calcimportance( int nseq, double *eff, char **seq, LocalHom **localhom )
10434 {
10435         int i, j, pos, len;
10436         double *importance; // static -> local, 2012/02/25
10437         double tmpdouble;
10438         double *ieff, totaleff; // counteff_simple_double ni utsusu kamo
10439         int *nogaplen; // static -> local, 2012/02/25
10440         LocalHom *tmpptr;
10441
10442         importance = AllocateDoubleVec( nlenmax );
10443         nogaplen = AllocateIntVec( nseq );
10444         ieff = AllocateDoubleVec( nseq );
10445
10446         totaleff = 0.0;
10447         for( i=0; i<nseq; i++ )
10448         {
10449                 nogaplen[i] = seqlen( seq[i] );
10450 //              reporterr(       "nogaplen[] = %d\n", nogaplen[i] );
10451                 if( nogaplen[i] == 0 ) ieff[i] = 0.0;
10452                 else ieff[i] = eff[i];
10453                 totaleff += ieff[i];
10454         }
10455         for( i=0; i<nseq; i++ ) ieff[i] /= totaleff;
10456 //      for( i=0; i<nseq; i++ ) reporterr(       "eff[%d] = %f\n", i, ieff[i] );
10457
10458 #if 0
10459         for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ )
10460         {
10461                 tmpptr = localhom[i]+j;
10462                 reporterr(       "%d-%d\n", i, j );
10463                 do
10464                 {
10465                         reporterr(       "reg1=%d-%d, reg2=%d-%d, opt=%f\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->opt );
10466                 } while( tmpptr=tmpptr->next );
10467         }
10468 #endif
10469
10470
10471         for( i=0; i<nseq; i++ )
10472         {
10473 //              reporterr(       "i = %d\n", i );
10474                 for( pos=0; pos<nlenmax; pos++ )
10475                         importance[pos] = 0.0;
10476                 for( j=0; j<nseq; j++ )
10477                 {
10478                         if( i == j ) continue;
10479                         tmpptr = localhom[i]+j;
10480                         for( tmpptr = localhom[i]+j; tmpptr; tmpptr=tmpptr->next )
10481                         {
10482                                 if( tmpptr->opt == -1 ) continue;
10483                                 for( pos=tmpptr->start1; pos<=tmpptr->end1; pos++ )
10484                                 {
10485 #if 1
10486 //                                      if( pos == 0 ) reporterr( "hit! i=%d, j=%d, pos=%d\n", i, j, pos );
10487                                         importance[pos] += ieff[j];
10488 #else
10489                                         importance[pos] += ieff[j] * tmpptr->opt / MIN( nogaplen[i], nogaplen[j] );
10490                                         importance[pos] += ieff[j] * tmpptr->opt / tmpptr->overlapaa;
10491 #endif
10492                                 }
10493                         }
10494                 }
10495 #if 0
10496                 reporterr(       "position specific importance of seq %d:\n", i );
10497                 for( pos=0; pos<nlenmax; pos++ )
10498                         reporterr(       "%d: %f\n", pos, importance[pos] );
10499                 reporterr(       "\n" );
10500 #endif
10501                 for( j=0; j<nseq; j++ )
10502                 {
10503 //                      reporterr(       "i=%d, j=%d\n", i, j );
10504                         if( i == j ) continue;
10505                         if( localhom[i][j].opt == -1.0 ) continue;
10506 #if 1
10507                         for( tmpptr = localhom[i]+j; tmpptr; tmpptr=tmpptr->next )
10508                         {
10509                                 if( tmpptr->opt == -1.0 ) continue;
10510                                 tmpdouble = 0.0;
10511                                 len = 0;
10512                                 for( pos=tmpptr->start1; pos<=tmpptr->end1; pos++ )
10513                                 {
10514                                         tmpdouble += importance[pos];
10515                                         len++;
10516                                 }
10517
10518                                 tmpdouble /= (double)len;
10519
10520                                 tmpptr->importance = tmpdouble * tmpptr->opt;
10521 //                              tmpptr->fimportance = (double)tmpptr->importance;
10522                         }
10523 #else
10524                         tmpdouble = 0.0;
10525                         len = 0;
10526                         for( tmpptr = localhom[i]+j; tmpptr; tmpptr=tmpptr->next )
10527                         {
10528                                 if( tmpptr->opt == -1.0 ) continue;
10529                                 for( pos=tmpptr->start1; pos<=tmpptr->end1; pos++ )
10530                                 {
10531                                         tmpdouble += importance[pos];
10532                                         len++;
10533                                 }
10534                         }
10535
10536                         tmpdouble /= (double)len;
10537
10538                         for( tmpptr = localhom[i]+j; tmpptr; tmpptr=tmpptr->next )
10539                         {
10540                                 if( tmpptr->opt == -1.0 ) continue;
10541                                 tmpptr->importance = tmpdouble * tmpptr->opt;
10542 //                              tmpptr->importance = tmpptr->opt / tmpptr->overlapaa; //\e$B$J$+$C$?$3$H$K$9$k\e(B
10543                         }
10544 #endif
10545
10546 //                      reporterr(       "importance of match between %d - %d = %f\n", i, j, tmpdouble );
10547                 }
10548         }
10549
10550 #if 0
10551         printf(       "before averaging:\n" );
10552
10553         for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ )
10554         {
10555                 if( i == j ) continue;
10556                 printf(       "%d-%d\n", i, j );
10557                 for( tmpptr = localhom[i]+j; tmpptr; tmpptr=tmpptr->next )
10558                 {
10559                         printf(       "reg1=%d-%d, reg2=%d-%d, imp=%f -> %f opt=%f\n", tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->opt / tmpptr->overlapaa, eff[i] * tmpptr->importance, tmpptr->opt );
10560                 }
10561         }
10562 #endif
10563
10564 #if 1
10565 //      reporterr(       "average?\n" );
10566         for( i=0; i<nseq-1; i++ ) for( j=i+1; j<nseq; j++ )
10567         {
10568                 double imp;
10569                 LocalHom *tmpptr1, *tmpptr2;
10570
10571 //              reporterr(       "i=%d, j=%d\n", i, j );
10572
10573                 tmpptr1 = localhom[i]+j; tmpptr2 = localhom[j]+i;
10574                 for( ; tmpptr1 && tmpptr2; tmpptr1 = tmpptr1->next, tmpptr2 = tmpptr2->next)
10575                 {
10576                         if( tmpptr1->opt == -1.0 || tmpptr2->opt == -1.0 ) 
10577                         {
10578 //                              reporterr(       "WARNING: i=%d, j=%d, tmpptr1->opt=%f, tmpptr2->opt=%f\n", i, j, tmpptr1->opt, tmpptr2->opt );
10579                                 continue;
10580                         }
10581 //                      reporterr(       "## importances = %f, %f\n", tmpptr1->importance, tmpptr2->importance );
10582                         imp = 0.5 * ( tmpptr1->importance + tmpptr2->importance );
10583                         tmpptr1->importance = tmpptr2->importance = imp;
10584 //                      tmpptr1->fimportance = tmpptr2->fimportance = (double)imp;
10585
10586 //                      reporterr(       "## importance = %f\n", tmpptr1->importance );
10587
10588                 }
10589
10590 #if 0 // commented out, 2012/02/10
10591                 if( ( tmpptr1 && !tmpptr2 ) || ( !tmpptr1 && tmpptr2 ) )
10592                 {
10593                         reporterr(       "ERROR: i=%d, j=%d\n", i, j );
10594                         exit( 1 );
10595                 }
10596 #endif
10597         }
10598 #endif
10599 #if 0
10600         printf(       "after averaging:\n" );
10601
10602         for( i=0; i<nseq; i++ ) for( j=0; j<nseq; j++ )
10603         {
10604                 for( tmpptr = localhom[i]+j; tmpptr; tmpptr=tmpptr->next )
10605                 {
10606                         if( tmpptr->end1 && tmpptr->start1 != -1 )
10607                                 printf(       "%d-%d, reg1=%d-%d, reg2=%d-%d, imp=%f -> %f opt=%f\n", i, j, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->opt / tmpptr->overlapaa, tmpptr->importance, tmpptr->opt );
10608                 }
10609         }
10610 exit( 1 );
10611 #endif
10612         free( importance );
10613         free( nogaplen );
10614         free( ieff );
10615 }
10616
10617
10618
10619
10620 static void addlocalhom2_e( LocalHom *pt, LocalHom *lh, int sti, int stj, int eni, int enj, double opt, int overlp, int interm )
10621 {
10622 // dokka machigatteru
10623         if( pt != lh ) // susumeru
10624         {
10625                 pt->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
10626                 pt = pt->next;
10627                 pt->next = NULL;
10628                 lh->last = pt;
10629         }
10630         else // sonomamatsukau
10631         {
10632                 lh->last = pt;
10633         }
10634         lh->nokori++;
10635 //      reporterr(       "in addlocalhom2_e, pt = %p, pt->next = %p, interm=%d, sti-eni-stj-enj=%d %d %d %d\n", pt, pt->next, interm, sti, eni, stj, enj );
10636
10637         pt->start1 = sti;
10638         pt->start2 = stj;
10639         pt->end1 = eni;
10640         pt->end2 = enj;
10641         pt->opt = opt;
10642         pt->extended = interm;
10643         pt->overlapaa = overlp;
10644 #if 0
10645         reporterr(       "i: %d-%d\n", sti, eni );
10646         reporterr(       "j: %d-%d\n", stj, enj );
10647         reporterr(       "opt=%f\n", opt );
10648         reporterr(       "overlp=%d\n", overlp );
10649 #endif
10650 }
10651
10652 void extendlocalhom2( int nseq, LocalHom **localhom, double **dist )
10653 {
10654         int overlp, plim;
10655         int i, j, k;
10656         int pi, pj, pk, len;
10657         int status, sti, stj;
10658         int *ipt;
10659         int co;
10660         static int *ini = NULL;
10661         static int *inj = NULL;
10662         LocalHom *pt;
10663
10664         sti = 0; // by D.Mathog, a guess
10665         stj = 0; // by D.Mathog, a guess
10666
10667         if( ini == NULL )
10668         {
10669                 ini = AllocateIntVec( nlenmax+1 );
10670                 inj = AllocateIntVec( nlenmax+1 );
10671         }
10672
10673
10674         for( i=0; i<nseq-1; i++ )
10675         {
10676                 for( j=i+1; j<nseq; j++ )
10677                 {
10678 #if 0
10679                         for( k=0; k<nseq; k++ ) sai[k] = 0;
10680                         numint = ncons;
10681                         while( 1 )
10682                         {
10683                                 k = (int)( rnd() * nseq );
10684                                 if( k == i || k == j ) continue; // mou yatta nomo habuita hoga ii 
10685                                 if( numint-- == 0 ) break;
10686                                 if( sai[k] ) continue;
10687                                 sai[k] = 1;
10688 #else
10689                         for( k=0; k<nseq; k++ )
10690                         {
10691 #endif
10692 //                              reporterr(       "i=%d, j=%d, k=%d, dists = %f,%f,%f thrinter=%f\n", i, j, k, dist[i][j], dist[MIN(i,k)][MAX(i,k)], dist[MIN(j,k)][MAX(j,k)], thrinter );
10693                                 if( k == i || k == j ) continue; // mou yatta nomo habuita hoga ii 
10694                                 if( dist[MIN(i,k)][MAX(i,k)] > dist[i][j] * thrinter || dist[MIN(j,k)][MAX(j,k)] > dist[i][j] * thrinter ) continue;
10695                                 ipt = ini; co = nlenmax+1;
10696                                 while( co-- ) *ipt++ = -1;
10697                                 ipt = inj; co = nlenmax+1;
10698                                 while( co-- ) *ipt++ = -1;
10699                                 overlp = 0;
10700
10701                                 {
10702                                         for( pt=localhom[i]+k; pt; pt=pt->next )
10703                                 {
10704 //                                              reporterr(       "i=%d,k=%d,st1:st2=%d:%d,pt=%p,extended=%p\n", i, k, pt->start1, pt->start2, pt, pt->extended );
10705                                                 if( pt->opt == -1 )
10706                                                 {
10707                                                         reporterr(       "opt kainaide tbfast.c = %f\n", pt->opt );
10708                                                 }
10709                                                 if( pt->extended > -1 ) break;
10710                                                 pi = pt->start1;
10711                                                 pk = pt->start2;
10712                                                 len = pt->end1 - pt->start1 + 1;
10713                                                 ipt = ini + pk;
10714                                                 while( len-- ) *ipt++ = pi++;
10715                                         }
10716                                 }
10717
10718                                 {
10719                                         for( pt=localhom[j]+k; pt; pt=pt->next )
10720                                 {
10721                                                 if( pt->opt == -1 )
10722                                                 {
10723                                                         reporterr(       "opt kainaide tbfast.c = %f\n", pt->opt );
10724                                                 }
10725                                                 if( pt->extended > -1 ) break;
10726                                                 pj = pt->start1;
10727                                                 pk = pt->start2;
10728                                                 len = pt->end1 - pt->start1 + 1;
10729                                                 ipt = inj + pk;
10730                                                 while( len-- ) *ipt++ = pj++;
10731                                         }
10732                                 }
10733 #if 0
10734                                 reporterr(       "i=%d,j=%d,k=%d\n", i, j, k );
10735                                 overlp = 0;
10736                                 for( pk = 0; pk < nlenmax; pk++ )
10737                                 {
10738                                         if( ini[pk] != -1 && inj[pk] != -1 ) overlp++;
10739                                         reporterr(       " %d", inj[pk] );
10740                                 }
10741                                 reporterr(       "\n" );
10742
10743                                 reporterr(       "i=%d,j=%d,k=%d\n", i, j, k );
10744                                 overlp = 0;
10745                                 for( pk = 0; pk < nlenmax; pk++ )
10746                                 {
10747                                         if( ini[pk] != -1 && inj[pk] != -1 ) overlp++;
10748                                         reporterr(       " %d", ini[pk] );
10749                                 }
10750                                 reporterr(       "\n" );
10751 #endif
10752                                 overlp = 0;
10753                                 plim = nlenmax+1;
10754                                 for( pk = 0; pk < plim; pk++ )
10755                                         if( ini[pk] != -1 && inj[pk] != -1 ) overlp++;
10756
10757
10758                                 status = 0;
10759                                 plim = nlenmax+1;
10760                                 for( pk=0; pk<plim; pk++ )
10761                                 {
10762 //                                      reporterr(       "%d %d: %d-%d\n", i, j, ini[pk], inj[pk] );
10763                                         if( status )
10764                                         {
10765                                                 if( ini[pk] == -1 || inj[pk] == -1 || ini[pk-1] != ini[pk] - 1 || inj[pk-1] != inj[pk] - 1 ) // saigonoshori
10766                                                 {
10767                                                         status = 0;
10768 //                                                      reporterr(       "end here!\n" );
10769
10770                                                         pt = localhom[i][j].last;
10771 //                                                      reporterr(       "in ex (ba), pt = %p, nokori=%d, i,j,k=%d,%d,%d\n", pt, localhom[i][j].nokori, i, j, k );
10772                                                         addlocalhom2_e( pt, localhom[i]+j, sti, stj, ini[pk-1], inj[pk-1], MIN( localhom[i][k].opt, localhom[j][k].opt ) * 1.0, overlp, k );
10773 //                                                      reporterr(       "in ex, pt = %p, pt->next = %p, pt->next->next = %p\n", pt, pt->next, pt->next->next );
10774
10775                                                         pt = localhom[j][i].last;
10776 //                                                      reporterr(       "in ex (ba), pt = %p, pt->next = %p\n", pt, pt->next );
10777 //                                                      reporterr(       "in ex (ba), pt = %p, pt->next = %p, k=%d\n", pt, pt->next, k );
10778                                                         addlocalhom2_e( pt, localhom[j]+i, stj, sti, inj[pk-1], ini[pk-1], MIN( localhom[i][k].opt, localhom[j][k].opt ) * 1.0, overlp, k );
10779 //                                                      reporterr(       "in ex, pt = %p, pt->next = %p, pt->next->next = %p\n", pt, pt->next, pt->next->next );
10780                                                 }
10781                                         }
10782                                         if( !status ) // else deha arimasenn.
10783                                         {
10784                                                 if( ini[pk] == -1 || inj[pk] == -1 ) continue;
10785                                                 sti = ini[pk];
10786                                                 stj = inj[pk];
10787 //                                              reporterr(       "start here!\n" );
10788                                                 status = 1;
10789                                         }
10790                                 }
10791 //                              if( status ) reporterr(       "end here\n" );
10792
10793 //                              exit( 1 );
10794 //                                      fprintf( hat3p, "%d %d %d %6.3f %d %d %d %d %p\n", i, j, tmpptr->overlapaa, tmpptr->opt, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->next ); 
10795                         }
10796 #if 0
10797                         for( pt=localhomtable[i]+j; pt; pt=pt->next )
10798                 {
10799                     if( tmpptr->opt == -1.0 ) continue;
10800                                 fprintf( hat3p, "%d %d %d %6.3f %d %d %d %d %p\n", i, j, tmpptr->overlapaa, tmpptr->opt, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, tmpptr->next ); 
10801                 }
10802 #endif
10803                 }
10804         }
10805 }
10806
10807 int makelocal( char *s1, char *s2, int thr )
10808 {
10809         int start, maxstart, maxend;
10810         char *pt1, *pt2;
10811         double score;
10812         double maxscore;
10813
10814         pt1 = s1;
10815         pt2 = s2;
10816
10817         maxend = 0; // by D.Mathog, a guess
10818
10819 //      reporterr(       "thr = %d, \ns1 = %s\ns2 = %s\n", thr, s1, s2 );
10820         maxscore = 0.0;
10821         score = 0.0;
10822         start = 0;
10823         maxstart = 0;
10824         while( *pt1 )
10825         {
10826 //              reporterr(       "*pt1 = %c*pt2 = %c\n", *pt1, *pt2 );
10827                 if( *pt1 == '-' || *pt2 == '-' )
10828                 {
10829 //                      reporterr(       "penalty = %d\n", penalty );
10830                         score += penalty;
10831                         while( *pt1 == '-' || *pt2 == '-' )
10832                         {
10833                                 pt1++; pt2++;
10834                         }
10835                         continue;
10836                 }
10837
10838                 score += ( amino_dis[(unsigned char)*pt1++][(unsigned char)*pt2++] - thr );
10839 //              score += ( amino_dis[(int)*pt1++][(int)*pt2++] );
10840                 if( score > maxscore ) 
10841                 {
10842 //                      reporterr(       "score = %f\n", score );
10843                         maxscore = score;
10844                         maxstart = start;
10845 //                      reporterr(       "## max! maxstart = %d, start = %d\n", maxstart, start );
10846                 }
10847                 if( score < 0.0 )
10848                 {
10849 //                      reporterr(       "## resetting, start = %d, maxstart = %d\n", start, maxstart );
10850                         if( start == maxstart )
10851                         {
10852                                 maxend = pt1 - s1;
10853 //                              reporterr(       "maxend = %d\n", maxend );
10854                         }
10855                         score = 0.0;
10856                         start = pt1 - s1;
10857                 }
10858         }
10859         if( start == maxstart )
10860                 maxend = pt1 - s1 - 1;
10861
10862 //      reporterr(       "maxstart = %d, maxend = %d, maxscore = %f\n", maxstart, maxend, maxscore );
10863         s1[maxend+1] = 0;
10864         s2[maxend+1] = 0;
10865         return( maxstart );
10866 }
10867
10868 void resetlocalhom( int nseq, LocalHom **lh )
10869 {
10870         int i, j;
10871         LocalHom *pt;
10872
10873         for( i=0; i<nseq-1; i++ ) for( j=i+1; j<nseq; j++ )
10874         {
10875                 for( pt=lh[i]+j; pt; pt=pt->next )
10876                         pt->opt = 1.0;
10877         }
10878
10879 }
10880
10881 void gapireru( char *res, char *ori, char *gt )
10882 {
10883         char g;
10884         while( (g = *gt++) )
10885         {
10886                 if( g == '-' )
10887                 {
10888                         *res++ = *newgapstr;
10889                 }
10890                 else
10891                 {
10892                         *res++ = *ori++;
10893                 }
10894         }
10895         *res = 0;
10896 }
10897
10898 void getkyokaigap( char *g, char **s, int pos, int n )
10899 {
10900 //      char *bk = g;
10901 //      while( n-- ) *g++ = '-';
10902         while( n-- ) *g++ = (*s++)[pos];
10903
10904 //      reporterr(       "bk = %s\n", bk );
10905 }
10906
10907 void new_OpeningGapCount( double *ogcp, int clus, char **seq, double *eff, int len, char *sgappat )
10908 #if 0
10909 {
10910         int i, j, gc, gb; 
10911         double feff;
10912
10913         
10914         for( i=0; i<len+1; i++ ) ogcp[i] = 0.0;
10915         for( j=0; j<clus; j++ ) 
10916         {
10917                 feff = (double)eff[j];
10918                 gc = ( sgappat[j] == '-' );
10919                 for( i=0; i<len; i++ ) 
10920                 {
10921                         gb = gc;
10922                         gc = ( seq[j][i] == '-' );
10923                         if( !gb *  gc ) ogcp[i] += feff;
10924                 }
10925         }
10926 }
10927 #else
10928 {
10929         int i, j, gc, gb; 
10930         double feff;
10931         double *fpt;
10932         char *spt;
10933         
10934         fpt = ogcp;
10935         i = len;
10936         while( i-- ) *fpt++ = 0.0;
10937         for( j=0; j<clus; j++ ) 
10938         {
10939                 feff = (double)eff[j];
10940                 spt = seq[j];
10941                 fpt = ogcp;
10942                 gc = ( sgappat[j] == '-' );
10943                 i = len;
10944                 while( i-- )
10945                 {
10946                         gb = gc;
10947                         gc = ( *spt++ == '-' );
10948                         {
10949                                 if( !gb *  gc ) *fpt += feff;
10950                                 fpt++;
10951                         }
10952                 }
10953         }
10954 }
10955 #endif
10956 void new_OpeningGapCount_zure( double *ogcp, int clus, char **seq, double *eff, int len, char *sgappat, char *egappat )
10957 #if 0
10958 {
10959         int i, j, gc, gb; 
10960         double feff;
10961
10962         
10963         for( i=0; i<len+1; i++ ) ogcp[i] = 0.0;
10964         for( j=0; j<clus; j++ ) 
10965         {
10966                 feff = (double)eff[j];
10967                 gc = ( sgappat[j] == '-' );
10968                 for( i=0; i<len; i++ ) 
10969                 {
10970                         gb = gc;
10971                         gc = ( seq[j][i] == '-' );
10972                         if( !gb *  gc ) ogcp[i] += feff;
10973                 }
10974                 {
10975                         gb = gc;
10976                         gc = ( egappat[j] == '-' );
10977                         if( !gb *  gc ) ogcp[i] += feff;
10978                 }
10979         }
10980 }
10981 #else
10982 {
10983         int i, j, gc, gb; 
10984         double feff;
10985         double *fpt;
10986         char *spt;
10987         
10988         fpt = ogcp;
10989         i = len+2;
10990         while( i-- ) *fpt++ = 0.0;
10991         for( j=0; j<clus; j++ ) 
10992         {
10993                 feff = (double)eff[j];
10994                 spt = seq[j];
10995                 fpt = ogcp;
10996                 gc = ( sgappat[j] == '-' );
10997                 i = len;
10998                 while( i-- )
10999                 {
11000                         gb = gc;
11001                         gc = ( *spt++ == '-' );
11002                         {
11003                                 if( !gb *  gc ) *fpt += feff;
11004                                 fpt++;
11005                         }
11006                 }
11007                 {
11008                         gb = gc;
11009                         gc = ( egappat[j] == '-' );
11010                         if( !gb *  gc ) *fpt += feff;
11011                 }
11012         }
11013 }
11014 #endif
11015
11016 void new_FinalGapCount_zure( double *fgcp, int clus, char **seq, double *eff, int len, char *sgappat, char *egappat )
11017 #if 0
11018 {
11019         int i, j, gc, gb; 
11020         double feff;
11021         
11022         for( i=0; i<len+1; i++ ) fgcp[i] = 0.0;
11023         for( j=0; j<clus; j++ ) 
11024         {
11025                 feff = (double)eff[j];
11026                 gc = ( sgappat[j] == '-' );
11027                 for( i=0; i<len; i++ ) 
11028                 {
11029                         gb = gc;
11030                         gc = ( seq[j][i] == '-' );
11031                         {
11032                                 if( gb * !gc ) fgcp[i] += feff;
11033                         }
11034                 }
11035                 {
11036                         gb = gc;
11037                         gc = ( egappat[j] == '-' );
11038                         {
11039                                 if( gb * !gc ) fgcp[len] += feff;
11040                         }
11041                 }
11042         }
11043 }
11044 #else
11045 {
11046         int i, j, gc, gb; 
11047         double feff;
11048         double *fpt;
11049         char *spt;
11050         
11051         fpt = fgcp;
11052         i = len+2;
11053         while( i-- ) *fpt++ = 0.0;
11054         for( j=0; j<clus; j++ ) 
11055         {
11056                 feff = (double)eff[j];
11057                 fpt = fgcp;
11058                 spt = seq[j];
11059                 gc = ( sgappat[j] == '-' );
11060                 i = len;
11061                 while( i-- )
11062                 {
11063                         gb = gc;
11064                         gc = ( *spt++ == '-' );
11065                         {
11066                                 if( gb * !gc ) *fpt += feff;
11067                                 fpt++;
11068                         }
11069                 }
11070                 {
11071                         gb = gc;
11072                         gc = ( egappat[j] == '-' );
11073                         {
11074                                 if( gb * !gc ) *fpt += feff;
11075                         }
11076                 }
11077         }
11078 }
11079 #endif
11080 void new_FinalGapCount( double *fgcp, int clus, char **seq, double *eff, int len, char *egappat )
11081 #if 0
11082 {
11083         int i, j, gc, gb; 
11084         double feff;
11085         
11086         for( i=0; i<len; i++ ) fgcp[i] = 0.0;
11087         for( j=0; j<clus; j++ ) 
11088         {
11089                 feff = (double)eff[j];
11090                 gc = ( seq[j][0] == '-' );
11091                 for( i=1; i<len; i++ ) 
11092                 {
11093                         gb = gc;
11094                         gc = ( seq[j][i] == '-' );
11095                         {
11096                                 if( gb * !gc ) fgcp[i-1] += feff;
11097                         }
11098                 }
11099                 {
11100                         gb = gc;
11101                         gc = ( egappat[j] == '-' );
11102                         {
11103                                 if( gb * !gc ) fgcp[len-1] += feff;
11104                         }
11105                 }
11106         }
11107 }
11108 #else
11109 {
11110         int i, j, gc, gb; 
11111         double feff;
11112         double *fpt;
11113         char *spt;
11114         
11115         fpt = fgcp;
11116         i = len;
11117         while( i-- ) *fpt++ = 0.0;
11118         for( j=0; j<clus; j++ ) 
11119         {
11120                 feff = (double)eff[j];
11121                 fpt = fgcp;
11122                 spt = seq[j];
11123                 gc = ( *spt == '-' );
11124                 i = len;
11125                 while( i-- )
11126                 {
11127                         gb = gc;
11128                         gc = ( *++spt == '-' );
11129                         {
11130                                 if( gb * !gc ) *fpt += feff;
11131                                 fpt++;
11132                         }
11133                 }
11134                 {
11135                         gb = gc;
11136                         gc = ( egappat[j] == '-' );
11137                         {
11138                                 if( gb * !gc ) *fpt += feff;
11139                         }
11140                 }
11141         }
11142 }
11143 #endif
11144
11145 void st_OpeningGapAdd( double *ogcp, int clus, char **seq, double *eff, int len )
11146 {
11147         int i, j, gc, gb; 
11148         double *fpt;
11149         char *spt;
11150         int newmem = clus-1;
11151         double neweff = eff[newmem];
11152         double orieff = 1.0 - neweff;
11153         double feff;
11154
11155 //      fpt = ogcp;
11156 //      i = len;
11157 //      while( i-- ) *fpt++ = 0.0;
11158         
11159         j = clus-1;
11160 //      for( j=0; j<clus; j++ ) 
11161         {
11162                 feff = (double)eff[j];
11163                 spt = seq[j];
11164                 fpt = ogcp;
11165                 i = len;
11166                 gc = 0;
11167                 while( i-- )
11168                 {
11169                         gb = gc;
11170                         gc = ( *spt++ == '-' );
11171                         *fpt *= orieff;
11172                         if( !gb *  gc ) *fpt += feff;
11173                         fpt++;
11174                 }
11175         }
11176         ogcp[len] = 0.0;
11177
11178 #if 0
11179         for( i=0; i<len; i++ )
11180                 reporterr( "ogcp[%d]=%f\n", i, ogcp[i] );
11181         for( i=0; i<clus; i++ )
11182                 reporterr( "%s\n", seq[i] );
11183         exit( 1 );
11184 #endif
11185 }
11186
11187 void st_OpeningGapCount( double *ogcp, int clus, char **seq, double *eff, int len )
11188 {
11189         int i, j, gc, gb; 
11190         double feff;
11191         double *fpt;
11192         char *spt;
11193         
11194         fpt = ogcp;
11195         i = len;
11196         while( i-- ) *fpt++ = 0.0;
11197         for( j=0; j<clus; j++ ) 
11198         {
11199                 feff = (double)eff[j];
11200                 spt = seq[j];
11201                 fpt = ogcp;
11202                 gc = 0;
11203 //              gc = 1;
11204                 i = len;
11205                 while( i-- )
11206                 {
11207                         gb = gc;
11208                         gc = ( *spt++ == '-' );
11209                         {
11210                                 if( !gb *  gc ) *fpt += feff;
11211                                 fpt++;
11212                         }
11213                 }
11214         }
11215         ogcp[len] = 0.0;
11216 }
11217
11218 void st_FinalGapCount_zure( double *fgcp, int clus, char **seq, double *eff, int len )
11219 {
11220         int i, j, gc, gb; 
11221         double feff;
11222         double *fpt;
11223         char *spt;
11224         
11225         fpt = fgcp;
11226         i = len+1;
11227         while( i-- ) *fpt++ = 0.0;
11228         for( j=0; j<clus; j++ ) 
11229         {
11230                 feff = (double)eff[j];
11231                 fpt = fgcp+1;
11232                 spt = seq[j];
11233                 gc = ( *spt == '-' );
11234                 i = len;
11235 //              for( i=1; i<len; i++ ) 
11236                 while( i-- )
11237                 {
11238                         gb = gc;
11239                         gc = ( *++spt == '-' );
11240                         {
11241                                 if( gb * !gc ) *fpt += feff;
11242                                 fpt++;
11243                         }
11244                 }
11245                 {
11246                         gb = gc;
11247                         gc = 0;
11248 //                      gc = 1;
11249                         {
11250                                 if( gb * !gc ) *fpt += feff;
11251                         }
11252                 }
11253         }
11254 }
11255
11256 void st_FinalGapAdd( double *fgcp, int clus, char **seq, double *eff, int len )
11257 {
11258         int i, j, gc, gb; 
11259         double *fpt;
11260         char *spt;
11261         int newmem = clus-1;
11262         double neweff = eff[newmem];
11263         double orieff = 1.0 - neweff;
11264         double feff;
11265         
11266 //      fpt = fgcp;
11267 //      i = len;
11268 //      while( i-- ) *fpt++ = 0.0;
11269
11270         j = clus-1;
11271 //      for( j=0; j<clus; j++ ) 
11272         {
11273                 feff = (double)eff[j];
11274                 fpt = fgcp;
11275                 spt = seq[j];
11276                 gc = ( *spt == '-' );
11277                 i = len;
11278 //              for( i=1; i<len; i++ ) 
11279                 while( i-- )
11280                 {
11281                         *fpt *= orieff;
11282                         gb = gc;
11283                         gc = ( *++spt == '-' );
11284                         {
11285                                 if( gb * !gc ) *fpt += feff;
11286                                 fpt++;
11287                         }
11288                 }
11289                 {
11290                         *fpt *= orieff;
11291                         gb = gc;
11292                         gc = 0;
11293 //                      gc = 1;
11294                         {
11295                                 if( gb * !gc ) *fpt += feff;
11296                         }
11297                 }
11298         }
11299 }
11300
11301 void st_FinalGapCount( double *fgcp, int clus, char **seq, double *eff, int len )
11302 {
11303         int i, j, gc, gb; 
11304         double feff;
11305         double *fpt;
11306         char *spt;
11307         
11308         fpt = fgcp;
11309         i = len;
11310         while( i-- ) *fpt++ = 0.0;
11311         for( j=0; j<clus; j++ ) 
11312         {
11313                 feff = (double)eff[j];
11314                 fpt = fgcp;
11315                 spt = seq[j];
11316                 gc = ( *spt == '-' );
11317                 i = len;
11318 //              for( i=1; i<len; i++ ) 
11319                 while( i-- )
11320                 {
11321                         gb = gc;
11322                         gc = ( *++spt == '-' );
11323                         {
11324                                 if( gb * !gc ) *fpt += feff;
11325                                 fpt++;
11326                         }
11327                 }
11328                 {
11329                         gb = gc;
11330                         gc = 0;
11331 //                      gc = 1;
11332                         {
11333                                 if( gb * !gc ) *fpt += feff;
11334                         }
11335                 }
11336         }
11337 }
11338
11339 void getGapPattern( double *fgcp, int clus, char **seq, double *eff, int len, char *xxx )
11340 {
11341         int i, j, gc, gb; 
11342         double feff;
11343         double *fpt;
11344         char *spt;
11345         
11346         fpt = fgcp;
11347         i = len+1;
11348         while( i-- ) *fpt++ = 0.0;
11349         for( j=0; j<clus; j++ ) 
11350         {
11351                 feff = (double)eff[j];
11352                 fpt = fgcp;
11353                 spt = seq[j];
11354                 gc = ( *spt == '-' );
11355                 i = len+1;
11356                 while( i-- )
11357                 {
11358                         gb = gc;
11359                         gc = ( *++spt == '-' );
11360                         {
11361                                 if( gb * !gc ) *fpt += feff;
11362                                 fpt++;
11363                         }
11364                 }
11365 #if 0
11366                 {
11367                         gb = gc;
11368                         gc = ( egappat[j] == '-' );
11369                         {
11370                                 if( gb * !gc ) *fpt += feff;
11371                         }
11372                 }
11373 #endif
11374         }
11375         for( j=0; j<len; j++ )
11376         {
11377                 reporterr(       "%d, %f\n", j, fgcp[j] );
11378         }
11379 }
11380
11381 void getdigapfreq_st( double *freq, int clus, char **seq, double *eff, int len )
11382 {
11383         int i, j;
11384         double feff;
11385         for( i=0; i<len+1; i++ ) freq[i] = 0.0;
11386         for( i=0; i<clus; i++ )
11387         {
11388                 feff = eff[i];
11389                 if( 0 && seq[i][0] == '-' ) // machigai kamo
11390                         freq[0] += feff;
11391                 for( j=1; j<len; j++ ) 
11392                 {
11393                         if( seq[i][j] == '-' && seq[i][j-1] == '-' )
11394                                 freq[j] += feff;
11395                 }
11396                 if( 0 && seq[i][len-1] == '-' )
11397                         freq[len] += feff;
11398         }
11399 //      reporterr(       "\ndigapf = \n" );
11400 //      for( i=0; i<len+1; i++ ) reporterr(       "%5.3f ", freq[i] );
11401 }
11402
11403 void getdiaminofreq_x( double *freq, int clus, char **seq, double *eff, int len )
11404 {
11405         int i, j;
11406         double feff;
11407         for( i=0; i<len+2; i++ ) freq[i] = 0.0;
11408         for( i=0; i<clus; i++ )
11409         {
11410                 feff = eff[i];
11411                 if( seq[i][0] != '-' ) // tadashii
11412                         freq[0] += feff;
11413                 for( j=1; j<len; j++ ) 
11414                 {
11415                         if( seq[i][j] != '-' && seq[i][j-1] != '-' )
11416                                 freq[j] += feff;
11417                 }
11418                 if( 1 && seq[i][len-1] != '-' ) // xxx wo tsukawanaitoki [len-1] nomi
11419                         freq[len] += feff;
11420         }
11421 //      reporterr(       "\ndiaaf = \n" );
11422 //      for( i=0; i<len+1; i++ ) reporterr(       "%5.3f ", freq[i] );
11423 }
11424
11425 void getdiaminofreq_st( double *freq, int clus, char **seq, double *eff, int len )
11426 {
11427         int i, j;
11428         double feff;
11429         for( i=0; i<len+1; i++ ) freq[i] = 0.0;
11430         for( i=0; i<clus; i++ )
11431         {
11432                 feff = eff[i];
11433                 if( seq[i][0] != '-' )
11434                         freq[0] += feff;
11435                 for( j=1; j<len; j++ ) 
11436                 {
11437                         if( seq[i][j] != '-' && seq[i][j-1] != '-' )
11438                                 freq[j] += feff;
11439                 }
11440 //              if( 1 && seq[i][len-1] != '-' ) // xxx wo tsukawanaitoki [len-1] nomi
11441                         freq[len] += feff;
11442         }
11443 //      reporterr(       "\ndiaaf = \n" );
11444 //      for( i=0; i<len+1; i++ ) reporterr(       "%5.3f ", freq[i] );
11445 }
11446
11447 void getdigapfreq_part( double *freq, int clus, char **seq, double *eff, int len, char *sgappat, char *egappat )
11448 {
11449         int i, j;
11450         double feff;
11451         for( i=0; i<len+2; i++ ) freq[i] = 0.0;
11452         for( i=0; i<clus; i++ )
11453         {
11454                 feff = eff[i];
11455 //              if( seq[i][0] == '-' )
11456                 if( seq[i][0] == '-' && sgappat[i] == '-' )
11457                         freq[0] += feff;
11458                 for( j=1; j<len; j++ ) 
11459                 {
11460                         if( seq[i][j] == '-' && seq[i][j-1] == '-' )
11461                                 freq[j] += feff;
11462                 }
11463 //              if( seq[i][len] == '-' && seq[i][len-1] == '-' ) // xxx wo tsukawanaitoki arienai
11464                 if( egappat[i] == '-' && seq[i][len-1] == '-' )
11465                         freq[len] += feff;
11466         }
11467 //      reporterr(       "\ndigapf = \n" );
11468 //      for( i=0; i<len+1; i++ ) reporterr(       "%5.3f ", freq[i] );
11469 }
11470
11471 void getdiaminofreq_part( double *freq, int clus, char **seq, double *eff, int len, char *sgappat, char *egappat )
11472 {
11473         int i, j;
11474         double feff;
11475         for( i=0; i<len+2; i++ ) freq[i] = 0.0;
11476         for( i=0; i<clus; i++ )
11477         {
11478                 feff = eff[i];
11479                 if( seq[i][0] != '-' && sgappat[i] != '-' )
11480                         freq[0] += feff;
11481                 for( j=1; j<len; j++ ) 
11482                 {
11483                         if( seq[i][j] != '-' && seq[i][j-1] != '-' )
11484                                 freq[j] += feff;
11485                 }
11486 //              if( 1 && seq[i][len-1] != '-' ) // xxx wo tsukawanaitoki [len-1] nomi
11487                 if( egappat[i] != '-'  && seq[i][len-1] != '-' ) // xxx wo tsukawanaitoki [len-1] nomi
11488                         freq[len] += feff;
11489         }
11490 //      reporterr(       "\ndiaaf = \n" );
11491 //      for( i=0; i<len+1; i++ ) reporterr(       "%5.3f ", freq[i] );
11492 }
11493
11494 void getgapfreq_zure_part( double *freq, int clus, char **seq, double *eff, int len, char *sgap )
11495 {
11496         int i, j;
11497         double feff;
11498         for( i=0; i<len+2; i++ ) freq[i] = 0.0;
11499         for( i=0; i<clus; i++ )
11500         {
11501                 feff = eff[i];
11502                 if( sgap[i] == '-' )
11503                         freq[0] += feff;
11504                 for( j=0; j<len; j++ ) 
11505                 {
11506                         if( seq[i][j] == '-' )
11507                                 freq[j+1] += feff;
11508                 }
11509 //              if( egap[i] == '-' )
11510 //                      freq[len+1] += feff;
11511         }
11512 //      reporterr(       "\ngapf = \n" );
11513 //      for( i=0; i<len+1; i++ ) reporterr(       "%5.3f ", freq[i] );
11514 }
11515
11516 void getgapfreq_zure( double *freq, int clus, char **seq, double *eff, int len )
11517 {
11518         int i, j;
11519         double feff;
11520         for( i=0; i<len+1; i++ ) freq[i] = 0.0;
11521         for( i=0; i<clus; i++ )
11522         {
11523                 feff = eff[i];
11524                 for( j=0; j<len; j++ ) 
11525                 {
11526                         if( seq[i][j] == '-' )
11527                                 freq[j+1] += feff;
11528                 }
11529         }
11530         freq[len+1] = 0.0;
11531 //      reporterr(       "\ngapf = \n" );
11532 //      for( i=0; i<len+1; i++ ) reporterr(       "%5.3f ", freq[i] );
11533 }
11534
11535 void getgapfreq( double *freq, int clus, char **seq, double *eff, int len )
11536 {
11537         int i, j;
11538         double feff;
11539         for( i=0; i<len+1; i++ ) freq[i] = 0.0;
11540         for( i=0; i<clus; i++ )
11541         {
11542                 feff = eff[i];
11543                 for( j=0; j<len; j++ ) 
11544                 {
11545                         if( seq[i][j] == '-' )
11546                                 freq[j] += feff;
11547                 }
11548         }
11549         freq[len] = 0.0;
11550 //      reporterr(       "\ngapf = \n" );
11551 //      for( i=0; i<len+1; i++ ) reporterr(       "%5.3f ", freq[i] );
11552 }
11553
11554 void st_getGapPattern( Gappat **pat, int clus, char **seq, double *eff, int len )
11555 {
11556         int i, j, k, gb, gc; 
11557         int known;
11558         double feff;
11559         Gappat **fpt;
11560         char *spt;
11561         int gaplen;
11562
11563         fpt = pat;
11564         i = len+1;
11565         while( i-- ) 
11566         {
11567                 if( *fpt ) free( *fpt );
11568                 *fpt++ = NULL;
11569         }
11570
11571         for( j=0; j<clus; j++ ) 
11572         {
11573 //              reporterr(       "seq[%d] = %s\n", j, seq[j] );
11574                 feff = (double)eff[j];
11575
11576                 fpt = pat;
11577                 *fpt = NULL; // Falign.c kara yobareru tokiha chigau.
11578                 spt = seq[j];
11579                 gc = 0;
11580                 gaplen = 0;
11581
11582                 for( i=0; i<len+1; i++ ) 
11583 //              while( i-- )
11584                 {
11585 //                      reporterr(       "i=%d, gaplen = %d\n", i, gaplen );
11586                         gb = gc;
11587                         gc = ( i != len && *spt++ == '-' );
11588                         if( gc ) 
11589                                 gaplen++;
11590                         else
11591                         {
11592                                 if( gb && gaplen )
11593                                 {
11594                                         k = 1;
11595                                         known = 0;
11596                                         if( *fpt ) for( ; (*fpt)[k].len != -1; k++ )
11597                                         {
11598                                                 if( (*fpt)[k].len == gaplen ) 
11599                                                 {
11600 //                                                      reporterr(       "known\n" );
11601                                                         known = 1;
11602                                                         break;
11603                                                 }
11604                                         }
11605
11606                                         if( known == 0 )
11607                                         {
11608                                                 *fpt = (Gappat *)realloc( *fpt, (k+3) *  sizeof( Gappat ) );  // mae1 (total), ato2 (len0), term
11609                                                 if( !*fpt )
11610                                                 {
11611                                                         reporterr(       "Cannot allocate gappattern!'n" );
11612                                                         reporterr(       "Use an approximate method, with the --mafft5 option.\n" );
11613                                                         exit( 1 );
11614                                                 }
11615                                                 (*fpt)[k].freq = 0.0;
11616                                                 (*fpt)[k].len = gaplen;
11617                                                 (*fpt)[k+1].len = -1;
11618                                                 (*fpt)[k+1].freq = 0.0; // iranai
11619 //                                              reporterr(       "gaplen=%d, Unknown, %f\n", gaplen, (*fpt)[k].freq );
11620                                         }
11621
11622 //                                      reporterr(       "adding pos %d, len=%d, k=%d, freq=%f->", i, gaplen, k, (*fpt)[k].freq );
11623                                         (*fpt)[k].freq += feff;
11624 //                                      reporterr(       "%f\n", (*fpt)[k].freq );
11625                                         gaplen = 0;
11626                                 }
11627                         }
11628                         fpt++;
11629                 }
11630         }
11631 #if 1
11632         for( j=0; j<len+1; j++ )
11633         {
11634                 if( pat[j] )
11635                 {
11636 //                      reporterr(       "j=%d\n", j );
11637 //                      for( i=1; pat[j][i].len!=-1; i++ )
11638 //                              reporterr(       "pos=%d, i=%d, len=%d, freq=%f\n", j, i, pat[j][i].len, pat[j][i].freq );
11639
11640                         pat[j][0].len = 0; // iminashi
11641                         pat[j][0].freq = 0.0;
11642                         for( i=1; pat[j][i].len!=-1;i++ )
11643                         {
11644                                 pat[j][0].freq += pat[j][i].freq;
11645 //                              reporterr(       "totaling, i=%d, result = %f\n", i, pat[j][0].freq );
11646                         }
11647 //                      reporterr(       "totaled, result = %f\n", pat[j][0].freq );
11648
11649                         pat[j][i].freq = 1.0 - pat[j][0].freq;
11650                         pat[j][i].len = 0; // imiari
11651                         pat[j][i+1].len = -1; 
11652                 }
11653                 else
11654                 {
11655                         pat[j] = (Gappat *)calloc( 3, sizeof( Gappat ) );
11656                         pat[j][0].freq = 0.0;
11657                         pat[j][0].len = 0; // iminashi
11658
11659                         pat[j][1].freq = 1.0 - pat[j][0].freq;
11660                         pat[j][1].len = 0; // imiari
11661                         pat[j][2].len = -1; 
11662                 }
11663         }
11664 #endif
11665 }
11666
11667 static int minimum( int i1, int i2 )
11668 {
11669         return MIN( i1, i2 );
11670 }
11671
11672 static void commongappickpairfast( char *r1, char *r2, char *i1, char *i2, int *skip1, int *skip2 )
11673 {
11674 //      char *i1bk = i1;
11675         int skip, skipped1, skipped2;
11676 //      int skip, skipped1, skipped2, scand1, scand2;
11677         skipped1 = skipped2 = 0;
11678 //      reporterr("\n");
11679 //      while( *i1 )
11680         while( 1 )
11681         {
11682 //              fprintf( stderr, "i1 pos =%d\n", (int)(i1- i1bk) );
11683 //              reporterr( "\nSkip cand %d-%d\n", *skip1-skipped1, *skip2-skipped2 );
11684 #if 0
11685                 scand1 = *skip1-skipped1;
11686                 scand2 = *skip2-skipped2;
11687                 skip = MIN( scand1, scand2 );
11688 #else
11689                 skip = minimum( *skip1-skipped1, *skip2-skipped2 );
11690 #endif
11691 //              reporterr( "Skip %d\n", skip );
11692                 i1 += skip;
11693                 i2 += skip;
11694                 skipped1 += skip;
11695                 skipped2 += skip;
11696 //              fprintf( stderr, "i1 pos =%d, nlenmax=%d\n", (int)(i1- i1bk), nlenmax );
11697                 if( !*i1 ) break;
11698 //              reporterr( "%d, %c-%c\n", i1-i1bk, *i1, *i2 );
11699 //              if( *i1 == '-' && *i2 == '-' )  // iranai?
11700 //              {
11701 //                      reporterr( "Error in commongappickpairfast" );
11702 //                      exit( 1 );
11703 //                      i1++;
11704 //                      i2++;
11705 //              }
11706                 if( *i1 != '-' ) 
11707                 {
11708                         skipped1 = 0;
11709                         skip1++;
11710                 }
11711                 else skipped1++;
11712
11713                 if( *i2 != '-' ) 
11714                 {
11715                         skipped2 = 0;
11716                         skip2++;
11717                 }
11718                 else skipped2++;
11719
11720                 *r1++ = *i1++;
11721                 *r2++ = *i2++;
11722         }
11723         *r1 = 0;
11724         *r2 = 0;
11725 }
11726
11727 static void commongappickpair( char *r1, char *r2, char *i1, char *i2 )
11728 {
11729 //      strcpy( r1, i1 );
11730 //      strcpy( r2, i2 );
11731 //      return; // not SP
11732         while( *i1 )
11733         {
11734                 if( *i1 == '-' && *i2 == '-' ) 
11735                 {
11736                         i1++;
11737                         i2++;
11738                 }
11739                 else
11740                 {
11741                         *r1++ = *i1++;
11742                         *r2++ = *i2++;
11743                 }
11744         }
11745         *r1 = 0;
11746         *r2 = 0;
11747 }
11748
11749 double naiveRpairscore( int n1, int n2, char **seq1, char **seq2, double *eff1, double *eff2, int penal )
11750 {
11751 //      return( 0 );
11752         int i, j;
11753         double val;
11754         double  valf;
11755         int  pv;
11756         double deff;
11757         char *p1, *p2, *p1p, *p2p;
11758         val = 0.0;
11759         for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
11760         {
11761                 deff = eff1[i] * eff2[j];
11762 //              reporterr(       "feff %d-%d = %f\n", i, j, feff );
11763 //              reporterr(       "i1 = %s\n", seq1[i] );
11764 //              reporterr(       "i2 = %s\n", seq2[j] );
11765 //              reporterr(       "s1 = %s\n", s1 );
11766 //              reporterr(       "s2 = %s\n", s2 );
11767 //              reporterr(       "penal = %d\n", penal );
11768
11769                 valf = 0;
11770                 p1 = seq1[i]; p2 = seq2[j];
11771                 pv = 0;
11772                 if( *p1 == '-' && *p2 != '-' )
11773                         pv = penal;
11774                 if( *p1 != '-' && *p2 == '-' )
11775                         pv = penal;
11776 //              if( pv ) reporterr(       "Penal!, %f, %d-%d, pos1,pos2=%d,%d\n", pv * deff * 0.5,  i, j, p1-seq1[i], p2-seq2[j] );
11777                 p1p = p1; p2p = p2;
11778                 valf += (double)amino_dis[(unsigned char)*p1++][(unsigned char)*p2++] + 0.5 * pv;
11779                 while( *p1p )
11780                 {
11781                         pv = 0;
11782                         if( *p1p != '-' && *p2p != '-' )
11783                         {
11784                                 if( *p1 == '-' && *p2 != '-' )
11785                                         pv = penal;
11786                                 if( *p1 != '-' && *p2 == '-' )
11787                                         pv = penal;
11788                                 if( *p1 != '-' && *p2 != '-' )
11789                                         ;
11790                                 if( *p1 == '-' && *p2 == '-' )
11791                                         ;
11792                         }
11793                         if( *p1p == '-' && *p2p == '-' )
11794                         {
11795                                 if( *p1 == '-' && *p2 != '-' )
11796                                         pv = penal;
11797 //                                      ;
11798                                 if( *p1 != '-' && *p2 == '-' )
11799                                         pv = penal;
11800 //                                      ;
11801                                 if( *p1 != '-' && *p2 != '-' )
11802                                         ;
11803                                 if( *p1 == '-' && *p2 == '-' )
11804                                         ;
11805                         }
11806                         if( *p1p != '-' && *p2p == '-' )
11807                         {
11808                                 if( *p1 == '-' && *p2 != '-' )
11809                                         pv = penal * 2; // ??
11810 //                                      ;
11811                                 if( *p1 != '-' && *p2 == '-' )
11812                                         ;
11813                                 if( *p1 != '-' && *p2 != '-' )
11814                                         pv = penal;
11815 //                                      ;
11816                                 if( *p1 == '-' && *p2 == '-' )
11817                                         pv = penal;
11818 //                                      ;
11819                         }
11820                         if( *p1p == '-' && *p2p != '-' )
11821                         {
11822                                 if( *p1 == '-' && *p2 != '-' )
11823                                         ;
11824                                 if( *p1 != '-' && *p2 == '-' )
11825                                         pv = penal * 2; // ??
11826 //                                      ;
11827                                 if( *p1 != '-' && *p2 != '-' )
11828                                         pv = penal;
11829 //                                      ;
11830                                 if( *p1 == '-' && *p2 == '-' )
11831                                         pv = penal;
11832 //                                      ;
11833                         }
11834 //                      reporterr(       "adding %c-%c, %d\n", *p1, *p2, amino_dis[*p1][*p2] );
11835 //                      if( pv ) reporterr(       "Penal!, %f, %d-%d, pos1,pos2=%d,%d\n", pv * deff * 0.5,  i, j, p1-seq1[i], p2-seq2[j] );
11836                         valf += amino_dis[(unsigned char)*p1++][(unsigned char)*p2++] + 0.5 * pv;
11837                         p1p++; p2p++;
11838                 }
11839 //              reporterr(       "valf = %d\n", valf );
11840                 val += deff * ( valf );
11841         }
11842         reporterr(       "val = %f\n", val );
11843         return( val );
11844 //      exit( 1 );
11845 }
11846 double naiveQpairscore( int n1, int n2, char **seq1, char **seq2, double *eff1, double *eff2, int penal )
11847 {
11848         int i, j;
11849         double val;
11850         double  valf;
11851         int  pv;
11852         double deff;
11853         char *p1, *p2, *p1p, *p2p;
11854         return( 0 );
11855         val = 0.0;
11856         for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
11857         {
11858                 deff = eff1[i] * eff2[j];
11859 //              reporterr(       "feff %d-%d = %f\n", i, j, feff );
11860 //              reporterr(       "i1 = %s\n", seq1[i] );
11861 //              reporterr(       "i2 = %s\n", seq2[j] );
11862 //              reporterr(       "s1 = %s\n", s1 );
11863 //              reporterr(       "s2 = %s\n", s2 );
11864 //              reporterr(       "penal = %d\n", penal );
11865
11866                 valf = 0;
11867                 p1 = seq1[i]; p2 = seq2[j];
11868                 pv = 0;
11869                 if( *p1 == '-' && *p2 != '-' )
11870                         pv = penal;
11871                 if( *p1 != '-' && *p2 == '-' )
11872                         pv = penal;
11873 //              if( pv ) reporterr(       "Penal!, %f, %d-%d, pos1,pos2=%d,%d\n", pv * deff * 0.5,  i, j, p1-seq1[i], p2-seq2[j] );
11874                 p1p = p1; p2p = p2;
11875                 valf += (double)amino_dis[(unsigned char)*p1++][(unsigned char)*p2++] + 0.5 * pv;
11876                 while( *p1p )
11877                 {
11878                         pv = 0;
11879                         if( *p1p != '-' && *p2p != '-' )
11880                         {
11881                                 if( *p1 == '-' && *p2 != '-' )
11882                                         pv = penal;
11883                                 if( *p1 != '-' && *p2 == '-' )
11884                                         pv = penal;
11885                                 if( *p1 != '-' && *p2 != '-' )
11886                                         ;
11887                                 if( *p1 == '-' && *p2 == '-' )
11888                                         ;
11889                         }
11890                         if( *p1p == '-' && *p2p == '-' )
11891                         {
11892                                 if( *p1 == '-' && *p2 != '-' )
11893 //                                      pv = penal;
11894                                         ;
11895                                 if( *p1 != '-' && *p2 == '-' )
11896 //                                      pv = penal;
11897                                         ;
11898                                 if( *p1 != '-' && *p2 != '-' )
11899                                         ;
11900                                 if( *p1 == '-' && *p2 == '-' )
11901                                         ;
11902                         }
11903                         if( *p1p != '-' && *p2p == '-' )
11904                         {
11905                                 if( *p1 == '-' && *p2 != '-' )
11906                                         pv = penal * 2; // ??
11907 //                                      ;
11908                                 if( *p1 != '-' && *p2 == '-' )
11909                                         ;
11910                                 if( *p1 != '-' && *p2 != '-' )
11911                                         pv = penal;
11912 //                                      ;
11913                                 if( *p1 == '-' && *p2 == '-' )
11914 //                                      pv = penal;
11915                                         ;
11916                         }
11917                         if( *p1p == '-' && *p2p != '-' )
11918                         {
11919                                 if( *p1 == '-' && *p2 != '-' )
11920                                         ;
11921                                 if( *p1 != '-' && *p2 == '-' )
11922                                         pv = penal * 2; // ??
11923 //                                      ;
11924                                 if( *p1 != '-' && *p2 != '-' )
11925                                         pv = penal;
11926 //                                      ;
11927                                 if( *p1 == '-' && *p2 == '-' )
11928 //                                      pv = penal;
11929                                         ;
11930                         }
11931 //                      reporterr(       "adding %c-%c, %d\n", *p1, *p2, amino_dis[*p1][*p2] );
11932 //                      if( pv ) reporterr(       "Penal!, %f, %d-%d, pos1,pos2=%d,%d\n", pv * deff * 0.5,  i, j, p1-seq1[i], p2-seq2[j] );
11933                         valf += amino_dis[(unsigned char)*p1++][(unsigned char)*p2++] + 0.5 * pv;
11934                         p1p++; p2p++;
11935                 }
11936 //              reporterr(       "valf = %d\n", valf );
11937                 val += deff * ( valf );
11938         }
11939         reporterr(       "val = %f\n", val );
11940         return( val );
11941 //      exit( 1 );
11942 }
11943 double naiveHpairscore( int n1, int n2, char **seq1, char **seq2, double *eff1, double *eff2, int penal )
11944 {
11945         int i, j;
11946         double val;
11947         double  valf;
11948         int  pv;
11949 //      double feff = 0.0; // by D.Mathog, a guess
11950         double deff;
11951         char *p1, *p2, *p1p, *p2p;
11952         val = 0.0;
11953         for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
11954         {
11955                 deff = eff1[i] * eff2[j];
11956 //              reporterr(       "i1 = %s\n", seq1[i] );
11957 //              reporterr(       "i2 = %s\n", seq2[j] );
11958 //              reporterr(       "s1 = %s\n", s1 );
11959 //              reporterr(       "s2 = %s\n", s2 );
11960 //              reporterr(       "penal = %d\n", penal );
11961
11962                 valf = 0;
11963                 p1 = seq1[i]; p2 = seq2[j];
11964                 pv = 0;
11965                 if( *p1 == '-' && *p2 != '-' )
11966                         pv = penal;
11967                 if( *p1 != '-' && *p2 == '-' )
11968                         pv = penal;
11969                 if( pv ) reporterr(       "Penal!, %f, %d-%d, pos1,pos2=%d,%d\n", pv * deff * 0.5,  i, j, (int)(p1-seq1[i]), (int)(p2-seq2[j]) );
11970                 p1p = p1; p2p = p2;
11971                 valf += (double)amino_dis[(unsigned char)*p1++][(unsigned char)*p2++] + 0.5 * pv;
11972                 while( *p1p )
11973                 {
11974                         pv = 0;
11975                         if( *p1p != '-' && *p2p != '-' )
11976                         {
11977                                 if( *p1 == '-' && *p2 != '-' )
11978                                         pv = penal;
11979                                 if( *p1 != '-' && *p2 == '-' )
11980                                         pv = penal;
11981                                 if( *p1 != '-' && *p2 != '-' )
11982                                         ;
11983                                 if( *p1 == '-' && *p2 == '-' )
11984                                         ;
11985                         }
11986                         if( *p1p == '-' && *p2p == '-' )
11987                         {
11988                                 if( *p1 == '-' && *p2 != '-' )
11989 //                                      pv = penal;
11990                                         ;
11991                                 if( *p1 != '-' && *p2 == '-' )
11992 //                                      pv = penal;
11993                                         ;
11994                                 if( *p1 != '-' && *p2 != '-' )
11995                                         ;
11996                                 if( *p1 == '-' && *p2 == '-' )
11997                                         ;
11998                         }
11999                         if( *p1p != '-' && *p2p == '-' )
12000                         {
12001                                 if( *p1 == '-' && *p2 != '-' )
12002 //                                      pv = penal;
12003                                         ;
12004                                 if( *p1 != '-' && *p2 == '-' )
12005                                         ;
12006                                 if( *p1 != '-' && *p2 != '-' )
12007                                         pv = penal;
12008                                 if( *p1 == '-' && *p2 == '-' )
12009 //                                      pv = penal;
12010                                         ;
12011                         }
12012                         if( *p1p == '-' && *p2p != '-' )
12013                         {
12014                                 if( *p1 == '-' && *p2 != '-' )
12015                                         ;
12016                                 if( *p1 != '-' && *p2 == '-' )
12017 //                                      pv = penal;
12018                                         ;
12019                                 if( *p1 != '-' && *p2 != '-' )
12020                                         pv = penal;
12021                                 if( *p1 == '-' && *p2 == '-' )
12022 //                                      pv = penal;
12023                                         ;
12024                         }
12025 //                      reporterr(       "adding %c-%c, %d\n", *p1, *p2, amino_dis[*p1][*p2] );
12026 //                      if( pv ) reporterr(       "Penal!, %f, %d-%d, pos1,pos2=%d,%d\n", pv * deff * 0.5,  i, j, p1-seq1[i], p2-seq2[j] );
12027                         valf += amino_dis[(unsigned char)*p1++][(unsigned char)*p2++] + 0.5 * pv;
12028                         p1p++; p2p++;
12029                 }
12030 //              reporterr(       "valf = %d\n", valf );
12031                 val += deff * ( valf );
12032         }
12033         reporterr(       "val = %f\n", val );
12034         return( val );
12035 //      exit( 1 );
12036 }
12037
12038 double naivepairscorefast( char *seq1, char *seq2, int *skip1, int *skip2, int penal )
12039 {
12040         double  vali;
12041         int len = strlen( seq1 );
12042         char *s1, *s2;
12043         char *p1, *p2;
12044
12045         s1 = calloc( len+1, sizeof( char ) );
12046         s2 = calloc( len+1, sizeof( char ) );
12047         {
12048                 vali = 0.0;
12049                 commongappickpairfast( s1, s2, seq1, seq2, skip1, skip2 );
12050 //              commongappickpair( s1, s2, seq1, seq2 );
12051 //              printf(       "\n###s1 = %s\n", seq1 );
12052 //              printf(       "###s2 = %s\n", seq2 );
12053 //              printf(       "\n###i1 = %s\n", s1 );
12054 //              printf(       "###i2 = %s\n", s2 );
12055 //              printf( "allocated size, len+1 = %d\n", len+1 );
12056 //              printf(       "###penal = %d\n", penal );
12057
12058                 p1 = s1; p2 = s2;
12059                 while( *p1 )
12060                 {
12061                         if( *p1 == '-' )
12062                         {
12063 //                              reporterr(       "Penal! %c-%c in %d-%d, %f\n", *(p1-1), *(p2-1), i, j, feff );
12064                                 vali += (double)penal;
12065 //                              while( *p1 == '-' || *p2 == '-' ) 
12066                                 while( *p1 == '-' )  // SP
12067                                 {
12068                                         p1++;
12069                                         p2++;
12070                                 }
12071                                 continue;
12072                         }
12073                         if( *p2 == '-' )
12074                         {
12075 //                              reporterr(       "Penal! %c-%c in %d-%d, %f\n", *(p1-1), *(p2-1), i, j, feff );
12076                                 vali +=  (double)penal;
12077 //                              while( *p2 == '-' || *p1 == '-' ) 
12078                                 while( *p2 == '-' )  // SP
12079                                 {
12080                                         p1++;
12081                                         p2++;
12082                                 }
12083                                 continue;
12084                         }
12085 //                      reporterr(       "adding %c-%c, %d\n", *p1, *p2, amino_dis[*p1][*p2] );
12086                         vali += (double)amino_dis[(unsigned char)*p1++][(unsigned char)*p2++];
12087                 }
12088         }
12089         free( s1 );
12090         free( s2 );
12091 //      reporterr(       "###vali = %d\n", vali );
12092         return( vali );
12093 }
12094
12095 double naivepairscore11_dynmtx( double **mtx, char *seq1, char *seq2, int penal )
12096 {
12097         double  vali;
12098         int len = strlen( seq1 );
12099         char *s1, *s2, *p1, *p2;
12100         int c1, c2;
12101
12102
12103         s1 = calloc( len+1, sizeof( char ) );
12104         s2 = calloc( len+1, sizeof( char ) );
12105         {
12106                 vali = 0.0;
12107                 commongappickpair( s1, s2, seq1, seq2 );
12108 //              reporterr(       "###i1 = %s\n", s1 );
12109 //              reporterr(       "###i2 = %s\n", s2 );
12110 //              reporterr(       "###penal = %d\n", penal );
12111
12112                 p1 = s1; p2 = s2;
12113                 while( *p1 )
12114                 {
12115                         if( *p1 == '-' )
12116                         {
12117 //                              reporterr(       "Penal! %c-%c in %d-%d, %f\n", *(p1-1), *(p2-1), i, j, feff );
12118                                 vali += (double)penal;
12119 //                              while( *p1 == '-' || *p2 == '-' ) 
12120                                 while( *p1 == '-' )  // SP
12121                                 {
12122                                         p1++;
12123                                         p2++;
12124                                 }
12125                                 continue;
12126                         }
12127                         if( *p2 == '-' )
12128                         {
12129 //                              reporterr(       "Penal! %c-%c in %d-%d, %f\n", *(p1-1), *(p2-1), i, j, feff );
12130                                 vali +=  (double)penal;
12131 //                              while( *p2 == '-' || *p1 == '-' ) 
12132                                 while( *p2 == '-' )  // SP
12133                                 {
12134                                         p1++;
12135                                         p2++;
12136                                 }
12137                                 continue;
12138                         }
12139 //                      reporterr(       "adding %c-%c, %d\n", *p1, *p2, amino_dis[*p1][*p2] );
12140                         c1 = amino_n[(unsigned char)*p1++];
12141                         c2 = amino_n[(unsigned char)*p2++];
12142                         vali += (double)mtx[c1][c2];
12143                 }
12144         }
12145         free( s1 );
12146         free( s2 );
12147 //      reporterr(       "###vali = %d\n", vali );
12148         return( vali );
12149 }
12150
12151 double naivepairscore11( char *seq1, char *seq2, int penal )
12152 {
12153         double  vali;
12154         int len = strlen( seq1 );
12155         char *s1, *s2, *p1, *p2;
12156
12157         s1 = calloc( len+1, sizeof( char ) );
12158         s2 = calloc( len+1, sizeof( char ) );
12159         {
12160                 vali = 0.0;
12161                 commongappickpair( s1, s2, seq1, seq2 );
12162 //              reporterr(       "###i1 = %s\n", s1 );
12163 //              reporterr(       "###i2 = %s\n", s2 );
12164 //              reporterr(       "###penal = %d\n", penal );
12165
12166                 p1 = s1; p2 = s2;
12167                 while( *p1 )
12168                 {
12169                         if( *p1 == '-' )
12170                         {
12171 //                              reporterr(       "Penal! %c-%c in %d-%d, %f\n", *(p1-1), *(p2-1), i, j, feff );
12172                                 vali += (double)penal;
12173 //                              while( *p1 == '-' || *p2 == '-' ) 
12174                                 while( *p1 == '-' )  // SP
12175                                 {
12176                                         p1++;
12177                                         p2++;
12178                                 }
12179                                 continue;
12180                         }
12181                         if( *p2 == '-' )
12182                         {
12183 //                              reporterr(       "Penal! %c-%c in %d-%d, %f\n", *(p1-1), *(p2-1), i, j, feff );
12184                                 vali +=  (double)penal;
12185 //                              while( *p2 == '-' || *p1 == '-' ) 
12186                                 while( *p2 == '-' )  // SP
12187                                 {
12188                                         p1++;
12189                                         p2++;
12190                                 }
12191                                 continue;
12192                         }
12193 //                      reporterr(       "adding %c-%c, %d\n", *p1, *p2, amino_dis[*p1][*p2] );
12194                         vali += (double)amino_dis[(unsigned char)*p1++][(unsigned char)*p2++];
12195                 }
12196         }
12197         free( s1 );
12198         free( s2 );
12199 //      reporterr(       "###vali = %d\n", vali );
12200         return( vali );
12201 }
12202
12203 double naivepairscore( int n1, int n2, char **seq1, char **seq2, double *eff1, double *eff2, int penal )
12204 {
12205 //      return( 0.0 );
12206         int i, j;
12207         double val;
12208         int  vali;
12209         double feff;
12210         int len = strlen( seq1[0] );
12211         char *s1, *s2, *p1, *p2;
12212         s1 = calloc( len+1, sizeof( char ) );
12213         s2 = calloc( len+1, sizeof( char ) );
12214         val = 0.0;
12215         for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
12216         {
12217                 vali = 0;
12218                 feff = eff1[i] * eff2[j];
12219 //              reporterr(       "feff %d-%d = %f\n", i, j, feff );
12220                 commongappickpair( s1, s2, seq1[i], seq2[j] );
12221 //              reporterr(       "i1 = %s\n", seq1[i] );
12222 //              reporterr(       "i2 = %s\n", seq2[j] );
12223 //              reporterr(       "s1 = %s\n", s1 );
12224 //              reporterr(       "s2 = %s\n", s2 );
12225 //              reporterr(       "penal = %d\n", penal );
12226
12227                 p1 = s1; p2 = s2;
12228                 while( *p1 )
12229                 {
12230                         if( *p1 == '-' )
12231                         {
12232 //                              reporterr(       "Penal! %c-%c in %d-%d, %f\n", *(p1-1), *(p2-1), i, j, feff );
12233                                 vali += penal;
12234 //                              while( *p1 == '-' || *p2 == '-' ) 
12235                                 while( *p1 == '-' )  // SP
12236                                 {
12237                                         p1++;
12238                                         p2++;
12239                                 }
12240                                 continue;
12241                         }
12242                         if( *p2 == '-' )
12243                         {
12244 //                              reporterr(       "Penal! %c-%c in %d-%d, %f\n", *(p1-1), *(p2-1), i, j, feff );
12245                                 vali +=  penal;
12246 //                              while( *p2 == '-' || *p1 == '-' ) 
12247                                 while( *p2 == '-' )  // SP
12248                                 {
12249                                         p1++;
12250                                         p2++;
12251                                 }
12252                                 continue;
12253                         }
12254 //                      reporterr(       "adding %c-%c, %d\n", *p1, *p2, amino_dis[*p1][*p2] );
12255                         vali += amino_dis[(unsigned char)*p1++][(unsigned char)*p2++];
12256                 }
12257 //              reporterr(       "vali = %d\n", vali );
12258                 val += feff * vali;
12259         }
12260         free( s1 );
12261         free( s2 );
12262         reporterr(       "val = %f\n", val );
12263         return( val );
12264 //      exit( 1 );
12265 }
12266
12267 double plainscore( int nseq, char **s )
12268 {
12269         int i, j, ilim;
12270         double v = 0.0;
12271         
12272         ilim = nseq-1;
12273         for( i=0; i<ilim; i++ ) for( j=i+1; j<nseq; j++ )
12274         {
12275                 v += (double)naivepairscore11( s[i], s[j], penalty );
12276         }
12277
12278         reporterr(       "penalty = %d\n", penalty );
12279
12280         return( v );
12281 }
12282
12283
12284 int addonetip( int njobc, int ***topolc, double **lenc, double **iscorec, int ***topol, double **len, Treedep *dep, int treeout, Addtree *addtree, int iadd, char **name, int *alnleninnode, int *nogaplen, int noalign )
12285 {
12286         int i, j, mem0, mem1, posinnew, m;
12287         int nstep;
12288         int norg;
12289         double minscore, minscoreo, eff0, eff1, addedlen, tmpmin;
12290         int nearest, nearesto;
12291         int repnorg;
12292         int *leaf2node;
12293         int *additionaltopol;
12294 //      double (*clusterfuncpt[1])(double,double);
12295         Bchain *ac, *acpt, *acori, *acnext, *acprev;
12296         int neighbor;
12297         char *neighborlist;
12298         char *npt;
12299         int reflen, nearestnode, nogaplentoadd;
12300         int *topoldum0 = NULL;
12301         int *topoldum1 = NULL;
12302         int *topolo0;
12303         int *topolo1;
12304         int seqlengthcondition;
12305         double sueff1_double_local = 1.0 - sueff_global;
12306         double sueff05_double_local = sueff_global * 0.5;
12307 //      char **tree; //static?
12308 //      char *treetmp; //static?
12309
12310 //      for( i=0; i<njobc; i++ ) reporterr( "nogaplen of %d = %d\n", i+1, nogaplen[i] );
12311 //exit( 1 );
12312
12313
12314 //      treetmp = AllocateCharVec( njob*150 );
12315 //      tree = AllocateCharMtx( njob, njob*150 );
12316
12317 //      sueff1_double = 1.0 - sueff_global;
12318 //      sueff05_double = sueff_global * 0.5;
12319 //      if ( treemethod == 'X' )
12320 //              clusterfuncpt[0] = cluster_mix_double;
12321 //      else if ( treemethod == 'E' )
12322 //              clusterfuncpt[0] = cluster_average_double;
12323 //      else if ( treemethod == 'q' )
12324 //              clusterfuncpt[0] = cluster_minimum_double;
12325 //      else
12326 //      {
12327 //              reporterr(       "Unknown treemethod, %c\n", treemethod );
12328 //              exit( 1 );
12329 //      }
12330
12331         norg = njobc-1;
12332         nstep = njobc-2;
12333
12334         additionaltopol = (int *)calloc( 2, sizeof( int ) );
12335         leaf2node= (int *)calloc( norg, sizeof( int ) );
12336         if( treeout )
12337         {
12338                 neighborlist = calloc( norg * 30, sizeof( char ) );
12339         }
12340 //      for( i=0; i<njobc; i++ ) sprintf( tree[i], "%d", i+1 );
12341         if( !leaf2node )
12342         {
12343                 reporterr(       "Cannot allocate leaf2node.\n" );
12344                 exit( 1 );
12345         }
12346         additionaltopol[0] = norg;
12347         additionaltopol[1] = -1;
12348
12349         ac = (Bchain *)malloc( norg * sizeof( Bchain ) );
12350         for( i=0; i<norg; i++ )
12351         {
12352                 ac[i].next = ac+i+1;
12353                 ac[i].prev = ac+i-1;
12354                 ac[i].pos = i;
12355         }
12356         ac[norg-1].next = NULL;
12357
12358
12359         acori = (Bchain *)malloc( 1 * sizeof( Bchain ) );
12360         acori->next = ac;
12361         acori->pos = -1;
12362         ac[0].prev = acori;
12363
12364
12365 //      for( i=0; i<nstep; i++ )
12366 //      {
12367 //              reporterr(       "distfromtip = %f\n", dep[i].distfromtip );
12368 //      }
12369 //
12370 //      for( i=0; i<norg; i++ )
12371 //      {
12372 //              reporterr(       "disttofrag(%d,%d) = %f\n", i, njobc-1, iscorec[i][norg-i] );
12373 //      }
12374
12375
12376         minscore = 9999.9;
12377         nearest = -1;
12378         for( i=0; i<norg; i++ )
12379         {
12380                 tmpmin = iscorec[i][norg-i];
12381                 if( minscore > tmpmin )
12382                 {
12383                         minscore = tmpmin;
12384                         nearest = i;
12385                 }
12386         }
12387         nearesto = nearest;
12388         minscoreo = minscore;
12389
12390
12391
12392 //      for( i=0; i<njobc-1; i++ ) for( j=i+1; j<njobc; j++ )
12393 //              reporterr(       "iscorec[%d][%d] = %f\n", i, j, iscorec[i][j-i] );
12394 //      reporterr( "nearest = %d\n", nearest+1 );
12395 //      reporterr( "nearesto = %d\n", nearesto+1 );
12396
12397         posinnew = 0;
12398         repnorg = -1;
12399         nogaplentoadd = nogaplen[norg];
12400
12401
12402
12403         for( i=0; i<norg; i++ ) leaf2node[i] = -1; 
12404         for( i=0; i<nstep; i++ )
12405         {
12406                 mem0 = topol[i][0][0];
12407                 mem1 = topol[i][1][0];
12408 #if 0
12409                 reporterr(       "\n\nstep %d (old) \n", i );
12410
12411                 reporterr( "group0 = \n" );
12412                 for( j=0; topol[i][0][j]>-1; j++ ) 
12413                 {
12414                         reporterr( "%d ", topol[i][0][j]+1 );
12415                 }
12416                 reporterr( "\n" );
12417                 reporterr( "len=%f\n", len[i][0] );
12418                 reporterr( "group1 = \n" );
12419                 for( j=0; topol[i][1][j]>-1; j++ ) 
12420                 {
12421                         reporterr( "%d ", topol[i][1][j]+1 );
12422                 }
12423                 reporterr( "\n" );
12424                 reporterr( "len=%f\n", len[i][1] );
12425                 
12426                 reporterr(       "\n\n\nminscore = %f ? %f\n", minscore, dep[i].distfromtip*2 );
12427                 reporterr(       "i = %d\n", i );
12428                 if( leaf2node[nearest] == -1 )
12429                 {
12430                         reporterr(       "nogaplen[nearest] = %d\n", nogaplen[nearest] );
12431                 }
12432                 else
12433                 {
12434                         reporterr(       "alnleninnode[leaf2node[nearest]] = %d\n", alnleninnode[leaf2node[nearest]] );
12435                         reporterr(       "leaf2node[nearest] = %d\n", leaf2node[nearest] );
12436                 }
12437 #endif
12438                 nearestnode = leaf2node[nearest];
12439                 if( nearestnode == -1 )
12440                         reflen = nogaplen[nearest];
12441                 else
12442                         reflen = alnleninnode[nearestnode];
12443 //                      reflen = alnleninnode[i]; // BUG!!
12444
12445                 if( noalign ) seqlengthcondition =  1;
12446                 else seqlengthcondition = ( nogaplentoadd <= reflen );
12447
12448 //seqlengthcondition = 1; // CHUUI
12449 //seqlengthcondition = ( nogaplentoadd <= reflen ); // CHUUI
12450
12451                 if( repnorg == -1 && dep[i].distfromtip * 2 > minscore && seqlengthcondition )  // Keitouteki ichi ha fuseikaku.
12452 //              if( repnorg == -1 && dep[i].distfromtip * 2 > minscore ) // Keitouteki ichi dake ga hitsuyouna baaiha kore wo tsukau.
12453                 {
12454 //                      reporterr(       "INSERT HERE, %d-%d\n", nearest, norg );
12455 //                      reporterr(       "nearest = %d\n", nearest );
12456 //                      reporterr(       "\n\n\nminscore = %f\n", minscore );
12457 //                      reporterr(       "distfromtip *2 = %f\n", dep[i].distfromtip * 2 );
12458 //                      reporterr(       "nearest=%d, leaf2node[]=%d\n", nearest, leaf2node[nearest] );
12459
12460                         if( nearestnode == -1 )
12461                         {
12462 //                              reporterr(       "INSERTING to 0!!!\n" );
12463 //                              reporterr(       "lastlength = %d\n", nogaplen[norg] );
12464 //                              reporterr(       "reflength = %d\n", nogaplen[nearest] );
12465                                 topolc[posinnew][0] = (int *)realloc( topolc[posinnew][0], ( 1 + 1 ) * sizeof( int ) );
12466                                 topolc[posinnew][0][0] = nearest;
12467                                 topolc[posinnew][0][1] = -1;
12468
12469                                 addedlen = lenc[posinnew][0] = minscore / 2;
12470
12471                         }
12472                         else
12473                         {
12474 //                              reporterr(       "INSERTING to g, leaf2node = %d, cm=%d!!!\n", leaf2node[nearest], countmem(topol[leaf2node[nearest]][0] ) );
12475 //                              reporterr(       "alnleninnode[i] = %d\n", alnleninnode[i] );
12476 //                              reporterr(       "alnleninnode[leaf2node[nearest]] = %d\n", alnleninnode[leaf2node[nearest]] );
12477
12478                                 topolc[posinnew][0] = (int *)realloc( topolc[posinnew][0], ( ( countmem( topol[nearestnode][0] ) + countmem( topol[nearestnode][1] ) + 1 ) * sizeof( int ) ) );
12479 //                              reporterr(       "leaf2node[%d] = %d\n", nearest, leaf2node[nearest] );
12480                                 intcpy( topolc[posinnew][0], topol[nearestnode][0] );
12481                                 intcat( topolc[posinnew][0], topol[nearestnode][1] );
12482 //                              addedlen = lenc[posinnew][0] = minscore / 2 - len[nearestnode][0]; // bug!!
12483                                 addedlen = lenc[posinnew][0] = dep[i].distfromtip - minscore / 2; // 2014/06/10
12484 //                              fprintf( stderr, "addedlen = %f, dep[i].distfromtip = %f, len[nearestnode][0] = %f, minscore/2 = %f, lenc[posinnew][0] = %f\n", addedlen, dep[i].distfromtip, len[nearestnode][0], minscore/2, lenc[posinnew][0] );
12485
12486                         }
12487                         neighbor = lastmem( topolc[posinnew][0] );
12488
12489                         if( treeout )
12490                         {
12491 #if 0
12492                                 fp = fopen( "infile.tree", "a" ); // kyougou!!
12493                                 if( fp == 0 )
12494                                 {
12495                                         reporterr(       "File error!\n" );
12496                                         exit( 1 );
12497                                 }
12498                                 fprintf( fp, "\n" );
12499                                 fprintf( fp, "%8d: %s\n", norg+iadd+1, name[norg+iadd] );
12500                                 fprintf( fp, "          nearest sequence: %d\n", nearest + 1 );
12501                                 fprintf( fp, "          distance: %f\n", minscore );
12502                                 fprintf( fp, "          cousin: " );
12503                                 for( j=0; topolc[posinnew][0][j]!=-1; j++ )
12504                                         fprintf( fp, "%d ", topolc[posinnew][0][j]+1 );
12505                                 fprintf( fp, "\n" );
12506                                 fclose( fp );
12507 #else
12508                                 addtree[iadd].nearest = nearesto;
12509                                 addtree[iadd].dist1 = minscoreo;
12510                                 addtree[iadd].dist2 = minscore;
12511                                 neighborlist[0] = 0;
12512                                 npt = neighborlist;
12513                                 for( j=0; topolc[posinnew][0][j]!=-1; j++ )
12514                                 {
12515                                         sprintf( npt, "%d ", topolc[posinnew][0][j]+1 );
12516                                         npt += strlen( npt );
12517                                 }
12518                                 addtree[iadd].neighbors = calloc( npt-neighborlist+1, sizeof( char ) );
12519                                 strcpy( addtree[iadd].neighbors, neighborlist );
12520 #endif
12521                         }
12522
12523 //                      reporterr(       "INSERTING to 1!!!\n" );
12524                         topolc[posinnew][1] = (int *)realloc( topolc[posinnew][1], ( 1 + 1 ) * sizeof( int ) );
12525                         topolc[posinnew][1][0] = norg;
12526                         topolc[posinnew][1][1] = -1;
12527                         lenc[posinnew][1] = minscore / 2;
12528
12529 //                      reporterr(       "STEP %d (newnew)\n", posinnew );
12530 //                      for( j=0; topolc[posinnew][0][j]!=-1; j++ ) reporterr(       " %d", topolc[posinnew][0][j]+1 );
12531 //                      reporterr(       "\n len=%f\n", lenc[posinnew][0] );
12532 //                      for( j=0; topolc[posinnew][1][j]!=-1; j++ ) reporterr(       " %d", topolc[posinnew][1][j]+1 );
12533 //                      reporterr(       "\n len=%f\n", lenc[posinnew][1] );
12534
12535                         repnorg = nearest;
12536
12537 //                      reporterr(       "STEP %d\n", posinnew );
12538 //                      for( j=0; topolc[posinnew][0][j]!=-1; j++ ) reporterr(       " %d", topolc[posinnew][0][j] );
12539 //                      reporterr(       "\n len=%f\n", lenc[i][0] );
12540 //                      for( j=0; topolc[posinnew][1][j]!=-1; j++ ) reporterr(       " %d", topolc[posinnew][1][j] );
12541 //                      reporterr(       "\n len=%f\n", lenc[i][1] );
12542
12543 //                      im = topolc[posinnew][0][0];
12544 //                      jm = topolc[posinnew][1][0];
12545 //                      sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], lenc[posinnew][0], tree[jm], lenc[posinnew][1] );
12546 //                      strcpy( tree[im], treetmp );
12547
12548                         posinnew++;
12549                 }
12550
12551 //              reporterr(       "minscore = %f\n", minscore );
12552 //              reporterr(       "distfromtip = %f\n", dep[i].distfromtip );
12553 //              reporterr(       "Modify matrix, %d-%d\n", nearest, norg );
12554                 eff0 = iscorec[mem0][norg-mem0];
12555                 eff1 = iscorec[mem1][norg-mem1];
12556
12557 //              iscorec[mem0][norg-mem0] = (clusterfuncpt[0])( eff0, eff1 );
12558                 iscorec[mem0][norg-mem0] =  MIN( eff0, eff1 ) * sueff1_double_local + ( eff0 + eff1 ) * sueff05_double_local; 
12559                 iscorec[mem1][norg-mem1] = 9999.9; // sukoshi muda
12560
12561                 acprev = ac[mem1].prev; 
12562                 acnext = ac[mem1].next; 
12563                 acprev->next = acnext;
12564                 if( acnext != NULL ) acnext->prev = acprev;
12565
12566                 if( ( nearest == mem1 || nearest == mem0 ) )
12567                 {
12568                         minscore = 9999.9;
12569 //                      for( j=0; j<norg; j++ ) // sukoshi muda
12570 //                      {
12571 //                              if( minscore > iscorec[j][norg-j] )
12572 //                              {
12573 //                                      minscore = iscorec[j][norg-j];
12574 //                                      nearest = j;
12575 //                              }
12576 //                      }
12577 //                      reporterr(       "searching on modified ac " );
12578                         for( acpt=acori->next; acpt!=NULL; acpt=acpt->next ) // sukoshi muda
12579                         {
12580 //                              reporterr(       "." );
12581                                 j = acpt->pos;
12582                                 tmpmin = iscorec[j][norg-j];
12583                                 if( minscore > tmpmin )
12584                                 {
12585                                         minscore = tmpmin;
12586                                         nearest = j;
12587                                 }
12588                         }
12589 //                      reporterr(       "done\n" );
12590                 }
12591
12592 //              reporterr(       "posinnew = %d\n", posinnew );
12593
12594
12595                 if( topol[i][0][0] == repnorg )
12596                 {
12597                         topolc[posinnew][0] = (int *)realloc( topolc[posinnew][0], ( countmem( topol[i][0] ) + 2 ) * sizeof( int ) );
12598                         intcpy( topolc[posinnew][0], topol[i][0] );
12599                         intcat( topolc[posinnew][0], additionaltopol );
12600                         lenc[posinnew][0] = len[i][0] - addedlen; // 2014/6/10
12601 //                      fprintf( stderr, "i=%d, dep[i].distfromtip=%f\n", i, dep[i].distfromtip );
12602 //                      fprintf( stderr, "addedlen=%f, len[i][0]=%f, lenc[][0]=%f\n", addedlen, len[i][0], lenc[posinnew][0] );
12603 //                      fprintf( stderr, "lenc[][1] = %f\n", lenc[posinnew][0] );
12604                         addedlen = 0.0;
12605                 }
12606                 else
12607                 {
12608                         topolc[posinnew][0] = (int *)realloc( topolc[posinnew][0], ( countmem( topol[i][0] ) + 1 ) * sizeof( int ) );
12609                         intcpy( topolc[posinnew][0], topol[i][0] );
12610                         lenc[posinnew][0] = len[i][0];
12611                 }
12612
12613                 if( topol[i][1][0] == repnorg )
12614                 {
12615                         topolc[posinnew][1] = (int *)realloc( topolc[posinnew][1], ( countmem( topol[i][1] ) + 2 ) * sizeof( int ) );
12616                         intcpy( topolc[posinnew][1], topol[i][1] );
12617                         intcat( topolc[posinnew][1], additionaltopol );
12618                         lenc[posinnew][1] = len[i][1] - addedlen; // 2014/6/10
12619 //                      fprintf( stderr, "i=%d, dep[i].distfromtip=%f\n", i, dep[i].distfromtip );
12620 //                      fprintf( stderr, "addedlen=%f, len[i][1]=%f, lenc[][1]=%f\n", addedlen, len[i][1], lenc[posinnew][1] );
12621 //                      fprintf( stderr, "lenc[][1] = %f\n", lenc[posinnew][1] );
12622                         addedlen = 0.0;
12623
12624                         repnorg = topolc[posinnew][0][0]; // juuyou
12625                 }
12626                 else
12627                 {
12628                         topolc[posinnew][1] = (int *)realloc( topolc[posinnew][1], ( countmem( topol[i][1] ) + 1 ) * sizeof( int ) );
12629                         intcpy( topolc[posinnew][1], topol[i][1] );
12630                         lenc[posinnew][1] = len[i][1];
12631                 }
12632
12633 //              reporterr(       "\nSTEP %d (new)\n", posinnew );
12634 //              for( j=0; topolc[posinnew][0][j]!=-1; j++ ) reporterr(       " %d", topolc[posinnew][0][j]+1 );
12635 //              reporterr(       "\n len=%f\n", lenc[posinnew][0] );
12636 //              for( j=0; topolc[posinnew][1][j]!=-1; j++ ) reporterr(       " %d", topolc[posinnew][1][j]+1 );
12637 //              reporterr(       "\n len=%f\n", lenc[posinnew][1] );
12638
12639 //              reporterr("\ni=%d\n####### leaf2node[nearest]= %d\n", i, leaf2node[nearest] );
12640
12641                 for( j=0; (m=topol[i][0][j])!=-1; j++ ) leaf2node[m] = i;
12642                 for( j=0; (m=topol[i][1][j])!=-1; j++ ) leaf2node[m] = i;
12643
12644 //              reporterr("####### leaf2node[nearest]= %d\n", leaf2node[nearest] );
12645
12646 //              im = topolc[posinnew][0][0];
12647 //              jm = topolc[posinnew][1][0];
12648 //              sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], lenc[posinnew][0], tree[jm], lenc[posinnew][1] );
12649 //              strcpy( tree[im], treetmp );
12650 //
12651 //              reporterr(       "%s\n", treetmp );
12652
12653                 posinnew++;
12654         }
12655
12656         if( nstep )
12657         {
12658                 i--;
12659                 topolo0 = topol[i][0];
12660                 topolo1 = topol[i][1];
12661         }
12662         else
12663         {
12664 //              i = 0;
12665 //              free( topol[i][0] );//?
12666 //              free( topol[i][1] );//?
12667 //              topol[i][0] = calloc( 2, sizeof( int ) );
12668 //              topol[i][1] = calloc( 1, sizeof( int ) );
12669 //              topol[i][0][0] = 0;
12670 //              topol[i][0][1] = -1;
12671 //              topol[i][1][0] = -1;
12672
12673                 topoldum0 = calloc( 2, sizeof( int ) );
12674                 topoldum1 = calloc( 1, sizeof( int ) );
12675                 topoldum0[0] = 0;
12676                 topoldum0[1] = -1;
12677                 topoldum1[0] = -1;
12678
12679                 topolo0 = topoldum0;
12680                 topolo1 = topoldum1;
12681         }
12682         if( repnorg == -1 )
12683         {
12684 //              topolc[posinnew][0] = (int *)realloc( topolc[posinnew][0], ( countmem( topol[i][0] ) + countmem( topol[i][1] ) + 1 ) * sizeof( int ) );
12685 //              intcpy( topolc[posinnew][0], topol[i][0] );
12686 //              intcat( topolc[posinnew][0], topol[i][1] );
12687                 topolc[posinnew][0] = (int *)realloc( topolc[posinnew][0], ( countmem( topolo0 ) + countmem( topolo1 ) + 1 ) * sizeof( int ) );
12688                 intcpy( topolc[posinnew][0], topolo0 );
12689                 intcat( topolc[posinnew][0], topolo1 );
12690 //              lenc[posinnew][0] = len[i][0] + len[i][1] - minscore / 2; // BUG!! 2014/06/07 ni hakken
12691                 if( nstep )
12692                         lenc[posinnew][0] = minscore / 2 - dep[nstep-1].distfromtip; // only when nstep>0, 2014/11/21
12693                 else
12694                         lenc[posinnew][0] = minscore / 2;
12695
12696 //              reporterr( "\ndep[nstep-1].distfromtip = %f\n", dep[nstep-1].distfromtip );
12697 //              reporterr( "lenc[][0] = %f\n", lenc[posinnew][0] );
12698
12699                 topolc[posinnew][1] = (int *)realloc( topolc[posinnew][1],  2  * sizeof( int ) );
12700                 intcpy( topolc[posinnew][1], additionaltopol );
12701                 lenc[posinnew][1] = minscore / 2;
12702
12703 //              neighbor = lastmem( topolc[posinnew][0] );
12704                 neighbor = norg-1;  // hakkirishita neighbor ga inai baai saigo ni hyouji
12705
12706                 if( treeout )
12707                 {
12708 #if 0
12709                         fp = fopen( "infile.tree", "a" ); // kyougou!!
12710                         if( fp == 0 )
12711                         {
12712                                 reporterr(       "File error!\n" );
12713                                 exit( 1 );
12714                         }
12715                         fprintf( fp, "\n" );
12716                         fprintf( fp, "%8d: %s\n", norg+iadd+1, name[norg+iadd] );
12717                         fprintf( fp, "          nearest sequence: %d\n", nearest + 1 );
12718                         fprintf( fp, "          cousin: " );
12719                         for( j=0; topolc[posinnew][0][j]!=-1; j++ )
12720                                 fprintf( fp, "%d ", topolc[posinnew][0][j]+1 );
12721                         fprintf( fp, "\n" );
12722                         fclose( fp );
12723 #else
12724                         addtree[iadd].nearest = nearesto;
12725                         addtree[iadd].dist1 = minscoreo;
12726                         addtree[iadd].dist2 = minscore;
12727                         neighborlist[0] = 0;
12728                         npt = neighborlist;
12729                         for( j=0; topolc[posinnew][0][j]!=-1; j++ )
12730                         {
12731                                 sprintf( npt, "%d ", topolc[posinnew][0][j]+1 );
12732                                 npt += strlen( npt );
12733                         }
12734                         addtree[iadd].neighbors = calloc( npt-neighborlist+1, sizeof( char ) );
12735                         strcpy( addtree[iadd].neighbors, neighborlist );
12736 #endif
12737                 }
12738
12739 //              reporterr(       "STEP %d\n", posinnew );
12740 //              for( j=0; topolc[posinnew][0][j]!=-1; j++ ) reporterr(       " %d", topolc[posinnew][0][j] );
12741 //              reporterr(       "\n len=%f", lenc[posinnew][0] );
12742 //              for( j=0; topolc[posinnew][1][j]!=-1; j++ ) reporterr(       " %d", topolc[posinnew][1][j] );
12743 //              reporterr(       "\n len=%f\n", lenc[posinnew][1] );
12744         }
12745
12746         if( topoldum0 ) free( topoldum0 );
12747         if( topoldum1 ) free( topoldum1 );
12748         free( leaf2node );
12749         free( additionaltopol );
12750         free( ac );
12751         free( acori );
12752         if( treeout ) free( neighborlist );
12753
12754 #if 0 //        create a newick tree for CHECK
12755         char **tree;
12756         char *treetmp;
12757         int im, jm;
12758
12759         treetmp = AllocateCharVec( njob*150 );
12760         tree = AllocateCharMtx( njob, njob*150 );
12761         for( i=0; i<njobc; i++ ) sprintf( tree[i], "%d", i+1 );
12762
12763         for( i=0; i<njobc-1; i++ )
12764         {
12765                 reporterr( "\nSTEP %d\n", i );
12766                 for( j=0; topolc[i][0][j]!=-1; j++ ) reporterr( " %d", topolc[i][0][j] );
12767                 reporterr( "\n len=%f\n", lenc[i][0] );
12768                 for( j=0; topolc[i][1][j]!=-1; j++ ) reporterr( " %d", topolc[i][1][j] );
12769                 reporterr( "\n len=%f\n", lenc[i][1] );
12770
12771                 im = topolc[i][0][0];
12772                 jm = topolc[i][1][0];
12773                 sprintf( treetmp, "(%s:%7.5f,%s:%7.5f)", tree[im], lenc[i][0], tree[jm], lenc[i][1] );
12774                 strcpy( tree[im], treetmp );
12775
12776         }
12777
12778         reporterr(       "%s\n", treetmp );
12779         FreeCharMtx( tree );
12780         free( treetmp );
12781 #endif
12782
12783         return( neighbor );
12784 }
12785
12786 #if 0
12787 int samemember( int *mem, int *cand )
12788 {
12789         int i, j;
12790
12791 #if 0
12792         reporterr(       "mem = " );
12793         for( i=0; mem[i]>-1; i++ )      reporterr(       "%d ", mem[i] );
12794         reporterr(       "\n" );
12795
12796         reporterr(       "cand = " );
12797         for( i=0; cand[i]>-1; i++ )     reporterr(       "%d ", cand[i] );
12798         reporterr(       "\n" );
12799 #endif
12800
12801         for( i=0, j=0; mem[i]>-1; )     
12802         {
12803                 if( mem[i++] != cand[j++] ) return( 0 );
12804         }
12805
12806         if( cand[j] == -1 )
12807         {
12808                 return( 1 );
12809         }
12810         else
12811         {
12812                 return( 0 );
12813         }
12814 }
12815 #else
12816 int samemember( int *mem, int *cand )
12817 {
12818         int i, j;
12819         int nm, nc;
12820
12821         nm = 0; for( i=0; mem[i]>-1; i++ ) nm++;
12822         nc = 0; for( i=0; cand[i]>-1; i++ ) nc++;
12823
12824         if( nm != nc ) return( 0 );
12825
12826         for( i=0; mem[i]>-1; i++ )      
12827         {
12828                 for( j=0; cand[j]>-1; j++ )
12829                         if( mem[i] == cand[j] ) break;
12830                 if( cand[j] == -1 ) return( 0 );
12831         }
12832
12833         if( mem[i] == -1 )
12834         {
12835 #if 0
12836                 reporterr(       "mem = " );
12837                 for( i=0; mem[i]>-1; i++ )      reporterr(       "%d ", mem[i] );
12838                 reporterr(       "\n" );
12839         
12840                 reporterr(       "cand = " );
12841                 for( i=0; cand[i]>-1; i++ )     reporterr(       "%d ", cand[i] );
12842                 reporterr(       "\n" );
12843 #endif
12844                 return( 1 );
12845         }
12846         else
12847         {
12848                 return( 0 );
12849         }
12850 }
12851 #endif
12852
12853 int samemembern( int *mem, int *cand, int nc )
12854 {
12855         int i, j;
12856         int nm;
12857
12858         nm = 0; 
12859         for( i=0; mem[i]>-1; i++ ) 
12860         {
12861                 nm++;
12862                 if( nm > nc ) return( 0 );
12863         }
12864
12865         if( nm != nc ) return( 0 );
12866
12867         for( i=0; mem[i]>-1; i++ )      
12868         {
12869                 for( j=0; j<nc; j++ )
12870                         if( mem[i] == cand[j] ) break;
12871                 if( j == nc ) return( 0 );
12872         }
12873
12874         if( mem[i] == -1 )
12875         {
12876 #if 0
12877                 reporterr(       "mem = " );
12878                 for( i=0; mem[i]>-1; i++ )      reporterr(       "%d ", mem[i] );
12879                 reporterr(       "\n" );
12880         
12881                 reporterr(       "cand = " );
12882                 for( i=0; cand[i]>-1; i++ )     reporterr(       "%d ", cand[i] );
12883                 reporterr(       "\n" );
12884 #endif
12885                 return( 1 );
12886         }
12887         else
12888         {
12889                 return( 0 );
12890         }
12891 }
12892
12893
12894 int includemember( int *mem, int *cand ) // mem in cand 
12895 {
12896         int i, j;
12897
12898 #if 0
12899         reporterr(       "mem = " );
12900         for( i=0; mem[i]>-1; i++ )      reporterr(       "%d ", mem[i] );
12901         reporterr(       "\n" );
12902
12903         reporterr(       "cand = " );
12904         for( i=0; cand[i]>-1; i++ )     reporterr(       "%d ", cand[i] );
12905         reporterr(       "\n" );
12906 #endif
12907
12908         for( i=0; mem[i]>-1; i++ )
12909         {
12910                 for( j=0; cand[j]>-1; j++ )
12911                         if( mem[i] == cand[j] ) break;
12912                 if( cand[j] == -1 ) return( 0 );
12913         }
12914 //      reporterr(       "INCLUDED! mem[0]=%d\n", mem[0] );
12915         return( 1 );
12916 }
12917
12918 int overlapmember( int *mem1, int *mem2 )
12919 {
12920         int i, j;
12921
12922         for( i=0; mem1[i]>-1; i++ )
12923                 for( j=0; mem2[j]>-1; j++ )
12924                         if( mem1[i] == mem2[j] ) return( 1 );
12925         return( 0 );
12926 }
12927 void gapcount( double *freq, char **seq, int nseq, double *eff, int lgth )
12928 {
12929         int i, j;
12930         double fr;
12931
12932 //      for( i=0; i<lgth; i++ ) freq[i] = 0.0;
12933 //      return;
12934
12935         for( i=0; i<lgth; i++ )
12936         {
12937                 fr = 0.0;
12938                 for( j=0; j<nseq; j++ )
12939                 {
12940                         if( seq[j][i] == '-' ) fr += eff[j];
12941                 }
12942                 freq[i] = fr;
12943 //              reporterr(       "freq[%d] = %f\n", i, freq[i] );
12944         }
12945 //      reporterr(       "\n" );
12946         return;
12947 }
12948
12949 void gapcountadd( double *freq, char **seq, int nseq, double *eff, int lgth )
12950 {
12951         int i;
12952         int j = nseq-1;
12953         double newfr = eff[j];
12954         double orifr = 1.0 - newfr;
12955
12956 //      for( i=0; i<lgth; i++ ) freq[i] = 0.0;
12957 //      return;
12958 //      for( i=0; i<nseq; i++ )
12959 //              reporterr( "%s\n", seq[i] );
12960
12961         for( i=0; i<lgth; i++ )
12962         {
12963 //              reporterr(       "freq[%d] = %f", i, freq[i] );
12964                 freq[i] = 1.0 - freq[i]; // modosu
12965                 freq[i] *= orifr;
12966
12967                 if( seq[j][i] == '-' ) freq[i] += newfr;
12968 //              reporterr(       "->         %f\n", i, freq[i] );
12969         }
12970 //      reporterr(       "\n" );
12971         return;
12972 }
12973 void gapcountf( double *freq, char **seq, int nseq, double *eff, int lgth )
12974 {
12975         int i, j;
12976         double fr;
12977
12978 //      for( i=0; i<lgth; i++ ) freq[i] = 0.0;
12979 //      return;
12980
12981         for( i=0; i<lgth; i++ )
12982         {
12983                 fr = 0.0;
12984                 for( j=0; j<nseq; j++ )
12985                 {
12986                         if( seq[j][i] == '-' ) fr += eff[j];
12987                 }
12988                 freq[i] = fr;
12989 //              reporterr(       "in gapcountf, freq[%d] = %f\n", i, freq[i] );
12990         }
12991 //      reporterr(       "\n" );
12992         return;
12993 }
12994
12995 void outgapcount( double *freq, int nseq, char *gappat, double *eff )
12996 {
12997         int j;
12998         double fr;
12999
13000         fr = 0.0;
13001         for( j=0; j<nseq; j++ )
13002         {
13003                 if( gappat[j] == '-' ) fr += eff[j];
13004         }
13005         *freq = fr;
13006         return;
13007 }
13008
13009 double dist2offset( double dist )
13010 {
13011         double val = dist * 0.5 - specificityconsideration; // dist ha 0..2 dakara
13012 //      double val = dist * 1.0 - specificityconsideration; // dist ha 0..2 dakara
13013         if( val > 0.0 ) val = 0.0;
13014         return val;
13015 }
13016
13017 void makedynamicmtx( double **out, double **in, double offset )
13018 {
13019         int i, j, ii, jj;
13020         double av;
13021  
13022         offset = dist2offset( offset * 2.0 ); // offset 0..1 -> 0..2
13023
13024 //      if( offset > 0.0 ) offset = 0.0;
13025 //      reporterr(       "dynamic offset = %f\n", offset );
13026
13027         for( i=0; i<nalphabets; i++ ) for( j=0; j<nalphabets; j++ )
13028         {
13029                 out[i][j] = in[i][j];
13030         }
13031         if( offset == 0.0 ) return;
13032
13033         for( i=0; i<nalphabets; i++ ) 
13034         {
13035                 ii = (int)amino[i];
13036                 if( ii == '-' ) continue; // text no toki arieru
13037                 for( j=0; j<nalphabets; j++ )
13038                 {
13039                         jj = (int)amino[j];
13040                         if( jj == '-' ) continue; // text no toki arieru
13041                         out[i][j] = in[i][j] + offset * 600;
13042 //                      reporterr(       "%c-%c: %f\n", ii, jj, out[i][j] );
13043                 }
13044         }
13045
13046 //      reporterr(       "offset = %f\n", offset );
13047 //      reporterr(       "out[W][W] = %f\n", out[amino_n['W']][amino_n['W']] );
13048 //      reporterr(       "out[A][A] = %f\n", out[amino_n['A']][amino_n['A']] );
13049
13050
13051         return;
13052
13053 // Taikaku youso no heikin ga 600 ni naruyouni re-scale.
13054 // Hitaikaku youso ga ookiku narisugi.
13055
13056         av = 0.0;
13057         for( i=0; i<nalphabets; i++ ) 
13058         {
13059                 if( ii == '-' ) continue; // text no toki arieru
13060                 av += out[i][i];
13061         }
13062         av /= (double)nalphabets;
13063
13064         for( i=0; i<nalphabets; i++ ) 
13065         {
13066                 if( amino[i] == '-' ) continue; // text no toki arieru
13067                 for( j=0; j<nalphabets; j++ )
13068                 {
13069                         if( amino[j] == '-' ) continue; // text no toki arieru
13070                         out[i][j] = out[i][j] * 600 / av;
13071                         reporterr(       "%c-%c: %f\n", amino[i], amino[j], out[i][j] );
13072                 }
13073         }
13074 }
13075 void FreeCommonIP()
13076 {
13077         if( commonIP ) FreeIntMtx( commonIP ); 
13078         commonIP = NULL;
13079         commonAlloc1 = 0;
13080         commonAlloc2 = 0;
13081 }
13082
13083 void makeskiptable( int n, int **skip, char **seq )
13084 {
13085         char *nogapseq;
13086         int nogaplen, alnlen;
13087         int i, j, posinseq, gaplen;
13088
13089         nogapseq = calloc( strlen( seq[0] )+1, sizeof( char ) );
13090         for( i=0; i<n; i++ )
13091         {
13092                 gappick0( nogapseq, seq[i] );
13093                 nogaplen = strlen( nogapseq );
13094                 alnlen = strlen( seq[i] );
13095                 skip[i] = calloc( nogaplen+1, sizeof( int ) );
13096
13097 //              reporterr( "%s\n", nogapseq );
13098
13099                 posinseq = 0;
13100                 gaplen = 0;
13101                 for( j=0; j<alnlen; j++ )
13102                 {
13103                         if( seq[i][j] == '-' )
13104                         {
13105                                 skip[i][posinseq]++;
13106                         }
13107                         else
13108                         {
13109                                 posinseq++;
13110                         }
13111                 }
13112 //              for( j=0; j<nogaplen+1; j++ )
13113 //                      reporterr( "%d ", skip[i][j] );
13114 //              reporterr( "\n" );
13115 //              exit( 1 );
13116         }
13117         free( nogapseq );
13118 }
13119
13120 int generatesubalignmentstable( int nseq, int ***tablept, int *nsubpt, int *maxmempt, int ***topol, double **len, double threshold )
13121 {
13122         int i, j, rep0, rep1, nmem, mem;
13123         double distfromtip0, distfromtip1;
13124         double *distfromtip;
13125         reporterr( "\n\n\n" );
13126
13127         *maxmempt = 0;
13128         *nsubpt = 0;
13129
13130         distfromtip = calloc( nseq, sizeof( double ) );
13131         for( i=0; i<nseq-1; i++ )
13132         {
13133 #if 0
13134                 reporterr( "STEP %d\n", i );
13135                 for( j=0; topol[i][0][j]!=-1; j++ )
13136                         reporterr( "%3d ", topol[i][0][j] );
13137                 reporterr( "\n" );
13138                 reporterr( "len=%f\n", len[i][0] );
13139 #endif
13140
13141                 rep0 = topol[i][0][0];
13142                 distfromtip0 = distfromtip[rep0];
13143                 distfromtip[rep0] += len[i][0];
13144 //              reporterr( "distfromtip[%d] = %f->%f\n", rep0, distfromtip0, distfromtip[rep0] );
13145
13146
13147 #if 0
13148                 for( j=0; topol[i][1][j]!=-1; j++ )
13149                         reporterr( "%3d ", topol[i][1][j] );
13150                 reporterr( "\n" );
13151                 reporterr( "len=%f\n", len[i][1] );
13152 #endif
13153
13154                 rep1 = topol[i][1][0];
13155                 distfromtip1 = distfromtip[rep1];
13156                 distfromtip[rep1] += len[i][1];
13157 //              reporterr( "distfromtip[%d] = %f->%f\n", rep1, distfromtip1, distfromtip[rep1] );
13158
13159                 if( topol[i][0][1] != -1 && distfromtip0 <= threshold && threshold < distfromtip[rep0] )
13160                 {
13161 //                      reporterr( "HIT 0!\n" );
13162                         *tablept = realloc( *tablept, sizeof( char * ) * (*nsubpt+2) );
13163                         for( j=0, nmem=0; (mem=topol[i][0][j])!=-1; j++ ) 
13164                                 nmem++;
13165 //                      reporterr( "allocating %d\n", nmem+1 );
13166                         (*tablept)[*nsubpt] = calloc( nmem+1, sizeof( int ) );
13167                         (*tablept)[*nsubpt+1] = NULL;
13168                         intcpy( (*tablept)[*nsubpt], topol[i][0] );
13169                         if( *maxmempt < nmem ) *maxmempt = nmem;
13170                         *nsubpt += 1;
13171                 }
13172
13173                 if( topol[i][1][1] != -1 && distfromtip1 <= threshold && threshold < distfromtip[rep1] )
13174                 {
13175 //                      reporterr( "HIT 1!\n" );
13176                         *tablept = realloc( *tablept, sizeof( char * ) * (*nsubpt+2) );
13177                         for( j=0, nmem=0; (mem=topol[i][1][j])!=-1; j++ )
13178                                 nmem++;
13179 //                      reporterr( "allocating %d\n", nmem+1 );
13180                         (*tablept)[*nsubpt] = calloc( nmem+1, sizeof( int ) );
13181                         (*tablept)[*nsubpt+1] = NULL;
13182                         intcpy( (*tablept)[*nsubpt], topol[i][1] );
13183                         if( *maxmempt < nmem ) *maxmempt = nmem;
13184                         *nsubpt += 1;
13185                 }
13186
13187         }
13188
13189         if( distfromtip[0] <= threshold ) 
13190         {
13191                 free( distfromtip );
13192                 return( 1 );
13193         }
13194
13195         free( distfromtip );
13196         return( 0 );
13197 }
13198
13199
13200
13201 double sumofpairsscore( int nseq, char **seq )
13202 {
13203         double v = 0;
13204         int i, j;
13205         for( i=1; i<nseq; i++ )
13206         {
13207                 for( j=0; j<i; j++ )
13208                 {
13209                         v += naivepairscore11( seq[i], seq[j], penalty ) / 600;
13210                 }
13211         }
13212 //      v /= ( (nseq-1) * nseq ) / 2;
13213         return( v );
13214 }
13215
13216 int commonsextet_p( int *table, int *pointt )
13217 {
13218         int value = 0;
13219         int tmp;
13220         int point;
13221         static TLS int *memo = NULL;
13222         static TLS int *ct = NULL;
13223         static TLS int *cp;
13224
13225         if( table == NULL )
13226         {
13227                 if( memo ) free( memo );
13228                 if( ct ) free( ct );
13229                 memo = NULL;
13230                 ct = NULL;
13231                 return( 0 );
13232         }
13233
13234         if( *pointt == -1 )
13235                 return( 0 );
13236
13237         if( !memo )
13238         {
13239                 memo = (int *)calloc( tsize, sizeof( int ) );
13240                 if( !memo ) ErrorExit( "Cannot allocate memo\n" );
13241                 ct = (int *)calloc( MIN( maxl, tsize )+1, sizeof( int ) ); // chuui!!
13242                 if( !ct ) ErrorExit( "Cannot allocate ct\n" );
13243         }
13244
13245         cp = ct;
13246         while( ( point = *pointt++ ) != END_OF_VEC )
13247         {
13248                 tmp = memo[point]++;
13249                 if( tmp < table[point] )
13250                         value++;
13251                 if( tmp == 0 ) *cp++ = point;
13252         }
13253         *cp = END_OF_VEC;
13254         
13255         cp =  ct;
13256         while( *cp != END_OF_VEC )
13257                 memo[*cp++] = 0;
13258
13259         return( value );
13260 }
13261
13262 double distcompact_msa( char *seq1, char *seq2, int *skiptable1, int *skiptable2, int ss1, int ss2 ) // osoi!
13263 {
13264         int bunbo = MIN( ss1, ss2 );
13265         double value;
13266
13267 //      reporterr( "msa-based dist\n" );
13268         if( bunbo == 0 )
13269                 return( 2.0 );
13270         else
13271         {
13272                 value = ( 1.0 - (double)naivepairscorefast( seq1, seq2, skiptable1, skiptable2, penalty_dist ) / bunbo ) * 2.0; // 2014/Aug/15 fast 
13273                 if( value > 10 ) value = 10.0;  // 2015/Mar/17
13274                 return( value );
13275         }
13276 }
13277
13278 double distcompact( int len1, int len2, int *table1, int *point2, int ss1, int ss2 )
13279 {
13280         double longer, shorter, lenfac, value;
13281
13282         if( len1 > len2 )
13283         {
13284                 longer=(double)len1;
13285                 shorter=(double)len2;
13286         }
13287         else
13288         {
13289                 longer=(double)len2;
13290                 shorter=(double)len1;
13291         }
13292         lenfac = 1.0 / ( shorter / longer * lenfacd + lenfacb / ( longer + lenfacc ) + lenfaca );
13293 //      reporterr( "lenfac=%f\n", lenfac );
13294 //      reporterr( "commonsextet_p()=%d\n", commonsextet_p( table1, point2 ) );
13295 //      reporterr( "ss1=%d, ss2=%d\n", ss1, ss2 );
13296 //      reporterr( "val=%f\n", (1.0-(double)commonsextet_p( table1, point2 )/ss1) );
13297
13298         if( ss1 == 0 || ss2 == 0 )
13299                 return( 2.0 );
13300
13301         value = ( 1.0 - (double)commonsextet_p( table1, point2 ) / MIN(ss1,ss2) ) * lenfac * 2.0;
13302
13303         return( value ); // 2013/Oct/17 -> 2bai
13304 }
13305
13306 static void movereg( char *seq1, char *seq2, LocalHom *tmpptr, int *start1pt, int *start2pt, int *end1pt, int *end2pt )
13307 {
13308         char *pt;
13309         int tmpint;
13310
13311         pt = seq1;
13312         tmpint = -1;
13313         while( *pt != 0 )
13314         {
13315                 if( *pt++ != '-' ) tmpint++;
13316                 if( tmpint == tmpptr->start1 ) break;
13317         }
13318         *start1pt = (int)( pt - seq1 ) - 1;
13319                 
13320         if( tmpptr->start1 == tmpptr->end1 ) *end1pt = *start1pt;
13321         else
13322         {
13323                 while( *pt != 0 )
13324                 {
13325 //                      fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
13326                         if( *pt++ != '-' ) tmpint++;
13327                         if( tmpint == tmpptr->end1 ) break;
13328                 }
13329                 *end1pt = (int)( pt - seq1 ) - 1;
13330         }
13331                 
13332         pt = seq2;
13333         tmpint = -1;
13334         while( *pt != 0 )
13335         {
13336                 if( *pt++ != '-' ) tmpint++;
13337                 if( tmpint == tmpptr->start2 ) break;
13338         }
13339         *start2pt = (int)( pt - seq2 ) - 1;
13340         if( tmpptr->start2 == tmpptr->end2 ) *end2pt = *start2pt;
13341         else
13342         {
13343                 while( *pt != 0 )
13344                 {
13345                         if( *pt++ != '-' ) tmpint++;
13346                         if( tmpint == tmpptr->end2 ) break;
13347                 }
13348                 *end2pt = (int)( pt - seq2 ) - 1;
13349         }
13350 }
13351
13352 static void movereg_swap( char *seq1, char *seq2, LocalHom *tmpptr, int *start1pt, int *start2pt, int *end1pt, int *end2pt )
13353 {
13354         char *pt;
13355         int tmpint;
13356
13357
13358         pt = seq1;
13359         tmpint = -1;
13360         while( *pt != 0 )
13361         {
13362                 if( *pt++ != '-' ) tmpint++;
13363                 if( tmpint == tmpptr->start2 ) break;
13364         }
13365         *start1pt = (int)( pt - seq1 ) - 1;
13366                 
13367         if( tmpptr->start2 == tmpptr->end2 ) *end1pt = *start1pt;
13368         else
13369         {
13370                 while( *pt != 0 )
13371                 {
13372 //                      fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
13373                         if( *pt++ != '-' ) tmpint++;
13374                         if( tmpint == tmpptr->end2 ) break;
13375                 }
13376                 *end1pt = (int)( pt - seq1 ) - 1;
13377         }
13378                 
13379         pt = seq2;
13380         tmpint = -1;
13381         while( *pt != 0 )
13382         {
13383                 if( *pt++ != '-' ) tmpint++;
13384                 if( tmpint == tmpptr->start1 ) break;
13385         }
13386         *start2pt = (int)( pt - seq2 ) - 1;
13387         if( tmpptr->start1 == tmpptr->end1 ) *end2pt = *start2pt;
13388         else
13389         {
13390                 while( *pt != 0 )
13391                 {
13392                         if( *pt++ != '-' ) tmpint++;
13393                         if( tmpint == tmpptr->end1 ) break;
13394                 }
13395                 *end2pt = (int)( pt - seq2 ) - 1;
13396         }
13397 }
13398
13399 void fillimp( double **impmtx, double *imp, int clus1, int clus2, int lgth1, int lgth2, char **seq1, char **seq2, double *eff1, double *eff2, double *eff1_kozo, double *eff2_kozo, LocalHom ***localhom, char *swaplist, int forscore, int *orinum1, int *orinum2 )
13400 {
13401         int i, j, k1, k2, start1, start2, end1, end2;
13402         double effij, effijx, effij_kozo; 
13403         char *pt1, *pt2;
13404         LocalHom *tmpptr;
13405         void (*movefunc)(char *, char *, LocalHom *, int *, int *, int *, int * );
13406
13407 #if 0
13408         fprintf( stderr, "eff1 in _init_strict = \n" );
13409         for( i=0; i<clus1; i++ )
13410                 fprintf( stderr, "eff1[] = %f\n", eff1[i] );
13411         for( i=0; i<clus2; i++ )
13412                 fprintf( stderr, "eff2[] = %f\n", eff2[i] );
13413 #endif
13414
13415         for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
13416                 impmtx[i][j] = 0.0;
13417         effijx = 1.0 * fastathreshold;
13418         for( i=0; i<clus1; i++ )
13419         {
13420                 if( swaplist && swaplist[i] ) movefunc = movereg_swap;
13421                 else movefunc = movereg;
13422                 for( j=0; j<clus2; j++ )
13423                 {
13424
13425                         if( swaplist == NULL && orinum1 && orinum2 ) // muda. 
13426                         {
13427                                 if( orinum1[i]>orinum2[j] )
13428                                         movefunc = movereg_swap;
13429                                 else
13430                                         movefunc = movereg;
13431                         }
13432
13433 //                      effij = eff1[i] * eff2[j] * effijx;
13434                         effij = eff1[i] * eff2[j] * effijx;
13435                         effij_kozo = eff1_kozo[i] * eff2_kozo[j] * effijx;
13436                         tmpptr = localhom[i][j];
13437                         while( tmpptr )
13438                         {
13439 //                              fprintf( stderr, "start1 = %d\n", tmpptr->start1 );
13440 //                              fprintf( stderr, "end1   = %d\n", tmpptr->end1   );
13441 //                              fprintf( stderr, "i = %d, seq1 = \n%s\n", i, seq1[i] );
13442 //                              fprintf( stderr, "j = %d, seq2 = \n%s\n", j, seq2[j] );
13443
13444                                 movefunc( seq1[i], seq2[j], tmpptr, &start1, &start2, &end1, &end2 );
13445
13446
13447 //                              fprintf( stderr, "start1 = %d (%c), end1 = %d (%c), start2 = %d (%c), end2 = %d (%c)\n", start1, seq1[i][start1], end1, seq1[i][end1], start2, seq2[j][start2], end2, seq2[j][end2] );
13448 //                              fprintf( stderr, "step 0\n" );
13449                                 if( end1 - start1 != end2 - start2 )
13450                                 {
13451 //                                      fprintf( stderr, "CHUUI!!, start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
13452                                 }
13453
13454                                 k1 = start1; k2 = start2;
13455                                 pt1 = seq1[i] + k1;
13456                                 pt2 = seq2[j] + k2;
13457                                 while( *pt1 && *pt2 )
13458                                 {
13459                                         if( *pt1 != '-' && *pt2 != '-' )
13460                                         {
13461 // ½Å¤ß¤òÆó½Å¤Ë¤«¤±¤Ê¤¤¤è¤¦¤ËÃí°Õ¤·¤Æ²¼¤µ¤¤¡£
13462 //                                              impmtx[k1][k2] += tmpptr->wimportance * fastathreshold;
13463 //                                              impmtx[k1][k2] += tmpptr->importance * effij;
13464 //                                              impmtx[k1][k2] += tmpptr->fimportance * effij;
13465                                                 if( tmpptr->korh == 'k' )
13466                                                         impmtx[k1][k2] += tmpptr->importance * effij_kozo;
13467                                                 else
13468                                                         impmtx[k1][k2] += tmpptr->importance * effij;
13469 //                                              fprintf( stderr, "k1=%d, k2=%d, impalloclen=%d\n", k1, k2, impalloclen );
13470 //                                              fprintf( stderr, "mark, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
13471                                                 k1++; k2++;
13472                                                 pt1++; pt2++;
13473                                         }
13474                                         else if( *pt1 != '-' && *pt2 == '-' )
13475                                         {
13476 //                                              fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
13477                                                 k2++; pt2++;
13478                                         }
13479                                         else if( *pt1 == '-' && *pt2 != '-' )
13480                                         {
13481 //                                              fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
13482                                                 k1++; pt1++;
13483                                         }
13484                                         else if( *pt1 == '-' && *pt2 == '-' )
13485                                         {
13486 //                                              fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
13487                                                 k1++; pt1++;
13488                                                 k2++; pt2++;
13489                                         }
13490                                         if( k1 > end1 || k2 > end2 ) break;
13491                                 }
13492                                 tmpptr = tmpptr->next;
13493                         }
13494                 }
13495         }
13496 #if 0
13497         printf( "orinum1=%d, orinum2=%d\n", *orinum1, *orinum2 );
13498         if( *orinum1 == 0 )
13499         {
13500                 fprintf( stdout, "impmtx = \n" );
13501                 for( k2=0; k2<lgth2; k2++ )
13502                         fprintf( stdout, "%6.3f ", (double)k2 );
13503                 fprintf( stdout, "\n" );
13504                 for( k1=0; k1<lgth1; k1++ )
13505                 {
13506                         fprintf( stdout, "%d", k1 );
13507                         for( k2=0; k2<lgth2; k2++ )
13508                                 fprintf( stdout, "%2.1f ", impmtx[k1][k2] );
13509                         fprintf( stdout, "\n" );
13510                 }
13511                 exit( 1 );
13512         }
13513 #endif
13514 }