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