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.JobStatus;
\r
60 import compbio.metadata.Option;
\r
61 import compbio.metadata.Limit;
\r
62 import compbio.metadata.Preset;
\r
63 import compbio.metadata.PresetManager;
\r
64 import compbio.metadata.ResultNotAvailableException;
\r
65 import compbio.metadata.WrongParameterException;
\r
66 import compbio.util.FileUtil;
\r
69 * A command line client for JAva Bioinformatics Analysis Web Services
\r
74 public class Jws2Client {
\r
77 * Use java.util.Logger instead of log4j logger to reduce the size of the client package
\r
79 private static final Logger log = Logger.getLogger(Jws2Client.class.getCanonicalName());
\r
82 * Attempt to construct the URL object from the string
\r
85 * @return true if it succeed false otherwise
\r
87 public static boolean validURL(String urlstr) {
\r
89 if (urlstr == null || urlstr.trim().length() == 0) {
\r
93 } catch (MalformedURLException e) {
\r
100 * Connects to the service and do the job as requested, if something goes
\r
101 * wrong reports or/and prints usage help.
\r
106 * command line options
\r
107 * @throws IOException
\r
109 @SuppressWarnings("unchecked")
\r
110 <T> Jws2Client(String[] cmd) throws IOException {
\r
113 String hostname = CmdHelper.getHost(cmd);
\r
114 if (hostname == null) {
\r
115 System.err.println("Host name is not provided!");
\r
116 System.out.println(Constraints.help_text);
\r
120 if (!validURL(hostname)) {
\r
121 System.err.println("Host name is not valid!");
\r
122 System.out.println(Constraints.help_text);
\r
126 boolean listServices = CmdHelper.listServices(cmd);
\r
127 if (listServices) {
\r
128 listAllServices(hostname);
\r
132 String serviceName = CmdHelper.getServiceName(cmd);
\r
133 if (serviceName == null) {
\r
134 System.err.println("Service name is no provided!");
\r
135 System.out.println(Constraints.help_text);
\r
139 Services service = Services.getService(serviceName);
\r
140 if (service == null) {
\r
141 String mess = "Service " + serviceName + " is no available! Valid values are: ";
\r
142 System.err.println(mess + Arrays.toString(Services.values()));
\r
143 System.out.println(Constraints.help_text);
\r
147 if (CmdHelper.testService(cmd)) {
\r
148 testService(hostname, service, new PrintWriter(System.out, true));
\r
152 Metadata<T> thews = (Metadata<T>) connect(hostname, service);
\r
153 Preset<T> preset = null;
\r
154 if (null != CmdHelper.getPresetName(cmd)) {
\r
155 preset = MetadataHelper.getPreset(thews, CmdHelper.getPresetName(cmd));
\r
158 List<Option<T>> customOptions = null;
\r
159 if (null != IOHelper.getFile(cmd, paramFile, true)) {
\r
160 List<String> prms = IOHelper.loadParameters(IOHelper.getFile(cmd, paramFile, true));
\r
161 customOptions = MetadataHelper.processParameters(prms, thews.getRunnerOptions());
\r
164 if (null != IOHelper.getFile(cmd, inputkey, true)) {
\r
165 File infile = IOHelper.getFile(cmd, inputkey, true);
\r
166 File outfile = IOHelper.getFile(cmd, outputkey, false);
\r
167 Writer writer = null;
\r
168 if (outfile != null) {
\r
169 writer = IOHelper.getWriter(outfile);
\r
171 writer = new PrintWriter(System.out, true);
\r
173 if (service.getServiceType() == SequenceAnnotation.class) {
\r
174 ScoreManager result = analize(infile, ((SequenceAnnotation<T>) thews), preset, customOptions);
\r
175 IOHelper.writeOut(writer, result);
\r
177 } else if (service.getServiceType() == MsaWS.class) {
\r
178 Alignment alignment = align(infile, (MsaWS<T>) thews, preset, customOptions);
\r
179 if (serviceName.equalsIgnoreCase("JpredWS")) {
\r
181 JpredAlignment jpred = (JpredAlignment)alignment;
\r
182 if (outfile != null) {
\r
183 FileOutputStream fout = new FileOutputStream(outfile);
\r
184 SequenceUtil.writeFastaKeepTheStream(fout, jpred.getJpredSequences(), 60);
\r
187 SequenceUtil.writeFasta(System.out, jpred.getJpredSequences());
\r
190 IOHelper.writeOut(writer, alignment);
\r
193 AlignmentMetadata md = alignment.getMetadata();
\r
194 System.out.println("Output has been prepared with " + md.getProgram());
\r
198 if (CmdHelper.listParameters(cmd)) {
\r
199 List<Option<T>> opts = MetadataHelper.getParametersList(thews, hostname);
\r
200 for (Option<T> o : opts) {
\r
201 System.out.println("##############################################################################\n" + o.toString());
\r
204 if (CmdHelper.listPresets(cmd)) {
\r
205 PresetManager<T> psm = MetadataHelper.getPresetList(thews);
\r
207 System.out.print(psm);
\r
209 System.out.println("No presets are defined for the Web service");
\r
212 if (CmdHelper.listLimits(cmd)) {
\r
213 List<Limit<T>> lims = MetadataHelper.getLimits(thews);
\r
214 for (Limit<T> l : lims) {
\r
215 System.out.println("##############################################################################\n" + l.toString());
\r
218 log.fine("Disconnecting...");
\r
219 ((Closeable) thews).close();
\r
220 log.fine("Disconnected successfully!");
\r
224 * Connects to a web service by the host and the service name web service type
\r
227 * the fully qualified name of JABAWS server including JABAWS
\r
229 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
231 * the name of the JABAWS service to connect to
\r
232 * @return JABAService<T>
\r
233 * @throws WebServiceException
\r
234 * @throws ConnectException
\r
235 * if fails to connect to the service on the host
\r
237 public static JABAService connect(String host, Services service)
\r
238 throws WebServiceException, ConnectException {
\r
240 log.log(Level.FINE, "Attempting to connect with " + service.toString() + "...");
\r
241 System.out.println ("Attempting to connect with " + service.toString() + "...");
\r
243 url = new URL(host + "/" + service.toString() + "?wsdl");
\r
244 } catch (MalformedURLException e) {
\r
245 e.printStackTrace();
\r
246 // ignore as the host name is already verified
\r
248 Service serv = null;
\r
250 serv = service.getService(url, service.getServiceNamespace());
\r
251 } catch (WebServiceException wse) {
\r
252 wse.printStackTrace();
\r
254 if (serv == null) {
\r
255 throw new ConnectException("Could not connect to " + url + ". Is the server down?");
\r
257 JABAService serviceIF = service.getInterface(serv);
\r
258 log.log(Level.INFO, "Connected successfully!");
\r
264 * Get a connection of JABAWS registry
\r
267 * the fully qualified name of JABAWS server including JABAWS
\r
269 * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba
\r
270 * @return compbio.data.msa.RegistryWS - instance of a RegistryWS web
\r
272 * @throws WebServiceException
\r
273 * @throws ConnectException
\r
275 public static compbio.data.msa.RegistryWS connectToRegistry(String host)
\r
276 throws WebServiceException, ConnectException {
\r
278 String service = "RegistryWS";
\r
279 log.log(Level.FINE, "Attempting to connect...");
\r
282 url = new URL(host + "/" + service + "?wsdl");
\r
283 } catch (MalformedURLException e) {
\r
284 e.printStackTrace();
\r
285 // ignore as the host name is already verified
\r
287 QName qname = new QName(JABAService.V2_SERVICE_NAMESPACE, service);
\r
288 Service serv = Service.create(url, qname);
\r
290 if (serv == null) {
\r
291 throw new ConnectException("Could not connect to " + url + ". Is the server down?");
\r
294 QName portName = new QName(serv.getServiceName().getNamespaceURI(), service + "Port");
\r
295 compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName, compbio.data.msa.RegistryWS.class);
\r
296 log.log(Level.INFO, "Connected to " + service + " successfully!");
\r
302 * Asks registry to test the service on the host hostname
\r
307 * @throws ConnectException
\r
308 * @throws WebServiceException
\r
310 public static void testService(String hostname, Services service, PrintWriter writer)
\r
311 throws ConnectException, WebServiceException {
\r
312 RegistryWS registry = connectToRegistry(hostname);
\r
313 if (registry != null) {
\r
314 String message = registry.testService(service);
\r
315 writer.println("Service " + service + " testing results: ");
\r
316 writer.println(message);
\r
317 FileUtil.closeSilently(((Closeable) registry));
\r
322 private static void listAllServices(String hostname) throws WebServiceException, IOException {
\r
323 RegistryWS registry = connectToRegistry(hostname);
\r
324 Set<Services> func_services = Collections.emptySet();
\r
325 //Set<Services> nonfunc_services = Collections.emptySet();
\r
326 if (registry != null) {
\r
327 func_services = registry.getSupportedServices();
\r
328 //nonfunc_services = registry.getNonoperatedServices();
\r
329 FileUtil.closeSilently(((Closeable) registry));
\r
331 System.out.println("Failed to connect to the registry! ");
\r
334 if (!func_services.isEmpty()) {
\r
335 System.out.println("There are " + func_services.size() + " services at " + hostname + ":");
\r
336 String mess = "\n\rThe list:\n";
\r
337 System.out.println(mess + Services.toString(func_services));
\r
340 if (!nonfunc_services.isEmpty()) {
\r
341 System.out.println("There are " + nonfunc_services.size() + " non-available services at " + hostname + ":");
\r
342 String mess = "The list (internal tests failed): ";
\r
343 System.out.println(mess + Services.toString(nonfunc_services));
\r
349 * Calculate conservation for sequences loaded from a FASTA record list structure
\r
352 * the list of FASTA records
\r
354 * a web service proxy
\r
356 * Preset to use optional
\r
357 * @param customOptions
\r
358 * the list of options
\r
359 * @return Set<Score> the conservation scores
\r
360 * @throws UnknownFileFormatException
\r
362 static <T> ScoreManager analize(List<FastaSequence> fastalist, SequenceAnnotation<T> wsproxy, Preset<T> preset, List<Option<T>> customOptions) {
\r
363 ScoreManager scores = null;
\r
365 String jobId = null;
\r
366 if (customOptions != null && preset != null) {
\r
367 System.out.println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
369 if (customOptions != null) {
\r
370 jobId = wsproxy.customAnalize(fastalist, customOptions);
\r
371 } else if (preset != null) {
\r
372 jobId = wsproxy.presetAnalize(fastalist, preset);
\r
374 jobId = wsproxy.analize(fastalist);
\r
376 System.out.println("\n\rcalling predictor.........");
\r
377 Thread.sleep(1000);
\r
379 JobStatus status = wsproxy.getJobStatus(jobId);
\r
380 System.out.println("\njob " + jobId + " status: " + status);
\r
382 scores = wsproxy.getAnnotation(jobId);
\r
383 } catch (JobSubmissionException e) {
\r
384 System.err.println("Exception while submitting job to a web server. Exception details are below:");
\r
385 e.printStackTrace();
\r
386 } catch (ResultNotAvailableException e) {
\r
387 System.err.println("Exception while waiting for results. Exception details are below:");
\r
388 e.printStackTrace();
\r
389 } catch (InterruptedException e) {
\r
390 Thread.currentThread().interrupt();
\r
391 System.err.println("Exception while waiting for results. Exception details are below:");
\r
392 e.printStackTrace();
\r
393 } catch (WrongParameterException e) {
\r
394 String mess = "Parsing the web method input parameters failed Exception details are below:";
\r
395 System.err.println(mess);
\r
396 e.printStackTrace();
\r
403 * Calculate conservation for sequences loaded from the file
\r
406 * a web service proxy
\r
408 * the file to read the results from
\r
410 * Preset to use optional
\r
411 * @param customOptions
\r
412 * the list of options
\r
413 * @return Set<Score> the conservation scores
\r
414 * @throws IOException
\r
415 * @throws UnknownFileFormatException
\r
417 static <T> ScoreManager analize(File file, SequenceAnnotation<T> wsproxy, Preset<T> preset, List<Option<T>> customOptions) {
\r
418 List<FastaSequence> fastalist = null;
\r
420 fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());
\r
421 assert !fastalist.isEmpty() : "Input is empty!";
\r
422 } catch (IOException e) {
\r
423 String mess = "Reading the input file failed. Check that the file contains a list of FASTA records!\n";
\r
424 System.err.println(mess + "Exception details are below:");
\r
425 e.printStackTrace();
\r
426 } catch (UnknownFileFormatException e) {
\r
427 String mess = "Reading the input file failed. Exception details are below:";
\r
428 System.err.println(mess);
\r
429 System.out.println(e.getMessage());
\r
430 e.printStackTrace();
\r
432 return analize(fastalist, wsproxy, preset, customOptions);
\r
436 * Align sequences from the file using MsaWS
\r
439 * web service type e.g. Clustal
\r
441 * to write the resulting alignment to
\r
445 * Preset to use optional
\r
446 * @param customOptions
\r
447 * file which contains new line separated list of options
\r
448 * @return Alignment
\r
450 static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,
\r
451 List<Option<T>> customOptions) {
\r
452 FileInputStream instream = null;
\r
453 Alignment alignment = null;
\r
455 instream = new FileInputStream(file);
\r
456 List<FastaSequence> fastalist = SequenceUtil.readFasta(instream);
\r
458 String jobId = null;
\r
459 if (customOptions != null && preset != null) {
\r
460 System.out.println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");
\r
462 if (customOptions != null) {
\r
463 jobId = msaws.customAlign(fastalist, customOptions);
\r
464 } else if (preset != null) {
\r
465 jobId = msaws.presetAlign(fastalist, preset);
\r
467 jobId = msaws.align(fastalist);
\r
469 System.out.println("\ncalling program.........");
\r
471 long startTime = System.nanoTime();
\r
472 while (JobStatus.RUNNING == msaws.getJobStatus(jobId)) {
\r
473 Thread.sleep(1000);
\r
474 long endTime = System.nanoTime();
\r
475 System.out.println("job " + jobId + " time executing: "+ (endTime - startTime) / 1000000 +" msec, status: " + msaws.getJobStatus(jobId));
\r
478 Thread.sleep(1000);
\r
479 alignment = msaws.getResult(jobId);
\r
480 } catch (IOException e) {
\r
481 System.err.println("Exception while reading the input file. Check that the input file is a FASTA file! "
\r
482 + "Exception details are below:");
\r
483 e.printStackTrace();
\r
484 } catch (JobSubmissionException e) {
\r
485 System.err.println("Exception while submitting job to a web server. Exception details are below:");
\r
486 e.printStackTrace();
\r
487 } catch (ResultNotAvailableException e) {
\r
488 System.err.println("Exception while waiting for results. Exception details are below:");
\r
489 e.printStackTrace();
\r
490 } catch (InterruptedException ignored) {
\r
491 // ignore and propagate an interruption
\r
492 Thread.currentThread().interrupt();
\r
493 } catch (WrongParameterException e) {
\r
494 e.printStackTrace();
\r
496 if (instream != null) {
\r
499 } catch (IOException ignored) {
\r
508 * Starts command line client, if no parameter are supported print help. Two
\r
509 * parameters are required for successful call the JWS2 host name and a
\r
513 * Usage: <Class or Jar file name> -h=host_and_context
\r
514 * -s=serviceName ACTION [OPTIONS]
\r
516 * -h=<host_and_context> - a full URL to the JWS2 web server
\r
517 * including context path e.g. http://10.31.1.159:8080/ws
\r
519 * -s=<ServiceName> - one of [MafftWS, MuscleWS, ClustalWS,
\r
520 * TcoffeeWS, ProbconsWS] ACTIONS:
\r
522 * -i=<inputFile> - full path to fasta formatted sequence file,
\r
523 * from which to align sequences
\r
525 * -parameters - lists parameters supported by web service
\r
527 * -presets - lists presets supported by web service
\r
529 * -limits - lists web services limits Please note that if input
\r
530 * file is specified other actions are ignored
\r
532 * OPTIONS: (only for use with -i action):
\r
534 * -r=<presetName> - name of the preset to use
\r
536 * -o=<outputFile> - full path to the file where to write an
\r
537 * alignment -f=<parameterInputFile> - the name of the file with
\r
538 * the list of parameters to use. Please note that -r and -f
\r
539 * options cannot be used together. Alignment is done with either
\r
540 * preset or a parameters from the file, but not both!
\r
543 public static void main(String[] args) {
\r
544 if (args == null) {
\r
545 System.out.println(Constraints.help_text);
\r
548 if (args.length < 2) {
\r
549 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
550 System.out.println(Constraints.help_text);
\r
554 new Jws2Client(args);
\r
555 } catch (IOException e) {
\r
556 log.log(Level.SEVERE, "IOException in client! " + e.getMessage(), e.getCause());
\r
557 System.err.println("Cannot write output file! Stack trace: ");
\r
558 e.printStackTrace();
\r