annotate commons/pyRepetUnit/convCoord/test/Test_PathChunkConnector.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents
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 time
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
3 import os
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
4 from commons.core.sql.TablePathAdaptator import TablePathAdaptator
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
5 from commons.pyRepetUnit.convCoord.PathChunkConnector import PathChunkConnector
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
6 from commons.core.sql.DbMySql import DbMySql
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
7
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
8
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
9 class Test_PathChunkConnector( unittest.TestCase ):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
10
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
11 def setUp(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
12 self._uniqId = "%s_%s" % ( time.strftime("%Y%m%d%H%M%S") , os.getpid() )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
13 self._configFileName = "dummyConfigFile_%s" % ( self._uniqId )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
14 configF = open(self._configFileName, "w" )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
15 configF.write( "[repet_env]\n" )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
16 configF.write( "repet_host: %s\n" % ( os.environ["REPET_HOST"] ) )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
17 configF.write( "repet_user: %s\n" % ( os.environ["REPET_USER"] ) )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
18 configF.write( "repet_pw: %s\n" % ( os.environ["REPET_PW"] ) )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
19 configF.write( "repet_db: %s\n" % ( os.environ["REPET_DB"] ) )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
20 configF.write( "repet_port: %s\n" % ( os.environ["REPET_PORT"] ) )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
21 configF.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
22 self._db = DbMySql( cfgFileName=self._configFileName )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
23 self._table = "dummyPathTable_%s" % ( self._uniqId )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
24 self._tpA = TablePathAdaptator( self._db, self._table )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
25 self._mapDict = {'chunk1': ('dmel_chr4', 1, 100000),'chunk2': ('dmel_chr4', 90001, 190000),'chunk3': ('dmel_chr4', 180001, 280000), }
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
26 self._mapFileName = "map_file.map"
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 self._db.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
30
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
31 def testTwoQueryOverlapsOnPlusStrand (self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
32 lines = [
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
33 "1\tdmel_chr4\t95535\t95570\tsbj2\t125423\t125467\t7e-15\t82\t97.78\n",
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34 "2\tdmel_chr4\t95545\t95576\tsbj2\t125457\t133465\t2e-38\t83\t65\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 ]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 expectedList =[("dmel_chr4", 95535, 95576, "sbj2", 125423, 133465, 2e-38, 83,97.78)]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 self._templateTest(lines, expectedList)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 def testTwoQueryOverlapsOnReverseStrand (self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41 lines = [
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42 "1\tdmel_chr4\t95535\t95570\tsbj2\t125467\t125423\t7e-15\t82\t97.78\n",
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 "2\tdmel_chr4\t95545\t95576\tsbj2\t133465\t125457\t2e-38\t83\t65\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44 ]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46 expectedList =[("dmel_chr4", 95535, 95576, "sbj2", 133465, 125423, 2e-38, 83,97.78)]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47 self._templateTest(lines, expectedList)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49 def testTwoQueryOverlapsOnDifferentStrands (self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50 lines = [
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51 "1\tdmel_chr4\t95535\t95570\tsbj2\t125423\t125467\t7e-15\t82\t97.78\n",
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52 "2\tdmel_chr4\t95545\t95576\tsbj2\t133465\t125457\t2e-38\t83\t65\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53 ]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 expectedList =[
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55 ("dmel_chr4", 95535, 95570, "sbj2", 125423, 125467, 7e-15, 82,97.78),
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56 ("dmel_chr4", 95545, 95576, "sbj2", 133465, 125457, 2e-38, 83,65)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57 ]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58 self._templateTest(lines, expectedList)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60 def _templateTest(self, datas2TestList, expectedList ):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61 pathFileName = "dummyPathFile_%s" % ( self._uniqId )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
62 _MockPathFile(pathFileName, datas2TestList)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
65 self._db.createTable( self._table, "path", pathFileName )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
66
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
67 _MockMapFile(self._mapFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68 chunkConnector = PathChunkConnector(self._mapFileName, self._db, self._table, 0)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69 chunkConnector.run()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
70
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
71 sql_cmd = 'select * from %s' % (self._table)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
72 self._db.execute(sql_cmd)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 res = self._db.fetchall()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75 for i in xrange(len(expectedList)):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76 resultTuple = res[i]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77 expectedTuple = expectedList[i]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 self._assertExpectedTupleEqualsObsTuple(expectedTuple, resultTuple)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80 self._db.dropTable( self._table )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81 os.remove(pathFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82 os.remove(self._mapFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84 def _assertExpectedTupleEqualsObsTuple(self, expectedTuple, resultTuple):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
85 self.assertEquals(expectedTuple[0], resultTuple[1])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
86 self.assertEquals(expectedTuple[1], resultTuple[2])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
87 self.assertEquals(expectedTuple[2], resultTuple[3])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
88 self.assertEquals(expectedTuple[3], resultTuple[4])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
89 self.assertEquals(expectedTuple[4], resultTuple[5])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
90 self.assertEquals(expectedTuple[5], resultTuple[6])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
91 self.assertEquals(expectedTuple[6], resultTuple[7])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
92 self.assertEquals(expectedTuple[7], resultTuple[8])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
93 self.assertEquals(expectedTuple[8], resultTuple[9])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
94
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
95
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
96 class _MockPathFile:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
97
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
98 def __init__(self, fileName, lines):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
99 path = open(fileName, "w");
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
100 for line in lines:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
101 path.write(line)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
102 path.close
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
103
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
104
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
105 class _MockMapFile:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
106
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
107 def __init__ (self, fileName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
108 map = open(fileName, "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
109 line1 = "chunk1" + '\t' + "dmel_chr4" + '\t' + "1" + '\t'+ "100000" + "\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
110 line2 = "chunk2" + '\t' + "dmel_chr4" + '\t' + "90001" + '\t'+ "190000" + "\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
111 line3 = "chunk3" + '\t' + "dmel_chr4" + '\t' + "180001" + '\t' + "280000" + "\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
112 map.write(line1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
113 map.write(line2)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
114 map.write(line3)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
115 map.close
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
116
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
117
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
118 test_suite = unittest.TestSuite()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
119 test_suite.addTest( unittest.makeSuite( Test_PathChunkConnector ) )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
120 if __name__ == "__main__":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
121 unittest.TextTestRunner(verbosity=2).run( test_suite )