1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.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
19 package compbio.ws.client;
\r
21 import static compbio.ws.client.Constraints.hostkey;
\r
22 import static compbio.ws.client.Constraints.inputkey;
\r
23 import static compbio.ws.client.Constraints.limitList;
\r
24 import static compbio.ws.client.Constraints.outputkey;
\r
25 import static compbio.ws.client.Constraints.paramFile;
\r
26 import static compbio.ws.client.Constraints.paramList;
\r
27 import static compbio.ws.client.Constraints.presetList;
\r
28 import static compbio.ws.client.Constraints.presetkey;
\r
29 import static compbio.ws.client.Constraints.pseparator;
\r
30 import static compbio.ws.client.Constraints.servicekey;
\r
32 import java.io.Closeable;
\r
33 import java.io.File;
\r
34 import java.io.FileInputStream;
\r
35 import java.io.IOException;
\r
36 import java.io.OutputStream;
\r
37 import java.net.MalformedURLException;
\r
38 import java.net.URL;
\r
39 import java.util.Arrays;
\r
40 import java.util.List;
\r
41 import java.util.Set;
\r
42 import java.util.logging.Level;
\r
43 import java.util.logging.Logger;
\r
45 import javax.xml.ws.Service;
\r
46 import javax.xml.ws.WebServiceException;
\r
48 import compbio.data.msa.Annotation;
\r
49 import compbio.data.msa.JABAService;
\r
50 import compbio.data.msa.Metadata;
\r
51 import compbio.data.msa.MsaWS;
\r
52 import compbio.data.sequence.Alignment;
\r
53 import compbio.data.sequence.FastaSequence;
\r
54 import compbio.data.sequence.Score;
\r
55 import compbio.data.sequence.SequenceUtil;
\r
56 import compbio.data.sequence.UnknownFileFormatException;
\r
57 import compbio.metadata.JobSubmissionException;
\r
58 import compbio.metadata.Option;
\r
59 import compbio.metadata.Preset;
\r
60 import compbio.metadata.ResultNotAvailableException;
\r
61 import compbio.metadata.WrongParameterException;
\r
64 * A command line client for JAva Bioinformatics Analysis Web Services
\r
69 public class Jws2Client {
\r
72 * Use java.util.Logger instead of log4j logger to reduce the size of the
\r
75 private static final Logger log = Logger.getLogger(Jws2Client.class
\r
76 .getCanonicalName());
\r
78 // JABAWS version 1.0 service name
\r
79 static final String QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/01/2010/";
\r
81 // JABAWS version 2.0 service name
\r
82 static final String V2_QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/12/2010/";
\r
85 * Attempt to construct the URL object from the string
\r
88 * @return true if it succeed false otherwise
\r
90 public static boolean validURL(String urlstr) {
\r
92 if (urlstr == null || urlstr.trim().length() == 0) {
\r
96 } catch (MalformedURLException e) {
\r
103 * Connects to the service and do the job as requested, if something goes
\r
104 * wrong reports or/and prints usage help.
\r
109 * command line options
\r
110 * @throws IOException
\r
112 <T> Jws2Client(String[] cmd) throws IOException {
\r
114 String hostname = CmdHelper.getHost(cmd);
\r
115 if (hostname == null) {
\r
116 System.out.println("Host name is not provided!");
\r
120 if (!validURL(hostname)) {
\r
121 System.out.println("Host name is not valid!");
\r
124 String serviceName = CmdHelper.getServiceName(cmd);
\r
125 if (serviceName == null) {
\r
126 System.out.println("Service name is no provided!");
\r
129 Services service = Services.getService(serviceName);
\r
130 if (service == null) {
\r
131 System.out.println("Service " + serviceName
\r
132 + " is no supported! Valid values are: "
\r
133 + Arrays.toString(Services.values()));
\r
136 File inputFile = IOHelper.getFile(cmd, inputkey, true);
\r
137 File outFile = IOHelper.getFile(cmd, outputkey, false);
\r
138 File parametersFile = IOHelper.getFile(cmd, paramFile, true);
\r
139 String presetName = CmdHelper.getPresetName(cmd);
\r
141 Metadata<T> msaws = (Metadata<T>) connect(hostname, service);
\r
142 Preset<T> preset = null;
\r
143 if (presetName != null) {
\r
144 preset = MetadataHelper.getPreset(msaws, presetName);
\r
146 List<Option<T>> customOptions = null;
\r
147 if (parametersFile != null) {
\r
148 List<String> prms = IOHelper.loadParameters(parametersFile);
\r
149 customOptions = MetadataHelper.processParameters(prms,
\r
150 msaws.getRunnerOptions());
\r
152 Alignment alignment = null;
\r
153 if (inputFile != null) {
\r
154 OutputStream outStream = null;
\r
155 if (outFile != null) {
\r
156 outStream = IOHelper.getOutStream(outFile);
\r
158 // this stream is going to be closed later which is fine as
\r
159 // std.out will not be
\r
160 outStream = System.out;
\r
162 if (service == Services.AAConWS) {
\r
163 Set<Score> result = analize(inputFile, ((Annotation<T>) msaws),
\r
164 preset, customOptions);
\r
165 IOHelper.writeOut(outStream, result);
\r
167 alignment = align(inputFile, (MsaWS<T>) msaws, preset,
\r
169 IOHelper.writeOut(outStream, alignment);
\r
172 // stream is closed in the method no need to close it here
\r
175 boolean listParameters = CmdHelper.listParameters(cmd);
\r
176 if (listParameters) {
\r
177 System.out.println(MetadataHelper.getParametersList(msaws));
\r
179 boolean listPreset = CmdHelper.listPresets(cmd);
\r
181 System.out.println(MetadataHelper.getPresetList(msaws));
\r
183 boolean listLimits = CmdHelper.listLimits(cmd);
\r
185 System.out.println(MetadataHelper.getLimits(msaws));
\r
187 log.fine("Disconnecting...");
\r
188 ((Closeable) msaws).close();
\r
189 log.fine("Disconnected successfully!");
\r
192 * Calculate conservation for sequences loaded from the file
\r
195 * a web service proxy
\r
197 * the file to read the results from
\r
199 * Preset to use optional
\r
200 * @param customOptions
\r
201 * the list of options
\r
202 * @return Set<Score> the conservation scores
\r
203 * @throws UnknownFileFormatException
\r
205 <T> Set<Score> analize(File file, Annotation<T> wsproxy, Preset<T> preset,
\r
206 List<Option<T>> customOptions) {
\r
208 List<FastaSequence> fastalist = null;
\r
209 Set<Score> scores = null;
\r
211 fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());
\r
213 String jobId = null;
\r
214 if (customOptions != null && preset != null) {
\r
216 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
218 if (customOptions != null) {
\r
219 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
220 } else if (preset != null) {
\r
221 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
223 jobId = wsproxy.analize(fastalist);
\r
225 Thread.sleep(1000);
\r
226 scores = wsproxy.getAnnotation(jobId);
\r
228 } catch (IOException e) {
\r
230 .println("Exception while reading the input file. "
\r
231 + "Check that the input file contains a list of fasta formatted sequences! "
\r
232 + "Exception details are below:");
\r
233 e.printStackTrace();
\r
234 } catch (JobSubmissionException e) {
\r
236 .println("Exception while submitting job to a web server. "
\r
237 + "Exception details are below:");
\r
238 e.printStackTrace();
\r
239 } catch (ResultNotAvailableException e) {
\r
240 System.err.println("Exception while waiting for results. "
\r
241 + "Exception details are below:");
\r
242 e.printStackTrace();
\r
243 } catch (InterruptedException ignored) {
\r
244 // ignore and propagate an interruption
\r
245 Thread.currentThread().interrupt();
\r
246 } catch (WrongParameterException e) {
\r
248 .println("Exception while parsing the web method input parameters. "
\r
249 + "Exception details are below:");
\r
250 e.printStackTrace();
\r
251 } catch (UnknownFileFormatException e) {
\r
253 .println("Exception while attempting to read the input file "
\r
254 + "Exception details are below:");
\r
255 System.out.println(e.getMessage());
\r
256 e.printStackTrace();
\r
262 * Connects to a web service by the host and the service name
\r
269 * @throws WebServiceException
\r
271 public static JABAService connect(String host, Services service)
\r
272 throws WebServiceException {
\r
274 log.log(Level.FINE, "Attempting to connect...");
\r
276 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
277 } catch (MalformedURLException e) {
\r
278 e.printStackTrace();
\r
279 // ignore as the host name is already verified
\r
281 Service serv = null;
\r
283 serv = service.getService(url, QUALIFIED_SERVICE_NAME);
\r
284 } catch (WebServiceException wse) {
\r
285 System.out.println("Conecting to JABAWS version 2 service");
\r
286 if (isV2service(wse)) {
\r
287 serv = service.getService(url, V2_QUALIFIED_SERVICE_NAME);
\r
290 JABAService serviceIF = service.getInterface(serv);
\r
291 log.log(Level.FINE, "Connected successfully!");
\r
296 static boolean isV2service(WebServiceException wse) {
\r
297 String message = wse.getMessage();
\r
298 int idx = message.indexOf("not a valid service");
\r
300 if (message.substring(idx).contains(V2_QUALIFIED_SERVICE_NAME)) {
\r
308 * Align sequences from the file using MsaWS
\r
311 * web service type e.g. Clustal
\r
313 * to write the resulting alignment to
\r
317 * Preset to use optional
\r
318 * @param customOptions
\r
319 * file which contains new line separated list of options
\r
320 * @return Alignment
\r
322 static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,
\r
323 List<Option<T>> customOptions) {
\r
324 FileInputStream instream = null;
\r
325 List<FastaSequence> fastalist = null;
\r
326 Alignment alignment = null;
\r
328 instream = new FileInputStream(file);
\r
329 fastalist = SequenceUtil.readFasta(instream);
\r
331 String jobId = null;
\r
332 if (customOptions != null && preset != null) {
\r
334 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
336 if (customOptions != null) {
\r
337 jobId = msaws.customAlign(fastalist, customOptions);
\r
338 } else if (preset != null) {
\r
339 jobId = msaws.presetAlign(fastalist, preset);
\r
341 jobId = msaws.align(fastalist);
\r
343 Thread.sleep(1000);
\r
344 alignment = msaws.getResult(jobId);
\r
346 } catch (IOException e) {
\r
348 .println("Exception while reading the input file. "
\r
349 + "Check that the input file contains a list of fasta formatted sequences! "
\r
350 + "Exception details are below:");
\r
351 e.printStackTrace();
\r
352 } catch (JobSubmissionException e) {
\r
354 .println("Exception while submitting job to a web server. "
\r
355 + "Exception details are below:");
\r
356 e.printStackTrace();
\r
357 } catch (ResultNotAvailableException e) {
\r
358 System.err.println("Exception while waiting for results. "
\r
359 + "Exception details are below:");
\r
360 e.printStackTrace();
\r
361 } catch (InterruptedException ignored) {
\r
362 // ignore and propagate an interruption
\r
363 Thread.currentThread().interrupt();
\r
364 } catch (WrongParameterException e) {
\r
365 e.printStackTrace();
\r
367 if (instream != null) {
\r
370 } catch (IOException ignored) {
\r
379 * Prints Jws2Client usage information to standard out
\r
381 * @param exitStatus
\r
383 static void printUsage(int exitStatus) {
\r
384 System.out.println();
\r
385 System.out.println("Usage: <Class or Jar file name> " + hostkey
\r
386 + pseparator + "host_and_context " + servicekey + pseparator
\r
387 + "serviceName ACTION [OPTIONS] ");
\r
388 System.out.println();
\r
392 + "<host_and_context> - a full URL to the JWS2 web server including context path e.g. http://10.31.1.159:8080/ws");
\r
393 System.out.println(servicekey + pseparator + "<ServiceName> - one of "
\r
394 + Arrays.toString(Services.values()));
\r
395 System.out.println();
\r
396 System.out.println("ACTIONS: ");
\r
400 + "<inputFile> - full path to fasta formatted sequence file, from which to align sequences");
\r
401 System.out.println(paramList
\r
402 + " - lists parameters supported by web service");
\r
403 System.out.println(presetList
\r
404 + " - lists presets supported by web service");
\r
405 System.out.println(limitList + " - lists web services limits");
\r
407 .println("Please note that if input file is specified other actions are ignored");
\r
409 System.out.println();
\r
410 System.out.println("OPTIONS (only for use with -i action):");
\r
412 System.out.println(presetkey + pseparator
\r
413 + "<presetName> - name of the preset to use");
\r
417 + "<outputFile> - full path to the file where to write an alignment");
\r
419 .println("-f=<parameterInputFile> - the name of the file with the list of parameters to use.");
\r
421 .println("Please note that -r and -f options cannot be used together. "
\r
422 + "Alignment is done with either preset or a parameters from the file, but not both!");
\r
424 System.exit(exitStatus);
\r
428 * Starts command line client, if no parameter are supported print help. Two
\r
429 * parameters are required for successfull call the JWS2 host name and a
\r
433 * Usage: <Class or Jar file name> -h=host_and_context
\r
434 * -s=serviceName ACTION [OPTIONS]
\r
436 * -h=<host_and_context> - a full URL to the JWS2 web server
\r
437 * including context path e.g. http://10.31.1.159:8080/ws
\r
439 * -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,
\r
440 * TcoffeeWS, ProbconsWS] ACTIONS:
\r
442 * -i=<inputFile> - full path to fasta formatted sequence file,
\r
443 * from which to align sequences
\r
445 * -parameters - lists parameters supported by web service
\r
447 * -presets - lists presets supported by web service
\r
449 * -limits - lists web services limits Please note that if input
\r
450 * file is specified other actions are ignored
\r
452 * OPTIONS: (only for use with -i action):
\r
454 * -r=<presetName> - name of the preset to use
\r
456 * -o=<outputFile> - full path to the file where to write an
\r
457 * alignment -f=<parameterInputFile> - the name of the file with
\r
458 * the list of parameters to use. Please note that -r and -f
\r
459 * options cannot be used together. Alignment is done with either
\r
460 * preset or a parameters from the file, but not both!
\r
463 public static void main(String[] args) {
\r
465 if (args == null) {
\r
468 if (args.length < 2) {
\r
469 System.out.println("Host and service names are required!");
\r
474 new Jws2Client(args);
\r
475 } catch (IOException e) {
\r
476 log.log(Level.SEVERE, "IOException in client! " + e.getMessage(),
\r
478 System.err.println("Cannot write output file! Stack trace: ");
\r
479 e.printStackTrace();
\r