Debian Codegraph @

interfaces/interfaces.go

raw symbols SCIP
1 // interfaces for genomic relations
2 // irelate operates on "Relatable"s which keep a slice of related intervals.
3 package interfaces
4
5 import "strings"
6
7 // RelatableChannel
8 type RelatableChannel chan Relatable
9
10 // RelatableIterator provides a method to iterate over Relatables
11 type RelatableIterator interface {
12 Next() (Relatable, error)
13 Close() error
14 }
15
16 // Queryable allows querying by genomic position. Anything that meets this interface
17 // can be used in irelate.
18 type Queryable interface {
19 Query(region IPosition) (RelatableIterator, error)
20 }
21
22 // IPosition allows accessing positional interface for genomic types.
23 type IPosition interface {
24 Chrom() string
25 Start() uint32
26 End() uint32
27 }
28
29 // Interface to get the CIPos and CIEND from a VCF. Returns start, end, ok.
30 type CIFace interface {
31 CIPos() (uint32, uint32, bool)
32 CIEnd() (uint32, uint32, bool)
33 }
34
35 // Relatable provides all the methods for irelate to function.
36 // See Interval in parsers/interval.go for a class that satisfies this interface.
37 // Related() likely returns and AddRelated() likely appends to a slice of
38 // relatables. Note that for performance reasons, Relatable should be implemented
39 // as a pointer to your data-structure (see Interval).
40 type Relatable interface {
41 IPosition
42 Related() []Relatable // A slice of related Relatable's filled by IRelate
43 AddRelated(Relatable) // Adds to the slice of relatables
44 Source() uint32
45 SetSource(source uint32)
46 }
47
48 // Info must implement stuff to get info out of a variant info field.
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 // IRefAlt will force matching on the Ref and Alt fields when they are present.
59 type IRefAlt interface {
60 IPosition
61 Ref() string
62 Alt() []string
63 }
64
65 // IVariant must implement IPosition, Ref, Alt, and Info() methods for genetic variants
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 // trun an IPosition into an IRelatalbe
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 // turn a position thingy into a Relatable
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 // AsIPosition take chrom, start, end and returns an struct that meets the IPosition interface
145 func AsIPosition(chrom string, start int, end int) IPosition {
146 return ip{chrom, uint32(start), uint32(end)}
147 }
148
149 // SameChrom returns true if the strings are the same chromosome. Adjust for "chr" prefix.
150 func SameChrom(a, b string) bool {
151 if a == b {
152 return true
153 }
154 return StripChr(a) == StripChr(b)
155 }
156
157 // StripChr removes the "chr" prefix if it is present
158 func StripChr(c string) string {
159 if strings.HasPrefix(c, "chr") {
160 return c[3:]
161 }
162 return c
163 }
164
165 // SamePosition tests if 2 IPositions are the same
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 // SamePosition tests if 2 IPositions overlap
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 // equalFoldASCII reports whether s and t are equal under ASCII case
176 // folding only: A-Z/a-z fold to each other, every other byte (including all
177 // non-ASCII) must match exactly. It is NOT equivalent to strings.EqualFold,
178 // which also applies Unicode simple folding. Intended for comparing REF and ALT
179 // VCF fields, which are limited to A, C, G, T, N by spec.
180 func equalFoldASCII(s, t string) bool {
181 if len(s) != len(t) {
182 return false
183 }
184 for i := 0; i < len(s); i++ {
185 sb := s[i]
186 tb := t[i]
187 if sb == tb {
188 continue
189 }
190 // ASCII letters of opposite case differ by exactly 0x20.
191 if sb^tb != 0x20 {
192 return false
193 }
194 // Make sure the character is actually A-Z/a-z.
195 c := sb | 0x20
196 if c < 'a' || c > 'z' {
197 return false
198 }
199 }
200 return true
201 }
202
203 // SameVariant tests if 2 IRefAlts share the same position and ref and alt.
204 func SameVariant(a, b IRefAlt) bool {
205 if !SamePosition(a, b) || !equalFoldASCII(a.Ref(), b.Ref()) {
206 return false
207 }
208 aalts := a.Alt()
209 balts := b.Alt()
210 for _, aalt := range aalts {
211 for _, balt := range balts {
212 if equalFoldASCII(aalt, balt) {
213 return true
214 }
215 }
216 }
217 if len(aalts) == 0 || len(balts) == 0 {
218 return false
219 }
220 if aalts[0] == "<NON_REF>" || balts[0] == "<NON_REF>" {
221 return true
222 }
223 return false
224 }
225
226 // Same tests the identity of 2 IPositions and attempts to cast to IRefAlts for more stringent
227 // checking.
228 func Same(a, b IPosition, strict bool) bool {
229 // strict only applies if both are IVariants, otherwise, we just check for overlap.
230 if !strict {
231 return OverlapsPosition(a, b)
232 }
233 if av, ok := a.(IRefAlt); ok {
234 if bv, ok := b.(IRefAlt); ok {
235 if strict {
236 return SameVariant(av, bv)
237 }
238 }
239 return OverlapsPosition(a, b)
240 }
241 // at most one of them is a variant, just check overlap.
242 return OverlapsPosition(a, b)
243 }