added test of len() method for SQLTable
[pygr.git] / tests / nlmsa_test.py
blob0741cfde6709bfb700b5ccf69598469af6efb77c
1 import unittest
2 from testlib import testutil, PygrTestProgram
3 from pygr import cnestedlist, nlmsa_utils, seqdb, sequence
5 class NestedList_Test(unittest.TestCase):
6 "Basic cnestedlist class tests"
8 def setUp(self):
9 self.db = cnestedlist.IntervalDB()
10 ivals = [(0,10,1,-110,-100), (-20,-5,2,300,315)]
11 self.db.save_tuples(ivals)
13 def test_query(self):
14 "NestedList query"
15 assert self.db.find_overlap_list(0,10) == \
16 [(0, 10, 1, -110, -100), (5, 20, 2, -315, -300)]
18 def test_reverse(self):
19 "NestedList reverse"
20 assert self.db.find_overlap_list(-11,-7) == \
21 [(-10, 0, 1, 100, 110), (-20, -5, 2, 300, 315)]
23 def test_filedb(self):
24 "NestedList filedb"
25 tempdir = testutil.TempDir('nlmsa-test')
26 filename = tempdir.subfile('nlmsa')
27 self.db.write_binaries(filename)
28 fdb=cnestedlist.IntervalFileDB(filename)
29 assert fdb.find_overlap_list(0,10) == \
30 [(0, 10, 1, -110, -100), (5, 20, 2, -315, -300)]
31 assert fdb.find_overlap_list(-11,-7) == \
32 [(-10, 0, 1, 100, 110), (-20, -5, 2, 300, 315)]
34 # fails on windows
35 #tempdir.remove() @CTB
37 class NLMSA_SimpleTests(unittest.TestCase):
39 def setUp(self):
40 pass
42 def test_empty(self):
43 "NLMSA Empty"
44 blasthits = testutil.tempdatafile('blasthits')
46 msa = cnestedlist.NLMSA(blasthits , 'memory', pairwiseMode=True)
47 try:
48 msa.build()
49 raise AssertionError('failed to trap empty alignment!')
50 except nlmsa_utils.EmptyAlignmentError:
51 pass
53 def test_empty2(self):
54 "NLMSA Empty 2"
55 blasthits = testutil.tempdatafile('blasthits2')
56 msa = cnestedlist.NLMSA(blasthits, mode='w', pairwiseMode=True)
57 try:
58 msa.build()
59 raise AssertionError('failed to trap empty alignment!')
60 except nlmsa_utils.EmptyAlignmentError:
61 pass
63 def test_build(self):
64 "NLMSA build"
66 testnlmsa = testutil.tempdatafile('testnlmsa')
67 msa = cnestedlist.NLMSA(testnlmsa, mode='w', pairwiseMode=True,
68 bidirectional=False)
69 # @CTB should there be something else here? What is this testing?
71 class NLMSA_Test(unittest.TestCase):
72 def setUp(self):
73 s = sequence.Sequence('ATGGACAGAGATGACAGATGAC', 'a')
74 s2 = sequence.Sequence('ATGGGAGCAGCATGACAGATGAC', 'b')
76 # make a non-empty NLMSA
77 nlmsa = cnestedlist.NLMSA('foo', mode='memory', pairwiseMode=True)
78 nlmsa += s
79 nlmsa[s] += s2
80 nlmsa.build()
82 self.s = s
83 self.s2 = s2
84 self.nlmsa = nlmsa
86 def test_iter(self):
87 "Iteration of NLMSA objects should return reasonable error."
89 # try iterating over it
90 try:
91 for x in self.nlmsa:
92 break # should fail before this
94 assert 0, "should not be able to iterate over NLMSA"
95 except NotImplementedError:
96 pass
98 def test_slice_repr(self):
99 "Ask for an informative __repr__ on NLMSASlice objects"
101 slice = self.nlmsa[self.s]
102 r = repr(slice)
103 assert 'seq=a' in r
105 slice = self.nlmsa[self.s2]
106 r = repr(slice)
107 assert 'seq=b' in r
109 class NLMSA_BuildWithAlignedIntervals_Test(unittest.TestCase):
110 def setUp(self):
111 seqdb_name = testutil.datafile('alignments.fa')
112 self.db = seqdb.SequenceFileDB(seqdb_name)
114 def _check_results(self, n):
115 db = self.db
117 a, b, c = db['a'], db['b'], db['c']
119 ival = a[0:8]
120 (result,) = n[ival].keys()
121 assert result == b[0:8]
123 ival = a[12:20]
124 (result,) = n[ival].keys()
125 assert result == c[0:8]
127 l = list(n[a].keys())
128 l.sort()
129 assert b[0:8] in l
130 assert c[0:8] in l
132 def test_simple(self):
133 # first set of intervals
134 ivals = [(('a', 0, 8, 1), ('b', 0, 8, 1),),
135 (('a', 12, 20, 1), ('c', 0, 8, 1)),]
137 n = cnestedlist.NLMSA('test', mode='memory', pairwiseMode=True)
139 alignedIvalsAttrs = dict(id=0, start=1, stop=2, idDest=0, startDest=1,
140 stopDest=2, ori=3, oriDest=3)
141 cti = nlmsa_utils.CoordsToIntervals(self.db, self.db,
142 alignedIvalsAttrs)
143 n.add_aligned_intervals(cti(ivals))
144 n.build()
146 self._check_results(n)
148 def test_simple_no_ori(self):
149 # first set of intervals
150 ivals = [(('a', 0, 8,), ('b', 0, 8,),),
151 (('a', 12, 20,), ('c', 0, 8,)),]
153 n = cnestedlist.NLMSA('test', mode='memory', pairwiseMode=True)
155 alignedIvalsAttrs = dict(id=0, start=1, stop=2, idDest=0, startDest=1,
156 stopDest=2)
157 cti = nlmsa_utils.CoordsToIntervals(self.db, self.db,
158 alignedIvalsAttrs)
159 n.add_aligned_intervals(cti(ivals))
160 n.build()
162 self._check_results(n)
164 def test_attr(self):
165 class Bag(object):
166 def __init__(self, **kw):
167 self.__dict__.update(kw)
169 # first set of intervals
170 db = self.db
171 a, b, c = db['a'], db['b'], db['c']
173 src_ival1 = Bag(id='a', start=0, stop=8, ori=1)
174 dst_ival1 = Bag(id='b', start=0, stop=8, ori=1)
176 src_ival2 = Bag(id='a', start=12, stop=20, ori=1)
177 dst_ival2 = Bag(id='c', start=0, stop=8, ori=1)
179 ivals = [(src_ival1, dst_ival1), (src_ival2, dst_ival2)]
181 n = cnestedlist.NLMSA('test', mode='memory', pairwiseMode=True)
183 cti = nlmsa_utils.CoordsToIntervals(self.db, self.db)
184 n.add_aligned_intervals(cti(ivals))
185 n.build()
187 self._check_results(n)
189 def test_single_ival_attr(self):
190 class Bag(object):
191 def __init__(self, **kw):
192 self.__dict__.update(kw)
194 # first set of intervals
195 db = self.db
196 a, b, c = db['a'], db['b'], db['c']
198 ival1 = Bag(id='a', start=0, stop=8, ori=1,
199 idDest='b', startDest=0, stopDest=8, stopOri=1)
200 ival2 = Bag(id='a', start=12, stop=20, ori=1,
201 idDest='c', startDest=0, stopDest=8, oriDest=1)
203 ivals = [ival1, ival2]
205 n = cnestedlist.NLMSA('test', mode='memory', pairwiseMode=True)
207 cti = nlmsa_utils.CoordsToIntervals(self.db, self.db, {})
208 n.add_aligned_intervals(cti(ivals))
209 n.build()
211 self._check_results(n)
214 def test_no_seqDict_args(self):
215 class Bag(object):
216 def __init__(self, **kw):
217 self.__dict__.update(kw)
219 # first set of intervals
220 db = self.db
222 src_ival1 = Bag(id='a', start=0, stop=8, ori=1)
223 dst_ival1 = Bag(id='b', start=0, stop=8, ori=1)
225 src_ival2 = Bag(id='a', start=12, stop=20, ori=1)
226 dst_ival2 = Bag(id='c', start=0, stop=8, ori=1)
228 ivals = [(src_ival1, dst_ival1), (src_ival2, dst_ival2)]
230 n = cnestedlist.NLMSA('test', mode='memory', pairwiseMode=True,
231 seqDict=db)
233 cti = nlmsa_utils.CoordsToIntervals(self.db)
234 n.add_aligned_intervals(cti(ivals))
235 n.build()
237 if __name__ == '__main__':
238 PygrTestProgram(verbosity=2)