RNAlib-2.2.5
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 
135 int E_stack(int i, int j, vrna_fold_compound_t *vc);
136 
137 /*
138 #################################
139 # BEGIN OF FUNCTION DEFINITIONS #
140 #################################
141 */
142 
143 /*
144  * ugly but fast interior loop evaluation
145  *
146  * Avoid including this function in your own code. It only serves
147  * as a fast inline block internally re-used throughout the RNAlib. It
148  * evalutes the free energy of interior loops in single sequences or sequence
149  * hybrids. Soft constraints are also applied if available.
150  *
151  * NOTE: do not include into doxygen reference manual!
152  */
153 PRIVATE INLINE int
154 ubf_eval_int_loop( int i,
155  int j,
156  int p,
157  int q,
158  int i1,
159  int j1,
160  int p1,
161  int q1,
162  short si,
163  short sj,
164  short sp,
165  short sq,
166  unsigned char type,
167  unsigned char type_2,
168  int *rtype,
169  int ij,
170  int cp,
171  vrna_param_t *P,
172  vrna_sc_t *sc){
173 
174  int energy, u1, u2;
175 
176  u1 = p1 - i;
177  u2 = j1 - q;
178 
179  if((cp < 0) || (ON_SAME_STRAND(i, p, cp) && ON_SAME_STRAND(q, j, cp))){ /* regular interior loop */
180  energy = E_IntLoop(u1, u2, type, type_2, si, sj, sp, sq, P);
181  } else { /* interior loop like cofold structure */
182  short Si, Sj;
183  Si = ON_SAME_STRAND(i, i1, cp) ? si : -1;
184  Sj = ON_SAME_STRAND(j1, j, cp) ? sj : -1;
185  energy = E_IntLoop_Co(rtype[type], rtype[type_2],
186  i, j, p, q,
187  cp,
188  Si, Sj,
189  sp, sq,
191  P);
192  }
193 
194  /* add soft constraints */
195  if(sc){
196  if(sc->energy_up)
197  energy += sc->energy_up[i1][u1]
198  + sc->energy_up[q1][u2];
199 
200  if(sc->energy_bp)
201  energy += sc->energy_bp[ij];
202 
203  if(sc->energy_stack)
204  if(u1 + u2 == 0){
205  int a = sc->energy_stack[i]
206  + sc->energy_stack[p]
207  + sc->energy_stack[q]
208  + sc->energy_stack[j];
209  energy += a;
210  }
211  if(sc->f)
212  energy += sc->f(i, j, p, q, VRNA_DECOMP_PAIR_IL, sc->data);
213  }
214 
215  return energy;
216 
217 }
218 
219 /*
220  * ugly but fast exterior interior loop evaluation
221  *
222  * Avoid including this function in your own code. It only serves
223  * as a fast inline block internally re-used throughout the RNAlib. It
224  * evalutes the free energy of interior loops in single sequences or sequence
225  * hybrids. Soft constraints are also applied if available.
226  *
227  * NOTE: do not include into doxygen reference manual!
228  */
229 PRIVATE INLINE int
230 ubf_eval_ext_int_loop(int i,
231  int j,
232  int p,
233  int q,
234  int i1,
235  int j1,
236  int p1,
237  int q1,
238  short si,
239  short sj,
240  short sp,
241  short sq,
242  unsigned char type,
243  unsigned char type_2,
244  int length,
245  vrna_param_t *P,
246  vrna_sc_t *sc){
247 
248  int energy, u1, u2, u3;
249 
250  u1 = i1;
251  u2 = p1 - j;
252  u3 = length - q;
253 
254  energy = E_IntLoop(u2, u1 + u3, type, type_2, si, sj, sp, sq, P);
255 
256  /* add soft constraints */
257  if(sc){
258  if(sc->energy_up)
259  energy += sc->energy_up[j1][u2]
260  + sc->energy_up[q1][u3]
261  + sc->energy_up[1][u1];
262 
263  if(sc->energy_stack)
264  if(u1 + u2 + u3 == 0)
265  energy += sc->energy_stack[i]
266  + sc->energy_stack[p]
267  + sc->energy_stack[q]
268  + sc->energy_stack[j];
269 
270  if(sc->f)
271  energy += sc->f(i, j, p, q, VRNA_DECOMP_PAIR_IL, sc->data);
272  }
273 
274  return energy;
275 
276 }
277 
278 PRIVATE INLINE int
279 E_IntLoop(int n1,
280  int n2,
281  int type,
282  int type_2,
283  int si1,
284  int sj1,
285  int sp1,
286  int sq1,
287  vrna_param_t *P){
288 
289  /* compute energy of degree 2 loop (stack bulge or interior) */
290  int nl, ns, u, energy;
291  energy = INF;
292 
293  if (n1>n2) { nl=n1; ns=n2;}
294  else {nl=n2; ns=n1;}
295 
296  if (nl == 0)
297  return P->stack[type][type_2]; /* stack */
298 
299  if (ns==0) { /* bulge */
300  energy = (nl<=MAXLOOP)?P->bulge[nl]:
301  (P->bulge[30]+(int)(P->lxc*log(nl/30.)));
302  if (nl==1) energy += P->stack[type][type_2];
303  else {
304  if (type>2) energy += P->TerminalAU;
305  if (type_2>2) energy += P->TerminalAU;
306  }
307  return energy;
308  }
309  else { /* interior loop */
310  if (ns==1) {
311  if (nl==1) /* 1x1 loop */
312  return P->int11[type][type_2][si1][sj1];
313  if (nl==2) { /* 2x1 loop */
314  if (n1==1)
315  energy = P->int21[type][type_2][si1][sq1][sj1];
316  else
317  energy = P->int21[type_2][type][sq1][si1][sp1];
318  return energy;
319  }
320  else { /* 1xn loop */
321  energy = (nl+1<=MAXLOOP)?(P->internal_loop[nl+1]) : (P->internal_loop[30]+(int)(P->lxc*log((nl+1)/30.)));
322  energy += MIN2(MAX_NINIO, (nl-ns)*P->ninio[2]);
323  energy += P->mismatch1nI[type][si1][sj1] + P->mismatch1nI[type_2][sq1][sp1];
324  return energy;
325  }
326  }
327  else if (ns==2) {
328  if(nl==2) { /* 2x2 loop */
329  return P->int22[type][type_2][si1][sp1][sq1][sj1];}
330  else if (nl==3){ /* 2x3 loop */
331  energy = P->internal_loop[5]+P->ninio[2];
332  energy += P->mismatch23I[type][si1][sj1] + P->mismatch23I[type_2][sq1][sp1];
333  return energy;
334  }
335 
336  }
337  { /* generic interior loop (no else here!)*/
338  u = nl + ns;
339  energy = (u <= MAXLOOP) ? (P->internal_loop[u]) : (P->internal_loop[30]+(int)(P->lxc*log((u)/30.)));
340 
341  energy += MIN2(MAX_NINIO, (nl-ns)*P->ninio[2]);
342 
343  energy += P->mismatchI[type][si1][sj1] + P->mismatchI[type_2][sq1][sp1];
344  }
345  }
346  return energy;
347 }
348 
349 PRIVATE INLINE FLT_OR_DBL
351  int u2,
352  int type,
353  int type2,
354  short si1,
355  short sj1,
356  short sp1,
357  short sq1,
358  vrna_exp_param_t *P){
359 
360  int ul, us, no_close = 0;
361  double z = 0.;
362  int noGUclosure = P->model_details.noGUclosure;
363 
364  if ((noGUclosure) && ((type2==3)||(type2==4)||(type==3)||(type==4)))
365  no_close = 1;
366 
367  if (u1>u2) { ul=u1; us=u2;}
368  else {ul=u2; us=u1;}
369 
370  if (ul==0) /* stack */
371  z = P->expstack[type][type2];
372  else if(!no_close){
373  if (us==0) { /* bulge */
374  z = P->expbulge[ul];
375  if (ul==1) z *= P->expstack[type][type2];
376  else {
377  if (type>2) z *= P->expTermAU;
378  if (type2>2) z *= P->expTermAU;
379  }
380  return (FLT_OR_DBL)z;
381  }
382  else if (us==1) {
383  if (ul==1){ /* 1x1 loop */
384  return (FLT_OR_DBL)(P->expint11[type][type2][si1][sj1]);
385  }
386  if (ul==2) { /* 2x1 loop */
387  if (u1==1)
388  return (FLT_OR_DBL)(P->expint21[type][type2][si1][sq1][sj1]);
389  else
390  return (FLT_OR_DBL)(P->expint21[type2][type][sq1][si1][sp1]);
391  }
392  else { /* 1xn loop */
393  z = P->expinternal[ul+us] * P->expmismatch1nI[type][si1][sj1] * P->expmismatch1nI[type2][sq1][sp1];
394  return (FLT_OR_DBL)(z * P->expninio[2][ul-us]);
395  }
396  }
397  else if (us==2) {
398  if(ul==2) /* 2x2 loop */
399  return (FLT_OR_DBL)(P->expint22[type][type2][si1][sp1][sq1][sj1]);
400  else if(ul==3){ /* 2x3 loop */
401  z = P->expinternal[5]*P->expmismatch23I[type][si1][sj1]*P->expmismatch23I[type2][sq1][sp1];
402  return (FLT_OR_DBL)(z * P->expninio[2][1]);
403  }
404  }
405  /* generic interior loop (no else here!)*/
406  z = P->expinternal[ul+us] * P->expmismatchI[type][si1][sj1] * P->expmismatchI[type2][sq1][sp1];
407  return (FLT_OR_DBL)(z * P->expninio[2][ul-us]);
408 
409  }
410  return (FLT_OR_DBL)z;
411 }
412 
413 PRIVATE INLINE int
414 E_IntLoop_Co( int type,
415  int type_2,
416  int i,
417  int j,
418  int p,
419  int q,
420  int cutpoint,
421  short si1,
422  short sj1,
423  short sp1,
424  short sq1,
425  int dangles,
426  vrna_param_t *P){
427 
428  int energy, ci, cj, cp, cq, d3, d5, d5_2, d3_2, tmm, tmm_2;
429 
430  energy = 0;
431  if(type > 2) energy += P->TerminalAU;
432  if(type_2 > 2) energy += P->TerminalAU;
433 
434  if(!dangles) return energy;
435 
436  ci = ON_SAME_STRAND(i, i + 1, cutpoint);
437  cj = ON_SAME_STRAND(j - 1, j, cutpoint);
438  cp = ON_SAME_STRAND(p - 1, p, cutpoint);
439  cq = ON_SAME_STRAND(q, q + 1, cutpoint);
440 
441  d3 = ci ? P->dangle3[type][si1] : 0;
442  d5 = cj ? P->dangle5[type][sj1] : 0;
443  d5_2 = cp ? P->dangle5[type_2][sp1] : 0;
444  d3_2 = cq ? P->dangle3[type_2][sq1] : 0;
445 
446  tmm = (cj && ci) ? P->mismatchExt[type][sj1][si1] : d5 + d3;
447  tmm_2 = (cp && cq) ? P->mismatchExt[type_2][sp1][sq1] : d5_2 + d3_2;
448 
449  if(dangles == 2) return energy + tmm + tmm_2;
450 
451  /* now we may have non-double dangles only */
452  if(i+2 < p){
453  if(q+2 < j){ energy += tmm + tmm_2;}
454  else if(q+2 == j){ energy += (cj && cq) ? MIN2(tmm + d5_2, tmm_2 + d3) : tmm + tmm_2;}
455  else energy += d3 + d5_2;
456  }
457  else if(i+2 == p){
458  if(q+2 < j){ energy += (ci && cp) ? MIN2(tmm + d3_2, tmm_2 + d5) : tmm + tmm_2;}
459  else if(q+2 == j){
460  energy += MIN2(tmm, MIN2(tmm_2, MIN2(d5 + d5_2, d3 + d3_2)));
461  }
462  else energy += MIN2(d3, d5_2);
463  }
464  else{
465  if(q+2 < j){ energy += d5 + d3_2;}
466  else if(q+2 == j){ energy += MIN2(d5, d3_2);}
467  }
468  return energy;
469 }
470 
471 int
472 vrna_E_int_loop(vrna_fold_compound_t *vc,
473  int i,
474  int j);
475 
476 PUBLIC FLT_OR_DBL
477 vrna_exp_E_int_loop(vrna_fold_compound_t *vc,
478  int i,
479  int j);
480 
481 int
482 vrna_E_ext_int_loop(vrna_fold_compound_t *vc,
483  int i,
484  int j,
485  int *ip,
486  int *iq);
487 
488 int
489 vrna_E_stack( vrna_fold_compound_t *vc,
490  int i,
491  int j);
492 
493 
494 int
495 E_IntLoop(int n1,
496  int n2,
497  int type,
498  int type_2,
499  int si1,
500  int sj1,
501  int sp1,
502  int sq1,
503  vrna_param_t *P);
504 
505 
506 PUBLIC FLT_OR_DBL
507 exp_E_IntLoop(int u1,
508  int u2,
509  int type,
510  int type2,
511  short si1,
512  short sj1,
513  short sp1,
514  short sq1,
515  vrna_exp_param_t *P);
516 
517 int
518 E_IntLoop_Co( int type,
519  int type_2,
520  int i,
521  int j,
522  int p,
523  int q,
524  int cutpoint,
525  short si1,
526  short sj1,
527  short sp1,
528  short sq1,
529  int dangles,
530  vrna_param_t *P);
531 
532 
537 int
539  int *i,
540  int *j,
541  int *en,
542  vrna_bp_stack_t *bp_stack,
543  int *stack_count);
548 int
550  int *i,
551  int *j,
552  int en,
553  vrna_bp_stack_t *bp_stack,
554  int *stack_count);
555 
556 
562 #endif
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:279
int dangles
Specifies the dangle model used in any energy evaluation (0,1,2 or 3)
Definition: model.h:193
#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:44
The most basic data structure required by many functions throughout the RNAlib.
Definition: data_structures.h:397
int noGUclosure
Do not allow loops to be closed by GU pair.
Definition: model.h:220
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
#define MIN2(A, B)
Get the minimum of two comparable values.
Definition: utils.h:114
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:350
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.
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 datastructure 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:199
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 .
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 E_stack(int i, int j, vrna_fold_compound_t *vc)
Evaluate energy of a base pair stack closed by (i,j)
int dangles
Switch the energy model for dangling end contributions (0, 1, 2, 3)
void * data
A pointer to the data object provided for for pseudo energy contribution functions of the generic sof...
Definition: constraints_soft.h:128