Mercurial > repos > yufei-luo > s_mart
comparison commons/tools/tests/Test_F_GameXmlMaker.py @ 18:94ab73e8a190
Uploaded
author | m-zytnicki |
---|---|
date | Mon, 29 Apr 2013 03:20:15 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
17:b0e8584489e6 | 18:94ab73e8a190 |
---|---|
1 import unittest | |
2 import os | |
3 import glob | |
4 from commons.core.utils.FileUtils import FileUtils | |
5 from commons.core.sql.DbFactory import DbFactory | |
6 from commons.tools.GameXmlMaker import GameXmlMaker | |
7 | |
8 | |
9 class Test_F_GameXmlMaker(unittest.TestCase): | |
10 | |
11 def setUp(self): | |
12 pass | |
13 | |
14 | |
15 def tearDown(self): | |
16 FileUtils.removeFilesByPattern("*gamexml") | |
17 | |
18 | |
19 def test_run_as_script_step1(self): | |
20 fastaFileName = "input.fa" | |
21 self._writeFastaFile(fastaFileName) | |
22 | |
23 cmd = "GameXmlMaker.py -f %s " % fastaFileName | |
24 os.system(cmd) | |
25 | |
26 lObsGameXmlNew = glob.glob("*.gamexml") | |
27 self._writeStep1ExpectedXmls() | |
28 self.assertTrue(len(lObsGameXmlNew) > 0) | |
29 for obsGameXmlNew in lObsGameXmlNew: | |
30 expGameXmlNew = "exp_%s" % obsGameXmlNew | |
31 self.assertTrue(FileUtils.are2FilesIdentical(expGameXmlNew,obsGameXmlNew)) | |
32 | |
33 os.remove(fastaFileName) | |
34 | |
35 def test_run_as_script_step2(self): | |
36 self._tableFileName = "annotation_tables.txt" | |
37 | |
38 self._writeStep2InputTableFile() | |
39 self._writeStep2InputXmls() | |
40 self._writeStep2InputPathFile() | |
41 self._writeStep2InputSetFile() | |
42 self._writeStep2InputMapFile() | |
43 self._createStep2Tables() | |
44 | |
45 lObsGameXmlNew = glob.glob("*.gamexml") | |
46 | |
47 cmd = "GameXmlMaker.py -t %s " % self._tableFileName | |
48 os.system(cmd) | |
49 | |
50 self._writeStep2ExpXmls() | |
51 | |
52 self.assertTrue(len(lObsGameXmlNew) > 0) | |
53 for obsGameXmlNew in lObsGameXmlNew: | |
54 expGameXmlNew = "exp_%s" % obsGameXmlNew | |
55 self.assertTrue(FileUtils.are2FilesIdentical(expGameXmlNew,obsGameXmlNew)) | |
56 | |
57 os.remove("Test_F_GameXmlMaker_path.path") | |
58 os.remove("Test_F_GameXmlMaker_set.set") | |
59 os.remove("Test_F_GameXmlMaker_chk_map.map") | |
60 | |
61 os.remove(self._tableFileName) | |
62 os.system("ListAndDropTables.py -d Test_F_GameXmlMaker") | |
63 | |
64 def test_run_step2(self): | |
65 self._tableFileName = "annotation_tables.txt" | |
66 | |
67 self._writeStep2InputTableFile() | |
68 self._writeStep2InputXmls() | |
69 self._writeStep2InputPathFile() | |
70 self._writeStep2InputSetFile() | |
71 self._writeStep2InputMapFile() | |
72 self._createStep2Tables() | |
73 | |
74 lObsGameXmlNew = glob.glob("*.gamexml") | |
75 | |
76 iGameXmlMaker = GameXmlMaker(tablesFileName = self._tableFileName) | |
77 iGameXmlMaker.run() | |
78 self._writeStep2ExpXmls() | |
79 self.assertTrue(len(lObsGameXmlNew) > 0) | |
80 for obsGameXmlNew in lObsGameXmlNew: | |
81 expGameXmlNew = "exp_%s" % obsGameXmlNew | |
82 self.assertTrue(FileUtils.are2FilesIdentical(expGameXmlNew,obsGameXmlNew)) | |
83 | |
84 os.remove("Test_F_GameXmlMaker_path.path") | |
85 os.remove("Test_F_GameXmlMaker_set.set") | |
86 os.remove("Test_F_GameXmlMaker_chk_map.map") | |
87 | |
88 os.remove(self._tableFileName) | |
89 os.system("ListAndDropTables.py -d Test_F_GameXmlMaker") | |
90 | |
91 def _writeFastaFile(self, inFileName): | |
92 f = open(inFileName,'w') | |
93 f.write('>seq1\n') | |
94 f.write('TGTTCATATTCATAGGATGGAGCTAGTAAGCGATGTCGGCTTAGCTCATCCACATGAATG\n') | |
95 f.write('CAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACACGAGCTGGGTTAAAT\n') | |
96 f.write('CAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACA\n') | |
97 f.write('>seq2\n') | |
98 f.write('TGTTCATATTCATAGGATGGAGCTAGTAAGCGATGTCGGCTTAGCTCATCCACATGAATG\n') | |
99 f.write('CAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACACGAGCTGGGTTAAAT\n') | |
100 f.write('CAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACACGAGCTGGGTTAAAT\n') | |
101 f.write('>seq3\n') | |
102 f.write('CCTAGACAATTAATTATAATAATTAATAAACTATTAGGCTAGTAGTAGGTAATAATAAAA\n') | |
103 f.write('GGATTACTACTAAGCTGCGCTATGTAGATATTTAAAACATGTGGCTTAGGCAAGAGTATA\n') | |
104 f.write('>seq4\n') | |
105 f.write('TGTTCATATTCATAGGATGGAGCTAGTAAGCGATGTCGGCTTAGCTCATCCACATGAATG\n') | |
106 f.write('CAGGAATCATGAAGGGTACGACTGTTCG\n') | |
107 f.close() | |
108 | |
109 def _writeStep1ExpectedXmls(self): | |
110 f = open("exp_seq1.gamexml", 'w') | |
111 f.write('<?xml version="1.0" ?><game><seq focus="true" id="seq1"><name>seq1</name>') | |
112 f.write('<residues>TGTTCATATTCATAGGATGGAGCTAGTAAGCGATGTCGGCTTAGCTCATCCACATGAATGCAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACACGAGCTGGGTTAAATCAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACA</residues>') | |
113 f.write('</seq><map_position><arm>seq1</arm><span><start>1</start><end>165</end></span></map_position></game>') | |
114 f.close() | |
115 f = open("exp_seq2.gamexml", 'w') | |
116 f.write('<?xml version="1.0" ?><game><seq focus="true" id="seq2"><name>seq2</name>') | |
117 f.write('<residues>TGTTCATATTCATAGGATGGAGCTAGTAAGCGATGTCGGCTTAGCTCATCCACATGAATGCAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACACGAGCTGGGTTAAATCAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACACGAGCTGGGTTAAAT</residues>') | |
118 f.write('</seq><map_position><arm>seq2</arm><span><start>1</start><end>180</end></span></map_position></game>') | |
119 f.close() | |
120 f = open("exp_seq3.gamexml", 'w') | |
121 f.write('<?xml version="1.0" ?><game><seq focus="true" id="seq3"><name>seq3</name>') | |
122 f.write('<residues>CCTAGACAATTAATTATAATAATTAATAAACTATTAGGCTAGTAGTAGGTAATAATAAAAGGATTACTACTAAGCTGCGCTATGTAGATATTTAAAACATGTGGCTTAGGCAAGAGTATA</residues>') | |
123 f.write('</seq><map_position><arm>seq3</arm><span><start>1</start><end>120</end></span></map_position></game>') | |
124 f.close() | |
125 f = open("exp_seq4.gamexml", 'w') | |
126 f.write('<?xml version="1.0" ?><game><seq focus="true" id="seq4"><name>seq4</name>') | |
127 f.write('<residues>TGTTCATATTCATAGGATGGAGCTAGTAAGCGATGTCGGCTTAGCTCATCCACATGAATGCAGGAATCATGAAGGGTACGACTGTTCG</residues>') | |
128 f.write('</seq><map_position><arm>seq4</arm><span><start>1</start><end>88</end></span></map_position></game>') | |
129 f.close() | |
130 | |
131 def _createStep2Tables(self): | |
132 iDb = DbFactory.createInstance() | |
133 iDb.createTable("Test_F_GameXmlMaker_set", "set", "Test_F_GameXmlMaker_set.set", True) | |
134 iDb.createTable("Test_F_GameXmlMaker_chk_map", "map", "Test_F_GameXmlMaker_chk_map.map", True) | |
135 iDb.createTable("Test_F_GameXmlMaker_path", "path", "Test_F_GameXmlMaker_path.path", True) | |
136 iDb.close() | |
137 | |
138 def _writeStep2InputXmls(self): | |
139 f = open("seq1.gamexml", 'w') | |
140 f.write('<?xml version="1.0" ?><game><seq focus="true" id="seq1"><name>seq1</name>') | |
141 f.write('<residues>TGTTCATATTCATAGGATGGAGCTAGTAAGCGATGTCGGCTTAGCTCATCCACATGAATGCAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACACGAGCTGGGTTAAATCAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACA</residues>') | |
142 f.write('</seq><map_position><arm>seq1</arm><span><start>1</start><end>165</end></span></map_position></game>') | |
143 f.close() | |
144 f = open("seq2.gamexml", 'w') | |
145 f.write('<?xml version="1.0" ?><game><seq focus="true" id="seq2"><name>seq2</name>') | |
146 f.write('<residues>TGTTCATATTCATAGGATGGAGCTAGTAAGCGATGTCGGCTTAGCTCATCCACATGAATGCAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACACGAGCTGGGTTAAATCAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACACGAGCTGGGTTAAAT</residues>') | |
147 f.write('</seq><map_position><arm>seq2</arm><span><start>1</start><end>180</end></span></map_position></game>') | |
148 f.close() | |
149 f = open("seq3.gamexml", 'w') | |
150 f.write('<?xml version="1.0" ?><game><seq focus="true" id="seq3"><name>seq3</name>') | |
151 f.write('<residues>CCTAGACAATTAATTATAATAATTAATAAACTATTAGGCTAGTAGTAGGTAATAATAAAAGGATTACTACTAAGCTGCGCTATGTAGATATTTAAAACATGTGGCTTAGGCAAGAGTATA</residues>') | |
152 f.write('</seq><map_position><arm>seq3</arm><span><start>1</start><end>120</end></span></map_position></game>') | |
153 f.close() | |
154 f = open("seq4.gamexml", 'w') | |
155 f.write('<?xml version="1.0" ?><game><seq focus="true" id="seq4"><name>seq4</name>') | |
156 f.write('<residues>TGTTCATATTCATAGGATGGAGCTAGTAAGCGATGTCGGCTTAGCTCATCCACATGAATGCAGGAATCATGAAGGGTACGACTGTTCG</residues>') | |
157 f.write('</seq><map_position><arm>seq4</arm><span><start>1</start><end>88</end></span></map_position></game>') | |
158 f.close() | |
159 | |
160 def _writeStep2InputPathFile(self): | |
161 f = open("Test_F_GameXmlMaker_path.path", 'w') | |
162 f.write("1\tseq1\t1\t10\tTE1\t1\t10\t0\t550\t80.7\n") | |
163 f.write("2\tseq1\t15\t50\tTE1\t12\t47\t0\t770\t90.8\n") | |
164 f.write("2\tseq1\t55\t70\tTE1\t50\t65\t0\t5410\t97.7\n") | |
165 f.write("3\tseq1\t75\t80\tTE2\t10\t15\t0\t8710\t60.0\n") | |
166 f.write("4\tseq2\t1\t10\tTE2\t20\t30\t0\t5410\t75.7\n") | |
167 f.write("5\tseq3\t1\t120\tTE2\t450\t550\t0\t5410\t75.7\n") | |
168 f.close() | |
169 | |
170 def _writeStep2InputSetFile(self): | |
171 f = open("Test_F_GameXmlMaker_set.set", 'w') | |
172 f.write("1\tseq1\tseq1\t1\t10\n") | |
173 f.write("2\tseq1\tseq1\t15\t50\n") | |
174 f.write("3\tseq1\tseq1\t75\t80\n") | |
175 f.write("4\tseq2\tseq2\t1\t10\n") | |
176 f.write("5\tseq3\tseq3\t1\t120\n") | |
177 f.close() | |
178 | |
179 def _writeStep2InputMapFile(self): | |
180 f = open("Test_F_GameXmlMaker_chk_map.map", 'w') | |
181 f.write("chunk1\tseq1\t1\t200000\n") | |
182 f.write("chunk2\tseq1\t190001\t390000\n") | |
183 f.write("chunk3\tseq3\t380001\t580000\n") | |
184 f.write("chunk4\tseq4\t570001\t770000\n") | |
185 f.write("chunk5\tseq5\t760001\t960000\n") | |
186 f.write("chunk6\tseq6\t950001\t1150000\n") | |
187 f.write("chunk7\tseq7\t1140001\t1281640\n") | |
188 f.close() | |
189 | |
190 def _writeStep2ExpXmls(self): | |
191 f = open("exp_seq1.gamexml", 'w') | |
192 f.write('<?xml version="1.0" ?><game><seq focus="true" id="seq1"><name>seq1</name>') | |
193 f.write('<residues>TGTTCATATTCATAGGATGGAGCTAGTAAGCGATGTCGGCTTAGCTCATCCACATGAATGCAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACACGAGCTGGGTTAAATCAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACA</residues>') | |
194 f.write('</seq><map_position><arm>seq1</arm><span><start>1</start><end>165</end></span></map_position>') | |
195 f.write('<computational_analysis><program>Test_F_GameXmlMaker_path</program><database>db</database><result_set id="1"><name>TE1::1</name>') | |
196 f.write('<result_span><seq_relationship seq="seq1" type="query"><span><start>1</start><end>10</end></span></seq_relationship><seq_relationship seq="TE1::1" type="subject"><span>') | |
197 f.write('<start>1</start><end>10</end></span></seq_relationship><score>80.7</score></result_span></result_set><result_set id="2"><name>TE1::2</name><result_span>') | |
198 f.write('<seq_relationship seq="seq1" type="query"><span><start>15</start><end>50</end></span></seq_relationship><seq_relationship seq="TE1::2" type="subject"><span><start>12</start>') | |
199 f.write('<end>47</end></span></seq_relationship><score>90.8</score></result_span><result_span><seq_relationship seq="seq1" type="query"><span><start>55</start><end>70</end></span></seq_relationship>') | |
200 f.write('<seq_relationship seq="TE1::2" type="subject"><span><start>50</start><end>65</end></span></seq_relationship><score>97.7</score></result_span></result_set><result_set id="3"><name>TE2::3</name>') | |
201 f.write('<result_span><seq_relationship seq="seq1" type="query"><span><start>75</start><end>80</end></span></seq_relationship><seq_relationship seq="TE2::3" type="subject"><span><start>10</start><end>15</end></span></seq_relationship>') | |
202 f.write('<score>60.0</score></result_span></result_set></computational_analysis>') | |
203 f.write('<computational_analysis><program>Test_F_GameXmlMaker_set</program><database>db</database>') | |
204 f.write('<result_set id="1"><name>seq1::1</name><result_span>') | |
205 f.write('<seq_relationship seq="seq1" type="query"><span><start>1</start><end>10</end></span></seq_relationship>') | |
206 f.write('<seq_relationship seq="seq1::1" type="subject"><span><start>-1</start><end>-1</end></span></seq_relationship>') | |
207 f.write('<score>0</score></result_span></result_set>') | |
208 f.write('<result_set id="2"><name>seq1::2</name><result_span>') | |
209 f.write('<seq_relationship seq="seq1" type="query"><span><start>15</start><end>50</end></span></seq_relationship>') | |
210 f.write('<seq_relationship seq="seq1::2" type="subject"><span><start>-1</start><end>-1</end></span></seq_relationship>') | |
211 f.write('<score>0</score></result_span></result_set>') | |
212 f.write('<result_set id="3"><name>seq1::3</name><result_span>') | |
213 f.write('<seq_relationship seq="seq1" type="query"><span><start>75</start><end>80</end></span></seq_relationship>') | |
214 f.write('<seq_relationship seq="seq1::3" type="subject"><span><start>-1</start><end>-1</end></span></seq_relationship>') | |
215 f.write('<score>0</score></result_span></result_set></computational_analysis>') | |
216 f.write('<computational_analysis><program>Test_F_GameXmlMaker_chk_map</program><database>db</database>') | |
217 f.write('<result_set id="-1"><name>chunk1::-1</name>') | |
218 f.write('<result_span><seq_relationship seq="seq1" type="query"><span><start>1</start><end>200000</end></span></seq_relationship>') | |
219 f.write('<seq_relationship seq="chunk1::-1" type="subject"><span><start>-1</start><end>-1</end></span></seq_relationship>') | |
220 f.write('<score>0</score></result_span></result_set>') | |
221 f.write('<result_set id="-1"><name>chunk2::-1</name>') | |
222 f.write('<result_span><seq_relationship seq="seq1" type="query"><span><start>190001</start><end>390000</end></span></seq_relationship>') | |
223 f.write('<seq_relationship seq="chunk2::-1" type="subject"><span><start>-1</start><end>-1</end></span></seq_relationship><score>0</score></result_span></result_set></computational_analysis></game>') | |
224 f.close() | |
225 | |
226 f = open("exp_seq2.gamexml", 'w') | |
227 f.write('<?xml version="1.0" ?><game><seq focus="true" id="seq2"><name>seq2</name>') | |
228 f.write('<residues>TGTTCATATTCATAGGATGGAGCTAGTAAGCGATGTCGGCTTAGCTCATCCACATGAATGCAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACACGAGCTGGGTTAAATCAGGAATCATGAAGGGTACGACTGTTCGTCGATTAAAGAGCTACACGAGCTGGGTTAAAT</residues>') | |
229 f.write('</seq><map_position><arm>seq2</arm><span><start>1</start><end>180</end></span></map_position>') | |
230 f.write('<computational_analysis><program>Test_F_GameXmlMaker_path</program><database>db</database><result_set id="4"><name>TE2::4</name>') | |
231 f.write('<result_span><seq_relationship seq="seq2" type="query"><span><start>1</start><end>10</end></span>') | |
232 f.write('</seq_relationship><seq_relationship seq="TE2::4" type="subject"><span><start>20</start><end>30</end></span></seq_relationship>') | |
233 f.write('<score>75.7</score></result_span></result_set></computational_analysis>') | |
234 f.write('<computational_analysis><program>Test_F_GameXmlMaker_set</program><database>db</database><result_set id="4"><name>seq2::4</name>') | |
235 f.write('<result_span><seq_relationship seq="seq2" type="query"><span><start>1</start><end>10</end></span></seq_relationship>') | |
236 f.write('<seq_relationship seq="seq2::4" type="subject"><span><start>-1</start><end>-1</end></span></seq_relationship>') | |
237 f.write('<score>0</score></result_span></result_set></computational_analysis>') | |
238 f.write('<computational_analysis><program>Test_F_GameXmlMaker_chk_map</program><database>db</database></computational_analysis></game>') | |
239 f.close() | |
240 | |
241 f = open("exp_seq3.gamexml", 'w') | |
242 f.write('<?xml version="1.0" ?><game><seq focus="true" id="seq3"><name>seq3</name>') | |
243 f.write('<residues>CCTAGACAATTAATTATAATAATTAATAAACTATTAGGCTAGTAGTAGGTAATAATAAAAGGATTACTACTAAGCTGCGCTATGTAGATATTTAAAACATGTGGCTTAGGCAAGAGTATA</residues>') | |
244 f.write('</seq><map_position><arm>seq3</arm><span><start>1</start><end>120</end></span></map_position>') | |
245 f.write('<computational_analysis><program>Test_F_GameXmlMaker_path</program><database>db</database><result_set id="5"><name>TE2::5</name>') | |
246 f.write('<result_span><seq_relationship seq="seq3" type="query"><span><start>1</start><end>120</end></span>') | |
247 f.write('</seq_relationship><seq_relationship seq="TE2::5" type="subject"><span><start>450</start><end>550</end></span></seq_relationship>') | |
248 f.write('<score>75.7</score></result_span></result_set></computational_analysis>') | |
249 f.write('<computational_analysis><program>Test_F_GameXmlMaker_set</program><database>db</database><result_set id="5"><name>seq3::5</name>') | |
250 f.write('<result_span><seq_relationship seq="seq3" type="query"><span><start>1</start><end>120</end></span></seq_relationship>') | |
251 f.write('<seq_relationship seq="seq3::5" type="subject"><span><start>-1</start><end>-1</end></span></seq_relationship>') | |
252 f.write('<score>0</score></result_span></result_set></computational_analysis>') | |
253 f.write('<computational_analysis><program>Test_F_GameXmlMaker_chk_map</program><database>db</database>') | |
254 f.write('<result_set id="-1"><name>chunk3::-1</name>') | |
255 f.write('<result_span><seq_relationship seq="seq3" type="query"><span><start>380001</start><end>580000</end></span></seq_relationship>') | |
256 f.write('<seq_relationship seq="chunk3::-1" type="subject"><span><start>-1</start><end>-1</end></span></seq_relationship>') | |
257 f.write('<score>0</score></result_span></result_set></computational_analysis></game>') | |
258 f.close() | |
259 | |
260 f = open("exp_seq4.gamexml", 'w') | |
261 f.write('<?xml version="1.0" ?><game><seq focus="true" id="seq4"><name>seq4</name>') | |
262 f.write('<residues>TGTTCATATTCATAGGATGGAGCTAGTAAGCGATGTCGGCTTAGCTCATCCACATGAATGCAGGAATCATGAAGGGTACGACTGTTCG</residues>') | |
263 f.write('</seq><map_position><arm>seq4</arm><span><start>1</start><end>88</end></span></map_position>') | |
264 f.write('<computational_analysis><program>Test_F_GameXmlMaker_path</program><database>db</database></computational_analysis>') | |
265 f.write('<computational_analysis><program>Test_F_GameXmlMaker_set</program><database>db</database></computational_analysis>') | |
266 f.write('<computational_analysis><program>Test_F_GameXmlMaker_chk_map</program><database>db</database>') | |
267 f.write('<result_set id="-1"><name>chunk4::-1</name>') | |
268 f.write('<result_span><seq_relationship seq="seq4" type="query"><span><start>570001</start><end>770000</end></span></seq_relationship>') | |
269 f.write('<seq_relationship seq="chunk4::-1" type="subject"><span><start>-1</start><end>-1</end></span></seq_relationship>') | |
270 f.write('<score>0</score></result_span></result_set></computational_analysis></game>') | |
271 f.close() | |
272 | |
273 def _writeStep2InputTableFile(self): | |
274 f = open(self._tableFileName,'w') | |
275 f.write("Test_F_GameXmlMaker_path\tpath\tTest_F_GameXmlMaker_path\n") | |
276 f.write("Test_F_GameXmlMaker_set\tset\tTest_F_GameXmlMaker_set\n") | |
277 f.write("Test_F_GameXmlMaker_chk_map\tmap\tTest_F_GameXmlMaker_chk_map") | |
278 f.close() | |
279 | |
280 if __name__ == "__main__": | |
281 unittest.main() |