| 1 | // Copyright ©2012 The bíogo Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package feat provides the base for storage and manipulation of biological interval information. |
| 6 | package featscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/ |
| 7 | |
| 8 | type Conformationerscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Conformationer# interface { |
| 9 | Conformationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Conformationer#Conformation.() Conformationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Conformation# |
| 10 | SetConformationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Conformationer#SetConformation.(Conformationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Conformation#) error |
| 11 | } |
| 12 | |
| 13 | type Conformationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Conformation# int8 |
| 14 | |
| 15 | func (c Conformationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Conformation#) Stringscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Conformation#String().() string { |
| 16 | switch c { |
| 17 | case UndefinedConformationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/UndefinedConformation.: |
| 18 | return "undefined" |
| 19 | case Linearscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Linear.: |
| 20 | return "linear" |
| 21 | case Circularscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Circular.: |
| 22 | return "circular" |
| 23 | } |
| 24 | panic("feat: illegal conformation") |
| 25 | } |
| 26 | |
| 27 | const ( |
| 28 | UndefinedConformationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/UndefinedConformation. Conformationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Conformation# = iota - 1 |
| 29 | Linearscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Linear. |
| 30 | Circularscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Circular. |
| 31 | ) |
| 32 | |
| 33 | // Orienter wraps the Orientation method. |
| 34 | // |
| 35 | // Orientation returns the orientation of the feature relative to its location. |
| 36 | type Orienterscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orienter# interface { |
| 37 | Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orienter#Orientation.() Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orientation# |
| 38 | } |
| 39 | |
| 40 | type OrientSetterscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/OrientSetter# interface { |
| 41 | SetOrientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/OrientSetter#SetOrientation.(Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orientation#) error |
| 42 | } |
| 43 | |
| 44 | type Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orientation# int8 |
| 45 | |
| 46 | func (o Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orientation#) Stringscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orientation#String().() string { |
| 47 | switch o { |
| 48 | case Reversescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Reverse.: |
| 49 | return "reverse" |
| 50 | case NotOrientedscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/NotOriented.: |
| 51 | return "not oriented" |
| 52 | case Forwardscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Forward.: |
| 53 | return "forward" |
| 54 | } |
| 55 | panic("feat: illegal orientation") |
| 56 | } |
| 57 | |
| 58 | const ( |
| 59 | Reversescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Reverse. Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orientation# = iota - 1 |
| 60 | NotOrientedscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/NotOriented. |
| 61 | Forwardscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Forward. |
| 62 | ) |
| 63 | |
| 64 | type Rangescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Range# interface { |
| 65 | Startscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Range#Start.() int |
| 66 | Endscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Range#End.() int |
| 67 | Lenscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Range#Len.() int |
| 68 | } |
| 69 | |
| 70 | // Feature is a Range whose coordinates are defined relative to a feature |
| 71 | // location. Start and End return the coordinates of the feature relative to |
| 72 | // its location which can be nil. In the latter case callers should make no |
| 73 | // assumptions whether coordinates of such features are comparable. |
| 74 | type Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature# interface { |
| 75 | Rangescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Range# |
| 76 | // Name returns the name of the feature. |
| 77 | Namescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Name.() string |
| 78 | // Description returns the description of the feature. |
| 79 | Descriptionscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Description.() string |
| 80 | // Location returns the reference feature on which the feature is located. |
| 81 | Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.() Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature# |
| 82 | } |
| 83 | |
| 84 | type Offsetterscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Offsetter# interface { |
| 85 | SetOffsetscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Offsetter#SetOffset.(int) error |
| 86 | } |
| 87 | |
| 88 | type Mutablescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Mutable# interface { |
| 89 | SetStartscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Mutable#SetStart.(int) error |
| 90 | SetEndscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Mutable#SetEnd.(int) error |
| 91 | } |
| 92 | |
| 93 | type LocationSetterscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/LocationSetter# interface { |
| 94 | SetLocationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/LocationSetter#SetLocation.(Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#) error |
| 95 | } |
| 96 | |
| 97 | type Pairscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Pair# interface { |
| 98 | Featuresscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Pair#Features.() [2]Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature# |
| 99 | } |
| 100 | |
| 101 | type Setscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Set# interface { |
| 102 | Featuresscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Set#Features.() []Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature# |
| 103 | } |
| 104 | |
| 105 | type Adderscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Adder# interface { |
| 106 | Setscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Set# |
| 107 | Addscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Adder#Add.(...Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#) |
| 108 | } |
| 109 | |
| 110 | type Collectionscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Collection# interface { |
| 111 | Setscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Set# |
| 112 | Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Collection#Location.() Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature# |
| 113 | } |
| 114 | |
| 115 | // BasePositionOf returns the position in f coordinates converted to |
| 116 | // coordinates relative to the first nil feature location, and a reference |
| 117 | // which is the feature location preceding the nil. The returned reference |
| 118 | // feature should be used by callers of BasePositionOf to verify that |
| 119 | // coordinates are comparable. |
| 120 | // BasePositionOf will panic if the feature chain is deeper than 1000 links. |
| 121 | func BasePositionOfscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/BasePositionOf().(f Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#, position int) (int, Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#) { |
| 122 | for n := 0; n < 1000; n++ { |
| 123 | position += f.Startscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Range#Start.() |
| 124 | if f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.() != nil { |
| 125 | f = f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.() |
| 126 | continue |
| 127 | } |
| 128 | return position, f |
| 129 | } |
| 130 | panic("feat: feature chain too long") |
| 131 | } |
| 132 | |
| 133 | // PositionWithin returns the position in f coordinates converted to |
| 134 | // coordinates relative to the given reference feature and a boolean |
| 135 | // indicating whether f can be located relative to ref. |
| 136 | // PositionWithin will panic if the feature chain is deeper than 1000 links. |
| 137 | func PositionWithinscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/PositionWithin().(f, ref Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#, position int) (pos int, ok bool) { |
| 138 | for n := 0; n < 1000; n++ { |
| 139 | if f == ref { |
| 140 | return position, f != nil |
| 141 | } |
| 142 | position += f.Startscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Range#Start.() |
| 143 | if f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.() != nil { |
| 144 | f = f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.() |
| 145 | continue |
| 146 | } |
| 147 | return 0, false |
| 148 | } |
| 149 | panic("feat: feature chain too long") |
| 150 | } |
| 151 | |
| 152 | // BaseOrientationOf returns the orientation of f relative to a reference |
| 153 | // which is the first non-nil, non-orientable feature location, and that |
| 154 | // reference feature. The returned reference feature should be used by callers |
| 155 | // of BaseOrientationOf to verify that orientations are comparable. If f is |
| 156 | // not orientable, the returned orientation will be NotOriented and the |
| 157 | // reference will be the first orientable or last non-nil feature. |
| 158 | // BaseOrientationOf will panic if the feature chain is deeper than 1000 links. |
| 159 | func BaseOrientationOfscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/BaseOrientationOf().(f Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#) (ori Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orientation#, ref Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#) { |
| 160 | o, ok := f.(Orienterscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orienter#) |
| 161 | if !ok || o.Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orienter#Orientation.() == NotOrientedscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/NotOriented. { |
| 162 | for n := 0; n < 1000; n++ { |
| 163 | if o, ok = f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.().(Orienterscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orienter#); ok && o.Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orienter#Orientation.() != NotOrientedscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/NotOriented. { |
| 164 | return NotOrientedscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/NotOriented., f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.() |
| 165 | } |
| 166 | if f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.() == nil { |
| 167 | return NotOrientedscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/NotOriented., f |
| 168 | } |
| 169 | f = f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.() |
| 170 | } |
| 171 | panic("feat: feature chain too long") |
| 172 | } |
| 173 | |
| 174 | ori = Forwardscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Forward. |
| 175 | for n := 0; n < 1000; n++ { |
| 176 | ori *= o.Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orienter#Orientation.() |
| 177 | if o, ok = f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.().(Orienterscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orienter#); ok && o.Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orienter#Orientation.() != NotOrientedscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/NotOriented. { |
| 178 | f = f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.() |
| 179 | continue |
| 180 | } |
| 181 | if f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.() != nil { |
| 182 | return ori, f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.() |
| 183 | } |
| 184 | return ori, f |
| 185 | } |
| 186 | panic("feat: feature chain too long") |
| 187 | } |
| 188 | |
| 189 | // OrientationWithin returns the orientation of f relative to the given |
| 190 | // reference feature. The returned orientation will be NotOriented if f is not |
| 191 | // located within the reference or if f is not orientable. |
| 192 | // OrientationWithin will panic if the feature chain is deeper than 1000 links. |
| 193 | func OrientationWithinscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/OrientationWithin().(f, ref Featurescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#) Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orientation# { |
| 194 | if ref == nil { |
| 195 | return NotOrientedscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/NotOriented. |
| 196 | } |
| 197 | ori := Forwardscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Forward. |
| 198 | for n := 0; n < 1000; n++ { |
| 199 | o, ok := f.(Orienterscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orienter#) |
| 200 | if !ok { |
| 201 | return NotOrientedscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/NotOriented. |
| 202 | } |
| 203 | if o := o.Orientationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Orienter#Orientation.(); o != NotOrientedscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/NotOriented. { |
| 204 | if f == ref { |
| 205 | return ori |
| 206 | } |
| 207 | ori *= o |
| 208 | f = f.Locationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/Feature#Location.() |
| 209 | if f == ref { |
| 210 | return ori |
| 211 | } |
| 212 | continue |
| 213 | } |
| 214 | return NotOrientedscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/feat`/NotOriented. |
| 215 | } |
| 216 | panic("feat: feature chain too long") |
| 217 | } |