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.ScoreManager;
\r
54 import compbio.data.sequence.ClustalAlignmentUtil;
\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
62 import compbio.util.FileUtil;
\r
65 * A command line client for JAva Bioinformatics Analysis Web Services
\r
70 public class Jws2Client {
\r
73 * Use java.util.Logger instead of log4j logger to reduce the size of the
\r
76 private static final Logger log = Logger.getLogger(Jws2Client.class
\r
77 .getCanonicalName());
\r
80 * Attempt to construct the URL object from the string
\r
83 * @return true if it succeed false otherwise
\r
85 public static boolean validURL(String urlstr) {
\r
87 if (urlstr == null || urlstr.trim().length() == 0) {
\r
91 } catch (MalformedURLException e) {
\r
98 * Connects to the service and do the job as requested, if something goes
\r
99 * wrong reports or/and prints usage help.
\r
104 * command line options
\r
105 * @throws IOException
\r
107 <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, new PrintWriter(System.out, true));
\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 // System.out.println("The Options read from the command line: " + customOptions);
\r
164 Alignment alignment = null;
\r
165 if (inputFile != null) {
\r
166 Writer writer = null;
\r
167 if (outFile != null) {
\r
168 writer = IOHelper.getWriter(outFile);
\r
170 // this stream is going to be closed later which is fine as
\r
171 // std.out will not be
\r
172 writer = new PrintWriter(System.out, true);
\r
174 if (service.getServiceType() == SequenceAnnotation.class) {
\r
175 ScoreManager result = analize(inputFile,
\r
176 ((SequenceAnnotation<T>) msaws), preset, customOptions);
\r
178 // A System.out.println just for testing!
\r
179 System.out.println(result.toString());
\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
190 boolean listParameters = CmdHelper.listParameters(cmd);
\r
191 if (listParameters) {
\r
192 System.out.println(MetadataHelper.getParametersList(msaws));
\r
194 boolean listPreset = CmdHelper.listPresets(cmd);
\r
196 System.out.println(MetadataHelper.getPresetList(msaws));
\r
198 boolean listLimits = CmdHelper.listLimits(cmd);
\r
200 System.out.println(MetadataHelper.getLimits(msaws));
\r
202 log.fine("Disconnecting...");
\r
203 ((Closeable) msaws).close();
\r
204 log.fine("Disconnected successfully!");
\r
208 * Asks registry to test the service on the host hostname
\r
213 * @throws ConnectException
\r
214 * @throws WebServiceException
\r
216 public static void testService(String hostname, Services service,
\r
217 PrintWriter writer) throws ConnectException, WebServiceException {
\r
218 RegistryWS registry = connectToRegistry(hostname);
\r
219 if (registry != null) {
\r
220 String message = registry.testService(service);
\r
221 writer.println("Service " + service + " testing results: ");
\r
222 writer.println(message);
\r
223 FileUtil.closeSilently(((Closeable) registry));
\r
228 public static Set<Services> getServices(String hostname)
\r
229 throws WebServiceException, ConnectException {
\r
230 RegistryWS registry = connectToRegistry(hostname);
\r
231 Set<Services> services = Collections.EMPTY_SET;
\r
232 if (registry != null) {
\r
233 services = registry.getSupportedServices();
\r
234 FileUtil.closeSilently(((Closeable) registry));
\r
239 private static void listServices(String hostname)
\r
240 throws WebServiceException, IOException {
\r
241 Set<Services> services = Jws2Client.getServices(hostname);
\r
242 if (!services.isEmpty()) {
\r
243 System.out.println("Supported services are: "
\r
244 + Services.toString(services));
\r
246 System.out.println("Failed to connect to the registry! ");
\r
251 * Calculate conservation for sequences loaded from the file
\r
254 * a web service proxy
\r
256 * the file to read the results from
\r
258 * Preset to use optional
\r
259 * @param customOptions
\r
260 * the list of options
\r
261 * @return Set<Score> the conservation scores
\r
262 * @throws UnknownFileFormatException
\r
264 static <T> ScoreManager analize(List<FastaSequence> fastalist,
\r
265 SequenceAnnotation<T> wsproxy, Preset<T> preset,
\r
266 List<Option<T>> customOptions) {
\r
268 ScoreManager scores = null;
\r
270 String jobId = null;
\r
271 if (customOptions != null && preset != null) {
\r
273 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
275 if (customOptions != null) {
\r
276 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
277 } else if (preset != null) {
\r
278 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
280 jobId = wsproxy.analize(fastalist);
\r
282 System.out.println("\n\ncalling analise.........");
\r
283 Thread.sleep(1000);
\r
284 scores = wsproxy.getAnnotation(jobId);
\r
286 } catch (JobSubmissionException e) {
\r
288 .println("Exception while submitting job to a web server. "
\r
289 + "Exception details are below:");
\r
290 e.printStackTrace();
\r
291 } catch (ResultNotAvailableException e) {
\r
292 System.err.println("Exception while waiting for results. "
\r
293 + "Exception details are below:");
\r
294 e.printStackTrace();
\r
295 } catch (InterruptedException e) {
\r
296 // ignore and propagate an interruption
\r
297 Thread.currentThread().interrupt();
\r
298 System.err.println("Exception while waiting for results. "
\r
299 + "Exception details are below:");
\r
300 e.printStackTrace();
\r
301 } catch (WrongParameterException e) {
\r
303 .println("Exception while parsing the web method input parameters. "
\r
304 + "Exception details are below:");
\r
305 e.printStackTrace();
\r
312 * Calculate conservation for sequences loaded from the file
\r
315 * a web service proxy
\r
317 * the file to read the results from
\r
319 * Preset to use optional
\r
320 * @param customOptions
\r
321 * the list of options
\r
322 * @return Set<Score> the conservation scores
\r
323 * @throws IOException
\r
324 * @throws UnknownFileFormatException
\r
326 static <T> ScoreManager analize(File file, SequenceAnnotation<T> wsproxy,
\r
327 Preset<T> preset, List<Option<T>> customOptions) {
\r
328 List<FastaSequence> fastalist = null;
\r
330 fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());
\r
331 assert !fastalist.isEmpty() : "Input is empty!";
\r
332 } catch (IOException e) {
\r
334 .println("Exception while reading the input file. "
\r
335 + "Check that the input file contains a list of fasta formatted sequences! "
\r
336 + "Exception details are below:");
\r
337 e.printStackTrace();
\r
338 } catch (UnknownFileFormatException e) {
\r
340 .println("Exception while attempting to read the input file "
\r
341 + "Exception details are below:");
\r
342 System.out.println(e.getMessage());
\r
343 e.printStackTrace();
\r
345 return analize(fastalist, wsproxy, preset, customOptions);
\r
348 * Connects to a web service by the host and the service name web service
\r
352 * the fully qualified name of JABAWS server including JABAWS
\r
354 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
356 * the name of the JABAWS service to connect to
\r
357 * @return JABAService<T>
\r
358 * @throws WebServiceException
\r
359 * @throws ConnectException
\r
360 * if fails to connect to the service on the host
\r
362 public static JABAService connect(String host, Services service)
\r
363 throws WebServiceException, ConnectException {
\r
365 log.log(Level.FINE, "Attempting to connect...");
\r
367 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
368 } catch (MalformedURLException e) {
\r
369 e.printStackTrace();
\r
370 // ignore as the host name is already verified
\r
372 Service serv = null;
\r
374 serv = service.getService(url, JABAService.SERVICE_NAMESPACE);
\r
375 } catch (WebServiceException wse) {
\r
376 System.out.println("Connecting to JABAWS version 2 service");
\r
377 if (isV2service(wse)) {
\r
379 .getService(url, JABAService.V2_SERVICE_NAMESPACE);
\r
382 if (serv == null) {
\r
383 throw new ConnectException("Could not connect to " + url
\r
384 + " the server is down?");
\r
386 JABAService serviceIF = service.getInterface(serv);
\r
387 log.log(Level.INFO, "Connected successfully!");
\r
392 static boolean isV2service(WebServiceException wse) {
\r
393 String message = wse.getMessage();
\r
394 int idx = message.indexOf("not a valid service");
\r
396 if (message.substring(idx).contains(
\r
397 JABAService.V2_SERVICE_NAMESPACE)) {
\r
405 * Get a connection of JABAWS registry
\r
408 * the fully qualified name of JABAWS server including JABAWS
\r
410 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
411 * @return compbio.data.msa.RegistryWS - instance of a RegistryWS web
\r
413 * @throws WebServiceException
\r
414 * @throws ConnectException
\r
416 public static compbio.data.msa.RegistryWS connectToRegistry(String host)
\r
417 throws WebServiceException, ConnectException {
\r
419 String service = "RegistryWS";
\r
420 log.log(Level.FINE, "Attempting to connect...");
\r
423 url = new URL(host + "/" + service + "?wsdl");
\r
424 } catch (MalformedURLException e) {
\r
425 e.printStackTrace();
\r
426 // ignore as the host name is already verified
\r
428 QName qname = new QName(JABAService.V2_SERVICE_NAMESPACE, service);
\r
429 Service serv = Service.create(url, qname);
\r
431 if (serv == null) {
\r
432 throw new ConnectException("Could not connect to " + url
\r
433 + " the server is down?");
\r
436 QName portName = new QName(serv.getServiceName().getNamespaceURI(),
\r
438 compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName,
\r
439 compbio.data.msa.RegistryWS.class);
\r
441 log.log(Level.INFO, "Connected to " + service + " successfully!");
\r
447 * Align sequences from the file using MsaWS
\r
450 * web service type e.g. Clustal
\r
452 * to write the resulting alignment to
\r
456 * Preset to use optional
\r
457 * @param customOptions
\r
458 * file which contains new line separated list of options
\r
459 * @return Alignment
\r
461 static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,
\r
462 List<Option<T>> customOptions) {
\r
463 FileInputStream instream = null;
\r
464 List<FastaSequence> fastalist = null;
\r
465 Alignment alignment = null;
\r
467 instream = new FileInputStream(file);
\r
468 fastalist = SequenceUtil.readFasta(instream);
\r
470 String jobId = null;
\r
471 if (customOptions != null && preset != null) {
\r
473 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
475 if (customOptions != null) {
\r
476 jobId = msaws.customAlign(fastalist, customOptions);
\r
477 } else if (preset != null) {
\r
478 jobId = msaws.presetAlign(fastalist, preset);
\r
480 jobId = msaws.align(fastalist);
\r
482 System.out.println("\n\ncalling align.........");
\r
483 Thread.sleep(1000);
\r
484 alignment = msaws.getResult(jobId);
\r
486 } catch (IOException e) {
\r
488 .println("Exception while reading the input file. "
\r
489 + "Check that the input file contains a list of fasta formatted sequences! "
\r
490 + "Exception details are below:");
\r
491 e.printStackTrace();
\r
492 } catch (JobSubmissionException e) {
\r
494 .println("Exception while submitting job to a web server. "
\r
495 + "Exception details are below:");
\r
496 e.printStackTrace();
\r
497 } catch (ResultNotAvailableException e) {
\r
498 System.err.println("Exception while waiting for results. "
\r
499 + "Exception details are below:");
\r
500 e.printStackTrace();
\r
501 } catch (InterruptedException ignored) {
\r
502 // ignore and propagate an interruption
\r
503 Thread.currentThread().interrupt();
\r
504 } catch (WrongParameterException e) {
\r
505 e.printStackTrace();
\r
507 if (instream != null) {
\r
510 } catch (IOException ignored) {
\r
519 * Return RNA secondary structure from a file using FoldWS
\r
522 * web service type e.g. Clustal
\r
524 * to read the results from
\r
528 * Preset to use optional
\r
529 * @param customOptions
\r
530 * file which contains new line separated list of options
\r
534 // static <T> String fold(File file, FoldWS<T> foldws, Preset<T> preset,
\r
535 // List<Option<T>> customOptions) {
\r
536 // FileInputStream instream = null;
\r
537 // Alignment alignment = null;
\r
538 // String rnastruct = null;
\r
540 // instream = new FileInputStream(file);
\r
541 // alignment = ClustalAlignmentUtil.readClustalFile(instream);
\r
542 // instream.close();
\r
543 // String jobId = null;
\r
544 // if (customOptions != null && preset != null) {
\r
545 // System.out.println("WARN: Parameters (-f) are defined together"
\r
546 // + "with a preset (-r), ignoring preset! ");
\r
548 // if (customOptions != null) {
\r
549 // jobId = foldws.customFold(alignment, customOptions);
\r
550 // } else if (preset != null) {
\r
551 // jobId = foldws.presetFold(alignment, preset);
\r
553 // jobId = foldws.fold(alignment);
\r
555 // System.out.println("\n\ncalling fold.........");
\r
556 // Thread.sleep(1000);
\r
557 // rnastruct = foldws.getResult(jobId);
\r
559 // } catch (IOException e) {
\r
560 // System.err.println("Exception while reading the input file. Exception details: ");
\r
561 // e.printStackTrace();
\r
562 // } catch (UnknownFileFormatException e) {
\r
563 // System.err.println("Exception while reading input file. Doesnt look like a Clustal format file");
\r
564 // e.printStackTrace();
\r
565 // } catch (JobSubmissionException e) {
\r
566 // System.err.println("Exception while submitting job to the web server. ");
\r
567 // e.printStackTrace();
\r
568 // } catch (ResultNotAvailableException e) {
\r
569 // System.err.println("Exception while waiting for results. Exception details: ");
\r
570 // e.printStackTrace();
\r
571 // } catch (InterruptedException ignored) {
\r
572 // // ignore and propagate an interruption
\r
573 // Thread.currentThread().interrupt();
\r
574 // } catch (WrongParameterException e) {
\r
575 // e.printStackTrace();
\r
577 // if (instream != null) {
\r
579 // instream.close();
\r
580 // } catch (IOException ignored) {
\r
585 // return rnastruct;
\r
589 * Prints Jws2Client usage information to standard out
\r
591 * @param exitStatus
\r
593 static void printUsage(int exitStatus) {
\r
594 System.out.println(Constraints.help_text);
\r
595 System.exit(exitStatus);
\r
599 * Starts command line client, if no parameter are supported print help. Two
\r
600 * parameters are required for successful call the JWS2 host name and a
\r
604 * Usage: <Class or Jar file name> -h=host_and_context
\r
605 * -s=serviceName ACTION [OPTIONS]
\r
607 * -h=<host_and_context> - a full URL to the JWS2 web server
\r
608 * including context path e.g. http://10.31.1.159:8080/ws
\r
610 * -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,
\r
611 * TcoffeeWS, ProbconsWS] ACTIONS:
\r
613 * -i=<inputFile> - full path to fasta formatted sequence file,
\r
614 * from which to align sequences
\r
616 * -parameters - lists parameters supported by web service
\r
618 * -presets - lists presets supported by web service
\r
620 * -limits - lists web services limits Please note that if input
\r
621 * file is specified other actions are ignored
\r
623 * OPTIONS: (only for use with -i action):
\r
625 * -r=<presetName> - name of the preset to use
\r
627 * -o=<outputFile> - full path to the file where to write an
\r
628 * alignment -f=<parameterInputFile> - the name of the file with
\r
629 * the list of parameters to use. Please note that -r and -f
\r
630 * options cannot be used together. Alignment is done with either
\r
631 * preset or a parameters from the file, but not both!
\r
634 public static void main(String[] args) {
\r
636 if (args == null) {
\r
639 if (args.length < 2) {
\r
640 System.err.println("Host and service names are required!");
\r
645 new Jws2Client(args);
\r
646 } catch (IOException e) {
\r
647 log.log(Level.SEVERE, "IOException in client! " + e.getMessage(),
\r
649 System.err.println("Cannot write output file! Stack trace: ");
\r
650 e.printStackTrace();
\r