RNAlib-2.2.0RC0
hairpin_loops.h
Go to the documentation of this file.
1 #ifndef __VIENNA_RNA_PACKAGE_HAIRPIN_LOOPS_H__
2 #define __VIENNA_RNA_PACKAGE_HAIRPIN_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 
59 INLINE PRIVATE int E_Hairpin(int size,
60  int type,
61  int si1,
62  int sj1,
63  const char *string,
64  paramT *P);
65 
84 INLINE PRIVATE double exp_E_Hairpin( int u,
85  int type,
86  short si1,
87  short sj1,
88  const char *string,
89  pf_paramT *P);
90 
91 
92 /*
93 #################################
94 # BEGIN OF FUNCTION DEFINITIONS #
95 #################################
96 */
97 
98 INLINE PRIVATE int
99 E_Hairpin(int size,
100  int type,
101  int si1,
102  int sj1,
103  const char *string,
104  paramT *P){
105 
106  int energy;
107 
108  if(size <= 30)
109  energy = P->hairpin[size];
110  else
111  energy = P->hairpin[30] + (int)(P->lxc*log((size)/30.));
112 
113  if(size < 3) return energy; /* should only be the case when folding alignments */
114 
115  if(P->model_details.special_hp){
116  if(size == 4){ /* check for tetraloop bonus */
117  char tl[7]={0}, *ts;
118  strncpy(tl, string, 6);
119  if ((ts=strstr(P->Tetraloops, tl)))
120  return (P->Tetraloop_E[(ts - P->Tetraloops)/7]);
121  }
122  else if(size == 6){
123  char tl[9]={0}, *ts;
124  strncpy(tl, string, 8);
125  if ((ts=strstr(P->Hexaloops, tl)))
126  return (energy = P->Hexaloop_E[(ts - P->Hexaloops)/9]);
127  }
128  else if(size == 3){
129  char tl[6]={0,0,0,0,0,0}, *ts;
130  strncpy(tl, string, 5);
131  if ((ts=strstr(P->Triloops, tl))) {
132  return (P->Triloop_E[(ts - P->Triloops)/6]);
133  }
134  return (energy + (type>2 ? P->TerminalAU : 0));
135  }
136  }
137  energy += P->mismatchH[type][si1][sj1];
138 
139  return energy;
140 }
141 
146 INLINE PRIVATE int
147 E_hp_loop(int i,
148  int j,
149  vrna_fold_compound *vc){
150 
151  int u, e, ij, type;
152  char hc;
153 
154  int cp = vc->cutpoint;
155  short *S = vc->sequence_encoding;
156  int *idx = vc->jindx;
157  paramT *P = vc->params;
158  int *hc_up = vc->hc->up_hp;
159  soft_constraintT *sc = vc->sc;
160  model_detailsT *md = &(P->model_details);
161 
162 
163  u = j - i - 1;
164  ij = idx[j] + i;
165  type = vc->ptype[ij];
166  hc = vc->hc->matrix[ij];
167 
168  /* is this base pair allowed to close a hairpin (like) loop ? */
169  if(hc & VRNA_HC_CONTEXT_HP_LOOP){
170  /* are all nucleotides in the loop allowed to be unpaired ? */
171  if(hc_up[i+1] >= u){
172 
173  if((cp < 0) || ((i >= cp) || (j < cp))){ /* regular hairpin loop */
174  e = E_Hairpin(u, type, S[i+1], S[j-1], vc->sequence+i-1, P);
175  } else { /* hairpin-like exterior loop (for cofolding) */
176  short si, sj;
177  si = ((i >= cp) || ((i + 1) < cp)) ? S[i+1] : -1;
178  sj = (((j - 1) >= cp) || (j < cp)) ? S[j-1] : -1;
179  if (md->dangles)
180  e = E_ExtLoop(md->rtype[type], sj, si, P);
181  else
182  e = E_ExtLoop(md->rtype[type], -1, -1, P);
183  }
184 
185  /* add soft constraints */
186  if(sc){
187  if(sc->free_energies)
188  e += sc->free_energies[i+1][u];
189 
190  if(sc->en_basepair){
191  e += sc->en_basepair[ij];
192  }
193  if(sc->f)
194  e += sc->f(i, j, i, j, VRNA_DECOMP_PAIR_HP, sc->data);
195  }
196 
197  return e;
198  }
199  }
200  return INF;
201 }
202 
203 INLINE PRIVATE int
204 E_hp_loop_ali(int i,
205  int j,
206  vrna_fold_compound *vc){
207 
208  int u, e, s, *type;
209  int n_seq = vc->n_seq;
210  int *idx = vc->jindx;
211  paramT *P = vc->params;
212  model_detailsT *md = &(P->model_details);
213  short **S = vc->S;
214  short **S5 = vc->S5; /*S5[s][i] holds next base 5' of i in sequence s*/
215  short **S3 = vc->S3; /*Sl[s][i] holds next base 3' of i in sequence s*/
216  char **Ss = vc->Ss;
217  unsigned short **a2s = vc->a2s;
218  char hc = vc->hc->matrix[idx[j]+i];
219  int *hc_up = vc->hc->up_hp;
220  soft_constraintT **sc = vc->scs;
221 
222  type = (int *)space(sizeof(int) * n_seq);
223 
224  for (s=0; s<n_seq; s++) {
225  type[s] = md->pair[S[s][i]][S[s][j]];
226  if (type[s]==0) type[s]=7;
227  }
228 
229  /* is this base pair allowed to close a hairpin loop ? */
230  if(hc & VRNA_HC_CONTEXT_HP_LOOP){
231  if(hc_up[i+1] >= j - i - 1){
232  for (e=s=0; s<n_seq; s++) {
233  u = a2s[s][j-1] - a2s[s][i];
234  if (u < 3) e += 600;
235  else e += E_Hairpin(u, type[s], S3[s][i], S5[s][j], Ss[s]+(a2s[s][i-1]), P);
236  }
237 
238  if(sc)
239  for(s = 0; s < n_seq; s++){
240  if(sc[s]){
241  u = a2s[s][j-1]-a2s[s][i];
242 
243  if(sc[s]->free_energies)
244  e += sc[s]->free_energies[a2s[s][i]+1][u];
245 
246  if(sc[s]->en_basepair)
247  e += sc[s]->en_basepair[idx[j] + i];
248 
249  if(sc[s]->f)
250  e += sc[s]->f(i, j, i, j, VRNA_DECOMP_PAIR_HP, sc[s]->data);
251  }
252  }
253 
254  free(type);
255  return e;
256  }
257  }
258 
259  free(type);
260  return INF;
261 }
262 
263 /*
264 *************************************
265 * Partition function variants below *
266 *************************************
267 */
268 
269 
270 INLINE PRIVATE double
272  int type,
273  short si1,
274  short sj1,
275  const char *string,
276  pf_paramT *P){
277 
278  double q, kT;
279  kT = P->kT; /* kT in cal/mol */
280 
281  if(u <= 30)
282  q = P->exphairpin[u];
283  else
284  q = P->exphairpin[30] * exp( -(P->lxc*log( u/30.))*10./kT);
285 
286  if(u < 3) return q; /* should only be the case when folding alignments */
287 
288  if(P->model_details.special_hp){
289  if(u==4){
290  char tl[7]={0,0,0,0,0,0,0}, *ts;
291  strncpy(tl, string, 6);
292  if ((ts=strstr(P->Tetraloops, tl))){
293  if(type != 7)
294  return (P->exptetra[(ts-P->Tetraloops)/7]);
295  else
296  q *= P->exptetra[(ts-P->Tetraloops)/7];
297  }
298  }
299  else if(u==6){
300  char tl[9]={0,0,0,0,0,0,0,0,0}, *ts;
301  strncpy(tl, string, 8);
302  if ((ts=strstr(P->Hexaloops, tl)))
303  return (P->exphex[(ts-P->Hexaloops)/9]);
304  }
305  else if(u==3){
306  char tl[6]={0,0,0,0,0,0}, *ts;
307  strncpy(tl, string, 5);
308  if ((ts=strstr(P->Triloops, tl)))
309  return (P->exptri[(ts-P->Triloops)/6]);
310  if (type>2)
311  return q * P->expTermAU;
312  else
313  return q;
314  }
315  }
316  q *= P->expmismatchH[type][si1][sj1];
317 
318  return q;
319 }
320 
326 INLINE PRIVATE double
328  int j,
329  vrna_fold_compound *vc){
330 
331  int u, ij, type;
332  char hc;
333  double q;
334 
335  int cp = vc->cutpoint;
336  short *S = vc->sequence_encoding;
337  int *idx = vc->jindx;
338  pf_paramT *P = vc->exp_params;
339  int *hc_up = vc->hc->up_hp;
340  soft_constraintT *sc = vc->sc;
341  model_detailsT *md = &(P->model_details);
342  FLT_OR_DBL *scale = vc->exp_matrices->scale;
343 
344  q = 0.;
345  u = j - i - 1;
346  ij = idx[j] + i;
347  type = vc->ptype[ij];
348  hc = vc->hc->matrix[ij];
349 
350  /* is this base pair allowed to close a hairpin (like) loop ? */
351  if(hc & VRNA_HC_CONTEXT_HP_LOOP){
352  /* are all nucleotides in the loop allowed to be unpaired ? */
353  if(hc_up[i+1] >= u){
354 
355  if((cp < 0) || ((i >= cp) || (j < cp))){ /* regular hairpin loop */
356  q = exp_E_Hairpin(u, type, S[i+1], S[j-1], vc->sequence+i-1, P)
357  * scale[u+2];
358  } else { /* hairpin-like exterior loop (for cofolding) */
359 
360  }
361 
362  /* add soft constraints */
363  if(sc){
364  if(sc->boltzmann_factors)
365  q *= sc->boltzmann_factors[i+1][u];
366 
367  if(sc->exp_en_basepair)
368  q *= sc->exp_en_basepair[ij];
369 
370  if(sc->exp_f)
371  q *= sc->exp_f(i, j, i, j, VRNA_DECOMP_PAIR_HP, sc->data);
372  }
373  }
374  }
375  return q;
376 }
377 
378 
379 #endif
int * up_hp
A linear array that holds the number of allowed unpaired nucleotides in a hairpin loop...
Definition: data_structures.h:307
The soft constraints data structure.
Definition: data_structures.h:323
void * space(unsigned size)
Allocate space safely.
int cutpoint
The position of the (cofold) cutpoint within the provided sequence. If there is no cutpoint...
Definition: data_structures.h:363
double(* exp_f)(int, int, int, int, char, void *)
A function pointer used for pseudo energy contribution boltzmann factors in PF calculations.
Definition: data_structures.h:343
double ** boltzmann_factors
Boltzmann Factors of the energy contributions for unpaired sequence stretches.
Definition: data_structures.h:327
#define VRNA_HC_CONTEXT_HP_LOOP
Hard constraints flag, base pair encloses hairpin loop.
Definition: constraints.h:185
int special_hp
Include special hairpin contributions for tri, tetra and hexaloops.
Definition: model.h:215
The datastructure that contains temperature scaled energy parameters.
Definition: data_structures.h:180
PRIVATE double exp_E_Hairpin(int u, int type, short si1, short sj1, const char *string, pf_paramT *P)
Compute Boltzmann weight of a hairpin loop.
Definition: hairpin_loops.h:271
model_detailsT model_details
Model details to be used in the recursions.
Definition: data_structures.h:218
#define VRNA_DECOMP_PAIR_HP
Generalized constraint folding flag indicating hairpin loop decomposition step.
Definition: constraints.h:230
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
PRIVATE int E_Hairpin(int size, int type, int si1, int sj1, const char *string, paramT *P)
Compute the Energy of a hairpin-loop.
Definition: hairpin_loops.h:99
#define INF
Definition: energy_const.h:16
PRIVATE int E_hp_loop(int i, int j, vrna_fold_compound *vc)
High-Level function for hairpin loop energy evaluation.
Definition: hairpin_loops.h:147
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
PRIVATE double exp_E_hp_loop(int i, int j, vrna_fold_compound *vc)
High-Level function for hairpin loop energy evaluation (partition function variant) ...
Definition: hairpin_loops.h:327
PRIVATE int E_ExtLoop(int type, int si1, int sj1, paramT *P)
Definition: exterior_loops.h:409
int(* f)(int, int, int, int, char, void *)
A function pointer used for pseudo energy contribution in MFE calculations.
Definition: data_structures.h:334
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
double * exp_en_basepair
Boltzmann Factors of the energy contribution for base pairs.
Definition: data_structures.h:328
model_detailsT model_details
Model details to be used in the recursions.
Definition: data_structures.h:273