]> git.donarmstrong.com Git - samtools.git/commitdiff
* BUGGY VERSION. Please NOT use it.
authorHeng Li <lh3@live.co.uk>
Sat, 24 Oct 2009 04:41:50 +0000 (04:41 +0000)
committerHeng Li <lh3@live.co.uk>
Sat, 24 Oct 2009 04:41:50 +0000 (04:41 +0000)
 * Fixed a minor bug, but the major bug is still there.

bam_import.c
sam_header.c
sam_header.h

index 427140db9cbe9e3c80d173b0d69e31b1774c3037..2b4fb7e03f956e6ea35a2216c5c853c414fa0aa4 100644 (file)
@@ -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;
 }
 
index fa8005079b7f5243f13b13ed5cd3c051815ef645..718bd1d8a4a2175fcbe3dc245aee00a868364d3c 100644 (file)
@@ -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;
index 50456168a638f4a41db86b38b001ed648805f8cc..e5c754f3649ce6a13e6f5cb3ca93275d13bb67fe 100644 (file)
@@ -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