/
sacsc.cc
332 lines (289 loc) · 8.15 KB
/
sacsc.cc
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
/**
CSC: Circular Sequence Comparison
Copyright (C) 2015 Solon P. Pissis, Ahmad Retha, Fatima Vayani
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <assert.h>
#include <sys/time.h>
#include "csc.h"
#include "sacsc.h"
#ifdef _USE_64
#include <divsufsort64.h> // include header for suffix sort
#endif
#ifdef _USE_32
#include <divsufsort.h> // include header for suffix sort
#endif
#include <sdsl/bit_vectors.hpp> // include header for bit vectors
using namespace sdsl;
using namespace std;
unsigned int LCParray ( unsigned char *text, INT n, INT * SA, INT * ISA, INT * LCP )
{
INT i=0, j=0;
LCP[0] = 0;
for ( i = 0; i < n; i++ ) // compute LCP[ISA[i]]
if ( ISA[i] != 0 )
{
if ( i == 0) j = 0;
else j = (LCP[ISA[i-1]] >= 2) ? LCP[ISA[i-1]]-1 : 0;
while ( text[i+j] == text[SA[ISA[i]-1]+j] )
j++;
LCP[ISA[i]] = j;
}
return ( 1 );
}
unsigned int circular_sequence_comparison ( unsigned char * x, unsigned char * y, struct TSwitch sw, unsigned int * rotation, unsigned int * distance )
{
INT * SA;
INT * LCP;
INT * invSA;
INT m = strlen ( ( char * ) x );
INT n = strlen ( ( char * ) y );
INT mmn = m + m + n;
unsigned char * xxy;
xxy = ( unsigned char * ) calloc( ( mmn + 1 ) , sizeof( unsigned char ) );
strcat ( ( char * ) xxy, ( char * ) x );
xxy[m] = '\0';
strcat ( ( char * ) xxy, ( char * ) x );
xxy[m + m] = '\0';
strcat ( ( char * ) xxy, ( char * ) y );
xxy[m + m + n] = '\0';
//fprintf(stderr, " %s.\n", xxy );
/* Compute the suffix array */
SA = ( INT * ) malloc( ( mmn ) * sizeof( INT ) );
if( ( SA == NULL) )
{
fprintf(stderr, " Error: Cannot allocate memory for SA.\n" );
return ( 0 );
}
#ifdef _USE_64
if( divsufsort64( xxy, SA, mmn ) != 0 )
{
fprintf(stderr, " Error: SA computation failed.\n" );
exit( EXIT_FAILURE );
}
#endif
#ifdef _USE_32
if( divsufsort( xxy, SA, mmn ) != 0 )
{
fprintf(stderr, " Error: SA computation failed.\n" );
exit( EXIT_FAILURE );
}
#endif
/* Compute the inverse SA array */
invSA = ( INT * ) calloc( mmn , sizeof( INT ) );
if( ( invSA == NULL) )
{
fprintf(stderr, " Error: Cannot allocate memory for invSA.\n" );
return ( 0 );
}
for ( INT i = 0; i < mmn; i ++ )
{
invSA [SA[i]] = i;
}
LCP = ( INT * ) calloc ( mmn, sizeof( INT ) );
if( ( LCP == NULL) )
{
fprintf(stderr, " Error: Cannot allocate memory for LCP.\n" );
return ( 0 );
}
/* Compute the LCP array */
if( LCParray( xxy, mmn, SA, invSA, LCP ) != 1 )
{
fprintf(stderr, " Error: LCP computation failed.\n" );
exit( EXIT_FAILURE );
}
/* Ranking of q-grams and creation of x' and y' */
int b = (int) ( m / sw . l );
int q = sw . q;
//fprintf(stderr, " %d %d.\n", b, q ); getchar();
int sigma = 0;
INT mm = m + m - q + 1;
INT nn = n - q + 1;
INT * xp; // x'
INT * yp; // y'
xp = ( INT * ) calloc( ( mm ) , sizeof( INT ) );
yp = ( INT * ) calloc( ( nn ) , sizeof( INT ) );
/* Here we rank the first q-gram in the suffix array */
if ( SA[0] >= 0 && SA[0] <= m + m - q ) // i belongs to xx
{
xp[SA[0]] = sigma;
}
if ( SA[0] >= 2*m && SA[0] <= mmn - q ) // i belongs to y
{
yp[SA[0] - 2 * m] = sigma;
}
//fprintf(stderr, " SA[0]: %d.\n", SA[0] );
/* Loop through the LCP array to rank the rest q-grams in the suffix array */
for ( INT i = 1; i < mmn; i++ )
{
INT lcp = LCP[i];
INT ii = SA[i];
//TODO: This could be optimised to ensure that sigma \in [0, m + 1]
if ( ( ii >= 0 && ii <= m + m - q ) || ( ii >= 2 * m && ii <= mmn - q ) )
if ( lcp < q )
{
sigma++;
//fprintf(stderr, " SA[i]: %d.\n", SA[i] );
}
if ( ii >= 0 && ii <= m + m - q ) // i belongs to xx
{
xp[ii] = sigma;
}
if ( ii >= 2 * m && ii <= mmn - q ) // i belongs to y
{
yp[ii - 2 * m] = sigma;
}
}
//fprintf(stderr, " sigma: %d.\n", sigma );
#if 0
for ( INT i = 0; i < mm; i++ )
{
fprintf ( stderr, "%d ", xp[i] );
}
fprintf ( stderr, "\n" );
for ( INT i = 0; i < nn; i++ )
{
fprintf ( stderr, "%d ", yp[i] );
}
fprintf ( stderr, "\n" );
#endif
/* Partitioning x' and y' as evenly as possible */
INT * xind; //this is the starting position of the fragment
INT * xmf; //this is the number of q-grams in the fragment
xind = ( INT * ) calloc ( b, sizeof ( INT ) );
xmf = ( INT * ) calloc ( b, sizeof ( INT ) );
for ( INT j = 0; j < b; j++ ) partitioning ( 0, j, b, m - q + 1, xmf, xind );
INT * yind; //this is the starting position of the fragment
INT * ymf; //this is the number of q-grams in the fragment
yind = ( INT * ) calloc ( b, sizeof ( INT ) );
ymf = ( INT * ) calloc ( b, sizeof ( INT ) );
for ( INT j = 0; j < b; j++ ) partitioning ( 0, j, b, nn, ymf, yind );
#if 0
for ( INT i = 0; i < b; i++ )
{
fprintf ( stderr, "(%d %d) ", xind[i], xmf[i] );
}
fprintf ( stderr, "\n" );
#endif
/* Allocate the diff vector */
INT ** diff;
diff = ( INT ** ) calloc ( b, sizeof ( INT * ) );
for ( INT i = 0; i < b; i++ ) diff[i] = ( INT * ) calloc ( sigma + 1, sizeof ( INT ) );
/* Step 1: Create diff, pvy, and D_0 */
INT * D;
D = ( INT * ) calloc ( b, sizeof ( INT * ) );
for ( INT i = 0; i < b; i++ )
{
for ( INT j = yind[i]; j < yind[i] + ymf[i]; j++ )
{
diff[i][yp[j]]++;
D[i]++;
}
}
//fprintf ( stderr, "D0 = %d\n", D[0] ); getchar();
/* Step 2: Compute the distances for position 0 */
int min_dist = 0;
for ( INT i = 0; i < b; i++ ) //first window
{
for ( INT j = xind[i]; j < xind[i] + xmf[i]; j++ )
{
diff[i][xp[j]]--;
if ( diff[i][xp[j]] >= 0 )
{
D[i]--;
}
else
{
D[i]++;
}
}
min_dist += D[i];
}
//fprintf ( stderr, "D0 = %d\n", D[0] ); getchar();
/* Step 3: Compute the rest of the distances */
int rot = 0;
for ( INT i = 1; i < m; i++ ) //all the rest windows
{
int dist = 0;
for ( INT j = 0; j < b; j++ )
{
diff[j][xp[i - 1 + xind[j]]]++; //letter out
diff[j][xp[i - 1 + xind[j] + xmf[j]]]--; //letter in
//For the letter we take out
if ( diff[j][xp[i - 1 + xind[j]]] <= 0 )
{
D[j]--;
}
else
{
D[j]++;
}
//For the letter we add in
if ( diff[j][xp[i - 1 + xind[j] + xmf[j]]] < 0 )
{
D[j]++;
}
else
{
D[j]--;
}
dist += D[j];
}
//fprintf ( stderr, "dist = %d\n", dist );
if ( dist < min_dist )
{
rot = i;
min_dist = dist;
}
}
( * distance ) = ( unsigned int ) min_dist;
( * rotation ) = ( unsigned int ) rot;
/* De-allocate the memory */
free ( D );
free ( xp );
free ( yp );
free ( xind );
free ( xmf );
free ( yind );
free ( ymf );
free ( xxy );
free ( invSA );
free ( SA );
free ( LCP );
for ( INT i = 0; i < b; i++ ) free ( diff[i] );
free ( diff );
return ( 1 );
}
void partitioning ( INT i, INT j, INT f, INT m, INT * mf, INT * ind )
{
INT modulo = m % f;
double nf = ( double ) ( m ) / f;
INT first;
INT last;
if ( j < modulo )
{
first = j * ( ceil( nf ) );
last = first + ceil( nf ) - 1;
}
else
{
first = j * ( floor( nf ) ) + modulo;
last = first + floor( nf ) - 1;
}
ind[j + i * f] = first;
mf[j + i * f] = last - first + 1;
}