Adding AAConWS
[jabaws.git] / testsrc / compbio / runner / conservation / AAConTester.java
1 /*\r
2  * Copyright (c) 2010 Peter Troshin JAva Bioinformatics Analysis Web Services\r
3  * (JABAWS) @version: 2.0 \r
4  * \r
5  * This library is free software; you can redistribute it and/or modify it under \r
6  * the terms of the Apache License version 2 as published\r
7  * by the Apache Software Foundation This library is distributed in the hope\r
8  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
9  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
10  * Apache License for more details. A copy of the license is in\r
11  * apache_license.txt. It is also available here:\r
12  * \r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt \r
14  * \r
15  * Any republication or derived work distributed in source code form must include \r
16  * this copyright and license notice.\r
17  */\r
18 package compbio.runner.conservation;\r
19 \r
20 import static org.testng.Assert.assertEquals;\r
21 import static org.testng.Assert.assertFalse;\r
22 import static org.testng.Assert.assertNotNull;\r
23 import static org.testng.Assert.assertNull;\r
24 import static org.testng.Assert.assertTrue;\r
25 import static org.testng.Assert.fail;\r
26 \r
27 import java.io.File;\r
28 import java.io.FileInputStream;\r
29 import java.io.FileNotFoundException;\r
30 import java.io.IOException;\r
31 import java.text.ParseException;\r
32 \r
33 import javax.xml.bind.ValidationException;\r
34 \r
35 import org.ggf.drmaa.DrmaaException;\r
36 import org.ggf.drmaa.JobInfo;\r
37 import org.testng.annotations.BeforeMethod;\r
38 import org.testng.annotations.Test;\r
39 \r
40 import compbio.conservation.Method;\r
41 import compbio.data.sequence.MultiAnnotatedSequence;\r
42 import compbio.engine.AsyncExecutor;\r
43 import compbio.engine.Configurator;\r
44 import compbio.engine.FilePuller;\r
45 import compbio.engine.SyncExecutor;\r
46 import compbio.engine.client.ConfExecutable;\r
47 import compbio.engine.client.ConfiguredExecutable;\r
48 import compbio.engine.client.Executable;\r
49 import compbio.engine.client.RunConfiguration;\r
50 import compbio.engine.cluster.drmaa.ClusterUtil;\r
51 import compbio.engine.cluster.drmaa.JobRunner;\r
52 import compbio.engine.cluster.drmaa.StatisticManager;\r
53 import compbio.engine.local.LocalRunner;\r
54 import compbio.metadata.ChunkHolder;\r
55 import compbio.metadata.JobExecutionException;\r
56 import compbio.metadata.JobStatus;\r
57 import compbio.metadata.JobSubmissionException;\r
58 import compbio.metadata.LimitsManager;\r
59 import compbio.metadata.PresetManager;\r
60 import compbio.metadata.ResultNotAvailableException;\r
61 import compbio.metadata.RunnerConfig;\r
62 import compbio.util.FileWatcher;\r
63 import compbio.util.SysPrefs;\r
64 \r
65 public class AAConTester {\r
66 \r
67         public static final String CURRENT_DIRECTORY = SysPrefs\r
68                         .getCurrentDirectory() + File.separator;\r
69 \r
70         public static String test_outfile = "TO1381.aacon.out"; // "/homes/pvtroshin/TO1381.clustal.cluster.out\r
71         public static String test_alignment_input = CURRENT_DIRECTORY + "testsrc"\r
72                         + File.separator + "testdata" + File.separator + "TO1381.fasta.aln";\r
73         private AACon aacon;\r
74 \r
75         @BeforeMethod(alwaysRun = true)\r
76         void init() {\r
77                 aacon = new AACon();\r
78                 aacon.setInput(test_alignment_input).setOutput(test_outfile);\r
79         }\r
80 \r
81         @Test()\r
82         public void testRunOnCluster() {\r
83                 assertFalse(SysPrefs.isWindows,\r
84                                 "Cluster execution can only be in unix environment");\r
85                 try {\r
86                         ConfiguredExecutable<AACon> confAAcon = Configurator\r
87                                         .configureExecutable(aacon, Executable.ExecProvider.Cluster);\r
88                         JobRunner runner = JobRunner.getInstance(confAAcon);\r
89 \r
90                         assertNotNull(runner, "Runner is NULL");\r
91                         runner.executeJob();\r
92                         // assertNotNull("JobId is null", jobId1);\r
93                         JobStatus status = runner.getJobStatus();\r
94                         assertTrue(status == JobStatus.PENDING\r
95                                         || status == JobStatus.RUNNING,\r
96                                         "Status of the process is wrong!");\r
97                         JobInfo info = runner.getJobInfo();\r
98                         assertNotNull(info, "JobInfo is null");\r
99                         StatisticManager sm = new StatisticManager(info);\r
100                         assertNotNull(sm, "Statictic manager is null");\r
101                         try {\r
102 \r
103                                 String exits = sm.getExitStatus();\r
104                                 assertNotNull("Exit status is null", exits);\r
105                                 // cut 4 trailing zeros from the number\r
106                                 int exitsInt = ClusterUtil.CLUSTER_STAT_IN_SEC.parse(exits)\r
107                                                 .intValue();\r
108                                 assertEquals(0, exitsInt);\r
109                                 System.out.println(sm.getAllStats());\r
110 \r
111                         } catch (ParseException e) {\r
112                                 e.printStackTrace();\r
113                                 fail("Parse Exception: " + e.getMessage());\r
114                         }\r
115                         // assertFalse(runner.cleanup());\r
116                         assertTrue(sm.hasExited());\r
117                         assertFalse(sm.wasAborted());\r
118                         assertFalse(sm.hasDump());\r
119                         assertFalse(sm.hasSignaled());\r
120 \r
121                 } catch (JobSubmissionException e) {\r
122                         e.printStackTrace();\r
123                         fail("DrmaaException caught:" + e.getMessage());\r
124                 } catch (JobExecutionException e) {\r
125                         e.printStackTrace();\r
126                         fail("DrmaaException caught:" + e.getMessage());\r
127                 } catch (DrmaaException e) {\r
128                         e.printStackTrace();\r
129                         fail("DrmaaException caught:" + e.getMessage());\r
130                 }\r
131         }\r
132 \r
133         /**\r
134          * This tests fails from time to time depending on the cluster load or some\r
135          * other factors. Any client code has to adjust for this issue\r
136          */\r
137         @Test()\r
138         public void testRunOnClusterAsync() {\r
139                 assertFalse(SysPrefs.isWindows,\r
140                                 "Cluster execution can only be in unix environment");\r
141                 try {\r
142                         ConfiguredExecutable<AACon> confAAcon = Configurator\r
143                                         .configureExecutable(aacon, Executable.ExecProvider.Cluster);\r
144                         AsyncExecutor aengine = Configurator.getAsyncEngine(confAAcon);\r
145                         String jobId = aengine.submitJob(confAAcon);\r
146                         assertNotNull(jobId, "Runner is NULL");\r
147                         // let drmaa to start\r
148                         Thread.sleep(500);\r
149                         JobStatus status = aengine.getJobStatus(jobId);\r
150                         while (status != JobStatus.FINISHED) {\r
151                                 System.out.println("Job Status: " + status);\r
152                                 Thread.sleep(1000);\r
153                                 status = aengine.getJobStatus(jobId);\r
154                                 ConfiguredExecutable<AACon> result = (ConfiguredExecutable<AACon>) aengine\r
155                                                 .getResults(jobId);\r
156                                 assertNotNull(result);\r
157                                 System.out.println("RES:" + result);\r
158                                 // Some times the job could be removed from the cluster\r
159                                 // accounting\r
160                                 // before it has been reported to finish. Make sure\r
161                                 // to stop waiting in such case\r
162                                 if (status == JobStatus.UNDEFINED) {\r
163                                         break;\r
164                                 }\r
165                         }\r
166                 } catch (JobSubmissionException e) {\r
167                         e.printStackTrace();\r
168                         fail("DrmaaException caught:" + e.getMessage());\r
169                 } catch (InterruptedException e) {\r
170                         e.printStackTrace();\r
171                         fail(e.getMessage());\r
172                 } catch (ResultNotAvailableException e) {\r
173                         e.printStackTrace();\r
174                         fail(e.getMessage());\r
175                 }\r
176         }\r
177 \r
178         @Test()\r
179         public void testRunLocally() {\r
180                 try {\r
181                         ConfiguredExecutable<AACon> confAAcon = Configurator\r
182                                         .configureExecutable(aacon, Executable.ExecProvider.Local);\r
183 \r
184                         // For local execution use relative\r
185                         LocalRunner lr = new LocalRunner(confAAcon);\r
186                         lr.executeJob();\r
187                         ConfiguredExecutable<?> al1 = lr.waitForResult();\r
188                         assertNotNull(al1.getResults());\r
189                         MultiAnnotatedSequence<Method> annotations = confAAcon.getResults();\r
190                         assertNotNull(annotations);\r
191                         assertEquals(annotations.getAnnotations().size(), 18);\r
192                         assertEquals(al1.getResults(), annotations);\r
193                 } catch (JobSubmissionException e) {\r
194                         e.printStackTrace();\r
195                         fail(e.getLocalizedMessage());\r
196                 } catch (ResultNotAvailableException e) {\r
197                         e.printStackTrace();\r
198                         fail(e.getLocalizedMessage());\r
199                 } catch (JobExecutionException e) {\r
200                         e.printStackTrace();\r
201                         fail(e.getLocalizedMessage());\r
202                 }\r
203         }\r
204 \r
205         @Test()\r
206         public void testRunLocallyOnTwoCpu() {\r
207                 try {\r
208                         aacon.setNCore(2);\r
209                         ConfiguredExecutable<AACon> confAAcon = Configurator\r
210                                         .configureExecutable(aacon, Executable.ExecProvider.Local);\r
211 \r
212                         // For local execution use relative\r
213                         LocalRunner lr = new LocalRunner(confAAcon);\r
214                         lr.executeJob();\r
215                         ConfiguredExecutable<?> al1 = lr.waitForResult();\r
216                         assertNotNull(al1.getResults());\r
217                         MultiAnnotatedSequence<Method> annotations = confAAcon.getResults();\r
218                         assertNotNull(annotations);\r
219                         assertEquals(annotations.getAnnotations().size(), 18);\r
220                         assertEquals(al1.getResults(), annotations);\r
221                 } catch (JobSubmissionException e) {\r
222                         e.printStackTrace();\r
223                         fail(e.getLocalizedMessage());\r
224                 } catch (ResultNotAvailableException e) {\r
225                         e.printStackTrace();\r
226                         fail(e.getLocalizedMessage());\r
227                 } catch (JobExecutionException e) {\r
228                         e.printStackTrace();\r
229                         fail(e.getLocalizedMessage());\r
230                 }\r
231         }\r
232 \r
233         @Test()\r
234         public void readStatistics() {\r
235                 try {\r
236                         ConfiguredExecutable<AACon> confAAcon = Configurator\r
237                                         .configureExecutable(aacon, Executable.ExecProvider.Local);\r
238                         // For local execution use relative\r
239 \r
240                         AsyncExecutor sexec = Configurator.getAsyncEngine(confAAcon);\r
241                         String jobId = sexec.submitJob(confAAcon);\r
242                         FilePuller fw = FilePuller.newFilePuller(\r
243                                         confAAcon.getWorkDirectory() + File.separator\r
244                                                         + AACon.getStatFile(),\r
245                                         FileWatcher.MIN_CHUNK_SIZE_BYTES);\r
246                         int count = 0;\r
247                         long position = 0;\r
248                         fw.waitForFile(2);\r
249                         JobStatus status = sexec.getJobStatus(jobId);\r
250                         do {\r
251                                 ChunkHolder ch = fw.pull(position);\r
252                                 String chunk = ch.getChunk();\r
253                                 position = ch.getNextPosition();\r
254                                 // System.out.println(chunk);\r
255                                 count++;\r
256                                 // Make sure the loop is terminated if the job fails\r
257                                 if ((status == JobStatus.UNDEFINED || status == JobStatus.FAILED)) {\r
258                                         fail("job failed!");\r
259                                         break;\r
260                                 }\r
261                                 Thread.sleep(300);\r
262                                 status = sexec.getJobStatus(jobId);\r
263                         } while (status != JobStatus.FINISHED || fw.hasMoreData());\r
264 \r
265                         assertTrue(count >= 1);\r
266                         ConfiguredExecutable<?> al = sexec.getResults(jobId);\r
267                         assertNotNull(al.getResults());\r
268                 } catch (JobSubmissionException e) {\r
269                         e.printStackTrace();\r
270                         fail(e.getMessage());\r
271                 } catch (ResultNotAvailableException e) {\r
272                         e.printStackTrace();\r
273                         fail(e.getMessage());\r
274                 } catch (IOException e) {\r
275                         e.printStackTrace();\r
276                         fail(e.getMessage());\r
277                 } catch (InterruptedException e) {\r
278                         e.printStackTrace();\r
279                         fail(e.getMessage());\r
280                 }\r
281         }\r
282 \r
283         @Test()\r
284         public void testPersistance() {\r
285                 try {\r
286                         AACon aacon = new AACon();\r
287                         aacon.setError("errrr.txt").setInput(test_alignment_input)\r
288                                         .setOutput("outtt.txt");\r
289                         assertEquals(aacon.getInput(), test_alignment_input);\r
290                         assertEquals(aacon.getError(), "errrr.txt");\r
291                         assertEquals(aacon.getOutput(), "outtt.txt");\r
292                         ConfiguredExecutable<AACon> cAAcon = Configurator\r
293                                         .configureExecutable(aacon, Executable.ExecProvider.Local);\r
294 \r
295                         SyncExecutor sexec = Configurator.getSyncEngine(cAAcon);\r
296                         sexec.executeJob();\r
297                         ConfiguredExecutable<?> al = sexec.waitForResult();\r
298                         assertNotNull(al.getResults());\r
299                         // Save run configuration\r
300                         assertTrue(cAAcon.saveRunConfiguration());\r
301 \r
302                         // See if loaded configuration is the same as saved\r
303                         RunConfiguration loadedRun = RunConfiguration\r
304                                         .load(new FileInputStream(new File(cAAcon\r
305                                                         .getWorkDirectory(), RunConfiguration.rconfigFile)));\r
306                         assertEquals(\r
307                                         ((ConfExecutable<AACon>) cAAcon).getRunConfiguration(),\r
308                                         loadedRun);\r
309                         // Load run configuration as ConfExecutable\r
310                         ConfiguredExecutable<AACon> resurrectedCAAcon = (ConfiguredExecutable<AACon>) cAAcon\r
311                                         .loadRunConfiguration(new FileInputStream(new File(cAAcon\r
312                                                         .getWorkDirectory(), RunConfiguration.rconfigFile)));\r
313                         assertNotNull(resurrectedCAAcon);\r
314                         assertEquals(resurrectedCAAcon.getExecutable().getInput(),\r
315                                         test_alignment_input);\r
316                         assertEquals(resurrectedCAAcon.getExecutable().getError(),\r
317                                         "errrr.txt");\r
318                         assertEquals(resurrectedCAAcon.getExecutable().getOutput(),\r
319                                         "outtt.txt");\r
320                         // See in details whether executables are the same\r
321                         assertEquals(resurrectedCAAcon.getExecutable(), aacon);\r
322 \r
323                         ConfiguredExecutable<AACon> resAAcon = Configurator\r
324                                         .configureExecutable(resurrectedCAAcon.getExecutable(),\r
325                                                         Executable.ExecProvider.Local);\r
326 \r
327                         sexec = Configurator.getSyncEngine(resAAcon,\r
328                                         Executable.ExecProvider.Local);\r
329                         sexec.executeJob();\r
330                         al = sexec.waitForResult();\r
331                         assertNotNull(al);\r
332 \r
333                 } catch (JobSubmissionException e) {\r
334                         e.printStackTrace();\r
335                         fail(e.getMessage());\r
336                 } catch (JobExecutionException e) {\r
337                         e.printStackTrace();\r
338                         fail(e.getMessage());\r
339                 } catch (FileNotFoundException e) {\r
340                         e.printStackTrace();\r
341                         fail(e.getMessage());\r
342                 } catch (IOException e) {\r
343                         e.printStackTrace();\r
344                         fail(e.getMessage());\r
345                 } catch (ResultNotAvailableException e) {\r
346                         e.printStackTrace();\r
347                         fail(e.getMessage());\r
348                 }\r
349         }\r
350 \r
351         @Test()\r
352         public void testConfigurationLoading() {\r
353                 try {\r
354                         RunnerConfig<AACon> aaconConfig = ConfExecutable\r
355                                         .getRunnerOptions(AACon.class);\r
356                         assertNotNull(aaconConfig);\r
357                         assertTrue(aaconConfig.getArguments().size() > 0);\r
358 \r
359                         PresetManager<AACon> aaconPresets = ConfExecutable\r
360                                         .getRunnerPresets(AACon.class);\r
361                         assertNull(aaconPresets); // there is no presets\r
362 \r
363                         LimitsManager<AACon> jronnLimits = ConfExecutable\r
364                                         .getRunnerLimits(AACon.class);\r
365                         assertNotNull(jronnLimits);\r
366                         assertTrue(jronnLimits.getLimits().size() > 0);\r
367                         jronnLimits.validate(aaconPresets);\r
368 \r
369                 } catch (FileNotFoundException e) {\r
370                         e.printStackTrace();\r
371                         fail(e.getLocalizedMessage());\r
372                 } catch (IOException e) {\r
373                         e.printStackTrace();\r
374                         fail(e.getLocalizedMessage());\r
375                 } catch (ValidationException e) {\r
376                         e.printStackTrace();\r
377                         fail(e.getLocalizedMessage());\r
378                 }\r
379         }\r
380 \r
381 }\r