1 package compbio.runner.structure;
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;
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;
17 import javax.xml.bind.JAXBException;
18 import javax.xml.bind.ValidationException;
20 import org.apache.log4j.Level;
21 import org.apache.log4j.Logger;
22 import org.testng.annotations.BeforeMethod;
23 import org.testng.annotations.Test;
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;
45 public class RNAalifoldParametersTester {
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";
53 private static Logger log = Logger
54 .getLogger(AllTestSuit.RUNNER_TEST_LOGGER);
57 log.setLevel(Level.INFO);
60 RunnerConfig<RNAalifold> rnaalifoldConfig = null;
61 OptionCombinator rnaalifoldOpc = null;
63 @BeforeMethod(groups = { AllTestSuit.test_group_runner })
64 @SuppressWarnings("unchecked")
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);
77 } catch (JAXBException e) {
79 fail(e.getLocalizedMessage());
80 } catch (FileNotFoundException e) {
82 fail(e.getLocalizedMessage());
87 public void testConfiguration() {
89 this.rnaalifoldConfig.validate();
90 } catch (ValidationException e) {
92 fail(e.getLocalizedMessage());
93 } catch (IllegalStateException e) {
95 fail(e.getLocalizedMessage());
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);
105 // For local execution use relavive
106 ConfiguredExecutable<RNAalifold> confRNAalifold = Configurator
107 .configureExecutable(rnaalifold);
108 LocalRunner lr = new LocalRunner(confRNAalifold);
110 confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
111 assertNotNull(confRNAalifold.getResults());
112 } catch (JobSubmissionException e) {
114 fail(e.getLocalizedMessage());
115 } catch (JobExecutionException e) {
117 fail(e.getLocalizedMessage());
118 } catch (ResultNotAvailableException e) {
120 fail(e.getLocalizedMessage());
127 public void testOptions() {
128 // populate list of incompatable pairs by their names. todo
129 List<List<String>> failPairs = new ArrayList<List<String>>();
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"));
139 // Prints all the incompatible option pairs
140 // (-c, -g) and (-p, -p0)
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>>();
148 boolean failed = true;
149 for (int i = 0; i<options.size(); i++) {
150 for (int j = i; j<options.size(); j++) {
153 pair.add(options.get(i)); pair.add(options.get(j));
154 List<String> args = rnaalifoldOpc.argumentsToCommandString(pair);
156 failed = singleRun(args);
157 } catch (ResultNotAvailableException e) {
158 System.out.println("Results not available: " + e.getMessage());
160 } catch (JobSubmissionException e) {
162 fail(e.getLocalizedMessage());
163 } catch (JobExecutionException e) {
165 fail(e.getLocalizedMessage());
166 } catch (IOException e) {
168 fail(e.getLocalizedMessage());
170 if (failed == true) {
171 failedOptionPairs.add(args);
176 System.out.println("failedOptionPairs: " + failedOptionPairs);
180 // tests for incompatible Pairs of Parameters
181 // there are none however the --betascale parameter requires -p
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>>();
190 boolean failed = true;
191 for (int i = 0; i<Parameters.size(); i++) {
192 for (int j = i; j<Parameters.size(); j++) {
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
199 failed = singleRun(args);
200 } catch (ResultNotAvailableException e) {
201 System.out.println("Results not available: " + e.getMessage());
203 } catch (JobSubmissionException e) {
205 fail(e.getLocalizedMessage());
206 } catch (JobExecutionException e) {
208 fail(e.getLocalizedMessage());
209 } catch (IOException e) {
211 fail(e.getLocalizedMessage());
213 if (failed == true) {
214 failedParameterPairs.add(args);
219 System.out.println("failedParameterPairs: " + failedParameterPairs);
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())) {
231 newL.add(optionList.get(i));
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));
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>();
255 test(params, precursor);
258 // incompatible pairs of arguments are
260 * the -c and -g options
261 * the -d2 option and the -d option
262 * the -p and -p0 option
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);
273 // This test supercedes the testParameterPair() and testOptionPair()
274 // tests by testing all pairs of arguments
278 public void testAllPairs() throws ResultNotAvailableException {
279 List<Option<?>> pair = new ArrayList<Option<?>>();
280 List<Option<?>> options = rnaalifoldOpc.getAllOptions();
282 // take out -p options so it can be added to all commands later
283 options = removeParam(options, "Partition Function");
285 options.addAll(rnaalifoldOpc.getAllParameters());
286 List<List<String>> failedOptionPairs = new ArrayList<List<String>>();
288 boolean failed = true;
289 for (int i = 0; i<options.size(); i++) {
290 for (int j = i; j<options.size(); j++) {
293 pair.add(options.get(i)); pair.add(options.get(j));
294 List<String> args = rnaalifoldOpc.argumentsToCommandString(pair);
298 failed = singleRun(args);
299 } catch (ResultNotAvailableException e) {
300 System.out.println("Results not available: " + e.getMessage());
302 } catch (JobSubmissionException e) {
304 fail(e.getLocalizedMessage());
305 } catch (JobExecutionException e) {
307 fail(e.getLocalizedMessage());
308 } catch (IOException e) {
310 fail(e.getLocalizedMessage());
312 if (failed == true) {
313 failedOptionPairs.add(args);
318 System.out.println("failedOptionPairs: " + failedOptionPairs);
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.
331 void test(List<? extends Option<?>> params) {
332 for (int i = 0; i < params.size(); i++) {
333 List<String> args = rnaalifoldOpc.argumentsToCommandString(params);
335 Collections.shuffle(params);
337 log.info("NUMBER OF COMBINATION TESTED: " + params.size());
340 // because some parameters presuppose the -p option
342 void test(List<? extends Option<?>> params, List<String> precursor) {
344 for (int i = 0; i < params.size(); i++) {
345 List<String> args = rnaalifoldOpc.argumentsToCommandString(params);
346 args.addAll(precursor);
348 Collections.shuffle(params);
350 log.info("NUMBER OF COMBINATION TESTED: " + params.size());
353 void singleParamTest() {
354 //List<Parameter<?>> params = rnaalifoldOpc.getAllParameters();
355 //System.out.println("special: params: " + params);
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
362 System.out.println("special: args: " + args);
367 void singleTest(List<String> params) {
369 log.info("Using arguments: " + params);
370 RNAalifold rnaalifold = new RNAalifold();
371 rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile);
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);
379 confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
380 assertNotNull(confRNAalifold.getResults(), "results is null");
382 //System.out.println("Results: \n" + confRNAalifold.getResults());
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));
389 assertTrue(errors.length() == 0, "Run with arguments : " + params
391 Collections.shuffle(params);
392 } catch (JobSubmissionException e) {
394 fail(e.getLocalizedMessage());
395 } catch (JobExecutionException e) {
397 fail(e.getLocalizedMessage());
398 } catch (IOException e) {
400 fail(e.getLocalizedMessage());
401 } catch (ResultNotAvailableException e) {
403 fail(e.getLocalizedMessage());
407 /* A version of singleTest that continues running instead of calling
408 * fail() when it encounters a problem
410 * Used to identify incompatible options and parameters
411 * returns -1 on failure
413 * Bad Progamming practice?
417 boolean singleRun(List<String> params) throws JobSubmissionException,
418 JobExecutionException, IOException, ResultNotAvailableException {
420 log.info("Using arguments: " + params);
421 RNAalifold rnaalifold = new RNAalifold();
422 rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile);
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);
430 confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
432 //System.out.println("Results: \n" + confRNAalifold.getResults());
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));
440 assertTrue(errors.length() == 0, "Run with arguments : " + params
442 Collections.shuffle(params);