Example #1
0
File: bix.go Project: brentp/bix
func (tbx *Bix) Query(region interfaces.IPosition) (interfaces.RelatableIterator, error) {
	tbx2, err := newShort(tbx)
	if err != nil {
		return nil, err
	}
	if region == nil {
		var l string
		var err error
		buf := bufio.NewReader(tbx2.bgzf)
		l, err = buf.ReadString('\n')
		for i := 0; i < int(tbx2.Index.Skip) || rune(l[0]) == tbx2.Index.MetaChar; i++ {
			l, err = buf.ReadString('\n')
			if err != nil {
				return nil, err
			}
		}
		if tbx2.Index.Skip == 0 && rune(l[0]) != tbx2.Index.MetaChar {
			buf = bufio.NewReader(io.MultiReader(strings.NewReader(l), buf))
		}
		return bixerator{nil, buf, tbx2, region}, nil
	}

	cr, err := tbx2.ChunkedReader(region.Chrom(), int(region.Start()), int(region.End()))
	if err != nil {
		if cr != nil {
			tbx2.Close()
			cr.Close()
		}
		return nil, err
	}
	return bixerator{cr, bufio.NewReader(cr), tbx2, region}, nil
}
Example #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
}