X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bam.c;h=8b517565026f276d860f91634789ec8fa2f36195;hb=d0e30eec1158752010659982342a611fc91ae8e3;hp=1b74c6d150a8d1003c3775785e7c93e108009b29;hpb=8cc87acf088966330e253d44e67791696d74f35b;p=samtools.git diff --git a/bam.c b/bam.c index 1b74c6d..8b51756 100644 --- a/bam.c +++ b/bam.c @@ -1,36 +1,19 @@ #include #include +#include +#include #include "bam.h" #include "bam_endian.h" +#include "kstring.h" +#include "sam_header.h" int bam_is_be = 0; +char *bam_flag2char_table = "pPuUrR12sfd\0\0\0\0\0"; /************************** * CIGAR related routines * **************************/ -int bam_segreg(int32_t pos, const bam1_core_t *c, const uint32_t *cigar, bam_segreg_t *reg) -{ - unsigned k; - int32_t x = c->pos, y = 0; - int state = 0; - for (k = 0; k < c->n_cigar; ++k) { - int op = cigar[k] & BAM_CIGAR_MASK; // operation - int l = cigar[k] >> BAM_CIGAR_SHIFT; // length - if (state == 0 && (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CINS) && x + l > pos) { - reg->tbeg = x; reg->qbeg = y; reg->cbeg = k; - state = 1; - } - if (op == BAM_CMATCH) { x += l; y += l; } - else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l; - else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l; - if (state == 1 && (op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP || op == BAM_CREF_SKIP || k == c->n_cigar - 1)) { - reg->tend = x; reg->qend = y; reg->cend = k; - } - } - return state? 0 : -1; -} - uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar) { uint32_t k, end; @@ -77,9 +60,9 @@ void bam_header_destroy(bam_header_t *header) free(header->target_len); } free(header->text); -#ifndef BAM_NO_HASH + if (header->dict) sam_header_free(header->dict); + if (header->rg2lib) sam_tbl_destroy(header->rg2lib); bam_destroy_header_hash(header); -#endif free(header); } @@ -87,7 +70,15 @@ bam_header_t *bam_header_read(bamFile fp) { bam_header_t *header; char buf[4]; - int32_t i, name_len; + int32_t i = 1, name_len; + // check EOF + i = bgzf_check_EOF(fp); + if (i < 0) { + // If the file is a pipe, checking the EOF marker will *always* fail + // with ESPIPE. Suppress the error message in this case. + if (errno != ESPIPE) perror("[bam_header_read] bgzf_check_EOF"); + } + 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)) { @@ -233,45 +224,77 @@ int bam_write1(bamFile fp, const bam1_t *b) return bam_write1_core(fp, &b->core, b->data_len, b->data); } -void bam_view1(const bam_header_t *header, const bam1_t *b) +char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int of) { uint8_t *s = bam1_seq(b), *t = bam1_qual(b); int i; const bam1_core_t *c = &b->core; - printf("%s\t%d\t", bam1_qname(b), c->flag); - if (c->tid < 0) printf("*\t"); - else printf("%s\t", header->target_name[c->tid]); - printf("%d\t%d\t", c->pos + 1, c->qual); - if (c->n_cigar == 0) putchar('*'); + kstring_t str; + str.l = str.m = 0; str.s = 0; + + ksprintf(&str, "%s\t", bam1_qname(b)); + if (of == BAM_OFDEC) ksprintf(&str, "%d\t", c->flag); + else if (of == BAM_OFHEX) ksprintf(&str, "0x%x\t", c->flag); + else { // BAM_OFSTR + for (i = 0; i < 16; ++i) + if ((c->flag & 1<tid < 0) kputs("*\t", &str); + else ksprintf(&str, "%s\t", header->target_name[c->tid]); + ksprintf(&str, "%d\t%d\t", c->pos + 1, c->qual); + if (c->n_cigar == 0) kputc('*', &str); else { for (i = 0; i < c->n_cigar; ++i) - printf("%d%c", bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, "MIDNSHP"[bam1_cigar(b)[i]&BAM_CIGAR_MASK]); + ksprintf(&str, "%d%c", bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, "MIDNSHP"[bam1_cigar(b)[i]&BAM_CIGAR_MASK]); } - putchar('\t'); - if (c->mtid < 0) printf("*\t"); - else if (c->mtid == c->tid) printf("=\t"); - else printf("%s\t", header->target_name[c->mtid]); - printf("%d\t%d\t", c->mpos + 1, c->isize); - for (i = 0; i < c->l_qseq; ++i) putchar(bam_nt16_rev_table[bam1_seqi(s, i)]); - putchar('\t'); - if (t[0] == 0xff) putchar('*'); - else for (i = 0; i < c->l_qseq; ++i) putchar(t[i] + 33); + kputc('\t', &str); + if (c->mtid < 0) kputs("*\t", &str); + else if (c->mtid == c->tid) kputs("=\t", &str); + else ksprintf(&str, "%s\t", header->target_name[c->mtid]); + ksprintf(&str, "%d\t%d\t", c->mpos + 1, c->isize); + if (c->l_qseq) { + for (i = 0; i < c->l_qseq; ++i) kputc(bam_nt16_rev_table[bam1_seqi(s, i)], &str); + kputc('\t', &str); + if (t[0] == 0xff) kputc('*', &str); + else for (i = 0; i < c->l_qseq; ++i) kputc(t[i] + 33, &str); + } else ksprintf(&str, "*\t*"); s = bam1_aux(b); while (s < b->data + b->data_len) { uint8_t type, key[2]; key[0] = s[0]; key[1] = s[1]; s += 2; type = *s; ++s; - printf("\t%c%c:", key[0], key[1]); - if (type == 'A') { printf("A:%c", *s); ++s; } - else if (type == 'C') { printf("i:%u", *s); ++s; } - else if (type == 'c') { printf("i:%d", *s); ++s; } - else if (type == 'S') { printf("i:%u", *(uint16_t*)s); s += 2; } - else if (type == 's') { printf("i:%d", *(int16_t*)s); s += 2; } - else if (type == 'I') { printf("i:%u", *(uint32_t*)s); s += 4; } - else if (type == 'i') { printf("i:%d", *(int32_t*)s); s += 4; } - else if (type == 'f') { printf("f:%g", *(float*)s); s += 4; } - else if (type == 'd') { printf("d:%lg", *(double*)s); s += 8; } - else if (type == 'Z' || type == 'H') { printf("%c:", type); while (*s) putchar(*s++); ++s; } + 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 == '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; } + else if (type == 'i') { ksprintf(&str, "i:%d", *(int32_t*)s); s += 4; } + else if (type == 'f') { ksprintf(&str, "f:%g", *(float*)s); s += 4; } + else if (type == 'd') { ksprintf(&str, "d:%lg", *(double*)s); s += 8; } + else if (type == 'Z' || type == 'H') { ksprintf(&str, "%c:", type); while (*s) kputc(*s++, &str); ++s; } } - putchar('\n'); + return str.s; +} + +char *bam_format1(const bam_header_t *header, const bam1_t *b) +{ + return bam_format1_core(header, b, BAM_OFDEC); +} + +void bam_view1(const bam_header_t *header, const bam1_t *b) +{ + char *s = bam_format1(header, b); + printf("%s\n", s); + free(s); +} + +const char *bam_get_library(const bam_header_t *header, const bam1_t *b) +{ + const uint8_t *rg; + rg = bam_aux_get(b, "RG"); + return (rg == 0)? 0 : sam_tbl_get(header->rg2lib, (const char*)(rg + 1)); }