File: parse_ft.c

package info (click to toggle)
staden 2.0.0%2Bb11-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 21,556 kB
  • sloc: ansic: 240,603; tcl: 65,360; cpp: 12,854; makefile: 11,201; sh: 2,952; fortran: 2,033; perl: 63; awk: 46
file content (655 lines) | stat: -rw-r--r-- 13,409 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "xalloc.h"
#include "parse_ft.h"

/*
 * Parses the feature table (held in the FT lines of an EMBL or Genbank file).
 */

ft_location *new_ft_location(void) {
    ft_location *l;

    l = (ft_location *)xmalloc(sizeof(*l));
    if (!l)
	return NULL;

    l->min = 0;
    l->max = 0;
    l->min_lt = 0;
    l->max_lt = 0;
    l->type = EXACT;

    return l;
}

void del_ft_location(ft_location *l) {
    if (l)
	xfree(l);
}

ft_range *new_ft_range(void) {
    ft_range *r;

    r = (ft_range *)xmalloc(sizeof(*r));
    if (!r)
	return NULL;

    r->left = NULL;
    r->right = NULL;
    r->complemented = 0;
    r->next = NULL;

    return r;
}

void del_ft_range(ft_range *r) {
    if (!r)
	return;

    /* Recursively delete the entire list */
    del_ft_range(r->next);

    if (r->left)
	del_ft_location(r->left);
    if (r->right)
	del_ft_location(r->right);
    xfree(r);
}

ft_entry *new_ft_entry(void) {
    ft_entry *e;
    e = (ft_entry *)xmalloc(sizeof(*e));
    if (!e)
	return NULL;

    e->range = NULL;
    e->qualifiers = NULL;
    e->qual_hash_init = 0;

    return e;
}

void del_ft_entry(ft_entry *e) {
    if (!e)
	return;

    if (e->range)
	del_ft_range(e->range);

    if (e->qualifiers)
	xfree(e->qualifiers);

    del_ft_qual_hash(e);
    
    xfree(e);
}

static void print_loc(ft_location *l) {
    if (l->type != EXACT)
	printf("{");

    if (l->min_lt)
	putchar("< >"[l->min_lt+1]);

    printf("%d", l->min);

    if (l->type == EXACT)
	return;

    printf("%c", (l->type == BASE) ? '.' : '^');

    if (l->max_lt)
	putchar("< >"[l->max_lt+1]);

    printf("%d", l->max);

    printf("}");
}

static void print_range(ft_range *r) {
    printf("RANGE='");
    if (r->complemented)
	printf("complement(");
    if (r->left)
	print_loc(r->left);
    if (r->right) {
	printf("..");
	print_loc(r->right);
    }
    if (r->complemented)
	printf(")");
    printf("'\n");
}

void print_entry(ft_entry *e) {
    ft_range *r;

    puts("\n>>>>>");
    if (!e) {
	printf("NULL entry");
	puts("<<<<<\n");
	return;
    }

    printf("Type='%s'\n", e->type);
    printf("Location='%s'\n", e->location);

    for (r = e->range; r; r = r->next) {
	print_range(r);
    }

    printf("Qualifiers='%s'\n", e->qualifiers);

    puts("<<<<<\n");
}


/*
 * Parses just the location portion of a feature table.
 * Returns 0 for success,
 *        -1 for failure.
 */
