8 static char *whereispairalign;
10 static int *alreadyoutput;
11 static int equivthreshold;
12 static int equivwinsize;
13 static int equivshortestlen;
15 static void cutpath( char *s )
18 pos = s + strlen( s );
22 if( *pos == '/' ) break;
28 static char getchainid( char *s )
31 if( isspace( s[0] ) && isalnum( s[1] ) )
37 static void extractfirstword( char *s )
41 if( isspace( *s ) ) break;
47 static char *strip( char *s )
53 if( !isspace( *s ) ) break;
73 static void makeequivdouble( double *d, char *c )
77 *d++ = (double)( *c++ - '0' );
81 static void maskequiv( double *d, int n )
87 halfwin = (int)( equivwinsize / 2 );
92 for( j = i-halfwin; j<i+halfwin; j++ )
94 if( j<0 || n=<j ) continue;
101 if( ok == 0 ) d[i] = 0.0;
105 static void maskequiv( double *d, int n )
110 double *dbk, *dori, *dbkori;
112 dbk = calloc( n, sizeof( double ) );
137 if( len >= equivshortestlen )
140 while( len-- ) d[i-len] = dbk[i-len];
146 if( len >= equivshortestlen )
149 while( len-- ) d[n-len] = dbk[n-len];
156 static void makeequivdouble_threshold( double *d, char *c, int n )
164 tmpi = (int)( *c++ - '0' );
165 tmpd = (double)( tmpi + 1 - equivthreshold ) / ( 10 - equivthreshold ) * 9.0;
166 if( tmpd < 0.0 ) tmpd = 0.0;
175 static void readrash( FILE *fp, char *seq1, char *seq2, double *equiv )
178 static char *equivchar = NULL;
182 if( equivchar == NULL )
184 equivchar = calloc( nlenmax * 2, sizeof( char ) );
192 fgets( line, 999, fp );
193 // fprintf( stdout, "line = :%s:\n", line );
194 if( !strncmp( line, "Query ", 6 ) ) break;
195 if( feof( fp ) == 1 ) break;
196 if( !strncmp( line, "QUERY ", 6 ) )
198 strcat( seq1, strip( line+5 ) );
199 fgets( line, 999, fp );
201 if( !strncmp( line, "TEMPL ", 6 ) )
203 strcat( seq2, strip( line+5 ) );
204 fgets( line, 999, fp );
206 if( !strncmp( line, "Equiva", 6 ) )
208 strcat( equivchar, strip( line+11 ) );
209 fgets( line, 999, fp );
213 printf( "seq1=:%s:\n", seq1 );
214 printf( "seq2=:%s:\n", seq2 );
215 printf( "equi=:%s:\n", equivchar );
219 makeequivdouble_threshold( equiv, equivchar, n );
222 fprintf( stdout, "\n" );
225 fprintf( stdout, "%1.0f", equiv[i] );
227 fprintf( stdout, "\n" );
231 static int checkcbeta( FILE *fp )
236 fgets( linec, 999, fp );
237 if( feof( fp ) == 1 ) break;
238 if( !strncmp( "ATOM ", linec, 5 ) )
240 if( !strncmp( "CB ", linec+13, 3 ) ) return( 0 );
247 static float callrash( char **mseq1, char **mseq2, double *equiv, char *fname1, char *chain1, char *fname2, char *chain2, int alloclen )
251 static char com[10000];
253 char cachedir[10000];
254 char cachefile[10000];
260 sprintf( cachedir, "%s/.rashoutcache", getenv( "HOME" ) );
261 sprintf( com, "mkdir -p %s", cachedir );
264 sprintf( cachefile, "%s/%s%s-%s%s", cachedir, fname1, chain1, fname2, chain2 );
267 fp = fopen( cachefile, "r" );
268 if( fp == NULL ) runnow = 1;
271 fgets( com, 100, fp );
272 if( strncmp( com, "successful", 10 ) ) runnow = 1;
284 sprintf( com, "ln -s %s %s.pdb 2>_dum", fname1, fname1 );
286 sprintf( com, "ln -s %s %s.pdb 2>_dum", fname2, fname2 );
289 sprintf( com, "env PATH=%s PDP_ASH.pl --qf %s.pdb --qc %s --tf %s.pdb --tc %s > _rashout 2>_dum", whereispairalign, fname1, chain1, fname2, chain2 );
290 fprintf( stderr, "command = %s\n", com );
294 fprintf( stderr, "Error in PDP_ASH\n" );
301 fprintf( stderr, "Use cache!\n" );
302 sprintf( com, "grep -v successful %s > _rashout", cachefile );
306 if( usecache && runnow )
308 sprintf( com, "echo successful > %s", cachefile );
310 sprintf( com, "cat _rashout >> %s", cachefile );
314 fp = fopen( "_rashout", "r" );
317 fprintf( stderr, "Cannot open _rashout\n" );
321 readrash( fp, *mseq1, *mseq2, equiv );
325 // fprintf( stderr, "*mseq1 = %s\n", *mseq1 );
326 // fprintf( stderr, "*mseq2 = %s\n", *mseq2 );
329 value = (float)naivepairscore11( *mseq1, *mseq2, penalty );
334 static void prepareash( FILE *fp, char ***strfiles, char ***chainids, char ***seqpt, char ***mseq1pt, char ***mseq2pt, double **equivpt, int *alloclenpt )
341 int linenum, istr, nstr;
349 fgets( line, 999, fp );
350 if( feof( fp ) == 1 ) break;
351 sline = strip( line );
353 if( sline[0] == '#' || strlen( sline ) < 2 )
359 extractfirstword( sline );
360 checkfp = fopen( sline, "r" );
361 if( checkfp == NULL )
363 fprintf( stderr, "Cannot open %s.\n", sline );
367 fgets( linec, 999, checkfp );
368 if( strncmp( "HEADER ", linec, 7 ) )
370 fprintf( stderr, "Check the format of %s.\n", sline );
374 if( checkcbeta( checkfp ) )
376 fprintf( stderr, "%s has no C-beta atoms.\n", sline );
385 fprintf( stderr, "nstr = %d\n", nstr );
387 *strfiles = AllocateCharMtx( nstr, 1000 );
388 *chainids = AllocateCharMtx( nstr, 2 );
395 fgets( line, 999, fp );
396 if( feof( fp ) == 1 ) break;
397 sline = strip( line );
400 (*chainids)[istr][0] = getchainid( sline );
401 (*chainids)[istr][1] = 0;
402 extractfirstword( sline );
403 sprintf( fname, "%s", sline );
405 sprintf( command, "cp %s %s.pdb", sline, fname );
407 // fprintf( stderr, "command = %s\n", command );
408 strcpy( (*strfiles)[istr++], fname );
412 *seqpt = AllocateCharMtx( njob, nlenmax*2+1 );
413 *mseq1pt = AllocateCharMtx( njob, 0 );
414 *mseq2pt = AllocateCharMtx( njob, 0 );
415 *equivpt = AllocateDoubleVec( nlenmax*2+1 );
416 *alloclenpt = nlenmax*2;
417 dumseq = AllocateCharVec( nlenmax*2+1 );
418 alreadyoutput = AllocateIntVec( njob );
419 for( i=0; i<njob; i++ ) alreadyoutput[i] = 0;
421 for( i=0; i<istr; i++ )
423 fprintf( stderr, "i=%d\n", i );
426 (*mseq1pt)[0] = (*seqpt)[i];
427 (*mseq2pt)[0] = dumseq;
429 callrash( *mseq1pt, *mseq2pt, *equivpt, (*strfiles)[i], (*chainids)[i], (*strfiles)[i], (*chainids)[i], *alloclenpt );
430 fprintf( stdout, ">%d_%s-%s\n%s\n", i+1, (*strfiles)[i], (*chainids)[i], (*seqpt)[i] );
431 alreadyoutput[i] = 1;
435 void arguments( int argc, char *argv[] )
443 equivshortestlen = 1;
477 ppenalty = NOTSPECIFIED;
478 ppenalty_OP = NOTSPECIFIED;
479 ppenalty_ex = NOTSPECIFIED;
480 ppenalty_EX = NOTSPECIFIED;
481 poffset = NOTSPECIFIED;
482 kimuraR = NOTSPECIFIED;
485 fftWinSize = NOTSPECIFIED;
486 fftThreshold = NOTSPECIFIED;
487 RNAppenalty = NOTSPECIFIED;
488 RNApthr = NOTSPECIFIED;
490 while( --argc > 0 && (*++argv)[0] == '-' )
492 while ( ( c = *++argv[0] ) )
498 fprintf( stderr, "inputfile = %s\n", inputfile );
502 ppenalty = (int)( atof( *++argv ) * 1000 - 0.5 );
506 ppenalty_ex = (int)( atof( *++argv ) * 1000 - 0.5 );
510 ppenalty_OP = (int)( atof( *++argv ) * 1000 - 0.5 );
514 ppenalty_EX = (int)( atof( *++argv ) * 1000 - 0.5 );
518 poffset = (int)( atof( *++argv ) * 1000 - 0.5 );
522 kimuraR = atoi( *++argv );
523 // fprintf( stderr, "kimuraR = %d\n", kimuraR );
527 nblosum = atoi( *++argv );
529 // fprintf( stderr, "blosum %d\n", nblosum );
533 pamN = atoi( *++argv );
536 fprintf( stderr, "jtt %d\n", pamN );
540 pamN = atoi( *++argv );
543 fprintf( stderr, "TM %d\n", pamN );
547 whereispairalign = *++argv;
548 fprintf( stderr, "whereispairalign = %s\n", whereispairalign );
552 equivthreshold = atoi( *++argv );
556 equivwinsize = atoi( *++argv );
560 equivshortestlen = atoi( *++argv );
564 scale = atof( *++argv );
648 /* Modified 01/08/27, default: user tree */
652 /* modification end. */
655 fftThreshold = atoi( *++argv );
659 fftWinSize = atoi( *++argv );
667 fprintf( stderr, "illegal option %c\n", c );
677 cut = atof( (*argv) );
682 fprintf( stderr, "options: Check source file !\n" );
685 if( tbitr == 1 && outgap == 0 )
687 fprintf( stderr, "conflicting options : o, m or u\n" );
690 if( alg == 'C' && outgap == 0 )
692 fprintf( stderr, "conflicting options : C, o\n" );
697 int countamino( char *s, int end )
701 if( *s++ != '-' ) val++;
705 static void pairalign( char name[M][B], int nlen[M], char **seq, char **aseq, char **mseq1, char **mseq2, double *equiv, double *effarr, char **strfiles, char **chainids, int alloclen )
710 float pscore = 0.0; // by D.Mathog
711 static char *indication1, *indication2;
713 static double **distancemtx;
714 static double *effarr1 = NULL;
715 static double *effarr2 = NULL;
717 char *hat2file = "hat2";
718 LocalHom **localhomtable, *tmpptr;
725 localhomtable = (LocalHom **)calloc( njob, sizeof( LocalHom *) );
726 for( i=0; i<njob; i++)
729 localhomtable[i] = (LocalHom *)calloc( njob, sizeof( LocalHom ) );
730 for( j=0; j<njob; j++)
732 localhomtable[i][j].start1 = -1;
733 localhomtable[i][j].end1 = -1;
734 localhomtable[i][j].start2 = -1;
735 localhomtable[i][j].end2 = -1;
736 localhomtable[i][j].opt = -1.0;
737 localhomtable[i][j].next = NULL;
738 localhomtable[i][j].nokori = 0;
742 if( effarr1 == NULL )
744 distancemtx = AllocateDoubleMtx( njob, njob );
745 effarr1 = AllocateDoubleVec( njob );
746 effarr2 = AllocateDoubleVec( njob );
747 indication1 = AllocateCharVec( 150 );
748 indication2 = AllocateCharVec( 150 );
749 checkseq = AllocateCharMtx( njob, alloclen );
752 pair = AllocateCharMtx( njob, njob );
757 fprintf( stderr, "##### fftwinsize = %d, fftthreshold = %d\n", fftWinSize, fftThreshold );
761 for( i=0; i<njob; i++ )
762 fprintf( stderr, "TBFAST effarr[%d] = %f\n", i, effarr[i] );
766 // writePre( njob, name, nlen, aseq, 0 );
768 for( i=0; i<njob; i++ ) for( j=0; j<njob; j++ ) pair[i][j] = 0;
769 for( i=0; i<njob; i++ ) pair[i][i] = 1;
771 for( i=0; i<njob; i++ )
773 strcpy( checkseq[i], seq[i] );
774 // fprintf( stderr, "checkseq[%d] = %s\n", i, checkseq[i] );
779 for( i=0; i<ilim; i++ )
781 fprintf( stderr, "% 5d / %d\r", i, njob );
784 for( j=i+1; j<njob; j++ )
789 if( strlen( seq[i] ) == 0 || strlen( seq[j] ) == 0 )
791 distancemtx[i][j] = pscore;
796 strcpy( aseq[i], seq[i] );
797 strcpy( aseq[j], seq[j] );
798 clus1 = conjuctionfortbfast( pair, i, aseq, mseq1, effarr1, effarr, indication1 );
799 clus2 = conjuctionfortbfast( pair, j, aseq, mseq2, effarr2, effarr, indication2 );
800 // fprintf( stderr, "mseq1 = %s\n", mseq1[0] );
801 // fprintf( stderr, "mseq2 = %s\n", mseq2[0] );
804 fprintf( stderr, "group1 = %.66s", indication1 );
805 fprintf( stderr, "\n" );
806 fprintf( stderr, "group2 = %.66s", indication2 );
807 fprintf( stderr, "\n" );
809 // for( l=0; l<clus1; l++ ) fprintf( stderr, "## STEP-eff for mseq1-%d %f\n", l, effarr1[l] );
814 pscore = Falign( mseq1, mseq2, effarr1, effarr2, clus1, clus2, alloclen, &intdum );
823 pscore = Aalign( mseq1, mseq2, effarr1, effarr2, clus1, clus2, alloclen );
827 pscore = G__align11( mseq1, mseq2, alloclen );
831 fprintf( stderr, " Calling PDP_ASH.pl %d-%d/%d \r", i+1, j+1, njob );
832 pscore = callrash( mseq1, mseq2, equiv, strfiles[i], chainids[i], strfiles[j], chainids[j], alloclen );
837 pscore = VAalign11( mseq1, mseq2, alloclen, &off1, &off2, localhomtable[i]+j );
838 fprintf( stderr, "i,j = %d,%d, score = %f\n", i,j, pscore );
841 fprintf( stderr, "aligning %d-%d\n", i, j );
842 pscore = suboptalign11( mseq1, mseq2, alloclen, &off1, &off2, localhomtable[i]+j );
843 fprintf( stderr, "i,j = %d,%d, score = %f\n", i,j, pscore );
847 pscore = genL__align11( mseq1, mseq2, alloclen, &off1, &off2 );
848 // fprintf( stderr, "pscore = %f\n", pscore );
851 pscore = genG__align11( mseq1, mseq2, alloclen );
852 // fprintf( stderr, "pscore = %f\n", pscore );
856 pscore = L__align11( mseq1, mseq2, alloclen, &off1, &off2 );
857 // fprintf( stderr, "pscore (1) = %f\n", pscore );
858 // pscore = (float)naivepairscore11( *mseq1, *mseq2, penalty ); // nennnotame
859 // fprintf( stderr, "pscore (2) = %f\n\n", pscore );
861 ErrorExit( "ERROR IN SOURCE FILE" );
864 distancemtx[i][j] = pscore;
866 fprintf( stderr, "score = %10.2f (%d,%d)\n", pscore, i, j );
869 putlocalhom_str( mseq1[0], mseq2[0], equiv, scale, localhomtable[i]+j, off1, off2, (int)pscore, strlen( mseq1[0] ) );
872 if( alreadyoutput[i] == 0 )
874 alreadyoutput[i] = 1;
875 gappick0( seq[i], mseq1[0] );
876 fprintf( stdout, ">%d_%s-%s\n%s\n", i+1, strfiles[i], chainids[i], seq[i] );
877 strcpy( checkseq[i], seq[i] );
881 gappick0( seq[i], mseq1[0] );
882 fprintf( stderr, "checking seq%d\n", i );
884 // fprintf( stderr, " seq=%s\n", seq[i] );
885 // fprintf( stderr, "checkseq=%s\n", checkseq[i] );
887 if( strcmp( checkseq[i], seq[i] ) )
889 fprintf( stderr, "\n\nWARNING: Sequence changed!!\n" );
890 fprintf( stderr, "i=%d\n", i );
891 fprintf( stderr, " seq=%s\n", seq[i] );
892 fprintf( stderr, "checkseq=%s\n", checkseq[i] );
896 if( alreadyoutput[j] == 0 )
898 alreadyoutput[j] = 1;
899 gappick0( seq[j], mseq2[0] );
900 fprintf( stdout, ">%d_%s-%s\n%s\n", j+1, strfiles[j], chainids[j], seq[j] );
901 strcpy( checkseq[j], seq[j] );
905 gappick0( seq[j], mseq2[0] );
906 fprintf( stderr, "checking seq%d\n", j );
907 if( strcmp( checkseq[j], seq[j] ) )
909 fprintf( stderr, "\n\nWARNING: Sequence changed!!\n" );
910 fprintf( stderr, "j=%d\n", j );
911 fprintf( stderr, " seq=%s\n", seq[j] );
912 fprintf( stderr, "checkseq=%s\n", checkseq[j] );
919 for( i=0; i<njob; i++ )
922 for( pt=seq[i]; *pt; pt++ )
923 pscore += amino_dis[(int)*pt][(int)*pt];
924 distancemtx[i][i] = pscore;
929 for( i=0; i<ilim; i++ )
931 for( j=i+1; j<njob; j++ )
933 bunbo = MIN( distancemtx[i][i], distancemtx[j][j] );
935 distancemtx[i][j] = 2.0;
937 distancemtx[i][j] = ( 1.0 - distancemtx[i][j] / bunbo ) * 2.0;
941 hat2p = fopen( hat2file, "w" );
942 if( !hat2p ) ErrorExit( "Cannot open hat2." );
943 WriteHat2( hat2p, njob, name, distancemtx );
946 fprintf( stderr, "##### writing hat3\n" );
947 hat3p = fopen( "hat3", "w" );
948 if( !hat3p ) ErrorExit( "Cannot open hat3." );
950 for( i=0; i<ilim; i++ )
952 for( j=i+1; j<njob; j++ )
954 for( tmpptr=localhomtable[i]+j; tmpptr; tmpptr=tmpptr->next )
956 if( tmpptr->opt == -1.0 ) continue;
958 // if( alg == 'B' || alg == 'T' )
959 // 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 );
961 fprintf( hat3p, "%d %d %d %7.5f %d %d %d %d k\n", i, j, tmpptr->overlapaa, tmpptr->opt, tmpptr->start1, tmpptr->end1, tmpptr->start2, tmpptr->end2 );
967 fprintf( stderr, "calling FreeLocalHomTable\n" );
969 FreeLocalHomTable( localhomtable, njob );
971 fprintf( stderr, "done. FreeLocalHomTable\n" );
975 static void WriteOptions( FILE *fp )
978 if( dorp == 'd' ) fprintf( fp, "DNA\n" );
981 if ( scoremtx == 0 ) fprintf( fp, "JTT %dPAM\n", pamN );
982 else if( scoremtx == 1 ) fprintf( fp, "BLOSUM %d\n", nblosum );
983 else if( scoremtx == 2 ) fprintf( fp, "M-Y\n" );
985 fprintf( stderr, "Gap Penalty = %+5.2f, %+5.2f, %+5.2f\n", (double)ppenalty/1000, (double)ppenalty_ex/1000, (double)poffset/1000 );
986 if( use_fft ) fprintf( fp, "FFT on\n" );
988 fprintf( fp, "tree-base method\n" );
989 if( tbrweight == 0 ) fprintf( fp, "unweighted\n" );
990 else if( tbrweight == 3 ) fprintf( fp, "clustalw-like weighting\n" );
991 if( tbitr || tbweight )
993 fprintf( fp, "iterate at each step\n" );
994 if( tbitr && tbrweight == 0 ) fprintf( fp, " unweighted\n" );
995 if( tbitr && tbrweight == 3 ) fprintf( fp, " reversely weighted\n" );
996 if( tbweight ) fprintf( fp, " weighted\n" );
1000 fprintf( fp, "Gap Penalty = %+5.2f, %+5.2f, %+5.2f\n", (double)ppenalty/1000, (double)ppenalty_ex/1000, (double)poffset/1000 );
1003 fprintf( fp, "Algorithm A\n" );
1004 else if( alg == 'A' )
1005 fprintf( fp, "Algorithm A+\n" );
1006 else if( alg == 'S' )
1007 fprintf( fp, "Apgorithm S\n" );
1008 else if( alg == 'C' )
1009 fprintf( fp, "Apgorithm A+/C\n" );
1011 fprintf( fp, "Unknown algorithm\n" );
1015 fprintf( fp, "FFT on\n" );
1017 fprintf( fp, "Basis : 4 nucleotides\n" );
1021 fprintf( fp, "Basis : Polarity and Volume\n" );
1023 fprintf( fp, "Basis : 20 amino acids\n" );
1025 fprintf( fp, "Threshold of anchors = %d%%\n", fftThreshold );
1026 fprintf( fp, "window size of anchors = %dsites\n", fftWinSize );
1029 fprintf( fp, "FFT off\n" );
1034 int main( int argc, char *argv[] )
1037 static char name[M][B], **seq;
1038 static char **mseq1, **mseq2;
1042 static double *equiv;
1050 arguments( argc, argv );
1052 if( equivthreshold < 1 || 9 < equivthreshold )
1054 fprintf( stderr, "-t n, n must be 1..9\n" );
1058 if( ( equivwinsize + 1 ) % 2 != 0 )
1060 fprintf( stderr, "equivwinsize = %d\n", equivwinsize );
1061 fprintf( stderr, "It must be an odd number.\n" );
1067 infp = fopen( inputfile, "r" );
1070 fprintf( stderr, "Cannot open %s\n", inputfile );
1077 nlenmax = 10000; // tekitou
1079 prepareash( infp, &strfiles, &chainids, &seq, &mseq1, &mseq2, &equiv, &alloclen );
1082 aseq = AllocateCharMtx( njob, nlenmax*2+1 );
1083 bseq = AllocateCharMtx( njob, nlenmax*2+1 );
1084 eff = AllocateDoubleVec( njob );
1086 for( i=0; i<njob; i++ )
1088 fprintf( stderr, "str%d = %s-%s\n", i, strfiles[i], chainids[i] );
1093 fprintf( stderr, "No structure found.\n" );
1098 fprintf( stderr, "Only %d structure found.\n", njob );
1103 fprintf( stderr, "The number of structures must be < %d\n", M );
1104 fprintf( stderr, "Please try sequence-based methods for such large data.\n" );
1111 readData( infp, name, nlen, seq );
1114 constants( njob, seq );
1117 fprintf( stderr, "params = %d, %d, %d\n", penalty, penalty_ex, offset );
1124 WriteOptions( trap_g );
1126 c = seqcheck( seq );
1129 fprintf( stderr, "Illegal character %c\n", c );
1133 // writePre( njob, name, nlen, seq, 0 );
1135 for( i=0; i<njob; i++ ) eff[i] = 1.0;
1138 for( i=0; i<njob; i++ ) gappick0( bseq[i], seq[i] );
1140 pairalign( name, nlen, bseq, aseq, mseq1, mseq2, equiv, eff, strfiles, chainids, alloclen );
1142 fprintf( trap_g, "done.\n" );
1144 fprintf( stderr, "closing trap_g\n" );
1148 // writePre( njob, name, nlen, aseq, !contin );
1150 writeData( stdout, njob, name, nlen, aseq );
1153 fprintf( stderr, "OSHIMAI\n" );