X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bam_import.c;h=9d84328bdbdafb5f4e76a19e7edf176121aca5b4;hb=205b696ea778f5b7cfe70e5531bfe6583b5af333;hp=427140db9cbe9e3c80d173b0d69e31b1774c3037;hpb=d0e30eec1158752010659982342a611fc91ae8e3;p=samtools.git diff --git a/bam_import.c b/bam_import.c index 427140d..9d84328 100644 --- a/bam_import.c +++ b/bam_import.c @@ -116,7 +116,7 @@ static bam_header_t *hash2header(const kh_ref_t *hash) bam_header_t *sam_header_read2(const char *fn) { bam_header_t *header; - int c, dret, ret; + int c, dret, ret, error = 0; gzFile fp; kstream_t *ks; kstring_t *str; @@ -135,6 +135,10 @@ bam_header_t *sam_header_read2(const char *fn) ks_getuntil(ks, 0, str, &dret); len = atoi(str->s); k = kh_put(ref, hash, s, &ret); + if (ret == 0) { + fprintf(stderr, "[sam_header_read2] duplicated sequence name: %s\n", s); + error = 1; + } kh_value(hash, k) = (uint64_t)len<<32 | i; if (dret != '\n') while ((c = ks_getc(ks)) != '\n' && c != -1); @@ -143,6 +147,7 @@ bam_header_t *sam_header_read2(const char *fn) gzclose(fp); free(str->s); free(str); fprintf(stderr, "[sam_header_read2] %d sequences loaded.\n", kh_size(hash)); + if (error) return 0; header = hash2header(hash); kh_destroy(ref, hash); return header; @@ -163,44 +168,48 @@ static inline void parse_error(int64_t n_lines, const char * __restrict msg) } static inline void append_text(bam_header_t *header, kstring_t *str) { - int x = header->l_text, y = header->l_text + str->l + 2; // 2 = 1 byte dret + 1 byte null + size_t x = header->l_text, y = header->l_text + str->l + 2; // 2 = 1 byte dret + 1 byte null kroundup32(x); kroundup32(y); - if (x < y) header->text = (char*)realloc(header->text, y); + if (x < y) + { + header->n_text = y; + header->text = (char*)realloc(header->text, y); + if ( !header->text ) + { + fprintf(stderr,"realloc failed to alloc %ld bytes\n", y); + abort(); + } + } + // Sanity check + if ( header->l_text+str->l+1 >= header->n_text ) + { + fprintf(stderr,"append_text FIXME: %ld>=%ld, x=%ld,y=%ld\n", header->l_text+str->l+1,header->n_text,x,y); + abort(); + } strncpy(header->text + header->l_text, str->s, str->l+1); // we cannot use strcpy() here. header->l_text += str->l + 1; header->text[header->l_text] = 0; } -int sam_header_parse_rg(bam_header_t *h) -{ - if (h->dict == 0) h->dict = sam_header_parse2(h->text); - if (h->rg2lib) h->rg2lib = sam_header2tbl(h->dict, "RG", "ID", "LB"); - return sam_tbl_size(h->rg2lib); -} - 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_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); + tmp = sam_header2list(h->dict, "SQ", "SN", &h->n_targets); + if (h->n_targets == 0) return 0; + h->target_name = calloc(h->n_targets, sizeof(void*)); + for (i = 0; i < h->n_targets; ++i) + h->target_name[i] = strdup(tmp[i]); + free(tmp); + tmp = sam_header2list(h->dict, "SQ", "LN", &h->n_targets); + h->target_len = calloc(h->n_targets, 4); 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; }