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 client package
\r
73 private static final Logger log = Logger.getLogger(Jws2Client.class.getCanonicalName());
\r
76 * Attempt to construct the URL object from the string
\r
79 * @return true if it succeed false otherwise
\r
81 public static boolean validURL(String urlstr) {
\r
83 if (urlstr == null || urlstr.trim().length() == 0) {
\r
87 } catch (MalformedURLException e) {
\r
94 * Connects to the service and do the job as requested, if something goes
\r
95 * wrong reports or/and prints usage help.
\r
100 * command line options
\r
101 * @throws IOException
\r
103 <T> Jws2Client(String[] cmd) throws IOException {
\r
105 String hostname = CmdHelper.getHost(cmd);
\r
106 if (hostname == null) {
\r
107 System.err.println("Host name is not provided!");
\r
108 System.out.println(Constraints.help_text);
\r
112 if (!validURL(hostname)) {
\r
113 System.err.println("Host name is not valid!");
\r
114 System.out.println(Constraints.help_text);
\r
118 boolean listServices = CmdHelper.listServices(cmd);
\r
119 if (listServices) {
\r
120 listAllServices(hostname);
\r
124 String serviceName = CmdHelper.getServiceName(cmd);
\r
125 if (serviceName == null) {
\r
126 System.err.println("Service name is no provided!");
\r
127 System.out.println(Constraints.help_text);
\r
131 Services service = Services.getService(serviceName);
\r
132 if (service == null) {
\r
133 String mess = "Service " + serviceName + " is no available! Valid values are: ";
\r
134 System.err.println(mess + Arrays.toString(Services.values()));
\r
135 System.out.println(Constraints.help_text);
\r
139 if (CmdHelper.testService(cmd)) {
\r
140 testService(hostname, service, new PrintWriter(System.out, true));
\r
144 Metadata<T> thews = (Metadata<T>) connect(hostname, service);
\r
145 Preset<T> preset = null;
\r
146 if (null != CmdHelper.getPresetName(cmd)) {
\r
147 preset = MetadataHelper.getPreset(thews, CmdHelper.getPresetName(cmd));
\r
150 List<Option<T>> customOptions = null;
\r
151 if (null != IOHelper.getFile(cmd, paramFile, true)) {
\r
152 List<String> prms = IOHelper.loadParameters(IOHelper.getFile(cmd, paramFile, true));
\r
153 customOptions = MetadataHelper.processParameters(prms, thews.getRunnerOptions());
\r
156 if (null != IOHelper.getFile(cmd, inputkey, true)) {
\r
157 File infile = IOHelper.getFile(cmd, inputkey, true);
\r
158 File outfile = IOHelper.getFile(cmd, outputkey, false);
\r
159 Writer writer = null;
\r
160 if (outfile != null) {
\r
161 writer = IOHelper.getWriter(outfile);
\r
163 // this stream is going to be closed later which is fine as std.out will not be
\r
164 writer = new PrintWriter(System.out, true);
\r
166 if (service.getServiceType() == SequenceAnnotation.class) {
\r
167 ScoreManager result = analize(infile, ((SequenceAnnotation<T>) thews), preset, customOptions);
\r
168 IOHelper.writeOut(writer, result);
\r
169 } else if (service.getServiceType() == MsaWS.class) {
\r
170 Alignment alignment = align(infile, (MsaWS<T>) thews, preset, customOptions);
\r
171 IOHelper.writeOut(writer, alignment);
\r
176 if (CmdHelper.listParameters(cmd)) {
\r
177 System.out.println(MetadataHelper.getParametersList(thews));
\r
179 if (CmdHelper.listPresets(cmd)) {
\r
180 System.out.println(MetadataHelper.getPresetList(thews));
\r
182 if (CmdHelper.listLimits(cmd)) {
\r
183 System.out.println(MetadataHelper.getLimits(thews));
\r
185 log.fine("Disconnecting...");
\r
186 ((Closeable) thews).close();
\r
187 log.fine("Disconnected successfully!");
\r
191 * Connects to a web service by the host and the service name web service type
\r
194 * the fully qualified name of JABAWS server including JABAWS
\r
196 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
198 * the name of the JABAWS service to connect to
\r
199 * @return JABAService<T>
\r
200 * @throws WebServiceException
\r
201 * @throws ConnectException
\r
202 * if fails to connect to the service on the host
\r
204 public static JABAService connect(String host, Services service)
\r
205 throws WebServiceException, ConnectException {
\r
207 log.log(Level.FINE, "Attempting to connect with " + service.toString() + "...");
\r
208 System.out.println ("Attempting to connect with " + service.toString() + "...");
\r
210 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
211 } catch (MalformedURLException e) {
\r
212 e.printStackTrace();
\r
213 // ignore as the host name is already verified
\r
215 Service serv = null;
\r
217 serv = service.getService(url, service.getServiceNamespace());
\r
218 } catch (WebServiceException wse) {
\r
219 wse.printStackTrace();
\r
221 if (serv == null) {
\r
222 throw new ConnectException("Could not connect to " + url + ". Is the server down?");
\r
224 JABAService serviceIF = service.getInterface(serv);
\r
225 log.log(Level.INFO, "Connected successfully!");
\r
231 * Get a connection of JABAWS registry
\r
234 * the fully qualified name of JABAWS server including JABAWS
\r
236 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
237 * @return compbio.data.msa.RegistryWS - instance of a RegistryWS web
\r
239 * @throws WebServiceException
\r
240 * @throws ConnectException
\r
242 public static compbio.data.msa.RegistryWS connectToRegistry(String host)
\r
243 throws WebServiceException, ConnectException {
\r
245 String service = "RegistryWS";
\r
246 log.log(Level.FINE, "Attempting to connect...");
\r
249 url = new URL(host + "/" + service + "?wsdl");
\r
250 } catch (MalformedURLException e) {
\r
251 e.printStackTrace();
\r
252 // ignore as the host name is already verified
\r
254 QName qname = new QName(JABAService.V3_SERVICE_NAMESPACE, service);
\r
255 Service serv = Service.create(url, qname);
\r
257 if (serv == null) {
\r
258 throw new ConnectException("Could not connect to " + url + ". Is the server down?");
\r
261 QName portName = new QName(serv.getServiceName().getNamespaceURI(), service + "Port");
\r
262 compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName, compbio.data.msa.RegistryWS.class);
\r
263 log.log(Level.INFO, "Connected to " + service + " successfully!");
\r
269 * Asks registry to test the service on the host hostname
\r
274 * @throws ConnectException
\r
275 * @throws WebServiceException
\r
277 public static void testService(String hostname, Services service, PrintWriter writer)
\r
278 throws ConnectException, WebServiceException {
\r
279 RegistryWS registry = connectToRegistry(hostname);
\r
280 if (registry != null) {
\r
281 String message = registry.testService(service);
\r
282 writer.println("Service " + service + " testing results: ");
\r
283 writer.println(message);
\r
284 FileUtil.closeSilently(((Closeable) registry));
\r
289 private static void listAllServices(String hostname) throws WebServiceException, IOException {
\r
290 RegistryWS registry = connectToRegistry(hostname);
\r
291 Set<Services> func_services = Collections.EMPTY_SET;
\r
292 Set<Services> nonfunc_services = Collections.EMPTY_SET;
\r
293 if (registry != null) {
\r
294 func_services = registry.getSupportedServices();
\r
295 nonfunc_services = registry.getNonoperatedServices();
\r
296 FileUtil.closeSilently(((Closeable) registry));
\r
298 System.out.println("Failed to connect to the registry! ");
\r
301 if (!func_services.isEmpty()) {
\r
302 String mess = "\n\rAvailable services: ";
\r
303 System.out.println(mess + Services.toString(func_services));
\r
305 if (!nonfunc_services.isEmpty()) {
\r
306 String mess = "Non-available services (internal tests failed): ";
\r
307 System.out.println(mess + Services.toString(nonfunc_services));
\r
312 * Calculate conservation for sequences loaded from a FASTA record list structure
\r
315 * the list of FASTA records
\r
317 * a web service proxy
\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 UnknownFileFormatException
\r
325 static <T> ScoreManager analize(List<FastaSequence> fastalist, SequenceAnnotation<T> wsproxy, Preset<T> preset, List<Option<T>> customOptions) {
\r
326 ScoreManager scores = null;
\r
328 String jobId = null;
\r
329 if (customOptions != null && preset != null) {
\r
330 System.out.println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
332 if (customOptions != null) {
\r
333 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
334 } else if (preset != null) {
\r
335 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
337 jobId = wsproxy.analize(fastalist);
\r
339 System.out.println("\n\rcalling predictor.........");
\r
341 scores = wsproxy.getAnnotation(jobId);
\r
342 } catch (JobSubmissionException e) {
\r
343 System.err.println("Exception while submitting job to a web server. Exception details are below:");
\r
344 e.printStackTrace();
\r
345 } catch (ResultNotAvailableException e) {
\r
346 System.err.println("Exception while waiting for results. Exception details are below:");
\r
347 e.printStackTrace();
\r
348 } catch (InterruptedException e) {
\r
349 Thread.currentThread().interrupt();
\r
350 System.err.println("Exception while waiting for results. Exception details are below:");
\r
351 e.printStackTrace();
\r
352 } catch (WrongParameterException e) {
\r
353 String mess = "Parsing the web method input parameters failed Exception details are below:";
\r
354 System.err.println(mess);
\r
355 e.printStackTrace();
\r
362 * Calculate conservation for sequences loaded from the file
\r
365 * a web service proxy
\r
367 * the file to read the results from
\r
369 * Preset to use optional
\r
370 * @param customOptions
\r
371 * the list of options
\r
372 * @return Set<Score> the conservation scores
\r
373 * @throws IOException
\r
374 * @throws UnknownFileFormatException
\r
376 static <T> ScoreManager analize(File file, SequenceAnnotation<T> wsproxy, Preset<T> preset, List<Option<T>> customOptions) {
\r
377 List<FastaSequence> fastalist = null;
\r
379 fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());
\r
380 assert !fastalist.isEmpty() : "Input is empty!";
\r
381 } catch (IOException e) {
\r
382 String mess = "Reading the input file failed. Check that the file contains a list of FASTA records!\n";
\r
383 System.err.println(mess + "Exception details are below:");
\r
384 e.printStackTrace();
\r
385 } catch (UnknownFileFormatException e) {
\r
386 String mess = "Reading the input file failed. Exception details are below:";
\r
387 System.err.println(mess);
\r
388 System.out.println(e.getMessage());
\r
389 e.printStackTrace();
\r
391 return analize(fastalist, wsproxy, preset, customOptions);
\r
395 * Align sequences from the file using MsaWS
\r
398 * web service type e.g. Clustal
\r
400 * to write the resulting alignment to
\r
404 * Preset to use optional
\r
405 * @param customOptions
\r
406 * file which contains new line separated list of options
\r
407 * @return Alignment
\r
409 static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,
\r
410 List<Option<T>> customOptions) {
\r
411 FileInputStream instream = null;
\r
412 List<FastaSequence> fastalist = null;
\r
413 Alignment alignment = null;
\r
415 instream = new FileInputStream(file);
\r
416 fastalist = SequenceUtil.readFasta(instream);
\r
418 String jobId = null;
\r
419 if (customOptions != null && preset != null) {
\r
420 System.out.println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
422 if (customOptions != null) {
\r
423 jobId = msaws.customAlign(fastalist, customOptions);
\r
424 } else if (preset != null) {
\r
425 jobId = msaws.presetAlign(fastalist, preset);
\r
427 jobId = msaws.align(fastalist);
\r
429 System.out.println("\ncalling aligner.........");
\r
430 Thread.sleep(1000);
\r
431 alignment = msaws.getResult(jobId);
\r
433 } catch (IOException e) {
\r
434 System.err.println("Exception while reading the input file. "
\r
435 + "Check that the input file contains a list of fasta formatted sequences! "
\r
436 + "Exception details are below:");
\r
437 e.printStackTrace();
\r
438 } catch (JobSubmissionException e) {
\r
439 System.err.println("Exception while submitting job to a web server. "
\r
440 + "Exception details are below:");
\r
441 e.printStackTrace();
\r
442 } catch (ResultNotAvailableException e) {
\r
443 System.err.println("Exception while waiting for results. "
\r
444 + "Exception details are below:");
\r
445 e.printStackTrace();
\r
446 } catch (InterruptedException ignored) {
\r
447 // ignore and propagate an interruption
\r
448 Thread.currentThread().interrupt();
\r
449 } catch (WrongParameterException e) {
\r
450 e.printStackTrace();
\r
452 if (instream != null) {
\r
455 } catch (IOException ignored) {
\r
464 * Starts command line client, if no parameter are supported print help. Two
\r
465 * parameters are required for successful call the JWS2 host name and a
\r
469 * Usage: <Class or Jar file name> -h=host_and_context
\r
470 * -s=serviceName ACTION [OPTIONS]
\r
472 * -h=<host_and_context> - a full URL to the JWS2 web server
\r
473 * including context path e.g. http://10.31.1.159:8080/ws
\r
475 * -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,
\r
476 * TcoffeeWS, ProbconsWS] ACTIONS:
\r
478 * -i=<inputFile> - full path to fasta formatted sequence file,
\r
479 * from which to align sequences
\r
481 * -parameters - lists parameters supported by web service
\r
483 * -presets - lists presets supported by web service
\r
485 * -limits - lists web services limits Please note that if input
\r
486 * file is specified other actions are ignored
\r
488 * OPTIONS: (only for use with -i action):
\r
490 * -r=<presetName> - name of the preset to use
\r
492 * -o=<outputFile> - full path to the file where to write an
\r
493 * alignment -f=<parameterInputFile> - the name of the file with
\r
494 * the list of parameters to use. Please note that -r and -f
\r
495 * options cannot be used together. Alignment is done with either
\r
496 * preset or a parameters from the file, but not both!
\r
499 public static void main(String[] args) {
\r
501 if (args == null) {
\r
502 System.out.println(Constraints.help_text);
\r
505 if (args.length < 2) {
\r
506 System.err.println("Host (-h=<host>, e.g. -h=http://www.compbio.dundee.ac.uk/jabaws) and service (-s=<ServiceName>, e.g. -s=Jpred) are required!");
\r
507 System.out.println(Constraints.help_text);
\r
512 new Jws2Client(args);
\r
513 } catch (IOException e) {
\r
514 log.log(Level.SEVERE, "IOException in client! " + e.getMessage(), e.getCause());
\r
515 System.err.println("Cannot write output file! Stack trace: ");
\r
516 e.printStackTrace();
\r