RNAlib-2.2.0RC0
multibranch_loops.h
1 #ifndef __VIENNA_RNA_PACKAGE_MULTIBRANCH_LOOPS_H__
2 #define __VIENNA_RNA_PACKAGE_MULTIBRANCH_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 
45 INLINE PRIVATE int E_MLstem( int type,
46  int si1,
47  int sj1,
48  paramT *P);
49 
56 INLINE PRIVATE double exp_E_MLstem(int type,
57  int si1,
58  int sj1,
59  pf_paramT *P);
60 
61 
62 
68 INLINE PRIVATE int E_mb_loop_stack(int i, int j, vrna_fold_compound *vc);
69 
70 /*
71 #################################
72 # BEGIN OF FUNCTION DEFINITIONS #
73 #################################
74 */
75 
76 
77 INLINE PRIVATE int
78 E_mb_loop_fast( int i,
79  int j,
81  int *dmli1,
82  int *dmli2){
83 
84  int k, decomp, MLenergy, en;
85  unsigned char type, type_2, tt;
86  short S_i1, S_j1;
87 
88  int cp = vc->cutpoint;
89  char *ptype = vc->ptype;
90  short *S = vc->sequence_encoding;
91  int *indx = vc->jindx;
92  char *hc = vc->hc->matrix;
93  int *hc_up = vc->hc->up_ml;
94  soft_constraintT *sc = vc->sc;
95  int *c = vc->matrices->c;
96  int *fML = vc->matrices->fML;
97  int *fc = vc->matrices->fc;
98  paramT *P = vc->params;
99 
100  int ij = indx[j] + i;
101  int hc_decompose = hc[ij];
102  int e = INF;
103  int dangle_model = P->model_details.dangles;
104  int *rtype = &(P->model_details.rtype[0]);
105 
106  type = (unsigned char)ptype[ij];
107 
108  if(cp < 0){
109  S_i1 = S[i+1];
110  S_j1 = S[j-1];
111  } else {
112  S_i1 = ((i >= cp) || ((i + 1) < cp)) ? S[i+1] : -1;
113  S_j1 = (((j - 1) >= cp) || (j < cp)) ? S[j-1] : -1;
114  }
115 
116  if(hc_decompose & VRNA_HC_CONTEXT_MB_LOOP){
117  if((S_i1 >= 0) && (S_j1 >= 0)){ /* regular multi branch loop */
118  decomp = dmli1[j-1];
119  tt = rtype[type];
120  switch(dangle_model){
121  /* no dangles */
122  case 0: decomp += E_MLstem(tt, -1, -1, P);
123  if(sc){
124  if(sc->en_basepair)
125  decomp += sc->en_basepair[ij];
126  }
127  break;
128 
129  /* double dangles */
130  case 2: decomp += E_MLstem(tt, S_j1, S_i1, P);
131  if(sc){
132  if(sc->en_basepair)
133  decomp += sc->en_basepair[ij];
134  }
135  break;
136 
137  /* normal dangles, aka dangles = 1 || 3 */
138  default: decomp += E_MLstem(tt, -1, -1, P);
139  if(sc){
140  if(sc->en_basepair)
141  decomp += sc->en_basepair[ij];
142  }
143  if(hc_up[i+1]){
144  en = dmli2[j-1] + E_MLstem(tt, -1, S_i1, P) + P->MLbase;
145  if(sc){
146  if(sc->free_energies)
147  en += sc->free_energies[i+1][1];
148 
149  if(sc->en_basepair)
150  en += sc->en_basepair[ij];
151  }
152  decomp = MIN2(decomp, en);
153  }
154  if(hc_up[j-1] && hc_up[i+1]){
155  en = dmli2[j-2] + E_MLstem(tt, S_j1, S_i1, P) + 2*P->MLbase;
156  if(sc){
157  if(sc->free_energies)
158  en += sc->free_energies[i+1][1]
159  + sc->free_energies[j-1][1];
160 
161  if(sc->en_basepair)
162  en += sc->en_basepair[ij];
163  }
164  decomp = MIN2(decomp, en);
165  }
166  if(hc_up[j-1]){
167  en = dmli1[j-2] + E_MLstem(tt, S_j1, -1, P) + P->MLbase;
168  if(sc){
169  if(sc->free_energies)
170  en += sc->free_energies[j-1][1];
171 
172  if(sc->en_basepair)
173  en += sc->en_basepair[ij];
174  }
175  decomp = MIN2(decomp, en);
176  }
177  break;
178  }
179  e = decomp + P->MLclosing;
180  }
181 
182  if(!((i >= cp) || (j < cp))){ /* multibrach like cofold structure with cut somewhere between i and j */
183  decomp = fc[i+1] + fc[j-1];
184  tt = rtype[type];
185  switch(dangle_model){
186  case 0: decomp += E_ExtLoop(tt, -1, -1, P);
187  break;
188  case 2: decomp += E_ExtLoop(tt, S_j1, S_i1, P);
189  break;
190  default: decomp += E_ExtLoop(tt, -1, -1, P);
191  if((hc_up[i+1]) && (hc_up[j-1])){
192  en = fc[i+2] + fc[j-2] + E_ExtLoop(tt, S_j1, S_i1, P);
193  decomp = MIN2(decomp, en);
194  }
195  if(hc_up[i+1]){
196  en = fc[i+2] + fc[j-1] + E_ExtLoop(tt, -1, S_i1, P);
197  decomp = MIN2(decomp, en);
198  }
199  if(hc_up[j-1]){
200  en = fc[i+1] + fc[j-2] + E_ExtLoop(tt, S_j1, -1, P);
201  decomp = MIN2(decomp, en);
202  }
203  break;
204  }
205  e = MIN2(e, decomp);
206  }
207  }
208  return e;
209 }
210 
211 INLINE PRIVATE int
212 E_mb_loop_stack(int i,
213  int j,
214  vrna_fold_compound *vc){
215 
216  int e, decomp, en, i1k, k1j1, ij, k;
217  unsigned char type, type_2;
218 
219  int *indx = vc->jindx;
220  char *hc = vc->hc->matrix;
221  int *c = vc->matrices->c;
222  int *fML = vc->matrices->fML;
223  paramT *P = vc->params;
224  model_detailsT *md = &(P->model_details);
225  int turn = md->min_loop_size;
226  char *ptype = vc->ptype;
227  int *rtype = &(md->rtype[0]);
228  soft_constraintT *sc = vc->sc;
229 
230  e = INF;
231  ij = indx[j] + i;
232 
233  if(hc[ij] & VRNA_HC_CONTEXT_MB_LOOP){
234  decomp = INF;
235  k1j1 = indx[j-1] + i + 2 + turn + 1;
236  for (k = i+2+turn; k < j-2-turn; k++, k1j1++){
237  i1k = indx[k] + i + 1;
238  if(hc[i1k] & VRNA_HC_CONTEXT_MB_LOOP_ENC){
239  type_2 = rtype[(unsigned char)ptype[i1k]];
240  en = c[i1k]+P->stack[type][type_2]+fML[k1j1];
241  decomp = MIN2(decomp, en);
242  }
243  if(hc[k1j1] & VRNA_HC_CONTEXT_MB_LOOP_ENC){
244  type_2 = rtype[(unsigned char)ptype[k1j1]];
245  en = c[k1j1]+P->stack[type][type_2]+fML[i1k];
246  decomp = MIN2(decomp, en);
247  }
248  }
249  /* no TermAU penalty if coax stack */
250  decomp += 2*P->MLintern[1] + P->MLclosing;
251  if(sc){
252  if(sc->en_basepair)
253  decomp += sc->en_basepair[ij];
254  }
255  e = decomp;
256 
257  }
258  return e;
259 }
260 
261 INLINE PRIVATE int
262 E_ml_rightmost_stem(int i,
263  int j,
264  vrna_fold_compound *vc){
265 
266  int en;
267  paramT *P = vc->params;
268  int length = vc->length;
269  short *S = vc->sequence_encoding;
270  int *indx = vc->jindx;
271  char *hc = vc->hc->matrix;
272  int *hc_up = vc->hc->up_ml;
273  soft_constraintT *sc = vc->sc;
274  int *c = vc->matrices->c;
275  int *fm = (P->model_details.uniq_ML) ? vc->matrices->fM1 : vc->matrices->fML;
276  int *ggg = vc->matrices->ggg;
277  int ij = indx[j] + i;
278  int type = vc->ptype[ij];
279  int hc_decompose = hc[ij];
280  int dangle_model = P->model_details.dangles;
281  int with_gquad = P->model_details.gquad;
282  int cp = vc->cutpoint;
283  int e = INF;
284 
285  if((cp < 0) || (((i - 1) >= cp) || (i < cp))){
286  if((cp < 0) || ((j >= cp) || ((j + 1) < cp))){
287  if(hc_decompose & VRNA_HC_CONTEXT_MB_LOOP_ENC){
288  e = c[ij];
289  switch(dangle_model){
290  case 2: e += E_MLstem(type, (i==1) ? S[length] : S[i-1], S[j+1], P);
291  break;
292  default: e += E_MLstem(type, -1, -1, P);
293  break;
294  }
295  }
296 
297  if(with_gquad)
298  if((cp < 0) || ((i >= cp) || (j < cp))){
299  en = ggg[ij] + E_MLstem(0, -1, -1, P);
300  e = MIN2(e, en);
301  }
302 
303  }
304 
305  if((cp < 0) || (((j - 1) >= cp) || (j < cp)))
306  if(hc_up[j]){
307  en = fm[indx[j - 1] + i] + P->MLbase;
308  if(sc)
309  if(sc->free_energies)
310  en += sc->free_energies[j][1];
311 
312  e = MIN2(e, en);
313  }
314  }
315  return e;
316 }
317 
318 INLINE PRIVATE int
319 E_ml_stems_fast(int i,
320  int j,
321  vrna_fold_compound *vc,
322  int *fmi,
323  int *dmli){
324 
325  int k, en, decomp, mm5, mm3, type_2, k1j;
326 
327  int length = (int)vc->length;
328  char *ptype = vc->ptype;
329  short *S = vc->sequence_encoding;
330  int *indx = vc->jindx;
331  char *hc = vc->hc->matrix;
332  int *hc_up = vc->hc->up_ml;
333  soft_constraintT *sc = vc->sc;
334  int *c = vc->matrices->c;
335  int *fm = vc->matrices->fML;
336  paramT *P = vc->params;
337  int ij = indx[j] + i;
338  int dangle_model = P->model_details.dangles;
339  int turn = P->model_details.min_loop_size;
340  int type = ptype[ij];
341  int *rtype = &(P->model_details.rtype[0]);
342  int circular = P->model_details.circ;
343  int cp = vc->cutpoint;
344  int e = INF;
345 
346  /* extension with one unpaired nucleotide at the right (3' site)
347  or full branch of (i,j)
348  */
349  e = E_ml_rightmost_stem(i,j,vc);
350 
351  /* extension with one unpaired nucleotide at 5' site
352  and all other variants which are needed for odd
353  dangle models
354  */
355  if((cp < 0) || (((i - 1) >= cp) || (i < cp))){
356  switch(dangle_model){
357  /* no dangles */
358  case 0: /* fall through */
359 
360  /* double dangles */
361  case 2: if((cp < 0) || ((i >= cp) || ((i + 1) < cp)))
362  if(hc_up[i]){
363  en = fm[ij + 1] + P->MLbase;
364  if(sc)
365  if(sc->free_energies)
366  en += sc->free_energies[i][1];
367  e = MIN2(e, en);
368  }
369  break;
370 
371  /* normal dangles, aka dangle_model = 1 || 3 */
372  default: mm5 = ((i>1) || circular) ? S[i] : -1;
373  mm3 = ((j<length) || circular) ? S[j] : -1;
374  if((cp < 0) || ((i >= cp) || ((i + 1) < cp)))
375  if(hc_up[i]){
376  en = fm[ij+1] + P->MLbase;
377  if(sc)
378  if(sc->free_energies)
379  en += sc->free_energies[i][1];
380  e = MIN2(e, en);
381  if(hc[ij+1] & VRNA_HC_CONTEXT_MB_LOOP_ENC){
382  type = ptype[ij+1];
383  en = c[ij+1] + E_MLstem(type, mm5, -1, P) + P->MLbase;
384  if(sc)
385  if(sc->free_energies)
386  en += sc->free_energies[i][1];
387  e = MIN2(e, en);
388  }
389  }
390 
391  if((cp < 0) || (((j - 1) >= cp) || (j < cp)))
392  if(hc_up[j]){
393  if(hc[indx[j-1]+i] & VRNA_HC_CONTEXT_MB_LOOP_ENC){
394  type = ptype[indx[j-1]+i];
395  en = c[indx[j-1]+i] + E_MLstem(type, -1, mm3, P) + P->MLbase;
396  if(sc)
397  if(sc->free_energies)
398  en += sc->free_energies[j][1];
399  e = MIN2(e, en);
400  }
401  }
402 
403  if( (cp < 0)
404  || ( (((j - 1) >= cp) || (j < cp))
405  && ((i >= cp) || ((i + 1) < cp))))
406  if(hc[indx[j-1]+i+1] & VRNA_HC_CONTEXT_MB_LOOP_ENC){
407  if(hc_up[i] && hc_up[j]){
408  type = ptype[indx[j-1]+i+1];
409  en = c[indx[j-1]+i+1] + E_MLstem(type, mm5, mm3, P) + 2*P->MLbase;
410  if(sc)
411  if(sc->free_energies)
412  en += sc->free_energies[j][1] + sc->free_energies[i][1];
413  e = MIN2(e, en);
414  }
415  }
416  break;
417  }
418  }
419 
420  /* modular decomposition -------------------------------*/
421  k1j = indx[j] + i + turn + 2;
422  int stop = (cp > 0) ? (cp - 1) : (j - 2 - turn);
423  for (decomp = INF, k = i + 1 + turn; k <= stop; k++, k1j++){
424  en = fmi[k] + fm[k1j];
425  decomp = MIN2(decomp, en);
426  }
427  k++; k1j++;
428  for (;k <= j - 2 - turn; k++, k1j++){
429  en = fmi[k] + fm[k1j];
430  decomp = MIN2(decomp, en);
431  }
432 
433  dmli[j] = decomp; /* store for use in fast ML decompositon */
434  e = MIN2(e, decomp);
435 
436  /* coaxial stacking */
437  if (dangle_model==3) {
438  /* additional ML decomposition as two coaxially stacked helices */
439  int ik;
440  k1j = indx[j]+i+turn+2;
441  for (decomp = INF, k = i + 1 + turn; k <= stop; k++, k1j++){
442  ik = indx[k]+i;
443  if((hc[ik] & VRNA_HC_CONTEXT_MB_LOOP_ENC) && (hc[k1j] & VRNA_HC_CONTEXT_MB_LOOP_ENC)){
444  type = rtype[(unsigned char)ptype[ik]];
445  type_2 = rtype[(unsigned char)ptype[k1j]];
446  en = c[ik] + c[k1j] + P->stack[type][type_2];
447  decomp = MIN2(decomp, en);
448  }
449  }
450  k++; k1j++;
451  for (; k <= j-2-turn; k++, k1j++){
452  ik = indx[k]+i;
453  if((hc[ik] & VRNA_HC_CONTEXT_MB_LOOP_ENC) && (hc[k1j] & VRNA_HC_CONTEXT_MB_LOOP_ENC)){
454  type = rtype[(unsigned char)ptype[ik]];
455  type_2 = rtype[(unsigned char)ptype[k1j]];
456  en = c[ik] + c[k1j] + P->stack[type][type_2];
457  decomp = MIN2(decomp, en);
458  }
459  }
460 
461  decomp += 2*P->MLintern[1]; /* no TermAU penalty if coax stack */
462 #if 0
463  /* This is needed for Y shaped ML loops with coax stacking of
464  interior pairts, but backtracking will fail if activated */
465  DMLi[j] = MIN2(DMLi[j], decomp);
466  DMLi[j] = MIN2(DMLi[j], DMLi[j-1]+P->MLbase);
467  DMLi[j] = MIN2(DMLi[j], DMLi1[j]+P->MLbase);
468  new_fML = MIN2(new_fML, DMLi[j]);
469 #endif
470  e = MIN2(e, decomp);
471  }
472 
473  fmi[j] = e;
474 
475  return e;
476 }
477 
478 
479 
480 INLINE PRIVATE int E_MLstem(int type, int si1, int sj1, paramT *P){
481  int energy = 0;
482  if(si1 >= 0 && sj1 >= 0){
483  energy += P->mismatchM[type][si1][sj1];
484  }
485  else if (si1 >= 0){
486  energy += P->dangle5[type][si1];
487  }
488  else if (sj1 >= 0){
489  energy += P->dangle3[type][sj1];
490  }
491 
492  if(type > 2)
493  energy += P->TerminalAU;
494 
495  energy += P->MLintern[type];
496 
497  return energy;
498 }
499 
500 
501 
502 INLINE PRIVATE double
503 exp_E_MLstem( int type,
504  int si1,
505  int sj1,
506  pf_paramT *P){
507 
508  double energy = 1.0;
509  if(si1 >= 0 && sj1 >= 0){
510  energy = P->expmismatchM[type][si1][sj1];
511  }
512  else if(si1 >= 0){
513  energy = P->expdangle5[type][si1];
514  }
515  else if(sj1 >= 0){
516  energy = P->expdangle3[type][sj1];
517  }
518 
519  if(type > 2)
520  energy *= P->expTermAU;
521 
522  energy *= P->expMLintern[type];
523  return energy;
524 }
525 
526 
527 #endif
The soft constraints data structure.
Definition: data_structures.h:323
#define MIN2(A, B)
Definition: utils.h:150
int cutpoint
The position of the (cofold) cutpoint within the provided sequence. If there is no cutpoint...
Definition: data_structures.h:363
int min_loop_size
Minimum size of hairpin loops.
Definition: model.h:231
int * up_ml
A linear array that holds the number of allowed unpaired nucleotides in a multi branched loop...
Definition: data_structures.h:313
int uniq_ML
Flag to ensure unique multibranch loop decomposition during folding.
Definition: model.h:223
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
#define VRNA_HC_CONTEXT_MB_LOOP
Hard constraints flag, base pair is enclosed in an interior loop.
Definition: constraints.h:199
The datastructure that contains temperature scaled Boltzmann weights of the energy parameters...
Definition: data_structures.h:225
int * en_basepair
Energy contribution for base pairs.
Definition: data_structures.h:326
Definition: data_structures.h:359
#define INF
Definition: energy_const.h:16
int gquad
Include G-quadruplexes in structure prediction.
Definition: model.h:221
int dangles
Specifies the dangle model used in any energy evaluation (0,1,2 or 3)
Definition: model.h:209
PRIVATE int E_ExtLoop(int type, int si1, int sj1, paramT *P)
Definition: exterior_loops.h:409
#define VRNA_HC_CONTEXT_MB_LOOP_ENC
Hard constraints flag, base pair is enclosed in a multi branch loop.
Definition: constraints.h:213
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 circ
Assume RNA to be circular instead of linear.
Definition: model.h:220
int ** free_energies
Energy contribution for unpaired sequence stretches.
Definition: data_structures.h:325