File: filter_words.c

package info (click to toggle)
staden 2.0.0%2Bb11-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,584 kB
  • sloc: ansic: 240,605; tcl: 65,360; cpp: 12,854; makefile: 11,203; sh: 3,023; fortran: 2,033; perl: 63; awk: 46
file content (392 lines) | stat: -rw-r--r-- 8,901 bytes parent folder | download | duplicates (5)
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
391
392
#include <stdio.h>
#include <string.h>
#include "filter_words.h"
#include "dna_utils.h"

#define MATCH 100
#define MISMATCH -100

/*
 *      n
 * A    A
 *  C   C
 * AC   M
 *   G  G
 * A G  R
 *  CG  S
 * ACG  V
 *    T T
 * A  T W
 *  C T Y
 * AC T H
 *   GT K
 * A GT D
 *  CGT B
 * ACGT N
 */

typedef int fkey_t;

/*
 * Construct a key and a mask (allbits = 1) from a sequence.
 * If sequence is longer than Number_of_bits(fkey_t)/4 then the start of the
 * key will be clipped.
 *
 * Seq can contain the standard ambiguity codes.
 */
#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
static fkey_t construct_key(char *seq, fkey_t *mask, int *keylen, int *keyjmp)
{
    fkey_t k = 0, m = 0;
    int seqlen = strlen(seq), i;
    int jumplen = seqlen;
    char seq2[202];

    memcpy(seq2, seq, MIN(100,seqlen));
    memcpy(seq2+seqlen, seq, MIN(100,seqlen));
    for (i = 1; i <= seqlen; i++) {
	if (memcmp(&seq2[i], seq, seqlen) == 0) {
	    jumplen = i;
	    break;
	}
    }

    for (; *seq; seq++) {
	k = (k << 4) | ambiguity2basebit(*seq);	/* key */
	m = (m << 4) | 15;			/* mask */
    }

    if (mask)
	*mask = m;
    if (keyjmp)
	*keyjmp = jumplen;
    if (keylen)
	*keylen = seqlen;

    return k;
}

/*
 * Search for occurences of rep in seq (of length len).
 * minsize is the minimum length of consecutive 'rep' matches to use for
 * reporting.
 */
int filter_words(char *seq, char *filt, size_t len, char *rep,
		 int minsize, int maxdrop, char filter_char) {
    size_t i, j;
    fkey_t word = 0;
    int score = -1, maxscore = 0;
    size_t start = 0, end = 0;
    fkey_t key, keymask;
    int keyjump, keylen;
    int pads = 0;

    key = construct_key(rep, &keymask, &keylen, &keyjump);

    /* Start with an entire word */
    for (i = j = 0; i < len && j < keylen-1; i++) {
	if (seq[i] == '*') {
	    pads++;
	    continue;
	}

	word = ((word << 4) | ambiguity2basebit(seq[i])) & keymask;
	/*
	 * printf("SEQ[%03d]=%c => word=%04x & %04x == %04x, %04x == %04x\n",
	 *       i, seq[i], word, key, word & key,
	 *       ~key, (signed)(word & ~key));
	 */

	j++;
    }

    /* Scan through looking for matches of a word */
    for (; i < len; i++) {
	if (seq[i] == '*'){
	    /* printf("Seq[%03d]='*'\n", i); */
	    pads++;
	    continue;
	}

	word = ((word << 4) | ambiguity2basebit(seq[i])) & keymask;
	/*
	 * printf("seq[%03d]=%c => word=%04x & %04x == %04x, %04x == %04x %c"
	 *       " score %d\n",
	 *       i, seq[i], word, key, word & key,
	 *       ~key, (signed)(word & ~key),
	 *       " M"[(word & key) && !(word & ~key)],
	 *       score);
	 */

	/*
	 * For exact matches with no ambiguity codes this can just be
	 * (word & key) == key.
	 */
	if ((word & key) && !(word & ~key)) {
	    if (score < 0) {
		pads = maxscore = score = 0;
		start = i-(keylen-1);
	    }
	    score += keyjump;
	    if (score >= maxscore) {
		maxscore = score;
		end = i;
	    }
	    for (j = 0; j < keyjump-1;) {
		if (seq[++i] == '*') {
		    /* printf("SEQ[%03d]='*'\n", i); */
		    pads++;
		    continue;
		}
		j++;
		word = ((word << 4) | ambiguity2basebit(seq[i])) & keymask;

		/*
		 * printf("SEQ[%03d]=%c => word=%04x & %04x == %04x, "
		 *        "%04x == %04x\n",
		 *        i, seq[i], word, key, word & key,
		 *        ~key, (signed)(word & ~key));
		 */
	    }
	} else {
	    if (score >= 0) {
		if (--score < 0 || score <= maxscore - maxdrop) {
		    /* printf("start=%d end=%d pads=%d minsize=%d\n",
		       start, end, pads, minsize); */
		    if ((signed)(end - start + 1 -pads) >= minsize) {
			memset(&filt[start], filter_char, end-start+1);
		    }
		    score = -1;
		    pads = 0;
		    maxscore = 0;
		}
	    } else {
		pads = 0;
		score = -1;
	    }
	}
    }

    if (score >= 0 && end-start+1-pads >= minsize) {
	memset(&filt[start], filter_char, end-start+1);
    }
    
    return 0;
}

/*
 * Search for occurences of rep in seq (of length len).
 * minsize is the minimum length of region containing 'rep' and minscore
 * is effectively the portion of minsize that is 'rep'.
 * So minsize==minscore implies 100% match.
 *    minsize==minscore+2 implies 2 bases mismatch allowed.
 */
