1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.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 java.io.BufferedReader;
\r
22 import java.io.Closeable;
\r
23 import java.io.File;
\r
24 import java.io.FileInputStream;
\r
25 import java.io.FileNotFoundException;
\r
26 import java.io.FileOutputStream;
\r
27 import java.io.FileReader;
\r
28 import java.io.IOException;
\r
29 import java.io.OutputStream;
\r
30 import java.net.MalformedURLException;
\r
31 import java.net.URL;
\r
32 import java.util.ArrayList;
\r
33 import java.util.Arrays;
\r
34 import java.util.List;
\r
35 import java.util.logging.Level;
\r
36 import java.util.logging.Logger;
\r
38 import javax.xml.namespace.QName;
\r
39 import javax.xml.ws.Service;
\r
40 import javax.xml.ws.WebServiceException;
\r
42 import compbio.data.msa.MsaWS;
\r
43 import compbio.data.sequence.Alignment;
\r
44 import compbio.data.sequence.ClustalAlignmentUtil;
\r
45 import compbio.data.sequence.FastaSequence;
\r
46 import compbio.data.sequence.SequenceUtil;
\r
47 import compbio.metadata.JobSubmissionException;
\r
48 import compbio.metadata.Limit;
\r
49 import compbio.metadata.LimitsManager;
\r
50 import compbio.metadata.Option;
\r
51 import compbio.metadata.Preset;
\r
52 import compbio.metadata.PresetManager;
\r
53 import compbio.metadata.ResultNotAvailableException;
\r
54 import compbio.metadata.RunnerConfig;
\r
55 import compbio.metadata.WrongParameterException;
\r
58 * A command line client for JAva Bioinformatics Analysis Web Services
\r
63 public class Jws2Client {
\r
66 * Use java.util.Logger instead of log4j logger to reduce the size of the
\r
69 private static final Logger log = Logger.getLogger(Jws2Client.class
\r
70 .getCanonicalName());
\r
72 final static String pseparator = "=";
\r
74 // Parameters for required command line options
\r
75 final static String hostkey = "-h";
\r
76 final static String servicekey = "-s";
\r
79 final static String inputkey = "-i";
\r
81 final static String paramList = "-parameters";
\r
82 final static String presetList = "-presets";
\r
83 final static String limitList = "-limits";
\r
86 final static String paramFile = "-f";
\r
87 final static String outputkey = "-o";
\r
88 final static String parameterkey = "-p";
\r
89 final static String presetkey = "-r";
\r
91 //JABAWS version 1.0 service name
\r
92 static final String qualifiedServiceName = "http://msa.data.compbio/01/01/2010/";
\r
95 * Searches the command line keys in the array of parameters
\r
98 * command line options
\r
99 * @return true is the list of Parameters is requested, false otherwise
\r
101 private boolean listParameters(String[] cmd) {
\r
102 return keyFound(cmd, paramList);
\r
106 * Check whether presetList is set in the command line
\r
109 * command line options
\r
110 * @return true if presetList is found, false otherwise
\r
112 boolean listPresets(String[] cmd) {
\r
113 return keyFound(cmd, presetList);
\r
117 * Returns {@code Preset} by its name
\r
122 * @param presetName
\r
123 * @return Return a Preset by its optionName
\r
125 <T> Preset<T> getPreset(MsaWS<T> msaws, String presetName) {
\r
126 assert presetName != null;
\r
127 PresetManager<T> presets = getPresetList(msaws);
\r
128 if (presets == null) {
\r
130 .println("No presets are supported by the service! Ignoring -r directive!");
\r
133 Preset<T> pre = presets.getPresetByName(presetName);
\r
135 System.out.println("Cannot find preset: " + presetName
\r
136 + " WARN: ignoring -r directive!");
\r
142 * Extracts preset name from the command line is any
\r
145 * command line options
\r
146 * @return presetName or null if no presets is defined
\r
148 String getPresetName(String[] cmd) {
\r
149 String preset = null;
\r
150 for (int i = 0; i < cmd.length; i++) {
\r
151 String presetPrm = cmd[i];
\r
152 if (presetPrm.trim().toLowerCase().startsWith(
\r
153 presetkey + pseparator)) {
\r
154 preset = presetPrm.substring(presetPrm.indexOf(pseparator) + 1);
\r
162 * Checks whether limitList parameter is in the command line
\r
165 * - command line options
\r
166 * @return true if it is, false otherwise
\r
168 boolean listLimits(String[] cmd) {
\r
169 return keyFound(cmd, limitList);
\r
173 * Checks whether the key is in the command line
\r
177 * @return true if it is, false otherwise
\r
179 boolean keyFound(String[] cmd, String key) {
\r
180 assert cmd != null && cmd.length > 0;
\r
181 assert key != null;
\r
182 for (int i = 0; i < cmd.length; i++) {
\r
183 String listPresets = cmd[i];
\r
184 if (listPresets.trim().equalsIgnoreCase(key)) {
\r
192 * Attempt to construct the URL object from the string
\r
195 * @return true if it succeed false otherwise
\r
197 public static boolean validURL(String urlstr) {
\r
199 if (urlstr == null || urlstr.trim().length() == 0) {
\r
203 } catch (MalformedURLException e) {
\r
210 * Extracts service name from the command line
\r
213 * command line options
\r
214 * @return service name or null if it is not defined
\r
216 public static String getServiceName(String[] cmd) {
\r
217 for (int i = 0; i < cmd.length; i++) {
\r
218 String serv = cmd[i];
\r
219 if (serv.trim().toLowerCase().startsWith(servicekey + pseparator)) {
\r
220 return serv.substring(serv.indexOf(pseparator) + 1);
\r
227 * Extracts host name from the command line
\r
230 * command line options
\r
231 * @return host name or null if it is not defined
\r
233 public static String getHost(String[] cmd) {
\r
234 for (int i = 0; i < cmd.length; i++) {
\r
235 String host = cmd[i];
\r
236 if (host.trim().toLowerCase().startsWith(hostkey + pseparator)) {
\r
237 return host.substring(host.indexOf(pseparator) + 1);
\r
244 * Checks -i options and return the File if one was provided, null otherwise
\r
250 * @throws IOException
\r
252 File getFile(String[] cmd, String key, boolean mustExist)
\r
253 throws IOException {
\r
254 assert key != null && key.trim().length() != 0;
\r
255 for (int i = 0; i < cmd.length; i++) {
\r
256 String filename = cmd[i];
\r
257 filename = filename.trim();
\r
258 if (filename.toLowerCase().startsWith(key + pseparator)) {
\r
259 filename = filename.substring((key + pseparator).length());
\r
260 File file = new File(filename);
\r
261 if (mustExist && !file.exists()) {
\r
262 System.out.println(key + " file " + file.getAbsolutePath()
\r
263 + " does not exist");
\r
266 if (!mustExist && !file.exists()) {
\r
267 file.createNewFile();
\r
269 if (!file.canRead()) {
\r
270 System.out.println("Cannot read " + key + " file "
\r
271 + file.getAbsolutePath());
\r
281 * Connects to the service and do the job as requested, if something goes
\r
282 * wrong reports or/and prints usage help.
\r
287 * command line options
\r
288 * @throws IOException
\r
290 <T> Jws2Client(String[] cmd) throws IOException {
\r
292 String hostname = getHost(cmd);
\r
293 if (hostname == null) {
\r
294 System.out.println("Host name is not provided!");
\r
298 if (!validURL(hostname)) {
\r
299 System.out.println("Host name is not valid!");
\r
302 String serviceName = getServiceName(cmd);
\r
303 if (serviceName == null) {
\r
304 System.out.println("Service name is no provided!");
\r
307 Services service = Services.getService(serviceName);
\r
308 if (service == null) {
\r
309 System.out.println("Service " + serviceName
\r
310 + " is no supported! Valid values are: "
\r
311 + Arrays.toString(Services.values()));
\r
314 File inputFile = getFile(cmd, inputkey, true);
\r
315 File outFile = getFile(cmd, outputkey, false);
\r
316 File parametersFile = getFile(cmd, paramFile, true);
\r
317 String presetName = getPresetName(cmd);
\r
319 MsaWS<T> msaws = connect(hostname, service);
\r
320 Preset<T> preset = null;
\r
321 if (presetName != null) {
\r
322 preset = getPreset(msaws, presetName);
\r
324 List<Option<T>> customOptions = null;
\r
325 if (parametersFile != null) {
\r
326 List<String> prms = loadParameters(parametersFile);
\r
327 customOptions = processParameters(prms, msaws.getRunnerOptions());
\r
329 Alignment alignment = null;
\r
330 if (inputFile != null) {
\r
331 alignment = align(inputFile, msaws, preset, customOptions);
\r
332 OutputStream outStream = null;
\r
333 if (outFile != null) {
\r
334 outStream = getOutStream(outFile);
\r
336 // this stream is going to be closed later which is fine as
\r
337 // std.out will not be
\r
338 outStream = System.out;
\r
340 writeOut(outStream, alignment);
\r
341 // stream is closed in the method no need to close it here
\r
344 boolean listParameters = listParameters(cmd);
\r
345 if (listParameters) {
\r
346 System.out.println(getParametersList(msaws));
\r
348 boolean listPreset = listPresets(cmd);
\r
350 System.out.println(getPresetList(msaws));
\r
352 boolean listLimits = listLimits(cmd);
\r
354 System.out.println(getLimits(msaws));
\r
356 log.fine("Disconnecting...");
\r
357 ((Closeable) msaws).close();
\r
358 log.fine("Disconnected successfully!");
\r
362 * Load parameters from file
\r
364 * @throws IOException
\r
366 List<String> loadParameters(File paramsfile) throws IOException {
\r
367 assert paramsfile != null && paramsfile.exists();
\r
368 BufferedReader reader = new BufferedReader(new FileReader(paramsfile));
\r
369 String line = null;
\r
370 ArrayList<String> params = new ArrayList<String>();
\r
371 while ((line = reader.readLine()) != null) {
\r
372 line = line.trim();
\r
373 if (line.length() == 0)
\r
381 * Converts options supplied via parameters file into {@code Option} objects
\r
387 * @return List of Options of type T
\r
389 <T> List<Option<T>> processParameters(List<String> params,
\r
390 RunnerConfig<T> options) {
\r
391 List<Option<T>> chosenOptions = new ArrayList<Option<T>>();
\r
392 for (String param : params) {
\r
393 String oname = null;
\r
394 if (isParameter(param)) {
\r
395 oname = this.getParamName(param);
\r
399 Option<T> o = options.getArgumentByOptionName(oname);
\r
401 System.out.println("WARN ignoring unsuppoted parameter: "
\r
405 if (isParameter(param)) {
\r
407 o.setValue(getParamValue(param));
\r
408 } catch (WrongParameterException e) {
\r
410 .println("Problem setting value for the parameter: "
\r
412 e.printStackTrace();
\r
415 chosenOptions.add(o);
\r
417 return chosenOptions;
\r
420 String getParamName(String fullName) {
\r
421 assert isParameter(fullName);
\r
422 return fullName.substring(0, fullName.indexOf(pseparator));
\r
425 String getParamValue(String fullName) {
\r
426 assert isParameter(fullName);
\r
427 return fullName.substring(fullName.indexOf(pseparator) + 1);
\r
430 boolean isParameter(String param) {
\r
431 return param.contains(pseparator);
\r
434 OutputStream getOutStream(File file) {
\r
435 assert file != null && file.exists();
\r
437 return new FileOutputStream(file);
\r
438 } catch (FileNotFoundException e) {
\r
439 e.printStackTrace();
\r
445 * Outputs clustal formatted alignment into the file represented by the
\r
450 * the alignment to output
\r
452 void writeOut(OutputStream outStream, Alignment align) {
\r
454 ClustalAlignmentUtil.writeClustalAlignment(outStream, align);
\r
455 } catch (IOException e) {
\r
457 .println("Problems writing output file! Stack trace is below: ");
\r
458 e.printStackTrace();
\r
460 if (outStream != null) {
\r
463 } catch (IOException ignored) {
\r
464 // e.printStackTrace();
\r
471 * Connects to a web service by the host and the service name
\r
478 * @throws WebServiceException
\r
480 public static <T> MsaWS<T> connect(String host, Services service)
\r
481 throws WebServiceException {
\r
483 log.log(Level.FINE, "Attempt to connect...");
\r
485 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
486 } catch (MalformedURLException e) {
\r
487 e.printStackTrace();
\r
488 // ignore as the host name is already verified
\r
490 QName qname = new QName(qualifiedServiceName, service.toString());
\r
491 Service serv = Service.create(url, qname);
\r
492 MsaWS<T> msaws = serv.getPort(new QName(qualifiedServiceName, service
\r
493 + "Port"), MsaWS.class);
\r
494 log.log(Level.FINE, "Connected successfully!");
\r
499 * Align sequences from the file using MsaWS
\r
502 * web service type e.g. Clustal
\r
504 * to write the resulting alignment to
\r
508 * Preset to use optional
\r
509 * @param customOptions
\r
510 * file which contains new line separated list of options
\r
511 * @return Alignment
\r
513 static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,
\r
514 List<Option<T>> customOptions) {
\r
515 FileInputStream instream = null;
\r
516 List<FastaSequence> fastalist = null;
\r
517 Alignment alignment = null;
\r
519 instream = new FileInputStream(file);
\r
520 fastalist = SequenceUtil.readFasta(instream);
\r
522 String jobId = null;
\r
523 if (customOptions != null && preset != null) {
\r
525 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
527 if (customOptions != null) {
\r
528 jobId = msaws.customAlign(fastalist, customOptions);
\r
529 } else if (preset != null) {
\r
530 jobId = msaws.presetAlign(fastalist, preset);
\r
532 jobId = msaws.align(fastalist);
\r
534 Thread.sleep(1000);
\r
535 alignment = msaws.getResult(jobId);
\r
537 } catch (IOException e) {
\r
539 .println("Exception while reading the input file. "
\r
540 + "Check that the input file contains a list of fasta formatted sequences! "
\r
541 + "Exception details are below:");
\r
542 e.printStackTrace();
\r
543 } catch (JobSubmissionException e) {
\r
545 .println("Exception while submitting job to a web server. "
\r
546 + "Exception details are below:");
\r
547 e.printStackTrace();
\r
548 } catch (ResultNotAvailableException e) {
\r
549 System.err.println("Exception while waiting for results. "
\r
550 + "Exception details are below:");
\r
551 e.printStackTrace();
\r
552 } catch (InterruptedException ignored) {
\r
553 // ignore and propagate an interruption
\r
554 Thread.currentThread().interrupt();
\r
555 } catch (WrongParameterException e) {
\r
556 e.printStackTrace();
\r
558 if (instream != null) {
\r
561 } catch (IOException ignored) {
\r
570 * Returns a list of options supported by web service
\r
575 * web service proxy
\r
576 * @return List of options supported by a web service
\r
578 <T> List<Option<T>> getParametersList(MsaWS<T> msaws) {
\r
579 assert msaws != null;
\r
580 return msaws.getRunnerOptions().getArguments();
\r
584 * Returns an objects from which the list of presets supported by web
\r
585 * service <T> can be obtained
\r
590 * web service proxy
\r
591 * @return PresetManager, object which operates on presets
\r
593 <T> PresetManager<T> getPresetList(MsaWS<T> msaws) {
\r
594 assert msaws != null;
\r
595 PresetManager<T> presetman = msaws.getPresets();
\r
600 * Returns a list of limits supported by web service Each limit correspond
\r
601 * to a particular preset.
\r
606 * web service proxy
\r
607 * @return List of limits supported by a web service
\r
609 <T> List<Limit<T>> getLimits(MsaWS<T> msaws) {
\r
610 assert msaws != null;
\r
611 LimitsManager<T> lmanger = msaws.getLimits();
\r
613 return lmanger != null ? lmanger.getLimits() : null;
\r
617 * Prints Jws2Client usage information to standard out
\r
619 * @param exitStatus
\r
621 static void printUsage(int exitStatus) {
\r
622 System.out.println();
\r
623 System.out.println("Usage: <Class or Jar file name> " + hostkey
\r
624 + pseparator + "host_and_context " + servicekey + pseparator
\r
625 + "serviceName ACTION [OPTIONS] ");
\r
626 System.out.println();
\r
630 + "<host_and_context> - a full URL to the JWS2 web server including context path e.g. http://10.31.1.159:8080/ws");
\r
631 System.out.println(servicekey + pseparator + "<ServiceName> - one of "
\r
632 + Arrays.toString(Services.values()));
\r
633 System.out.println();
\r
634 System.out.println("ACTIONS: ");
\r
638 + "<inputFile> - full path to fasta formatted sequence file, from which to align sequences");
\r
639 System.out.println(paramList
\r
640 + " - lists parameters supported by web service");
\r
641 System.out.println(presetList
\r
642 + " - lists presets supported by web service");
\r
643 System.out.println(limitList + " - lists web services limits");
\r
645 .println("Please note that if input file is specified other actions are ignored");
\r
647 System.out.println();
\r
648 System.out.println("OPTIONS (only for use with -i action):");
\r
650 System.out.println(presetkey + pseparator
\r
651 + "<presetName> - name of the preset to use");
\r
655 + "<outputFile> - full path to the file where to write an alignment");
\r
657 .println("-f=<parameterInputFile> - the name of the file with the list of parameters to use.");
\r
659 .println("Please note that -r and -f options cannot be used together. "
\r
660 + "Alignment is done with either preset or a parameters from the file, but not both!");
\r
662 System.exit(exitStatus);
\r
666 * Starts command line client, if no parameter are supported print help. Two
\r
667 * parameters are required for successfull call the JWS2 host name and a
\r
671 * Usage: <Class or Jar file name> -h=host_and_context
\r
672 * -s=serviceName ACTION [OPTIONS]
\r
674 * -h=<host_and_context> - a full URL to the JWS2 web server
\r
675 * including context path e.g. http://10.31.1.159:8080/ws
\r
677 * -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,
\r
678 * TcoffeeWS, ProbconsWS] ACTIONS:
\r
680 * -i=<inputFile> - full path to fasta formatted sequence file,
\r
681 * from which to align sequences
\r
683 * -parameters - lists parameters supported by web service
\r
685 * -presets - lists presets supported by web service
\r
687 * -limits - lists web services limits Please note that if input
\r
688 * file is specified other actions are ignored
\r
690 * OPTIONS: (only for use with -i action):
\r
692 * -r=<presetName> - name of the preset to use
\r
694 * -o=<outputFile> - full path to the file where to write an
\r
695 * alignment -f=<parameterInputFile> - the name of the file with
\r
696 * the list of parameters to use. Please note that -r and -f
\r
697 * options cannot be used together. Alignment is done with either
\r
698 * preset or a parameters from the file, but not both!
\r
701 public static void main(String[] args) {
\r
703 if (args == null) {
\r
706 if (args.length < 2) {
\r
707 System.out.println("Host and service names are required!");
\r
712 new Jws2Client(args);
\r
713 } catch (IOException e) {
\r
714 log.log(Level.SEVERE, "IOException in client! " + e.getMessage(), e
\r
716 System.err.println("Cannot write output file! Stack trace: ");
\r
717 e.printStackTrace();
\r