X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bam_aux.c;h=fbcd9822b233dab64f6c861fb332846acb951ce7;hb=6ee6fd65332260af63fbfbf0a625ea3847371eda;hp=d6f18ea281712a7d89007d7d87291cbf13718c1b;hpb=63e4e54189bc9e7f43852edf865f95e713b93dc4;p=samtools.git diff --git a/bam_aux.c b/bam_aux.c index d6f18ea..fbcd982 100644 --- a/bam_aux.c +++ b/bam_aux.c @@ -1,7 +1,9 @@ #include #include "bam.h" #include "khash.h" +typedef char *str_p; KHASH_MAP_INIT_STR(s, int) +KHASH_MAP_INIT_STR(r2l, str_p) void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *data) { @@ -18,24 +20,46 @@ void bam_aux_append(bam1_t *b, const char tag[2], char type, int len, uint8_t *d memcpy(b->data + ori_len + 3, data, len); } -uint8_t *bam_aux_get(bam1_t *b, const char tag[2]) +uint8_t *bam_aux_get_core(bam1_t *b, const char tag[2]) +{ + return bam_aux_get(b, tag); +} + +#define __skip_tag(s) do { \ + int type = toupper(*(s)); \ + ++(s); \ + if (type == 'C' || type == 'A') ++(s); \ + else if (type == 'S') (s) += 2; \ + else if (type == 'I' || type == 'F') (s) += 4; \ + else if (type == 'D') (s) += 8; \ + else if (type == 'Z' || type == 'H') { while (*(s)) ++(s); ++(s); } \ + } while (0) + +uint8_t *bam_aux_get(const bam1_t *b, const char tag[2]) { uint8_t *s; int y = tag[0]<<8 | tag[1]; s = bam1_aux(b); while (s < b->data + b->data_len) { - int type, x = (int)s[0]<<8 | s[1]; + int x = (int)s[0]<<8 | s[1]; s += 2; if (x == y) return s; - type = toupper(*s); ++s; - if (type == 'C') ++s; - else if (type == 'S') s += 2; - else if (type == 'I' || type == 'F') s += 4; - else if (type == 'D') s += 8; - else if (type == 'Z' || type == 'H') { while (*s) putchar(*s++); ++s; } + __skip_tag(s); } return 0; } +// s MUST BE returned by bam_aux_get() +int bam_aux_del(bam1_t *b, uint8_t *s) +{ + uint8_t *p, *aux; + aux = bam1_aux(b); + p = s - 2; + __skip_tag(s); + memmove(p, s, b->l_aux - (s - aux)); + b->data_len -= s - p; + b->l_aux -= s - p; + return 0; +} void bam_init_header_hash(bam_header_t *header) { @@ -65,7 +89,7 @@ int32_t bam_get_tid(const bam_header_t *header, const char *seq_name) return k == kh_end(h)? -1 : kh_value(h, k); } -void bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end) +int bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *begin, int *end) { char *s, *p; int i, l, k; @@ -74,7 +98,7 @@ void bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *b bam_init_header_hash(header); h = (khash_t(s)*)header->hash; - + l = strlen(str); p = s = (char*)malloc(l+1); /* squeeze out "," */ @@ -86,12 +110,12 @@ void bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *b iter = kh_get(s, h, s); /* get the ref_id */ if (iter == kh_end(h)) { // name not found *ref_id = -1; free(s); - return; + return -1; } *ref_id = kh_value(h, iter); if (i == k) { /* dump the whole sequence */ *begin = 0; *end = 1<<29; free(s); - return; + return 0; } for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break; *begin = atoi(p); @@ -100,8 +124,12 @@ void bam_parse_region(bam_header_t *header, const char *str, int *ref_id, int *b *end = atoi(p); } else *end = 1<<29; if (*begin > 0) --*begin; - assert(*begin <= *end); free(s); + if (*begin > *end) { + fprintf(stderr, "[bam_parse_region] invalid region.\n"); + return -1; + } + return 0; } int32_t bam_aux2i(const uint8_t *s)