X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=webservices%2Fcompbio%2Fws%2Fclient%2FJws2Client.java;h=aa048b001a0a62aad936af2f80943fb5e3314710;hb=f687d44d69ce305901cc18305332ae07f69c7845;hp=ceb9501cea191653179d4f10a6d3bd351f3d1224;hpb=a2619dbd95b874abd03ae551c02524fe09e9f631;p=jabaws.git diff --git a/webservices/compbio/ws/client/Jws2Client.java b/webservices/compbio/ws/client/Jws2Client.java index ceb9501..aa048b0 100644 --- a/webservices/compbio/ws/client/Jws2Client.java +++ b/webservices/compbio/ws/client/Jws2Client.java @@ -1,6 +1,6 @@ -/* Copyright (c) 2009 Peter Troshin +/* Copyright (c) 2011 Peter Troshin * - * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0 + * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0 * * This library is free software; you can redistribute it and/or modify it under the terms of the * Apache License version 2 as published by the Apache Software Foundation @@ -18,26 +18,23 @@ package compbio.ws.client; -import static compbio.ws.client.Constraints.hostkey; import static compbio.ws.client.Constraints.inputkey; -import static compbio.ws.client.Constraints.limitList; import static compbio.ws.client.Constraints.outputkey; import static compbio.ws.client.Constraints.paramFile; -import static compbio.ws.client.Constraints.paramList; -import static compbio.ws.client.Constraints.presetList; -import static compbio.ws.client.Constraints.presetkey; -import static compbio.ws.client.Constraints.pseparator; -import static compbio.ws.client.Constraints.servicekey; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.Writer; +import java.net.ConnectException; import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -46,12 +43,16 @@ import javax.xml.ws.Service; import javax.xml.ws.WebServiceException; import compbio.data.msa.JABAService; +import compbio.data.msa.FoldWS; import compbio.data.msa.Metadata; import compbio.data.msa.MsaWS; +import compbio.data.msa.RegistryWS; import compbio.data.msa.SequenceAnnotation; import compbio.data.sequence.Alignment; import compbio.data.sequence.FastaSequence; +import compbio.data.sequence.RNAStruct; import compbio.data.sequence.ScoreManager; +import compbio.data.sequence.ClustalAlignmentUtil; import compbio.data.sequence.SequenceUtil; import compbio.data.sequence.UnknownFileFormatException; import compbio.metadata.JobSubmissionException; @@ -59,6 +60,7 @@ import compbio.metadata.Option; import compbio.metadata.Preset; import compbio.metadata.ResultNotAvailableException; import compbio.metadata.WrongParameterException; +import compbio.util.FileUtil; /** * A command line client for JAva Bioinformatics Analysis Web Services @@ -69,18 +71,12 @@ import compbio.metadata.WrongParameterException; public class Jws2Client { /* - * TODO Use java.util.Logger instead of log4j logger to reduce the size of - * the client package + * Use java.util.Logger instead of log4j logger to reduce the size of the + * client package */ private static final Logger log = Logger.getLogger(Jws2Client.class .getCanonicalName()); - // JABAWS version 1.0 service name - static final String QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/01/2010/"; - - // JABAWS version 2.0 service name - static final String V2_QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/12/2010/"; - /** * Attempt to construct the URL object from the string * @@ -110,35 +106,51 @@ public class Jws2Client { * @throws IOException */ Jws2Client(String[] cmd) throws IOException { + String hostname = CmdHelper.getHost(cmd); if (hostname == null) { - System.out.println("Host name is not provided!"); + System.err.println("Host name is not provided!"); printUsage(1); } if (!validURL(hostname)) { - System.out.println("Host name is not valid!"); + System.err.println("Host name is not valid!"); printUsage(1); } + // Just list available services and quit + boolean listServices = CmdHelper.listServices(cmd); + if (listServices) { + listServices(hostname); + System.exit(0); + } + String serviceName = CmdHelper.getServiceName(cmd); if (serviceName == null) { - System.out.println("Service name is no provided!"); + System.err.println("Service name is no provided!"); printUsage(1); } Services service = Services.getService(serviceName); if (service == null) { - System.out.println("Service " + serviceName - + " is no supported! Valid values are: " + System.err.println("Service " + serviceName + + " is no recognized! Valid values are: " + Arrays.toString(Services.values())); printUsage(1); } + // Test service and quit + boolean testService = CmdHelper.testService(cmd); + if (testService) { + testService(hostname, service, new PrintWriter(System.out, true)); + System.exit(0); + } + File inputFile = IOHelper.getFile(cmd, inputkey, true); File outFile = IOHelper.getFile(cmd, outputkey, false); File parametersFile = IOHelper.getFile(cmd, paramFile, true); String presetName = CmdHelper.getPresetName(cmd); Metadata msaws = (Metadata) connect(hostname, service); + Metadata foldws = (Metadata) connect(hostname, service); Preset preset = null; if (presetName != null) { preset = MetadataHelper.getPreset(msaws, presetName); @@ -149,27 +161,36 @@ public class Jws2Client { customOptions = MetadataHelper.processParameters(prms, msaws.getRunnerOptions()); } +// System.out.println("The Options read from the command line: " + customOptions); + Alignment alignment = null; + String rnastruct = null; if (inputFile != null) { - OutputStream outStream = null; + Writer writer = null; if (outFile != null) { - outStream = IOHelper.getOutStream(outFile); + writer = IOHelper.getWriter(outFile); } else { // this stream is going to be closed later which is fine as // std.out will not be - outStream = System.out; + writer = new PrintWriter(System.out, true); } if (service.getServiceType() == SequenceAnnotation.class) { ScoreManager result = analize(inputFile, ((SequenceAnnotation) msaws), preset, customOptions); - IOHelper.writeOut(outStream, result); - } else { + IOHelper.writeOut(writer, result); + } else if (service.getServiceType() == MsaWS.class) { alignment = align(inputFile, (MsaWS) msaws, preset, customOptions); - IOHelper.writeOut(outStream, alignment); + IOHelper.writeOut(writer, alignment); + } else if (service.getServiceType() == FoldWS.class) { + rnastruct = fold(inputFile, (FoldWS) foldws, preset, customOptions); + // No IOHelper method for rnastruct/String yet + if (writer != null) { + writer.write(rnastruct); + } } - outStream.close(); + writer.close(); } boolean listParameters = CmdHelper.listParameters(cmd); @@ -188,6 +209,50 @@ public class Jws2Client { ((Closeable) msaws).close(); log.fine("Disconnected successfully!"); } + + /** + * Asks registry to test the service on the host hostname + * + * @param hostname + * @param service + * @param writer + * @throws ConnectException + * @throws WebServiceException + */ + public static void testService(String hostname, Services service, + PrintWriter writer) throws ConnectException, WebServiceException { + RegistryWS registry = connectToRegistry(hostname); + if (registry != null) { + String message = registry.testService(service); + writer.println("Service " + service + " testing results: "); + writer.println(message); + FileUtil.closeSilently(((Closeable) registry)); + } + writer.flush(); + } + + public static Set getServices(String hostname) + throws WebServiceException, ConnectException { + RegistryWS registry = connectToRegistry(hostname); + Set services = Collections.EMPTY_SET; + if (registry != null) { + services = registry.getSupportedServices(); + FileUtil.closeSilently(((Closeable) registry)); + } + return services; + } + + private static void listServices(String hostname) + throws WebServiceException, IOException { + Set services = Jws2Client.getServices(hostname); + if (!services.isEmpty()) { + System.out.println("Supported services are: " + + Services.toString(services)); + } else { + System.out.println("Failed to connect to the registry! "); + } + } + /** * Calculate conservation for sequences loaded from the file * @@ -286,17 +351,22 @@ public class Jws2Client { return analize(fastalist, wsproxy, preset, customOptions); } /** - * Connects to a web service by the host and the service name + * Connects to a web service by the host and the service name web service + * type * - * @param T - * web service type * @param host + * the fully qualified name of JABAWS server including JABAWS + * context name e.g + * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba * @param service - * @return MsaWS + * the name of the JABAWS service to connect to + * @return JABAService * @throws WebServiceException + * @throws ConnectException + * if fails to connect to the service on the host */ public static JABAService connect(String host, Services service) - throws WebServiceException { + throws WebServiceException, ConnectException { URL url = null; log.log(Level.FINE, "Attempting to connect..."); try { @@ -307,17 +377,17 @@ public class Jws2Client { } Service serv = null; try { - serv = service.getService(url, QUALIFIED_SERVICE_NAME); + serv = service.getService(url, JABAService.SERVICE_NAMESPACE); } catch (WebServiceException wse) { System.out.println("Connecting to JABAWS version 2 service"); if (isV2service(wse)) { - serv = service.getService(url, V2_QUALIFIED_SERVICE_NAME); + serv = service + .getService(url, JABAService.V2_SERVICE_NAMESPACE); } } if (serv == null) { - System.err.println("Could not connect to " + url + throw new ConnectException("Could not connect to " + url + " the server is down?"); - // FIXME } JABAService serviceIF = service.getInterface(serv); log.log(Level.INFO, "Connected successfully!"); @@ -329,15 +399,28 @@ public class Jws2Client { String message = wse.getMessage(); int idx = message.indexOf("not a valid service"); if (idx > 0) { - if (message.substring(idx).contains(V2_QUALIFIED_SERVICE_NAME)) { + if (message.substring(idx).contains( + JABAService.V2_SERVICE_NAMESPACE)) { return true; } } return false; } + /** + * Get a connection of JABAWS registry + * + * @param host + * the fully qualified name of JABAWS server including JABAWS + * context name e.g + * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba + * @return compbio.data.msa.RegistryWS - instance of a RegistryWS web + * service + * @throws WebServiceException + * @throws ConnectException + */ public static compbio.data.msa.RegistryWS connectToRegistry(String host) - throws WebServiceException { + throws WebServiceException, ConnectException { URL url = null; String service = "RegistryWS"; log.log(Level.FINE, "Attempting to connect..."); @@ -348,11 +431,11 @@ public class Jws2Client { e.printStackTrace(); // ignore as the host name is already verified } - QName qname = new QName(V2_QUALIFIED_SERVICE_NAME, service); + QName qname = new QName(JABAService.V2_SERVICE_NAMESPACE, service); Service serv = Service.create(url, qname); if (serv == null) { - System.err.println("Could not connect to " + url + throw new ConnectException("Could not connect to " + url + " the server is down?"); } @@ -437,53 +520,84 @@ public class Jws2Client { } return alignment; } - + + /** + * Return RNA secondary structure from a file using FoldWS + * + * @param + * web service type e.g. Clustal + * @param file + * to read the results from + * @param foldws + * FoldWS required + * @param preset + * Preset to use optional + * @param customOptions + * file which contains new line separated list of options + * @return String + */ + + static String fold(File file, FoldWS foldws, Preset preset, + List> customOptions) { + FileInputStream instream = null; + Alignment alignment = null; + String rnastruct = null; + try { + instream = new FileInputStream(file); + alignment = ClustalAlignmentUtil.readClustalFile(instream); + instream.close(); + String jobId = null; + if (customOptions != null && preset != null) { + System.out.println("WARN: Parameters (-f) are defined together" + + "with a preset (-r), ignoring preset! "); + } + if (customOptions != null) { + jobId = foldws.customFold(alignment, customOptions); + } else if (preset != null) { + jobId = foldws.presetFold(alignment, preset); + } else { + jobId = foldws.fold(alignment); + } + System.out.println("\n\ncalling fold........."); + Thread.sleep(1000); + rnastruct = foldws.getResult(jobId); + + } catch (IOException e) { + System.err.println("Exception while reading the input file. Exception details: "); + e.printStackTrace(); + } catch (UnknownFileFormatException e) { + System.err.println("Exception while reading input file. Doesnt look like a Clustal format file"); + e.printStackTrace(); + } catch (JobSubmissionException e) { + System.err.println("Exception while submitting job to the web server. "); + e.printStackTrace(); + } catch (ResultNotAvailableException e) { + System.err.println("Exception while waiting for results. Exception details: "); + e.printStackTrace(); + } catch (InterruptedException ignored) { + // ignore and propagate an interruption + Thread.currentThread().interrupt(); + } catch (WrongParameterException e) { + e.printStackTrace(); + } finally { + if (instream != null) { + try { + instream.close(); + } catch (IOException ignored) { + // ignore + } + } + } + return rnastruct; + } + /** * Prints Jws2Client usage information to standard out * * @param exitStatus */ static void printUsage(int exitStatus) { - System.out.println(); - System.out.println("Usage: " + hostkey - + pseparator + "host_and_context " + servicekey + pseparator - + "serviceName ACTION [OPTIONS] "); - System.out.println(); - System.out - .println(hostkey - + pseparator - + " - a full URL to the JWS2 web server including context path e.g. http://10.31.1.159:8080/ws"); - System.out.println(servicekey + pseparator + " - one of " - + Arrays.toString(Services.values())); - System.out.println(); - System.out.println("ACTIONS: "); - System.out - .println(inputkey - + pseparator - + " - full path to fasta formatted sequence file, from which to align sequences"); - System.out.println(paramList - + " - lists parameters supported by web service"); - System.out.println(presetList - + " - lists presets supported by web service"); - System.out.println(limitList + " - lists web services limits"); - System.out - .println("Please note that if input file is specified other actions are ignored"); - - System.out.println(); - System.out.println("OPTIONS (only for use with -i action):"); - - System.out.println(presetkey + pseparator - + " - name of the preset to use"); - System.out - .println(outputkey - + pseparator - + " - full path to the file where to write an alignment"); - System.out - .println("-f= - the name of the file with the list of parameters to use."); - System.out - .println("Please note that -r and -f options cannot be used together. " - + "Alignment is done with either preset or a parameters from the file, but not both!"); - + System.out.println(Constraints.help_text); System.exit(exitStatus); } @@ -529,7 +643,7 @@ public class Jws2Client { printUsage(1); } if (args.length < 2) { - System.out.println("Host and service names are required!"); + System.err.println("Host and service names are required!"); printUsage(1); }