RNAlib-2.1.9h
loop_energies.h
Go to the documentation of this file.
1 #ifndef __VIENNA_RNA_PACKAGE_LOOP_ENERGIES_H__
2 #define __VIENNA_RNA_PACKAGE_LOOP_ENERGIES_H__
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <math.h>
7 #include <ctype.h>
8 #include <string.h>
9 #include "params.h"
10 #include "fold_vars.h"
11 #include "energy_par.h"
12 
13 #ifdef __GNUC__
14 # define INLINE inline
15 #else
16 # define INLINE
17 #endif
18 
54 INLINE PRIVATE int E_MLstem( int type,
55  int si1,
56  int sj1,
57  paramT *P);
58 
65 INLINE PRIVATE double exp_E_MLstem(int type,
66  int si1,
67  int sj1,
68  pf_paramT *P);
69 
89 INLINE PRIVATE int E_ExtLoop(int type,
90  int si1,
91  int sj1,
92  paramT *P);
93 
100 INLINE PRIVATE double exp_E_ExtLoop( int type,
101  int si1,
102  int sj1,
103  pf_paramT *P);
104 
149 INLINE PRIVATE int E_IntLoop(int n1,
150  int n2,
151  int type,
152  int type_2,
153  int si1,
154  int sj1,
155  int sp1,
156  int sq1,
157  paramT *P);
158 
159 
191 INLINE PRIVATE int E_Hairpin(int size,
192  int type,
193  int si1,
194  int sj1,
195  const char *string,
196  paramT *P);
197 
243 INLINE PRIVATE int E_Stem( int type,
244  int si1,
245  int sj1,
246  int extLoop,
247  paramT *P);
248 
257 INLINE PRIVATE double exp_E_Stem(int type,
258  int si1,
259  int sj1,
260  int extLoop,
261  pf_paramT *P);
262 
280 INLINE PRIVATE double exp_E_Hairpin( int u,
281  int type,
282  short si1,
283  short sj1,
284  const char *string,
285  pf_paramT *P);
286 
306 INLINE PRIVATE double exp_E_IntLoop(int u1,
307  int u2,
308  int type,
309  int type2,
310  short si1,
311  short sj1,
312  short sp1,
313  short sq1,
314  pf_paramT *P);
315 
316 
317 /*
318 #################################
319 # BEGIN OF FUNCTION DEFINITIONS #
320 #################################
321 */
322 INLINE PRIVATE int E_Hairpin(int size, int type, int si1, int sj1, const char *string, paramT *P){
323  int energy;
324 
325  if(type < 8){
326  energy = (size <= 30) ? P->hairpin[size] : P->hairpin[30]+(int)(P->lxc*log((size)/30.));
327  if (P->model_details.special_hp){
328  if (size == 4) { /* check for tetraloop bonus */
329  char tl[7]={0}, *ts;
330  strncpy(tl, string, 6);
331  if ((ts=strstr(P->Tetraloops, tl)))
332  return (P->Tetraloop_E[(ts - P->Tetraloops)/7]);
333  }
334  if (size == 6) {
335  char tl[9]={0}, *ts;
336  strncpy(tl, string, 8);
337  if ((ts=strstr(P->Hexaloops, tl)))
338  return (energy = P->Hexaloop_E[(ts - P->Hexaloops)/9]);
339  }
340  if (size == 3) {
341  char tl[6]={0,0,0,0,0,0}, *ts;
342  strncpy(tl, string, 5);
343  if ((ts=strstr(P->Triloops, tl))) {
344  return (P->Triloop_E[(ts - P->Triloops)/6]);
345  }
346  return (energy + P->TerminalAU_t[type]);
347  }
348  }
349  } else if (type > 16){
350  energy = (size <= 30) ? P->hairpin_D[size] : P->hairpin_D[30]+(int)(P->lxc_D*log((size)/30.));
351  if (P->model_details.special_hp){
352  if (size == 4) { /* check for tetraloop bonus */
353  char tl[7]={0}, *ts;
354  strncpy(tl, string, 6);
355  if ((ts=strstr(P->Tetraloops_D, tl)))
356  return (P->Tetraloop_E_D[(ts - P->Tetraloops_D)/7]);
357  }
358  if (size == 6) {
359  char tl[9]={0}, *ts;
360  strncpy(tl, string, 8);
361  if ((ts=strstr(P->Hexaloops_D, tl)))
362  return (energy = P->Hexaloop_E_D[(ts - P->Hexaloops_D)/9]);
363  }
364  if (size == 3) {
365  char tl[6]={0,0,0,0,0,0}, *ts;
366  strncpy(tl, string, 5);
367  if ((ts=strstr(P->Triloops_D, tl))) {
368  return (P->Triloop_E_D[(ts - P->Triloops_D)/6]);
369  }
370  return (energy + P->TerminalAU_t[type]);
371  }
372  }
373  } else {;}
374 
375  energy += P->mismatchH[type][si1][sj1];
376 
377  return energy;
378 }
379 
380 INLINE PRIVATE int E_IntLoop(int n1, int n2, int type, int type_2, int si1, int sj1, int sp1, int sq1, paramT *P){
381  /* compute energy of degree 2 loop (stack bulge or interior) */
382  int nl, ns, energy;
383  energy = INF;
384 
385  if (n1>n2) { nl=n1; ns=n2;}
386  else {nl=n2; ns=n1;}
387 
388 
389 
390  if (nl == 0)
391  return P->stack[type][type_2]; /* stack */
392 
393  if (ns==0) { /* bulge */
394  if(type < 8){
395  energy = (nl<=MAXLOOP) ? P->bulge[nl] : (P->bulge[30]+(int)(P->lxc*log(nl/30.)));
396  } else if(type < 16) {
397  energy = (nl<=MAXLOOP) ? P->bulge_RD[nl] : (P->bulge_RD[30]+(int)(P->lxc_RD*log(nl/30.)));
398  } else {
399  energy = (nl<=MAXLOOP) ? P->bulge_D[nl] : (P->bulge_D[30]+(int)(P->lxc_D*log(nl/30.)));
400  }
401  if (nl==1) energy += P->stack[type][type_2];
402  else {
403  energy += P->TerminalAU_t[type];
404  energy += P->TerminalAU_t[type_2];
405  }
406  return energy;
407  }
408  else { /* interior loop */
409  if (ns==1) {
410  if (nl==1) /* 1x1 loop */
411  return P->int11[type][type_2][si1][sj1];
412  if (nl==2) { /* 2x1 loop */
413  if (n1==1)
414  energy = P->int21[type][type_2][si1][sq1][sj1];
415  else
416  energy = P->int21[type_2][type][sq1][si1][sp1];
417  return energy;
418  }
419  else { /* 1xn loop */
420  if(type < 8){
421  energy = (nl+1<=MAXLOOP)?(P->internal_loop[nl+1]) : (P->internal_loop[30]+(int)(P->lxc*log((nl+1)/30.)));
422  energy += MIN2(MAX_NINIO, (nl-ns)*P->ninio[2]);
423  } else if(type < 16){
424  energy = (nl+1<=MAXLOOP)?(P->internal_loop_RD[nl+1]) : (P->internal_loop_RD[30]+(int)(P->lxc_RD*log((nl+1)/30.)));
425  energy += MIN2(MAX_NINIO_RD, (nl-ns)*P->ninio_RD[2]);
426  } else {
427  energy = (nl+1<=MAXLOOP)?(P->internal_loop_D[nl+1]) : (P->internal_loop_D[30]+(int)(P->lxc_D*log((nl+1)/30.)));
428  energy += MIN2(MAX_NINIO_D, (nl-ns)*P->ninio_D[2]);
429  }
430  energy += P->mismatch1nI[type][si1][sj1] + P->mismatch1nI[type_2][sq1][sp1];
431  return energy;
432  }
433  }
434  else if (ns==2) {
435  if(nl==2) { /* 2x2 loop */
436  return P->int22[type][type_2][si1][sp1][sq1][sj1];}
437  else if (nl==3){ /* 2x3 loop */
438  if(type < 8){
439  energy = P->internal_loop[5]+P->ninio[2];
440  } else if(type < 16){
441  energy = P->internal_loop_RD[5]+P->ninio_RD[2];
442  } else {
443  energy = P->internal_loop_D[5]+P->ninio_D[2];
444  }
445  energy += P->mismatch23I[type][si1][sj1] + P->mismatch23I[type_2][sq1][sp1];
446  return energy;
447  }
448 
449  }
450  { /* generic interior loop (no else here!)*/
451  if(type < 8){
452  energy = (n1+n2<=MAXLOOP)?(P->internal_loop[n1+n2]) : (P->internal_loop[30]+(int)(P->lxc*log((n1+n2)/30.)));
453  energy += MIN2(MAX_NINIO, (nl-ns)*P->ninio[2]);
454  } else if(type < 16){
455  energy = (n1+n2<=MAXLOOP)?(P->internal_loop_RD[n1+n2]) : (P->internal_loop_RD[30]+(int)(P->lxc_RD*log((n1+n2)/30.)));
456  energy += MIN2(MAX_NINIO_RD, (nl-ns)*P->ninio_RD[2]);
457  } else {
458  energy = (n1+n2<=MAXLOOP)?(P->internal_loop_D[n1+n2]) : (P->internal_loop_D[30]+(int)(P->lxc_D*log((n1+n2)/30.)));
459  energy += MIN2(MAX_NINIO_D, (nl-ns)*P->ninio_D[2]);
460  }
461  energy += P->mismatchI[type][si1][sj1] + P->mismatchI[type_2][sq1][sp1];
462  }
463  }
464  return energy;
465 }
466 
467 INLINE PRIVATE int E_Stem(int type, int si1, int sj1, int extLoop, paramT *P){
468  int energy = 0;
469  int d5 = (si1 >= 0) ? P->dangle5[type][si1] : 0;
470  int d3 = (sj1 >= 0) ? P->dangle3[type][sj1] : 0;
471 
472  energy += P->TerminalAU_t[type];
473 
474  if(si1 >= 0 && sj1 >= 0)
475  energy += (extLoop) ? P->mismatchExt[type][si1][sj1] : P->mismatchM[type][si1][sj1];
476  else
477  energy += d5 + d3;
478 
479  if(!extLoop) energy += P->MLintern[type];
480  return energy;
481 }
482 
483 INLINE PRIVATE int E_ExtLoop(int type, int si1, int sj1, paramT *P){
484  int energy = 0;
485  if(si1 >= 0 && sj1 >= 0){
486  energy += P->mismatchExt[type][si1][sj1];
487  }
488  else if (si1 >= 0){
489  energy += P->dangle5[type][si1];
490  }
491  else if (sj1 >= 0){
492  energy += P->dangle3[type][sj1];
493  }
494 
495  energy += P->TerminalAU_t[type];
496 
497  return energy;
498 }
499 
500 INLINE PRIVATE int E_MLstem(int type, int si1, int sj1, paramT *P){
501  int energy = 0;
502  if(si1 >= 0 && sj1 >= 0){
503  energy += P->mismatchM[type][si1][sj1];
504  }
505  else if (si1 >= 0){
506  energy += P->dangle5[type][si1];
507  }
508  else if (sj1 >= 0){
509  energy += P->dangle3[type][sj1];
510  }
511 
512  energy += P->TerminalAU_t[type];
513 
514  energy += P->MLintern[type];
515 
516  return energy;
517 }
518 
519 INLINE PRIVATE double exp_E_Hairpin(int u, int type, short si1, short sj1, const char *string, pf_paramT *P){
520  double q, kT;
521  kT = P->kT; /* kT in cal/mol */
522 
523  if(type < 8){
524  q = (u <= 30) ? P->exphairpin[u] : P->exphairpin[30] * exp( -(P->lxc*log( u/30.))*10./kT);
525 
526  if(u < 3) return q; /* should only be the case when folding alignments */
527 
528  if(P->model_details.special_hp){
529  if(u==4){
530  char tl[7]={0,0,0,0,0,0,0}, *ts;
531  strncpy(tl, string, 6);
532  if ((ts=strstr(P->Tetraloops, tl))){
533  if(type != 7)
534  return (P->exptetra[(ts-P->Tetraloops)/7]);
535  else
536  q *= P->exptetra[(ts-P->Tetraloops)/7];
537  }
538  }
539  if(u==6){
540  char tl[9]={0,0,0,0,0,0,0,0,0}, *ts;
541  strncpy(tl, string, 8);
542  if ((ts=strstr(P->Hexaloops, tl)))
543  return (P->exphex[(ts-P->Hexaloops)/9]);
544  }
545  if (u==3) {
546  char tl[6]={0,0,0,0,0,0}, *ts;
547  strncpy(tl, string, 5);
548  if ((ts=strstr(P->Triloops, tl)))
549  return (P->exptri[(ts-P->Triloops)/6]);
550  if (type>2)
551  return q * P->expTermAU_t[type];
552  return q;
553  }
554  }
555  } else if (type > 16){
556  q = (u <= 30) ? P->exphairpin_D[u] : P->exphairpin_D[30] * exp( -(P->lxc_D*log( u/30.))*10./kT);
557 
558  if(u < 3) return q; /* should only be the case when folding alignments */
559 
560  if(P->model_details.special_hp){
561  if(u==4){
562  char tl[7]={0,0,0,0,0,0,0}, *ts;
563  strncpy(tl, string, 6);
564  if ((ts=strstr(P->Tetraloops_D, tl))){
565  if(type != 7)
566  return (P->exptetra_D[(ts-P->Tetraloops_D)/7]);
567  else
568  q *= P->exptetra_D[(ts-P->Tetraloops_D)/7];
569  }
570  }
571  if(u==6){
572  char tl[9]={0,0,0,0,0,0,0,0,0}, *ts;
573  strncpy(tl, string, 8);
574  if ((ts=strstr(P->Hexaloops_D, tl)))
575  return (P->exphex_D[(ts-P->Hexaloops_D)/9]);
576  }
577  if (u==3) {
578  char tl[6]={0,0,0,0,0,0}, *ts;
579  strncpy(tl, string, 5);
580  if ((ts=strstr(P->Triloops_D, tl)))
581  return (P->exptri_D[(ts-P->Triloops_D)/6]);
582  return (q * P->expTermAU_t[type]);
583  }
584  }
585  } else {;}
586 
587  q *= P->expmismatchH[type][si1][sj1];
588 
589  return q;
590 }
591 
592 INLINE PRIVATE double exp_E_IntLoop(int u1, int u2, int type, int type2, short si1, short sj1, short sp1, short sq1, pf_paramT *P){
593  int ul, us, no_close = 0;
594  double z = 0.;
595 
596  if ((no_closingGU) && ((type2==3)||(type2==4)||(type==3)||(type==4)))
597  no_close = 1;
598 
599  if (u1>u2) { ul=u1; us=u2;}
600  else {ul=u2; us=u1;}
601 
602  if (ul==0) /* stack */
603  z = P->expstack[type][type2];
604  else if(!no_close){
605  if (us==0) { /* bulge */
606  if(type < 8){
607  z = P->expbulge[ul];
608  } else if (type < 16){
609  z = P->expbulge_RD[ul];
610  } else {
611  z = P->expbulge_D[ul];
612  }
613  if (ul==1) z *= P->expstack[type][type2];
614  else {
615  if (type>2) z *= P->expTermAU_t[type];
616  if (type2>2) z *= P->expTermAU_t[type2];
617  }
618  return z;
619  }
620  else if (us==1) {
621  if (ul==1){ /* 1x1 loop */
622  return P->expint11[type][type2][si1][sj1];
623  }
624  if (ul==2) { /* 2x1 loop */
625  if (u1==1)
626  return P->expint21[type][type2][si1][sq1][sj1];
627  else
628  return P->expint21[type2][type][sq1][si1][sp1];
629  }
630  else { /* 1xn loop */
631  if(type < 8){
632  z = P->expinternal[ul+us] * P->expmismatch1nI[type][si1][sj1] * P->expmismatch1nI[type2][sq1][sp1];
633  return z * P->expninio[2][ul-us];
634  } else if (type < 16){
635  z = P->expinternal_RD[ul+us] * P->expmismatch1nI[type][si1][sj1] * P->expmismatch1nI[type2][sq1][sp1];
636  return z * P->expninio_RD[2][ul-us];
637  } else {
638  z = P->expinternal_D[ul+us] * P->expmismatch1nI[type][si1][sj1] * P->expmismatch1nI[type2][sq1][sp1];
639  return z * P->expninio_D[2][ul-us];
640  }
641  }
642  }
643  else if (us==2) {
644  if(ul==2) /* 2x2 loop */
645  return P->expint22[type][type2][si1][sp1][sq1][sj1];
646  else if(ul==3){ /* 2x3 loop */
647  if(type < 8){
648  z = P->expinternal[5]*P->expmismatch23I[type][si1][sj1]*P->expmismatch23I[type2][sq1][sp1];
649  return z * P->expninio[2][1];
650  } else if (type < 16){
651  z = P->expinternal_RD[5]*P->expmismatch23I[type][si1][sj1]*P->expmismatch23I[type2][sq1][sp1];
652  return z * P->expninio_RD[2][1];
653  } else {
654  z = P->expinternal_D[5]*P->expmismatch23I[type][si1][sj1]*P->expmismatch23I[type2][sq1][sp1];
655  return z * P->expninio_D[2][1];
656  }
657  }
658  }
659  /* generic interior loop (no else here!)*/
660  if(type < 8){
661  z = P->expinternal[ul+us] * P->expmismatchI[type][si1][sj1] * P->expmismatchI[type2][sq1][sp1];
662  return z * P->expninio[2][ul-us];
663  } else if (type < 16){
664  z = P->expinternal_RD[ul+us] * P->expmismatchI[type][si1][sj1] * P->expmismatchI[type2][sq1][sp1];
665  return z * P->expninio_RD[2][ul-us];
666  } else {
667  z = P->expinternal_D[ul+us] * P->expmismatchI[type][si1][sj1] * P->expmismatchI[type2][sq1][sp1];
668  return z * P->expninio_D[2][ul-us];
669  }
670  }
671  return z;
672 }
673 
674 INLINE PRIVATE double exp_E_Stem(int type, int si1, int sj1, int extLoop, pf_paramT *P){
675  double energy = 1.0;
676  double d5 = (si1 >= 0) ? P->expdangle5[type][si1] : 1.;
677  double d3 = (sj1 >= 0) ? P->expdangle3[type][sj1] : 1.;
678 
679  if(type > 2)
680  energy *= P->expTermAU_t[type];
681 
682  if(si1 >= 0 && sj1 >= 0)
683  energy *= (extLoop) ? P->expmismatchExt[type][si1][sj1] : P->expmismatchM[type][si1][sj1];
684  else
685  energy *= d5 * d3;
686 
687  if(!extLoop) energy *= P->expMLintern[type];
688  return energy;
689 }
690 
691 INLINE PRIVATE double exp_E_MLstem(int type, int si1, int sj1, pf_paramT *P){
692  double energy = 1.0;
693  if(si1 >= 0 && sj1 >= 0){
694  energy *= P->expmismatchM[type][si1][sj1];
695  }
696  else if(si1 >= 0){
697  energy *= P->expdangle5[type][si1];
698  }
699  else if(sj1 >= 0){
700  energy *= P->expdangle3[type][sj1];
701  }
702 
703  if(type > 2)
704  energy *= P->expTermAU_t[type];
705 
706  energy *= P->expMLintern[type];
707  return energy;
708 }
709 
710 INLINE PRIVATE double exp_E_ExtLoop(int type, int si1, int sj1, pf_paramT *P){
711  double energy = 1.0;
712  if(si1 >= 0 && sj1 >= 0){
713  energy *= P->expmismatchExt[type][si1][sj1];
714  }
715  else if(si1 >= 0){
716  energy *= P->expdangle5[type][si1];
717  }
718  else if(sj1 >= 0){
719  energy *= P->expdangle3[type][sj1];
720  }
721 
722  if(type > 2)
723  energy *= P->expTermAU_t[type];
724 
725  return energy;
726 }
727 
728 INLINE PRIVATE int E_IntLoop_Co(int type, int type_2, int i, int j, int p, int q, int cutpoint, short si1, short sj1, short sp1, short sq1, int dangles, paramT *P){
729  int energy = 0;
730  energy += P->TerminalAU_t[type];
731  energy += P->TerminalAU_t[type_2];
732 
733  if(!dangles) return energy;
734 
735  int ci = (i>=cutpoint)||((i+1)<cutpoint);
736  int cj = ((j-1)>=cutpoint)||(j<cutpoint);
737  int cp = ((p-1)>=cutpoint)||(p<cutpoint);
738  int cq = (q>=cutpoint)||((q+1)<cutpoint);
739 
740  int d3 = ci ? P->dangle3[type][si1] : 0;
741  int d5 = cj ? P->dangle5[type][sj1] : 0;
742  int d5_2 = cp ? P->dangle5[type_2][sp1] : 0;
743  int d3_2 = cq ? P->dangle3[type_2][sq1] : 0;
744 
745  int tmm = (cj && ci) ? P->mismatchExt[type][sj1][si1] : d5 + d3;
746  int tmm_2 = (cp && cq) ? P->mismatchExt[type_2][sp1][sq1] : d5_2 + d3_2;
747 
748  if(dangles == 2) return energy + tmm + tmm_2;
749 
750  /* now we may have non-double dangles only */
751  if(i+2 < p){
752  if(q+2 < j){ energy += tmm + tmm_2;}
753  else if(q+2 == j){ energy += (cj && cq) ? MIN2(tmm + d5_2, tmm_2 + d3) : tmm + tmm_2;}
754  else energy += d3 + d5_2;
755  }
756  else if(i+2 == p){
757  if(q+2 < j){ energy += (ci && cp) ? MIN2(tmm + d3_2, tmm_2 + d5) : tmm + tmm_2;}
758  else if(q+2 == j){
759  energy += MIN2(tmm, MIN2(tmm_2, MIN2(d5 + d5_2, d3 + d3_2)));
760  }
761  else energy += MIN2(d3, d5_2);
762  }
763  else{
764  if(q+2 < j){ energy += d5 + d3_2;}
765  else if(q+2 == j){ energy += MIN2(d5, d3_2);}
766  }
767  return energy;
768 }
769 
770 #endif
#define MIN2(A, B)
Definition: utils.h:134
PRIVATE int E_Hairpin(int size, int type, int si1, int sj1, const char *string, paramT *P)
Definition: loop_energies.h:322
PRIVATE double exp_E_IntLoop(int u1, int u2, int type, int type2, short si1, short sj1, short sp1, short sq1, pf_paramT *P)
Definition: loop_energies.h:592
#define MAXLOOP
Definition: energy_const.h:28
int special_hp
Include special hairpin contributions for tri, tetra and hexaloops.
Definition: data_structures.h:109
The datastructure that contains temperature scaled energy parameters.
Definition: data_structures.h:122
model_detailsT model_details
Model details to be used in the recursions.
Definition: data_structures.h:196
The datastructure that contains temperature scaled Boltzmann weights of the energy parameters...
Definition: data_structures.h:203
PRIVATE int E_Stem(int type, int si1, int sj1, int extLoop, paramT *P)
Definition: loop_energies.h:467
PRIVATE double exp_E_Hairpin(int u, int type, short si1, short sj1, const char *string, pf_paramT *P)
Definition: loop_energies.h:519
#define INF
Definition: energy_const.h:16
PRIVATE int E_IntLoop(int n1, int n2, int type, int type_2, int si1, int sj1, int sp1, int sq1, paramT *P)
Definition: loop_energies.h:380
Here all all declarations of the global variables used throughout RNAlib.
int no_closingGU
GU allowed only inside stacks if set to 1.
int dangles
Switch the energy model for dangling end contributions (0, 1, 2, 3)
model_detailsT model_details
Model details to be used in the recursions.
Definition: data_structures.h:286
PRIVATE double exp_E_Stem(int type, int si1, int sj1, int extLoop, pf_paramT *P)
Definition: loop_energies.h:674