-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmp.cpp
334 lines (322 loc) · 10.3 KB
/
kmp.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
// Author: Lalitha Viswanathan
//Affiliation: Tata Consultancy Services
#include <iostream>
extern int listofpotentialcutsites;
class Text;
class KMPPattern{
int *suffixprefixvalues;
char* patternstring;
int lengthofpattern;
public:
// Constructor
KMPPattern(char *);
// Destructor
~KMPPattern();
//Print suffix prefix vals along pattern
void getsuffixprefixpreprocessedvals() const{
for(int counter=0;counter<lengthofpattern;counter++)
std::cout<<" "<<suffixprefixvalues[counter];
}
// return length of pattern
int getpatternlength() const{return lengthofpattern;}
// return pattern string
char* getpatternstring() const{return patternstring;}
void buildsuffixprefixvalarrayforpattern();
char *substringofpattern_prefix_suffix_to_buildspindex(int,int);
int getsuffixprefixvalueatposinpattern(int x)const{return suffixprefixvalues[x];}
};
// Text in which pattern is to be searched
class Text{
char* text,*complementarysequence;
int textlength;
public:
Text(char *,int);
Text(char *);
~Text();
// return length of text
int getlength() const{return textlength;}
// return substring within text
char* getstr() const{return text;}
};
// Overloaded contrusctor along text string
Text::Text(char *textstring,int size)
{
textlength=size;
text=(char *)malloc(sizeof(char)*textlength);
complementarysequence = (char*)malloc(sizeof(char) * textlength);
strcpy(text,textstring);
}
// Overloaded constructor to build text object from file
Text::Text(char *filename)
{
FILE* pointertotextfile;
char tempstr[500],ch;
int temp=0,counterforcomplementarysequence=0,counter;
if(!(pointertotextfile=fopen(filename ,"r")))
{
printf("file %s does not exist\n",filename);
exit(1);
}
fseek(pointertotextfile,0L,SEEK_END);
textlength=ftell(pointertotextfile);
text=(char *)(malloc(sizeof(char)*textlength));
complementarysequence=(char *)(malloc(sizeof(char)*textlength));
fseek(pointertotextfile,0L,SEEK_SET);
fgets(tempstr,500,pointertotextfile);
while(!feof(pointertotextfile))
{
ch=fgetc(pointertotextfile);
if((ch=='A')||(ch=='T')||(ch=='C')||(ch=='G'))
text[temp++]=ch;
}
fclose(pointertotextfile);
textlength=temp;
for(counter=textlength-1;counter>=0;counter--)
{
switch(text[counter])
{
case 'A':
complementarysequence[counterforcomplementarysequence++]='T';
break;
case 'T':
complementarysequence[counterforcomplementarysequence++]='A';
break;
case 'G':
complementarysequence[counterforcomplementarysequence++]='C';
break;
case 'C':
complementarysequence[counterforcomplementarysequence++]='G';
break;
}
}
}
Text::~Text()
{
}
long int* generic(KMPPattern pattern, Text text)
{
int patternlength=pattern.getpatternlength();
int textlength=text.getlength();
// pattern is pointer to str (pattern string) in KMPPattern class
// text is pointer to text string in Text class
char* pattern = pattern.getpatternstring();
char* text = text.getstr();
long int *results=(long int *)malloc(sizeof(long int)*(textlength/patternlength));
for(int counter=0;counter<textlength;counter++)
{
int counteralongpattern=0;
int counteralongtext=counter;
while(counteralongpattern<patternlength)
{
if(pattern[counteralongpattern]==text[counteralongtext])
{
counteralongtext++;
counteralongpattern++;
}
// move along till a mismatch is encountered
//
else if(pattern[counteralongpattern]=='R')
{
if((text[counteralongtext]=='G')||(text[counteralongtext]=='A'))
{counteralongtext++;
counteralongpattern++;}
else
break;
}
else if(pattern[counteralongpattern]=='Y')
{
if((text[counteralongtext]=='C')||(text[counteralongtext]=='T'))
{counteralongtext++;
counteralongpattern++;}
else
break;
}
else if(pattern[counteralongpattern]=='M')
{
if((text[counteralongtext]=='C')||(text[counteralongtext]=='A'))
{counteralongtext++;
counteralongpattern++;}
else
break;
}
else if(pattern[counteralongpattern]=='K')
{
if((text[counteralongtext]=='G')||(text[counteralongtext]=='T'))
{counteralongtext++;
counteralongpattern++;}
else
break;
}
else if(pattern[counteralongpattern]=='S')
{
if((text[counteralongtext]=='G')||(text[counteralongtext]=='C'))
{counteralongtext++;
counteralongpattern++;}
else
break;
}
else if(pattern[counteralongpattern]=='W')
{
if((text[counteralongtext]=='T')||(text[counteralongtext]=='A'))
{counteralongtext++;
counteralongpattern++;}
else
break;
}
else if(pattern[counteralongpattern]=='B')
{
if((text[counteralongtext]=='G')||(text[counteralongtext]=='C')||(text[counteralongtext]=='T'))
{counteralongtext++;
counteralongpattern++;}
else
break;
}
else if(pattern[counteralongpattern]=='D')
{
if((text[counteralongtext]=='G')||(text[counteralongtext]=='A')||(text[counteralongtext]=='T'))
{counteralongtext++;
counteralongpattern++;}
else
break;
}
else if(pattern[counteralongpattern]=='H')
{
if((text[counteralongtext]=='C')||(text[counteralongtext]=='A')||(text[counteralongtext]=='T'))
{counteralongtext++;
counteralongpattern++;}
else
break;
}
else if(pattern[counteralongpattern]=='V')
{
if((text[counteralongtext]=='G')||(text[counteralongtext]=='A')||(text[counteralongtext]=='C'))
{counteralongtext++;
counteralongpattern++;}
else
break;
}
else if(pattern[counteralongpattern]=='N')
{
counteralongtext++;
counteralongpattern++;
}
else
break;
}
if(counteralongpattern>=patternlength)
{
results[listofpotentialcutsites]=counter;
listofpotentialcutsites++;
}
}
// results contains index positions where pattern and text match
return results;
}
// Function that sets suffix prefix values for a given index position in the pattern
void KMPPattern::buildsuffixprefixvalarrayforpattern()
{
suffixprefixvalues=(int *)malloc(sizeof(int)*lengthofpattern);
for(int counteralongpatternstring=0;counteralongpatternstring<lengthofpattern;counteralongpatternstring++)
suffixprefixvalues[counteralongpatternstring]=0;
if(patternstring[1]==patternstring[0])
suffixprefixvalues[1]=1;
else
suffixprefixvalues[1]=0;
int suffixprefixvalueatthatpositioninpatternstring;
// flag when longest prefix that is also matching suffix is reached
for(int innercounteralongpatternstring=2,flag=0;innercounteralongpatternstring<lengthofpattern;innercounteralongpatternstring++,flag=0)
{
// find prefix , suffix
// store index past the matching suffix (end of matching suffix string)
// speeds up pattern search in function call kmpwrapper
// for a given index position i in pattern, find prefix and all suffixes along length of pattern
// for a given position along the pattern string, store index of suffix
for(int lengthofprefixthatisalsopropersuffix=1;(lengthofprefixthatisalsopropersuffix-1)<(innercounteralongpatternstring-lengthofprefixthatisalsopropersuffix);lengthofprefixthatisalsopropersuffix++)
{
char* prefixsubstr=this->substringofpattern_prefix_suffix_to_buildspindex(innercounteralongpatternstring-lengthofprefixthatisalsopropersuffix,innercounteralongpatternstring-1);
char* suffixsubstr=this->substringofpattern_prefix_suffix_to_buildspindex(0,lengthofprefixthatisalsopropersuffix-1);
if(!strcmp(prefixsubstr,suffixsubstr))
// if prefix and suffix don't match, move to start of next suffix string
// store index of position along pattern string where prefix and suffix strings don't match
// repeat process for all sub-suffixes
// these indices are used to jump that many positions along text string while searching for pattern in string
//
{suffixprefixvalueatthatpositioninpatternstring=lengthofprefixthatisalsopropersuffix;flag=1;}
}
if(flag)
suffixprefixvalues[innercounteralongpatternstring]=suffixprefixvalueatthatpositioninpatternstring;
}
}
// Overloaded constructor
KMPPattern::KMPPattern(char *pattern)
{
patternstring=(char *)malloc(sizeof(char)*strlen(pattern));
strcpy(patternstring,pattern);
lengthofpattern=strlen(pattern);
}
//Destructor
KMPPattern::~KMPPattern()
{
}
// return substring of pattern string
// used to get suffixes / sub strings
char* KMPPattern::substringofpattern_prefix_suffix_to_buildspindex(int index,int indexofkmppatternmatchsubstring)
{
char* temp=(char*)malloc(sizeof(char)*(indexofkmppatternmatchsubstring-index+1));
char *pointertostr=patternstring;
for(int counteralongpattern=0;counteralongpattern<index;counteralongpattern++)
pointertostr++;
strncpy(temp,pointertostr,indexofkmppatternmatchsubstring-index+1);
return temp;
}
// Function that builds the suffix prefix list for the pattern
// uses the same to search for pattern in text
// returns position of matches
long int* kmpwrapper(KMPPattern pattern, Text text)
{
pattern.buildsuffixprefixvalarrayforpattern();
char *pointertotext=text.getstr();
int textlength=text.getlength();
char *pointertopattern=pattern.getpatternstring();
int patternlenggth=pattern.getpatternlength();
long int *results=(long int *)malloc(sizeof(long int)*(textlength/patternlenggth));
int counterforkmpsearch=0,matchpos;
// use the suffix prefix values built in pattern.setsuffixprefixvalsforpattern
// to search for pattern in text
// return index of matches
while(counterforkmpsearch+patternlenggth<textlength)
{
int patterncounter=0,flag=0;
matchpos=counterforkmpsearch;
while((pointertopattern[patterncounter]==pointertotext[counterforkmpsearch])&&(patterncounter<patternlenggth))
{
patterncounter++;
flag=1;
counterforkmpsearch++;
}
if(!flag)
counterforkmpsearch++;
else
{
if(patterncounter==patternlenggth)
{
results[listofpotentialcutsites]=matchpos;
counterforkmpsearch=matchpos+1;
listofpotentialcutsites++;
}
else
{
int suffixprefixvalforgivenposalongpatternstring=pattern.getsuffixprefixvalueatposinpattern(patterncounter);
// if value is zero, no characters are matching
// move to next position along text
// suffix prefix value gives number of matching characters and so how much to jump forward along text
if(suffixprefixvalforgivenposalongpatternstring==0)
counterforkmpsearch+=1;
else
// jump that many positions along text
counterforkmpsearch+=suffixprefixvalforgivenposalongpatternstring;
}
}
}
return results;
}