std::string GenomicRegion::ChrName(const BamHeader& h) const { std::string cc; if (!h.isEmpty()) { if (chr >= h.NumSequences()) throw std::invalid_argument( "GenomicRegion::ChrName - not enough targets in BamHeader to cover ref id"); else cc = h.IDtoName(chr); // std::string(h->target_name[chr]); } else { cc = chrToString(chr); } return cc; }
GenomicRegion::GenomicRegion(const std::string& reg, const BamHeader& hdr) { if (hdr.isEmpty()) throw std::invalid_argument("GenomicRegion constructor - supplied empty BamHeader"); // scrub String //std::string reg2 = SeqLib::scrubString(reg, "chr"); // use htslib region parsing code int tid, beg, end; const char * q = hts_parse_reg(reg.c_str(), &beg, &end); if (q) { char *tmp = (char*)alloca(q - reg.c_str() + 1); // stack alloc strncpy(tmp, reg.c_str(), q - reg.c_str()); tmp[q - reg.c_str()] = 0; tid = hdr.Name2ID(std::string(tmp)); //bam_name2id(h.get(), tmp); if (tid < 0) { std::string inv = "GenomicRegion constructor: Failed to set region for " + reg; throw std::invalid_argument(inv); } if (end == INT_MAX) { // single chrome tid = hdr.Name2ID(reg); beg = 0; end = hdr.GetSequenceLength(reg); } } else { std::string inv = "GenomicRegion constructor: Failed to set region for " + reg; throw std::invalid_argument(inv); } chr = tid; pos1 = beg+1; pos2 = end; strand = '*'; }