@@ -74,7 +74,7 @@ def display_int(self):
74
74
75
75
76
76
# @enum.unique
77
- class CoordianteStart (enum .Enum ):
77
+ class CoordinateStart (enum .Enum ):
78
78
zero = 0
79
79
one = 1
80
80
@@ -124,9 +124,19 @@ class CoordinateSystem(object):
124
124
http://genome.ucsc.edu/blog/the-ucsc-genome-browser-coordinate-counting-systems/
125
125
"""
126
126
127
- basis = attr .ib (validator = is_a (CoordianteStart ))
127
+ basis = attr .ib (validator = is_a (CoordinateStart ))
128
128
close_status = attr .ib (validator = is_a (CloseStatus ))
129
129
130
+ @classmethod
131
+ def build (cls , value ):
132
+ if isinstance (value , six .text_type ):
133
+ return cls .from_name (value )
134
+ if isinstance (value , dict ):
135
+ return cls (** value )
136
+ if isinstance (value , cls ):
137
+ return value
138
+ raise ValueError ("Cannot build CoordinateSystem from %s" % str (value ))
139
+
130
140
@classmethod
131
141
def from_name (cls , name ):
132
142
"""
@@ -143,7 +153,7 @@ def from_name(cls, name):
143
153
raise UnknownCoordinateSystem (name )
144
154
145
155
return cls (
146
- basis = CoordianteStart .from_name (basis_name ),
156
+ basis = CoordinateStart .from_name (basis_name ),
147
157
close_status = CloseStatus .from_name (close_name ),
148
158
)
149
159
@@ -178,9 +188,9 @@ def size(self, location):
178
188
179
189
def as_zero_based (self , location ):
180
190
start = location .start
181
- if self .basis is CoordianteStart .zero :
191
+ if self .basis is CoordinateStart .zero :
182
192
pass
183
- elif self .basis is CoordianteStart .one :
193
+ elif self .basis is CoordinateStart .one :
184
194
start = start - 1
185
195
else :
186
196
raise ValueError ("Unknown type of start: %s" % self .basis )
@@ -189,9 +199,9 @@ def as_zero_based(self, location):
189
199
190
200
def as_one_based (self , location ):
191
201
start = location .start
192
- if self .basis is CoordianteStart .zero :
202
+ if self .basis is CoordinateStart .zero :
193
203
start = start + 1
194
- elif self .basis is CoordianteStart .one :
204
+ elif self .basis is CoordinateStart .one :
195
205
pass
196
206
else :
197
207
raise ValueError ("Unknown type of start: %s" % self .basis )
@@ -218,16 +228,26 @@ def greater_than_start(self, attribute, value):
218
228
(value , self .start ))
219
229
220
230
231
+ def as_sorted_exons (raw ):
232
+ exons = []
233
+ for entry in raw :
234
+ if isinstance (entry , dict ):
235
+ exons .append (Exon (** entry ))
236
+ else :
237
+ exons .append (entry )
238
+ return tuple (sorted (exons , key = op .attrgetter ('start' )))
239
+
240
+
221
241
@attr .s (frozen = True , hash = True , slots = True )
222
242
class SequenceRegion (object ):
223
243
assembly_id = attr .ib (validator = is_a (six .text_type ), converter = six .text_type )
224
244
chromosome = attr .ib (validator = is_a (six .text_type ), converter = six .text_type )
225
245
strand = attr .ib (validator = is_a (Strand ), converter = Strand .build )
226
- exons = attr .ib (
227
- validator = is_a (tuple ),
228
- converter = lambda es : tuple (sorted (es , key = op .attrgetter ('start' ))),
246
+ exons = attr .ib (validator = is_a (tuple ), converter = as_sorted_exons )
247
+ coordinate_system = attr .ib (
248
+ validator = is_a (CoordinateSystem ),
249
+ converter = CoordinateSystem .build ,
229
250
)
230
- coordinate_system = attr .ib (validator = is_a (CoordinateSystem ))
231
251
232
252
@property
233
253
def start (self ):
0 commit comments