6 #define SHISHAGONYU 0 // for debug
10 static char *whereispairalign;
11 static char *laraparams;
12 static char foldalignopt[1000];
13 static int stdout_align;
14 static int stdout_dist;
15 static int store_localhom;
16 static int store_dist;
20 static int lastsubopt;
22 static int usenaivescoreinsteadofalignmentscore;
23 static int specifictarget;
25 typedef struct _lastres
47 typedef struct _lastresx
54 #ifdef enablemultithread
55 typedef struct _jobtable
61 typedef struct _thread_arg
69 int *thereisxineachseq;
70 LocalHom **localhomtable;
77 pthread_mutex_t *mutex_counter;
78 pthread_mutex_t *mutex_stdout;
82 typedef struct _lastcallthread_arg
88 #ifdef enablemultithread
91 pthread_mutex_t *mutex;
93 } lastcallthread_arg_t;
95 static void t2u( char *seq )
99 if ( *seq == 'A' ) *seq = 'a';
100 else if( *seq == 'a' ) *seq = 'a';
101 else if( *seq == 'T' ) *seq = 'u';
102 else if( *seq == 't' ) *seq = 'u';
103 else if( *seq == 'U' ) *seq = 'u';
104 else if( *seq == 'u' ) *seq = 'u';
105 else if( *seq == 'G' ) *seq = 'g';
106 else if( *seq == 'g' ) *seq = 'g';
107 else if( *seq == 'C' ) *seq = 'c';
108 else if( *seq == 'c' ) *seq = 'c';
114 static int removex( char *d, char *m )
119 if( *m == 'X' || *m == 'x' )
133 static void putlocalhom_last( char *s1, char *s2, LocalHom *localhompt, Lastresx *lastresx, char korh )
140 LocalHom *tmppt = localhompt;
142 LocalHom *localhompt0;
148 // fprintf( stderr, "s1=%s\n", s1 );
149 // fprintf( stderr, "s2=%s\n", s2 );
152 naln = lastresx->naln;
155 if( naln == 0 ) return;
165 if( nlocalhom++ > 0 )
167 // fprintf( stderr, "reallocating ...\n" );
168 tmppt->next = (LocalHom *)calloc( 1, sizeof( LocalHom ) );
169 // fprintf( stderr, "done\n" );
173 tmppt->start1 = rpt1->start;
174 tmppt->start2 = rpt2->start;
175 tmppt->end1 = rpt1->end;
176 tmppt->end2 = rpt2->end;
178 if( rpt1 == apt->reg1 ) localhompt0 = tmppt; // ?
180 // fprintf( stderr, "in putlocalhom, reg1: %d-%d (nreg=%d)\n", rpt1->start, rpt1->end, lastresx->nreg );
181 // fprintf( stderr, "in putlocalhom, reg2: %d-%d (nreg=%d)\n", rpt2->start, rpt2->end, lastresx->nreg );
183 len = tmppt->end1 - tmppt->start1 + 1;
185 // fprintf( stderr, "tmppt->start1=%d\n", tmppt->start1 );
186 // fprintf( stderr, "tmppt->start2=%d\n", tmppt->start2 );
188 // fprintf( stderr, "s1+tmppt->start1=%*.*s\n", len, len, s1+tmppt->start1 );
189 // fprintf( stderr, "s2+tmppt->start2=%*.*s\n", len, len, s2+tmppt->start2 );
191 pt1 = s1 + tmppt->start1;
192 pt2 = s2 + tmppt->start2;
196 iscore += n_dis[(int)amino_n[(unsigned char)*pt1++]][(int)amino_n[(unsigned char)*pt2++]]; // - offset
\e$B$O$$$i$J$$$+$b
\e(B
197 // fprintf( stderr, "len=%d, %c-%c, iscore(0) = %d\n", len, *(pt1-1), *(pt2-1), iscore );
202 tmppt->overlapaa = tmppt->end2-tmppt->start2+1;
203 tmppt->opt = (double)iscore / tmppt->overlapaa * 5.8 / 600;
208 sumoverlap += tmppt->end2-tmppt->start2+1;
214 fprintf( stderr, "iscore (1)= %d\n", iscore );
215 fprintf( stderr, "al1: %d - %d\n", start1, end1 );
216 fprintf( stderr, "al2: %d - %d\n", start2, end2 );
221 for( tmppt2=localhompt0; tmppt2; tmppt2=tmppt2->next )
223 tmppt2->overlapaa = sumoverlap;
224 tmppt2->opt = (double)isumscore * 5.8 / ( 600 * sumoverlap );
225 // fprintf( stderr, "tmpptr->opt = %f\n", tmppt->opt );
232 static int countcomma( char *s )
235 while( *s ) if( *s++ == ',' ) v++;
239 static double recallpairfoldalign( char **mseq1, char **mseq2, int m1, int m2, int *of1pt, int *of2pt, int alloclen )
241 static FILE *fp = NULL;
249 fp = fopen( "_foldalignout", "r" );
252 fprintf( stderr, "Cannot open _foldalignout\n" );
257 aln1 = calloc( alloclen, sizeof( char ) );
258 aln2 = calloc( alloclen, sizeof( char ) );
260 readpairfoldalign( fp, *mseq1, *mseq2, aln1, aln2, m1, m2, &of1tmp, &of2tmp, alloclen );
262 if( strstr( foldalignopt, "-global") )
264 fprintf( stderr, "Calling G__align11\n" );
265 value = G__align11( n_dis_consweight_multi, mseq1, mseq2, alloclen, outgap, outgap );
271 fprintf( stderr, "Calling L__align11\n" );
272 value = L__align11( n_dis_consweight_multi, 0.0, mseq1, mseq2, alloclen, of1pt, of2pt );
275 // value = (double)naivepairscore11( *mseq1, *mseq2, penalty ); // nennnotame
279 fprintf( stderr, "FOLDALIGN returned no alignment between %d and %d. Sequence alignment is used instead.\n", m1+1, m2+1 );
283 strcpy( *mseq1, aln1 );
284 strcpy( *mseq2, aln2 );
289 // value = naivepairscore11( *mseq1, *mseq2, penalty ); // v6.511 ha kore wo tsukau, global nomi dakara.
291 // fclose( fp ); // saigo dake yatta houga yoi.
293 // fprintf( stderr, "*mseq1 = %s\n", *mseq1 );
294 // fprintf( stderr, "*mseq2 = %s\n", *mseq2 );
303 static void block2reg( char *block, Reg *reg1, Reg *reg2, int start1, int start2 )
308 int len, glen1, glen2;
316 // fprintf( stderr, "block = %s\n", block );
317 tpt = strchr( block, ':' );
318 npt = strchr( block, ',' );
319 if( !tpt || tpt > npt )
328 // fprintf( stderr, "in loop reg1: %d-%d\n", reg1->start, reg1->end );
329 // fprintf( stderr, "in loop reg2: %d-%d\n", reg2->start, reg2->end );
335 sscanf( block, "%d:%d", &glen1, &glen2 );
342 reg1->start = reg1->end = reg2->start = reg2->end = -1;
344 while( rpt1->start != -1 )
346 // fprintf( stderr, "reg1: %d-%d\n", rpt1->start, rpt1->end );
347 // fprintf( stderr, "reg2: %d-%d\n", rpt2->start, rpt2->end );
351 // *apt1 = *apt2 = 0;
352 // fprintf( stderr, "aln1 = %s\n", aln1 );
353 // fprintf( stderr, "aln2 = %s\n", aln2 );
357 static void readlastresx_singleq( FILE *fp, int n1, int nameq, Lastresx **lastresx )
361 int prevnaln, naln, nreg;
363 int i, pstart, pend, end1, end2;
365 int score, name1, start1, alnSize1, seqSize1;
366 int name2, start2, alnSize2, seqSize2;
367 char strand1, strand2;
368 int includeintoscore;
369 gett = calloc( 10000, sizeof( char ) );
371 // fprintf( stderr, "seq2[0] = %s\n", seq2[0] );
372 // fprintf( stderr, "seq1[0] = %s\n", seq1[0] );
376 fgets( gett, 9999, fp );
377 if( feof( fp ) ) break;
378 if( gett[0] == '#' ) continue;
379 // fprintf( stdout, "gett = %s\n", gett );
380 if( gett[strlen(gett)-1] != '\n' )
382 fprintf( stderr, "Too long line?\n" );
386 sscanf( gett, "%d %d %d %d %c %d %d %d %d %c %d",
387 &score, &name1, &start1, &alnSize1, &strand1, &seqSize1,
388 &name2, &start2, &alnSize2, &strand2, &seqSize2 );
390 if( alg == 'R' && name2 <= name1 ) continue;
393 fprintf( stderr, "BUG!!!\n" );
397 // if( lastresx[name1][name2].score ) continue; // dame!!!!
400 prevnaln = lastresx[name1][name2].naln;
402 for( i=0; i<prevnaln; i++ )
404 nreg = lastresx[name1][name2].aln[i].nreg;
406 pstart = lastresx[name1][name2].aln[i].reg1[0].start + 0;
407 pend = lastresx[name1][name2].aln[i].reg1[nreg-1].end - 0;
408 end1 = start1 + alnSize1;
409 // fprintf( stderr, "pstart = %d, pend = %d\n", pstart, pend );
410 if( pstart <= start1 && start1 <= pend && pend - start1 > 1 ) break;
411 if( pstart <= end1 && end1 <= pend && end1 - pstart > 1 ) break;
413 pstart = lastresx[name1][name2].aln[i].reg2[0].start + 0;
414 pend = lastresx[name1][name2].aln[i].reg2[nreg-1].end - 0;
415 end2 = start2 + alnSize2;
416 // fprintf( stderr, "pstart = %d, pend = %d\n", pstart, pend );
417 if( pstart <= start2 && start2 <= pend && pend - start2 > 1 ) break;
418 if( pstart <= end2 && end2 <= pend && end2 - pstart > 1 ) break;
420 includeintoscore = ( i == prevnaln );
422 if( prevnaln ) includeintoscore = 0;
423 else includeintoscore = 1;
425 if( !includeintoscore && !lastsubopt )
429 lastresx[name1][name2].naln = naln;
430 // fprintf( stderr, "OK! add this alignment to hat3, %d-%d, naln = %d->%d\n", name1, name2, prevnaln, naln );
432 if( ( tmpaln = (Aln *)realloc( lastresx[name1][name2].aln, (naln) * sizeof( Aln ) ) ) == NULL ) // yoyu nashi
434 fprintf( stderr, "Cannot reallocate lastresx[][].aln\n" );
438 lastresx[name1][name2].aln = tmpaln;
440 nreg = countcomma( gett )/2 + 1;
441 lastresx[name1][name2].aln[prevnaln].nreg = nreg;
442 // lastresx[name1][name2].aln[naln].nreg = -1;
443 // lastresx[name1][name2].aln[naln].reg1 = NULL;
444 // lastresx[name1][name2].aln[naln].reg2 = NULL;
445 // fprintf( stderr, "name1=%d, name2=%d, nreg=%d, prevnaln=%d\n", name1, name2, nreg, prevnaln );
447 if( ( lastresx[name1][name2].aln[prevnaln].reg1 = (Reg *)calloc( nreg+1, sizeof( Reg ) ) ) == NULL ) // yoyu nashi
449 fprintf( stderr, "Cannot reallocate lastresx[][].reg2\n" );
453 if( ( lastresx[name1][name2].aln[prevnaln].reg2 = (Reg *)calloc( nreg+1, sizeof( Reg ) ) ) == NULL ) // yoyu nashi
455 fprintf( stderr, "Cannot reallocate lastresx[][].reg2\n" );
459 // lastresx[name1][name2].aln[prevnaln].reg1[0].start = -1; // iranai?
460 // lastresx[name1][name2].aln[prevnaln].reg2[0].start = -1; // iranai?
461 block2reg( strrchr( gett, '\t' ), lastresx[name1][name2].aln[prevnaln].reg1, lastresx[name1][name2].aln[prevnaln].reg2, start1, start2 );
463 if( includeintoscore )
465 if( lastresx[name1][name2].score ) score += penalty;
466 lastresx[name1][name2].score += score;
469 // fprintf( stderr, "score(%d,%d) = %d\n", name1, name2, lastresx[name1][name2].score );
474 #ifdef enablemultithread
476 static void readlastresx_group( FILE *fp, Lastresx **lastresx )
480 int prevnaln, naln, nreg;
482 int i, pstart, pend, end1, end2;
484 int score, name1, start1, alnSize1, seqSize1;
485 int name2, start2, alnSize2, seqSize2;
486 char strand1, strand2;
487 int includeintoscore;
488 gett = calloc( 10000, sizeof( char ) );
490 // fprintf( stderr, "seq2[0] = %s\n", seq2[0] );
491 // fprintf( stderr, "seq1[0] = %s\n", seq1[0] );
495 fgets( gett, 9999, fp );
496 if( feof( fp ) ) break;
497 if( gett[0] == '#' ) continue;
498 // fprintf( stdout, "gett = %s\n", gett );
499 if( gett[strlen(gett)-1] != '\n' )
501 fprintf( stderr, "Too long line?\n" );
505 sscanf( gett, "%d %d %d %d %c %d %d %d %d %c %d",
506 &score, &name1, &start1, &alnSize1, &strand1, &seqSize1,
507 &name2, &start2, &alnSize2, &strand2, &seqSize2 );
509 if( alg == 'R' && name2 <= name1 ) continue;
511 // if( lastresx[name1][name2].score ) continue; // dame!!!!
513 prevnaln = lastresx[name1][name2].naln;
515 for( i=0; i<prevnaln; i++ )
517 nreg = lastresx[name1][name2].aln[i].nreg;
519 pstart = lastresx[name1][name2].aln[i].reg1[0].start + 0;
520 pend = lastresx[name1][name2].aln[i].reg1[nreg-1].end - 0;
521 end1 = start1 + alnSize1;
522 // fprintf( stderr, "pstart = %d, pend = %d\n", pstart, pend );
523 if( pstart <= start1 && start1 <= pend && pend - start1 > 3 ) break;
524 if( pstart <= end1 && end1 <= pend && end1 - pstart > 3 ) break;
526 pstart = lastresx[name1][name2].aln[i].reg2[0].start + 0;
527 pend = lastresx[name1][name2].aln[i].reg2[nreg-1].end - 0;
528 end2 = start2 + alnSize2;
529 // fprintf( stderr, "pstart = %d, pend = %d\n", pstart, pend );
530 if( pstart <= start2 && start2 <= pend && pend - start2 > 3 ) break;
531 if( pstart <= end2 && end2 <= pend && end2 - pstart > 3 ) break;
533 includeintoscore = ( i == prevnaln );
535 if( prevnaln ) includeintoscore = 0;
536 else includeintoscore = 1;
538 if( !includeintoscore && !lastsubopt )
542 lastresx[name1][name2].naln = naln;
543 // fprintf( stderr, "OK! add this alignment to hat3, %d-%d, naln = %d->%d\n", name1, name2, prevnaln, naln );
545 if( ( tmpaln = (Aln *)realloc( lastresx[name1][name2].aln, (naln) * sizeof( Aln ) ) ) == NULL ) // yoyu nashi
547 fprintf( stderr, "Cannot reallocate lastresx[][].aln\n" );
551 lastresx[name1][name2].aln = tmpaln;
555 nreg = countcomma( gett )/2 + 1;
556 lastresx[name1][name2].aln[prevnaln].nreg = nreg;
557 // lastresx[name1][name2].aln[naln].nreg = -1;
558 // lastresx[name1][name2].aln[naln].reg1 = NULL;
559 // lastresx[name1][name2].aln[naln].reg2 = NULL;
560 // fprintf( stderr, "name1=%d, name2=%d, nreg=%d, prevnaln=%d\n", name1, name2, nreg, prevnaln );
562 if( ( lastresx[name1][name2].aln[prevnaln].reg1 = (Reg *)calloc( nreg+1, sizeof( Reg ) ) ) == NULL ) // yoyu nashi
564 fprintf( stderr, "Cannot reallocate lastresx[][].reg2\n" );
568 if( ( lastresx[name1][name2].aln[prevnaln].reg2 = (Reg *)calloc( nreg+1, sizeof( Reg ) ) ) == NULL ) // yoyu nashi
570 fprintf( stderr, "Cannot reallocate lastresx[][].reg2\n" );
574 // lastresx[name1][name2].aln[prevnaln].reg1[0].start = -1; // iranai?
575 // lastresx[name1][name2].aln[prevnaln].reg2[0].start = -1; // iranai?
576 block2reg( strrchr( gett, '\t' ), lastresx[name1][name2].aln[prevnaln].reg1, lastresx[name1][name2].aln[prevnaln].reg2, start1, start2 );
578 if( includeintoscore )
580 if( lastresx[name1][name2].score ) score += penalty;
581 lastresx[name1][name2].score += score;
584 // fprintf( stderr, "score(%d,%d) = %d\n", name1, name2, lastresx[name1][name2].score );
591 static void readlastresx( FILE *fp, int n1, int n2, Lastresx **lastresx, char **seq1, char **seq2 )
595 int prevnaln, naln, nreg;
597 int i, pstart, pend, end1, end2;
599 int score, name1, start1, alnSize1, seqSize1;
600 int name2, start2, alnSize2, seqSize2;
601 char strand1, strand2;
602 int includeintoscore;
603 gett = calloc( 10000, sizeof( char ) );
605 // fprintf( stderr, "seq2[0] = %s\n", seq2[0] );
606 // fprintf( stderr, "seq1[0] = %s\n", seq1[0] );
610 fgets( gett, 9999, fp );
611 if( feof( fp ) ) break;
612 if( gett[0] == '#' ) continue;
613 // fprintf( stdout, "gett = %s\n", gett );
614 if( gett[strlen(gett)-1] != '\n' )
616 fprintf( stderr, "Too long line?\n" );
620 sscanf( gett, "%d %d %d %d %c %d %d %d %d %c %d",
621 &score, &name1, &start1, &alnSize1, &strand1, &seqSize1,
622 &name2, &start2, &alnSize2, &strand2, &seqSize2 );
624 if( alg == 'R' && name2 <= name1 ) continue;
626 // if( lastresx[name1][name2].score ) continue; // dame!!!!
628 prevnaln = lastresx[name1][name2].naln;
630 for( i=0; i<prevnaln; i++ )
632 nreg = lastresx[name1][name2].aln[i].nreg;
634 pstart = lastresx[name1][name2].aln[i].reg1[0].start + 0;
635 pend = lastresx[name1][name2].aln[i].reg1[nreg-1].end - 0;
636 end1 = start1 + alnSize1;
637 // fprintf( stderr, "pstart = %d, pend = %d\n", pstart, pend );
638 if( pstart <= start1 && start1 <= pend && pend - start1 > 3 ) break;
639 if( pstart <= end1 && end1 <= pend && end1 - pstart > 3 ) break;
641 pstart = lastresx[name1][name2].aln[i].reg2[0].start + 0;
642 pend = lastresx[name1][name2].aln[i].reg2[nreg-1].end - 0;
643 end2 = start2 + alnSize2;
644 // fprintf( stderr, "pstart = %d, pend = %d\n", pstart, pend );
645 if( pstart <= start2 && start2 <= pend && pend - start2 > 3 ) break;
646 if( pstart <= end2 && end2 <= pend && end2 - pstart > 3 ) break;
648 includeintoscore = ( i == prevnaln );
650 if( prevnaln ) includeintoscore = 0;
651 else includeintoscore = 1;
653 if( !includeintoscore && !lastsubopt )
657 lastresx[name1][name2].naln = naln;
658 // fprintf( stderr, "OK! add this alignment to hat3, %d-%d, naln = %d->%d\n", name1, name2, prevnaln, naln );
660 if( ( tmpaln = (Aln *)realloc( lastresx[name1][name2].aln, (naln) * sizeof( Aln ) ) ) == NULL ) // yoyu nashi
662 fprintf( stderr, "Cannot reallocate lastresx[][].aln\n" );
666 lastresx[name1][name2].aln = tmpaln;
670 nreg = countcomma( gett )/2 + 1;
671 lastresx[name1][name2].aln[prevnaln].nreg = nreg;
672 // lastresx[name1][name2].aln[naln].nreg = -1;
673 // lastresx[name1][name2].aln[naln].reg1 = NULL;
674 // lastresx[name1][name2].aln[naln].reg2 = NULL;
675 // fprintf( stderr, "name1=%d, name2=%d, nreg=%d, prevnaln=%d\n", name1, name2, nreg, prevnaln );
677 if( ( lastresx[name1][name2].aln[prevnaln].reg1 = (Reg *)calloc( nreg+1, sizeof( Reg ) ) ) == NULL ) // yoyu nashi
679 fprintf( stderr, "Cannot reallocate lastresx[][].reg2\n" );
683 if( ( lastresx[name1][name2].aln[prevnaln].reg2 = (Reg *)calloc( nreg+1, sizeof( Reg ) ) ) == NULL ) // yoyu nashi
685 fprintf( stderr, "Cannot reallocate lastresx[][].reg2\n" );
689 // lastresx[name1][name2].aln[prevnaln].reg1[0].start = -1; // iranai?
690 // lastresx[name1][name2].aln[prevnaln].reg2[0].start = -1; // iranai?
691 block2reg( strrchr( gett, '\t' ), lastresx[name1][name2].aln[prevnaln].reg1, lastresx[name1][name2].aln[prevnaln].reg2, start1, start2 );
693 if( includeintoscore )
695 if( lastresx[name1][name2].score ) score += penalty;
696 lastresx[name1][name2].score += score;
699 // fprintf( stderr, "score(%d,%d) = %d\n", name1, name2, lastresx[name1][name2].score );
704 #ifdef enablemultithread
706 static void *lastcallthread_group( void *arg )
708 lastcallthread_arg_t *targ = (lastcallthread_arg_t *)arg;
712 #ifdef enablemultithread
713 int thread_no = targ->thread_no;
714 int *kshare = targ->kshare;
716 Lastresx **lastresx = targ->lastresx;
717 char **dseq = targ->dseq;
718 char **qseq = targ->qseq;
723 int qstart, qend, shou, amari;
729 amari = nq - shou * nthread;
730 fprintf( stderr, "shou: %d, amari: %d\n", shou, amari );
732 qstart = thread_no * shou;
733 if( thread_no - 1 < amari ) qstart += thread_no;
734 else qstart += amari;
736 qend = qstart + shou - 1;
737 if( thread_no < amari ) qend += 1;
738 fprintf( stderr, "%d: %d-%d\n", thread_no, qstart, qend );
745 if( qstart > qend ) break;
746 if( k == thread_no ) break;
747 fprintf( stderr, "\n%d-%d / %d (thread %d) \n", qstart, qend, nq, thread_no );
754 fprintf( stderr, "\r%d / %d \r", k, nq );
757 if( alg == 'R' ) // if 'r' -> calllast_fast
759 fprintf( stderr, "Not supported\n" );
767 sprintf( command, "_q%d", k );
768 lfp = fopen( command, "w" );
771 fprintf( stderr, "Cannot open %s", command );
774 for( i=qstart; i<=qend; i++ )
775 fprintf( lfp, ">%d\n%s\n", i, qseq[i] );
778 // if( alg == 'R' ) msize = MAX(10,k+nq);
779 // else msize = MAX(10,nd+nq);
780 if( alg == 'R' ) msize = MAX(10,k*lastm);
781 else msize = MAX(10,nd*lastm);
783 // fprintf( stderr, "Calling lastal from lastcallthread, msize = %d, k=%d\n", msize, k );
784 // sprintf( command, "grep '>' _db%sd", kd );
785 // system( command );
786 sprintf( command, "%s/lastal -m %d -e %d -f 0 -s 1 -p _scoringmatrixforlast -a %d -b %d _db%sd _q%d > _lastres%d", whereispairalign, msize, laste, -penalty, -penalty_ex, kd, k, k );
787 if( system( command ) ) exit( 1 );
789 sprintf( command, "_lastres%d", k );
790 lfp = fopen( command, "r" );
793 fprintf( stderr, "Cannot read _lastres%d", k );
796 // readlastres( lfp, nd, nq, lastres, dseq, qseq );
797 // fprintf( stderr, "Reading lastres\n" );
798 readlastresx_group( lfp, lastresx );
806 static void *lastcallthread( void *arg )
808 lastcallthread_arg_t *targ = (lastcallthread_arg_t *)arg;
812 #ifdef enablemultithread
813 int thread_no = targ->thread_no;
814 int *kshare = targ->kshare;
816 Lastresx **lastresx = targ->lastresx;
817 char **dseq = targ->dseq;
818 char **qseq = targ->qseq;
829 #ifdef enablemultithread
832 pthread_mutex_lock( targ->mutex );
836 pthread_mutex_unlock( targ->mutex );
839 fprintf( stderr, "\r%d / %d (thread %d) \r", k, nq, thread_no );
841 pthread_mutex_unlock( targ->mutex );
848 fprintf( stderr, "\r%d / %d \r", k, nq );
851 if( alg == 'R' ) // if 'r' -> calllast_fast
853 klim = MIN( k, njob-nadd );
854 // klim = k; // dochira demo yoi
857 sprintf( command, "_db%dd", k );
858 lfp = fopen( command, "w" );
861 fprintf( stderr, "Cannot open _db." );
864 for( i=0; i<klim; i++ ) fprintf( lfp, ">%d\n%s\n", i, dseq[i] );
867 // sprintf( command, "md5sum _db%dd > /dev/tty", k );
868 // system( command );
871 sprintf( command, "%s/lastdb _db%dd _db%dd", whereispairalign, k, k );
873 sprintf( command, "%s/lastdb -p _db%dd _db%dd", whereispairalign, k, k );
875 sprintf( kd, "%d", k );
877 else // calllast_fast de tsukutta nowo riyou
880 // fprintf( stderr, "klim=%d, njob=%d, nadd=%d, skip!\n", klim, njob, nadd );
888 sprintf( command, "_q%d", k );
889 lfp = fopen( command, "w" );
892 fprintf( stderr, "Cannot open %s", command );
895 fprintf( lfp, ">%d\n%s\n", k, qseq[k] );
898 // if( alg == 'R' ) msize = MAX(10,k+nq);
899 // else msize = MAX(10,nd+nq);
900 if( alg == 'R' ) msize = MAX(10,k*lastm);
901 else msize = MAX(10,nd*lastm);
903 // fprintf( stderr, "Calling lastal from lastcallthread, msize = %d, k=%d\n", msize, k );
904 // sprintf( command, "grep '>' _db%sd", kd );
905 // system( command );
906 sprintf( command, "%s/lastal -m %d -e %d -f 0 -s 1 -p _scoringmatrixforlast -a %d -b %d _db%sd _q%d > _lastres%d", whereispairalign, msize, laste, -penalty, -penalty_ex, kd, k, k );
907 if( system( command ) ) exit( 1 );
909 sprintf( command, "_lastres%d", k );
910 lfp = fopen( command, "r" );
913 fprintf( stderr, "Cannot read _lastres%d", k );
916 // readlastres( lfp, nd, nq, lastres, dseq, qseq );
917 // fprintf( stderr, "Reading lastres\n" );
918 readlastresx_singleq( lfp, nd, k, lastresx );
925 static void calllast_fast( int nd, char **dseq, int nq, char **qseq, Lastresx **lastresx )
931 lfp = fopen( "_scoringmatrixforlast", "w" );
934 fprintf( stderr, "Cannot open _scoringmatrixforlast" );
940 for( j=0; j<4; j++ ) fprintf( lfp, " %c ", amino[j] );
941 fprintf( lfp, "\n" );
944 fprintf( lfp, "%c ", amino[i] );
945 for( j=0; j<4; j++ ) fprintf( lfp, " %d ", n_dis[i][j] );
946 fprintf( lfp, "\n" );
952 for( j=0; j<20; j++ ) fprintf( lfp, " %c ", amino[j] );
953 fprintf( lfp, "\n" );
954 for( i=0; i<20; i++ )
956 fprintf( lfp, "%c ", amino[i] );
957 for( j=0; j<20; j++ ) fprintf( lfp, " %d ", n_dis[i][j] );
958 fprintf( lfp, "\n" );
963 // if( alg == 'r' ) // if 'R' -> lastcallthread, kokonoha nadd>0 no toki nomi shiyou
965 sprintf( command, "_dbd" );
966 lfp = fopen( command, "w" );
969 fprintf( stderr, "Cannot open _dbd" );
976 for( i=0; i<j; i++ ) fprintf( lfp, ">%d\n%s\n", i, dseq[i] );
980 sprintf( command, "%s/lastdb _dbd _dbd", whereispairalign );
982 sprintf( command, "%s/lastdb -p _dbd _dbd", whereispairalign );
986 #ifdef enablemultithread
990 pthread_mutex_t mutex;
991 lastcallthread_arg_t *targ;
993 targ = (lastcallthread_arg_t *)calloc( nthread, sizeof( lastcallthread_arg_t ) );
994 handle = calloc( nthread, sizeof( pthread_t ) );
995 ksharept = calloc( 1, sizeof(int) );
997 pthread_mutex_init( &mutex, NULL );
998 for( i=0; i<nthread; i++ )
1000 targ[i].thread_no = i;
1001 targ[i].kshare = ksharept;
1004 targ[i].dseq = dseq;
1005 targ[i].qseq = qseq;
1006 targ[i].lastresx = lastresx;
1007 targ[i].mutex = &mutex;
1008 pthread_create( handle+i, NULL, lastcallthread, (void *)(targ+i) );
1011 for( i=0; i<nthread; i++ )
1013 pthread_join( handle[i], NULL );
1015 pthread_mutex_destroy( &mutex );
1023 lastcallthread_arg_t *targ;
1024 targ = (lastcallthread_arg_t *)calloc( 1, sizeof( lastcallthread_arg_t ) );
1027 targ[0].dseq = dseq;
1028 targ[0].qseq = qseq;
1029 targ[0].lastresx = lastresx;
1030 lastcallthread( targ );
1036 static void calllast_once( int nd, char **dseq, int nq, char **qseq, Lastresx **lastresx )
1044 fprintf( stderr, "nq=%d\n", nq );
1046 lfp = fopen( "_db", "w" );
1049 fprintf( stderr, "Cannot open _db" );
1052 for( i=0; i<nd; i++ ) fprintf( lfp, ">%d\n%s\n", i, dseq[i] );
1057 sprintf( command, "%s/lastdb _db _db", whereispairalign );
1059 lfp = fopen( "_scoringmatrixforlast", "w" );
1062 fprintf( stderr, "Cannot open _scoringmatrixforlast" );
1065 fprintf( lfp, " " );
1066 for( j=0; j<4; j++ ) fprintf( lfp, " %c ", amino[j] );
1067 fprintf( lfp, "\n" );
1068 for( i=0; i<4; i++ )
1070 fprintf( lfp, "%c ", amino[i] );
1071 for( j=0; j<4; j++ ) fprintf( lfp, " %d ", n_dis[i][j] );
1072 fprintf( lfp, "\n" );
1076 sprintf( command, "lastex -s 2 -a %d -b %d -p _scoringmatrixforlast -E 10000 _db.prj _db.prj > _lastex", -penalty, -penalty_ex );
1078 lfp = fopen( "_lastex", "r" );
1079 fgets( command, 4999, lfp );
1080 fgets( command, 4999, lfp );
1081 fgets( command, 4999, lfp );
1082 fgets( command, 4999, lfp );
1083 laste = atoi( command );
1085 fprintf( stderr, "laste = %d\n", laste );
1093 sprintf( command, "%s/lastdb -p _db _db", whereispairalign );
1095 lfp = fopen( "_scoringmatrixforlast", "w" );
1098 fprintf( stderr, "Cannot open _scoringmatrixforlast" );
1101 fprintf( lfp, " " );
1102 for( j=0; j<20; j++ ) fprintf( lfp, " %c ", amino[j] );
1103 fprintf( lfp, "\n" );
1104 for( i=0; i<20; i++ )
1106 fprintf( lfp, "%c ", amino[i] );
1107 for( j=0; j<20; j++ ) fprintf( lfp, " %d ", n_dis[i][j] );
1108 fprintf( lfp, "\n" );
1111 // fprintf( stderr, "Not written yet\n" );
1114 lfp = fopen( "_q", "w" );
1117 fprintf( stderr, "Cannot open _q" );
1120 for( i=0; i<nq; i++ )
1122 fprintf( lfp, ">%d\n%s\n", i, qseq[i] );
1126 msize = MAX(10,nd*lastm);
1128 // fprintf( stderr, "Calling lastal from calllast_once, msize=%d\n", msize );
1129 sprintf( command, "%s/lastal -v -m %d -e %d -f 0 -s 1 -p _scoringmatrixforlast -a %d -b %d _db _q > _lastres", whereispairalign, msize, laste, -penalty, -penalty_ex );
1130 // sprintf( command, "lastal -v -m %d -e %d -f 0 -s 1 -p _scoringmatrixforlast -a %d -b %d _db _q > _lastres", 1, laste, -penalty, -penalty_ex );
1131 // sprintf( command, "lastal -v -e 40 -f 0 -s 1 -p _scoringmatrixforlast -a %d -b %d _db _q > _lastres", -penalty, -penalty_ex );
1132 res = system( command );
1135 fprintf( stderr, "LAST aborted\n" );
1139 lfp = fopen( "_lastres", "r" );
1142 fprintf( stderr, "Cannot read _lastres" );
1145 // readlastres( lfp, nd, nq, lastres, dseq, qseq );
1146 fprintf( stderr, "Reading lastres\n" );
1147 readlastresx( lfp, nd, nq, lastresx, dseq, qseq );
1151 static void callfoldalign( int nseq, char **mseq )
1156 static char com[10000];
1158 for( i=0; i<nseq; i++ )
1161 fp = fopen( "_foldalignin", "w" );
1164 fprintf( stderr, "Cannot open _foldalignin\n" );
1167 for( i=0; i<nseq; i++ )
1169 fprintf( fp, ">%d\n", i+1 );
1170 fprintf( fp, "%s\n", mseq[i] );
1174 sprintf( com, "env PATH=%s foldalign210 %s _foldalignin > _foldalignout ", whereispairalign, foldalignopt );
1175 res = system( com );
1178 fprintf( stderr, "Error in foldalign\n" );
1184 static void calllara( int nseq, char **mseq, char *laraarg )
1189 static char com[10000];
1191 // for( i=0; i<nseq; i++ )
1193 fp = fopen( "_larain", "w" );
1196 fprintf( stderr, "Cannot open _larain\n" );
1199 for( i=0; i<nseq; i++ )
1201 fprintf( fp, ">%d\n", i+1 );
1202 fprintf( fp, "%s\n", mseq[i] );
1207 // fprintf( stderr, "calling LaRA\n" );
1208 sprintf( com, "env PATH=%s:/bin:/usr/bin mafft_lara -i _larain -w _laraout -o _lara.params %s", whereispairalign, laraarg );
1209 res = system( com );
1212 fprintf( stderr, "Error in lara\n" );
1217 static double recalllara( char **mseq1, char **mseq2, int alloclen )
1219 static FILE *fp = NULL;
1220 static char *ungap1;
1221 static char *ungap2;
1225 static char com[10000];
1231 fp = fopen( "_laraout", "r" );
1234 fprintf( stderr, "Cannot open _laraout\n" );
1237 ungap1 = AllocateCharVec( alloclen );
1238 ungap2 = AllocateCharVec( alloclen );
1239 ori1 = AllocateCharVec( alloclen );
1240 ori2 = AllocateCharVec( alloclen );
1244 strcpy( ori1, *mseq1 );
1245 strcpy( ori2, *mseq2 );
1247 fgets( com, 999, fp );
1248 myfgets( com, 9999, fp );
1249 strcpy( *mseq1, com );
1250 myfgets( com, 9999, fp );
1251 strcpy( *mseq2, com );
1253 gappick0( ungap1, *mseq1 );
1254 gappick0( ungap2, *mseq2 );
1260 if( strcmp( ungap1, ori1 ) || strcmp( ungap2, ori2 ) )
1262 fprintf( stderr, "SEQUENCE CHANGED!!\n" );
1263 fprintf( stderr, "*mseq1 = %s\n", *mseq1 );
1264 fprintf( stderr, "ungap1 = %s\n", ungap1 );
1265 fprintf( stderr, "ori1 = %s\n", ori1 );
1266 fprintf( stderr, "*mseq2 = %s\n", *mseq2 );
1267 fprintf( stderr, "ungap2 = %s\n", ungap2 );
1268 fprintf( stderr, "ori2 = %s\n", ori2 );
1272 value = (double)naivepairscore11( *mseq1, *mseq2, penalty );
1274 // fclose( fp ); // saigo dake yatta houga yoi.
1280 static double calldafs_giving_bpp( char **mseq1, char **mseq2, char **bpp1, char **bpp2, int alloclen, int i, int j )
1289 dirname = calloc( 100, sizeof( char ) );
1290 com = calloc( 1000, sizeof( char ) );
1291 sprintf( dirname, "_%d-%d", i, j );
1292 sprintf( com, "rm -rf %s", dirname );
1294 sprintf( com, "mkdir %s", dirname );
1298 sprintf( com, "%s/_bpporg", dirname );
1299 fp = fopen( com, "w" );
1302 fprintf( stderr, "Cannot write to %s/_bpporg\n", dirname );
1305 fprintf( fp, ">a\n" );
1307 fprintf( fp, "%s", *bpp1++ );
1309 fprintf( fp, ">b\n" );
1311 fprintf( fp, "%s", *bpp2++ );
1314 sprintf( com, "tr -d '\\r' < %s/_bpporg > %s/_bpp", dirname, dirname );
1315 system( com ); // for cygwin, wakaran
1320 sprintf( com, "%s/_dafsinorg", dirname );
1321 fp = fopen( com, "w" );
1324 fprintf( stderr, "Cannot open %s/_dafsinorg\n", dirname );
1327 fprintf( fp, ">1\n" );
1328 // fprintf( fp, "%s\n", *mseq1 );
1329 write1seq( fp, *mseq1 );
1330 fprintf( fp, ">2\n" );
1331 // fprintf( fp, "%s\n", *mseq2 );
1332 write1seq( fp, *mseq2 );
1335 sprintf( com, "tr -d '\\r' < %s/_dafsinorg > %s/_dafsin", dirname, dirname );
1336 system( com ); // for cygwin, wakaran
1338 sprintf( com, "_dafssh%s", dirname );
1339 fp = fopen( com, "w" );
1340 fprintf( fp, "cd %s\n", dirname );
1341 fprintf( fp, "%s/dafs --mafft-in _bpp _dafsin > _dafsout 2>_dum\n", whereispairalign );
1342 fprintf( fp, "exit $tatus\n" );
1345 sprintf( com, "tr -d '\\r' < _dafssh%s > _dafssh%s.unix", dirname, dirname );
1346 system( com ); // for cygwin, wakaran
1348 sprintf( com, "sh _dafssh%s.unix 2>_dum%s", dirname, dirname );
1349 res = system( com );
1352 fprintf( stderr, "Error in dafs\n" );
1356 sprintf( com, "%s/_dafsout", dirname );
1358 fp = fopen( com, "r" );
1361 fprintf( stderr, "Cannot open %s/_dafsout\n", dirname );
1365 myfgets( com, 999, fp ); // nagai kanousei ga arunode
1366 fgets( com, 999, fp );
1367 myfgets( com, 999, fp ); // nagai kanousei ga arunode
1368 fgets( com, 999, fp );
1369 load1SeqWithoutName_new( fp, *mseq1 );
1370 fgets( com, 999, fp );
1371 load1SeqWithoutName_new( fp, *mseq2 );
1375 // fprintf( stderr, "*mseq1 = %s\n", *mseq1 );
1376 // fprintf( stderr, "*mseq2 = %s\n", *mseq2 );
1378 value = (double)naivepairscore11( *mseq1, *mseq2, penalty );
1381 sprintf( com, "rm -rf %s > /dev/null 2>&1", dirname );
1384 fprintf( stderr, "retrying to rmdir\n" );
1397 static double callmxscarna_giving_bpp( char **mseq1, char **mseq2, char **bpp1, char **bpp2, int alloclen, int i, int j )
1406 dirname = calloc( 100, sizeof( char ) );
1407 com = calloc( 1000, sizeof( char ) );
1408 sprintf( dirname, "_%d-%d", i, j );
1409 sprintf( com, "rm -rf %s", dirname );
1411 sprintf( com, "mkdir %s", dirname );
1415 sprintf( com, "%s/_bpporg", dirname );
1416 fp = fopen( com, "w" );
1419 fprintf( stderr, "Cannot write to %s/_bpporg\n", dirname );
1422 fprintf( fp, ">a\n" );
1424 fprintf( fp, "%s", *bpp1++ );
1426 fprintf( fp, ">b\n" );
1428 fprintf( fp, "%s", *bpp2++ );
1431 sprintf( com, "tr -d '\\r' < %s/_bpporg > %s/_bpp", dirname, dirname );
1432 system( com ); // for cygwin, wakaran
1437 sprintf( com, "%s/_mxscarnainorg", dirname );
1438 fp = fopen( com, "w" );
1441 fprintf( stderr, "Cannot open %s/_mxscarnainorg\n", dirname );
1444 fprintf( fp, ">1\n" );
1445 // fprintf( fp, "%s\n", *mseq1 );
1446 write1seq( fp, *mseq1 );
1447 fprintf( fp, ">2\n" );
1448 // fprintf( fp, "%s\n", *mseq2 );
1449 write1seq( fp, *mseq2 );
1452 sprintf( com, "tr -d '\\r' < %s/_mxscarnainorg > %s/_mxscarnain", dirname, dirname );
1453 system( com ); // for cygwin, wakaran
1456 sprintf( com, "cd %s; %s/mxscarnamod -readbpp _mxscarnain > _mxscarnaout 2>_dum", dirname, whereispairalign );
1458 sprintf( com, "_mxscarnash%s", dirname );
1459 fp = fopen( com, "w" );
1460 fprintf( fp, "cd %s\n", dirname );
1461 fprintf( fp, "%s/mxscarnamod -readbpp _mxscarnain > _mxscarnaout 2>_dum\n", whereispairalign );
1462 fprintf( fp, "exit $tatus\n" );
1466 sprintf( com, "tr -d '\\r' < _mxscarnash%s > _mxscarnash%s.unix", dirname, dirname );
1467 system( com ); // for cygwin, wakaran
1469 sprintf( com, "sh _mxscarnash%s.unix 2>_dum%s", dirname, dirname );
1471 res = system( com );
1474 fprintf( stderr, "Error in mxscarna\n" );
1478 sprintf( com, "%s/_mxscarnaout", dirname );
1480 fp = fopen( com, "r" );
1483 fprintf( stderr, "Cannot open %s/_mxscarnaout\n", dirname );
1487 fgets( com, 999, fp );
1488 load1SeqWithoutName_new( fp, *mseq1 );
1489 fgets( com, 999, fp );
1490 load1SeqWithoutName_new( fp, *mseq2 );
1494 // fprintf( stderr, "*mseq1 = %s\n", *mseq1 );
1495 // fprintf( stderr, "*mseq2 = %s\n", *mseq2 );
1497 value = (double)naivepairscore11( *mseq1, *mseq2, penalty );
1500 sprintf( com, "rm -rf %s > /dev/null 2>&1", dirname );
1503 fprintf( stderr, "retrying to rmdir\n" );
1516 static void readhat4( FILE *fp, char ***bpp )
1525 // fprintf( stderr, "reading hat4\n" );
1527 // fprintf( stderr, "onechar = %c\n", onechar );
1528 if( onechar != '>' )
1530 fprintf( stderr, "Format error\n" );
1533 ungetc( onechar, fp );
1534 fgets( oneline, 999, fp );
1538 ungetc( onechar, fp );
1539 if( onechar == '>' || onechar == EOF )
1541 // fprintf( stderr, "Next\n" );
1542 *bpp = realloc( *bpp, (bppsize+2) * sizeof( char * ) );
1543 (*bpp)[bppsize] = NULL;
1546 fgets( oneline, 999, fp );
1547 // fprintf( stderr, "oneline=%s\n", oneline );
1548 // sscanf( oneline, "%d %d %lf", &posi, &posj, &prob );
1549 // fprintf( stderr, "%d %d -> %f\n", posi, posj, prob );
1550 *bpp = realloc( *bpp, (bppsize+2) * sizeof( char * ) );
1551 (*bpp)[bppsize] = calloc( 100, sizeof( char ) );
1552 strcpy( (*bpp)[bppsize], oneline );
1557 static void preparebpp( int nseq, char ***bpp )
1562 fp = fopen( "hat4", "r" );
1565 fprintf( stderr, "Cannot open hat4\n" );
1568 for( i=0; i<nseq; i++ )
1569 readhat4( fp, bpp+i );
1573 static void arguments( int argc, char *argv[] )
1583 foldalignopt[0] = 0;
1615 kobetsubunkatsu = 0;
1621 // dorp = NOTSPECIFIED;
1622 ppenalty = NOTSPECIFIED;
1623 ppenalty_OP = NOTSPECIFIED;
1624 ppenalty_ex = NOTSPECIFIED;
1625 ppenalty_EX = NOTSPECIFIED;
1626 penalty_shift_factor = 1000.0;
1627 poffset = NOTSPECIFIED;
1628 kimuraR = NOTSPECIFIED;
1629 pamN = NOTSPECIFIED;
1631 fftWinSize = NOTSPECIFIED;
1632 fftThreshold = NOTSPECIFIED;
1633 RNAppenalty = NOTSPECIFIED;
1634 RNApthr = NOTSPECIFIED;
1635 specificityconsideration = 0.0;
1636 usenaivescoreinsteadofalignmentscore = 0;
1640 // reporterr( "argc=%d\n", argc );
1641 // reporterr( "*argv=%s\n", *argv );
1642 // reporterr( "(*argv)[0]=%c\n", (*argv)[0] );
1643 while( --argc > 0 && (*++argv)[0] == '-' )
1645 // reporterr( "(*argv)[0] in while loop = %s\n", (*argv) );
1646 while ( ( c = *++argv[0] ) )
1651 inputfile = *++argv;
1652 // fprintf( stderr, "inputfile = %s\n", inputfile );
1656 ppenalty = (int)( atof( *++argv ) * 1000 - 0.5 );
1660 ppenalty_ex = (int)( atof( *++argv ) * 1000 - 0.5 );
1664 ppenalty_OP = (int)( atof( *++argv ) * 1000 - 0.5 );
1668 ppenalty_EX = (int)( atof( *++argv ) * 1000 - 0.5 );
1672 penalty_shift_factor = atof( *++argv );
1676 poffset = (int)( atof( *++argv ) * 1000 - 0.5 );
1680 kimuraR = myatoi( *++argv );
1681 // fprintf( stderr, "kimuraR = %d\n", kimuraR );
1685 nblosum = myatoi( *++argv );
1687 // fprintf( stderr, "blosum %d\n", nblosum );
1691 pamN = myatoi( *++argv );
1694 // fprintf( stderr, "jtt %d\n", pamN );
1698 pamN = myatoi( *++argv );
1701 // fprintf( stderr, "TM %d\n", pamN );
1706 ppslocal = (int)( atof( *++argv ) * 1000 + 0.5 );
1707 pslocal = (int)( 600.0 / 1000.0 * ppslocal + 0.5);
1708 // fprintf( stderr, "ppslocal = %d\n", ppslocal );
1709 // fprintf( stderr, "pslocal = %d\n", pslocal );
1714 if( atof( *++argv ) < 0.00001 ) store_localhom = 0;
1719 whereispairalign = *++argv;
1720 fprintf( stderr, "whereispairalign = %s\n", whereispairalign );
1724 laraparams = *++argv;
1725 fprintf( stderr, "laraparams = %s\n", laraparams );
1729 nthread = myatoi( *++argv );
1730 // fprintf( stderr, "nthread = %d\n", nthread );
1732 #ifndef enablemultithread
1737 nadd = myatoi( *++argv );
1738 // fprintf( stderr, "nadd = %d\n", nadd );
1742 lastm = myatoi( *++argv );
1743 fprintf( stderr, "lastm = %d\n", lastm );
1747 laste = myatoi( *++argv );
1748 fprintf( stderr, "laste = %d\n", laste );
1752 specificityconsideration = (double)myatof( *++argv );
1753 // fprintf( stderr, "specificityconsideration = %f\n", specificityconsideration );
1756 case 'K': // Hontou ha iranai. disttbfast.c, tbfast.c to awaserutame.
1820 alg = 'Y'; // nadd>0 no toki nomi. moto no hairetsu to atarashii hairetsuno alignmnt -> L;
1823 usenaivescoreinsteadofalignmentscore = 1;
1847 alg = 'r'; // nadd>0 no toki nomi. moto no hairetsu to atarashii hairetsuno alignmnt -> R, last
1873 /* Modified 01/08/27, default: user tree */
1877 /* modification end. */
1879 // foldalignopt = *++argv;
1880 strcat( foldalignopt, " " );
1881 strcat( foldalignopt, *++argv );
1882 fprintf( stderr, "foldalignopt = %s\n", foldalignopt );
1887 fftThreshold = myatoi( *++argv );
1891 fftWinSize = myatoi( *++argv );
1899 fprintf( stderr, "illegal option %c\n", c );
1909 cut = atof( (*argv) );
1914 fprintf( stderr, "pairlocalalign options: Check source file !\n" );
1917 if( tbitr == 1 && outgap == 0 )
1919 fprintf( stderr, "conflicting options : o, m or u\n" );
1924 int countamino( char *s, int end )
1928 if( *s++ != '-' ) val++;
1932 static double score2dist( double pscore, double selfscore1, double selfscore2)
1936 // fprintf( stderr, "In score2dist\n" );
1938 if( (bunbo=MIN( selfscore1, selfscore2 )) == 0.0 )
1940 else if( bunbo < pscore ) // mondai ari
1943 val = ( 1.0 - pscore / bunbo ) * 2.0;
1947 #if enablemultithread
1948 static void *athread( void *arg ) // alg='R', alg='r' -> tsukawarenai.
1950 thread_arg_t *targ = (thread_arg_t *)arg;
1951 int i, ilim, j, jst;
1952 int off1, off2, dum1, dum2, thereisx;
1954 double pscore = 0.0; // by D.Mathog
1957 char **mseq1, **mseq2, **distseq1, **distseq2, **dumseq1, **dumseq2;
1959 double **dynamicmtx = NULL;
1964 int thread_no = targ->thread_no;
1965 int njob = targ->njob;
1966 Jobtable *jobpospt = targ->jobpospt;
1967 char **name = targ->name;
1968 char **seq = targ->seq;
1969 char **dseq = targ->dseq;
1970 int *thereisxineachseq = targ->thereisxineachseq;
1971 LocalHom **localhomtable = targ->localhomtable;
1972 double **distancemtx = targ->distancemtx;
1973 double *selfscore = targ->selfscore;
1974 char ***bpp = targ->bpp;
1975 Lastresx **lastresx = targ->lastresx;
1976 int alloclen = targ->alloclen;
1977 int *targetmap = targ->targetmap;
1979 // fprintf( stderr, "thread %d start!\n", thread_no );
1981 effarr1 = AllocateDoubleVec( 1 );
1982 effarr2 = AllocateDoubleVec( 1 );
1983 mseq1 = AllocateCharMtx( njob, 0 );
1984 mseq2 = AllocateCharMtx( njob, 0 );
1987 dumseq1 = AllocateCharMtx( 1, alloclen+10 );
1988 dumseq2 = AllocateCharMtx( 1, alloclen+10 );
1990 distseq1 = AllocateCharMtx( 1, 0 );
1991 distseq2 = AllocateCharMtx( 1, 0 );
1992 aseq = AllocateCharMtx( 2, alloclen+10 );
1993 if( specificityconsideration > 0.0 ) dynamicmtx = AllocateDoubleMtx( nalphabets, nalphabets );
1995 if( alg == 'Y' || alg == 'r' ) ilim = njob - nadd;
1996 else ilim = njob - 1;
2001 pthread_mutex_lock( targ->mutex_counter );
2009 if( alg == 'Y' || alg == 'r' ) jst = njob - nadd;
2015 // fprintf( stderr, "thread %d end!\n", thread_no );
2016 pthread_mutex_unlock( targ->mutex_counter );
2018 if( commonIP ) FreeIntMtx( commonIP );
2020 if( commonJP ) FreeIntMtx( commonJP );
2022 Falign( NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, NULL, NULL, 0, NULL );
2023 G__align11( NULL, NULL, NULL, 0, 0, 0 ); // 20130603
2024 G__align11_noalign( NULL, 0, 0, NULL, NULL, 0 );
2025 L__align11( NULL, 0.0, NULL, NULL, 0, NULL, NULL );
2026 L__align11_noalign( NULL, NULL, NULL );
2027 genL__align11( NULL, NULL, NULL, 0, NULL, NULL );
2034 FreeCharMtx( dumseq1 );
2035 FreeCharMtx( dumseq2 );
2039 FreeCharMtx( aseq );
2040 if( dynamicmtx ) FreeDoubleMtx( dynamicmtx );
2046 pthread_mutex_unlock( targ->mutex_counter );
2049 // if( j == i+1 || j % 100 == 0 )
2050 if( j == i+1 && i % 10 == 0 )
2052 fprintf( stderr, "% 5d / %d (by thread %3d) \r", i, njob-nadd, thread_no );
2053 // fprintf( stderr, "% 5d - %5d / %d (thread %d)\n", i, j, njob, thread_no );
2057 if( strlen( seq[i] ) == 0 || strlen( seq[j] ) == 0 )
2061 if( alg == 'Y' || alg == 'r' ) distancemtx[i][j-(njob-nadd)] = 3.0;
2062 else distancemtx[i][j-i] = 3.0;
2066 pthread_mutex_lock( targ->mutex_stdout );
2067 fprintf( stdout, "%d %d d=%.3f\n", i+1, j+1, 3.0 );
2068 pthread_mutex_unlock( targ->mutex_stdout );
2073 strcpy( aseq[0], seq[i] );
2074 strcpy( aseq[1], seq[j] );
2075 // clus1 = conjuctionfortbfast( pair, i, aseq, mseq1, effarr1, effarr, indication1 );
2076 // clus2 = conjuctionfortbfast( pair, j, aseq, mseq2, effarr2, effarr, indication2 );
2077 // fprintf( stderr, "Skipping conjuction..\n" );
2084 thereisx = thereisxineachseq[i] + thereisxineachseq[j];
2085 // strcpy( distseq1[0], dseq[i] ); // nen no tame
2086 // strcpy( distseq2[0], dseq[j] ); // nen no tame
2087 distseq1[0] = dseq[i];
2088 distseq2[0] = dseq[j];
2090 // fprintf( stderr, "mseq1 = %s\n", mseq1[0] );
2091 // fprintf( stderr, "mseq2 = %s\n", mseq2[0] );
2094 fprintf( stderr, "group1 = %.66s", indication1 );
2095 fprintf( stderr, "\n" );
2096 fprintf( stderr, "group2 = %.66s", indication2 );
2097 fprintf( stderr, "\n" );
2099 // for( l=0; l<clus1; l++ ) fprintf( stderr, "## STEP-eff for mseq1-%d %f\n", l, effarr1[l] );
2103 pscore = Falign( NULL, NULL, n_dis_consweight_multi, mseq1, mseq2, effarr1, effarr2, NULL, NULL, 1, 1, alloclen, &intdum, NULL, 0, NULL );
2104 // fprintf( stderr, "pscore (fft) = %f\n", pscore );
2112 if( nadd && njob-nadd <= j && njob-nadd <= i ) // new sequence doushi ha mushi
2115 pscore = (double)lastresx[i][j].score; // all pair
2118 if( nadd == 0 || ( i < njob-nadd && njob-nadd <= j ) )
2119 pscore = (double)lastresx[i][j-(njob-nadd)].score;
2124 if( nadd && njob-nadd <= j && njob-nadd <= i ) // new sequence doushi ha mushi
2128 if( usenaivescoreinsteadofalignmentscore )
2130 L__align11( n_dis_consweight_multi, 0.0, mseq1, mseq2, alloclen, &off1, &off2 );
2131 pscore = (double)naivepairscore11( mseq1[0], mseq2[0], 0.0 ); // uwagaki
2135 // if( store_localhom )
2136 if( store_localhom && ( targetmap[i] != -1 || targetmap[j] != -1 ) )
2138 pscore = L__align11( n_dis_consweight_multi, 0.0, mseq1, mseq2, alloclen, &off1, &off2 );
2139 if( thereisx ) pscore = L__align11_noalign( n_dis_consweight_multi, distseq1, distseq2 ); // uwagaki
2141 if( specificityconsideration > 0.0 )
2143 dist = score2dist( pscore, selfscore[i], selfscore[j] );
2144 if( ( scoreoffset = dist2offset( dist ) ) < 0.0 )
2146 makedynamicmtx( dynamicmtx, n_dis_consweight_multi, 0.5 * dist ); // upgma ni awaseru.
2147 strcpy( mseq1[0], seq[i] );
2148 strcpy( mseq2[0], seq[j] );
2149 L__align11( dynamicmtx, scoreoffset, mseq1, mseq2, alloclen, &off1, &off2 );
2155 pscore = L__align11_noalign( n_dis_consweight_multi, distseq1, distseq2 );
2158 // pscore = G__align11_noalign( n_dis_consweight_multi, penalty, penalty_ex, distseq1, distseq2, alloclen ); // CHUUI!!!!!!
2161 if( nadd == 0 || ( i < njob-nadd && njob-nadd <= j ) ) // new sequence vs exiting sequence nomi keisan
2163 if( usenaivescoreinsteadofalignmentscore )
2165 L__align11( n_dis_consweight_multi, 0.0, mseq1, mseq2, alloclen, &off1, &off2 );
2166 pscore = (double)naivepairscore11( mseq1[0], mseq2[0], 0.0 ); // uwagaki
2170 if( store_localhom )
2172 pscore = L__align11( n_dis_consweight_multi, 0.0, mseq1, mseq2, alloclen, &off1, &off2 );
2173 if( thereisx ) pscore = L__align11_noalign( n_dis_consweight_multi, distseq1, distseq2 ); // uwagaki
2176 pscore = L__align11_noalign( n_dis_consweight_multi, distseq1, distseq2 );
2183 if( usenaivescoreinsteadofalignmentscore )
2185 G__align11( n_dis_consweight_multi, mseq1, mseq2, alloclen, outgap, outgap );
2186 pscore = (double)naivepairscore11( mseq1[0], mseq2[0], 0.0 ); // uwagaki
2190 // if( store_localhom )
2191 if( store_localhom && ( targetmap[i] != -1 || targetmap[j] != -1 ) )
2193 pscore = G__align11( n_dis_consweight_multi, mseq1, mseq2, alloclen, outgap, outgap );
2194 if( thereisx ) pscore = G__align11_noalign( n_dis_consweight_multi, penalty, penalty_ex, distseq1, distseq2, alloclen ); // uwagaki
2196 if( specificityconsideration > 0.0 )
2198 dist = score2dist( pscore, selfscore[i], selfscore[j] );
2199 // dist = score2dist( L__align11_noalign( n_dis_consweight_multi, distseq1, distseq2 ), selfscore[i], selfscore[j] ); // 2014/Feb/20
2200 if( dist2offset( dist ) < 0.0 )
2202 makedynamicmtx( dynamicmtx, n_dis_consweight_multi, 0.5 * dist ); // upgma ni awaseru.
2203 strcpy( mseq1[0], seq[i] );
2204 strcpy( mseq2[0], seq[j] );
2205 G__align11( dynamicmtx, mseq1, mseq2, alloclen, outgap, outgap );
2208 // pscore = (double)naivepairscore11( *mseq1, *mseq2, 0.0 );
2213 pscore = G__align11_noalign( n_dis_consweight_multi, penalty, penalty_ex, distseq1, distseq2, alloclen ); // uwagaki
2218 if( usenaivescoreinsteadofalignmentscore )
2220 genL__align11( n_dis_consweight_multi, mseq1, mseq2, alloclen, &off1, &off2 );
2221 pscore = (double)naivepairscore11( mseq1[0], mseq2[0], 0.0 ); // uwagaki
2225 // pscore = G__align11_noalign( n_dis_consweight_multi, penalty, penalty_ex, mseq1, mseq2, alloclen );
2226 pscore = genL__align11( n_dis_consweight_multi, mseq1, mseq2, alloclen, &off1, &off2 );
2229 strcpy( dumseq1[0], distseq1[0] );
2230 strcpy( dumseq2[0], distseq2[0] );
2231 pscore = genL__align11( n_dis_consweight_multi, dumseq1, dumseq2, alloclen, &dum1, &dum2 ); // uwagaki
2234 if( specificityconsideration > 0.0 )
2236 dist = score2dist( pscore, selfscore[i], selfscore[j] );
2237 if( dist2offset( dist ) < 0.0 )
2239 makedynamicmtx( dynamicmtx, n_dis_consweight_multi, 0.5 * dist ); // upgma ni awaseru.
2240 strcpy( mseq1[0], seq[i] );
2241 strcpy( mseq2[0], seq[j] );
2242 genL__align11( dynamicmtx, mseq1, mseq2, alloclen, &off1, &off2 );
2250 // pscore = G__align11_noalign( n_dis_consweight_multi, penalty, penalty_ex, mseq1, mseq2, alloclen );
2251 pscore = G__align11_noalign( n_dis_consweight_multi, penalty, penalty_ex, distseq1, distseq2, alloclen ); // tsuneni distseq shiyou
2254 pscore = callmxscarna_giving_bpp( mseq1, mseq2, bpp[i], bpp[j], alloclen, i, j );
2258 pscore = calldafs_giving_bpp( mseq1, mseq2, bpp[i], bpp[j], alloclen, i, j );
2263 pscore = Aalign( mseq1, mseq2, effarr1, effarr2, clus1, clus2, alloclen );
2267 pscore = genG__align11( n_dis_consweight_multi, mseq1, mseq2, alloclen );
2271 pscore = recallpairfoldalign( mseq1, mseq2, i, j, &off1, &off2, alloclen );
2275 pscore = recalllara( mseq1, mseq2, alloclen );
2279 pscore = MSalign11( mseq1, mseq2, alloclen );
2283 ErrorExit( "\n\nERROR IN SOURCE FILE\n\n" );
2287 if( alg == 't' || ( mseq1[0][0] != 0 && mseq2[0][0] != 0 ) ) // 't' no jouken ha iranai to omou. if( ( mseq1[0][0] != 0 && mseq2[0][0] != 0 ) )
2290 fprintf( stderr, "score = %10.2f (%d,%d)\n", pscore, i, j );
2292 // if( pscore > 0.0 && ( nadd == 0 || ( alg != 'Y' && alg != 'r' ) || ( i < njob-nadd && njob-nadd <= j ) ) ) x-ins-i de seido teika
2293 if( ( nadd == 0 || ( alg != 'Y' && alg != 'r' ) || ( i < njob-nadd && njob-nadd <= j ) ) )
2295 if( !store_localhom )
2297 else if( specifictarget && targetmap[i] == -1 && targetmap[j] == -1)
2299 else if( alg == 'R' )
2300 putlocalhom_last( mseq1[0], mseq2[0], localhomtable[i]+j, lastresx[i]+j, 'h' );
2301 else if( alg == 'r' )
2302 putlocalhom_last( mseq1[0], mseq2[0], localhomtable[i]+j-(njob-nadd), lastresx[i]+j-(njob-nadd), 'h' );// ?????
2303 else if( alg == 'H' )
2304 putlocalhom_ext( mseq1[0], mseq2[0], localhomtable[i]+j, off1, off2, (int)pscore, strlen( mseq1[0] ), 'h' );
2305 else if( alg == 'Y' )
2306 putlocalhom2( mseq1[0], mseq2[0], localhomtable[i]+j-(njob-nadd), off1, off2, (int)pscore, strlen( mseq1[0] ), 'h' );
2307 else if( !specifictarget && alg != 'S' && alg != 'V' )
2308 putlocalhom2( mseq1[0], mseq2[0], localhomtable[i]+j-i, off1, off2, (int)pscore, strlen( mseq1[0] ), 'h' );
2310 // putlocalhom2( mseq1[0], mseq2[0], localhomtable[i]+j, off1, off2, (int)pscore, strlen( mseq1[0] ) );
2312 if( targetmap[i] != -1 && targetmap[j] != -1 )
2314 putlocalhom2( mseq2[0], mseq1[0], localhomtable[targetmap[j]]+i, off2, off1, (int)pscore, strlen( mseq2[0] ), 'h' );
2315 putlocalhom2( mseq1[0], mseq2[0], localhomtable[targetmap[i]]+j, off1, off2, (int)pscore, strlen( mseq1[0] ), 'h' ); // sukoshi muda.
2317 else if( targetmap[j] != -1 )
2318 putlocalhom2( mseq2[0], mseq1[0], localhomtable[targetmap[j]]+i, off2, off1, (int)pscore, strlen( mseq2[0] ), 'h' );
2319 else if( targetmap[i] != -1 )
2320 putlocalhom2( mseq1[0], mseq2[0], localhomtable[targetmap[i]]+j, off1, off2, (int)pscore, strlen( mseq1[0] ), 'h' );
2322 if( targetmap[i] != -1 )
2323 putlocalhom2( mseq1[0], mseq2[0], localhomtable[targetmap[i]]+j, off1, off2, (int)pscore, strlen( mseq1[0] ), 'h' );
2325 else if( targetmap[j] != -1 )
2326 putlocalhom2( mseq2[0], mseq1[0], localhomtable[targetmap[j]]+i, off2, off1, (int)pscore, strlen( mseq2[0] ), 'h' );
2330 reporterr( "okashii\n" );
2335 pscore = score2dist( pscore, selfscore[i], selfscore[j] );
2337 // pscore = (double)naivepairscore11( mseq1[0], mseq2[0], 0.0 );
2338 // pscore = score2dist( pscore, selfscore[i], selfscore[j] );
2339 // reporterr( "->pscore = %f\n", pscore );
2350 pthread_mutex_lock( targ->mutex_stdout );
2353 fprintf( stdout, "sequence %d - sequence %d, pairwise distance = %10.5f\n", i+1, j+1, pscore );
2354 fprintf( stdout, ">%s\n", name[i] );
2355 write1seq( stdout, mseq1[0] );
2356 fprintf( stdout, ">%s\n", name[j] );
2357 write1seq( stdout, mseq2[0] );
2358 fprintf( stdout, "\n" );
2360 pthread_mutex_unlock( targ->mutex_stdout );
2364 pthread_mutex_lock( targ->mutex_stdout );
2365 if( j == i+1 ) fprintf( stdout, "%d %d d=%.3f\n", i+1, i+1, 0.0 );
2366 fprintf( stdout, "%d %d d=%.3f\n", i+1, j+1, pscore );
2367 pthread_mutex_unlock( targ->mutex_stdout );
2372 if( alg == 'Y' || alg == 'r' ) distancemtx[i][j-(njob-nadd)] = pscore;
2373 else distancemtx[i][j-i] = pscore;
2379 static void pairalign( char **name, int *nlen, char **seq, char **aseq, char **dseq, int *thereisxineachseq, char **mseq1, char **mseq2, int alloclen, Lastresx **lastresx, double **distancemtx, LocalHom **localhomtable, int ngui )
2381 int i, j, ilim, jst, jj;
2382 int off1, off2, dum1, dum2, thereisx;
2383 double pscore = 0.0; // by D.Mathog
2384 FILE *hat2p, *hat3p;
2385 // double **distancemtx;
2390 char *hat2file = "hat2";
2391 // LocalHom **localhomtable = NULL,
2394 char ***bpp = NULL; // mxscarna no toki dake
2395 char **distseq1, **distseq2;
2396 char **dumseq1, **dumseq2;
2400 int *targetmap, *targetmapr;
2403 if( specifictarget )
2405 targetmap = calloc( njob, sizeof( int ) );
2407 for( i=0; i<njob; i++ )
2410 if( !strncmp( name[i]+1, "_focus_", 7 ) )
2411 targetmap[i] = ntarget++;
2413 targetmapr = calloc( ntarget, sizeof( int ) );
2414 for( i=0; i<njob; i++ )
2415 if( targetmap[i] != -1 ) targetmapr[targetmap[i]] = i;
2419 reporterr( "\n\nAdd '>_focus_' to the title lines of the sequences to be focused on.\n\n" );
2424 reporterr( "nfocus = %d \n", ntarget );
2430 targetmap = calloc( njob, sizeof( int ) );
2431 targetmapr = calloc( njob, sizeof( int ) );
2432 for( i=0; i<njob; i++ )
2433 targetmap[i] = targetmapr[i] = i;
2437 for( i=0; i<njob; i++ )
2438 reporterr( "targetmap[%d] = %d\n", i, targetmap[i] );
2439 for( i=0; i<ntarget; i++ )
2440 reporterr( "targetmapr[%d] = %d\n", i, targetmapr[i] );
2443 if( store_localhom && localhomtable == NULL )
2445 if( alg == 'Y' || alg == 'r' )
2455 localhomtable = (LocalHom **)calloc( ilim, sizeof( LocalHom *) );
2456 for( i=0; i<ilim; i++)
2458 localhomtable[i] = (LocalHom *)calloc( jst, sizeof( LocalHom ) );
2459 for( j=0; j<jst; j++)
2461 localhomtable[i][j].start1 = -1;
2462 localhomtable[i][j].end1 = -1;
2463 localhomtable[i][j].start2 = -1;
2464 localhomtable[i][j].end2 = -1;
2465 localhomtable[i][j].opt = -1.0;
2466 localhomtable[i][j].next = NULL;
2467 localhomtable[i][j].nokori = 0;
2468 localhomtable[i][j].extended = -1;
2469 localhomtable[i][j].last = localhomtable[i]+j;
2470 localhomtable[i][j].korh = 'h';
2472 if( !specifictarget && alg != 'Y' && alg != 'r' ) jst--;
2480 if( alg == 'Y' || alg == 'r' )
2481 distancemtx = AllocateDoubleMtx( njob, nadd );
2483 distancemtx = AllocateDoubleHalfMtx( njob );
2484 // distancemtx = AllocateDoubleMtx( njob, njob );
2487 else distancemtx = NULL;
2491 dumseq1 = AllocateCharMtx( 1, alloclen+10 );
2492 dumseq2 = AllocateCharMtx( 1, alloclen+10 );
2494 distseq1 = AllocateCharMtx( 1, 0 ); // muda
2495 distseq2 = AllocateCharMtx( 1, 0 ); // muda
2497 selfscore = AllocateDoubleVec( njob );
2498 effarr1 = AllocateDoubleVec( njob );
2499 effarr2 = AllocateDoubleVec( njob );
2502 fprintf( stderr, "##### fftwinsize = %d, fftthreshold = %d\n", fftWinSize, fftThreshold );
2506 for( i=0; i<njob; i++ )
2507 fprintf( stderr, "TBFAST effarr[%d] = %f\n", i, effarr[i] );
2511 // writePre( njob, name, nlen, aseq, 0 );
2513 reporterr( "All-to-all alignment.\n" );
2516 fprintf( stderr, "Calling last (http://last.cbrc.jp/)\n" );
2518 calllast_once( njob, seq, njob, seq, lastresx );
2520 calllast_fast( njob, seq, njob, seq, lastresx );
2521 fprintf( stderr, "done.\n" );
2522 // nthread = 0; // igo multithread nashi
2526 fprintf( stderr, "Calling last (http://last.cbrc.jp/)\n" );
2527 fprintf( stderr, "nadd=%d\n", nadd );
2528 #if 1 // last_fast ha, lastdb ga muda
2530 calllast_once( njob-nadd, seq, nadd, seq+njob-nadd, lastresx );
2532 calllast_fast( njob-nadd, seq, nadd, seq+njob-nadd, lastresx );
2534 calllast_once( njob-nadd, seq, nadd, seq+njob-nadd, lastresx );
2537 fprintf( stderr, "nadd=%d\n", nadd );
2538 fprintf( stderr, "done.\n" );
2539 // nthread = 0; // igo multithread nashi
2544 fprintf( stderr, "Calling FOLDALIGN with option '%s'\n", foldalignopt );
2545 callfoldalign( njob, seq );
2546 fprintf( stderr, "done.\n" );
2550 fprintf( stderr, "Running LARA (Bauer et al. http://www.planet-lisa.net/)\n" );
2551 calllara( njob, seq, "" );
2552 fprintf( stderr, "done.\n" );
2556 fprintf( stderr, "Running SLARA (Bauer et al. http://www.planet-lisa.net/)\n" );
2557 calllara( njob, seq, "-s" );
2558 fprintf( stderr, "done.\n" );
2562 fprintf( stderr, "Preparing bpp\n" );
2563 // bpp = AllocateCharCub( njob, nlenmax, 0 );
2564 bpp = calloc( njob, sizeof( char ** ) );
2565 preparebpp( njob, bpp );
2566 fprintf( stderr, "done.\n" );
2567 fprintf( stderr, "Running MXSCARNA (Tabei et al. http://www.ncrna.org/software/mxscarna)\n" );
2571 fprintf( stderr, "Preparing bpp\n" );
2572 // bpp = AllocateCharCub( njob, nlenmax, 0 );
2573 bpp = calloc( njob, sizeof( char ** ) );
2574 preparebpp( njob, bpp );
2575 fprintf( stderr, "done.\n" );
2576 fprintf( stderr, "Running DAFS (Sato et al. http://www.ncrna.org/)\n" );
2579 for( i=0; i<njob; i++ )
2582 for( pt=seq[i]; *pt; pt++ )
2583 pscore += amino_dis[(unsigned char)*pt][(unsigned char)*pt];
2584 selfscore[i] = pscore;
2585 // fprintf( stderr, "selfscore[%d] = %f\n", i, selfscore[i] );
2588 #if enablemultithread
2589 if( nthread > 0 ) // alg=='r' || alg=='R' -> nthread:=0 (sukoshi ue)
2593 pthread_mutex_t mutex_counter;
2594 pthread_mutex_t mutex_stdout;
2597 if( alg == 'Y' || alg == 'r' ) jobpos.j = njob - nadd - 1;
2601 targ = calloc( nthread, sizeof( thread_arg_t ) );
2602 handle = calloc( nthread, sizeof( pthread_t ) );
2603 pthread_mutex_init( &mutex_counter, NULL );
2604 pthread_mutex_init( &mutex_stdout, NULL );
2606 for( i=0; i<nthread; i++ )
2608 targ[i].thread_no = i;
2609 targ[i].njob = njob;
2610 targ[i].jobpospt = &jobpos;
2611 targ[i].name = name;
2613 targ[i].dseq = dseq;
2614 targ[i].thereisxineachseq = thereisxineachseq;
2615 targ[i].localhomtable = localhomtable;
2616 targ[i].distancemtx = distancemtx;
2617 targ[i].selfscore = selfscore;
2619 targ[i].lastresx = lastresx;
2620 targ[i].alloclen = alloclen;
2621 targ[i].targetmap = targetmap;
2622 targ[i].mutex_counter = &mutex_counter;
2623 targ[i].mutex_stdout = &mutex_stdout;
2625 // athread( (void *)targ );
2626 pthread_create( handle+i, NULL, athread, (void *)(targ+i) );
2627 // pthread_create( handle+i, NULL, bthread, (void *)(targ+i) );
2631 for( i=0; i<nthread; i++ )
2633 pthread_join( handle[i], NULL );
2635 pthread_mutex_destroy( &mutex_counter );
2636 pthread_mutex_destroy( &mutex_stdout );
2643 double **dynamicmtx = NULL;
2644 if( specificityconsideration > 0.0 ) dynamicmtx = AllocateDoubleMtx( nalphabets, nalphabets );
2646 if( alg == 'Y' || alg == 'r' ) ilim = njob - nadd;
2647 else ilim = njob - 1;
2648 for( i=0; i<ilim; i++ )
2650 if( stdout_dist) fprintf( stdout, "%d %d d=%.3f\n", i+1, i+1, 0.0 );
2651 fprintf( stderr, "% 5d / %d\r", i, njob-nadd );
2654 if( alg == 'Y' || alg == 'r' ) jst = njob - nadd;
2656 for( j=jst; j<njob; j++ )
2659 if( strlen( seq[i] ) == 0 || strlen( seq[j] ) == 0 )
2663 if( alg == 'Y' || alg == 'r' ) distancemtx[i][j-(njob-nadd)] = 3.0;
2664 else distancemtx[i][j-i] = 3.0;
2666 if( stdout_dist) fprintf( stdout, "%d %d d=%.3f\n", i+1, j+1, 3.0 );
2670 strcpy( aseq[0], seq[i] );
2671 strcpy( aseq[1], seq[j] );
2672 // clus1 = conjuctionfortbfast( pair, i, aseq, mseq1, effarr1, effarr, indication1 );
2673 // clus2 = conjuctionfortbfast( pair, j, aseq, mseq2, effarr2, effarr, indication2 );
2674 // fprintf( stderr, "Skipping conjuction..\n" );
2681 thereisx = thereisxineachseq[i] + thereisxineachseq[j];
2682 // strcpy( distseq1[0], dseq[i] ); // nen no tame
2683 // strcpy( distseq2[0], dseq[j] ); // nen no tame
2684 distseq1[0] = dseq[i];
2685 distseq2[0] = dseq[j];
2687 // fprintf( stderr, "mseq1 = %s\n", mseq1[0] );
2688 // fprintf( stderr, "mseq2 = %s\n", mseq2[0] );
2691 fprintf( stderr, "group1 = %.66s", indication1 );
2692 fprintf( stderr, "\n" );
2693 fprintf( stderr, "group2 = %.66s", indication2 );
2694 fprintf( stderr, "\n" );
2696 // for( l=0; l<clus1; l++ ) fprintf( stderr, "## STEP-eff for mseq1-%d %f\n", l, effarr1[l] );
2700 pscore = Falign( NULL, NULL, n_dis_consweight_multi, mseq1, mseq2, effarr1, effarr2, NULL, NULL, 1, 1, alloclen, &intdum, NULL, 0, NULL );
2701 // fprintf( stderr, "pscore (fft) = %f\n", pscore );
2709 // pscore = G__align11_noalign( n_dis_consweight_multi, penalty, penalty_ex, mseq1, mseq2, alloclen );
2710 pscore = G__align11_noalign( n_dis_consweight_multi, penalty, penalty_ex, distseq1, distseq2, alloclen ); // tsuneni distseq shiyou
2714 if( usenaivescoreinsteadofalignmentscore )
2716 G__align11( n_dis_consweight_multi, mseq1, mseq2, alloclen, outgap, outgap );
2717 pscore = (double)naivepairscore11( mseq1[0], mseq2[0], 0.0 ); // uwagaki
2721 // if( store_localhom )
2722 if( store_localhom && ( targetmap[i] != -1 || targetmap[j] != -1 ) )
2724 pscore = G__align11( n_dis_consweight_multi, mseq1, mseq2, alloclen, outgap, outgap );
2725 if( thereisx ) pscore = G__align11_noalign( n_dis_consweight_multi, penalty, penalty_ex, distseq1, distseq2, alloclen ); // uwagaki
2727 if( specificityconsideration > 0.0 )
2729 dist = score2dist( pscore, selfscore[i], selfscore[j] );
2730 // dist = score2dist( L__align11_noalign( n_dis_consweight_multi, distseq1, distseq2 ), selfscore[i], selfscore[j] ); // 2014/Feb/20
2731 if( dist2offset( dist ) < 0.0 )
2733 makedynamicmtx( dynamicmtx, n_dis_consweight_multi, 0.5 * dist ); // upgma ni awaseru.
2734 strcpy( mseq1[0], seq[i] );
2735 strcpy( mseq2[0], seq[j] );
2736 G__align11( dynamicmtx, mseq1, mseq2, alloclen, outgap, outgap );
2738 // pscore = (double)naivepairscore11( *mseq1, *mseq2, 0.0 );
2743 pscore = G__align11_noalign( n_dis_consweight_multi, penalty, penalty_ex, distseq1, distseq2, alloclen ); // uwagaki
2748 // pscore = G__align11_noalign( n_dis_consweight_multi, penalty, penalty_ex, mseq1, mseq2, alloclen );
2749 if( usenaivescoreinsteadofalignmentscore )
2751 genL__align11( n_dis_consweight_multi, mseq1, mseq2, alloclen, &off1, &off2 );
2752 pscore = (double)naivepairscore11( mseq1[0], mseq2[0], 0.0 ); // uwagaki
2756 pscore = genL__align11( n_dis_consweight_multi, mseq1, mseq2, alloclen, &off1, &off2 );
2759 strcpy( dumseq1[0], distseq1[0] );
2760 strcpy( dumseq2[0], distseq2[0] );
2761 pscore = genL__align11( n_dis_consweight_multi, dumseq1, dumseq2, alloclen, &dum1, &dum2 ); // uwagaki
2764 if( specificityconsideration > 0.0 )
2766 // fprintf( stderr, "dist = %f\n", score2dist( pscore, selfscore[i], selfscore[j] ) );
2767 dist = score2dist( pscore, selfscore[i], selfscore[j] );
2768 if( dist2offset( dist ) < 0.0 )
2770 makedynamicmtx( dynamicmtx, n_dis_consweight_multi, 0.5 * dist ); // upgma ni awaseru.
2771 strcpy( mseq1[0], seq[i] );
2772 strcpy( mseq2[0], seq[j] );
2773 genL__align11( dynamicmtx, mseq1, mseq2, alloclen, &off1, &off2 );
2780 if( nadd && njob-nadd <= j && njob-nadd <= i ) // new sequence doushi ha mushi
2783 pscore = (double)lastresx[i][j].score; // all pair
2786 if( nadd == 0 || ( i < njob-nadd && njob-nadd <= j ) )
2787 pscore = (double)lastresx[i][j-(njob-nadd)].score;
2792 if( nadd && njob-nadd <= j && njob-nadd <= i ) // new sequence doushi ha mushi
2796 if( usenaivescoreinsteadofalignmentscore )
2798 L__align11( n_dis_consweight_multi, 0.0, mseq1, mseq2, alloclen, &off1, &off2 );
2799 pscore = (double)naivepairscore11( mseq1[0], mseq2[0], 0.0 ); // uwagaki
2803 // if( store_localhom )
2804 if( store_localhom && ( targetmap[i] != -1 || targetmap[j] != -1 ) )
2806 pscore = L__align11( n_dis_consweight_multi, 0.0, mseq1, mseq2, alloclen, &off1, &off2 ); // all pair
2807 if( thereisx ) pscore = L__align11_noalign( n_dis_consweight_multi, distseq1, distseq2 ); // all pair
2809 if( specificityconsideration > 0.0 )
2811 dist = score2dist( pscore, selfscore[i], selfscore[j] );
2812 if( ( scoreoffset = dist2offset( dist ) ) < 0.0 )
2814 makedynamicmtx( dynamicmtx, n_dis_consweight_multi, 0.5 * dist ); // upgma ni awaseru.
2815 strcpy( mseq1[0], seq[i] );
2816 strcpy( mseq2[0], seq[j] );
2817 L__align11( dynamicmtx, scoreoffset, mseq1, mseq2, alloclen, &off1, &off2 );
2823 pscore = L__align11_noalign( n_dis_consweight_multi, distseq1, distseq2 ); // all pair
2826 // pscore = G__align11_noalign( n_dis_consweight_multi, penalty, penalty_ex, distseq1, distseq2, alloclen ); // CHUUI!!!!!!
2829 if( nadd == 0 || ( i < njob-nadd && njob-nadd <= j ) ) // new sequence vs exiting sequence nomi keisan
2831 if( usenaivescoreinsteadofalignmentscore )
2833 L__align11( n_dis_consweight_multi, 0.0, mseq1, mseq2, alloclen, &off1, &off2 );
2834 pscore = (double)naivepairscore11( mseq1[0], mseq2[0], 0.0 ); // uwagaki
2838 if( store_localhom )
2840 pscore = L__align11( n_dis_consweight_multi, 0.0, mseq1, mseq2, alloclen, &off1, &off2 );
2841 if( thereisx ) pscore = L__align11_noalign( n_dis_consweight_multi, distseq1, distseq2 ); // uwagaki
2844 pscore = L__align11_noalign( n_dis_consweight_multi, distseq1, distseq2 );
2851 pscore = Aalign( mseq1, mseq2, effarr1, effarr2, 1, 1, alloclen );
2856 pscore = genG__align11( mseq1, mseq2, alloclen );
2861 pscore = recallpairfoldalign( mseq1, mseq2, i, j, &off1, &off2, alloclen );
2865 pscore = recalllara( mseq1, mseq2, alloclen );
2869 pscore = callmxscarna_giving_bpp( mseq1, mseq2, bpp[i], bpp[j], alloclen, i, j );
2873 pscore = calldafs_giving_bpp( mseq1, mseq2, bpp[i], bpp[j], alloclen, i, j );
2877 pscore = MSalign11( mseq1, mseq2, alloclen );
2880 ErrorExit( "ERROR IN SOURCE FILE" );
2884 if( alg == 't' || ( mseq1[0][0] != 0 && mseq2[0][0] != 0 ) ) // 't' no jouken ha iranai to omou. if( ( mseq1[0][0] != 0 && mseq2[0][0] != 0 ) )
2887 fprintf( stderr, "score = %10.2f (%d,%d)\n", pscore, i, j );
2889 // if( pscore > 0.0 && ( nadd == 0 || ( alg != 'Y' && alg != 'r' ) || ( i < njob-nadd && njob-nadd <= j ) ) ) // x-ins-i de seido teika
2890 if( ( nadd == 0 || ( alg != 'Y' && alg != 'r' ) || ( i < njob-nadd && njob-nadd <= j ) ) )
2892 if( !store_localhom )
2894 else if( specifictarget && targetmap[i] == -1 && targetmap[j] == -1)
2896 else if( alg == 'R' )
2897 putlocalhom_last( mseq1[0], mseq2[0], localhomtable[i]+j, lastresx[i]+j, 'h' );
2898 else if( alg == 'r' )
2899 putlocalhom_last( mseq1[0], mseq2[0], localhomtable[i]+j-(njob-nadd), lastresx[i]+j-(njob-nadd), 'h' );// ?????
2900 else if( alg == 'H' )
2901 putlocalhom_ext( mseq1[0], mseq2[0], localhomtable[i]+j, off1, off2, (int)pscore, strlen( mseq1[0] ), 'h' );
2902 else if( alg == 'Y' )
2903 putlocalhom2( mseq1[0], mseq2[0], localhomtable[i]+j-(njob-nadd), off1, off2, (int)pscore, strlen( mseq1[0] ), 'h' );
2904 else if( !specifictarget && alg != 'S' && alg != 'V' )
2905 putlocalhom2( mseq1[0], mseq2[0], localhomtable[i]+j-i, off1, off2, (int)pscore, strlen( mseq1[0] ), 'h' );
2908 if( targetmap[i] != -1 && targetmap[j] != -1 )
2910 putlocalhom2( mseq1[0], mseq2[0], localhomtable[targetmap[i]]+j, off1, off2, (int)pscore, strlen( mseq1[0] ), 'h' );
2911 putlocalhom2( mseq2[0], mseq1[0], localhomtable[targetmap[j]]+i, off2, off1, (int)pscore, strlen( mseq2[0] ), 'h' ); // sukoshi muda.
2913 else if( targetmap[j] != -1 )
2914 putlocalhom2( mseq2[0], mseq1[0], localhomtable[targetmap[j]]+i, off2, off1, (int)pscore, strlen( mseq2[0] ), 'h' );
2915 else if( targetmap[i] != -1 )
2916 putlocalhom2( mseq1[0], mseq2[0], localhomtable[targetmap[i]]+j, off1, off2, (int)pscore, strlen( mseq1[0] ), 'h' );
2919 reporterr( "okashii\n" );
2925 pscore = score2dist( pscore, selfscore[i], selfscore[j] );
2936 fprintf( stdout, "sequence %d - sequence %d, pairwise distance = %10.5f\n", i+1, j+1, pscore );
2937 fprintf( stdout, ">%s\n", name[i] );
2938 write1seq( stdout, mseq1[0] );
2939 fprintf( stdout, ">%s\n", name[j] );
2940 write1seq( stdout, mseq2[0] );
2941 fprintf( stdout, "\n" );
2944 if( stdout_dist ) fprintf( stdout, "%d %d d=%.3f\n", i+1, j+1, pscore );
2947 if( alg == 'Y' || alg == 'r' ) distancemtx[i][j-(njob-nadd)] = pscore;
2948 else distancemtx[i][j-i] = pscore;
2952 if( dynamicmtx ) FreeDoubleMtx( dynamicmtx );
2956 if( store_dist && ngui == 0 )
2958 hat2p = fopen( hat2file, "w" );
2959 if( !hat2p ) ErrorExit( "Cannot open hat2." );
2960 if( alg == 'Y' || alg == 'r' )
2961 WriteHat2_part_pointer( hat2p, njob, nadd, name, distancemtx );
2963 // WriteHat2_pointer( hat2p, njob, name, distancemtx );
2964 WriteFloatHat2_pointer_halfmtx( hat2p, njob, name, distancemtx ); // jissiha double
2968 hat3p = fopen( "hat3", "w" );
2969 if( !hat3p ) ErrorExit( "Cannot open hat3." );
2970 if( store_localhom && ngui == 0 )
2973 fprintf( stderr, "\n\n##### writing hat3\n" );
2974 if( alg == 'Y' || alg == 'r' )
2976 else if( specifictarget )
2980 for( i=0; i<ilim; i++ )
2982 if( alg == 'Y' || alg == 'r' )
2987 else if( specifictarget )
2997 for( j=jst; j<njob; j++, jj++ )
2999 for( tmpptr=localhomtable[i]+jj; tmpptr; tmpptr=tmpptr->next )
3001 // fprintf( stderr, "j=%d, jj=%d\n", j, jj );
3002 if( tmpptr->opt == -1.0 ) continue;
3004 // if( alg == 'B' || alg == 'T' )
3005 // fprintf( hat3p, "%d %d %d %7.5f %d %d %d %d %p\n", i, j, tmpptr->overlapaa, 1.0, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2, (void *)tmpptr->next );
3007 if( targetmap[j] == -1 || targetmap[i] < targetmap[j] )
3008 fprintf( hat3p, "%d %d %d %7.5f %d %d %d %d h\n", targetmapr[i], j, tmpptr->overlapaa, tmpptr->opt, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2 );
3009 // fprintf( hat3p, "%d %d %d %7.5f %d %d %d %d h\n", i, j, tmpptr->overlapaa, tmpptr->opt, tmpptr->start1, tmpptr->end1, tmpptr->start2+1, tmpptr->end2+1 ); // zettai dame!!!!
3016 fprintf( stderr, "calling FreeLocalHomTable\n" );
3018 if( alg == 'Y' || alg == 'r' )
3019 FreeLocalHomTable_part( localhomtable, (njob-nadd), nadd );
3020 else if( specifictarget )
3021 FreeLocalHomTable_part( localhomtable, ntarget, njob );
3023 FreeLocalHomTable_half( localhomtable, njob );
3025 fprintf( stderr, "done. FreeLocalHomTable\n" );
3034 for( i=0; i<njob; i++ )
3039 if( *ptpt ) free( *ptpt );
3052 FreeCharMtx( dumseq1 );
3053 FreeCharMtx( dumseq2 );
3057 if( store_dist && ngui == 0 ) FreeDoubleHalfMtx( distancemtx, njob );
3064 int pairlocalalign( int ngui, int lgui, char **namegui, char **seqgui, double **distancemtx, LocalHom **localhomtable, int argc, char **argv )
3066 int *nlen, *thereisxineachseq;
3068 char **mseq1, **mseq2;
3076 Lastresx **lastresx;
3078 // reporterr( "argc=%d, argv[0]=%s\n", argc, argv[0] );
3080 arguments( argc, argv );
3087 infp = fopen( inputfile, "r" );
3090 fprintf( stderr, "Cannot open %s\n", inputfile );
3102 fprintf( stderr, "At least 2 sequences should be input!\n"
3103 "Only %d sequence found.\n", njob );
3108 fprintf( stderr, "The number of sequences must be < %d\n", M );
3109 fprintf( stderr, "Please try the splittbfast program for such large data.\n" );
3114 if( ( alg == 'r' || alg == 'R' ) && dorp == 'p' )
3116 fprintf( stderr, "Not yet supported\n" );
3120 alloclen = nlenmax*2;
3128 seq = AllocateCharMtx( njob, alloclen+10 );
3129 name = AllocateCharMtx( njob, B );
3132 aseq = AllocateCharMtx( 2, alloclen+10 );
3133 bseq = AllocateCharMtx( njob, alloclen+10 );
3134 dseq = AllocateCharMtx( njob, alloclen+10 );
3135 mseq1 = AllocateCharMtx( njob, 0 );
3136 mseq2 = AllocateCharMtx( njob, 0 );
3137 nlen = AllocateIntVec( njob );
3138 thereisxineachseq = AllocateIntVec( njob );
3143 lastresx = calloc( njob+1, sizeof( Lastresx * ) );
3144 for( i=0; i<njob; i++ )
3146 lastresx[i] = calloc( njob+1, sizeof( Lastresx ) ); // muda
3147 for( j=0; j<njob; j++ )
3149 lastresx[i][j].score = 0;
3150 lastresx[i][j].naln = 0;
3151 lastresx[i][j].aln = NULL;
3153 lastresx[i][njob].naln = -1;
3155 lastresx[njob] = NULL;
3157 else if( alg == 'r' )
3159 // fprintf( stderr, "Allocating lastresx (%d), njob=%d, nadd=%d\n", njob-nadd+1, njob, nadd );
3160 lastresx = calloc( njob-nadd+1, sizeof( Lastresx * ) );
3161 for( i=0; i<njob-nadd; i++ )
3163 // fprintf( stderr, "Allocating lastresx[%d]\n", i );
3164 lastresx[i] = calloc( nadd+1, sizeof( Lastresx ) );
3165 for( j=0; j<nadd; j++ )
3167 // fprintf( stderr, "Initializing lastresx[%d][%d]\n", i, j );
3168 lastresx[i][j].score = 0;
3169 lastresx[i][j].naln = 0;
3170 lastresx[i][j].aln = NULL;
3172 lastresx[i][nadd].naln = -1;
3174 lastresx[njob-nadd] = NULL;
3180 Read( name, nlen, seq );
3184 readData_pointer( infp, name, nlen, seq );
3189 constants( njob, seq );
3192 fprintf( stderr, "params = %d, %d, %d\n", penalty, penalty_ex, offset );
3199 // WriteOptions( trap_g );
3201 c = seqcheck( seq );
3204 fprintf( stderr, "Illegal character %c\n", c );
3208 // writePre( njob, name, nlen, seq, 0 );
3213 for( i=0; i<njob; i++ )
3215 gappick0( bseq[i], seq[i] );
3216 thereisxineachseq[i] = removex( dseq[i], bseq[i] );
3219 pairalign( name, nlen, bseq, aseq, dseq, thereisxineachseq, mseq1, mseq2, alloclen, lastresx, distancemtx, localhomtable, ngui );
3221 fprintf( trap_g, "done.\n" );
3223 fprintf( stderr, "closing trap_g\n" );
3228 // writePre( njob, name, nlen, aseq, !contin );
3230 writeData( stdout, njob, name, nlen, aseq );
3233 fprintf( stderr, "OSHIMAI\n" );
3237 if( stdout_dist && nthread > 1 )
3239 fprintf( stderr, "\nThe order of distances is not identical to that in the input file, because of the parallel calculation. Reorder them by yourself, using sort -n -k 2 | sort -n -k 1 -s\n" );
3241 if( stdout_align && nthread > 1 )
3243 fprintf( stderr, "\nThe order of pairwise alignments is not identical to that in the input file, because of the parallel calculation. Reorder them by yourself.\n" );
3249 for( i=0; lastresx[i]; i++ )
3251 for( j=0; lastresx[i][j].naln!=-1; j++ )
3253 for( k=0; k<lastresx[i][j].naln; k++ )
3255 free( lastresx[i][j].aln[k].reg1 );
3256 free( lastresx[i][j].aln[k].reg2 );
3258 free( lastresx[i][j].aln );
3260 free( lastresx[i] );
3268 FreeCharMtx( name );
3270 FreeCharMtx( aseq );
3271 FreeCharMtx( bseq );
3272 FreeCharMtx( dseq );
3276 free( thereisxineachseq );
3283 Falign( NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, NULL, NULL, 0, NULL );
3284 G__align11( NULL, NULL, NULL, 0, 0, 0 ); // 20130603
3285 G__align11_noalign( NULL, 0, 0, NULL, NULL, 0 );
3286 L__align11( NULL, 0.0, NULL, NULL, 0, NULL, NULL );
3287 L__align11_noalign( NULL, NULL, NULL );
3288 genL__align11( NULL, NULL, NULL, 0, NULL, NULL );
3294 for( i=0; i<njob-1; i++ ) for( j=i+1; j<njob; j++ )
3296 sprintf( buf, "%5.3f", distancemtx[i][j-i] );
3297 distancemtx[i][j-i] = 0.0;
3298 sscanf( buf, "%lf", distancemtx[i]+j-i );
3299 // distancemtx[i][j-i] = 0.001 * (int)(distancemtx[i][j-i] * 1000 + 0.5);