RNAlib-2.4.4
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 
87 
107 PRIVATE INLINE FLT_OR_DBL exp_E_IntLoop(int u1,
108  int u2,
109  int type,
110  int type2,
111  short si1,
112  short sj1,
113  short sp1,
114  short sq1,
115  vrna_exp_param_t *P);
116 
117 
118 PRIVATE INLINE int E_IntLoop_Co(int type,
119  int type_2,
120  int i,
121  int j,
122  int p,
123  int q,
124  int cutpoint,
125  short si1,
126  short sj1,
127  short sp1,
128  short sq1,
129  int dangles,
130  vrna_param_t *P);
131 
132 
133 /*
134  #################################
135  # BEGIN OF FUNCTION DEFINITIONS #
136  #################################
137  */
138 
139 /*
140  * ugly but fast interior loop evaluation
141  *
142  * Avoid including this function in your own code. It only serves
143  * as a fast inline block internally re-used throughout the RNAlib. It
144  * evalutes the free energy of interior loops in single sequences or sequence
145  * hybrids. Soft constraints are also applied if available.
146  *
147  * NOTE: do not include into doxygen reference manual!
148  */
149 PRIVATE INLINE int
150 ubf_eval_int_loop(int i,
151  int j,
152  int p,
153  int q,
154  int i1,
155  int j1,
156  int p1,
157  int q1,
158  short si,
159  short sj,
160  short sp,
161  short sq,
162  unsigned char type,
163  unsigned char type_2,
164  int *rtype,
165  int ij,
166  int cp,
167  vrna_param_t *P,
168  vrna_sc_t *sc)
169 {
170  int energy, u1, u2;
171 
172  u1 = p1 - i;
173  u2 = j1 - q;
174 
175  if ((cp < 0) || (ON_SAME_STRAND(i, p, cp) && ON_SAME_STRAND(q, j, cp))) {
176  /* regular interior loop */
177  energy = E_IntLoop(u1, u2, type, type_2, si, sj, sp, sq, P);
178  } else {
179  /* interior loop like cofold structure */
180  short Si, Sj;
181  Si = ON_SAME_STRAND(i, i1, cp) ? si : -1;
182  Sj = ON_SAME_STRAND(j1, j, cp) ? sj : -1;
183  energy = E_IntLoop_Co(rtype[type], rtype[type_2],
184  i, j, p, q,
185  cp,
186  Si, Sj,
187  sp, sq,
189  P);
190  }
191 
192  /* add soft constraints */
193  if (sc) {
194  if (sc->energy_up)
195  energy += sc->energy_up[i1][u1]
196  + sc->energy_up[q1][u2];
197 
198  if (sc->energy_bp)
199  energy += sc->energy_bp[ij];
200 
201  if (sc->energy_stack)
202  if (u1 + u2 == 0) {
203  int a = sc->energy_stack[i]
204  + sc->energy_stack[p]
205  + sc->energy_stack[q]
206  + sc->energy_stack[j];
207  energy += a;
208  }
209 
210  if (sc->f)
211  energy += sc->f(i, j, p, q, VRNA_DECOMP_PAIR_IL, sc->data);
212  }
213 
214  return energy;
215 }
216 
217 
218 PRIVATE INLINE int
219 ubf_eval_int_loop2(int i,
220  int j,
221  int p,
222  int q,
223  int i1,
224  int j1,
225  int p1,
226  int q1,
227  short si,
228  short sj,
229  short sp,
230  short sq,
231  unsigned char type,
232  unsigned char type_2,
233  int *rtype,
234  int ij,
235  unsigned int *sn,
236  unsigned int *ss,
237  vrna_param_t *P,
238  vrna_sc_t *sc)
239 {
240  int energy, u1, u2;
241 
242  u1 = p1 - i;
243  u2 = j1 - q;
244 
245  if ((sn[i] == sn[p]) && (sn[q] == sn[j])) {
246  /* regular interior loop */
247  energy = E_IntLoop(u1, u2, type, type_2, si, sj, sp, sq, P);
248  } else {
249  /* interior loop like cofold structure */
250  short Si, Sj;
251  Si = (sn[i1] == sn[i]) ? si : -1;
252  Sj = (sn[j] == sn[j1]) ? sj : -1;
253  energy = E_IntLoop_Co(rtype[type], rtype[type_2],
254  i, j, p, q,
255  ss[1],
256  Si, Sj,
257  sp, sq,
259  P);
260  }
261 
262  /* add soft constraints */
263  if (sc) {
264  if (sc->energy_up)
265  energy += sc->energy_up[i1][u1]
266  + sc->energy_up[q1][u2];
267 
268  if (sc->energy_bp)
269  energy += sc->energy_bp[ij];
270 
271  if (sc->energy_stack)
272  if (u1 + u2 == 0) {
273  int a = sc->energy_stack[i]
274  + sc->energy_stack[p]
275  + sc->energy_stack[q]
276  + sc->energy_stack[j];
277  energy += a;
278  }
279 
280  if (sc->f)
281  energy += sc->f(i, j, p, q, VRNA_DECOMP_PAIR_IL, sc->data);
282  }
283 
284  return energy;
285 }
286 
287 
288 /*
289  * ugly but fast exterior interior loop evaluation
290  *
291  * Avoid including this function in your own code. It only serves
292  * as a fast inline block internally re-used throughout the RNAlib. It
293  * evalutes the free energy of interior loops in single sequences or sequence
294  * hybrids. Soft constraints are also applied if available.
295  *
296  * NOTE: do not include into doxygen reference manual!
297  */
298 PRIVATE INLINE int
299 ubf_eval_ext_int_loop(int i,
300  int j,
301  int p,
302  int q,
303  int i1,
304  int j1,
305  int p1,
306  int q1,
307  short si,
308  short sj,
309  short sp,
310  short sq,
311  unsigned char type,
312  unsigned char type_2,
313  int length,
314  vrna_param_t *P,
315  vrna_sc_t *sc)
316 {
317  int energy, u1, u2, u3;
318 
319  u1 = i1;
320  u2 = p1 - j;
321  u3 = length - q;
322 
323  energy = E_IntLoop(u2, u1 + u3, type, type_2, si, sj, sp, sq, P);
324 
325  /* add soft constraints */
326  if (sc) {
327  if (sc->energy_up) {
328  energy += sc->energy_up[j1][u2]
329  + ((u3 > 0) ? sc->energy_up[q1][u3] : 0)
330  + ((u1 > 0) ? sc->energy_up[1][u1] : 0);
331  }
332 
333  if (sc->energy_stack)
334  if (u1 + u2 + u3 == 0)
335  energy += sc->energy_stack[i]
336  + sc->energy_stack[p]
337  + sc->energy_stack[q]
338  + sc->energy_stack[j];
339 
340  if (sc->f)
341  energy += sc->f(i, j, p, q, VRNA_DECOMP_PAIR_IL, sc->data);
342  }
343 
344  return energy;
345 }
346 
347 
348 PRIVATE INLINE int
349 E_IntLoop(int n1,
350  int n2,
351  int type,
352  int type_2,
353  int si1,
354  int sj1,
355  int sp1,
356  int sq1,
357  vrna_param_t *P)
358 {
359  /* compute energy of degree 2 loop (stack bulge or interior) */
360  int nl, ns, u, energy;
361 
362  energy = INF;
363 
364  if (n1 > n2) {
365  nl = n1;
366  ns = n2;
367  } else {
368  nl = n2;
369  ns = n1;
370  }
371 
372  if (nl == 0)
373  return P->stack[type][type_2]; /* stack */
374 
375  if (ns == 0) {
376  /* bulge */
377  energy = (nl <= MAXLOOP) ? P->bulge[nl] :
378  (P->bulge[30] + (int)(P->lxc * log(nl / 30.)));
379  if (nl == 1) {
380  energy += P->stack[type][type_2];
381  } else {
382  if (type > 2)
383  energy += P->TerminalAU;
384 
385  if (type_2 > 2)
386  energy += P->TerminalAU;
387  }
388 
389  return energy;
390  } else {
391  /* interior loop */
392  if (ns == 1) {
393  if (nl == 1) /* 1x1 loop */
394  return P->int11[type][type_2][si1][sj1];
395 
396  if (nl == 2) {
397  /* 2x1 loop */
398  if (n1 == 1)
399  energy = P->int21[type][type_2][si1][sq1][sj1];
400  else
401  energy = P->int21[type_2][type][sq1][si1][sp1];
402 
403  return energy;
404  } else {
405  /* 1xn loop */
406  energy =
407  (nl + 1 <=
408  MAXLOOP) ? (P->internal_loop[nl + 1]) : (P->internal_loop[30] +
409  (int)(P->lxc * log((nl + 1) / 30.)));
410  energy += MIN2(MAX_NINIO, (nl - ns) * P->ninio[2]);
411  energy += P->mismatch1nI[type][si1][sj1] + P->mismatch1nI[type_2][sq1][sp1];
412  return energy;
413  }
414  } else if (ns == 2) {
415  if (nl == 2) {
416  /* 2x2 loop */
417  return P->int22[type][type_2][si1][sp1][sq1][sj1];
418  } else if (nl == 3) {
419  /* 2x3 loop */
420  energy = P->internal_loop[5] + P->ninio[2];
421  energy += P->mismatch23I[type][si1][sj1] + P->mismatch23I[type_2][sq1][sp1];
422  return energy;
423  }
424  }
425 
426  {
427  /* generic interior loop (no else here!)*/
428  u = nl + ns;
429  energy =
430  (u <=
431  MAXLOOP) ? (P->internal_loop[u]) : (P->internal_loop[30] + (int)(P->lxc * log((u) / 30.)));
432 
433  energy += MIN2(MAX_NINIO, (nl - ns) * P->ninio[2]);
434 
435  energy += P->mismatchI[type][si1][sj1] + P->mismatchI[type_2][sq1][sp1];
436  }
437  }
438 
439  return energy;
440 }
441 
442 
443 PRIVATE INLINE FLT_OR_DBL
445  int u2,
446  int type,
447  int type2,
448  short si1,
449  short sj1,
450  short sp1,
451  short sq1,
452  vrna_exp_param_t *P)
453 {
454  int ul, us, no_close = 0;
455  double z = 0.;
456  int noGUclosure = P->model_details.noGUclosure;
457 
458  if ((noGUclosure) && ((type2 == 3) || (type2 == 4) || (type == 3) || (type == 4)))
459  no_close = 1;
460 
461  if (u1 > u2) {
462  ul = u1;
463  us = u2;
464  } else {
465  ul = u2;
466  us = u1;
467  }
468 
469  if (ul == 0) {
470  /* stack */
471  z = P->expstack[type][type2];
472  } else if (!no_close) {
473  if (us == 0) {
474  /* bulge */
475  z = P->expbulge[ul];
476  if (ul == 1) {
477  z *= P->expstack[type][type2];
478  } else {
479  if (type > 2)
480  z *= P->expTermAU;
481 
482  if (type2 > 2)
483  z *= P->expTermAU;
484  }
485 
486  return (FLT_OR_DBL)z;
487  } else if (us == 1) {
488  if (ul == 1) /* 1x1 loop */
489  return (FLT_OR_DBL)(P->expint11[type][type2][si1][sj1]);
490 
491  if (ul == 2) {
492  /* 2x1 loop */
493  if (u1 == 1)
494  return (FLT_OR_DBL)(P->expint21[type][type2][si1][sq1][sj1]);
495  else
496  return (FLT_OR_DBL)(P->expint21[type2][type][sq1][si1][sp1]);
497  } else {
498  /* 1xn loop */
499  z = P->expinternal[ul + us] * P->expmismatch1nI[type][si1][sj1] *
500  P->expmismatch1nI[type2][sq1][sp1];
501  return (FLT_OR_DBL)(z * P->expninio[2][ul - us]);
502  }
503  } else if (us == 2) {
504  if (ul == 2) {
505  /* 2x2 loop */
506  return (FLT_OR_DBL)(P->expint22[type][type2][si1][sp1][sq1][sj1]);
507  } else if (ul == 3) {
508  /* 2x3 loop */
509  z = P->expinternal[5] * P->expmismatch23I[type][si1][sj1] *
510  P->expmismatch23I[type2][sq1][sp1];
511  return (FLT_OR_DBL)(z * P->expninio[2][1]);
512  }
513  }
514 
515  /* generic interior loop (no else here!)*/
516  z = P->expinternal[ul + us] * P->expmismatchI[type][si1][sj1] *
517  P->expmismatchI[type2][sq1][sp1];
518  return (FLT_OR_DBL)(z * P->expninio[2][ul - us]);
519  }
520 
521  return (FLT_OR_DBL)z;
522 }
523 
524 
525 PRIVATE INLINE int
526 E_IntLoop_Co(int type,
527  int type_2,
528  int i,
529  int j,
530  int p,
531  int q,
532  int cutpoint,
533  short si1,
534  short sj1,
535  short sp1,
536  short sq1,
537  int dangles,
538  vrna_param_t *P)
539 {
540  int energy, ci, cj, cp, cq, d3, d5, d5_2, d3_2, tmm, tmm_2;
541 
542  energy = 0;
543  if (type > 2)
544  energy += P->TerminalAU;
545 
546  if (type_2 > 2)
547  energy += P->TerminalAU;
548 
549  if (!dangles)
550  return energy;
551 
552  ci = ON_SAME_STRAND(i, i + 1, cutpoint);
553  cj = ON_SAME_STRAND(j - 1, j, cutpoint);
554  cp = ON_SAME_STRAND(p - 1, p, cutpoint);
555  cq = ON_SAME_STRAND(q, q + 1, cutpoint);
556 
557  d3 = ci ? P->dangle3[type][si1] : 0;
558  d5 = cj ? P->dangle5[type][sj1] : 0;
559  d5_2 = cp ? P->dangle5[type_2][sp1] : 0;
560  d3_2 = cq ? P->dangle3[type_2][sq1] : 0;
561 
562  tmm = (cj && ci) ? P->mismatchExt[type][sj1][si1] : d5 + d3;
563  tmm_2 = (cp && cq) ? P->mismatchExt[type_2][sp1][sq1] : d5_2 + d3_2;
564 
565  if (dangles == 2)
566  return energy + tmm + tmm_2;
567 
568  /* now we may have non-double dangles only */
569  if (i + 2 < p) {
570  if (q + 2 < j)
571  energy += tmm + tmm_2;
572  else if (q + 2 == j)
573  energy += (cj && cq) ? MIN2(tmm + d5_2, tmm_2 + d3) : tmm + tmm_2;
574  else
575  energy += d3 + d5_2;
576  } else if (i + 2 == p) {
577  if (q + 2 < j)
578  energy += (ci && cp) ? MIN2(tmm + d3_2, tmm_2 + d5) : tmm + tmm_2;
579  else if (q + 2 == j)
580  energy += MIN2(tmm, MIN2(tmm_2, MIN2(d5 + d5_2, d3 + d3_2)));
581  else
582  energy += MIN2(d3, d5_2);
583  } else {
584  if (q + 2 < j)
585  energy += d5 + d3_2;
586  else if (q + 2 == j)
587  energy += MIN2(d5, d3_2);
588  }
589 
590  return energy;
591 }
592 
593 
594 int
595 vrna_E_int_loop(vrna_fold_compound_t *vc,
596  int i,
597  int j);
598 
599 
607 int
609  int i,
610  int j,
611  int k,
612  int l);
613 
614 
615 /* j < i indicates circular folding, i.e. collect contributions for exterior int loops */
617 vrna_exp_E_int_loop(vrna_fold_compound_t *vc,
618  int i,
619  int j);
620 
621 
623 vrna_exp_E_interior_loop(vrna_fold_compound_t *vc,
624  int i,
625  int j,
626  int k,
627  int l);
628 
629 
630 int
631 vrna_E_ext_int_loop(vrna_fold_compound_t *vc,
632  int i,
633  int j,
634  int *ip,
635  int *iq);
636 
637 
638 int
639 vrna_E_stack(vrna_fold_compound_t *vc,
640  int i,
641  int j);
642 
643 
648 int
650  int *i,
651  int *j,
652  int *en,
653  vrna_bp_stack_t *bp_stack,
654  int *stack_count);
655 
656 
661 int
663  int *i,
664  int *j,
665  int en,
666  vrna_bp_stack_t *bp_stack,
667  int *stack_count);
668 
669 
675 #endif
#define MAXLOOP
Definition: energy_const.h:29
vrna_md_t model_details
Model details to be used in the recursions.
Definition: params.h:94
vrna_md_t model_details
Model details to be used in the recursions.
Definition: params.h:151
double FLT_OR_DBL
Typename for floating point number in partition function computations.
Definition: data_structures.h:43
The most basic data structure required by many functions throughout the RNAlib.
Definition: fold_compound.h:131
The datastructure that contains temperature scaled energy parameters.
Definition: params.h:56
int * energy_stack
Pseudo Energy contribution per base pair involved in a stack.
Definition: constraints_soft.h:198
#define MIN2(A, B)
Get the minimum of two comparable values.
Definition: utils.h:115
vrna_callback_sc_energy * f
A function pointer used for pseudo energy contribution in MFE calculations.
Definition: constraints_soft.h:202
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)
Evaluate the free energy contribution of an interior loop with delimiting base pairs and ...
Various data structures and pre-processor macros.
The soft constraints data structure.
Definition: constraints_soft.h:166
#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:101
int ** energy_up
Energy contribution for stretches of unpaired nucleotides.
Definition: constraints_soft.h:172
#define VRNA_DECOMP_PAIR_IL
Indicator for interior loop decomposition step.
Definition: constraints.h:151
int dangles
Specifies the dangle model used in any energy evaluation (0,1,2 or 3)
Definition: model.h:180
Base pair stack element.
Definition: data_structures.h:141
Functions and data structures for constraining secondary structure predictions and evaluation...
int * energy_bp
Energy contribution for base pairs.
Definition: constraints_soft.h:183
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:349
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 noGUclosure
Do not allow loops to be closed by GU pair.
Definition: model.h:207
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:444
void * data
A pointer to the data object provided for for pseudo energy contribution functions of the generic sof...
Definition: constraints_soft.h:219