Webservice which implements FoldWS and returns String. Parameters.xml file
[jabaws.git] / testsrc / compbio / runner / structure / RNAalifoldParametersTester.java
index 21ed5d4..b93cd18 100644 (file)
@@ -70,11 +70,8 @@ public class RNAalifoldParametersTester {
                                        rnaalifoldConfigFile)), RunnerConfig.class);
                        // set Name value separator
                        rnaalifoldConfig.setPrmSeparator(" ");
-                       log.info("prmSeparator: " + rnaalifoldConfig.getPrmSeparator());
                        rnaalifoldOpc = new OptionCombinator(rnaalifoldConfig);
                        
-                       log.info("Num options: " + rnaalifoldOpc.getAllOptions().size());
-                       log.info("Num params: " + rnaalifoldOpc.getAllParameters().size());
                        
                        
                } catch (JAXBException e) {
@@ -124,42 +121,146 @@ public class RNAalifoldParametersTester {
                }
        }
        
-       @Test(groups = { AllTestSuit.test_group_runner })
+       
+       
+       @Test
        public void testOptions() {
-               test(rnaalifoldOpc.getAllOptions());
+               // populate list of incompatable pairs by their names. todo
+               List<List<String>> failPairs = new ArrayList<List<String>>();
+
+               
+               // test the parameters without -g option
+               test(removeParam(rnaalifoldOpc.getAllOptions(), "G-Quadruplex"));
+               // now test without -c option
+               test(removeParam(rnaalifoldOpc.getAllOptions(), "Circular"));
        }
        
-       @Test(groups = { AllTestSuit.test_group_runner })
+
+       // Prints all the incompatible option pairs 
+       // (-c, -g) and (-p, -p0)
+       
+       @Test
        public void testOptionPairs() throws ResultNotAvailableException {
                List<Option<?>> pair = new ArrayList<Option<?>>();
                List<Option<?>> options = rnaalifoldOpc.getAllOptions();
-               int numpairs = 0;
+               List<List<String>> failedOptionPairs = new ArrayList<List<String>>();
+               
+               boolean failed = true;
                for (int i = 0; i<options.size(); i++) {
                        for (int j = i; j<options.size(); j++) {
                                if (i != j) {
-                                       //System.out.println("special numOptions: " + options.size());
-                                       //System.out.println("special loop varaibles:-  (i,j): (" + i + "," + j +")");
                                        pair.clear();
                                        pair.add(options.get(i)); pair.add(options.get(j));
-                                       log.info("option pair to test: " + pair);
-                                       // I want to catch ResultNotAvailableException here so 
-                                       // I can return a list of imcompatable options but
-                                       // singleTest calls a fail() method...
-                                       // can i fail the test and continue running?
-                                       test(pair);
-                                       
-                                       //System.out.println("special numpairstested: " + ++numpairs);
+                                       List<String> args = rnaalifoldOpc.argumentsToCommandString(pair);
+                                       try { 
+                                               failed = singleRun(args);
+                                       } catch (ResultNotAvailableException e) {
+                                               System.out.println("Results not available: " + e.getMessage());
+                                               failed = true;
+                                       } catch (JobSubmissionException e) {
+                                               e.printStackTrace();
+                                               fail(e.getLocalizedMessage());
+                                       } catch (JobExecutionException e) {
+                                               e.printStackTrace();
+                                               fail(e.getLocalizedMessage());
+                                       } catch (IOException e) {
+                                               e.printStackTrace();
+                                               fail(e.getLocalizedMessage());
+                                       }
+                                       if (failed == true) {
+                                               failedOptionPairs.add(args);
+                                       }
                                }
                        }
                }
+               System.out.println("failedOptionPairs: " + failedOptionPairs);
+               
        }