static int parse_ft_location(ft_entry *entry, char *s) {
    int start, end;
    ft_location *loc;
    ft_range *range = NULL;
    int complemented = 0;

    enum state_t {
	STATE_TEXT,         /* 0 default state when between ranges */
	STATE_START,        /* 1 The 10 in "10.20" */
	STATE_SINGLE_DOT,   /* 2 The dot in "10.20" */
	STATE_END,          /* 3 The 20 in "10.20" */
	STATE_DOUBLE_DOT,   /* 4 The .. in "10..20"; separates start and end */
	STATE_CARET,	    /* 5 The ^ in "10^20" */
	STATE_EXIT    	    /* 6 Newline or nul char */
    } state;

    start = end = 0;
    state = STATE_TEXT;
    loc = NULL;

    while (state != STATE_EXIT) {
	/* printf("STATE=%d, *s = '%c'\n", state, *s); */

	switch (state) {
	case STATE_TEXT:
	    if (*s == '<' || *s == '>' || isdigit(*s)) {
		ft_range *tmp;

		if (NULL == (tmp = new_ft_range()))
		    goto error;

		if (!entry->range) {
		    entry->range = range = tmp;
		} else {
		    range->next = tmp;
		    range = tmp;
		}

		if (NULL == (loc = new_ft_location()))
		    goto error;
		    
		range->left = loc;
		range->complemented = complemented;
		complemented = 0;
		state = STATE_START;
		continue; /* consume no characters */
	    } else if (*s == 'c') {
		/* only valid 'c' in the location string is in "complement" */
		complemented = 1;
	    }
	    break;

	case STATE_START:
	    if (*s == '<')
		loc->min_lt = -1;
	    else if (*s == '>')
		loc->min_lt = 1;
	    else if (isdigit(*s)) {
		loc->max = loc->min = loc->min * 10 + *s - '0';
	    } else if (*s == ')') {
		/* skip brackets */
		break;
	    } else if (*s == '.') {
		state = STATE_SINGLE_DOT;
	    } else if (*s == '^') {
		state = STATE_CARET;
	    } else {
		state = STATE_TEXT;
	    }
	    break;

	case STATE_SINGLE_DOT:
	    if (*s == '.') {
		state = STATE_DOUBLE_DOT;
	    } else if (*s == '<' || *s == '>' || isdigit(*s)) {
		loc->type = BASE;
		loc->max = 0;
		state = STATE_END;
		continue; /* consume no characters */
	    } else if (*s == '(') {
		/* skip brackets */
		break;
	    } else {
		goto error;
	    }
	    break;

	case STATE_END:
	    if (*s == '<')
		loc->max_lt = -1;
	    else if (*s == '>')
		loc->max_lt = 1;
	    else if (isdigit(*s)) {
		loc->max = loc->max * 10 + *s - '0';
	    } else if (*s == ')') {
		/* skip brackets */
		break;
	    } else if (*s == '.') {
		state = STATE_SINGLE_DOT;
	    } else {
		state = STATE_TEXT;
	    }
	    break;

	case STATE_CARET:
	    loc->type = SITE;
	    if (*s == '<' || *s == '>' || isdigit(*s)) {
		loc->max = 0;
		state = STATE_END;
		continue; /* consume no characters */
	    } else {
		goto error;
	    }
	    break;

	case STATE_DOUBLE_DOT:
	    if (*s == '<' || *s == '>' || isdigit(*s)) {
		if (NULL == (loc = new_ft_location()))
		    goto error;

		range->right = loc;
		state = STATE_START;
		continue; /* consume no characters */
	    } else if (*s == '(') {
		/* skip brackets */
		break;
	    } else {
		state = STATE_TEXT;
	    }
	    break;

	case STATE_EXIT:
	    /* Do nothing, but it removes the warning from gcc -Wswitch */
	    break;
	}

	if (!*s || *s == '/') {
	    state = STATE_EXIT;
	}

	++s;
    }

    return 0;

 error:
    return -1;
}


#if 0
int range_sort_func(const void *v1, const void *v2) {
    ft_range **r1 = (ft_range **)v1;
    ft_range **r2 = (ft_range **)v2;

    /* Sort by left min position */
    if ((*r1)->left && (*r2)->left)
	return (*r1)->left->min - (*r2)->left->min;
    else
	return 0;
}

/*
 * Sorts the location ranges in an FT entry into left to right order if
 * not complemented, and right to left if complemented.
 *
 * Returns 0 for success
 *        -1 for failure
 */
static int sort_ft_location(ft_entry *e) {
    int i, n;
    ft_range **ranges, *rptr, **rptrptr;
    
    /* Count number of ranges */
    for (n = 0, rptr = e->range; rptr; rptr = rptr->next)
	n++;

    /* Create an array of range pointers */
    if (NULL == (ranges = (ft_range **)xmalloc(n * sizeof(ft_range *))))
	return -1;

    for (n = 0, rptr = e->range; rptr; rptr = rptr->next) {
	ranges[n++] = rptr;
    }

    /* Sort the array */
    qsort(ranges, n, sizeof(ft_range *), range_sort_func);

    /* Relink the ranges list */
    if (e->strand == 0) {
	rptrptr = &e->range;
	for (i = 0; i < n; i++) {
	    *rptrptr = ranges[i];
	    rptrptr = &(ranges[i]->next);
	}
	*rptrptr = NULL;
    } else {
	rptrptr = &e->range;
	for (n--; n >= 0; n--) {
	    *rptrptr = ranges[n];
	    rptrptr = &(ranges[n]->next);
	}
	*rptrptr = NULL;
    }

    xfree(ranges);

    return 0;
}
#endif

