Webservice works! but only supports some parameters. Replacing RNAstuct
[jabaws.git] / webservices / compbio / ws / server / WSUtil.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\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\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.ws.server;\r
19 \r
20 import java.security.InvalidParameterException;\r
21 import java.util.ArrayList;\r
22 import java.util.HashSet;\r
23 import java.util.List;\r
24 import java.util.Set;\r
25 \r
26 import org.apache.log4j.Logger;\r
27 \r
28 import compbio.data.sequence.Alignment;\r
29 import compbio.data.sequence.FastaSequence;\r
30 import compbio.data.sequence.ScoreManager;\r
31 import compbio.engine.AsyncExecutor;\r
32 import compbio.engine.Configurator;\r
33 import compbio.engine.ProgressGetter;\r
34 import compbio.engine.client.ConfiguredExecutable;\r
35 import compbio.metadata.ChunkHolder;\r
36 import compbio.metadata.JobStatus;\r
37 import compbio.metadata.JobSubmissionException;\r
38 import compbio.metadata.Limit;\r
39 import compbio.metadata.LimitExceededException;\r
40 import compbio.metadata.Option;\r
41 import compbio.metadata.ResultNotAvailableException;\r
42 import compbio.ws.client.Services;\r
43 import compbio.ws.client.ServicesUtil;\r
44 \r
45 public final class WSUtil {\r
46 \r
47         public static final void validateJobId(String jobId)\r
48                         throws InvalidParameterException {\r
49                 if (!compbio.engine.client.Util.isValidJobId(jobId)) {\r
50                         throw new InvalidParameterException(\r
51                                         "JobId is not provided or cannot be recognised! Given value: "\r
52                                                         + jobId);\r
53                 }\r
54         }\r
55 \r
56         public static final void validateFastaInput(List<FastaSequence> sequences)\r
57                         throws JobSubmissionException {\r
58                 if (sequences == null || sequences.isEmpty()) {\r
59                         throw new JobSubmissionException(\r
60                                         "List of fasta sequences required but not provided! ");\r
61                 }\r
62                 Set<String> names = new HashSet<String>();\r
63                 for (FastaSequence fs : sequences) {\r
64                         boolean unique = names.add(fs.getId());\r
65                         if (!unique) {\r
66                                 throw new JobSubmissionException(\r
67                                                 "Input sequences must have unique names! \n"\r
68                                                                 + "Sequence " + fs.getId() + " is a duplicate!");\r
69                         }\r
70                         if (fs.getLength() == 0) {\r
71                                 throw new JobSubmissionException(\r
72                                                 "Sequence must not be empty! Sequence: " + fs.getId()\r
73                                                                 + " was empty");\r
74                         }\r
75                 }\r
76         }\r
77 \r
78         public static JobStatus getJobStatus(String jobId) {\r
79                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
80                 return asyncEngine.getJobStatus(jobId);\r
81         }\r
82 \r
83         public static ChunkHolder pullFile(String file, long position) {\r
84                 return ProgressGetter.pull(file, position);\r
85         }\r
86 \r
87         public static byte getProgress(String jobId) {\r
88                 throw new UnsupportedOperationException();\r
89         }\r
90 \r
91         public static AsyncExecutor getEngine(ConfiguredExecutable<?> confClustal) {\r
92                 assert confClustal != null;\r
93                 return Configurator.getAsyncEngine(confClustal);\r
94         }\r
95 \r
96         public static boolean cancelJob(String jobId) {\r
97                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
98                 return asyncEngine.cancelJob(jobId);\r
99         }\r
100 \r
101         public static <T> String align(List<FastaSequence> sequences,\r
102                         ConfiguredExecutable<T> confExec, Logger logger,\r
103                         String callingMethod, Limit<T> limit)\r
104                         throws LimitExceededException, JobSubmissionException {\r
105 \r
106                 if (limit != null && limit.isExceeded(sequences)) {\r
107                         throw LimitExceededException.newLimitExceeded(limit, sequences);\r
108                 }\r
109                 compbio.runner.Util.writeInput(sequences, confExec);\r
110                 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
111                 String jobId = engine.submitJob(confExec);\r
112                 reportUsage(confExec, logger);\r
113                 return jobId;\r
114         }\r
115         \r
116         // Hardcoded gapchar '-' in this method\r
117         \r
118         public static <T> String fold(Alignment alignment,\r
119                         ConfiguredExecutable<T> confExec, Logger logger,\r
120                         String callingMethod, Limit<T> limit)\r
121                         throws LimitExceededException, JobSubmissionException {\r
122                 \r
123                 List<FastaSequence> sequences = alignment.getSequences();\r
124                 if (limit != null && limit.isExceeded(sequences)) {\r
125                         throw LimitExceededException.newLimitExceeded(limit, sequences);\r
126                 }\r
127                 compbio.runner.Util.writeClustalInput(sequences, confExec, '-');\r
128                 System.out.println("WSUtil-fold: Writing the clustal input file on the server: to file: "\r
129                                 + confExec.getInput());\r
130                 System.out.println("WSUtil-fold: The executable is configured with parameters: " \r
131                                 + confExec.getParameters());\r
132 //              System.out.println("WSUTil-fold: Dump the configured executable:\n" \r
133 //                              + confExec.toString());\r
134                 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
135                 String jobId = engine.submitJob(confExec);\r
136                 reportUsage(confExec, logger);\r
137                 return jobId;\r
138         }\r
139 \r
140         static <T> void reportUsage(ConfiguredExecutable<T> confExec, Logger logger) {\r
141                 if (GAUtils.IS_GA_ENABLED) {\r
142                         Services service = ServicesUtil.getServiceByRunner(confExec\r
143                                         .getExecutable().getClass());\r
144                         GAUtils.reportUsage(service);\r
145                         logger.info("Reporting GA usage for " + service);\r
146                 }\r
147         }\r
148 \r
149         public static <T> String analize(List<FastaSequence> sequences,\r
150                         ConfiguredExecutable<T> confExec, Logger log, String method,\r
151                         Limit<T> limit) throws JobSubmissionException {\r
152                 if (limit != null && limit.isExceeded(sequences)) {\r
153                         throw LimitExceededException.newLimitExceeded(limit, sequences);\r
154                 }\r
155                 log.debug("Method: " + method + " with task: " + confExec.getTaskId());\r
156 \r
157                 compbio.runner.Util.writeInput(sequences, confExec);\r
158                 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
159                 String jobId = engine.submitJob(confExec);\r
160                 reportUsage(confExec, log);\r
161                 return jobId;\r
162         }\r
163 \r
164         /*\r
165          * TODO Rewrite using purely CommandBuilder. This is breaking encapsulation\r
166          */\r
167         public static final <T> List<String> getCommands(List<Option<T>> options,\r
168                         String keyValueSeparator) {\r
169                 List<String> oList = new ArrayList<String>();\r
170                 for (Option<T> o : options) {\r
171                         oList.add(o.toCommand(keyValueSeparator));\r
172                 }\r
173                 return oList;\r
174         }\r
175 \r
176         public static void validateAAConInput(List<FastaSequence> sequences)\r
177                         throws JobSubmissionException {\r
178                 validateFastaInput(sequences);\r
179                 int len = 0;\r
180                 for (FastaSequence fs : sequences) {\r
181                         if (len == 0) {\r
182                                 len = fs.getLength();\r
183                                 continue;\r
184                         }\r
185                         if (fs.getLength() != len) {\r
186                                 throw new JobSubmissionException(\r
187                                                 "All sequences must be of the same length. Please align "\r
188                                                                 + "the sequences prior to submission! The first sequence length is : "\r
189                                                                 + len + " but the sequence '" + fs.getId()\r
190                                                                 + "' length is " + fs.getLength());\r
191                         }\r
192                 }\r
193         }\r
194 \r
195         public static <T> ScoreManager getAnnotation(String jobId, Logger log)\r
196                         throws ResultNotAvailableException {\r
197                 WSUtil.validateJobId(jobId);\r
198                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
199                 ConfiguredExecutable<T> aacon = (ConfiguredExecutable<T>) asyncEngine\r
200                                 .getResults(jobId);\r
201                 ScoreManager mas = aacon.getResults();\r
202                 log.trace(jobId + " getConservation : " + mas);\r
203                 return mas;\r
204         }\r
205 \r
206         /*\r
207          * UNUSED\r
208          * \r
209          * @SuppressWarnings("unchecked") static <T> LimitsManager<T>\r
210          * getLimits(Class<? extends Executable<T>> clazz, WebServiceContext\r
211          * wsContext) {\r
212          * \r
213          * String LIMIT_KEY = CACHE_KEY + clazz.getCanonicalName(); LimitsManager<T>\r
214          * limit = (LimitsManager<T>) getObjectFromApplContext( LIMIT_KEY,\r
215          * wsContext); if (limit == null) { synchronized (WSUtil.class) { limit =\r
216          * (LimitsManager<T>) getObjectFromApplContext(LIMIT_KEY, wsContext); if\r
217          * (limit == null) { limit = compbio.runner.Util\r
218          * .getLimits((Class<Executable<T>>) clazz);\r
219          * addObjectToApplContext(wsContext, LIMIT_KEY, limit); } } } return limit;\r
220          * }\r
221          * \r
222          * static void addObjectToApplContext(WebServiceContext wsContext, String\r
223          * objKey, Object obj) { assert !Util.isEmpty(objKey) :\r
224          * "Key for the object must not be empty! "; assert wsContext != null;\r
225          * \r
226          * ServletContext ctx = ((javax.servlet.ServletContext) wsContext\r
227          * .getMessageContext().get(MessageContext. SERVLET_CONTEXT)); assert ctx !=\r
228          * null; log.debug("Adding object with key '" + objKey + "' and value '" +\r
229          * obj + "' to the application context"); ctx.setAttribute(objKey, obj); }\r
230          * static Object getObjectFromApplContext(String objKey, WebServiceContext\r
231          * wsContext) { assert !Util.isEmpty(objKey) :\r
232          * "Key for the object must not be empty! "; assert wsContext != null;\r
233          * \r
234          * ServletContext ctx = ((javax.servlet.ServletContext) wsContext\r
235          * .getMessageContext().get(MessageContext. SERVLET_CONTEXT)); Object obj =\r
236          * ctx.getAttribute(objKey); log.trace("Retrieving object with key '" +\r
237          * objKey + "' and value '" + obj + "' from the application context");\r
238          * return obj; }\r
239          */\r
240 }\r