func (t *Tbx) getChrom(chrom string) C.int { t.mu.RLock() if i, ok := t.chromCache[chrom]; ok { t.mu.RUnlock() return i } t.mu.RUnlock() cchrom := C.CString(chrom) ichrom := C.tbx_name2id(t.tbx, cchrom) C.free(unsafe.Pointer(cchrom)) if ichrom == -1 { if strings.HasPrefix(chrom, "chr") { cchrom = C.CString(chrom[3:]) } else { cchrom = C.CString("chr" + chrom) } ichrom = C.tbx_name2id(t.tbx, cchrom) C.free(unsafe.Pointer(cchrom)) } t.mu.Lock() t.chromCache[chrom] = ichrom t.mu.Unlock() return ichrom }
func (t *Tabix) Get(q interfaces.IPosition) []interfaces.IPosition { //cs := C.CString(fmt.Sprintf("%s:%d-%d", q.Chrom(), q.Start()+1, q.End())) ch := C.CString(q.Chrom()) cs := C.tbx_name2id(t.tbx, ch) itr := C.tabix_itr_queryi(t.tbx, cs, C.int(q.Start()), C.int(q.End())) kstr := C.kstring_t{} overlaps := make([]interfaces.IPosition, 0, 4) l := C.int(10) for l > 0 { l := C.atbx_itr_next(t.htf, t.tbx, itr, &kstr) if l < 0 { break } //res := C.GoString(kstr.s) if t.typ == VCF { b := C.bcf_init() ret := C.vcf_parse(&kstr, t.hdr, b) if ret < 0 { log.Printf("error parsing %s\n", C.GoStringN(kstr.s, C.int(kstr.l))) } overlaps = append(overlaps, NewVariant(b, t.hdr, 1)) } else if t.typ == BED { iv, err := parsers.IntervalFromBedLine(C.GoBytes(unsafe.Pointer(kstr.s), C.int(kstr.l))) if err != nil { log.Printf("error parsing %s:%s\n", C.GoStringN(kstr.s, C.int(kstr.l)), err) } if iv != nil { overlaps = append(overlaps, iv) } } } C.hts_itr_destroy(itr) C.free(unsafe.Pointer(ch)) C.free(unsafe.Pointer(kstr.s)) return overlaps }