Final fixing of Jpred problem with casting
[jabaws.git] / webservices / compbio / ws / server / WSUtil.java
index dd1051f..d0c9226 100644 (file)
@@ -1,6 +1,6 @@
-/* Copyright (c) 2009 Peter Troshin\r
+/* Copyright (c) 2011 Peter Troshin\r
  *  \r
- *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0\r
+ *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
  * \r
  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
  *  Apache License version 2 as published by the Apache Software Foundation\r
@@ -19,9 +19,14 @@ package compbio.ws.server;
 \r
 import java.security.InvalidParameterException;\r
 import java.util.ArrayList;\r
+import java.util.HashSet;\r
 import java.util.List;\r
+import java.util.Set;\r
+\r
+import org.apache.log4j.Logger;\r
 \r
 import compbio.data.sequence.FastaSequence;\r
+import compbio.data.sequence.ScoreManager;\r
 import compbio.engine.AsyncExecutor;\r
 import compbio.engine.Configurator;\r
 import compbio.engine.ProgressGetter;\r
@@ -32,77 +37,177 @@ import compbio.metadata.JobSubmissionException;
 import compbio.metadata.Limit;\r
 import compbio.metadata.LimitExceededException;\r
 import compbio.metadata.Option;\r
-import compbio.util.Timer;\r
+import compbio.metadata.ResultNotAvailableException;\r
+import compbio.ws.client.Services;\r
+import compbio.ws.client.ServicesUtil;\r
 \r
 public final class WSUtil {\r
 \r
-    public static final void validateJobId(String jobId)\r
-           throws InvalidParameterException {\r
-       if (!compbio.engine.client.Util.isValidJobId(jobId)) {\r
-           throw new InvalidParameterException(\r
-                   "JobId is not provided or cannot be recognised! Given value: "\r
-                           + jobId);\r
+       public static final void validateJobId(String jobId)\r
+                       throws InvalidParameterException {\r
+               if (!compbio.engine.client.Util.isValidJobId(jobId)) {\r
+                       throw new InvalidParameterException(\r
+                                       "JobId is not provided or cannot be recognised! Given value: "\r
+                                                       + jobId);\r
+               }\r
+       }\r
+\r
+       public static final void validateFastaInput(List<FastaSequence> sequences)\r
+                       throws JobSubmissionException {\r
+               if (sequences == null || sequences.isEmpty()) {\r
+                       throw new JobSubmissionException(\r
+                                       "List of fasta sequences required but not provided! ");\r
+               }\r
+               Set<String> names = new HashSet<String>();\r
+               for (FastaSequence fs : sequences) {\r
+                       boolean unique = names.add(fs.getId());\r
+                       if (!unique) {\r
+                               throw new JobSubmissionException(\r
+                                               "Input sequences must have unique names! \n"\r
+                                                               + "Sequence " + fs.getId() + " is a duplicate!");\r
+                       }\r
+                       if (fs.getLength() == 0) {\r
+                               throw new JobSubmissionException(\r
+                                               "Sequence must not be empty! Sequence: " + fs.getId()\r
+                                                               + " was empty");\r
+                       }\r
+               }\r
        }\r
-    }\r
 \r
-    public static final void validateFastaInput(List<FastaSequence> sequences)\r
-           throws InvalidParameterException {\r
-       if (sequences == null || sequences.isEmpty()) {\r
-           throw new InvalidParameterException(\r
-                   "List of fasta sequences required but not provided! ");\r
+       public static JobStatus getJobStatus(String jobId) {\r
+               AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
+               return asyncEngine.getJobStatus(jobId);\r
        }\r
-    }\r
-\r
-    public static JobStatus getJobStatus(String jobId) {\r
-       AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
-       return asyncEngine.getJobStatus(jobId);\r
-    }\r
-\r
-    public static ChunkHolder pullFile(String file, long position) {\r
-       return ProgressGetter.pull(file, position);\r
-    }\r
-\r
-    public static byte getProgress(String jobId) {\r
-       throw new UnsupportedOperationException();\r
-    }\r
-\r
-    public static AsyncExecutor getEngine(ConfiguredExecutable<?> confClustal) {\r
-       assert confClustal != null;\r
-       return Configurator.getAsyncEngine(confClustal);\r
-    }\r
-\r
-    public static boolean cancelJob(String jobId) {\r
-       AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
-       return asyncEngine.cancelJob(jobId);\r
-    }\r
-\r
-    public static <T> String align(List<FastaSequence> sequences,\r
-           ConfiguredExecutable<T> confExec, WSLogger logger,\r
-           String callingMethod, Limit<T> limit)\r
-           throws LimitExceededException, JobSubmissionException {\r
-       Timer timer = Timer.getMilliSecondsTimer();\r
-       if (limit.isExceeded(sequences)) {\r
-           throw LimitExceededException.newLimitExceeded(limit, sequences);\r
+\r
+       public static ChunkHolder pullFile(String file, long position) {\r
+               return ProgressGetter.pull(file, position);\r
        }\r
-       compbio.runner.Util.writeInput(sequences, confExec);\r
-       AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
-       String jobId = engine.submitJob(confExec);\r
-       if (logger != null) {\r
-           logger.log(timer, callingMethod, jobId);\r
+\r
+       public static byte getProgress(String jobId) {\r
+               throw new UnsupportedOperationException();\r
+       }\r
+\r
+       public static AsyncExecutor getEngine(ConfiguredExecutable<?> confClustal) {\r
+               assert confClustal != null;\r
+               return Configurator.getAsyncEngine(confClustal);\r
        }\r
-       return jobId;\r
-    }\r
-\r
-    /*\r
-     * TODO Rewrite using purely CommandBuilder. This is breaking encapsulation\r
-     */\r
-    public static final <T> List<String> getCommands(List<Option<T>> options,\r
-           String keyValueSeparator) {\r
-       List<String> oList = new ArrayList<String>();\r
-       for (Option<T> o : options) {\r
-           oList.add(o.toCommand(keyValueSeparator));\r
+\r
+       public static boolean cancelJob(String jobId) {\r
+               AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
+               return asyncEngine.cancelJob(jobId);\r
+       }\r
+\r
+       public static <T> String align(List<FastaSequence> sequences,\r
+                       ConfiguredExecutable<T> confExec, Logger logger,\r
+                       String callingMethod, Limit<T> limit)\r
+                       throws LimitExceededException, JobSubmissionException {\r
+\r
+               if (limit != null && limit.isExceeded(sequences)) {\r
+                       throw LimitExceededException.newLimitExceeded(limit, sequences);\r
+               }\r
+               compbio.runner.Util.writeInput(sequences, confExec);\r
+               AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
+               String jobId = engine.submitJob(confExec);\r
+               reportUsage(confExec, logger);\r
+               return jobId;\r
+       }\r
+       \r
+\r
+       static <T> void reportUsage(ConfiguredExecutable<T> confExec, Logger logger) {\r
+               if (GAUtils.IS_GA_ENABLED) {\r
+                       Services service = ServicesUtil.getServiceByRunner(confExec.getExecutable().getClass());\r
+                       GAUtils.reportUsage(service);\r
+                       logger.info("Reporting GA usage for " + service);\r
+               }\r
+       }\r
+\r
+       public static <T> String analize(List<FastaSequence> sequences,\r
+                       ConfiguredExecutable<T> confExec, Logger log, String method,\r
+                       Limit<T> limit) throws JobSubmissionException {\r
+               if (limit != null && limit.isExceeded(sequences)) {\r
+                       throw LimitExceededException.newLimitExceeded(limit, sequences);\r
+               }\r
+               compbio.runner.Util.writeInput(sequences, confExec);\r
+               AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
+               String jobId = engine.submitJob(confExec);\r
+               reportUsage(confExec, log);\r
+               return jobId;\r
+       }\r
+\r
+       // Same as analize(...) but RNAalifold takes clustal input not fasta\r
+       // An if condition in the above method might be a better solution but \r
+       // you need a way of finding out the type of confExec at runtime\r
+       \r
+       public static <T> String fold(List<FastaSequence> sequences,\r
+                       ConfiguredExecutable<T> confExec, Logger log, String method,\r
+                       Limit<T> limit) throws JobSubmissionException {\r
+               if (limit != null && limit.isExceeded(sequences)) {\r
+                       throw LimitExceededException.newLimitExceeded(limit, sequences);\r
+               }\r
+               compbio.runner.Util.writeClustalInput(sequences, confExec, '-');\r
+               AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
+               String jobId = engine.submitJob(confExec);\r
+               reportUsage(confExec, log);\r
+               return jobId;\r
+       }\r
+\r
+       /*\r
+        * TODO Rewrite using purely CommandBuilder. This is breaking encapsulation\r
+        */\r
+       public static final <T> List<String> getCommands(List<Option<T>> options,\r
+                       String keyValueSeparator) {\r
+               List<String> oList = new ArrayList<String>();\r
+               for (Option<T> o : options) {\r
+                       oList.add(o.toCommand(keyValueSeparator));\r
+               }\r
+               return oList;\r
+       }\r
+\r
+       public static void validateAAConInput(List<FastaSequence> sequences)\r
+                       throws JobSubmissionException {\r
+               validateFastaInput(sequences);\r
+               int len = 0;\r
+               for (FastaSequence fs : sequences) {\r
+                       if (len == 0) {\r
+                               len = fs.getLength();\r
+                               continue;\r
+                       }\r
+                       if (fs.getLength() != len) {\r
+                               throw new JobSubmissionException(\r
+                                               "All sequences must be of the same length. Please align the sequences " + \r
+                                               " prior to submission! The first sequence length is : " + len + \r
+                                               " but the sequence '" + fs.getId() + "' length is " + fs.getLength());\r
+                       }\r
+               }\r
+       }\r
+\r
+       public static void validateJpredInput(List<FastaSequence> sequences)\r
+                       throws JobSubmissionException {\r
+               validateFastaInput(sequences);\r
+               int len = 0;\r
+               for (FastaSequence fs : sequences) {\r
+                       if (len == 0) {\r
+                               len = fs.getLength();\r
+                               continue;\r
+                       }\r
+                       if (fs.getLength() != len) {\r
+                               System.out.println("FASTA rec: id = " + fs.getId() + ": seq = " + fs.getSequence());\r
+                               throw new JobSubmissionException(\r
+                                               "All sequences must be of the same length. Please align the sequences " + \r
+                                               " prior to submission! The first sequence length is : " + len + \r
+                                               " but the sequence '" + fs.getId() + "' length is " + fs.getLength());\r
+                       }\r
+               }\r
+       }\r
+\r
+       public static <T> ScoreManager getAnnotation(String jobId, Logger log)\r
+                       throws ResultNotAvailableException {\r
+               WSUtil.validateJobId(jobId);\r
+               AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
+               ConfiguredExecutable<T> aacon = (ConfiguredExecutable<T>) asyncEngine.getResults(jobId);\r
+               ScoreManager mas = aacon.getResults();\r
+               log.trace(jobId + " getConservation : " + mas);\r
+               return mas;\r
        }\r
-       return oList;\r
-    }\r
 \r
 }\r