| 1 |
#include "bcftools.pysam.h" |
| 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 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include <stdint.h> |
| 32 |
#include <ctype.h> |
| 33 |
|
| 34 |
#include "str_finder.h" |
| 35 |
#include "utlist.h" |
| 36 |
|
| 37 |
#define MAX(a,b) ((a)>(b)?(a):(b)) |
| 38 |
#define MIN(a,b) ((a)<(b)?(a):(b)) |
| 39 |
|
| 40 |
typedef unsigned char uc; |
| 41 |
|
| 42 |
static void add_rep(rep_ele **list, char *cons, int clen, int pos, int rlen, |
| 43 |
int lower_only, unsigned int w) { |
| 44 |
rep_ele *el, *tmp, *prev; |
| 45 |
char *cp1, *cp2, *cp_end; |
| 46 |
int i; |
| 47 |
|
| 48 |
|
| 49 |
if (*list) { |
| 50 |
tmp = DL_TAIL(*list); |
| 51 |
if (tmp->start <= pos-rlen*2+1 && tmp->end >= pos) |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
cp2 = &cons[pos+1]; |
| 58 |
|
| 59 |
|
| 60 |
for (cp1 = &cons[pos], i = 1; i < rlen; cp1--) |
| 61 |
if (*cp1 == '*') |
| 62 |
continue; |
| 63 |
else |
| 64 |
i++; |
| 65 |
while (*cp1 == '*') |
| 66 |
cp1--; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
cp_end = &cons[clen]; |
| 71 |
while (cp2 < cp_end) { |
| 72 |
if (*cp1 != *cp2) |
| 73 |
break; |
| 74 |
|
| 75 |
w<<=2; |
| 76 |
w|=*cp2; |
| 77 |
cp1++; |
| 78 |
cp2++; |
| 79 |
} |
| 80 |
|
| 81 |
if (!(el = malloc(sizeof(*el)))) |
| 82 |
return; |
| 83 |
|
| 84 |
el->end = pos + cp2-&cons[pos+1]; |
| 85 |
el->rep_len = rlen; |
| 86 |
pos++; |
| 87 |
while (rlen--) { |
| 88 |
while (cons[--pos] == '*'); |
| 89 |
while (cons[--pos] == '*'); |
| 90 |
} |
| 91 |
|
| 92 |
while (pos > 1 && cons[pos-1] == '*') pos--; |
| 93 |
el->start = pos; |
| 94 |
|
| 95 |
|
| 96 |
if (lower_only) { |
| 97 |
int lc = 0; |
| 98 |
for (i = el->start; i <= el->end; i++) { |
| 99 |
if (islower(cons[i])) { |
| 100 |
lc = 1; |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if (!lc) { |
| 106 |
free(el); |
| 107 |
return; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
if (*list) { |
| 113 |
tmp = DL_TAIL(*list); |
| 114 |
do { |
| 115 |
prev = tmp->prev; |
| 116 |
if (tmp->end < el->start) |
| 117 |
break; |
| 118 |
|
| 119 |
if (tmp->start >= el->start) { |
| 120 |
DL_DELETE(*list, tmp); |
| 121 |
free(tmp); |
| 122 |
} |
| 123 |
|
| 124 |
if (tmp == DL_HEAD(*list)) |
| 125 |
break; |
| 126 |
tmp = prev; |
| 127 |
} while (*list); |
| 128 |
} |
| 129 |
|
| 130 |
DL_APPEND(*list, el); |
| 131 |
|
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
rep_ele *find_STR64(char *cons, int len, int lower_only) { |
| 143 |
int i, j; |
| 144 |
uint64_t w = 0; |
| 145 |
rep_ele *reps = NULL; |
| 146 |
|
| 147 |
for (i = j = 0; i < len && j < 26; i++) { |
| 148 |
if (cons[i] == '*') continue; |
| 149 |
|
| 150 |
w <<= 2; |
| 151 |
w |= cons[i]; |
| 152 |
|
| 153 |
if (j>= 1 && (w&0x0003) == ((w>> 2)&0x0003)) |
| 154 |
add_rep(&reps, cons, len, i, 1, lower_only, w); |
| 155 |
if (j>= 3 && (w&0x000f) == ((w>> 4)&0x000f)) |
| 156 |
add_rep(&reps, cons, len, i, 2, lower_only, w); |
| 157 |
if (j>= 5 && (w&0x003f) == ((w>> 6)&0x003f)) |
| 158 |
add_rep(&reps, cons, len, i, 3, lower_only, w); |
| 159 |
if (j>= 7 && (w&0x00ff) == ((w>> 8)&0x00ff)) |
| 160 |
add_rep(&reps, cons, len, i, 4, lower_only, w); |
| 161 |
if (j>= 9 && (w&0x03ff) == ((w>>10)&0x03ff)) |
| 162 |
add_rep(&reps, cons, len, i, 5, lower_only, w); |
| 163 |
if (j>=11 && (w&0x0fff) == ((w>>12)&0x0fff)) |
| 164 |
add_rep(&reps, cons, len, i, 6, lower_only, w); |
| 165 |
if (j>=13 && (w&0x3fff) == ((w>>14)&0x3fff)) |
| 166 |
add_rep(&reps, cons, len, i, 7, lower_only, w); |
| 167 |
if (j>=15 && (w&0xffff) == ((w>>16)&0xffff)) |
| 168 |
add_rep(&reps, cons, len, i, 8, lower_only, w); |
| 169 |
if (j>=17 && (w&0x003ffff) == ((w>>18)&0x003ffff)) |
| 170 |
add_rep(&reps, cons, len, i, 9, lower_only, w); |
| 171 |
if (j>=19 && (w&0x00fffff) == ((w>>20)&0x00fffff)) |
| 172 |
add_rep(&reps, cons, len, i,10, lower_only, w); |
| 173 |
if (j>=21 && (w&0x03fffff) == ((w>>22)&0x03fffff)) |
| 174 |
add_rep(&reps, cons, len, i,11, lower_only, w); |
| 175 |
if (j>=23 && (w&0x0ffffff) == ((w>>24)&0x0ffffff)) |
| 176 |
add_rep(&reps, cons, len, i,12, lower_only, w); |
| 177 |
if (j>=24 && (w&0x3ffffff) == ((w>>26)&0x3ffffff)) |
| 178 |
add_rep(&reps, cons, len, i,13, lower_only, w); |
| 179 |
|
| 180 |
j++; |
| 181 |
} |
| 182 |
|
| 183 |
for (; i < len; i++) { |
| 184 |
if (cons[i] == '*') continue; |
| 185 |
|
| 186 |
w <<= 2; |
| 187 |
w |= cons[i]; |
| 188 |
|
| 189 |
if ((w&0xfffffff) == ((w>>28)&0xfffffff)) |
| 190 |
add_rep(&reps, cons, len, i, 14, lower_only, w); |
| 191 |
else if ((w&0x3ffffff) == ((w>>26)&0x3ffffff)) |
| 192 |
add_rep(&reps, cons, len, i, 13, lower_only, w); |
| 193 |
else if ((w&0x0ffffff) == ((w>>24)&0x0ffffff)) |
| 194 |
add_rep(&reps, cons, len, i, 12, lower_only, w); |
| 195 |
else if ((w&0x03fffff) == ((w>>22)&0x03fffff)) |
| 196 |
add_rep(&reps, cons, len, i, 11, lower_only, w); |
| 197 |
else if ((w&0x00fffff) == ((w>>20)&0x00fffff)) |
| 198 |
add_rep(&reps, cons, len, i, 10, lower_only, w); |
| 199 |
else if ((w&0x003ffff) == ((w>>18)&0x003ffff)) |
| 200 |
add_rep(&reps, cons, len, i, 9, lower_only, w); |
| 201 |
else if ((w&0xffff) == ((w>>16)&0xffff)) |
| 202 |
add_rep(&reps, cons, len, i, 8, lower_only, w); |
| 203 |
else if ((w&0x3fff) == ((w>>14)&0x3fff)) |
| 204 |
add_rep(&reps, cons, len, i, 7, lower_only, w); |
| 205 |
else if ((w&0x0fff) == ((w>>12)&0x0fff)) |
| 206 |
add_rep(&reps, cons, len, i, 6, lower_only, w); |
| 207 |
else if ((w&0x03ff) == ((w>>10)&0x03ff)) |
| 208 |
add_rep(&reps, cons, len, i, 5, lower_only, w); |
| 209 |
else if ((w&0x00ff) == ((w>> 8)&0x00ff)) |
| 210 |
add_rep(&reps, cons, len, i, 4, lower_only, w); |
| 211 |
else if ((w&0x003f) == ((w>> 6)&0x003f)) |
| 212 |
add_rep(&reps, cons, len, i, 3, lower_only, w); |
| 213 |
else if ((w&0x000f) == ((w>> 4)&0x000f)) |
| 214 |
add_rep(&reps, cons, len, i, 2, lower_only, w); |
| 215 |
else if ((w&0x0003) == ((w>> 2)&0x0003)) |
| 216 |
add_rep(&reps, cons, len, i, 1, lower_only, w); |
| 217 |
} |
| 218 |
|
| 219 |
return reps; |
| 220 |
} |
| 221 |
|
| 222 |
rep_ele *find_STR(char *cons, int len, int lower_only) { |
| 223 |
int i, j; |
| 224 |
uint32_t w = 0; |
| 225 |
rep_ele *reps = NULL; |
| 226 |
|
| 227 |
for (i = j = 0; i < len && j < 15; i++) { |
| 228 |
if (cons[i] == '*') continue; |
| 229 |
|
| 230 |
w <<= 2; |
| 231 |
w |= cons[i]; |
| 232 |
|
| 233 |
if (j>= 1 && (w&0x0003) == ((w>> 2)&0x0003)) |
| 234 |
add_rep(&reps, cons, len, i, 1, lower_only, w); |
| 235 |
if (j>= 3 && (w&0x000f) == ((w>> 4)&0x000f)) |
| 236 |
add_rep(&reps, cons, len, i, 2, lower_only, w); |
| 237 |
if (j>= 5 && (w&0x003f) == ((w>> 6)&0x003f)) |
| 238 |
add_rep(&reps, cons, len, i, 3, lower_only, w); |
| 239 |
if (j>= 7 && (w&0x00ff) == ((w>> 8)&0x00ff)) |
| 240 |
add_rep(&reps, cons, len, i, 4, lower_only, w); |
| 241 |
if (j>= 9 && (w&0x03ff) == ((w>>10)&0x03ff)) |
| 242 |
add_rep(&reps, cons, len, i, 5, lower_only, w); |
| 243 |
if (j>=11 && (w&0x0fff) == ((w>>12)&0x0fff)) |
| 244 |
add_rep(&reps, cons, len, i, 6, lower_only, w); |
| 245 |
if (j>=13 && (w&0x3fff) == ((w>>14)&0x3fff)) |
| 246 |
add_rep(&reps, cons, len, i, 7, lower_only, w); |
| 247 |
|
| 248 |
j++; |
| 249 |
} |
| 250 |
|
| 251 |
for (; i < len; i++) { |
| 252 |
if (cons[i] == '*') continue; |
| 253 |
|
| 254 |
w <<= 2; |
| 255 |
w |= cons[i]; |
| 256 |
|
| 257 |
if ((w&0xffff) == ((w>>16)&0xffff)) |
| 258 |
add_rep(&reps, cons, len, i, 8, lower_only, w); |
| 259 |
else if ((w&0x3fff) == ((w>>14)&0x3fff)) |
| 260 |
add_rep(&reps, cons, len, i, 7, lower_only, w); |
| 261 |
else if ((w&0x0fff) == ((w>>12)&0x0fff)) |
| 262 |
add_rep(&reps, cons, len, i, 6, lower_only, w); |
| 263 |
else if ((w&0x03ff) == ((w>>10)&0x03ff)) |
| 264 |
add_rep(&reps, cons, len, i, 5, lower_only, w); |
| 265 |
else if ((w&0x00ff) == ((w>> 8)&0x00ff)) |
| 266 |
add_rep(&reps, cons, len, i, 4, lower_only, w); |
| 267 |
else if ((w&0x003f) == ((w>> 6)&0x003f)) |
| 268 |
add_rep(&reps, cons, len, i, 3, lower_only, w); |
| 269 |
else if ((w&0x000f) == ((w>> 4)&0x000f)) |
| 270 |
add_rep(&reps, cons, len, i, 2, lower_only, w); |
| 271 |
else if ((w&0x0003) == ((w>> 2)&0x0003)) |
| 272 |
add_rep(&reps, cons, len, i, 1, lower_only, w); |
| 273 |
} |
| 274 |
|
| 275 |
return reps; |
| 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 |
char *cons_mark_STR(char *cons, int len, int lower_only) { |
| 321 |
rep_ele *reps, *elt, *tmp; |
| 322 |
char *str; |
| 323 |
|
| 324 |
str = calloc(1, len); |
| 325 |
reps = find_STR(cons, len, lower_only); |
| 326 |
|
| 327 |
DL_FOREACH_SAFE(reps, elt, tmp) { |
| 328 |
int i, v = 0; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
for (i = MAX(elt->start-1,0); i <= MIN(elt->end+1,len-1); i++) |
| 335 |
v |= str[i]; |
| 336 |
|
| 337 |
for (i = 0; i < 8; i++) { |
| 338 |
if (!(v&(1<<i))) |
| 339 |
break; |
| 340 |
} |
| 341 |
v = (i == 8) ? 1 : (1<<i); |
| 342 |
|
| 343 |
|
| 344 |
for (i = elt->start; i <= elt->end; i++) |
| 345 |
str[i] |= v; |
| 346 |
|
| 347 |
DL_DELETE(reps, elt); |
| 348 |
free(elt); |
| 349 |
} |
| 350 |
|
| 351 |
return str; |
| 352 |
} |