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