-
Notifications
You must be signed in to change notification settings - Fork 0
/
motif.cpp
392 lines (365 loc) · 8.52 KB
/
motif.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "motif.h"
motif::motif()
{
}
motif::motif(bool is_nondegenerate)
{
// Generate a random motif.
int i;
int maxlength=6;
int length;
string out;
ExpVal = 0.0;
length = ran2(2, maxlength+1);
sequence.resize(length);
for (i=0; i<length; i++)
{
sequence[i] = GetRandomBase(is_nondegenerate);
}
doComplement();
CalcDegeneracy();
get_max_length();
}
motif::motif(string input)
{
// Set motif to input sting.
ExpVal = 0.0;
sequence = input;
CalcDegeneracy();
get_max_length();
doComplement();
}
motif::~motif()
{
}
char motif::getComplement(char base)
{
switch (base)
{
case 'a': return 't';
case 'c': return 'g';
case 'g': return 'c';
case 't': return 'a';
case 'r': return 'y';
case 'y': return 'r';
case 'k': return 'm';
case 'm': return 'k';
case 's': return 's';
case 'w': return 'w';
case 'b': return 'v';
case 'd': return 'h';
case 'h': return 'd';
case 'v': return 'b';
case 'n': return 'n';
case '1': return '1';
case '2': return '2';
case '3': return '3';
case '4': return '4';
case '5': return '5';
case '6': return '6';
case '7': return '7';
case '8': return '8';
case '9': return '9';
default:
cout << "Cannot find base complement of : " << base << endl;
abort();
return base;
}
}
void motif::doComplement()
{
unsigned int i;
char base;
unsigned int seqlen;
seqlen = (unsigned int)sequence.length();
complement.resize(seqlen);
for (i=0; i<seqlen; i++)
{
base = sequence[i];
complement[seqlen-i-1] = getComplement(base);
}
}
inline bool motif::basematch(const char base1, const char base2)
{
switch (base1)
{
case 'a':
switch (base2)
{
/*case 'c': case 'g': case 't': case 'y': case 'k': case 's': case 'b': // quicker to test the -ve case
return false;
default:
return true;*/
case 'a': case 'r': case 'm': case 'w': case 'd': case 'h': case 'v': case 'n':
return true;
default:
return false;
}
break;
case 'c':
switch (base2)
{
/*case 'a': case 'g': case 't': case 'r': case 'k': case 'w': case 'h': // quicker to test the -ve case
return false;
default:
return true;*/
case 'c': case 'y': case 'm': case 's': case 'b': case 'h': case 'v': case 'n':
return true;
default:
return false;
}
break;
case 'g':
switch (base2)
{
/*case 'a': case 'c': case 't': case 'y': case 'm': case 'w': case 'h': // quicker to test the -ve case
return false;
default:
return true;*/
case 'g': case 'r': case 'k': case 's': case 'b': case 'd': case 'v': case 'n':
return true;
default:
return false;
}
break;
case 't':
switch (base2)
{
/*case 'a': case 'c': case 'g': case 'r': case 'm': case 's': case 'v': // quicker to test the -ve case
return false;
default:
return true;*/
case 't': case 'y': case 'k': case 'w': case 'b': case 'd': case 'h': case 'n':
return true;
default:
return false;
}
break;
case 'n':
return false;
default:
cout << "\nNon-standard character in sequence file: " << base1 << " : " << base2 << endl;
//abort();
return false;
}
return false;
}
// The main search routine. This code is running 99.9% of the time so needs to be fast
// (hence the use of char*)
bool motif::IsMotifInSequence(const char* inputseq, const char* wild, int allowed_mutations)
{
const char *cp = inputseq;
const char *mp = wild;
bool match;
int max_gap_size;
int max_gap_size_plus_one;
char *buffer;
size_t sizeof_char = sizeof( char );
char null_char = (char)NULL;
int mutations = 0;
while ((*inputseq) && (*wild))
{
if (basematch(*inputseq, *wild)) // Compare degenerate bases
{
wild++; // Move to next character
inputseq++;
}
else if ((*wild > '0') && (*wild <= '9')) // This bit allows for gaps in the motif.
{
max_gap_size = (*wild - '0') + max_length;
max_gap_size_plus_one = max_gap_size+1;
while ((*wild) && (*wild > '0') && (*wild <= '9')) // Remove all prefixed numbers
wild++;
buffer = new char[max_gap_size_plus_one];
strncpy(buffer, inputseq, sizeof_char * max_gap_size_plus_one);
buffer[max_gap_size] = null_char;
match = IsMotifInSequence(buffer, wild, allowed_mutations - mutations);
delete [] buffer;
if (match)
return true; // The call to the recursion returned true, so we should return true.
else
{
wild = mp; // We do not have a match,
cp++; // so reset the wild string, and goto the next char on the inputsequence.
inputseq = cp;
mutations = 0;
}
}
else if (mutations < allowed_mutations)
{
wild++; // Move to next character
inputseq++;
mutations++;
}
else
{
wild = mp; // We do not have a match,
cp++; // so reset the wild string, and goto the next char on the inputsequence.
inputseq = cp;
mutations = 0;
}
}
return (bool)!*wild;
}
vector<int> motif::returnMotifLocations(const char* inputseq, const char* wild, int allowed_mutations)
{
const char *cp = inputseq;
const char *mp = wild;
bool match;
int max_gap_size;
vector<int> positions;
int pos = 0;
int max_gap_size_plus_one;
char *buffer;
size_t sizeof_char = sizeof( char );
char null_char = (char)NULL;
int mutations = 0;
while (*inputseq)
{
if (basematch(*inputseq, *wild)) // Compare degenerate bases
{
wild++; // Move to next character
inputseq++;
//pos++;
}
else if ((*wild > '0') && (*wild <= '9')) // This bit allows for gaps in the motif.
{
max_gap_size = (*wild - '0') + max_length;
max_gap_size_plus_one = max_gap_size+1;
while ((*wild) && (*wild > '0') && (*wild <= '9')) // Remove all prefixed numbers
wild++;
buffer = new char[max_gap_size_plus_one];
strncpy(buffer, inputseq, sizeof_char * max_gap_size_plus_one);
buffer[max_gap_size] = null_char;
match = IsMotifInSequence(buffer, wild, allowed_mutations - mutations);
delete [] buffer;
if (match)
positions.push_back(pos); // The call to the recursion returned true, so we should save the position.
else
{
wild = mp; // We do not have a match,
cp++; // so reset the wild string, and goto the next char on the inputsequence.
inputseq = cp;
pos++;
mutations = 0;
}
}
else if (mutations < allowed_mutations)
{
wild++; // Move to next character
inputseq++;
mutations++;
}
else
{
wild = mp; // We do not have a match,
cp++; // so reset the wild string, and goto the next char on the inputsequence.
inputseq = cp;
pos++;
mutations = 0;
}
if ((bool)!*wild)
{
positions.push_back(pos);
wild = mp;
cp++;
inputseq = cp;
pos++;
mutations = 0;
}
}
return positions;
}
void motif::get_max_length()
{
unsigned int out = 0;
const char* wild = sequence.c_str();
while (*wild)
{
if ((*wild > '1') && (*wild <= '9'))
{
out += (*wild - '0');
}
else
out++;
wild++;
}
//return out;
max_length = out;
}
void motif::CalcDegeneracy()
{
unsigned int i;
char base;
degeneracy= 0.0;
for (i=0; i<sequence.length(); i++)
{
base = sequence[i];
switch (base)
{
case 'a': case 't': case 'g': case 'c':
degeneracy++;
break;
case 'r': case 'y': case 'k': case 'm': case 's': case 'w':
degeneracy += 2;
break;
case 'b': case 'd': case 'h': case 'v':
degeneracy += 3;
break;
case 'n':
degeneracy += 4;
break;
case '1': case '2': case '3': case '4': case '5': case '6': case '7':case '8': case '9':
degeneracy += (4 * (base - '0' + 1));
break;
default:
printf("Non-standard base in motif\n");
cout << base << endl;
cout << sequence << endl;
abort();
}
}
degeneracy /= (double)sequence.length();
}
char motif::GetRandomBase(bool is_nondegenerate)
{
int letter;
if (is_nondegenerate == true)
{
letter = ran2(0,6);
if (letter == 4) letter = 14;
if (letter == 5) letter = 15;
}
else
letter = ran2(0, 16);
switch(letter)
{
case 0: return 'a'; break;
case 1: return 'c'; break;
case 2: return 'g'; break;
case 3: return 't'; break;
case 4: return 'r'; break;
case 5: return 'y'; break;
case 6: return 'k'; break;
case 7: return 'm'; break;
case 8: return 's'; break;
case 9: return 'w'; break;
case 10: return 'b'; break;
case 11: return 'd'; break;
case 12: return 'h'; break;
case 13: return 'v'; break;
case 14: return 'n'; break;
case 15:
{ // insert a gap of size 1 to 9.
int i; char out;
i=ran2(1, 10);
out = '0' + i;
return out;
break;
}
default:
printf("error - illegal nucleotide\n");
abort();
return '-';
}
}