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.inputkey;
\r
22 import static compbio.ws.client.Constraints.outputkey;
\r
23 import static compbio.ws.client.Constraints.paramFile;
\r
25 import java.io.Closeable;
\r
26 import java.io.File;
\r
27 import java.io.FileInputStream;
\r
28 import java.io.IOException;
\r
29 import java.io.PrintWriter;
\r
30 import java.io.Writer;
\r
31 import java.net.MalformedURLException;
\r
32 import java.net.URL;
\r
33 import java.util.Arrays;
\r
34 import java.util.List;
\r
35 import java.util.Set;
\r
36 import java.util.logging.Level;
\r
37 import java.util.logging.Logger;
\r
39 import javax.xml.namespace.QName;
\r
40 import javax.xml.ws.Service;
\r
41 import javax.xml.ws.WebServiceException;
\r
43 import compbio.data.msa.JABAService;
\r
44 import compbio.data.msa.Metadata;
\r
45 import compbio.data.msa.MsaWS;
\r
46 import compbio.data.msa.RegistryWS;
\r
47 import compbio.data.msa.SequenceAnnotation;
\r
48 import compbio.data.sequence.Alignment;
\r
49 import compbio.data.sequence.FastaSequence;
\r
50 import compbio.data.sequence.ScoreManager;
\r
51 import compbio.data.sequence.SequenceUtil;
\r
52 import compbio.data.sequence.UnknownFileFormatException;
\r
53 import compbio.metadata.JobSubmissionException;
\r
54 import compbio.metadata.Option;
\r
55 import compbio.metadata.Preset;
\r
56 import compbio.metadata.ResultNotAvailableException;
\r
57 import compbio.metadata.WrongParameterException;
\r
60 * A command line client for JAva Bioinformatics Analysis Web Services
\r
65 public class Jws2Client {
\r
68 * Use java.util.Logger instead of log4j logger to reduce the size of the
\r
71 private static final Logger log = Logger.getLogger(Jws2Client.class
\r
72 .getCanonicalName());
\r
74 // JABAWS version 1.0 service name
\r
75 static final String QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/01/2010/";
\r
77 // JABAWS version 2.0 service name
\r
78 static final String V2_QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/12/2010/";
\r
81 * Attempt to construct the URL object from the string
\r
84 * @return true if it succeed false otherwise
\r
86 public static boolean validURL(String urlstr) {
\r
88 if (urlstr == null || urlstr.trim().length() == 0) {
\r
92 } catch (MalformedURLException e) {
\r
99 * Connects to the service and do the job as requested, if something goes
\r
100 * wrong reports or/and prints usage help.
\r
105 * command line options
\r
106 * @throws IOException
\r
108 <T> Jws2Client(String[] cmd) throws IOException {
\r
110 String hostname = CmdHelper.getHost(cmd);
\r
111 if (hostname == null) {
\r
112 System.err.println("Host name is not provided!");
\r
116 if (!validURL(hostname)) {
\r
117 System.err.println("Host name is not valid!");
\r
120 // Just list available services and quit
\r
121 boolean listServices = CmdHelper.listServices(cmd);
\r
122 if (listServices) {
\r
123 listServices(hostname);
\r
127 String serviceName = CmdHelper.getServiceName(cmd);
\r
128 if (serviceName == null) {
\r
129 System.err.println("Service name is no provided!");
\r
132 Services service = Services.getService(serviceName);
\r
133 if (service == null) {
\r
134 System.err.println("Service " + serviceName
\r
135 + " is no recognized! Valid values are: "
\r
136 + Arrays.toString(Services.values()));
\r
139 // Test service and quit
\r
140 boolean testService = CmdHelper.testService(cmd);
\r
142 testService(hostname, service);
\r
146 File inputFile = IOHelper.getFile(cmd, inputkey, true);
\r
147 File outFile = IOHelper.getFile(cmd, outputkey, false);
\r
148 File parametersFile = IOHelper.getFile(cmd, paramFile, true);
\r
149 String presetName = CmdHelper.getPresetName(cmd);
\r
151 Metadata<T> msaws = (Metadata<T>) connect(hostname, service);
\r
152 Preset<T> preset = null;
\r
153 if (presetName != null) {
\r
154 preset = MetadataHelper.getPreset(msaws, presetName);
\r
156 List<Option<T>> customOptions = null;
\r
157 if (parametersFile != null) {
\r
158 List<String> prms = IOHelper.loadParameters(parametersFile);
\r
159 customOptions = MetadataHelper.processParameters(prms,
\r
160 msaws.getRunnerOptions());
\r
162 Alignment alignment = null;
\r
163 if (inputFile != null) {
\r
164 Writer writer = null;
\r
165 if (outFile != null) {
\r
166 writer = IOHelper.getWriter(outFile);
\r
168 // this stream is going to be closed later which is fine as
\r
169 // std.out will not be
\r
170 writer = new PrintWriter(System.out, true);
\r
172 if (service.getServiceType() == SequenceAnnotation.class) {
\r
173 ScoreManager result = analize(inputFile,
\r
174 ((SequenceAnnotation<T>) msaws), preset, customOptions);
\r
176 IOHelper.writeOut(writer, result);
\r
178 alignment = align(inputFile, (MsaWS<T>) msaws, preset,
\r
180 IOHelper.writeOut(writer, alignment);
\r
185 boolean listParameters = CmdHelper.listParameters(cmd);
\r
186 if (listParameters) {
\r
187 System.out.println(MetadataHelper.getParametersList(msaws));
\r
189 boolean listPreset = CmdHelper.listPresets(cmd);
\r
191 System.out.println(MetadataHelper.getPresetList(msaws));
\r
193 boolean listLimits = CmdHelper.listLimits(cmd);
\r
195 System.out.println(MetadataHelper.getLimits(msaws));
\r
197 log.fine("Disconnecting...");
\r
198 ((Closeable) msaws).close();
\r
199 log.fine("Disconnected successfully!");
\r
202 private void testService(String hostname, Services service)
\r
203 throws IOException {
\r
204 RegistryWS registry = connectToRegistry(hostname);
\r
205 if (registry != null) {
\r
206 String message = registry.testService(service);
\r
207 System.out.println("Service " + service + " testing results: ");
\r
208 System.out.println(message);
\r
209 ((Closeable) registry).close();
\r
213 private void listServices(String hostname) throws IOException {
\r
214 RegistryWS registry = connectToRegistry(hostname);
\r
215 if (registry != null) {
\r
216 Set<Services> services = registry.getSupportedServices();
\r
217 System.out.println("Supported services are: "
\r
218 + Services.toString(services));
\r
219 ((Closeable) registry).close();
\r
221 System.out.println("Failed to connect to the registry! ");
\r
226 * Calculate conservation for sequences loaded from the file
\r
229 * a web service proxy
\r
231 * the file to read the results from
\r
233 * Preset to use optional
\r
234 * @param customOptions
\r
235 * the list of options
\r
236 * @return Set<Score> the conservation scores
\r
237 * @throws UnknownFileFormatException
\r
239 static <T> ScoreManager analize(List<FastaSequence> fastalist,
\r
240 SequenceAnnotation<T> wsproxy, Preset<T> preset,
\r
241 List<Option<T>> customOptions) {
\r
243 ScoreManager scores = null;
\r
245 String jobId = null;
\r
246 if (customOptions != null && preset != null) {
\r
248 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
250 if (customOptions != null) {
\r
251 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
252 } else if (preset != null) {
\r
253 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
255 jobId = wsproxy.analize(fastalist);
\r
257 System.out.println("\n\ncalling analise.........");
\r
258 Thread.sleep(1000);
\r
259 scores = wsproxy.getAnnotation(jobId);
\r
261 } catch (JobSubmissionException e) {
\r
263 .println("Exception while submitting job to a web server. "
\r
264 + "Exception details are below:");
\r
265 e.printStackTrace();
\r
266 } catch (ResultNotAvailableException e) {
\r
267 System.err.println("Exception while waiting for results. "
\r
268 + "Exception details are below:");
\r
269 e.printStackTrace();
\r
270 } catch (InterruptedException e) {
\r
271 // ignore and propagate an interruption
\r
272 Thread.currentThread().interrupt();
\r
273 System.err.println("Exception while waiting for results. "
\r
274 + "Exception details are below:");
\r
275 e.printStackTrace();
\r
276 } catch (WrongParameterException e) {
\r
278 .println("Exception while parsing the web method input parameters. "
\r
279 + "Exception details are below:");
\r
280 e.printStackTrace();
\r
287 * Calculate conservation for sequences loaded from the file
\r
290 * a web service proxy
\r
292 * the file to read the results from
\r
294 * Preset to use optional
\r
295 * @param customOptions
\r
296 * the list of options
\r
297 * @return Set<Score> the conservation scores
\r
298 * @throws IOException
\r
299 * @throws UnknownFileFormatException
\r
301 static <T> ScoreManager analize(File file, SequenceAnnotation<T> wsproxy,
\r
302 Preset<T> preset, List<Option<T>> customOptions) {
\r
303 List<FastaSequence> fastalist = null;
\r
305 fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());
\r
306 assert !fastalist.isEmpty() : "Input is empty!";
\r
307 } catch (IOException e) {
\r
309 .println("Exception while reading the input file. "
\r
310 + "Check that the input file contains a list of fasta formatted sequences! "
\r
311 + "Exception details are below:");
\r
312 e.printStackTrace();
\r
313 } catch (UnknownFileFormatException e) {
\r
315 .println("Exception while attempting to read the input file "
\r
316 + "Exception details are below:");
\r
317 System.out.println(e.getMessage());
\r
318 e.printStackTrace();
\r
320 return analize(fastalist, wsproxy, preset, customOptions);
\r
323 * Connects to a web service by the host and the service name
\r
330 * @throws WebServiceException
\r
332 public static JABAService connect(String host, Services service)
\r
333 throws WebServiceException {
\r
335 log.log(Level.FINE, "Attempting to connect...");
\r
337 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
338 } catch (MalformedURLException e) {
\r
339 e.printStackTrace();
\r
340 // ignore as the host name is already verified
\r
342 Service serv = null;
\r
344 serv = service.getService(url, QUALIFIED_SERVICE_NAME);
\r
345 } catch (WebServiceException wse) {
\r
346 System.out.println("Connecting to JABAWS version 2 service");
\r
347 if (isV2service(wse)) {
\r
348 serv = service.getService(url, V2_QUALIFIED_SERVICE_NAME);
\r
351 if (serv == null) {
\r
352 System.err.println("Could not connect to " + url
\r
353 + " the server is down?");
\r
356 JABAService serviceIF = service.getInterface(serv);
\r
357 log.log(Level.INFO, "Connected successfully!");
\r
362 static boolean isV2service(WebServiceException wse) {
\r
363 String message = wse.getMessage();
\r
364 int idx = message.indexOf("not a valid service");
\r
366 if (message.substring(idx).contains(V2_QUALIFIED_SERVICE_NAME)) {
\r
373 public static compbio.data.msa.RegistryWS connectToRegistry(String host)
\r
374 throws WebServiceException {
\r
376 String service = "RegistryWS";
\r
377 log.log(Level.FINE, "Attempting to connect...");
\r
380 url = new URL(host + "/" + service + "?wsdl");
\r
381 } catch (MalformedURLException e) {
\r
382 e.printStackTrace();
\r
383 // ignore as the host name is already verified
\r
385 QName qname = new QName(V2_QUALIFIED_SERVICE_NAME, service);
\r
386 Service serv = Service.create(url, qname);
\r
388 if (serv == null) {
\r
389 System.err.println("Could not connect to " + url
\r
390 + " the server is down?");
\r
393 QName portName = new QName(serv.getServiceName().getNamespaceURI(),
\r
395 compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName,
\r
396 compbio.data.msa.RegistryWS.class);
\r
398 log.log(Level.INFO, "Connected to " + service + " successfully!");
\r
404 * Align sequences from the file using MsaWS
\r
407 * web service type e.g. Clustal
\r
409 * to write the resulting alignment to
\r
413 * Preset to use optional
\r
414 * @param customOptions
\r
415 * file which contains new line separated list of options
\r
416 * @return Alignment
\r
418 static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,
\r
419 List<Option<T>> customOptions) {
\r
420 FileInputStream instream = null;
\r
421 List<FastaSequence> fastalist = null;
\r
422 Alignment alignment = null;
\r
424 instream = new FileInputStream(file);
\r
425 fastalist = SequenceUtil.readFasta(instream);
\r
427 String jobId = null;
\r
428 if (customOptions != null && preset != null) {
\r
430 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
432 if (customOptions != null) {
\r
433 jobId = msaws.customAlign(fastalist, customOptions);
\r
434 } else if (preset != null) {
\r
435 jobId = msaws.presetAlign(fastalist, preset);
\r
437 jobId = msaws.align(fastalist);
\r
439 System.out.println("\n\ncalling align.........");
\r
440 Thread.sleep(1000);
\r
441 alignment = msaws.getResult(jobId);
\r
443 } catch (IOException e) {
\r
445 .println("Exception while reading the input file. "
\r
446 + "Check that the input file contains a list of fasta formatted sequences! "
\r
447 + "Exception details are below:");
\r
448 e.printStackTrace();
\r
449 } catch (JobSubmissionException e) {
\r
451 .println("Exception while submitting job to a web server. "
\r
452 + "Exception details are below:");
\r
453 e.printStackTrace();
\r
454 } catch (ResultNotAvailableException e) {
\r
455 System.err.println("Exception while waiting for results. "
\r
456 + "Exception details are below:");
\r
457 e.printStackTrace();
\r
458 } catch (InterruptedException ignored) {
\r
459 // ignore and propagate an interruption
\r
460 Thread.currentThread().interrupt();
\r
461 } catch (WrongParameterException e) {
\r
462 e.printStackTrace();
\r
464 if (instream != null) {
\r
467 } catch (IOException ignored) {
\r
476 * Prints Jws2Client usage information to standard out
\r
478 * @param exitStatus
\r
480 static void printUsage(int exitStatus) {
\r
481 System.out.println(Constraints.help_text);
\r
482 System.exit(exitStatus);
\r
486 * Starts command line client, if no parameter are supported print help. Two
\r
487 * parameters are required for successful call the JWS2 host name and a
\r
491 * Usage: <Class or Jar file name> -h=host_and_context
\r
492 * -s=serviceName ACTION [OPTIONS]
\r
494 * -h=<host_and_context> - a full URL to the JWS2 web server
\r
495 * including context path e.g. http://10.31.1.159:8080/ws
\r
497 * -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,
\r
498 * TcoffeeWS, ProbconsWS] ACTIONS:
\r
500 * -i=<inputFile> - full path to fasta formatted sequence file,
\r
501 * from which to align sequences
\r
503 * -parameters - lists parameters supported by web service
\r
505 * -presets - lists presets supported by web service
\r
507 * -limits - lists web services limits Please note that if input
\r
508 * file is specified other actions are ignored
\r
510 * OPTIONS: (only for use with -i action):
\r
512 * -r=<presetName> - name of the preset to use
\r
514 * -o=<outputFile> - full path to the file where to write an
\r
515 * alignment -f=<parameterInputFile> - the name of the file with
\r
516 * the list of parameters to use. Please note that -r and -f
\r
517 * options cannot be used together. Alignment is done with either
\r
518 * preset or a parameters from the file, but not both!
\r
521 public static void main(String[] args) {
\r
523 if (args == null) {
\r
526 if (args.length < 2) {
\r
527 System.err.println("Host and service names are required!");
\r
532 new Jws2Client(args);
\r
533 } catch (IOException e) {
\r
534 log.log(Level.SEVERE, "IOException in client! " + e.getMessage(),
\r
536 System.err.println("Cannot write output file! Stack trace: ");
\r
537 e.printStackTrace();
\r