-
+       
+       // tests for incompatible Pairs of Parameters 
+       // there are none however the --betascale parameter requires -p
+       
+       @Test
+       public void testParameterPairs() throws ResultNotAvailableException {
+               List<Parameter<?>> pair = new ArrayList<Parameter<?>>();
+               List<Parameter<?>> Parameters = rnaalifoldOpc.getAllParameters();
+               List<List<String>> failedParameterPairs = new ArrayList<List<String>>();
+               
+               
+               boolean failed = true;
+               for (int i = 0; i<Parameters.size(); i++) {
+                       for (int j = i; j<Parameters.size(); j++) {
+                               if (i != j) {
+                                       pair.clear();
+                                       pair.add(Parameters.get(i)); pair.add(Parameters.get(j));
+                                       List<String> args = rnaalifoldOpc.argumentsToCommandString(pair);
+                                       args.add("-p"); // --betascale requires -p
+                                       try { 
+                                               failed = singleRun(args);
+                                       } catch (ResultNotAvailableException e) {
+                                               System.out.println("Results not available: " + e.getMessage());
+                                               failed = true;
+                                       } catch (JobSubmissionException e) {
+                                               e.printStackTrace();
+                                               fail(e.getLocalizedMessage());
+                                       } catch (JobExecutionException e) {
+                                               e.printStackTrace();
+                                               fail(e.getLocalizedMessage());
+                                       } catch (IOException e) {
+                                               e.printStackTrace();
+                                               fail(e.getLocalizedMessage());
+                                       }
+                                       if (failed == true) {
+                                               failedParameterPairs.add(args);
+                                       }
+                               }
+                       }
+               }
+               System.out.println("failedParameterPairs: " + failedParameterPairs);
+               
+       }
+       
+       
+       // removes an argument from the list by name
+       public <U extends Option<?>> List<U> removeParam(List<U> optionList, String name) {
+               List<U> newL = new ArrayList<U>();
+               for (int i = 0; i < optionList.size(); i++) {
+                       System.out.println(name.equals(optionList.get(i).getName()));
+                       if (!name.equals(optionList.get(i).getName())) {
+                               
+                               newL.add(optionList.get(i));
+                       }
+                               
+               }
+               return newL;
+       }
+       
+       public <U extends Option<?>> List<U> removeParams(List<U> optionList, List<String> names) {
+               for (int i = 0; i < names.size(); i++) {
+                       optionList = removeParam(optionList, names.get(i));
+               }
+               return optionList;
+       }
+       
+       
        @Test(groups = { AllTestSuit.test_group_runner })
        public void testParameters() {
                List<Parameter<?>> params = rnaalifoldOpc.getAllParameters();
+               System.out.println("param list: " + params);
                Collections.shuffle(params);
-               test(params);
+               // test with -p for betascale option
+               List<String> precursor = new ArrayList<String>();
+               precursor.add("-p");
+               
+               test(params, precursor);
        }