int filter_words_local(char *seq, char *filt, size_t len, char *rep,
		       int minsize, int minscore, char filter_char) {
    size_t i, j;
    fkey_t word = 0;
    int score = -1, maxscore = 0;
    size_t start = 0, end = 0;
    fkey_t key, keymask;
    int keyjump, keylen;
    int pads = 0;

    key = construct_key(rep, &keymask, &keylen, &keyjump);
    minscore *= MATCH*100; /* precision to 2 decimal points */

    /* Start with an entire word */
    for (i = j = 0; i < len && j < keylen-1; i++) {
	if (seq[i] == '*') {
	    pads++;
	    continue;
	}

	word = ((word << 4) | ambiguity2basebit(seq[i])) & keymask;
	j++;
    }

    /* Scan through looking for matches of a word */
    for (; i < len; i++) {
	if (seq[i] == '*'){
	    pads++;
	    continue;
	}

	word = ((word << 4) | ambiguity2basebit(seq[i])) & keymask;

	/*
	 * For exact matches with no ambiguity codes this can just be
	 * (word & key) == key.
	 */
	if ((word & key) && !(word & ~key)) {
	    if (score < 0) {
		pads = maxscore = score = 0;
		start = i-(keylen-1);
	    }
	    score += keyjump * MATCH;
	    if (score >= maxscore) {
		maxscore = score;
		end = i;
	    }
	    for (j = 0; j < keyjump-1;) {
		if (seq[++i] == '*') {
		    pads++;
		    continue;
		}
		j++;
		word = ((word << 4) | ambiguity2basebit(seq[i])) & keymask;
	    }
	} else {
	    score += (MISMATCH);
	    if (score <= 0 || maxscore-score > minscore) {
		if (end-start+1-pads >= minsize && maxscore >= minscore) {
		    memset(&filt[start], filter_char, end-start+1);
		}
		maxscore = pads = 0;
		score = -1;
	    }
	}
    }

    if (end-start+1-pads >= minsize && maxscore >= minscore) {
	memset(&filt[start], filter_char, end-start+1);
    }

    return 0;
}


/*
 * As per filter_words_local but without allowing for ambiguous bases
 * in the rep string and the rep string is precisely 1 base.
 *
 * This version runs considerably faster due to being able to optimise for
 * the 1-base case.
 */
int filter_words_local1(char *seq, char *filt, size_t len, char *rep,
			int minsize, int minscore, char filter_char) {
    size_t i, j;
    int score = -1, maxscore = 0;
    size_t start = 0, end = 0;
    fkey_t key;
    int pads = 0;

    key = ambiguity2basebit(*rep);
    minscore *= MATCH;

    /* Scan through looking for matches of a word */
    for (i = 0; i < len; i++) {
	if (seq[i] == '*'){
	    pads++;
	    continue;
	}

	if ((ambiguity2basebit(seq[i]) & key)) {
	    score += MATCH;
	    if (score >= maxscore) {
		maxscore = score;
		end = i;
	    }
	} else {
	    score += (MISMATCH);
	    if (score <= 0 || maxscore-score > minscore) {
		if (end-start+1-pads >= minsize && maxscore >= minscore) {
		    memset(&filt[start], filter_char, end-start+1);
		}
		maxscore = pads = 0;
		score = -1;

		/* Skip until we find a match again */
		i++;
		while (i < len && !(ambiguity2basebit(seq[i]) & key))
		    i++;
		maxscore = score = MATCH;
		start = end = i;
		pads = 0;
	    }
	}
    }

    if (end > len) end = len;

    if (end-start+1-pads >= minsize && maxscore >= minscore) {
	memset(&filt[start], filter_char, end-start+1);
    }

    return 0;
}

/* As per filter_words_local, but optimised for 2 character rep strings */
int filter_words_local2(char *seq, char *filt, size_t len, char *rep,
			int minsize, int minscore, char filter_char) {
    size_t i, j;
    fkey_t word = 0;
    int score = -1, maxscore = 0;
    size_t start = 0, end = 0;
    fkey_t key;
    int pads = 0;

    key = (ambiguity2basebit(rep[0])<<4) | ambiguity2basebit(rep[1]);
    minscore *= MATCH;

    /* Start with an entire word */
    for (i = j = 0; i < len && j < 1; i++) {
	if (seq[i] == '*') {
	    pads++;
	    continue;
	}

	word = ((word << 4) | ambiguity2basebit(seq[i])) & 255;
	j++;
    }

    /* Scan through looking for matches of a word */
    for (; i < len; i++) {
	if (seq[i] == '*'){
	    pads++;
	    continue;
	}

	word = ((word << 4) | ambiguity2basebit(seq[i])) & 255;

	/*
	 * For exact matches with no ambiguity codes this can just be
	 * (word & key) == key.
	 */
	if ((word & key) && !(word & ~key)) {
	    if (score < 0) {
		pads = maxscore = score = 0;
		start = i-1;
	    }
	    score += 2 * MATCH;
	    if (score >= maxscore) {
		maxscore = score;
		end = i;
	    }
	    while (seq[++i] == '*') {
		pads++;
		continue;
	    }
	    word = ((word << 4) | ambiguity2basebit(seq[i]));
	} else {
	    score += MISMATCH;
	    if (score > 0 && maxscore-score <= minscore)
		continue;

	    if (end-start+1-pads >= minsize && maxscore >= minscore) {
		memset(&filt[start], filter_char, end-start+1);
	    }
	    maxscore = pads = 0;
	    score = -1;
	}
    }

    if (end-start+1-pads >= minsize && maxscore >= minscore) {
	memset(&filt[start], filter_char, end-start+1);
    }

    return 0;
}