2 from testlib
import testutil
, PygrTestProgram
3 from pygr
import sequence
, seqdb
, sequtil
, annotation
4 from pygr
.sequence
import Sequence
5 from pygr
.annotation
import AnnotationDB
7 class AnnotationSeq_Test(unittest
.TestCase
):
9 class Annotation(object):
10 def __init__(self
, **kwargs
):
11 self
.__dict
__.update(kwargs
)
13 slicedb
= dict(X
=Annotation(id='seq', start
=0, stop
=10),
14 Y
=Annotation(id='seq', start
=0, stop
=10),
15 Z
=Annotation(id='seq2', start
=0, stop
=10))
17 sequence_dict
= dict(seq
= Sequence('ATGGGGCCGATTG', 'seq',),
18 seq2
= Sequence('ATGGGGCCGATTG', 'seq2'))
20 self
.db
= AnnotationDB(slicedb
, sequence_dict
)
22 self
.annot
= self
.db
['X']
24 def test_orientation_index_error(self
):
26 db
.sliceAttrDict
= dict(id=0, start
=1, stop
=2, orientation
=3)
28 # index error should be caught silently, so this should succeed.
29 db
.new_annotation('some name', ('seq', 5, 8))
32 assert cmp(self
.annot
, None) == -1
33 assert cmp(self
.annot
, self
.annot
) == 0
39 assert a
[1:2] == b
[1:2]
41 # different annotations, even though they point at the same sequence
42 assert cmp(self
.annot
, self
.db
['Y']) == -1
44 # different sequences, even though they point at the same actual seq
45 assert cmp(self
.annot
, self
.db
['Z']) == -1
47 def test_strslice(self
):
50 assert 0, "should not get here"
56 assert repr(annot
) == 'annotX[0:10]'
57 assert repr(-annot
) == '-annotX[0:10]'
59 annot
.annotationType
= 'foo'
60 assert repr(annot
) == 'fooX[0:10]'
61 del annot
.annotationType
64 assert repr(self
.annot
.sequence
) == 'seq[0:10]'
67 assert repr(self
.annot
[1:2].sequence
) == 'seq[1:2]'
69 class AnnotationDB_Test(unittest
.TestCase
):
71 Test for all of the basic dictionary functions on 'AnnotationDB'.
74 class Annotation(object):
75 def __init__(self
, **kwargs
):
76 self
.__dict
__.update(kwargs
)
77 slicedb
= dict(annot1
=Annotation(id='seq', start
=0, stop
=10),
78 annot2
=Annotation(id='seq', start
=5, stop
=9))
79 sequence_dict
= dict(seq
= Sequence('ATGGGGCCGATTG', 'seq'))
80 self
.db
= AnnotationDB(slicedb
, sequence_dict
)
82 def test_setitem(self
):
84 self
.db
['foo'] = 'bar' # use 'add_annotation' instead
85 assert 0, "should not reach this point"
90 x
= hash(self
.db
) # works!
91 d
= dict(foo
=self
.db
) # also works!
97 assert k
== ['annot1', 'annot2'], k
99 def test_contains(self
):
100 "AnnotationDB contains"
101 assert 'annot1' in self
.db
, self
.db
.keys()
102 assert 'annot2' in self
.db
103 assert 'foo' not in self
.db
105 def test_has_key(self
):
106 "AnnotationDB has key"
107 assert self
.db
.has_key('annot1')
108 assert self
.db
.has_key('annot2')
109 assert not self
.db
.has_key('foo')
113 assert self
.db
.get('foo') is None
114 assert self
.db
.get('annot1') is not None
115 assert str(self
.db
.get('annot1').sequence
).startswith('ATGGGGC')
116 assert self
.db
.get('annot2') is not None
117 assert str(self
.db
.get('annot2').sequence
).startswith('GCCG')
119 def test_items(self
):
121 i
= [ k
for (k
,v
) in self
.db
.items() ]
123 assert i
== ['annot1', 'annot2']
125 def test_iterkeys(self
):
126 "AnnotationDB iterkeys"
129 ik
= list(self
.db
.iterkeys())
133 def test_itervalues(self
):
134 "AnnotationDB itervalues"
135 kv
= self
.db
.values()
137 iv
= list(self
.db
.itervalues())
139 assert kv
[0] == iv
[0]
140 assert kv
== iv
, (kv
, iv
)
142 def test_iteritems(self
):
143 "AnnotationDB iteritems"
146 ii
= list(self
.db
.iteritems())
148 assert ki
== ii
, (ki
, ii
)
150 def test_readonly(self
):
151 "AnnotationDB readonly"
153 self
.db
.copy() # what should 'copy' do on AD?
154 assert 0, 'this method should raise NotImplementedError'
155 except NotImplementedError:
157 try: # what should 'setdefault' do on AD?
158 self
.db
.setdefault('foo')
159 assert 0, 'this method should raise NotImplementedError'
160 except NotImplementedError:
162 try: # what should 'update' do on AD?
164 assert 0, 'this method should raise NotImplementedError'
165 except NotImplementedError:
169 assert 0, 'this method should raise NotImplementedError'
170 except NotImplementedError:
174 assert 0, 'this method should raise NotImplementedError'
175 except NotImplementedError:
179 assert 0, 'this method should raise NotImplementedError'
180 except NotImplementedError:
183 def test_equality(self
):
184 "AnnotationDB equality"
185 # Check that separately generated annotation objects test equal"
188 x
= db
.sliceAnnotation(key
, db
.sliceDB
[key
])
189 y
= db
.sliceAnnotation(key
, db
.sliceDB
[key
])
192 def test_bad_seqdict(self
):
193 "AnnotationDB bad seqdict"
194 class Annotation(object):
195 def __init__(self
, **kwargs
):
196 self
.__dict
__.update(kwargs
)
197 slicedb
= dict(annot1
=Annotation(id='seq', start
=0, stop
=10),
198 annot2
=Annotation(id='seq', start
=5, stop
=9))
199 foo_dict
= dict(foo
=Sequence('ATGGGGCCGATTG', 'foo'))
201 db
= AnnotationDB(slicedb
, foo_dict
)
202 assert 0, "incorrect seqdb; key error should be raised"
206 class Translation_Test(unittest
.TestCase
):
208 self
.M
= sequence
.Sequence('ATG', 'methionine')
209 self
.FLIM
= sequence
.Sequence('TTTCTAATTATG', 'flim')
210 self
.db
= dict(methionine
=self
.M
, flim
=self
.FLIM
)
212 def test_simple_translate(self
):
215 assert sequtil
.translate_orf(str(db
['methionine'])) == 'M'
216 assert sequtil
.translate_orf(str(db
['flim'])) == 'FLIM'
218 def test_translation_db(self
):
219 aa_db
= annotation
.AnnotationDB({}, self
.db
,
220 itemClass
=annotation
.TranslationAnnot
,
221 itemSliceClass
=annotation
.TranslationAnnotSlice
,
222 sliceAttrDict
=dict(id=0, start
=1, stop
=2))
224 aa
= aa_db
.new_annotation('foo', (self
.M
.id, 0, 3))
226 assert str(orf
) == 'M'
228 aa2
= aa_db
.new_annotation('bar', (self
.FLIM
.id, 0, 12))
230 assert str(orf
) == 'FLIM'
232 def test_slice_descr(self
):
233 aa_db
= annotation
.AnnotationDB({}, self
.db
,
234 itemClass
=annotation
.TranslationAnnot
,
235 itemSliceClass
=annotation
.TranslationAnnotSlice
,
236 sliceAttrDict
=dict(id=0, start
=1, stop
=2))
238 aa
= aa_db
.new_annotation('bar', (self
.FLIM
.id, 0, 12))
239 assert str(aa
) == 'FLIM'
240 assert str(aa
[1:3].sequence
) == 'CTAATT'
242 def test_positive_frames(self
):
243 aa_db
= annotation
.AnnotationDB({}, self
.db
,
244 itemClass
=annotation
.TranslationAnnot
,
245 itemSliceClass
=annotation
.TranslationAnnotSlice
,
246 sliceAttrDict
=dict(id=0, start
=1, stop
=2))
248 f1
= aa_db
.new_annotation('f1', (self
.FLIM
.id, 0, 12))
249 assert str(f1
) == 'FLIM'
250 assert f1
.frame
== +1
252 f2
= aa_db
.new_annotation('f2', (self
.FLIM
.id, 1, 10))
253 assert str(f2
) == 'F*L'
254 assert f2
.frame
== +2
256 f3
= aa_db
.new_annotation('f3', (self
.FLIM
.id, 2, 11))
257 assert str(f3
) == 'SNY'
258 assert f3
.frame
== +3
260 def test_negative_frames(self
):
261 aa_db
= annotation
.AnnotationDB({}, self
.db
,
262 itemClass
=annotation
.TranslationAnnot
,
263 itemSliceClass
=annotation
.TranslationAnnotSlice
,
264 sliceAttrDict
=dict(id=0, start
=1, stop
=2,
267 f1
= aa_db
.new_annotation('f1', (self
.FLIM
.id, 0, 12, -1))
268 assert str(f1
) == 'HN*K'
269 assert f1
.frame
== -2
271 f2
= aa_db
.new_annotation('f2', (self
.FLIM
.id, 1, 10, -1))
272 assert str(f2
) == '*LE'
273 assert f2
.frame
== -1
275 f3
= aa_db
.new_annotation('f3', (self
.FLIM
.id, 2, 11, -1))
276 assert str(f3
) == 'IIR'
277 assert f3
.frame
== -3
279 if __name__
== '__main__':
280 PygrTestProgram(verbosity
=2)