Refactoring (renaming) 2 classes: AsyncJobRunner.java -> AsyncClusterRunner.java...
[jabaws.git] / testsrc / compbio / runner / msa / TcoffeeTester.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  * Copyright (c) 2013 Alexander Sherstnev\r
3  *  \r
4  *  Java Bioinformatics Analysis Web Services (JABAWS)\r
5  *  @version: 2.5     \r
6  * \r
7  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
8  *  Apache License version 2 as published by the Apache Software Foundation\r
9  * \r
10  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
11  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
12  *  License for more details.\r
13  * \r
14  *  A copy of the license is in apache_license.txt. It is also available here:\r
15  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
16  * \r
17  * Any republication or derived work distributed in source code form\r
18  * must include this copyright and license notice.\r
19  */\r
20 \r
21 package compbio.runner.msa;\r
22 \r
23 import static org.testng.Assert.assertEquals;\r
24 import static org.testng.Assert.assertNotNull;\r
25 import static org.testng.Assert.assertTrue;\r
26 import static org.testng.Assert.fail;\r
27 \r
28 import java.io.File;\r
29 import java.io.FileInputStream;\r
30 import java.io.FileNotFoundException;\r
31 import java.io.IOException;\r
32 import java.util.Arrays;\r
33 \r
34 import javax.xml.bind.ValidationException;\r
35 \r
36 import org.testng.annotations.BeforeMethod;\r
37 import org.testng.annotations.Test;\r
38 \r
39 import compbio.data.sequence.Alignment;\r
40 import compbio.engine.AsyncExecutor;\r
41 import compbio.engine.Configurator;\r
42 import compbio.engine.FilePuller;\r
43 import compbio.engine.SyncExecutor;\r
44 import compbio.engine.client.ConfExecutable;\r
45 import compbio.engine.client.ConfiguredExecutable;\r
46 import compbio.engine.client.Executable;\r
47 import compbio.engine.client.RunConfiguration;\r
48 import compbio.engine.cluster.drmaa.ClusterRunner;\r
49 import compbio.engine.local.LocalRunner;\r
50 import compbio.metadata.AllTestSuit;\r
51 import compbio.metadata.ChunkHolder;\r
52 import compbio.metadata.JobExecutionException;\r
53 import compbio.metadata.JobStatus;\r
54 import compbio.metadata.JobSubmissionException;\r
55 import compbio.metadata.LimitsManager;\r
56 import compbio.metadata.PresetManager;\r
57 import compbio.metadata.ResultNotAvailableException;\r
58 import compbio.metadata.RunnerConfig;\r
59 import compbio.runner.msa.Tcoffee;\r
60 import compbio.runner.predictors.Jpred;\r
61 import compbio.util.FileWatcher;\r
62 import compbio.util.SysPrefs;\r
63 \r
64 public class TcoffeeTester {\r
65 \r
66         private Tcoffee tcoffee;\r
67         public static final String CURRENT_DIRECTORY = SysPrefs.getCurrentDirectory() + File.separator;\r
68         public static String test_output = "tcoffee.out";\r
69         public static String test_input = CURRENT_DIRECTORY + "testsrc" + File.separator + "testdata" + File.separator + "TO1381.fasta";\r
70 \r
71         @BeforeMethod(groups = {AllTestSuit.test_group_cluster, AllTestSuit.test_group_runner, AllTestSuit.test_group_non_windows})\r
72         public void init() {\r
73                 tcoffee = new Tcoffee();\r
74                 tcoffee.setInput(test_input);\r
75                 tcoffee.setOutput(test_output);\r
76                 tcoffee.setError("tcoffee.progress");\r
77                 //System.out.println("Tcoffee has been configured!");\r
78         }\r
79 \r
80         @Test(groups = { AllTestSuit.test_group_runner, AllTestSuit.test_group_non_windows })\r
81         public void RunLocally() {\r
82                 try {\r
83                         ConfiguredExecutable<Tcoffee> ctcoffee = Configurator.configureExecutable(tcoffee, Executable.ExecProvider.Local);\r
84                         // matrix does not appear to work\r
85                         // ctcoffee.getParameters().setParam("-matrix","BLOSUM62");\r
86 \r
87                         SyncExecutor sexecutor = Configurator.getSyncEngine(ctcoffee);\r
88                         sexecutor.executeJob();\r
89                         ConfiguredExecutable<?> al = sexecutor.waitForResult();\r
90                         Alignment align = al.getResults();\r
91                         assertNotNull(align);\r
92 \r
93 //                      LocalRunner sexecutor = new LocalRunner(ctcoffee);\r
94 //                      sexecutor.executeJob();\r
95 //                      ConfiguredExecutable<?> al = sexecutor.waitForResult();\r
96 //                      Alignment align = al.getResults();\r
97 //                      assertNotNull(align);\r
98 \r
99                 } catch (JobSubmissionException e) {\r
100                         e.printStackTrace();\r
101                         fail(e.getMessage());\r
102                 } catch (JobExecutionException e) {\r
103                         e.printStackTrace();\r
104                         fail(e.getMessage());\r
105                 } catch (ResultNotAvailableException e) {\r
106                         e.printStackTrace();\r
107                         fail(e.getMessage());\r
108                 }\r
109         }\r
110 \r
111         @Test(groups = {AllTestSuit.test_group_runner, AllTestSuit.test_group_non_windows})\r
112         public void readStatistics() {\r
113                 try {\r
114                         Tcoffee tcoffee = new Tcoffee().setInput(AllTestSuit.test_input);\r
115                         ConfiguredExecutable<Tcoffee> confTcoffee = Configurator.configureExecutable(tcoffee, Executable.ExecProvider.Local);\r
116 \r
117                         AsyncExecutor sexec = Configurator.getAsyncEngine(confTcoffee);\r
118                         String jobId = sexec.submitJob(confTcoffee);\r
119                         String file = confTcoffee.getWorkDirectory() + File.separator + tcoffee.getError();\r
120                         FilePuller fw = FilePuller.newFilePuller(file, FileWatcher.MIN_CHUNK_SIZE_BYTES);\r
121                         int count = 0;\r
122                         long position = 0;\r
123                         fw.waitForFile(4);\r
124                         while (!(sexec.getJobStatus(jobId) == JobStatus.FINISHED || \r
125                                          sexec.getJobStatus(jobId) == JobStatus.FAILED || \r
126                                          sexec.getJobStatus(jobId) == JobStatus.UNDEFINED) || fw.hasMoreData()) {\r
127                                 if (fw.hasMoreData()) {\r
128                                         ChunkHolder ch = fw.pull(position);\r
129                                         String chunk = ch.getChunk();\r
130                                         position = ch.getNextPosition();\r
131                                         System.out.print(chunk);\r
132                                 }\r
133                                 count++;\r
134                         }\r
135                         assertTrue(count > 1);\r
136                         ConfiguredExecutable<?> al = sexec.getResults(jobId);\r
137                         assertNotNull(al.getResults());\r
138                 } catch (JobSubmissionException e) {\r
139                         e.printStackTrace();\r
140                         fail(e.getMessage());\r
141                 } catch (ResultNotAvailableException e) {\r
142                         e.printStackTrace();\r
143                         fail(e.getMessage());\r
144                 } catch (IOException e) {\r
145                         e.printStackTrace();\r
146                         fail(e.getMessage());\r
147                 }\r
148         }\r
149 \r
150         @Test(groups = { AllTestSuit.test_group_runner, AllTestSuit.test_group_non_windows })\r
151         public void SetNcore() {\r
152                 Tcoffee tc = new Tcoffee();\r
153                 assertEquals(tc.getParameters(null).size(), 3);\r
154                 tc.setNCore(2);\r
155                 assertEquals(2, tc.getNCore());\r
156                 assertEquals(tc.getParameters(null).size(), 3);\r
157                 tc.setNCore(4);\r
158                 assertEquals(4, tc.getNCore());\r
159                 assertEquals(tc.getParameters(null).size(), 3);\r
160         }\r
161 \r
162         @Test(groups = { AllTestSuit.test_group_cluster, AllTestSuit.test_group_runner, AllTestSuit.test_group_non_windows })\r
163         public void Persistance() {\r
164                 try {\r
165                         Tcoffee tcoffee = new Tcoffee();\r
166                         tcoffee.setError("errrr.txt");\r
167                         tcoffee.setInput(AllTestSuit.test_input);\r
168                         tcoffee.setOutput("outtt.txt");\r
169                         assertEquals(tcoffee.getInput(), AllTestSuit.test_input);\r
170                         assertEquals(tcoffee.getError(), "errrr.txt");\r
171                         assertEquals(tcoffee.getOutput(), "outtt.txt");\r
172 \r
173                         ConfiguredExecutable<Tcoffee> ctcofee = Configurator.configureExecutable(tcoffee, Executable.ExecProvider.Local);\r
174                         SyncExecutor sexec = Configurator.getSyncEngine(ctcofee);\r
175                         sexec.executeJob();\r
176                         ConfiguredExecutable<?> al = sexec.waitForResult();\r
177                         assertNotNull(al.getResults());\r
178                         // Save run configuration\r
179                         assertTrue(ctcofee.saveRunConfiguration());\r
180 \r
181                         // See if loaded configuration is the same as saved\r
182                         RunConfiguration loadedRun = RunConfiguration.load(new FileInputStream(new File(ctcofee.getWorkDirectory(), RunConfiguration.rconfigFile)));\r
183                         assertEquals(((ConfExecutable<Tcoffee>) ctcofee).getRunConfiguration(), loadedRun);\r
184 \r
185                         // Load run configuration as ConfExecutable\r
186                         ConfiguredExecutable<Tcoffee> resurrectedCTcoffee = (ConfiguredExecutable<Tcoffee>) ctcofee\r
187                                         .loadRunConfiguration(new FileInputStream(new File(ctcofee.getWorkDirectory(), RunConfiguration.rconfigFile)));\r
188                         assertNotNull(resurrectedCTcoffee);\r
189 \r
190                         // See in details whether executables are the same\r
191                         assertEquals(resurrectedCTcoffee.getExecutable(), tcoffee);\r
192 \r
193                         // Finally rerun the job in the new task directory\r
194                         ConfiguredExecutable<Tcoffee> restcoffee = Configurator.configureExecutable(resurrectedCTcoffee.getExecutable(), Executable.ExecProvider.Local);\r
195 \r
196                         sexec = Configurator.getSyncEngine(restcoffee,Executable.ExecProvider.Local);\r
197                         sexec.executeJob();\r
198                         al = sexec.waitForResult();\r
199                         assertNotNull(al);\r
200 \r
201                 } catch (JobSubmissionException e) {\r
202                         e.printStackTrace();\r
203                         fail(e.getMessage());\r
204                 } catch (JobExecutionException e) {\r
205                         e.printStackTrace();\r
206                         fail(e.getMessage());\r
207                 } catch (FileNotFoundException e) {\r
208                         e.printStackTrace();\r
209                         fail(e.getMessage());\r
210                 } catch (IOException e) {\r
211                         e.printStackTrace();\r
212                         fail(e.getMessage());\r
213                 } catch (ResultNotAvailableException e) {\r
214                         e.printStackTrace();\r
215                         fail(e.getMessage());\r
216                 }\r
217         }\r
218 \r
219         @Test(groups = { AllTestSuit.test_group_runner })\r
220         public void ConfigurationLoading() {\r
221                 try {\r
222                         RunnerConfig<Tcoffee> tcoffeeConfig = ConfExecutable.getRunnerOptions(Tcoffee.class);\r
223                         assertNotNull(tcoffeeConfig);\r
224                         assertTrue(tcoffeeConfig.getArguments().size() > 0);\r
225 \r
226                         PresetManager<Tcoffee> tcoffeePresets = ConfExecutable.getRunnerPresets(Tcoffee.class);\r
227                         assertNotNull(tcoffeePresets);\r
228                         assertTrue(tcoffeePresets.getPresets().size() > 0);\r
229                         tcoffeePresets.validate(tcoffeeConfig);\r
230 \r
231                         LimitsManager<Tcoffee> tcoffeeLimits = ConfExecutable.getRunnerLimits(Tcoffee.class);\r
232                         assertNotNull(tcoffeeLimits);\r
233                         assertTrue(tcoffeeLimits.getLimits().size() > 0);\r
234                         tcoffeeLimits.validate(tcoffeePresets);\r
235                 } catch (FileNotFoundException e) {\r
236                         e.printStackTrace();\r
237                         fail(e.getLocalizedMessage());\r
238                 } catch (IOException e) {\r
239                         e.printStackTrace();\r
240                         fail(e.getLocalizedMessage());\r
241                 } catch (ValidationException e) {\r
242                         e.printStackTrace();\r
243                         fail(e.getLocalizedMessage());\r
244                 }\r
245         }\r
246 \r
247         // disabled\r
248         @Test(enabled=false,groups = { AllTestSuit.test_group_cluster, AllTestSuit.test_group_runner, AllTestSuit.test_group_non_windows })\r
249         public void RunOnCluster() {\r
250                 try {\r
251                         ConfiguredExecutable<Tcoffee> cmafft = Configurator.configureExecutable(tcoffee, Executable.ExecProvider.Cluster);\r
252                         ClusterRunner sexecutor = (ClusterRunner) Configurator.getSyncEngine(cmafft, Executable.ExecProvider.Cluster);\r
253                         sexecutor.executeJob();\r
254                         ConfiguredExecutable<?> al = sexecutor.waitForResult();\r
255                         Alignment align = al.getResults();\r
256                         assertNotNull(align);\r
257                 } catch (JobSubmissionException e) {\r
258                         e.printStackTrace();\r
259                         fail(e.getMessage());\r
260                 } catch (JobExecutionException e) {\r
261                         e.printStackTrace();\r
262                         fail(e.getMessage());\r
263                 } catch (ResultNotAvailableException e) {\r
264                         e.printStackTrace();\r
265                         fail(e.getMessage());\r
266                 }\r
267         }\r
268 }\r