+       
+       // incompatible pairs of arguments are
+       /*
+        * the -c and -g options
+        * the -d2 option and the -d option
+        * the -p and -p0 option
+        */
 
        @Test(groups = { AllTestSuit.test_group_runner })
        public void testArguments() {
@@ -169,19 +270,63 @@ public class RNAalifoldParametersTester {
                test(options);
        }
        
-       /*
-       void test(Map<Parameter<?>, String> paramValue) {
-               List<Parameter<?>> paramList = new ArrayList<Parameter<?>>(paramValue
-                               .keySet());
-               for (int i = 0; i < paramValue.size(); i++) {
-                       List<String> args = rnaalifoldOpc.parametersToCommandString(paramList, 
-                                       paramValue);
-                       singleTest(args);
-                       Collections.shuffle(paramList);
+       // This test supercedes the testParameterPair() and testOptionPair()
+       // tests by testing all pairs of arguments
+       
+       
+       @Test
+       public void testAllPairs() throws ResultNotAvailableException {
+               List<Option<?>> pair = new ArrayList<Option<?>>();
+               List<Option<?>> options = rnaalifoldOpc.getAllOptions();
+               
+               // take out -p options so it can be added to all commands later
+               options = removeParam(options, "Partition Function");
+               
+               options.addAll(rnaalifoldOpc.getAllParameters());
+               List<List<String>> failedOptionPairs = new ArrayList<List<String>>();
+               
+               boolean failed = true;
+               for (int i = 0; i<options.size(); i++) {
+                       for (int j = i; j<options.size(); j++) {
+                               if (i != j) {
+                                       pair.clear();
+                                       pair.add(options.get(i)); pair.add(options.get(j));
+                                       List<String> args = rnaalifoldOpc.argumentsToCommandString(pair);
+                                       // add -p
+                                       args.add("-p");
+                                       try { 
+                                               failed = singleRun(args);
+                                       } catch (ResultNotAvailableException e) {
+                                               System.out.println("Results not available: " + e.getMessage());
+                                               failed = true;
+                                       } catch (JobSubmissionException e) {
+                                               e.printStackTrace();
+                                               fail(e.getLocalizedMessage());
+                                       } catch (JobExecutionException e) {
+                                               e.printStackTrace();
+                                               fail(e.getLocalizedMessage());
+                                       } catch (IOException e) {
+                                               e.printStackTrace();
+                                               fail(e.getLocalizedMessage());
+                                       }
+                                       if (failed == true) {
+                                               failedOptionPairs.add(args);
+                                       }
+                               }
+                       }
                }
-               log.info("NUMBER OF COMBINATION TESTED: " + paramValue.size());
+               System.out.println("failedOptionPairs: " + failedOptionPairs);
        }
-       */
+       
+       /*
+        *  This test method stolen from the other parameter testing classes
+        *  the only purpose of the Collections.shuffle(params) and the for loop
+        *  is to test giving the executable the parameters in different orders
+        *  which leads to a lot of (unnecessary?) tests with an argument list
+        *  as long as this one.
+        *  
+        */
+       
        
        void test(List<? extends Option<?>> params) {
                for (int i = 0; i < params.size(); i++) {
@@ -192,6 +337,18 @@ public class RNAalifoldParametersTester {
                log.info("NUMBER OF COMBINATION TESTED: " + params.size());
        }
        
+       // because some parameters presuppose the -p option
+       
+       void test(List<? extends Option<?>> params, List<String> precursor) {
+
+               for (int i = 0; i < params.size(); i++) {
+                       List<String> args = rnaalifoldOpc.argumentsToCommandString(params);
+                       args.addAll(precursor);
+                       singleTest(args);
+                       Collections.shuffle(params);
+               }
+               log.info("NUMBER OF COMBINATION TESTED: " + params.size());
+       }
        @Test
        void singleParamTest() {
                //List<Parameter<?>> params = rnaalifoldOpc.getAllParameters();
@@ -199,7 +356,8 @@ public class RNAalifoldParametersTester {
                
                //List<String> args = rnaalifoldOpc.argumentsToCommandString(params);
                List<String> args = new ArrayList<String>();
-               args.add("-T 37"); args.add("-S 1.07"); args.add("--stochBT_en=10");
+               args.add("-T 37"); args.add("-S 1.07"); args.add("--stochBT_en 10");
+               // replace "=" with " " to fail test
                args.add("--MEA=1");
                System.out.println("special: args: " + args);
                singleTest(args);
@@ -219,7 +377,10 @@ public class RNAalifoldParametersTester {
                        LocalRunner lr = new LocalRunner(confRNAalifold);
                        lr.executeJob();
                        confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
-                       assertNotNull(confRNAalifold.getResults());
+                       assertNotNull(confRNAalifold.getResults(), "results is null");
+                       
+                       //System.out.println("Results: \n" + confRNAalifold.getResults());
+                       
                        File errors = new File(confRNAalifold.getWorkDirectory(),
                                        ExecutableWrapper.PROC_ERR_FILE);
                        if (errors.length() != 0) {
@@ -243,4 +404,42 @@ public class RNAalifoldParametersTester {
                }
        }
        
+       /* A version of singleTest that continues running instead of calling
+        * fail() when it encounters a problem
+        * 
+        * Used to identify incompatible options and parameters
+        * returns -1 on failure
+        * 
+        * Bad Progamming practice? 
+        */
+       
+       
+       boolean singleRun(List<String> params) throws JobSubmissionException,
+                       JobExecutionException, IOException, ResultNotAvailableException {
+               boolean fail = true;
+               log.info("Using arguments: " + params);
+               RNAalifold rnaalifold = new RNAalifold();
+               rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile);
+
+               ConfiguredExecutable<RNAalifold> confRNAalifold = Configurator
+                               .configureExecutable(rnaalifold, ExecProvider.Local);
+               // Add options to the executable
+               confRNAalifold.addParameters(params);
+               LocalRunner lr = new LocalRunner(confRNAalifold);
+               lr.executeJob();
+               confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
+               
+               //System.out.println("Results: \n" + confRNAalifold.getResults());
+               
+               if (confRNAalifold.getResults() != null) fail = false;
+               File errors = new File(confRNAalifold.getWorkDirectory(),
+                               ExecutableWrapper.PROC_ERR_FILE);
+               if (errors.length() != 0) {
+                       log.error("PROBLEMS:\n " + FileUtil.readFileToString(errors));
+               }
+               assertTrue(errors.length() == 0, "Run with arguments : " + params
+                               + " FAILED!");
+               Collections.shuffle(params);
+               return fail;
+               }
 }