6
|
1 import unittest
|
|
2 from SMART.Java.Python.structure.Interval import Interval
|
|
3
|
|
4 class Test_Interval(unittest.TestCase):
|
|
5
|
|
6 def setUp(self):
|
|
7 self.iInterval = Interval()
|
|
8 self.iInterval1 = Interval()
|
|
9 self.iInterval2 = Interval()
|
|
10
|
|
11 def test__init__(self):
|
|
12 self.iInterval.setChromosome("chromosome")
|
|
13 self.iInterval.setName("sequence")
|
|
14 self.iInterval.setStart(0)
|
|
15 self.iInterval.setEnd(123)
|
|
16 obsStart = self.iInterval.getStart()
|
|
17 obsEnd = self.iInterval.getEnd()
|
|
18 expStart = 0
|
|
19 expEnd = 123
|
|
20
|
|
21 self.assertEqual(expStart, obsStart)
|
|
22 self.assertEqual(expEnd, obsEnd)
|
|
23
|
|
24 def test_copy(self):
|
|
25 self.iInterval1.setName("interval1")
|
|
26 self.iInterval1.setChromosome("chr1")
|
|
27 self.iInterval1.setStart(100)
|
|
28 self.iInterval1.setEnd(300)
|
|
29 self.iInterval1.setDirection("+")
|
|
30
|
|
31 self.iInterval2.copy(self.iInterval1)
|
|
32 self.assertEqual(self.iInterval2.getName(), "interval1")
|
|
33 self.assertEqual(self.iInterval2.getChromosome(), "chr1")
|
|
34 self.assertEqual(self.iInterval2.getStart(), 100)
|
|
35 self.assertEqual(self.iInterval2.getEnd(), 300)
|
|
36 self.assertEqual(self.iInterval2.getDirection(), 1)
|
|
37
|
|
38 self.iInterval1.setStart(200)
|
|
39 self.assertEqual(self.iInterval2.getStart(), 100)
|
|
40
|
|
41 def test_getDirection(self):
|
|
42 self.iInterval1.setName("interval1")
|
|
43 self.iInterval1.setChromosome("chr1")
|
|
44 self.iInterval1.setStart(100)
|
|
45 self.iInterval1.setEnd(300)
|
|
46 self.iInterval1.setDirection("+")
|
|
47 expDirect = 1
|
|
48 self.assertEquals(expDirect,self.iInterval1.getDirection())
|
|
49
|
|
50 #!!!! Warning: two methods getStart() and getEnd() give the information maximum and minimum in interval.!!!!#
|
|
51 #In case strand = "+", start < end; strand = "-", start > end
|
|
52 def test_setStartEnd(self):
|
|
53 self.iInterval1 = Interval()
|
|
54 self.iInterval1.setName("interval1")
|
|
55 self.iInterval1.setChromosome("chr1")
|
|
56 self.iInterval1.setStart(100)
|
|
57 self.iInterval1.setEnd(300)
|
|
58 self.iInterval1.setDirection("+")
|
|
59
|
|
60 self.assertEqual(self.iInterval1.getName(), "interval1")
|
|
61 self.assertEqual(self.iInterval1.getChromosome(), "chr1")
|
|
62 self.assertEqual(self.iInterval1.getStart(),100)
|
|
63 self.assertEqual(self.iInterval1.getEnd(), 300)
|
|
64 self.assertEqual(self.iInterval1.getDirection(), 1)
|
|
65
|
|
66 self.iInterval1.setStart(200)
|
|
67 self.assertEqual(self.iInterval1.getStart(), 200)
|
|
68 self.assertEqual(self.iInterval1.getEnd(), 300)
|
|
69
|
|
70 self.iInterval1.setEnd(300)
|
|
71 self.iInterval1.setStart(100)
|
|
72 self.assertEqual(self.iInterval1.getStart(), 100)
|
|
73 self.assertEqual(self.iInterval1.getEnd(), 300)
|
|
74
|
|
75 self.iInterval1.setEnd(1200)
|
|
76 self.iInterval1.setStart(1000)
|
|
77 self.assertEqual(self.iInterval1.getStart(), 1000)
|
|
78 self.assertEqual(self.iInterval1.getEnd(), 1200)
|
|
79
|
|
80 self.iInterval1.reverse()
|
|
81 self.assertEqual(self.iInterval1.getDirection(), -1)
|
|
82 self.assertEqual(self.iInterval1.getStart(), 1000)
|
|
83 self.assertEqual(self.iInterval1.getEnd(), 1200)
|
|
84
|
|
85 self.iInterval1.setStart(1100)
|
|
86 self.assertEqual(self.iInterval1.getStart(), 1100)
|
|
87 self.assertEqual(self.iInterval1.getEnd(), 1200)
|
|
88
|
|
89 self.iInterval1.setEnd(2200)
|
|
90 self.iInterval1.setStart(2000)
|
|
91 self.assertEqual(self.iInterval1.getStart(), 2000)
|
|
92 self.assertEqual(self.iInterval1.getEnd(), 2200)
|
|
93
|
|
94 self.iInterval1.setStart(1000)
|
|
95 self.iInterval1.setEnd(1200)
|
|
96 self.assertEqual(self.iInterval1.getStart(), 1000)
|
|
97 self.assertEqual(self.iInterval1.getEnd(), 1200)
|
|
98
|
|
99 def test_reverse(self):
|
|
100 self.iInterval1 = Interval()
|
|
101 self.iInterval1.setName("interval1")
|
|
102 self.iInterval1.setChromosome("chr1")
|
|
103 self.iInterval1.setStart(100)
|
|
104 self.iInterval1.setEnd(200)
|
|
105 self.iInterval1.setDirection("+")
|
|
106 self.iInterval1.reverse()
|
|
107 self.assertEqual(self.iInterval1.getStart(), 100)
|
|
108 self.assertEqual(self.iInterval1.getEnd(), 200)
|
|
109 self.assertEqual(self.iInterval1.getDirection(), -1)
|
|
110
|
|
111 def test_overlapWith(self):
|
|
112 self.iInterval1 = Interval()
|
|
113 self.iInterval1.setName("interval1")
|
|
114 self.iInterval1.setChromosome("chr1")
|
|
115 self.iInterval1.setStart(100)
|
|
116 self.iInterval1.setEnd(200)
|
|
117 self.iInterval1.setDirection("+")
|
|
118
|
|
119 self.iInterval2 = Interval()
|
|
120 self.iInterval2.copy(self.iInterval1)
|
|
121 self.iInterval2.setName("interval2")
|
|
122
|
|
123 self.assertTrue(self.iInterval1.overlapWith(self.iInterval2))
|
|
124
|
|
125 self.iInterval2.setChromosome("chr2")
|
|
126 try:
|
|
127 self.iInterval1.overlapWith(self.iInterval2)
|
|
128 self.fail()
|
|
129 except Exception:
|
|
130 pass
|
|
131
|
|
132 self.iInterval2.setChromosome("chr1")
|
|
133 self.iInterval2.setEnd(400)
|
|
134 self.iInterval2.setStart(300)
|
|
135 self.assertFalse(self.iInterval1.overlapWith(self.iInterval2))
|
|
136
|
|
137 self.iInterval2.setStart(200)
|
|
138 self.assertTrue(self.iInterval1.overlapWith(self.iInterval2))
|
|
139
|
|
140 def test_isIncludeIn(self):
|
|
141 self.iInterval1 = Interval()
|
|
142 self.iInterval1.setName("interval1")
|
|
143 self.iInterval1.setChromosome("chr1")
|
|
144 self.iInterval1.setStart(100)
|
|
145 self.iInterval1.setEnd(200)
|
|
146 self.iInterval1.setDirection("+")
|
|
147
|
|
148 self.iInterval2 = Interval()
|
|
149 self.iInterval2.setName("interval2")
|
|
150 self.iInterval2.setChromosome("chr1")
|
|
151 self.iInterval2.setStart(80)
|
|
152 self.iInterval2.setEnd(280)
|
|
153 self.iInterval2.setDirection("+")
|
|
154 self.assertTrue(self.iInterval1.isIncludeIn(self.iInterval2))
|
|
155
|
|
156 def test_getDistance(self):
|
|
157 self.iInterval1 = Interval()
|
|
158 self.iInterval1.setName("interval1")
|
|
159 self.iInterval1.setChromosome("chr1")
|
|
160 self.iInterval1.setStart(100)
|
|
161 self.iInterval1.setEnd(200)
|
|
162 self.iInterval1.setDirection("+")
|
|
163
|
|
164 self.iInterval2 = Interval()
|
|
165 self.iInterval2.copy(self.iInterval1)
|
|
166 self.iInterval2.setName("interval2")
|
|
167
|
|
168 self.assertEqual(self.iInterval1.getDistance(self.iInterval2), 0)
|
|
169 self.assertEqual(self.iInterval2.getDistance(self.iInterval1), 0)
|
|
170
|
|
171 self.iInterval2.setChromosome("chr2")
|
|
172 try:
|
|
173 self.iInterval1.getDistance(self.iInterval2)
|
|
174 self.fail()
|
|
175 except Exception:
|
|
176 pass
|
|
177
|
|
178 self.iInterval2.setChromosome("chr1")
|
|
179 self.iInterval2.setEnd(400)
|
|
180 self.iInterval2.setStart(300)
|
|
181 self.assertEqual(self.iInterval1.getDistance(self.iInterval2), 100)
|
|
182 self.assertEqual(self.iInterval2.getDistance(self.iInterval1), 100)
|
|
183
|
|
184 def test_getRelativeDistance(self):
|
|
185 self.iInterval1 = Interval()
|
|
186 self.iInterval1.setName("interval1")
|
|
187 self.iInterval1.setChromosome("chr1")
|
|
188 self.iInterval1.setStart(100)
|
|
189 self.iInterval1.setEnd(200)
|
|
190 self.iInterval1.setDirection("+")
|
|
191
|
|
192 self.iInterval2 = Interval()
|
|
193 self.iInterval2.copy(self.iInterval1)
|
|
194 self.iInterval2.setName("interval2")
|
|
195
|
|
196 self.assertEqual(self.iInterval1.getDistance(self.iInterval2), 0)
|
|
197 self.assertEqual(self.iInterval2.getDistance(self.iInterval1), 0)
|
|
198
|
|
199 self.iInterval2.setChromosome("chr2")
|
|
200 try:
|
|
201 self.iInterval1.getDistance(self.iInterval2)
|
|
202 self.fail()
|
|
203 except Exception:
|
|
204 pass
|
|
205
|
|
206 self.iInterval2.setChromosome("chr1")
|
|
207 self.iInterval2.setEnd(400)
|
|
208 self.iInterval2.setStart(300)
|
|
209 self.assertEqual(self.iInterval1.getRelativeDistance(self.iInterval2), 100)
|
|
210 self.assertEqual(self.iInterval2.getRelativeDistance(self.iInterval1), -100)
|
|
211
|
|
212 def test_merge(self):
|
|
213 self.iInterval1 = Interval()
|
|
214 self.iInterval1.setName("interval1")
|
|
215 self.iInterval1.setChromosome("chr1")
|
|
216 self.iInterval1.setStart(100)
|
|
217 self.iInterval1.setEnd(200)
|
|
218 self.iInterval1.setDirection("+")
|
|
219
|
|
220 self.iInterval2 = Interval()
|
|
221 self.iInterval2.copy(self.iInterval1)
|
|
222 self.iInterval2.setName("interval2")
|
|
223 self.iInterval2.merge(self.iInterval1)
|
|
224
|
|
225 self.assertEqual(self.iInterval1, self.iInterval2)
|
|
226
|
|
227 self.iInterval2.setChromosome("chr2")
|
|
228 expMessage = "Cannot merge '%s' and '%s' for they are on different chromosomes." % (str(self.iInterval2), str(self.iInterval1))
|
|
229 isExceptionRaised = False
|
|
230 try:
|
|
231 self.iInterval2.merge(self.iInterval1)
|
|
232 except Exception, e:
|
|
233 isExceptionRaised = True
|
|
234 obsMessage = str(e)
|
|
235
|
|
236 self.assertTrue(isExceptionRaised)
|
|
237 self.assertEquals(expMessage, obsMessage)
|
|
238 #Warning! Both two intervals should be on the same chromosome and direction.
|
|
239 self.iInterval2.setChromosome("chr1")
|
|
240 self.iInterval2.setStart(300)
|
|
241 self.iInterval2.setEnd(400)
|
|
242 self.iInterval2.merge(self.iInterval1)
|
|
243 self.assertEqual(self.iInterval2.getStart(), 100)
|
|
244 self.assertEqual(self.iInterval2.getEnd(), 400)
|
|
245 self.assertEqual(self.iInterval2.getChromosome(), "chr1")
|
|
246
|
|
247 def test_include(self):
|
|
248 iInterval1 = Interval()
|
|
249 iInterval1.setName("interval1")
|
|
250 iInterval1.setChromosome("chr1")
|
|
251 iInterval1.setStart(100)
|
|
252 iInterval1.setEnd(200)
|
|
253 iInterval1.setDirection("+")
|
|
254
|
|
255 iInterval2 = Interval()
|
|
256 iInterval2.copy(iInterval1)
|
|
257 iInterval2.setName("interval2")
|
|
258 self.assertTrue(iInterval1.include(iInterval2))
|
|
259 self.assertTrue(iInterval2.include(iInterval1))
|
|
260
|
|
261 iInterval2.setChromosome("chr2")
|
|
262 self.assertFalse(iInterval1.include(iInterval2))
|
|
263 self.assertFalse(iInterval2.include(iInterval1))
|
|
264
|
|
265 iInterval2.setChromosome("chr1")
|
|
266 iInterval1.setStart(1)
|
|
267 self.assertTrue(iInterval1.include(iInterval2))
|
|
268 self.assertFalse(iInterval2.include(iInterval1))
|
|
269
|
|
270 iInterval1.setStart(100)
|
|
271 iInterval1.setEnd(300)
|
|
272 self.assertTrue(iInterval1.include(iInterval2))
|
|
273 self.assertFalse(iInterval2.include(iInterval1))
|
|
274
|
|
275
|
|
276 def test_getDifference(self):
|
|
277 iInterval1 = Interval()
|
|
278 iInterval1.setName("interval1")
|
|
279 iInterval1.setChromosome("chr1")
|
|
280 iInterval1.setStart(100)
|
|
281 iInterval1.setEnd(400)
|
|
282 iInterval1.setDirection("+")
|
|
283
|
|
284 iInterval2 = Interval()
|
|
285 iInterval2.copy(iInterval1)
|
|
286 iInterval2.setName("interval2")
|
|
287 self.assertEqual(iInterval1.getDifference(iInterval2), [])
|
|
288 self.assertEqual(iInterval2.getDifference(iInterval1), [])
|
|
289
|
|
290 iInterval2.setChromosome("chr2")
|
|
291 results = iInterval1.getDifference(iInterval2)
|
|
292 self.assertEqual(len(results), 1)
|
|
293 resultInterval = results[0]
|
|
294 self.assertEqual(resultInterval.getStart(), iInterval1.getStart())
|
|
295 self.assertEqual(resultInterval.getEnd(), iInterval1.getEnd())
|
|
296 self.assertEqual(resultInterval.getDirection(), iInterval1.getDirection())
|
|
297 self.assertEqual(resultInterval.getChromosome(), iInterval1.getChromosome())
|
|
298
|
|
299 iInterval2.setChromosome("chr1")
|
|
300 iInterval2.setEnd(300)
|
|
301 results = iInterval1.getDifference(iInterval2)
|
|
302 self.assertEqual(len(results), 1)
|
|
303 resultInterval = results[0]
|
|
304 self.assertEqual(resultInterval.getStart(), 301)
|
|
305 self.assertEqual(resultInterval.getEnd(), iInterval1.getEnd())
|
|
306 self.assertEqual(resultInterval.getDirection(), iInterval1.getDirection())
|
|
307 self.assertEqual(resultInterval.getChromosome(), iInterval1.getChromosome())
|
|
308
|
|
309 iInterval2.setDirection("-")
|
|
310 results = iInterval1.getDifference(iInterval2, True)
|
|
311 self.assertEqual(len(results), 1)
|
|
312 resultInterval = results[0]
|
|
313 self.assertEqual(resultInterval.getStart(), iInterval1.getStart())
|
|
314 self.assertEqual(resultInterval.getEnd(), iInterval1.getEnd())
|
|
315 self.assertEqual(resultInterval.getDirection(), iInterval1.getDirection())
|
|
316 self.assertEqual(resultInterval.getChromosome(), iInterval1.getChromosome())
|
|
317
|
|
318 iInterval2.setDirection("+")
|
|
319 iInterval2.setStart(200)
|
|
320 results = iInterval1.getDifference(iInterval2)
|
|
321 self.assertEqual(len(results), 2)
|
|
322 resultInterval1, resultInterval2 = results
|
|
323 self.assertEqual(resultInterval1.getStart(), iInterval1.getStart())
|
|
324 self.assertEqual(resultInterval1.getEnd(), 199)
|
|
325 self.assertEqual(resultInterval1.getDirection(), iInterval1.getDirection())
|
|
326 self.assertEqual(resultInterval1.getChromosome(), iInterval1.getChromosome())
|
|
327 self.assertEqual(resultInterval2.getStart(), 301)
|
|
328 self.assertEqual(resultInterval2.getEnd(), iInterval1.getEnd())
|
|
329 self.assertEqual(resultInterval2.getDirection(), iInterval1.getDirection())
|
|
330 self.assertEqual(resultInterval2.getChromosome(), iInterval1.getChromosome())
|
|
331
|
|
332 iInterval2.setEnd(2000)
|
|
333 iInterval2.setStart(1000)
|
|
334 results = iInterval1.getDifference(iInterval2)
|
|
335 self.assertEqual(len(results), 1)
|
|
336 resultInterval = results[0]
|
|
337 self.assertEqual(resultInterval.getStart(), iInterval1.getStart())
|
|
338 self.assertEqual(resultInterval.getEnd(), iInterval1.getEnd())
|
|
339 self.assertEqual(resultInterval.getDirection(), iInterval1.getDirection())
|
|
340 self.assertEqual(resultInterval.getChromosome(), iInterval1.getChromosome())
|
|
341
|
|
342 def test_mergeWithDifferentStrand(self):
|
|
343 self.iInterval1 = Interval()
|
|
344 self.iInterval1.setName("interval1")
|
|
345 self.iInterval1.setChromosome("chr1")
|
|
346 self.iInterval1.setStart(100)
|
|
347 self.iInterval1.setEnd(200)
|
|
348 self.iInterval1.setDirection("+")
|
|
349
|
|
350 self.iInterval2 = Interval()
|
|
351 self.iInterval2.setName("interval2")
|
|
352 self.iInterval2.setChromosome("chr1")
|
|
353 self.iInterval2.setStart(300)
|
|
354 self.iInterval2.setEnd(400)
|
|
355 self.iInterval2.setDirection("-")
|
|
356
|
|
357 expMessage = "Cannot merge '%s' and '%s' for they are on different strands." % (str(self.iInterval2), str(self.iInterval1))
|
|
358 isExceptionRaised = False
|
|
359 try:
|
|
360 self.iInterval2.merge(self.iInterval1)
|
|
361 except Exception, e:
|
|
362 isExceptionRaised = True
|
|
363 obsMessage = str(e)
|
|
364
|
|
365 self.assertTrue(isExceptionRaised)
|
|
366 self.assertEquals(expMessage, obsMessage)
|
|
367
|
|
368 if __name__ == "__main__":
|
|
369 unittest.main()
|