RNAlib-2.3.1
interior_loops.h
Go to the documentation of this file.
1 #ifndef VIENNA_RNA_PACKAGE_INTERIOR_LOOPS_H
2 #define VIENNA_RNA_PACKAGE_INTERIOR_LOOPS_H
3 
4 #include <ViennaRNA/utils.h>
5 #include "ViennaRNA/energy_par.h"
7 #include <ViennaRNA/params.h>
9 
10 #ifdef __GNUC__
11 # define INLINE inline
12 #else
13 # define INLINE
14 #endif
15 
16 #ifdef ON_SAME_STRAND
17 #undef ON_SAME_STRAND
18 #endif
19 
20 #define ON_SAME_STRAND(I,J,C) (((I)>=(C))||((J)<(C)))
21 
77 PRIVATE INLINE int E_IntLoop(int n1,
78  int n2,
79  int type,
80  int type_2,
81  int si1,
82  int sj1,
83  int sp1,
84  int sq1,
85  vrna_param_t *P);
86 
106 PRIVATE INLINE FLT_OR_DBL exp_E_IntLoop(int u1,
107  int u2,
108  int type,
109  int type2,
110  short si1,
111  short sj1,
112  short sp1,
113  short sq1,
114  vrna_exp_param_t *P);
115 
116 
117 PRIVATE INLINE int E_IntLoop_Co(int type,
118  int type_2,
119  int i,
120  int j,
121  int p,
122  int q,
123  int cutpoint,
124  short si1,
125  short sj1,
126  short sp1,
127  short sq1,
128  int dangles,
129  vrna_param_t *P);
130 
131 
132 /*
133 #################################
134 # BEGIN OF FUNCTION DEFINITIONS #
135 #################################
136 */
137 
138 /*
139  * ugly but fast interior loop evaluation
140  *
141  * Avoid including this function in your own code. It only serves
142  * as a fast inline block internally re-used throughout the RNAlib. It
143  * evalutes the free energy of interior loops in single sequences or sequence
144  * hybrids. Soft constraints are also applied if available.
145  *
146  * NOTE: do not include into doxygen reference manual!
147  */
148 PRIVATE INLINE int
149 ubf_eval_int_loop( int i,
150  int j,
151  int p,
152  int q,
153  int i1,
154  int j1,
155  int p1,
156  int q1,
157  short si,
158  short sj,
159  short sp,
160  short sq,
161  unsigned char type,
162  unsigned char type_2,
163  int *rtype,
164  int ij,
165  int cp,
166  vrna_param_t *P,
167  vrna_sc_t *sc){
168 
169  int energy, u1, u2;
170 
171  u1 = p1 - i;
172  u2 = j1 - q;
173 
174  if((cp < 0) || (ON_SAME_STRAND(i, p, cp) && ON_SAME_STRAND(q, j, cp))){ /* regular interior loop */
175  energy = E_IntLoop(u1, u2, type, type_2, si, sj, sp, sq, P);
176  } else { /* interior loop like cofold structure */
177  short Si, Sj;
178  Si = ON_SAME_STRAND(i, i1, cp) ? si : -1;
179  Sj = ON_SAME_STRAND(j1, j, cp) ? sj : -1;
180  energy = E_IntLoop_Co(rtype[type], rtype[type_2],
181  i, j, p, q,
182  cp,
183  Si, Sj,
184  sp, sq,
186  P);
187  }
188 
189  /* add soft constraints */
190  if(sc){
191  if(sc->energy_up)
192  energy += sc->energy_up[i1][u1]
193  + sc->energy_up[q1][u2];
194 
195  if(sc->energy_bp)
196  energy += sc->energy_bp[ij];
197 
198  if(sc->energy_stack)
199  if(u1 + u2 == 0){
200  int a = sc->energy_stack[i]
201  + sc->energy_stack[p]
202  + sc->energy_stack[q]
203  + sc->energy_stack[j];
204  energy += a;
205  }
206  if(sc->f)
207  energy += sc->f(i, j, p, q, VRNA_DECOMP_PAIR_IL, sc->data);
208  }
209 
210  return energy;
211 
212 }
213 
214 /*
215  * ugly but fast exterior interior loop evaluation
216  *
217  * Avoid including this function in your own code. It only serves
218  * as a fast inline block internally re-used throughout the RNAlib. It
219  * evalutes the free energy of interior loops in single sequences or sequence
220  * hybrids. Soft constraints are also applied if available.
221  *
222  * NOTE: do not include into doxygen reference manual!
223  */
224 PRIVATE INLINE int
225 ubf_eval_ext_int_loop(int i,
226  int j,
227  int p,
228  int q,
229  int i1,
230  int j1,
231  int p1,
232  int q1,
233  short si,
234  short sj,
235  short sp,
236  short sq,
237  unsigned char type,
238  unsigned char type_2,
239  int length,
240  vrna_param_t *P,
241  vrna_sc_t *sc){
242 
243  int energy, u1, u2, u3;
244 
245  u1 = i1;
246  u2 = p1 - j;
247  u3 = length - q;
248 
249  energy = E_IntLoop(u2, u1 + u3, type, type_2, si, sj, sp, sq, P);
250 
251  /* add soft constraints */
252  if(sc){
253  if(sc->energy_up){
254  energy += sc->energy_up[j1][u2]
255  + ((u3 > 0) ? sc->energy_up[q1][u3] : 0)
256  + ((u1 > 0) ? sc->energy_up[1][u1] : 0);
257  }
258  if(sc->energy_stack)
259  if(u1 + u2 + u3 == 0)
260  energy += sc->energy_stack[i]
261  + sc->energy_stack[p]
262  + sc->energy_stack[q]
263  + sc->energy_stack[j];
264 
265  if(sc->f)
266  energy += sc->f(i, j, p, q, VRNA_DECOMP_PAIR_IL, sc->data);
267  }
268 
269  return energy;
270 
271 }
272 
273 PRIVATE INLINE int
274 E_IntLoop(int n1,
275  int n2,
276  int type,
277  int type_2,
278  int si1,
279  int sj1,
280  int sp1,
281  int sq1,
282  vrna_param_t *P){
283 
284  /* compute energy of degree 2 loop (stack bulge or interior) */
285  int nl, ns, u, energy;
286  energy = INF;
287 
288  if (n1>n2) { nl=n1; ns=n2;}
289  else {nl=n2; ns=n1;}
290 
291  if (nl == 0)
292  return P->stack[type][type_2]; /* stack */
293 
294  if (ns==0) { /* bulge */
295  energy = (nl<=MAXLOOP)?P->bulge[nl]:
296  (P->bulge[30]+(int)(P->lxc*log(nl/30.)));
297  if (nl==1) energy += P->stack[type][type_2];
298  else {
299  if (type>2) energy += P->TerminalAU;
300  if (type_2>2) energy += P->TerminalAU;
301  }
302  return energy;
303  }
304  else { /* interior loop */
305  if (ns==1) {
306  if (nl==1) /* 1x1 loop */
307  return P->int11[type][type_2][si1][sj1];
308  if (nl==2) { /* 2x1 loop */
309  if (n1==1)
310  energy = P->int21[type][type_2][si1][sq1][sj1];
311  else
312  energy = P->int21[type_2][type][sq1][si1][sp1];
313  return energy;
314  }
315  else { /* 1xn loop */
316  energy = (nl+1<=MAXLOOP)?(P->internal_loop[nl+1]) : (P->internal_loop[30]+(int)(P->lxc*log((nl+1)/30.)));
317  energy += MIN2(MAX_NINIO, (nl-ns)*P->ninio[2]);
318  energy += P->mismatch1nI[type][si1][sj1] + P->mismatch1nI[type_2][sq1][sp1];
319  return energy;
320  }
321  }
322  else if (ns==2) {
323  if(nl==2) { /* 2x2 loop */
324  return P->int22[type][type_2][si1][sp1][sq1][sj1];}
325  else if (nl==3){ /* 2x3 loop */
326  energy = P->internal_loop[5]+P->ninio[2];
327  energy += P->mismatch23I[type][si1][sj1] + P->mismatch23I[type_2][sq1][sp1];
328  return energy;
329  }
330 
331  }
332  { /* generic interior loop (no else here!)*/
333  u = nl + ns;
334  energy = (u <= MAXLOOP) ? (P->internal_loop[u]) : (P->internal_loop[30]+(int)(P->lxc*log((u)/30.)));
335 
336  energy += MIN2(MAX_NINIO, (nl-ns)*P->ninio[2]);
337 
338  energy += P->mismatchI[type][si1][sj1] + P->mismatchI[type_2][sq1][sp1];
339  }
340  }
341  return energy;
342 }
343 
344 PRIVATE INLINE FLT_OR_DBL
346  int u2,
347  int type,
348  int type2,
349  short si1,
350  short sj1,
351  short sp1,
352  short sq1,
353  vrna_exp_param_t *P){
354 
355  int ul, us, no_close = 0;
356  double z = 0.;
357  int noGUclosure = P->model_details.noGUclosure;
358 
359  if ((noGUclosure) && ((type2==3)||(type2==4)||(type==3)||(type==4)))
360  no_close = 1;
361 
362  if (u1>u2) { ul=u1; us=u2;}
363  else {ul=u2; us=u1;}
364 
365  if (ul==0) /* stack */
366  z = P->expstack[type][type2];
367  else if(!no_close){
368  if (us==0) { /* bulge */
369  z = P->expbulge[ul];
370  if (ul==1) z *= P->expstack[type][type2];
371  else {
372  if (type>2) z *= P->expTermAU;
373  if (type2>2) z *= P->expTermAU;
374  }
375  return (FLT_OR_DBL)z;
376  }
377  else if (us==1) {
378  if (ul==1){ /* 1x1 loop */
379  return (FLT_OR_DBL)(P->expint11[type][type2][si1][sj1]);
380  }
381  if (ul==2) { /* 2x1 loop */
382  if (u1==1)
383  return (FLT_OR_DBL)(P->expint21[type][type2][si1][sq1][sj1]);
384  else
385  return (FLT_OR_DBL)(P->expint21[type2][type][sq1][si1][sp1]);
386  }
387  else { /* 1xn loop */
388  z = P->expinternal[ul+us] * P->expmismatch1nI[type][si1][sj1] * P->expmismatch1nI[type2][sq1][sp1];
389  return (FLT_OR_DBL)(z * P->expninio[2][ul-us]);
390  }
391  }
392  else if (us==2) {
393  if(ul==2) /* 2x2 loop */
394  return (FLT_OR_DBL)(P->expint22[type][type2][si1][sp1][sq1][sj1]);
395  else if(ul==3){ /* 2x3 loop */
396  z = P->expinternal[5]*P->expmismatch23I[type][si1][sj1]*P->expmismatch23I[type2][sq1][sp1];
397  return (FLT_OR_DBL)(z * P->expninio[2][1]);
398  }
399  }
400  /* generic interior loop (no else here!)*/
401  z = P->expinternal[ul+us] * P->expmismatchI[type][si1][sj1] * P->expmismatchI[type2][sq1][sp1];
402  return (FLT_OR_DBL)(z * P->expninio[2][ul-us]);
403 
404  }
405  return (FLT_OR_DBL)z;
406 }
407 
408 PRIVATE INLINE int
409 E_IntLoop_Co( int type,
410  int type_2,
411  int i,
412  int j,
413  int p,
414  int q,
415  int cutpoint,
416  short si1,
417  short sj1,
418  short sp1,
419  short sq1,
420  int dangles,
421  vrna_param_t *P){
422 
423  int energy, ci, cj, cp, cq, d3, d5, d5_2, d3_2, tmm, tmm_2;
424 
425  energy = 0;
426  if(type > 2) energy += P->TerminalAU;
427  if(type_2 > 2) energy += P->TerminalAU;
428 
429  if(!dangles) return energy;
430 
431  ci = ON_SAME_STRAND(i, i + 1, cutpoint);
432  cj = ON_SAME_STRAND(j - 1, j, cutpoint);
433  cp = ON_SAME_STRAND(p - 1, p, cutpoint);
434  cq = ON_SAME_STRAND(q, q + 1, cutpoint);
435 
436  d3 = ci ? P->dangle3[type][si1] : 0;
437  d5 = cj ? P->dangle5[type][sj1] : 0;
438  d5_2 = cp ? P->dangle5[type_2][sp1] : 0;
439  d3_2 = cq ? P->dangle3[type_2][sq1] : 0;
440 
441  tmm = (cj && ci) ? P->mismatchExt[type][sj1][si1] : d5 + d3;
442  tmm_2 = (cp && cq) ? P->mismatchExt[type_2][sp1][sq1] : d5_2 + d3_2;
443 
444  if(dangles == 2) return energy + tmm + tmm_2;
445 
446  /* now we may have non-double dangles only */
447  if(i+2 < p){
448  if(q+2 < j){ energy += tmm + tmm_2;}
449  else if(q+2 == j){ energy += (cj && cq) ? MIN2(tmm + d5_2, tmm_2 + d3) : tmm + tmm_2;}
450  else energy += d3 + d5_2;
451  }
452  else if(i+2 == p){
453  if(q+2 < j){ energy += (ci && cp) ? MIN2(tmm + d3_2, tmm_2 + d5) : tmm + tmm_2;}
454  else if(q+2 == j){
455  energy += MIN2(tmm, MIN2(tmm_2, MIN2(d5 + d5_2, d3 + d3_2)));
456  }
457  else energy += MIN2(d3, d5_2);
458  }
459  else{
460  if(q+2 < j){ energy += d5 + d3_2;}
461  else if(q+2 == j){ energy += MIN2(d5, d3_2);}
462  }
463  return energy;
464 }
465 
466 int
467 vrna_E_int_loop(vrna_fold_compound_t *vc,
468  int i,
469  int j);
470 
471 int
473  int i,
474  int j,
475  int k,
476  int l);
477 
479 vrna_exp_E_int_loop(vrna_fold_compound_t *vc,
480  int i,
481  int j);
482 
484 vrna_exp_E_interior_loop( vrna_fold_compound_t *vc,
485  int i,
486  int j,
487  int k,
488  int l);
489 
490 int
491 vrna_E_ext_int_loop(vrna_fold_compound_t *vc,
492  int i,
493  int j,
494  int *ip,
495  int *iq);
496 
497 int
498 vrna_E_stack( vrna_fold_compound_t *vc,
499  int i,
500  int j);
501 
502 
507 int
509  int *i,
510  int *j,
511  int *en,
512  vrna_bp_stack_t *bp_stack,
513  int *stack_count);
518 int
520  int *i,
521  int *j,
522  int en,
523  vrna_bp_stack_t *bp_stack,
524  int *stack_count);
525 
526 
532 #endif
int dangles
Specifies the dangle model used in any energy evaluation (0,1,2 or 3)
Definition: model.h:194
#define MAXLOOP
Definition: energy_const.h:29
vrna_md_t model_details
Model details to be used in the recursions.
Definition: params.h:93
vrna_md_t model_details
Model details to be used in the recursions.
Definition: params.h:149
double FLT_OR_DBL
Typename for floating point number in partition function computations.
Definition: data_structures.h:48
The most basic data structure required by many functions throughout the RNAlib.
Definition: data_structures.h:454
int noGUclosure
Do not allow loops to be closed by GU pair.
Definition: model.h:221
The datastructure that contains temperature scaled energy parameters.
Definition: params.h:55
int * energy_stack
Pseudo Energy contribution per base pair involved in a stack.
Definition: constraints_soft.h:107
vrna_callback_sc_energy * f
A function pointer used for pseudo energy contribution in MFE calculations.
Definition: constraints_soft.h:111
General utility- and helper-functions used throughout the ViennaRNA Package.
int vrna_eval_int_loop(vrna_fold_compound_t *vc, int i, int j, int k, int l)
Various data structures and pre-processor macros.
The soft constraints data structure.
Definition: constraints_soft.h:101
#define INF
Definition: energy_const.h:17
Functions to deal with sets of energy parameters.
The data structure that contains temperature scaled Boltzmann weights of the energy parameters...
Definition: params.h:99
int ** energy_up
Energy contribution for stretches of unpaired nucleotides.
Definition: constraints_soft.h:102
#define VRNA_DECOMP_PAIR_IL
Indicator for interior loop decomposition step.
Definition: constraints.h:72
Base pair stack element.
Definition: data_structures.h:222
Functions and data structures for constraining secondary structure predictions and evaluation...
int * energy_bp
Energy contribution for base pairs.
Definition: constraints_soft.h:103
int vrna_BT_stack(vrna_fold_compound_t *vc, int *i, int *j, int *en, vrna_bp_stack_t *bp_stack, int *stack_count)
Backtrack a stacked pair closed by .
PRIVATE int E_IntLoop(int n1, int n2, int type, int type_2, int si1, int sj1, int sp1, int sq1, vrna_param_t *P)
Definition: interior_loops.h:274
#define MIN2(A, B)
Get the minimum of two comparable values.
Definition: utils.h:115
int vrna_BT_int_loop(vrna_fold_compound_t *vc, int *i, int *j, int en, vrna_bp_stack_t *bp_stack, int *stack_count)
Backtrack an interior loop closed by .
int dangles
Switch the energy model for dangling end contributions (0, 1, 2, 3)
PRIVATE FLT_OR_DBL exp_E_IntLoop(int u1, int u2, int type, int type2, short si1, short sj1, short sp1, short sq1, vrna_exp_param_t *P)
Definition: interior_loops.h:345
void * data
A pointer to the data object provided for for pseudo energy contribution functions of the generic sof...
Definition: constraints_soft.h:128