RNAlib-2.2.0RC0
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 <stdio.h>
5 #include <stdlib.h>
6 #include <math.h>
7 #include <ctype.h>
8 #include <string.h>
9 #include <ViennaRNA/params.h>
10 #include <ViennaRNA/fold_vars.h>
11 #include <ViennaRNA/energy_par.h>
12 #include <ViennaRNA/constraints.h>
13 #include <ViennaRNA/gquad.h>
14 
15 #ifdef __GNUC__
16 # define INLINE inline
17 #else
18 # define INLINE
19 #endif
20 
70 INLINE PRIVATE int E_IntLoop(int n1,
71  int n2,
72  int type,
73  int type_2,
74  int si1,
75  int sj1,
76  int sp1,
77  int sq1,
78  paramT *P);
79 
99 INLINE PRIVATE double exp_E_IntLoop(int u1,
100  int u2,
101  int type,
102  int type2,
103  short si1,
104  short sj1,
105  short sp1,
106  short sq1,
107  pf_paramT *P);
108 
109 
110 INLINE PRIVATE int E_IntLoop_Co(int type,
111  int type_2,
112  int i,
113  int j,
114  int p,
115  int q,
116  int cutpoint,
117  short si1,
118  short sj1,
119  short sp1,
120  short sq1,
121  int dangles,
122  paramT *P);
123 
124 
128 INLINE PRIVATE int E_stack(int i, int j, vrna_fold_compound *vc);
129 
130 /*
131 #################################
132 # BEGIN OF FUNCTION DEFINITIONS #
133 #################################
134 */
135 
136 INLINE PRIVATE int
137 E_int_loop( int i,
138  int j,
139  vrna_fold_compound *vc){
140 
141  int q, p, j_q, p_i, pq, *c_pq, max_q, max_p, tmp, *rtype, noGUclosure, no_close, energy;
142  short *S_p1, *S_q1;
143  char *ptype_pq;
144  unsigned char type, type_2;
145  char *hc_pq;
146  int cp = vc->cutpoint;
147  char *ptype = vc->ptype;
148  short *S = vc->sequence_encoding;
149  short S_i1 = S[i+1];
150  short S_j1 = S[j-1];
151  int *indx = vc->jindx;
152  char *hc = vc->hc->matrix;
153  int *hc_up = vc->hc->up_int;
154  soft_constraintT *sc = vc->sc;
155  paramT *P = vc->params;
156  int ij = indx[j] + i;
157  int hc_decompose = hc[ij];
158  int e = INF;
159  int *c = vc->matrices->c;
160  int *ggg = vc->matrices->ggg;
161  model_detailsT *md = &(P->model_details);
162  int with_gquad = md->gquad;
163  int turn = md->min_loop_size;
164 
165  /* CONSTRAINED INTERIOR LOOP start */
166  if(hc_decompose & VRNA_HC_CONTEXT_INT_LOOP){
167 
168  type = (unsigned char)ptype[ij];
169  rtype = &(md->rtype[0]);
170  noGUclosure = md->noGUclosure;
171  no_close = (((type==3)||(type==4))&&noGUclosure);
172  max_q = i+turn+2;
173  max_q = MAX2(max_q, j - MAXLOOP - 1);
174  for(q = j - 1; q >= max_q; q--){
175  j_q = j - q - 1;
176 
177  if(hc_up[q+1] < j_q) break;
178 
179  pq = indx[q] + i + 1;
180  p_i = 0;
181  max_p = i + 1;
182  tmp = i + 1 + MAXLOOP - j_q;
183  max_p = MAX2(max_p, tmp);
184  tmp = q - turn;
185  max_p = MIN2(max_p, tmp);
186  tmp = i + 1 + hc_up[i + 1];
187  max_p = MIN2(max_p, tmp);
188  hc_pq = hc + pq;
189  c_pq = c + pq;
190  ptype_pq = ptype + pq;
191  S_p1 = S + i;
192  S_q1 = S + q + 1;
193  for(p = i+1; p <= max_p; p++){
194 
195  /* discard this configuration if (p,q) is not allowed to be enclosed pair of an interior loop */
196  if(*hc_pq & VRNA_HC_CONTEXT_INT_LOOP_ENC){
197 
198  type_2 = rtype[(unsigned char)*ptype_pq];
199 
200  if (noGUclosure)
201  if (no_close||(type_2==3)||(type_2==4))
202  if ((p>i+1)||(q<j-1)) continue; /* continue unless stack */
203 
204  if((cp < 0) || ( ((i >= cp) || (p < cp)) && ((q >= cp) || (j < cp)))){ /* regular interior loop */
205  energy = E_IntLoop(p_i, j_q, type, type_2, S_i1, S_j1, *S_p1, *S_q1, P);
206  } else { /* interior loop like cofold structure */
207  short si, sj;
208  si = ((i >= cp) || ((i + 1) < cp)) ? S_i1 : -1;
209  sj = (((j - 1) >= cp) || (j < cp)) ? S_j1 : -1;
210  energy = E_IntLoop_Co(rtype[type], rtype[type_2],
211  i, j, p, q,
212  cp,
213  si, sj,
214  *S_p1, *S_q1,
215  md->dangles,
216  P);
217  }
218  energy += *c_pq;
219 
220  /* add soft constraints */
221  if(sc){
222  if(sc->free_energies)
223  energy += sc->free_energies[i+1][p_i]
224  + sc->free_energies[q+1][j_q];
225 
226  if(sc->en_basepair)
227  energy += sc->en_basepair[ij];
228 
229  if(sc->en_stack)
230  if((p==i+1) && (q == j-1))
231  energy += sc->en_stack[i]
232  + sc->en_stack[p]
233  + sc->en_stack[q]
234  + sc->en_stack[j];
235 
236  if(sc->f)
237  energy += sc->f(i, j, p, q, VRNA_DECOMP_PAIR_IL, sc->data);
238  }
239 
240  e = MIN2(e, energy);
241 
242  }
243  hc_pq++; /* get hc[pq + 1] */
244  c_pq++; /* get c[pq + 1] */
245  p_i++; /* increase unpaired region [i+1...p-1] */
246  ptype_pq++; /* get ptype[pq + 1] */
247  S_p1++;
248  pq++;
249  } /* end q-loop */
250  } /* end p-loop */
251 
252  if(with_gquad){
253  /* include all cases where a g-quadruplex may be enclosed by base pair (i,j) */
254  if ((!no_close) && ((cp < 0) || ((i >= cp) || (j < cp)))) {
255  energy = E_GQuad_IntLoop(i, j, type, S, ggg, indx, P);
256  e = MIN2(e, energy);
257  }
258  }
259 
260  }
261  return e;
262 }
263 
264 INLINE PRIVATE int
265 E_stack(int i,
266  int j,
267  vrna_fold_compound *vc){
268 
269  int e, ij, pq, p, q;
270  unsigned char type, type_2;
271 
272  int cp = vc->cutpoint;
273  short *S = vc->sequence_encoding;
274  char *ptype = vc->ptype;
275  paramT *P = vc->params;
276  model_detailsT *md = &(P->model_details);
277  int *rtype = &(md->rtype[0]);
278  int *indx = vc->jindx;
279  char *hard_constraints = vc->hc->matrix;
280  soft_constraintT *sc = vc->sc;
281 
282  e = INF;
283  p = i + 1;
284  q = j - 1;
285  ij = indx[j] + i;
286  pq = indx[q] + p;
287  type = (unsigned char)ptype[ij];
288  type_2 = rtype[(unsigned char)ptype[pq]];
289 
290  if((hard_constraints[pq] & VRNA_HC_CONTEXT_INT_LOOP_ENC) && (hard_constraints[ij] & VRNA_HC_CONTEXT_INT_LOOP)){
291  if ((cp < 0) || (((i >= cp) || (p < cp)) && ((q >= cp) || (j < cp)))){ /* regular stack */
292  e = P->stack[type][type_2];
293  } else { /* stack like cofold structure */
294  short si, sj;
295  si = ((i >= cp) || ((i + 1) < cp)) ? S[i+1] : -1;
296  sj = (((j - 1) >= cp) || (j < cp)) ? S[j-1] : -1;
297  e = E_IntLoop_Co(rtype[type], rtype[type_2],
298  i, j, p, q,
299  cp,
300  si, sj,
301  S[p-1], S[q+1],
302  md->dangles,
303  P);
304  }
305 
306  /* add soft constraints */
307  if(sc){
308  if(sc->en_basepair)
309  e += sc->en_basepair[ij];
310 
311  if(sc->en_stack)
312  if((p==i+1) && (q == j-1))
313  e += sc->en_stack[i]
314  + sc->en_stack[p]
315  + sc->en_stack[q]
316  + sc->en_stack[j];
317 
318  if(sc->f)
319  e += sc->f(i, j, p, q, VRNA_DECOMP_PAIR_IL, sc->data);
320  }
321  }
322 
323  return e;
324 }
325 
326 INLINE PRIVATE int
327 E_IntLoop(int n1,
328  int n2,
329  int type,
330  int type_2,
331  int si1,
332  int sj1,
333  int sp1,
334  int sq1,
335  paramT *P){
336 
337  /* compute energy of degree 2 loop (stack bulge or interior) */
338  int nl, ns, u, energy;
339  energy = INF;
340 
341  if (n1>n2) { nl=n1; ns=n2;}
342  else {nl=n2; ns=n1;}
343 
344  if (nl == 0)
345  return P->stack[type][type_2]; /* stack */
346 
347  if (ns==0) { /* bulge */
348  energy = (nl<=MAXLOOP)?P->bulge[nl]:
349  (P->bulge[30]+(int)(P->lxc*log(nl/30.)));
350  if (nl==1) energy += P->stack[type][type_2];
351  else {
352  if (type>2) energy += P->TerminalAU;
353  if (type_2>2) energy += P->TerminalAU;
354  }
355  return energy;
356  }
357  else { /* interior loop */
358  if (ns==1) {
359  if (nl==1) /* 1x1 loop */
360  return P->int11[type][type_2][si1][sj1];
361  if (nl==2) { /* 2x1 loop */
362  if (n1==1)
363  energy = P->int21[type][type_2][si1][sq1][sj1];
364  else
365  energy = P->int21[type_2][type][sq1][si1][sp1];
366  return energy;
367  }
368  else { /* 1xn loop */
369  energy = (nl+1<=MAXLOOP)?(P->internal_loop[nl+1]) : (P->internal_loop[30]+(int)(P->lxc*log((nl+1)/30.)));
370  energy += MIN2(MAX_NINIO, (nl-ns)*P->ninio[2]);
371  energy += P->mismatch1nI[type][si1][sj1] + P->mismatch1nI[type_2][sq1][sp1];
372  return energy;
373  }
374  }
375  else if (ns==2) {
376  if(nl==2) { /* 2x2 loop */
377  return P->int22[type][type_2][si1][sp1][sq1][sj1];}
378  else if (nl==3){ /* 2x3 loop */
379  energy = P->internal_loop[5]+P->ninio[2];
380  energy += P->mismatch23I[type][si1][sj1] + P->mismatch23I[type_2][sq1][sp1];
381  return energy;
382  }
383 
384  }
385  { /* generic interior loop (no else here!)*/
386  u = nl + ns;
387  energy = (u <= MAXLOOP) ? (P->internal_loop[u]) : (P->internal_loop[30]+(int)(P->lxc*log((u)/30.)));
388 
389  energy += MIN2(MAX_NINIO, (nl-ns)*P->ninio[2]);
390 
391  energy += P->mismatchI[type][si1][sj1] + P->mismatchI[type_2][sq1][sp1];
392  }
393  }
394  return energy;
395 }
396 
397 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){
398  int ul, us, no_close = 0;
399  double z = 0.;
400 
401  if ((no_closingGU) && ((type2==3)||(type2==4)||(type==3)||(type==4)))
402  no_close = 1;
403 
404  if (u1>u2) { ul=u1; us=u2;}
405  else {ul=u2; us=u1;}
406 
407  if (ul==0) /* stack */
408  z = P->expstack[type][type2];
409  else if(!no_close){
410  if (us==0) { /* bulge */
411  z = P->expbulge[ul];
412  if (ul==1) z *= P->expstack[type][type2];
413  else {
414  if (type>2) z *= P->expTermAU;
415  if (type2>2) z *= P->expTermAU;
416  }
417  return z;
418  }
419  else if (us==1) {
420  if (ul==1){ /* 1x1 loop */
421  return P->expint11[type][type2][si1][sj1];
422  }
423  if (ul==2) { /* 2x1 loop */
424  if (u1==1)
425  return P->expint21[type][type2][si1][sq1][sj1];
426  else
427  return P->expint21[type2][type][sq1][si1][sp1];
428  }
429  else { /* 1xn loop */
430  z = P->expinternal[ul+us] * P->expmismatch1nI[type][si1][sj1] * P->expmismatch1nI[type2][sq1][sp1];
431  return z * P->expninio[2][ul-us];
432  }
433  }
434  else if (us==2) {
435  if(ul==2) /* 2x2 loop */
436  return P->expint22[type][type2][si1][sp1][sq1][sj1];
437  else if(ul==3){ /* 2x3 loop */
438  z = P->expinternal[5]*P->expmismatch23I[type][si1][sj1]*P->expmismatch23I[type2][sq1][sp1];
439  return z * P->expninio[2][1];
440  }
441  }
442  /* generic interior loop (no else here!)*/
443  z = P->expinternal[ul+us] * P->expmismatchI[type][si1][sj1] * P->expmismatchI[type2][sq1][sp1];
444  return z * P->expninio[2][ul-us];
445 
446  }
447  return z;
448 }
449 
450 INLINE PRIVATE int
451 E_IntLoop_Co( int type,
452  int type_2,
453  int i,
454  int j,
455  int p,
456  int q,
457  int cutpoint,
458  short si1,
459  short sj1,
460  short sp1,
461  short sq1,
462  int dangles,
463  paramT *P){
464 
465  int energy, ci, cj, cp, cq, d3, d5, d5_2, d3_2, tmm, tmm_2;
466 
467  energy = 0;
468  if(type > 2) energy += P->TerminalAU;
469  if(type_2 > 2) energy += P->TerminalAU;
470 
471  if(!dangles) return energy;
472 
473  ci = (i>=cutpoint)||((i+1)<cutpoint);
474  cj = ((j-1)>=cutpoint)||(j<cutpoint);
475  cp = ((p-1)>=cutpoint)||(p<cutpoint);
476  cq = (q>=cutpoint)||((q+1)<cutpoint);
477 
478  d3 = ci ? P->dangle3[type][si1] : 0;
479  d5 = cj ? P->dangle5[type][sj1] : 0;
480  d5_2 = cp ? P->dangle5[type_2][sp1] : 0;
481  d3_2 = cq ? P->dangle3[type_2][sq1] : 0;
482 
483  tmm = (cj && ci) ? P->mismatchExt[type][sj1][si1] : d5 + d3;
484  tmm_2 = (cp && cq) ? P->mismatchExt[type_2][sp1][sq1] : d5_2 + d3_2;
485 
486  if(dangles == 2) return energy + tmm + tmm_2;
487 
488  /* now we may have non-double dangles only */
489  if(i+2 < p){
490  if(q+2 < j){ energy += tmm + tmm_2;}
491  else if(q+2 == j){ energy += (cj && cq) ? MIN2(tmm + d5_2, tmm_2 + d3) : tmm + tmm_2;}
492  else energy += d3 + d5_2;
493  }
494  else if(i+2 == p){
495  if(q+2 < j){ energy += (ci && cp) ? MIN2(tmm + d3_2, tmm_2 + d5) : tmm + tmm_2;}
496  else if(q+2 == j){
497  energy += MIN2(tmm, MIN2(tmm_2, MIN2(d5 + d5_2, d3 + d3_2)));
498  }
499  else energy += MIN2(d3, d5_2);
500  }
501  else{
502  if(q+2 < j){ energy += d5 + d3_2;}
503  else if(q+2 == j){ energy += MIN2(d5, d3_2);}
504  }
505  return energy;
506 }
507 
508 
509 #endif
The soft constraints data structure.
Definition: data_structures.h:323
#define MIN2(A, B)
Definition: utils.h:150
int dangles
Switch the energy model for dangling end contributions (0, 1, 2, 3)
int cutpoint
The position of the (cofold) cutpoint within the provided sequence. If there is no cutpoint...
Definition: data_structures.h:363
#define MAXLOOP
Definition: energy_const.h:28
int min_loop_size
Minimum size of hairpin loops.
Definition: model.h:231
int * en_stack
Pseudo Energy contribution per base pair involved in a stack.
Definition: data_structures.h:330
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: interior_loops.h:397
#define VRNA_HC_CONTEXT_INT_LOOP_ENC
Hard constraints flag, base pair encloses a multi branch loop.
Definition: constraints.h:206
The datastructure that contains temperature scaled energy parameters.
Definition: data_structures.h:180
model_detailsT model_details
Model details to be used in the recursions.
Definition: data_structures.h:218
The datastructure that contains temperature scaled Boltzmann weights of the energy parameters...
Definition: data_structures.h:225
int * up_int
A linear array that holds the number of allowed unpaired nucleotides in an interior loop...
Definition: data_structures.h:310
int * en_basepair
Energy contribution for base pairs.
Definition: data_structures.h:326
Definition: data_structures.h:359
int noGUclosure
Do not allow loops to be closed by GU pair.
Definition: model.h:218
#define INF
Definition: energy_const.h:16
PRIVATE int E_stack(int i, int j, vrna_fold_compound *vc)
Evaluate energy of a base pair stack closed by (i,j)
Definition: interior_loops.h:265
#define VRNA_HC_CONTEXT_INT_LOOP
Hard constraints flag, base pair encloses an interior loop.
Definition: constraints.h:192
int gquad
Include G-quadruplexes in structure prediction.
Definition: model.h:221
#define VRNA_DECOMP_PAIR_IL
Generalized constraint folding flag indicating interior loop decomposition step.
Definition: constraints.h:238
int dangles
Specifies the dangle model used in any energy evaluation (0,1,2 or 3)
Definition: model.h:209
void * data
A pointer to the data object necessary for for pseudo energy contribution functions.
Definition: data_structures.h:353
int(* f)(int, int, int, int, char, void *)
A function pointer used for pseudo energy contribution in MFE calculations.
Definition: data_structures.h:334
int no_closingGU
GU allowed only inside stacks if set to 1.
Various functions related to G-quadruplex computations.
char * ptype
Pair type array.
Definition: data_structures.h:371
char * matrix
Upper triangular matrix encoding where a base pair or unpaired nucleotide is allowed.
Definition: data_structures.h:301
Here all all declarations of the global variables used throughout RNAlib.
The data structure that contains the complete model details used throughout the calculations.
Definition: model.h:206
int ** free_energies
Energy contribution for unpaired sequence stretches.
Definition: data_structures.h:325
#define MAX2(A, B)
Definition: utils.h:154
PRIVATE int E_IntLoop(int n1, int n2, int type, int type_2, int si1, int sj1, int sp1, int sq1, paramT *P)
Definition: interior_loops.h:327