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.FoldWS;
\r
47 import compbio.data.msa.Metadata;
\r
48 import compbio.data.msa.MsaWS;
\r
49 import compbio.data.msa.RegistryWS;
\r
50 import compbio.data.msa.SequenceAnnotation;
\r
51 import compbio.data.sequence.Alignment;
\r
52 import compbio.data.sequence.FastaSequence;
\r
53 import compbio.data.sequence.RNAStruct;
\r
54 import compbio.data.sequence.ScoreManager;
\r
55 import compbio.data.sequence.ClustalAlignmentUtil;
\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
63 import compbio.util.FileUtil;
\r
66 * A command line client for JAva Bioinformatics Analysis Web Services
\r
71 public class Jws2Client {
\r
74 * Use java.util.Logger instead of log4j logger to reduce the size of the
\r
77 private static final Logger log = Logger.getLogger(Jws2Client.class
\r
78 .getCanonicalName());
\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
111 String hostname = CmdHelper.getHost(cmd);
\r
112 if (hostname == null) {
\r
113 System.err.println("Host name is not provided!");
\r
117 if (!validURL(hostname)) {
\r
118 System.err.println("Host name is not valid!");
\r
121 // Just list available services and quit
\r
122 boolean listServices = CmdHelper.listServices(cmd);
\r
123 if (listServices) {
\r
124 listServices(hostname);
\r
128 String serviceName = CmdHelper.getServiceName(cmd);
\r
129 if (serviceName == null) {
\r
130 System.err.println("Service name is no provided!");
\r
133 Services service = Services.getService(serviceName);
\r
134 if (service == null) {
\r
135 System.err.println("Service " + serviceName
\r
136 + " is no recognized! Valid values are: "
\r
137 + Arrays.toString(Services.values()));
\r
140 // Test service and quit
\r
141 boolean testService = CmdHelper.testService(cmd);
\r
143 testService(hostname, service, new PrintWriter(System.out, true));
\r
147 File inputFile = IOHelper.getFile(cmd, inputkey, true);
\r
148 File outFile = IOHelper.getFile(cmd, outputkey, false);
\r
149 File parametersFile = IOHelper.getFile(cmd, paramFile, true);
\r
150 String presetName = CmdHelper.getPresetName(cmd);
\r
152 Metadata<T> msaws = (Metadata<T>) connect(hostname, service);
\r
153 Metadata<T> foldws = (Metadata<T>) connect(hostname, service);
\r
154 Preset<T> preset = null;
\r
155 if (presetName != null) {
\r
156 preset = MetadataHelper.getPreset(msaws, presetName);
\r
158 List<Option<T>> customOptions = null;
\r
159 if (parametersFile != null) {
\r
160 List<String> prms = IOHelper.loadParameters(parametersFile);
\r
161 customOptions = MetadataHelper.processParameters(prms,
\r
162 msaws.getRunnerOptions());
\r
164 // System.out.println("The Options read from the command line: " + customOptions);
\r
166 Alignment alignment = null;
\r
167 String rnastruct = null;
\r
168 if (inputFile != null) {
\r
169 Writer writer = null;
\r
170 if (outFile != null) {
\r
171 writer = IOHelper.getWriter(outFile);
\r
173 // this stream is going to be closed later which is fine as
\r
174 // std.out will not be
\r
175 writer = new PrintWriter(System.out, true);
\r
177 if (service.getServiceType() == SequenceAnnotation.class) {
\r
178 ScoreManager result = analize(inputFile,
\r
179 ((SequenceAnnotation<T>) msaws), preset, customOptions);
\r
181 IOHelper.writeOut(writer, result);
\r
182 } else if (service.getServiceType() == MsaWS.class) {
\r
183 alignment = align(inputFile, (MsaWS<T>) msaws, preset,
\r
185 IOHelper.writeOut(writer, alignment);
\r
186 } else if (service.getServiceType() == FoldWS.class) {
\r
187 rnastruct = fold(inputFile, (FoldWS<T>) foldws, preset, customOptions);
\r
188 // No IOHelper method for rnastruct/String yet
\r
189 if (writer != null) {
\r
190 writer.write(rnastruct);
\r
196 boolean listParameters = CmdHelper.listParameters(cmd);
\r
197 if (listParameters) {
\r
198 System.out.println(MetadataHelper.getParametersList(msaws));
\r
200 boolean listPreset = CmdHelper.listPresets(cmd);
\r
202 System.out.println(MetadataHelper.getPresetList(msaws));
\r
204 boolean listLimits = CmdHelper.listLimits(cmd);
\r
206 System.out.println(MetadataHelper.getLimits(msaws));
\r
208 log.fine("Disconnecting...");
\r
209 ((Closeable) msaws).close();
\r
210 log.fine("Disconnected successfully!");
\r
214 * Asks registry to test the service on the host hostname
\r
219 * @throws ConnectException
\r
220 * @throws WebServiceException
\r
222 public static void testService(String hostname, Services service,
\r
223 PrintWriter writer) throws ConnectException, WebServiceException {
\r
224 RegistryWS registry = connectToRegistry(hostname);
\r
225 if (registry != null) {
\r
226 String message = registry.testService(service);
\r
227 writer.println("Service " + service + " testing results: ");
\r
228 writer.println(message);
\r
229 FileUtil.closeSilently(((Closeable) registry));
\r
234 public static Set<Services> getServices(String hostname)
\r
235 throws WebServiceException, ConnectException {
\r
236 RegistryWS registry = connectToRegistry(hostname);
\r
237 Set<Services> services = Collections.EMPTY_SET;
\r
238 if (registry != null) {
\r
239 services = registry.getSupportedServices();
\r
240 FileUtil.closeSilently(((Closeable) registry));
\r
245 private static void listServices(String hostname)
\r
246 throws WebServiceException, IOException {
\r
247 Set<Services> services = Jws2Client.getServices(hostname);
\r
248 if (!services.isEmpty()) {
\r
249 System.out.println("Supported services are: "
\r
250 + Services.toString(services));
\r
252 System.out.println("Failed to connect to the registry! ");
\r
257 * Calculate conservation for sequences loaded from the file
\r
260 * a web service proxy
\r
262 * the file to read the results from
\r
264 * Preset to use optional
\r
265 * @param customOptions
\r
266 * the list of options
\r
267 * @return Set<Score> the conservation scores
\r
268 * @throws UnknownFileFormatException
\r
270 static <T> ScoreManager analize(List<FastaSequence> fastalist,
\r
271 SequenceAnnotation<T> wsproxy, Preset<T> preset,
\r
272 List<Option<T>> customOptions) {
\r
274 ScoreManager scores = null;
\r
276 String jobId = null;
\r
277 if (customOptions != null && preset != null) {
\r
279 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
281 if (customOptions != null) {
\r
282 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
283 } else if (preset != null) {
\r
284 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
286 jobId = wsproxy.analize(fastalist);
\r
288 System.out.println("\n\ncalling analise.........");
\r
289 Thread.sleep(1000);
\r
290 scores = wsproxy.getAnnotation(jobId);
\r
292 } catch (JobSubmissionException e) {
\r
294 .println("Exception while submitting job to a web server. "
\r
295 + "Exception details are below:");
\r
296 e.printStackTrace();
\r
297 } catch (ResultNotAvailableException e) {
\r
298 System.err.println("Exception while waiting for results. "
\r
299 + "Exception details are below:");
\r
300 e.printStackTrace();
\r
301 } catch (InterruptedException e) {
\r
302 // ignore and propagate an interruption
\r
303 Thread.currentThread().interrupt();
\r
304 System.err.println("Exception while waiting for results. "
\r
305 + "Exception details are below:");
\r
306 e.printStackTrace();
\r
307 } catch (WrongParameterException e) {
\r
309 .println("Exception while parsing the web method input parameters. "
\r
310 + "Exception details are below:");
\r
311 e.printStackTrace();
\r
318 * Calculate conservation for sequences loaded from the file
\r
321 * a web service proxy
\r
323 * the file to read the results from
\r
325 * Preset to use optional
\r
326 * @param customOptions
\r
327 * the list of options
\r
328 * @return Set<Score> the conservation scores
\r
329 * @throws IOException
\r
330 * @throws UnknownFileFormatException
\r
332 static <T> ScoreManager analize(File file, SequenceAnnotation<T> wsproxy,
\r
333 Preset<T> preset, List<Option<T>> customOptions) {
\r
334 List<FastaSequence> fastalist = null;
\r
336 fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());
\r
337 assert !fastalist.isEmpty() : "Input is empty!";
\r
338 } catch (IOException e) {
\r
340 .println("Exception while reading the input file. "
\r
341 + "Check that the input file contains a list of fasta formatted sequences! "
\r
342 + "Exception details are below:");
\r
343 e.printStackTrace();
\r
344 } catch (UnknownFileFormatException e) {
\r
346 .println("Exception while attempting to read the input file "
\r
347 + "Exception details are below:");
\r
348 System.out.println(e.getMessage());
\r
349 e.printStackTrace();
\r
351 return analize(fastalist, wsproxy, preset, customOptions);
\r
354 * Connects to a web service by the host and the service name web service
\r
358 * the fully qualified name of JABAWS server including JABAWS
\r
360 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
362 * the name of the JABAWS service to connect to
\r
363 * @return JABAService<T>
\r
364 * @throws WebServiceException
\r
365 * @throws ConnectException
\r
366 * if fails to connect to the service on the host
\r
368 public static JABAService connect(String host, Services service)
\r
369 throws WebServiceException, ConnectException {
\r
371 log.log(Level.FINE, "Attempting to connect...");
\r
373 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
374 } catch (MalformedURLException e) {
\r
375 e.printStackTrace();
\r
376 // ignore as the host name is already verified
\r
378 Service serv = null;
\r
380 serv = service.getService(url, JABAService.SERVICE_NAMESPACE);
\r
381 } catch (WebServiceException wse) {
\r
382 System.out.println("Connecting to JABAWS version 2 service");
\r
383 if (isV2service(wse)) {
\r
385 .getService(url, JABAService.V2_SERVICE_NAMESPACE);
\r
388 if (serv == null) {
\r
389 throw new ConnectException("Could not connect to " + url
\r
390 + " the server is down?");
\r
392 JABAService serviceIF = service.getInterface(serv);
\r
393 log.log(Level.INFO, "Connected successfully!");
\r
398 static boolean isV2service(WebServiceException wse) {
\r
399 String message = wse.getMessage();
\r
400 int idx = message.indexOf("not a valid service");
\r
402 if (message.substring(idx).contains(
\r
403 JABAService.V2_SERVICE_NAMESPACE)) {
\r
411 * Get a connection of JABAWS registry
\r
414 * the fully qualified name of JABAWS server including JABAWS
\r
416 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
417 * @return compbio.data.msa.RegistryWS - instance of a RegistryWS web
\r
419 * @throws WebServiceException
\r
420 * @throws ConnectException
\r
422 public static compbio.data.msa.RegistryWS connectToRegistry(String host)
\r
423 throws WebServiceException, ConnectException {
\r
425 String service = "RegistryWS";
\r
426 log.log(Level.FINE, "Attempting to connect...");
\r
429 url = new URL(host + "/" + service + "?wsdl");
\r
430 } catch (MalformedURLException e) {
\r
431 e.printStackTrace();
\r
432 // ignore as the host name is already verified
\r
434 QName qname = new QName(JABAService.V2_SERVICE_NAMESPACE, service);
\r
435 Service serv = Service.create(url, qname);
\r
437 if (serv == null) {
\r
438 throw new ConnectException("Could not connect to " + url
\r
439 + " the server is down?");
\r
442 QName portName = new QName(serv.getServiceName().getNamespaceURI(),
\r
444 compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName,
\r
445 compbio.data.msa.RegistryWS.class);
\r
447 log.log(Level.INFO, "Connected to " + service + " successfully!");
\r
453 * Align sequences from the file using MsaWS
\r
456 * web service type e.g. Clustal
\r
458 * to write the resulting alignment to
\r
462 * Preset to use optional
\r
463 * @param customOptions
\r
464 * file which contains new line separated list of options
\r
465 * @return Alignment
\r
467 static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,
\r
468 List<Option<T>> customOptions) {
\r
469 FileInputStream instream = null;
\r
470 List<FastaSequence> fastalist = null;
\r
471 Alignment alignment = null;
\r
473 instream = new FileInputStream(file);
\r
474 fastalist = SequenceUtil.readFasta(instream);
\r
476 String jobId = null;
\r
477 if (customOptions != null && preset != null) {
\r
479 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
481 if (customOptions != null) {
\r
482 jobId = msaws.customAlign(fastalist, customOptions);
\r
483 } else if (preset != null) {
\r
484 jobId = msaws.presetAlign(fastalist, preset);
\r
486 jobId = msaws.align(fastalist);
\r
488 System.out.println("\n\ncalling align.........");
\r
489 Thread.sleep(1000);
\r
490 alignment = msaws.getResult(jobId);
\r
492 } catch (IOException e) {
\r
494 .println("Exception while reading the input file. "
\r
495 + "Check that the input file contains a list of fasta formatted sequences! "
\r
496 + "Exception details are below:");
\r
497 e.printStackTrace();
\r
498 } catch (JobSubmissionException e) {
\r
500 .println("Exception while submitting job to a web server. "
\r
501 + "Exception details are below:");
\r
502 e.printStackTrace();
\r
503 } catch (ResultNotAvailableException e) {
\r
504 System.err.println("Exception while waiting for results. "
\r
505 + "Exception details are below:");
\r
506 e.printStackTrace();
\r
507 } catch (InterruptedException ignored) {
\r
508 // ignore and propagate an interruption
\r
509 Thread.currentThread().interrupt();
\r
510 } catch (WrongParameterException e) {
\r
511 e.printStackTrace();
\r
513 if (instream != null) {
\r
516 } catch (IOException ignored) {
\r
525 * Return RNA secondary structure from a file using FoldWS
\r
528 * web service type e.g. Clustal
\r
530 * to read the results from
\r
534 * Preset to use optional
\r
535 * @param customOptions
\r
536 * file which contains new line separated list of options
\r
540 static <T> String fold(File file, FoldWS<T> foldws, Preset<T> preset,
\r
541 List<Option<T>> customOptions) {
\r
542 FileInputStream instream = null;
\r
543 Alignment alignment = null;
\r
544 String rnastruct = null;
\r
546 instream = new FileInputStream(file);
\r
547 alignment = ClustalAlignmentUtil.readClustalFile(instream);
\r
549 String jobId = null;
\r
550 if (customOptions != null && preset != null) {
\r
551 System.out.println("WARN: Parameters (-f) are defined together"
\r
552 + "with a preset (-r), ignoring preset! ");
\r
554 if (customOptions != null) {
\r
555 jobId = foldws.customFold(alignment, customOptions);
\r
556 } else if (preset != null) {
\r
557 jobId = foldws.presetFold(alignment, preset);
\r
559 jobId = foldws.fold(alignment);
\r
561 System.out.println("\n\ncalling fold.........");
\r
562 Thread.sleep(1000);
\r
563 rnastruct = foldws.getResult(jobId);
\r
565 } catch (IOException e) {
\r
566 System.err.println("Exception while reading the input file. Exception details: ");
\r
567 e.printStackTrace();
\r
568 } catch (UnknownFileFormatException e) {
\r
569 System.err.println("Exception while reading input file. Doesnt look like a Clustal format file");
\r
570 e.printStackTrace();
\r
571 } catch (JobSubmissionException e) {
\r
572 System.err.println("Exception while submitting job to the web server. ");
\r
573 e.printStackTrace();
\r
574 } catch (ResultNotAvailableException e) {
\r
575 System.err.println("Exception while waiting for results. Exception details: ");
\r
576 e.printStackTrace();
\r
577 } catch (InterruptedException ignored) {
\r
578 // ignore and propagate an interruption
\r
579 Thread.currentThread().interrupt();
\r
580 } catch (WrongParameterException e) {
\r
581 e.printStackTrace();
\r
583 if (instream != null) {
\r
586 } catch (IOException ignored) {
\r
595 * Prints Jws2Client usage information to standard out
\r
597 * @param exitStatus
\r
599 static void printUsage(int exitStatus) {
\r
600 System.out.println(Constraints.help_text);
\r
601 System.exit(exitStatus);
\r
605 * Starts command line client, if no parameter are supported print help. Two
\r
606 * parameters are required for successful call the JWS2 host name and a
\r
610 * Usage: <Class or Jar file name> -h=host_and_context
\r
611 * -s=serviceName ACTION [OPTIONS]
\r
613 * -h=<host_and_context> - a full URL to the JWS2 web server
\r
614 * including context path e.g. http://10.31.1.159:8080/ws
\r
616 * -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,
\r
617 * TcoffeeWS, ProbconsWS] ACTIONS:
\r
619 * -i=<inputFile> - full path to fasta formatted sequence file,
\r
620 * from which to align sequences
\r
622 * -parameters - lists parameters supported by web service
\r
624 * -presets - lists presets supported by web service
\r
626 * -limits - lists web services limits Please note that if input
\r
627 * file is specified other actions are ignored
\r
629 * OPTIONS: (only for use with -i action):
\r
631 * -r=<presetName> - name of the preset to use
\r
633 * -o=<outputFile> - full path to the file where to write an
\r
634 * alignment -f=<parameterInputFile> - the name of the file with
\r
635 * the list of parameters to use. Please note that -r and -f
\r
636 * options cannot be used together. Alignment is done with either
\r
637 * preset or a parameters from the file, but not both!
\r
640 public static void main(String[] args) {
\r
642 if (args == null) {
\r
645 if (args.length < 2) {
\r
646 System.err.println("Host and service names are required!");
\r
651 new Jws2Client(args);
\r
652 } catch (IOException e) {
\r
653 log.log(Level.SEVERE, "IOException in client! " + e.getMessage(),
\r
655 System.err.println("Cannot write output file! Stack trace: ");
\r
656 e.printStackTrace();
\r