comparison commons/core/coord/test/Test_Map.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 from commons.core.coord.Map import Map
35 from commons.core.utils.FileUtils import FileUtils
36
37
38 class Test_Map( unittest.TestCase ):
39
40 def setUp(self):
41 self._map = Map()
42
43 def test_setFromString(self):
44 line = "MbQ12Gr2Cl2\tconsensus1\t51\t1230\n" # test with '\t' separator
45 self._map.setFromString(line)
46 self.assertEqual( self._map.name, "MbQ12Gr2Cl2" )
47 self.assertEqual( self._map.seqname, "consensus1" )
48 self.assertEqual( self._map.start, 51 )
49 self.assertEqual( self._map.end, 1230 )
50 line = "MbQ12Gr2Cl2;consensus1;51;1230" # test with ';' separator
51 self._map.setFromString(line,";")
52 self.assertEqual( self._map.name, "MbQ12Gr2Cl2" )
53 self.assertEqual( self._map.seqname, "consensus1" )
54 self.assertEqual( self._map.start, 51 )
55 self.assertEqual( self._map.end, 1230 )
56
57 def test___eq__(self):
58 self._map.setFromString( "MbQ12Gr2Cl2\tconsensus1\t51\t1230\n" )
59 o = Map()
60 o.setFromString( "MbQ12Gr2Cl2\tconsensus1\t51\t1230\n" )
61 self.assertEqual( self._map, o ) # same data
62 o.setFromString( "MbQ12Gr2Cl1\tconsensus1\t51\t1230\n" )
63 self.assertNotEqual( self._map, o ) # different name
64 o.setFromString( "MbQ12Gr2Cl2\tconsensus2\t51\t1230\n" )
65 self.assertNotEqual( self._map, o ) # different seqname
66 o.setFromString( "MbQ12Gr2Cl2\tconsensus1\t15\t1230\n" )
67 self.assertNotEqual( self._map, o ) # different start
68 o.setFromString( "MbQ12Gr2Cl2\tconsensus1\t51\t123000\n" )
69 self.assertNotEqual( self._map, o ) # different end
70 o.setFromString( "MbQ12Gr2Cl2\tconsensus1\t1230\t51\n" )
71 self.assertNotEqual( self._map, o ) # same start/end but in different order
72
73 def test_setFromTuple(self):
74 tuple = ("MbQ12Gr2Cl2", "consensus1","51","1230")
75 self._map.setFromTuple(tuple)
76
77 expMap = Map("MbQ12Gr2Cl2", "consensus1",51,1230)
78 obsMap = self._map
79
80 self.assertEquals(expMap, obsMap)
81
82 def test_read_empty_file(self):
83
84 fileName = "dummyFile"
85 os.system("touch " + fileName)
86 fileHandle = open(fileName, "r")
87
88 obsResult = self._map.read(fileHandle)
89 expResult = 0
90
91 fileHandle.close()
92 os.remove(fileName)
93
94 self.assertEquals(expResult, obsResult)
95
96 def test_read_uncompleted_line( self):
97 uncompletedLine = "MbQ12Gr2Cl2\tconsensus1\t51"
98 fileName = "dummyFile"
99
100 fileHandle = open(fileName, "w")
101 fileHandle.write(uncompletedLine)
102 fileHandle.close()
103
104 fileHandle = open(fileName, "r")
105
106 obsResult = self._map.read(fileHandle)
107 expResult = 0
108
109 fileHandle.close()
110 os.remove(fileName)
111
112 self.assertEquals(obsResult, expResult)
113
114 def test_read(self):
115 line = "MbQ12Gr2Cl2\tconsensus1\t51\t1230\n"
116 fileName = "dummyFile"
117
118 fileHandle = open(fileName, "w")
119 fileHandle.write(line)
120 fileHandle.close()
121
122 fileHandle = open(fileName, "r")
123 self._map.read(fileHandle)
124 obsResult = self._map
125
126 expResult = Map()
127 expResult.setFromString(line)
128
129 fileHandle.close()
130 os.remove(fileName)
131
132 self.assertEquals(obsResult, expResult)
133
134 def test_write(self):
135 line = "MbQ12Gr2Cl2\tconsensus1\t51\t1230\n"
136 expFileName = "expFileName"
137
138 fileHandle = open(expFileName, "w")
139 fileHandle.write(line)
140 fileHandle.close()
141
142 obsFileName = "obsFileName"
143 fileHandle = open(obsFileName, "w")
144 self._map.setFromString(line)
145 self._map.write(fileHandle)
146 fileHandle.close()
147
148 self.assertTrue( FileUtils.are2FilesIdentical( expFileName, obsFileName ) )
149
150 os.remove(obsFileName)
151 os.remove(expFileName)
152
153 def test_diff1(self):
154 map1 = Map("seq1","DmelChr4", 190000, 390000)
155 map2 = Map("seq2","DmelChr4", 290000, 590000)
156
157 expMap1 = Map("seq1", "DmelChr4", 190000, 289999)
158 expReturnedMap = Map()
159
160 obsReturnedMap = map1.diff(map2)
161 obsMap1 = map1
162
163 self.assertEquals(expMap1, obsMap1)
164 self.assertEquals(expReturnedMap, obsReturnedMap)
165
166 def test_diff2(self):
167 map1 = Map("seq1","DmelChr4", 190000, 590000)
168 map2 = Map("seq2","DmelChr4", 290000, 390000)
169
170 expMap1 = Map("seq1", "DmelChr4", 190000, 289999)
171 expReturnedMap = Map("seq1", "DmelChr4", 390001, 590000)
172
173 obsReturnedMap = map1.diff(map2)
174 obsMap1 = map1
175
176 self.assertEquals(expMap1, obsMap1)
177 self.assertEquals(expReturnedMap, obsReturnedMap)
178
179
180 test_suite = unittest.TestSuite()
181 test_suite.addTest( unittest.makeSuite( Test_Map ) )
182 if __name__ == "__main__":
183 unittest.TextTestRunner(verbosity=2).run( test_suite )