-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinning.go
192 lines (166 loc) · 5.07 KB
/
binning.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Package binning implements the interval binning scheme.
//
// These are some utility functions for working with the interval binning
// scheme as used in the UCSC Genome Browser: http://genome.cshlp.org/content/12/6/996.full
//
// This scheme can be used to implement fast overlap-based querying of
// intervals, essentially mimicking an R-tree index: https://en.wikipedia.org/wiki/R-tree
//
// Note that some database systems natively support spatial index methods such
// as R-trees. See for example the PostGIS extension for PostgreSQL: http://postgis.net
//
// Although in principle the method can be used for binning any kind of
// intervals, be aware that the largest position supported by this
// implementation is 2^29 (which covers the longest human chromosome).
//
// All positions and ranges in this package are zero-based and open-ended,
// following standard Go indexing and slicing notation.
package binning
import (
"errors"
"fmt"
)
// A Binning implements a specific interval binning scheme.
type Binning struct {
MaxPosition int
MaxBin int
binOffsets []int
shiftFirst uint
shiftNext uint
}
// The closure created by ranges for the interval start:stop returns the first
// and last bin overlapping the interval for each level, starting with the
// smallest bins.
// Algorithm by Jim Kent: http://genomewiki.ucsc.edu/index.php/Bin_indexing_system
func (b Binning) ranges(start, stop int) (func() (int, int, bool), error) {
if start < 0 || stop > b.MaxPosition+1 {
return nil, errors.New(fmt.Sprintf("interval out of range: %d-%d (maximum position is %d)", start, stop, b.MaxPosition))
}
if stop <= start {
stop = start + 1
}
startBin := start >> b.shiftFirst
stopBin := (stop - 1) >> b.shiftFirst
maxLevel := len(b.binOffsets) - 1
level := 0
return func() (int, int, bool) {
if level > maxLevel {
return 0, 0, false
}
if level > 0 {
startBin >>= b.shiftNext
stopBin >>= b.shiftNext
}
level++
return b.binOffsets[level-1] + startBin, b.binOffsets[level-1] + stopBin, true
}, nil
}
// Assign returns the smallest bin fitting the interval start:stop.
func (b Binning) Assign(start, stop int) (int, error) {
nextRange, err := b.ranges(start, stop)
if err != nil {
return 0, err
}
for {
startBin, stopBin, ok := nextRange()
if !ok {
break
}
if startBin == stopBin {
return startBin, nil
}
}
panic("unexpected loop fall-through")
}
// Overlapping returns bins for all intervals overlapping the interval
// start:stop by at least one position.
func (b Binning) Overlapping(start, stop int) ([]int, error) {
nextRange, err := b.ranges(start, stop)
if err != nil {
return nil, err
}
bins := []int{}
for {
startBin, stopBin, ok := nextRange()
if !ok {
break
}
tmp := make([]int, stopBin-startBin+1)
for bin := startBin; bin <= stopBin; bin++ {
tmp[bin-startBin] = bin
}
bins = append(bins, tmp...)
}
return bins, nil
}
// Containing returns bins for all intervals completely containing the
// interval start:stop.
func (b Binning) Containing(start, stop int) ([]int, error) {
maxBin, err := b.Assign(start, stop)
if err != nil {
return nil, err
}
overlapping, err := b.Overlapping(start, stop)
if err != nil {
return nil, err
}
bins := overlapping[:0]
for _, bin := range overlapping {
if bin <= maxBin {
bins = append(bins, bin)
}
}
return bins, nil
}
// Contained returns bins for all intervals completely contained by the
// interval start:stop.
func (b Binning) Contained(start, stop int) ([]int, error) {
minBin, err := b.Assign(start, stop)
if err != nil {
return nil, err
}
overlapping, err := b.Overlapping(start, stop)
if err != nil {
return nil, err
}
bins := overlapping[:0]
for _, bin := range overlapping {
if bin >= minBin {
bins = append(bins, bin)
}
}
return bins, nil
}
// Covered returns the interval covered by bin.
func (b Binning) Covered(bin int) (int, int, error) {
if bin < 0 || bin > b.MaxBin {
return 0, 0, errors.New(fmt.Sprintf("not a valid bin number: %d (must be >= 0 and <= %d)", bin, b.MaxBin))
}
shift := b.shiftFirst
for _, offset := range b.binOffsets {
if offset <= bin {
return (bin - offset) << shift, (bin + 1 - offset) << shift, nil
}
shift += b.shiftNext
}
panic("unexpected loop fall-through")
}
// NewBinning creates a new binning scheme with maxPosition the maximum
// position that can be binned, binOffsets the first bin number per level,
// shiftFirst how much to shift to get to the smallest bin, and shiftNext how
// much to shift to get to the next larger bin.
func NewBinning(maxPosition int, binOffsets []int, shiftFirst, shiftNext uint) Binning {
return Binning{
MaxPosition: maxPosition,
MaxBin: binOffsets[0] + (maxPosition >> shiftFirst),
binOffsets: binOffsets,
shiftFirst: shiftFirst,
shiftNext: shiftNext,
}
}
// StandardBinning returns the standard binning scheme used by the UCSC Genome
// Browser covering positions >= 0 and <= 2^29-1.
// http://genomewiki.ucsc.edu/index.php/Bin_indexing_system
func StandardBinning() Binning {
return NewBinning(1<<29-1, []int{512 + 64 + 8 + 1, 64 + 8 + 1, 8 + 1, 1, 0}, 17, 3)
}