1 #include "mltaln.h"
2 #include "dp.h"
3 
4 #define DEBUG 0
5 
6 #define WMCHECK 1
7 #define ALGZSTRAIGHT 0
8 #define ALGZGAP 0
9 #define USEGAPLENMTX 0
10 #define USEGAPLENHALF 0
11 #define FREEFREQUENTLY 1
12 
13 #define IDATEND 0
14 
15 
16 #define MACHIGAI 0
17 #define OUTGAP0TRY 0
18 #define XXXXXXX    0
19 #define USE_PENALTY_EX  0
20 #define FASTMATCHCALC 1
21 #define SLOW 0
22 
23 #define zero 0
24 #define one 1
25 
26 #if USEGAPLENHALF
27 #define USEGAPLENHALFORMTX 1
28 #endif
29 #if USEGAPLENMTX
30 #define USEGAPLENHALFORMTX 1
31 #endif
32 
33 
34 #if WMCHECK
35 static int PFACERROR = 0;
36 #endif
37 
38 
39 static TLS double **impmtx = NULL;
40 static TLS int impalloclen = 0;
41 
imp_match_out_scD(int i1,int j1)42 double imp_match_out_scD( int i1, int j1 )
43 {
44 //	fprintf( stderr, "imp+match = %f\n", impmtx[i1][j1] * fastathreshold );
45 //	fprintf( stderr, "val = %f\n", impmtx[i1][j1] );
46 	return( impmtx[i1][j1] );
47 }
48 
49 typedef struct _gaplenvec
50 {
51 	int relend;
52 #if USEGAPLENHALFORMTX
53 	int idatend;
54 #endif
55 	int idatnext;
56 	int idatprev;
57 	int npat;
58 	int len; // sukoshi muda.
59 	double freq;
60 } Gaplen;
61 
62 #if 0
63 static void imp_match_out_vead_gapmap( double *imp, int i1, int lgth2, int *gapmap2 )
64 {
65 #if FASTMATCHCALC
66 	double *pt = impmtx[i1];
67 	int *gapmappt = gapmap2;
68 	while( lgth2-- )
69 		*imp++ += pt[*gapmappt++];
70 #else
71 	int j;
72 	double *pt = impmtx[i1];
73 	for( j=0; j<lgth2; j++ )
74 		*imp++ += pt[gapmap2[j]];
75 #endif
76 }
77 
78 
79 static void imp_match_out_vead_tate_gapmap( double *imp, int j1, int lgth1, int *gapmap1 )
80 {
81 #if FASTMATCHCALC
82 	int *gapmappt = gapmap1;
83 	while( lgth1-- )
84 		*imp++ += impmtx[*gapmappt++][j1];
85 #else
86 	int i;
87 	for( i=0; i<lgth1; i++ )
88 		*imp++ += impmtx[gapmap1[i]][j1];
89 #endif
90 }
91 #endif
92 
imp_match_out_vead(double * imp,int i1,int lgth2)93 static void imp_match_out_vead( double *imp, int i1, int lgth2 )
94 {
95 #if FASTMATCHCALC
96 	double *pt = impmtx[i1];
97 	while( lgth2-- )
98 		*imp++ += *pt++;
99 #else
100 	int j;
101 	double *pt = impmtx[i1];
102 	for( j=0; j<lgth2; j++ )
103 		*imp++ += pt[j];
104 #endif
105 }
imp_match_out_vead_tate(double * imp,int j1,int lgth1)106 static void imp_match_out_vead_tate( double *imp, int j1, int lgth1 )
107 {
108 	int i;
109 	for( i=0; i<lgth1; i++ )
110 		*imp++ += impmtx[i][j1];
111 }
112 
imp_rnaD(int nseq1,int nseq2,char ** seq1,char ** seq2,double * eff1,double * eff2,RNApair *** grouprna1,RNApair *** grouprna2,int * gapmap1,int * gapmap2,RNApair * pair)113 void imp_rnaD( int nseq1, int nseq2, char **seq1, char **seq2, double *eff1, double *eff2, RNApair ***grouprna1, RNApair ***grouprna2, int *gapmap1, int *gapmap2, RNApair *pair )
114 {
115 	foldrna( nseq1, nseq2, seq1, seq2, eff1, eff2, grouprna1, grouprna2, impmtx, gapmap1, gapmap2, pair );
116 }
117 
imp_match_init_strictD(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,int forscore)118 void imp_match_init_strictD( 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, int forscore )
119 {
120 	int i, j, k1, k2, tmpint, start1, start2, end1, end2;
121 	double effij;
122 	double effij_kozo;
123 	double effijx;
124 	char *pt, *pt1, *pt2;
125 	static TLS char *nocount1 = NULL;
126 	static TLS char *nocount2 = NULL;
127 	LocalHom *tmpptr;
128 
129 
130 	if( seq1 == NULL )
131 	{
132 		if( impmtx ) FreeFloatMtx( impmtx );
133 		impmtx = NULL;
134 		if( nocount1 ) free( nocount1 );
135 		nocount1 = NULL;
136 		if( nocount2 ) free( nocount2 );
137 		nocount2 = NULL;
138 
139 		return;
140 	}
141 
142 	if( impalloclen < lgth1 + 2 || impalloclen < lgth2 + 2 )
143 	{
144 		if( impmtx ) FreeFloatMtx( impmtx );
145 		if( nocount1 ) free( nocount1 );
146 		if( nocount2 ) free( nocount2 );
147 		impalloclen = MAX( lgth1, lgth2 ) + 2;
148 		impmtx = AllocateFloatMtx( impalloclen, impalloclen );
149 		nocount1 = AllocateCharVec( impalloclen );
150 		nocount2 = AllocateCharVec( impalloclen );
151 	}
152 
153 	for( i=0; i<lgth1; i++ )
154 	{
155 		for( j=0; j<clus1; j++ )
156 			if( seq1[j][i] == '-' ) break;
157 		if( j != clus1 ) nocount1[i] = 1;
158 		else			 nocount1[i] = 0;
159 	}
160 	for( i=0; i<lgth2; i++ )
161 	{
162 		for( j=0; j<clus2; j++ )
163 			if( seq2[j][i] == '-' ) break;
164 		if( j != clus2 ) nocount2[i] = 1;
165 		else			 nocount2[i] = 0;
166 	}
167 
168 #if 0
169 fprintf( stderr, "nocount2 =\n" );
170 for( i = 0; i<impalloclen; i++ )
171 {
172 	fprintf( stderr, "nocount2[%d] = %d (%c)\n", i, nocount2[i], seq2[0][i] );
173 }
174 #endif
175 
176 
177 
178 #if 0
179 	fprintf( stderr, "eff1 in _init_strict = \n" );
180 	for( i=0; i<clus1; i++ )
181 		fprintf( stderr, "eff1[] = %f\n", eff1[i] );
182 	for( i=0; i<clus2; i++ )
183 		fprintf( stderr, "eff2[] = %f\n", eff2[i] );
184 #endif
185 
186 	for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
187 		impmtx[i][j] = 0.0;
188 	effijx =  fastathreshold;
189 	for( i=0; i<clus1; i++ )
190 	{
191 		for( j=0; j<clus2; j++ )
192 		{
193 			effij = (double)( eff1[i] * eff2[j] * effijx );
194 			effij_kozo = (double)( eff1_kozo[i] * eff2_kozo[j] * effijx );
195 			tmpptr = localhom[i][j];
196 			while( tmpptr )
197 			{
198 //				fprintf( stderr, "start1 = %d\n", tmpptr->start1 );
199 //				fprintf( stderr, "end1   = %d\n", tmpptr->end1   );
200 //				fprintf( stderr, "i = %d, seq1 = \n%s\n", i, seq1[i] );
201 //				fprintf( stderr, "j = %d, seq2 = \n%s\n", j, seq2[j] );
202 				pt = seq1[i];
203 				tmpint = -1;
204 				while( *pt != 0 )
205 				{
206 					if( *pt++ != '-' ) tmpint++;
207 					if( tmpint == tmpptr->start1 ) break;
208 				}
209 				start1 = pt - seq1[i] - 1;
210 
211 				if( tmpptr->start1 == tmpptr->end1 ) end1 = start1;
212 				else
213 				{
214 #if MACHIGAI
215 					while( *pt != 0 )
216 					{
217 //						fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
218 						if( tmpint == tmpptr->end1 ) break;
219 						if( *pt++ != '-' ) tmpint++;
220 					}
221 					end1 = pt - seq1[i] - 0;
222 #else
223 					while( *pt != 0 )
224 					{
225 //						fprintf( stderr, "tmpint = %d, end1 = %d pos = %d\n", tmpint, tmpptr->end1, pt-seq1[i] );
226 						if( *pt++ != '-' ) tmpint++;
227 						if( tmpint == tmpptr->end1 ) break;
228 					}
229 					end1 = pt - seq1[i] - 1;
230 #endif
231 				}
232 
233 				pt = seq2[j];
234 				tmpint = -1;
235 				while( *pt != 0 )
236 				{
237 					if( *pt++ != '-' ) tmpint++;
238 					if( tmpint == tmpptr->start2 ) break;
239 				}
240 				start2 = pt - seq2[j] - 1;
241 				if( tmpptr->start2 == tmpptr->end2 ) end2 = start2;
242 				else
243 				{
244 #if MACHIGAI
245 					while( *pt != 0 )
246 					{
247 						if( tmpint == tmpptr->end2 ) break;
248 						if( *pt++ != '-' ) tmpint++;
249 					}
250 					end2 = pt - seq2[j] - 0;
251 #else
252 					while( *pt != 0 )
253 					{
254 						if( *pt++ != '-' ) tmpint++;
255 						if( tmpint == tmpptr->end2 ) break;
256 					}
257 					end2 = pt - seq2[j] - 1;
258 #endif
259 				}
260 //				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] );
261 //				fprintf( stderr, "step 0\n" );
262 				if( end1 - start1 != end2 - start2 )
263 				{
264 //					fprintf( stderr, "CHUUI!!, start1 = %d, end1 = %d, start2 = %d, end2 = %d\n", start1, end1, start2, end2 );
265 				}
266 
267 #if 1
268 				k1 = start1; k2 = start2;
269 				pt1 = seq1[i] + k1;
270 				pt2 = seq2[j] + k2;
271 				while( *pt1 && *pt2 )
272 				{
273 					if( *pt1 != '-' && *pt2 != '-' )
274 					{
275 // �Ťߤ���Ťˤ����ʤ��褦����դ��Ʋ�������
276 //						impmtx[k1][k2] += tmpptr->wimportance * fastathreshold;
277 //						impmtx[k1][k2] += tmpptr->importance * effij;
278 //						impmtx[k1][k2] += tmpptr->fimportance * effij;
279 						if( tmpptr->korh == 'k' )
280 							impmtx[k1][k2] += tmpptr->fimportance * effij_kozo;
281 						else
282 							impmtx[k1][k2] += tmpptr->fimportance * effij;
283 
284 //						fprintf( stderr, "#### impmtx[k1][k2] = %f, tmpptr->fimportance=%f, effij=%f\n", impmtx[k1][k2], tmpptr->fimportance, effij );
285 //						fprintf( stderr, "mark, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
286 //						fprintf( stderr, "%d (%c) - %d (%c)  - %f\n", k1, *pt1, k2, *pt2, tmpptr->fimportance * effij );
287 						k1++; k2++;
288 						pt1++; pt2++;
289 					}
290 					else if( *pt1 != '-' && *pt2 == '-' )
291 					{
292 //						fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
293 						k2++; pt2++;
294 					}
295 					else if( *pt1 == '-' && *pt2 != '-' )
296 					{
297 //						fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
298 						k1++; pt1++;
299 					}
300 					else if( *pt1 == '-' && *pt2 == '-' )
301 					{
302 //						fprintf( stderr, "skip, %d (%c) - %d (%c) \n", k1, *pt1, k2, *pt2 );
303 						k1++; pt1++;
304 						k2++; pt2++;
305 					}
306 					if( k1 > end1 || k2 > end2 ) break;
307 				}
308 #else
309 				while( k1 <= end1 && k2 <= end2 )
310 				{
311 					fprintf( stderr, "k1,k2=%d,%d - ", k1, k2 );
312 					if( !nocount1[k1] && !nocount2[k2] )
313 					{
314 						impmtx[k1][k2] += tmpptr->wimportance * eff1[i] * eff2[j]  * fastathreshold;
315 						fprintf( stderr, "marked\n" );
316 					}
317 					else
318 						fprintf( stderr, "no count\n" );
319 					k1++; k2++;
320 				}
321 #endif
322 				tmpptr = tmpptr->next;
323 			}
324 		}
325 	}
326 
327 #if 0
328 //	if( clus1 == 1 && clus2 == 1 )
329 	reporterr( "In imp_match_init_strictD(), clus1=%d, clus2=%d\n", clus1, clus2 );
330 	{
331 		fprintf( stderr, "writing impmtx\n" );
332 		fprintf( stderr, "\n" );
333 		fprintf( stderr, "seq1[0] =  %s\n", seq1[0] );
334 		fprintf( stderr, "seq2[0] =  %s\n", seq2[0] );
335 		fprintf( stderr, "impmtx = \n" );
336 		for( k2=0; k2<lgth2; k2++ )
337 			fprintf( stderr, "%6.3f ", (double)k2 );
338 		fprintf( stderr, "\n" );
339 		for( k1=0; k1<lgth1; k1++ )
340 		{
341 			fprintf( stderr, "%d ", k1 );
342 			for( k2=0; k2<30; k2++ )
343 				fprintf( stderr, "%2.1f ", impmtx[k1][k2] );
344 			fprintf( stderr, "\n" );
345 		}
346 //		exit( 1 );
347 	}
348 #endif
349 }
350 
351 
352 
match_calc_del(int ** which,double *** matrices,double * match,int n1,char ** seq1,double * eff1,int n2,char ** seq2,double * eff2,int i1,int lgth2,int mid,int nmask,int * mask1,int * mask2)353 static void match_calc_del( int **which, double ***matrices, double *match, int n1, char **seq1, double *eff1, int n2, char **seq2, double *eff2, int i1, int lgth2, int mid, int nmask, int *mask1, int *mask2 )
354 {
355 // osoi!
356 	int i, j, k, m;
357 	int c1, c2;
358 //	fprintf( stderr, "\nmatch_calc_dynamicmtx... %d", i1 );
359 //	fprintf( stderr, "\nseq1[0]=%s\n", seq1[0] );
360 //	fprintf( stderr, "\nseq2[0]=%s\n", seq2[0] );
361 //	for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
362 //	{
363 //		if( flip ) reporterr( "in match_calc_slow, which[%d][%d] = %d\n", j, i, which[j][i] );
364 //		else       reporterr( "in match_calc_slow, which[%d][%d] = %d\n", i, j, which[i][j] );
365 //	}
366 	for( k=0; k<lgth2; k++ )
367 	{
368 		for( m=0; m<nmask; m++ )
369 		{
370 			i = mask1[m];
371 			j = mask2[m];
372 //			reporterr( "Deleting %d-%d (c=%d)\n", i, j, mid );
373 //			if( k==0 ) fprintf( stderr, "pairoffset[%d][%d] = %f\n", i, j, po );
374 			c1 = amino_n[(int)seq1[i][i1]];
375 			c2 = amino_n[(int)seq2[j][k]];
376 //			reporterr( "k=%d, c1=%d, c2=%d, seq1[i][i1]=%c, seq2[%d][%d]=%c\n", k, c1, c2, seq1[i][i1], j, k, seq2[j][k] );
377 			if( seq1[i][i1] == '-' || seq2[j][k] == '-' ) continue;
378 			if( c1 < 0 || c2 < 0 ) continue;
379 //			fprintf( stderr, "c1=%d, c2=%d\n", c1, c2 );
380 //			fprintf( stderr, "match[k] = %f -> ", match[k], mid );
381 			match[k] -= matrices[mid][c1][c2] * eff1[i] * eff2[j];
382 //			fprintf( stderr, "match[k] = %f (mid=%d)\n", match[k], mid );
383 		}
384 	}
385 //	fprintf( stderr, "done\n" );
386 	return;
387 }
388 
389 #if SLOW
match_calc_slow(int ** which,double *** matrices,double * match,int n1,char ** seq1,double * eff1,int n2,char ** seq2,double * eff2,int i1,int lgth2,double ** doublework,int ** intwork,int initialize,int flip)390 static void match_calc_slow( int **which, double ***matrices, double *match, int n1, char **seq1, double *eff1, int n2, char **seq2, double *eff2, int i1, int lgth2, double **doublework, int **intwork, int initialize, int flip )
391 {
392 // osoi!
393 	int i, j, k;
394 	int c1, c2;
395 	int mid;
396 //	fprintf( stderr, "\nmatch_calc_dynamicmtx... %d", i1 );
397 //	fprintf( stderr, "\nseq1[0]=%s\n", seq1[0] );
398 //	fprintf( stderr, "\nseq2[0]=%s\n", seq2[0] );
399 //	for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
400 //	{
401 //		if( flip ) reporterr( "in match_calc_slow, which[%d][%d] = %d\n", j, i, which[j][i] );
402 //		else       reporterr( "in match_calc_slow, which[%d][%d] = %d\n", i, j, which[i][j] );
403 //	}
404 	for( k=0; k<lgth2; k++ )
405 	{
406 		match[k] = 0.0;
407 		for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
408 		{
409 			if( flip ) mid = which[j][i];
410 			else       mid = which[i][j];
411 //			if( k==0 ) fprintf( stderr, "pairoffset[%d][%d] = %f\n", i, j, po );
412 			c1 = amino_n[(int)seq1[i][i1]];
413 			c2 = amino_n[(int)seq2[j][k]];
414 			if( seq1[i][i1] == '-' || seq2[j][k] == '-' ) continue;
415 			if( c1 < 0 || c2 < 0 ) continue;
416 //			fprintf( stderr, "c1=%d, c2=%d\n", c1, c2 );
417 			if( flip )
418 				match[k] += matrices[mid][c1][c2] * eff1[i] * eff2[j];
419 			else
420 				match[k] += matrices[mid][c1][c2] * eff1[i] * eff2[j];
421 //			fprintf( stderr, "match[k] = %f (which=%d)\n", match[k], mid );
422 		}
423 	}
424 //	fprintf( stderr, "done\n" );
425 	return;
426 }
427 #endif
428 
fillzero(double * s,int l)429 static void fillzero( double *s, int l )
430 {
431 	while( l-- ) *s++ = 0.0;
432 }
433 
434 
match_calc_add(double ** scoreingmtx,double * match,double ** cpmx1,double ** cpmx2,int i1,int lgth2,double ** doublework,int ** intwork,int initialize)435 static void match_calc_add( double **scoreingmtx, double *match, double **cpmx1, double **cpmx2, int i1, int lgth2, double **doublework, int **intwork, int initialize )
436 {
437 #if FASTMATCHCALC
438 //	fprintf( stderr, "\nmatch_calc... %d", i1 );
439 	int j, l;
440 //	double scarr[26];
441 	double **cpmxpd = doublework;
442 	int **cpmxpdn = intwork;
443 	double *matchpt, *cpmxpdpt, **cpmxpdptpt;
444 	int *cpmxpdnpt, **cpmxpdnptpt;
445 	double *scarr;
446 	scarr = calloc( nalphabets, sizeof( double ) );
447 	if( initialize )
448 	{
449 		int count = 0;
450 		for( j=0; j<lgth2; j++ )
451 		{
452 			count = 0;
453 			for( l=0; l<nalphabets; l++ )
454 			{
455 				if( cpmx2[l][j] )
456 				{
457 					cpmxpd[j][count] = cpmx2[l][j];
458 					cpmxpdn[j][count] = l;
459 					count++;
460 				}
461 			}
462 			cpmxpdn[j][count] = -1;
463 		}
464 	}
465 
466 	{
467 		for( l=0; l<nalphabets; l++ )
468 		{
469 			scarr[l] = 0.0;
470 			for( j=0; j<nalphabets; j++ )
471 //				scarr[l] += n_dis[j][l] * cpmx1[j][i1];
472 //				scarr[l] += n_dis_consweight_multi[j][l] * cpmx1[j][i1];
473 				scarr[l] += scoreingmtx[j][l] * cpmx1[j][i1];
474 		}
475 		matchpt = match;
476 		cpmxpdnptpt = cpmxpdn;
477 		cpmxpdptpt = cpmxpd;
478 		while( lgth2-- )
479 		{
480 //			*matchpt = 0.0;
481 			cpmxpdnpt = *cpmxpdnptpt++;
482 			cpmxpdpt = *cpmxpdptpt++;
483 			while( *cpmxpdnpt>-1 )
484 				*matchpt += scarr[*cpmxpdnpt++] * *cpmxpdpt++;
485 			matchpt++;
486 		}
487 	}
488 	free( scarr );
489 //	fprintf( stderr, "done\n" );
490 #else
491 	int j, k, l;
492 //	double scarr[26];
493 	double **cpmxpd = doublework;
494 	int **cpmxpdn = intwork;
495 	double *scarr;
496 	scarr = calloc( nalphabets, sizeof( double ) );
497 // simple
498 	if( initialize )
499 	{
500 		int count = 0;
501 		for( j=0; j<lgth2; j++ )
502 		{
503 			count = 0;
504 			for( l=0; l<nalphabets; l++ )
505 			{
506 				if( cpmx2[l][j] )
507 				{
508 					cpmxpd[count][j] = cpmx2[l][j];
509 					cpmxpdn[count][j] = l;
510 					count++;
511 				}
512 			}
513 			cpmxpdn[count][j] = -1;
514 		}
515 	}
516 	for( l=0; l<nalphabets; l++ )
517 	{
518 		scarr[l] = 0.0;
519 		for( k=0; k<nalphabets; k++ )
520 //			scarr[l] += n_dis[k][l] * cpmx1[k][i1];
521 //			scarr[l] += n_dis_consweight_multi[k][l] * cpmx1[k][i1];
522 			scarr[l] += scoreingmtx[k][l] * cpmx1[k][i1];
523 	}
524 	for( j=0; j<lgth2; j++ )
525 	{
526 		match[j] = 0.0;
527 		for( k=0; cpmxpdn[k][j]>-1; k++ )
528 			match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
529 	}
530 	free( scarr );
531 #endif
532 }
533 
match_calc(double ** n_dynamicmtx,double * match,double ** cpmx1,double ** cpmx2,int i1,int lgth2,double ** doublework,int ** intwork,int initialize)534 static void match_calc( double **n_dynamicmtx, double *match, double **cpmx1, double **cpmx2, int i1, int lgth2, double **doublework, int **intwork, int initialize )
535 {
536 #if FASTMATCHCALC
537 //	fprintf( stderr, "\nmatch_calc... %d", i1 );
538 	int j, l;
539 //	double scarr[26];
540 	double **cpmxpd = doublework;
541 	int **cpmxpdn = intwork;
542 	double *matchpt, *cpmxpdpt, **cpmxpdptpt;
543 	int *cpmxpdnpt, **cpmxpdnptpt;
544 	double *scarr;
545 	scarr = calloc( nalphabets, sizeof( double ) );
546 	if( initialize )
547 	{
548 		int count = 0;
549 		for( j=0; j<lgth2; j++ )
550 		{
551 			count = 0;
552 			for( l=0; l<nalphabets; l++ )
553 			{
554 				if( cpmx2[l][j] )
555 				{
556 					cpmxpd[j][count] = cpmx2[l][j];
557 					cpmxpdn[j][count] = l;
558 					count++;
559 				}
560 			}
561 			cpmxpdn[j][count] = -1;
562 		}
563 	}
564 
565 	{
566 		for( l=0; l<nalphabets; l++ )
567 		{
568 			scarr[l] = 0.0;
569 			for( j=0; j<nalphabets; j++ )
570 //				scarr[l] += n_dis[j][l] * cpmx1[j][i1];
571 //				scarr[l] += n_dis_consweight_multi[j][l] * cpmx1[j][i1];
572 				scarr[l] += n_dynamicmtx[j][l] * cpmx1[j][i1];
573 		}
574 		matchpt = match;
575 		cpmxpdnptpt = cpmxpdn;
576 		cpmxpdptpt = cpmxpd;
577 		while( lgth2-- )
578 		{
579 			*matchpt = 0.0;
580 			cpmxpdnpt = *cpmxpdnptpt++;
581 			cpmxpdpt = *cpmxpdptpt++;
582 			while( *cpmxpdnpt>-1 )
583 				*matchpt += scarr[*cpmxpdnpt++] * *cpmxpdpt++;
584 			matchpt++;
585 		}
586 	}
587 	free( scarr );
588 //	fprintf( stderr, "done\n" );
589 #else
590 	int j, k, l;
591 //	double scarr[26];
592 	double **cpmxpd = doublework;
593 	int **cpmxpdn = intwork;
594 	double *scarr;
595 	scarr = calloc( nalphabets, sizeof( double ) );
596 // simple
597 	if( initialize )
598 	{
599 		int count = 0;
600 		for( j=0; j<lgth2; j++ )
601 		{
602 			count = 0;
603 			for( l=0; l<nalphabets; l++ )
604 			{
605 				if( cpmx2[l][j] )
606 				{
607 					cpmxpd[count][j] = cpmx2[l][j];
608 					cpmxpdn[count][j] = l;
609 					count++;
610 				}
611 			}
612 			cpmxpdn[count][j] = -1;
613 		}
614 	}
615 	for( l=0; l<nalphabets; l++ )
616 	{
617 		scarr[l] = 0.0;
618 		for( k=0; k<nalphabets; k++ )
619 //			scarr[l] += n_dis[k][l] * cpmx1[k][i1];
620 //			scarr[l] += n_dis_consweight_multi[k][l] * cpmx1[k][i1];
621 			scarr[l] += n_dynamicmtx[k][l] * cpmx1[k][i1];
622 	}
623 	for( j=0; j<lgth2; j++ )
624 	{
625 		match[j] = 0.0;
626 		for( k=0; cpmxpdn[k][j]>-1; k++ )
627 			match[j] += scarr[cpmxpdn[k][j]] * cpmxpd[k][j];
628 	}
629 	free( scarr );
630 #endif
631 }
632 
Atracking_localhom(double * impwmpt,double * lasthorizontalw,double * lastverticalw,char ** seq1,char ** seq2,char ** mseq1,char ** mseq2,int ** ijp,int icyc,int jcyc,int * warpis,int * warpjs,int warpbase)633 static void Atracking_localhom( double *impwmpt, double *lasthorizontalw, double *lastverticalw,
634 						char **seq1, char **seq2,
635                         char **mseq1, char **mseq2,
636                         int **ijp, int icyc, int jcyc,
637 						int *warpis, int *warpjs, int warpbase )
638 {
639 	int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k, limk;
640 	double wm;
641 	char *gaptable1, *gt1bk;
642 	char *gaptable2, *gt2bk;
643 	lgth1 = strlen( seq1[0] );
644 	lgth2 = strlen( seq2[0] );
645 	gt1bk = AllocateCharVec( lgth1+lgth2+1 );
646 	gt2bk = AllocateCharVec( lgth1+lgth2+1 );
647 
648 #if 0
649 	for( i=0; i<lgth1; i++ )
650 	{
651 		fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
652 	}
653 #endif
654 
655 	if( outgap == 1 )
656 		;
657 	else
658 	{
659 		wm = lastverticalw[0];
660 		for( i=0; i<lgth1; i++ )
661 		{
662 			if( lastverticalw[i] >= wm )
663 			{
664 				wm = lastverticalw[i];
665 				iin = i; jin = lgth2-1;
666 				ijp[lgth1][lgth2] = +( lgth1 - i );
667 			}
668 		}
669 		for( j=0; j<lgth2; j++ )
670 		{
671 			if( lasthorizontalw[j] >= wm )
672 			{
673 				wm = lasthorizontalw[j];
674 				iin = lgth1-1; jin = j;
675 				ijp[lgth1][lgth2] = -( lgth2 - j );
676 			}
677 		}
678 	}
679 
680     for( i=0; i<lgth1+1; i++ )
681     {
682         ijp[i][0] = i + 1;
683     }
684     for( j=0; j<lgth2+1; j++ )
685     {
686         ijp[0][j] = -( j + 1 );
687     }
688 
689 	gaptable1 = gt1bk + lgth1+lgth2;
690 	*gaptable1 = 0;
691 	gaptable2 = gt2bk + lgth1+lgth2;
692 	*gaptable2 = 0;
693 
694 	iin = lgth1; jin = lgth2;
695 	limk = lgth1+lgth2 + 1;
696 	*impwmpt = 0.0;
697 	for( k=0; k<limk; k++ )
698 	{
699 		if( ijp[iin][jin] >= warpbase )
700 		{
701 			ifi = warpis[ijp[iin][jin]-warpbase];
702 			jfi = warpjs[ijp[iin][jin]-warpbase];
703 		}
704 		else if( ijp[iin][jin] < 0 )
705 		{
706 			ifi = iin-1; jfi = jin+ijp[iin][jin];
707 		}
708 		else if( ijp[iin][jin] > 0 )
709 		{
710 			ifi = iin-ijp[iin][jin]; jfi = jin-1;
711 		}
712 		else
713 		{
714 			ifi = iin-1; jfi = jin-1;
715 		}
716 		if( ifi == -warpbase && jfi == -warpbase )
717 		{
718 			l = iin;
719 			while( --l >= 0 )
720 			{
721 				*--gaptable1 = 'o';
722 				*--gaptable2 = '-';
723 				k++;
724 			}
725 			l= jin;
726 			while( --l >= 0 )
727 			{
728 				*--gaptable1 = '-';
729 				*--gaptable2 = 'o';
730 			}
731 			break;
732 		}
733 		else
734 		{
735 			l = iin - ifi;
736 			while( --l )
737 			{
738 				*--gaptable1 = 'o';
739 				*--gaptable2 = '-';
740 				k++;
741 			}
742 			l= jin - jfi;
743 			while( --l )
744 			{
745 				*--gaptable1 = '-';
746 				*--gaptable2 = 'o';
747 				k++;
748 			}
749 		}
750 		if( iin == lgth1 || jin == lgth2 )
751 			;
752 		else
753 		{
754 			*impwmpt += (double)imp_match_out_scD( iin, jin );
755 
756 //		fprintf( stderr, "impwm = %f (iin=%d, jin=%d) seq1=%c, seq2=%c\n", *impwmpt, iin, jin, seq1[0][iin], seq2[0][jin] );
757 		}
758 		if( iin <= 0 || jin <= 0 ) break;
759 		*--gaptable1 = 'o';
760 		*--gaptable2 = 'o';
761 		k++;
762 		iin = ifi; jin = jfi;
763 	}
764 
765 	for( i=0; i<icyc; i++ ) gapireru( mseq1[i], seq1[i], gaptable1 );
766 	for( j=0; j<jcyc; j++ ) gapireru( mseq2[j], seq2[j], gaptable2 );
767 
768 	free( gt1bk );
769 	free( gt2bk );
770 }
771 
Atracking(double * lasthorizontalw,double * lastverticalw,char ** seq1,char ** seq2,char ** mseq1,char ** mseq2,int ** ijp,int icyc,int jcyc,int tailgp,int * warpis,int * warpjs,int warpbase)772 static double Atracking( double *lasthorizontalw, double *lastverticalw,
773 						char **seq1, char **seq2,
774                         char **mseq1, char **mseq2,
775                         int **ijp, int icyc, int jcyc,
776 						int tailgp,
777 						int *warpis, int *warpjs, int warpbase )
778 {
779 	int i, j, l, iin, jin, ifi, jfi, lgth1, lgth2, k, limk;
780 	double wm;
781 	char *gaptable1, *gt1bk;
782 	char *gaptable2, *gt2bk;
783 	lgth1 = strlen( seq1[0] );
784 	lgth2 = strlen( seq2[0] );
785 
786 	gt1bk = AllocateCharVec( lgth1+lgth2+1 );
787 	gt2bk = AllocateCharVec( lgth1+lgth2+1 );
788 
789 #if 0
790 	for( i=0; i<lgth1; i++ )
791 	{
792 		fprintf( stderr, "lastverticalw[%d] = %f\n", i, lastverticalw[i] );
793 	}
794 #endif
795 
796 	if( tailgp == 1 )
797 		;
798 	else
799 	{
800 		wm = lastverticalw[0];
801 		for( i=0; i<lgth1; i++ )
802 		{
803 			if( lastverticalw[i] >= wm )
804 			{
805 				wm = lastverticalw[i];
806 				iin = i; jin = lgth2-1;
807 				ijp[lgth1][lgth2] = +( lgth1 - i );
808 			}
809 		}
810 		for( j=0; j<lgth2; j++ )
811 		{
812 			if( lasthorizontalw[j] >= wm )
813 			{
814 				wm = lasthorizontalw[j];
815 				iin = lgth1-1; jin = j;
816 				ijp[lgth1][lgth2] = -( lgth2 - j );
817 			}
818 		}
819 	}
820 
821     for( i=0; i<lgth1+1; i++ )
822     {
823         ijp[i][0] = i + 1;
824     }
825     for( j=0; j<lgth2+1; j++ )
826     {
827         ijp[0][j] = -( j + 1 );
828     }
829 
830 	gaptable1 = gt1bk + lgth1+lgth2;
831 	*gaptable1 = 0;
832 	gaptable2 = gt2bk + lgth1+lgth2;
833 	*gaptable2 = 0;
834 
835 	iin = lgth1; jin = lgth2;
836 	limk = lgth1+lgth2 + 1;
837 	for( k=0; k<limk; k++ )
838 	{
839 		if( ijp[iin][jin] >= warpbase )
840 		{
841 			ifi = warpis[ijp[iin][jin]-warpbase];
842 			jfi = warpjs[ijp[iin][jin]-warpbase];
843 		}
844 		else if( ijp[iin][jin] < 0 )
845 		{
846 			ifi = iin-1; jfi = jin+ijp[iin][jin];
847 		}
848 		else if( ijp[iin][jin] > 0 )
849 		{
850 			ifi = iin-ijp[iin][jin]; jfi = jin-1;
851 		}
852 		else
853 		{
854 			ifi = iin-1; jfi = jin-1;
855 		}
856 
857 		if( ifi == -warpbase && jfi == -warpbase )
858 		{
859 			l = iin;
860 			while( --l >= 0 )
861 			{
862 				*--gaptable1 = 'o';
863 				*--gaptable2 = '-';
864 				k++;
865 			}
866 			l= jin;
867 			while( --l >= 0 )
868 			{
869 				*--gaptable1 = '-';
870 				*--gaptable2 = 'o';
871 			}
872 			break;
873 		}
874 		else
875 		{
876 			l = iin - ifi;
877 			while( --l )
878 			{
879 				*--gaptable1 = 'o';
880 				*--gaptable2 = '-';
881 				k++;
882 			}
883 			l= jin - jfi;
884 			while( --l )
885 			{
886 				*--gaptable1 = '-';
887 				*--gaptable2 = 'o';
888 				k++;
889 			}
890 		}
891 		if( iin <= 0 || jin <= 0 ) break;
892 		*--gaptable1 = 'o';
893 		*--gaptable2 = 'o';
894 		k++;
895 		iin = ifi; jin = jfi;
896 	}
897 
898 	for( i=0; i<icyc; i++ ) gapireru( mseq1[i], seq1[i], gaptable1 );
899 	for( j=0; j<jcyc; j++ ) gapireru( mseq2[j], seq2[j], gaptable2 );
900 
901 	free( gt1bk );
902 	free( gt2bk );
903 
904 	return( 0.0 );
905 }
906 
907 #if 1
cleargaplens(Gaplen **** gaplens)908 static void cleargaplens( Gaplen ****gaplens )
909 {
910 	int i;
911 //	int j, k;
912 	Gaplen ****ptptptpt, ***ptptpt, ***ptptptmv, **ptptmv, **ptpt;
913 	for( i=0; i<12; i++ )
914 	{
915 		ptptptpt = gaplens+i;
916 
917 		ptptptmv = *ptptptpt;
918 		while( *(ptptpt=ptptptmv++) )
919 //		for( j=0; *(ptptpt=*ptptptpt+j); j++ )
920 		{
921 			ptptmv = *ptptpt;
922 			while( *(ptpt=ptptmv++)!=(Gaplen *)1 )
923 //			for( k=0; gaplens[i][j][k]!=(Gaplen *)1; k++ )
924 //			for( k=0; *(ptpt=*ptptpt+k)!=(Gaplen *)1; k++ )
925 			{
926 //				if( gaplens[i][j][k] ) free( gaplens[i][j][k] );
927 				if( *ptpt ) free( *ptpt );
928 				*ptpt = NULL;
929 			}
930 //			free( gaplens[i][j] );
931 			free( *ptptpt );
932 //			gaplens[i][j] = NULL;
933 			*ptptpt = NULL;
934 		}
935 	}
936 }
937 #else
cleargaplens(Gaplen **** gaplens)938 static void cleargaplens( Gaplen ****gaplens )
939 {
940 	int i, j, k;
941 	for( i=0; i<12; i++ )
942 	{
943 		for( j=0; gaplens[i][j]; j++ )
944 		{
945 			for( k=0; gaplens[i][j][k]!=(Gaplen *)1; k++ )
946 			{
947 				if( gaplens[i][j][k] ) free( gaplens[i][j][k] );
948 				gaplens[i][j][k] = NULL;
949 			}
950 			free( gaplens[i][j] );
951 			gaplens[i][j] = NULL;
952 		}
953 	}
954 }
955 #endif
956 
957 #if USEGAPLENHALFORMTX
FreeGaplenMtxReport(Gaplen ** mtx)958 static void FreeGaplenMtxReport( Gaplen **mtx )
959 {
960 	int i;
961 	if( mtx == NULL ) return;
962 
963 	for( i=0; ; i++ )
964 	{
965 		reporterr( "i=%d, mtx[i] = %p\n", i, mtx[i] );
966 		if( mtx[i] )
967 		{
968 			if( mtx[i] == (Gaplen *)1 ) break;
969 			free( mtx[i] ); mtx[i] = NULL;
970 		}
971 		mtx[i] = NULL;
972 	}
973 	free( mtx );
974 	mtx = NULL;
975 }
976 #endif
977 
FreeGaplenMtx(Gaplen ** mtx,int inclfreq)978 static void FreeGaplenMtx( Gaplen **mtx, int inclfreq )
979 {
980 	int i;
981 	if( mtx == NULL ) return;
982 
983 	for( i=0; ; i++ )
984 	{
985 		if( mtx[i] )
986 		{
987 			if( mtx[i] == (Gaplen *)1 ) break;
988 
989 #if 0
990 			if( inclfreq )
991 			{
992 //				reporterr( "inclfreq=%d\n", inclfreq );
993 				for( j=0; mtx[i][j].relend==0; j++ )
994 				{
995 //					reporterr( "j=%d\n", j );
996 //					reporterr( "Free! freq\n" );
997 					if( mtx[i][j].freq )
998 					{
999 						free( mtx[i][j].freq );
1000 					}
1001 					mtx[i][j].freq = NULL;
1002 				}
1003 			}
1004 #endif
1005 
1006 			free( mtx[i] ); mtx[i] = NULL;
1007 		}
1008 	}
1009 	free( mtx );
1010 	mtx = NULL;
1011 }
1012 
1013 #if USEGAPLENHALFORMTX
FreeGaplenCubgaplenReport(Gaplen *** cub)1014 static void FreeGaplenCubgaplenReport( Gaplen ***cub )
1015 {
1016 	int i;
1017 	if( cub == NULL ) return;
1018 
1019 	for( i=0; cub[i]; i++ )
1020 	{
1021 		reporterr( "i=%d, cub[i]=%p\n", i, cub[i] );
1022 		FreeGaplenMtx( cub[i], 0 );
1023 		cub[i] = NULL;
1024 	}
1025 	free( cub );
1026 	cub = NULL;
1027 }
1028 #endif
1029 
FreeGaplenCub(Gaplen *** cub)1030 static void FreeGaplenCub( Gaplen ***cub )
1031 {
1032 	int i;
1033 	if( cub == NULL ) return;
1034 
1035 	for( i=0; cub[i]; i++ )
1036 	{
1037 		FreeGaplenMtx( cub[i], 0 );
1038 		cub[i] = NULL;
1039 	}
1040 	free( cub );
1041 	cub = NULL;
1042 }
1043 
strralpha(const char * s,const char * first)1044 static int strralpha( const char *s, const char *first )
1045 {
1046 	int v = 0;
1047 	s--;
1048 	while( s >= first )
1049 	{
1050 		if( *s-- != '-' ) return( v );
1051 		v++;
1052 	}
1053 	if( s == first-1 ) return( v );
1054 	return( -1 );
1055 }
1056 
fillgaplen(Gaplen ** mtx,int l)1057 static void fillgaplen( Gaplen **mtx, int l )
1058 {
1059 	int i, j, n, k, len, pos, idatnext;
1060 	double freq;
1061 	for( i=0; i<=l; i++ )
1062 	{
1063 //		reporterr( "i=%d\n", i );
1064 		if( mtx[i] == NULL ) continue;
1065 		for( n=0; (len=mtx[i][n].len)!=-1; n++ )
1066 		{
1067 			freq = mtx[i][n].freq;
1068 			idatnext = n;
1069 			for( j=0; j<len; j++ )
1070 			{
1071 //				reporterr( "n=%d, j=%d, i=%d, len=%d\n", n, j, i, len );
1072 				pos = i-1-j;
1073 //				reporterr( "pos = %d\n", pos );
1074 				if( mtx[pos] == NULL )
1075 				{
1076 					mtx[pos] = calloc( 2, sizeof( Gaplen ) );
1077 					mtx[pos][0].len = -1;
1078 #if USEGAPLENHALFORMTX
1079 					mtx[pos][0].idatend = -1;
1080 #endif
1081 					mtx[pos][0].idatnext = -1;
1082 					mtx[pos][0].idatprev = -1;
1083 					mtx[pos][0].relend = -1;
1084 					mtx[pos][0].freq = 0.0;
1085 					mtx[pos][0].npat = -1;
1086 					k = 0;
1087 				}
1088 				else
1089 				{
1090 					k = mtx[pos][0].npat;
1091 				}
1092 				mtx[pos] = realloc( mtx[pos], sizeof( Gaplen ) * ( k + 2 ) );
1093 //				mtx[pos][k].len = -100000; // tsukawanai!
1094 				mtx[pos][k].len = len; // compact no toki tsukau!
1095 				mtx[pos][k].relend = j+1;
1096 				mtx[pos][k].freq = freq;// tsukawanai! mtx[i][n].freq;
1097 				mtx[pos][k].idatnext = idatnext;
1098 				mtx[pos][k].idatprev =-1;
1099 				mtx[pos][k].npat = -1;
1100 				mtx[pos][k+1].len = -1;
1101 				mtx[pos][k+1].idatnext = -1;
1102 				mtx[pos][k+1].relend = -1;
1103 				mtx[pos][k+1].freq = 0.0;
1104 				mtx[pos][k+1].npat = -1;
1105 				mtx[pos][0].npat = k+1;
1106 #if USEGAPLENHALFORMTX
1107 				mtx[pos][k].idatend = n;
1108 				mtx[pos][k+1].idatend = -1;
1109 #endif
1110 
1111 				mtx[pos+1][idatnext].idatprev = k; // kanarazu aru?
1112 
1113 				idatnext = k;
1114 			}
1115 		}
1116 	}
1117 }
1118 
gapvariety(int n,int l,char ** seq)1119 static int gapvariety( int n, int l, char **seq )
1120 {
1121 	int i, j, gl, *known, nknown, val;
1122 	known = calloc( l+1, sizeof( int ) );
1123 //	for( i=0; i<n; i++ ) reporterr( "seq[%d] = %s\n", i, seq[i] );
1124 
1125 	val = 0;
1126 	for( j=0; j<=l; j++ )
1127 	{
1128 		for( i=0; i<j; i++ ) known[i] = 0;
1129 		nknown = 0;
1130 		for( i=0; i<n; i++ )
1131 		{
1132 			if( seq[i][j] == '-' ) continue;
1133 
1134 			gl = strralpha( seq[i]+j, seq[i] );
1135 //			reporterr( "gl = %d\n", gl );
1136 			if( gl > 0 )
1137 			{
1138 				if( known[gl] )
1139 				{
1140 					;
1141 				}
1142 				else
1143 				{
1144 					nknown++;
1145 				}
1146 			}
1147 		}
1148 		val += nknown;
1149 	}
1150 	free( known );
1151 
1152 	return( val );
1153 }
1154 
1155 
gaplencount(int n,int l,Gaplen ** mtx,char ** seq,double * eff)1156 static void gaplencount( int n, int l, Gaplen **mtx, char **seq, double *eff )
1157 {
1158 	int i, j, k, gl, *known, nknown;
1159 	known = calloc( l+1, sizeof( int ) );
1160 //	for( i=0; i<n; i++ ) reporterr( "seq[%d] = %s\n", i, seq[i] );
1161 
1162 	for( j=0; j<=l; j++ )
1163 	{
1164 		if( mtx[j] )
1165 		{
1166 			if( mtx[j] == (Gaplen *)1 ) break;
1167 			for( k=0; mtx[j][k].relend==0; k++ )
1168 			{
1169 #if 0
1170 //				reporterr( "j=%d\n", j );
1171 //				reporterr( "Free! freq\n" );
1172 				if( mtx[j][k].freq )
1173 				{
1174 					free( mtx[j][k].freq );
1175 				}
1176 				mtx[j][k].freq = NULL;
1177 #endif
1178 			}
1179 			free( mtx[j] );
1180 		}
1181 		mtx[j] = NULL;
1182 	}
1183 
1184 	for( j=0; j<=l; j++ )
1185 	{
1186 		for( i=0; i<j; i++ ) known[i] = 0;
1187 		nknown = 0;
1188 		for( i=0; i<n; i++ )
1189 		{
1190 			if( seq[i][j] == '-' ) continue;
1191 
1192 			gl = strralpha( seq[i]+j, seq[i] );
1193 //			reporterr( "gl = %d\n", gl );
1194 			if( gl > 0 )
1195 			{
1196 				if( known[gl] )
1197 				{
1198 //					reporterr( "gl=%d, Known!\n", gl );
1199 					for( k=0; mtx[j][k].len!=-1; k++ ) if( mtx[j][k].len == gl ) break;
1200 					if( mtx[j][k].len == -1 )
1201 					{
1202 						reporterr( "Unexpected error!\n" );
1203 						exit( 1 );
1204 					}
1205 					mtx[j][k].freq += eff[i];
1206 				}
1207 				else
1208 				{
1209 //					reporterr( "gl=%d, First!\n", gl );
1210 					mtx[j] = realloc( mtx[j], sizeof( Gaplen ) * (nknown+2) );
1211 					mtx[j][nknown].len = gl;
1212 					mtx[j][nknown].relend = 0;
1213 					mtx[j][nknown].freq = eff[i];
1214 					mtx[j][nknown].idatnext = -2;
1215 					mtx[j][nknown+1].len = -1;
1216 					mtx[j][nknown+1].idatnext = -1;
1217 					mtx[j][nknown+1].relend = -1;
1218 					mtx[j][nknown+1].freq = 0.0;
1219 					mtx[j][nknown+1].npat = -1;
1220 #if USEGAPLENHALFORMTX
1221 					mtx[j][nknown].idatend = nknown;
1222 					mtx[j][nknown+1].idatend = -1;
1223 #endif
1224 					known[gl]++;
1225 					nknown++;
1226 					mtx[j][0].npat = nknown;
1227 				}
1228 			}
1229 		}
1230 	}
1231 	fillgaplen( mtx, l );
1232 #if 0
1233 	reporterr( "Gaplen:\n" );
1234 	for( i=0; i<=l; i++ )
1235 	{
1236 //		reporterr( "i=%d, gaplen[i] = %p\n", i, mtx[i] );
1237 		if( mtx[i] )
1238 		{
1239 			for( j=0; mtx[i][j].len!=-1; j++ )
1240 				reporterr( "i=%d, len = %d, relend = %d, freq = %f\n", i, mtx[i][j].len, mtx[i][j].relend, mtx[i][j].freq );
1241 		}
1242 	}
1243 
1244 #endif
1245 
1246 	free( known );
1247 }
1248 
1249 
1250 #if DEBUG
showgaplen(Gaplen ** mtx,int seqlen)1251 static void showgaplen( Gaplen **mtx, int seqlen )
1252 {
1253 	int i, l;
1254 #if USEGAPLENHALFORMTX
1255 	int id, pos;
1256 #endif
1257 //	for( i=0; i<=seqlen; i++ )
1258 	for( i=0; ; i++ )
1259 	{
1260 //		reporterr( "chain[%d] = %d\n", i, chain[i] );
1261 		if( mtx[i] == NULL ) continue;
1262 		if( mtx[i] == (Gaplen *)1 ) break;
1263 		for( l=0; mtx[i][l].idatnext!=-1; l++ )
1264 		{
1265 #if USEGAPLENHALFORMTX
1266 			reporterr( "i=%d, l=%d, len=%d, relend=%d, idatend=%d, idatnext=%d, idatprev=%d, freq=%f\n", i, l, mtx[i][l].len, mtx[i][l].relend, mtx[i][l].idatend, mtx[i][l].idatnext, mtx[i][l].idatprev, mtx[i][l].freq );
1267 			pos = mtx[i][l].relend;
1268 			id = mtx[i][l].idatend;
1269 			if( mtx[i+pos] == NULL )
1270 			{
1271 //				reporterr( "Error in SOURCE\n" );
1272 				reporterr( ".len and .freq were lost when i=%d!\n", i );
1273 //				exit( 1 );
1274 			}
1275 #else
1276 			reporterr( "i=%d, l=%d, len=%d, relend=%d, idatnext=%d, idatprev=%d, freq=%f\n", i, l, mtx[i][l].len, mtx[i][l].relend, mtx[i][l].idatnext, mtx[i][l].idatprev, mtx[i][l].freq );
1277 #endif
1278 		}
1279 	}
1280 }
1281 #endif
1282 
1283 #if WMCHECK
pairgapcount(char * s1,char * s2)1284 static int pairgapcount( char *s1, char *s2 )
1285 {
1286 	char **tmpseq;
1287 	int i, len, st, k;
1288 	int v = 0;
1289 
1290 	len = strlen( s1 );
1291 	tmpseq = calloc( sizeof( char * ), 2 );
1292 	tmpseq[0] = malloc( sizeof( char ) * ( len + 1 ) );
1293 	tmpseq[1] = malloc( sizeof( char ) * ( len + 1 ) );
1294 
1295 	strcpy( tmpseq[0], s1 );
1296 	strcpy( tmpseq[1], s2 );
1297 
1298 	commongappick( 2, tmpseq );
1299 	len = strlen( tmpseq[0] );
1300 
1301 
1302 	for( k=0; k<2; k++ )
1303 	{
1304 		st = 0;
1305 		for( i=0; i<len; i++ )
1306 		{
1307 			if( tmpseq[k][i] == '-' )
1308 			{
1309 				if( st == 0 )
1310 				{
1311 					v++;
1312 					st = 1;
1313 				}
1314 			}
1315 			else
1316 			{
1317 				st = 0;
1318 			}
1319 		}
1320 	}
1321 	free( tmpseq[0] );
1322 	free( tmpseq[1] );
1323 	free( tmpseq );
1324 
1325 	return( v );
1326 }
1327 #endif
1328 
calcpfac_gap_noidatend(Gaplen ** gaplen1,Gaplen ** gaplen2,int newgaplen,int i,int j,char * seq1,char * seq2,int disp)1329 static double calcpfac_gap_noidatend( Gaplen **gaplen1, Gaplen **gaplen2, int newgaplen, int i, int j, char *seq1, char *seq2, int disp ) // seq1 to seq2 ha debug you
1330 {
1331 #if 1
1332 	double pfac, pfac1, pfac10, pfac2;
1333 	int k, l, pos1, pos2;
1334 	Gaplen *gaplen1i, *gaplen2j, *g1, *g2;
1335 
1336 #if 0 // .len no shouryaku ni taiou shiteinai
1337 	int gl;
1338 	if( disp )
1339 	{
1340 		reporterr( "calcpfac_gap_noidatend, %c (%d) - %c (%d)\n", seq1[i], i, seq2[j], j );
1341 		reporterr( "newgaplen = %d\n", newgaplen );
1342 		reporterr( "In calcpfac_gap, gaplen1[%d(%c)] = \n", i, seq1[i] );
1343 		for( k=0; gaplen1[i]&&(id1=gaplen1[i][k].idatnext)!=-1; k++ )
1344 		{
1345 			pos1 = gaplen1[i][k].relend;
1346 			reporterr( ".len=%d, .relend=%d, .freq=%f\n", gaplen1[i+pos1][id1].len, gaplen1[i][k].relend, gaplen1[i+pos1][id1].freq[0] );
1347 		}
1348 		reporterr( "In calcpfac_gap, gaplen2[%d(%c)] = \n", j, seq2[j] );
1349 		showgaplen( gaplen2, strlen(seq2) );
1350 		for( k=0; gaplen2[j]&&(id2=gaplen2[j][k].idatnext)!=-1; k++ )
1351 		{
1352 			pos2 = gaplen2[j][k].relend;
1353 			reporterr( ".len=%d, .relend=%d, .freq=%f\n", gaplen2[j+pos2][id2].len, gaplen2[j][k].relend, gaplen2[j+pos2][id2].freq[0] );
1354 		}
1355 	}
1356 #endif
1357 	gaplen2j = gaplen2[j];
1358 	gaplen1i = gaplen1[i];
1359 
1360 	pfac = 0.0;
1361 	pfac1 = 0.0;
1362 	pfac10 = 0.0;
1363 	if( gaplen1i ) for( k=0; (g1=gaplen1i+k)->idatnext!=-1; k++ )
1364 	{
1365 		pos1 = g1->relend;
1366 		if( pos1 != 0 )
1367 		{
1368 			pfac2 = 0.0;
1369 			if( gaplen2j ) for( l=0; (g2=gaplen2j+l)->idatnext!=-1; l++ )
1370 			{
1371 				pos2 = g2->relend;
1372 				if( pos2 == 0 && g2->len >g1->len - (pos1) + newgaplen )
1373 				{
1374 					pfac2 += g2->freq;
1375 //					reporterr( "hit! pfac2=%f, .freq=%f\n", pfac2, gaplen2[j][l].freq );
1376 				}
1377 //				else
1378 //					reporterr( "does not hit! pfac2=%f, gaplen1[i][k].len=%d, gaplen[i][k].relend=%d, newgaplen=%d\n", pfac2, gaplen1[i][k].len, gaplen1[i][k].relend, newgaplen );
1379 			}
1380 			pfac += pfac2 * g1->freq;
1381 			pfac1 += g1->freq;
1382 		}
1383 		else if( pos1 == 0 )
1384 		{
1385 			pfac2 = 1.0;
1386 			if( gaplen2j ) for( l=0; (g2=gaplen2j+l)->idatnext!=-1; l++ )
1387 			{
1388 				pos2 = g2->relend;
1389 				if( pos2 == 0 && g2->len == g1->len+newgaplen ) pfac2 -= g2->freq;// kokode shuryou suru gap, gaplen1 ha kangaenai.
1390 				if( pos2 != 0 && g2->len - (pos2-1) > g1->len+newgaplen ) pfac2 -= g2->freq;// keizoku suru gap, gaplen1 ha kangaenai.
1391 			}
1392 //			reporterr( "pfac2 in line 1056 = %f\n", pfac2 );
1393 			pfac += pfac2 * g1->freq;
1394 			pfac10 += g1->freq;
1395 		}
1396 	}
1397 #if DEBUG
1398 	reporterr( "pfac1 (step2) = %f\n", pfac1 );
1399 	reporterr( "pfac10 (step2) = %f\n", pfac10 );
1400 	reporterr( "pfac (step2) = %f\n", pfac );
1401 #endif
1402 
1403 	pfac1 = 1.0 - pfac1 - pfac10;
1404 	pfac2 = 1.0;
1405 	if( gaplen2j ) for( l=0; (g2=gaplen2j+l)->idatnext!=-1; l++ )
1406 	{
1407 		pos2 = g2->relend;
1408 		if( pos2 == 0 && g2->len == newgaplen ) pfac2 -= g2->freq;// kokode shuryou suru gap, gaplen1 ha kangaenai.
1409 		if( pos2 != 0 && g2->len - (pos2-1) > newgaplen ) pfac2 -= g2->freq;// keizoku suru gap, gaplen1 ha kangaenai.
1410 	}
1411 #if DEBUG
1412 	reporterr( "pfac1 (type3) = %f\n", pfac1 );
1413 	reporterr( "pfac2 (type3) = %f\n", pfac2 );
1414 	reporterr( "pfac (step3) = %f\n", pfac );
1415 #endif
1416 	pfac += pfac1 * pfac2;
1417 #if DEBUG
1418 	reporterr( "incomplete pfac = %f, pfac1,pfac2 (%c%d,%c%d) = %f, %f\n", pfac, seq1[i], i, seq2[j], j, pfac1, pfac2 );
1419 #endif
1420 
1421 
1422 	return( pfac );
1423 
1424 #else
1425 
1426 	double pfac, pfac1, pfac10, pfac2;
1427 	int k, l, pos1, pos2, id1, id2;
1428 	Gaplen *gaplen1i, *gaplen2j;
1429 
1430 #if 0 // .len no shouryaku ni taiou shiteinai
1431 	int gl;
1432 	if( disp )
1433 	{
1434 		reporterr( "calcpfac_gap_noidatend, %c (%d) - %c (%d)\n", seq1[i], i, seq2[j], j );
1435 		reporterr( "newgaplen = %d\n", newgaplen );
1436 		reporterr( "In calcpfac_gap, gaplen1[%d(%c)] = \n", i, seq1[i] );
1437 		for( k=0; gaplen1[i]&&(id1=gaplen1[i][k].idatend)!=-1; k++ )
1438 		{
1439 			pos1 = gaplen1[i][k].relend;
1440 			reporterr( ".len=%d, .relend=%d, .freq=%f\n", gaplen1[i+pos1][id1].len, gaplen1[i][k].relend, gaplen1[i+pos1][id1].freq[0] );
1441 		}
1442 		reporterr( "In calcpfac_gap, gaplen2[%d(%c)] = \n", j, seq2[j] );
1443 		showgaplen( gaplen2, strlen(seq2) );
1444 		for( k=0; gaplen2[j]&&(id2=gaplen2[j][k].idatend)!=-1; k++ )
1445 		{
1446 			pos2 = gaplen2[j][k].relend;
1447 			reporterr( ".len=%d, .relend=%d, .freq=%f\n", gaplen2[j+pos2][id2].len, gaplen2[j][k].relend, gaplen2[j+pos2][id2].freq[0] );
1448 		}
1449 	}
1450 #endif
1451 	gaplen2j = gaplen2[j];
1452 	gaplen1i = gaplen1[i];
1453 
1454 	pfac = 0.0;
1455 	pfac1 = 0.0;
1456 	pfac10 = 0.0;
1457 	if( gaplen1i ) for( k=0; (gaplen1i[k].idatnext)!=-1; k++ )
1458 	{
1459 		pos1 = gaplen1i[k].relend;
1460 		if( pos1 != 0 )
1461 		{
1462 			pfac2 = 0.0;
1463 			if( gaplen2j ) for( l=0; (gaplen2j[l].idatnext)!=-1; l++ )
1464 			{
1465 				pos2 = gaplen2j[l].relend;
1466 				if( pos2 == 0 && gaplen2j[l].len > gaplen1i[k].len - (pos1) + newgaplen )
1467 				{
1468 					pfac2 += gaplen2j[l].freq;
1469 //					reporterr( "hit! pfac2=%f, .freq=%f\n", pfac2, gaplen2[j][l].freq );
1470 				}
1471 //				else
1472 //					reporterr( "does not hit! pfac2=%f, gaplen1[i][k].len=%d, gaplen[i][k].relend=%d, newgaplen=%d\n", pfac2, gaplen1[i][k].len, gaplen1[i][k].relend, newgaplen );
1473 			}
1474 			pfac += pfac2 * gaplen1i[k].freq;
1475 			pfac1 += gaplen1i[k].freq;
1476 		}
1477 		else if( pos1 == 0 )
1478 		{
1479 			pfac2 = 1.0;
1480 			if( gaplen2j ) for( l=0; (gaplen2j[l].idatnext)!=-1; l++ )
1481 			{
1482 				pos2 = gaplen2j[l].relend;
1483 				if( pos2 == 0 && gaplen2j[l].len == gaplen1i[k].len+newgaplen ) pfac2 -= gaplen2j[l].freq;// kokode shuryou suru gap, gaplen1 ha kangaenai.
1484 				if( pos2 != 0 && gaplen2j[l].len - (pos2-1) > gaplen1i[k].len+newgaplen ) pfac2 -= gaplen2j[l].freq;// keizoku suru gap, gaplen1 ha kangaenai.
1485 			}
1486 //			reporterr( "pfac2 in line 1056 = %f\n", pfac2 );
1487 			pfac += pfac2 * gaplen1i[k].freq;
1488 			pfac10 += gaplen1i[k].freq;
1489 		}
1490 	}
1491 #if DEBUG
1492 	reporterr( "pfac1 (step2) = %f\n", pfac1 );
1493 	reporterr( "pfac10 (step2) = %f\n", pfac10 );
1494 	reporterr( "pfac (step2) = %f\n", pfac );
1495 #endif
1496 
1497 	pfac1 = 1.0 - pfac1 - pfac10;
1498 	pfac2 = 1.0;
1499 	if( gaplen2j ) for( l=0; (gaplen2j[l].idatnext)!=-1; l++ )
1500 	{
1501 		pos2 = gaplen2j[l].relend;
1502 		if( pos2 == 0 && gaplen2j[l].len == newgaplen ) pfac2 -= gaplen2j[l].freq;// kokode shuryou suru gap, gaplen1 ha kangaenai.
1503 		if( pos2 != 0 && gaplen2j[l].len - (pos2-1) > newgaplen ) pfac2 -= gaplen2j[l].freq;// keizoku suru gap, gaplen1 ha kangaenai.
1504 	}
1505 #if DEBUG
1506 	reporterr( "pfac1 (type3) = %f\n", pfac1 );
1507 	reporterr( "pfac2 (type3) = %f\n", pfac2 );
1508 	reporterr( "pfac (step3) = %f\n", pfac );
1509 #endif
1510 	pfac += pfac1 * pfac2;
1511 #if DEBUG
1512 	reporterr( "incomplete pfac = %f, pfac1,pfac2 (%c%d,%c%d) = %f, %f\n", pfac, seq1[i], i, seq2[j], j, pfac1, pfac2 );
1513 #endif
1514 
1515 
1516 	return( pfac );
1517 
1518 #endif
1519 }
1520 
1521 #if USEGAPLENHALFORMTX
1522 
calcpfac_gap_incomplete(Gaplen ** gaplen1,Gaplen ** gaplen2,int newgaplen,int i,int j,char * seq1,char * seq2,int disp)1523 static double calcpfac_gap_incomplete( Gaplen **gaplen1, Gaplen **gaplen2, int newgaplen, int i, int j, char *seq1, char *seq2, int disp ) // seq1 to seq2 ha debug you
1524 {
1525 	double pfac, pfac1, pfac10, pfac2;
1526 	int k, l, pos1, pos2, id1, id2;
1527 	Gaplen *gapend1, *gapend2;
1528 	Gaplen *gaplen1i, *gaplen2j;
1529 
1530 #if 0 // .len no shouryaku ni taiou shiteinai
1531 	int gl;
1532 	if( disp )
1533 	{
1534 		reporterr( "calcpfac_gap_incomplete, %c (%d) - %c (%d)\n", seq1[i], i, seq2[j], j );
1535 		reporterr( "newgaplen = %d\n", newgaplen );
1536 		reporterr( "In calcpfac_gap, gaplen1[%d(%c)] = \n", i, seq1[i] );
1537 		for( k=0; gaplen1[i]&&(id1=gaplen1[i][k].idatend)!=-1; k++ )
1538 		{
1539 			pos1 = gaplen1[i][k].relend;
1540 			reporterr( ".len=%d, .relend=%d, .freq=%f\n", gaplen1[i+pos1][id1].len, gaplen1[i][k].relend, gaplen1[i+pos1][id1].freq[0] );
1541 		}
1542 		reporterr( "In calcpfac_gap, gaplen2[%d(%c)] = \n", j, seq2[j] );
1543 		showgaplen( gaplen2, strlen(seq2) );
1544 		for( k=0; gaplen2[j]&&(id2=gaplen2[j][k].idatend)!=-1; k++ )
1545 		{
1546 			pos2 = gaplen2[j][k].relend;
1547 			reporterr( ".len=%d, .relend=%d, .freq=%f\n", gaplen2[j+pos2][id2].len, gaplen2[j][k].relend, gaplen2[j+pos2][id2].freq[0] );
1548 		}
1549 	}
1550 #endif
1551 	gaplen2j = gaplen2[j];
1552 	gaplen1i = gaplen1[i];
1553 
1554 	pfac = 0.0;
1555 	pfac1 = 0.0;
1556 	pfac10 = 0.0;
1557 	if( gaplen1i ) for( k=0; (id1=gaplen1i[k].idatend)!=-1; k++ )
1558 	{
1559 		pos1 = gaplen1i[k].relend;
1560 		gapend1 = gaplen1[i+pos1]+id1;
1561 		if( pos1 != 0 )
1562 		{
1563 			pfac2 = 0.0;
1564 			if( gaplen2j ) for( l=0; (id2=gaplen2j[l].idatend)!=-1; l++ )
1565 			{
1566 				pos2 = gaplen2j[l].relend;
1567 				gapend2 = gaplen2[j+pos2]+id2;
1568 //				if( pos2 == 0 && gapend2->len + 1 > gapend1->len - (pos1-1) + newgaplen )
1569 				if( pos2 == 0 && gapend2->len > gapend1->len - (pos1) + newgaplen )
1570 				{
1571 					pfac2 += gapend2->freq;
1572 //					reporterr( "hit! pfac2=%f, .freq=%f\n", pfac2, gaplen2[j][l].freq );
1573 				}
1574 //				else
1575 //					reporterr( "does not hit! pfac2=%f, gaplen1[i][k].len=%d, gaplen[i][k].relend=%d, newgaplen=%d\n", pfac2, gaplen1[i][k].len, gaplen1[i][k].relend, newgaplen );
1576 			}
1577 			pfac += pfac2 * gapend1->freq;
1578 			pfac1 += gapend1->freq;
1579 		}
1580 		else if( pos1 == 0 )
1581 		{
1582 			pfac2 = 1.0;
1583 			if( gaplen2j ) for( l=0; (id2=gaplen2j[l].idatend)!=-1; l++ )
1584 			{
1585 				pos2 = gaplen2j[l].relend;
1586 				gapend2 = gaplen2[j+pos2]+id2;
1587 				if( pos2 == 0 && gapend2->len == gapend1->len+newgaplen ) pfac2 -= gapend2->freq;// kokode shuryou suru gap, gaplen1 ha kangaenai.
1588 				if( pos2 != 0 && gapend2->len - (pos2-1) > gapend1->len+newgaplen ) pfac2 -= gapend2->freq;// keizoku suru gap, gaplen1 ha kangaenai.
1589 			}
1590 //			reporterr( "pfac2 in line 1056 = %f\n", pfac2 );
1591 			pfac += pfac2 * gapend1->freq;
1592 			pfac10 += gapend1->freq;
1593 		}
1594 	}
1595 #if DEBUG
1596 	reporterr( "pfac1 (step2) = %f\n", pfac1 );
1597 	reporterr( "pfac10 (step2) = %f\n", pfac10 );
1598 	reporterr( "pfac (step2) = %f\n", pfac );
1599 #endif
1600 
1601 	pfac1 = 1.0 - pfac1 - pfac10;
1602 	pfac2 = 1.0;
1603 	if( gaplen2j ) for( l=0; (id2=gaplen2j[l].idatend)!=-1; l++ )
1604 	{
1605 		pos2 = gaplen2j[l].relend;
1606 		gapend2 = gaplen2[j+pos2]+id2;
1607 		if( pos2 == 0 && gapend2->len == newgaplen ) pfac2 -= gapend2->freq;// kokode shuryou suru gap, gaplen1 ha kangaenai.
1608 		if( pos2 != 0 && gapend2->len - (pos2-1) > newgaplen ) pfac2 -= gapend2->freq;// keizoku suru gap, gaplen1 ha kangaenai.
1609 	}
1610 #if DEBUG
1611 	reporterr( "pfac1 (type3) = %f\n", pfac1 );
1612 	reporterr( "pfac2 (type3) = %f\n", pfac2 );
1613 	reporterr( "pfac (step3) = %f\n", pfac );
1614 #endif
1615 	pfac += pfac1 * pfac2;
1616 #if DEBUG
1617 	reporterr( "incomplete pfac = %f, pfac1,pfac2 (%c%d,%c%d) = %f, %f\n", pfac, seq1[i], i, seq2[j], j, pfac1, pfac2 );
1618 #endif
1619 
1620 
1621 	return( pfac );
1622 
1623 }
1624 
calcpfac_gapex(Gaplen ** gaplen1,Gaplen ** gaplen2,int i,int j,int newgaplen,char * seq1,char * seq2,int disp)1625 static double calcpfac_gapex( Gaplen **gaplen1, Gaplen **gaplen2, int i, int j, int newgaplen, char *seq1, char *seq2, int disp )
1626 {
1627 	double pfac, pfac1, pfac2, pfac10;
1628 	int k, l, id1, id2, pos1, pos2;
1629 	Gaplen *gapend1, *gapend2;
1630 	Gaplen *gaplen1i, *gaplen2j;
1631 
1632 	gaplen1i = gaplen1[i];
1633 	gaplen2j = gaplen2[j];
1634 
1635 	pfac = 0.0;
1636 	pfac2 = 0.0;
1637 //	for( k=0; gaplen2[j]&&(gl=gaplen2[j][k].len)!=-1; k++ ) // ososugi!  hash ni atode henkou
1638 	if( gaplen2j ) for( k=0; (id2=gaplen2j[k].idatend)!=-1; k++ ) // ososugi!  hash ni atode henkou
1639 	{
1640 #if DEBUG
1641 		int gl;
1642 		pos2 = gaplen2j[k].relend;
1643 		id2 = gaplen2j[k].idatend;
1644 		gl = gaplen2[j+pos2][id2].len;
1645 		if( disp ) reporterr( "gaplen2[][].len=%d, .relend=%d, .freq=%f\n", gaplen2[j+pos2][id2].len, gaplen2[j][k].relend, gaplen2[j+pos2][id2].freq );
1646 		if( disp ) reporterr( "gl = %d, newgaplen=%d\n", gl, newgaplen );
1647 #endif
1648 		if( (pos2=gaplen2[j][k].relend) != 0 ) continue;
1649 
1650 		gapend2 = gaplen2[j+pos2]+id2;
1651 		pfac1 = 1.0;
1652 		pfac10 = 1.0;
1653 		if( gaplen1i ) for( l=0; (id1=gaplen1i[l].idatend)!=-1; l++ ) // ososugi!  hash ni atode henkou
1654 		{
1655 			pos1 = gaplen1i[l].relend;
1656 			gapend1 = gaplen1[i+pos1]+id1;
1657 			pfac10 -= gapend1->freq;
1658 #if DEBUG
1659 			if( disp ) reporterr( "gaplen1[][].len=%d, .relend=%d, .freq=%f\n", gaplen1[i+pos1][id1].len, gaplen1[i][l].relend, gaplen1[i+pos1][id1].freq );
1660 #endif
1661 			if( newgaplen + gapend1->len - (pos1) > gapend2->len - (pos2) ) pfac1 -= gapend1->freq;
1662 //			reporterr( "pfac1 = %f\n", pfac1 );
1663 		}
1664 		pfac += pfac1 * gapend2->freq;
1665 
1666 
1667 /* ???? */
1668 		if( newgaplen >= gapend2->len - (pos2-1) )  // >= or >??
1669 		{
1670 			pfac -= pfac10 * gapend2->freq;
1671 //			reporterr( "Hit! pfac1 = %f\n", pfac1 );
1672 		}
1673 /* ???? */
1674 
1675 
1676 //		if( gaplen2[j][k].relend == -1 ) pfac += gaplen2[j][k].freq;
1677 	}
1678 
1679 	return( pfac );
1680 }
1681 
calcpfac(Gaplen ** gaplen1,Gaplen ** gaplen2,int i,int j,char * seq1,char * seq2,int disp)1682 static double calcpfac( Gaplen **gaplen1, Gaplen **gaplen2, int i, int j, char *seq1, char *seq2, int disp ) // seq1 to seq2 ha debug you
1683 {
1684 	double pfac, pfac1, pfac2;
1685 	int k, l, pos1, pos2, id1, id2;
1686 	Gaplen *gapend1, *gapend2;
1687 	Gaplen *gaplen1i, *gaplen2j;
1688 
1689 	gaplen1i = gaplen1[i];
1690 	gaplen2j = gaplen2[j];
1691 
1692 #if DEBUG
1693 	if( disp )
1694 	{
1695 		reporterr( "seq1[0] = %s\n", seq1 );
1696 		reporterr( "seq2[0] = %s\n", seq2 );
1697 		reporterr( "i,j=%d,%d\n", i, j );
1698 
1699 		reporterr( "In calcpfac(), gaplen1[%d(%c)] = \n", i, seq1[i] );
1700 //		showgaplen( gaplen1, seqlen( seq1 ) );
1701 		for( k=0; gaplen1[i]&&(id1=gaplen1[i][k].idatend)!=-1; k++ )
1702 		{
1703 			pos1 = gaplen1[i][k].relend;
1704 			reporterr( "pos1=%d, id1=%d\n", pos1, id1 );
1705 			reporterr( ".len=%d, .relend=%d, .freq=%f\n", gaplen1[i+pos1][id1].len, gaplen1[i][k].relend, gaplen1[i+pos1][id1].freq );
1706 		}
1707 
1708 		reporterr( "In calcpfac(), gaplen2[%d(%c)] = \n", j, seq2[j] );
1709 //		showgaplen( gaplen2, seqlen( seq2 ) );
1710 		for( k=0; gaplen2[j]&&(id2=gaplen2[j][k].idatend)!=-1; k++ )
1711 		{
1712 			pos2 = gaplen2[j][k].relend;
1713 			reporterr( "j=%d, k=%d, id2=%d, pos2=%d\n", j, k, id2, pos2 );
1714 			reporterr( ".len=%d, .relend=%d\n", gaplen2[j+pos2][id2].len, gaplen2[j][k].relend );
1715 			reporterr( ".freq=%f\n", gaplen2[j+pos2][id2].freq );
1716 		}
1717 	}
1718 #endif
1719 
1720 	pfac1 = pfac2 = 0.0;
1721 	if( gaplen1i ) for( k=0; (id1=gaplen1i[k].idatend)!=-1; k++ )
1722 	{
1723 		if( (pos1=gaplen1i[k].relend) == 0 ) pfac1 += gaplen1[i+pos1][id1].freq;
1724 	}
1725 
1726 	if( gaplen2j ) for( l=0; (id2=gaplen2j[l].idatend)!=-1; l++ ) // ososugi!  hash ni atode henkou
1727 	{
1728 		if( (pos2=gaplen2j[l].relend) == 0 ) pfac2 += gaplen2[j+pos2][id2].freq;
1729 	}
1730 #if DEBUG
1731 	reporterr( "\n\nInitial pfac1,pfac2 (%c%d,%c%d) = %f, %f\n", seq1[i], i, seq2[j], j, pfac1, pfac2 );
1732 #endif
1733 	pfac = pfac1 * pfac2 + pfac1 * (1-pfac2) + pfac2 * (1-pfac1);
1734 #if DEBUG
1735 	reporterr( "\n\nInitial pfac (%d,%d) = %f\n", i, j, pfac );
1736 #endif
1737 
1738 #if 1
1739 //	if( pfac ) reporterr( "i,j=%d,%d, Cancel (eq len)? pfac = %f -> ", i, j, pfac );
1740 	if( gaplen1i ) for( k=0; (id1=gaplen1i[k].idatend)!=-1; k++ ) // ososugi!  hash ni atode henkou
1741 	{
1742 		pos1=gaplen1i[k].relend;
1743 		gapend1 = gaplen1[i+pos1]+id1;
1744 		if( gaplen2j ) for( l=0; (id2=gaplen2j[l].idatend)!=-1; l++ ) // ososugi!  hash ni atode henkou
1745 		{
1746 			pos2 = gaplen2j[l].relend;
1747 			gapend2 = gaplen2[j+pos2]+id2;
1748 			if     ( pos1 == 0 && pos2 == 0 && gapend1->len == gapend2->len ) pfac -= gapend1->freq * gapend2->freq;
1749 			else if( pos1 == 0 && pos2 != 0 && gapend2->len - (pos2-1) > gapend1->len ) pfac -= gapend1->freq * gapend2->freq;
1750 			else if( pos1 != 0 && pos2 == 0 && gapend1->len - (pos1-1) > gapend2->len ) pfac -= gapend1->freq * gapend2->freq;
1751 		}
1752 	}
1753 
1754 #if DEBUG
1755 	reporterr( "\n\nFinal pfac1,pfac2 (%c%d,%c%d, straight) = %f\n\n", seq1[i], i, seq2[j], j, pfac );
1756 #endif
1757 #else
1758 #endif
1759 	return( pfac );
1760 }
1761 #endif
1762 
calcpfac_gapex_noidatend(Gaplen ** gaplen1,Gaplen ** gaplen2,int i,int j,int newgaplen,char * seq1,char * seq2,int disp)1763 static double calcpfac_gapex_noidatend( Gaplen **gaplen1, Gaplen **gaplen2, int i, int j, int newgaplen, char *seq1, char *seq2, int disp )
1764 {
1765 #if 1
1766 	double pfac, pfac1, pfac2, pfac10;
1767 	int k, l,  pos1, pos2;
1768 	Gaplen *gaplen1i, *gaplen2j, *g1, *g2;
1769 
1770 	gaplen1i = gaplen1[i];
1771 	gaplen2j = gaplen2[j];
1772 
1773 	pfac = 0.0;
1774 	pfac2 = 0.0;
1775 	if( gaplen2j ) for( k=0; (g2=gaplen2j+k)->idatnext!=-1; k++ )
1776 	{
1777 #if DEBUG
1778 		int gl;
1779 		pos2 = gaplen2j[k].relend;
1780 		gl = gaplen2j[k].len;
1781 		if( disp ) reporterr( "gaplen2[][].len=%d, .relend=%d, .freq=%f\n", gaplen2[j][k].len, gaplen2[j][k].relend, gaplen2[j][k].freq );
1782 		if( disp ) reporterr( "gl = %d, newgaplen=%d\n", gl, newgaplen );
1783 #endif
1784 		if( (pos2=g2->relend) != 0 ) continue;
1785 
1786 		pfac1 = 1.0;
1787 		pfac10 = 1.0;
1788 		if( gaplen1i ) for( l=0; (g1=gaplen1i+l)->idatnext!=-1; l++ )
1789 		{
1790 			pos1 = g1->relend;
1791 			pfac10 -= g1->freq;
1792 #if DEBUG
1793 			if( disp ) reporterr( "gaplen1[][].len=%d, .relend=%d, .freq=%f\n", gaplen1[i][l].len, gaplen1[i][l].relend, gaplen1[i][l].freq );
1794 #endif
1795 			if( newgaplen + g1->len - (pos1) > g2->len - (pos2) ) pfac1 -= g1->freq;
1796 //			reporterr( "pfac1 = %f\n", pfac1 );
1797 		}
1798 		pfac += pfac1 * g2->freq;
1799 
1800 
1801 /* ???? */
1802 		if( newgaplen >= g2->len - (pos2-1) )  // >= or >??
1803 		{
1804 			pfac -= pfac10 * g2->freq;
1805 //			reporterr( "Hit! pfac1 = %f\n", pfac1 );
1806 		}
1807 /* ???? */
1808 
1809 
1810 //		if( gaplen2[j][k].relend == -1 ) pfac += gaplen2[j][k].freq;
1811 	}
1812 
1813 	return( pfac );
1814 #else
1815 	double pfac, pfac1, pfac2, pfac10;
1816 	int k, l, id1, id2, pos1, pos2;
1817 	Gaplen *gaplen1i, *gaplen2j;
1818 
1819 	gaplen1i = gaplen1[i];
1820 	gaplen2j = gaplen2[j];
1821 
1822 	pfac = 0.0;
1823 	pfac2 = 0.0;
1824 //	for( k=0; gaplen2[j]&&(gl=gaplen2[j][k].len)!=-1; k++ ) // ososugi!  hash ni atode henkou
1825 	if( gaplen2j ) for( k=0; (gaplen2j[k].idatnext)!=-1; k++ ) // ososugi!  hash ni atode henkou
1826 	{
1827 #if DEBUG
1828 		int gl;
1829 		pos2 = gaplen2j[k].relend;
1830 		gl = gaplen2j[k].len;
1831 		if( disp ) reporterr( "gaplen2[][].len=%d, .relend=%d, .freq=%f\n", gaplen2[j][k].len, gaplen2[j][k].relend, gaplen2[j][k].freq );
1832 		if( disp ) reporterr( "gl = %d, newgaplen=%d\n", gl, newgaplen );
1833 #endif
1834 		if( (pos2=gaplen2[j][k].relend) != 0 ) continue;
1835 
1836 		pfac1 = 1.0;
1837 		pfac10 = 1.0;
1838 		if( gaplen1i ) for( l=0; (gaplen1i[l].idatnext)!=-1; l++ ) // ososugi!  hash ni atode henkou
1839 		{
1840 			pos1 = gaplen1i[l].relend;
1841 			pfac10 -= gaplen1i[l].freq;
1842 #if DEBUG
1843 			if( disp ) reporterr( "gaplen1[][].len=%d, .relend=%d, .freq=%f\n", gaplen1[i][l].len, gaplen1[i][l].relend, gaplen1[i][l].freq );
1844 #endif
1845 			if( newgaplen + gaplen1i[l].len - (pos1) > gaplen2j[k].len - (pos2) ) pfac1 -= gaplen1i[l].freq;
1846 //			reporterr( "pfac1 = %f\n", pfac1 );
1847 		}
1848 		pfac += pfac1 * gaplen2j[k].freq;
1849 
1850 
1851 /* ???? */
1852 		if( newgaplen >= gaplen2j[k].len - (pos2-1) )  // >= or >??
1853 		{
1854 			pfac -= pfac10 * gaplen2j[k].freq;
1855 //			reporterr( "Hit! pfac1 = %f\n", pfac1 );
1856 		}
1857 /* ???? */
1858 
1859 
1860 //		if( gaplen2[j][k].relend == -1 ) pfac += gaplen2[j][k].freq;
1861 	}
1862 
1863 	return( pfac );
1864 
1865 #endif
1866 }
1867 
1868 
calcpfacnoidatend(Gaplen ** gaplen1,Gaplen ** gaplen2,int i,int j,char * seq1,char * seq2,int disp)1869 static double calcpfacnoidatend( Gaplen **gaplen1, Gaplen **gaplen2, int i, int j, char *seq1, char *seq2, int disp ) // seq1 to seq2 ha debug you
1870 {
1871 	double pfac, pfac1, pfac2;
1872 	int k, l, pos1, pos2;
1873 	Gaplen *gaplen1i, *gaplen2j, *g1, *g2;
1874 
1875 	gaplen1i = gaplen1[i];
1876 	gaplen2j = gaplen2[j];
1877 
1878 #if DEBUG
1879 	if( disp )
1880 	{
1881 		reporterr( "seq1[0] = %s\n", seq1 );
1882 		reporterr( "seq2[0] = %s\n", seq2 );
1883 		reporterr( "i,j=%d,%d\n", i, j );
1884 
1885 		reporterr( "In calcpfacnoidatend(), gaplen1[%d(%c)] = \n", i, seq1[i] );
1886 		showgaplen( gaplen1, seqlen( seq1 ) );
1887 		for( k=0; gaplen1[i]&&gaplen1[i][k].idatnext!=-1; k++ )
1888 		{
1889 			pos1 = gaplen1[i][k].relend;
1890 			reporterr( ".len=%d, .relend=%d, .freq=%f (i=%d)\n", gaplen1[i][k].len, gaplen1[i][k].relend, gaplen1[i][k].freq, i );
1891 		}
1892 
1893 		reporterr( "In calcpfacnoidatend(), gaplen2[%d(%c)] = \n", j, seq2[j] );
1894 		showgaplen( gaplen2, seqlen( seq2 ) );
1895 		for( k=0; gaplen2[j]&&gaplen2[j][k].idatnext!=-1; k++ )
1896 		{
1897 			pos2 = gaplen2[j][k].relend;
1898 			reporterr( ".len=%d, .relend=%d (j=%d)\n", gaplen2[j][k].len, gaplen2[j][k].relend, j );
1899 			reporterr( ".freq=%f\n", gaplen2[j][k].freq );
1900 		}
1901 	}
1902 #endif
1903 
1904 #if 1
1905 	pfac1 = pfac2 = 0.0;
1906 	if( gaplen1i ) for( k=0; (g1=gaplen1i+k)->idatnext!=-1; k++ )
1907 	{
1908 		if( (pos1=g1->relend) == 0 ) pfac1 += g1->freq;
1909 	}
1910 
1911 	if( gaplen2j ) for( l=0; (g2=gaplen2j+l)->idatnext!=-1; l++ ) // ososugi!  hash ni atode henkou
1912 	{
1913 		if( (pos2=g2->relend) == 0 ) pfac2 += g2->freq;
1914 	}
1915 #if DEBUG
1916 	reporterr( "\n\nInitial pfac1,pfac2 (%c%d,%c%d) = %f, %f\n", seq1[i], i, seq2[j], j, pfac1, pfac2 );
1917 #endif
1918 	pfac = pfac1 * pfac2 + pfac1 * (1-pfac2) + pfac2 * (1-pfac1);
1919 #if DEBUG
1920 	reporterr( "\n\nInitial pfac (%d,%d) = %f\n", i, j, pfac );
1921 #endif
1922 
1923 //	if( pfac ) reporterr( "i,j=%d,%d, Cancel (eq len)? pfac = %f -> ", i, j, pfac );
1924 	if( gaplen1i ) for( k=0; (g1=gaplen1i+k)->idatnext!=-1; k++ ) // ososugi!  hash ni atode henkou
1925 	{
1926 		pos1=g1->relend;
1927 		if( gaplen2j ) for( l=0; (g2=gaplen2j+l)->idatnext!=-1; l++ ) // ososugi!  hash ni atode henkou
1928 		{
1929 			pos2 = gaplen2j[l].relend;
1930 			if     ( pos1 == 0 && pos2 == 0 && g1->len == g2->len ) pfac -= g1->freq * g2->freq;
1931 			else if( pos1 == 0 && pos2 != 0 && g2->len - (pos2-1) > g1->len ) pfac -= g1->freq * g2->freq;
1932 			else if( pos1 != 0 && pos2 == 0 && g1->len - (pos1-1) > g2->len ) pfac -= g1->freq * g2->freq;
1933 		}
1934 	}
1935 
1936 #else
1937 
1938 	pfac1 = pfac2 = 0.0;
1939 	if( gaplen1i ) for( k=0; (gaplen1i[k].idatnext)!=-1; k++ )
1940 	{
1941 		if( gaplen1i[k].relend == 0 ) pfac1 += gaplen1[i][k].freq;
1942 	}
1943 
1944 	if( gaplen2j ) for( l=0; (gaplen2j[l].idatnext)!=-1; l++ ) // ososugi!  hash ni atode henkou
1945 	{
1946 		if( gaplen2j[l].relend == 0 ) pfac2 += gaplen2[j][l].freq;
1947 	}
1948 #if DEBUG
1949 	reporterr( "\n\nInitial pfac1,pfac2 (%c%d,%c%d) = %f, %f\n", seq1[i], i, seq2[j], j, pfac1, pfac2 );
1950 #endif
1951 	pfac = pfac1 * pfac2 + pfac1 * (1-pfac2) + pfac2 * (1-pfac1);
1952 #if DEBUG
1953 	reporterr( "\n\nInitial pfac (%d,%d) = %f\n", i, j, pfac );
1954 #endif
1955 
1956 #if 1
1957 //	if( pfac ) reporterr( "i,j=%d,%d, Cancel (eq len)? pfac = %f -> ", i, j, pfac );
1958 	if( gaplen1i ) for( k=0; (gaplen1i[k].idatnext)!=-1; k++ ) // ososugi!  hash ni atode henkou
1959 	{
1960 		pos1=gaplen1i[k].relend;
1961 		if( gaplen2j ) for( l=0; (gaplen2j[l].idatnext)!=-1; l++ ) // ososugi!  hash ni atode henkou
1962 		{
1963 			pos2 = gaplen2j[l].relend;
1964 			if     ( pos1 == 0 && pos2 == 0 && gaplen1i[k].len == gaplen2j[l].len ) pfac -= gaplen1i[k].freq * gaplen2j[l].freq;
1965 			else if( pos1 == 0 && pos2 != 0 && gaplen2j[l].len - (pos2-1) > gaplen1i[k].len ) pfac -= gaplen1i[k].freq * gaplen2j[l].freq;
1966 			else if( pos1 != 0 && pos2 == 0 && gaplen1i[k].len - (pos1-1) > gaplen2j[l].len ) pfac -= gaplen1i[k].freq * gaplen2j[l].freq;
1967 		}
1968 	}
1969 #endif
1970 #endif
1971 
1972 #if DEBUG
1973 	reporterr( "\n\nFinal pfac1,pfac2 (%c%d,%c%d, straight) = %f\n\n", seq1[i], i, seq2[j], j, pfac );
1974 #endif
1975 	return( pfac );
1976 }
1977 
1978 
extendgaplencompactx(Gaplen ** cpy,Gaplen ** orig,int start)1979 static void extendgaplencompactx( Gaplen **cpy, Gaplen **orig, int start )
1980 {
1981 	Gaplen *opt, *cpt;
1982 	int l, id;
1983 #if DEBUG
1984 	Gaplen cpybk;
1985 #endif
1986 
1987 //	if( start < 0 ) start = 0;
1988 
1989 	if( orig[start] == NULL )
1990 	{
1991 		if( cpy[start] )
1992 		{
1993 			free( cpy[start] );
1994 			cpy[start] = NULL;
1995 		}
1996 		return;
1997 	}
1998 
1999 
2000 #if DEBUG
2001 	reporterr( "At first, cpy -> \n" );
2002 	showgaplen( cpy, 100 );
2003 	reporterr( "Look at %d \n", start );
2004 #endif
2005 
2006 	if( cpy[start] == NULL )
2007 	{
2008 		l = orig[start][0].npat;
2009 
2010 		cpy[start] = realloc( cpy[start], (l+2) * sizeof( Gaplen ) );
2011 
2012 #if 0
2013 		for( l=0; (gl=orig[start][l].idatend)!=-1; l++ )
2014 			cpy[start][l] = orig[start][l]; // freq ha pointer de copy
2015 		cpy[start][l] = orig[start][l]; // dekiru?
2016 #else
2017 		for( opt = orig[start],cpt = cpy[start]; opt->idatnext!=-1; )
2018 			*cpt++ = *opt++;
2019 		*cpt = *opt;
2020 #endif
2021 	}
2022 
2023 #if DEBUG
2024 	cpybk = cpy[start][0];
2025 #endif
2026 
2027 #if 0
2028 	for( l=0; (opt=orig[start]+l)->idatend!=-1; l++ )
2029 	{
2030 		if( (pos=opt->relend) == 0 ) continue;
2031 
2032 		if( cpy[posplus=start+pos] != NULL )
2033 		{
2034 			id = opt->idatend;
2035 //			reporterr( "cpy[%d][%d].len: %d -> %d (relend=%d)\n", start, l, cpy[start][l].len, cpy[posplus][id].len, pos );
2036 			cpy[start][l].len = cpy[posplus][id].len; // Ato de posplus wo tsukawanaiyouni henkou.
2037 			continue; // HITSUYOU!!!
2038 		}
2039 		else
2040 		{
2041 //			reporterr( "cpy[%d][%d].len: %d (relend=%d)\n", start, l, cpy[start][l].len, pos );
2042 		}
2043 
2044 #if 0
2045 		for( k=0; orig[start+pos][k].idatend!=-1; k++ )
2046 			;
2047 #else
2048 		optplus = orig[posplus];
2049 		k = optplus->npat;
2050 #endif
2051 
2052 
2053 		cptplus = cpy[posplus] = realloc( cpy[posplus], (k+2) * sizeof( Gaplen ) );
2054 //		cptplus = realloc( cptplus, (k+2) * sizeof( Gaplen ) );
2055 
2056 #if 0
2057 		for( k=0; optplus[k].idatend!=-1; k++ )
2058 		{
2059 			cptplus[k] = optplus[k]; // dekiru?
2060 		}
2061 		cptplus[k] = optplus[k]; // dekiru?
2062 #else
2063 		while( optplus->idatend!=-1 ) *cptplus++ = *optplus++;
2064 		*cptplus = *optplus;
2065 #endif
2066 	}
2067 #endif
2068 
2069 
2070 	if( start == 0 ) return;
2071 	if( cpy[start-1] == NULL ) return;
2072 
2073 #if DEBUG
2074 	reporterr( "cpy -> \n" );
2075 	showgaplen( cpy, 100 );
2076 	reporterr( "Look at %d \n", start );
2077 #endif
2078 
2079 	for( l=0; orig[start][l].idatnext!=-1; l++ )
2080 	{
2081 		if( (id=orig[start][l].idatprev) == -1 ) continue;
2082 
2083 //		if( cpy[start][l].relend != 0 ) cpy[start][l].len = cpy[start-1][id].len; // Shinchou ni
2084 		cpy[start][l].len = cpy[start-1][id].len; // Shinchou ni
2085 
2086 //		if( cpy[start][l].len != cpy[start-1][id].len )
2087 #if DEBUG
2088 		if( 1 || cpy[start][l].len != cpy[start-1][id].len )
2089 		{
2090 			reporterr( "Check!! cpy[%d][%d].len=%d, but [start-1][].len=%d, relend=%d\n", start, l, cpy[start][l].len, cpy[start-1][id].len, cpy[start][l].relend );
2091 			reporterr( "orig[%d][%d].len=%d, relend=%d\n", start, l, orig[start][l].len, orig[start][l].relend );
2092 			reporterr( "cpybk.len=%d, relend=%d\n", cpybk.len, cpybk.relend );
2093 
2094 		}
2095 		else
2096 		{
2097 //			reporterr( "OK, cpy[%d][%d].len=%d, relend=%d\n", start, l, cpy[start][l].len, cpy[start][l].relend );
2098 		}
2099 #endif
2100 	}
2101 
2102 }
2103 
2104 
2105 #if USEGAPLENHALFORMTX
extendgaplenpartly(Gaplen ** cpy,Gaplen ** orig,int start,int end)2106 static void extendgaplenpartly( Gaplen **cpy, Gaplen **orig, int start, int end )
2107 {
2108 	int i, l, gl, extrascope;
2109 	Gaplen *pt;
2110 
2111 	if( start < 0 ) start = 0;
2112 //	for( i=start; i<=end; i++ )
2113 //	{
2114 //		if( cpy[i] == (Gaplen *)1 )
2115 //		{
2116 //			end = i-1;
2117 //			break;
2118 ////			reporterr( "Okashii! i=%d\n", i );
2119 ////			exit( 1 );
2120 //		}
2121 //		if( cpy[i] ) free( cpy[i] );
2122 //		cpy[i] = NULL;
2123 //	}
2124 
2125 
2126 	extrascope = 0;
2127 #if 0
2128 	for( i=start; i<=end; i++ ) if( orig[i] )
2129 	{
2130 		for( pt=orig[i]; (pt->idatend)!=-1; )
2131 		{
2132 			if( (gl=pt++->relend) > extrascope ) extrascope = i+gl-end+1;
2133 		}
2134 //		extrascope = 10; // Kinji
2135 
2136 	}
2137 #else
2138 	if( orig[end] )
2139 	{
2140 		for( pt=orig[end]; (pt->idatend)!=-1; )
2141 		{
2142 			if( (gl=pt++->relend) > extrascope ) extrascope = gl;
2143 		}
2144 //		extrascope = 10; // Kinji
2145 
2146 	}
2147 #endif
2148 	end += extrascope;
2149 
2150 	for( i=start; i<=end; i++ )
2151 	{
2152 		if( cpy[i] != NULL ) continue;
2153 
2154 		if( orig[i] == NULL )
2155 		{
2156 			if( cpy[i] ) free( cpy[i] ); // muda dakedo
2157 			cpy[i] = NULL;
2158 			continue;
2159 		}
2160 
2161 		for( l=0; (gl=orig[i][l].idatend)!=-1; l++ )
2162 			;
2163 
2164 		cpy[i] = realloc( cpy[i], (l+2) * sizeof( Gaplen ) );
2165 //		cpy[i] = calloc( sizeof( Gaplen ), l+2 );
2166 
2167 		for( l=0; (gl=orig[i][l].idatend)!=-1; l++ )
2168 		{
2169 #if 1
2170 			cpy[i][l] = orig[i][l]; // freq ha pointer de copy
2171 #else
2172 			cpy[i][l].len = gl;
2173 			cpy[i][l].relend = orig[i][l].relend;
2174 			cpy[i][l].freq = orig[i][l].freq;
2175 			cpy[i][l].gapidatend = orig[i][l].gapidatend;
2176 #endif
2177 
2178 //			reporterr( "i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend );
2179 		}
2180 		cpy[i][l] = orig[i][l]; // dekiru?
2181 //		cpy[i][l].relend = -1;
2182 //		cpy[i][l].len = -1;
2183 	}
2184 
2185 }
2186 #endif
2187 
duplicategaplencompactx(Gaplen ** cpy,Gaplen ** orig,int maxlen,int start,int end)2188 static void duplicategaplencompactx( Gaplen **cpy, Gaplen **orig, int maxlen, int start, int end )
2189 {
2190 	int i, l;
2191 
2192 
2193 	if( start < 0 ) start = 0;
2194 	for( i=start; i<=end; i++ )
2195 	{
2196 //		reporterr( "i=%d / %d\n", i, maxlen );
2197 		if( cpy[i] == (Gaplen *)1 )
2198 		{
2199 			end = i-1;
2200 			break;
2201 //			reporterr( "Okashii! i=%d\n", i );
2202 //			exit( 1 );
2203 		}
2204 		if( cpy[i] ) free( cpy[i] );
2205 		cpy[i] = NULL;
2206 	}
2207 
2208 	for( i=start; i<=end; i++ )
2209 	{
2210 		if( orig[i] == NULL )
2211 		{
2212 			if( cpy[i] ) free( cpy[i] ); // muda dakedo
2213 			cpy[i] = NULL;
2214 			continue;
2215 		}
2216 
2217 #if 0
2218 		for( l=0; (gl=orig[i][l].idatend)!=-1; l++ )
2219 			;
2220 #else
2221 		l = orig[i][0].npat;
2222 #endif
2223 
2224 		cpy[i] = realloc( cpy[i], (l+2) * sizeof( Gaplen ) );
2225 //		cpy[i] = calloc( sizeof( Gaplen ), l+2 );
2226 
2227 		for( l=0; orig[i][l].idatnext!=-1; l++ )
2228 		{
2229 			cpy[i][l] = orig[i][l]; // freq ha pointer de copy
2230 //			reporterr( "i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend );
2231 		}
2232 		cpy[i][l] = orig[i][l]; // dekiru?
2233 //		cpy[i][l].relend = -1;
2234 //		cpy[i][l].len = -1;
2235 	}
2236 
2237 	return;
2238 }
2239 
2240 
2241 
2242 #if USEGAPLENHALFORMTX
duplicategaplenpartly(Gaplen ** cpy,Gaplen ** orig,int start,int end)2243 static void duplicategaplenpartly( Gaplen **cpy, Gaplen **orig, int start, int end )
2244 {
2245 	int i, l, gl, extrascope;
2246 	Gaplen *pt;
2247 
2248 	if( start < 0 ) start = 0;
2249 	for( i=start; i<=end; i++ )
2250 	{
2251 		if( cpy[i] == (Gaplen *)1 )
2252 		{
2253 			end = i-1;
2254 			break;
2255 //			reporterr( "Okashii! i=%d\n", i );
2256 //			exit( 1 );
2257 		}
2258 		if( cpy[i] ) free( cpy[i] );
2259 		cpy[i] = NULL;
2260 	}
2261 
2262 
2263 	extrascope = 0;
2264 #if 0
2265 	for( i=start; i<=end; i++ ) if( orig[i] )
2266 	{
2267 		for( pt=orig[i]; (pt->idatend)!=-1; )
2268 		{
2269 			if( (gl=pt++->relend) > extrascope ) extrascope = i+gl-end+1;
2270 		}
2271 //		extrascope = 10; // Kinji
2272 
2273 	}
2274 #else
2275 	if( orig[end] )
2276 	{
2277 		for( pt=orig[end]; (pt->idatend)!=-1; )
2278 		{
2279 			if( (gl=pt++->relend) > extrascope ) extrascope = gl;
2280 		}
2281 //		extrascope = 10; // Kinji
2282 
2283 	}
2284 #endif
2285 	end += extrascope;
2286 
2287 	for( i=start; i<=end; i++ )
2288 	{
2289 		if( orig[i] == NULL )
2290 		{
2291 			if( cpy[i] ) free( cpy[i] ); // muda dakedo
2292 			cpy[i] = NULL;
2293 			continue;
2294 		}
2295 
2296 		for( l=0; (gl=orig[i][l].idatend)!=-1; l++ )
2297 			;
2298 
2299 		cpy[i] = realloc( cpy[i], (l+2) * sizeof( Gaplen ) );
2300 //		cpy[i] = calloc( sizeof( Gaplen ), l+2 );
2301 
2302 		for( l=0; (gl=orig[i][l].idatend)!=-1; l++ )
2303 		{
2304 #if 1
2305 			cpy[i][l] = orig[i][l]; // freq ha pointer de copy
2306 #else
2307 			cpy[i][l].len = gl;
2308 			cpy[i][l].relend = orig[i][l].relend;
2309 			cpy[i][l].freq = orig[i][l].freq;
2310 			cpy[i][l].gapidatend = orig[i][l].gapidatend;
2311 #endif
2312 
2313 //			reporterr( "i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend );
2314 		}
2315 		cpy[i][l] = orig[i][l]; // dekiru?
2316 //		cpy[i][l].relend = -1;
2317 //		cpy[i][l].len = -1;
2318 	}
2319 
2320 }
2321 #endif
2322 
2323 
gaplenextendnoidatend(Gaplen ** cpy,int gapstartpos,int insertionlen)2324 static void gaplenextendnoidatend( Gaplen **cpy, int gapstartpos, int insertionlen )
2325 {
2326 	int l, id, idn, pos, len;
2327 
2328 #if 0
2329 //	reporterr( "inserting %d gaps at position %d\n", insertionlen, gapstartpos );
2330 	for( l=0; cpy[gapstartpos] && (id=cpy[gapstartpos][l].idatend) !=-1; l++ )
2331 	{
2332 		pos = cpy[gapstartpos][l].relend;
2333 		cpy[gapstartpos+pos][id].len += insertionlen;
2334 	}
2335 #endif
2336 
2337 #if 1
2338 	for( l=0; cpy[gapstartpos] && (id=cpy[gapstartpos][l].idatnext) !=-1; l++ )
2339 	{
2340 		len = cpy[gapstartpos][l].len + insertionlen;
2341 //		reporterr( "ext\n" );
2342 		for( pos=gapstartpos, idn=l; cpy[pos] != NULL && cpy[pos][idn].relend != 0; pos++ )
2343 		{
2344 //			reporterr( "%d, plus %d %d->%d\n", pos, insertionlen, cpy[pos][idn].len, cpy[pos][idn].len+insertionlen );
2345 			cpy[pos][idn].len = len;
2346 			idn = cpy[pos][idn].idatnext;
2347 //			if( pos == gapstartpos + 1 ) break;
2348 			break;
2349 		}
2350 //		reporterr( "end\n" );
2351 
2352 		idn = cpy[gapstartpos][l].idatprev;
2353 		if( gapstartpos != 0 && idn != -1 && cpy[gapstartpos-1] ) cpy[gapstartpos-1][idn].len = len;
2354 	}
2355 #endif
2356 }
2357 
2358 
2359 #if USEGAPLENHALFORMTX
2360 
gaplenextend(Gaplen ** cpy,int gapstartpos,int insertionlen)2361 static void gaplenextend( Gaplen **cpy, int gapstartpos, int insertionlen )
2362 {
2363 	int l, id, idn, pos, len;
2364 
2365 #if 1
2366 //	reporterr( "inserting %d gaps at position %d\n", insertionlen, gapstartpos );
2367 	for( l=0; cpy[gapstartpos] && (id=cpy[gapstartpos][l].idatend) !=-1; l++ )
2368 	{
2369 		pos = cpy[gapstartpos][l].relend;
2370 		cpy[gapstartpos+pos][id].len += insertionlen;
2371 	}
2372 #endif
2373 
2374 #if 1
2375 	for( l=0; cpy[gapstartpos] && (id=cpy[gapstartpos][l].idatend) !=-1; l++ )
2376 	{
2377 		len = cpy[gapstartpos][l].len + insertionlen;
2378 //		reporterr( "ext\n" );
2379 		for( pos=gapstartpos, idn=l; cpy[pos] != NULL && cpy[pos][idn].relend != 0; pos++ )
2380 		{
2381 //			reporterr( "%d, plus %d %d->%d\n", pos, insertionlen, cpy[pos][idn].len, cpy[pos][idn].len+insertionlen );
2382 			cpy[pos][idn].len = len;
2383 			idn = cpy[pos][idn].idatnext;
2384 //			if( pos == gapstartpos + 1 ) break;
2385 //			break;
2386 		}
2387 //		reporterr( "end\n" );
2388 
2389 		idn = cpy[gapstartpos][l].idatprev;
2390 		if( gapstartpos != 0 && idn != -1 && cpy[gapstartpos-1] ) cpy[gapstartpos-1][idn].len = len;
2391 	}
2392 #endif
2393 }
2394 #endif
2395 
copygaplencompactx(Gaplen ** cpy,Gaplen ** orig,int seqlen,int gapstartpos,int insertionlen,int posincopy,int posinori)2396 static void copygaplencompactx( Gaplen **cpy, Gaplen **orig, int seqlen, int gapstartpos, int insertionlen, int posincopy, int posinori )
2397 {
2398 	Gaplen *pt, *cpt;
2399 
2400 
2401 
2402 #if DEBUG
2403 	reporterr( "At the head of copygaplencompactx, cpy=\n" );
2404 	showgaplen( cpy+posincopy, 100 );
2405 	reporterr( "At the head of copygaplencompactx, orig=\n" );
2406 	showgaplen( orig+posinori, 100 );
2407 	reporterr( "posinori=%d\n", posinori );
2408 #endif
2409 
2410 	if( orig[posinori] == NULL ) return;
2411 
2412 //	for( pt=orig[posinori],cpt=cpy[posincopy]; pt->relend==0; ) // zenhan ni relend=0 ga matomatteirukara.
2413 	for( pt=orig[posinori],cpt=cpy[posincopy]; pt->idatnext!=-1; ) // kouhan mo copy
2414 	{
2415 		cpt++->len = pt++->len;
2416 	}
2417 
2418 
2419 #if 0
2420 	for( l=0; (id=orig[posinori][l].idatend)!=-1; l++ )
2421 	{
2422 		pos = orig[posinori][l].relend;
2423 		if( pos == 0 ) continue;
2424 		if( orig[posinori+pos] == NULL )
2425 		{
2426 			reporterr( "Okashii\n" );
2427 			PFACERROR = 1;
2428 			continue;
2429 		}
2430 
2431 #if 0
2432 		for( k=0; orig[posinori+pos][k].relend==0; k++ ) // zenhan dake
2433 		{
2434 			cpy[posincopy+pos][k].len = orig[posinori+pos][k].len; // dekiru?
2435 		}
2436 #else
2437 		cpy[posincopy+pos][id].len = orig[posinori+pos][id].len; // dekiru?
2438 #endif
2439 	}
2440 #endif
2441 
2442 
2443 	if( gapstartpos == -1 ) gapstartpos = posincopy;
2444 	gaplenextendnoidatend( cpy, gapstartpos, insertionlen );
2445 
2446 #if DEBUG
2447 	reporterr( "At the end of copygaplencompactx, cpy=\n" );
2448 	showgaplen( cpy+posincopy, 100 );
2449 #endif
2450 }
2451 
2452 
2453 #if USEGAPLENHALF
copygaplenrestricted_zurasu(Gaplen ** cpy,Gaplen ** orig,int seqlen,int gapstartpos,int insertionlen,int startincopy,int endincopy,int startinori,int endinori)2454 static void copygaplenrestricted_zurasu( Gaplen **cpy, Gaplen **orig, int seqlen, int gapstartpos, int insertionlen, int startincopy, int endincopy, int startinori, int endinori )
2455 {
2456 	int i, extrascope, gl, j;
2457 	int zure, newend;
2458 	Gaplen *pt, *cpt;
2459 //	int ncopied = 0;
2460 
2461 #if 0
2462 // mae houkou nimo renzoku gap de enchou suru hitsuyou ga aru to omou.
2463 	for( i=startinori-1; 0<=i&&i<=seqlen; i-- )
2464 	{
2465 //		reporterr( "i=%d\n", i );
2466 		if( orig[i] == NULL ) break;
2467 		for( pt=orig[i],cpt=cpy[i]; (gl=pt++->len)!=-1; ) cpt++->len = gl;
2468 	}
2469 #endif
2470 
2471 	zure = startincopy - startinori; // end ha check shinai
2472 
2473 //	int ncopied = 0;
2474 	if( orig[endinori] )
2475 	{
2476 		extrascope = 0;
2477 		for( pt=orig[endinori]; (pt->idatend)!=-1; )
2478 		{
2479 			if( (gl=pt++->relend) > extrascope ) extrascope = gl;
2480 		}
2481 //		extrascope = 10; // Kinji
2482 
2483 		newend = endinori + extrascope;
2484 	}
2485 	else newend = endinori;
2486 
2487 //	reporterr( "ncopy = %d\n", newend - startinori );
2488 //
2489 #if 0 // extra end wo shizen ni kimereba iranai
2490 	if( newend > seqlen ) newend = seqlen;
2491 //	if( startinori < 0 ) startinori = 0;
2492 #endif
2493 
2494 	for( i=startinori, j=startincopy; i<=newend; i++, j++ )
2495 	{
2496 		if( orig[i] == NULL ) continue;
2497 
2498 //		ncopied += 1;
2499 
2500 #if 0
2501 		for( pt=orig[i],cpt=cpy[i]; (gl=pt++->len)!=-1; )
2502 			cpt++->len = gl;
2503 #else
2504 		for( pt=orig[i],cpt=cpy[j]; pt->relend==0; ) // zenhan ni relend=0 ga matomatteirukara.
2505 //		int k;
2506 //		for( k=0; orig[i][k].relend==0; k++ ) // zenhan ni relend=0 ga matomatteirukara.
2507 		{
2508 			cpt++->len = pt++->len;
2509 //			reporterr( "i=%d, k=%d\n", i, k );
2510 //			cpy[i][k].len = orig[i][k].len;
2511 		}
2512 #endif
2513 	}
2514 
2515 
2516 
2517 #if 0
2518 	for( i=0; i<=seqlen; i++ )
2519 	{
2520 		for( l=0; cpy[i]&&(gl=cpy[i][l].len)!=-1; l++ )
2521 			reporterr( "after copy, i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend );
2522 	}
2523 #endif
2524 
2525 	if( gapstartpos < 0 ) return;
2526 
2527 	gaplenextend( cpy, gapstartpos, insertionlen );
2528 
2529 
2530 
2531 //	return;
2532 
2533 
2534 // TEST
2535 //	for( i=endinori+1; i<=newend; i++ )
2536 	for( i=endincopy+1; i<=newend+zure; i++ )
2537 	{
2538 		if( cpy[i] == NULL ) continue;
2539 		for( j=0; cpy[i][j].idatend!=-1; j++ )
2540 		{
2541 			if( cpy[i][j].relend == 0 )
2542 			{
2543 				break;
2544 			}
2545 		}
2546 		if( cpy[i][j].idatend == -1 )
2547 		{
2548 			free( cpy[i] );
2549 			cpy[i] = NULL;
2550 		}
2551 	}
2552 
2553 
2554 
2555 
2556 
2557 
2558 #if 0
2559 	reporterr( "\n" );
2560 	for( i=0; i<=seqlen; i++ )
2561 	{
2562 		for( l=0; cpy[i]&&(gl=cpy[i][l].len)!=-1; l++ )
2563 			reporterr( "after add, i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend );
2564 	}
2565 #endif
2566 }
2567 #endif
2568 
2569 #if USEGAPLENHALFORMTX
copygaplenrestricted(Gaplen ** cpy,Gaplen ** orig,int seqlen,int gapstartpos,int insertionlen,int scopestart,int scopeend)2570 static void copygaplenrestricted( Gaplen **cpy, Gaplen **orig, int seqlen, int gapstartpos, int insertionlen, int scopestart, int scopeend )
2571 {
2572 	int i, j, extrascope, gl, endinori, newend;
2573 	Gaplen *pt, *cpt;
2574 //	int ncopied = 0;
2575 
2576 #if 0
2577 // mae houkou nimo renzoku gap de enchou suru hitsuyou ga aru to omou.
2578 	for( i=scopestart-1; 0<=i&&i<=seqlen; i-- )
2579 	{
2580 //		reporterr( "i=%d\n", i );
2581 		if( orig[i] == NULL ) break;
2582 		for( pt=orig[i],cpt=cpy[i]; (gl=pt++->len)!=-1; ) cpt++->len = gl;
2583 	}
2584 #endif
2585 
2586 //	int ncopied = 0;
2587 	endinori = scopeend;
2588 	if( orig[scopeend] )
2589 	{
2590 		extrascope = 0;
2591 		for( pt=orig[scopeend]; (pt->idatend)!=-1; )
2592 		{
2593 			if( (gl=pt++->relend) > extrascope ) extrascope = gl;
2594 		}
2595 //		extrascope = 10; // Kinji
2596 
2597 		scopeend += extrascope;
2598 	}
2599 	newend = scopeend;
2600 
2601 //	reporterr( "ncopy = %d\n", scopeend - scopestart );
2602 //
2603 #if 0 // extra end wo shizen ni kimereba iranai
2604 	if( scopeend > seqlen ) scopeend = seqlen;
2605 //	if( scopestart < 0 ) scopestart = 0;
2606 #endif
2607 
2608 	if( scopestart < 0 ) scopestart = 0;
2609 	for( i=scopestart; i<=scopeend; i++ )
2610 	{
2611 		if( orig[i] == NULL ) continue;
2612 
2613 //		ncopied += 1;
2614 
2615 #if 0
2616 		for( pt=orig[i],cpt=cpy[i]; (gl=pt++->len)!=-1; )
2617 			cpt++->len = gl;
2618 #else
2619 		for( pt=orig[i],cpt=cpy[i]; pt->relend==0; ) // zenhan ni relend=0 ga matomatteirukara.
2620 //		int k;
2621 //		for( k=0; orig[i][k].relend==0; k++ ) // zenhan ni relend=0 ga matomatteirukara.
2622 		{
2623 			cpt++->len = pt++->len;
2624 //			reporterr( "i=%d, k=%d\n", i, k );
2625 //			cpy[i][k].len = orig[i][k].len;
2626 		}
2627 #endif
2628 	}
2629 
2630 
2631 
2632 #if 0
2633 	for( i=0; i<=seqlen; i++ )
2634 	{
2635 		for( l=0; cpy[i]&&(gl=cpy[i][l].len)!=-1; l++ )
2636 			reporterr( "after copy, i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend );
2637 	}
2638 #endif
2639 
2640 	if( gapstartpos < 0 ) return;
2641 
2642 	gaplenextend( cpy, gapstartpos, insertionlen );
2643 
2644 	return;
2645 
2646 // TEST extra scope de tsukaunoha end dake?
2647 	for( i=endinori+1; i<=newend; i++ )
2648 	{
2649 		if( cpy[i] == NULL ) continue;
2650 		for( j=0; cpy[i][j].idatend!=-1; j++ )
2651 		{
2652 			if( cpy[i][j].relend == 0 ) break;
2653 		}
2654 		if( cpy[i][j].idatend == -1 )
2655 		{
2656 			free( cpy[i] );
2657 			cpy[i] = NULL;
2658 		}
2659 	}
2660 
2661 
2662 
2663 #if 0
2664 	reporterr( "\n" );
2665 	for( i=0; i<=seqlen; i++ )
2666 	{
2667 		for( l=0; cpy[i]&&(gl=cpy[i][l].len)!=-1; l++ )
2668 			reporterr( "after add, i=%d, l=%d, len=%d, freq=%f, relend=%d\n", i, l, cpy[i][l].len, cpy[i][l].freq, cpy[i][l].relend );
2669 	}
2670 #endif
2671 }
2672 #endif
2673 
2674 #if 1
freegaplenpartly(Gaplen ** mtx,int startpos,int endpos)2675 static void freegaplenpartly( Gaplen **mtx, int startpos, int endpos )
2676 {
2677 	int i;
2678 	Gaplen **pt;
2679 	if( startpos < 0 ) startpos = 0;
2680 
2681 	for( i=startpos; i<=endpos; i++ )
2682 	{
2683 		if( *(pt=mtx+i) == (Gaplen *)1 ) break;
2684 		if( *pt ) free( *pt );
2685 		*pt = NULL;
2686 	}
2687 }
2688 #else
freegaplenpartly(Gaplen ** mtx,int startpos,int endpos)2689 static void freegaplenpartly( Gaplen **mtx, int startpos, int endpos )
2690 {
2691 	int i;
2692 	if( startpos < 0 ) startpos = 0;
2693 
2694 	for( i=startpos; i<=endpos; i++ )
2695 	{
2696 		if( mtx[i] == (Gaplen *)1 ) break;
2697 		if( mtx[i] ) free( mtx[i] );
2698 		mtx[i] = NULL;
2699 	}
2700 }
2701 #endif
2702 
2703 
D__align(double ** n_dynamicmtx,char ** seq1,char ** seq2,double * eff1,double * eff2,int icyc,int jcyc,int alloclen,LocalHom *** localhom,double * impmatch,char * sgap1,char * sgap2,char * egap1,char * egap2,int * chudanpt,int chudanref,int * chudanres,int headgp,int tailgp)2704 double D__align( double **n_dynamicmtx, char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, LocalHom ***localhom, double *impmatch, char *sgap1, char *sgap2, char *egap1, char *egap2, int *chudanpt, int chudanref, int *chudanres, int headgp, int tailgp )
2705 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
2706 {
2707 
2708 //	int k;
2709 	register int i, j;
2710 
2711 
2712 
2713 
2714 	int lasti, lastj;      /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
2715 	int lgth1, lgth2;
2716 	int resultlen;
2717 	double wm = 0.0;   /* int ?????? */
2718 	double g;
2719 	double *currentw, *previousw;
2720 //	double fpenalty = (double)penalty;
2721 #if USE_PENALTY_EX
2722 	double fpenalty_ex = (double)penalty_ex;
2723 #endif
2724 #if 1
2725 	double *wtmp;
2726 	int *ijppt;
2727 	double *mjpt, *prept, *curpt;
2728 	int *mpjpt;
2729 #endif
2730 	static TLS double mi, *m;
2731 	static TLS int **ijp;
2732 	static TLS int mpi, *mp;
2733 	static TLS double *w1, *w2;
2734 	static TLS double *match;
2735 	static TLS double *initverticalw;    /* kufuu sureba iranai */
2736 	static TLS double *lastverticalw;    /* kufuu sureba iranai */
2737 	static TLS char **mseq1;
2738 	static TLS char **mseq2;
2739 	static TLS char **mseq;
2740 	static TLS double **cpmx1;
2741 	static TLS double **cpmx2;
2742 	static TLS int **intwork;
2743 	static TLS double **doublework;
2744 	static TLS int orlgth1 = 0, orlgth2 = 0;
2745 #if USEGAPLENHALF
2746 	Gaplen ****gaplen1half = NULL; // NULL ga iru to omou.
2747 	Gaplen ****gaplen2half = NULL; // NULL ga iru to omou.
2748 #endif
2749 #if USEGAPLENMTX
2750 	Gaplen ****gaplen1mtx = NULL; // NULL ga iru to omou.
2751 	Gaplen ****gaplen2mtx = NULL; // NULL ga iru to omou.
2752 #endif
2753 	static TLS Gaplen **gaplen1 = NULL; // NULL ga iru to omou.
2754 	static TLS Gaplen **gaplen2 = NULL; // NULL ga iru to omou.
2755 	static TLS Gaplen ***gaplen1jprev = NULL;
2756 	static TLS Gaplen ***gaplen2jprev = NULL;
2757 	static TLS Gaplen ***gaplen1jcurr = NULL;
2758 	static TLS Gaplen ***gaplen2jcurr = NULL;
2759 	static TLS Gaplen ***gaplen1icurr = NULL;
2760 	static TLS Gaplen ***gaplen2icurr = NULL;
2761 	static TLS Gaplen ***gaplen1jbestkamo = NULL;
2762 	static TLS Gaplen ***gaplen2jbestkamo = NULL;
2763 	static TLS Gaplen ***gaplen1ibestkamo = NULL;
2764 	static TLS Gaplen ***gaplen2ibestkamo = NULL;
2765 	static TLS Gaplen ***gaplen1jbest = NULL;
2766 	static TLS Gaplen ***gaplen2jbest = NULL;
2767 	double fpenalty = (double)penalty;
2768 	double fpenalty_shift = (double)penalty_shift;
2769 	static TLS Gaplen ****gaplens = NULL;
2770 
2771 	Gaplen ***gaplentmp = NULL;
2772 	int *warpis = NULL;
2773 	int *warpjs = NULL;
2774 	int *warpi = NULL;
2775 	int *warpj = NULL;
2776 	int *prevwarpi = NULL;
2777 	int *prevwarpj = NULL;
2778 	double *wmrecords = NULL;
2779 	double *prevwmrecords = NULL;
2780 	int warpn = 0;
2781 	int warpbase;
2782 	double curm = 0.0;
2783 	double *wmrecordspt, *wmrecords1pt, *prevwmrecordspt;
2784 	int *warpipt, *warpjpt;
2785 	int k;
2786 	double pfac, pfactmp;
2787 	int newgaplen;
2788 
2789 //	for( i=0; i<icyc; i++ ) fprintf( stderr, "%s, %f\n", seq1[i], eff1[i] );
2790 //	for( i=0; i<jcyc; i++ ) fprintf( stderr, "%s, %f\n", seq2[i], eff2[i] );
2791 
2792 //	reporterr( "\nsizeof(gaplen) = %d\n", sizeof( Gaplen ) );
2793 //	reporterr( "\nsizeof(int) = %d\n", sizeof( int ) );
2794 //	reporterr( "\nsizeof(double) = %d\n", sizeof( double ) );
2795 //	reporterr( "\nsizeof(double*) = %d\n", sizeof( double * ) );
2796 
2797 
2798 	if( seq1 == NULL )
2799 	{
2800 		if( orlgth1 )
2801 		{
2802 //			fprintf( stderr, "## Freeing local arrays in D__align\n" );
2803 			orlgth1 = 0;
2804 			orlgth2 = 0;
2805 
2806 			imp_match_init_strictD( NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0 );
2807 
2808 			free( mseq1 );
2809 			free( mseq2 );
2810 			FreeFloatVec( w1 );
2811 			FreeFloatVec( w2 );
2812 			FreeFloatVec( match );
2813 			FreeFloatVec( initverticalw );
2814 			FreeFloatVec( lastverticalw );
2815 
2816 			FreeFloatVec( m );
2817 			FreeIntVec( mp );
2818 
2819 			FreeCharMtx( mseq );
2820 
2821 			FreeFloatMtx( cpmx1 );
2822 			FreeFloatMtx( cpmx2 );
2823 
2824 			FreeFloatMtx( doublework );
2825 			FreeIntMtx( intwork );
2826 
2827 
2828 
2829 			free( gaplens );
2830 			if( gaplen1ibestkamo ) FreeGaplenCub( gaplen1ibestkamo ); gaplen1ibestkamo = NULL;
2831 			if( gaplen2ibestkamo ) FreeGaplenCub( gaplen2ibestkamo ); gaplen2ibestkamo = NULL;
2832 			if( gaplen1icurr ) FreeGaplenCub( gaplen1icurr ); gaplen1icurr = NULL;
2833 			if( gaplen2icurr ) FreeGaplenCub( gaplen2icurr ); gaplen2icurr = NULL;
2834 
2835 			if( gaplen1jprev ) FreeGaplenCub( gaplen1jprev ); gaplen1jprev = NULL;
2836 			if( gaplen2jprev ) FreeGaplenCub( gaplen2jprev ); gaplen2jprev = NULL;
2837 			if( gaplen1jcurr ) FreeGaplenCub( gaplen1jcurr ); gaplen1jcurr = NULL;
2838 			if( gaplen2jcurr ) FreeGaplenCub( gaplen2jcurr ); gaplen2jcurr = NULL;
2839 			if( gaplen1jbestkamo ) FreeGaplenCub( gaplen1jbestkamo ); gaplen1jbestkamo = NULL;
2840 			if( gaplen2jbestkamo ) FreeGaplenCub( gaplen2jbestkamo ); gaplen2jbestkamo = NULL;
2841 			if( gaplen1jbest ) FreeGaplenCub( gaplen1jbest ); gaplen1jbest = NULL;
2842 			if( gaplen2jbest ) FreeGaplenCub( gaplen2jbest ); gaplen2jbest = NULL;
2843 			if( gaplen1 ) FreeGaplenMtx( gaplen1, 1 ); gaplen1 = NULL;
2844 			if( gaplen2 ) FreeGaplenMtx( gaplen2, 1 ); gaplen2 = NULL;
2845 		}
2846 		else
2847 		{
2848 //			fprintf( stderr, "## Not allocated\n" );
2849 		}
2850 		return( 0.0 );
2851 	}
2852 
2853 
2854 	lgth1 = strlen( seq1[0] );
2855 	lgth2 = strlen( seq2[0] );
2856 
2857 
2858 	reporterr( "%d x %d sequences, len=%d, %d\n", icyc, jcyc, lgth1, lgth2 );
2859 
2860 
2861 #if 0
2862 	if( lgth1 == 0 || lgth2 == 0 )
2863 	{
2864 		fprintf( stderr, "WARNING (Aalignmm): lgth1=%d, lgth2=%d\n", lgth1, lgth2 );
2865 	}
2866 #endif
2867 	if( lgth1 == 0 && lgth2 == 0 )
2868 		return( 0.0 );
2869 
2870 	if( lgth1 == 0 )
2871 	{
2872 		for( i=0; i<icyc; i++ )
2873 		{
2874 			j = lgth2;
2875 			seq1[i][j] = 0;
2876 			while( j ) seq1[i][--j] = *newgapstr;
2877 //			fprintf( stderr, "seq1[i] = %s\n", seq1[i] );
2878 		}
2879 		return( 0.0 );
2880 	}
2881 
2882 	if( lgth2 == 0 )
2883 	{
2884 		for( i=0; i<jcyc; i++ )
2885 		{
2886 			j = lgth1;
2887 			seq2[i][j] = 0;
2888 			while( j ) seq2[i][--j] = *newgapstr;
2889 //			fprintf( stderr, "seq2[i] = %s\n", seq2[i] );
2890 		}
2891 		return( 0.0 );
2892 	}
2893 
2894 	warpbase = lgth1 + lgth2;
2895 	warpis = NULL;
2896 	warpjs = NULL;
2897 	warpn = 0;
2898 
2899 
2900 
2901 	if( trywarp )
2902 	{
2903 //		reporterr( "Not supported yet!\n" );
2904 //		exit( 1 );
2905 //		fprintf( stderr, "IN D__align, penalty_shift = %d\n", penalty_shift );
2906 		if( headgp == 0 || tailgp == 0 )
2907 		{
2908 			fprintf( stderr, "At present, headgp and tailgp must be 1 to allow shift.\n" );
2909 			exit( 1 );
2910 		}
2911 		wmrecords = AllocateFloatVec( lgth2+1 );
2912 		warpi = AllocateIntVec( lgth2+1 );
2913 		warpj = AllocateIntVec( lgth2+1 );
2914 		prevwmrecords = AllocateFloatVec( lgth2+1 );
2915 		prevwarpi = AllocateIntVec( lgth2+1 );
2916 		prevwarpj = AllocateIntVec( lgth2+1 );
2917 		for( i=0; i<lgth2+1; i++ ) wmrecords[i] = 0.0;
2918 		for( i=0; i<lgth2+1; i++ ) prevwmrecords[i] = 0.0;
2919 		for( i=0; i<lgth2+1; i++ ) prevwarpi[i] = -warpbase;
2920 		for( i=0; i<lgth2+1; i++ ) prevwarpj[i] = -warpbase;
2921 		for( i=0; i<lgth2+1; i++ ) warpi[i] = -warpbase;
2922 		for( i=0; i<lgth2+1; i++ ) warpj[i] = -warpbase;
2923 	}
2924 
2925 
2926 #if 0
2927 	fprintf( stderr, "####  eff in SA+++align\n" );
2928 	fprintf( stderr, "####  seq1[0] = %s\n", seq1[0] );
2929 	fprintf( stderr, "####  strlen( seq1[0] ) = %d\n", strlen( seq1[0] ) );
2930 	for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
2931 	fprintf( stderr, "####  seq2[0] = %s\n", seq2[0] );
2932 	fprintf( stderr, "####  strlen( seq2[0] ) = %d\n", strlen( seq2[0] ) );
2933 	for( i=0; i<jcyc; i++ ) fprintf( stderr, "eff2[%d] = %f\n", i, eff2[i] );
2934 #endif
2935 	if( orlgth1 == 0 )
2936 	{
2937 		mseq1 = AllocateCharMtx( njob, 0 );
2938 		mseq2 = AllocateCharMtx( njob, 0 );
2939 	}
2940 
2941 	if( lgth1 > orlgth1 || lgth2 > orlgth2 )
2942 	{
2943 		int ll1, ll2;
2944 
2945 
2946 		if( orlgth1 > 0 && orlgth2 > 0 )
2947 		{
2948 			FreeFloatVec( w1 );
2949 			FreeFloatVec( w2 );
2950 			FreeFloatVec( match );
2951 			FreeFloatVec( initverticalw );
2952 			FreeFloatVec( lastverticalw );
2953 
2954 			FreeFloatVec( m );
2955 			FreeIntVec( mp );
2956 
2957 			FreeCharMtx( mseq );
2958 
2959 			FreeFloatMtx( cpmx1 );
2960 			FreeFloatMtx( cpmx2 );
2961 
2962 			FreeFloatMtx( doublework );
2963 			FreeIntMtx( intwork );
2964 
2965 
2966 			free( gaplens );
2967 
2968 			if( gaplen1ibestkamo ) FreeGaplenCub( gaplen1ibestkamo ); gaplen1ibestkamo = NULL;
2969 			if( gaplen2ibestkamo ) FreeGaplenCub( gaplen2ibestkamo ); gaplen2ibestkamo = NULL;
2970 			if( gaplen1icurr ) FreeGaplenCub( gaplen1icurr ); gaplen1icurr = NULL;
2971 			if( gaplen2icurr ) FreeGaplenCub( gaplen2icurr ); gaplen2icurr = NULL;
2972 
2973 			if( gaplen1jcurr ) FreeGaplenCub( gaplen1jcurr ); gaplen1jcurr = NULL;
2974 			if( gaplen1jprev ) FreeGaplenCub( gaplen1jprev ); gaplen1jprev = NULL;
2975 			if( gaplen2jcurr ) FreeGaplenCub( gaplen2jcurr ); gaplen2jcurr = NULL;
2976 			if( gaplen2jprev ) FreeGaplenCub( gaplen2jprev ); gaplen2jprev = NULL;
2977 			if( gaplen1jbestkamo ) FreeGaplenCub( gaplen1jbestkamo ); gaplen1jbestkamo = NULL;
2978 			if( gaplen2jbestkamo ) FreeGaplenCub( gaplen2jbestkamo ); gaplen2jbestkamo = NULL;
2979 			if( gaplen1jbest ) FreeGaplenCub( gaplen1jbest ); gaplen1jbest = NULL;
2980 			if( gaplen2jbest ) FreeGaplenCub( gaplen2jbest ); gaplen2jbest = NULL;
2981 			if( gaplen1 ) FreeGaplenMtx( gaplen1, 1 ); gaplen1 = NULL;
2982 			if( gaplen2 ) FreeGaplenMtx( gaplen2, 1 ); gaplen2 = NULL;
2983 
2984 
2985 		}
2986 
2987 		ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
2988 		ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
2989 
2990 #if DEBUG
2991 		fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
2992 #endif
2993 
2994 		w1 = AllocateFloatVec( ll2+2 );
2995 		w2 = AllocateFloatVec( ll2+2 );
2996 		match = AllocateFloatVec( ll2+2 );
2997 
2998 		initverticalw = AllocateFloatVec( ll1+2 );
2999 		lastverticalw = AllocateFloatVec( ll1+2 );
3000 
3001 		m = AllocateFloatVec( ll2+2 );
3002 		mp = AllocateIntVec( ll2+2 );
3003 
3004 		mseq = AllocateCharMtx( njob, ll1+ll2 );
3005 
3006 		cpmx1 = AllocateFloatMtx( nalphabets, ll1+2 );
3007 		cpmx2 = AllocateFloatMtx( nalphabets, ll2+2 );
3008 
3009 #if FASTMATCHCALC
3010 		doublework = AllocateFloatMtx( MAX( ll1, ll2 )+2, nalphabets );
3011 		intwork = AllocateIntMtx( MAX( ll1, ll2 )+2, nalphabets+1 );
3012 #else
3013 		doublework = AllocateFloatMtx( nalphabets, MAX( ll1, ll2 )+2 );
3014 		intwork = AllocateIntMtx( nalphabets, MAX( ll1, ll2 )+2 );
3015 #endif
3016 
3017 #if DEBUG
3018 		fprintf( stderr, "succeeded\n" );
3019 #endif
3020 
3021 		orlgth1 = ll1 - 100;
3022 		orlgth2 = ll2 - 100;
3023 
3024 
3025 //		reporterr( "Allocating gaplen1 and gaplen2\n" );
3026 		gaplen1 = (Gaplen ** )calloc( ll1+2, sizeof( Gaplen * ) );
3027 		gaplen1[ll1+1] = (Gaplen *)1;
3028 		gaplen2 = (Gaplen ** )calloc( ll2+2, sizeof( Gaplen * ) );
3029 		gaplen2[ll2+1] = (Gaplen *)1;
3030 
3031 
3032 //		reporterr( "Allocating gaplen*\n" );
3033 		gaplen1ibestkamo = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) );
3034 		gaplen2ibestkamo = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) );
3035 		gaplen1icurr = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) );
3036 		gaplen2icurr = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) );
3037 		gaplen1jbestkamo = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
3038 		gaplen2jbestkamo = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
3039 		gaplen1jbest = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
3040 		gaplen2jbest = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
3041 		gaplen1jcurr = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
3042 		gaplen2jcurr = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
3043 		gaplen1jprev = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
3044 		gaplen2jprev = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
3045 
3046 		gaplens = calloc( sizeof( Gaplen ***), 12 );
3047 		gaplens[0] = gaplen1ibestkamo;
3048 		gaplens[1] = gaplen2ibestkamo;
3049 		gaplens[2] = gaplen1icurr;
3050 		gaplens[3] = gaplen2icurr;
3051 		gaplens[4] = gaplen1jbestkamo;
3052 		gaplens[5] = gaplen2jbestkamo;
3053 		gaplens[6] = gaplen1jbest;
3054 		gaplens[7] = gaplen2jbest;
3055 		gaplens[8] = gaplen1jcurr;
3056 		gaplens[9] = gaplen2jcurr;
3057 		gaplens[10] = gaplen1jprev;
3058 		gaplens[11] = gaplen2jprev;
3059 //		reporterr( "Allocation end\n" );
3060 	}
3061 
3062 	{
3063 		int ll1 = lgth1;
3064 		int ll2 = lgth2;
3065 
3066 //		reporterr( "Allocating gaplen*i\n" );
3067 		for(i=0; i<ll1+1; i++ )
3068 		{
3069 			gaplen1ibestkamo[i] = (Gaplen **)calloc( ll1+2-i, sizeof( Gaplen * ) );
3070 			for( k=0; k<ll1+1-i; k++ ) gaplen1ibestkamo[i][k] = NULL;
3071 			gaplen1ibestkamo[i][k] = (Gaplen *)1;
3072 
3073 			gaplen2ibestkamo[i] = (Gaplen **)calloc( ll2+2, sizeof( Gaplen * ) );
3074 			for( k=0; k<ll2+1; k++ ) gaplen2ibestkamo[i][k] = NULL;
3075 			gaplen2ibestkamo[i][k] = (Gaplen *)1;
3076 
3077 			gaplen1icurr[i] = (Gaplen **)calloc( ll1+2-i, sizeof( Gaplen * ) );
3078 			for( k=0; k<ll1+1-i; k++ ) gaplen1icurr[i][k] = NULL;
3079 			gaplen1icurr[i][k] = (Gaplen *)1;
3080 
3081 			gaplen2icurr[i] = (Gaplen **)calloc( ll2+2, sizeof( Gaplen * ) );
3082 			for( k=0; k<ll2+1; k++ ) gaplen2icurr[i][k] = NULL;
3083 			gaplen2icurr[i][k] = (Gaplen *)1;
3084 		}
3085 		gaplen1ibestkamo[ll1+1] = NULL;
3086 		gaplen2ibestkamo[ll1+1] = NULL;
3087 		gaplen1icurr[ll1+1] = NULL;
3088 		gaplen2icurr[ll1+1] = NULL;
3089 
3090 //		reporterr( "Allocating gaplen*j\n" );
3091 		for(i=0; i<ll2+1; i++ )
3092 		{
3093 			gaplen1jbestkamo[i] = (Gaplen **)calloc( ll1+2, sizeof( Gaplen * ) );
3094 			for( k=0; k<ll1+1; k++ ) gaplen1jbestkamo[i][k] = NULL;
3095 			gaplen1jbestkamo[i][k] = (Gaplen *)1;
3096 
3097 			gaplen2jbestkamo[i] = (Gaplen **)calloc( ll2+2-i, sizeof( Gaplen * ) );
3098 			for( k=0; k<ll2+1-i; k++ ) gaplen2jbestkamo[i][k] = NULL;
3099 			gaplen2jbestkamo[i][k] = (Gaplen *)1;
3100 
3101 			gaplen1jbest[i] = (Gaplen **)calloc( ll1+2, sizeof( Gaplen * ) );
3102 			for( k=0; k<ll1+1; k++ ) gaplen1jbest[i][k] = NULL;
3103 			gaplen1jbest[i][k] = (Gaplen *)1;
3104 
3105 			gaplen2jbest[i] = (Gaplen **)calloc( ll2+2-i, sizeof( Gaplen * ) );
3106 			for( k=0; k<ll2+1-i; k++ ) gaplen2jbest[i][k] = NULL;
3107 			gaplen2jbest[i][k] = (Gaplen *)1;
3108 
3109 			gaplen1jcurr[i] = (Gaplen **)calloc( ll1+2, sizeof( Gaplen * ) );
3110 			for( k=0; k<ll1+1; k++ ) gaplen1jcurr[i][k] = NULL;
3111 			gaplen1jcurr[i][k] = (Gaplen *)1;
3112 
3113 			gaplen2jcurr[i] = (Gaplen **)calloc( ll2+2-i, sizeof( Gaplen * ) );
3114 			for( k=0; k<ll2+1-i; k++ ) gaplen2jcurr[i][k] = NULL;
3115 			gaplen2jcurr[i][k] = (Gaplen *)1;
3116 
3117 			gaplen1jprev[i] = (Gaplen **)calloc( ll1+2, sizeof( Gaplen * ) );
3118 			for( k=0; k<ll1+1; k++ ) gaplen1jprev[i][k] = NULL;
3119 			gaplen1jprev[i][k] = (Gaplen *)1;
3120 
3121 			gaplen2jprev[i] = (Gaplen **)calloc( ll2+2-i, sizeof( Gaplen * ) );
3122 			for( k=0; k<ll2+1-i; k++ ) gaplen2jprev[i][k] = NULL;
3123 			gaplen2jprev[i][k] = (Gaplen *)1;
3124 
3125 		}
3126 		gaplen1jbestkamo[ll2+1] = NULL;
3127 		gaplen2jbestkamo[ll2+1] = NULL;
3128 		gaplen1jbest[ll2+1] = NULL;
3129 		gaplen2jbest[ll2+1] = NULL;
3130 		gaplen1jcurr[ll2+1] = NULL;
3131 		gaplen2jcurr[ll2+1] = NULL;
3132 		gaplen1jprev[ll2+1] = NULL;
3133 		gaplen2jprev[ll2+1] = NULL;
3134 	}
3135 
3136 
3137 #if USEGAPLENMTX
3138 /* maikai allocate */
3139 
3140 	reporterr( "Allocating gaplenmtx1\n" );
3141 	gaplen1mtx = (Gaplen ****)calloc( (lgth1+2), sizeof( Gaplen ***) );
3142 	for(i=0; i<lgth1+1; i++ ) gaplen1mtx[i] = (Gaplen ***)calloc( lgth2+2, sizeof( Gaplen ** ) );
3143 	for(i=0; i<lgth1+1; i++ )
3144 	{
3145 		for(j=0; j<lgth2+1; j++ )
3146 		{
3147 			gaplen1mtx[i][j] = (Gaplen **)calloc( lgth1+2, sizeof( Gaplen * ) );
3148 			for( k=0; k<lgth1+1; k++ ) gaplen1mtx[i][j][k] = NULL;
3149 			gaplen1mtx[i][j][k] = (Gaplen *)1;
3150 		}
3151 		gaplen1mtx[i][j] = NULL;
3152 	}
3153 	gaplen1mtx[i] = NULL;
3154 
3155 	reporterr( "Allocating gaplenmtx2\n" );
3156 	gaplen2mtx = (Gaplen ****)calloc( (lgth1+2), sizeof( Gaplen ***) );
3157 	for(i=0; i<lgth1+1; i++ ) gaplen2mtx[i] = (Gaplen ***)calloc( lgth2+2, sizeof( Gaplen ** ) );
3158 	for(i=0; i<lgth1+1; i++ )
3159 	{
3160 		for(j=0; j<lgth2+1; j++ )
3161 		{
3162 			gaplen2mtx[i][j] = (Gaplen **)calloc( lgth2+2, sizeof( Gaplen * ) );
3163 			for( k=0; k<lgth2+1; k++ ) gaplen2mtx[i][j][k] = NULL;
3164 			gaplen2mtx[i][j][k] = (Gaplen *)1;
3165 		}
3166 		gaplen2mtx[i][j] = NULL;
3167 	}
3168 	gaplen2mtx[i] = NULL;
3169 
3170 #endif
3171 
3172 #if USEGAPLENHALF
3173 	reporterr( "Allocating gaplenhalf1\n" );
3174 	gaplen1half = (Gaplen ****)calloc( (lgth1+2), sizeof( Gaplen ***) );
3175 	for(i=0; i<lgth1+1; i++ ) gaplen1half[i] = (Gaplen ***)calloc( lgth2+2, sizeof( Gaplen ** ) );
3176 	for(i=0; i<lgth1+1; i++ )
3177 	{
3178 		for(j=0; j<lgth2+1; j++ )
3179 		{
3180 			gaplen1half[i][j] = (Gaplen **)calloc( lgth1+2 - i, sizeof( Gaplen * ) );
3181 			for( k=0; k<lgth1+1-i; k++ ) gaplen1half[i][j][k] = NULL;
3182 			gaplen1half[i][j][k] = (Gaplen *)1;
3183 		}
3184 		gaplen1half[i][j] = NULL;
3185 	}
3186 	gaplen1half[i] = NULL;
3187 
3188 	reporterr( "Allocating gaplenhalf2\n" );
3189 	gaplen2half = (Gaplen ****)calloc( (lgth1+2), sizeof( Gaplen ***) );
3190 	for(i=0; i<lgth1+1; i++ ) gaplen2half[i] = (Gaplen ***)calloc( lgth2+2, sizeof( Gaplen ** ) );
3191 	for(i=0; i<lgth1+1; i++ )
3192 	{
3193 		for(j=0; j<lgth2+1; j++ )
3194 		{
3195 			gaplen2half[i][j] = (Gaplen **)calloc( lgth2+2 - j, sizeof( Gaplen * ) );
3196 			for( k=0; k<lgth2+1-j; k++ ) gaplen2half[i][j][k] = NULL;
3197 			gaplen2half[i][j][k] = (Gaplen *)1;
3198 		}
3199 		gaplen2half[i][j] = NULL;
3200 	}
3201 	gaplen2half[i] = NULL;
3202 #endif
3203 
3204 
3205 /* maikai allocate */
3206 
3207 
3208 	for( i=0; i<icyc; i++ )
3209 	{
3210 		mseq1[i] = mseq[i];
3211 		seq1[i][lgth1] = 0;
3212 	}
3213 	for( j=0; j<jcyc; j++ )
3214 	{
3215 		mseq2[j] = mseq[icyc+j];
3216 		seq2[j][lgth2] = 0;
3217 	}
3218 
3219 
3220 	if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
3221 	{
3222 		int ll1, ll2;
3223 
3224 		if( commonAlloc1 && commonAlloc2 )
3225 		{
3226 			FreeIntMtx( commonIP );
3227 		}
3228 
3229 		ll1 = MAX( orlgth1, commonAlloc1 );
3230 		ll2 = MAX( orlgth2, commonAlloc2 );
3231 
3232 #if DEBUG
3233 		fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
3234 #endif
3235 
3236 		commonIP = AllocateIntMtx( ll1+10, ll2+10 );
3237 
3238 #if DEBUG
3239 		fprintf( stderr, "succeeded\n\n" );
3240 #endif
3241 
3242 		commonAlloc1 = ll1;
3243 		commonAlloc2 = ll2;
3244 	}
3245 	ijp = commonIP;
3246 
3247 #if 0
3248 	{
3249 		double t = 0.0;
3250 		for( i=0; i<icyc; i++ )
3251 			t += eff1[i];
3252 	fprintf( stderr, "## totaleff = %f\n", t );
3253 	}
3254 #endif
3255 
3256 	cpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
3257 	cpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
3258 
3259 
3260 //	reporterr( "Counting gaplen\n" );
3261 	gaplencount( icyc, lgth1, gaplen1, seq1, eff1 );
3262 	gaplencount( jcyc, lgth2, gaplen2, seq2, eff2 );
3263 #if DEBUG
3264 	reporterr( "group1 = \n" );
3265 	showgaplen( gaplen1, lgth1 );
3266 	reporterr( "group2 = \n" );
3267 	showgaplen( gaplen2, lgth2 );
3268 #endif
3269 //	reporterr( "done.\n" );
3270 
3271 
3272 	for( i=0; i<lgth1+1; i++ ) for( j=0; j<lgth2+1; j++ )
3273 	{
3274 #if USEGAPLENMTX
3275 //		duplicategaplen( gaplen1mtx[i][j], gaplen1, lgth1 );
3276 //		duplicategaplen( gaplen2mtx[i][j], gaplen2, lgth2 );
3277 
3278 //		duplicategaplenpartly( gaplen2mtx[i][j], gaplen2, j-0, lgth2 ); // anzen
3279 //		duplicategaplenpartly( gaplen1mtx[i][j], gaplen1, i-0, lgth1 ); // anzen
3280 		duplicategaplenpartly( gaplen1mtx[i][j], gaplen1, i-0, i ); // iranaikamo
3281 		duplicategaplenpartly( gaplen2mtx[i][j], gaplen2, j-0, j ); // iranaikamo
3282 #endif
3283 #if USEGAPLENHALF
3284 
3285 //		duplicategaplenpartly( gaplen1half[i][j], gaplen1+i, 0, lgth1-i ); // KOKO de setsuyaku dekiru to omou.
3286 //		duplicategaplenpartly( gaplen2half[i][j], gaplen2+j, 0, lgth2-j ); //  originally, j-1, lgth2
3287 		duplicategaplenpartly( gaplen1half[i][j], gaplen1+i, 0, 0 ); // test
3288 		duplicategaplenpartly( gaplen2half[i][j], gaplen2+j, 0, 0 ); // test
3289 #endif
3290 	}
3291 
3292 
3293 #if USEGAPLENMTX
3294 	reporterr( "Duplicating gaplen*mtx\n" );
3295 	for( i=0; i<lgth1+1; i++ )
3296 	{
3297 //		addnewgaplen( gaplen1mtx[i][0], gaplen1, gaplen1, lgth1, -1, 0 );
3298 //		addnewgaplen( gaplen2mtx[i][0], gaplen2, gaplen2, lgth2, 0, i );
3299 //		duplicategaplenpartly( gaplen1mtx[i][0], gaplen1, 0, lgth1 );
3300 //		duplicategaplenpartly( gaplen2mtx[i][0], gaplen2, 0, lgth2 );
3301 		copygaplenrestricted( gaplen2mtx[i][0], gaplen2, lgth2, 0, i, 0, 0 );
3302 	}
3303 #endif
3304 #if USEGAPLENHALF
3305 	reporterr( "Duplicating gaplen*mtx\n" );
3306 	for( i=0; i<lgth1+1; i++ )
3307 	{
3308 		copygaplenrestricted( gaplen2half[i][0], gaplen2, lgth2, 0, i, 0, 0 );
3309 	}
3310 #endif
3311 
3312 
3313 
3314 	for( i=0; i<1; i++ )
3315 	{
3316 //		duplicategaplencompactx( gaplen1icurr[i], gaplen1, lgth1, i-0, lgth1 ); //originally, 0, lgth1
3317 //
3318 //		duplicategaplencompactx( gaplen1icurr[i], gaplen1+i, lgth1-i, 0, lgth1-i ); // half
3319 		duplicategaplencompactx( gaplen1icurr[i], gaplen1+i, lgth1-i, 0, 1 ); //  0, 1  hitsuyou
3320 
3321 
3322 //		duplicategaplencompactx( gaplen2icurr[i], gaplen2, lgth2, 0, lgth2 ); // ichiou zenbu
3323 		duplicategaplencompactx( gaplen2icurr[i], gaplen2, lgth2, 0, 0 );
3324 
3325 
3326 		copygaplencompactx( gaplen2icurr[i], gaplen2, lgth2, 0, i, 0, 0 ); // -> zurasu -> error?
3327 
3328 
3329 //		duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1, lgth1, 0, 1 );
3330 //		duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1+i, lgth1-i, 0, 1 ); // half
3331 		duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1+i, lgth1-i, 0, 0 ); // half
3332 //		duplicategaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, lgth2 );
3333 		duplicategaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, 0 );
3334 //		copygaplenrestricted( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, 0 ); // -> zurasu -> error?
3335 //		copygaplenrestricted_zurasu( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, lgth2, 0, lgth2 ); // -> zurasu -> error?
3336 		copygaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, 0 ); // -> zurasu -> error?
3337 	}
3338 
3339 //	reporterr( "Duplicating gaplen*j*curr \n" );
3340 //	int nduplicated = 0;
3341 	for( j=0; j<lgth2+1; j++ )
3342 //	for( j=0; j<1; j++ )
3343 	{
3344 #if USEGAPLENMTX
3345 //		addnewgaplen( gaplen1mtx[0][j], gaplen1, gaplen1, lgth1, 0, j );
3346 //		addnewgaplen( gaplen2mtx[0][j], gaplen2, gaplen2, lgth2, -1, 0 );
3347 //		duplicategaplenpartly( gaplen1mtx[0][j], gaplen1, 0, lgth1 );
3348 //		duplicategaplenpartly( gaplen2mtx[0][j], gaplen2, 0, lgth2 );
3349 		copygaplenrestricted( gaplen1mtx[0][j], gaplen1, lgth1, 0, j, 0, 0 );
3350 #endif
3351 
3352 #if USEGAPLENHALF
3353 		copygaplenrestricted( gaplen1half[0][j], gaplen1, lgth1, 0, j, 0, 0 );
3354 #endif
3355 //		reporterr( "1jcurr?\n" );
3356 //		duplicategaplencompactx( gaplen1jcurr[j], gaplen1, lgth1, 0, lgth1 ); // test
3357 		duplicategaplencompactx( gaplen1jcurr[j], gaplen1, lgth1, 0, 0 ); // dame?
3358 //		reporterr( "done\n" );
3359 //		duplicategaplencompactx( gaplen1jcurr[j], gaplen1, lgth1, 0, 0 ); //test
3360 
3361 //		duplicategaplencompactx( gaplen2jcurr[j], gaplen2, lgth2, j-0, lgth2 ); // full
3362 //		duplicategaplencompactx( gaplen2jcurr[j], gaplen2+j, lgth2-j, 0, lgth2-j ); //half! KOKO????
3363 //reporterr( "starting suspicious duplication\n" );
3364 		duplicategaplencompactx( gaplen2jcurr[j], gaplen2+j, lgth2-j, 0, 0 ); //half!
3365 //reporterr( "starting suspicious copy\n" );
3366 		copygaplencompactx( gaplen1jcurr[j], gaplen1, lgth1, 0, j, 0, 0 ); // TEST
3367 //reporterr( "finished\n" );
3368 
3369 //		reporterr( "Checking gaplen1jcurr[%d]\n", j );
3370 //		checkgaplen( gaplen1jcurr[j], 100 );
3371 //		reporterr( "Checking gaplen2jcurr[%d]\n", j );
3372 //		checkgaplen( gaplen2jcurr[j], 100 );
3373 	}
3374 
3375 //	reporterr( "nduplicated (corrected) = %d\n", nduplicated );
3376 
3377 //	reporterr( "Duplicating gaplen*j*prev \n\n" );
3378 	for( j=0; j<lgth2+1; j++ ) // allocate nominotame, atode uwagaki
3379 	{
3380 //		duplicategaplencompactx( gaplen1jprev[j], gaplen1, lgth1, 0, lgth1 );
3381 		duplicategaplencompactx( gaplen1jprev[j], gaplen1, lgth1, 0, 0 ); // TEST
3382 //		duplicategaplencompactx( gaplen2jprev[j], gaplen2, lgth2, j-0, lgth2 ); // originally, 0,lgth2
3383 //		duplicategaplencompactx( gaplen2jprev[j], gaplen2+j, lgth2-j, 0, lgth2-j ); // half
3384 		duplicategaplencompactx( gaplen2jprev[j], gaplen2+j, lgth2-j, 0, 0 ); // half
3385 
3386 
3387 		copygaplencompactx( gaplen1jprev[j], gaplen1, lgth1, 0, j, 0, 0 ); // wasuretetakamo
3388 
3389 
3390 	}
3391 
3392 
3393 //	reporterr( "Duplicating gaplen*j*best \n\n" );
3394 
3395 	for( j=0; j<lgth2+1; j++ )
3396 //	for( j=0; j<1; j++ )
3397 	{
3398 //		duplicategaplencompactx( gaplen1jbestkamo[j], gaplen1, lgth1, 0, lgth1 ); // KOKO
3399 //		duplicategaplencompactx( gaplen1jbestkamo[j], gaplen1, lgth1, 0, 0 ); // test
3400 //		duplicategaplencompactx( gaplen1jbestkamo[j], gaplen1, lgth1, 0, 1 );
3401 		duplicategaplencompactx( gaplen1jbestkamo[j], gaplen1, lgth1, 0, 0 );
3402 
3403 
3404 //		duplicategaplencompactx( gaplen1jbestkamo[j], gaplen1, lgth1, 0, 1 );
3405 //		duplicategaplencompactx( gaplen2jbestkamo[j], gaplen2, lgth2, j-0, j+1 ); // originally, 0, j+1
3406 		duplicategaplencompactx( gaplen2jbestkamo[j], gaplen2+j, lgth2-j, 0, 1 ); // half!
3407 		copygaplencompactx( gaplen1jbestkamo[j], gaplen1, lgth1, 0, j, 0, 0 ); // TEST
3408 
3409 //		duplicategaplencompactx( gaplen1jbest[j], gaplen1, lgth1, 0, lgth1 ); // KOKO
3410 		duplicategaplencompactx( gaplen1jbest[j], gaplen1, lgth1, 0, 0 ); // test
3411 //		duplicategaplencompactx( gaplen2jbest[j], gaplen2, lgth2,j-0, j+1 ); // originally, 0,j+1
3412 		duplicategaplencompactx( gaplen2jbest[j], gaplen2+j, lgth2-j, 0, 1 ); // half!
3413 		copygaplencompactx( gaplen1jbest[j], gaplen1, lgth1, 0, j, 0, 0 ); // TEST
3414 
3415 
3416 	}
3417 
3418 //	reporterr( "Duplication end\n" );
3419 
3420 
3421 #if 0
3422 	reporterr( "Checking gaplen1icurr\n" );
3423 	checkgaplen( gaplen1icurr[0], 100 );
3424 	reporterr( "Checking gaplen2icurr\n" );
3425 	checkgaplen( gaplen2icurr[0], 100 );
3426 #endif
3427 
3428 
3429 
3430 
3431 //	showgaplen( gaplen1jcurr[50], lgth2 );
3432 
3433 	currentw = w1;
3434 	previousw = w2;
3435 
3436 	match_calc( n_dynamicmtx, initverticalw, cpmx2, cpmx1, 0, lgth1, doublework, intwork, 1 );
3437 	if( localhom )
3438 		imp_match_out_vead_tate( initverticalw, 0, lgth1 ); // 060306
3439 
3440 	match_calc( n_dynamicmtx, currentw, cpmx1, cpmx2, 0, lgth2, doublework, intwork, 1 );
3441 	if( localhom )
3442 		imp_match_out_vead( currentw, 0, lgth2 ); // 060306
3443 #if 0 // -> tbfast.c
3444 	if( localhom )
3445 		imp_match_calc( n_dynamicmtx, currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
3446 
3447 #endif
3448 
3449 	for( j=1; j<lgth2+1; j++ )
3450 	{
3451 		pfac = calcpfac_gap_noidatend( gaplen1, gaplen2, j, 0, j, seq1[0], seq2[0], 0 );
3452 //		reporterr( "computing initial end gap penalty for %c-%c, i=0, j=%d, pfac=%f\n", seq1[0][0], seq2[0][j], j, pfac );
3453 //		reporterr( "%c-%c, i=0, j=%d, currentw[j]=%f -> ", seq1[0][0], seq2[0][j], j, currentw[j] );
3454 		currentw[j] += fpenalty * pfac; // tekitou
3455 //		reporterr( " %f\n", currentw[j] );
3456 	}
3457 	for( i=1; i<lgth1+1; i++ )
3458 	{
3459 		pfac = calcpfac_gap_noidatend( gaplen2, gaplen1, i, 0, i, seq2[0], seq1[0], 0 );
3460 //		reporterr( "computing initial end gap penalty for %c-%c, i=%d, j=0, pfac=%f\n", seq1[0][i], seq2[0][0], i, pfac );
3461 		initverticalw[i] += fpenalty * pfac; // tekitou
3462 	}
3463 
3464 
3465 
3466 	for( j=1; j<lgth2+1; ++j )
3467 	{
3468 
3469 
3470 #if ALGZGAP
3471 		m[j] = currentw[j-1] + ogcp1[1] * gapfreq2[j-1]; mp[j] = 0;;
3472 #else
3473 		pfac = calcpfac_gapex_noidatend( gaplen2, gaplen1, j, 1, j, seq2[0], seq1[0], 1 );
3474 #if DEBUG
3475 		reporterr( "%c-%c, INITIAL jgap extension check, pfac = %f\n\n", seq1[0][j], '=', pfac );
3476 #endif
3477 		m[j] = currentw[j-1] + fpenalty * pfac;
3478 		mp[j] = 0;
3479 #endif
3480 	}
3481 	if( lgth2 == 0 )
3482 		lastverticalw[0] = 0.0; // Falign kara yobaretatoki kounarukanousei ari
3483 	else
3484 		lastverticalw[0] = currentw[lgth2-1];
3485 
3486 	if( tailgp ) lasti = lgth1+1; else lasti = lgth1;
3487 	lastj = lgth2+1;
3488 
3489 
3490 	for( i=1; i<lasti; i++ )
3491 	{
3492 //		reporterr( "i = %d\n", i );
3493 
3494 //		reporterr( "err1? i=%d/%d\n", i, lgth1 );
3495 #ifdef enablemultithread
3496 //		fprintf( stderr, "chudan = %d, %d\n", *chudanpt, chudanref );
3497 		if( chudanpt && *chudanpt != chudanref )
3498 		{
3499 			cleargaplens( gaplens );
3500 //			fprintf( stderr, "\n\n## CHUUDAN!!! S\n" );
3501 			*chudanres = 1;
3502 			return( -1.0 );
3503 		}
3504 #endif
3505 
3506 
3507 		wtmp = previousw;
3508 		previousw = currentw;
3509 		currentw = wtmp;
3510 
3511 		previousw[0] = initverticalw[i-1];
3512 
3513 #if 1
3514 		gaplentmp = gaplen1jprev;
3515 		gaplen1jprev = gaplen1jcurr;
3516 		gaplen1jcurr = gaplentmp;
3517 
3518 		gaplentmp = gaplen2jprev;
3519 		gaplen2jprev = gaplen2jcurr;
3520 		gaplen2jcurr = gaplentmp;
3521 
3522 #if DEBUG
3523 		reporterr( "Entering a small j loop, i=%d\n", i );
3524 		for( j=1; j<lgth2+1; j++ )
3525 		{
3526 			reporterr( "before j loop, i=%d, gaplen2jcurr[%d] = \n", i, j );
3527 			showgaplen( gaplen2jcurr[j], 100 );
3528 			reporterr( "\n" );
3529 			reporterr( "before j loop, i=%d, gaplen2prev[%d] = \n", i, j );
3530 			showgaplen( gaplen2jprev[j], 100 );
3531 			reporterr( "\n" );
3532 		}
3533 #endif
3534 #else
3535 
3536 		reporterr( "Entering a small j loop, i=%\n", i );
3537 		for( j=1; j<lgth2+1; j++ )
3538 		{
3539 //			addnewgaplen( gaplen1jprev[j], gaplen1jcurr[j], gaplen1, lgth1, -1, 0 );
3540 //			addnewgaplen( gaplen2jprev[j], gaplen2jcurr[j], gaplen2, lgth2, -1, 0 );
3541 			reporterr( "err1? j=%d/%d\n", j, lgth2 );
3542 			copygaplencompactx( gaplen1jprev[j-1], gaplen1jcurr[j-1], lgth1, -1, 0, i-1, i-1 ); // TEST
3543 			reporterr( "err1? j=%d/%d\n", j, lgth2 );
3544 			copygaplencompactx( gaplen2jprev[j-1], gaplen2jcurr[j-1], lgth2, -1, 0, j-1, j-1 ); // TETS
3545 #if DEBUG
3546 			reporterr( "before j loop, i=%d, gaplen2jcurr[%d] = \n", i, j );
3547 			showgaplen( gaplen2jcurr[j], 100 );
3548 			reporterr( "\n" );
3549 			reporterr( "before j loop, i=%d, gaplen2prev[%d] = \n", i, j );
3550 			showgaplen( gaplen2jprev[j], 100 );
3551 			reporterr( "\n" );
3552 #endif
3553 		}
3554 #endif
3555 
3556 //		reporterr( "err2? i=%d/%d\n", i, lgth1 );
3557 
3558 //		duplicategaplencompactx( gaplen1icurr[i], gaplen1, lgth1, i, i+1 ); //originally 0, i+1
3559 //		reporterr( "gaplen+0=\n");
3560 //		showgaplen( gaplen1, 10 );
3561 //		reporterr( "i=%d, lgth1=%d, lgth1-i=%d, gaplen+i-1=\n", i, lgth1, lgth1-i );
3562 //		showgaplen( gaplen1+i-1, 100 );
3563 		duplicategaplencompactx( gaplen1icurr[i], gaplen1+i, lgth1-i, 0, 1 ); // half!!
3564 //		duplicategaplencompactx( gaplen2icurr[i], gaplen2, lgth2, 0, lgth2 ); // KOKO
3565 		duplicategaplencompactx( gaplen2icurr[i], gaplen2, lgth2, 0, 0 ); // test
3566 		copygaplencompactx( gaplen2icurr[i], gaplen2, lgth2, 0, i, 0, 0 ); // IRU? TEST
3567 
3568 
3569 
3570 //		duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1, lgth1, i, i+1 ); //originally 0, i+1
3571 		duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1+i, lgth1-i, 0, 1 ); //half
3572 //		duplicategaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, lgth2 ); // ORIGINALLY, 0, lgth2
3573 		duplicategaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, 0 ); // ORIGINALLY, 0, lgth2
3574 //		copygaplenrestricted( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, lgth2, 0, 0 ); // IRU? // TEST
3575 		copygaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, 0 ); // IRU? // TEST
3576 
3577 		extendgaplencompactx( gaplen1jprev[0], gaplen1, i ); // ???
3578 
3579 
3580 //		addnewgaplen( gaplen1jprev[0], gaplen1icurr[i-1], gaplen1, lgth1, -1, 0 );
3581 //		addnewgaplen( gaplen2jprev[0], gaplen2icurr[i-1], gaplen2, lgth2, -1, 0 );
3582 //		copygaplenrestricted( gaplen1jprev[0], gaplen1icurr[i-1], lgth1, -1, 0, i, i ); // i-1, i da to omou.
3583 		copygaplencompactx( gaplen1jprev[0], gaplen1icurr[i-1], lgth1-i, -1, 0, i, 1 ); // half? lgth1-i?
3584 //		copygaplenrestricted( gaplen2jprev[0], gaplen2icurr[i-1], lgth2, -1, 0, 0, 0 );
3585 		copygaplencompactx( gaplen2jprev[0], gaplen2icurr[i-1], lgth2-j, -1, 0, 0, 0 ); // half?? lgth2-j?
3586 
3587 
3588 		match_calc( n_dynamicmtx, currentw, cpmx1, cpmx2, i, lgth2, doublework, intwork, 0 );
3589 #if XXXXXXX
3590 fprintf( stderr, "\n" );
3591 fprintf( stderr, "i=%d\n", i );
3592 fprintf( stderr, "currentw = \n" );
3593 for( j=0; j<lgth2; j++ )
3594 {
3595 	fprintf( stderr, "%5.2f ", currentw[j] );
3596 }
3597 fprintf( stderr, "\n" );
3598 #endif
3599 		if( localhom )
3600 		{
3601 //			fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
3602 #if  0
3603 			imp_match_out_vead( currentw, i, lgth2 );
3604 #else
3605 			imp_match_out_vead( currentw, i, lgth2 );
3606 #endif
3607 		}
3608 #if XXXXXXX
3609 fprintf( stderr, "\n" );
3610 fprintf( stderr, "i=%d\n", i );
3611 fprintf( stderr, "currentw = \n" );
3612 for( j=0; j<lgth2; j++ )
3613 {
3614 	fprintf( stderr, "%5.2f ", currentw[j] );
3615 }
3616 fprintf( stderr, "\n" );
3617 #endif
3618 		currentw[0] = initverticalw[i];
3619 
3620 #if 0
3621 		fprintf( stderr, "%c ", seq1[0][i] );
3622 		for( j=0; j<lgth2+1; j++ )
3623 		{
3624 			fprintf( stderr, "%5.0f ", currentw[j] );
3625 		}
3626 		fprintf( stderr, "\n"  );
3627 #endif
3628 
3629 //		mi = previousw[0] + ogcp2[1]; mpi = 0;
3630 
3631 
3632 
3633 
3634 
3635 #if ALGZGAP
3636 		mi = previousw[0] + ogcp2[1] * gapfreq1[i-1]; mpi=0;
3637 #else
3638 		pfac = calcpfac_gapex_noidatend( gaplen1, gaplen2, i, 1, i, seq1[0], seq2[0], 1 );
3639 #if DEBUG
3640 		reporterr( "%c-%c, INITIAL igap extension check, pfac = %f\n\n", '=', seq2[0][j], pfac );
3641 #endif
3642 		mi = previousw[0] + fpenalty * pfac;
3643 		mpi=0;
3644 #endif
3645 		ijppt = ijp[i] + 1;
3646 		mjpt = m + 1;
3647 		prept = previousw;
3648 		curpt = currentw + 1;
3649 		mpjpt = mp + 1;
3650 
3651 
3652 		if( trywarp )
3653 		{
3654 			prevwmrecordspt = prevwmrecords;
3655 			wmrecordspt = wmrecords+1;
3656 			wmrecords1pt = wmrecords;
3657 			warpipt = warpi + 1;
3658 			warpjpt = warpj + 1;
3659 		}
3660 
3661 
3662 
3663 
3664 //		reporterr( "\n\ni=%d, %c\n", i, seq1[0][i] );
3665 		for( j=1; j<lastj; j++ )
3666 		{
3667 
3668 #if DEBUG
3669 			reporterr( "*****   %c%d-%c%d   ******* \n", seq1[0][i], i, seq2[0][j], j );
3670 			reporterr( "mpi=%d (%c), *mpjpt=%d (%c)\n", mpi, seq2[0][mpi], *mpjpt, seq1[0][*mpjpt] );
3671 #endif
3672 
3673 
3674 // Hitsuyou na bun dake tsuika
3675 #if USEGAPLENMTX
3676 			extendgaplenpartly( gaplen1mtx[i-1][mpi], gaplen1, i, i );
3677 			extendgaplenpartly( gaplen2mtx[i-1][mpi], gaplen2, j, j );
3678 			extendgaplenpartly( gaplen1mtx[*mpjpt][j-1], gaplen1, i, i );
3679 			extendgaplenpartly( gaplen2mtx[*mpjpt][j-1], gaplen2, j, j );
3680 			extendgaplenpartly( gaplen1mtx[i-1][j-1], gaplen1, i, i );
3681 			extendgaplenpartly( gaplen2mtx[i-1][j-1], gaplen2, j, j );
3682 #endif
3683 #if USEGAPLENHALF
3684 			extendgaplenpartly( gaplen1half[i-1][mpi], gaplen1+i-1, 1, 1 );
3685 			extendgaplenpartly( gaplen2half[i-1][mpi], gaplen2+mpi, j-mpi, j-mpi );
3686 			extendgaplenpartly( gaplen1half[*mpjpt][j-1], gaplen1+*mpjpt, i-*mpjpt, i-*mpjpt );
3687 			extendgaplenpartly( gaplen2half[*mpjpt][j-1], gaplen2+j-1, 1, 1 );
3688 			extendgaplenpartly( gaplen1half[i-1][j-1], gaplen1+i-1, 1, 1 );
3689 			extendgaplenpartly( gaplen2half[i-1][j-1], gaplen2+j-1, 1, 1 );
3690 
3691 #endif
3692 
3693 //			reporterr( "extending gaplen1icurr\n" );
3694 			extendgaplencompactx( gaplen1icurr[i-1], gaplen1+i-1, 1 ); // iruhazu
3695 //			reporterr( "extending gaplen2icurr\n" );
3696 			extendgaplencompactx( gaplen2icurr[i-1], gaplen2, j ); // iruhazu
3697 //			reporterr( "extending gaplen1jprev[j-1], j-1=%d\n", j-1 );
3698 			extendgaplencompactx( gaplen1jprev[j-1], gaplen1, i );
3699 //			reporterr( "extending gaplen1jcurr, j-1=%d\n", j-1 );
3700 			extendgaplencompactx( gaplen1jcurr[j-1], gaplen1, i );
3701 //			reporterr( "extending gaplen2jprev\n" );
3702 			extendgaplencompactx( gaplen2jprev[j-1], gaplen2+j-1, 1 );
3703 //			reporterr( "extending gaplen2jcurr\n" );
3704 			extendgaplencompactx( gaplen2jcurr[j-1], gaplen2+j-1, 1 );
3705 //			reporterr( "extending gaplen1jbest[j-1]\n" );
3706 			extendgaplencompactx( gaplen1jbest[j-1], gaplen1, i );
3707 //			reporterr( "extending gaplen1jbestkamo[j-1]\n" );
3708 			extendgaplencompactx( gaplen1jbestkamo[j-1], gaplen1, i );
3709 //			reporterr( "extending gaplen1jprev[mpi], j-1=%d\n", j-1 );
3710 			extendgaplencompactx( gaplen1jprev[mpi], gaplen1, i ); // full
3711 //			reporterr( "extending gaplen2jprev[mpi]\n" );
3712 			extendgaplencompactx( gaplen2jprev[mpi], gaplen2+mpi, j-mpi ); // half
3713 //			reporterr( "extending gaplen1ibestkamo[i-1]\n" );
3714 			extendgaplencompactx( gaplen1ibestkamo[i-1], gaplen1+i-1, 1 );
3715 //			reporterr( "extending gaplen2ibestkamo[i-1]\n" );
3716 			extendgaplencompactx( gaplen2ibestkamo[i-1], gaplen2, j );
3717 
3718 
3719 #if DEBUG
3720 			reporterr( "at the head of j loop, i,j=%d,%d, gaplen2jcurr[j]=\n", i, j );
3721 			showgaplen( gaplen2jcurr[j], 100 );
3722 
3723 			reporterr( "at the head of j loop, i,j=%d,%d, gaplen2jcurr[j-1]=\n", i, j );
3724 			showgaplen( gaplen2jcurr[j-1], 100 );
3725 
3726 
3727 			reporterr( "at the head of j loop, i,j=%d,%d, gaplen2jprev[j]=\n", i, j );
3728 			showgaplen( gaplen2jprev[j], 100 );
3729 
3730 			reporterr( "at the head of j loop, i,j=%d,%d, gaplen2jprev[j-1]=\n", i, j );
3731 			showgaplen( gaplen2jprev[j-1], 100 );
3732 #endif
3733 
3734 
3735 #ifdef xxxenablemultithread
3736 //			fprintf( stderr, "chudan = %d, %d\n", *chudanpt, chudanref );
3737 			if( chudanpt && *chudanpt != chudanref )
3738 			{
3739 				cleargaplens( gaplens );
3740 //				fprintf( stderr, "\n\n## CHUUDAN!!! S\n" );
3741 				*chudanres = 1;
3742 				return( -1.0 );
3743 			}
3744 #endif
3745 #if USEGAPLENHALF
3746 // i==248, j==80 wo check
3747 #if DEBUG80
3748 			if( j==80 )
3749 			{
3750 				reporterr( "When i==%d, j==%d,\n", i, j );
3751 				reporterr( "gaplen2jprev[j-1=%d]=\n", j-1 );
3752 				showgaplen( gaplen2jprev[j-1], lgth1 );
3753 				reporterr( "gaplen2half[i-1=%d][j-1=%d]=\n", i-1, j-1 );
3754 				showgaplen( gaplen2half[i-1][j-1], lgth1 );
3755 			}
3756 			if( j==79 )
3757 			{
3758 				reporterr( "When i==%d, j==%d,\n", i, j );
3759 				reporterr( "gaplen2jprev[j-1=%d]=\n", j-1 );
3760 				showgaplen( gaplen2jprev[j-1], lgth1 );
3761 				reporterr( "gaplen2half[i-1=%d][j-1=%d]=\n", i-1, j-1 );
3762 				showgaplen( gaplen2half[i-1][j-1], lgth1 );
3763 			}
3764 #endif
3765 #endif
3766 
3767 
3768 //			pfac = calcpfac( gaplen1jprev[j-1], gaplen2jprev[j-1], i, j, seq1[0], seq2[0] );
3769 //reporterr( "#### COMPACT, i,j=%d,%d\n", i, j );
3770 			pfac = calcpfacnoidatend( gaplen1jprev[j-1], gaplen2jprev[j-1], i, 1, seq1[0], seq2[0]+j, one ); // 1j->full, 2j->half
3771 #if USEGAPLENMTX
3772 //reporterr( "#### FULL, i,j=%d,%d\n", i, j );
3773 			pfactmp = calcpfac( gaplen1mtx[i-1][j-1], gaplen2mtx[i-1][j-1], i, j, seq1[0], seq2[0], one );
3774 #endif
3775 #if USEGAPLENHALF
3776 //reporterr( "#### HALF, i,j=%d/%d,%d/%d\n", i, lgth1, j, lgth2 );
3777 //			showgaplen( gaplen2half[i-1][j-1], lgth2-j );
3778 			pfactmp = calcpfac( gaplen1half[i-1][j-1], gaplen2half[i-1][j-1], 1, 1, seq1[0]+i, seq2[0]+j, zero );
3779 #endif
3780 #if USEGAPLENMTX + USEGAPLENHALF
3781 			if( pfac != pfactmp )
3782 			{
3783 				reporterr( "(straight) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j );
3784 				PFACERROR = 1;
3785 				exit( 1 );
3786 			}
3787 #endif
3788 //if( i==50 && j==135 ) exit( 1 );
3789 
3790 
3791 //			reporterr( "i,j=%d,%d, *prept = %f\n", i, j, *prept );
3792 
3793 #if ALGZSTRAIGHT
3794 			wm = *prept;  // Machigai!!
3795 #else
3796 			wm = *prept + fpenalty * pfac;
3797 #endif
3798 			*ijppt = 0;
3799 
3800 
3801 #if DEBUG
3802 			if( i == j )
3803 			{
3804 				fprintf( stderr, "\n i=%d, j=%d %c, %c ", i, j, seq1[0][i], seq2[0][j] );
3805 				fprintf( stderr, "%5.0f, pfac for straight =%f\n", wm, pfac );
3806 			}
3807 #endif
3808 			newgaplen = j-mpi-1;
3809 
3810 
3811 //if( i == 53 && j == 93 ) exit( 1 );
3812 
3813 
3814 
3815 
3816 //			pfac = calcpfac_gap_incomplete( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], newgaplen, i, j, seq1[0], seq2[0], 0 ); // i-1
3817 			pfac = calcpfac_gap_noidatend( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], newgaplen, 1, j, seq1[0]+i-1, seq2[0], 0 ); // i-1
3818 #if USEGAPLENMTX
3819 			pfactmp = calcpfac_gap_incomplete( gaplen1mtx[i-1][mpi], gaplen2mtx[i-1][mpi], newgaplen, i, j, seq1[0], seq2[0], 1 );
3820 #endif
3821 #if USEGAPLENHALF
3822 			pfactmp = calcpfac_gap_incomplete( gaplen1half[i-1][mpi], gaplen2half[i-1][mpi], newgaplen, 1, j-mpi, seq1[0]+i-1, seq2[0]+mpi, 1 );
3823 #endif
3824 #if USEGAPLENMTX || USEGAPLENHALF
3825 			if( pfac != pfactmp )
3826 			{
3827 				reporterr( "(igap) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j );
3828 				PFACERROR = 1;
3829 			}
3830 #endif
3831 
3832 
3833 #if DEBUG
3834 			reporterr( "%c-%c pfac for igap end incomplete = %f\n", seq1[0][i], seq2[0][j], pfac );
3835 			reporterr( "mi when igap end checking = %f\n", mi );
3836 			reporterr( "wm = %f, mi+fpenalty*pfac=%f\n", wm, mi+fpenalty*pfac );
3837 #endif
3838 
3839 
3840 #if ALGZGAP
3841 			if( (g=mi+*fgcp2pt*gf1va) > wm )
3842 #else
3843 			if( (g=mi+fpenalty*pfac) > wm )
3844 #endif
3845 			{
3846 				wm = g;
3847 				*ijppt = -( j - mpi );
3848 #if DEBUG80
3849 				reporterr( "Selected as igap end! wm = %f, mi = %f\n", wm, mi );
3850 				fprintf( stderr, "Jump from %d-%d (%c-%c) to %d (%c-%c)!\n", i, j, seq1[0][i], seq2[0][j], mpi, seq1[0][i-1], seq2[0][mpi] );
3851 #endif
3852 			}
3853 
3854 
3855 #if 0
3856 			fprintf( stderr, "%5.0f->", wm );
3857 #endif
3858 //			if( (g=*mjpt+ fgcp1va* *gf2pt) > wm )
3859 
3860 #if 0
3861 //			reporterr( "Checking %c, (current pos = %c)\n", seq2[0][j+1], seq2[0][j] );
3862 			sfac = 1.0;
3863 			for( k=0; gaplen2[j+1]&&(gl=gaplen2[j+1][k].len)!=-1; k++ ) // ososugi!  hash ni atode henkou
3864 			{
3865 //				reporterr( ".len = %d, .relend = %d\n", gaplen2[j+1][k].len, gaplen2[j+1][k].relend );
3866 				if( gl - 1 == gaplen2[j+1][k].relend )
3867 				{
3868 					sfac -= gaplen2[j+1][k].freq;
3869 //					reporterr( "Hit! sfac = %f\n", sfac );
3870 				}
3871 			}
3872 			sfac2 = 1.0;
3873 			for( k=0; gaplen1[i+1]&&(gl=gaplen1[i+1][k].len)!=-1; k++ ) // ososugi!  hash ni atode henkou
3874 				if( gaplen1[i+1][k].relend != -1 ) sfac2 -= gaplen1[i+1][k].freq;
3875 			sfac *= sfac2;
3876 #else
3877 //			sfac = 0.0;
3878 #endif
3879 
3880 
3881 
3882 #if ALGZGAP
3883 			if( (g=*prept+*ogcp2pt*gf1vapre) >= mi )
3884 #else
3885 //			if( (g=*prept + fpenalty * sfac ) >= mi )
3886 			if( (g=*prept ) >= mi )
3887 #endif
3888 			{
3889 //				mpibk = mpi;
3890 //				mi = g - fpenalty * sfac;
3891 				mi = g;
3892 				mpi = j-1;
3893 #if DEBUG80
3894 				reporterr( "Selected as igap start! %c%d-%c%d, mi=%f, g=%f\n", seq1[0][i-1], i-1, seq2[0][mpi], mpi, mi, g );
3895 #endif
3896 
3897 #if FREEFREQUENTLY
3898 //				freegaplenpartly( gaplen1ibestkamo[i-1], 0, i-1 );
3899 				freegaplenpartly( gaplen2ibestkamo[i-1], j-3, j-2 );
3900 #endif
3901 //				freegaplenpartly( gaplen1jprev[mpibk], 0, lgth2 ); // full
3902 //				freegaplenpartly( gaplen2jprev[mpibk], 0, lgth2-mpibk ); // half
3903 //				if( gaplen1jprev[mpibk] ) FreeGaplenMtx( gaplen1jprev[mpibk], 0 );
3904 //				gaplen1jprev[mpibk] = NULL;
3905 //				if( gaplen2jprev[mpibk] ) FreeGaplenMtx( gaplen2jprev[mpibk], 0 );
3906 //				gaplen2jprev[mpibk] = NULL;
3907 
3908 
3909 //				addnewgaplen( gaplen1ibestkamo[i-1], gaplen1jprev[j-1], gaplen1, lgth1, -1, 0 );
3910 //				addnewgaplen( gaplen2ibestkamo[i-1], gaplen2jprev[j-1], gaplen2, lgth2, -1, 0 );
3911 //				copygaplenrestricted( gaplen1ibestkamo[i-1], gaplen1jprev[j-1], lgth1, -1, 0, i, i ); // i-1, i
3912 				copygaplencompactx( gaplen1ibestkamo[i-1], gaplen1jprev[j-1], lgth1, -1, 0, 1, i ); // half
3913 //				copygaplenrestricted( gaplen2ibestkamo[i-1], gaplen2jprev[j-1], lgth2, -1, 0, j, j ); // mpi, j
3914 				copygaplencompactx( gaplen2ibestkamo[i-1], gaplen2jprev[j-1], lgth2, -1, 0, j, 1 ); //half
3915 
3916 
3917 			}
3918 
3919 
3920 
3921 
3922 
3923 
3924 //			reporterr( "g=%f, *prept=%f, mi=%f\n", g, *prept, mi );
3925 
3926 
3927 #if USE_PENALTY_EX
3928 			mi += fpenalty_ex;
3929 #endif
3930 
3931 #if ALGZGAP
3932 			pfac = 0.0; // CHUUI!
3933 #else
3934 
3935 //			pfac = calcpfac_gapex( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], i, j, j-mpi, seq1[0], seq2[0], 1 ); // i-1
3936 			pfac = calcpfac_gapex_noidatend( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], 1, j, j-mpi, seq1[0]+i, seq2[0], 1 ); // 1ibest->half, 2ibest->full
3937 #if USEGAPLENMTX
3938 			pfactmp = calcpfac_gapex( gaplen1mtx[i-1][mpi], gaplen2mtx[i-1][mpi], i, j, j-mpi, seq1[0], seq2[0], 1 );
3939 #endif
3940 #if USEGAPLENHALF
3941 			pfactmp = calcpfac_gapex( gaplen1half[i-1][mpi], gaplen2half[i-1][mpi], 1, j-mpi, j-mpi, seq1[0]+i, seq2[0]+mpi, 1 );
3942 #endif
3943 #if USEGAPLENMTX || USEGAPLENHALF
3944 			if( pfac != pfactmp )
3945 			{
3946 				reporterr( "(igapex) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j );
3947 				PFACERROR = 1;
3948 			}
3949 #endif
3950 
3951 
3952 
3953 
3954 
3955 
3956 
3957 #if DEBUG
3958 			reporterr( "%c-%c, igap extension check, pfac = %f\n\n", '=', seq2[0][j], pfac );
3959 #endif
3960 #endif
3961 //			reporterr( "mi = %f -> ", mi );
3962 			mi += fpenalty * pfac;
3963 //			reporterr( "mi = %f\n", mi );
3964 
3965 
3966 //			reporterr( "using %d-%d, %d, %d\n", *mpjpt, j-1, i, j );
3967 			newgaplen = i-*mpjpt-1;
3968 //			pfac = calcpfac_gap_incomplete( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], newgaplen, j, i, seq2[0], seq1[0], 0 ); // j-1 deha???
3969 
3970 
3971 			pfac = calcpfac_gap_noidatend( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], newgaplen, 1, i, seq2[0]+j-1, seq1[0], 1 ); // 2jbestkamo->half, 1jbestkamo->full
3972 #if USEGAPLENMTX
3973 			pfactmp = calcpfac_gap_incomplete( gaplen2mtx[*mpjpt][j-1], gaplen1mtx[*mpjpt][j-1], newgaplen, j, i, seq2[0], seq1[0], 1 );
3974 #endif
3975 #if USEGAPLENHALF
3976 			pfactmp = calcpfac_gap_incomplete( gaplen2half[*mpjpt][j-1], gaplen1half[*mpjpt][j-1], newgaplen, 1, i-*mpjpt, seq2[0]+j-1, seq1[0]+*mpjpt, 1 );
3977 #endif
3978 #if USEGAPLENMTX || USEGAPLENHALF
3979 			if( pfac != pfactmp )
3980 			{
3981 				reporterr( "(jgap) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j );
3982 //				exit( 1 );
3983 				PFACERROR = 1;
3984 			}
3985 #endif
3986 
3987 #if ALGZGAP
3988 			if( (g=*mjpt+ fgcp1va* *gf2pt) > wm )
3989 #else
3990 			if( (g=*mjpt + fpenalty*pfac) > wm )
3991 #endif
3992 			{
3993 				wm = g;
3994 				*ijppt = +( i - *mpjpt );
3995 
3996 
3997 #if FREEFREQUENTLY
3998 				freegaplenpartly( gaplen1jbest[j-1], i-3, i-2 );
3999 //				freegaplenpartly( gaplen2jbest[j-1], j-3, j-2 );
4000 #endif
4001 
4002 
4003 #if DEBUG
4004 				reporterr( "Selected as jgap end!, pfac = %f\n", pfac );
4005 				fprintf( stderr, "Jump from %d (%c) to %d (%c)!\n", j, seq1[0][j], *mpjpt, seq1[0][*mpjpt] );
4006 #endif
4007 //				addnewgaplen( gaplen1jbest[j-1], gaplen1jbestkamo[j-1], gaplen1, lgth1, -1, 0 );
4008 //				addnewgaplen( gaplen2jbest[j-1], gaplen2jbestkamo[j-1], gaplen2, lgth2, -1, 0 );
4009 				copygaplencompactx( gaplen1jbest[j-1], gaplen1jbestkamo[j-1], lgth1, -1, 0, i, i );// *mpjpt, i
4010 //				copygaplenrestricted( gaplen2jbest[j-1], gaplen2jbestkamo[j-1], lgth2, -1, 0, j, j ); // j-1, j
4011 				copygaplencompactx( gaplen2jbest[j-1], gaplen2jbestkamo[j-1], lgth2, -1, 0, 1, 1 ); // half!
4012 
4013 
4014 
4015 
4016 			}
4017 
4018 
4019 //			extendgaplenpartly( gaplen1jbest[j-1], gaplen1, i, i ); // tmptmptmp
4020 //			extendgaplenpartly( gaplen2jbest[j-1], gaplen2, 0, 0 ); // tmptmptmp
4021 
4022 #if 0
4023 			sfac = 1.0;
4024 			for( l=0; gaplen1[i+1]&&(gl=gaplen1[i+1][l].len)!=-1; l++ ) // ososugi!  hash ni atode henkou
4025 				if( gl - 1 == gaplen1[i+1][l].relend ) sfac -= gaplen1[i+1][l].freq;
4026 			sfac2 = 1.0;
4027 			for( k=0; gaplen2[j+1]&&(gl=gaplen2[j+1][k].len)!=-1; k++ ) // ososugi!  hash ni atode henkou
4028 				if( gaplen2[j+1][k].relend != -1 ) sfac2 -= gaplen2[j+1][k].freq;
4029 			sfac *= sfac2;
4030 #else
4031 //			sfac = 0.0;
4032 #endif
4033 
4034 #if DEBUG
4035 			reporterr( " (jgap start check i=%d) -> *prept=%f, *mjpt=%f\n", i, seq1[0][i], seq2[0][j], *prept, *mjpt );
4036 #endif
4037 
4038 #if ALGZGAP
4039 			if( (g=*prept+ ogcp1va* *gf2ptpre) >= *mjpt )
4040 #else
4041 //			if( (g=*prept + fpenalty * sfac ) >= *mjpt )
4042 			if( (g=*prept ) >= *mjpt )
4043 #endif
4044 			{
4045 //				*mjpt = g - fpenalty * sfac;
4046 				*mjpt = g;
4047 				*mpjpt = i-1;
4048 #if DEBUG
4049 				reporterr( "Selected as jgap start!\n" );
4050 #endif
4051 
4052 
4053 #if FREEFREQUENTLY
4054 				freegaplenpartly( gaplen1jbestkamo[j-1], i-3, i-2 );
4055 //				freegaplenpartly( gaplen2jbestkamo[j-1], j-3, j-2 );
4056 #endif
4057 
4058 
4059 //				addnewgaplen( gaplen1jbestkamo[j-1], gaplen1jprev[j-1], gaplen1, lgth1, -1, 0 );
4060 //				addnewgaplen( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], gaplen2, lgth2, -1, 0 );
4061 //				reporterr( "copying gaplen1jbestkamo[%d-1] from galpen1jprev, j=%d, i=%d\n", j, j, i );
4062 				copygaplencompactx( gaplen1jbestkamo[j-1], gaplen1jprev[j-1], lgth1, -1, 0, i, i ); // *mpjpt, i
4063 //				copygaplenrestricted( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], lgth2, -1, 0, j, j ); // j-1, j
4064 //				copygaplencompactx( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], lgth2, -1, 0, j, 1 ); // half!
4065 //				reporterr( "copying gaplen2jbestkamo[%d-1] from galpen2jprev\n", j );
4066 				copygaplencompactx( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], lgth2-j, -1, 0, 1, 1 ); // ryouhou half!
4067 
4068 
4069 //				if( j==2 && i==1 ) exit( 1 );
4070 
4071 
4072 
4073 			}
4074 
4075 //			extendgaplenpartly( gaplen1ibestkamo[i-1], gaplen1, 0, 0 ); // tmptmptmp
4076 //			extendgaplenpartly( gaplen2ibestkamo[i-1], gaplen2, j, j ); // tmptmptmp
4077 
4078 
4079 //			extendgaplenpartly( gaplen1jbestkamo[j-1], gaplen1, i, i ); // tmptmptmp
4080 //			extendgaplenpartly( gaplen2jbestkamo[j-1], gaplen2, 0, 0 ); // tmptmptmp
4081 
4082 
4083 #if USE_PENALTY_EX
4084 			m[j] += fpenalty_ex;
4085 #endif
4086 
4087 #if ALGZGAP
4088 			pfac = 0.0;
4089 #else
4090 
4091 //			pfactmp = calcpfac_gapex( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], j, i, i-*mpjpt, seq2[0], seq1[0], 0 ); // j-1
4092 			pfactmp = calcpfac_gapex_noidatend( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], 1, i, i-*mpjpt, seq2[0]+j, seq1[0], 0 ); // 2jbestkamo->half, 1jbestkamo->full
4093 #if USEGAPLENMTX
4094 			pfac = calcpfac_gapex( gaplen2mtx[*mpjpt][j-1], gaplen1mtx[*mpjpt][j-1], j, i, i-*mpjpt, seq2[0], seq1[0], 0 );
4095 #endif
4096 #if USEGAPLENHALF
4097 			pfac = calcpfac_gapex( gaplen2half[*mpjpt][j-1], gaplen1half[*mpjpt][j-1], 1, i-*mpjpt, i-*mpjpt, seq2[0]+j, seq1[0]+*mpjpt, 0 );
4098 #endif
4099 #if USEGAPLENMTX || USEGAPLENHALF
4100 			if( pfac != pfactmp )
4101 			{
4102 				reporterr( "(jgapex) pfac=%f, but pfactmp=%f (i,j=%d,%d) diff=%f\n", pfac, pfactmp, i, j, pfac-pfactmp );
4103 //				exit( 1 );
4104 				PFACERROR = 1;
4105 			}
4106 #endif
4107 			pfac = pfactmp;
4108 #if DEBUG
4109 			reporterr( "%c-%c, jgap extension check (j=%d), pfac = %f\n", seq1[0][i], '=', j, pfac );
4110 #endif
4111 #endif
4112 			m[j] += fpenalty * pfac;
4113 
4114 
4115 
4116 			if( trywarp )
4117 			{
4118 #if USE_PENALTY_EX
4119 				if( ( g=*prevwmrecordspt++ + fpenalty_shift + fpenalty_ex * ( i - prevwarpi[j-1] + j - prevwarpj[j-1] ) ) > wm ) // naka ha osokute kamawanai
4120 #else
4121 				if( ( g=*prevwmrecordspt++ + fpenalty_shift ) > wm ) // naka ha osokute kamawanai
4122 #endif
4123 				{
4124 //					fprintf( stderr, "WARP!!\n" );
4125 					if( warpn && prevwarpi[j-1] == warpis[warpn-1] && prevwarpj[j-1] == warpjs[warpn-1] )
4126 					{
4127 						*ijppt = warpbase + warpn - 1;
4128 					}
4129 					else
4130 					{
4131 						*ijppt = warpbase + warpn;
4132 						warpis = realloc( warpis, sizeof(int) * ( warpn+1 ) );
4133 						warpjs = realloc( warpjs, sizeof(int) * ( warpn+1 ) );
4134 						warpis[warpn] = prevwarpi[j-1];
4135 						warpjs[warpn] = prevwarpj[j-1];
4136 						warpn++;
4137 					}
4138 					wm = g;
4139 				}
4140 
4141 #if 0
4142 				fprintf( stderr, "%5.0f ", wm );
4143 #endif
4144 				curm = *curpt + wm;
4145 
4146 				if( *wmrecords1pt > *wmrecordspt )
4147 				{
4148 					*wmrecordspt = *wmrecords1pt;
4149 					*warpipt  = *(warpipt-1);
4150 					*warpjpt  = *(warpjpt-1);
4151 				}
4152 				if( curm > *wmrecordspt )
4153 				{
4154 					*wmrecordspt = curm;
4155 					*warpipt = i;
4156 					*warpjpt = j;
4157 				}
4158 				wmrecordspt++;
4159 				wmrecords1pt++;
4160 				warpipt++;
4161 				warpjpt++;
4162 			}
4163 
4164 #if DEBUG
4165 			reporterr( "extention-x 1j???, before extention-x, j=%d\n", j );
4166 			showgaplen( gaplen1jcurr[j], 100 );
4167 #endif
4168 			extendgaplencompactx( gaplen1jcurr[j], gaplen1, i );
4169 
4170 #if DEBUG
4171 			reporterr( "after extention-x\n" );
4172 			showgaplen( gaplen1jcurr[j], 100 );
4173 			reporterr( "extention-x 2j???\n" );
4174 #endif
4175 			extendgaplencompactx( gaplen2jcurr[j], gaplen2+j, 0 );
4176 
4177 
4178 #if 1
4179 			if( *ijppt < 0 )
4180 			{
4181 #if DEBUG
4182 				reporterr( "Path: %d-%d->%d-%d, i=%d (%c), j=%d (%c), *ijppt=%d\n", i, j, i-1, j+*ijppt, i, seq1[0][i], j, seq2[0][j], *ijppt );
4183 				reporterr( "Inserting %d gaps to gaplen1 and copying gaplen2 (%c%d-%c%d)\n", -*ijppt-1, seq1[0][i], i, seq2[0][j], j );
4184 #endif
4185 #if USEGAPLENMTX
4186 //				addnewgaplen( gaplen1mtx[i][j], gaplen1mtx[i-1][j+*ijppt], gaplen1, lgth1, i, -*ijppt-1 );
4187 //				addnewgaplen( gaplen2mtx[i][j], gaplen2mtx[i-1][j+*ijppt], gaplen2, lgth2, -1, 0 );
4188 				copygaplenrestricted( gaplen1mtx[i][j], gaplen1mtx[i-1][j+*ijppt], lgth1, i, -*ijppt-1, i, i ); // 0, lgth1
4189 				copygaplenrestricted( gaplen2mtx[i][j], gaplen2mtx[i-1][j+*ijppt], lgth2, -1, 0, j, j ); // 0, lgth2
4190 #endif
4191 #if USEGAPLENHALF
4192 				copygaplenrestricted_zurasu( gaplen1half[i][j], gaplen1half[i-1][j+*ijppt], lgth1, 0, -*ijppt-1, 0, 0, 1, 1 ); // 0, lgth1
4193 				copygaplenrestricted_zurasu( gaplen2half[i][j], gaplen2half[i-1][j+*ijppt], lgth2, -1, 0, 0, 0, -*ijppt, -*ijppt ); // 0, lgth2
4194 #endif
4195 //				addnewgaplen( gaplen1jcurr[j], gaplen1jprev[j+*ijppt], gaplen1, lgth1, i, -*ijppt-1 );
4196 //				addnewgaplen( gaplen2jcurr[j], gaplen2jprev[j+*ijppt], gaplen2, lgth2, -1, 0 );
4197 //				reporterr( "copying gaplen1jcurr from gaplen1jbest, with a %d insertion\n", -*ijppt-1 );
4198 				copygaplencompactx( gaplen1jcurr[j], gaplen1jprev[j+*ijppt], lgth1, i, -*ijppt-1, i, i ); // scope: i+*ijppt+1, i ?
4199 //				reporterr( "copy end\n" );
4200 //				copygaplenrestricted( gaplen2jcurr[j], gaplen2jprev[j+*ijppt], lgth2, -1, 0, j, j );
4201 				copygaplencompactx( gaplen2jcurr[j], gaplen2jprev[j+*ijppt], lgth2, -1, 0, 0, -*ijppt ); // half! ryouho zureteru
4202 			}
4203 			else if( *ijppt > 0 )
4204 			{
4205 #if DEBUG
4206 				reporterr( "Path: %d-%d->%d-%d, i=%d (%c), j=%d (%c), *ijppt=%d\n", i, j, i-*ijppt, j-1, i, seq1[0][i], j, seq2[0][j], *ijppt );
4207 				reporterr( "Copying gaplen1 inserting %d gaps to gaplen2 (%c%d-%c%d)\n", *ijppt-1, seq1[0][i], i, seq2[0][j], j );
4208 #endif
4209 #if USEGAPLENMTX
4210 //				addnewgaplen( gaplen1mtx[i][j], gaplen1mtx[i-*ijppt][j-1], gaplen1, lgth1, -1, 0 );
4211 //				addnewgaplen( gaplen2mtx[i][j], gaplen2mtx[i-*ijppt][j-1], gaplen2, lgth2, j, *ijppt-1 );
4212 				copygaplenrestricted( gaplen1mtx[i][j], gaplen1mtx[i-*ijppt][j-1], lgth1, -1, 0, i, i ); // 0, lgth1
4213 				copygaplenrestricted( gaplen2mtx[i][j], gaplen2mtx[i-*ijppt][j-1], lgth2, j, *ijppt-1, j, j ); // 0, lgth2
4214 #endif
4215 #if USEGAPLENHALF
4216 				copygaplenrestricted_zurasu( gaplen1half[i][j], gaplen1half[i-*ijppt][j-1], lgth1, -1, 0, 0, 0, *ijppt, *ijppt ); // 0, lgth1
4217 				copygaplenrestricted_zurasu( gaplen2half[i][j], gaplen2half[i-*ijppt][j-1], lgth2, 0, *ijppt-1, 0, 0, 1, 1 ); // 0, lgth2
4218 #endif
4219 //				addnewgaplen( gaplen1jcurr[j], gaplen1jbest[j-1], gaplen1, lgth1, -1, 0 );
4220 //				addnewgaplen( gaplen2jcurr[j], gaplen2jbest[j-1], gaplen2, lgth2, j, *ijppt-1 );
4221 				copygaplencompactx( gaplen1jcurr[j], gaplen1jbest[j-1], lgth1, -1, 0, i, i );
4222 //				copygaplenrestricted( gaplen2jcurr[j], gaplen2jbest[j-1], lgth2, j, *ijppt-1, j, j ); // j-*ijppt+1?
4223 //				copygaplenrestricted_zurasu( gaplen2jcurr[j], gaplen2jbest[j-1], lgth2, 0, *ijppt-1, 0, 0, j, j ); // 2jcurr->half, but 2jbest->full, imanotokoro
4224 				copygaplencompactx( gaplen2jcurr[j], gaplen2jbest[j-1], lgth2, 0, *ijppt-1, 0, 1 ); //ryouhou half
4225 
4226 			}
4227 			else
4228 #endif
4229 			{
4230 #if DEBUG
4231 				reporterr( "Path: %d-%d->%d-%d, i=%d (%c), j=%d (%c), *ijppt=%d\n", i, j, i-1, j-1, i, seq1[0][i], j, seq2[0][j], *ijppt );
4232 				reporterr( "Copying gaplen1 and gaplen2 (%c%d-%c%d)\n", seq1[0][i], i, seq2[0][j], j );
4233 #endif
4234 #if USEGAPLENMTX
4235 //				addnewgaplen( gaplen1mtx[i][j], gaplen1mtx[i-1][j-1], gaplen1, lgth1, -1, 0 );
4236 //				addnewgaplen( gaplen2mtx[i][j], gaplen2mtx[i-1][j-1], gaplen2, lgth2, -1, 0 );
4237 				copygaplenrestricted( gaplen1mtx[i][j], gaplen1mtx[i-1][j-1], lgth1, -1, 0, i, i ); // 0, lgth1
4238 				copygaplenrestricted( gaplen2mtx[i][j], gaplen2mtx[i-1][j-1], lgth2, -1, 0, j, j ); // 0, lgth2
4239 #endif
4240 #if USEGAPLENHALF
4241 				copygaplenrestricted_zurasu( gaplen1half[i][j], gaplen1half[i-1][j-1], lgth1, -1, 0, 0, 0, 1, 1 ); // 0, lgth1
4242 				copygaplenrestricted_zurasu( gaplen2half[i][j], gaplen2half[i-1][j-1], lgth2, -1, 0, 0, 0, 1, 1 ); // 0, lgth2
4243 #endif
4244 //				addnewgaplen( gaplen1jcurr[j], gaplen1jprev[j-1], gaplen1, lgth1, -1, 0 );
4245 //				addnewgaplen( gaplen2jcurr[j], gaplen2jprev[j-1], gaplen2, lgth2, -1, 0 );
4246 				copygaplencompactx( gaplen1jcurr[j], gaplen1jprev[j-1], lgth1, -1, 0, i, i );
4247 //				copygaplenrestricted( gaplen2jcurr[j], gaplen2jprev[j-1], lgth2, -1, 0, j, j );
4248 				copygaplencompactx( gaplen2jcurr[j], gaplen2jprev[j-1], lgth2, -1, 0, 0, 1 ); // half
4249 			}
4250 
4251 #if DEBUG
4252 			reporterr( "at the end of j loop, gaplen1jcurr[%d] = \n", j );
4253 			showgaplen( gaplen1jcurr[j], 100 );
4254 			reporterr( "at the end of j loop, gaplen1prev[%d] = \n", j );
4255 			showgaplen( gaplen1jprev[j], 100 );
4256 #endif
4257 
4258 
4259 
4260 #if 1
4261 			freegaplenpartly( gaplen1jcurr[j-1], i-3, i-2 ); // -1 dehanaku -2??
4262 //			freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // -1 dehanaku -2??
4263 //			freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // half!
4264 			freegaplenpartly( gaplen1jbestkamo[j-1], i-3, i-2 ); // -1 dehanaku -2??
4265 //			freegaplenpartly( gaplen2jbestkamo[j-1], j-3, j-2 ); // -1 dehanaku -2??
4266 			freegaplenpartly( gaplen1jbest[j-1], i-3, i-2 ); // -1 dehanaku -2??
4267 //			freegaplenpartly( gaplen2jbest[j-1], j-3, j-2 ); // -1 dehanaku -2??
4268 #else
4269 			freegaplenpartly( gaplen1jprev[j-1], 0, i-2 ); // -1 dehanaku -2??
4270 			freegaplenpartly( gaplen1jcurr[j-1], 0, i-2 ); // -1 dehanaku -2??
4271 //			freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // -1 dehanaku -2??
4272 //			freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // half!
4273 			freegaplenpartly( gaplen1jbestkamo[j-1], 0, i-2 ); // -1 dehanaku -2??
4274 //			freegaplenpartly( gaplen2jbestkamo[j-1], j-3, j-2 ); // -1 dehanaku -2??
4275 			freegaplenpartly( gaplen1jbest[j-1], 0, i-2 ); // -1 dehanaku -2??
4276 //			freegaplenpartly( gaplen2jbest[j-1], j-3, j-2 ); // -1 dehanaku -2??
4277 #endif
4278 
4279 
4280 #if USEGAPLENMTX
4281 //			freegaplenpartly( gaplen1mtx[i-1][j-1], 0, i-2 );
4282 //			freegaplenpartly( gaplen2mtx[i-1][j-1], 0, j-2 );
4283 #endif
4284 
4285 
4286 			*curpt++ += wm;
4287 			ijppt++;
4288 			mjpt++;
4289 			prept++;
4290 			mpjpt++;
4291 		}
4292 		lastverticalw[i] = currentw[lgth2-1];
4293 
4294 #if 1
4295 //		freegaplenpartly( gaplen1icurr[i-1], i-1, i-1 );
4296 		freegaplenpartly( gaplen1icurr[i-1], 0, lgth1-i );
4297 		freegaplenpartly( gaplen2icurr[i-1], 0, lgth2 );
4298 //		freegaplenpartly( gaplen1ibestkamo[i-1], i-1, i-1 );
4299 		freegaplenpartly( gaplen1ibestkamo[i-1], 0, lgth1-i );
4300 		freegaplenpartly( gaplen2ibestkamo[i-1], 0, lgth2 );
4301 #endif
4302 
4303 		if( trywarp )
4304 		{
4305 			fltncpy( prevwmrecords, wmrecords, lastj );
4306 			intncpy( prevwarpi, warpi, lastj );
4307 			intncpy( prevwarpj, warpj, lastj );
4308 		}
4309 #if 0
4310 		fprintf( stderr, "i=%d, %15.5f \n", i, wm );
4311 #endif
4312 //if( i == 2 ) exit( 1 );
4313 	}
4314 
4315 	if( trywarp )
4316 	{
4317 //		fprintf( stderr, "wm = %f\n", wm );
4318 //		fprintf( stderr, "warpn = %d\n", warpn );
4319 		free( wmrecords );
4320 		free( prevwmrecords );
4321 		free( warpi );
4322 		free( warpj );
4323 		free( prevwarpi );
4324 		free( prevwarpj );
4325 	}
4326 
4327 
4328 #if OUTGAP0TRY
4329 	if( !outgap )
4330 	{
4331 		for( j=1; j<lgth2+1; j++ )
4332 			currentw[j] -= offset * ( lgth2 - j ) / 2.0;
4333 		for( i=1; i<lgth1+1; i++ )
4334 			lastverticalw[i] -= offset * ( lgth1 - i  / 2.0);
4335 	}
4336 #endif
4337 
4338 	/*
4339 	fprintf( stderr, "\n" );
4340 	for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
4341 	fprintf( stderr, "#####\n" );
4342 	for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
4343 	fprintf( stderr, "====>" );
4344 	for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
4345 	for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
4346 	*/
4347 	if( localhom )
4348 	{
4349 		Atracking_localhom( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, ijp, icyc, jcyc, warpis, warpjs, warpbase );
4350 	}
4351 	else
4352 		Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, ijp, icyc, jcyc, tailgp, warpis, warpjs, warpbase );
4353 
4354 	if( warpis ) free( warpis );
4355 	if( warpjs ) free( warpjs );
4356 
4357 //	fprintf( stderr, "### impmatch = %f\n", *impmatch );
4358 
4359 	resultlen = strlen( mseq1[0] );
4360 	if( alloclen < resultlen || resultlen > N )
4361 	{
4362 		fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
4363 		ErrorExit( "LENGTH OVER!\n" );
4364 	}
4365 
4366 
4367 	for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
4368 	for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
4369 #if 0
4370 	fprintf( stderr, "\n" );
4371 	for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
4372 	fprintf( stderr, "#####\n" );
4373 	for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
4374 #endif
4375 
4376 //	reporterr( "clearing\n" );
4377 	cleargaplens( gaplens );
4378 
4379 #if USEGAPLENMTX
4380 /* maikai free */
4381 	reporterr( "Freeing!\n" );
4382 	for( i=0; i<lgth1+1; i++ )
4383 	{
4384 		for( j=0; j<lgth2+1; j++ )
4385 		{
4386 			if( gaplen1mtx[i][j] ) FreeGaplenMtx( gaplen1mtx[i][j], 0 );
4387 			gaplen1mtx[i][j] = NULL;
4388 		}
4389 		free( gaplen1mtx[i] );
4390 		gaplen1mtx[i] = NULL;
4391 	}
4392 	free( gaplen1mtx );
4393 	gaplen1mtx = NULL;
4394 
4395 	for( i=0; i<lgth1+1; i++ )
4396 	{
4397 		for( j=0; j<lgth2+1; j++ )
4398 		{
4399 			if( gaplen2mtx[i][j] ) FreeGaplenMtx( gaplen2mtx[i][j], 0 );
4400 			gaplen2mtx[i][j] = NULL;
4401 		}
4402 		free( gaplen2mtx[i] );
4403 		gaplen2mtx[i] = NULL;
4404 	}
4405 	free( gaplen2mtx );
4406 	gaplen2mtx = NULL;
4407 #endif
4408 
4409 
4410 #if USEGAPLENHALF
4411 	for( i=0; i<lgth1+1; i++ )
4412 	{
4413 		for( j=0; j<lgth2+1; j++ )
4414 		{
4415 			if( gaplen1half[i][j] ) FreeGaplenMtx( gaplen1half[i][j], 0 );
4416 			gaplen1half[i][j] = NULL;
4417 		}
4418 		free( gaplen1half[i] );
4419 		gaplen1half[i] = NULL;
4420 	}
4421 	free( gaplen1half );
4422 	gaplen1half = NULL;
4423 
4424 	for( i=0; i<lgth1+1; i++ )
4425 	{
4426 		for( j=0; j<lgth2+1; j++ )
4427 		{
4428 			if( gaplen2half[i][j] ) FreeGaplenMtx( gaplen2half[i][j], 0 );
4429 			gaplen2half[i][j] = NULL;
4430 		}
4431 		free( gaplen2half[i] );
4432 		gaplen2half[i] = NULL;
4433 	}
4434 	free( gaplen2half );
4435 	gaplen2half = NULL;
4436 #endif
4437 /* maikai free */
4438 
4439 
4440 #if WMCHECK
4441 	fprintf( stderr, "wm = %f\n", wm - *impmatch);
4442 	fprintf( stderr, "*impmatch = %f\n", *impmatch);
4443 
4444 	int kenzan = 0;
4445 	for( i=0; i<icyc; i++ ) for( j=0; j<jcyc; j++ )
4446 	{
4447 		kenzan += pairgapcount( mseq1[i], mseq2[j] );
4448 	}
4449 
4450 
4451 	reporterr( "kenzan = %d -> %f\n", kenzan, (double)kenzan /( icyc*jcyc ) );
4452 
4453 	double pairscore, nogappairscore;
4454 	char **pseq;
4455 	pseq = AllocateCharMtx( 2, strlen( seq1[0] ) + 1 );
4456 	pairscore = nogappairscore = 0.0;
4457 	for( i=0; i<icyc; i++ ) for( j=0; j<jcyc; j++ )
4458 	{
4459 		strcpy( pseq[0], seq1[i] );
4460 		strcpy( pseq[1], seq2[j] );
4461 		commongappick( 2, pseq );
4462 		pairscore += eff1[i] * eff2[j] * naivepairscore11_dynmtx( n_dynamicmtx, pseq[0], pseq[1], penalty );
4463 		nogappairscore += eff1[i] * eff2[j] * naivepairscore11_dynmtx( n_dynamicmtx, pseq[0], pseq[1], 0 );
4464 	}
4465 
4466 	FreeCharMtx( pseq );
4467 	reporterr( "pairscore = %f\n", (double)pairscore );
4468 	reporterr( "pairscore-nogappairscore = %f\n", (double)(pairscore-nogappairscore) );
4469 	reporterr( "pairscore-nogappairscore / penalty = %f\n", (double)(pairscore-nogappairscore)/(double)(fpenalty) );
4470 	reporterr( "diff = %f\n\n", (pairscore - wm + *impmatch ) / fpenalty );
4471 
4472 #if 1
4473 	if( ( !trywarp && abs( pairscore - wm +*impmatch ) > 0.01 ) || PFACERROR )
4474 //	if( abs( pairscore - wm +*impmatch ) > 0.01 )
4475 #else
4476 	if( abs( pairscore - wm +*impmatch ) > 0.01 )
4477 #endif
4478 //	if( abs( pairscore - wm +*impmatch ) > 0.01 )
4479 	{
4480 		for( i=0; i<icyc; i++ )
4481 			printf( ">group1\n%s\n", seq1[i] );
4482 		for( j=0; j<jcyc; j++ )
4483 			printf( ">group2\n%s\n", seq2[j] );
4484 		exit( 1 );
4485 	}
4486 #else
4487 	reporterr( "\n" );
4488 #endif
4489 
4490 #if 0
4491 //	if( strlen( seq1[0] ) - lgth1 > 100 && icyc > 1 || strlen( seq2[0] ) - lgth2 > 100 & jcyc > 1 )
4492 	if( strstr( seq1[0], "LNDDAT" ) && icyc == 1 || strstr( seq2[0], "LNDDAT" ) && jcyc==1)
4493 	{
4494 		for( i=0; i<icyc; i++ )
4495 			printf( ">group1\n%s\n", seq1[i] );
4496 		for( j=0; j<jcyc; j++ )
4497 			printf( ">group2\n%s\n", seq2[j] );
4498 		exit( 1 );
4499 	}
4500 #endif
4501 
4502 
4503 	return( wm );
4504 }
4505 
4506 
D__align_ls(double ** n_dynamicmtx,char ** seq1,char ** seq2,double * eff1,double * eff2,int icyc,int jcyc,int alloclen,LocalHom *** localhom,double * impmatch,char * sgap1,char * sgap2,char * egap1,char * egap2,int * chudanpt,int chudanref,int * chudanres,int headgp,int tailgp)4507 double D__align_ls( double **n_dynamicmtx, char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, LocalHom ***localhom, double *impmatch, char *sgap1, char *sgap2, char *egap1, char *egap2, int *chudanpt, int chudanref, int *chudanres, int headgp, int tailgp )
4508 {
4509 	int v1, v2;
4510 	double val;
4511 
4512 #if 1
4513 	v1 = gapvariety( icyc, strlen( seq1[0] ), seq1 );
4514 	v2 = gapvariety( jcyc, strlen( seq2[0] ), seq2 );
4515 #else
4516 	v1 = icyc;
4517 	v2 = jcyc;
4518 #endif
4519 
4520 //	reporterr( "\nicyc,jcyc = %d,%d\n", icyc, jcyc );
4521 	reporterr( " v1,v2 = %d,%d\n", v1, v2 );
4522 
4523 	if( v1 >= v2 )
4524 	{
4525 		val = D__align( n_dynamicmtx, seq1, seq2, eff1, eff2, icyc, jcyc, alloclen, localhom, impmatch, sgap1, sgap2, egap1, egap2, chudanpt, chudanref, chudanres, headgp, tailgp );
4526 	}
4527 	else
4528 	{
4529 		val = D__align( n_dynamicmtx, seq2, seq1, eff2, eff1, jcyc, icyc, alloclen, localhom, impmatch, sgap2, sgap1, egap2, egap1, chudanpt, chudanref, chudanres, headgp, tailgp );
4530 	}
4531 	return val;
4532 }
4533 
4534 
4535 
D__align_gapmap(char ** seq1,char ** seq2,double * eff1,double * eff2,int icyc,int jcyc,int alloclen,LocalHom *** localhom,double * impmatch,int * gapmap1,int * gapmap2)4536 double D__align_gapmap( char **seq1, char **seq2, double *eff1, double *eff2, int icyc, int jcyc, int alloclen, LocalHom ***localhom, double *impmatch, int *gapmap1, int *gapmap2 )
4537 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
4538 {
4539 	fprintf( stderr, "Unexpected error.  Please contact kazutaka.katoh@aist.go.jp\n" );
4540 	exit( 1 );
4541 }
4542 
4543 
D__align_variousdist(int ** which,double *** matrices,double ** n_dynamicmtx,char ** seq1,char ** seq2,double * eff1,double * eff2,double ** eff1s,double ** eff2s,int icyc,int jcyc,int alloclen,LocalHom *** localhom,double * impmatch,char * sgap1,char * sgap2,char * egap1,char * egap2,int * chudanpt,int chudanref,int * chudanres,int headgp,int tailgp)4544 double D__align_variousdist( int **which, double ***matrices, double **n_dynamicmtx, char **seq1, char **seq2, double *eff1, double *eff2, double **eff1s, double **eff2s, int icyc, int jcyc, int alloclen, LocalHom ***localhom, double *impmatch, char *sgap1, char *sgap2, char *egap1, char *egap2, int *chudanpt, int chudanref, int *chudanres, int headgp, int tailgp )
4545 /* score no keisan no sai motokaraaru gap no atukai ni mondai ga aru */
4546 {
4547 
4548 //	int k;
4549 	register int i, j, c;
4550 	int lasti, lastj;      /* outgap == 0 -> lgth1, outgap == 1 -> lgth1+1 */
4551 	int lgth1, lgth2;
4552 	int resultlen;
4553 	double wm = 0.0;   /* int ?????? */
4554 	double g;
4555 	double *currentw, *previousw;
4556 //	double fpenalty = (double)penalty;
4557 #if USE_PENALTY_EX
4558 	double fpenalty_ex = (double)penalty_ex;
4559 #endif
4560 #if 1
4561 	double *wtmp;
4562 	int *ijppt;
4563 	double *mjpt, *prept, *curpt;
4564 	int *mpjpt;
4565 #endif
4566 	static TLS double mi, *m;
4567 	static TLS int **ijp;
4568 	static TLS int mpi, *mp;
4569 	static TLS double *w1, *w2;
4570 	static TLS double *match;
4571 	static TLS double *initverticalw;    /* kufuu sureba iranai */
4572 	static TLS double *lastverticalw;    /* kufuu sureba iranai */
4573 	static TLS char **mseq1;
4574 	static TLS char **mseq2;
4575 	static TLS char **mseq;
4576 	static TLS double ***cpmx1s;
4577 	static TLS double ***cpmx2s;
4578 	static TLS int ***intwork;
4579 	static TLS double ***doublework;
4580 	static TLS int orlgth1 = 0, orlgth2 = 0;
4581 #if USEGAPLENHALF
4582 	Gaplen ****gaplen1half = NULL; // NULL ga iru to omou.
4583 	Gaplen ****gaplen2half = NULL; // NULL ga iru to omou.
4584 #endif
4585 #if USEGAPLENMTX
4586 	Gaplen ****gaplen1mtx = NULL; // NULL ga iru to omou.
4587 	Gaplen ****gaplen2mtx = NULL; // NULL ga iru to omou.
4588 #endif
4589 	static TLS Gaplen **gaplen1 = NULL; // NULL ga iru to omou.
4590 	static TLS Gaplen **gaplen2 = NULL; // NULL ga iru to omou.
4591 	static TLS Gaplen ***gaplen1jprev = NULL;
4592 	static TLS Gaplen ***gaplen2jprev = NULL;
4593 	static TLS Gaplen ***gaplen1jcurr = NULL;
4594 	static TLS Gaplen ***gaplen2jcurr = NULL;
4595 	static TLS Gaplen ***gaplen1icurr = NULL;
4596 	static TLS Gaplen ***gaplen2icurr = NULL;
4597 	static TLS Gaplen ***gaplen1jbestkamo = NULL;
4598 	static TLS Gaplen ***gaplen2jbestkamo = NULL;
4599 	static TLS Gaplen ***gaplen1ibestkamo = NULL;
4600 	static TLS Gaplen ***gaplen2ibestkamo = NULL;
4601 	static TLS Gaplen ***gaplen1jbest = NULL;
4602 	static TLS Gaplen ***gaplen2jbest = NULL;
4603 	double fpenalty = (double)penalty;
4604 	double fpenalty_shift = (double)penalty_shift;
4605 	static TLS Gaplen ****gaplens = NULL;
4606 
4607 	Gaplen ***gaplentmp = NULL;
4608 	int *warpis = NULL;
4609 	int *warpjs = NULL;
4610 	int *warpi = NULL;
4611 	int *warpj = NULL;
4612 	int *prevwarpi = NULL;
4613 	int *prevwarpj = NULL;
4614 	double *wmrecords = NULL;
4615 	double *prevwmrecords = NULL;
4616 	int warpn = 0;
4617 	int warpbase;
4618 	double curm = 0.0;
4619 	double *wmrecordspt, *wmrecords1pt, *prevwmrecordspt;
4620 	int *warpipt, *warpjpt;
4621 	int k;
4622 	double pfac, pfactmp;
4623 	int newgaplen;
4624 	int **masklist1 = NULL, **masklist2 = NULL;
4625 	int *nmask;
4626 
4627 //	for( i=0; i<icyc; i++ ) fprintf( stderr, "%s, %f\n", seq1[i], eff1[i] );
4628 //	for( i=0; i<jcyc; i++ ) fprintf( stderr, "%s, %f\n", seq2[i], eff2[i] );
4629 
4630 //	reporterr( "\nsizeof(gaplen) = %d\n", sizeof( Gaplen ) );
4631 //	reporterr( "\nsizeof(int) = %d\n", sizeof( int ) );
4632 //	reporterr( "\nsizeof(double) = %d\n", sizeof( double ) );
4633 //	reporterr( "\nsizeof(double*) = %d\n", sizeof( double * ) );
4634 
4635 
4636 	if( seq1 == NULL )
4637 	{
4638 		if( orlgth1 )
4639 		{
4640 //			fprintf( stderr, "## Freeing local arrays in D__align\n" );
4641 			orlgth1 = 0;
4642 			orlgth2 = 0;
4643 
4644 			imp_match_init_strictD( NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0 );
4645 
4646 			free( mseq1 );
4647 			free( mseq2 );
4648 			FreeFloatVec( w1 );
4649 			FreeFloatVec( w2 );
4650 			FreeFloatVec( match );
4651 			FreeFloatVec( initverticalw );
4652 			FreeFloatVec( lastverticalw );
4653 
4654 			FreeFloatVec( m );
4655 			FreeIntVec( mp );
4656 
4657 			FreeCharMtx( mseq );
4658 
4659 			FreeFloatCub( cpmx1s );
4660 			FreeFloatCub( cpmx2s );
4661 
4662 			FreeFloatCub( doublework );
4663 			FreeIntCub( intwork );
4664 
4665 
4666 
4667 			free( gaplens );
4668 			if( gaplen1ibestkamo ) FreeGaplenCub( gaplen1ibestkamo ); gaplen1ibestkamo = NULL;
4669 			if( gaplen2ibestkamo ) FreeGaplenCub( gaplen2ibestkamo ); gaplen2ibestkamo = NULL;
4670 			if( gaplen1icurr ) FreeGaplenCub( gaplen1icurr ); gaplen1icurr = NULL;
4671 			if( gaplen2icurr ) FreeGaplenCub( gaplen2icurr ); gaplen2icurr = NULL;
4672 
4673 			if( gaplen1jprev ) FreeGaplenCub( gaplen1jprev ); gaplen1jprev = NULL;
4674 			if( gaplen2jprev ) FreeGaplenCub( gaplen2jprev ); gaplen2jprev = NULL;
4675 			if( gaplen1jcurr ) FreeGaplenCub( gaplen1jcurr ); gaplen1jcurr = NULL;
4676 			if( gaplen2jcurr ) FreeGaplenCub( gaplen2jcurr ); gaplen2jcurr = NULL;
4677 			if( gaplen1jbestkamo ) FreeGaplenCub( gaplen1jbestkamo ); gaplen1jbestkamo = NULL;
4678 			if( gaplen2jbestkamo ) FreeGaplenCub( gaplen2jbestkamo ); gaplen2jbestkamo = NULL;
4679 			if( gaplen1jbest ) FreeGaplenCub( gaplen1jbest ); gaplen1jbest = NULL;
4680 			if( gaplen2jbest ) FreeGaplenCub( gaplen2jbest ); gaplen2jbest = NULL;
4681 			if( gaplen1 ) FreeGaplenMtx( gaplen1, 1 ); gaplen1 = NULL;
4682 			if( gaplen2 ) FreeGaplenMtx( gaplen2, 1 ); gaplen2 = NULL;
4683 
4684 		}
4685 		else
4686 		{
4687 //			fprintf( stderr, "## Not allocated\n" );
4688 		}
4689 		return( 0.0 );
4690 	}
4691 
4692 
4693 #if SLOW
4694 	nmask = calloc( maxdistclass, sizeof( int ) );
4695 #else
4696 	masklist1 = AllocateIntMtx( maxdistclass, 0 );
4697 	masklist2 = AllocateIntMtx( maxdistclass, 0 );
4698 	nmask = calloc( maxdistclass, sizeof( int ) );
4699 
4700 	for( c=0; c<maxdistclass; c++ )
4701 	{
4702 		for( i=0; i<icyc; i++ ) for( j=0; j<jcyc; j++ )
4703 		{
4704 			if( eff1s[c][i] * eff2s[c][j] != 0.0 )
4705 			{
4706 //				reporterr( "%d-%d, c=%d\n", i, j, c );
4707 				if( c != which[i][j] )
4708 				{
4709 					masklist1[c] = realloc( masklist1[c], sizeof( int ) * nmask[c]+1 );
4710 					masklist2[c] = realloc( masklist2[c], sizeof( int ) * nmask[c]+1 );
4711 
4712 					masklist1[c][nmask[c]] = i;
4713 					masklist2[c][nmask[c]] = j;
4714 					nmask[c]++;
4715 				}
4716 			}
4717 		}
4718 	}
4719 	for( c=0; c<maxdistclass; c++ ) if( nmask[c] ) break;
4720 	if( c<maxdistclass ) reporterr( "Found a complex grouping. This step may be a bit slow.\n", c );
4721 #endif
4722 
4723 
4724 	lgth1 = strlen( seq1[0] );
4725 	lgth2 = strlen( seq2[0] );
4726 
4727 
4728 	reporterr( "%d x %d sequences, len=%d, %d\n", icyc, jcyc, lgth1, lgth2 );
4729 
4730 
4731 #if 0
4732 	if( lgth1 == 0 || lgth2 == 0 )
4733 	{
4734 		fprintf( stderr, "WARNING (Aalignmm): lgth1=%d, lgth2=%d\n", lgth1, lgth2 );
4735 	}
4736 #endif
4737 	if( lgth1 == 0 && lgth2 == 0 )
4738 		return( 0.0 );
4739 
4740 	if( lgth1 == 0 )
4741 	{
4742 		for( i=0; i<icyc; i++ )
4743 		{
4744 			j = lgth2;
4745 			seq1[i][j] = 0;
4746 			while( j ) seq1[i][--j] = *newgapstr;
4747 //			fprintf( stderr, "seq1[i] = %s\n", seq1[i] );
4748 		}
4749 		return( 0.0 );
4750 	}
4751 
4752 	if( lgth2 == 0 )
4753 	{
4754 		for( i=0; i<jcyc; i++ )
4755 		{
4756 			j = lgth1;
4757 			seq2[i][j] = 0;
4758 			while( j ) seq2[i][--j] = *newgapstr;
4759 //			fprintf( stderr, "seq2[i] = %s\n", seq2[i] );
4760 		}
4761 		return( 0.0 );
4762 	}
4763 
4764 	warpbase = lgth1 + lgth2;
4765 	warpis = NULL;
4766 	warpjs = NULL;
4767 	warpn = 0;
4768 
4769 
4770 
4771 	if( trywarp )
4772 	{
4773 //		reporterr( "Not supported yet!\n" );
4774 //		exit( 1 );
4775 //		fprintf( stderr, "IN D__align, penalty_shift = %d\n", penalty_shift );
4776 		if( headgp == 0 || tailgp == 0 )
4777 		{
4778 			fprintf( stderr, "At present, headgp and tailgp must be 1 to allow shift.\n" );
4779 			exit( 1 );
4780 		}
4781 		wmrecords = AllocateFloatVec( lgth2+1 );
4782 		warpi = AllocateIntVec( lgth2+1 );
4783 		warpj = AllocateIntVec( lgth2+1 );
4784 		prevwmrecords = AllocateFloatVec( lgth2+1 );
4785 		prevwarpi = AllocateIntVec( lgth2+1 );
4786 		prevwarpj = AllocateIntVec( lgth2+1 );
4787 		for( i=0; i<lgth2+1; i++ ) wmrecords[i] = 0.0;
4788 		for( i=0; i<lgth2+1; i++ ) prevwmrecords[i] = 0.0;
4789 		for( i=0; i<lgth2+1; i++ ) prevwarpi[i] = -warpbase;
4790 		for( i=0; i<lgth2+1; i++ ) prevwarpj[i] = -warpbase;
4791 		for( i=0; i<lgth2+1; i++ ) warpi[i] = -warpbase;
4792 		for( i=0; i<lgth2+1; i++ ) warpj[i] = -warpbase;
4793 	}
4794 
4795 
4796 #if 0
4797 	fprintf( stderr, "####  eff in SA+++align\n" );
4798 	fprintf( stderr, "####  seq1[0] = %s\n", seq1[0] );
4799 	fprintf( stderr, "####  strlen( seq1[0] ) = %d\n", strlen( seq1[0] ) );
4800 	for( i=0; i<icyc; i++ ) fprintf( stderr, "eff1[%d] = %f\n", i, eff1[i] );
4801 	fprintf( stderr, "####  seq2[0] = %s\n", seq2[0] );
4802 	fprintf( stderr, "####  strlen( seq2[0] ) = %d\n", strlen( seq2[0] ) );
4803 	for( i=0; i<jcyc; i++ ) fprintf( stderr, "eff2[%d] = %f\n", i, eff2[i] );
4804 #endif
4805 	if( orlgth1 == 0 )
4806 	{
4807 		mseq1 = AllocateCharMtx( njob, 0 );
4808 		mseq2 = AllocateCharMtx( njob, 0 );
4809 	}
4810 
4811 	if( lgth1 > orlgth1 || lgth2 > orlgth2 )
4812 	{
4813 		int ll1, ll2;
4814 
4815 
4816 		if( orlgth1 > 0 && orlgth2 > 0 )
4817 		{
4818 			FreeFloatVec( w1 );
4819 			FreeFloatVec( w2 );
4820 			FreeFloatVec( match );
4821 			FreeFloatVec( initverticalw );
4822 			FreeFloatVec( lastverticalw );
4823 
4824 			FreeFloatVec( m );
4825 			FreeIntVec( mp );
4826 
4827 			FreeCharMtx( mseq );
4828 
4829 			FreeFloatCub( cpmx1s );
4830 			FreeFloatCub( cpmx2s );
4831 
4832 			FreeFloatCub( doublework );
4833 			FreeIntCub( intwork );
4834 
4835 
4836 			free( gaplens );
4837 
4838 			if( gaplen1ibestkamo ) FreeGaplenCub( gaplen1ibestkamo ); gaplen1ibestkamo = NULL;
4839 			if( gaplen2ibestkamo ) FreeGaplenCub( gaplen2ibestkamo ); gaplen2ibestkamo = NULL;
4840 			if( gaplen1icurr ) FreeGaplenCub( gaplen1icurr ); gaplen1icurr = NULL;
4841 			if( gaplen2icurr ) FreeGaplenCub( gaplen2icurr ); gaplen2icurr = NULL;
4842 
4843 			if( gaplen1jcurr ) FreeGaplenCub( gaplen1jcurr ); gaplen1jcurr = NULL;
4844 			if( gaplen1jprev ) FreeGaplenCub( gaplen1jprev ); gaplen1jprev = NULL;
4845 			if( gaplen2jcurr ) FreeGaplenCub( gaplen2jcurr ); gaplen2jcurr = NULL;
4846 			if( gaplen2jprev ) FreeGaplenCub( gaplen2jprev ); gaplen2jprev = NULL;
4847 			if( gaplen1jbestkamo ) FreeGaplenCub( gaplen1jbestkamo ); gaplen1jbestkamo = NULL;
4848 			if( gaplen2jbestkamo ) FreeGaplenCub( gaplen2jbestkamo ); gaplen2jbestkamo = NULL;
4849 			if( gaplen1jbest ) FreeGaplenCub( gaplen1jbest ); gaplen1jbest = NULL;
4850 			if( gaplen2jbest ) FreeGaplenCub( gaplen2jbest ); gaplen2jbest = NULL;
4851 			if( gaplen1 ) FreeGaplenMtx( gaplen1, 1 ); gaplen1 = NULL;
4852 			if( gaplen2 ) FreeGaplenMtx( gaplen2, 1 ); gaplen2 = NULL;
4853 
4854 		}
4855 
4856 		ll1 = MAX( (int)(1.3*lgth1), orlgth1 ) + 100;
4857 		ll2 = MAX( (int)(1.3*lgth2), orlgth2 ) + 100;
4858 
4859 #if DEBUG
4860 		fprintf( stderr, "\ntrying to allocate (%d+%d)xn matrices ... ", ll1, ll2 );
4861 #endif
4862 
4863 		w1 = AllocateFloatVec( ll2+2 );
4864 		w2 = AllocateFloatVec( ll2+2 );
4865 		match = AllocateFloatVec( ll2+2 );
4866 
4867 		initverticalw = AllocateFloatVec( ll1+2 );
4868 		lastverticalw = AllocateFloatVec( ll1+2 );
4869 
4870 		m = AllocateFloatVec( ll2+2 );
4871 		mp = AllocateIntVec( ll2+2 );
4872 
4873 		mseq = AllocateCharMtx( njob, ll1+ll2 );
4874 
4875 		cpmx1s = AllocateFloatCub( maxdistclass, nalphabets, ll1+2 );
4876 		cpmx2s = AllocateFloatCub( maxdistclass, nalphabets, ll2+2 );
4877 
4878 		doublework = AllocateFloatCub( maxdistclass, MAX( ll1, ll2 )+2, nalphabets );
4879 		intwork = AllocateIntCub( maxdistclass, MAX( ll1, ll2 )+2, nalphabets+1 );
4880 
4881 #if DEBUG
4882 		fprintf( stderr, "succeeded\n" );
4883 #endif
4884 
4885 		orlgth1 = ll1 - 100;
4886 		orlgth2 = ll2 - 100;
4887 
4888 
4889 //		reporterr( "Allocating gaplen1 and gaplen2\n" );
4890 		gaplen1 = (Gaplen ** )calloc( ll1+2, sizeof( Gaplen * ) );
4891 		gaplen1[ll1+1] = (Gaplen *)1;
4892 		gaplen2 = (Gaplen ** )calloc( ll2+2, sizeof( Gaplen * ) );
4893 		gaplen2[ll2+1] = (Gaplen *)1;
4894 
4895 
4896 //		reporterr( "Allocating gaplen*\n" );
4897 		gaplen1ibestkamo = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) );
4898 		gaplen2ibestkamo = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) );
4899 		gaplen1icurr = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) );
4900 		gaplen2icurr = (Gaplen ***)calloc( (ll1+2), sizeof( Gaplen **) );
4901 		gaplen1jbestkamo = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
4902 		gaplen2jbestkamo = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
4903 		gaplen1jbest = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
4904 		gaplen2jbest = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
4905 		gaplen1jcurr = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
4906 		gaplen2jcurr = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
4907 		gaplen1jprev = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
4908 		gaplen2jprev = (Gaplen ***)calloc( (ll2+2), sizeof( Gaplen **) );
4909 
4910 		gaplens = calloc( sizeof( Gaplen ***), 12 );
4911 		gaplens[0] = gaplen1ibestkamo;
4912 		gaplens[1] = gaplen2ibestkamo;
4913 		gaplens[2] = gaplen1icurr;
4914 		gaplens[3] = gaplen2icurr;
4915 		gaplens[4] = gaplen1jbestkamo;
4916 		gaplens[5] = gaplen2jbestkamo;
4917 		gaplens[6] = gaplen1jbest;
4918 		gaplens[7] = gaplen2jbest;
4919 		gaplens[8] = gaplen1jcurr;
4920 		gaplens[9] = gaplen2jcurr;
4921 		gaplens[10] = gaplen1jprev;
4922 		gaplens[11] = gaplen2jprev;
4923 //		reporterr( "Allocation end\n" );
4924 	}
4925 
4926 	{
4927 		int ll1 = lgth1;
4928 		int ll2 = lgth2;
4929 
4930 //		reporterr( "Allocating gaplen*i\n" );
4931 		for(i=0; i<ll1+1; i++ )
4932 		{
4933 			gaplen1ibestkamo[i] = (Gaplen **)calloc( ll1+2-i, sizeof( Gaplen * ) );
4934 			for( k=0; k<ll1+1-i; k++ ) gaplen1ibestkamo[i][k] = NULL;
4935 			gaplen1ibestkamo[i][k] = (Gaplen *)1;
4936 
4937 			gaplen2ibestkamo[i] = (Gaplen **)calloc( ll2+2, sizeof( Gaplen * ) );
4938 			for( k=0; k<ll2+1; k++ ) gaplen2ibestkamo[i][k] = NULL;
4939 			gaplen2ibestkamo[i][k] = (Gaplen *)1;
4940 
4941 			gaplen1icurr[i] = (Gaplen **)calloc( ll1+2-i, sizeof( Gaplen * ) );
4942 			for( k=0; k<ll1+1-i; k++ ) gaplen1icurr[i][k] = NULL;
4943 			gaplen1icurr[i][k] = (Gaplen *)1;
4944 
4945 			gaplen2icurr[i] = (Gaplen **)calloc( ll2+2, sizeof( Gaplen * ) );
4946 			for( k=0; k<ll2+1; k++ ) gaplen2icurr[i][k] = NULL;
4947 			gaplen2icurr[i][k] = (Gaplen *)1;
4948 		}
4949 		gaplen1ibestkamo[ll1+1] = NULL;
4950 		gaplen2ibestkamo[ll1+1] = NULL;
4951 		gaplen1icurr[ll1+1] = NULL;
4952 		gaplen2icurr[ll1+1] = NULL;
4953 
4954 //		reporterr( "Allocating gaplen*j\n" );
4955 		for(i=0; i<ll2+1; i++ )
4956 		{
4957 			gaplen1jbestkamo[i] = (Gaplen **)calloc( ll1+2, sizeof( Gaplen * ) );
4958 			for( k=0; k<ll1+1; k++ ) gaplen1jbestkamo[i][k] = NULL;
4959 			gaplen1jbestkamo[i][k] = (Gaplen *)1;
4960 
4961 			gaplen2jbestkamo[i] = (Gaplen **)calloc( ll2+2-i, sizeof( Gaplen * ) );
4962 			for( k=0; k<ll2+1-i; k++ ) gaplen2jbestkamo[i][k] = NULL;
4963 			gaplen2jbestkamo[i][k] = (Gaplen *)1;
4964 
4965 			gaplen1jbest[i] = (Gaplen **)calloc( ll1+2, sizeof( Gaplen * ) );
4966 			for( k=0; k<ll1+1; k++ ) gaplen1jbest[i][k] = NULL;
4967 			gaplen1jbest[i][k] = (Gaplen *)1;
4968 
4969 			gaplen2jbest[i] = (Gaplen **)calloc( ll2+2-i, sizeof( Gaplen * ) );
4970 			for( k=0; k<ll2+1-i; k++ ) gaplen2jbest[i][k] = NULL;
4971 			gaplen2jbest[i][k] = (Gaplen *)1;
4972 
4973 			gaplen1jcurr[i] = (Gaplen **)calloc( ll1+2, sizeof( Gaplen * ) );
4974 			for( k=0; k<ll1+1; k++ ) gaplen1jcurr[i][k] = NULL;
4975 			gaplen1jcurr[i][k] = (Gaplen *)1;
4976 
4977 			gaplen2jcurr[i] = (Gaplen **)calloc( ll2+2-i, sizeof( Gaplen * ) );
4978 			for( k=0; k<ll2+1-i; k++ ) gaplen2jcurr[i][k] = NULL;
4979 			gaplen2jcurr[i][k] = (Gaplen *)1;
4980 
4981 			gaplen1jprev[i] = (Gaplen **)calloc( ll1+2, sizeof( Gaplen * ) );
4982 			for( k=0; k<ll1+1; k++ ) gaplen1jprev[i][k] = NULL;
4983 			gaplen1jprev[i][k] = (Gaplen *)1;
4984 
4985 			gaplen2jprev[i] = (Gaplen **)calloc( ll2+2-i, sizeof( Gaplen * ) );
4986 			for( k=0; k<ll2+1-i; k++ ) gaplen2jprev[i][k] = NULL;
4987 			gaplen2jprev[i][k] = (Gaplen *)1;
4988 
4989 		}
4990 		gaplen1jbestkamo[ll2+1] = NULL;
4991 		gaplen2jbestkamo[ll2+1] = NULL;
4992 		gaplen1jbest[ll2+1] = NULL;
4993 		gaplen2jbest[ll2+1] = NULL;
4994 		gaplen1jcurr[ll2+1] = NULL;
4995 		gaplen2jcurr[ll2+1] = NULL;
4996 		gaplen1jprev[ll2+1] = NULL;
4997 		gaplen2jprev[ll2+1] = NULL;
4998 	}
4999 
5000 
5001 #if USEGAPLENMTX
5002 /* maikai allocate */
5003 
5004 	reporterr( "Allocating gaplenmtx1\n" );
5005 	gaplen1mtx = (Gaplen ****)calloc( (lgth1+2), sizeof( Gaplen ***) );
5006 	for(i=0; i<lgth1+1; i++ ) gaplen1mtx[i] = (Gaplen ***)calloc( lgth2+2, sizeof( Gaplen ** ) );
5007 	for(i=0; i<lgth1+1; i++ )
5008 	{
5009 		for(j=0; j<lgth2+1; j++ )
5010 		{
5011 			gaplen1mtx[i][j] = (Gaplen **)calloc( lgth1+2, sizeof( Gaplen * ) );
5012 			for( k=0; k<lgth1+1; k++ ) gaplen1mtx[i][j][k] = NULL;
5013 			gaplen1mtx[i][j][k] = (Gaplen *)1;
5014 		}
5015 		gaplen1mtx[i][j] = NULL;
5016 	}
5017 	gaplen1mtx[i] = NULL;
5018 
5019 	reporterr( "Allocating gaplenmtx2\n" );
5020 	gaplen2mtx = (Gaplen ****)calloc( (lgth1+2), sizeof( Gaplen ***) );
5021 	for(i=0; i<lgth1+1; i++ ) gaplen2mtx[i] = (Gaplen ***)calloc( lgth2+2, sizeof( Gaplen ** ) );
5022 	for(i=0; i<lgth1+1; i++ )
5023 	{
5024 		for(j=0; j<lgth2+1; j++ )
5025 		{
5026 			gaplen2mtx[i][j] = (Gaplen **)calloc( lgth2+2, sizeof( Gaplen * ) );
5027 			for( k=0; k<lgth2+1; k++ ) gaplen2mtx[i][j][k] = NULL;
5028 			gaplen2mtx[i][j][k] = (Gaplen *)1;
5029 		}
5030 		gaplen2mtx[i][j] = NULL;
5031 	}
5032 	gaplen2mtx[i] = NULL;
5033 
5034 #endif
5035 
5036 #if USEGAPLENHALF
5037 	reporterr( "Allocating gaplenhalf1\n" );
5038 	gaplen1half = (Gaplen ****)calloc( (lgth1+2), sizeof( Gaplen ***) );
5039 	for(i=0; i<lgth1+1; i++ ) gaplen1half[i] = (Gaplen ***)calloc( lgth2+2, sizeof( Gaplen ** ) );
5040 	for(i=0; i<lgth1+1; i++ )
5041 	{
5042 		for(j=0; j<lgth2+1; j++ )
5043 		{
5044 			gaplen1half[i][j] = (Gaplen **)calloc( lgth1+2 - i, sizeof( Gaplen * ) );
5045 			for( k=0; k<lgth1+1-i; k++ ) gaplen1half[i][j][k] = NULL;
5046 			gaplen1half[i][j][k] = (Gaplen *)1;
5047 		}
5048 		gaplen1half[i][j] = NULL;
5049 	}
5050 	gaplen1half[i] = NULL;
5051 
5052 	reporterr( "Allocating gaplenhalf2\n" );
5053 	gaplen2half = (Gaplen ****)calloc( (lgth1+2), sizeof( Gaplen ***) );
5054 	for(i=0; i<lgth1+1; i++ ) gaplen2half[i] = (Gaplen ***)calloc( lgth2+2, sizeof( Gaplen ** ) );
5055 	for(i=0; i<lgth1+1; i++ )
5056 	{
5057 		for(j=0; j<lgth2+1; j++ )
5058 		{
5059 			gaplen2half[i][j] = (Gaplen **)calloc( lgth2+2 - j, sizeof( Gaplen * ) );
5060 			for( k=0; k<lgth2+1-j; k++ ) gaplen2half[i][j][k] = NULL;
5061 			gaplen2half[i][j][k] = (Gaplen *)1;
5062 		}
5063 		gaplen2half[i][j] = NULL;
5064 	}
5065 	gaplen2half[i] = NULL;
5066 #endif
5067 
5068 
5069 /* maikai allocate */
5070 
5071 
5072 	for( i=0; i<icyc; i++ )
5073 	{
5074 		mseq1[i] = mseq[i];
5075 		seq1[i][lgth1] = 0;
5076 	}
5077 	for( j=0; j<jcyc; j++ )
5078 	{
5079 		mseq2[j] = mseq[icyc+j];
5080 		seq2[j][lgth2] = 0;
5081 	}
5082 
5083 
5084 	if( orlgth1 > commonAlloc1 || orlgth2 > commonAlloc2 )
5085 	{
5086 		int ll1, ll2;
5087 
5088 		if( commonAlloc1 && commonAlloc2 )
5089 		{
5090 			FreeIntMtx( commonIP );
5091 		}
5092 
5093 		ll1 = MAX( orlgth1, commonAlloc1 );
5094 		ll2 = MAX( orlgth2, commonAlloc2 );
5095 
5096 #if DEBUG
5097 		fprintf( stderr, "\n\ntrying to allocate %dx%d matrices ... ", ll1+1, ll2+1 );
5098 #endif
5099 
5100 		commonIP = AllocateIntMtx( ll1+10, ll2+10 );
5101 
5102 #if DEBUG
5103 		fprintf( stderr, "succeeded\n\n" );
5104 #endif
5105 
5106 		commonAlloc1 = ll1;
5107 		commonAlloc2 = ll2;
5108 	}
5109 	ijp = commonIP;
5110 
5111 #if 0
5112 	{
5113 		double t = 0.0;
5114 		for( i=0; i<icyc; i++ )
5115 			t += eff1[i];
5116 	fprintf( stderr, "## totaleff = %f\n", t );
5117 	}
5118 #endif
5119 
5120 
5121 #if SLOW
5122 #else
5123 //	cpmx_calc_new( seq1, cpmx1, eff1, lgth1, icyc );
5124 //	cpmx_calc_new( seq2, cpmx2, eff2, lgth2, jcyc );
5125 	for( c=0; c<maxdistclass; c++ )
5126 	{
5127 		cpmx_calc_new( seq1, cpmx1s[c], eff1s[c], lgth1, icyc );
5128 		cpmx_calc_new( seq2, cpmx2s[c], eff2s[c], lgth2, jcyc );
5129 	}
5130 #endif
5131 
5132 
5133 //	reporterr( "Counting gaplen\n" );
5134 	gaplencount( icyc, lgth1, gaplen1, seq1, eff1 );
5135 	gaplencount( jcyc, lgth2, gaplen2, seq2, eff2 );
5136 #if DEBUG
5137 	reporterr( "group1 = \n" );
5138 	showgaplen( gaplen1, lgth1 );
5139 	reporterr( "group2 = \n" );
5140 	showgaplen( gaplen2, lgth2 );
5141 #endif
5142 //	reporterr( "done.\n" );
5143 
5144 
5145 	for( i=0; i<lgth1+1; i++ ) for( j=0; j<lgth2+1; j++ )
5146 	{
5147 #if USEGAPLENMTX
5148 //		duplicategaplen( gaplen1mtx[i][j], gaplen1, lgth1 );
5149 //		duplicategaplen( gaplen2mtx[i][j], gaplen2, lgth2 );
5150 
5151 //		duplicategaplenpartly( gaplen2mtx[i][j], gaplen2, j-0, lgth2 ); // anzen
5152 //		duplicategaplenpartly( gaplen1mtx[i][j], gaplen1, i-0, lgth1 ); // anzen
5153 		duplicategaplenpartly( gaplen1mtx[i][j], gaplen1, i-0, i ); // iranaikamo
5154 		duplicategaplenpartly( gaplen2mtx[i][j], gaplen2, j-0, j ); // iranaikamo
5155 #endif
5156 #if USEGAPLENHALF
5157 
5158 //		duplicategaplenpartly( gaplen1half[i][j], gaplen1+i, 0, lgth1-i ); // KOKO de setsuyaku dekiru to omou.
5159 //		duplicategaplenpartly( gaplen2half[i][j], gaplen2+j, 0, lgth2-j ); //  originally, j-1, lgth2
5160 		duplicategaplenpartly( gaplen1half[i][j], gaplen1+i, 0, 0 ); // test
5161 		duplicategaplenpartly( gaplen2half[i][j], gaplen2+j, 0, 0 ); // test
5162 #endif
5163 	}
5164 
5165 
5166 #if USEGAPLENMTX
5167 	reporterr( "Duplicating gaplen*mtx\n" );
5168 	for( i=0; i<lgth1+1; i++ )
5169 	{
5170 //		addnewgaplen( gaplen1mtx[i][0], gaplen1, gaplen1, lgth1, -1, 0 );
5171 //		addnewgaplen( gaplen2mtx[i][0], gaplen2, gaplen2, lgth2, 0, i );
5172 //		duplicategaplenpartly( gaplen1mtx[i][0], gaplen1, 0, lgth1 );
5173 //		duplicategaplenpartly( gaplen2mtx[i][0], gaplen2, 0, lgth2 );
5174 		copygaplenrestricted( gaplen2mtx[i][0], gaplen2, lgth2, 0, i, 0, 0 );
5175 	}
5176 #endif
5177 #if USEGAPLENHALF
5178 	reporterr( "Duplicating gaplen*mtx\n" );
5179 	for( i=0; i<lgth1+1; i++ )
5180 	{
5181 		copygaplenrestricted( gaplen2half[i][0], gaplen2, lgth2, 0, i, 0, 0 );
5182 	}
5183 #endif
5184 
5185 
5186 
5187 	for( i=0; i<1; i++ )
5188 	{
5189 //		duplicategaplencompactx( gaplen1icurr[i], gaplen1, lgth1, i-0, lgth1 ); //originally, 0, lgth1
5190 //
5191 //		duplicategaplencompactx( gaplen1icurr[i], gaplen1+i, lgth1-i, 0, lgth1-i ); // half
5192 		duplicategaplencompactx( gaplen1icurr[i], gaplen1+i, lgth1-i, 0, 1 ); //  0, 1  hitsuyou
5193 
5194 
5195 //		duplicategaplencompactx( gaplen2icurr[i], gaplen2, lgth2, 0, lgth2 ); // ichiou zenbu
5196 		duplicategaplencompactx( gaplen2icurr[i], gaplen2, lgth2, 0, 0 );
5197 
5198 
5199 		copygaplencompactx( gaplen2icurr[i], gaplen2, lgth2, 0, i, 0, 0 ); // -> zurasu -> error?
5200 
5201 
5202 //		duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1, lgth1, 0, 1 );
5203 //		duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1+i, lgth1-i, 0, 1 ); // half
5204 		duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1+i, lgth1-i, 0, 0 ); // half
5205 //		duplicategaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, lgth2 );
5206 		duplicategaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, 0 );
5207 //		copygaplenrestricted( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, 0 ); // -> zurasu -> error?
5208 //		copygaplenrestricted_zurasu( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, lgth2, 0, lgth2 ); // -> zurasu -> error?
5209 		copygaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, 0 ); // -> zurasu -> error?
5210 	}
5211 
5212 //	reporterr( "Duplicating gaplen*j*curr \n" );
5213 //	int nduplicated = 0;
5214 	for( j=0; j<lgth2+1; j++ )
5215 //	for( j=0; j<1; j++ )
5216 	{
5217 #if USEGAPLENMTX
5218 //		addnewgaplen( gaplen1mtx[0][j], gaplen1, gaplen1, lgth1, 0, j );
5219 //		addnewgaplen( gaplen2mtx[0][j], gaplen2, gaplen2, lgth2, -1, 0 );
5220 //		duplicategaplenpartly( gaplen1mtx[0][j], gaplen1, 0, lgth1 );
5221 //		duplicategaplenpartly( gaplen2mtx[0][j], gaplen2, 0, lgth2 );
5222 		copygaplenrestricted( gaplen1mtx[0][j], gaplen1, lgth1, 0, j, 0, 0 );
5223 #endif
5224 
5225 #if USEGAPLENHALF
5226 		copygaplenrestricted( gaplen1half[0][j], gaplen1, lgth1, 0, j, 0, 0 );
5227 #endif
5228 //		reporterr( "1jcurr?\n" );
5229 //		duplicategaplencompactx( gaplen1jcurr[j], gaplen1, lgth1, 0, lgth1 ); // test
5230 		duplicategaplencompactx( gaplen1jcurr[j], gaplen1, lgth1, 0, 0 ); // dame?
5231 //		reporterr( "done\n" );
5232 //		duplicategaplencompactx( gaplen1jcurr[j], gaplen1, lgth1, 0, 0 ); //test
5233 
5234 //		duplicategaplencompactx( gaplen2jcurr[j], gaplen2, lgth2, j-0, lgth2 ); // full
5235 //		duplicategaplencompactx( gaplen2jcurr[j], gaplen2+j, lgth2-j, 0, lgth2-j ); //half! KOKO????
5236 //reporterr( "starting suspicious duplication\n" );
5237 		duplicategaplencompactx( gaplen2jcurr[j], gaplen2+j, lgth2-j, 0, 0 ); //half!
5238 //reporterr( "starting suspicious copy\n" );
5239 		copygaplencompactx( gaplen1jcurr[j], gaplen1, lgth1, 0, j, 0, 0 ); // TEST
5240 //reporterr( "finished\n" );
5241 
5242 //		reporterr( "Checking gaplen1jcurr[%d]\n", j );
5243 //		checkgaplen( gaplen1jcurr[j], 100 );
5244 //		reporterr( "Checking gaplen2jcurr[%d]\n", j );
5245 //		checkgaplen( gaplen2jcurr[j], 100 );
5246 	}
5247 
5248 //	reporterr( "nduplicated (corrected) = %d\n", nduplicated );
5249 
5250 //	reporterr( "Duplicating gaplen*j*prev \n\n" );
5251 	for( j=0; j<lgth2+1; j++ ) // allocate nominotame, atode uwagaki
5252 	{
5253 //		duplicategaplencompactx( gaplen1jprev[j], gaplen1, lgth1, 0, lgth1 );
5254 		duplicategaplencompactx( gaplen1jprev[j], gaplen1, lgth1, 0, 0 ); // TEST
5255 //		duplicategaplencompactx( gaplen2jprev[j], gaplen2, lgth2, j-0, lgth2 ); // originally, 0,lgth2
5256 //		duplicategaplencompactx( gaplen2jprev[j], gaplen2+j, lgth2-j, 0, lgth2-j ); // half
5257 		duplicategaplencompactx( gaplen2jprev[j], gaplen2+j, lgth2-j, 0, 0 ); // half
5258 
5259 
5260 		copygaplencompactx( gaplen1jprev[j], gaplen1, lgth1, 0, j, 0, 0 ); // wasuretetakamo
5261 
5262 
5263 	}
5264 
5265 
5266 //	reporterr( "Duplicating gaplen*j*best \n\n" );
5267 
5268 	for( j=0; j<lgth2+1; j++ )
5269 //	for( j=0; j<1; j++ )
5270 	{
5271 //		duplicategaplencompactx( gaplen1jbestkamo[j], gaplen1, lgth1, 0, lgth1 ); // KOKO
5272 //		duplicategaplencompactx( gaplen1jbestkamo[j], gaplen1, lgth1, 0, 0 ); // test
5273 //		duplicategaplencompactx( gaplen1jbestkamo[j], gaplen1, lgth1, 0, 1 );
5274 		duplicategaplencompactx( gaplen1jbestkamo[j], gaplen1, lgth1, 0, 0 );
5275 
5276 
5277 //		duplicategaplencompactx( gaplen1jbestkamo[j], gaplen1, lgth1, 0, 1 );
5278 //		duplicategaplencompactx( gaplen2jbestkamo[j], gaplen2, lgth2, j-0, j+1 ); // originally, 0, j+1
5279 		duplicategaplencompactx( gaplen2jbestkamo[j], gaplen2+j, lgth2-j, 0, 1 ); // half!
5280 		copygaplencompactx( gaplen1jbestkamo[j], gaplen1, lgth1, 0, j, 0, 0 ); // TEST
5281 
5282 //		duplicategaplencompactx( gaplen1jbest[j], gaplen1, lgth1, 0, lgth1 ); // KOKO
5283 		duplicategaplencompactx( gaplen1jbest[j], gaplen1, lgth1, 0, 0 ); // test
5284 //		duplicategaplencompactx( gaplen2jbest[j], gaplen2, lgth2,j-0, j+1 ); // originally, 0,j+1
5285 		duplicategaplencompactx( gaplen2jbest[j], gaplen2+j, lgth2-j, 0, 1 ); // half!
5286 		copygaplencompactx( gaplen1jbest[j], gaplen1, lgth1, 0, j, 0, 0 ); // TEST
5287 
5288 
5289 	}
5290 
5291 //	reporterr( "Duplication end\n" );
5292 
5293 
5294 #if 0
5295 	reporterr( "Checking gaplen1icurr\n" );
5296 	checkgaplen( gaplen1icurr[0], 100 );
5297 	reporterr( "Checking gaplen2icurr\n" );
5298 	checkgaplen( gaplen2icurr[0], 100 );
5299 #endif
5300 
5301 
5302 
5303 
5304 //	showgaplen( gaplen1jcurr[50], lgth2 );
5305 
5306 	currentw = w1;
5307 	previousw = w2;
5308 
5309 //	match_calc( n_dynamicmtx, initverticalw, cpmx2, cpmx1, 0, lgth1, doublework, intwork, 1 );
5310 #if SLOW
5311 	match_calc_slow( which, matrices, initverticalw, jcyc, seq2, eff2, icyc, seq1, eff1, 0, lgth1, *doublework, *intwork, 1, 1 );
5312 //	for( i=0; i<lgth1; i++ ) fprintf( stderr, "%d - %f\n", i, initverticalw[i] );
5313 #else
5314 	fillzero( initverticalw, lgth1 );
5315 	for( c=0; c<maxdistclass; c++ )
5316 	{
5317 //		fprintf( stderr, "c=%d matrices[c][W][W] = %f\n", c, matrices[c][amino_n['W']][amino_n['W']] );
5318 //		for( i=0; i<lgth1; i++ ) fprintf( stderr, "seq1[i] = %c, cpmx1s[c][3][%d] = %f\n", seq1[0][i], i, cpmx1s[c][3][i] );
5319 //		for( i=0; i<lgth2; i++ ) fprintf( stderr, "seq2[i] = %c, cpmx2s[c][3][%d] = %f\n", seq2[0][i], i, cpmx2s[c][3][i] );
5320 		match_calc_add( matrices[c], initverticalw, cpmx2s[c], cpmx1s[c], 0, lgth1, doublework[c], intwork[c], 1 );
5321 //		for( i=0; i<lgth1; i++ ) fprintf( stderr, "c=%d, %d - %f\n", c, i, initverticalw[i] );
5322 
5323 		if( nmask[c] ) match_calc_del( which, matrices, initverticalw, jcyc, seq2, eff2, icyc, seq1, eff1, 0, lgth1, c, nmask[c], masklist2[c], masklist1[c] );
5324 	}
5325 #endif
5326 //	reporterr( "initverticalw = \n" );
5327 //	for( i=0; i<lgth1; i++ ) fprintf( stderr, "%d - %f\n", i, initverticalw[i] );
5328 
5329 
5330 	if( localhom )
5331 		imp_match_out_vead_tate( initverticalw, 0, lgth1 ); // 060306
5332 
5333 //	match_calc( n_dynamicmtx, currentw, cpmx1, cpmx2, 0, lgth2, doublework, intwork, 1 );
5334 #if SLOW
5335 	match_calc_slow( which, matrices, currentw, icyc, seq1, eff1, jcyc, seq2, eff2, 0, lgth2, *doublework, *intwork, 1, 0 );
5336 //	for( i=0; i<lgth2; i++ ) fprintf( stderr, "%d - %f\n", i, currentw[i] );
5337 //	exit( 1 );
5338 #else
5339 	fillzero( currentw, lgth2 );
5340 	for( c=0; c<maxdistclass; c++ )
5341 	{
5342 		match_calc_add( matrices[c], currentw, cpmx1s[c], cpmx2s[c], 0, lgth2, doublework[c], intwork[c], 1 );
5343 		if( nmask[c] ) match_calc_del( which, matrices, currentw, icyc, seq1, eff1, jcyc, seq2, eff2, 0, lgth2, c, nmask[c], masklist1[c], masklist2[c] );
5344 	}
5345 #endif
5346 //	reporterr( "currentw = \n" );
5347 //	for( i=0; i<lgth2; i++ ) fprintf( stderr, "%d - %f\n", i, currentw[i] );
5348 
5349 
5350 
5351 
5352 
5353 //	exit( 1 );
5354 
5355 
5356 	if( localhom )
5357 		imp_match_out_vead( currentw, 0, lgth2 ); // 060306
5358 #if 0 // -> tbfast.c
5359 	if( localhom )
5360 		imp_match_calc( n_dynamicmtx, currentw, icyc, jcyc, lgth1, lgth2, seq1, seq2, eff1, eff2, localhom, 1, 0 );
5361 
5362 #endif
5363 
5364 	for( j=1; j<lgth2+1; j++ )
5365 	{
5366 		pfac = calcpfac_gap_noidatend( gaplen1, gaplen2, j, 0, j, seq1[0], seq2[0], 0 );
5367 //		reporterr( "computing initial end gap penalty for %c-%c, i=0, j=%d, pfac=%f\n", seq1[0][0], seq2[0][j], j, pfac );
5368 //		reporterr( "%c-%c, i=0, j=%d, currentw[j]=%f -> ", seq1[0][0], seq2[0][j], j, currentw[j] );
5369 		currentw[j] += fpenalty * pfac; // tekitou
5370 //		reporterr( " %f\n", currentw[j] );
5371 	}
5372 	for( i=1; i<lgth1+1; i++ )
5373 	{
5374 		pfac = calcpfac_gap_noidatend( gaplen2, gaplen1, i, 0, i, seq2[0], seq1[0], 0 );
5375 //		reporterr( "computing initial end gap penalty for %c-%c, i=%d, j=0, pfac=%f\n", seq1[0][i], seq2[0][0], i, pfac );
5376 		initverticalw[i] += fpenalty * pfac; // tekitou
5377 	}
5378 
5379 
5380 
5381 	for( j=1; j<lgth2+1; ++j )
5382 	{
5383 
5384 
5385 #if ALGZGAP
5386 		m[j] = currentw[j-1] + ogcp1[1] * gapfreq2[j-1]; mp[j] = 0;;
5387 #else
5388 		pfac = calcpfac_gapex_noidatend( gaplen2, gaplen1, j, 1, j, seq2[0], seq1[0], 1 );
5389 #if DEBUG
5390 		reporterr( "%c-%c, INITIAL jgap extension check, pfac = %f\n\n", seq1[0][j], '=', pfac );
5391 #endif
5392 		m[j] = currentw[j-1] + fpenalty * pfac;
5393 		mp[j] = 0;
5394 #endif
5395 	}
5396 	if( lgth2 == 0 )
5397 		lastverticalw[0] = 0.0; // Falign kara yobaretatoki kounarukanousei ari
5398 	else
5399 		lastverticalw[0] = currentw[lgth2-1];
5400 
5401 	if( tailgp ) lasti = lgth1+1; else lasti = lgth1;
5402 	lastj = lgth2+1;
5403 
5404 
5405 	for( i=1; i<lasti; i++ )
5406 	{
5407 //		reporterr( "i = %d\n", i );
5408 
5409 //		reporterr( "err1? i=%d/%d\n", i, lgth1 );
5410 #ifdef enablemultithread
5411 //		fprintf( stderr, "chudan = %d, %d\n", *chudanpt, chudanref );
5412 		if( chudanpt && *chudanpt != chudanref )
5413 		{
5414 			cleargaplens( gaplens );
5415 			if( masklist1 ) FreeIntMtx( masklist1 ); masklist1 = NULL;
5416 			if( masklist2 ) FreeIntMtx( masklist2 ); masklist2 = NULL;
5417 			if( nmask ) free( nmask ); nmask = NULL;
5418 //			fprintf( stderr, "\n\n## CHUUDAN!!! S\n" );
5419 			*chudanres = 1;
5420 			return( -1.0 );
5421 		}
5422 #endif
5423 
5424 
5425 		wtmp = previousw;
5426 		previousw = currentw;
5427 		currentw = wtmp;
5428 
5429 		previousw[0] = initverticalw[i-1];
5430 
5431 #if 1
5432 		gaplentmp = gaplen1jprev;
5433 		gaplen1jprev = gaplen1jcurr;
5434 		gaplen1jcurr = gaplentmp;
5435 
5436 		gaplentmp = gaplen2jprev;
5437 		gaplen2jprev = gaplen2jcurr;
5438 		gaplen2jcurr = gaplentmp;
5439 
5440 #if DEBUG
5441 		reporterr( "Entering a small j loop, i=%d\n", i );
5442 		for( j=1; j<lgth2+1; j++ )
5443 		{
5444 			reporterr( "before j loop, i=%d, gaplen2jcurr[%d] = \n", i, j );
5445 			showgaplen( gaplen2jcurr[j], 100 );
5446 			reporterr( "\n" );
5447 			reporterr( "before j loop, i=%d, gaplen2prev[%d] = \n", i, j );
5448 			showgaplen( gaplen2jprev[j], 100 );
5449 			reporterr( "\n" );
5450 		}
5451 #endif
5452 #else
5453 
5454 		reporterr( "Entering a small j loop, i=%\n", i );
5455 		for( j=1; j<lgth2+1; j++ )
5456 		{
5457 //			addnewgaplen( gaplen1jprev[j], gaplen1jcurr[j], gaplen1, lgth1, -1, 0 );
5458 //			addnewgaplen( gaplen2jprev[j], gaplen2jcurr[j], gaplen2, lgth2, -1, 0 );
5459 			reporterr( "err1? j=%d/%d\n", j, lgth2 );
5460 			copygaplencompactx( gaplen1jprev[j-1], gaplen1jcurr[j-1], lgth1, -1, 0, i-1, i-1 ); // TEST
5461 			reporterr( "err1? j=%d/%d\n", j, lgth2 );
5462 			copygaplencompactx( gaplen2jprev[j-1], gaplen2jcurr[j-1], lgth2, -1, 0, j-1, j-1 ); // TETS
5463 #if DEBUG
5464 			reporterr( "before j loop, i=%d, gaplen2jcurr[%d] = \n", i, j );
5465 			showgaplen( gaplen2jcurr[j], 100 );
5466 			reporterr( "\n" );
5467 			reporterr( "before j loop, i=%d, gaplen2prev[%d] = \n", i, j );
5468 			showgaplen( gaplen2jprev[j], 100 );
5469 			reporterr( "\n" );
5470 #endif
5471 		}
5472 #endif
5473 
5474 //		reporterr( "err2? i=%d/%d\n", i, lgth1 );
5475 
5476 //		duplicategaplencompactx( gaplen1icurr[i], gaplen1, lgth1, i, i+1 ); //originally 0, i+1
5477 //		reporterr( "gaplen+0=\n");
5478 //		showgaplen( gaplen1, 10 );
5479 //		reporterr( "i=%d, lgth1=%d, lgth1-i=%d, gaplen+i-1=\n", i, lgth1, lgth1-i );
5480 //		showgaplen( gaplen1+i-1, 100 );
5481 		duplicategaplencompactx( gaplen1icurr[i], gaplen1+i, lgth1-i, 0, 1 ); // half!!
5482 //		duplicategaplencompactx( gaplen2icurr[i], gaplen2, lgth2, 0, lgth2 ); // KOKO
5483 		duplicategaplencompactx( gaplen2icurr[i], gaplen2, lgth2, 0, 0 ); // test
5484 		copygaplencompactx( gaplen2icurr[i], gaplen2, lgth2, 0, i, 0, 0 ); // IRU? TEST
5485 
5486 
5487 
5488 //		duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1, lgth1, i, i+1 ); //originally 0, i+1
5489 		duplicategaplencompactx( gaplen1ibestkamo[i], gaplen1+i, lgth1-i, 0, 1 ); //half
5490 //		duplicategaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, lgth2 ); // ORIGINALLY, 0, lgth2
5491 		duplicategaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, 0 ); // ORIGINALLY, 0, lgth2
5492 //		copygaplenrestricted( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, lgth2, 0, 0 ); // IRU? // TEST
5493 		copygaplencompactx( gaplen2ibestkamo[i], gaplen2, lgth2, 0, i, 0, 0 ); // IRU? // TEST
5494 
5495 		extendgaplencompactx( gaplen1jprev[0], gaplen1, i ); // ???
5496 
5497 
5498 //		addnewgaplen( gaplen1jprev[0], gaplen1icurr[i-1], gaplen1, lgth1, -1, 0 );
5499 //		addnewgaplen( gaplen2jprev[0], gaplen2icurr[i-1], gaplen2, lgth2, -1, 0 );
5500 //		copygaplenrestricted( gaplen1jprev[0], gaplen1icurr[i-1], lgth1, -1, 0, i, i ); // i-1, i da to omou.
5501 		copygaplencompactx( gaplen1jprev[0], gaplen1icurr[i-1], lgth1-i, -1, 0, i, 1 ); // half? lgth1-i?
5502 //		copygaplenrestricted( gaplen2jprev[0], gaplen2icurr[i-1], lgth2, -1, 0, 0, 0 );
5503 		copygaplencompactx( gaplen2jprev[0], gaplen2icurr[i-1], lgth2-j, -1, 0, 0, 0 ); // half?? lgth2-j?
5504 
5505 
5506 //		match_calc( n_dynamicmtx, currentw, cpmx1, cpmx2, i, lgth2, doublework, intwork, 0 );
5507 #if SLOW
5508 		match_calc_slow( which, matrices, currentw, icyc, seq1, eff1, jcyc, seq2, eff2, i, lgth2, *doublework, *intwork, 0, 0 );
5509 #else
5510 		fillzero( currentw, lgth2 );
5511 		for( c=0; c<maxdistclass; c++ )
5512 		{
5513 			match_calc_add( matrices[c], currentw, cpmx1s[c], cpmx2s[c], i, lgth2, doublework[c], intwork[c], 0 );
5514 			if( nmask[c] ) match_calc_del( which, matrices, currentw, icyc, seq1, eff1, jcyc, seq2, eff2, i, lgth2, c, nmask[c], masklist1[c], masklist2[c] );
5515 		}
5516 #endif
5517 
5518 
5519 #if XXXXXXX
5520 fprintf( stderr, "\n" );
5521 fprintf( stderr, "i=%d\n", i );
5522 fprintf( stderr, "currentw = \n" );
5523 for( j=0; j<lgth2; j++ )
5524 {
5525 	fprintf( stderr, "%5.2f ", currentw[j] );
5526 }
5527 fprintf( stderr, "\n" );
5528 #endif
5529 		if( localhom )
5530 		{
5531 //			fprintf( stderr, "Calling imp_match_calc (o) lgth = %d, i = %d\n", lgth1, i );
5532 #if  0
5533 			imp_match_out_vead( currentw, i, lgth2 );
5534 #else
5535 			imp_match_out_vead( currentw, i, lgth2 );
5536 #endif
5537 		}
5538 #if XXXXXXX
5539 fprintf( stderr, "\n" );
5540 fprintf( stderr, "i=%d\n", i );
5541 fprintf( stderr, "currentw = \n" );
5542 for( j=0; j<lgth2; j++ )
5543 {
5544 	fprintf( stderr, "%5.2f ", currentw[j] );
5545 }
5546 fprintf( stderr, "\n" );
5547 #endif
5548 		currentw[0] = initverticalw[i];
5549 
5550 #if 0
5551 		fprintf( stderr, "%c ", seq1[0][i] );
5552 		for( j=0; j<lgth2+1; j++ )
5553 		{
5554 			fprintf( stderr, "%5.0f ", currentw[j] );
5555 		}
5556 		fprintf( stderr, "\n"  );
5557 #endif
5558 
5559 //		mi = previousw[0] + ogcp2[1]; mpi = 0;
5560 
5561 
5562 
5563 
5564 
5565 #if ALGZGAP
5566 		mi = previousw[0] + ogcp2[1] * gapfreq1[i-1]; mpi=0;
5567 #else
5568 		pfac = calcpfac_gapex_noidatend( gaplen1, gaplen2, i, 1, i, seq1[0], seq2[0], 1 );
5569 #if DEBUG
5570 		reporterr( "%c-%c, INITIAL igap extension check, pfac = %f\n\n", '=', seq2[0][j], pfac );
5571 #endif
5572 		mi = previousw[0] + fpenalty * pfac;
5573 		mpi=0;
5574 #endif
5575 		ijppt = ijp[i] + 1;
5576 		mjpt = m + 1;
5577 		prept = previousw;
5578 		curpt = currentw + 1;
5579 		mpjpt = mp + 1;
5580 
5581 
5582 		if( trywarp )
5583 		{
5584 			prevwmrecordspt = prevwmrecords;
5585 			wmrecordspt = wmrecords+1;
5586 			wmrecords1pt = wmrecords;
5587 			warpipt = warpi + 1;
5588 			warpjpt = warpj + 1;
5589 		}
5590 
5591 
5592 
5593 
5594 //		reporterr( "\n\ni=%d, %c\n", i, seq1[0][i] );
5595 		for( j=1; j<lastj; j++ )
5596 		{
5597 
5598 #if DEBUG
5599 			reporterr( "*****   %c%d-%c%d   ******* \n", seq1[0][i], i, seq2[0][j], j );
5600 			reporterr( "mpi=%d (%c), *mpjpt=%d (%c)\n", mpi, seq2[0][mpi], *mpjpt, seq1[0][*mpjpt] );
5601 #endif
5602 
5603 
5604 // Hitsuyou na bun dake tsuika
5605 #if USEGAPLENMTX
5606 			extendgaplenpartly( gaplen1mtx[i-1][mpi], gaplen1, i, i );
5607 			extendgaplenpartly( gaplen2mtx[i-1][mpi], gaplen2, j, j );
5608 			extendgaplenpartly( gaplen1mtx[*mpjpt][j-1], gaplen1, i, i );
5609 			extendgaplenpartly( gaplen2mtx[*mpjpt][j-1], gaplen2, j, j );
5610 			extendgaplenpartly( gaplen1mtx[i-1][j-1], gaplen1, i, i );
5611 			extendgaplenpartly( gaplen2mtx[i-1][j-1], gaplen2, j, j );
5612 #endif
5613 #if USEGAPLENHALF
5614 			extendgaplenpartly( gaplen1half[i-1][mpi], gaplen1+i-1, 1, 1 );
5615 			extendgaplenpartly( gaplen2half[i-1][mpi], gaplen2+mpi, j-mpi, j-mpi );
5616 			extendgaplenpartly( gaplen1half[*mpjpt][j-1], gaplen1+*mpjpt, i-*mpjpt, i-*mpjpt );
5617 			extendgaplenpartly( gaplen2half[*mpjpt][j-1], gaplen2+j-1, 1, 1 );
5618 			extendgaplenpartly( gaplen1half[i-1][j-1], gaplen1+i-1, 1, 1 );
5619 			extendgaplenpartly( gaplen2half[i-1][j-1], gaplen2+j-1, 1, 1 );
5620 
5621 #endif
5622 
5623 //			reporterr( "extending gaplen1icurr\n" );
5624 			extendgaplencompactx( gaplen1icurr[i-1], gaplen1+i-1, 1 ); // iruhazu
5625 //			reporterr( "extending gaplen2icurr\n" );
5626 			extendgaplencompactx( gaplen2icurr[i-1], gaplen2, j ); // iruhazu
5627 //			reporterr( "extending gaplen1jprev[j-1], j-1=%d\n", j-1 );
5628 			extendgaplencompactx( gaplen1jprev[j-1], gaplen1, i );
5629 //			reporterr( "extending gaplen1jcurr, j-1=%d\n", j-1 );
5630 			extendgaplencompactx( gaplen1jcurr[j-1], gaplen1, i );
5631 //			reporterr( "extending gaplen2jprev\n" );
5632 			extendgaplencompactx( gaplen2jprev[j-1], gaplen2+j-1, 1 );
5633 //			reporterr( "extending gaplen2jcurr\n" );
5634 			extendgaplencompactx( gaplen2jcurr[j-1], gaplen2+j-1, 1 );
5635 //			reporterr( "extending gaplen1jbest[j-1]\n" );
5636 			extendgaplencompactx( gaplen1jbest[j-1], gaplen1, i );
5637 //			reporterr( "extending gaplen1jbestkamo[j-1]\n" );
5638 			extendgaplencompactx( gaplen1jbestkamo[j-1], gaplen1, i );
5639 //			reporterr( "extending gaplen1jprev[mpi], j-1=%d\n", j-1 );
5640 			extendgaplencompactx( gaplen1jprev[mpi], gaplen1, i ); // full
5641 //			reporterr( "extending gaplen2jprev[mpi]\n" );
5642 			extendgaplencompactx( gaplen2jprev[mpi], gaplen2+mpi, j-mpi ); // half
5643 //			reporterr( "extending gaplen1ibestkamo[i-1]\n" );
5644 			extendgaplencompactx( gaplen1ibestkamo[i-1], gaplen1+i-1, 1 );
5645 //			reporterr( "extending gaplen2ibestkamo[i-1]\n" );
5646 			extendgaplencompactx( gaplen2ibestkamo[i-1], gaplen2, j );
5647 
5648 
5649 #if DEBUG
5650 			reporterr( "at the head of j loop, i,j=%d,%d, gaplen2jcurr[j]=\n", i, j );
5651 			showgaplen( gaplen2jcurr[j], 100 );
5652 
5653 			reporterr( "at the head of j loop, i,j=%d,%d, gaplen2jcurr[j-1]=\n", i, j );
5654 			showgaplen( gaplen2jcurr[j-1], 100 );
5655 
5656 
5657 			reporterr( "at the head of j loop, i,j=%d,%d, gaplen2jprev[j]=\n", i, j );
5658 			showgaplen( gaplen2jprev[j], 100 );
5659 
5660 			reporterr( "at the head of j loop, i,j=%d,%d, gaplen2jprev[j-1]=\n", i, j );
5661 			showgaplen( gaplen2jprev[j-1], 100 );
5662 #endif
5663 
5664 
5665 #ifdef xxxenablemultithread
5666 //			fprintf( stderr, "chudan = %d, %d\n", *chudanpt, chudanref );
5667 			if( chudanpt && *chudanpt != chudanref )
5668 			{
5669 				cleargaplens( gaplens );
5670 				if( masklist1 ) FreeIntMtx( masklist1 ); masklist1 = NULL;
5671 				if( masklist2 ) FreeIntMtx( masklist2 ); masklist2 = NULL;
5672 				if( nmask ) free( nmask ); nmask = NULL;
5673 //				fprintf( stderr, "\n\n## CHUUDAN!!! S\n" );
5674 				*chudanres = 1;
5675 				return( -1.0 );
5676 			}
5677 #endif
5678 #if USEGAPLENHALF
5679 // i==248, j==80 wo check
5680 #if DEBUG80
5681 			if( j==80 )
5682 			{
5683 				reporterr( "When i==%d, j==%d,\n", i, j );
5684 				reporterr( "gaplen2jprev[j-1=%d]=\n", j-1 );
5685 				showgaplen( gaplen2jprev[j-1], lgth1 );
5686 				reporterr( "gaplen2half[i-1=%d][j-1=%d]=\n", i-1, j-1 );
5687 				showgaplen( gaplen2half[i-1][j-1], lgth1 );
5688 			}
5689 			if( j==79 )
5690 			{
5691 				reporterr( "When i==%d, j==%d,\n", i, j );
5692 				reporterr( "gaplen2jprev[j-1=%d]=\n", j-1 );
5693 				showgaplen( gaplen2jprev[j-1], lgth1 );
5694 				reporterr( "gaplen2half[i-1=%d][j-1=%d]=\n", i-1, j-1 );
5695 				showgaplen( gaplen2half[i-1][j-1], lgth1 );
5696 			}
5697 #endif
5698 #endif
5699 
5700 
5701 //			pfac = calcpfac( gaplen1jprev[j-1], gaplen2jprev[j-1], i, j, seq1[0], seq2[0] );
5702 //reporterr( "#### COMPACT, i,j=%d,%d\n", i, j );
5703 			pfac = calcpfacnoidatend( gaplen1jprev[j-1], gaplen2jprev[j-1], i, 1, seq1[0], seq2[0]+j, one ); // 1j->full, 2j->half
5704 #if USEGAPLENMTX
5705 //reporterr( "#### FULL, i,j=%d,%d\n", i, j );
5706 			pfactmp = calcpfac( gaplen1mtx[i-1][j-1], gaplen2mtx[i-1][j-1], i, j, seq1[0], seq2[0], one );
5707 #endif
5708 #if USEGAPLENHALF
5709 //reporterr( "#### HALF, i,j=%d/%d,%d/%d\n", i, lgth1, j, lgth2 );
5710 //			showgaplen( gaplen2half[i-1][j-1], lgth2-j );
5711 			pfactmp = calcpfac( gaplen1half[i-1][j-1], gaplen2half[i-1][j-1], 1, 1, seq1[0]+i, seq2[0]+j, zero );
5712 #endif
5713 #if USEGAPLENMTX + USEGAPLENHALF
5714 			if( pfac != pfactmp )
5715 			{
5716 				reporterr( "(straight) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j );
5717 				PFACERROR = 1;
5718 				exit( 1 );
5719 			}
5720 #endif
5721 //if( i==50 && j==135 ) exit( 1 );
5722 
5723 
5724 //			reporterr( "i,j=%d,%d, *prept = %f\n", i, j, *prept );
5725 
5726 #if ALGZSTRAIGHT
5727 			wm = *prept;  // Machigai!!
5728 #else
5729 			wm = *prept + fpenalty * pfac;
5730 #endif
5731 			*ijppt = 0;
5732 
5733 
5734 #if DEBUG
5735 			if( i == j )
5736 			{
5737 				fprintf( stderr, "\n i=%d, j=%d %c, %c ", i, j, seq1[0][i], seq2[0][j] );
5738 				fprintf( stderr, "%5.0f, pfac for straight =%f\n", wm, pfac );
5739 			}
5740 #endif
5741 			newgaplen = j-mpi-1;
5742 
5743 
5744 //if( i == 53 && j == 93 ) exit( 1 );
5745 
5746 
5747 
5748 
5749 //			pfac = calcpfac_gap_incomplete( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], newgaplen, i, j, seq1[0], seq2[0], 0 ); // i-1
5750 			pfac = calcpfac_gap_noidatend( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], newgaplen, 1, j, seq1[0]+i-1, seq2[0], 0 ); // i-1
5751 #if USEGAPLENMTX
5752 			pfactmp = calcpfac_gap_incomplete( gaplen1mtx[i-1][mpi], gaplen2mtx[i-1][mpi], newgaplen, i, j, seq1[0], seq2[0], 1 );
5753 #endif
5754 #if USEGAPLENHALF
5755 			pfactmp = calcpfac_gap_incomplete( gaplen1half[i-1][mpi], gaplen2half[i-1][mpi], newgaplen, 1, j-mpi, seq1[0]+i-1, seq2[0]+mpi, 1 );
5756 #endif
5757 #if USEGAPLENMTX || USEGAPLENHALF
5758 			if( pfac != pfactmp )
5759 			{
5760 				reporterr( "(igap) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j );
5761 				PFACERROR = 1;
5762 			}
5763 #endif
5764 
5765 
5766 #if DEBUG
5767 			reporterr( "%c-%c pfac for igap end incomplete = %f\n", seq1[0][i], seq2[0][j], pfac );
5768 			reporterr( "mi when igap end checking = %f\n", mi );
5769 			reporterr( "wm = %f, mi+fpenalty*pfac=%f\n", wm, mi+fpenalty*pfac );
5770 #endif
5771 
5772 
5773 #if ALGZGAP
5774 			if( (g=mi+*fgcp2pt*gf1va) > wm )
5775 #else
5776 			if( (g=mi+fpenalty*pfac) > wm )
5777 #endif
5778 			{
5779 				wm = g;
5780 				*ijppt = -( j - mpi );
5781 #if DEBUG80
5782 				reporterr( "Selected as igap end! wm = %f, mi = %f\n", wm, mi );
5783 				fprintf( stderr, "Jump from %d-%d (%c-%c) to %d (%c-%c)!\n", i, j, seq1[0][i], seq2[0][j], mpi, seq1[0][i-1], seq2[0][mpi] );
5784 #endif
5785 			}
5786 
5787 
5788 #if 0
5789 			fprintf( stderr, "%5.0f->", wm );
5790 #endif
5791 //			if( (g=*mjpt+ fgcp1va* *gf2pt) > wm )
5792 
5793 #if 0
5794 //			reporterr( "Checking %c, (current pos = %c)\n", seq2[0][j+1], seq2[0][j] );
5795 			sfac = 1.0;
5796 			for( k=0; gaplen2[j+1]&&(gl=gaplen2[j+1][k].len)!=-1; k++ ) // ososugi!  hash ni atode henkou
5797 			{
5798 //				reporterr( ".len = %d, .relend = %d\n", gaplen2[j+1][k].len, gaplen2[j+1][k].relend );
5799 				if( gl - 1 == gaplen2[j+1][k].relend )
5800 				{
5801 					sfac -= gaplen2[j+1][k].freq;
5802 //					reporterr( "Hit! sfac = %f\n", sfac );
5803 				}
5804 			}
5805 			sfac2 = 1.0;
5806 			for( k=0; gaplen1[i+1]&&(gl=gaplen1[i+1][k].len)!=-1; k++ ) // ososugi!  hash ni atode henkou
5807 				if( gaplen1[i+1][k].relend != -1 ) sfac2 -= gaplen1[i+1][k].freq;
5808 			sfac *= sfac2;
5809 #else
5810 //			sfac = 0.0;
5811 #endif
5812 
5813 
5814 
5815 #if ALGZGAP
5816 			if( (g=*prept+*ogcp2pt*gf1vapre) >= mi )
5817 #else
5818 //			if( (g=*prept + fpenalty * sfac ) >= mi )
5819 			if( (g=*prept ) >= mi )
5820 #endif
5821 			{
5822 //				mpibk = mpi;
5823 //				mi = g - fpenalty * sfac;
5824 				mi = g;
5825 				mpi = j-1;
5826 #if DEBUG80
5827 				reporterr( "Selected as igap start! %c%d-%c%d, mi=%f, g=%f\n", seq1[0][i-1], i-1, seq2[0][mpi], mpi, mi, g );
5828 #endif
5829 
5830 #if FREEFREQUENTLY
5831 //				freegaplenpartly( gaplen1ibestkamo[i-1], 0, i-1 );
5832 				freegaplenpartly( gaplen2ibestkamo[i-1], j-3, j-2 );
5833 #endif
5834 //				freegaplenpartly( gaplen1jprev[mpibk], 0, lgth2 ); // full
5835 //				freegaplenpartly( gaplen2jprev[mpibk], 0, lgth2-mpibk ); // half
5836 //				if( gaplen1jprev[mpibk] ) FreeGaplenMtx( gaplen1jprev[mpibk], 0 );
5837 //				gaplen1jprev[mpibk] = NULL;
5838 //				if( gaplen2jprev[mpibk] ) FreeGaplenMtx( gaplen2jprev[mpibk], 0 );
5839 //				gaplen2jprev[mpibk] = NULL;
5840 
5841 
5842 //				addnewgaplen( gaplen1ibestkamo[i-1], gaplen1jprev[j-1], gaplen1, lgth1, -1, 0 );
5843 //				addnewgaplen( gaplen2ibestkamo[i-1], gaplen2jprev[j-1], gaplen2, lgth2, -1, 0 );
5844 //				copygaplenrestricted( gaplen1ibestkamo[i-1], gaplen1jprev[j-1], lgth1, -1, 0, i, i ); // i-1, i
5845 				copygaplencompactx( gaplen1ibestkamo[i-1], gaplen1jprev[j-1], lgth1, -1, 0, 1, i ); // half
5846 //				copygaplenrestricted( gaplen2ibestkamo[i-1], gaplen2jprev[j-1], lgth2, -1, 0, j, j ); // mpi, j
5847 				copygaplencompactx( gaplen2ibestkamo[i-1], gaplen2jprev[j-1], lgth2, -1, 0, j, 1 ); //half
5848 
5849 
5850 			}
5851 
5852 
5853 
5854 
5855 
5856 
5857 //			reporterr( "g=%f, *prept=%f, mi=%f\n", g, *prept, mi );
5858 
5859 
5860 #if USE_PENALTY_EX
5861 			mi += fpenalty_ex;
5862 #endif
5863 
5864 #if ALGZGAP
5865 			pfac = 0.0; // CHUUI!
5866 #else
5867 
5868 //			pfac = calcpfac_gapex( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], i, j, j-mpi, seq1[0], seq2[0], 1 ); // i-1
5869 			pfac = calcpfac_gapex_noidatend( gaplen1ibestkamo[i-1], gaplen2ibestkamo[i-1], 1, j, j-mpi, seq1[0]+i, seq2[0], 1 ); // 1ibest->half, 2ibest->full
5870 #if USEGAPLENMTX
5871 			pfactmp = calcpfac_gapex( gaplen1mtx[i-1][mpi], gaplen2mtx[i-1][mpi], i, j, j-mpi, seq1[0], seq2[0], 1 );
5872 #endif
5873 #if USEGAPLENHALF
5874 			pfactmp = calcpfac_gapex( gaplen1half[i-1][mpi], gaplen2half[i-1][mpi], 1, j-mpi, j-mpi, seq1[0]+i, seq2[0]+mpi, 1 );
5875 #endif
5876 #if USEGAPLENMTX || USEGAPLENHALF
5877 			if( pfac != pfactmp )
5878 			{
5879 				reporterr( "(igapex) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j );
5880 				PFACERROR = 1;
5881 			}
5882 #endif
5883 
5884 
5885 
5886 
5887 
5888 
5889 
5890 #if DEBUG
5891 			reporterr( "%c-%c, igap extension check, pfac = %f\n\n", '=', seq2[0][j], pfac );
5892 #endif
5893 #endif
5894 //			reporterr( "mi = %f -> ", mi );
5895 			mi += fpenalty * pfac;
5896 //			reporterr( "mi = %f\n", mi );
5897 
5898 
5899 //			reporterr( "using %d-%d, %d, %d\n", *mpjpt, j-1, i, j );
5900 			newgaplen = i-*mpjpt-1;
5901 //			pfac = calcpfac_gap_incomplete( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], newgaplen, j, i, seq2[0], seq1[0], 0 ); // j-1 deha???
5902 
5903 
5904 			pfac = calcpfac_gap_noidatend( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], newgaplen, 1, i, seq2[0]+j-1, seq1[0], 1 ); // 2jbestkamo->half, 1jbestkamo->full
5905 #if USEGAPLENMTX
5906 			pfactmp = calcpfac_gap_incomplete( gaplen2mtx[*mpjpt][j-1], gaplen1mtx[*mpjpt][j-1], newgaplen, j, i, seq2[0], seq1[0], 1 );
5907 #endif
5908 #if USEGAPLENHALF
5909 			pfactmp = calcpfac_gap_incomplete( gaplen2half[*mpjpt][j-1], gaplen1half[*mpjpt][j-1], newgaplen, 1, i-*mpjpt, seq2[0]+j-1, seq1[0]+*mpjpt, 1 );
5910 #endif
5911 #if USEGAPLENMTX || USEGAPLENHALF
5912 			if( pfac != pfactmp )
5913 			{
5914 				reporterr( "(jgap) pfac=%f, but pfactmp=%f (i,j=%d,%d)\n", pfac, pfactmp, i, j );
5915 //				exit( 1 );
5916 				PFACERROR = 1;
5917 			}
5918 #endif
5919 
5920 #if ALGZGAP
5921 			if( (g=*mjpt+ fgcp1va* *gf2pt) > wm )
5922 #else
5923 			if( (g=*mjpt + fpenalty*pfac) > wm )
5924 #endif
5925 			{
5926 				wm = g;
5927 				*ijppt = +( i - *mpjpt );
5928 
5929 
5930 #if FREEFREQUENTLY
5931 				freegaplenpartly( gaplen1jbest[j-1], i-3, i-2 );
5932 //				freegaplenpartly( gaplen2jbest[j-1], j-3, j-2 );
5933 #endif
5934 
5935 
5936 #if DEBUG
5937 				reporterr( "Selected as jgap end!, pfac = %f\n", pfac );
5938 				fprintf( stderr, "Jump from %d (%c) to %d (%c)!\n", j, seq1[0][j], *mpjpt, seq1[0][*mpjpt] );
5939 #endif
5940 //				addnewgaplen( gaplen1jbest[j-1], gaplen1jbestkamo[j-1], gaplen1, lgth1, -1, 0 );
5941 //				addnewgaplen( gaplen2jbest[j-1], gaplen2jbestkamo[j-1], gaplen2, lgth2, -1, 0 );
5942 				copygaplencompactx( gaplen1jbest[j-1], gaplen1jbestkamo[j-1], lgth1, -1, 0, i, i );// *mpjpt, i
5943 //				copygaplenrestricted( gaplen2jbest[j-1], gaplen2jbestkamo[j-1], lgth2, -1, 0, j, j ); // j-1, j
5944 				copygaplencompactx( gaplen2jbest[j-1], gaplen2jbestkamo[j-1], lgth2, -1, 0, 1, 1 ); // half!
5945 
5946 
5947 
5948 
5949 			}
5950 
5951 
5952 //			extendgaplenpartly( gaplen1jbest[j-1], gaplen1, i, i ); // tmptmptmp
5953 //			extendgaplenpartly( gaplen2jbest[j-1], gaplen2, 0, 0 ); // tmptmptmp
5954 
5955 #if 0
5956 			sfac = 1.0;
5957 			for( l=0; gaplen1[i+1]&&(gl=gaplen1[i+1][l].len)!=-1; l++ ) // ososugi!  hash ni atode henkou
5958 				if( gl - 1 == gaplen1[i+1][l].relend ) sfac -= gaplen1[i+1][l].freq;
5959 			sfac2 = 1.0;
5960 			for( k=0; gaplen2[j+1]&&(gl=gaplen2[j+1][k].len)!=-1; k++ ) // ososugi!  hash ni atode henkou
5961 				if( gaplen2[j+1][k].relend != -1 ) sfac2 -= gaplen2[j+1][k].freq;
5962 			sfac *= sfac2;
5963 #else
5964 //			sfac = 0.0;
5965 #endif
5966 
5967 #if DEBUG
5968 			reporterr( " (jgap start check i=%d) -> *prept=%f, *mjpt=%f\n", i, seq1[0][i], seq2[0][j], *prept, *mjpt );
5969 #endif
5970 
5971 #if ALGZGAP
5972 			if( (g=*prept+ ogcp1va* *gf2ptpre) >= *mjpt )
5973 #else
5974 //			if( (g=*prept + fpenalty * sfac ) >= *mjpt )
5975 			if( (g=*prept ) >= *mjpt )
5976 #endif
5977 			{
5978 //				*mjpt = g - fpenalty * sfac;
5979 				*mjpt = g;
5980 				*mpjpt = i-1;
5981 #if DEBUG
5982 				reporterr( "Selected as jgap start!\n" );
5983 #endif
5984 
5985 
5986 #if FREEFREQUENTLY
5987 				freegaplenpartly( gaplen1jbestkamo[j-1], i-3, i-2 );
5988 //				freegaplenpartly( gaplen2jbestkamo[j-1], j-3, j-2 );
5989 #endif
5990 
5991 
5992 //				addnewgaplen( gaplen1jbestkamo[j-1], gaplen1jprev[j-1], gaplen1, lgth1, -1, 0 );
5993 //				addnewgaplen( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], gaplen2, lgth2, -1, 0 );
5994 //				reporterr( "copying gaplen1jbestkamo[%d-1] from galpen1jprev, j=%d, i=%d\n", j, j, i );
5995 				copygaplencompactx( gaplen1jbestkamo[j-1], gaplen1jprev[j-1], lgth1, -1, 0, i, i ); // *mpjpt, i
5996 //				copygaplenrestricted( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], lgth2, -1, 0, j, j ); // j-1, j
5997 //				copygaplencompactx( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], lgth2, -1, 0, j, 1 ); // half!
5998 //				reporterr( "copying gaplen2jbestkamo[%d-1] from galpen2jprev\n", j );
5999 				copygaplencompactx( gaplen2jbestkamo[j-1], gaplen2jprev[j-1], lgth2-j, -1, 0, 1, 1 ); // ryouhou half!
6000 
6001 
6002 //				if( j==2 && i==1 ) exit( 1 );
6003 
6004 
6005 
6006 			}
6007 
6008 //			extendgaplenpartly( gaplen1ibestkamo[i-1], gaplen1, 0, 0 ); // tmptmptmp
6009 //			extendgaplenpartly( gaplen2ibestkamo[i-1], gaplen2, j, j ); // tmptmptmp
6010 
6011 
6012 //			extendgaplenpartly( gaplen1jbestkamo[j-1], gaplen1, i, i ); // tmptmptmp
6013 //			extendgaplenpartly( gaplen2jbestkamo[j-1], gaplen2, 0, 0 ); // tmptmptmp
6014 
6015 
6016 #if USE_PENALTY_EX
6017 			m[j] += fpenalty_ex;
6018 #endif
6019 
6020 #if ALGZGAP
6021 			pfac = 0.0;
6022 #else
6023 
6024 //			pfactmp = calcpfac_gapex( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], j, i, i-*mpjpt, seq2[0], seq1[0], 0 ); // j-1
6025 			pfactmp = calcpfac_gapex_noidatend( gaplen2jbestkamo[j-1], gaplen1jbestkamo[j-1], 1, i, i-*mpjpt, seq2[0]+j, seq1[0], 0 ); // 2jbestkamo->half, 1jbestkamo->full
6026 #if USEGAPLENMTX
6027 			pfac = calcpfac_gapex( gaplen2mtx[*mpjpt][j-1], gaplen1mtx[*mpjpt][j-1], j, i, i-*mpjpt, seq2[0], seq1[0], 0 );
6028 #endif
6029 #if USEGAPLENHALF
6030 			pfac = calcpfac_gapex( gaplen2half[*mpjpt][j-1], gaplen1half[*mpjpt][j-1], 1, i-*mpjpt, i-*mpjpt, seq2[0]+j, seq1[0]+*mpjpt, 0 );
6031 #endif
6032 #if USEGAPLENMTX || USEGAPLENHALF
6033 			if( pfac != pfactmp )
6034 			{
6035 				reporterr( "(jgapex) pfac=%f, but pfactmp=%f (i,j=%d,%d) diff=%f\n", pfac, pfactmp, i, j, pfac-pfactmp );
6036 //				exit( 1 );
6037 				PFACERROR = 1;
6038 			}
6039 #endif
6040 			pfac = pfactmp;
6041 #if DEBUG
6042 			reporterr( "%c-%c, jgap extension check (j=%d), pfac = %f\n", seq1[0][i], '=', j, pfac );
6043 #endif
6044 #endif
6045 			m[j] += fpenalty * pfac;
6046 
6047 
6048 
6049 			if( trywarp )
6050 			{
6051 #if USE_PENALTY_EX
6052 				if( ( g=*prevwmrecordspt++ + fpenalty_shift + fpenalty_ex * ( i - prevwarpi[j-1] + j - prevwarpj[j-1] ) ) > wm ) // naka ha osokute kamawanai
6053 #else
6054 				if( ( g=*prevwmrecordspt++ + fpenalty_shift ) > wm ) // naka ha osokute kamawanai
6055 #endif
6056 				{
6057 //					fprintf( stderr, "WARP!!\n" );
6058 					if( warpn && prevwarpi[j-1] == warpis[warpn-1] && prevwarpj[j-1] == warpjs[warpn-1] )
6059 					{
6060 						*ijppt = warpbase + warpn - 1;
6061 					}
6062 					else
6063 					{
6064 						*ijppt = warpbase + warpn;
6065 						warpis = realloc( warpis, sizeof(int) * ( warpn+1 ) );
6066 						warpjs = realloc( warpjs, sizeof(int) * ( warpn+1 ) );
6067 						warpis[warpn] = prevwarpi[j-1];
6068 						warpjs[warpn] = prevwarpj[j-1];
6069 						warpn++;
6070 					}
6071 					wm = g;
6072 				}
6073 
6074 #if 0
6075 				fprintf( stderr, "%5.0f ", wm );
6076 #endif
6077 				curm = *curpt + wm;
6078 
6079 				if( *wmrecords1pt > *wmrecordspt )
6080 				{
6081 					*wmrecordspt = *wmrecords1pt;
6082 					*warpipt  = *(warpipt-1);
6083 					*warpjpt  = *(warpjpt-1);
6084 				}
6085 				if( curm > *wmrecordspt )
6086 				{
6087 					*wmrecordspt = curm;
6088 					*warpipt = i;
6089 					*warpjpt = j;
6090 				}
6091 				wmrecordspt++;
6092 				wmrecords1pt++;
6093 				warpipt++;
6094 				warpjpt++;
6095 			}
6096 
6097 #if DEBUG
6098 			reporterr( "extention-x 1j???, before extention-x, j=%d\n", j );
6099 			showgaplen( gaplen1jcurr[j], 100 );
6100 #endif
6101 			extendgaplencompactx( gaplen1jcurr[j], gaplen1, i );
6102 
6103 #if DEBUG
6104 			reporterr( "after extention-x\n" );
6105 			showgaplen( gaplen1jcurr[j], 100 );
6106 			reporterr( "extention-x 2j???\n" );
6107 #endif
6108 			extendgaplencompactx( gaplen2jcurr[j], gaplen2+j, 0 );
6109 
6110 
6111 #if 1
6112 			if( *ijppt < 0 )
6113 			{
6114 #if DEBUG
6115 				reporterr( "Path: %d-%d->%d-%d, i=%d (%c), j=%d (%c), *ijppt=%d\n", i, j, i-1, j+*ijppt, i, seq1[0][i], j, seq2[0][j], *ijppt );
6116 				reporterr( "Inserting %d gaps to gaplen1 and copying gaplen2 (%c%d-%c%d)\n", -*ijppt-1, seq1[0][i], i, seq2[0][j], j );
6117 #endif
6118 #if USEGAPLENMTX
6119 //				addnewgaplen( gaplen1mtx[i][j], gaplen1mtx[i-1][j+*ijppt], gaplen1, lgth1, i, -*ijppt-1 );
6120 //				addnewgaplen( gaplen2mtx[i][j], gaplen2mtx[i-1][j+*ijppt], gaplen2, lgth2, -1, 0 );
6121 				copygaplenrestricted( gaplen1mtx[i][j], gaplen1mtx[i-1][j+*ijppt], lgth1, i, -*ijppt-1, i, i ); // 0, lgth1
6122 				copygaplenrestricted( gaplen2mtx[i][j], gaplen2mtx[i-1][j+*ijppt], lgth2, -1, 0, j, j ); // 0, lgth2
6123 #endif
6124 #if USEGAPLENHALF
6125 				copygaplenrestricted_zurasu( gaplen1half[i][j], gaplen1half[i-1][j+*ijppt], lgth1, 0, -*ijppt-1, 0, 0, 1, 1 ); // 0, lgth1
6126 				copygaplenrestricted_zurasu( gaplen2half[i][j], gaplen2half[i-1][j+*ijppt], lgth2, -1, 0, 0, 0, -*ijppt, -*ijppt ); // 0, lgth2
6127 #endif
6128 //				addnewgaplen( gaplen1jcurr[j], gaplen1jprev[j+*ijppt], gaplen1, lgth1, i, -*ijppt-1 );
6129 //				addnewgaplen( gaplen2jcurr[j], gaplen2jprev[j+*ijppt], gaplen2, lgth2, -1, 0 );
6130 //				reporterr( "copying gaplen1jcurr from gaplen1jbest, with a %d insertion\n", -*ijppt-1 );
6131 				copygaplencompactx( gaplen1jcurr[j], gaplen1jprev[j+*ijppt], lgth1, i, -*ijppt-1, i, i ); // scope: i+*ijppt+1, i ?
6132 //				reporterr( "copy end\n" );
6133 //				copygaplenrestricted( gaplen2jcurr[j], gaplen2jprev[j+*ijppt], lgth2, -1, 0, j, j );
6134 				copygaplencompactx( gaplen2jcurr[j], gaplen2jprev[j+*ijppt], lgth2, -1, 0, 0, -*ijppt ); // half! ryouho zureteru
6135 			}
6136 			else if( *ijppt > 0 )
6137 			{
6138 #if DEBUG
6139 				reporterr( "Path: %d-%d->%d-%d, i=%d (%c), j=%d (%c), *ijppt=%d\n", i, j, i-*ijppt, j-1, i, seq1[0][i], j, seq2[0][j], *ijppt );
6140 				reporterr( "Copying gaplen1 inserting %d gaps to gaplen2 (%c%d-%c%d)\n", *ijppt-1, seq1[0][i], i, seq2[0][j], j );
6141 #endif
6142 #if USEGAPLENMTX
6143 //				addnewgaplen( gaplen1mtx[i][j], gaplen1mtx[i-*ijppt][j-1], gaplen1, lgth1, -1, 0 );
6144 //				addnewgaplen( gaplen2mtx[i][j], gaplen2mtx[i-*ijppt][j-1], gaplen2, lgth2, j, *ijppt-1 );
6145 				copygaplenrestricted( gaplen1mtx[i][j], gaplen1mtx[i-*ijppt][j-1], lgth1, -1, 0, i, i ); // 0, lgth1
6146 				copygaplenrestricted( gaplen2mtx[i][j], gaplen2mtx[i-*ijppt][j-1], lgth2, j, *ijppt-1, j, j ); // 0, lgth2
6147 #endif
6148 #if USEGAPLENHALF
6149 				copygaplenrestricted_zurasu( gaplen1half[i][j], gaplen1half[i-*ijppt][j-1], lgth1, -1, 0, 0, 0, *ijppt, *ijppt ); // 0, lgth1
6150 				copygaplenrestricted_zurasu( gaplen2half[i][j], gaplen2half[i-*ijppt][j-1], lgth2, 0, *ijppt-1, 0, 0, 1, 1 ); // 0, lgth2
6151 #endif
6152 //				addnewgaplen( gaplen1jcurr[j], gaplen1jbest[j-1], gaplen1, lgth1, -1, 0 );
6153 //				addnewgaplen( gaplen2jcurr[j], gaplen2jbest[j-1], gaplen2, lgth2, j, *ijppt-1 );
6154 				copygaplencompactx( gaplen1jcurr[j], gaplen1jbest[j-1], lgth1, -1, 0, i, i );
6155 //				copygaplenrestricted( gaplen2jcurr[j], gaplen2jbest[j-1], lgth2, j, *ijppt-1, j, j ); // j-*ijppt+1?
6156 //				copygaplenrestricted_zurasu( gaplen2jcurr[j], gaplen2jbest[j-1], lgth2, 0, *ijppt-1, 0, 0, j, j ); // 2jcurr->half, but 2jbest->full, imanotokoro
6157 				copygaplencompactx( gaplen2jcurr[j], gaplen2jbest[j-1], lgth2, 0, *ijppt-1, 0, 1 ); //ryouhou half
6158 
6159 			}
6160 			else
6161 #endif
6162 			{
6163 #if DEBUG
6164 				reporterr( "Path: %d-%d->%d-%d, i=%d (%c), j=%d (%c), *ijppt=%d\n", i, j, i-1, j-1, i, seq1[0][i], j, seq2[0][j], *ijppt );
6165 				reporterr( "Copying gaplen1 and gaplen2 (%c%d-%c%d)\n", seq1[0][i], i, seq2[0][j], j );
6166 #endif
6167 #if USEGAPLENMTX
6168 //				addnewgaplen( gaplen1mtx[i][j], gaplen1mtx[i-1][j-1], gaplen1, lgth1, -1, 0 );
6169 //				addnewgaplen( gaplen2mtx[i][j], gaplen2mtx[i-1][j-1], gaplen2, lgth2, -1, 0 );
6170 				copygaplenrestricted( gaplen1mtx[i][j], gaplen1mtx[i-1][j-1], lgth1, -1, 0, i, i ); // 0, lgth1
6171 				copygaplenrestricted( gaplen2mtx[i][j], gaplen2mtx[i-1][j-1], lgth2, -1, 0, j, j ); // 0, lgth2
6172 #endif
6173 #if USEGAPLENHALF
6174 				copygaplenrestricted_zurasu( gaplen1half[i][j], gaplen1half[i-1][j-1], lgth1, -1, 0, 0, 0, 1, 1 ); // 0, lgth1
6175 				copygaplenrestricted_zurasu( gaplen2half[i][j], gaplen2half[i-1][j-1], lgth2, -1, 0, 0, 0, 1, 1 ); // 0, lgth2
6176 #endif
6177 //				addnewgaplen( gaplen1jcurr[j], gaplen1jprev[j-1], gaplen1, lgth1, -1, 0 );
6178 //				addnewgaplen( gaplen2jcurr[j], gaplen2jprev[j-1], gaplen2, lgth2, -1, 0 );
6179 				copygaplencompactx( gaplen1jcurr[j], gaplen1jprev[j-1], lgth1, -1, 0, i, i );
6180 //				copygaplenrestricted( gaplen2jcurr[j], gaplen2jprev[j-1], lgth2, -1, 0, j, j );
6181 				copygaplencompactx( gaplen2jcurr[j], gaplen2jprev[j-1], lgth2, -1, 0, 0, 1 ); // half
6182 			}
6183 
6184 #if DEBUG
6185 			reporterr( "at the end of j loop, gaplen1jcurr[%d] = \n", j );
6186 			showgaplen( gaplen1jcurr[j], 100 );
6187 			reporterr( "at the end of j loop, gaplen1prev[%d] = \n", j );
6188 			showgaplen( gaplen1jprev[j], 100 );
6189 #endif
6190 
6191 
6192 
6193 #if 1
6194 			freegaplenpartly( gaplen1jcurr[j-1], i-3, i-2 ); // -1 dehanaku -2??
6195 //			freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // -1 dehanaku -2??
6196 //			freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // half!
6197 			freegaplenpartly( gaplen1jbestkamo[j-1], i-3, i-2 ); // -1 dehanaku -2??
6198 //			freegaplenpartly( gaplen2jbestkamo[j-1], j-3, j-2 ); // -1 dehanaku -2??
6199 			freegaplenpartly( gaplen1jbest[j-1], i-3, i-2 ); // -1 dehanaku -2??
6200 //			freegaplenpartly( gaplen2jbest[j-1], j-3, j-2 ); // -1 dehanaku -2??
6201 #else
6202 			freegaplenpartly( gaplen1jprev[j-1], 0, i-2 ); // -1 dehanaku -2??
6203 			freegaplenpartly( gaplen1jcurr[j-1], 0, i-2 ); // -1 dehanaku -2??
6204 //			freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // -1 dehanaku -2??
6205 //			freegaplenpartly( gaplen2jcurr[j-1], j-3, j-2 ); // half!
6206 			freegaplenpartly( gaplen1jbestkamo[j-1], 0, i-2 ); // -1 dehanaku -2??
6207 //			freegaplenpartly( gaplen2jbestkamo[j-1], j-3, j-2 ); // -1 dehanaku -2??
6208 			freegaplenpartly( gaplen1jbest[j-1], 0, i-2 ); // -1 dehanaku -2??
6209 //			freegaplenpartly( gaplen2jbest[j-1], j-3, j-2 ); // -1 dehanaku -2??
6210 #endif
6211 
6212 
6213 #if USEGAPLENMTX
6214 //			freegaplenpartly( gaplen1mtx[i-1][j-1], 0, i-2 );
6215 //			freegaplenpartly( gaplen2mtx[i-1][j-1], 0, j-2 );
6216 #endif
6217 
6218 
6219 			*curpt++ += wm;
6220 			ijppt++;
6221 			mjpt++;
6222 			prept++;
6223 			mpjpt++;
6224 		}
6225 		lastverticalw[i] = currentw[lgth2-1];
6226 
6227 #if 1
6228 //		freegaplenpartly( gaplen1icurr[i-1], i-1, i-1 );
6229 		freegaplenpartly( gaplen1icurr[i-1], 0, lgth1-i );
6230 		freegaplenpartly( gaplen2icurr[i-1], 0, lgth2 );
6231 //		freegaplenpartly( gaplen1ibestkamo[i-1], i-1, i-1 );
6232 		freegaplenpartly( gaplen1ibestkamo[i-1], 0, lgth1-i );
6233 		freegaplenpartly( gaplen2ibestkamo[i-1], 0, lgth2 );
6234 #endif
6235 
6236 		if( trywarp )
6237 		{
6238 			fltncpy( prevwmrecords, wmrecords, lastj );
6239 			intncpy( prevwarpi, warpi, lastj );
6240 			intncpy( prevwarpj, warpj, lastj );
6241 		}
6242 #if 0
6243 		fprintf( stderr, "i=%d, %15.5f \n", i, wm );
6244 #endif
6245 //if( i == 2 ) exit( 1 );
6246 	}
6247 
6248 	if( trywarp )
6249 	{
6250 //		fprintf( stderr, "wm = %f\n", wm );
6251 //		fprintf( stderr, "warpn = %d\n", warpn );
6252 		free( wmrecords );
6253 		free( prevwmrecords );
6254 		free( warpi );
6255 		free( warpj );
6256 		free( prevwarpi );
6257 		free( prevwarpj );
6258 	}
6259 
6260 
6261 #if OUTGAP0TRY
6262 	if( !outgap )
6263 	{
6264 		for( j=1; j<lgth2+1; j++ )
6265 			currentw[j] -= offset * ( lgth2 - j ) / 2.0;
6266 		for( i=1; i<lgth1+1; i++ )
6267 			lastverticalw[i] -= offset * ( lgth1 - i  / 2.0);
6268 	}
6269 #endif
6270 
6271 	/*
6272 	fprintf( stderr, "\n" );
6273 	for( i=0; i<icyc; i++ ) fprintf( stderr,"%s\n", seq1[i] );
6274 	fprintf( stderr, "#####\n" );
6275 	for( j=0; j<jcyc; j++ ) fprintf( stderr,"%s\n", seq2[j] );
6276 	fprintf( stderr, "====>" );
6277 	for( i=0; i<icyc; i++ ) strcpy( mseq1[i], seq1[i] );
6278 	for( j=0; j<jcyc; j++ ) strcpy( mseq2[j], seq2[j] );
6279 	*/
6280 	if( localhom )
6281 	{
6282 		Atracking_localhom( impmatch, currentw, lastverticalw, seq1, seq2, mseq1, mseq2, ijp, icyc, jcyc, warpis, warpjs, warpbase );
6283 	}
6284 	else
6285 		Atracking( currentw, lastverticalw, seq1, seq2, mseq1, mseq2, ijp, icyc, jcyc, tailgp, warpis, warpjs, warpbase );
6286 
6287 	if( warpis ) free( warpis );
6288 	if( warpjs ) free( warpjs );
6289 
6290 //	fprintf( stderr, "### impmatch = %f\n", *impmatch );
6291 
6292 	resultlen = strlen( mseq1[0] );
6293 	if( alloclen < resultlen || resultlen > N )
6294 	{
6295 		fprintf( stderr, "alloclen=%d, resultlen=%d, N=%d\n", alloclen, resultlen, N );
6296 		ErrorExit( "LENGTH OVER!\n" );
6297 	}
6298 
6299 
6300 	for( i=0; i<icyc; i++ ) strcpy( seq1[i], mseq1[i] );
6301 	for( j=0; j<jcyc; j++ ) strcpy( seq2[j], mseq2[j] );
6302 #if 0
6303 	fprintf( stderr, "\n" );
6304 	for( i=0; i<icyc; i++ ) fprintf( stderr, "%s\n", mseq1[i] );
6305 	fprintf( stderr, "#####\n" );
6306 	for( j=0; j<jcyc; j++ ) fprintf( stderr, "%s\n", mseq2[j] );
6307 #endif
6308 
6309 //	reporterr( "clearing\n" );
6310 	cleargaplens( gaplens );
6311 	if( masklist1 ) FreeIntMtx( masklist1 ); masklist1 = NULL;
6312 	if( masklist2 ) FreeIntMtx( masklist2 ); masklist2 = NULL;
6313 	if( nmask ) free( nmask ); nmask = NULL;
6314 
6315 #if USEGAPLENMTX
6316 /* maikai free */
6317 	reporterr( "Freeing!\n" );
6318 	for( i=0; i<lgth1+1; i++ )
6319 	{
6320 		for( j=0; j<lgth2+1; j++ )
6321 		{
6322 			if( gaplen1mtx[i][j] ) FreeGaplenMtx( gaplen1mtx[i][j], 0 );
6323 			gaplen1mtx[i][j] = NULL;
6324 		}
6325 		free( gaplen1mtx[i] );
6326 		gaplen1mtx[i] = NULL;
6327 	}
6328 	free( gaplen1mtx );
6329 	gaplen1mtx = NULL;
6330 
6331 	for( i=0; i<lgth1+1; i++ )
6332 	{
6333 		for( j=0; j<lgth2+1; j++ )
6334 		{
6335 			if( gaplen2mtx[i][j] ) FreeGaplenMtx( gaplen2mtx[i][j], 0 );
6336 			gaplen2mtx[i][j] = NULL;
6337 		}
6338 		free( gaplen2mtx[i] );
6339 		gaplen2mtx[i] = NULL;
6340 	}
6341 	free( gaplen2mtx );
6342 	gaplen2mtx = NULL;
6343 #endif
6344 
6345 
6346 #if USEGAPLENHALF
6347 	for( i=0; i<lgth1+1; i++ )
6348 	{
6349 		for( j=0; j<lgth2+1; j++ )
6350 		{
6351 			if( gaplen1half[i][j] ) FreeGaplenMtx( gaplen1half[i][j], 0 );
6352 			gaplen1half[i][j] = NULL;
6353 		}
6354 		free( gaplen1half[i] );
6355 		gaplen1half[i] = NULL;
6356 	}
6357 	free( gaplen1half );
6358 	gaplen1half = NULL;
6359 
6360 	for( i=0; i<lgth1+1; i++ )
6361 	{
6362 		for( j=0; j<lgth2+1; j++ )
6363 		{
6364 			if( gaplen2half[i][j] ) FreeGaplenMtx( gaplen2half[i][j], 0 );
6365 			gaplen2half[i][j] = NULL;
6366 		}
6367 		free( gaplen2half[i] );
6368 		gaplen2half[i] = NULL;
6369 	}
6370 	free( gaplen2half );
6371 	gaplen2half = NULL;
6372 #endif
6373 /* maikai free */
6374 
6375 
6376 #if WMCHECK
6377 	fprintf( stderr, "wm = %f\n", wm - *impmatch);
6378 	fprintf( stderr, "*impmatch = %f\n", *impmatch);
6379 
6380 	int kenzan = 0;
6381 	for( i=0; i<icyc; i++ ) for( j=0; j<jcyc; j++ )
6382 	{
6383 		kenzan += pairgapcount( mseq1[i], mseq2[j] );
6384 	}
6385 
6386 
6387 	reporterr( "kenzan = %d -> %f\n", kenzan, (double)kenzan /( icyc*jcyc ) );
6388 
6389 	double pairscore, nogappairscore, diff;
6390 	char **pseq;
6391 	pseq = AllocateCharMtx( 2, strlen( seq1[0] ) + 1 );
6392 	pairscore = nogappairscore = 0.0;
6393 #if 1
6394 	for( i=0; i<icyc; i++ ) for( j=0; j<jcyc; j++ )
6395 	{
6396 		strcpy( pseq[0], seq1[i] );
6397 		strcpy( pseq[1], seq2[j] );
6398 		commongappick( 2, pseq );
6399 		c = which[i][j];
6400 		pairscore += eff1[i] * eff2[j] * naivepairscore11_dynmtx( matrices[c], pseq[0], pseq[1], penalty );
6401 		nogappairscore += eff1[i] * eff2[j] * naivepairscore11_dynmtx( matrices[c], pseq[0], pseq[1], 0 );
6402 	}
6403 #else
6404 	for( c=0; c<maxdistclass; c++ )
6405 	{
6406 		for( i=0; i<icyc; i++ ) for( j=0; j<jcyc; j++ )
6407 		{
6408 			strcpy( pseq[0], seq1[i] );
6409 			strcpy( pseq[1], seq2[j] );
6410 			commongappick( 2, pseq );
6411 			pairscore += eff1s[c][i] * eff2s[c][j] * naivepairscore11_dynmtx( matrices[c], pseq[0], pseq[1], penalty );
6412 			nogappairscore += eff1s[c][i] * eff2s[c][j] * naivepairscore11_dynmtx( matrices[c], pseq[0], pseq[1], 0 );
6413 		}
6414 	}
6415 #endif
6416 
6417 	FreeCharMtx( pseq );
6418 	diff = (pairscore - wm + *impmatch ) / (double)strlen( seq1[0] );
6419 	reporterr( "pairscore = %f\n", (double)pairscore );
6420 	reporterr( "pairscore-nogappairscore = %f\n", (double)(pairscore-nogappairscore) );
6421 	reporterr( "pairscore-nogappairscore / penalty = %f\n", (double)(pairscore-nogappairscore)/(double)(fpenalty) );
6422 	reporterr( "diff = %f\n\n", diff );
6423 
6424 #if 1
6425 	if( ( !trywarp && fabs( diff ) > 0.01 ) || PFACERROR )
6426 //	if( abs( pairscore - wm +*impmatch ) > 0.01 )
6427 #else
6428 	if( abs( pairscore - wm +*impmatch ) > 0.01 )
6429 #endif
6430 //	if( abs( pairscore - wm +*impmatch ) > 0.01 )
6431 	{
6432 		for( i=0; i<icyc; i++ )
6433 			printf( ">group1\n%s\n", seq1[i] );
6434 		for( j=0; j<jcyc; j++ )
6435 			printf( ">group2\n%s\n", seq2[j] );
6436 		exit( 1 );
6437 	}
6438 #else
6439 	reporterr( "\n" );
6440 #endif
6441 
6442 #if 0
6443 //	if( strlen( seq1[0] ) - lgth1 > 100 && icyc > 1 || strlen( seq2[0] ) - lgth2 > 100 & jcyc > 1 )
6444 	if( strstr( seq1[0], "LNDDAT" ) && icyc == 1 || strstr( seq2[0], "LNDDAT" ) && jcyc==1)
6445 	{
6446 		for( i=0; i<icyc; i++ )
6447 			printf( ">group1\n%s\n", seq1[i] );
6448 		for( j=0; j<jcyc; j++ )
6449 			printf( ">group2\n%s\n", seq2[j] );
6450 		exit( 1 );
6451 	}
6452 #endif
6453 
6454 
6455 	return( wm );
6456 }
6457