| 1 |
|
| 2 |
|
| 3 |
package interfaces |
| 4 |
|
| 5 |
import "strings" |
| 6 |
|
| 7 |
|
| 8 |
type RelatableChannel chan Relatable |
| 9 |
|
| 10 |
|
| 11 |
type RelatableIterator interface { |
| 12 |
Next() (Relatable, error) |
| 13 |
Close() error |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
type Queryable interface { |
| 19 |
Query(region IPosition) (RelatableIterator, error) |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
type IPosition interface { |
| 24 |
Chrom() string |
| 25 |
Start() uint32 |
| 26 |
End() uint32 |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
type CIFace interface { |
| 31 |
CIPos() (uint32, uint32, bool) |
| 32 |
CIEnd() (uint32, uint32, bool) |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
type Relatable interface { |
| 41 |
IPosition |
| 42 |
Related() []Relatable |
| 43 |
AddRelated(Relatable) |
| 44 |
Source() uint32 |
| 45 |
SetSource(source uint32) |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
type Info interface { |
| 50 |
Get(key string) (interface{}, error) |
| 51 |
Set(key string, val interface{}) error |
| 52 |
Delete(key string) |
| 53 |
Keys() []string |
| 54 |
String() string |
| 55 |
Bytes() []byte |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
type IRefAlt interface { |
| 60 |
IPosition |
| 61 |
Ref() string |
| 62 |
Alt() []string |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
type IVariant interface { |
| 67 |
IRefAlt |
| 68 |
CIFace |
| 69 |
Info() Info |
| 70 |
Id() string |
| 71 |
String() string |
| 72 |
} |
| 73 |
|
| 74 |
type SIPosition interface { |
| 75 |
IPosition |
| 76 |
String() string |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
type RelWrap struct { |
| 81 |
related []Relatable |
| 82 |
source uint32 |
| 83 |
} |
| 84 |
|
| 85 |
func (w *RelWrap) Source() uint32 { |
| 86 |
return w.source |
| 87 |
} |
| 88 |
func (w *RelWrap) SetSource(s uint32) { |
| 89 |
w.source = s |
| 90 |
} |
| 91 |
func (w *RelWrap) AddRelated(r Relatable) { |
| 92 |
if w.related == nil { |
| 93 |
w.related = make([]Relatable, 0, 2) |
| 94 |
} |
| 95 |
w.related = append(w.related, r) |
| 96 |
} |
| 97 |
|
| 98 |
func (w *RelWrap) Related() []Relatable { |
| 99 |
return w.related |
| 100 |
} |
| 101 |
|
| 102 |
type VarWrap struct { |
| 103 |
IVariant |
| 104 |
*RelWrap |
| 105 |
} |
| 106 |
|
| 107 |
type PosWrap struct { |
| 108 |
SIPosition |
| 109 |
*RelWrap |
| 110 |
} |
| 111 |
|
| 112 |
type RAWrap struct { |
| 113 |
IRefAlt |
| 114 |
*RelWrap |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
func AsRelatable(p SIPosition) Relatable { |
| 119 |
if v, ok := p.(IVariant); ok { |
| 120 |
return VarWrap{IVariant: v, RelWrap: &RelWrap{}} |
| 121 |
} |
| 122 |
if v, ok := p.(IRefAlt); ok { |
| 123 |
return &RAWrap{IRefAlt: v, RelWrap: &RelWrap{}} |
| 124 |
} |
| 125 |
return &PosWrap{SIPosition: p, RelWrap: &RelWrap{}} |
| 126 |
} |
| 127 |
|
| 128 |
type ip struct { |
| 129 |
chrom string |
| 130 |
start uint32 |
| 131 |
end uint32 |
| 132 |
} |
| 133 |
|
| 134 |
func (p ip) Chrom() string { |
| 135 |
return p.chrom |
| 136 |
} |
| 137 |
func (p ip) Start() uint32 { |
| 138 |
return p.start |
| 139 |
} |
| 140 |
func (p ip) End() uint32 { |
| 141 |
return p.end |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
func AsIPosition(chrom string, start int, end int) IPosition { |
| 146 |
return ip{chrom, uint32(start), uint32(end)} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
func SameChrom(a, b string) bool { |
| 151 |
if a == b { |
| 152 |
return true |
| 153 |
} |
| 154 |
return StripChr(a) == StripChr(b) |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
func StripChr(c string) string { |
| 159 |
if strings.HasPrefix(c, "chr") { |
| 160 |
return c[3:] |
| 161 |
} |
| 162 |
return c |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
func SamePosition(a, b IPosition) bool { |
| 167 |
return a.Start() == b.Start() && a.End() == b.End() && SameChrom(a.Chrom(), b.Chrom()) |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
func OverlapsPosition(a, b IPosition) bool { |
| 172 |
return (b.Start() < a.End() && b.End() > a.Start()) && SameChrom(a.Chrom(), b.Chrom()) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
func SameVariant(a, b IRefAlt) bool { |
| 177 |
if !SamePosition(a, b) || a.Ref() != b.Ref() { |
| 178 |
return false |
| 179 |
} |
| 180 |
for _, aalt := range a.Alt() { |
| 181 |
for _, balt := range b.Alt() { |
| 182 |
if aalt == balt { |
| 183 |
return true |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
if a.Alt()[0] == "<NON_REF>" || b.Alt()[0] == "<NON_REF>" { |
| 188 |
return true |
| 189 |
} |
| 190 |
return false |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
func Same(a, b IPosition, strict bool) bool { |
| 196 |
|
| 197 |
if !strict { |
| 198 |
return OverlapsPosition(a, b) |
| 199 |
} |
| 200 |
if av, ok := a.(IRefAlt); ok { |
| 201 |
if bv, ok := b.(IRefAlt); ok { |
| 202 |
if strict { |
| 203 |
return SameVariant(av, bv) |
| 204 |
} |
| 205 |
} |
| 206 |
return OverlapsPosition(a, b) |
| 207 |
} |
| 208 |
|
| 209 |
return OverlapsPosition(a, b) |
| 210 |
} |