]> git.donarmstrong.com Git - samtools.git/blobdiff - bam.c
* samtools-0.1.7-7 (r584)
[samtools.git] / bam.c
diff --git a/bam.c b/bam.c
index 8b517565026f276d860f91634789ec8fa2f36195..86cf3f3b71f4ecedbb8e80a2033c664cd7ae3b27 100644 (file)
--- a/bam.c
+++ b/bam.c
@@ -70,6 +70,7 @@ bam_header_t *bam_header_read(bamFile fp)
 {
        bam_header_t *header;
        char buf[4];
+       int magic_len;
        int32_t i = 1, name_len;
        // check EOF
        i = bgzf_check_EOF(fp);
@@ -80,9 +81,9 @@ bam_header_t *bam_header_read(bamFile fp)
        }
        else if (i == 0) fprintf(stderr, "[bam_header_read] EOF marker is absent.\n");
        // read "BAM1"
-       if (bam_read(fp, buf, 4) != 4) return 0;
-       if (strncmp(buf, "BAM\001", 4)) {
-               fprintf(stderr, "[bam_header_read] wrong header\n");
+       magic_len = bam_read(fp, buf, 4);
+       if (magic_len != 4 || strncmp(buf, "BAM\001", 4) != 0) {
+               fprintf(stderr, "[bam_header_read] invalid BAM binary header (this is not a BAM file).\n");
                return 0;
        }
        header = bam_header_init();
@@ -140,6 +141,7 @@ int bam_header_write(bamFile fp, const bam_header_t *header)
                        bam_write(fp, &x, 4);
                } else bam_write(fp, &header->target_len[i], 4);
        }
+       bgzf_flush(fp);
        return 0;
 }
 
@@ -207,6 +209,7 @@ inline int bam_write1_core(bamFile fp, const bam1_core_t *c, int data_len, uint8
        x[5] = c->mtid;
        x[6] = c->mpos;
        x[7] = c->isize;
+       bgzf_flush_try(fp, 4 + block_len);
        if (bam_is_be) {
                for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
                y = block_len;
@@ -268,7 +271,7 @@ char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int of)
                ksprintf(&str, "\t%c%c:", key[0], key[1]);
                if (type == 'A') { ksprintf(&str, "A:%c", *s); ++s; }
                else if (type == 'C') { ksprintf(&str, "i:%u", *s); ++s; }
-               else if (type == 'c') { ksprintf(&str, "i:%d", *s); ++s; }
+               else if (type == 'c') { ksprintf(&str, "i:%d", *(int8_t*)s); ++s; }
                else if (type == 'S') { ksprintf(&str, "i:%u", *(uint16_t*)s); s += 2; }
                else if (type == 's') { ksprintf(&str, "i:%d", *(int16_t*)s); s += 2; }
                else if (type == 'I') { ksprintf(&str, "i:%u", *(uint32_t*)s); s += 4; }
@@ -292,9 +295,12 @@ void bam_view1(const bam_header_t *header, const bam1_t *b)
        free(s);
 }
 
-const char *bam_get_library(const bam_header_t *header, const bam1_t *b)
+// FIXME: we should also check the LB tag associated with each alignment
+const char *bam_get_library(bam_header_t *h, const bam1_t *b)
 {
        const uint8_t *rg;
+       if (h->dict == 0) h->dict = sam_header_parse2(h->text);
+       if (h->rg2lib == 0) h->rg2lib = sam_header2tbl(h->dict, "RG", "ID", "LB");
        rg = bam_aux_get(b, "RG");
-       return (rg == 0)? 0 : sam_tbl_get(header->rg2lib, (const char*)(rg + 1));
+       return (rg == 0)? 0 : sam_tbl_get(h->rg2lib, (const char*)(rg + 1));
 }