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.PrintWriter;
\r
37 import java.io.Writer;
\r
38 import java.net.MalformedURLException;
\r
39 import java.net.URL;
\r
40 import java.util.Arrays;
\r
41 import java.util.List;
\r
42 import java.util.logging.Level;
\r
43 import java.util.logging.Logger;
\r
45 import javax.xml.namespace.QName;
\r
46 import javax.xml.ws.Service;
\r
47 import javax.xml.ws.WebServiceException;
\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.msa.SequenceAnnotation;
\r
53 import compbio.data.sequence.Alignment;
\r
54 import compbio.data.sequence.FastaSequence;
\r
55 import compbio.data.sequence.ScoreManager;
\r
56 import compbio.data.sequence.SequenceUtil;
\r
57 import compbio.data.sequence.UnknownFileFormatException;
\r
58 import compbio.metadata.JobSubmissionException;
\r
59 import compbio.metadata.Option;
\r
60 import compbio.metadata.Preset;
\r
61 import compbio.metadata.ResultNotAvailableException;
\r
62 import compbio.metadata.WrongParameterException;
\r
65 * A command line client for JAva Bioinformatics Analysis Web Services
\r
70 public class Jws2Client {
\r
73 * TODO Use java.util.Logger instead of log4j logger to reduce the size of
\r
74 * the client package
\r
76 private static final Logger log = Logger.getLogger(Jws2Client.class
\r
77 .getCanonicalName());
\r
79 // JABAWS version 1.0 service name
\r
80 static final String QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/01/2010/";
\r
82 // JABAWS version 2.0 service name
\r
83 static final String V2_QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/12/2010/";
\r
86 * Attempt to construct the URL object from the string
\r
89 * @return true if it succeed false otherwise
\r
91 public static boolean validURL(String urlstr) {
\r
93 if (urlstr == null || urlstr.trim().length() == 0) {
\r
97 } catch (MalformedURLException e) {
\r
104 * Connects to the service and do the job as requested, if something goes
\r
105 * wrong reports or/and prints usage help.
\r
110 * command line options
\r
111 * @throws IOException
\r
113 <T> Jws2Client(String[] cmd) throws IOException {
\r
115 String hostname = CmdHelper.getHost(cmd);
\r
116 if (hostname == null) {
\r
117 System.out.println("Host name is not provided!");
\r
121 if (!validURL(hostname)) {
\r
122 System.out.println("Host name is not valid!");
\r
125 String serviceName = CmdHelper.getServiceName(cmd);
\r
126 if (serviceName == null) {
\r
127 System.out.println("Service name is no provided!");
\r
130 Services service = Services.getService(serviceName);
\r
131 if (service == null) {
\r
132 System.out.println("Service " + serviceName
\r
133 + " is no supported! Valid values are: "
\r
134 + Arrays.toString(Services.values()));
\r
137 File inputFile = IOHelper.getFile(cmd, inputkey, true);
\r
138 File outFile = IOHelper.getFile(cmd, outputkey, false);
\r
139 File parametersFile = IOHelper.getFile(cmd, paramFile, true);
\r
140 String presetName = CmdHelper.getPresetName(cmd);
\r
142 Metadata<T> msaws = (Metadata<T>) connect(hostname, service);
\r
143 Preset<T> preset = null;
\r
144 if (presetName != null) {
\r
145 preset = MetadataHelper.getPreset(msaws, presetName);
\r
147 List<Option<T>> customOptions = null;
\r
148 if (parametersFile != null) {
\r
149 List<String> prms = IOHelper.loadParameters(parametersFile);
\r
150 customOptions = MetadataHelper.processParameters(prms,
\r
151 msaws.getRunnerOptions());
\r
153 Alignment alignment = null;
\r
154 if (inputFile != null) {
\r
155 Writer writer = null;
\r
156 if (outFile != null) {
\r
157 writer = IOHelper.getWriter(outFile);
\r
159 // this stream is going to be closed later which is fine as
\r
160 // std.out will not be
\r
161 writer = new PrintWriter(System.out, true);
\r
163 if (service.getServiceType() == SequenceAnnotation.class) {
\r
164 ScoreManager result = analize(inputFile,
\r
165 ((SequenceAnnotation<T>) msaws), preset, customOptions);
\r
167 IOHelper.writeOut(writer, result);
\r
169 alignment = align(inputFile, (MsaWS<T>) msaws, preset,
\r
171 IOHelper.writeOut(writer, alignment);
\r
176 boolean listParameters = CmdHelper.listParameters(cmd);
\r
177 if (listParameters) {
\r
178 System.out.println(MetadataHelper.getParametersList(msaws));
\r
180 boolean listPreset = CmdHelper.listPresets(cmd);
\r
182 System.out.println(MetadataHelper.getPresetList(msaws));
\r
184 boolean listLimits = CmdHelper.listLimits(cmd);
\r
186 System.out.println(MetadataHelper.getLimits(msaws));
\r
188 log.fine("Disconnecting...");
\r
189 ((Closeable) msaws).close();
\r
190 log.fine("Disconnected successfully!");
\r
193 * Calculate conservation for sequences loaded from the file
\r
196 * a web service proxy
\r
198 * the file to read the results from
\r
200 * Preset to use optional
\r
201 * @param customOptions
\r
202 * the list of options
\r
203 * @return Set<Score> the conservation scores
\r
204 * @throws UnknownFileFormatException
\r
206 static <T> ScoreManager analize(List<FastaSequence> fastalist,
\r
207 SequenceAnnotation<T> wsproxy, Preset<T> preset,
\r
208 List<Option<T>> customOptions) {
\r
210 ScoreManager scores = null;
\r
212 String jobId = null;
\r
213 if (customOptions != null && preset != null) {
\r
215 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
217 if (customOptions != null) {
\r
218 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
219 } else if (preset != null) {
\r
220 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
222 jobId = wsproxy.analize(fastalist);
\r
224 System.out.println("\n\ncalling analise.........");
\r
225 Thread.sleep(1000);
\r
226 scores = wsproxy.getAnnotation(jobId);
\r
228 } catch (JobSubmissionException e) {
\r
230 .println("Exception while submitting job to a web server. "
\r
231 + "Exception details are below:");
\r
232 e.printStackTrace();
\r
233 } catch (ResultNotAvailableException e) {
\r
234 System.err.println("Exception while waiting for results. "
\r
235 + "Exception details are below:");
\r
236 e.printStackTrace();
\r
237 } catch (InterruptedException e) {
\r
238 // ignore and propagate an interruption
\r
239 Thread.currentThread().interrupt();
\r
240 System.err.println("Exception while waiting for results. "
\r
241 + "Exception details are below:");
\r
242 e.printStackTrace();
\r
243 } catch (WrongParameterException e) {
\r
245 .println("Exception while parsing the web method input parameters. "
\r
246 + "Exception details are below:");
\r
247 e.printStackTrace();
\r
254 * Calculate conservation for sequences loaded from the file
\r
257 * a web service proxy
\r
259 * the file to read the results from
\r
261 * Preset to use optional
\r
262 * @param customOptions
\r
263 * the list of options
\r
264 * @return Set<Score> the conservation scores
\r
265 * @throws IOException
\r
266 * @throws UnknownFileFormatException
\r
268 static <T> ScoreManager analize(File file, SequenceAnnotation<T> wsproxy,
\r
269 Preset<T> preset, List<Option<T>> customOptions) {
\r
270 List<FastaSequence> fastalist = null;
\r
272 fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());
\r
273 assert !fastalist.isEmpty() : "Input is empty!";
\r
274 } catch (IOException e) {
\r
276 .println("Exception while reading the input file. "
\r
277 + "Check that the input file contains a list of fasta formatted sequences! "
\r
278 + "Exception details are below:");
\r
279 e.printStackTrace();
\r
280 } catch (UnknownFileFormatException e) {
\r
282 .println("Exception while attempting to read the input file "
\r
283 + "Exception details are below:");
\r
284 System.out.println(e.getMessage());
\r
285 e.printStackTrace();
\r
287 return analize(fastalist, wsproxy, preset, customOptions);
\r
290 * Connects to a web service by the host and the service name
\r
297 * @throws WebServiceException
\r
299 public static JABAService connect(String host, Services service)
\r
300 throws WebServiceException {
\r
302 log.log(Level.FINE, "Attempting to connect...");
\r
304 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
305 } catch (MalformedURLException e) {
\r
306 e.printStackTrace();
\r
307 // ignore as the host name is already verified
\r
309 Service serv = null;
\r
311 serv = service.getService(url, QUALIFIED_SERVICE_NAME);
\r
312 } catch (WebServiceException wse) {
\r
313 System.out.println("Connecting to JABAWS version 2 service");
\r
314 if (isV2service(wse)) {
\r
315 serv = service.getService(url, V2_QUALIFIED_SERVICE_NAME);
\r
318 if (serv == null) {
\r
319 System.err.println("Could not connect to " + url
\r
320 + " the server is down?");
\r
323 JABAService serviceIF = service.getInterface(serv);
\r
324 log.log(Level.INFO, "Connected successfully!");
\r
329 static boolean isV2service(WebServiceException wse) {
\r
330 String message = wse.getMessage();
\r
331 int idx = message.indexOf("not a valid service");
\r
333 if (message.substring(idx).contains(V2_QUALIFIED_SERVICE_NAME)) {
\r
340 public static compbio.data.msa.RegistryWS connectToRegistry(String host)
\r
341 throws WebServiceException {
\r
343 String service = "RegistryWS";
\r
344 log.log(Level.FINE, "Attempting to connect...");
\r
347 url = new URL(host + "/" + service + "?wsdl");
\r
348 } catch (MalformedURLException e) {
\r
349 e.printStackTrace();
\r
350 // ignore as the host name is already verified
\r
352 QName qname = new QName(V2_QUALIFIED_SERVICE_NAME, service);
\r
353 Service serv = Service.create(url, qname);
\r
355 if (serv == null) {
\r
356 System.err.println("Could not connect to " + url
\r
357 + " the server is down?");
\r
360 QName portName = new QName(serv.getServiceName().getNamespaceURI(),
\r
362 compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName,
\r
363 compbio.data.msa.RegistryWS.class);
\r
365 log.log(Level.INFO, "Connected to " + service + " successfully!");
\r
371 * Align sequences from the file using MsaWS
\r
374 * web service type e.g. Clustal
\r
376 * to write the resulting alignment to
\r
380 * Preset to use optional
\r
381 * @param customOptions
\r
382 * file which contains new line separated list of options
\r
383 * @return Alignment
\r
385 static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,
\r
386 List<Option<T>> customOptions) {
\r
387 FileInputStream instream = null;
\r
388 List<FastaSequence> fastalist = null;
\r
389 Alignment alignment = null;
\r
391 instream = new FileInputStream(file);
\r
392 fastalist = SequenceUtil.readFasta(instream);
\r
394 String jobId = null;
\r
395 if (customOptions != null && preset != null) {
\r
397 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
399 if (customOptions != null) {
\r
400 jobId = msaws.customAlign(fastalist, customOptions);
\r
401 } else if (preset != null) {
\r
402 jobId = msaws.presetAlign(fastalist, preset);
\r
404 jobId = msaws.align(fastalist);
\r
406 System.out.println("\n\ncalling align.........");
\r
407 Thread.sleep(1000);
\r
408 alignment = msaws.getResult(jobId);
\r
410 } catch (IOException e) {
\r
412 .println("Exception while reading the input file. "
\r
413 + "Check that the input file contains a list of fasta formatted sequences! "
\r
414 + "Exception details are below:");
\r
415 e.printStackTrace();
\r
416 } catch (JobSubmissionException e) {
\r
418 .println("Exception while submitting job to a web server. "
\r
419 + "Exception details are below:");
\r
420 e.printStackTrace();
\r
421 } catch (ResultNotAvailableException e) {
\r
422 System.err.println("Exception while waiting for results. "
\r
423 + "Exception details are below:");
\r
424 e.printStackTrace();
\r
425 } catch (InterruptedException ignored) {
\r
426 // ignore and propagate an interruption
\r
427 Thread.currentThread().interrupt();
\r
428 } catch (WrongParameterException e) {
\r
429 e.printStackTrace();
\r
431 if (instream != null) {
\r
434 } catch (IOException ignored) {
\r
443 * Prints Jws2Client usage information to standard out
\r
445 * @param exitStatus
\r
447 static void printUsage(int exitStatus) {
\r
448 System.out.println();
\r
449 System.out.println("Usage: <Class or Jar file name> " + hostkey
\r
450 + pseparator + "host_and_context " + servicekey + pseparator
\r
451 + "serviceName ACTION [OPTIONS] ");
\r
452 System.out.println();
\r
456 + "<host_and_context> - a full URL to the JWS2 web server including context path e.g. http://10.31.1.159:8080/ws");
\r
457 System.out.println(servicekey + pseparator + "<ServiceName> - one of "
\r
458 + Arrays.toString(Services.values()));
\r
459 System.out.println();
\r
460 System.out.println("ACTIONS: ");
\r
464 + "<inputFile> - full path to fasta formatted sequence file, from which to align sequences");
\r
465 System.out.println(paramList
\r
466 + " - lists parameters supported by web service");
\r
467 System.out.println(presetList
\r
468 + " - lists presets supported by web service");
\r
469 System.out.println(limitList + " - lists web services limits");
\r
471 .println("Please note that if input file is specified other actions are ignored");
\r
473 System.out.println();
\r
474 System.out.println("OPTIONS (only for use with -i action):");
\r
476 System.out.println(presetkey + pseparator
\r
477 + "<presetName> - name of the preset to use");
\r
481 + "<outputFile> - full path to the file where to write an alignment");
\r
483 .println("-f=<parameterInputFile> - the name of the file with the list of parameters to use.");
\r
485 .println("Please note that -r and -f options cannot be used together. "
\r
486 + "Alignment is done with either preset or a parameters from the file, but not both!");
\r
488 System.exit(exitStatus);
\r
492 * Starts command line client, if no parameter are supported print help. Two
\r
493 * parameters are required for successful call the JWS2 host name and a
\r
497 * Usage: <Class or Jar file name> -h=host_and_context
\r
498 * -s=serviceName ACTION [OPTIONS]
\r
500 * -h=<host_and_context> - a full URL to the JWS2 web server
\r
501 * including context path e.g. http://10.31.1.159:8080/ws
\r
503 * -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,
\r
504 * TcoffeeWS, ProbconsWS] ACTIONS:
\r
506 * -i=<inputFile> - full path to fasta formatted sequence file,
\r
507 * from which to align sequences
\r
509 * -parameters - lists parameters supported by web service
\r
511 * -presets - lists presets supported by web service
\r
513 * -limits - lists web services limits Please note that if input
\r
514 * file is specified other actions are ignored
\r
516 * OPTIONS: (only for use with -i action):
\r
518 * -r=<presetName> - name of the preset to use
\r
520 * -o=<outputFile> - full path to the file where to write an
\r
521 * alignment -f=<parameterInputFile> - the name of the file with
\r
522 * the list of parameters to use. Please note that -r and -f
\r
523 * options cannot be used together. Alignment is done with either
\r
524 * preset or a parameters from the file, but not both!
\r
527 public static void main(String[] args) {
\r
529 if (args == null) {
\r
532 if (args.length < 2) {
\r
533 System.out.println("Host and service names are required!");
\r
538 new Jws2Client(args);
\r
539 } catch (IOException e) {
\r
540 log.log(Level.SEVERE, "IOException in client! " + e.getMessage(),
\r
542 System.err.println("Cannot write output file! Stack trace: ");
\r
543 e.printStackTrace();
\r