1 | import sys, os |
---|
2 | import unittest |
---|
3 | try: |
---|
4 | sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) |
---|
5 | except: |
---|
6 | sys.path.insert(0, os.path.dirname(os.path.abspath("."))) |
---|
7 | |
---|
8 | from bx.intervals.intersection import Interval |
---|
9 | from bx.intervals.intersection import IntervalNode |
---|
10 | from bx.intervals.intersection import IntervalTree |
---|
11 | |
---|
12 | class NeighborTestCase(unittest.TestCase): |
---|
13 | |
---|
14 | def setUp(self): |
---|
15 | iv = IntervalNode( 50, 59, Interval(50, 59)) |
---|
16 | for i in range(0, 110, 10): |
---|
17 | if i == 50: continue |
---|
18 | f = Interval(i, i + 9) |
---|
19 | iv = iv.insert( f.start, f.end, f) |
---|
20 | self.intervals = iv |
---|
21 | |
---|
22 | def test_left(self): |
---|
23 | iv = self.intervals |
---|
24 | self.assertEqual(str(iv.left(60, n=2)), str([Interval(50, 59), Interval(40, 49)])) |
---|
25 | |
---|
26 | for i in range(10, 100, 10): |
---|
27 | r = iv.left(i, max_dist=10, n=1) |
---|
28 | self.assertEqual(r[0].end, i - 1) |
---|
29 | |
---|
30 | def test_toomany(self): |
---|
31 | iv = self.intervals |
---|
32 | self.assertEqual(len(iv.left(60, n=200)) , 6) |
---|
33 | |
---|
34 | |
---|
35 | def test_right(self): |
---|
36 | iv = self.intervals |
---|
37 | self.assertEqual(str(iv.left(60, n=2)), str([Interval(50, 59), Interval(40, 49)])) |
---|
38 | |
---|
39 | def get_right_start(b10): |
---|
40 | r = iv.right(b10+1, n=1) |
---|
41 | assert len(r) == 1 |
---|
42 | return r[0].start |
---|
43 | |
---|
44 | for i in range(10, 100, 10): |
---|
45 | self.assertEqual(get_right_start(i), i + 10) |
---|
46 | |
---|
47 | for i in range(0, 100, 10): |
---|
48 | r = iv.right(i-1, max_dist=10, n=1) |
---|
49 | print r |
---|
50 | self.assertEqual(r[0].start, i) |
---|
51 | |
---|
52 | class UpDownStreamTestCase(unittest.TestCase): |
---|
53 | |
---|
54 | def setUp(self): |
---|
55 | iv = IntervalTree() |
---|
56 | iv.add_interval(Interval(50, 59)) |
---|
57 | for i in range(0, 110, 10): |
---|
58 | if i == 50: continue |
---|
59 | f = Interval(i, i + 9) |
---|
60 | iv.add_interval(f) |
---|
61 | self.intervals = iv |
---|
62 | |
---|
63 | |
---|
64 | def test_upstream(self): |
---|
65 | iv = self.intervals |
---|
66 | upstreams = iv.upstream_of_interval(Interval(59, 60), num_intervals=200) |
---|
67 | for u in upstreams: |
---|
68 | self.assertTrue(u.end < 59) |
---|
69 | |
---|
70 | upstreams = iv.upstream_of_interval(Interval(60, 70, strand=-1), |
---|
71 | num_intervals=200) |
---|
72 | for u in upstreams: |
---|
73 | self.assertTrue(u.start > 70) |
---|
74 | |
---|
75 | |
---|
76 | upstreams = iv.upstream_of_interval(Interval(58, 58, strand=-1), |
---|
77 | num_intervals=200) |
---|
78 | for u in upstreams: |
---|
79 | self.assertTrue(u.start > 59) |
---|
80 | |
---|
81 | |
---|
82 | def test_downstream(self): |
---|
83 | iv = self.intervals |
---|
84 | downstreams = iv.downstream_of_interval(Interval(59, 60), |
---|
85 | num_intervals=200) |
---|
86 | for d in downstreams: |
---|
87 | self.assertTrue(d.start > 60) |
---|
88 | |
---|
89 | downstreams = iv.downstream_of_interval(Interval(59, 60, strand=-1), |
---|
90 | num_intervals=200) |
---|
91 | for d in downstreams: |
---|
92 | self.assertTrue(d.start < 59) |
---|
93 | |
---|
94 | |
---|
95 | def test_n(self): |
---|
96 | iv = self.intervals |
---|
97 | for i in range(0, 90, 10): |
---|
98 | r = iv.after(i, max_dist=20, num_intervals=2) |
---|
99 | self.assertEqual(r[0].start, i + 10) |
---|
100 | self.assertEqual(r[1].start, i + 20) |
---|
101 | |
---|
102 | r = iv.after_interval(Interval(i, i), max_dist=20, num_intervals=2) |
---|
103 | self.assertEqual(r[0].start, i + 10) |
---|
104 | self.assertEqual(r[1].start, i + 20) |
---|
105 | |
---|
106 | |
---|
107 | class LotsaTestCase(unittest.TestCase): |
---|
108 | """ put lotsa data in the tree and make sure it works""" |
---|
109 | def setUp(self): |
---|
110 | iv = IntervalNode(1, 2, Interval(1, 2)) |
---|
111 | self.max = 1000000 |
---|
112 | for i in range(0, self.max, 10): |
---|
113 | f = Interval(i, i) |
---|
114 | iv = iv.insert(f.start, f.end, f) |
---|
115 | |
---|
116 | for i in range(600): |
---|
117 | iv = iv.insert( 0, 1, Interval(0, 1) ) |
---|
118 | self.intervals = iv |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | def test_count(self): |
---|
123 | iv = self.intervals |
---|
124 | |
---|
125 | r = iv.right(1, n=33) |
---|
126 | self.assertEqual(len(r), 33) |
---|
127 | |
---|
128 | l = iv.left(1, n=33) |
---|
129 | self.assertEqual(len(l), 1) |
---|
130 | |
---|
131 | u = iv.right(1, n=9999) |
---|
132 | self.assertEqual(len(u), 250) |
---|
133 | |
---|
134 | # now increase max_dist |
---|
135 | u = iv.right(1, n=9999, max_dist=99999) |
---|
136 | self.assertEqual(len(u), 9999) |
---|
137 | |
---|
138 | |
---|
139 | def test_max_dist(self): |
---|
140 | iv = self.intervals |
---|
141 | r = iv.right(1, max_dist=0, n=10) |
---|
142 | self.assertEqual(len(r), 0) |
---|
143 | |
---|
144 | for n, d in enumerate(range(10, 1000, 10)): |
---|
145 | r = iv.right(1, max_dist=d, n=10000) |
---|
146 | self.assertEqual(len(r), n + 1) |
---|
147 | |
---|
148 | def test_find(self): |
---|
149 | iv = self.intervals |
---|
150 | random = __import__("random") |
---|
151 | for t in range(25): |
---|
152 | start = random.randint(0, self.max - 10000) |
---|
153 | end = start + random.randint(100, 10000) |
---|
154 | |
---|
155 | results = iv.find(start, end) |
---|
156 | for feat in results: |
---|
157 | self.assertTrue( |
---|
158 | (feat.end >= start and feat.end <= end) |
---|
159 | or |
---|
160 | (feat.start <= end and feat.start >= start) |
---|
161 | ) |
---|
162 | |
---|
163 | |
---|
164 | class IntervalTreeTest(unittest.TestCase): |
---|
165 | def setUp(self): |
---|
166 | |
---|
167 | iv = IntervalTree() |
---|
168 | n = 0 |
---|
169 | for i in range(1, 1000, 80): |
---|
170 | iv.insert(i, i + 10, dict(value=i*i)) |
---|
171 | # add is synonym for insert. |
---|
172 | iv.add(i + 20, i + 30, dict(astr=str(i*i))) |
---|
173 | |
---|
174 | # or insert/add an interval object with start, end attrs. |
---|
175 | iv.insert_interval(Interval(i + 40, i + 50, |
---|
176 | value=dict(astr=str(i*i)))) |
---|
177 | iv.add_interval(Interval(i + 60, i + 70, |
---|
178 | value=dict(astr=str(i*i)))) |
---|
179 | |
---|
180 | n += 4 |
---|
181 | self.intervals = self.iv = iv |
---|
182 | self.nintervals = n |
---|
183 | |
---|
184 | |
---|
185 | def test_find(self): |
---|
186 | |
---|
187 | r = self.iv.find(100, 200) |
---|
188 | self.assertEqual(len(r), 5) |
---|
189 | |
---|
190 | def test_traverse(self): |
---|
191 | a = [] |
---|
192 | fn = a.append |
---|
193 | |
---|
194 | self.iv.traverse(fn) |
---|
195 | self.assertEqual(len(a), self.nintervals) |
---|
196 | |
---|
197 | def test_empty(self): |
---|
198 | iv = IntervalTree() |
---|
199 | self.assertEqual([], iv.find(100, 300)) |
---|
200 | self.assertEqual([], iv.after(100)) |
---|
201 | self.assertEqual([], iv.before(100)) |
---|
202 | self.assertEqual([], iv.after_interval(100)) |
---|
203 | self.assertEqual([], iv.before_interval(100)) |
---|
204 | self.assertEqual([], iv.upstream_of_interval(100)) |
---|
205 | self.assertEqual([], iv.downstream_of_interval(100)) |
---|
206 | self.assertEqual(None, iv.traverse(lambda x: x.append(1))) |
---|
207 | |
---|
208 | def test_public_interval(self): |
---|
209 | |
---|
210 | fn = lambda ival: self.assert_(ival.interval) |
---|
211 | self.iv.traverse(fn) |
---|
212 | |
---|
213 | if __name__ == "__main__": |
---|
214 | |
---|
215 | unittest.main() |
---|