1
#include "sam_header.h"
9
KHASH_MAP_INIT_STR(str, const char *)
13
struct _HeaderList *next;
16
typedef struct _HeaderList list_t;
17
typedef list_t HeaderDict;
33
const char *o_hd_tags[] = {"SO","GO",NULL};
34
const char *r_hd_tags[] = {"VN",NULL};
36
const char *o_sq_tags[] = {"AS","M5","UR","SP",NULL};
37
const char *r_sq_tags[] = {"SN","LN",NULL};
38
const char *u_sq_tags[] = {"SN",NULL};
40
const char *o_rg_tags[] = {"LB","DS","PU","PI","CN","DT","PL",NULL};
41
const char *r_rg_tags[] = {"ID",NULL};
42
const char *u_rg_tags[] = {"ID",NULL};
44
const char *o_pg_tags[] = {"VN","CL",NULL};
45
const char *r_pg_tags[] = {"ID",NULL};
47
const char *types[] = {"HD","SQ","RG","PG","CO",NULL};
48
const char **optional_tags[] = {o_hd_tags,o_sq_tags,o_rg_tags,o_pg_tags,NULL,NULL};
49
const char **required_tags[] = {r_hd_tags,r_sq_tags,r_rg_tags,r_pg_tags,NULL,NULL};
50
const char **unique_tags[] = {NULL, u_sq_tags,u_rg_tags,NULL,NULL,NULL};
53
static void debug(const char *format, ...)
57
vfprintf(stderr, format, ap);
61
static list_t *list_append(list_t *root, void *data)
68
l->next = malloc(sizeof(list_t));
73
l = malloc(sizeof(list_t));
81
static void list_free(list_t *root)
94
// Look for a tag "XY" in a predefined const char *[] array.
95
static int tag_exists(const char *tag, const char **tags)
98
if ( !tags ) return -1;
101
if ( tags[itag][0]==tag[0] && tags[itag][1]==tag[1] ) return itag;
109
// Mimics the behaviour of getline, except it returns pointer to the next chunk of the text
110
// or NULL if everything has been read. The lineptr should be freed by the caller. The
111
// newline character is stripped.
112
static const char *nextline(char **lineptr, size_t *n, const char *text)
115
const char *to = text;
117
if ( !*to ) return NULL;
119
while ( *to && *to!='\n' && *to!='\r' ) to++;
124
// Advance the pointer for the next call
125
if ( *to=='\n' ) to++;
126
else if ( *to=='\r' && *(to+1)=='\n' ) to+=2;
133
*lineptr = malloc(len);
138
*lineptr = realloc(*lineptr, len);
142
debug("[nextline] Insufficient memory!\n");
146
memcpy(*lineptr,text,len);
147
(*lineptr)[len-1] = 0;
152
// name points to "XY", value_from points to the first character of the value string and
153
// value_to points to the last character of the value string.
154
static HeaderTag *new_tag(const char *name, const char *value_from, const char *value_to)
156
HeaderTag *tag = malloc(sizeof(HeaderTag));
157
int len = value_to-value_from+1;
159
tag->key[0] = name[0];
160
tag->key[1] = name[1];
161
tag->value = malloc(len+1);
162
memcpy(tag->value,value_from,len+1);
167
static HeaderTag *header_line_has_tag(HeaderLine *hline, const char *key)
169
list_t *tags = hline->tags;
172
HeaderTag *tag = tags->data;
173
if ( tag->key[0]==key[0] && tag->key[1]==key[1] ) return tag;
181
// 0 .. different types or unique tags differ or conflicting tags, cannot be merged
182
// 1 .. all tags identical -> no need to merge, drop one
183
// 2 .. the unique tags match and there are some conflicting tags (same tag, different value) -> error, cannot be merged nor duplicated
184
// 3 .. there are some missing complementary tags and no unique conflict -> can be merged into a single line
185
static int sam_header_compare_lines(HeaderLine *hline1, HeaderLine *hline2)
189
if ( hline1->type[0]!=hline2->type[0] || hline1->type[1]!=hline2->type[1] )
192
int itype = tag_exists(hline1->type,types);
194
debug("[sam_header_compare_lines] Unknown type [%c%c]\n", hline1->type[0],hline1->type[1]);
195
return -1; // FIXME (lh3): error; I do not know how this will be handled in Petr's code
198
if ( unique_tags[itype] )
200
t1 = header_line_has_tag(hline1,unique_tags[itype][0]);
201
t2 = header_line_has_tag(hline2,unique_tags[itype][0]);
202
if ( !t1 || !t2 ) // this should never happen, the unique tags are required
205
if ( strcmp(t1->value,t2->value) )
206
return 0; // the unique tags differ, cannot be merged
208
if ( !required_tags[itype] && !optional_tags[itype] )
210
t1 = hline1->tags->data;
211
t2 = hline2->tags->data;
212
if ( !strcmp(t1->value,t2->value) ) return 1; // identical comments
216
int missing=0, itag=0;
217
while ( required_tags[itype] && required_tags[itype][itag] )
219
t1 = header_line_has_tag(hline1,required_tags[itype][itag]);
220
t2 = header_line_has_tag(hline2,required_tags[itype][itag]);
222
return 2; // this should never happen
223
else if ( !t1 || !t2 )
224
missing = 1; // there is some tag missing in one of the hlines
225
else if ( strcmp(t1->value,t2->value) )
227
if ( unique_tags[itype] )
228
return 2; // the lines have a matching unique tag but have a conflicting tag
230
return 0; // the lines contain conflicting tags, cannot be merged
235
while ( optional_tags[itype] && optional_tags[itype][itag] )
237
t1 = header_line_has_tag(hline1,optional_tags[itype][itag]);
238
t2 = header_line_has_tag(hline2,optional_tags[itype][itag]);
245
missing = 1; // there is some tag missing in one of the hlines
246
else if ( strcmp(t1->value,t2->value) )
248
if ( unique_tags[itype] )
249
return 2; // the lines have a matching unique tag but have a conflicting tag
251
return 0; // the lines contain conflicting tags, cannot be merged
255
if ( missing ) return 3; // there are some missing complementary tags with no conflicts, can be merged
260
static HeaderLine *sam_header_line_clone(const HeaderLine *hline)
263
HeaderLine *out = malloc(sizeof(HeaderLine));
264
out->type[0] = hline->type[0];
265
out->type[1] = hline->type[1];
271
HeaderTag *old = tags->data;
273
HeaderTag *new = malloc(sizeof(HeaderTag));
274
new->key[0] = old->key[0];
275
new->key[1] = old->key[1];
276
new->value = strdup(old->value);
277
out->tags = list_append(out->tags, new);
284
static int sam_header_line_merge_with(HeaderLine *out_hline, const HeaderLine *tmpl_hline)
288
if ( out_hline->type[0]!=tmpl_hline->type[0] || out_hline->type[1]!=tmpl_hline->type[1] )
291
tmpl_tags = tmpl_hline->tags;
294
HeaderTag *tmpl_tag = tmpl_tags->data;
295
HeaderTag *out_tag = header_line_has_tag(out_hline, tmpl_tag->key);
298
HeaderTag *tag = malloc(sizeof(HeaderTag));
299
tag->key[0] = tmpl_tag->key[0];
300
tag->key[1] = tmpl_tag->key[1];
301
tag->value = strdup(tmpl_tag->value);
302
out_hline->tags = list_append(out_hline->tags,tag);
304
tmpl_tags = tmpl_tags->next;
310
static HeaderLine *sam_header_line_parse(const char *headerLine)
314
const char *from, *to;
317
if ( *from != '@' ) {
318
debug("[sam_header_line_parse] expected '@', got [%s]\n", headerLine);
323
while (*to && *to!='\t') to++;
324
if ( to-from != 2 ) {
325
debug("[sam_header_line_parse] expected '@XY', got [%s]\n", headerLine);
329
hline = malloc(sizeof(HeaderLine));
330
hline->type[0] = from[0];
331
hline->type[1] = from[1];
334
int itype = tag_exists(hline->type, types);
337
while (*to && *to=='\t') to++;
338
if ( to-from != 1 ) {
339
debug("[sam_header_line_parse] multiple tabs on line [%s] (%d)\n", headerLine,(int)(to-from));
345
while (*to && *to!='\t') to++;
347
if ( !required_tags[itype] && !optional_tags[itype] )
348
tag = new_tag(" ",from,to-1);
350
tag = new_tag(from,from+3,to-1);
352
if ( header_line_has_tag(hline,tag->key) )
353
debug("The tag '%c%c' present (at least) twice on line [%s]\n", tag->key[0],tag->key[1], headerLine);
354
hline->tags = list_append(hline->tags, tag);
357
while (*to && *to=='\t') to++;
358
if ( *to && to-from != 1 ) {
359
debug("[sam_header_line_parse] multiple tabs on line [%s] (%d)\n", headerLine,(int)(to-from));
369
// Must be of an existing type, all tags must be recognised and all required tags must be present
370
static int sam_header_line_validate(HeaderLine *hline)
376
// Is the type correct?
377
itype = tag_exists(hline->type, types);
380
debug("The type [%c%c] not recognised.\n", hline->type[0],hline->type[1]);
384
// Has all required tags?
386
while ( required_tags[itype] && required_tags[itype][itag] )
388
if ( !header_line_has_tag(hline,required_tags[itype][itag]) )
390
debug("The tag [%c%c] required for [%c%c] not present.\n", required_tags[itype][itag][0],required_tags[itype][itag][1],
391
hline->type[0],hline->type[1]);
397
// Are all tags recognised?
402
if ( !tag_exists(tag->key,required_tags[itype]) && !tag_exists(tag->key,optional_tags[itype]) )
404
debug("Unknown tag [%c%c] for [%c%c].\n", tag->key[0],tag->key[1], hline->type[0],hline->type[1]);
414
static void print_header_line(FILE *fp, HeaderLine *hline)
416
list_t *tags = hline->tags;
419
fprintf(fp, "@%c%c", hline->type[0],hline->type[1]);
425
if ( tag->key[0]!=' ' || tag->key[1]!=' ' )
426
fprintf(fp, "%c%c:", tag->key[0],tag->key[1]);
427
fprintf(fp, "%s", tag->value);
435
static void sam_header_line_free(HeaderLine *hline)
437
list_t *tags = hline->tags;
440
HeaderTag *tag = tags->data;
445
list_free(hline->tags);
449
void sam_header_free(void *_header)
451
HeaderDict *header = (HeaderDict*)_header;
452
list_t *hlines = header;
455
sam_header_line_free(hlines->data);
456
hlines = hlines->next;
461
HeaderDict *sam_header_clone(const HeaderDict *dict)
463
HeaderDict *out = NULL;
466
HeaderLine *hline = dict->data;
467
out = list_append(out, sam_header_line_clone(hline));
473
// Returns a newly allocated string
474
char *sam_header_write(const void *_header)
476
const HeaderDict *header = (const HeaderDict*)_header;
479
const list_t *hlines;
481
// Calculate the length of the string to allocate
485
len += 4; // @XY and \n
487
HeaderLine *hline = hlines->data;
488
list_t *tags = hline->tags;
491
HeaderTag *tag = tags->data;
492
len += strlen(tag->value) + 1; // \t
493
if ( tag->key[0]!=' ' || tag->key[1]!=' ' )
494
len += strlen(tag->value) + 3; // XY:
497
hlines = hlines->next;
505
HeaderLine *hline = hlines->data;
507
nout += sprintf(out+nout,"@%c%c",hline->type[0],hline->type[1]);
509
list_t *tags = hline->tags;
512
HeaderTag *tag = tags->data;
513
nout += sprintf(out+nout,"\t");
514
if ( tag->key[0]!=' ' || tag->key[1]!=' ' )
515
nout += sprintf(out+nout,"%c%c:", tag->key[0],tag->key[1]);
516
nout += sprintf(out+nout,"%s", tag->value);
519
hlines = hlines->next;
520
nout += sprintf(out+nout,"\n");
526
void *sam_header_parse2(const char *headerText)
528
list_t *hlines = NULL;
538
while ( (text=nextline(&buf, &nbuf, text)) )
540
hline = sam_header_line_parse(buf);
541
if ( hline && sam_header_line_validate(hline) )
542
hlines = list_append(hlines, hline);
545
if (hline) sam_header_line_free(hline);
546
sam_header_free(hlines);
547
if ( buf ) free(buf);
551
if ( buf ) free(buf);
556
void *sam_header2tbl(const void *_dict, char type[2], char key_tag[2], char value_tag[2])
558
const HeaderDict *dict = (const HeaderDict*)_dict;
559
const list_t *l = dict;
560
khash_t(str) *tbl = kh_init(str);
564
if (_dict == 0) return tbl; // return an empty (not null) hash table
567
HeaderLine *hline = l->data;
568
if ( hline->type[0]!=type[0] || hline->type[1]!=type[1] )
574
HeaderTag *key, *value;
575
key = header_line_has_tag(hline,key_tag);
576
value = header_line_has_tag(hline,value_tag);
577
if ( !key || !value )
583
k = kh_get(str, tbl, key->value);
584
if ( k != kh_end(tbl) )
585
debug("[sam_header_lookup_table] They key %s not unique.\n", key->value);
586
k = kh_put(str, tbl, key->value, &ret);
587
kh_value(tbl, k) = value->value;
594
char **sam_header2list(const void *_dict, char type[2], char key_tag[2], int *_n)
596
const HeaderDict *dict = (const HeaderDict*)_dict;
597
const list_t *l = dict;
601
ret = 0; *_n = max = n = 0;
604
HeaderLine *hline = l->data;
605
if ( hline->type[0]!=type[0] || hline->type[1]!=type[1] )
612
key = header_line_has_tag(hline,key_tag);
620
max = max? max<<1 : 4;
621
ret = realloc(ret, max * sizeof(void*));
623
ret[n++] = key->value;
631
const char *sam_tbl_get(void *h, const char *key)
633
khash_t(str) *tbl = (khash_t(str)*)h;
635
k = kh_get(str, tbl, key);
636
return k == kh_end(tbl)? 0 : kh_val(tbl, k);
639
int sam_tbl_size(void *h)
641
khash_t(str) *tbl = (khash_t(str)*)h;
642
return h? kh_size(tbl) : 0;
645
void sam_tbl_destroy(void *h)
647
khash_t(str) *tbl = (khash_t(str)*)h;
648
kh_destroy(str, tbl);
651
void *sam_header_merge(int n, const void **_dicts)
653
const HeaderDict **dicts = (const HeaderDict**)_dicts;
654
HeaderDict *out_dict;
657
if ( n<2 ) return NULL;
659
out_dict = sam_header_clone(dicts[0]);
661
for (idict=1; idict<n; idict++)
663
const list_t *tmpl_hlines = dicts[idict];
665
while ( tmpl_hlines )
667
list_t *out_hlines = out_dict;
671
status = sam_header_compare_lines(tmpl_hlines->data, out_hlines->data);
674
out_hlines = out_hlines->next;
680
print_header_line(stderr,tmpl_hlines->data);
681
print_header_line(stderr,out_hlines->data);
682
debug("Conflicting lines, cannot merge the headers.\n");
686
sam_header_line_merge_with(out_hlines->data, tmpl_hlines->data);
692
out_dict = list_append(out_dict, sam_header_line_clone(tmpl_hlines->data));
694
tmpl_hlines = tmpl_hlines->next;