21ed5d480de1dc3a45a99a81b0a437f2fc8c94f9
[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.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;
43
44
45 public class RNAalifoldParametersTester {
46
47         
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";
52
53         private static Logger log = Logger
54                         .getLogger(AllTestSuit.RUNNER_TEST_LOGGER);
55         
56         static {
57                 log.setLevel(Level.INFO);
58         }
59         
60         RunnerConfig<RNAalifold> rnaalifoldConfig = null;
61         OptionCombinator rnaalifoldOpc = null;
62         
63         @BeforeMethod(groups = { AllTestSuit.test_group_runner })
64         @SuppressWarnings("unchecked")
65         public void setup() {
66                 try {
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                         log.info("prmSeparator: " + rnaalifoldConfig.getPrmSeparator());
74                         rnaalifoldOpc = new OptionCombinator(rnaalifoldConfig);
75                         
76                         log.info("Num options: " + rnaalifoldOpc.getAllOptions().size());
77                         log.info("Num params: " + rnaalifoldOpc.getAllParameters().size());
78                         
79                         
80                 } catch (JAXBException e) {
81                         e.printStackTrace();
82                         fail(e.getLocalizedMessage());
83                 } catch (FileNotFoundException e) {
84                         e.printStackTrace();
85                         fail(e.getLocalizedMessage());
86                 }
87         }
88         
89         @Test
90         public void testConfiguration() {
91                 try {
92                         this.rnaalifoldConfig.validate();
93                 } catch (ValidationException e) {
94                         e.printStackTrace();
95                         fail(e.getLocalizedMessage());
96                 } catch (IllegalStateException e) {
97                         e.printStackTrace();
98                         fail(e.getLocalizedMessage());
99                 }
100         }
101
102         @Test(groups = { AllTestSuit.test_group_runner })
103         public void testDefaultParameters() {
104                 RNAalifold rnaalifold = new RNAalifold();
105                 rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile);
106
107                 try {
108                         // For local execution use relavive
109                         ConfiguredExecutable<RNAalifold> confRNAalifold = Configurator
110                                         .configureExecutable(rnaalifold);
111                         LocalRunner lr = new LocalRunner(confRNAalifold);
112                         lr.executeJob();
113                         confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
114                         assertNotNull(confRNAalifold.getResults());
115                 } catch (JobSubmissionException e) {
116                         e.printStackTrace();
117                         fail(e.getLocalizedMessage());
118                 } catch (JobExecutionException e) {
119                         e.printStackTrace();
120                         fail(e.getLocalizedMessage());
121                 } catch (ResultNotAvailableException e) {
122                         e.printStackTrace();
123                         fail(e.getLocalizedMessage());
124                 }
125         }
126         
127         @Test(groups = { AllTestSuit.test_group_runner })
128         public void testOptions() {
129                 test(rnaalifoldOpc.getAllOptions());
130         }
131         
132         @Test(groups = { AllTestSuit.test_group_runner })
133         public void testOptionPairs() throws ResultNotAvailableException {
134                 List<Option<?>> pair = new ArrayList<Option<?>>();
135                 List<Option<?>> options = rnaalifoldOpc.getAllOptions();
136                 int numpairs = 0;
137                 for (int i = 0; i<options.size(); i++) {
138                         for (int j = i; j<options.size(); j++) {
139                                 if (i != j) {
140                                         //System.out.println("special numOptions: " + options.size());
141                                         //System.out.println("special loop varaibles:-  (i,j): (" + i + "," + j +")");
142                                         pair.clear();
143                                         pair.add(options.get(i)); pair.add(options.get(j));
144                                         log.info("option pair to test: " + pair);
145                                         // I want to catch ResultNotAvailableException here so 
146                                         // I can return a list of imcompatable options but
147                                         // singleTest calls a fail() method...
148                                         // can i fail the test and continue running?
149                                         test(pair);
150                                         
151                                         //System.out.println("special numpairstested: " + ++numpairs);
152                                 }
153                         }
154                 }
155         }
156
157         @Test(groups = { AllTestSuit.test_group_runner })
158         public void testParameters() {
159                 List<Parameter<?>> params = rnaalifoldOpc.getAllParameters();
160                 Collections.shuffle(params);
161                 test(params);
162         }
163
164         @Test(groups = { AllTestSuit.test_group_runner })
165         public void testArguments() {
166                 List<Option<?>> options = rnaalifoldOpc.getAllOptions();
167                 options.addAll(rnaalifoldOpc.getAllParameters());
168                 Collections.shuffle(options);
169                 test(options);
170         }
171         
172         /*
173         void test(Map<Parameter<?>, String> paramValue) {
174                 List<Parameter<?>> paramList = new ArrayList<Parameter<?>>(paramValue
175                                 .keySet());
176                 for (int i = 0; i < paramValue.size(); i++) {
177                         List<String> args = rnaalifoldOpc.parametersToCommandString(paramList, 
178                                         paramValue);
179                         singleTest(args);
180                         Collections.shuffle(paramList);
181                 }
182                 log.info("NUMBER OF COMBINATION TESTED: " + paramValue.size());
183         }
184         */
185         
186         void test(List<? extends Option<?>> params) {
187                 for (int i = 0; i < params.size(); i++) {
188                         List<String> args = rnaalifoldOpc.argumentsToCommandString(params);
189                         singleTest(args);
190                         Collections.shuffle(params);
191                 }
192                 log.info("NUMBER OF COMBINATION TESTED: " + params.size());
193         }
194         
195         @Test
196         void singleParamTest() {
197                 //List<Parameter<?>> params = rnaalifoldOpc.getAllParameters();
198                 //System.out.println("special: params: " + params);
199                 
200                 //List<String> args = rnaalifoldOpc.argumentsToCommandString(params);
201                 List<String> args = new ArrayList<String>();
202                 args.add("-T 37"); args.add("-S 1.07"); args.add("--stochBT_en=10");
203                 args.add("--MEA=1");
204                 System.out.println("special: args: " + args);
205                 singleTest(args);
206                 
207         }
208         
209         void singleTest(List<String> params) {
210                 try {
211                         log.info("Using arguments: " + params);
212                         RNAalifold rnaalifold = new RNAalifold();
213                         rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile);
214                         
215                         ConfiguredExecutable<RNAalifold> confRNAalifold = Configurator
216                                         .configureExecutable(rnaalifold, ExecProvider.Local);
217                         // Add options to the executable
218                         confRNAalifold.addParameters(params);
219                         LocalRunner lr = new LocalRunner(confRNAalifold);
220                         lr.executeJob();
221                         confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
222                         assertNotNull(confRNAalifold.getResults());
223                         File errors = new File(confRNAalifold.getWorkDirectory(),
224                                         ExecutableWrapper.PROC_ERR_FILE);
225                         if (errors.length() != 0) {
226                                 log.error("PROBLEMS:\n " + FileUtil.readFileToString(errors));
227                         }
228                         assertTrue(errors.length() == 0, "Run with arguments : " + params
229                                         + " FAILED!");
230                         Collections.shuffle(params);
231                 } catch (JobSubmissionException e) {
232                         e.printStackTrace();
233                         fail(e.getLocalizedMessage());
234                 } catch (JobExecutionException e) {
235                         e.printStackTrace();
236                         fail(e.getLocalizedMessage());
237                 } catch (IOException e) {
238                         e.printStackTrace();
239                         fail(e.getLocalizedMessage());
240                 } catch (ResultNotAvailableException e) {
241                         e.printStackTrace();
242                         fail(e.getLocalizedMessage());
243                 }
244         }
245         
246 }