Webservice which implements FoldWS and returns String. Parameters.xml file
[jabaws.git] / testsrc / compbio / runner / structure / RNAalifoldParametersTester.java
1 package compbio.runner.structure;
2
3 import static org.testng.Assert.assertFalse;
4 import static org.testng.Assert.assertNotNull;
5 import static org.testng.Assert.assertTrue;
6 import static org.testng.Assert.fail;
7
8 import java.io.File;
9 import java.io.FileInputStream;
10 import java.io.FileNotFoundException;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Map;
16
17 import javax.xml.bind.JAXBException;
18 import javax.xml.bind.ValidationException;
19
20 import org.apache.log4j.Level;
21 import org.apache.log4j.Logger;
22 import org.testng.annotations.BeforeMethod;
23 import org.testng.annotations.Test;
24
25 import compbio.engine.Configurator;
26 import compbio.engine.client.ConfiguredExecutable;
27 import compbio.engine.client.Executable.ExecProvider;
28 import compbio.engine.conf.RunnerConfigMarshaller;
29 import compbio.engine.local.ExecutableWrapper;
30 import compbio.engine.local.LocalRunner;
31 import compbio.metadata.AllTestSuit;
32 import compbio.metadata.JobExecutionException;
33 import compbio.metadata.JobSubmissionException;
34 import compbio.metadata.Option;
35 import compbio.metadata.Parameter;
36 import compbio.metadata.Preset;
37 import compbio.metadata.PresetManager;
38 import compbio.metadata.ResultNotAvailableException;
39 import compbio.metadata.RunnerConfig;
40 import compbio.runner.OptionCombinator;
41 import compbio.runner.structure.RNAalifold;
42 import compbio.util.FileUtil;
43
44
45 public class RNAalifoldParametersTester {
46
47         
48         // should be: AllTestSuit.TEST_DATA_PATH + "RNAalifoldParameters.xml"
49         static final String rnaalifoldConfigFile = 
50                         AllTestSuit.TEST_DATA_PATH + "RNAalifoldParameters.xml";
51         public static String test_outfile = "rnaalifold.out.txt";
52
53         private static Logger log = Logger
54                         .getLogger(AllTestSuit.RUNNER_TEST_LOGGER);
55         
56         static {
57                 log.setLevel(Level.INFO);
58         }
59         
60         RunnerConfig<RNAalifold> rnaalifoldConfig = null;
61         OptionCombinator rnaalifoldOpc = null;
62         
63         @BeforeMethod(groups = { AllTestSuit.test_group_runner })
64         @SuppressWarnings("unchecked")
65         public void setup() {
66                 try {
67                         RunnerConfigMarshaller<RNAalifold> rnaalifoldmarsh = 
68                                         new RunnerConfigMarshaller<RNAalifold>(RunnerConfig.class);
69                         rnaalifoldConfig = rnaalifoldmarsh.read(new FileInputStream(new File(
70                                         rnaalifoldConfigFile)), RunnerConfig.class);
71                         // set Name value separator
72                         rnaalifoldConfig.setPrmSeparator(" ");
73                         rnaalifoldOpc = new OptionCombinator(rnaalifoldConfig);
74                         
75                         
76                         
77                 } catch (JAXBException e) {
78                         e.printStackTrace();
79                         fail(e.getLocalizedMessage());
80                 } catch (FileNotFoundException e) {
81                         e.printStackTrace();
82                         fail(e.getLocalizedMessage());
83                 }
84         }
85         
86         @Test
87         public void testConfiguration() {
88                 try {
89                         this.rnaalifoldConfig.validate();
90                 } catch (ValidationException e) {
91                         e.printStackTrace();
92                         fail(e.getLocalizedMessage());
93                 } catch (IllegalStateException e) {
94                         e.printStackTrace();
95                         fail(e.getLocalizedMessage());
96                 }
97         }
98
99         @Test(groups = { AllTestSuit.test_group_runner })
100         public void testDefaultParameters() {
101                 RNAalifold rnaalifold = new RNAalifold();
102                 rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile);
103
104                 try {
105                         // For local execution use relavive
106                         ConfiguredExecutable<RNAalifold> confRNAalifold = Configurator
107                                         .configureExecutable(rnaalifold);
108                         LocalRunner lr = new LocalRunner(confRNAalifold);
109                         lr.executeJob();
110                         confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
111                         assertNotNull(confRNAalifold.getResults());
112                 } catch (JobSubmissionException e) {
113                         e.printStackTrace();
114                         fail(e.getLocalizedMessage());
115                 } catch (JobExecutionException e) {
116                         e.printStackTrace();
117                         fail(e.getLocalizedMessage());
118                 } catch (ResultNotAvailableException e) {
119                         e.printStackTrace();
120                         fail(e.getLocalizedMessage());
121                 }
122         }
123         
124         
125         
126         @Test
127         public void testOptions() {
128                 // populate list of incompatable pairs by their names. todo
129                 List<List<String>> failPairs = new ArrayList<List<String>>();
130
131                 
132                 // test the parameters without -g option
133                 test(removeParam(rnaalifoldOpc.getAllOptions(), "G-Quadruplex"));
134                 // now test without -c option
135                 test(removeParam(rnaalifoldOpc.getAllOptions(), "Circular"));
136         }
137         
138
139         // Prints all the incompatible option pairs 
140         // (-c, -g) and (-p, -p0)
141         
142         @Test
143         public void testOptionPairs() throws ResultNotAvailableException {
144                 List<Option<?>> pair = new ArrayList<Option<?>>();
145                 List<Option<?>> options = rnaalifoldOpc.getAllOptions();
146                 List<List<String>> failedOptionPairs = new ArrayList<List<String>>();
147                 
148                 boolean failed = true;
149                 for (int i = 0; i<options.size(); i++) {
150                         for (int j = i; j<options.size(); j++) {
151                                 if (i != j) {
152                                         pair.clear();
153                                         pair.add(options.get(i)); pair.add(options.get(j));
154                                         List<String> args = rnaalifoldOpc.argumentsToCommandString(pair);
155                                         try { 
156                                                 failed = singleRun(args);
157                                         } catch (ResultNotAvailableException e) {
158                                                 System.out.println("Results not available: " + e.getMessage());
159                                                 failed = true;
160                                         } catch (JobSubmissionException e) {
161                                                 e.printStackTrace();
162                                                 fail(e.getLocalizedMessage());
163                                         } catch (JobExecutionException e) {
164                                                 e.printStackTrace();
165                                                 fail(e.getLocalizedMessage());
166                                         } catch (IOException e) {
167                                                 e.printStackTrace();
168                                                 fail(e.getLocalizedMessage());
169                                         }
170                                         if (failed == true) {
171                                                 failedOptionPairs.add(args);
172                                         }
173                                 }
174                         }
175                 }
176                 System.out.println("failedOptionPairs: " + failedOptionPairs);
177                 
178         }
179         
180         // tests for incompatible Pairs of Parameters 
181         // there are none however the --betascale parameter requires -p
182         
183         @Test
184         public void testParameterPairs() throws ResultNotAvailableException {
185                 List<Parameter<?>> pair = new ArrayList<Parameter<?>>();
186                 List<Parameter<?>> Parameters = rnaalifoldOpc.getAllParameters();
187                 List<List<String>> failedParameterPairs = new ArrayList<List<String>>();
188                 
189                 
190                 boolean failed = true;
191                 for (int i = 0; i<Parameters.size(); i++) {
192                         for (int j = i; j<Parameters.size(); j++) {
193                                 if (i != j) {
194                                         pair.clear();
195                                         pair.add(Parameters.get(i)); pair.add(Parameters.get(j));
196                                         List<String> args = rnaalifoldOpc.argumentsToCommandString(pair);
197                                         args.add("-p"); // --betascale requires -p
198                                         try { 
199                                                 failed = singleRun(args);
200                                         } catch (ResultNotAvailableException e) {
201                                                 System.out.println("Results not available: " + e.getMessage());
202                                                 failed = true;
203                                         } catch (JobSubmissionException e) {
204                                                 e.printStackTrace();
205                                                 fail(e.getLocalizedMessage());
206                                         } catch (JobExecutionException e) {
207                                                 e.printStackTrace();
208                                                 fail(e.getLocalizedMessage());
209                                         } catch (IOException e) {
210                                                 e.printStackTrace();
211                                                 fail(e.getLocalizedMessage());
212                                         }
213                                         if (failed == true) {
214                                                 failedParameterPairs.add(args);
215                                         }
216                                 }
217                         }
218                 }
219                 System.out.println("failedParameterPairs: " + failedParameterPairs);
220                 
221         }
222         
223         
224         // removes an argument from the list by name
225         public <U extends Option<?>> List<U> removeParam(List<U> optionList, String name) {
226                 List<U> newL = new ArrayList<U>();
227                 for (int i = 0; i < optionList.size(); i++) {
228                         System.out.println(name.equals(optionList.get(i).getName()));
229                         if (!name.equals(optionList.get(i).getName())) {
230                                 
231                                 newL.add(optionList.get(i));
232                         }
233                                 
234                 }
235                 return newL;
236         }
237         
238         public <U extends Option<?>> List<U> removeParams(List<U> optionList, List<String> names) {
239                 for (int i = 0; i < names.size(); i++) {
240                         optionList = removeParam(optionList, names.get(i));
241                 }
242                 return optionList;
243         }
244         
245         
246         @Test(groups = { AllTestSuit.test_group_runner })
247         public void testParameters() {
248                 List<Parameter<?>> params = rnaalifoldOpc.getAllParameters();
249                 System.out.println("param list: " + params);
250                 Collections.shuffle(params);
251                 // test with -p for betascale option
252                 List<String> precursor = new ArrayList<String>();
253                 precursor.add("-p");
254                 
255                 test(params, precursor);
256         }
257         
258         // incompatible pairs of arguments are
259         /*
260          * the -c and -g options
261          * the -d2 option and the -d option
262          * the -p and -p0 option
263          */
264
265         @Test(groups = { AllTestSuit.test_group_runner })
266         public void testArguments() {
267                 List<Option<?>> options = rnaalifoldOpc.getAllOptions();
268                 options.addAll(rnaalifoldOpc.getAllParameters());
269                 Collections.shuffle(options);
270                 test(options);
271         }
272         
273         // This test supercedes the testParameterPair() and testOptionPair()
274         // tests by testing all pairs of arguments
275         
276         
277         @Test
278         public void testAllPairs() throws ResultNotAvailableException {
279                 List<Option<?>> pair = new ArrayList<Option<?>>();
280                 List<Option<?>> options = rnaalifoldOpc.getAllOptions();
281                 
282                 // take out -p options so it can be added to all commands later
283                 options = removeParam(options, "Partition Function");
284                 
285                 options.addAll(rnaalifoldOpc.getAllParameters());
286                 List<List<String>> failedOptionPairs = new ArrayList<List<String>>();
287                 
288                 boolean failed = true;
289                 for (int i = 0; i<options.size(); i++) {
290                         for (int j = i; j<options.size(); j++) {
291                                 if (i != j) {
292                                         pair.clear();
293                                         pair.add(options.get(i)); pair.add(options.get(j));
294                                         List<String> args = rnaalifoldOpc.argumentsToCommandString(pair);
295                                         // add -p
296                                         args.add("-p");
297                                         try { 
298                                                 failed = singleRun(args);
299                                         } catch (ResultNotAvailableException e) {
300                                                 System.out.println("Results not available: " + e.getMessage());
301                                                 failed = true;
302                                         } catch (JobSubmissionException e) {
303                                                 e.printStackTrace();
304                                                 fail(e.getLocalizedMessage());
305                                         } catch (JobExecutionException e) {
306                                                 e.printStackTrace();
307                                                 fail(e.getLocalizedMessage());
308                                         } catch (IOException e) {
309                                                 e.printStackTrace();
310                                                 fail(e.getLocalizedMessage());
311                                         }
312                                         if (failed == true) {
313                                                 failedOptionPairs.add(args);
314                                         }
315                                 }
316                         }
317                 }
318                 System.out.println("failedOptionPairs: " + failedOptionPairs);
319         }
320         
321         /*
322          *  This test method stolen from the other parameter testing classes
323          *  the only purpose of the Collections.shuffle(params) and the for loop
324          *  is to test giving the executable the parameters in different orders
325          *  which leads to a lot of (unnecessary?) tests with an argument list
326          *  as long as this one.
327          *  
328          */
329         
330         
331         void test(List<? extends Option<?>> params) {
332                 for (int i = 0; i < params.size(); i++) {
333                         List<String> args = rnaalifoldOpc.argumentsToCommandString(params);
334                         singleTest(args);
335                         Collections.shuffle(params);
336                 }
337                 log.info("NUMBER OF COMBINATION TESTED: " + params.size());
338         }
339         
340         // because some parameters presuppose the -p option
341         
342         void test(List<? extends Option<?>> params, List<String> precursor) {
343
344                 for (int i = 0; i < params.size(); i++) {
345                         List<String> args = rnaalifoldOpc.argumentsToCommandString(params);
346                         args.addAll(precursor);
347                         singleTest(args);
348                         Collections.shuffle(params);
349                 }
350                 log.info("NUMBER OF COMBINATION TESTED: " + params.size());
351         }
352         @Test
353         void singleParamTest() {
354                 //List<Parameter<?>> params = rnaalifoldOpc.getAllParameters();
355                 //System.out.println("special: params: " + params);
356                 
357                 //List<String> args = rnaalifoldOpc.argumentsToCommandString(params);
358                 List<String> args = new ArrayList<String>();
359                 args.add("-T 37"); args.add("-S 1.07"); args.add("--stochBT_en 10");
360                 // replace "=" with " " to fail test
361                 args.add("--MEA=1");
362                 System.out.println("special: args: " + args);
363                 singleTest(args);
364                 
365         }
366         
367         void singleTest(List<String> params) {
368                 try {
369                         log.info("Using arguments: " + params);
370                         RNAalifold rnaalifold = new RNAalifold();
371                         rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile);
372                         
373                         ConfiguredExecutable<RNAalifold> confRNAalifold = Configurator
374                                         .configureExecutable(rnaalifold, ExecProvider.Local);
375                         // Add options to the executable
376                         confRNAalifold.addParameters(params);
377                         LocalRunner lr = new LocalRunner(confRNAalifold);
378                         lr.executeJob();
379                         confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
380                         assertNotNull(confRNAalifold.getResults(), "results is null");
381                         
382                         //System.out.println("Results: \n" + confRNAalifold.getResults());
383                         
384                         File errors = new File(confRNAalifold.getWorkDirectory(),
385                                         ExecutableWrapper.PROC_ERR_FILE);
386                         if (errors.length() != 0) {
387                                 log.error("PROBLEMS:\n " + FileUtil.readFileToString(errors));
388                         }
389                         assertTrue(errors.length() == 0, "Run with arguments : " + params
390                                         + " FAILED!");
391                         Collections.shuffle(params);
392                 } catch (JobSubmissionException e) {
393                         e.printStackTrace();
394                         fail(e.getLocalizedMessage());
395                 } catch (JobExecutionException e) {
396                         e.printStackTrace();
397                         fail(e.getLocalizedMessage());
398                 } catch (IOException e) {
399                         e.printStackTrace();
400                         fail(e.getLocalizedMessage());
401                 } catch (ResultNotAvailableException e) {
402                         e.printStackTrace();
403                         fail(e.getLocalizedMessage());
404                 }
405         }
406         
407         /* A version of singleTest that continues running instead of calling
408          * fail() when it encounters a problem
409          * 
410          * Used to identify incompatible options and parameters
411          * returns -1 on failure
412          * 
413          * Bad Progamming practice? 
414          */
415         
416         
417         boolean singleRun(List<String> params) throws JobSubmissionException,
418                         JobExecutionException, IOException, ResultNotAvailableException {
419                 boolean fail = true;
420                 log.info("Using arguments: " + params);
421                 RNAalifold rnaalifold = new RNAalifold();
422                 rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile);
423
424                 ConfiguredExecutable<RNAalifold> confRNAalifold = Configurator
425                                 .configureExecutable(rnaalifold, ExecProvider.Local);
426                 // Add options to the executable
427                 confRNAalifold.addParameters(params);
428                 LocalRunner lr = new LocalRunner(confRNAalifold);
429                 lr.executeJob();
430                 confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
431                 
432                 //System.out.println("Results: \n" + confRNAalifold.getResults());
433                 
434                 if (confRNAalifold.getResults() != null) fail = false;
435                 File errors = new File(confRNAalifold.getWorkDirectory(),
436                                 ExecutableWrapper.PROC_ERR_FILE);
437                 if (errors.length() != 0) {
438                         log.error("PROBLEMS:\n " + FileUtil.readFileToString(errors));
439                 }
440                 assertTrue(errors.length() == 0, "Run with arguments : " + params
441                                 + " FAILED!");
442                 Collections.shuffle(params);
443                 return fail;
444                 }
445 }