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.FileOutputStream;
\r
29 import java.io.IOException;
\r
30 import java.io.PrintWriter;
\r
31 import java.io.Writer;
\r
32 import java.net.ConnectException;
\r
33 import java.net.MalformedURLException;
\r
34 import java.net.URL;
\r
35 import java.util.Arrays;
\r
36 import java.util.Collections;
\r
37 import java.util.List;
\r
38 import java.util.Set;
\r
39 import java.util.logging.Level;
\r
40 import java.util.logging.Logger;
\r
42 import javax.xml.namespace.QName;
\r
43 import javax.xml.ws.Service;
\r
44 import javax.xml.ws.WebServiceException;
\r
46 import compbio.data.msa.JABAService;
\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.AlignmentMetadata;
\r
53 import compbio.data.sequence.JpredAlignment;
\r
54 import compbio.data.sequence.FastaSequence;
\r
55 import compbio.data.sequence.ScoreManager;
\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.Limit;
\r
61 import compbio.metadata.Preset;
\r
62 import compbio.metadata.PresetManager;
\r
63 import compbio.metadata.ResultNotAvailableException;
\r
64 import compbio.metadata.WrongParameterException;
\r
65 import compbio.util.FileUtil;
\r
68 * A command line client for JAva Bioinformatics Analysis Web Services
\r
73 public class Jws2Client {
\r
76 * Use java.util.Logger instead of log4j logger to reduce the size of the client package
\r
78 private static final Logger log = Logger.getLogger(Jws2Client.class.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 @SuppressWarnings("unchecked")
\r
109 <T> Jws2Client(String[] cmd) throws IOException {
\r
112 String hostname = CmdHelper.getHost(cmd);
\r
113 if (hostname == null) {
\r
114 System.err.println("Host name is not provided!");
\r
115 System.out.println(Constraints.help_text);
\r
119 if (!validURL(hostname)) {
\r
120 System.err.println("Host name is not valid!");
\r
121 System.out.println(Constraints.help_text);
\r
125 boolean listServices = CmdHelper.listServices(cmd);
\r
126 if (listServices) {
\r
127 listAllServices(hostname);
\r
131 String serviceName = CmdHelper.getServiceName(cmd);
\r
132 if (serviceName == null) {
\r
133 System.err.println("Service name is no provided!");
\r
134 System.out.println(Constraints.help_text);
\r
138 Services service = Services.getService(serviceName);
\r
139 if (service == null) {
\r
140 String mess = "Service " + serviceName + " is no available! Valid values are: ";
\r
141 System.err.println(mess + Arrays.toString(Services.values()));
\r
142 System.out.println(Constraints.help_text);
\r
146 if (CmdHelper.testService(cmd)) {
\r
147 testService(hostname, service, new PrintWriter(System.out, true));
\r
151 Metadata<T> thews = (Metadata<T>) connect(hostname, service);
\r
152 Preset<T> preset = null;
\r
153 if (null != CmdHelper.getPresetName(cmd)) {
\r
154 preset = MetadataHelper.getPreset(thews, CmdHelper.getPresetName(cmd));
\r
157 List<Option<T>> customOptions = null;
\r
158 if (null != IOHelper.getFile(cmd, paramFile, true)) {
\r
159 List<String> prms = IOHelper.loadParameters(IOHelper.getFile(cmd, paramFile, true));
\r
160 customOptions = MetadataHelper.processParameters(prms, thews.getRunnerOptions());
\r
163 if (null != IOHelper.getFile(cmd, inputkey, true)) {
\r
164 File infile = IOHelper.getFile(cmd, inputkey, true);
\r
165 File outfile = IOHelper.getFile(cmd, outputkey, false);
\r
166 Writer writer = null;
\r
167 if (outfile != null) {
\r
168 writer = IOHelper.getWriter(outfile);
\r
170 writer = new PrintWriter(System.out, true);
\r
172 if (service.getServiceType() == SequenceAnnotation.class) {
\r
173 ScoreManager result = analize(infile, ((SequenceAnnotation<T>) thews), preset, customOptions);
\r
174 IOHelper.writeOut(writer, result);
\r
176 } else if (service.getServiceType() == MsaWS.class) {
\r
177 Alignment alignment = align(infile, (MsaWS<T>) thews, preset, customOptions);
\r
178 if (serviceName.equalsIgnoreCase("JpredWS")) {
\r
180 JpredAlignment jpred = (JpredAlignment)alignment;
\r
181 if (outfile != null) {
\r
182 FileOutputStream fout = new FileOutputStream(outfile);
\r
183 SequenceUtil.writeFastaKeepTheStream(fout, jpred.getJpredSequences(), 60);
\r
186 SequenceUtil.writeFasta(System.out, jpred.getJpredSequences());
\r
189 IOHelper.writeOut(writer, alignment);
\r
192 AlignmentMetadata md = alignment.getMetadata();
\r
193 System.out.println("Output has been prepared with " + md.getProgram());
\r
197 if (CmdHelper.listParameters(cmd)) {
\r
198 List<Option<T>> opts = MetadataHelper.getParametersList(thews, hostname);
\r
199 for (Option<T> o : opts) {
\r
200 System.out.println("##############################################################################\n" + o.toString());
\r
203 if (CmdHelper.listPresets(cmd)) {
\r
204 PresetManager<T> psm = MetadataHelper.getPresetList(thews);
\r
206 System.out.print(psm);
\r
208 System.out.println("No presets are defined for the Web service");
\r
211 if (CmdHelper.listLimits(cmd)) {
\r
212 List<Limit<T>> lims = MetadataHelper.getLimits(thews);
\r
213 for (Limit<T> l : lims) {
\r
214 System.out.println("##############################################################################\n" + l.toString());
\r
217 log.fine("Disconnecting...");
\r
218 ((Closeable) thews).close();
\r
219 log.fine("Disconnected successfully!");
\r
223 * Connects to a web service by the host and the service name web service type
\r
226 * the fully qualified name of JABAWS server including JABAWS
\r
228 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
230 * the name of the JABAWS service to connect to
\r
231 * @return JABAService<T>
\r
232 * @throws WebServiceException
\r
233 * @throws ConnectException
\r
234 * if fails to connect to the service on the host
\r
236 public static JABAService connect(String host, Services service)
\r
237 throws WebServiceException, ConnectException {
\r
239 log.log(Level.FINE, "Attempting to connect with " + service.toString() + "...");
\r
240 System.out.println ("Attempting to connect with " + service.toString() + "...");
\r
242 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
243 } catch (MalformedURLException e) {
\r
244 e.printStackTrace();
\r
245 // ignore as the host name is already verified
\r
247 Service serv = null;
\r
249 serv = service.getService(url, service.getServiceNamespace());
\r
250 } catch (WebServiceException wse) {
\r
251 wse.printStackTrace();
\r
253 if (serv == null) {
\r
254 throw new ConnectException("Could not connect to " + url + ". Is the server down?");
\r
256 JABAService serviceIF = service.getInterface(serv);
\r
257 log.log(Level.INFO, "Connected successfully!");
\r
263 * Get a connection of JABAWS registry
\r
266 * the fully qualified name of JABAWS server including JABAWS
\r
268 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
269 * @return compbio.data.msa.RegistryWS - instance of a RegistryWS web
\r
271 * @throws WebServiceException
\r
272 * @throws ConnectException
\r
274 public static compbio.data.msa.RegistryWS connectToRegistry(String host)
\r
275 throws WebServiceException, ConnectException {
\r
277 String service = "RegistryWS";
\r
278 log.log(Level.FINE, "Attempting to connect...");
\r
281 url = new URL(host + "/" + service + "?wsdl");
\r
282 } catch (MalformedURLException e) {
\r
283 e.printStackTrace();
\r
284 // ignore as the host name is already verified
\r
286 QName qname = new QName(JABAService.V2_SERVICE_NAMESPACE, service);
\r
287 Service serv = Service.create(url, qname);
\r
289 if (serv == null) {
\r
290 throw new ConnectException("Could not connect to " + url + ". Is the server down?");
\r
293 QName portName = new QName(serv.getServiceName().getNamespaceURI(), service + "Port");
\r
294 compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName, compbio.data.msa.RegistryWS.class);
\r
295 log.log(Level.INFO, "Connected to " + service + " successfully!");
\r
301 * Asks registry to test the service on the host hostname
\r
306 * @throws ConnectException
\r
307 * @throws WebServiceException
\r
309 public static void testService(String hostname, Services service, PrintWriter writer)
\r
310 throws ConnectException, WebServiceException {
\r
311 RegistryWS registry = connectToRegistry(hostname);
\r
312 if (registry != null) {
\r
313 String message = registry.testService(service);
\r
314 writer.println("Service " + service + " testing results: ");
\r
315 writer.println(message);
\r
316 FileUtil.closeSilently(((Closeable) registry));
\r
321 private static void listAllServices(String hostname) throws WebServiceException, IOException {
\r
322 RegistryWS registry = connectToRegistry(hostname);
\r
323 Set<Services> func_services = Collections.emptySet();
\r
324 Set<Services> nonfunc_services = Collections.emptySet();
\r
325 if (registry != null) {
\r
326 func_services = registry.getSupportedServices();
\r
327 nonfunc_services = registry.getNonoperatedServices();
\r
328 FileUtil.closeSilently(((Closeable) registry));
\r
330 System.out.println("Failed to connect to the registry! ");
\r
333 if (!func_services.isEmpty()) {
\r
334 System.out.println("There are " + func_services.size() + " services at " + hostname + ":");
\r
335 String mess = "\n\rThe list:\n";
\r
336 System.out.println(mess + Services.toString(func_services));
\r
338 if (!nonfunc_services.isEmpty()) {
\r
339 System.out.println("There are " + nonfunc_services.size() + " non-available services at " + hostname + ":");
\r
340 String mess = "The list (internal tests failed): ";
\r
341 System.out.println(mess + Services.toString(nonfunc_services));
\r
346 * Calculate conservation for sequences loaded from a FASTA record list structure
\r
349 * the list of FASTA records
\r
351 * a web service proxy
\r
353 * Preset to use optional
\r
354 * @param customOptions
\r
355 * the list of options
\r
356 * @return Set<Score> the conservation scores
\r
357 * @throws UnknownFileFormatException
\r
359 static <T> ScoreManager analize(List<FastaSequence> fastalist, SequenceAnnotation<T> wsproxy, Preset<T> preset, List<Option<T>> customOptions) {
\r
360 ScoreManager scores = null;
\r
362 String jobId = null;
\r
363 if (customOptions != null && preset != null) {
\r
364 System.out.println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
366 if (customOptions != null) {
\r
367 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
368 } else if (preset != null) {
\r
369 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
371 jobId = wsproxy.analize(fastalist);
\r
373 System.out.println("\n\rcalling predictor.........");
\r
375 scores = wsproxy.getAnnotation(jobId);
\r
376 } catch (JobSubmissionException e) {
\r
377 System.err.println("Exception while submitting job to a web server. Exception details are below:");
\r
378 e.printStackTrace();
\r
379 } catch (ResultNotAvailableException e) {
\r
380 System.err.println("Exception while waiting for results. Exception details are below:");
\r
381 e.printStackTrace();
\r
382 } catch (InterruptedException e) {
\r
383 Thread.currentThread().interrupt();
\r
384 System.err.println("Exception while waiting for results. Exception details are below:");
\r
385 e.printStackTrace();
\r
386 } catch (WrongParameterException e) {
\r
387 String mess = "Parsing the web method input parameters failed Exception details are below:";
\r
388 System.err.println(mess);
\r
389 e.printStackTrace();
\r
396 * Calculate conservation for sequences loaded from the file
\r
399 * a web service proxy
\r
401 * the file to read the results from
\r
403 * Preset to use optional
\r
404 * @param customOptions
\r
405 * the list of options
\r
406 * @return Set<Score> the conservation scores
\r
407 * @throws IOException
\r
408 * @throws UnknownFileFormatException
\r
410 static <T> ScoreManager analize(File file, SequenceAnnotation<T> wsproxy, Preset<T> preset, List<Option<T>> customOptions) {
\r
411 List<FastaSequence> fastalist = null;
\r
413 fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());
\r
414 assert !fastalist.isEmpty() : "Input is empty!";
\r
415 } catch (IOException e) {
\r
416 String mess = "Reading the input file failed. Check that the file contains a list of FASTA records!\n";
\r
417 System.err.println(mess + "Exception details are below:");
\r
418 e.printStackTrace();
\r
419 } catch (UnknownFileFormatException e) {
\r
420 String mess = "Reading the input file failed. Exception details are below:";
\r
421 System.err.println(mess);
\r
422 System.out.println(e.getMessage());
\r
423 e.printStackTrace();
\r
425 return analize(fastalist, wsproxy, preset, customOptions);
\r
429 * Align sequences from the file using MsaWS
\r
432 * web service type e.g. Clustal
\r
434 * to write the resulting alignment to
\r
438 * Preset to use optional
\r
439 * @param customOptions
\r
440 * file which contains new line separated list of options
\r
441 * @return Alignment
\r
443 static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,
\r
444 List<Option<T>> customOptions) {
\r
445 FileInputStream instream = null;
\r
446 Alignment alignment = null;
\r
448 instream = new FileInputStream(file);
\r
449 List<FastaSequence> fastalist = SequenceUtil.readFasta(instream);
\r
451 String jobId = null;
\r
452 if (customOptions != null && preset != null) {
\r
453 System.out.println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
455 if (customOptions != null) {
\r
456 jobId = msaws.customAlign(fastalist, customOptions);
\r
457 } else if (preset != null) {
\r
458 jobId = msaws.presetAlign(fastalist, preset);
\r
460 jobId = msaws.align(fastalist);
\r
462 System.out.println("\ncalling program.........");
\r
464 alignment = msaws.getResult(jobId);
\r
465 } catch (IOException e) {
\r
466 System.err.println("Exception while reading the input file. Check that the input file is a FASTA file! "
\r
467 + "Exception details are below:");
\r
468 e.printStackTrace();
\r
469 } catch (JobSubmissionException e) {
\r
470 System.err.println("Exception while submitting job to a web server. Exception details are below:");
\r
471 e.printStackTrace();
\r
472 } catch (ResultNotAvailableException e) {
\r
473 System.err.println("Exception while waiting for results. Exception details are below:");
\r
474 e.printStackTrace();
\r
475 } catch (InterruptedException ignored) {
\r
476 // ignore and propagate an interruption
\r
477 Thread.currentThread().interrupt();
\r
478 } catch (WrongParameterException e) {
\r
479 e.printStackTrace();
\r
481 if (instream != null) {
\r
484 } catch (IOException ignored) {
\r
493 * Starts command line client, if no parameter are supported print help. Two
\r
494 * parameters are required for successful call the JWS2 host name and a
\r
498 * Usage: <Class or Jar file name> -h=host_and_context
\r
499 * -s=serviceName ACTION [OPTIONS]
\r
501 * -h=<host_and_context> - a full URL to the JWS2 web server
\r
502 * including context path e.g. http://10.31.1.159:8080/ws
\r
504 * -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,
\r
505 * TcoffeeWS, ProbconsWS] ACTIONS:
\r
507 * -i=<inputFile> - full path to fasta formatted sequence file,
\r
508 * from which to align sequences
\r
510 * -parameters - lists parameters supported by web service
\r
512 * -presets - lists presets supported by web service
\r
514 * -limits - lists web services limits Please note that if input
\r
515 * file is specified other actions are ignored
\r
517 * OPTIONS: (only for use with -i action):
\r
519 * -r=<presetName> - name of the preset to use
\r
521 * -o=<outputFile> - full path to the file where to write an
\r
522 * alignment -f=<parameterInputFile> - the name of the file with
\r
523 * the list of parameters to use. Please note that -r and -f
\r
524 * options cannot be used together. Alignment is done with either
\r
525 * preset or a parameters from the file, but not both!
\r
528 public static void main(String[] args) {
\r
529 if (args == null) {
\r
530 System.out.println(Constraints.help_text);
\r
533 if (args.length < 2) {
\r
534 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
535 System.out.println(Constraints.help_text);
\r
539 new Jws2Client(args);
\r
540 } catch (IOException e) {
\r
541 log.log(Level.SEVERE, "IOException in client! " + e.getMessage(), e.getCause());
\r
542 System.err.println("Cannot write output file! Stack trace: ");
\r
543 e.printStackTrace();
\r