Next version of JABA
[jabaws.git] / testsrc / compbio / runner / msa / TcoffeeParametersTester.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.util.ArrayList;\r
25 import java.util.Collections;\r
26 import java.util.List;\r
27 import java.util.Map;\r
28 \r
29 import javax.xml.bind.ValidationException;\r
30 \r
31 import org.apache.log4j.Level;\r
32 import org.apache.log4j.Logger;\r
33 import org.testng.annotations.BeforeMethod;\r
34 import org.testng.annotations.Test;\r
35 \r
36 import compbio.engine.Configurator;\r
37 import compbio.engine.SyncExecutor;\r
38 import compbio.engine.client.ConfiguredExecutable;\r
39 import compbio.engine.client.Executable.ExecProvider;\r
40 import compbio.metadata.AllTestSuit;\r
41 import compbio.metadata.JobExecutionException;\r
42 import compbio.metadata.JobSubmissionException;\r
43 import compbio.metadata.Option;\r
44 import compbio.metadata.Parameter;\r
45 import compbio.metadata.Preset;\r
46 import compbio.metadata.PresetManager;\r
47 import compbio.metadata.ResultNotAvailableException;\r
48 import compbio.metadata.RunnerConfig;\r
49 import compbio.runner.OptionCombinator;\r
50 import compbio.runner.msa.Tcoffee;\r
51 import compbio.util.Util;\r
52 \r
53 public class TcoffeeParametersTester {\r
54 \r
55         public static String test_outfile = "TO1381.tcoffee.out";\r
56 \r
57         private static Logger log = Logger\r
58                         .getLogger(AllTestSuit.RUNNER_TEST_LOGGER);\r
59         static {\r
60                 log.setLevel(Level.INFO);\r
61         }\r
62 \r
63         RunnerConfig<Tcoffee> tcoffeeConfig = AllTestSuit.TCOFFEE_PARAMETERS;\r
64         OptionCombinator tcoffeeOpc = null;\r
65         PresetManager<Tcoffee> presets = AllTestSuit.TCOFFEE_PRESETS;\r
66 \r
67         @BeforeMethod(groups = { AllTestSuit.test_group_runner,\r
68                         AllTestSuit.test_group_non_windows })\r
69         public void setup() {\r
70                 tcoffeeOpc = new OptionCombinator(tcoffeeConfig);\r
71         }\r
72 \r
73         @Test(groups = { AllTestSuit.test_group_runner,\r
74                         AllTestSuit.test_group_non_windows })\r
75         public void testDefaultParameters() {\r
76                 Tcoffee tcoffee = new Tcoffee();\r
77                 tcoffee.setInput(AllTestSuit.test_input).setOutput(test_outfile);\r
78 \r
79                 try {\r
80                         // For local execution use relavive\r
81                         ConfiguredExecutable<Tcoffee> confTcoffee = Configurator\r
82                                         .configureExecutable(tcoffee, ExecProvider.Cluster);\r
83                         SyncExecutor sexecutor = Configurator.getSyncEngine(confTcoffee);\r
84                         sexecutor.executeJob();\r
85                         ConfiguredExecutable<?> al = sexecutor.waitForResult();\r
86                         assertNotNull(al.getResults());\r
87                 } catch (JobSubmissionException e) {\r
88                         e.printStackTrace();\r
89                         fail(e.getLocalizedMessage());\r
90                 } catch (JobExecutionException e) {\r
91                         e.printStackTrace();\r
92                         fail(e.getLocalizedMessage());\r
93                 } catch (ResultNotAvailableException e) {\r
94                         e.printStackTrace();\r
95                         fail(e.getLocalizedMessage());\r
96                 }\r
97         }\r
98 \r
99         @Test\r
100         public void testConfiguration() {\r
101                 try {\r
102                         this.tcoffeeConfig.validate();\r
103                 } catch (ValidationException e) {\r
104                         e.printStackTrace();\r
105                         fail(e.getLocalizedMessage());\r
106                 } catch (IllegalStateException e) {\r
107                         e.printStackTrace();\r
108                         fail(e.getLocalizedMessage());\r
109                 }\r
110         }\r
111 \r
112         @Test(groups = { AllTestSuit.test_group_runner,\r
113                         AllTestSuit.test_group_non_windows })\r
114         public void testOptions() {\r
115                 test(tcoffeeOpc.getAllOptions());\r
116         }\r
117 \r
118         @Test(groups = { AllTestSuit.test_group_runner,\r
119                         AllTestSuit.test_group_non_windows })\r
120         public void testParameters() {\r
121                 List<Parameter<?>> params = tcoffeeOpc.getAllParameters();\r
122                 Collections.shuffle(params);\r
123                 test(params);\r
124         }\r
125 \r
126         @Test(groups = { AllTestSuit.test_group_runner,\r
127                         AllTestSuit.test_group_non_windows })\r
128         public void testArguments() {\r
129                 List<Option<?>> options = new ArrayList<Option<?>>(tcoffeeConfig\r
130                                 .getOptions());\r
131                 options.addAll(tcoffeeOpc.getAllParameters());\r
132                 Collections.shuffle(options);\r
133                 test(options);\r
134         }\r
135 \r
136         @Test(groups = { AllTestSuit.test_group_runner,\r
137                         AllTestSuit.test_group_non_windows })\r
138         public void testConstrainedParametersMinValues() {\r
139                 Map<Parameter<?>, String> params = tcoffeeOpc\r
140                                 .getAllConstrainedParametersWithBorderValues(true);\r
141                 test(params);\r
142         }\r
143 \r
144         @Test(groups = { AllTestSuit.test_group_runner,\r
145                         AllTestSuit.test_group_non_windows })\r
146         public void testConstrainedParametersMaxValues() {\r
147                 Map<Parameter<?>, String> params = tcoffeeOpc\r
148                                 .getAllConstrainedParametersWithBorderValues(false);\r
149                 test(params);\r
150         }\r
151 \r
152         @Test(groups = { AllTestSuit.test_group_runner,\r
153                         AllTestSuit.test_group_non_windows })\r
154         public void testConstrainedParametersRandomValues() {\r
155                 for (int i = 0; i < 20; i++) {\r
156                         Map<Parameter<?>, String> params = tcoffeeOpc\r
157                                         .getAllConstrainedParametersWithRandomValues();\r
158                         List<Parameter<?>> paramList = new ArrayList<Parameter<?>>(params\r
159                                         .keySet());\r
160                         Collections.shuffle(paramList);\r
161                         List<Parameter<?>> subList = paramList.subList(0, Util\r
162                                         .getRandomNumber(1, paramList.size()));\r
163                         List<String> args = tcoffeeOpc.parametersToCommandString(subList,\r
164                                         params);\r
165                         singleTest(args);\r
166                 }\r
167         }\r
168 \r
169         // TODO fix this!\r
170         // This is due to incorrect installation of Blast with tcoffee mode\r
171         // expresso.\r
172         // It is either have to be fixed or removed\r
173         @Test(groups = { AllTestSuit.test_group_runner,\r
174                         AllTestSuit.test_group_non_windows })\r
175         public void testPresets() {\r
176                 for (Preset<Tcoffee> p : presets.getPresets()) {\r
177                         singleTest(p.getOptions());\r
178                 }\r
179         }\r
180 \r
181         void test(Map<Parameter<?>, String> paramValue) {\r
182                 List<Parameter<?>> paramList = new ArrayList<Parameter<?>>(paramValue\r
183                                 .keySet());\r
184                 for (int i = 0; i < paramValue.size(); i++) {\r
185                         List<String> args = tcoffeeOpc.parametersToCommandString(paramList,\r
186                                         paramValue);\r
187                         singleTest(args);\r
188                         Collections.shuffle(paramList);\r
189                 }\r
190                 log.info("NUMBER OF COBINATION TESTED: " + paramValue.size());\r
191         }\r
192 \r
193         void test(List<? extends Option<?>> params) {\r
194                 for (int i = 0; i < params.size(); i++) {\r
195                         List<String> args = tcoffeeOpc.argumentsToCommandString(params);\r
196                         singleTest(args);\r
197                         Collections.shuffle(params);\r
198                 }\r
199                 log.info("NUMBER OF COBINATION TESTED: " + params.size());\r
200         }\r
201 \r
202         void singleTest(List<String> params) {\r
203                 try {\r
204                         log.info("Using arguments: " + params);\r
205                         String input = AllTestSuit.test_input;\r
206                         if (params.contains("-mode rcoffee")) {\r
207                                 input = AllTestSuit.test_input_dna;\r
208                         }\r
209                         Tcoffee tcoffee = new Tcoffee();\r
210                         tcoffee.setInput(input).setOutput(test_outfile);\r
211 \r
212                         // For local execution use relative\r
213                         ConfiguredExecutable<Tcoffee> confTcoffee = Configurator\r
214                                         .configureExecutable(tcoffee, ExecProvider.Local);\r
215                         // Add options to the executable\r
216                         confTcoffee.addParameters(params);\r
217 \r
218                         SyncExecutor sexecutor = Configurator.getSyncEngine(confTcoffee,\r
219                                         ExecProvider.Local);\r
220                         sexecutor.executeJob();\r
221                         ConfiguredExecutable<?> al = sexecutor.waitForResult();\r
222                         assertNotNull(al.getResults());\r
223                         /*\r
224                          * TODO File errors = new File(confMafft.getWorkDirectory(),\r
225                          * ExecutableWrapper.PROC_ERR_FILE); if (errors.length() != 0) {\r
226                          * log.error("PROBLEMS:\n " + FileUtil.readFileToString(errors)); }\r
227                          * assertTrue("Run with arguments : " + params + " FAILED!", errors\r
228                          * .length() == 0);\r
229                          */\r
230                 } catch (JobSubmissionException e) {\r
231                         e.printStackTrace();\r
232                         fail(e.getLocalizedMessage());\r
233                 } catch (JobExecutionException e) {\r
234                         e.printStackTrace();\r
235                         fail(e.getLocalizedMessage());\r
236                 } catch (ResultNotAvailableException e) {\r
237                         e.printStackTrace();\r
238                         fail(e.getLocalizedMessage());\r
239                 }\r
240         }\r
241 \r
242 }\r