-
Notifications
You must be signed in to change notification settings - Fork 5
/
mreps.c
383 lines (316 loc) · 11.7 KB
/
mreps.c
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
/* Computing exact repetitions */
#include <stdlib.h>
#include <stdio.h>
#include "defs.h"
extern void print_rep(int rinitpos, int rendpos, int rlength, int rperiod, int rnumerr);
extern void print_score(int rinitpos,int rendpos,int rlength, int rperiod, float rscore);
extern void finalrepets (int prvFacBgn, int curFacBgn);
extern void SortFirstTypeRepeats(void);
extern void EmergSortFirstTypeRepeats(void);
extern void FindSecondTypeExactRepeats(void);
void Showmreps(void);
void print_mrepeat(int rinitpos, int rendpos, int rperiod);
extern void RecodeSmalSeq(int chkLength);
extern void RecodeCapSeq(int chkLength);
extern int *computeLIx(char *pattern, int patLen, int dir, int maxPerBnd);
extern void mainrepets(int prvFacBgn, int curFacBgn, int nxtFacBgn);
extern sfactorization FactorizeforGDR(void);
extern void guessDepth(int length);
extern int step, LastWindow;
extern int maxPer, oldPerBnd;
extern char *seq_factor_copy;
extern int FACTYPE;
extern listreps *presortLists, *sortedLists, *maxIntPtr;
extern char nextLetterCheck, prevLetterCheck ;
extern limits lim ;
extern FILE *output_file; // output file (for xml output)
extern char * output_file_name;
extern int xmloutput;
int lenWrd; // declare it here!
int tooBigReps ;
char *seq_original, *seq_original1; // treated window of original sequence
int verbose = 1; // defined as extern in defs.h
int verbose_long = 1 ; // defined as extern in defs.h
void Savemreps(void);
int check_mrepeat(listreps checkRepeat);
void ComputePerBound(void);
extern int numFact, *factorBgns;
extern listreps *allReps;
void maxreps (char *seq_parameter, int length_parameter)
/* seaching for exact reps in the current window */
{
int i;
sfactorization sfact ;
seq_original=seq_parameter; // remember the sequence address in global variable 'seq_original'
seq_original1=seq_original-1;
lenWrd=length_parameter; // remember the sequence length in global variable 'lenWrd'
VERB(2) printf("input : %s\n", seq_original);
VERB(2) printf("length : %d\n", length_parameter);
/* validating maximal possible period */
if (LastWindow)
maxPer=(lim.max_period>length_parameter/2) ? length_parameter/2 :lim.max_period;
else
maxPer=(lim.max_period>step/2) ? step/2 :lim.max_period;
VERB(1) printf("Maximal possible period of repeats is %d\n\n", maxPer);
/* s-factorization (sfact.c) */
// create a copy of the sequence to process it in RecodeSeq()
seq_factor_copy = (char *)malloc(length_parameter+2);
seq_factor_copy[0]='\0';
/* strcpy (seq_factor_copy+1, seq_original); */
if (*seq_original>='a')
RecodeSmalSeq(length_parameter);
else
RecodeCapSeq(length_parameter);
VERB(2)
{
printf("recoded seq: ");
for (i=1;i<=length_parameter;i++) printf("%1d",seq_factor_copy[i]);
printf("\n");
}
FACTYPE=WITHOUT_EXTRA_SYMBOL;
guessDepth(length_parameter);
sfact = FactorizeforGDR();
if (lim.err_number)
ComputePerBound();
else
free(seq_factor_copy);
VERB(1) printf("s-factorization : %d factors\n", sfact->nbfactors);
/* repets of type 1 (mainrepets.c) */
if( ( presortLists=(listreps *) calloc ((2+lenWrd), sizeof(listreps)) ) == NULL )
{
fprintf(stderr,"Not enough memory for saving the repetitions\n");
RECOVER_XML_OUTPUT(output_file,output_file_name,20);
exit(20);
}
for (i=1; i< sfact -> nbfactors ; i++)
{
VERB(1) printf(" s-fact: [%d, %d]\n",
sfact->factor[i], sfact->factor[i+1]) ;
mainrepets (sfact->factor[i-1], sfact->factor[i], sfact->factor[i+1]);
}
finalrepets (sfact->factor[i-1], sfact->factor[i]);
VERB(1) printf("repetitions of type 1 : OK.\n");
/* Now we sort repetitions of type 1 */
/* if ( (sortedLists=(listreps *)calloc(lenWrd+2, sizeof(listreps))) )
* SortFirstTypeRepeats();
* else
*/
EmergSortFirstTypeRepeats();
VERB(1) printf("sorting m-repets of type 1 : OK.\n");
VERB(2) printf ("now repetitions of type 2 \n");
/* repetitions of type 2 */
FindSecondTypeExactRepeats();
VERB(1) printf("finding repetitions of type 2 : OK.\n");
free (sfact);
/* Now we print the appropriate repetitions */
if (lim.err_number==0)
Showmreps();
else
Savemreps();
}
void Showmreps(void)
/* filtering out exact reps which start in the second half of the window (unless it is the last one),
or extend beyond the window;
call print_mrepeat on those reps which go through filtering */
{
int endPstn, initPos, repPer;
listreps current_rep, *current_pointer, remListRep;
/* if (sortedLists+step<=maxIntPtr) */
/* if (step > 0 && sortedLists+step<=maxIntPtr) // if it is not the last window (?) */
if (LastWindow==NO)
maxIntPtr=sortedLists+step-1; // output repetition starting in the first half only
current_pointer=sortedLists;
if ( (current_rep=(*current_pointer)) ) // if there are reps starting at the first position
// treat them separately (because of prevLetterCheck)
do
{
repPer=current_rep->rep.period;
if (seq_original1[repPer]!=prevLetterCheck) // repeat does not extend to the left
{
if ( (endPstn=current_rep->rep.endpos)<lenWrd ) // it does not touch the end
print_mrepeat(0, endPstn, repPer);
else
{
if (seq_original[lenWrd-repPer]!=nextLetterCheck)
print_mrepeat(0, lenWrd, repPer);
else
tooBigReps=YES;
}
}
current_rep=(remListRep=current_rep)->next;
free(remListRep);
}
while(current_rep);
while(++current_pointer<=maxIntPtr)
{
if ((current_rep=(*current_pointer)))
{
initPos=current_rep->rep.initpos;
do
{
repPer=current_rep->rep.period;
if ( (endPstn=current_rep->rep.endpos)<lenWrd ) // the repeat does not
// touch the end of the window
print_mrepeat(initPos, endPstn, repPer);
else
{
if (seq_original[lenWrd-repPer]!=nextLetterCheck)
print_mrepeat(initPos, lenWrd, repPer);
else
tooBigReps=YES;
}
current_rep=(remListRep=current_rep)->next;
free(remListRep);
}
while(current_rep);
}
}
free(sortedLists);
}
void Savemreps(void)
/* filtering out exact reps which start in the second half of the window (unless it is the last one),
or extend beyond the window;
save those reps which go through filtering */
{
listreps current_rep, *current_pointer, next_rep;
if ( (allReps=(listreps *)calloc(oldPerBnd, sizeof(listreps)))==NULL )
{
printf("Error: Not enough memory for storing exact repeats\n");
RECOVER_XML_OUTPUT(output_file,output_file_name,20);
exit(20);
}
if (LastWindow==NO)
maxIntPtr=sortedLists+step-1; // output repetition starting in the first half only
for(current_pointer=maxIntPtr;current_pointer>sortedLists;current_pointer--)
{
if ((current_rep=(*current_pointer)))
{ while ( (next_rep=current_rep->next) )
{ if ( check_mrepeat(current_rep) )
{ current_rep->next=allReps[current_rep->rep.period];
allReps[current_rep->rep.period]=current_rep;
}
else
free(current_rep);
current_rep=next_rep;
}
if ( (current_rep->rep.endpos<lenWrd) || // the repeat does not
(seq_original[lenWrd-current_rep->rep.period] // cross the end of the window
!= nextLetterCheck) )
{ if ( check_mrepeat(current_rep) )
{ current_rep->next=allReps[current_rep->rep.period];
allReps[current_rep->rep.period]=current_rep;
}
else
free(current_rep);
}
else
{ free(current_rep);
tooBigReps=YES;
}
}
}
if ( (current_rep=(*current_pointer)) ) // if there are reps starting at the first position
// treat them separately (because of prevLetterCheck)
{ while ( (next_rep=current_rep->next) )
{ if (seq_original1[current_rep->rep.period]!=prevLetterCheck)
// repeat does not extend to the left
{ if ( check_mrepeat(current_rep) )
{ current_rep->next=allReps[current_rep->rep.period];
allReps[current_rep->rep.period]=current_rep;
}
else
free(current_rep);
}
else
free(current_rep);
current_rep=next_rep;
}
if (seq_original1[current_rep->rep.period]!=prevLetterCheck)
// repeat does not extend to the left
{ if ( (current_rep->rep.endpos<lenWrd) || // the repeat does not
(seq_original[lenWrd-current_rep->rep.period] // cross the end of the window
!= nextLetterCheck) )
{ if ( check_mrepeat(current_rep) )
{ current_rep->next=allReps[current_rep->rep.period];
allReps[current_rep->rep.period]=current_rep;
}
else
free(current_rep);
}
else
{ free(current_rep);
tooBigReps=YES;
}
}
else
free(current_rep);
}
free(sortedLists);
}
int check_mrepeat(listreps checkRep)
/* check if the period of the given exact rep is primitive and pass it further for
saving in structure "allReps" */
{ int rinitpos, rendpos;
int rlength, j, *LP;
rinitpos=checkRep->rep.initpos++;
rendpos=checkRep->rep.endpos++;
rlength=rendpos-rinitpos;
if (lim.min_period>1)
{
/* compute the minimal period */
LP=computeLIx(seq_original+rinitpos, rlength, 1, lim.min_period);
for (j = 1 ; j<lim.min_period ; j++)
{
if ( j+LP[j]>=rlength )
{ free(LP); // throw away the repetition if its minimal period
return(NO); } // is smaller than the user-specified minimal period
}
free(LP);
}
return(YES);
}
void ComputePerBound()
{ int fctIndx, sumFact;
if (numFact<3)
{
printf("Error: number of errors is too big for this sequence\n");
printf(" (or the sequence has too regular structure)\n");
printf(" Run with a smaller number of errors\n");
RECOVER_XML_OUTPUT(output_file,output_file_name,71);
exit(71);
}
oldPerBnd=factorBgns[fctIndx=2];
while(++fctIndx<=numFact)
{ if ( (sumFact=factorBgns[fctIndx]-factorBgns[fctIndx-2])>oldPerBnd )
oldPerBnd=sumFact;
}
}
void print_mrepeat(int rinitpos, int rendpos, int rperiod)
/* check if the period of the given exact rep is primitive and pass it further for printing
to function 'print_rep' (for exe version) or 'printWeb' (for web version)
(both defined in file printOutput.c) */
{
int rlength, j, *LP;
float rexp;
/* compute the minimal period */
rlength=rendpos-rinitpos; // the length of the repeat
if (lim.min_period>1)
{
LP=computeLIx(seq_original+rinitpos, rlength, 1, lim.min_period);
for (j = 1 ; j<lim.min_period ; j++)
{
if ( j+LP[j]>=rlength )
{ free(LP); // throw away the repetition if its minimal period
return; } // is smaller than the user-specified minimal period
}
free(LP);
}
rexp = (float)rlength/rperiod; // the exp of the repeat
if ( (rlength >= lim.min_size) && (rlength <= lim.max_size) // LIMIT for size
&& (rexp >= lim.min_exponent) ) // LIMIT for exponent
//This part is for the exe version of the program. Comment it out for web version.
/* print_rep(rinitpos+1, rendpos, rlength, rperiod, 0); */
print_score(rinitpos+1, rendpos, rlength, rperiod, 0.0);
//This part is for the web version which should be compiled on a SUN machine. Comment it out if used for the exe version.
//printWeb(start_pstn, rinitpos, rendpos, rlength, rperiod)
}