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.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
45 public final class WSUtil {
\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
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
59 Set<String> names = new HashSet<String>();
\r
60 for (FastaSequence fs : sequences) {
\r
61 boolean unique = names.add(fs.getId());
\r
63 throw new JobSubmissionException(
\r
64 "Input sequences must have unique names! \nSequence " + fs.getId() + " is a duplicate!");
\r
66 if (fs.getLength() == 0) {
\r
67 throw new JobSubmissionException("Sequence must not be empty! Sequence: " + fs.getId() + " was empty");
\r
72 public static JobStatus getJobStatus(String jobId) {
\r
73 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);
\r
74 return asyncEngine.getJobStatus(jobId);
\r
77 public static ChunkHolder pullFile(String file, long position) {
\r
78 return ProgressGetter.pull(file, position);
\r
81 public static byte getProgress(String jobId) {
\r
82 throw new UnsupportedOperationException();
\r
85 public static AsyncExecutor getEngine(ConfiguredExecutable<?> confClustal) {
\r
86 assert confClustal != null;
\r
87 return Configurator.getAsyncEngine(confClustal);
\r
90 public static boolean cancelJob(String jobId) {
\r
91 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);
\r
92 return asyncEngine.cancelJob(jobId);
\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
100 if (limit != null && limit.isExceeded(sequences)) {
\r
101 throw LimitExceededException.newLimitExceeded(limit, sequences);
\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
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
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
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
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
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
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
150 * TODO Rewrite using purely CommandBuilder. This is breaking encapsulation
\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
161 public static void validateAAConInput(List<FastaSequence> sequences)
\r
162 throws JobSubmissionException {
\r
163 validateFastaInput(sequences);
\r
165 for (FastaSequence fs : sequences) {
\r
167 len = fs.getLength();
\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
179 public static void validateJpredInput(List<FastaSequence> sequences)
\r
180 throws JobSubmissionException {
\r
181 validateFastaInput(sequences);
\r
183 for (FastaSequence fs : sequences) {
\r
185 len = fs.getLength();
\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
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