Exemplo n.º 1
0
// At takes a region like 1:45678-56789 and returns a channel on which
// it sends a string for each line that falls in that interval.
func (t *Tabix) At(region string) interfaces.RelatableChannel {
	cs := C.CString(region)
	defer C.free(unsafe.Pointer(cs))

	out := make(interfaces.RelatableChannel, 20)
	itr := C.tabix_itr_querys(t.tbx, cs)

	go func() {
		kstr := C.kstring_t{}
		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)
			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)))
			}
			out <- NewVariant(b, t.hdr, 1)
		}
		close(out)
		C.hts_itr_destroy(itr)
		C.free(unsafe.Pointer(kstr.s))
	}()
	return out
}
Exemplo n.º 2
0
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
}