// 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)); }
samfile_t* b2g_samfile_open(char* path, char* mode, void* aux, ...) { char filename[1024] = {}; va_list args; va_start(args, aux); vsprintf(filename, path, args); va_end(args); if ('r' == mode[0] && access(filename, R_OK)) return NULL; samfile_t *in = samopen(filename, mode, aux); if (in) { if (!in->header) return NULL; // Make sure to parse the header for later use. bam_header_t *header = in->header; if (header->dict == 0) header->dict = sam_header_parse2(header->text); if (header->rg2lib == 0) header->rg2lib = sam_header2tbl(header->dict, "RG", "ID", "LB"); } return in; }