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] )
349 // CO is a special case, it can contain anything, including tabs
350 if ( *to ) { to++; continue; }
351 tag = new_tag(" ",from,to-1);
354 tag = new_tag(from,from+3,to-1);
356 if ( header_line_has_tag(hline,tag->key) )
357 debug("The tag '%c%c' present (at least) twice on line [%s]\n", tag->key[0],tag->key[1], headerLine);
358 hline->tags = list_append(hline->tags, tag);
361 while (*to && *to=='\t') to++;
362 if ( *to && to-from != 1 ) {
363 debug("[sam_header_line_parse] multiple tabs on line [%s] (%d)\n", headerLine,(int)(to-from));
373 // Must be of an existing type, all tags must be recognised and all required tags must be present
374 static int sam_header_line_validate(HeaderLine *hline)
380 // Is the type correct?
381 itype = tag_exists(hline->type, types);
384 debug("The type [%c%c] not recognised.\n", hline->type[0],hline->type[1]);
388 // Has all required tags?
390 while ( required_tags[itype] && required_tags[itype][itag] )
392 if ( !header_line_has_tag(hline,required_tags[itype][itag]) )
394 debug("The tag [%c%c] required for [%c%c] not present.\n", required_tags[itype][itag][0],required_tags[itype][itag][1],
395 hline->type[0],hline->type[1]);
401 // Are all tags recognised?
406 if ( !tag_exists(tag->key,required_tags[itype]) && !tag_exists(tag->key,optional_tags[itype]) )
408 debug("Unknown tag [%c%c] for [%c%c].\n", tag->key[0],tag->key[1], hline->type[0],hline->type[1]);
418 static void print_header_line(FILE *fp, HeaderLine *hline)
420 list_t *tags = hline->tags;
423 fprintf(fp, "@%c%c", hline->type[0],hline->type[1]);
429 if ( tag->key[0]!=' ' || tag->key[1]!=' ' )
430 fprintf(fp, "%c%c:", tag->key[0],tag->key[1]);
431 fprintf(fp, "%s", tag->value);
439 static void sam_header_line_free(HeaderLine *hline)
441 list_t *tags = hline->tags;
444 HeaderTag *tag = tags->data;
449 list_free(hline->tags);
453 void sam_header_free(void *_header)
455 HeaderDict *header = (HeaderDict*)_header;
456 list_t *hlines = header;
459 sam_header_line_free(hlines->data);
460 hlines = hlines->next;
465 HeaderDict *sam_header_clone(const HeaderDict *dict)
467 HeaderDict *out = NULL;
470 HeaderLine *hline = dict->data;
471 out = list_append(out, sam_header_line_clone(hline));
477 // Returns a newly allocated string
478 char *sam_header_write(const void *_header)
480 const HeaderDict *header = (const HeaderDict*)_header;
483 const list_t *hlines;
485 // Calculate the length of the string to allocate
489 len += 4; // @XY and \n
491 HeaderLine *hline = hlines->data;
492 list_t *tags = hline->tags;
495 HeaderTag *tag = tags->data;
496 len += strlen(tag->value) + 1; // \t
497 if ( tag->key[0]!=' ' || tag->key[1]!=' ' )
498 len += strlen(tag->value) + 3; // XY:
501 hlines = hlines->next;
509 HeaderLine *hline = hlines->data;
511 nout += sprintf(out+nout,"@%c%c",hline->type[0],hline->type[1]);
513 list_t *tags = hline->tags;
516 HeaderTag *tag = tags->data;
517 nout += sprintf(out+nout,"\t");
518 if ( tag->key[0]!=' ' || tag->key[1]!=' ' )
519 nout += sprintf(out+nout,"%c%c:", tag->key[0],tag->key[1]);
520 nout += sprintf(out+nout,"%s", tag->value);
523 hlines = hlines->next;
524 nout += sprintf(out+nout,"\n");
530 void *sam_header_parse2(const char *headerText)
532 list_t *hlines = NULL;
542 while ( (text=nextline(&buf, &nbuf, text)) )
544 hline = sam_header_line_parse(buf);
545 if ( hline && sam_header_line_validate(hline) )
546 hlines = list_append(hlines, hline);
549 if (hline) sam_header_line_free(hline);
550 sam_header_free(hlines);
551 if ( buf ) free(buf);
555 if ( buf ) free(buf);
560 void *sam_header2tbl(const void *_dict, char type[2], char key_tag[2], char value_tag[2])
562 const HeaderDict *dict = (const HeaderDict*)_dict;
563 const list_t *l = dict;
564 khash_t(str) *tbl = kh_init(str);
568 if (_dict == 0) return tbl; // return an empty (not null) hash table
571 HeaderLine *hline = l->data;
572 if ( hline->type[0]!=type[0] || hline->type[1]!=type[1] )
578 HeaderTag *key, *value;
579 key = header_line_has_tag(hline,key_tag);
580 value = header_line_has_tag(hline,value_tag);
581 if ( !key || !value )
587 k = kh_get(str, tbl, key->value);
588 if ( k != kh_end(tbl) )
589 debug("[sam_header_lookup_table] They key %s not unique.\n", key->value);
590 k = kh_put(str, tbl, key->value, &ret);
591 kh_value(tbl, k) = value->value;
598 char **sam_header2list(const void *_dict, char type[2], char key_tag[2], int *_n)
600 const HeaderDict *dict = (const HeaderDict*)_dict;
601 const list_t *l = dict;
605 ret = 0; *_n = max = n = 0;
608 HeaderLine *hline = l->data;
609 if ( hline->type[0]!=type[0] || hline->type[1]!=type[1] )
616 key = header_line_has_tag(hline,key_tag);
624 max = max? max<<1 : 4;
625 ret = realloc(ret, max * sizeof(void*));
627 ret[n++] = key->value;
635 const char *sam_tbl_get(void *h, const char *key)
637 khash_t(str) *tbl = (khash_t(str)*)h;
639 k = kh_get(str, tbl, key);
640 return k == kh_end(tbl)? 0 : kh_val(tbl, k);
643 int sam_tbl_size(void *h)
645 khash_t(str) *tbl = (khash_t(str)*)h;
646 return h? kh_size(tbl) : 0;
649 void sam_tbl_destroy(void *h)
651 khash_t(str) *tbl = (khash_t(str)*)h;
652 kh_destroy(str, tbl);
655 void *sam_header_merge(int n, const void **_dicts)
657 const HeaderDict **dicts = (const HeaderDict**)_dicts;
658 HeaderDict *out_dict;
661 if ( n<2 ) return NULL;
663 out_dict = sam_header_clone(dicts[0]);
665 for (idict=1; idict<n; idict++)
667 const list_t *tmpl_hlines = dicts[idict];
669 while ( tmpl_hlines )
671 list_t *out_hlines = out_dict;
675 status = sam_header_compare_lines(tmpl_hlines->data, out_hlines->data);
678 out_hlines = out_hlines->next;
684 print_header_line(stderr,tmpl_hlines->data);
685 print_header_line(stderr,out_hlines->data);
686 debug("Conflicting lines, cannot merge the headers.\n");
690 sam_header_line_merge_with(out_hlines->data, tmpl_hlines->data);
696 out_dict = list_append(out_dict, sam_header_line_clone(tmpl_hlines->data));
698 tmpl_hlines = tmpl_hlines->next;