RNAlib-2.4.8
pair_mat.h
1 #ifndef VIENNA_RNA_PACKAGE_PAIR_MAT_H
2 #define VIENNA_RNA_PACKAGE_PAIR_MAT_H
3 
4 #include <ctype.h>
6 #include <ViennaRNA/fold_vars.h>
7 
8 #define NBASES 8
9 /*@notnull@*/
10 
11 #ifndef INLINE
12 # ifdef __GNUC__
13 # define INLINE inline
14 # else
15 # define INLINE
16 # endif
17 #endif
18 
19 static const char Law_and_Order[] = "_ACGUTXKI";
20 static int BP_pair[NBASES][NBASES] =
21  /* _ A C G U X K I */
22 { { 0, 0, 0, 0, 0, 0, 0, 0 },
23  { 0, 0, 0, 0, 5, 0, 0, 5 },
24  { 0, 0, 0, 1, 0, 0, 0, 0 },
25  { 0, 0, 2, 0, 3, 0, 0, 0 },
26  { 0, 6, 0, 4, 0, 0, 0, 6 },
27  { 0, 0, 0, 0, 0, 0, 2, 0 },
28  { 0, 0, 0, 0, 0, 1, 0, 0 },
29  { 0, 6, 0, 0, 5, 0, 0, 0 } };
30 
31 #define MAXALPHA 20 /* maximal length of alphabet */
32 
33 static short alias[MAXALPHA + 1];
34 static int pair[MAXALPHA + 1][MAXALPHA + 1];
35 /* rtype[pair[i][j]]:=pair[j][i] */
36 static int rtype[8] = {
37  0, 2, 1, 4, 3, 6, 5, 7
38 };
39 
40 #ifdef _OPENMP
41 #pragma omp threadprivate(Law_and_Order, BP_pair, alias, pair, rtype)
42 #endif
43 
44 /* for backward compatibility */
45 #define ENCODE(c) encode_char(c)
46 
47 static INLINE int
48 encode_char(char c)
49 {
50  /* return numerical representation of base used e.g. in pair[][] */
51  int code;
52 
53  if (energy_set > 0) {
54  code = (int)(c - 'A') + 1;
55  } else {
56  const char *pos;
57  pos = strchr(Law_and_Order, c);
58  if (pos == NULL)
59  code = 0;
60  else
61  code = (int)(pos - Law_and_Order);
62 
63  if (code > 5)
64  code = 0;
65 
66  if (code > 4)
67  code--; /* make T and U equivalent */
68  }
69 
70  return code;
71 }
72 
73 
74 /*@+boolint +charint@*/
75 /*@null@*/
76 extern char *nonstandards;
77 
78 static INLINE void
79 make_pair_matrix(void)
80 {
81  int i, j;
82 
83  if (energy_set == 0) {
84  for (i = 0; i < 5; i++)
85  alias[i] = (short)i;
86  alias[5] = 3; /* X <-> G */
87  alias[6] = 2; /* K <-> C */
88  alias[7] = 0; /* I <-> default base '@' */
89  for (i = 0; i < NBASES; i++)
90  for (j = 0; j < NBASES; j++)
91  pair[i][j] = BP_pair[i][j];
92  if (noGU)
93  pair[3][4] = pair[4][3] = 0;
94 
95  if (nonstandards != NULL) {
96  /* allow nonstandard bp's */
97  for (i = 0; i < (int)strlen(nonstandards); i += 2)
98  pair[encode_char(nonstandards[i])]
99  [encode_char(nonstandards[i + 1])] = 7;
100  }
101 
102  for (i = 0; i < NBASES; i++)
103  for (j = 0; j < NBASES; j++)
104  rtype[pair[i][j]] = pair[j][i];
105  } else {
106  for (i = 0; i <= MAXALPHA; i++)
107  for (j = 0; j <= MAXALPHA; j++)
108  pair[i][j] = 0;
109  if (energy_set == 1) {
110  for (i = 1; i < MAXALPHA; ) {
111  alias[i++] = 3; /* A <-> G */
112  alias[i++] = 2; /* B <-> C */
113  }
114  for (i = 1; i < MAXALPHA; i++) {
115  pair[i][i + 1] = 2; /* AB <-> GC */
116  i++;
117  pair[i][i - 1] = 1; /* BA <-> CG */
118  }
119  } else if (energy_set == 2) {
120  for (i = 1; i < MAXALPHA; ) {
121  alias[i++] = 1; /* A <-> A*/
122  alias[i++] = 4; /* B <-> U */
123  }
124  for (i = 1; i < MAXALPHA; i++) {
125  pair[i][i + 1] = 5; /* AB <-> AU */
126  i++;
127  pair[i][i - 1] = 6; /* BA <-> UA */
128  }
129  } else if (energy_set == 3) {
130  for (i = 1; i < MAXALPHA - 2; ) {
131  alias[i++] = 3; /* A <-> G */
132  alias[i++] = 2; /* B <-> C */
133  alias[i++] = 1; /* C <-> A */
134  alias[i++] = 4; /* D <-> U */
135  }
136  for (i = 1; i < MAXALPHA - 2; i++) {
137  pair[i][i + 1] = 2; /* AB <-> GC */
138  i++;
139  pair[i][i - 1] = 1; /* BA <-> CG */
140  i++;
141  pair[i][i + 1] = 5; /* CD <-> AU */
142  i++;
143  pair[i][i - 1] = 6; /* DC <-> UA */
144  }
145  } else {
146  vrna_message_error("What energy_set are YOU using??");
147  }
148 
149  for (i = 0; i <= MAXALPHA; i++)
150  for (j = 0; j <= MAXALPHA; j++)
151  rtype[pair[i][j]] = pair[j][i];
152  }
153 }
154 
155 
156 static INLINE short *
157 encode_sequence(const char *sequence,
158  short how)
159 {
160  unsigned int i, l = (unsigned int)strlen(sequence);
161  short *S = (short *)vrna_alloc(sizeof(short) * (l + 2));
162 
163  switch (how) {
164  /* standard encoding as always used for S */
165  case 0:
166  for (i = 1; i <= l; i++) /* make numerical encoding of sequence */
167  S[i] = (short)encode_char(toupper(sequence[i - 1]));
168  S[l + 1] = S[1];
169  S[0] = (short)l;
170  break;
171  /* encoding for mismatches of nostandard bases (normally used for S1) */
172  case 1:
173  for (i = 1; i <= l; i++)
174  S[i] = alias[(short)encode_char(toupper(sequence[i - 1]))];
175  S[l + 1] = S[1];
176  S[0] = S[l];
177  break;
178  }
179 
180  return S;
181 }
182 
183 
184 #endif /* VIENNA_RNA_PACKAGE_PAIR_MAT_H */
void * vrna_alloc(unsigned size)
Allocate space safely.
int energy_set
0 = BP; 1=any with GC; 2=any with AU-parameter
int noGU
Global switch to forbid/allow GU base pairs at all.
char * nonstandards
contains allowed non standard base pairs
#define MAXALPHA
Maximal length of alphabet.
Definition: model.h:163
void vrna_message_error(const char *format,...)
Print an error message and die.
Here all all declarations of the global variables used throughout RNAlib.
General utility- and helper-functions used throughout the ViennaRNA Package.