comparison commons/core/coord/test/Test_MapUtils.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 # Copyright INRA (Institut National de la Recherche Agronomique)
2 # http://www.inra.fr
3 # http://urgi.versailles.inra.fr
4 #
5 # This software is governed by the CeCILL license under French law and
6 # abiding by the rules of distribution of free software. You can use,
7 # modify and/ or redistribute the software under the terms of the CeCILL
8 # license as circulated by CEA, CNRS and INRIA at the following URL
9 # "http://www.cecill.info".
10 #
11 # As a counterpart to the access to the source code and rights to copy,
12 # modify and redistribute granted by the license, users are provided only
13 # with a limited warranty and the software's author, the holder of the
14 # economic rights, and the successive licensors have only limited
15 # liability.
16 #
17 # In this respect, the user's attention is drawn to the risks associated
18 # with loading, using, modifying and/or developing or reproducing the
19 # software by the user in light of its specific status of free software,
20 # that may mean that it is complicated to manipulate, and that also
21 # therefore means that it is reserved for developers and experienced
22 # professionals having in-depth computer knowledge. Users are therefore
23 # encouraged to load and test the software's suitability as regards their
24 # requirements in conditions enabling the security of their systems and/or
25 # data to be ensured and, more generally, to use and operate it in the
26 # same conditions as regards security.
27 #
28 # The fact that you are presently reading this means that you have had
29 # knowledge of the CeCILL license and that you accept its terms.
30
31
32 import unittest
33 import os
34 import sys
35 from commons.core.coord.MapUtils import MapUtils
36 from commons.core.coord.Map import Map
37 from commons.core.coord.Set import Set
38 from commons.core.utils.FileUtils import FileUtils
39
40
41 class Test_MapUtils( unittest.TestCase ):
42
43 def test_getMapListSortedByIncreasingMinThenMax( self ):
44 iMap1 = Map("name1", "chr1", 1, 350)
45 iMap2 = Map("name2", "chr1", 1, 100)
46 iMap3 = Map("name3", "chr1", 50, 350)
47 iMap4 = Map("name4", "chr1", 5, 450)
48 lMaps = [ iMap1, iMap2, iMap3, iMap4 ]
49
50 expLMaps = [ iMap2, iMap1, iMap4, iMap3 ]
51
52 obsLMaps = MapUtils.getMapListSortedByIncreasingMinThenMax( lMaps )
53
54 self.assertEquals( expLMaps, obsLMaps )
55
56
57 def test_getMapListSortedByIncreasingMinThenMax_ordered( self ):
58 iMap1 = Map("name1", "chr1", 1, 100)
59 iMap2 = Map("name2", "chr1", 1, 350)
60
61 lMaps = [ iMap1, iMap2 ]
62 expLMaps = [ iMap1, iMap2 ]
63
64 obsLMaps = MapUtils.getMapListSortedByIncreasingMinThenMax( lMaps )
65
66 self.assertEquals( expLMaps, obsLMaps )
67
68
69 def test_getMapListSortedByIncreasingMinThenMax_unordered( self ):
70 iMap1 = Map("name1", "chr1", 1, 350)
71 iMap2 = Map("name2", "chr1", 1, 100)
72
73 lMaps = [ iMap1, iMap2 ]
74 expLMaps = [ iMap2, iMap1 ]
75
76 obsLMaps = MapUtils.getMapListSortedByIncreasingMinThenMax( lMaps )
77
78 self.assertEquals( expLMaps, obsLMaps )
79
80
81 def test_getMapListSortedByIncreasingMinThenMax_nonOverlapping( self ):
82 iMap1 = Map("name1", "chr1", 1, 350)
83 iMap2 = Map("name2", "chr1", 400, 600)
84
85 lMaps = [ iMap2, iMap1 ]
86 expLMaps = [ iMap1, iMap2 ]
87
88 obsLMaps = MapUtils.getMapListSortedByIncreasingMinThenMax( lMaps )
89
90 self.assertEquals( expLMaps, obsLMaps )
91
92
93 def test_getMapListSortedByIncreasingMinThenMax_sameMinThreeMaps( self ):
94 iMap1 = Map("name1", "chr1", 350, 1)
95 iMap2 = Map("name2", "chr1", 400, 1)
96 iMap3 = Map("name3", "chr1", 500, 1)
97
98 lMaps = [ iMap2, iMap1, iMap3 ]
99 expLMaps = [ iMap1, iMap2, iMap3 ]
100
101 obsLMaps = MapUtils.getMapListSortedByIncreasingMinThenMax( lMaps )
102
103 self.assertEquals( expLMaps, obsLMaps )
104
105
106 def test_getMapListSortedByIncreasingMinThenMax_included( self ):
107 iMap1 = Map("name1", "chr1", 350, 1)
108 iMap2 = Map("name2", "chr1", 300, 10)
109
110 lMaps = [ iMap2, iMap1 ]
111 expLMaps = [ iMap1, iMap2]
112
113 obsLMaps = MapUtils.getMapListSortedByIncreasingMinThenMax( lMaps )
114
115 self.assertEquals( expLMaps, obsLMaps )
116
117
118 def test_getMapListSortedByIncreasingMinThenMax_equal( self ):
119 iMap1 = Map("name1", "chr1", 350, 1)
120 iMap2 = Map("name2", "chr1", 350, 1)
121
122 lMaps = [ iMap2, iMap1 ]
123 expLMaps = [ iMap2, iMap1 ]
124 notExpLMaps = [ iMap1, iMap2 ]
125
126 obsLMaps = MapUtils.getMapListSortedByIncreasingMinThenMax( lMaps )
127
128 self.assertEquals( expLMaps, obsLMaps )
129 self.assertNotEquals( notExpLMaps, obsLMaps )
130
131
132 def test_getMapListSortedByIncreasingNameThenSeqnameThenMinThenMax( self ):
133 iMap1 = Map("name1", "chr1", 1, 350)
134 iMap2 = Map("name4", "chr2", 5, 450)
135 iMap3 = Map("name4", "chr1", 10, 450)
136 iMap4 = Map("name4", "chr1", 10, 500)
137 iMap5 = Map("name4", "chr1", 5, 450)
138 iMap6 = Map("name3", "chr1", 50, 350)
139 iMap7 = Map("name2", "chr1", 1, 100)
140
141 lMaps = [ iMap1, iMap2, iMap3, iMap4, iMap5, iMap6, iMap7 ]
142
143 expLMaps = [ iMap1, iMap7, iMap6, iMap5, iMap3, iMap4, iMap2 ]
144
145 obsLMaps = MapUtils.getMapListSortedByIncreasingNameThenSeqnameThenMinThenMax( lMaps )
146
147 self.assertEquals( expLMaps, obsLMaps )
148
149
150 def test_getMapListSortedByIncreasingNameThenSeqnameThenMinThenMax_ordered( self ):
151 iMap1 = Map("name1", "chr1", 1, 100)
152 iMap2 = Map("name1", "chr2", 1, 350)
153
154 lMaps = [ iMap1, iMap2 ]
155 expLMaps = [ iMap1, iMap2 ]
156
157 obsLMaps = MapUtils.getMapListSortedByIncreasingNameThenSeqnameThenMinThenMax( lMaps )
158
159 self.assertEquals( expLMaps, obsLMaps )
160
161
162 def test_getMapListSortedByIncreasingNameThenSeqnameThenMinThenMax_unordered( self ):
163 iMap1 = Map("name1", "chr2", 1, 350)
164 iMap2 = Map("name1", "chr1", 1, 100)
165
166 lMaps = [ iMap1, iMap2 ]
167 expLMaps = [ iMap2, iMap1 ]
168
169 obsLMaps = MapUtils.getMapListSortedByIncreasingNameThenSeqnameThenMinThenMax( lMaps )
170
171 self.assertEquals( expLMaps, obsLMaps )
172
173
174 def test_getMapListSortedByIncreasingNameThenSeqnameThenMinThenMax_nonOverlapping( self ):
175 iMap1 = Map("name1", "chr1", 1, 350)
176 iMap2 = Map("name2", "chr1", 400, 600)
177
178 lMaps = [ iMap2, iMap1 ]
179 expLMaps = [ iMap1, iMap2 ]
180
181 obsLMaps = MapUtils.getMapListSortedByIncreasingNameThenSeqnameThenMinThenMax( lMaps )
182
183 self.assertEquals( expLMaps, obsLMaps )
184
185
186 def test_getMapListSortedByIncreasingNameThenSeqnameThenMinThenMax_sameMinThreeMaps( self ):
187 iMap1 = Map("name2", "chr1", 150, 10)
188 iMap2 = Map("name2", "chr1", 1, 100)
189 iMap3 = Map("name2", "chr1", 100, 1)
190
191 lMaps = [ iMap1, iMap2, iMap3 ]
192 expLMaps = [ iMap2, iMap3, iMap1 ]
193
194 obsLMaps = MapUtils.getMapListSortedByIncreasingNameThenSeqnameThenMinThenMax( lMaps )
195
196 self.assertEquals( expLMaps, obsLMaps )
197
198
199 def test_getDictPerNameFromMapFile( self ):
200 iMap1 = Map( "chunk1", "chromosome1", 1, 100 )
201 iMap2 = Map( "chunk2", "chromosome1", 91, 190 )
202 iMap3 = Map( "chunk3", "chromosome2", 1, 100 )
203 iMap4 = Map( "chunk1", "chromosome1", 1, 100 ) # redundant with iMap1
204
205 mapFile = "dummyFile.map"
206 mapFileHandler = open( mapFile, "w" )
207 for iMap in [ iMap1, iMap2, iMap3, iMap4 ]:
208 iMap.write( mapFileHandler )
209 mapFileHandler.close()
210
211 dExp = { "chunk1": iMap1, "chunk2": iMap2, "chunk3": iMap3 }
212
213 dObs = MapUtils.getDictPerNameFromMapFile( mapFile )
214
215 self.assertEquals( dExp, dObs )
216
217 os.remove( mapFile )
218
219
220 def test_mapList2SetList(self):
221 map1 = Map( "name1", "desc1", 1, 120 )
222 map2 = Map( "name2", "desc2", 1, 20 )
223 lMap = [ map1, map2 ]
224 set1 = Set( 1, "name1", "desc1", 1, 120 )
225 set2 = Set( 2, "name2", "desc2", 1, 20 )
226 explMapSet = [ set1, set2 ]
227 obslMapSet = MapUtils.mapList2SetList( lMap )
228
229 self.assertEquals( explMapSet, obslMapSet )
230
231
232 def test_mergeCoordsInFile( self ):
233 if sys.modules.has_key( "commons.core.checker.CheckerUtils" ):
234 inFile = "dummyInFile"
235 inFileHandler = open( inFile, "w" )
236 inFileHandler.write( "TE1\tchr1\t1501\t2500\n" )
237 inFileHandler.write( "TE3\tchr1\t4000\t3401\n" )
238 inFileHandler.write( "TE3\tchr1\t3800\t3601\n" )
239 inFileHandler.close()
240 expFile = "dummyExpFile"
241 expFileHandler = open( expFile, "w" )
242 expFileHandler.write( "TE1\tchr1\t1501\t2500\n" )
243 expFileHandler.write( "TE3\tchr1\t4000\t3401\n" )
244 expFileHandler.close()
245 obsFile = "dummyObsFile"
246 MapUtils.mergeCoordsInFile( inFile, obsFile )
247 self.assertTrue( FileUtils.are2FilesIdentical( expFile, obsFile ) )
248 for f in [ inFile, expFile, obsFile ]:
249 os.remove( f )
250
251
252 def test_getDictPerSeqNameFromMapFile( self ):
253 inFile = "dummyInFile"
254 inFileHandler = open( inFile, "w" )
255 inFileHandler.write( "TE1\tchr1\t1\t10\n" )
256 inFileHandler.write( "TE2\tchr1\t60\t41\n" )
257 inFileHandler.write( "TE3\tchr2\t5\t36\n" )
258 inFileHandler.close()
259 dExp = { "chr1": [ Map( "TE1", "chr1", 1, 10 ), Map( "TE2", "chr1", 60, 41 ) ],
260 "chr2": [ Map( "TE3", "chr2", 5, 36 ) ] }
261 dObs = MapUtils.getDictPerSeqNameFromMapFile( inFile )
262 self.assertEqual( dExp, dObs )
263 os.remove( inFile )
264
265 def test_convertMapFileIntoSetFile(self):
266 mapInputFile = "dummyExpFile"
267 mapFileHandler = open( mapInputFile, "w" )
268 mapFileHandler.write( "seq31\tchr1\t151\t250\n" )
269 mapFileHandler.write( "seq27\tchr2\t301\t500\n" )
270 mapFileHandler.write( "seq40\tchr2\t600\t700\n" )
271 mapFileHandler.write( "seq2\tchr3\t301\t500\n" )
272 mapFileHandler.close()
273
274 expSetFile = "dummyexpSetFile"
275 expSetFileHandler = open( expSetFile, "w" )
276 expSetFileHandler.write( "1\tseq31\tchr1\t151\t250\n" )
277 expSetFileHandler.write( "2\tseq27\tchr2\t301\t500\n" )
278 expSetFileHandler.write( "3\tseq40\tchr2\t600\t700\n" )
279 expSetFileHandler.write( "4\tseq2\tchr3\t301\t500\n" )
280 expSetFileHandler.close()
281
282 obsFile = "dummyObsFile"
283
284 MapUtils.convertMapFileIntoSetFile( mapInputFile, obsFile )
285
286 self.assertTrue( FileUtils.are2FilesIdentical( expSetFile, obsFile ) )
287
288 for f in [ expSetFile, mapInputFile, obsFile ]:
289 os.remove( f )
290
291 def test_convertMapFileIntoSetFile_one_line(self):
292 mapInputFile = "dummyExpFile"
293 mapFileHandler = open( mapInputFile, "w" )
294 mapFileHandler.write( "seq31\tchr1\t151\t250\n" )
295 mapFileHandler.close()
296
297 expSetFile = "dummyexpSetFile"
298 expSetFileHandler = open( expSetFile, "w" )
299 expSetFileHandler.write( "1\tseq31\tchr1\t151\t250\n" )
300 expSetFileHandler.close()
301
302 obsFile = "dummyObsFile"
303
304 MapUtils.convertMapFileIntoSetFile( mapInputFile, obsFile )
305
306 self.assertTrue( FileUtils.are2FilesIdentical( expSetFile, obsFile ) )
307
308 for f in [ expSetFile, mapInputFile, obsFile ]:
309 os.remove( f )
310
311 def test_convertMapFileIntoSetFile_empty_file(self):
312 mapInputFile = "dummyFile.map"
313 mapFileHandler = open( mapInputFile, "w" )
314 mapFileHandler.close()
315
316 expFile = "dummyExpFile.map.set"
317 expFileHandler = open( expFile, "w" )
318 expFileHandler.close()
319
320 obsFile = "dummyFile.map.set"
321
322 MapUtils.convertMapFileIntoSetFile( mapInputFile )
323
324 self.assertTrue( FileUtils.are2FilesIdentical( expFile, obsFile ) )
325
326 for f in [ expFile, mapInputFile, obsFile ]:
327 os.remove( f )
328
329 def test_writeListInFile_empty_list(self):
330 lMaps = [ ]
331 expFileName = "expFileName"
332 fileHandle = open(expFileName, "w")
333 fileHandle.close()
334
335 obsFileName = "obsFileName"
336 fileHandle = open(obsFileName, "w")
337 MapUtils.writeListInFile(lMaps, obsFileName, "w")
338 fileHandle.close()
339
340 self.assertTrue( FileUtils.are2FilesIdentical( expFileName, obsFileName ) )
341
342 os.remove(obsFileName)
343 os.remove(expFileName)
344
345 def test_writeListInFile_list_one_set(self):
346 lMaps = [ Map( "map1", "map1seq", 1, 10 ) ]
347 line = "map1\tmap1seq\t1\t10\n"
348
349 expFileName = "expFileName"
350
351 fileHandle = open(expFileName, "w")
352 fileHandle.write(line)
353 fileHandle.close()
354
355 obsFileName = "obsFileName"
356 fileHandle = open(obsFileName, "w")
357 MapUtils.writeListInFile(lMaps, obsFileName, "w")
358 fileHandle.close()
359
360 self.assertTrue( FileUtils.are2FilesIdentical( expFileName, obsFileName ) )
361
362 os.remove(obsFileName)
363 os.remove(expFileName)
364
365 def test_getMinLengthOfMapFile(self):
366 mapFileName = "%s/Gnome_tools/Vein_v4_scaffold_00001.fa.Nstretch.map" % os.environ["REPET_DATA"]
367 expMinLengthofMapFile = 20
368 iMap = MapUtils()
369 obsMinLengthofMapFile = iMap.getMinLengthOfMapFile(mapFileName)
370 self.assertEquals(expMinLengthofMapFile, obsMinLengthofMapFile)
371
372 def test_getMaxLengthOfMapFile(self):
373 mapFileName = "%s/Gnome_tools/Vein_v4_scaffold_00001.fa.Nstretch.map" % os.environ["REPET_DATA"]
374 expMinLengthofMapFile = 6344
375 iMap = MapUtils()
376 obsMinLengthofMapFile = iMap.getMaxLengthOfMapFile(mapFileName)
377 self.assertEquals(expMinLengthofMapFile, obsMinLengthofMapFile)
378
379
380
381 test_suite = unittest.TestSuite()
382 test_suite.addTest( unittest.makeSuite( Test_MapUtils ) )
383 if __name__ == "__main__":
384 unittest.TextTestRunner(verbosity=2).run( test_suite )