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 // SameVariant tests if 2 IRefAlts share the same position and ref and alt.
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 // Same tests the identity of 2 IPositions and attempts to cast to IRefAlts for more stringent
194 // checking.
195 func Same(a, b IPosition, strict bool) bool {
196 // strict only applies if both are IVariants, otherwise, we just check for overlap.
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 // at most one of them is a variant, just check overlap.
209 return OverlapsPosition(a, b)
210 }