Necessary modifications in JABAWS for Jpred
authorSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Wed, 7 Aug 2013 16:28:09 +0000 (17:28 +0100)
committerSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Wed, 7 Aug 2013 16:28:09 +0000 (17:28 +0100)
datamodel/compbio/data/sequence/Program.java
datamodel/compbio/data/sequence/SequenceUtil.java
runner/compbio/runner/predictors/Jpred.java
testsrc/compbio/runner/predictors/JpredTester.java [new file with mode: 0644]

index 2ae3c3a..e5f4e76 100644 (file)
@@ -49,5 +49,9 @@ public enum Program {
        /**\r
         * Probcons\r
         */\r
-       Probcons\r
+       Probcons,\r
+       /**\r
+        * Jpred\r
+        */\r
+       Jpred\r
 }\r
index 14dedf4..3a19e44 100644 (file)
@@ -23,6 +23,7 @@ import java.io.BufferedWriter;
 import java.io.Closeable;\r
 import java.io.File;\r
 import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
 import java.io.IOException;\r
 import java.io.InputStream;\r
 import java.io.InputStreamReader;\r
@@ -30,6 +31,7 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;\r
 import java.util.ArrayList;\r
 import java.util.HashMap;\r
+import java.util.Collections;\r
 import java.util.HashSet;\r
 import java.util.List;\r
 import java.util.Map;\r
@@ -286,6 +288,12 @@ public final class SequenceUtil {
                outWriter.close();\r
        }\r
 \r
