Merge commit 'remotes/ctb/xmlrpc_patches' into tryme
[pygr.git] / tests / annotation_test.py
blobdfb9e8ed30c72774814d5dd827333074d619a91a
1 import unittest
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):
8 def setUp(self):
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):
25 db = self.db
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))
31 def test_cmp(self):
32 assert cmp(self.annot, None) == -1
33 assert cmp(self.annot, self.annot) == 0
35 a = self.annot
36 b = self.annot
38 assert cmp(a, b) == 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):
48 try:
49 str(self.annot)
50 assert 0, "should not get here"
51 except ValueError:
52 pass
54 def test_repr(self):
55 annot = self.annot
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
63 def test_seq(self):
64 assert repr(self.annot.sequence) == 'seq[0:10]'
66 def test_slice(self):
67 assert repr(self.annot[1:2].sequence) == 'seq[1:2]'
69 class AnnotationDB_Test(unittest.TestCase):
70 """
71 Test for all of the basic dictionary functions on 'AnnotationDB'.
72 """
73 def setUp(self):
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):
83 try:
84 self.db['foo'] = 'bar' # use 'add_annotation' instead
85 assert 0, "should not reach this point"
86 except KeyError:
87 pass
89 def test_hash(self):
90 x = hash(self.db) # works!
91 d = dict(foo=self.db) # also works!
93 def test_keys(self):
94 "AnnotationDB keys"
95 k = self.db.keys()
96 k.sort()
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')
111 def test_get(self):
112 "AnnotationDB get"
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):
120 "AnnotationDB items"
121 i = [ k for (k,v) in self.db.items() ]
122 i.sort()
123 assert i == ['annot1', 'annot2']
125 def test_iterkeys(self):
126 "AnnotationDB iterkeys"
127 kk = self.db.keys()
128 kk.sort()
129 ik = list(self.db.iterkeys())
130 ik.sort()
131 assert kk == ik
133 def test_itervalues(self):
134 "AnnotationDB itervalues"
135 kv = self.db.values()
136 kv.sort()
137 iv = list(self.db.itervalues())
138 iv.sort()
139 assert kv[0] == iv[0]
140 assert kv == iv, (kv, iv)
142 def test_iteritems(self):
143 "AnnotationDB iteritems"
144 ki = self.db.items()
145 ki.sort()
146 ii = list(self.db.iteritems())
147 ii.sort()
148 assert ki == ii, (ki, ii)
150 def test_readonly(self):
151 "AnnotationDB readonly"
152 try:
153 self.db.copy() # what should 'copy' do on AD?
154 assert 0, 'this method should raise NotImplementedError'
155 except NotImplementedError:
156 pass
157 try: # what should 'setdefault' do on AD?
158 self.db.setdefault('foo')
159 assert 0, 'this method should raise NotImplementedError'
160 except NotImplementedError:
161 pass
162 try: # what should 'update' do on AD?
163 self.db.update({})
164 assert 0, 'this method should raise NotImplementedError'
165 except NotImplementedError:
166 pass
167 try:
168 self.db.clear()
169 assert 0, 'this method should raise NotImplementedError'
170 except NotImplementedError:
171 pass
172 try:
173 self.db.pop()
174 assert 0, 'this method should raise NotImplementedError'
175 except NotImplementedError:
176 pass
177 try:
178 self.db.popitem()
179 assert 0, 'this method should raise NotImplementedError'
180 except NotImplementedError:
181 pass
183 def test_equality(self):
184 "AnnotationDB equality"
185 # Check that separately generated annotation objects test equal"
186 key = 'annot1'
187 db = self.db
188 x = db.sliceAnnotation(key, db.sliceDB[key])
189 y = db.sliceAnnotation(key, db.sliceDB[key])
190 assert x == y
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'))
200 try:
201 db = AnnotationDB(slicedb, foo_dict)
202 assert 0, "incorrect seqdb; key error should be raised"
203 except KeyError:
204 pass
206 class Translation_Test(unittest.TestCase):
207 def setUp(self):
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):
213 db = self.db
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))
225 orf = aa_db['foo']
226 assert str(orf) == 'M'
228 aa2 = aa_db.new_annotation('bar', (self.FLIM.id, 0, 12))
229 orf = aa_db['bar']
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,
265 orientation=3))
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)