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
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.ConnectException;
\r
32 import java.net.MalformedURLException;
\r
33 import java.net.URL;
\r
34 import java.util.Arrays;
\r
35 import java.util.Collections;
\r
36 import java.util.List;
\r
37 import java.util.Set;
\r
38 import java.util.logging.Level;
\r
39 import java.util.logging.Logger;
\r
41 import javax.xml.namespace.QName;
\r
42 import javax.xml.ws.Service;
\r
43 import javax.xml.ws.WebServiceException;
\r
45 import compbio.data.msa.JABAService;
\r
46 import compbio.data.msa.Metadata;
\r
47 import compbio.data.msa.MsaWS;
\r
48 import compbio.data.msa.RegistryWS;
\r
49 import compbio.data.msa.SequenceAnnotation;
\r
50 import compbio.data.sequence.Alignment;
\r
51 import compbio.data.sequence.FastaSequence;
\r
52 import compbio.data.sequence.ScoreManager;
\r
53 import compbio.data.sequence.SequenceUtil;
\r
54 import compbio.data.sequence.UnknownFileFormatException;
\r
55 import compbio.metadata.JobSubmissionException;
\r
56 import compbio.metadata.Option;
\r
57 import compbio.metadata.Preset;
\r
58 import compbio.metadata.ResultNotAvailableException;
\r
59 import compbio.metadata.WrongParameterException;
\r
60 import compbio.util.FileUtil;
\r
63 * A command line client for JAva Bioinformatics Analysis Web Services
\r
68 public class Jws2Client {
\r
71 * Use java.util.Logger instead of log4j logger to reduce the size of the
\r
74 private static final Logger log = Logger.getLogger(Jws2Client.class
\r
75 .getCanonicalName());
\r
78 * Attempt to construct the URL object from the string
\r
81 * @return true if it succeed false otherwise
\r
83 public static boolean validURL(String urlstr) {
\r
85 if (urlstr == null || urlstr.trim().length() == 0) {
\r
89 } catch (MalformedURLException e) {
\r
96 * Connects to the service and do the job as requested, if something goes
\r
97 * wrong reports or/and prints usage help.
\r
102 * command line options
\r
103 * @throws IOException
\r
105 <T> Jws2Client(String[] cmd) throws IOException {
\r
107 String hostname = CmdHelper.getHost(cmd);
\r
108 if (hostname == null) {
\r
109 System.err.println("Host name is not provided!");
\r
113 if (!validURL(hostname)) {
\r
114 System.err.println("Host name is not valid!");
\r
117 // Just list available services and quit
\r
118 boolean listServices = CmdHelper.listServices(cmd);
\r
119 if (listServices) {
\r
120 listServices(hostname);
\r
124 String serviceName = CmdHelper.getServiceName(cmd);
\r
125 if (serviceName == null) {
\r
126 System.err.println("Service name is no provided!");
\r
129 Services service = Services.getService(serviceName);
\r
130 if (service == null) {
\r
131 System.err.println("Service " + serviceName
\r
132 + " is no recognized! Valid values are: "
\r
133 + Arrays.toString(Services.values()));
\r
136 // Test service and quit
\r
137 boolean testService = CmdHelper.testService(cmd);
\r
139 testService(hostname, service, new PrintWriter(System.out, true));
\r
143 File inputFile = IOHelper.getFile(cmd, inputkey, true);
\r
144 File outFile = IOHelper.getFile(cmd, outputkey, false);
\r
145 File parametersFile = IOHelper.getFile(cmd, paramFile, true);
\r
146 String presetName = CmdHelper.getPresetName(cmd);
\r
148 Metadata<T> msaws = (Metadata<T>) connect(hostname, service);
\r
149 Preset<T> preset = null;
\r
150 if (presetName != null) {
\r
151 preset = MetadataHelper.getPreset(msaws, presetName);
\r
153 List<Option<T>> customOptions = null;
\r
154 if (parametersFile != null) {
\r
155 List<String> prms = IOHelper.loadParameters(parametersFile);
\r
156 customOptions = MetadataHelper.processParameters(prms,
\r
157 msaws.getRunnerOptions());
\r
159 Alignment alignment = null;
\r
160 if (inputFile != null) {
\r
161 Writer writer = null;
\r
162 if (outFile != null) {
\r
163 writer = IOHelper.getWriter(outFile);
\r
165 // this stream is going to be closed later which is fine as
\r
166 // std.out will not be
\r
167 writer = new PrintWriter(System.out, true);
\r
169 if (service.getServiceType() == SequenceAnnotation.class) {
\r
170 ScoreManager result = analize(inputFile,
\r
171 ((SequenceAnnotation<T>) msaws), preset, customOptions);
\r
173 IOHelper.writeOut(writer, result);
\r
175 alignment = align(inputFile, (MsaWS<T>) msaws, preset,
\r
177 IOHelper.writeOut(writer, alignment);
\r
182 boolean listParameters = CmdHelper.listParameters(cmd);
\r
183 if (listParameters) {
\r
184 System.out.println(MetadataHelper.getParametersList(msaws));
\r
186 boolean listPreset = CmdHelper.listPresets(cmd);
\r
188 System.out.println(MetadataHelper.getPresetList(msaws));
\r
190 boolean listLimits = CmdHelper.listLimits(cmd);
\r
192 System.out.println(MetadataHelper.getLimits(msaws));
\r
194 log.fine("Disconnecting...");
\r
195 ((Closeable) msaws).close();
\r
196 log.fine("Disconnected successfully!");
\r
200 * Asks registry to test the service on the host hostname
\r
205 * @throws ConnectException
\r
206 * @throws WebServiceException
\r
208 public static void testService(String hostname, Services service,
\r
209 PrintWriter writer) throws ConnectException, WebServiceException {
\r
210 RegistryWS registry = connectToRegistry(hostname);
\r
211 if (registry != null) {
\r
212 String message = registry.testService(service);
\r
213 writer.println("Service " + service + " testing results: ");
\r
214 writer.println(message);
\r
215 FileUtil.closeSilently(((Closeable) registry));
\r
220 public static Set<Services> getServices(String hostname)
\r
221 throws WebServiceException, ConnectException {
\r
222 RegistryWS registry = connectToRegistry(hostname);
\r
223 Set<Services> services = Collections.EMPTY_SET;
\r
224 if (registry != null) {
\r
225 services = registry.getSupportedServices();
\r
226 FileUtil.closeSilently(((Closeable) registry));
\r
231 private static void listServices(String hostname)
\r
232 throws WebServiceException, IOException {
\r
233 Set<Services> services = Jws2Client.getServices(hostname);
\r
234 if (!services.isEmpty()) {
\r
235 System.out.println("Supported services are: "
\r
236 + Services.toString(services));
\r
238 System.out.println("Failed to connect to the registry! ");
\r
243 * Calculate conservation for sequences loaded from the file
\r
246 * a web service proxy
\r
248 * the file to read the results from
\r
250 * Preset to use optional
\r
251 * @param customOptions
\r
252 * the list of options
\r
253 * @return Set<Score> the conservation scores
\r
254 * @throws UnknownFileFormatException
\r
256 static <T> ScoreManager analize(List<FastaSequence> fastalist,
\r
257 SequenceAnnotation<T> wsproxy, Preset<T> preset,
\r
258 List<Option<T>> customOptions) {
\r
260 ScoreManager scores = null;
\r
262 String jobId = null;
\r
263 if (customOptions != null && preset != null) {
\r
265 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
267 if (customOptions != null) {
\r
268 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
269 } else if (preset != null) {
\r
270 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
272 jobId = wsproxy.analize(fastalist);
\r
274 System.out.println("\n\ncalling analise.........");
\r
275 Thread.sleep(1000);
\r
276 scores = wsproxy.getAnnotation(jobId);
\r
278 } catch (JobSubmissionException e) {
\r
280 .println("Exception while submitting job to a web server. "
\r
281 + "Exception details are below:");
\r
282 e.printStackTrace();
\r
283 } catch (ResultNotAvailableException e) {
\r
284 System.err.println("Exception while waiting for results. "
\r
285 + "Exception details are below:");
\r
286 e.printStackTrace();
\r
287 } catch (InterruptedException e) {
\r
288 // ignore and propagate an interruption
\r
289 Thread.currentThread().interrupt();
\r
290 System.err.println("Exception while waiting for results. "
\r
291 + "Exception details are below:");
\r
292 e.printStackTrace();
\r
293 } catch (WrongParameterException e) {
\r
295 .println("Exception while parsing the web method input parameters. "
\r
296 + "Exception details are below:");
\r
297 e.printStackTrace();
\r
304 * Calculate conservation for sequences loaded from the file
\r
307 * a web service proxy
\r
309 * the file to read the results from
\r
311 * Preset to use optional
\r
312 * @param customOptions
\r
313 * the list of options
\r
314 * @return Set<Score> the conservation scores
\r
315 * @throws IOException
\r
316 * @throws UnknownFileFormatException
\r
318 static <T> ScoreManager analize(File file, SequenceAnnotation<T> wsproxy,
\r
319 Preset<T> preset, List<Option<T>> customOptions) {
\r
320 List<FastaSequence> fastalist = null;
\r
322 fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());
\r
323 assert !fastalist.isEmpty() : "Input is empty!";
\r
324 } catch (IOException e) {
\r
326 .println("Exception while reading the input file. "
\r
327 + "Check that the input file contains a list of fasta formatted sequences! "
\r
328 + "Exception details are below:");
\r
329 e.printStackTrace();
\r
330 } catch (UnknownFileFormatException e) {
\r
332 .println("Exception while attempting to read the input file "
\r
333 + "Exception details are below:");
\r
334 System.out.println(e.getMessage());
\r
335 e.printStackTrace();
\r
337 return analize(fastalist, wsproxy, preset, customOptions);
\r
340 * Connects to a web service by the host and the service name web service
\r
344 * the fully qualified name of JABAWS server including JABAWS
\r
346 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
348 * the name of the JABAWS service to connect to
\r
349 * @return JABAService<T>
\r
350 * @throws WebServiceException
\r
351 * @throws ConnectException
\r
352 * if fails to connect to the service on the host
\r
354 public static JABAService connect(String host, Services service)
\r
355 throws WebServiceException, ConnectException {
\r
357 log.log(Level.FINE, "Attempting to connect...");
\r
359 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
360 } catch (MalformedURLException e) {
\r
361 e.printStackTrace();
\r
362 // ignore as the host name is already verified
\r
364 Service serv = null;
\r
366 serv = service.getService(url, JABAService.SERVICE_NAMESPACE);
\r
367 } catch (WebServiceException wse) {
\r
368 System.out.println("Connecting to JABAWS version 2 service");
\r
369 if (isV2service(wse)) {
\r
371 .getService(url, JABAService.V2_SERVICE_NAMESPACE);
\r
374 if (serv == null) {
\r
375 throw new ConnectException("Could not connect to " + url
\r
376 + " the server is down?");
\r
378 JABAService serviceIF = service.getInterface(serv);
\r
379 log.log(Level.INFO, "Connected successfully!");
\r
384 static boolean isV2service(WebServiceException wse) {
\r
385 String message = wse.getMessage();
\r
386 int idx = message.indexOf("not a valid service");
\r
388 if (message.substring(idx).contains(
\r
389 JABAService.V2_SERVICE_NAMESPACE)) {
\r
397 * Get a connection of JABAWS registry
\r
400 * the fully qualified name of JABAWS server including JABAWS
\r
402 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
403 * @return compbio.data.msa.RegistryWS - instance of a RegistryWS web
\r
405 * @throws WebServiceException
\r
406 * @throws ConnectException
\r
408 public static compbio.data.msa.RegistryWS connectToRegistry(String host)
\r
409 throws WebServiceException, ConnectException {
\r
411 String service = "RegistryWS";
\r
412 log.log(Level.FINE, "Attempting to connect...");
\r
415 url = new URL(host + "/" + service + "?wsdl");
\r
416 } catch (MalformedURLException e) {
\r
417 e.printStackTrace();
\r
418 // ignore as the host name is already verified
\r
420 QName qname = new QName(JABAService.V2_SERVICE_NAMESPACE, service);
\r
421 Service serv = Service.create(url, qname);
\r
423 if (serv == null) {
\r
424 throw new ConnectException("Could not connect to " + url
\r
425 + " the server is down?");
\r
428 QName portName = new QName(serv.getServiceName().getNamespaceURI(),
\r
430 compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName,
\r
431 compbio.data.msa.RegistryWS.class);
\r
433 log.log(Level.INFO, "Connected to " + service + " successfully!");
\r
439 * Align sequences from the file using MsaWS
\r
442 * web service type e.g. Clustal
\r
444 * to write the resulting alignment to
\r
448 * Preset to use optional
\r
449 * @param customOptions
\r
450 * file which contains new line separated list of options
\r
451 * @return Alignment
\r
453 static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,
\r
454 List<Option<T>> customOptions) {
\r
455 FileInputStream instream = null;
\r
456 List<FastaSequence> fastalist = null;
\r
457 Alignment alignment = null;
\r
459 instream = new FileInputStream(file);
\r
460 fastalist = SequenceUtil.readFasta(instream);
\r
462 String jobId = null;
\r
463 if (customOptions != null && preset != null) {
\r
465 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
467 if (customOptions != null) {
\r
468 jobId = msaws.customAlign(fastalist, customOptions);
\r
469 } else if (preset != null) {
\r
470 jobId = msaws.presetAlign(fastalist, preset);
\r
472 jobId = msaws.align(fastalist);
\r
474 System.out.println("\n\ncalling align.........");
\r
475 Thread.sleep(1000);
\r
476 alignment = msaws.getResult(jobId);
\r
478 } catch (IOException e) {
\r
480 .println("Exception while reading the input file. "
\r
481 + "Check that the input file contains a list of fasta formatted sequences! "
\r
482 + "Exception details are below:");
\r
483 e.printStackTrace();
\r
484 } catch (JobSubmissionException e) {
\r
486 .println("Exception while submitting job to a web server. "
\r
487 + "Exception details are below:");
\r
488 e.printStackTrace();
\r
489 } catch (ResultNotAvailableException e) {
\r
490 System.err.println("Exception while waiting for results. "
\r
491 + "Exception details are below:");
\r
492 e.printStackTrace();
\r
493 } catch (InterruptedException ignored) {
\r
494 // ignore and propagate an interruption
\r
495 Thread.currentThread().interrupt();
\r
496 } catch (WrongParameterException e) {
\r
497 e.printStackTrace();
\r
499 if (instream != null) {
\r
502 } catch (IOException ignored) {
\r
511 * Prints Jws2Client usage information to standard out
\r
513 * @param exitStatus
\r
515 static void printUsage(int exitStatus) {
\r
516 System.out.println(Constraints.help_text);
\r
517 System.exit(exitStatus);
\r
521 * Starts command line client, if no parameter are supported print help. Two
\r
522 * parameters are required for successful call the JWS2 host name and a
\r
526 * Usage: <Class or Jar file name> -h=host_and_context
\r
527 * -s=serviceName ACTION [OPTIONS]
\r
529 * -h=<host_and_context> - a full URL to the JWS2 web server
\r
530 * including context path e.g. http://10.31.1.159:8080/ws
\r
532 * -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,
\r
533 * TcoffeeWS, ProbconsWS] ACTIONS:
\r
535 * -i=<inputFile> - full path to fasta formatted sequence file,
\r
536 * from which to align sequences
\r
538 * -parameters - lists parameters supported by web service
\r
540 * -presets - lists presets supported by web service
\r
542 * -limits - lists web services limits Please note that if input
\r
543 * file is specified other actions are ignored
\r
545 * OPTIONS: (only for use with -i action):
\r
547 * -r=<presetName> - name of the preset to use
\r
549 * -o=<outputFile> - full path to the file where to write an
\r
550 * alignment -f=<parameterInputFile> - the name of the file with
\r
551 * the list of parameters to use. Please note that -r and -f
\r
552 * options cannot be used together. Alignment is done with either
\r
553 * preset or a parameters from the file, but not both!
\r
556 public static void main(String[] args) {
\r
558 if (args == null) {
\r
561 if (args.length < 2) {
\r
562 System.err.println("Host and service names are required!");
\r
567 new Jws2Client(args);
\r
568 } catch (IOException e) {
\r
569 log.log(Level.SEVERE, "IOException in client! " + e.getMessage(),
\r
571 System.err.println("Cannot write output file! Stack trace: ");
\r
572 e.printStackTrace();
\r