+       \r
+       public static final List<FastaSequence> readJpredFile(InputStream result)\r
+                       throws IOException, FileNotFoundException,NullPointerException {\r
+               return readFasta (result);\r
+       }\r
+       \r
        /**\r
         * Read IUPred output\r
         * \r
@@ -299,7 +307,7 @@ public final class SequenceUtil {
                InputStream input = new FileInputStream(result);\r
                Map<String, Score> sequences = readIUPred(input,\r
                                IUPredResult.getType(result));\r
-               input.close();\r
+               input.close(); \r
                return sequences;\r
        }\r
 \r
index 017dcfc..7c27a37 100644 (file)
@@ -28,17 +28,21 @@ import java.util.List;
 \r
 import org.apache.log4j.Logger;\r
 \r
+import compbio.data.sequence.Alignment;\r
+import compbio.data.sequence.AlignmentMetadata;\r
+import compbio.data.sequence.Program;\r
 import compbio.data.sequence.ScoreManager;\r
 import compbio.data.sequence.SequenceUtil;\r
 import compbio.engine.client.CommandBuilder;\r
 import compbio.engine.client.Executable;\r
 import compbio.engine.client.SkeletalExecutable;\r
 import compbio.metadata.ResultNotAvailableException;\r
+import compbio.runner.Util;\r
 \r
 /**\r
  * Command line\r
  * \r
- * jpred.pl -in d16vpa_.fas -out res_d16vpa_ -dbname ported_db -dbpath /data/UNIREFdb -ncpu 4\r
+ * jpred.pl -in d16vpa_.fas -outfile res_d16vpa_ -dbname ported_db -dbpath /data/UNIREFdb -ncpu 4\r
  * \r
  * @author asherstnev\r
  * \r
@@ -57,17 +61,17 @@ public class Jpred extends SkeletalExecutable<Jpred> {
        public static final String STAT_FILE = "stat.txt";\r
 \r
        public Jpred() {\r
-//             addParameters(Arrays.asList());\r
+               addParameters(Arrays.asList("-logfile " + STAT_FILE));\r
        }\r
+\r
        // HashMap<Method, float[]>\r
        @Override\r
-       public ScoreManager getResults(String workDirectory)\r
+       public Alignment getResults(String workDirectory)\r
                        throws ResultNotAvailableException {\r
-               ScoreManager annotations = null;\r
+               Alignment annotations = null;\r
                try {\r
                        InputStream inStream = new FileInputStream(new File(workDirectory, getOutput()));\r
-                       annotations = ScoreManager.newInstanceSingleSequence(SequenceUtil.readAAConResults(inStream));\r
-                       inStream.close();\r
+                       annotations = new Alignment(SequenceUtil.readJpredFile(inStream), new AlignmentMetadata(Program.Jpred, '-'));\r
                } catch (FileNotFoundException e) {\r
                        log.error(e.getMessage(), e.getCause());\r
                        throw new ResultNotAvailableException(e);\r
@@ -96,7 +100,7 @@ public class Jpred extends SkeletalExecutable<Jpred> {
        @Override\r
        public Jpred setOutput(String outFile) {\r
                super.setOutput(outFile);\r
-               cbuilder.setParam("-out " + outFile);\r
+               cbuilder.setParam("-outfile " + outFile);\r
                return this;\r
        }\r
 \r
diff --git a/testsrc/compbio/runner/predictors/JpredTester.java b/testsrc/compbio/runner/predictors/JpredTester.java
new file mode 100644 (file)
index 0000000..7d6c61d
--- /dev/null
@@ -0,0 +1,363 @@
+/*\r
+ * Copyright (c) 2010 Alexander Sherstnev, Java Bioinformatics Analysis Web Services\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.predictors;\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.Alignment;\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.ClusterUtil;\r
+import compbio.engine.cluster.drmaa.JobRunner;\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.Util;\r
+import compbio.util.FileWatcher;\r
+import compbio.util.SysPrefs;\r
+\r
+public class JpredTester {\r
+\r
+       public static final String CURRENT_DIRECTORY = SysPrefs.getCurrentDirectory() + File.separator;\r
+\r
+       public static String test_output = "Jpred.test1.out";\r
+       public static String test_input = CURRENT_DIRECTORY + "testsrc" + File.separator + "testdata" + File.separator + "Jpred.test1.fasta";\r
+       private Jpred pred;\r
+\r
+       @BeforeMethod(alwaysRun = true)\r
+       void init() {\r
+               pred = new Jpred();\r
+               pred.setInput(test_input);\r
+               pred.setOutput(test_output);\r
+       }\r
+\r
+       @Test(groups = {AllTestSuit.test_group_runner})\r
+       public void testRunOnCluster() {\r
+               assertFalse(SysPrefs.isWindows, "Cluster execution can only be in unix environment");\r
+               try {\r
+                       PresetManager<Jpred> jpredPreset = Util.getPresets(Jpred.class);\r
+                       assert jpredPreset != null;\r
+                       ConfiguredExecutable<Jpred> confpred = Configurator.configureExecutable(pred, Executable.ExecProvider.Cluster);\r
+                       Preset<Jpred> conf = jpredPreset.getPresetByName("cluster configuration");\r
+                       confpred.addParameters(conf.getOptions());\r
+                       JobRunner runner = JobRunner.getInstance(confpred);\r
+                       assertNotNull(runner, "Runner is NULL");\r
+\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, "Statistic 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 = ClusterUtil.CLUSTER_STAT_IN_SEC.parse(exits).intValue();\r
+                               assertEquals(0, exitsInt);\r
+                               System.out.println(sm.getAllStats());\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
+\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 tests 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 testRunOnClusterAsync() {\r
+               assertFalse(SysPrefs.isWindows, "Cluster execution can only be in unix environment");\r
+               try {\r
+                       pred.addParameters(Arrays.asList("-dbname uniref90", "-dbpath /homes/www-jpred/databases/"));\r
+                       ConfiguredExecutable<Jpred> confpred = Configurator.configureExecutable(pred, Executable.ExecProvider.Cluster);\r
+                       AsyncExecutor aengine = Configurator.getAsyncEngine(confpred);\r
+                       String jobId = aengine.submitJob(confpred);\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
+                               Thread.sleep(1000);\r
+                               status = aengine.getJobStatus(jobId);\r
+                               ConfiguredExecutable<Jpred> result = (ConfiguredExecutable<Jpred>) aengine.getResults(jobId);\r
+                               assertNotNull(result);\r
+                               if (status == JobStatus.UNDEFINED || status == JobStatus.FAILED) {\r
+                                       fail("job " + jobId +" failed!");\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 testRunLocally() {\r
+               try {\r
+                       ConfiguredExecutable<Jpred> confpred = Configurator.configureExecutable(pred, Executable.ExecProvider.Local);\r
+                       confpred.addParameters(Arrays.asList("-dbname ported_db", "-dbpath /data/UNIREFdb/"));\r
+\r
+                       // For local execution use relative\r
+                       LocalRunner lr = new LocalRunner(confpred);\r
+                       lr.executeJob();\r
+                       ConfiguredExecutable<?> al1 = lr.waitForResult();\r
+                       assertNotNull(al1.getResults());\r
+                       Alignment annotations = confpred.getResults();\r
+                       assertNotNull(annotations);\r
+                       assertEquals(annotations.getSize(), 19);\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 testRunLocallyWithPreset() {\r
+               try {\r
+                       ConfiguredExecutable<Jpred> confpred = Configurator.configureExecutable(pred, Executable.ExecProvider.Local);\r
+                       PresetManager<Jpred> preset = Util.getPresets(Jpred.class);\r
+                       assert preset != null;\r
+                       Preset<Jpred> conf = preset.getPresetByName("laptop configuration");\r
+                       confpred.addParameters(conf.getOptions());\r
+\r
+                       // For local execution use relative\r
+                       LocalRunner lr = new LocalRunner(confpred);\r
+                       lr.executeJob();\r
+                       ConfiguredExecutable<?> al1 = lr.waitForResult();\r
+                       assertNotNull(al1.getResults());\r
+                       Alignment annotations = confpred.getResults();\r
+                       assertNotNull(annotations);\r
+                       assertEquals(annotations.getSize(), 19);\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
+                       Jpred jpred = new Jpred();\r
+                       jpred.setInput(test_input);\r
+                       jpred.setOutput(test_output);\r
+                       ConfiguredExecutable<Jpred> confpred = Configurator.configureExecutable(jpred, Executable.ExecProvider.Local);\r
+                       PresetManager<Jpred> preset = Util.getPresets(Jpred.class);\r
+                       assert preset != null;\r
+                       Preset<Jpred> conf = preset.getPresetByName("laptop configuration");\r
+                       confpred.addParameters(conf.getOptions());\r
+\r
+                       // For local execution use relative\r
+                       AsyncExecutor sexec = Configurator.getAsyncEngine(confpred);\r
+                       String jobId = sexec.submitJob(confpred);\r
+                       String file = confpred.getWorkDirectory() + File.separator + Jpred.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 " + jobId +" failed!");\r
+                                       break;\r
+                               }\r
+                               Thread.sleep(200);\r
+                               status = sexec.getJobStatus(jobId);\r
+                       }  while (status != JobStatus.FINISHED || fw.hasMoreData());\r
+\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 testPersistance() {\r
+               try {\r
+                       Jpred jpred = new Jpred();\r
+                       jpred.setError("errrr.txt");\r
+                       jpred.setInput(test_input);\r
+                       jpred.setOutput("outtt.txt");\r
+                       assertEquals(jpred.getInput(), test_input);\r
+                       assertEquals(jpred.getError(), "errrr.txt");\r
+                       assertEquals(jpred.getOutput(), "outtt.txt");\r
+                       ConfiguredExecutable<Jpred> confpred = Configurator.configureExecutable(jpred, Executable.ExecProvider.Local);\r
+                       confpred.addParameters(Arrays.asList("-dbname ported_db", "-dbpath /data/UNIREFdb/"));\r
+\r
+                       SyncExecutor sexec = Configurator.getSyncEngine(confpred);\r
+                       sexec.executeJob();\r
+                       ConfiguredExecutable<?> al = sexec.waitForResult();\r
+                       assertNotNull(al.getResults());\r
+\r
+                       // Save run configuration\r
+                       assertTrue(confpred.saveRunConfiguration());\r
+\r
+                       // See if loaded configuration is the same as saved\r
+                       RunConfiguration loadedRun = RunConfiguration.load(new FileInputStream(new File(confpred.getWorkDirectory(), RunConfiguration.rconfigFile)));\r
+                       assertEquals(((ConfExecutable<Jpred>) confpred).getRunConfiguration(),loadedRun);\r
+\r
+                       // Load run configuration as ConfExecutable\r
+                       ConfiguredExecutable<Jpred> jpred2 = (ConfiguredExecutable<Jpred>) confpred.loadRunConfiguration(new FileInputStream(new File(confpred.getWorkDirectory(), RunConfiguration.rconfigFile)));\r
+                       assertNotNull(jpred2);\r
+                       assertEquals(jpred2.getExecutable().getInput(), test_input);\r
+                       assertEquals(jpred2.getExecutable().getError(), "errrr.txt");\r
+                       assertEquals(jpred2.getExecutable().getOutput(), "outtt.txt");\r
+\r
+                       // See in details whether executables are the same\r
+                       assertEquals(jpred2.getExecutable(), jpred);\r
+                       ConfiguredExecutable<Jpred> resjpred2 = Configurator.configureExecutable(jpred2.getExecutable(), Executable.ExecProvider.Local);\r
+\r
+                       sexec = Configurator.getSyncEngine(resjpred2, 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 testConfigurationLoading() {\r
+               try {\r
+                       RunnerConfig<Jpred> jpredConfig = ConfExecutable.getRunnerOptions(Jpred.class);\r
+                       assertNotNull(jpredConfig);\r
+                       assertTrue(jpredConfig.getArguments().size() > 0);\r
+\r
+                       PresetManager<Jpred> jpredPreset = ConfExecutable.getRunnerPresets(Jpred.class);\r
+                       assertNotNull(jpredPreset);\r
+\r
+                       LimitsManager<Jpred> jpredLimits = ConfExecutable.getRunnerLimits(Jpred.class);\r
+                       assertNotNull(jpredLimits);\r
+                       assertTrue(jpredLimits.getLimits().size() > 0);\r
+                       jpredLimits.validate(jpredPreset);\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
+}\r