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 def test_lpo_query(self
):
72 s1
=sequence
.Sequence('aaaa','s1')
73 s2
=sequence
.Sequence('bbbb','s2')
74 msa
= cnestedlist
.NLMSA(mode
='memory')
79 assert len(msaSlice
) == 2
80 l
= [t
[0:2] for t
in msaSlice
.edges()]
82 correct
= [(slice(0,4),s1
),(slice(0,4),s2
)]
86 class NLMSA_Test(unittest
.TestCase
):
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)
102 "Iteration of NLMSA objects should return reasonable error."
104 # try iterating over it
107 break # should fail before this
109 assert 0, "should not be able to iterate over NLMSA"
110 except NotImplementedError:
113 def test_slice_repr(self
):
114 "Ask for an informative __repr__ on NLMSASlice objects"
116 slice = self
.nlmsa
[self
.s
]
120 slice = self
.nlmsa
[self
.s2
]
124 class NLMSA_BuildWithAlignedIntervals_Test(unittest
.TestCase
):
126 seqdb_name
= testutil
.datafile('alignments.fa')
127 self
.db
= seqdb
.SequenceFileDB(seqdb_name
)
129 def _check_results(self
, n
):
132 a
, b
, c
= db
['a'], db
['b'], db
['c']
135 (result
,) = n
[ival
].keys()
136 assert result
== b
[0:8]
139 (result
,) = n
[ival
].keys()
140 assert result
== c
[0:8]
142 l
= list(n
[a
].keys())
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
,
158 n
.add_aligned_intervals(cti(ivals
))
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,
172 cti
= nlmsa_utils
.CoordsToIntervals(self
.db
, self
.db
,
174 n
.add_aligned_intervals(cti(ivals
))
177 self
._check
_results
(n
)
181 def __init__(self
, **kw
):
182 self
.__dict
__.update(kw
)
184 # first set of intervals
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
))
202 self
._check
_results
(n
)
204 def test_single_ival_attr(self
):
206 def __init__(self
, **kw
):
207 self
.__dict
__.update(kw
)
209 # first set of intervals
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
))
226 self
._check
_results
(n
)
229 def test_no_seqDict_args(self
):
231 def __init__(self
, **kw
):
232 self
.__dict
__.update(kw
)
234 # first set of intervals
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,
248 cti
= nlmsa_utils
.CoordsToIntervals(self
.db
)
249 n
.add_aligned_intervals(cti(ivals
))
252 if __name__
== '__main__':
253 PygrTestProgram(verbosity
=2)