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.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;
46 public class RNAalifoldParametersTester {
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";
54 private static Logger log = Logger
55 .getLogger(AllTestSuit.RUNNER_TEST_LOGGER);
58 log.setLevel(Level.INFO);
61 RunnerConfig<RNAalifold> rnaalifoldConfig = null;
62 OptionCombinator rnaalifoldOpc = null;
64 @BeforeMethod(groups = { AllTestSuit.test_group_runner })
65 @SuppressWarnings("unchecked")
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);
78 } catch (JAXBException e) {
80 fail(e.getLocalizedMessage());
81 } catch (FileNotFoundException e) {
83 fail(e.getLocalizedMessage());
88 public void testConfiguration() {
90 this.rnaalifoldConfig.validate();
91 } catch (ValidationException e) {
93 fail(e.getLocalizedMessage());
94 } catch (IllegalStateException e) {
96 fail(e.getLocalizedMessage());
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);
106 // For local execution use relavive
107 ConfiguredExecutable<RNAalifold> confRNAalifold = Configurator
108 .configureExecutable(rnaalifold);
109 LocalRunner lr = new LocalRunner(confRNAalifold);
111 confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
112 assertNotNull(confRNAalifold.getResults());
113 } catch (JobSubmissionException e) {
115 fail(e.getLocalizedMessage());
116 } catch (JobExecutionException e) {
118 fail(e.getLocalizedMessage());
119 } catch (ResultNotAvailableException e) {
121 fail(e.getLocalizedMessage());
128 public void testOptions() {
129 // populate list of incompatable pairs by their names. todo
130 List<List<String>> failPairs = new ArrayList<List<String>>();
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"));
140 // Prints all the incompatible option pairs
141 // (-c, -g) and (-p, -p0)
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>>();
149 boolean failed = true;
150 for (int i = 0; i<options.size(); i++) {
151 for (int j = i; j<options.size(); j++) {
154 pair.add(options.get(i)); pair.add(options.get(j));
155 List<String> args = rnaalifoldOpc.argumentsToCommandString(pair);
157 failed = singleRun(args);
158 } catch (ResultNotAvailableException e) {
159 System.out.println("Results not available: " + e.getMessage());
161 } catch (JobSubmissionException e) {
163 fail(e.getLocalizedMessage());
164 } catch (JobExecutionException e) {
166 fail(e.getLocalizedMessage());
167 } catch (IOException e) {
169 fail(e.getLocalizedMessage());
171 if (failed == true) {
172 failedOptionPairs.add(args);
177 System.out.println("failedOptionPairs: " + failedOptionPairs);
181 // tests for incompatible Pairs of Parameters
182 // there are none however the --betascale parameter requires -p
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>>();
191 boolean failed = true;
192 for (int i = 0; i<Parameters.size(); i++) {
193 for (int j = i; j<Parameters.size(); j++) {
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
200 failed = singleRun(args);
201 } catch (ResultNotAvailableException e) {
202 System.out.println("Results not available: " + e.getMessage());
204 } catch (JobSubmissionException e) {
206 fail(e.getLocalizedMessage());
207 } catch (JobExecutionException e) {
209 fail(e.getLocalizedMessage());
210 } catch (IOException e) {
212 fail(e.getLocalizedMessage());
214 if (failed == true) {
215 failedParameterPairs.add(args);
220 System.out.println("failedParameterPairs: " + failedParameterPairs);
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())) {
232 newL.add(optionList.get(i));
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));
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>();
256 test(params, precursor);
259 // incompatible pairs of arguments are
261 * the -c and -g options
262 * the -d2 option and the -d option
263 * the -p and -p0 option
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);
274 // This test supercedes the testParameterPair() and testOptionPair()
275 // tests by testing all pairs of arguments
279 public void testAllPairs() throws ResultNotAvailableException {
280 List<Option<?>> pair = new ArrayList<Option<?>>();
281 List<Option<?>> options = rnaalifoldOpc.getAllOptions();
283 // take out -p options so it can be added to all commands later
284 // options = removeParam(options, "Partition Function");
286 options.addAll(rnaalifoldOpc.getAllParameters());
287 List<List<String>> failedOptionPairs = new ArrayList<List<String>>();
289 boolean failed = true;
290 for (int i = 0; i<options.size(); i++) {
291 for (int j = i; j<options.size(); j++) {
294 pair.add(options.get(i)); pair.add(options.get(j));
295 List<String> args = rnaalifoldOpc.argumentsToCommandString(pair);
299 failed = singleRun(args);
300 } catch (ResultNotAvailableException e) {
301 System.out.println("Results not available: " + e.getMessage());
303 } catch (JobSubmissionException e) {
305 fail(e.getLocalizedMessage());
306 } catch (JobExecutionException e) {
308 fail(e.getLocalizedMessage());
309 } catch (IOException e) {
311 fail(e.getLocalizedMessage());
313 if (failed == true) {
314 failedOptionPairs.add(args);
319 System.out.println("failedOptionPairs: " + failedOptionPairs);
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.
332 void test(List<? extends Option<?>> params) {
333 for (int i = 0; i < params.size(); i++) {
334 List<String> args = rnaalifoldOpc.argumentsToCommandString(params);
336 Collections.shuffle(params);
338 log.info("NUMBER OF COMBINATION TESTED: " + params.size());
341 // because some parameters presuppose the -p option
343 void test(List<? extends Option<?>> params, List<String> precursor) {
345 for (int i = 0; i < params.size(); i++) {
346 List<String> args = rnaalifoldOpc.argumentsToCommandString(params);
347 args.addAll(precursor);
349 Collections.shuffle(params);
351 log.info("NUMBER OF COMBINATION TESTED: " + params.size());
354 void singleParamTest() {
355 //List<Parameter<?>> params = rnaalifoldOpc.getAllParameters();
356 //System.out.println("special: params: " + params);
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
368 void singleTest(List<String> params) {
370 log.info("Using arguments: " + params);
371 RNAalifold rnaalifold = new RNAalifold();
372 rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile);
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);
380 confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
381 assertNotNull(confRNAalifold.getResults(), "results is null");
383 System.out.println("Results: \n"
384 + ((ScoreManager) confRNAalifold.getResults()).asRNAStruct().toString());
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));
391 assertTrue(errors.length() == 0, "Run with arguments : " + params
393 Collections.shuffle(params);
394 } catch (JobSubmissionException e) {
396 fail(e.getLocalizedMessage());
397 } catch (JobExecutionException e) {
399 fail(e.getLocalizedMessage());
400 } catch (IOException e) {
402 fail(e.getLocalizedMessage());
403 } catch (ResultNotAvailableException e) {
405 fail(e.getLocalizedMessage());
409 /* A version of singleTest that continues running instead of calling
410 * fail() when it encounters a problem
412 * Used to identify incompatible options and parameters
413 * returns -1 on failure
415 * Bad Progamming practice?
419 boolean singleRun(List<String> params) throws JobSubmissionException,
420 JobExecutionException, IOException, ResultNotAvailableException {
422 log.info("Using arguments: " + params);
423 RNAalifold rnaalifold = new RNAalifold();
424 rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile);
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);
432 confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
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));
442 assertTrue(errors.length() == 0, "Run with arguments : " + params
444 Collections.shuffle(params);