X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=webservices%2Fcompbio%2Fws%2Fclient%2FJws2Client.java;h=666fa58a6dba17d489be89af4b26d19ef22cde0a;hb=5578f2d3e8226bd931755da3feeecb91fdb26c3f;hp=a0c95d1366c2adbf42a9b3a528441dcf6c33dfec;hpb=79e1d66ef2100149b1117c798dd7b4edd9333912;p=jabaws.git diff --git a/webservices/compbio/ws/client/Jws2Client.java b/webservices/compbio/ws/client/Jws2Client.java index a0c95d1..666fa58 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 @@ -25,13 +25,17 @@ import static compbio.ws.client.Constraints.paramFile; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; 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; @@ -42,8 +46,11 @@ import javax.xml.ws.WebServiceException; import compbio.data.msa.JABAService; 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.AlignmentMetadata; +import compbio.data.sequence.JpredAlignment; import compbio.data.sequence.FastaSequence; import compbio.data.sequence.ScoreManager; import compbio.data.sequence.SequenceUtil; @@ -53,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 @@ -62,18 +70,10 @@ 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/"; + private static final Logger log = Logger.getLogger(Jws2Client.class.getCanonicalName()); /** * Attempt to construct the URL object from the string @@ -103,92 +103,239 @@ public class Jws2Client { * command line options * @throws IOException */ + @SuppressWarnings("unchecked") Jws2Client(String[] cmd) throws IOException { + String hostname = CmdHelper.getHost(cmd); if (hostname == null) { - System.out.println("Host name is not provided!"); - printUsage(1); + System.err.println("Host name is not provided!"); + System.out.println(Constraints.help_text); + System.exit(1); } if (!validURL(hostname)) { - System.out.println("Host name is not valid!"); - printUsage(1); + System.err.println("Host name is not valid!"); + System.out.println(Constraints.help_text); + System.exit(1); + } + + boolean listServices = CmdHelper.listServices(cmd); + if (listServices) { + listAllServices(hostname); + System.exit(0); } + String serviceName = CmdHelper.getServiceName(cmd); if (serviceName == null) { - System.out.println("Service name is no provided!"); - printUsage(1); + System.err.println("Service name is no provided!"); + System.out.println(Constraints.help_text); + System.exit(1); } + Services service = Services.getService(serviceName); if (service == null) { - System.out.println("Service " + serviceName - + " is no supported! Valid values are: " - + Arrays.toString(Services.values())); - printUsage(1); + String mess = "Service " + serviceName + " is no available! Valid values are: "; + System.err.println(mess + Arrays.toString(Services.values())); + System.out.println(Constraints.help_text); + System.exit(1); } - 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); + if (CmdHelper.testService(cmd)) { + testService(hostname, service, new PrintWriter(System.out, true)); + System.exit(0); + } + + Metadata thews = (Metadata) connect(hostname, service); Preset preset = null; - if (presetName != null) { - preset = MetadataHelper.getPreset(msaws, presetName); + if (null != CmdHelper.getPresetName(cmd)) { + preset = MetadataHelper.getPreset(thews, CmdHelper.getPresetName(cmd)); } + List> customOptions = null; - if (parametersFile != null) { - List prms = IOHelper.loadParameters(parametersFile); - customOptions = MetadataHelper.processParameters(prms, - msaws.getRunnerOptions()); + if (null != IOHelper.getFile(cmd, paramFile, true)) { + List prms = IOHelper.loadParameters(IOHelper.getFile(cmd, paramFile, true)); + customOptions = MetadataHelper.processParameters(prms, thews.getRunnerOptions()); } - Alignment alignment = null; - if (inputFile != null) { + + if (null != IOHelper.getFile(cmd, inputkey, true)) { + File infile = IOHelper.getFile(cmd, inputkey, true); + File outfile = IOHelper.getFile(cmd, outputkey, false); Writer writer = null; - if (outFile != null) { - writer = IOHelper.getWriter(outFile); + if (outfile != null) { + writer = IOHelper.getWriter(outfile); } else { - // this stream is going to be closed later which is fine as - // std.out will not be writer = new PrintWriter(System.out, true); } if (service.getServiceType() == SequenceAnnotation.class) { - ScoreManager result = analize(inputFile, - ((SequenceAnnotation) msaws), preset, customOptions); - + ScoreManager result = analize(infile, ((SequenceAnnotation) thews), preset, customOptions); IOHelper.writeOut(writer, result); - } else { - alignment = align(inputFile, (MsaWS) msaws, preset, - customOptions); - IOHelper.writeOut(writer, alignment); + writer.close(); + } else if (service.getServiceType() == MsaWS.class) { + Alignment alignment = align(infile, (MsaWS) thews, preset, customOptions); + if (serviceName.equalsIgnoreCase("JpredWS")) { + writer.close(); + JpredAlignment jpred = (JpredAlignment)alignment; + if (outfile != null) { + FileOutputStream fout = new FileOutputStream(outfile); + SequenceUtil.writeFastaKeepTheStream(fout, jpred.getJpredSequences(), 60); + fout.close(); + } else { + SequenceUtil.writeFasta(System.out, jpred.getJpredSequences()); + } + } else { + IOHelper.writeOut(writer, alignment); + writer.close(); + } + AlignmentMetadata md = alignment.getMetadata(); + System.out.println("Output has been prepared with " + md.getProgram()); } - writer.close(); } - boolean listParameters = CmdHelper.listParameters(cmd); - if (listParameters) { - System.out.println(MetadataHelper.getParametersList(msaws)); + if (CmdHelper.listParameters(cmd)) { + System.out.println(MetadataHelper.getParametersList(thews)); } - boolean listPreset = CmdHelper.listPresets(cmd); - if (listPreset) { - System.out.println(MetadataHelper.getPresetList(msaws)); + if (CmdHelper.listPresets(cmd)) { + System.out.println(MetadataHelper.getPresetList(thews)); } - boolean listLimits = CmdHelper.listLimits(cmd); - if (listLimits) { - System.out.println(MetadataHelper.getLimits(msaws)); + if (CmdHelper.listLimits(cmd)) { + System.out.println(MetadataHelper.getLimits(thews)); } log.fine("Disconnecting..."); - ((Closeable) msaws).close(); + ((Closeable) thews).close(); log.fine("Disconnected successfully!"); } + /** - * Calculate conservation for sequences loaded from the file + * Connects to a web service by the host and the service name 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 + * 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, ConnectException { + URL url = null; + log.log(Level.FINE, "Attempting to connect with " + service.toString() + "..."); + System.out.println ("Attempting to connect with " + service.toString() + "..."); + try { + url = new URL(host + "/" + service.toString() + "?wsdl"); + } catch (MalformedURLException e) { + e.printStackTrace(); + // ignore as the host name is already verified + } + Service serv = null; + try { + serv = service.getService(url, service.getServiceNamespace()); + } catch (WebServiceException wse) { + wse.printStackTrace(); + } + if (serv == null) { + throw new ConnectException("Could not connect to " + url + ". Is the server down?"); + } + JABAService serviceIF = service.getInterface(serv); + log.log(Level.INFO, "Connected successfully!"); + + return serviceIF; + } + + /** + * 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, ConnectException { + URL url = null; + String service = "RegistryWS"; + log.log(Level.FINE, "Attempting to connect..."); + + try { + url = new URL(host + "/" + service + "?wsdl"); + } catch (MalformedURLException e) { + e.printStackTrace(); + // ignore as the host name is already verified + } + QName qname = new QName(JABAService.V2_SERVICE_NAMESPACE, service); + Service serv = Service.create(url, qname); + + if (serv == null) { + throw new ConnectException("Could not connect to " + url + ". Is the server down?"); + } + + QName portName = new QName(serv.getServiceName().getNamespaceURI(), service + "Port"); + compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName, compbio.data.msa.RegistryWS.class); + log.log(Level.INFO, "Connected to " + service + " successfully!"); + + return serviceIF; + } + + /** + * 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(); + } + + private static void listAllServices(String hostname) throws WebServiceException, IOException { + RegistryWS registry = connectToRegistry(hostname); + Set func_services = Collections.EMPTY_SET; + Set nonfunc_services = Collections.EMPTY_SET; + if (registry != null) { + func_services = registry.getSupportedServices(); + nonfunc_services = registry.getNonoperatedServices(); + FileUtil.closeSilently(((Closeable) registry)); + } else { + System.out.println("Failed to connect to the registry! "); + return; + } + if (!func_services.isEmpty()) { + System.out.println("There are " + func_services.size() + " services at " + hostname + ":"); + String mess = "\n\rThe list:\n"; + System.out.println(mess + Services.toString(func_services)); + } + if (!nonfunc_services.isEmpty()) { + System.out.println("There are " + nonfunc_services.size() + " non-available services at " + hostname + ":"); + String mess = "The list (internal tests failed): "; + System.out.println(mess + Services.toString(nonfunc_services)); + } + } + + /** + * Calculate conservation for sequences loaded from a FASTA record list structure * + * @param fastalist + * the list of FASTA records * @param wsproxy * a web service proxy - * @param file - * the file to read the results from * @param preset * Preset to use optional * @param customOptions @@ -196,16 +343,12 @@ public class Jws2Client { * @return Set the conservation scores * @throws UnknownFileFormatException */ - static ScoreManager analize(List fastalist, - SequenceAnnotation wsproxy, Preset preset, - List> customOptions) { - + static ScoreManager analize(List fastalist, SequenceAnnotation wsproxy, Preset preset, List> customOptions) { ScoreManager scores = null; try { String jobId = null; if (customOptions != null && preset != null) { - System.out - .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!"); + System.out.println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!"); } if (customOptions != null) { jobId = wsproxy.customAnalize(fastalist, customOptions); @@ -214,29 +357,22 @@ public class Jws2Client { } else { jobId = wsproxy.analize(fastalist); } - System.out.println("\n\ncalling analise........."); - Thread.sleep(1000); + System.out.println("\n\rcalling predictor........."); + Thread.sleep(100); scores = wsproxy.getAnnotation(jobId); - } catch (JobSubmissionException e) { - System.err - .println("Exception while submitting job to a web server. " - + "Exception details are below:"); + System.err.println("Exception while submitting job to a web server. Exception details are below:"); e.printStackTrace(); } catch (ResultNotAvailableException e) { - System.err.println("Exception while waiting for results. " - + "Exception details are below:"); + System.err.println("Exception while waiting for results. Exception details are below:"); e.printStackTrace(); } catch (InterruptedException e) { - // ignore and propagate an interruption Thread.currentThread().interrupt(); - System.err.println("Exception while waiting for results. " - + "Exception details are below:"); + System.err.println("Exception while waiting for results. Exception details are below:"); e.printStackTrace(); } catch (WrongParameterException e) { - System.err - .println("Exception while parsing the web method input parameters. " - + "Exception details are below:"); + String mess = "Parsing the web method input parameters failed Exception details are below:"; + System.err.println(mess); e.printStackTrace(); } return scores; @@ -258,107 +394,23 @@ public class Jws2Client { * @throws IOException * @throws UnknownFileFormatException */ - static ScoreManager analize(File file, SequenceAnnotation wsproxy, - Preset preset, List> customOptions) { + static ScoreManager analize(File file, SequenceAnnotation wsproxy, Preset preset, List> customOptions) { List fastalist = null; try { fastalist = SequenceUtil.openInputStream(file.getAbsolutePath()); assert !fastalist.isEmpty() : "Input is empty!"; } catch (IOException e) { - System.err - .println("Exception while reading the input file. " - + "Check that the input file contains a list of fasta formatted sequences! " - + "Exception details are below:"); + String mess = "Reading the input file failed. Check that the file contains a list of FASTA records!\n"; + System.err.println(mess + "Exception details are below:"); e.printStackTrace(); } catch (UnknownFileFormatException e) { - System.err - .println("Exception while attempting to read the input file " - + "Exception details are below:"); + String mess = "Reading the input file failed. Exception details are below:"; + System.err.println(mess); System.out.println(e.getMessage()); e.printStackTrace(); } return analize(fastalist, wsproxy, preset, customOptions); } - /** - * Connects to a web service by the host and the service name - * - * @param T - * web service type - * @param host - * @param service - * @return MsaWS - * @throws WebServiceException - */ - public static JABAService connect(String host, Services service) - throws WebServiceException { - URL url = null; - log.log(Level.FINE, "Attempting to connect..."); - try { - url = new URL(host + "/" + service.toString() + "?wsdl"); - } catch (MalformedURLException e) { - e.printStackTrace(); - // ignore as the host name is already verified - } - Service serv = null; - try { - serv = service.getService(url, QUALIFIED_SERVICE_NAME); - } catch (WebServiceException wse) { - System.out.println("Connecting to JABAWS version 2 service"); - if (isV2service(wse)) { - serv = service.getService(url, V2_QUALIFIED_SERVICE_NAME); - } - } - if (serv == null) { - System.err.println("Could not connect to " + url - + " the server is down?"); - // FIXME - } - JABAService serviceIF = service.getInterface(serv); - log.log(Level.INFO, "Connected successfully!"); - - return serviceIF; - } - - static boolean isV2service(WebServiceException wse) { - String message = wse.getMessage(); - int idx = message.indexOf("not a valid service"); - if (idx > 0) { - if (message.substring(idx).contains(V2_QUALIFIED_SERVICE_NAME)) { - return true; - } - } - return false; - } - - public static compbio.data.msa.RegistryWS connectToRegistry(String host) - throws WebServiceException { - URL url = null; - String service = "RegistryWS"; - log.log(Level.FINE, "Attempting to connect..."); - - try { - url = new URL(host + "/" + service + "?wsdl"); - } catch (MalformedURLException e) { - e.printStackTrace(); - // ignore as the host name is already verified - } - QName qname = new QName(V2_QUALIFIED_SERVICE_NAME, service); - Service serv = Service.create(url, qname); - - if (serv == null) { - System.err.println("Could not connect to " + url - + " the server is down?"); - } - - QName portName = new QName(serv.getServiceName().getNamespaceURI(), - service + "Port"); - compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName, - compbio.data.msa.RegistryWS.class); - - log.log(Level.INFO, "Connected to " + service + " successfully!"); - - return serviceIF; - } /** * Align sequences from the file using MsaWS @@ -378,16 +430,14 @@ public class Jws2Client { static Alignment align(File file, MsaWS msaws, Preset preset, List> customOptions) { FileInputStream instream = null; - List fastalist = null; Alignment alignment = null; try { instream = new FileInputStream(file); - fastalist = SequenceUtil.readFasta(instream); + List fastalist = SequenceUtil.readFasta(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!"); + System.out.println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!"); } if (customOptions != null) { jobId = msaws.customAlign(fastalist, customOptions); @@ -396,24 +446,18 @@ public class Jws2Client { } else { jobId = msaws.align(fastalist); } - System.out.println("\n\ncalling align........."); - Thread.sleep(1000); + System.out.println("\ncalling program........."); + Thread.sleep(100); alignment = msaws.getResult(jobId); - } catch (IOException e) { - System.err - .println("Exception while reading the input file. " - + "Check that the input file contains a list of fasta formatted sequences! " - + "Exception details are below:"); + System.err.println("Exception while reading the input file. Check that the input file is a FASTA file! " + + "Exception details are below:"); e.printStackTrace(); } catch (JobSubmissionException e) { - System.err - .println("Exception while submitting job to a web server. " - + "Exception details are below:"); + System.err.println("Exception while submitting job to a web server. Exception details are below:"); e.printStackTrace(); } catch (ResultNotAvailableException e) { - System.err.println("Exception while waiting for results. " - + "Exception details are below:"); + System.err.println("Exception while waiting for results. Exception details are below:"); e.printStackTrace(); } catch (InterruptedException ignored) { // ignore and propagate an interruption @@ -431,17 +475,7 @@ public class Jws2Client { } return alignment; } - - /** - * Prints Jws2Client usage information to standard out - * - * @param exitStatus - */ - static void printUsage(int exitStatus) { - System.out.println(Constraints.help_text); - System.exit(exitStatus); - } - + /** * Starts command line client, if no parameter are supported print help. Two * parameters are required for successful call the JWS2 host name and a @@ -479,20 +513,19 @@ public class Jws2Client { * */ public static void main(String[] args) { - if (args == null) { - printUsage(1); + System.out.println(Constraints.help_text); + System.exit(1); } if (args.length < 2) { - System.out.println("Host and service names are required!"); - printUsage(1); + System.err.println("Host (-h=, e.g. -h=http://www.compbio.dundee.ac.uk/jabaws) and service (-s=, e.g. -s=Jpred) are required!"); + System.out.println(Constraints.help_text); + System.exit(1); } - try { new Jws2Client(args); } catch (IOException e) { - log.log(Level.SEVERE, "IOException in client! " + e.getMessage(), - e.getCause()); + log.log(Level.SEVERE, "IOException in client! " + e.getMessage(), e.getCause()); System.err.println("Cannot write output file! Stack trace: "); e.printStackTrace(); }