package compbio.runner.structure; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import javax.xml.bind.JAXBException; import javax.xml.bind.ValidationException; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import compbio.data.sequence.RNAStructScoreManager; import compbio.data.sequence.ScoreManager; import compbio.engine.Configurator; import compbio.engine.client.ConfiguredExecutable; import compbio.engine.client.Executable.ExecProvider; import compbio.engine.conf.RunnerConfigMarshaller; import compbio.engine.local.ExecutableWrapper; import compbio.engine.local.LocalRunner; import compbio.metadata.AllTestSuit; import compbio.metadata.JobExecutionException; import compbio.metadata.JobSubmissionException; import compbio.metadata.Option; import compbio.metadata.Parameter; import compbio.metadata.Preset; import compbio.metadata.PresetManager; import compbio.metadata.ResultNotAvailableException; import compbio.metadata.RunnerConfig; import compbio.runner.OptionCombinator; import compbio.runner.structure.RNAalifold; import compbio.util.FileUtil; public class RNAalifoldParametersTester { // should be: AllTestSuit.TEST_DATA_PATH + "RNAalifoldParameters.xml" static final String rnaalifoldConfigFile = AllTestSuit.TEST_DATA_PATH + "RNAalifoldParameters.xml"; public static String test_outfile = "rnaalifold.out.txt"; private static Logger log = Logger .getLogger(AllTestSuit.RUNNER_TEST_LOGGER); static { log.setLevel(Level.INFO); } RunnerConfig rnaalifoldConfig = null; OptionCombinator rnaalifoldOpc = null; @BeforeMethod(groups = { AllTestSuit.test_group_runner }) @SuppressWarnings("unchecked") public void setup() { try { RunnerConfigMarshaller rnaalifoldmarsh = new RunnerConfigMarshaller(RunnerConfig.class); rnaalifoldConfig = rnaalifoldmarsh.read(new FileInputStream(new File( rnaalifoldConfigFile)), RunnerConfig.class); // set Name value separator rnaalifoldConfig.setPrmSeparator(" "); rnaalifoldOpc = new OptionCombinator(rnaalifoldConfig); } catch (JAXBException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } catch (FileNotFoundException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } } @Test public void testConfiguration() { try { this.rnaalifoldConfig.validate(); } catch (ValidationException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } catch (IllegalStateException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } } @Test(groups = { AllTestSuit.test_group_runner }) public void testDefaultParameters() { RNAalifold rnaalifold = new RNAalifold(); rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile); try { // For local execution use relavive ConfiguredExecutable confRNAalifold = Configurator .configureExecutable(rnaalifold); LocalRunner lr = new LocalRunner(confRNAalifold); lr.executeJob(); confRNAalifold = (ConfiguredExecutable) lr.waitForResult(); assertNotNull(confRNAalifold.getResults()); } catch (JobSubmissionException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } catch (JobExecutionException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } catch (ResultNotAvailableException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } } @Test public void testOptions() { // populate list of incompatable pairs by their names. todo List> failPairs = new ArrayList>(); // test the parameters without -g option test(removeParam(rnaalifoldOpc.getAllOptions(), "G-Quadruplex")); // now test without -c option test(removeParam(rnaalifoldOpc.getAllOptions(), "Circular")); } // Prints all the incompatible option pairs // (-c, -g) and (-p, -p0) @Test public void testOptionPairs() throws ResultNotAvailableException { List> pair = new ArrayList>(); List> options = rnaalifoldOpc.getAllOptions(); List> failedOptionPairs = new ArrayList>(); boolean failed = true; for (int i = 0; i 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> pair = new ArrayList>(); List> Parameters = rnaalifoldOpc.getAllParameters(); List> failedParameterPairs = new ArrayList>(); boolean failed = true; for (int i = 0; i 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 > List removeParam(List optionList, String name) { List newL = new ArrayList(); 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 > List removeParams(List optionList, List 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> params = rnaalifoldOpc.getAllParameters(); System.out.println("param list: " + params); Collections.shuffle(params); // test with -p for betascale option List precursor = new ArrayList(); 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() { List> options = rnaalifoldOpc.getAllOptions(); options.addAll(rnaalifoldOpc.getAllParameters()); Collections.shuffle(options); test(options); } // This test supercedes the testParameterPair() and testOptionPair() // tests by testing all pairs of arguments @Test public void testAllPairs() throws ResultNotAvailableException { List> pair = new ArrayList>(); List> 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> failedOptionPairs = new ArrayList>(); boolean failed = true; for (int i = 0; i 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); } } } } 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> params) { for (int i = 0; i < params.size(); i++) { List args = rnaalifoldOpc.argumentsToCommandString(params); singleTest(args); Collections.shuffle(params); } log.info("NUMBER OF COMBINATION TESTED: " + params.size()); } // because some parameters presuppose the -p option void test(List> params, List precursor) { for (int i = 0; i < params.size(); i++) { List args = rnaalifoldOpc.argumentsToCommandString(params); args.addAll(precursor); singleTest(args); Collections.shuffle(params); } log.info("NUMBER OF COMBINATION TESTED: " + params.size()); } @Test void singleParamTest() { //List> params = rnaalifoldOpc.getAllParameters(); //System.out.println("special: params: " + params); //List args = rnaalifoldOpc.argumentsToCommandString(params); List args = new ArrayList(); //args.add("-T 37"); args.add("-S 1.07"); args.add("--stochBT_en 10"); // replace "=" with " " to fail test args.add("--MEA=1"); args.add("-p"); singleTest(args); } void singleTest(List params) { try { log.info("Using arguments: " + params); RNAalifold rnaalifold = new RNAalifold(); rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile); ConfiguredExecutable confRNAalifold = Configurator .configureExecutable(rnaalifold, ExecProvider.Local); // Add options to the executable confRNAalifold.addParameters(params); LocalRunner lr = new LocalRunner(confRNAalifold); lr.executeJob(); confRNAalifold = (ConfiguredExecutable) lr.waitForResult(); assertNotNull(confRNAalifold.getResults(), "results is null"); System.out.println("Results: \n" + ((RNAStructScoreManager) confRNAalifold.getResults()).toString()); 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); } catch (JobSubmissionException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } catch (JobExecutionException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } catch (IOException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } catch (ResultNotAvailableException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } } /* 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 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 confRNAalifold = Configurator .configureExecutable(rnaalifold, ExecProvider.Local); // Add options to the executable confRNAalifold.addParameters(params); LocalRunner lr = new LocalRunner(confRNAalifold); lr.executeJob(); confRNAalifold = (ConfiguredExecutable) lr.waitForResult(); System.out.println("Results: \n" + ((RNAStructScoreManager) confRNAalifold.getResults()).toString()); 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; } }