Mercurial > repos > yufei-luo > s_mart
comparison commons/core/parsing/test/Test_VarscanFile.py @ 6:769e306b7933
Change the repository level.
author | yufei-luo |
---|---|
date | Fri, 18 Jan 2013 04:54:14 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5:ea3082881bf8 | 6:769e306b7933 |
---|---|
1 from commons.core.parsing.VarscanFile import VarscanFile | |
2 from commons.core.parsing.VarscanHit import VarscanHit | |
3 import unittest | |
4 import os | |
5 from commons.core.parsing.VarscanHit_WithTag import VarscanHit_WithTag | |
6 from commons.core.parsing.VarscanHit_v2_2_8 import VarscanHit_v2_2_8 | |
7 from commons.core.parsing.VarscanHit_v2_2_8_WithTag import VarscanHit_v2_2_8_WithTag | |
8 from commons.core.checker.CheckerException import CheckerException | |
9 | |
10 class Test_VarscanFile(unittest.TestCase): | |
11 | |
12 def test_parse_fileWithHeader(self): | |
13 varscanFileName = "file.varscan" | |
14 self._writeVarscanFile(varscanFileName) | |
15 | |
16 varscanHit1 = VarscanHit() | |
17 varscanHit1.setChrom('C02HBa0291P19_LR48') | |
18 varscanHit1.setPosition('32') | |
19 varscanHit1.setRef('C') | |
20 varscanHit1.setVar('T') | |
21 | |
22 varscanHit2 = VarscanHit() | |
23 varscanHit2.setChrom('C02HBa0291P19_LR48') | |
24 varscanHit2.setPosition('34') | |
25 varscanHit2.setRef('A') | |
26 varscanHit2.setVar('T') | |
27 expVarscanHitsList = [varscanHit1, varscanHit2] | |
28 | |
29 iVarscanFile = VarscanFile(varscanFileName) | |
30 iVarscanFile.parse() | |
31 obsVarscanHitsList = iVarscanFile.getVarscanHitsList() | |
32 os.remove(varscanFileName) | |
33 | |
34 self.assertEquals(expVarscanHitsList, obsVarscanHitsList) | |
35 | |
36 def test_parse_FileWithoutHeader(self): | |
37 varscanFileName = "file.varscan" | |
38 self._writeVarscanFileWithoutHeader(varscanFileName) | |
39 | |
40 varscanHit1 = VarscanHit() | |
41 varscanHit1.setChrom('C02HBa0291P19_LR48') | |
42 varscanHit1.setPosition('32') | |
43 varscanHit1.setRef('C') | |
44 varscanHit1.setVar('T') | |
45 | |
46 varscanHit2 = VarscanHit() | |
47 varscanHit2.setChrom('C02HBa0291P19_LR48') | |
48 varscanHit2.setPosition('34') | |
49 varscanHit2.setRef('A') | |
50 varscanHit2.setVar('T') | |
51 expVarscanHitsList = [varscanHit1, varscanHit2] | |
52 | |
53 iVarscanFile = VarscanFile(varscanFileName) | |
54 iVarscanFile.parse() | |
55 obsVarscanHitsList = iVarscanFile.getVarscanHitsList() | |
56 obsTypeOfVarscanFile = iVarscanFile.getTypeOfVarscanFile() | |
57 expTypeOfVarscanFile = "Varscan_2_2" | |
58 | |
59 self.assertEquals(expVarscanHitsList, obsVarscanHitsList) | |
60 self.assertEquals(expTypeOfVarscanFile, obsTypeOfVarscanFile) | |
61 os.remove(varscanFileName) | |
62 | |
63 def test_parse_VarscanFileWithTag(self): | |
64 inputFileName = "%s/commons/core/parsing/test/varscan.tab" % os.environ["REPET_PATH"] | |
65 self._writeVarscanFileWithTag(inputFileName) | |
66 launcher = VarscanFile(inputFileName) | |
67 launcher.parse() | |
68 obsListOfVarscanHits = launcher.getListOfVarscanHits() | |
69 | |
70 varscanHit1 = VarscanHit_WithTag() | |
71 varscanHit1.setChrom('C02HBa0291P19_LR48') | |
72 varscanHit1.setPosition('32') | |
73 varscanHit1.setRef('C') | |
74 varscanHit1.setVar('T') | |
75 varscanHit1.setTag('EspeceA') | |
76 | |
77 varscanHit2 = VarscanHit_WithTag() | |
78 varscanHit2.setChrom('C02HBa0291P19_LR48') | |
79 varscanHit2.setPosition('34') | |
80 varscanHit2.setRef('A') | |
81 varscanHit2.setVar('T') | |
82 varscanHit2.setTag('EspeceA') | |
83 expVarscanHitsList = [varscanHit1, varscanHit2] | |
84 | |
85 obsTypeOfVarscanFile = launcher.getTypeOfVarscanFile() | |
86 expTypeOfVarscanFile = "Varscan_2_2_WithTag" | |
87 | |
88 self.assertEquals(expVarscanHitsList, obsListOfVarscanHits) | |
89 self.assertEquals(expTypeOfVarscanFile, obsTypeOfVarscanFile) | |
90 os.remove(inputFileName) | |
91 | |
92 def test_parse_VarscanFile_v2_2_8(self): | |
93 inputFileName = "%s/commons/core/parsing/test/varscan.tab" % os.environ["REPET_PATH"] | |
94 self._writeVarscanFile_v2_2_8(inputFileName) | |
95 launcher = VarscanFile(inputFileName) | |
96 launcher.parse() | |
97 obsListOfVarscanHits = launcher.getListOfVarscanHits() | |
98 | |
99 varscanHit1 = VarscanHit_v2_2_8() | |
100 varscanHit1.setChrom('C11HBa0064J13_LR285') | |
101 varscanHit1.setPosition('3227') | |
102 varscanHit1.setRef('G') | |
103 varscanHit1.setVar('A') | |
104 varscanHit1.setCns('A') | |
105 | |
106 varscanHit2 = VarscanHit_v2_2_8() | |
107 varscanHit2.setChrom('C11HBa0064J13_LR285') | |
108 varscanHit2.setPosition('3230') | |
109 varscanHit2.setRef('G') | |
110 varscanHit2.setVar('T') | |
111 varscanHit2.setCns('T') | |
112 expVarscanHitsList = [varscanHit1, varscanHit2] | |
113 | |
114 obsTypeOfVarscanFile = launcher.getTypeOfVarscanFile() | |
115 expTypeOfVarscanFile = "Varscan_2_2_8" | |
116 | |
117 self.assertEquals(expVarscanHitsList, obsListOfVarscanHits) | |
118 self.assertEquals(expTypeOfVarscanFile, obsTypeOfVarscanFile) | |
119 os.remove(inputFileName) | |
120 | |
121 def test_parse_other(self): | |
122 inputFileName = "%s/commons/core/parsing/test/varscan.tab" % os.environ["REPET_PATH"] | |
123 self._writeOther(inputFileName) | |
124 launcher = VarscanFile(inputFileName) | |
125 try: | |
126 launcher.parse() | |
127 except CheckerException, e: | |
128 checkerExceptionInstance = e | |
129 expMessage = "Warning: this line (l.1) is not a valid varscan line !" | |
130 obsMessage = checkerExceptionInstance.msg | |
131 os.remove(inputFileName) | |
132 self.assertEquals(expMessage, obsMessage) | |
133 | |
134 def test__eq__notEqual(self): | |
135 lVarscanHits1 = [VarscanHit("chrom", "12", "T", "G"), VarscanHit("chrom", "14", "T", "G")] | |
136 iVarscanFile1 = VarscanFile() | |
137 iVarscanFile1.setVarscanHitsList(lVarscanHits1) | |
138 lVarscanHits2 = [VarscanHit("chrom", "12", "T", "G"), VarscanHit("chrom", "14", "T", "C")] | |
139 iVarscanFile2 = VarscanFile() | |
140 iVarscanFile2.setVarscanHitsList(lVarscanHits2) | |
141 | |
142 self.assertNotEquals(iVarscanFile1, iVarscanFile2) | |
143 | |
144 def test__eq__equal(self): | |
145 lVarscanHits1 = [VarscanHit("chrom", "12", "T", "G"), VarscanHit("chrom", "14", "T", "G")] | |
146 iVarscanFile1 = VarscanFile() | |
147 iVarscanFile1.setVarscanHitsList(lVarscanHits1) | |
148 lVarscanHits2 = [VarscanHit("chrom", "12", "T", "G"), VarscanHit("chrom", "14", "T", "G")] | |
149 iVarscanFile2 = VarscanFile() | |
150 iVarscanFile2.setVarscanHitsList(lVarscanHits2) | |
151 | |
152 self.assertEquals(iVarscanFile1, iVarscanFile2) | |
153 | |
154 def test_selectTypeOfVarscanHitObject_noVarscanHit(self): | |
155 launcher = VarscanFile() | |
156 launcher.setTypeOfVarscanFile("") | |
157 try: | |
158 launcher.selectTypeOfVarscanHitObject() | |
159 except CheckerException, e: | |
160 checkerExceptionInstance = e | |
161 expMessage = "Error: no varscan object found !" | |
162 obsMessage = checkerExceptionInstance.msg | |
163 self.assertEquals(expMessage, obsMessage) | |
164 | |
165 def test_selectTypeOfVarscanHitObject_VarscanHit(self): | |
166 launcher = VarscanFile() | |
167 launcher.setTypeOfVarscanFile("Varscan_2_2") | |
168 obsVarscanHit = launcher.selectTypeOfVarscanHitObject() | |
169 expVarscanHit = VarscanHit() | |
170 self.assertEquals(expVarscanHit, obsVarscanHit) | |
171 | |
172 def test_selectTypeOfVarscanHitObject_VarscanHitWithTag(self): | |
173 launcher = VarscanFile() | |
174 launcher.setTypeOfVarscanFile("Varscan_2_2_WithTag") | |
175 obsVarscanHit = launcher.selectTypeOfVarscanHitObject() | |
176 expVarscanHit = VarscanHit_WithTag() | |
177 self.assertEquals(expVarscanHit, obsVarscanHit) | |
178 | |
179 def test_selectTypeOfVarscanHitObject_noVarscanHit_2_2_8(self): | |
180 launcher = VarscanFile() | |
181 launcher.setTypeOfVarscanFile("Varscan_2_2_8") | |
182 obsVarscanHit = launcher.selectTypeOfVarscanHitObject() | |
183 expVarscanHit = VarscanHit_v2_2_8() | |
184 self.assertEquals(expVarscanHit, obsVarscanHit) | |
185 | |
186 def test_selectTypeOfVarscanHitObject_noVarscanHit_2_2_8_WithTag(self): | |
187 launcher = VarscanFile() | |
188 launcher.setTypeOfVarscanFile("Varscan_2_2_8_WithTag") | |
189 obsVarscanHit = launcher.selectTypeOfVarscanHitObject() | |
190 expVarscanHit = VarscanHit_v2_2_8_WithTag() | |
191 self.assertEquals(expVarscanHit, obsVarscanHit) | |
192 | |
193 def test_createVarscanObjectFromLine_VarscanHit(self): | |
194 line = "C02HBa0291P19_LR48\t32\tC\tT\t1\t2\t66,67%\t1\t1\t37\t35\t0.3999999999999999\n" | |
195 nbLine = 1 | |
196 launcher = VarscanFile() | |
197 launcher.setTypeOfVarscanFile("Varscan_2_2") | |
198 obsVarscanHit = launcher.createVarscanObjectFromLine(line, nbLine) | |
199 expVarscanHit = VarscanHit() | |
200 expVarscanHit.setChrom('C02HBa0291P19_LR48') | |
201 expVarscanHit.setPosition('32') | |
202 expVarscanHit.setRef('C') | |
203 expVarscanHit.setVar('T') | |
204 self.assertEquals(expVarscanHit, obsVarscanHit) | |
205 | |
206 def test_createVarscanObjectFromLine_VarscanHitWithTag(self): | |
207 line = "C02HBa0291P19_LR48\t32\tC\tT\t1\t2\t66,67%\t1\t1\t37\t35\t0.3999999999999999\tEspeceA\n" | |
208 nbLine = 1 | |
209 launcher = VarscanFile() | |
210 launcher.setTypeOfVarscanFile("Varscan_2_2_WithTag") | |
211 obsVarscanHit = launcher.createVarscanObjectFromLine(line, nbLine) | |
212 expVarscanHit = VarscanHit_WithTag() | |
213 expVarscanHit.setChrom('C02HBa0291P19_LR48') | |
214 expVarscanHit.setPosition('32') | |
215 expVarscanHit.setRef('C') | |
216 expVarscanHit.setVar('T') | |
217 expVarscanHit.setTag('EspeceA') | |
218 self.assertEquals(expVarscanHit, obsVarscanHit) | |
219 | |
220 def test_createVarscanObjectFromLine_VarscanHit_v2_2_8(self): | |
221 line = "C11HBa0064J13_LR285\t3227\tG\tA\t0\t1\t100%\t0\t1\t0\t54\t0.98\t0\t1\t0\t0\t1\t0\tA\n" | |
222 nbLine = 1 | |
223 launcher = VarscanFile() | |
224 launcher.setTypeOfVarscanFile("Varscan_2_2_8") | |
225 obsVarscanHit = launcher.createVarscanObjectFromLine(line, nbLine) | |
226 expVarscanHit = VarscanHit_v2_2_8() | |
227 expVarscanHit.setChrom('C11HBa0064J13_LR285') | |
228 expVarscanHit.setPosition('3227') | |
229 expVarscanHit.setRef('G') | |
230 expVarscanHit.setVar('A') | |
231 expVarscanHit.setCns('A') | |
232 self.assertEquals(expVarscanHit, obsVarscanHit) | |
233 | |
234 def test_createVarscanObjectFromLine_VarscanHit_v2_2_8_WithTag(self): | |
235 line = "C11HBa0064J13_LR285\t3227\tG\tA\t0\t1\t100%\t0\t1\t0\t54\t0.98\t0\t1\t0\t0\t1\t0\tA\tEspeceA\n" | |
236 nbLine = 1 | |
237 launcher = VarscanFile() | |
238 launcher.setTypeOfVarscanFile("Varscan_2_2_8_WithTag") | |
239 obsVarscanHit = launcher.createVarscanObjectFromLine(line, nbLine) | |
240 expVarscanHit = VarscanHit_v2_2_8_WithTag() | |
241 expVarscanHit.setChrom('C11HBa0064J13_LR285') | |
242 expVarscanHit.setPosition('3227') | |
243 expVarscanHit.setRef('G') | |
244 expVarscanHit.setVar('A') | |
245 expVarscanHit.setCns('A') | |
246 expVarscanHit.setTag('EspeceA') | |
247 self.assertEquals(expVarscanHit, obsVarscanHit) | |
248 | |
249 def _writeVarscanFile(self, varscanFileName): | |
250 varscanFile = open(varscanFileName, 'w') | |
251 varscanFile.write("Chrom\tPosition\tRef\tVar\tReads1\tReads2\tVarFreq\tStrands1\tStrands2\tQual1\tQual2\tPvalue\n") | |
252 varscanFile.write("C02HBa0291P19_LR48\t32\tC\tT\t1\t2\t66,67%\t1\t1\t37\t35\t0.3999999999999999\n") | |
253 varscanFile.write("C02HBa0291P19_LR48\t34\tA\tT\t1\t2\t66,67%\t1\t1\t40\t34\t0.3999999999999999\n") | |
254 varscanFile.close() | |
255 | |
256 def _writeVarscanFileWithoutHeader(self, varscanFileName): | |
257 varscanFile = open(varscanFileName, 'w') | |
258 varscanFile.write("C02HBa0291P19_LR48\t32\tC\tT\t1\t2\t66,67%\t1\t1\t37\t35\t0.3999999999999999\n") | |
259 varscanFile.write("C02HBa0291P19_LR48\t34\tA\tT\t1\t2\t66,67%\t1\t1\t40\t34\t0.3999999999999999\n") | |
260 varscanFile.close() | |
261 | |
262 def _writeVarscanFileWithTag(self, varscanFileName): | |
263 varscanFile = open(varscanFileName, 'w') | |
264 varscanFile.write("Chrom\tPosition\tRef\tVar\tReads1\tReads2\tVarFreq\tStrands1\tStrands2\tQual1\tQual2\tPvalue\n") | |
265 varscanFile.write("C02HBa0291P19_LR48\t32\tC\tT\t1\t2\t66,67%\t1\t1\t37\t35\t0.3999999999999999\tEspeceA\n") | |
266 varscanFile.write("C02HBa0291P19_LR48\t34\tA\tT\t1\t2\t66,67%\t1\t1\t40\t34\t0.3999999999999999\tEspeceA\n") | |
267 varscanFile.close() | |
268 | |
269 def _writeVarscanFile_v2_2_8(self, varscanFileName): | |
270 varscanFile = open(varscanFileName, 'w') | |
271 varscanFile.write("Chrom\tPosition\tRef\tCons\tReads1\tReads2\tVarFreq\tStrands1\tStrands2\tQual1\tQual2\tPvalue\tMapQual1\tMapQual2\tReads1Plus\tReads1Minus\tReads2Plus\tReads2Minus\tVarAllele\n") | |
272 varscanFile.write("C11HBa0064J13_LR285\t3227\tG\tA\t0\t1\t100%\t0\t1\t0\t54\t0.98\t0\t1\t0\t0\t1\t0\tA\n") | |
273 varscanFile.write("C11HBa0064J13_LR285\t3230\tG\tT\t0\t1\t100%\t0\t1\t0\t54\t0.98\t0\t1\t0\t0\t1\t0\tT\n") | |
274 varscanFile.close() | |
275 | |
276 def _writeOther(self, fileName): | |
277 file = open(fileName, 'w') | |
278 file.write('##gff-version 3\n') | |
279 file.write('chr16\tBlatToGff\tBES\t21686950\t21687294\t.\t+\t.\tID=MRRE1H001H13FM1;Name=MRRE1H001H13FM1;bes_start=21686950;bes_end=21687294;bes_size=22053297\n') | |
280 file.write('chr16\tBlatToGff\tBES\t21736364\t21737069\t.\t+\t.\tID=machin1;Name=machin1;bes_start=21736364;bes_end=21737069;bes_size=22053297\n') | |
281 file.write('chr11\tBlatToGff\tBES\t3725876\t3726473\t.\t+\t.\tID=MRRE1H032F08FM1;Name=MRRE1H032F08FM1;bes_start=3725876;bes_end=3726473;bes_size=19818926\n') | |
282 file.write('chr11\tBlatToGff\tBES\t3794984\t3795627\t.\t+\t.\tID=machin2;Name=machin2;bes_start=3794984;bes_end=3795627;bes_size=19818926\n') | |
283 file.write('chr18\tBlatToGff\tBES\t12067347\t12067719\t.\t+\t.\tID=machin3;Name=machin3;bes_start=12067347;bes_end=12067719;bes_size=29360087\n') | |
284 file.close() | |
285 | |
286 if __name__ == "__main__": | |
287 unittest.main() |