7 #define USE_PENALTY_EX 1
12 static float singleribosumscore( int n1, int n2, char **s1, char **s2, double *eff1, double *eff2, int p1, int p2 )
19 for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
21 code1 = amino_n[(int)s1[i][p1]];
22 if( code1 > 3 ) code1 = 36;
23 code2 = amino_n[(int)s2[j][p2]];
24 if( code2 > 3 ) code2 = 36;
26 // fprintf( stderr, "'l'%c-%c: %f\n", s1[i][p1], s2[j][p2], (float)ribosumdis[code1][code2] );
28 val += (float)ribosumdis[code1][code2] * eff1[i] * eff2[j];
32 static float pairedribosumscore53( int n1, int n2, char **s1, char **s2, double *eff1, double *eff2, int p1, int p2, int c1, int c2 )
36 int code1o, code1u, code2o, code2u, code1, code2;
39 for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
41 code1o = amino_n[(int)s1[i][p1]];
42 code1u = amino_n[(int)s1[i][c1]];
43 if( code1o > 3 ) code1 = code1o = 36;
44 else if( code1u > 3 ) code1 = 36;
45 else code1 = 4 + code1o * 4 + code1u;
47 code2o = amino_n[(int)s2[j][p2]];
48 code2u = amino_n[(int)s2[j][c2]];
49 if( code2o > 3 ) code2 = code1o = 36;
50 else if( code2u > 3 ) code2 = 36;
51 else code2 = 4 + code2o * 4 + code2u;
54 // fprintf( stderr, "%c%c-%c%c: %f\n", s1[i][p1], s1[i][c1], s2[j][p2], s2[j][c2], (float)ribosumdis[code1][code2] );
56 if( code1 == 36 || code2 == 36 )
57 val += (float)n_dis[code1o][code2o] * eff1[i] * eff2[j];
59 val += (float)ribosumdis[code1][code2] * eff1[i] * eff2[j];
64 static float pairedribosumscore35( int n1, int n2, char **s1, char **s2, double *eff1, double *eff2, int p1, int p2, int c1, int c2 )
68 int code1o, code1u, code2o, code2u, code1, code2;
71 for( i=0; i<n1; i++ ) for( j=0; j<n2; j++ )
73 code1o = amino_n[(int)s1[i][p1]];
74 code1u = amino_n[(int)s1[i][c1]];
75 if( code1o > 3 ) code1 = code1o = 36;
76 else if( code1u > 3 ) code1 = 36;
77 else code1 = 4 + code1u * 4 + code1o;
79 code2o = amino_n[(int)s2[j][p2]];
80 code2u = amino_n[(int)s2[j][c2]];
81 if( code2o > 3 ) code2 = code1o = 36;
82 else if( code2u > 3 ) code2 = 36;
83 else code2 = 4 + code2u * 4 + code2o;
86 // fprintf( stderr, "%c%c-%c%c: %f\n", s1[i][p1], s1[i][c1], s2[j][p2], s2[j][c2], (float)ribosumdis[code1][code2] );
88 if( code1 == 36 || code2 == 36 )
89 val += (float)n_dis[code1o][code2o] * eff1[i] * eff2[j];
91 val += (float)ribosumdis[code1][code2] * eff1[i] * eff2[j];
97 static void mccaskillextract( char **seq, char **nogap, int nseq, RNApair **pairprob, RNApair ***single, int **sgapmap, double *eff )
102 int left, right, adpos;
104 static TLS int *pairnum;
107 lgth = strlen( seq[0] );
108 pairnum = calloc( lgth, sizeof( int ) );
109 for( i=0; i<lgth; i++ ) pairnum[i] = 0;
111 for( i=0; i<nseq; i++ )
113 nogaplgth = strlen( nogap[i] );
114 for( j=0; j<nogaplgth; j++ ) for( pt=single[i][j]; pt->bestpos!=-1; pt++ )
116 left = sgapmap[i][j];
117 right = sgapmap[i][pt->bestpos];
118 prob = pt->bestscore;
121 for( pt2=pairprob[left]; pt2->bestpos!=-1; pt2++ )
122 if( pt2->bestpos == right ) break;
124 // fprintf( stderr, "i,j=%d,%d, left=%d, right=%d, pt=%d, pt2->bestpos = %d\n", i, j, left, right, pt-single[i][j], pt2->bestpos );
125 if( pt2->bestpos == -1 )
127 pairprob[left] = (RNApair *)realloc( pairprob[left], (pairnum[left]+2) * sizeof( RNApair ) );
128 adpos = pairnum[left];
130 pairprob[left][adpos].bestscore = 0.0;
131 pairprob[left][adpos].bestpos = right;
132 pairprob[left][adpos+1].bestscore = -1.0;
133 pairprob[left][adpos+1].bestpos = -1;
134 pt2 = pairprob[left]+adpos;
137 adpos = pt2-pairprob[left];
139 pt2->bestscore += prob * eff[i];
141 if( pt2->bestpos != right )
143 fprintf( stderr, "okashii!\n" );
146 // fprintf( stderr, "adding %d-%d, %f\n", left, right, prob );
147 // fprintf( stderr, "pairprob[0][0].bestpos=%d\n", pairprob[0][0].bestpos );
148 // fprintf( stderr, "pairprob[0][0].bestscore=%f\n", pairprob[0][0].bestscore );
152 // fprintf( stderr, "before taikakuka\n" );
153 for( i=0; i<lgth; i++ ) for( j=0; j<pairnum[i]; j++ )
155 if( pairprob[i][j].bestpos > -1 )
157 // pairprob[i][j].bestscore /= (float)nseq;
158 // fprintf( stderr, "pair of %d = %d (%f) %c:%c\n", i, pairprob[i][j].bestpos, pairprob[i][j].bestscore, seq[0][i], seq[0][pairprob[i][j].bestpos] );
163 for( i=0; i<lgth; i++ ) for( j=0; j<pairnum[i]; j++ )
165 right=pairprob[i][j].bestpos;
166 if( right < i ) continue;
167 fprintf( stderr, "no%d-%d, adding %d -> %d\n", i, j, right, i );
168 pairprob[right] = (RNApair *)realloc( pairprob[right], (pairnum[right]+2) * sizeof( RNApair ) );
169 pairprob[right][pairnum[right]].bestscore = pairprob[i][j].bestscore;
170 pairprob[right][pairnum[right]].bestpos = i;
172 pairprob[right][pairnum[right]].bestscore = -1.0;
173 pairprob[right][pairnum[right]].bestpos = -1;
182 void rnaalifoldcall( char **seq, int nseq, RNApair **pairprob )
186 static TLS int *order = NULL;
187 static TLS char **name = NULL;
190 int left, right, dumm;
193 static TLS char fnamein[100];
194 static TLS char cmd[1000];
195 static TLS int *pairnum;
197 lgth = strlen( seq[0] );
201 sprintf( fnamein, "/tmp/_rnaalifoldin.%d", pid );
202 order = AllocateIntVec( njob );
203 name = AllocateCharMtx( njob, 10 );
204 for( i=0; i<njob; i++ )
207 sprintf( name[i], "seq%d", i );
210 pairnum = calloc( lgth, sizeof( int ) );
211 for( i=0; i<lgth; i++ ) pairnum[i] = 0;
213 fp = fopen( fnamein, "w" );
216 fprintf( stderr, "Cannot open /tmp/_rnaalifoldin\n" );
219 clustalout_pointer( fp, nseq, lgth, seq, name, NULL, NULL, order, 15 );
222 sprintf( cmd, "RNAalifold -p %s", fnamein );
225 fp = fopen( "alifold.out", "r" );
228 fprintf( stderr, "Cannot open /tmp/_rnaalifoldin\n" );
233 for( i=0; i<lgth; i++ ) // atode kesu
235 pairprob[i] = (RNApair *)realloc( pairprob[i], (2) * sizeof( RNApair ) ); // atode kesu
236 pairprob[i][1].bestscore = -1.0;
237 pairprob[i][1].bestpos = -1;
243 fgets( gett, 999, fp );
244 if( gett[0] == '(' ) break;
245 if( gett[0] == '{' ) break;
246 if( gett[0] == '.' ) break;
247 if( gett[0] == ',' ) break;
248 if( gett[0] != ' ' ) continue;
250 sscanf( gett, "%d %d %d %f", &left, &right, &dumm, &prob );
256 if( prob > 50.0 && prob > pairprob[left][0].bestscore )
258 pairprob[left][0].bestscore = prob;
259 pairprob[left][0].bestpos = right;
263 pairprob[left] = (RNApair *)realloc( pairprob[left], (pairnum[left]+2) * sizeof( RNApair ) );
264 pairprob[left][pairnum[left]].bestscore = prob / 100.0;
265 pairprob[left][pairnum[left]].bestpos = right;
267 pairprob[left][pairnum[left]].bestscore = -1.0;
268 pairprob[left][pairnum[left]].bestpos = -1;
269 fprintf( stderr, "%d-%d, %f\n", left, right, prob );
271 pairprob[right] = (RNApair *)realloc( pairprob[right], (pairnum[right]+2) * sizeof( RNApair ) );
272 pairprob[right][pairnum[right]].bestscore = prob / 100.0;
273 pairprob[right][pairnum[right]].bestpos = left;
275 pairprob[right][pairnum[right]].bestscore = -1.0;
276 pairprob[right][pairnum[right]].bestpos = -1;
277 fprintf( stderr, "%d-%d, %f\n", left, right, prob );
282 sprintf( cmd, "rm -f %s", fnamein );
285 for( i=0; i<lgth; i++ )
287 if( (right=pairprob[i][0].bestpos) > -1 )
289 pairprob[right][0].bestpos = i;
290 pairprob[right][0].bestscore = pairprob[i][0].bestscore;
295 for( i=0; i<lgth; i++ ) // atode kesu
296 if( pairprob[i][0].bestscore > -1 ) pairprob[i][0].bestscore = 1.0; // atode kesu
299 // fprintf( stderr, "after taikakuka in rnaalifoldcall\n" );
300 // for( i=0; i<lgth; i++ )
302 // fprintf( stderr, "pair of %d = %d (%f) %c:%c\n", i, pairprob[i][0].bestpos, pairprob[i][0].bestscore, seq[0][i], seq[0][pairprob[i][0].bestpos] );
309 static void utot( int n, int l, char **s )
316 if ( s[j][i] == 'a' ) s[j][i] = 'a';
317 else if( s[j][i] == 't' ) s[j][i] = 't';
318 else if( s[j][i] == 'u' ) s[j][i] = 't';
319 else if( s[j][i] == 'g' ) s[j][i] = 'g';
320 else if( s[j][i] == 'c' ) s[j][i] = 'c';
321 else if( s[j][i] == '-' ) s[j][i] = '-';
328 void foldrna( int nseq1, int nseq2, char **seq1, char **seq2, double *eff1, double *eff2, RNApair ***grouprna1, RNApair ***grouprna2, float **impmtx, int *gapmap1, int *gapmap2, RNApair *additionalpair )
334 static TLS char **useq1, **useq2;
335 static TLS char **oseq1, **oseq2, **oseq1r, **oseq2r, *odir1, *odir2;
336 static TLS RNApair **pairprob1, **pairprob2;
337 static TLS RNApair *pairpt1, *pairpt2;
338 int lgth1 = strlen( seq1[0] );
339 int lgth2 = strlen( seq2[0] );
340 static TLS float **impmtx2;
341 static TLS float **map;
344 int **sgapmap1, **sgapmap2;
349 // fprintf( stderr, "nseq1=%d, lgth1=%d\n", nseq1, lgth1 );
350 useq1 = AllocateCharMtx( nseq1, lgth1+10 );
351 useq2 = AllocateCharMtx( nseq2, lgth2+10 );
352 oseq1 = AllocateCharMtx( nseq1, lgth1+10 );
353 oseq2 = AllocateCharMtx( nseq2, lgth2+10 );
354 oseq1r = AllocateCharMtx( nseq1, lgth1+10 );
355 oseq2r = AllocateCharMtx( nseq2, lgth2+10 );
356 odir1 = AllocateCharVec( lgth1+10 );
357 odir2 = AllocateCharVec( lgth2+10 );
358 sgapmap1 = AllocateIntMtx( nseq1, lgth1+1 );
359 sgapmap2 = AllocateIntMtx( nseq2, lgth2+1 );
360 nogapdum = AllocateCharVec( MAX( lgth1, lgth2 ) );
361 pairprob1 = (RNApair **)calloc( lgth1, sizeof( RNApair *) );
362 pairprob2 = (RNApair **)calloc( lgth2, sizeof( RNApair *) );
363 map = AllocateFloatMtx( lgth1, lgth2 );
364 impmtx2 = AllocateFloatMtx( lgth1, lgth2 );
365 tbppmtx = AllocateFloatMtx( lgth1, lgth2 );
367 for( i=0; i<nseq1; i++ ) strcpy( useq1[i], seq1[i] );
368 for( i=0; i<nseq2; i++ ) strcpy( useq2[i], seq2[i] );
369 for( i=0; i<nseq1; i++ ) strcpy( oseq1[i], seq1[i] );
370 for( i=0; i<nseq2; i++ ) strcpy( oseq2[i], seq2[i] );
372 for( i=0; i<nseq1; i++ ) commongappick_record( 1, useq1+i, sgapmap1[i] );
373 for( i=0; i<nseq2; i++ ) commongappick_record( 1, useq2+i, sgapmap2[i] );
375 for( i=0; i<lgth1; i++ )
377 pairprob1[i] = (RNApair *)calloc( 1, sizeof( RNApair ) );
378 pairprob1[i][0].bestpos = -1;
379 pairprob1[i][0].bestscore = -1;
381 for( i=0; i<lgth2; i++ )
383 pairprob2[i] = (RNApair *)calloc( 1, sizeof( RNApair ) );
384 pairprob2[i][0].bestpos = -1;
385 pairprob2[i][0].bestscore = -1;
388 utot( nseq1, lgth1, oseq1 );
389 utot( nseq2, lgth2, oseq2 );
391 // fprintf( stderr, "folding group1\n" );
392 // rnalocal( oseq1, useq1, eff1, eff1, nseq1, nseq1, lgth1+10, pair1 );
394 /* base-pairing probability of group 1 */
395 if( rnaprediction == 'r' )
396 rnaalifoldcall( oseq1, nseq1, pairprob1 );
398 mccaskillextract( oseq1, useq1, nseq1, pairprob1, grouprna1, sgapmap1, eff1 );
401 // fprintf( stderr, "folding group2\n" );
402 // rnalocal( oseq2, useq2, eff2, eff2, nseq2, nseq2, lgth2+10, pair2 );
404 /* base-pairing probability of group 2 */
405 if( rnaprediction == 'r' )
406 rnaalifoldcall( oseq2, nseq2, pairprob2 );
408 mccaskillextract( oseq2, useq2, nseq2, pairprob2, grouprna2, sgapmap2, eff2 );
413 makerseq( oseq1, oseq1r, odir1, pairprob1, nseq1, lgth1 );
414 makerseq( oseq2, oseq2r, odir2, pairprob2, nseq2, lgth2 );
416 fprintf( stderr, "%s\n", odir2 );
418 for( i=0; i<nseq1; i++ )
420 fprintf( stdout, ">ori\n%s\n", oseq1[0] );
421 fprintf( stdout, ">rev\n%s\n", oseq1r[0] );
425 /* similarity score */
426 Lalignmm_hmout( oseq1, oseq2, eff1, eff2, nseq1, nseq2, 10000, NULL, NULL, NULL, NULL, map );
430 if( RNAscoremtx == 'n' )
432 for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
434 // impmtx2[i][j] = osoiaveragescore( nseq1, nseq2, oseq1, oseq2, eff1, eff2, i, j ) * consweight_multi;
438 else if( RNAscoremtx == 'r' )
440 for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
445 for( i=0; i<lgth1; i++ ) for( pairpt1=pairprob1[i]; pairpt1->bestpos!=-1; pairpt1++ )
447 for( j=0; j<lgth2; j++ ) for( pairpt2=pairprob2[j]; pairpt2->bestpos!=-1; pairpt2++ )
449 uido = pairpt1->bestpos;
450 ujdo = pairpt2->bestpos;
451 prob = pairpt1->bestscore * pairpt2->bestscore;
452 if( uido > -1 && ujdo > -1 )
454 if( uido > i && j > ujdo )
456 impmtx2[i][j] += prob * pairedribosumscore53( nseq1, nseq2, oseq1, oseq2, eff1, eff2, i, j, uido, ujdo ) * consweight_multi;
457 tbppmtx[i][j] -= prob;
459 else if( i < uido && j < ujdo )
461 impmtx2[i][j] += prob * pairedribosumscore35( nseq1, nseq2, oseq1, oseq2, eff1, eff2, i, j, uido, ujdo ) * consweight_multi;
462 tbppmtx[i][j] -= prob;
469 for( i=0; i<lgth1; i++ )
471 for( j=0; j<lgth2; j++ )
473 impmtx2[i][j] += tbppmtx[i][j] * singleribosumscore( nseq1, nseq2, oseq1, oseq2, eff1, eff2, i, j ) * consweight_multi;
479 /* four-way consistency */
481 for( i=0; i<lgth1; i++ ) for( pairpt1=pairprob1[i]; pairpt1->bestpos!=-1; pairpt1++ )
484 // if( pairprob1[i] == NULL ) continue;
486 for( j=0; j<lgth2; j++ ) for( pairpt2=pairprob2[j]; pairpt2->bestpos!=-1; pairpt2++ )
488 // fprintf( stderr, "i=%d, j=%d, pn1=%d, pn2=%d\n", i, j, pairpt1-pairprob1[i], pairpt2-pairprob2[j] );
489 // if( pairprob2[j] == NULL ) continue;
491 uido = pairpt1->bestpos;
492 ujdo = pairpt2->bestpos;
493 prob = pairpt1->bestscore * pairpt2->bestscore;
495 // fprintf( stderr, "i=%d->uido=%d, j=%d->ujdo=%d\n", i, uido, j, ujdo );
497 // fprintf( stderr, "impmtx2[%d][%d] = %f\n", i, j, impmtx2[i][j] );
499 // if( i < uido && j > ujdo ) continue;
500 // if( i > uido && j < ujdo ) continue;
503 // posdistj = abs( ujdo-j );
505 // if( uido > -1 && ujdo > -1 )
506 if( uido > -1 && ujdo > -1 && ( ( i > uido && j > ujdo ) || ( i < uido && j < ujdo ) ) )
509 impmtx2[i][j] += MAX( 0, map[uido][ujdo] ) * consweight_rna * 600 * prob; // osoi
515 for( i=0; i<lgth1; i++ ) for( j=0; j<lgth2; j++ )
517 impmtx[i][j] += impmtx2[i][j];
518 // fprintf( stderr, "fastathreshold=%f, consweight_multi=%f, consweight_rna=%f\n", fastathreshold, consweight_multi, consweight_rna );
519 // impmtx[i][j] *= 0.5;
522 // impmtx[0][0] += 10000.0;
523 // impmtx[lgth1-1][lgth2-1] += 10000.0;
528 fprintf( stdout, "#impmtx2 = \n" );
529 for( i=0; i<lgth1; i++ )
531 for( j=0; j<lgth2; j++ )
533 fprintf( stdout, "%d %d %f\n", i, j, impmtx2[i][j] );
535 fprintf( stdout, "\n" );
541 FreeCharMtx( useq1 );
542 FreeCharMtx( useq2 );
543 FreeCharMtx( oseq1 );
544 FreeCharMtx( oseq2 );
545 FreeCharMtx( oseq1r );
546 FreeCharMtx( oseq2r );
549 FreeFloatMtx( impmtx2 );
551 FreeIntMtx( sgapmap1 );
552 FreeIntMtx( sgapmap2 );
553 FreeFloatMtx( tbppmtx );
555 for( i=0; i<lgth1; i++ ) free( pairprob1[i] );
556 for( i=0; i<lgth2; i++ ) free( pairprob2[i] );