Merge commit 'remotes/ctb/xmlrpc_patches' into tryme
[pygr.git] / tests / nlmsa_test.py
blob0ae4c795673d7ed5dedeff8830485adf9c73066a
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 def test_lpo_query(self):
72 s1=sequence.Sequence('aaaa','s1')
73 s2=sequence.Sequence('bbbb','s2')
74 msa = cnestedlist.NLMSA(mode='memory')
75 msa[0:4]+=s1
76 msa[0:4]+=s2
77 msa.build()
78 msaSlice = msa[0:4]
79 assert len(msaSlice) == 2
80 l = [t[0:2] for t in msaSlice.edges()]
81 l.sort()
82 correct = [(slice(0,4),s1),(slice(0,4),s2)]
83 correct.sort()
84 assert l == correct
86 class NLMSA_Test(unittest.TestCase):
87 def setUp(self):
88 s = sequence.Sequence('ATGGACAGAGATGACAGATGAC', 'a')
89 s2 = sequence.Sequence('ATGGGAGCAGCATGACAGATGAC', 'b')
91 # make a non-empty NLMSA
92 nlmsa = cnestedlist.NLMSA('foo', mode='memory', pairwiseMode=True)
93 nlmsa += s
94 nlmsa[s] += s2
95 nlmsa.build()
97 self.s = s
98 self.s2 = s2
99 self.nlmsa = nlmsa
101 def test_iter(self):
102 "Iteration of NLMSA objects should return reasonable error."
104 # try iterating over it
105 try:
106 for x in self.nlmsa:
107 break # should fail before this
109 assert 0, "should not be able to iterate over NLMSA"
110 except NotImplementedError:
111 pass
113 def test_slice_repr(self):
114 "Ask for an informative __repr__ on NLMSASlice objects"
116 slice = self.nlmsa[self.s]
117 r = repr(slice)
118 assert 'seq=a' in r
120 slice = self.nlmsa[self.s2]
121 r = repr(slice)
122 assert 'seq=b' in r
124 class NLMSA_BuildWithAlignedIntervals_Test(unittest.TestCase):
125 def setUp(self):
126 seqdb_name = testutil.datafile('alignments.fa')
127 self.db = seqdb.SequenceFileDB(seqdb_name)
129 def _check_results(self, n):
130 db = self.db
132 a, b, c = db['a'], db['b'], db['c']
134 ival = a[0:8]
135 (result,) = n[ival].keys()
136 assert result == b[0:8]
138 ival = a[12:20]
139 (result,) = n[ival].keys()
140 assert result == c[0:8]
142 l = list(n[a].keys())
143 l.sort()
144 assert b[0:8] in l
145 assert c[0:8] in l
147 def test_simple(self):
148 # first set of intervals
149 ivals = [(('a', 0, 8, 1), ('b', 0, 8, 1),),
150 (('a', 12, 20, 1), ('c', 0, 8, 1)),]
152 n = cnestedlist.NLMSA('test', mode='memory', pairwiseMode=True)
154 alignedIvalsAttrs = dict(id=0, start=1, stop=2, idDest=0, startDest=1,
155 stopDest=2, ori=3, oriDest=3)
156 cti = nlmsa_utils.CoordsToIntervals(self.db, self.db,
157 alignedIvalsAttrs)
158 n.add_aligned_intervals(cti(ivals))
159 n.build()
161 self._check_results(n)
163 def test_simple_no_ori(self):
164 # first set of intervals
165 ivals = [(('a', 0, 8,), ('b', 0, 8,),),
166 (('a', 12, 20,), ('c', 0, 8,)),]
168 n = cnestedlist.NLMSA('test', mode='memory', pairwiseMode=True)
170 alignedIvalsAttrs = dict(id=0, start=1, stop=2, idDest=0, startDest=1,
171 stopDest=2)
172 cti = nlmsa_utils.CoordsToIntervals(self.db, self.db,
173 alignedIvalsAttrs)
174 n.add_aligned_intervals(cti(ivals))
175 n.build()
177 self._check_results(n)
179 def test_attr(self):
180 class Bag(object):
181 def __init__(self, **kw):
182 self.__dict__.update(kw)
184 # first set of intervals
185 db = self.db
186 a, b, c = db['a'], db['b'], db['c']
188 src_ival1 = Bag(id='a', start=0, stop=8, ori=1)
189 dst_ival1 = Bag(id='b', start=0, stop=8, ori=1)
191 src_ival2 = Bag(id='a', start=12, stop=20, ori=1)
192 dst_ival2 = Bag(id='c', start=0, stop=8, ori=1)
194 ivals = [(src_ival1, dst_ival1), (src_ival2, dst_ival2)]
196 n = cnestedlist.NLMSA('test', mode='memory', pairwiseMode=True)
198 cti = nlmsa_utils.CoordsToIntervals(self.db, self.db)
199 n.add_aligned_intervals(cti(ivals))
200 n.build()
202 self._check_results(n)
204 def test_single_ival_attr(self):
205 class Bag(object):
206 def __init__(self, **kw):
207 self.__dict__.update(kw)
209 # first set of intervals
210 db = self.db
211 a, b, c = db['a'], db['b'], db['c']
213 ival1 = Bag(id='a', start=0, stop=8, ori=1,
214 idDest='b', startDest=0, stopDest=8, stopOri=1)
215 ival2 = Bag(id='a', start=12, stop=20, ori=1,
216 idDest='c', startDest=0, stopDest=8, oriDest=1)
218 ivals = [ival1, ival2]
220 n = cnestedlist.NLMSA('test', mode='memory', pairwiseMode=True)
222 cti = nlmsa_utils.CoordsToIntervals(self.db, self.db, {})
223 n.add_aligned_intervals(cti(ivals))
224 n.build()
226 self._check_results(n)
229 def test_no_seqDict_args(self):
230 class Bag(object):
231 def __init__(self, **kw):
232 self.__dict__.update(kw)
234 # first set of intervals
235 db = self.db
237 src_ival1 = Bag(id='a', start=0, stop=8, ori=1)
238 dst_ival1 = Bag(id='b', start=0, stop=8, ori=1)
240 src_ival2 = Bag(id='a', start=12, stop=20, ori=1)
241 dst_ival2 = Bag(id='c', start=0, stop=8, ori=1)
243 ivals = [(src_ival1, dst_ival1), (src_ival2, dst_ival2)]
245 n = cnestedlist.NLMSA('test', mode='memory', pairwiseMode=True,
246 seqDict=db)
248 cti = nlmsa_utils.CoordsToIntervals(self.db)
249 n.add_aligned_intervals(cti(ivals))
250 n.build()
252 if __name__ == '__main__':
253 PygrTestProgram(verbosity=2)