Assign JABAService.V2_SERVICE_NAMESPACE to all new services: JpredWS, RNAalifoldWS...
[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.FastaSequence;\r
29 import compbio.data.sequence.ScoreManager;\r
30 import compbio.engine.AsyncExecutor;\r
31 import compbio.engine.Configurator;\r
32 import compbio.engine.ProgressGetter;\r
33 import compbio.engine.client.ConfiguredExecutable;\r
34 import compbio.engine.client.EngineUtil;\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 (!EngineUtil.isValidJobId(jobId)) {\r
50                         throw new InvalidParameterException("JobId is not provided or cannot be recognised! Given value: " + jobId);\r
51                 }\r
52         }\r
53 \r
54         public static final void validateFastaInput(List<FastaSequence> sequences)\r
55                         throws JobSubmissionException {\r
56                 if (sequences == null || sequences.isEmpty()) {\r
57                         throw new JobSubmissionException("List of fasta sequences required but not provided! ");\r
58                 }\r
59                 Set<String> names = new HashSet<String>();\r
60                 for (FastaSequence fs : sequences) {\r
61                         boolean unique = names.add(fs.getId());\r
62                         if (!unique) {\r
63                                 throw new JobSubmissionException(\r
64                                                 "Input sequences must have unique names! \nSequence " + fs.getId() + " is a duplicate!");\r
65                         }\r
66                         if (fs.getLength() == 0) {\r
67                                 throw new JobSubmissionException("Sequence must not be empty! Sequence: " + fs.getId() + " was empty");\r
68                         }\r
69                 }\r
70         }\r
71 \r
72         public static JobStatus getJobStatus(String jobId) {\r
73                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
74                 return asyncEngine.getJobStatus(jobId);\r
75         }\r
76 \r
77         public static ChunkHolder pullFile(String file, long position) {\r
78                 return ProgressGetter.pull(file, position);\r
79         }\r
80 \r
81         public static byte getProgress(String jobId) {\r
82                 throw new UnsupportedOperationException();\r
83         }\r
84 \r
85         public static AsyncExecutor getEngine(ConfiguredExecutable<?> confClustal) {\r
86                 assert confClustal != null;\r
87                 return Configurator.getAsyncEngine(confClustal);\r
88         }\r
89 \r
90         public static boolean cancelJob(String jobId) {\r
91                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
92                 return asyncEngine.cancelJob(jobId);\r
93         }\r
94 \r
95         public static <T> String align(List<FastaSequence> sequences,\r
96                         ConfiguredExecutable<T> confExec, Logger logger,\r
97                         String callingMethod, Limit<T> limit)\r
98                         throws LimitExceededException, JobSubmissionException {\r
99 \r
100                 if (limit != null && limit.isExceeded(sequences)) {\r
101                         throw LimitExceededException.newLimitExceeded(limit, sequences);\r
102                 }\r
103                 compbio.runner.RunnerUtil.writeInput(sequences, confExec);\r
104                 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
105                 String jobId = engine.submitJob(confExec);\r
106                 reportUsage(confExec, logger);\r
107                 return jobId;\r
108         }\r
109         \r
110 \r
111         static <T> void reportUsage(ConfiguredExecutable<T> confExec, Logger logger) {\r
112                 if (GAUtils.IS_GA_ENABLED) {\r
113                         Services service = ServicesUtil.getServiceByRunner(confExec.getExecutable().getClass());\r
114                         GAUtils.reportUsage(service);\r
115                         logger.info("Reporting GA usage for " + service);\r
116                 }\r
117         }\r
118 \r
119         public static <T> String analize(List<FastaSequence> sequences,\r
120                         ConfiguredExecutable<T> confExec, Logger log, String method,\r
121                         Limit<T> limit) throws JobSubmissionException {\r
122                 if (limit != null && limit.isExceeded(sequences)) {\r
123                         throw LimitExceededException.newLimitExceeded(limit, sequences);\r
124                 }\r
125                 compbio.runner.RunnerUtil.writeInput(sequences, confExec);\r
126                 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
127                 String jobId = engine.submitJob(confExec);\r
128                 reportUsage(confExec, log);\r
129                 return jobId;\r
130         }\r
131 \r
132         // Same as analize(...) but RNAalifold takes clustal input not fasta\r
133         // An if condition in the above method might be a better solution but \r
134         // you need a way of finding out the type of confExec at runtime\r
135         \r
136         public static <T> String fold(List<FastaSequence> sequences,\r
137                         ConfiguredExecutable<T> confExec, Logger log, String method,\r
138                         Limit<T> limit) throws JobSubmissionException {\r
139                 if (limit != null && limit.isExceeded(sequences)) {\r
140                         throw LimitExceededException.newLimitExceeded(limit, sequences);\r
141                 }\r
142                 compbio.runner.RunnerUtil.writeClustalInput(sequences, confExec, '-');\r
143                 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
144                 String jobId = engine.submitJob(confExec);\r
145                 reportUsage(confExec, log);\r
146                 return jobId;\r
147         }\r
148 \r
149         /*\r
150          * TODO Rewrite using purely CommandBuilder. This is breaking encapsulation\r
151          */\r
152         public static final <T> List<String> getCommands(List<Option<T>> options,\r
153                         String keyValueSeparator) {\r
154                 List<String> oList = new ArrayList<String>();\r
155                 for (Option<T> o : options) {\r
156                         oList.add(o.toCommand(keyValueSeparator));\r
157                 }\r
158                 return oList;\r
159         }\r
160 \r
161         public static void validateAAConInput(List<FastaSequence> sequences)\r
162                         throws JobSubmissionException {\r
163                 validateFastaInput(sequences);\r
164                 int len = 0;\r
165                 for (FastaSequence fs : sequences) {\r
166                         if (len == 0) {\r
167                                 len = fs.getLength();\r
168                                 continue;\r
169                         }\r
170                         if (fs.getLength() != len) {\r
171                                 throw new JobSubmissionException(\r
172                                                 "All sequences must be of the same length. Please align the sequences " + \r
173                                                 " prior to submission! The first sequence length is : " + len + \r
174                                                 " but the sequence '" + fs.getId() + "' length is " + fs.getLength());\r
175                         }\r
176                 }\r
177         }\r
178 \r
179         public static void validateJpredInput(List<FastaSequence> sequences)\r
180                         throws JobSubmissionException {\r
181                 validateFastaInput(sequences);\r
182                 int len = 0;\r
183                 for (FastaSequence fs : sequences) {\r
184                         if (len == 0) {\r
185                                 len = fs.getLength();\r
186                                 continue;\r
187                         }\r
188                         if (fs.getLength() != len) {\r
189                                 System.out.println("FASTA rec: id = " + fs.getId() + ": seq = " + fs.getSequence());\r
190                                 throw new JobSubmissionException(\r
191                                                 "All sequences must be of the same length. Please align the sequences " + \r
192                                                 " prior to submission! The first sequence length is : " + len + \r
193                                                 " but the sequence '" + fs.getId() + "' length is " + fs.getLength());\r
194                         }\r
195                 }\r
196         }\r
197 \r
198         public static <T> ScoreManager getAnnotation(String jobId, Logger log)\r
199                         throws ResultNotAvailableException {\r
200                 WSUtil.validateJobId(jobId);\r
201                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
202                 ConfiguredExecutable<T> aacon = (ConfiguredExecutable<T>) asyncEngine.getResults(jobId);\r
203                 ScoreManager mas = aacon.getResults();\r
204                 log.trace(jobId + " getConservation : " + mas);\r
205                 return mas;\r
206         }\r
207 \r
208 }\r