Add testing dirs
[proteocache.git] / testsrc / compbio / casscode / conservation / AAConTester.java
diff --git a/testsrc/compbio/casscode/conservation/AAConTester.java b/testsrc/compbio/casscode/conservation/AAConTester.java
new file mode 100644 (file)
index 0000000..37f50cd
--- /dev/null
@@ -0,0 +1,387 @@
+/* Copyright (c) 2010 Peter Troshin\r
+ * Copyright (c) 2013 Alexander Sherstnev\r
+ * \r
+ * Java Bioinformatics Analysis Web Services (JABAWS)\r
+ * (JABAWS) @version: 2.5 \r
+ * \r
+ * This library is free software; you can redistribute it and/or modify it under \r
+ * the terms of the Apache License version 2 as published\r
+ * by the Apache Software Foundation This library is distributed in the hope\r
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * Apache License for more details. A copy of the license is in\r
+ * apache_license.txt. It is also available here:\r
+ * \r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt \r
+ * \r
+ * Any republication or derived work distributed in source code form must include \r
+ * this copyright and license notice.\r
+ */\r
+package compbio.runner.conservation;\r
+\r
+import static org.testng.Assert.assertEquals;\r
+import static org.testng.Assert.assertFalse;\r
+import static org.testng.Assert.assertNotNull;\r
+import static org.testng.Assert.assertTrue;\r
+import static org.testng.Assert.fail;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.text.ParseException;\r
+import java.util.Arrays;\r
+\r
+import javax.xml.bind.ValidationException;\r
+\r
+import org.ggf.drmaa.DrmaaException;\r
+import org.ggf.drmaa.JobInfo;\r
+import org.testng.annotations.BeforeMethod;\r
+import org.testng.annotations.Test;\r
+\r
+import compbio.data.sequence.ScoreManager;\r
+import compbio.engine.AsyncExecutor;\r
+import compbio.engine.Configurator;\r
+import compbio.engine.FilePuller;\r
+import compbio.engine.SyncExecutor;\r
+import compbio.engine.client.ConfExecutable;\r
+import compbio.engine.client.ConfiguredExecutable;\r
+import compbio.engine.client.Executable;\r
+import compbio.engine.client.RunConfiguration;\r
+import compbio.engine.cluster.drmaa.ClusterEngineUtil;\r
+import compbio.engine.cluster.drmaa.ClusterRunner;\r
+import compbio.engine.cluster.drmaa.StatisticManager;\r
+import compbio.engine.local.LocalRunner;\r
+import compbio.metadata.AllTestSuit;\r
+import compbio.metadata.ChunkHolder;\r
+import compbio.metadata.JobExecutionException;\r
+import compbio.metadata.JobStatus;\r
+import compbio.metadata.JobSubmissionException;\r
+import compbio.metadata.LimitsManager;\r
+import compbio.metadata.Preset;\r
+import compbio.metadata.PresetManager;\r
+import compbio.metadata.ResultNotAvailableException;\r
+import compbio.metadata.RunnerConfig;\r
+import compbio.runner.RunnerUtil;\r
+import compbio.util.FileWatcher;\r
+import compbio.util.SysPrefs;\r
+\r
+public class AAConTester {\r
+\r
+       public static final String CURRENT_DIRECTORY = SysPrefs.getCurrentDirectory() + File.separator;\r
+       public static String test_outfile = "TO1381.aacon.out";\r
+       public static String test_alignment_input = CURRENT_DIRECTORY + "testsrc" + File.separator + "testdata" + File.separator + "TO1381.fasta.aln";\r
+       private AACon aacon;\r
+\r
+       @BeforeMethod(alwaysRun = true)\r
+       void init() {\r
+               aacon = new AACon();\r
+               aacon.setInput(test_alignment_input).setOutput(test_outfile);\r
+       }\r
+\r
+       @Test(groups = {AllTestSuit.test_group_runner})\r
+       public void RunOnCluster() {\r
+               assertFalse(SysPrefs.isWindows, "Cluster execution can only be in unix environment");\r
+               try {\r
+                       ConfiguredExecutable<AACon> confAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Cluster);\r
+                       ClusterRunner runner = ClusterRunner.getInstance(confAAcon);\r
+\r
+                       assertNotNull(runner, "Runner is NULL");\r
+                       runner.executeJob();\r
+                       JobStatus status = runner.getJobStatus();\r
+                       assertTrue(status == JobStatus.PENDING || status == JobStatus.RUNNING, "Status of the process is wrong!");\r
+                       JobInfo info = runner.getJobInfo();\r
+                       assertNotNull(info, "JobInfo is null");\r
+                       StatisticManager sm = new StatisticManager(info);\r
+                       assertNotNull(sm, "Statictic manager is null");\r
+                       try {\r
+                               String exits = sm.getExitStatus();\r
+                               assertNotNull("Exit status is null", exits);\r
+                               // cut 4 trailing zeros from the number\r
+                               int exitsInt = ClusterEngineUtil.CLUSTER_STAT_IN_SEC.parse(exits).intValue();\r
+                               assertEquals(0, exitsInt);\r
+                               System.out.println(sm.getAllStats());\r
+\r
+                       } catch (ParseException e) {\r
+                               e.printStackTrace();\r
+                               fail("Parse Exception: " + e.getMessage());\r
+                       }\r
+                       assertTrue(sm.hasExited());\r
+                       assertFalse(sm.wasAborted());\r
+                       assertFalse(sm.hasDump());\r
+                       assertFalse(sm.hasSignaled());\r
+               } catch (JobSubmissionException e) {\r
+                       e.printStackTrace();\r
+                       fail("DrmaaException caught:" + e.getMessage());\r
+               } catch (JobExecutionException e) {\r
+                       e.printStackTrace();\r
+                       fail("DrmaaException caught:" + e.getMessage());\r
+               } catch (DrmaaException e) {\r
+                       e.printStackTrace();\r
+                       fail("DrmaaException caught:" + e.getMessage());\r
+               }\r
+       }\r
+\r
+       /**\r
+        * This test fails from time to time depending on the cluster load or some\r
+        * other factors. Any client code has to adjust for this issue\r
+        */\r
+       @Test(groups = {AllTestSuit.test_group_runner, AllTestSuit.test_group_cluster})\r
+       public void RunOnClusterAsync() {\r
+               assertFalse(SysPrefs.isWindows, "Cluster execution can only be in unix environment");\r
+               try {\r
+                       ConfiguredExecutable<AACon> confAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Cluster);\r
+                       AsyncExecutor aengine = Configurator.getAsyncEngine(confAAcon);\r
+                       String jobId = aengine.submitJob(confAAcon);\r
+                       assertNotNull(jobId, "Runner is NULL");\r
+                       // let drmaa to start\r
+                       Thread.sleep(500);\r
+                       JobStatus status = aengine.getJobStatus(jobId);\r
+                       while (status != JobStatus.FINISHED) {\r
+                               System.out.println("Job Status: " + status);\r
+                               Thread.sleep(1000);\r
+                               status = aengine.getJobStatus(jobId);\r
+                               ConfiguredExecutable<AACon> result = (ConfiguredExecutable<AACon>) aengine.getResults(jobId);\r
+                               assertNotNull(result);\r
+                               System.out.println("RES:" + result);\r
+                               // Some times the job could be removed from the cluster accounting \r
+                               // before it has been reported to finish. Make sure to stop waiting in such case\r
+                               if (status == JobStatus.UNDEFINED) {\r
+                                       break;\r
+                               }\r
+                       }\r
+               } catch (JobSubmissionException e) {\r
+                       e.printStackTrace();\r
+                       fail("DrmaaException caught:" + e.getMessage());\r
+               } catch (InterruptedException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               } catch (ResultNotAvailableException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               }\r
+       }\r
+\r
+       @Test(groups = {AllTestSuit.test_group_runner})\r
+       public void RunLocally() {\r
+               try {\r
+                       ConfiguredExecutable<AACon> confAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Local);\r
+\r
+                       // For local execution use relative\r
+                       LocalRunner lr = new LocalRunner(confAAcon);\r
+                       lr.executeJob();\r
+                       ConfiguredExecutable<?> al1 = lr.waitForResult();\r
+                       assertNotNull(al1.getResults());\r
+                       ScoreManager annotations = confAAcon.getResults();\r
+                       assertNotNull(annotations);\r
+                       assertEquals(annotations.asSet().size(), 18);\r
+                       assertEquals(al1.getResults(), annotations);\r
+               } catch (JobSubmissionException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               } catch (ResultNotAvailableException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               } catch (JobExecutionException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               }\r
+       }\r
+\r
+       @Test(groups = {AllTestSuit.test_group_runner})\r
+       public void RunLocallyWithPreset() {\r
+               try {\r
+                       PresetManager<AACon> aaconPresets = RunnerUtil.getPresets(AACon.class);\r
+                       assert aaconPresets != null;\r
+                       ConfiguredExecutable<AACon> confAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Local);\r
+                       Preset<AACon> quick = aaconPresets.getPresetByName("Quick conservation");\r
+                       confAAcon.addParameters(quick.getOptions());\r
+                       // For local execution use relative\r
+                       LocalRunner lr = new LocalRunner(confAAcon);\r
+                       lr.executeJob();\r
+                       ConfiguredExecutable<?> al1 = lr.waitForResult();\r
+                       assertNotNull(al1.getResults());\r
+                       ScoreManager annotations = confAAcon.getResults();\r
+                       assertNotNull(annotations);\r
+                       assertEquals(annotations.asSet().size(), 13);\r
+                       assertEquals(al1.getResults(), annotations);\r
+               } catch (JobSubmissionException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               } catch (ResultNotAvailableException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               } catch (JobExecutionException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               }\r
+       }\r
+\r
+       @Test(groups = {AllTestSuit.test_group_runner})\r
+       public void RunLocallyOnTwoCpu() {\r
+               try {\r
+                       aacon = new AACon();\r
+                       aacon.setInput(test_alignment_input).setOutput(test_outfile);\r
+\r
+                       aacon.setNCore(2);\r
+                       ConfiguredExecutable<AACon> confAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Local);\r
+                       confAAcon.addParameters(Arrays.asList("-m=KABAT,JORES"));\r
+                       confAAcon.addParameters(Arrays.asList("-m=TAYLOR_GAPS"));\r
+                       // For local execution use relative\r
+                       LocalRunner lr = new LocalRunner(confAAcon);\r
+\r
+                       lr.executeJob();\r
+                       ConfiguredExecutable<?> al1 = lr.waitForResult();\r
+                       assertNotNull(al1.getResults());\r
+                       ScoreManager annotations = confAAcon.getResults();\r
+                       assertNotNull(annotations);\r
+                       assertEquals(annotations.asSet().size(), 3);\r
+                       assertEquals(al1.getResults(), annotations);\r
+               } catch (JobSubmissionException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               } catch (ResultNotAvailableException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               } catch (JobExecutionException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               }\r
+       }\r
+       \r
+       @Test(groups = {AllTestSuit.test_group_runner})\r
+       public void readStatistics() {\r
+               try {\r
+                       ConfiguredExecutable<AACon> confAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Local);\r
+                       // For local execution use relative\r
+\r
+                       AsyncExecutor sexec = Configurator.getAsyncEngine(confAAcon);\r
+                       String jobId = sexec.submitJob(confAAcon);\r
+                       String file = confAAcon.getWorkDirectory() + File.separator + AACon.getStatFile();\r
+                       FilePuller fw = FilePuller.newFilePuller(file, FileWatcher.MIN_CHUNK_SIZE_BYTES);\r
+                       int count = 0;\r
+                       long position = 0;\r
+                       fw.waitForFile(2);\r
+                       JobStatus status = sexec.getJobStatus(jobId);\r
+                       do {\r
+                               if (fw.hasMoreData()) {\r
+                                       ChunkHolder ch = fw.pull(position);\r
+                                       String chunk = ch.getChunk();\r
+                                       position = ch.getNextPosition();\r
+                               }\r
+                               count++;\r
+                               // Make sure the loop is terminated if the job fails\r
+                               if ((status == JobStatus.UNDEFINED || status == JobStatus.FAILED)) {\r
+                                       fail("job failed!");\r
+                                       break;\r
+                               }\r
+                               Thread.sleep(300);\r
+                               status = sexec.getJobStatus(jobId);\r
+                       } while (status != JobStatus.FINISHED || fw.hasMoreData());\r
+                       assertTrue(count >= 1);\r
+                       ConfiguredExecutable<?> al = sexec.getResults(jobId);\r
+                       assertNotNull(al.getResults());\r
+               } catch (JobSubmissionException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               } catch (ResultNotAvailableException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               } catch (IOException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               } catch (InterruptedException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               }\r
+       }\r
+\r
+       @Test(groups = {AllTestSuit.test_group_runner})\r
+       public void Persistance() {\r
+               try {\r
+                       AACon aacon = new AACon();\r
+                       aacon.setError("errrr.txt");\r
+                       aacon.setInput(test_alignment_input);\r
+                       aacon.setOutput("outtt.txt");\r
+                       assertEquals(aacon.getInput(), test_alignment_input);\r
+                       assertEquals(aacon.getError(), "errrr.txt");\r
+                       assertEquals(aacon.getOutput(), "outtt.txt");\r
+                       ConfiguredExecutable<AACon> cAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Local);\r
+\r
+                       SyncExecutor sexec = Configurator.getSyncEngine(cAAcon);\r
+                       sexec.executeJob();\r
+                       ConfiguredExecutable<?> al = sexec.waitForResult();\r
+                       assertNotNull(al.getResults());\r
+\r
+                       // Save run configuration\r
+                       assertTrue(cAAcon.saveRunConfiguration());\r
+\r
+                       // See if loaded configuration is the same as saved\r
+                       RunConfiguration loadedRun = RunConfiguration\r
+                                       .load(new FileInputStream(new File(cAAcon\r
+                                                       .getWorkDirectory(), RunConfiguration.rconfigFile)));\r
+                       assertEquals(((ConfExecutable<AACon>) cAAcon).getRunConfiguration(),loadedRun);\r
+\r
+                       // Load run configuration as ConfExecutable\r
+                       ConfiguredExecutable<AACon> resurrectedCAAcon = (ConfiguredExecutable<AACon>) cAAcon\r
+                                       .loadRunConfiguration(new FileInputStream(new File(cAAcon\r
+                                                       .getWorkDirectory(), RunConfiguration.rconfigFile)));\r
+                       assertNotNull(resurrectedCAAcon);\r
+                       assertEquals(resurrectedCAAcon.getExecutable().getInput(), test_alignment_input);\r
+                       assertEquals(resurrectedCAAcon.getExecutable().getError(), "errrr.txt");\r
+                       assertEquals(resurrectedCAAcon.getExecutable().getOutput(), "outtt.txt");\r
+\r
+                       // See in details whether executables are the same\r
+                       assertEquals(resurrectedCAAcon.getExecutable(), aacon);\r
+\r
+                       ConfiguredExecutable<AACon> resAAcon = Configurator.configureExecutable(resurrectedCAAcon.getExecutable(),\r
+                                                       Executable.ExecProvider.Local);\r
+\r
+                       sexec = Configurator.getSyncEngine(resAAcon, Executable.ExecProvider.Local);\r
+                       sexec.executeJob();\r
+                       al = sexec.waitForResult();\r
+                       assertNotNull(al);\r
+               } catch (JobSubmissionException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               } catch (JobExecutionException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               } catch (FileNotFoundException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               } catch (IOException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               } catch (ResultNotAvailableException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               }\r
+       }\r
+\r
+       @Test(groups = {AllTestSuit.test_group_runner})\r
+       public void ConfigurationLoading() {\r
+               try {\r
+                       RunnerConfig<AACon> aaconConfig = ConfExecutable.getRunnerOptions(AACon.class);\r
+                       assertNotNull(aaconConfig);\r
+                       assertTrue(aaconConfig.getArguments().size() > 0);\r
+\r
+                       PresetManager<AACon> aaconPresets = ConfExecutable.getRunnerPresets(AACon.class);\r
+                       assertNotNull(aaconPresets);\r
+\r
+                       LimitsManager<AACon> aaconLimits = ConfExecutable.getRunnerLimits(AACon.class);\r
+                       assertNotNull(aaconLimits);\r
+                       assertTrue(aaconLimits.getLimits().size() > 0);\r
+                       aaconLimits.validate(aaconPresets);\r
+               } catch (FileNotFoundException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               } catch (IOException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               } catch (ValidationException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               }\r
+       }\r
+}\r