annotate SMART/Java/Python/test/Test_FindOverlapsOptim.py @ 31:0ab839023fe4

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 14:33:21 -0400
parents 94ab73e8a190
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
1 import unittest
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
2 import os
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
3 from SMART.Java.Python.FindOverlapsOptim import FindOverlapsOptim
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
4 from SMART.Java.Python.ncList.NCListCursor import NCListCursor
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
5 from SMART.Java.Python.ncList.test.MockFindOverlapsWithSeveralIntervals import *
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
6
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
7 REFERENCE = 0
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
8 QUERY = 1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
9
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
10 class Test_FindOverlapsOptim(unittest.TestCase):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
11
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
12 def setUp(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
13 self._inputRefGff3FileName = 'sorted_Ref.gff3'
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
14 self._inputQueryGff3FileName = 'sorted_Query.gff3'
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
15 self._writeQueryGff3File(self._inputQueryGff3FileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
16 self._outputGff3FileName = 'overlaps.gff3'
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
17 iMock = MockFindOverlapsWithServeralIntervals_case1()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
18 iMock.write(self._inputRefGff3FileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
19 self._iFOO = FindOverlapsOptim(0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
20 self._iFOO.setRefFileName(self._inputRefGff3FileName, "gff3")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
21 self._iFOO.setQueryFileName(self._inputQueryGff3FileName, "gff3")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
22 self._iFOO.setOutputFileName(self._outputGff3FileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
23 self._iFOO.prepareIntermediateFiles()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
24 self._iFOO.createNCLists()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
25 self._queryNcList = self._iFOO._ncLists[QUERY]["chr1"]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
26 self._refNcList = self._iFOO._ncLists[REFERENCE]["chr1"]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
27
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
28 def tearDown(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
29 os.remove(self._inputRefGff3FileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
30 os.remove(self._inputQueryGff3FileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
31 os.remove(self._outputGff3FileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
32
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
33 def test_isOverlapping_true(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34 queryCursor = NCListCursor(None, self._queryNcList, 5, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 refCursor = NCListCursor(None, self._refNcList, 4, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36 obs = self._iFOO.isOverlapping(queryCursor, refCursor)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 exp = 0
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 self.assertEquals(exp, obs)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 def test_isOverlapping_false_left(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41 queryCursor = NCListCursor(None, self._queryNcList, 5, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42 refCursor = NCListCursor(None, self._refNcList, 2, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 obs = self._iFOO.isOverlapping(queryCursor, refCursor)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44 exp = -1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45 self.assertEquals(exp, obs)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47 def test_isOverlapping_false_right(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48 queryCursor = NCListCursor(None, self._queryNcList, 5, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49 refCursor = NCListCursor(None, self._refNcList, 1, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50 obs = self._iFOO.isOverlapping(queryCursor, refCursor)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51 exp = 1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52 self.assertEquals(exp, obs)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 def test_isLastElement_true(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55 refCursor = NCListCursor(None, self._refNcList, 4, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56 obsBool = refCursor.isLast()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57 expBool = True
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58 self.assertEquals(expBool, obsBool)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60 def test_isLastElement_false(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61 refCursor = NCListCursor(None, self._refNcList, 3, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
62 obsBool = refCursor.isLast()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63 expBool = False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64 self.assertEquals(expBool, obsBool)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
65
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
66 def test_isLastElement_highestLevel_true(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
67 refCursor = NCListCursor(None, self._refNcList, 1, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68 obsBool = refCursor.isLast()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69 expBool = True
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
70 self.assertEquals(expBool, obsBool)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
71
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
72 def test_isLastElement_highestLevel_false(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 refCursor = NCListCursor(None, self._refNcList, 0, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74 obsBool = refCursor.isLast()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75 expBool = False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76 self.assertEquals(expBool, obsBool)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 def test_findOverlapIter(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79 queryCursor = NCListCursor(None, self._queryNcList, 2, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80 refCursor = NCListCursor(None, self._refNcList, 0, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81 queryTranscript = queryCursor.getTranscript()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82 done = False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83 (cursor, done, empty) = self._iFOO.findOverlapIter(queryTranscript, refCursor, done)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84 obsFirstOverlapLAddr = (cursor._lIndex, done, empty)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
85 expFirstOverlapLAddr = 4, True, False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
86 self.assertEquals(expFirstOverlapLAddr, obsFirstOverlapLAddr)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
87
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
88 def test_not_findOverlapIter(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
89 queryCursor = NCListCursor(None, self._queryNcList, 4, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
90 refCursor = NCListCursor(None, self._refNcList, 1, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
91 queryTranscript = queryCursor.getTranscript()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
92 done = False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
93 (cursor, done, empty) = self._iFOO.findOverlapIter(queryTranscript, refCursor, done)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
94 obsFirstOverlapLAddr = (cursor._lIndex, done, empty)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
95 expFirstOverlapLAddr = -1, False, True
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
96 self.assertEquals(expFirstOverlapLAddr, obsFirstOverlapLAddr)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
97
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
98 def test_findOverlapIter_not_the_first_RefOverlap(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
99 queryCursor = NCListCursor(None, self._queryNcList, 3, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
100 refCursor = NCListCursor(None, self._refNcList, 4, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
101 queryTranscript = queryCursor.getTranscript()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
102 done = True
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
103 (cursor, done, empty) = self._iFOO.findOverlapIter(queryTranscript, refCursor, done)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
104 obsFirstOverlapLAddr = (cursor._lIndex, done, empty)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
105 expFirstOverlapLAddr = 1, True, False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
106 self.assertEquals(expFirstOverlapLAddr, obsFirstOverlapLAddr)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
107
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
108 def test_moveDown(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
109 refCursor = NCListCursor(None, self._refNcList, 0, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
110 refCursor.moveDown()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
111 expFirstChildLAddr = 2
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
112 self.assertEquals(expFirstChildLAddr, refCursor._lIndex)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
113
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
114 def test_moveUp(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
115 refCursor = NCListCursor(None, self._refNcList, 4, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
116 refCursor.moveUp()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
117 expFirstChildLAddr = 0
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
118 self.assertEquals(expFirstChildLAddr, refCursor._lIndex)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
119
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
120 def test_moveRight(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
121 refCursor = NCListCursor(None, self._refNcList, 3, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
122 refCursor.moveRight()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
123 expFirstChildLAddr = 4
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
124 self.assertEquals(expFirstChildLAddr, refCursor._lIndex)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
125
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
126 def test_moveNext(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
127 refCursor = NCListCursor(None, self._refNcList, 6, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
128 refCursor.moveNext()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
129 expFirstChildLAddr = 1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
130 self.assertEquals(expFirstChildLAddr, refCursor._lIndex)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
131
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
132 def test_not_findOverlapIter_between2RefIntervals(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
133 inputQueryGff3FileName = 'query2.gff3'
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
134 self._writeQueryGff3File2(inputQueryGff3FileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
135 self._outputGff3FileName = 'overlaps.gff3'
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
136 iMock = MockFindOverlapsWithServeralIntervals_case1()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
137 iMock.write(self._inputRefGff3FileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
138 _iFOO = FindOverlapsOptim(0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
139 _iFOO.setRefFileName(self._inputRefGff3FileName, "gff3")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
140 _iFOO.setQueryFileName(inputQueryGff3FileName, "gff3")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
141 _iFOO.setOutputFileName(self._outputGff3FileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
142 _iFOO.prepareIntermediateFiles()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
143 _iFOO.createNCLists()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
144 _queryNcList = _iFOO._ncLists[QUERY]["chr1"]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
145 _refNcList = _iFOO._ncLists[REFERENCE]["chr1"]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
146 queryCursor = NCListCursor(None, _queryNcList, 0, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
147 refCursor = NCListCursor(None, _refNcList, 0, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
148 queryTranscript = queryCursor.getTranscript()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
149 done = True
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
150 (cursor, done, empty) = _iFOO.findOverlapIter(queryTranscript, refCursor, done)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
151 lIndex = cursor._lIndex
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
152 obsFirstOverlapLAddr = (lIndex, done, empty)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
153 expFirstOverlapLAddr = 1, False, True
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
154 self.assertEquals(expFirstOverlapLAddr, obsFirstOverlapLAddr)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
155 os.remove(inputQueryGff3FileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
156
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
157 def _writeQueryGff3File2(self, fileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
158 f = open(fileName, 'w')
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
159 f.write("chr1\tquery\ttest1\t1100\t1150\t126\t+\t.\tID=test1.1;Name=test1.1\n")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
160 f.write("chr1\tquery\ttest2\t1250\t1300\t781\t+\t.\tID=test1.2;Name=test1.2\n")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
161 f.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
162
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
163 def _writeQueryGff3File(self, fileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
164 f = open(fileName, 'w')
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
165 f.write("chr1\tquery\ttest1.1\t25\t150\t126\t+\t.\tID=test1.1;Name=test1.1\n")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
166 f.write("chr1\tquery\ttest1.2\t70\t850\t781\t+\t.\tID=test1.2;Name=test1.2\n")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
167 f.write("chr1\tquery\ttest1.3\t550\t850\t201\t+\t.\tID=test1.3;Name=test1.3\n")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
168 f.write("chr1\tquery\ttest1.4\t925\t1025\t101\t+\t.\tID=test1.4;Name=test1.4\n")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
169 f.write("chr1\tquery\ttest1.5\t1201\t1210\t10\t+\t.\tID=test1.5;Name=test1.5\n")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
170 f.write("chr1\tquery\ttest1.6\t1500\t1600\t101\t+\t.\tID=test1.6;Name=test1.6\n")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
171 f.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
172
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
173 if __name__ == "__main__":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
174 unittest.main()