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
108 String hostname = CmdHelper.getHost(cmd);
\r
109 if (hostname == null) {
\r
110 System.err.println("Host name is not provided!");
\r
114 if (!validURL(hostname)) {
\r
115 System.err.println("Host name is not valid!");
\r
118 // Just list available services and quit
\r
119 boolean listServices = CmdHelper.listServices(cmd);
\r
120 if (listServices) {
\r
121 listServices(hostname);
\r
125 String serviceName = CmdHelper.getServiceName(cmd);
\r
126 if (serviceName == null) {
\r
127 System.err.println("Service name is no provided!");
\r
130 Services service = Services.getService(serviceName);
\r
131 if (service == null) {
\r
132 System.err.println("Service " + serviceName
\r
133 + " is no recognized! Valid values are: "
\r
134 + Arrays.toString(Services.values()));
\r
137 // Test service and quit
\r
138 boolean testService = CmdHelper.testService(cmd);
\r
140 testService(hostname, service, new PrintWriter(System.out, true));
\r
144 File inputFile = IOHelper.getFile(cmd, inputkey, true);
\r
145 File outFile = IOHelper.getFile(cmd, outputkey, false);
\r
146 File parametersFile = IOHelper.getFile(cmd, paramFile, true);
\r
147 String presetName = CmdHelper.getPresetName(cmd);
\r
149 Metadata<T> msaws = (Metadata<T>) connect(hostname, service);
\r
150 Preset<T> preset = null;
\r
151 if (presetName != null) {
\r
152 preset = MetadataHelper.getPreset(msaws, presetName);
\r
154 List<Option<T>> customOptions = null;
\r
155 if (parametersFile != null) {
\r
156 List<String> prms = IOHelper.loadParameters(parametersFile);
\r
157 customOptions = MetadataHelper.processParameters(prms,
\r
158 msaws.getRunnerOptions());
\r
160 // System.out.println("The Options read from the command line: " + customOptions);
\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 // A System.out.println just for testing!
\r
177 System.out.println(result.toString());
\r
179 IOHelper.writeOut(writer, result);
\r
180 } else if (service.getServiceType() == MsaWS.class) {
\r
181 alignment = align(inputFile, (MsaWS<T>) msaws, preset,
\r
183 IOHelper.writeOut(writer, alignment);
\r
188 boolean listParameters = CmdHelper.listParameters(cmd);
\r
189 if (listParameters) {
\r
190 System.out.println(MetadataHelper.getParametersList(msaws));
\r
192 boolean listPreset = CmdHelper.listPresets(cmd);
\r
194 System.out.println(MetadataHelper.getPresetList(msaws));
\r
196 boolean listLimits = CmdHelper.listLimits(cmd);
\r
198 System.out.println(MetadataHelper.getLimits(msaws));
\r
200 log.fine("Disconnecting...");
\r
201 ((Closeable) msaws).close();
\r
202 log.fine("Disconnected successfully!");
\r
206 * Asks registry to test the service on the host hostname
\r
211 * @throws ConnectException
\r
212 * @throws WebServiceException
\r
214 public static void testService(String hostname, Services service,
\r
215 PrintWriter writer) throws ConnectException, WebServiceException {
\r
216 RegistryWS registry = connectToRegistry(hostname);
\r
217 if (registry != null) {
\r
218 String message = registry.testService(service);
\r
219 writer.println("Service " + service + " testing results: ");
\r
220 writer.println(message);
\r
221 FileUtil.closeSilently(((Closeable) registry));
\r
226 public static Set<Services> getServices(String hostname)
\r
227 throws WebServiceException, ConnectException {
\r
228 RegistryWS registry = connectToRegistry(hostname);
\r
229 Set<Services> services = Collections.EMPTY_SET;
\r
230 if (registry != null) {
\r
231 services = registry.getSupportedServices();
\r
232 FileUtil.closeSilently(((Closeable) registry));
\r
237 private static void listServices(String hostname)
\r
238 throws WebServiceException, IOException {
\r
239 Set<Services> services = Jws2Client.getServices(hostname);
\r
240 if (!services.isEmpty()) {
\r
241 System.out.println("Supported services are: "
\r
242 + Services.toString(services));
\r
244 System.out.println("Failed to connect to the registry! ");
\r
249 * Calculate conservation for sequences loaded from the file
\r
252 * a web service proxy
\r
254 * the file to read the results from
\r
256 * Preset to use optional
\r
257 * @param customOptions
\r
258 * the list of options
\r
259 * @return Set<Score> the conservation scores
\r
260 * @throws UnknownFileFormatException
\r
262 static <T> ScoreManager analize(List<FastaSequence> fastalist,
\r
263 SequenceAnnotation<T> wsproxy, Preset<T> preset,
\r
264 List<Option<T>> customOptions) {
\r
266 ScoreManager scores = null;
\r
268 String jobId = null;
\r
269 if (customOptions != null && preset != null) {
\r
271 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
273 if (customOptions != null) {
\r
274 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
275 } else if (preset != null) {
\r
276 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
278 jobId = wsproxy.analize(fastalist);
\r
280 System.out.println("\n\ncalling analise.........");
\r
281 Thread.sleep(1000);
\r
282 scores = wsproxy.getAnnotation(jobId);
\r
284 } catch (JobSubmissionException e) {
\r
286 .println("Exception while submitting job to a web server. "
\r
287 + "Exception details are below:");
\r
288 e.printStackTrace();
\r
289 } catch (ResultNotAvailableException e) {
\r
290 System.err.println("Exception while waiting for results. "
\r
291 + "Exception details are below:");
\r
292 e.printStackTrace();
\r
293 } catch (InterruptedException e) {
\r
294 // ignore and propagate an interruption
\r
295 Thread.currentThread().interrupt();
\r
296 System.err.println("Exception while waiting for results. "
\r
297 + "Exception details are below:");
\r
298 e.printStackTrace();
\r
299 } catch (WrongParameterException e) {
\r
301 .println("Exception while parsing the web method input parameters. "
\r
302 + "Exception details are below:");
\r
303 e.printStackTrace();
\r
310 * Calculate conservation for sequences loaded from the file
\r
313 * a web service proxy
\r
315 * the file to read the results from
\r
317 * Preset to use optional
\r
318 * @param customOptions
\r
319 * the list of options
\r
320 * @return Set<Score> the conservation scores
\r
321 * @throws IOException
\r
322 * @throws UnknownFileFormatException
\r
324 static <T> ScoreManager analize(File file, SequenceAnnotation<T> wsproxy,
\r
325 Preset<T> preset, List<Option<T>> customOptions) {
\r
326 List<FastaSequence> fastalist = null;
\r
328 fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());
\r
329 assert !fastalist.isEmpty() : "Input is empty!";
\r
330 } catch (IOException e) {
\r
332 .println("Exception while reading the input file. "
\r
333 + "Check that the input file contains a list of fasta formatted sequences! "
\r
334 + "Exception details are below:");
\r
335 e.printStackTrace();
\r
336 } catch (UnknownFileFormatException e) {
\r
338 .println("Exception while attempting to read the input file "
\r
339 + "Exception details are below:");
\r
340 System.out.println(e.getMessage());
\r
341 e.printStackTrace();
\r
343 return analize(fastalist, wsproxy, preset, customOptions);
\r
346 * Connects to a web service by the host and the service name web service
\r
350 * the fully qualified name of JABAWS server including JABAWS
\r
352 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
354 * the name of the JABAWS service to connect to
\r
355 * @return JABAService<T>
\r
356 * @throws WebServiceException
\r
357 * @throws ConnectException
\r
358 * if fails to connect to the service on the host
\r
360 public static JABAService connect(String host, Services service)
\r
361 throws WebServiceException, ConnectException {
\r
363 log.log(Level.FINE, "Attempting to connect...");
\r
365 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
366 } catch (MalformedURLException e) {
\r
367 e.printStackTrace();
\r
368 // ignore as the host name is already verified
\r
370 Service serv = null;
\r
372 serv = service.getService(url, JABAService.SERVICE_NAMESPACE);
\r
373 } catch (WebServiceException wse) {
\r
374 System.out.println("Connecting to JABAWS version 2 service");
\r
375 if (isV2service(wse)) {
\r
377 .getService(url, JABAService.V2_SERVICE_NAMESPACE);
\r
380 if (serv == null) {
\r
381 throw new ConnectException("Could not connect to " + url
\r
382 + " the server is down?");
\r
384 JABAService serviceIF = service.getInterface(serv);
\r
385 log.log(Level.INFO, "Connected successfully!");
\r
390 static boolean isV2service(WebServiceException wse) {
\r
391 String message = wse.getMessage();
\r
392 int idx = message.indexOf("not a valid service");
\r
394 if (message.substring(idx).contains(
\r
395 JABAService.V2_SERVICE_NAMESPACE)) {
\r
403 * Get a connection of JABAWS registry
\r
406 * the fully qualified name of JABAWS server including JABAWS
\r
408 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
409 * @return compbio.data.msa.RegistryWS - instance of a RegistryWS web
\r
411 * @throws WebServiceException
\r
412 * @throws ConnectException
\r
414 public static compbio.data.msa.RegistryWS connectToRegistry(String host)
\r
415 throws WebServiceException, ConnectException {
\r
417 String service = "RegistryWS";
\r
418 log.log(Level.FINE, "Attempting to connect...");
\r
421 url = new URL(host + "/" + service + "?wsdl");
\r
422 } catch (MalformedURLException e) {
\r
423 e.printStackTrace();
\r
424 // ignore as the host name is already verified
\r
426 QName qname = new QName(JABAService.V2_SERVICE_NAMESPACE, service);
\r
427 Service serv = Service.create(url, qname);
\r
429 if (serv == null) {
\r
430 throw new ConnectException("Could not connect to " + url
\r
431 + " the server is down?");
\r
434 QName portName = new QName(serv.getServiceName().getNamespaceURI(),
\r
436 compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName,
\r
437 compbio.data.msa.RegistryWS.class);
\r
439 log.log(Level.INFO, "Connected to " + service + " successfully!");
\r
445 * Align sequences from the file using MsaWS
\r
448 * web service type e.g. Clustal
\r
450 * to write the resulting alignment to
\r
454 * Preset to use optional
\r
455 * @param customOptions
\r
456 * file which contains new line separated list of options
\r
457 * @return Alignment
\r
459 static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,
\r
460 List<Option<T>> customOptions) {
\r
461 FileInputStream instream = null;
\r
462 List<FastaSequence> fastalist = null;
\r
463 Alignment alignment = null;
\r
465 instream = new FileInputStream(file);
\r
466 fastalist = SequenceUtil.readFasta(instream);
\r
468 String jobId = null;
\r
469 if (customOptions != null && preset != null) {
\r
471 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
473 if (customOptions != null) {
\r
474 jobId = msaws.customAlign(fastalist, customOptions);
\r
475 } else if (preset != null) {
\r
476 jobId = msaws.presetAlign(fastalist, preset);
\r
478 jobId = msaws.align(fastalist);
\r
480 System.out.println("\n\ncalling align.........");
\r
481 Thread.sleep(1000);
\r
482 alignment = msaws.getResult(jobId);
\r
484 } catch (IOException e) {
\r
486 .println("Exception while reading the input file. "
\r
487 + "Check that the input file contains a list of fasta formatted sequences! "
\r
488 + "Exception details are below:");
\r
489 e.printStackTrace();
\r
490 } catch (JobSubmissionException e) {
\r
492 .println("Exception while submitting job to a web server. "
\r
493 + "Exception details are below:");
\r
494 e.printStackTrace();
\r
495 } catch (ResultNotAvailableException e) {
\r
496 System.err.println("Exception while waiting for results. "
\r
497 + "Exception details are below:");
\r
498 e.printStackTrace();
\r
499 } catch (InterruptedException ignored) {
\r
500 // ignore and propagate an interruption
\r
501 Thread.currentThread().interrupt();
\r
502 } catch (WrongParameterException e) {
\r
503 e.printStackTrace();
\r
505 if (instream != null) {
\r
508 } catch (IOException ignored) {
\r
517 * Prints Jws2Client usage information to standard out
\r
519 * @param exitStatus
\r
521 static void printUsage(int exitStatus) {
\r
522 System.out.println(Constraints.help_text);
\r
523 System.exit(exitStatus);
\r
527 * Starts command line client, if no parameter are supported print help. Two
\r
528 * parameters are required for successful call the JWS2 host name and a
\r
532 * Usage: <Class or Jar file name> -h=host_and_context
\r
533 * -s=serviceName ACTION [OPTIONS]
\r
535 * -h=<host_and_context> - a full URL to the JWS2 web server
\r
536 * including context path e.g. http://10.31.1.159:8080/ws
\r
538 * -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,
\r
539 * TcoffeeWS, ProbconsWS] ACTIONS:
\r
541 * -i=<inputFile> - full path to fasta formatted sequence file,
\r
542 * from which to align sequences
\r
544 * -parameters - lists parameters supported by web service
\r
546 * -presets - lists presets supported by web service
\r
548 * -limits - lists web services limits Please note that if input
\r
549 * file is specified other actions are ignored
\r
551 * OPTIONS: (only for use with -i action):
\r
553 * -r=<presetName> - name of the preset to use
\r
555 * -o=<outputFile> - full path to the file where to write an
\r
556 * alignment -f=<parameterInputFile> - the name of the file with
\r
557 * the list of parameters to use. Please note that -r and -f
\r
558 * options cannot be used together. Alignment is done with either
\r
559 * preset or a parameters from the file, but not both!
\r
562 public static void main(String[] args) {
\r
564 if (args == null) {
\r
567 if (args.length < 2) {
\r
568 System.err.println("Host and service names are required!");
\r
573 new Jws2Client(args);
\r
574 } catch (IOException e) {
\r
575 log.log(Level.SEVERE, "IOException in client! " + e.getMessage(),
\r
577 System.err.println("Cannot write output file! Stack trace: ");
\r
578 e.printStackTrace();
\r