How to use Payload in interval

14 views
Skip to first unread message

Charles Vejnar

unread,
Aug 14, 2014, 6:53:24 PM8/14/14
to biogo...@googlegroups.com
Hi,

I would like to use intervals with names. In the example attached, I tried to use the "Payload" field, but as it is not defined in the interface, it doesn't work: Payload is not defined.

Sorry if this is trivial: how would you give a name to an interval in the attached example (coming from biogo tests), so that when you use the Ge method you can also get the name of the overlap interval w/o using a map to store ID<>names?

Thanks for your help.

Charles

interval_example.go

Dan Kortschak

unread,
Aug 14, 2014, 8:00:10 PM8/14/14
to Charles Vejnar, biogo...@googlegroups.com
Basically, you define a type that you want to store as an interval and
provide the necessary methods for that type to satisfy the
interval.IntInterface[1] (if using integral position data since the
general interval.Interface is much slower).

Since Get and the other methods on the Tree types return interface
values, you need to assert to the concrete type. This part is what you
were not doing.

So for example:

// Integer-specific intervals
type IntInterval struct {
Start, End int
UID uintptr
Name string
}

func (i IntInterval) Overlap(b interval.IntRange) bool {
// Half-open interval indexing.
return i.End > b.Start && i.Start < b.End
}
func (i IntInterval) ID() uintptr { return i.UID }
func (i IntInterval) Range() interval.IntRange { return
interval.IntRange{i.Start, i.End} }
func (i IntInterval) String() string { return fmt.Sprintf("[%
d,%d)#%d %q", i.Start, i.End, i.UID, i.Name) }

var intIvs = []IntInterval{
{Start: 0, End: 2, Name: "a"},
{Start: 2, End: 4, Name: "b"},
{Start: 1, End: 6, Name: "c"},
{Start: 3, End: 4, Name: "d"},
{Start: 1, End: 3, Name: "e"},
{Start: 4, End: 6, Name: "f"},
{Start: 5, End: 8, Name: "g"},
{Start: 6, End: 8, Name: "h"},
{Start: 5, End: 7, Name: "i"},
{Start: 8, End: 9, Name: "j"},
}

func Example_2() {
t := &interval.IntTree{}
for i, iv := range intIvs {
iv.UID = uintptr(i)
err := t.Insert(iv, false)
if err != nil {
fmt.Println(err)
}
}

fmt.Println("Integer-specific interval tree:")
matched := t.Get(IntInterval{Start: 3, End: 6})
fmt.Println(matched)

for _, i := range matched {
fmt.Println(i.(IntInterval).Name)
}
}

A much more detailed example is provided in the biogo.talks repo for
kdtree (different store, but the same general principle)[2]. The code in
biogo/align/pals/piler.go is also probably worth reading, since it does
all this with interval.IntInterface using a number of the idiomatic
approaches.

Dan

[1]http://godoc.org/code.google.com/p/biogo.store/interval#IntInterface
[2]http://go-talks.appspot.com/code.google.com/p/biogo.talks/illumination/illumina.article

Charles Vejnar

unread,
Aug 14, 2014, 9:38:32 PM8/14/14
to biogo...@googlegroups.com, charles...@gmail.com
Thanks for your great help. I think Go interfaces aren't 100% clear to me yet :)

Charles

Dan Kortschak

unread,
Aug 14, 2014, 9:41:32 PM8/14/14
to Charles Vejnar, biogo...@googlegroups.com
On Thu, 2014-08-14 at 18:38 -0700, Charles Vejnar wrote:
> Thanks for your great help. I think Go interfaces aren't 100% clear to
> me yet :)

They are the zen of Go.

Reply all
Reply to author
Forward
0 new messages