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 static compbio.ws.client.Constraints.hostkey;
\r
22 import static compbio.ws.client.Constraints.inputkey;
\r
23 import static compbio.ws.client.Constraints.limitList;
\r
24 import static compbio.ws.client.Constraints.outputkey;
\r
25 import static compbio.ws.client.Constraints.paramFile;
\r
26 import static compbio.ws.client.Constraints.paramList;
\r
27 import static compbio.ws.client.Constraints.presetList;
\r
28 import static compbio.ws.client.Constraints.presetkey;
\r
29 import static compbio.ws.client.Constraints.pseparator;
\r
30 import static compbio.ws.client.Constraints.servicekey;
\r
32 import java.io.Closeable;
\r
33 import java.io.File;
\r
34 import java.io.FileInputStream;
\r
35 import java.io.IOException;
\r
36 import java.io.OutputStream;
\r
37 import java.net.MalformedURLException;
\r
38 import java.net.URL;
\r
39 import java.util.Arrays;
\r
40 import java.util.HashMap;
\r
41 import java.util.HashSet;
\r
42 import java.util.List;
\r
43 import java.util.Map;
\r
44 import java.util.logging.Level;
\r
45 import java.util.logging.Logger;
\r
47 import javax.xml.ws.Service;
\r
48 import javax.xml.ws.WebServiceException;
\r
50 import compbio.data.msa.JABAService;
\r
51 import compbio.data.msa.Metadata;
\r
52 import compbio.data.msa.MsaWS;
\r
53 import compbio.data.msa.SequenceAnnotation;
\r
54 import compbio.data.sequence.Alignment;
\r
55 import compbio.data.sequence.FastaSequence;
\r
56 import compbio.data.sequence.Score;
\r
57 import compbio.data.sequence.SequenceUtil;
\r
58 import compbio.data.sequence.UnknownFileFormatException;
\r
59 import compbio.metadata.JobSubmissionException;
\r
60 import compbio.metadata.Option;
\r
61 import compbio.metadata.Preset;
\r
62 import compbio.metadata.ResultNotAvailableException;
\r
63 import compbio.metadata.WrongParameterException;
\r
66 * A command line client for JAva Bioinformatics Analysis Web Services
\r
71 public class Jws2Client {
\r
74 * Use java.util.Logger instead of log4j logger to reduce the size of the
\r
77 private static final Logger log = Logger.getLogger(Jws2Client.class
\r
78 .getCanonicalName());
\r
80 // JABAWS version 1.0 service name
\r
81 static final String QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/01/2010/";
\r
83 // JABAWS version 2.0 service name
\r
84 static final String V2_QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/12/2010/";
\r
87 * Attempt to construct the URL object from the string
\r
90 * @return true if it succeed false otherwise
\r
92 public static boolean validURL(String urlstr) {
\r
94 if (urlstr == null || urlstr.trim().length() == 0) {
\r
98 } catch (MalformedURLException e) {
\r
105 * Connects to the service and do the job as requested, if something goes
\r
106 * wrong reports or/and prints usage help.
\r
111 * command line options
\r
112 * @throws IOException
\r
114 <T> Jws2Client(String[] cmd) throws IOException {
\r
116 String hostname = CmdHelper.getHost(cmd);
\r
117 if (hostname == null) {
\r
118 System.out.println("Host name is not provided!");
\r
122 if (!validURL(hostname)) {
\r
123 System.out.println("Host name is not valid!");
\r
126 String serviceName = CmdHelper.getServiceName(cmd);
\r
127 if (serviceName == null) {
\r
128 System.out.println("Service name is no provided!");
\r
131 Services service = Services.getService(serviceName);
\r
132 if (service == null) {
\r
133 System.out.println("Service " + serviceName
\r
134 + " is no supported! Valid values are: "
\r
135 + Arrays.toString(Services.values()));
\r
138 File inputFile = IOHelper.getFile(cmd, inputkey, true);
\r
139 File outFile = IOHelper.getFile(cmd, outputkey, false);
\r
140 File parametersFile = IOHelper.getFile(cmd, paramFile, true);
\r
141 String presetName = CmdHelper.getPresetName(cmd);
\r
143 Metadata<T> msaws = (Metadata<T>) connect(hostname, service);
\r
144 Preset<T> preset = null;
\r
145 if (presetName != null) {
\r
146 preset = MetadataHelper.getPreset(msaws, presetName);
\r
148 List<Option<T>> customOptions = null;
\r
149 if (parametersFile != null) {
\r
150 List<String> prms = IOHelper.loadParameters(parametersFile);
\r
151 customOptions = MetadataHelper.processParameters(prms,
\r
152 msaws.getRunnerOptions());
\r
154 Alignment alignment = null;
\r
155 if (inputFile != null) {
\r
156 OutputStream outStream = null;
\r
157 if (outFile != null) {
\r
158 outStream = IOHelper.getOutStream(outFile);
\r
160 // this stream is going to be closed later which is fine as
\r
161 // std.out will not be
\r
162 outStream = System.out;
\r
164 if (service.getServiceType() == SequenceAnnotation.class) {
\r
165 Map<String, HashSet<Score>> result = analize(inputFile,
\r
166 ((SequenceAnnotation<T>) msaws), preset, customOptions);
\r
167 assert result != null && !result.values().isEmpty() : "No Result reported!";
\r
168 IOHelper.writeOut(outStream, result);
\r
170 alignment = align(inputFile, (MsaWS<T>) msaws, preset,
\r
172 IOHelper.writeOut(outStream, alignment);
\r
177 boolean listParameters = CmdHelper.listParameters(cmd);
\r
178 if (listParameters) {
\r
179 System.out.println(MetadataHelper.getParametersList(msaws));
\r
181 boolean listPreset = CmdHelper.listPresets(cmd);
\r
183 System.out.println(MetadataHelper.getPresetList(msaws));
\r
185 boolean listLimits = CmdHelper.listLimits(cmd);
\r
187 System.out.println(MetadataHelper.getLimits(msaws));
\r
189 log.fine("Disconnecting...");
\r
190 ((Closeable) msaws).close();
\r
191 log.fine("Disconnected successfully!");
\r
194 * Calculate conservation for sequences loaded from the file
\r
197 * a web service proxy
\r
199 * the file to read the results from
\r
201 * Preset to use optional
\r
202 * @param customOptions
\r
203 * the list of options
\r
204 * @return Set<Score> the conservation scores
\r
205 * @throws UnknownFileFormatException
\r
207 <T> HashMap<String, HashSet<Score>> analize(File file,
\r
208 SequenceAnnotation<T> wsproxy, Preset<T> preset,
\r
209 List<Option<T>> customOptions) {
\r
211 List<FastaSequence> fastalist = null;
\r
212 HashMap<String, HashSet<Score>> scores = null;
\r
214 fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());
\r
216 String jobId = null;
\r
217 if (customOptions != null && preset != null) {
\r
219 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
221 if (customOptions != null) {
\r
222 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
223 } else if (preset != null) {
\r
224 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
226 jobId = wsproxy.analize(fastalist);
\r
228 Thread.sleep(2000);
\r
229 scores = wsproxy.getAnnotation(jobId);
\r
230 assert scores != null && !scores.values().isEmpty() : "Scores are NULL";
\r
232 } catch (IOException e) {
\r
234 .println("Exception while reading the input file. "
\r
235 + "Check that the input file contains a list of fasta formatted sequences! "
\r
236 + "Exception details are below:");
\r
237 e.printStackTrace();
\r
238 } catch (JobSubmissionException e) {
\r
240 .println("Exception while submitting job to a web server. "
\r
241 + "Exception details are below:");
\r
242 e.printStackTrace();
\r
243 } catch (ResultNotAvailableException e) {
\r
244 System.err.println("Exception while waiting for results. "
\r
245 + "Exception details are below:");
\r
246 e.printStackTrace();
\r
247 } catch (InterruptedException ignored) {
\r
248 // ignore and propagate an interruption
\r
249 Thread.currentThread().interrupt();
\r
250 } catch (WrongParameterException e) {
\r
252 .println("Exception while parsing the web method input parameters. "
\r
253 + "Exception details are below:");
\r
254 e.printStackTrace();
\r
255 } catch (UnknownFileFormatException e) {
\r
257 .println("Exception while attempting to read the input file "
\r
258 + "Exception details are below:");
\r
259 System.out.println(e.getMessage());
\r
260 e.printStackTrace();
\r
266 * Connects to a web service by the host and the service name
\r
273 * @throws WebServiceException
\r
275 public static JABAService connect(String host, Services service)
\r
276 throws WebServiceException {
\r
278 log.log(Level.FINE, "Attempting to connect...");
\r
280 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
281 } catch (MalformedURLException e) {
\r
282 e.printStackTrace();
\r
283 // ignore as the host name is already verified
\r
285 Service serv = null;
\r
287 serv = service.getService(url, QUALIFIED_SERVICE_NAME);
\r
288 } catch (WebServiceException wse) {
\r
289 System.out.println("Conecting to JABAWS version 2 service");
\r
290 if (isV2service(wse)) {
\r
291 serv = service.getService(url, V2_QUALIFIED_SERVICE_NAME);
\r
294 if (serv == null) {
\r
295 System.err.println("Could not connect to " + url);
\r
298 JABAService serviceIF = service.getInterface(serv);
\r
299 log.log(Level.FINE, "Connected successfully!");
\r
304 static boolean isV2service(WebServiceException wse) {
\r
305 String message = wse.getMessage();
\r
306 int idx = message.indexOf("not a valid service");
\r
308 if (message.substring(idx).contains(V2_QUALIFIED_SERVICE_NAME)) {
\r
316 * Align sequences from the file using MsaWS
\r
319 * web service type e.g. Clustal
\r
321 * to write the resulting alignment to
\r
325 * Preset to use optional
\r
326 * @param customOptions
\r
327 * file which contains new line separated list of options
\r
328 * @return Alignment
\r
330 static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,
\r
331 List<Option<T>> customOptions) {
\r
332 FileInputStream instream = null;
\r
333 List<FastaSequence> fastalist = null;
\r
334 Alignment alignment = null;
\r
336 instream = new FileInputStream(file);
\r
337 fastalist = SequenceUtil.readFasta(instream);
\r
339 String jobId = null;
\r
340 if (customOptions != null && preset != null) {
\r
342 .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
344 if (customOptions != null) {
\r
345 jobId = msaws.customAlign(fastalist, customOptions);
\r
346 } else if (preset != null) {
\r
347 jobId = msaws.presetAlign(fastalist, preset);
\r
349 jobId = msaws.align(fastalist);
\r
351 Thread.sleep(1000);
\r
352 alignment = msaws.getResult(jobId);
\r
354 } catch (IOException e) {
\r
356 .println("Exception while reading the input file. "
\r
357 + "Check that the input file contains a list of fasta formatted sequences! "
\r
358 + "Exception details are below:");
\r
359 e.printStackTrace();
\r
360 } catch (JobSubmissionException e) {
\r
362 .println("Exception while submitting job to a web server. "
\r
363 + "Exception details are below:");
\r
364 e.printStackTrace();
\r
365 } catch (ResultNotAvailableException e) {
\r
366 System.err.println("Exception while waiting for results. "
\r
367 + "Exception details are below:");
\r
368 e.printStackTrace();
\r
369 } catch (InterruptedException ignored) {
\r
370 // ignore and propagate an interruption
\r
371 Thread.currentThread().interrupt();
\r
372 } catch (WrongParameterException e) {
\r
373 e.printStackTrace();
\r
375 if (instream != null) {
\r
378 } catch (IOException ignored) {
\r
387 * Prints Jws2Client usage information to standard out
\r
389 * @param exitStatus
\r
391 static void printUsage(int exitStatus) {
\r
392 System.out.println();
\r
393 System.out.println("Usage: <Class or Jar file name> " + hostkey
\r
394 + pseparator + "host_and_context " + servicekey + pseparator
\r
395 + "serviceName ACTION [OPTIONS] ");
\r
396 System.out.println();
\r
400 + "<host_and_context> - a full URL to the JWS2 web server including context path e.g. http://10.31.1.159:8080/ws");
\r
401 System.out.println(servicekey + pseparator + "<ServiceName> - one of "
\r
402 + Arrays.toString(Services.values()));
\r
403 System.out.println();
\r
404 System.out.println("ACTIONS: ");
\r
408 + "<inputFile> - full path to fasta formatted sequence file, from which to align sequences");
\r
409 System.out.println(paramList
\r
410 + " - lists parameters supported by web service");
\r
411 System.out.println(presetList
\r
412 + " - lists presets supported by web service");
\r
413 System.out.println(limitList + " - lists web services limits");
\r
415 .println("Please note that if input file is specified other actions are ignored");
\r
417 System.out.println();
\r
418 System.out.println("OPTIONS (only for use with -i action):");
\r
420 System.out.println(presetkey + pseparator
\r
421 + "<presetName> - name of the preset to use");
\r
425 + "<outputFile> - full path to the file where to write an alignment");
\r
427 .println("-f=<parameterInputFile> - the name of the file with the list of parameters to use.");
\r
429 .println("Please note that -r and -f options cannot be used together. "
\r
430 + "Alignment is done with either preset or a parameters from the file, but not both!");
\r
432 System.exit(exitStatus);
\r
436 * Starts command line client, if no parameter are supported print help. Two
\r
437 * parameters are required for successful call the JWS2 host name and a
\r
441 * Usage: <Class or Jar file name> -h=host_and_context
\r
442 * -s=serviceName ACTION [OPTIONS]
\r
444 * -h=<host_and_context> - a full URL to the JWS2 web server
\r
445 * including context path e.g. http://10.31.1.159:8080/ws
\r
447 * -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,
\r
448 * TcoffeeWS, ProbconsWS] ACTIONS:
\r
450 * -i=<inputFile> - full path to fasta formatted sequence file,
\r
451 * from which to align sequences
\r
453 * -parameters - lists parameters supported by web service
\r
455 * -presets - lists presets supported by web service
\r
457 * -limits - lists web services limits Please note that if input
\r
458 * file is specified other actions are ignored
\r
460 * OPTIONS: (only for use with -i action):
\r
462 * -r=<presetName> - name of the preset to use
\r
464 * -o=<outputFile> - full path to the file where to write an
\r
465 * alignment -f=<parameterInputFile> - the name of the file with
\r
466 * the list of parameters to use. Please note that -r and -f
\r
467 * options cannot be used together. Alignment is done with either
\r
468 * preset or a parameters from the file, but not both!
\r
471 public static void main(String[] args) {
\r
473 if (args == null) {
\r
476 if (args.length < 2) {
\r
477 System.out.println("Host and service names are required!");
\r
482 new Jws2Client(args);
\r
483 } catch (IOException e) {
\r
484 log.log(Level.SEVERE, "IOException in client! " + e.getMessage(),
\r
486 System.err.println("Cannot write output file! Stack trace: ");
\r
487 e.printStackTrace();
\r