Add testing dirs
[proteocache.git] / testsrc / compbio / casscode / msa / ProbconsParametersTester.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.runner.msa;\r
20 \r
21 import static org.testng.Assert.assertNotNull;\r
22 import static org.testng.Assert.fail;\r
23 \r
24 import java.io.File;\r
25 import java.io.FileInputStream;\r
26 import java.io.FileNotFoundException;\r
27 import java.util.ArrayList;\r
28 import java.util.Collections;\r
29 import java.util.List;\r
30 import java.util.Map;\r
31 \r
32 import javax.xml.bind.JAXBException;\r
33 import javax.xml.bind.ValidationException;\r
34 \r
35 import org.apache.log4j.Level;\r
36 import org.apache.log4j.Logger;\r
37 import org.testng.annotations.BeforeMethod;\r
38 import org.testng.annotations.Test;\r
39 \r
40 import compbio.engine.Configurator;\r
41 import compbio.engine.SyncExecutor;\r
42 import compbio.engine.client.ConfiguredExecutable;\r
43 import compbio.engine.client.Executable.ExecProvider;\r
44 import compbio.engine.conf.RunnerConfigMarshaller;\r
45 import compbio.metadata.AllTestSuit;\r
46 import compbio.metadata.JobExecutionException;\r
47 import compbio.metadata.JobSubmissionException;\r
48 import compbio.metadata.Option;\r
49 import compbio.metadata.Parameter;\r
50 import compbio.metadata.PresetManager;\r
51 import compbio.metadata.ResultNotAvailableException;\r
52 import compbio.metadata.RunnerConfig;\r
53 import compbio.runner.OptionCombinator;\r
54 import compbio.runner.msa.Probcons;\r
55 import compbio.util.Util;\r
56 \r
57 public class ProbconsParametersTester {\r
58 \r
59         static final String probconsConfigFile = AllTestSuit.TEST_DATA_PATH\r
60                         + "ProbconsParameters.xml";\r
61 \r
62         public static String test_outfile = "TO1381.probcons.out";\r
63 \r
64         private static Logger log = Logger\r
65                         .getLogger(AllTestSuit.RUNNER_TEST_LOGGER);\r
66         static {\r
67                 log.setLevel(Level.INFO);\r
68         }\r
69 \r
70         PresetManager<Probcons> presets = null;\r
71         RunnerConfig<Probcons> probconsConfig = null;\r
72         OptionCombinator probconsOpc = null;\r
73 \r
74         @BeforeMethod(groups = { AllTestSuit.test_group_runner,\r
75                         AllTestSuit.test_group_non_windows })\r
76         public void setup() {\r
77                 try {\r
78                         // Load parameters\r
79                         RunnerConfigMarshaller<Probcons> mafftmarsh = new RunnerConfigMarshaller<Probcons>(\r
80                                         RunnerConfig.class);\r
81                         probconsConfig = mafftmarsh.read(new FileInputStream(new File(\r
82                                         probconsConfigFile)), RunnerConfig.class);\r
83                         probconsOpc = new OptionCombinator(probconsConfig);\r
84 \r
85                 } catch (JAXBException e) {\r
86                         e.printStackTrace();\r
87                         fail(e.getLocalizedMessage());\r
88                 } catch (FileNotFoundException e) {\r
89                         e.printStackTrace();\r
90                         fail(e.getLocalizedMessage());\r
91                 }\r
92 \r
93         }\r
94 \r
95         @Test\r
96         public void testConfiguration() {\r
97                 try {\r
98                         this.probconsConfig.validate();\r
99                 } catch (ValidationException e) {\r
100                         e.printStackTrace();\r
101                         fail(e.getLocalizedMessage());\r
102                 } catch (IllegalStateException e) {\r
103                         e.printStackTrace();\r
104                         fail(e.getLocalizedMessage());\r
105                 }\r
106         }\r
107 \r
108         @Test(groups = { AllTestSuit.test_group_runner,\r
109                         AllTestSuit.test_group_non_windows })\r
110         public void testDefaultParameters() {\r
111                 Probcons mafft = new Probcons();\r
112                 mafft.setInput(AllTestSuit.test_input).setOutput(test_outfile);\r
113 \r
114                 try {\r
115                         // For local execution use relavive\r
116                         ConfiguredExecutable<Probcons> confMafft = Configurator\r
117                                         .configureExecutable(mafft, ExecProvider.Cluster);\r
118                         SyncExecutor sexecutor = Configurator.getSyncEngine(confMafft);\r
119                         sexecutor.executeJob();\r
120                         confMafft = (ConfiguredExecutable<Probcons>) sexecutor\r
121                                         .waitForResult();\r
122                         assertNotNull(confMafft.getResults());\r
123                 } catch (JobSubmissionException e) {\r
124                         e.printStackTrace();\r
125                         fail(e.getLocalizedMessage());\r
126                 } catch (JobExecutionException e) {\r
127                         e.printStackTrace();\r
128                         fail(e.getLocalizedMessage());\r
129                 } catch (ResultNotAvailableException e) {\r
130                         e.printStackTrace();\r
131                         fail(e.getLocalizedMessage());\r
132                 }\r
133         }\r
134 \r
135         @Test(groups = { AllTestSuit.test_group_runner,\r
136                         AllTestSuit.test_group_non_windows })\r
137         public void testOptions() {\r
138                 test(probconsOpc.getAllOptions());\r
139         }\r
140 \r
141         @Test(groups = { AllTestSuit.test_group_runner,\r
142                         AllTestSuit.test_group_non_windows })\r
143         public void testParameters() {\r
144                 List<Parameter<?>> params = probconsOpc.getAllParameters();\r
145                 Collections.shuffle(params);\r
146                 test(params);\r
147         }\r
148 \r
149         @Test(groups = { AllTestSuit.test_group_runner,\r
150                         AllTestSuit.test_group_non_windows })\r
151         public void testArguments() {\r
152                 List<Option<?>> options = new ArrayList<Option<?>>(probconsConfig\r
153                                 .getOptions());\r
154                 options.addAll(probconsOpc.getAllParameters());\r
155                 Collections.shuffle(options);\r
156                 test(options);\r
157         }\r
158 \r
159         @Test(groups = { AllTestSuit.test_group_runner,\r
160                         AllTestSuit.test_group_non_windows })\r
161         public void testConstrainedParametersMinValues() {\r
162                 Map<Parameter<?>, String> params = probconsOpc\r
163                                 .getAllConstrainedParametersWithBorderValues(true);\r
164                 test(params);\r
165         }\r
166 \r
167         @Test(groups = { AllTestSuit.test_group_runner,\r
168                         AllTestSuit.test_group_non_windows })\r
169         public void testConstrainedParametersMaxValues() {\r
170                 Map<Parameter<?>, String> params = probconsOpc\r
171                                 .getAllConstrainedParametersWithBorderValues(false);\r
172                 test(params);\r
173         }\r
174 \r
175         @Test(groups = { AllTestSuit.test_group_runner,\r
176                         AllTestSuit.test_group_non_windows })\r
177         public void testConstrainedParametersRandomValues() {\r
178                 for (int i = 0; i < 20; i++) {\r
179                         Map<Parameter<?>, String> params = probconsOpc\r
180                                         .getAllConstrainedParametersWithRandomValues();\r
181                         List<Parameter<?>> paramList = new ArrayList<Parameter<?>>(params\r
182                                         .keySet());\r
183                         Collections.shuffle(paramList);\r
184                         List<Parameter<?>> subList = paramList.subList(0, Util\r
185                                         .getRandomNumber(1, paramList.size()));\r
186                         List<String> args = probconsOpc.parametersToCommandString(subList,\r
187                                         params);\r
188                         singleTest(args);\r
189                 }\r
190         }\r
191 \r
192         void test(Map<Parameter<?>, String> paramValue) {\r
193                 List<Parameter<?>> paramList = new ArrayList<Parameter<?>>(paramValue\r
194                                 .keySet());\r
195                 for (int i = 0; i < paramValue.size(); i++) {\r
196                         List<String> args = probconsOpc.parametersToCommandString(\r
197                                         paramList, paramValue);\r
198                         singleTest(args);\r
199                         Collections.shuffle(paramList);\r
200                 }\r
201                 log.info("NUMBER OF COBINATION TESTED: " + paramValue.size());\r
202         }\r
203 \r
204         void test(List<? extends Option<?>> params) {\r
205                 for (int i = 0; i < params.size(); i++) {\r
206                         List<String> args = probconsOpc.argumentsToCommandString(params);\r
207                         singleTest(args);\r
208                         Collections.shuffle(params);\r
209                 }\r
210                 log.info("NUMBER OF COBINATION TESTED: " + params.size());\r
211         }\r
212 \r
213         void singleTest(List<String> params) {\r
214                 try {\r
215                         log.info("Using arguments: " + params);\r
216                         Probcons mafft = new Probcons();\r
217                         mafft.setInput(AllTestSuit.test_input).setOutput(test_outfile);\r
218 \r
219                         // For local execution use relative\r
220                         ConfiguredExecutable<Probcons> confMafft = Configurator\r
221                                         .configureExecutable(mafft, ExecProvider.Local);\r
222                         // Add options to the executable\r
223                         confMafft.addParameters(params);\r
224 \r
225                         SyncExecutor sexecutor = Configurator.getSyncEngine(confMafft);\r
226                         sexecutor.executeJob();\r
227                         ConfiguredExecutable<?> al = sexecutor.waitForResult();\r
228                         assertNotNull(al.getResults());\r
229                         /*\r
230                          * TODO File errors = new File(confMafft.getWorkDirectory(),\r
231                          * ExecutableWrapper.PROC_ERR_FILE); if (errors.length() != 0) {\r
232                          * log.error("PROBLEMS:\n " + FileUtil.readFileToString(errors)); }\r
233                          * assertTrue("Run with arguments : " + params + " FAILED!", errors\r
234                          * .length() == 0);\r
235                          */\r
236                 } catch (JobSubmissionException e) {\r
237                         e.printStackTrace();\r
238                         fail(e.getLocalizedMessage());\r
239                 } catch (JobExecutionException e) {\r
240                         e.printStackTrace();\r
241                         fail(e.getLocalizedMessage());\r
242                 } catch (ResultNotAvailableException e) {\r
243                         e.printStackTrace();\r
244                         fail(e.getLocalizedMessage());\r
245                 }\r
246         }\r
247 \r
248 }\r