func (t *Tbx) Get(chrom string, start int, end int) (io.Reader, error) { ichrom := t.getChrom(chrom) if ichrom == -1.0 { log.Printf("chromosome: %s not found in %s \n", chrom, t.path) return strings.NewReader(""), nil } ret := C.int(1) itr := C.tabix_itr_queryi(t.tbx, ichrom, C.int(start), C.int(end), &ret) if ret < 0 { return strings.NewReader(""), nil } p := newPipe(1) go func() { l := C.int(10) kstr1, kstr2 := <-t.kCache, <-t.kCache htf := <-t.htfs for l > 0 { // < 0 means no data // == 0 means we got data, but end after this l = C.tbx_itr_next5(htf, t.tbx, itr, &kstr1, &kstr2) if l < 0 { break } res := C.GoBytes(unsafe.Pointer(kstr1.s), C.int(kstr1.l)) _, err := p.Write(res) if err != nil { log.Fatal(err) } } close(p.ch) // unlock t.htfs <- htf t.kCache <- kstr1 t.kCache <- kstr2 }() return p, nil }
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 }