#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_tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int32_t *_tpos)
-{
- int k, x = c->pos, y = 0, last_y = 0;
- *_tpos = c->pos;
- for (k = 0; k < c->n_cigar; ++k) {
- int op = cigar[k] & BAM_CIGAR_MASK;
- int l = cigar[k] >> BAM_CIGAR_SHIFT;
- if (op == BAM_CMATCH) {
- if (c->pos > tpos) return y;
- if (x + l > tpos) {
- *_tpos = tpos;
- return y + (tpos - x);
- }
- x += l; y += l;
- last_y = y;
- } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
- else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
- if (x + l > tpos) {
- *_tpos = x;
- return y;
- }
- x += l;
- }
- }
- *_tpos = x;
- return last_y;
-}
-
uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar)
{
uint32_t k, end;
free(header->target_len);
}
free(header->text);
-#ifndef BAM_NO_HASH
- if (header->rg2lib) bam_strmap_destroy(header->rg2lib);
+ if (header->dict) sam_header_free(header->dict);
+ if (header->rg2lib) sam_tbl_destroy(header->rg2lib);
bam_destroy_header_hash(header);
-#endif
free(header);
}
{
bam_header_t *header;
char buf[4];
+ int magic_len;
int32_t i = 1, name_len;
// check EOF
i = bgzf_check_EOF(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();
bam_write(fp, &x, 4);
} else bam_write(fp, &header->target_len[i], 4);
}
+ bgzf_flush(fp);
return 0;
}
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;
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; }
printf("%s\n", s);
free(s);
}
+
+// 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(h->rg2lib, (const char*)(rg + 1));
+}