00001
00002 #include "rnautil.h"
00003 #include "common.h"
00004
00005 const char *RNA_PAIRS[] = {"AU","UA","GC","CG","GU","UG",0};
00006
00007
00008 void dna2rna(char *s)
00009
00010 {
00011 for (;*s;s++)
00012 {
00013 if (*s == 't')
00014 *s = 'u';
00015 if (*s == 'T')
00016 *s = 'U';
00017 }
00018 }
00019
00020 bool rnaPair(char a, char b)
00021
00022 {
00023 char pair[] = {a,b,'\0'};
00024 int i;
00025 dna2rna(pair);
00026 touppers(pair);
00027
00028 for (i=0;RNA_PAIRS[i] != 0; i++)
00029 if (pair[0] == RNA_PAIRS[i][0] && pair[1] == RNA_PAIRS[i][1] )
00030 return TRUE;
00031 return FALSE;
00032 }
00033
00034 void reverseFold(char *s)
00035
00036 {
00037 reverseBytes(s, strlen(s));
00038 for (;*s;s++)
00039 {
00040 if (*s == '(')
00041 *s = ')';
00042 else if (*s == ')')
00043 *s = '(';
00044 }
00045 }
00046
00047 void fold2pairingList(char *fold, int len, int **p2pairList)
00048
00049
00050
00051 {
00052 int i,j, stackSize = 0;
00053 int *pairList = needMem(len * sizeof(int));
00054 *p2pairList = pairList;
00055
00056
00057 for (i = 0; i < len; i++)
00058 pairList[i] = -1;
00059
00060
00061 for (i = 0; i < len; i++)
00062 {
00063 if (fold[i] == '(')
00064 {
00065 stackSize = 1;
00066 for (j = i+1; j < len; j++)
00067 {
00068 if (fold[j] == '(')
00069 stackSize += 1;
00070 else if (fold[j] == ')')
00071 stackSize -= 1;
00072 if (stackSize == 0)
00073 {
00074 pairList[i] = j;
00075 pairList[j] = i;
00076 break;
00077 }
00078 }
00079 }
00080 }
00081 }
00082
00083 void mkPairPartnerSymbols(int *pairList, char *pairSymbols, int size)
00084 {
00085
00086 int i;
00087 char symbols[] = "abcdefghiklmnopqrstuvwxyzABCDEFGHIKLMNOPQRSTUVWXYZ1234567890!@#$%^&*=+{}|[]\\;'<>";
00088 int symbolMax = strlen(symbols);
00089 int index;
00090 for (i = 0, index = 0; i < size; i++)
00091 {
00092 pairSymbols[i] = ' ';
00093 if (pairList[i] >= 0)
00094 {
00095 if (pairList[i] < i)
00096 {
00097 --index;
00098 if (index<0)
00099 index = symbolMax-1;
00100 }
00101 pairSymbols[i] = symbols[index];
00102 if (pairList[i] > i)
00103 {
00104 ++index;
00105 if (index>=symbolMax)
00106 index=0;
00107 }
00108 }
00109 }
00110 }
00111
00112 char * projectString(char *s, char *ref, char refChar, char insertChar)
00113
00114 {
00115 int i,j,size = strlen(ref);
00116 char *copy = (char *) needMem(size + 1);
00117
00118 if (strlen(s) != strlen(ref) - countChars(ref, refChar))
00119 errAbort("ERROR from rnautil::projectString: Input string 's' has wrong length.\n");
00120
00121 for (i = 0, j = 0; i < size; i++)
00122 {
00123 if (ref[i] == refChar)
00124 copy[i] = insertChar;
00125 else
00126 {
00127 copy[i] = s[j];
00128 j++;
00129 }
00130 }
00131 return copy;
00132 }
00133
00134 char *gapAdjustFold(char *s, char *ref)
00135
00136 {
00137 return projectString(s, ref, '-', ' ');
00138 }
00139
00140
00141 int *projectIntArray(int *in, char *ref, char refChar, int insertInt)
00142
00143 {
00144 int i,j,size = strlen(ref);
00145 int *copy = (int *) needMem(size *sizeof(int) );
00146
00147 for (i = 0, j = 0; i < size; i++)
00148 {
00149 if (ref[i] == refChar)
00150 copy[i] = insertInt;
00151 else
00152 {
00153 copy[i] = in[j];
00154 j++;
00155 }
00156 }
00157 return copy;
00158 }
00159
00160 int * gapIntArrayAdjust(int *in, char *ref)
00161
00162 {
00163 return projectIntArray(in, ref, '-', 0);
00164 }
00165
00166 void markCompensatoryMutations(char *s, char *ref, int *pairList, int *markList)
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 {
00179 int i, size = strlen(s);
00180 for (i = 0; i < size; i++)
00181 {
00182 if (pairList[i] == -1)
00183 if (toupper(s[i]) != toupper(ref[i]) && s[i] != '.' && s[i] != '-' && ref[i] != '-')
00184 markList[i] = 1;
00185 else
00186 markList[i] = 0;
00187 else
00188 {
00189 if (s[i] == '.' || s[pairList[i]] == '.')
00190 markList[i] = 2;
00191 else if (!rnaPair(s[i], s[pairList[i]]))
00192 markList[i] = 5;
00193 else if (toupper( s[i] ) != toupper( ref[i] ) && toupper( s[pairList[i]] ) != toupper( ref[pairList[i]] ) )
00194 markList[i] = 4;
00195 else if (toupper( s[i] ) != toupper( ref[i] ) || toupper( s[pairList[i]] ) != toupper( ref[pairList[i]] ) )
00196 markList[i] = 3;
00197 else
00198 markList[i] = 2;
00199 }
00200 }
00201 }
00202
00203 int assignBin(double val, double minVal, double maxVal, int binCount)
00204
00205
00206 {
00207 double range = maxVal - minVal;
00208 int maxBin = binCount - 1;
00209 int level = (int) ( (val-minVal)*maxBin/range);
00210 if (level <= 0) level = 0;
00211 if (level > maxBin) level = maxBin;
00212 return level;
00213 }