1 /* Copyright (c) 2011 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.ws.server;
\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
26 import org.apache.log4j.Logger;
\r
28 import compbio.data.sequence.Alignment;
\r
29 import compbio.data.sequence.FastaSequence;
\r
30 import compbio.data.sequence.RNAStructScoreManager;
\r
31 import compbio.data.sequence.ScoreManager;
\r
32 import compbio.engine.AsyncExecutor;
\r
33 import compbio.engine.Configurator;
\r
34 import compbio.engine.ProgressGetter;
\r
35 import compbio.engine.client.ConfiguredExecutable;
\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
46 public final class WSUtil {
\r
48 public static final void validateJobId(String jobId)
\r
49 throws InvalidParameterException {
\r
50 if (!compbio.engine.client.Util.isValidJobId(jobId)) {
\r
51 throw new InvalidParameterException(
\r
52 "JobId is not provided or cannot be recognised! Given value: "
\r
57 public static final void validateFastaInput(List<FastaSequence> sequences)
\r
58 throws JobSubmissionException {
\r
59 if (sequences == null || sequences.isEmpty()) {
\r
60 throw new JobSubmissionException(
\r
61 "List of fasta sequences required but not provided! ");
\r
63 Set<String> names = new HashSet<String>();
\r
64 for (FastaSequence fs : sequences) {
\r
65 boolean unique = names.add(fs.getId());
\r
67 throw new JobSubmissionException(
\r
68 "Input sequences must have unique names! \n"
\r
69 + "Sequence " + fs.getId() + " is a duplicate!");
\r
71 if (fs.getLength() == 0) {
\r
72 throw new JobSubmissionException(
\r
73 "Sequence must not be empty! Sequence: " + fs.getId()
\r
79 public static JobStatus getJobStatus(String jobId) {
\r
80 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);
\r
81 return asyncEngine.getJobStatus(jobId);
\r
84 public static ChunkHolder pullFile(String file, long position) {
\r
85 return ProgressGetter.pull(file, position);
\r
88 public static byte getProgress(String jobId) {
\r
89 throw new UnsupportedOperationException();
\r
92 public static AsyncExecutor getEngine(ConfiguredExecutable<?> confClustal) {
\r
93 assert confClustal != null;
\r
94 return Configurator.getAsyncEngine(confClustal);
\r
97 public static boolean cancelJob(String jobId) {
\r
98 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);
\r
99 return asyncEngine.cancelJob(jobId);
\r
102 public static <T> String align(List<FastaSequence> sequences,
\r
103 ConfiguredExecutable<T> confExec, Logger logger,
\r
104 String callingMethod, Limit<T> limit)
\r
105 throws LimitExceededException, JobSubmissionException {
\r
107 if (limit != null && limit.isExceeded(sequences)) {
\r
108 throw LimitExceededException.newLimitExceeded(limit, sequences);
\r
110 compbio.runner.Util.writeInput(sequences, confExec);
\r
111 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);
\r
112 String jobId = engine.submitJob(confExec);
\r
113 reportUsage(confExec, logger);
\r
117 // Hardcoded gapchar '-' in this method
\r
119 // public static <T> String fold(Alignment alignment,
\r
120 // ConfiguredExecutable<T> confExec, Logger logger,
\r
121 // String callingMethod, Limit<T> limit)
\r
122 // throws LimitExceededException, JobSubmissionException {
\r
124 // List<FastaSequence> sequences = alignment.getSequences();
\r
125 // if (limit != null && limit.isExceeded(sequences)) {
\r
126 // throw LimitExceededException.newLimitExceeded(limit, sequences);
\r
128 // compbio.runner.Util.writeClustalInput(sequences, confExec, '-');
\r
129 // System.out.println("WSUtil-fold: Writing the clustal input file on the server: to file: "
\r
130 // + confExec.getInput());
\r
131 // System.out.println("WSUtil-fold: The executable is configured with parameters: "
\r
132 // + confExec.getParameters());
\r
133 //// System.out.println("WSUTil-fold: Dump the configured executable:\n"
\r
134 //// + confExec.toString());
\r
135 // AsyncExecutor engine = Configurator.getAsyncEngine(confExec);
\r
136 // String jobId = engine.submitJob(confExec);
\r
137 // reportUsage(confExec, logger);
\r
141 static <T> void reportUsage(ConfiguredExecutable<T> confExec, Logger logger) {
\r
142 if (GAUtils.IS_GA_ENABLED) {
\r
143 Services service = ServicesUtil.getServiceByRunner(confExec
\r
144 .getExecutable().getClass());
\r
145 GAUtils.reportUsage(service);
\r
146 logger.info("Reporting GA usage for " + service);
\r
150 public static <T> String analize(List<FastaSequence> sequences,
\r
151 ConfiguredExecutable<T> confExec, Logger log, String method,
\r
152 Limit<T> limit) throws JobSubmissionException {
\r
153 if (limit != null && limit.isExceeded(sequences)) {
\r
154 throw LimitExceededException.newLimitExceeded(limit, sequences);
\r
156 log.debug("Method: " + method + " with task: " + confExec.getTaskId());
\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
164 // Same as analize but Alifold takes clustal input not fasta
\r
166 public static <T> String fold(List<FastaSequence> sequences,
\r
167 ConfiguredExecutable<T> confExec, Logger log, String method,
\r
168 Limit<T> limit) throws JobSubmissionException {
\r
169 if (limit != null && limit.isExceeded(sequences)) {
\r
170 throw LimitExceededException.newLimitExceeded(limit, sequences);
\r
172 log.debug("Method: " + method + " with task: " + confExec.getTaskId());
\r
173 // This line is different from the above method
\r
174 compbio.runner.Util.writeClustalInput(sequences, confExec, '-');
\r
175 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);
\r
176 String jobId = engine.submitJob(confExec);
\r
177 reportUsage(confExec, log);
\r
182 * TODO Rewrite using purely CommandBuilder. This is breaking encapsulation
\r
184 public static final <T> List<String> getCommands(List<Option<T>> options,
\r
185 String keyValueSeparator) {
\r
186 List<String> oList = new ArrayList<String>();
\r
187 for (Option<T> o : options) {
\r
188 oList.add(o.toCommand(keyValueSeparator));
\r
193 public static void validateAAConInput(List<FastaSequence> sequences)
\r
194 throws JobSubmissionException {
\r
195 validateFastaInput(sequences);
\r
197 for (FastaSequence fs : sequences) {
\r
199 len = fs.getLength();
\r
202 if (fs.getLength() != len) {
\r
203 throw new JobSubmissionException(
\r
204 "All sequences must be of the same length. Please align "
\r
205 + "the sequences prior to submission! The first sequence length is : "
\r
206 + len + " but the sequence '" + fs.getId()
\r
207 + "' length is " + fs.getLength());
\r
213 // set return type back to ScoreManager
\r
214 public static <T> RNAStructScoreManager getAnnotation(String jobId, Logger log)
\r
215 throws ResultNotAvailableException {
\r
216 WSUtil.validateJobId(jobId);
\r
217 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);
\r
218 ConfiguredExecutable<T> aacon = (ConfiguredExecutable<T>) asyncEngine
\r
219 .getResults(jobId);
\r
223 // ScoreManager mas = aacon.getResults();
\r
225 RNAStructScoreManager mas = aacon.getResults();
\r
227 log.trace(jobId + " getConservation : " + mas);
\r
234 * @SuppressWarnings("unchecked") static <T> LimitsManager<T>
\r
235 * getLimits(Class<? extends Executable<T>> clazz, WebServiceContext
\r
238 * String LIMIT_KEY = CACHE_KEY + clazz.getCanonicalName(); LimitsManager<T>
\r
239 * limit = (LimitsManager<T>) getObjectFromApplContext( LIMIT_KEY,
\r
240 * wsContext); if (limit == null) { synchronized (WSUtil.class) { limit =
\r
241 * (LimitsManager<T>) getObjectFromApplContext(LIMIT_KEY, wsContext); if
\r
242 * (limit == null) { limit = compbio.runner.Util
\r
243 * .getLimits((Class<Executable<T>>) clazz);
\r
244 * addObjectToApplContext(wsContext, LIMIT_KEY, limit); } } } return limit;
\r
247 * static void addObjectToApplContext(WebServiceContext wsContext, String
\r
248 * objKey, Object obj) { assert !Util.isEmpty(objKey) :
\r
249 * "Key for the object must not be empty! "; assert wsContext != null;
\r
251 * ServletContext ctx = ((javax.servlet.ServletContext) wsContext
\r
252 * .getMessageContext().get(MessageContext. SERVLET_CONTEXT)); assert ctx !=
\r
253 * null; log.debug("Adding object with key '" + objKey + "' and value '" +
\r
254 * obj + "' to the application context"); ctx.setAttribute(objKey, obj); }
\r
255 * static Object getObjectFromApplContext(String objKey, WebServiceContext
\r
256 * wsContext) { assert !Util.isEmpty(objKey) :
\r
257 * "Key for the object must not be empty! "; assert wsContext != null;
\r
259 * ServletContext ctx = ((javax.servlet.ServletContext) wsContext
\r
260 * .getMessageContext().get(MessageContext. SERVLET_CONTEXT)); Object obj =
\r
261 * ctx.getAttribute(objKey); log.trace("Retrieving object with key '" +
\r
262 * objKey + "' and value '" + obj + "' from the application context");
\r