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"
9 self
.db
= cnestedlist
.IntervalDB()
10 ivals
= [(0,10,1,-110,-100), (-20,-5,2,300,315)]
11 self
.db
.save_tuples(ivals
)
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
):
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
):
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)]
35 #tempdir.remove() @CTB
37 class NLMSA_SimpleTests(unittest
.TestCase
):
44 blasthits
= testutil
.tempdatafile('blasthits')
46 msa
= cnestedlist
.NLMSA(blasthits
, 'memory', pairwiseMode
=True)
49 raise AssertionError('failed to trap empty alignment!')
50 except nlmsa_utils
.EmptyAlignmentError
:
53 def test_empty2(self
):
55 blasthits
= testutil
.tempdatafile('blasthits2')
56 msa
= cnestedlist
.NLMSA(blasthits
, mode
='w', pairwiseMode
=True)
59 raise AssertionError('failed to trap empty alignment!')
60 except nlmsa_utils
.EmptyAlignmentError
:
66 testnlmsa
= testutil
.tempdatafile('testnlmsa')
67 msa
= cnestedlist
.NLMSA(testnlmsa
, mode
='w', pairwiseMode
=True,
69 # @CTB should there be something else here? What is this testing?
71 class NLMSA_Test(unittest
.TestCase
):
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)
87 "Iteration of NLMSA objects should return reasonable error."
89 # try iterating over it
92 break # should fail before this
94 assert 0, "should not be able to iterate over NLMSA"
95 except NotImplementedError:
98 def test_slice_repr(self
):
99 "Ask for an informative __repr__ on NLMSASlice objects"
101 slice = self
.nlmsa
[self
.s
]
105 slice = self
.nlmsa
[self
.s2
]
109 class NLMSA_BuildWithAlignedIntervals_Test(unittest
.TestCase
):
111 seqdb_name
= testutil
.datafile('alignments.fa')
112 self
.db
= seqdb
.SequenceFileDB(seqdb_name
)
114 def _check_results(self
, n
):
117 a
, b
, c
= db
['a'], db
['b'], db
['c']
120 (result
,) = n
[ival
].keys()
121 assert result
== b
[0:8]
124 (result
,) = n
[ival
].keys()
125 assert result
== c
[0:8]
127 l
= list(n
[a
].keys())
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
,
143 n
.add_aligned_intervals(cti(ivals
))
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,
157 cti
= nlmsa_utils
.CoordsToIntervals(self
.db
, self
.db
,
159 n
.add_aligned_intervals(cti(ivals
))
162 self
._check
_results
(n
)
166 def __init__(self
, **kw
):
167 self
.__dict
__.update(kw
)
169 # first set of intervals
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
))
187 self
._check
_results
(n
)
189 def test_single_ival_attr(self
):
191 def __init__(self
, **kw
):
192 self
.__dict
__.update(kw
)
194 # first set of intervals
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
))
211 self
._check
_results
(n
)
214 def test_no_seqDict_args(self
):
216 def __init__(self
, **kw
):
217 self
.__dict
__.update(kw
)
219 # first set of intervals
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,
233 cti
= nlmsa_utils
.CoordsToIntervals(self
.db
)
234 n
.add_aligned_intervals(cti(ivals
))
237 if __name__
== '__main__':
238 PygrTestProgram(verbosity
=2)