ft_entry *parse_ft_entry(char *str) {
    ft_entry *entry = NULL;
    int i, j;
    int loc_start;

    if (NULL == (entry = new_ft_entry()))
	goto error;

    /* Parse 1st line to get feature type */
    for (i = 0; str[i] && !isspace(str[i]) && i < 19; i++) {
	entry->type[i] = str[i];
    }
    entry->type[i] = 0;

    /* Extract text copy of location */
    while (isspace(str[i]))
	i++;
    loc_start = i;
    while (str[i] && str[i] != '/')
	i++;

    entry->location = (char *)xmalloc(i - loc_start + 1);
    if (!entry->location)
	goto error;
    strncpy(entry->location, &str[loc_start], i - loc_start);
    entry->location[i-loc_start] = 0;

    /* Process qualifiers */
    if (str[i]) {
	size_t len = strlen(str);

	entry->qualifiers = (char *)xmalloc(len-i+1);
	if (!entry->qualifiers)
	    goto error;

	while (i < len && isspace(str[i]))
	    i++;
	for (j = 0; i < len && str[i];) {
	    entry->qualifiers[j++] = str[i++];
	    if (str[i-1] == '\n') {
		while (i < len && isspace(str[i]))
		    i++;
	    }
	}
	entry->qualifiers[j] = 0;
    }

    /* Hash the qualifiers too */
    init_ft_qual_hash(entry, entry->qualifiers);

    /* Parse location */
    if (-1 == parse_ft_location(entry, str))
	goto error;

#if 0
    /* Sort locations in left to right order (right to left if complemented) */
    sort_ft_location(entry);
#endif

    return entry;

 error:
    if (entry)
	del_ft_entry(entry);

    return NULL;
}

ft_value_element *new_ft_value_element(void) {

    ft_value_element* e;
    e = (ft_value_element*) xmalloc( sizeof(*e) );
    if( e ) {
        e->value = NULL;
        e->next  = NULL;
    }
    return e;
}

void del_ft_value_element_list(ft_value_element *e) {
    if (!e)
	return;

    del_ft_value_element_list(e->next);
    if (e->value)
	xfree(e->value);
    xfree(e);
}

/*
 * Frees memory allocated to store the qualifier hash table.
 */
void del_ft_qual_hash(ft_entry *e) {
    Tcl_HashEntry *hash;
    Tcl_HashSearch s;
	
    if (!e->qual_hash_init)
	return;

    /* Delete the malloced value strings contained in the table */
    for (hash = Tcl_FirstHashEntry(&e->qual_hash, &s);
	 hash;
	 hash = Tcl_NextHashEntry(&s)) {
	del_ft_value_element_list((ft_value_element *)Tcl_GetHashValue(hash));
    }

    /* Delete the table itself */
    Tcl_DeleteHashTable(&e->qual_hash);
    e->qual_hash_init = 0;
}

/*
 * This parses the qualifier information held in 'str' and stores it in a Tcl
 * hash table, which may be accessed via search_ft_qual_hash.
 *
 * Memory allocated in this function is owned by these routines. It needs to be
 * freed using del_ft_qual_hash.
 *
 * Qualifiers value paris are of the formats:
 * /qualifier
 * /qualifier=value
 * /qualifier="value"
 *
 * Quotes may be escaped using "". Eg:
 * /qualifier="a quote ("") character".
 *
 * Quoted values may span multiple lines, but non quoted ones do not.
 *
 * http://www.ebi.ac.uk/embl/Documentation/FT_definitions/feature_table.html
 */
