From: Heng Li Date: Sat, 24 Oct 2009 04:41:50 +0000 (+0000) Subject: * BUGGY VERSION. Please NOT use it. X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=ed9cedc66455cf33331261e0d230c508411b3df6;p=samtools.git * BUGGY VERSION. Please NOT use it. * Fixed a minor bug, but the major bug is still there. --- diff --git a/bam_import.c b/bam_import.c index 427140d..2b4fb7e 100644 --- a/bam_import.c +++ b/bam_import.c @@ -180,27 +180,19 @@ int sam_header_parse_rg(bam_header_t *h) int sam_header_parse(bam_header_t *h) { - void *tbl; char **tmp; int i; free(h->target_len); free(h->target_name); h->n_targets = 0; h->target_len = 0; h->target_name = 0; if (h->l_text < 3) return 0; if (h->dict == 0) h->dict = sam_header_parse2(h->text); - tbl = sam_header2tbl(h->dict, "SQ", "SN", "LN"); - h->n_targets = sam_tbl_size(tbl); - if (h->n_targets == 0) { - sam_tbl_destroy(tbl); - return 0; - } + h->target_name = sam_header2list(h->dict, "SQ", "SN", &h->n_targets); + if (h->n_targets == 0) return 0; + tmp = sam_header2list(h->dict, "SQ", "LN", &h->n_targets); h->target_len = (uint32_t*)calloc(h->n_targets, 4); - h->target_name = (char**)calloc(h->n_targets, sizeof(void*)); - tmp = (char**)calloc(h->n_targets, sizeof(void*)); - sam_tbl_pair(tbl, h->target_name, tmp); for (i = 0; i < h->n_targets; ++i) h->target_len[i] = atoi(tmp[i]); free(tmp); - sam_tbl_destroy(tbl); return h->n_targets; } diff --git a/sam_header.c b/sam_header.c index fa80050..718bd1d 100644 --- a/sam_header.c +++ b/sam_header.c @@ -578,6 +578,43 @@ void *sam_header2tbl(const void *_dict, char type[2], char key_tag[2], char valu return tbl; } +char **sam_header2list(const void *_dict, char type[2], char key_tag[2], int *_n) +{ + const HeaderDict *dict = (const HeaderDict*)_dict; + const list_t *l = dict; + int max, n; + char **ret; + + ret = 0; *_n = max = n = 0; + while (l) + { + HeaderLine *hline = l->data; + if ( hline->type[0]!=type[0] || hline->type[1]!=type[1] ) + { + l = l->next; + continue; + } + + HeaderTag *key; + key = header_line_has_tag(hline,key_tag); + if ( !key ) + { + l = l->next; + continue; + } + + if (n == max) { + max = max? max<<1 : 4; + ret = realloc(ret, max * sizeof(void*)); + } + ret[n++] = key->value; + + l = l->next; + } + *_n = n; + return ret; +} + const char *sam_tbl_get(void *h, const char *key) { khash_t(str) *tbl = (khash_t(str)*)h; @@ -592,21 +629,6 @@ int sam_tbl_size(void *h) return h? kh_size(tbl) : 0; } -int sam_tbl_pair(void *h, char **keys, char **vals) -{ - khash_t(str) *tbl = (khash_t(str)*)h; - int i = 0; - khint_t k; - if (h == 0) return -1; - for (k = kh_begin(tbl); k != kh_end(tbl); ++k) { - if (kh_exist(tbl, k)) { - keys[i] = (char*)kh_key(tbl, k); - vals[i++] = (char*)kh_val(tbl, k); - } - } - return kh_size(tbl); -} - void sam_tbl_destroy(void *h) { khash_t(str) *tbl = (khash_t(str)*)h; diff --git a/sam_header.h b/sam_header.h index 5045616..e5c754f 100644 --- a/sam_header.h +++ b/sam_header.h @@ -10,10 +10,11 @@ extern "C" { void sam_header_free(void *header); char *sam_header_write(const void *headerDict); // returns a newly allocated string + char **sam_header2list(const void *_dict, char type[2], char key_tag[2], int *_n); + void *sam_header2tbl(const void *dict, char type[2], char key_tag[2], char value_tag[2]); const char *sam_tbl_get(void *h, const char *key); int sam_tbl_size(void *h); - int sam_tbl_pair(void *h, char **keys, char **vals); void sam_tbl_destroy(void *h); #ifdef __cplusplus