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