void init_ft_qual_hash(ft_entry *e, char *str) {
    char *cp, *cp_alloc;
    char *qualifier = NULL;
    char *value = NULL;
    int not_done = 1;
    enum state_t {
	STATE_START,		/* start */
	STATE_QUALIFIER,	/* inside a qualifier */
	STATE_EQUALS,		/* found an = */
	STATE_VALUE,		/* inside a value */
	STATE_QVALUE,		/* inside a quoted value */
	STATE_QUOTE,		/* " in value, "" escapes " */
	STATE_STORE		/* at end of 'value'; add to hash table */
    } state;

    if (!str)
	return;

    /* Create hash table */
    Tcl_InitHashTable(&e->qual_hash, TCL_STRING_KEYS);
    e->qual_hash_init = 1;

    cp = cp_alloc = strdup(str); /* We want to write to our copy */
    state = STATE_START;
    while (not_done) {
	/*
	fprintf(stderr, "state=%d *cp=%c not_done=%d\n", state, *cp, not_done);
	*/
	switch(state) {
	case STATE_START:
	    qualifier = NULL;
	    value = NULL;
	    if (*cp == '/') {
		state = STATE_QUALIFIER;
		cp++;
		qualifier = cp;
	    } else if (*cp) {
		cp++;
	    } else {
		not_done = 0;
	    }
	    break;

	case STATE_QUALIFIER:
	    if (*cp == '=') {
		*cp = 0;
		state = STATE_EQUALS;
		cp++;
	    } else if (*cp == '\n' || *cp == '\0') {
		state = STATE_STORE;
	    } else {
		cp++;
	    }
	    break;

	case STATE_EQUALS:
	    if (*cp == '"') {
		state = STATE_QVALUE;
		cp++;
		value = cp;
	    } else if (*cp == '\n' || *cp == '\0') {
		state = STATE_STORE;
	    } else {
		value = cp;
		state = STATE_VALUE;
	    }
	    break;

	case STATE_QVALUE:
	    /* Value extends up to trailing quote' */
	    if (*cp == '\0') {
		state = STATE_STORE;
	    } else if (*cp == '"') {
		state = STATE_QUOTE;
		cp++;
	    } else {
		cp++;
	    }
	    break;

	case STATE_QUOTE:
	    if (*cp == '"') {
		state = STATE_QVALUE;
		/* Not particularly efficient, but it works */
		memmove(cp, cp+1, strlen(cp+1));
	    } else {
		cp--; /* back up one byte to real end of string */
		state = STATE_STORE;
	    }
	    break;

	case STATE_VALUE:
	    /* Value extends up to end of line, quotes are literal */
	    if (*cp == '\n' || *cp == '\0') {
		state = STATE_STORE;
	    } else {
		cp++;
	    }
	    break;

	case STATE_STORE:
	    /* Store the (qualifier,value) pair in the hash table */
	    {
		int new;
		Tcl_HashEntry *hash;
		ft_value_element *ele = new_ft_value_element();
		char tmp;

		tmp = *cp;
		if (*cp) {
		    *cp = 0;
		} else {
		    not_done = 0;
		}

		if (value && *value)
		    ele->value = strdup(value);
		else
		    ele->value = NULL;

		hash = Tcl_CreateHashEntry(&e->qual_hash, qualifier, &new);
		*cp = tmp;

		if (!new) {
		    /* Note, this reverses the order of the linked list */
		    ft_value_element* curr = (ft_value_element*) Tcl_GetHashValue(hash);
		    curr->next = ele;
		    ele = curr;
		}

		Tcl_SetHashValue(hash, (ClientData)ele);
		state = STATE_START;
	    }
	    break;
	}
    }

    xfree(cp_alloc);
}

/*
 * Searches for a qualifier in a hashed qualifier table.
 * Returns the pointer to the value, or NULL if none found. The memory returned
 * is owned by this function and should not be freed by the caller.
 *
 * Values are returned as a linked list, so that if multiple /qualifier copies
 * exist then the values can be found.
 */
ft_value_element *search_ft_qual_hash(ft_entry *e, char *qual) {
    Tcl_HashEntry *hash;

    if (!e->qual_hash_init)
	return NULL;

    if (!(hash = Tcl_FindHashEntry(&e->qual_hash, qual)))
	return NULL;

    return (ft_value_element *)Tcl_GetHashValue(hash);
}