From: pvtroshin Date: Fri, 3 Dec 2010 19:03:59 +0000 (+0000) Subject: Refactored client class to accomodate new WS interface and reduce the complexity X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=6e1c873d0879f462b1fb1a1efa444cf993a9e76a;p=jabaws.git Refactored client class to accomodate new WS interface and reduce the complexity git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@3428 e3abac25-378b-4346-85de-24260fe3988d --- diff --git a/webservices/compbio/data/msa/Annotation.java b/webservices/compbio/data/msa/Annotation.java index fb8436d..25e6ac1 100644 --- a/webservices/compbio/data/msa/Annotation.java +++ b/webservices/compbio/data/msa/Annotation.java @@ -29,7 +29,7 @@ import compbio.metadata.WrongParameterException; * executable type / web service type */ @WebService(targetNamespace = "http://msa.data.compbio/01/12/2010/") -public interface Annotation extends JManagement, Metadata { +public interface Annotation extends JABAService, JManagement, Metadata { /** * diff --git a/webservices/compbio/data/msa/JABAService.java b/webservices/compbio/data/msa/JABAService.java new file mode 100644 index 0000000..69d53f7 --- /dev/null +++ b/webservices/compbio/data/msa/JABAService.java @@ -0,0 +1,11 @@ +package compbio.data.msa; + +/** + * This is a marker interface, contains no methods + * + * @author pvtroshin + * + */ +public interface JABAService { + +} diff --git a/webservices/compbio/data/msa/MsaWS.java b/webservices/compbio/data/msa/MsaWS.java index cea5cdf..43d1581 100644 --- a/webservices/compbio/data/msa/MsaWS.java +++ b/webservices/compbio/data/msa/MsaWS.java @@ -45,7 +45,7 @@ import compbio.metadata.WrongParameterException; * executable type / web service type */ @WebService(targetNamespace = "http://msa.data.compbio/01/01/2010/") -public interface MsaWS extends JManagement, Metadata { +public interface MsaWS extends JABAService, JManagement, Metadata { /** * Align a list of sequences with default settings. diff --git a/webservices/compbio/ws/client/CmdHelper.java b/webservices/compbio/ws/client/CmdHelper.java new file mode 100644 index 0000000..1faf3dc --- /dev/null +++ b/webservices/compbio/ws/client/CmdHelper.java @@ -0,0 +1,118 @@ +package compbio.ws.client; +import static compbio.ws.client.Constraints.hostkey; +import static compbio.ws.client.Constraints.limitList; +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; + +class CmdHelper { + + /** + * Check whether presetList is set in the command line + * + * @param cmd + * command line options + * @return true if presetList is found, false otherwise + */ + static boolean listPresets(String[] cmd) { + return keyFound(cmd, presetList); + } + + /** + * Checks whether limitList parameter is in the command line + * + * @param cmd + * - command line options + * @return true if it is, false otherwise + */ + static boolean listLimits(String[] cmd) { + return keyFound(cmd, limitList); + } + + /** + * Checks whether the key is in the command line + * + * @param cmd + * @param key + * @return true if it is, false otherwise + */ + static boolean keyFound(String[] cmd, String key) { + assert cmd != null && cmd.length > 0; + assert key != null; + for (int i = 0; i < cmd.length; i++) { + String listPresets = cmd[i]; + if (listPresets.trim().equalsIgnoreCase(key)) { + return true; + } + } + return false; + } + + /** + * Extracts preset name from the command line is any + * + * @param cmd + * command line options + * @return presetName or null if no presets is defined + */ + static String getPresetName(String[] cmd) { + String preset = null; + for (int i = 0; i < cmd.length; i++) { + String presetPrm = cmd[i]; + if (presetPrm.trim().toLowerCase() + .startsWith(presetkey + pseparator)) { + preset = presetPrm.substring(presetPrm.indexOf(pseparator) + 1); + break; + } + } + return preset; + } + + /** + * Extracts service name from the command line + * + * @param cmd + * command line options + * @return service name or null if it is not defined + */ + public static String getServiceName(String[] cmd) { + for (int i = 0; i < cmd.length; i++) { + String serv = cmd[i]; + if (serv.trim().toLowerCase().startsWith(servicekey + pseparator)) { + return serv.substring(serv.indexOf(pseparator) + 1); + } + } + return null; + } + + /** + * Extracts host name from the command line + * + * @param cmd + * command line options + * @return host name or null if it is not defined + */ + public static String getHost(String[] cmd) { + for (int i = 0; i < cmd.length; i++) { + String host = cmd[i]; + if (host.trim().toLowerCase().startsWith(hostkey + pseparator)) { + return host.substring(host.indexOf(pseparator) + 1); + } + } + return null; + } + + /** + * Searches the command line keys in the array of parameters + * + * @param cmd + * command line options + * @return true is the list of Parameters is requested, false otherwise + */ + static boolean listParameters(String[] cmd) { + return keyFound(cmd, paramList); + } + +} diff --git a/webservices/compbio/ws/client/Constraints.java b/webservices/compbio/ws/client/Constraints.java new file mode 100644 index 0000000..08b06ac --- /dev/null +++ b/webservices/compbio/ws/client/Constraints.java @@ -0,0 +1,24 @@ +package compbio.ws.client; + +class Constraints { + + final static String pseparator = "="; + + // Parameters for required command line options + final static String hostkey = "-h"; + final static String servicekey = "-s"; + + // Actions + final static String inputkey = "-i"; + + final static String paramList = "-parameters"; + final static String presetList = "-presets"; + final static String limitList = "-limits"; + + // Options + final static String paramFile = "-f"; + final static String outputkey = "-o"; + final static String parameterkey = "-p"; + final static String presetkey = "-r"; + +} diff --git a/webservices/compbio/ws/client/IOHelper.java b/webservices/compbio/ws/client/IOHelper.java new file mode 100644 index 0000000..af9a52c --- /dev/null +++ b/webservices/compbio/ws/client/IOHelper.java @@ -0,0 +1,112 @@ +package compbio.ws.client; + +import static compbio.ws.client.Constraints.pseparator; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; + +import compbio.data.sequence.Alignment; +import compbio.data.sequence.ClustalAlignmentUtil; + +public class IOHelper { + + /** + * Checks -i options and return the File if one was provided, null otherwise + * + * @param cmd + * @param key + * @param mustExist + * @return + * @throws IOException + */ + static File getFile(String[] cmd, String key, boolean mustExist) + throws IOException { + assert key != null && key.trim().length() != 0; + for (int i = 0; i < cmd.length; i++) { + String filename = cmd[i]; + filename = filename.trim(); + if (filename.toLowerCase().startsWith(key + pseparator)) { + filename = filename.substring((key + pseparator).length()); + File file = new File(filename); + if (mustExist && !file.exists()) { + System.out.println(key + " file " + file.getAbsolutePath() + + " does not exist"); + return null; + } + if (!mustExist && !file.exists()) { + file.createNewFile(); + } + if (!file.canRead()) { + System.out.println("Cannot read " + key + " file " + + file.getAbsolutePath()); + return null; + } + return file; + } + } + return null; + } + + /** + * Load parameters from file + * + * @throws IOException + */ + static List loadParameters(File paramsfile) throws IOException { + assert paramsfile != null && paramsfile.exists(); + BufferedReader reader = new BufferedReader(new FileReader(paramsfile)); + String line = null; + ArrayList params = new ArrayList(); + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (line.length() == 0) + continue; + params.add(line); + } + return params; + } + + static OutputStream getOutStream(File file) { + assert file != null && file.exists(); + try { + return new FileOutputStream(file); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + return null; + } + + /** + * Outputs clustal formatted alignment into the file represented by the + * outStream + * + * @param outStream + * @param align + * the alignment to output + */ + static void writeOut(OutputStream outStream, Alignment align) { + try { + ClustalAlignmentUtil.writeClustalAlignment(outStream, align); + } catch (IOException e) { + System.err + .println("Problems writing output file! Stack trace is below: "); + e.printStackTrace(); + } finally { + if (outStream != null) { + try { + outStream.close(); + } catch (IOException ignored) { + // e.printStackTrace(); + } + } + } + } + +} diff --git a/webservices/compbio/ws/client/Jws2Client.java b/webservices/compbio/ws/client/Jws2Client.java index 152646d..5c7a879 100644 --- a/webservices/compbio/ws/client/Jws2Client.java +++ b/webservices/compbio/ws/client/Jws2Client.java @@ -18,40 +18,42 @@ package compbio.ws.client; -import java.io.BufferedReader; +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.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; -import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.WebServiceException; +import compbio.data.msa.JABAService; +import compbio.data.msa.Metadata; import compbio.data.msa.MsaWS; import compbio.data.sequence.Alignment; -import compbio.data.sequence.ClustalAlignmentUtil; import compbio.data.sequence.FastaSequence; import compbio.data.sequence.SequenceUtil; import compbio.metadata.JobSubmissionException; -import compbio.metadata.Limit; -import compbio.metadata.LimitsManager; import compbio.metadata.Option; import compbio.metadata.Preset; -import compbio.metadata.PresetManager; import compbio.metadata.ResultNotAvailableException; -import compbio.metadata.RunnerConfig; import compbio.metadata.WrongParameterException; /** @@ -62,659 +64,344 @@ import compbio.metadata.WrongParameterException; */ public class Jws2Client { - /* - * 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()); - - final static String pseparator = "="; - - // Parameters for required command line options - final static String hostkey = "-h"; - final static String servicekey = "-s"; - - // Actions - final static String inputkey = "-i"; - - final static String paramList = "-parameters"; - final static String presetList = "-presets"; - final static String limitList = "-limits"; - - // Options - final static String paramFile = "-f"; - final static String outputkey = "-o"; - final static String parameterkey = "-p"; - final static String presetkey = "-r"; - - //JABAWS version 1.0 service name - static final String qualifiedServiceName = "http://msa.data.compbio/01/01/2010/"; - - /** - * Searches the command line keys in the array of parameters - * - * @param cmd - * command line options - * @return true is the list of Parameters is requested, false otherwise - */ - private boolean listParameters(String[] cmd) { - return keyFound(cmd, paramList); - } - - /** - * Check whether presetList is set in the command line - * - * @param cmd - * command line options - * @return true if presetList is found, false otherwise - */ - boolean listPresets(String[] cmd) { - return keyFound(cmd, presetList); - } - - /** - * Returns {@code Preset} by its name - * - * @see Preset - * @param - * @param msaws - * @param presetName - * @return Return a Preset by its optionName - */ - Preset getPreset(MsaWS msaws, String presetName) { - assert presetName != null; - PresetManager presets = getPresetList(msaws); - if (presets == null) { - System.out - .println("No presets are supported by the service! Ignoring -r directive!"); - return null; - } - Preset pre = presets.getPresetByName(presetName); - if (pre == null) { - System.out.println("Cannot find preset: " + presetName - + " WARN: ignoring -r directive!"); - } - return pre; - } - - /** - * Extracts preset name from the command line is any - * - * @param cmd - * command line options - * @return presetName or null if no presets is defined - */ - String getPresetName(String[] cmd) { - String preset = null; - for (int i = 0; i < cmd.length; i++) { - String presetPrm = cmd[i]; - if (presetPrm.trim().toLowerCase().startsWith( - presetkey + pseparator)) { - preset = presetPrm.substring(presetPrm.indexOf(pseparator) + 1); - break; - } - } - return preset; - } - - /** - * Checks whether limitList parameter is in the command line - * - * @param cmd - * - command line options - * @return true if it is, false otherwise - */ - boolean listLimits(String[] cmd) { - return keyFound(cmd, limitList); - } - - /** - * Checks whether the key is in the command line - * - * @param cmd - * @param key - * @return true if it is, false otherwise - */ - boolean keyFound(String[] cmd, String key) { - assert cmd != null && cmd.length > 0; - assert key != null; - for (int i = 0; i < cmd.length; i++) { - String listPresets = cmd[i]; - if (listPresets.trim().equalsIgnoreCase(key)) { + /* + * 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 + * + * @param urlstr + * @return true if it succeed false otherwise + */ + public static boolean validURL(String urlstr) { + try { + if (urlstr == null || urlstr.trim().length() == 0) { + return false; + } + new URL(urlstr); + } catch (MalformedURLException e) { + return false; + } return true; - } - } - return false; - } - - /** - * Attempt to construct the URL object from the string - * - * @param urlstr - * @return true if it succeed false otherwise - */ - public static boolean validURL(String urlstr) { - try { - if (urlstr == null || urlstr.trim().length() == 0) { - return false; - } - new URL(urlstr); - } catch (MalformedURLException e) { - return false; } - return true; - } - /** - * Extracts service name from the command line - * - * @param cmd - * command line options - * @return service name or null if it is not defined - */ - public static String getServiceName(String[] cmd) { - for (int i = 0; i < cmd.length; i++) { - String serv = cmd[i]; - if (serv.trim().toLowerCase().startsWith(servicekey + pseparator)) { - return serv.substring(serv.indexOf(pseparator) + 1); - } - } - return null; - } - - /** - * Extracts host name from the command line - * - * @param cmd - * command line options - * @return host name or null if it is not defined - */ - public static String getHost(String[] cmd) { - for (int i = 0; i < cmd.length; i++) { - String host = cmd[i]; - if (host.trim().toLowerCase().startsWith(hostkey + pseparator)) { - return host.substring(host.indexOf(pseparator) + 1); - } - } - return null; - } + /** + * Connects to the service and do the job as requested, if something goes + * wrong reports or/and prints usage help. + * + * @param + * web service type + * @param cmd + * command line options + * @throws IOException + */ + Jws2Client(String[] cmd) throws IOException { + + String hostname = CmdHelper.getHost(cmd); + if (hostname == null) { + System.out.println("Host name is not provided!"); + printUsage(1); + } - /** - * Checks -i options and return the File if one was provided, null otherwise - * - * @param cmd - * @param key - * @param mustExist - * @return - * @throws IOException - */ - File getFile(String[] cmd, String key, boolean mustExist) - throws IOException { - assert key != null && key.trim().length() != 0; - for (int i = 0; i < cmd.length; i++) { - String filename = cmd[i]; - filename = filename.trim(); - if (filename.toLowerCase().startsWith(key + pseparator)) { - filename = filename.substring((key + pseparator).length()); - File file = new File(filename); - if (mustExist && !file.exists()) { - System.out.println(key + " file " + file.getAbsolutePath() - + " does not exist"); - return null; + if (!validURL(hostname)) { + System.out.println("Host name is not valid!"); + printUsage(1); } - if (!mustExist && !file.exists()) { - file.createNewFile(); + String serviceName = CmdHelper.getServiceName(cmd); + if (serviceName == null) { + System.out.println("Service name is no provided!"); + printUsage(1); } - if (!file.canRead()) { - System.out.println("Cannot read " + key + " file " - + file.getAbsolutePath()); - return null; + 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); + } + 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); + Preset preset = null; + if (presetName != null) { + preset = MetadataHelper.getPreset(msaws, presetName); + } + List> customOptions = null; + if (parametersFile != null) { + List prms = IOHelper.loadParameters(parametersFile); + customOptions = MetadataHelper.processParameters(prms, + msaws.getRunnerOptions()); + } + Alignment alignment = null; + if (inputFile != null) { + if (service == Services.AAConWS) { + System.out.println("calc conserv!"); + } else { + + alignment = align(inputFile, (MsaWS) msaws, preset, + customOptions); + OutputStream outStream = null; + if (outFile != null) { + outStream = IOHelper.getOutStream(outFile); + } else { + // this stream is going to be closed later which is fine as + // std.out will not be + outStream = System.out; + } + IOHelper.writeOut(outStream, alignment); + // stream is closed in the method no need to close it here + } } - return file; - } - } - return null; - } - - /** - * Connects to the service and do the job as requested, if something goes - * wrong reports or/and prints usage help. - * - * @param - * web service type - * @param cmd - * command line options - * @throws IOException - */ - Jws2Client(String[] cmd) throws IOException { - - String hostname = getHost(cmd); - if (hostname == null) { - System.out.println("Host name is not provided!"); - printUsage(1); - } - if (!validURL(hostname)) { - System.out.println("Host name is not valid!"); - printUsage(1); - } - String serviceName = getServiceName(cmd); - if (serviceName == null) { - System.out.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: " - + Arrays.toString(Services.values())); - printUsage(1); + boolean listParameters = CmdHelper.listParameters(cmd); + if (listParameters) { + System.out.println(MetadataHelper.getParametersList(msaws)); + } + boolean listPreset = CmdHelper.listPresets(cmd); + if (listPreset) { + System.out.println(MetadataHelper.getPresetList(msaws)); + } + boolean listLimits = CmdHelper.listLimits(cmd); + if (listLimits) { + System.out.println(MetadataHelper.getLimits(msaws)); + } + log.fine("Disconnecting..."); + ((Closeable) msaws).close(); + log.fine("Disconnected successfully!"); } - File inputFile = getFile(cmd, inputkey, true); - File outFile = getFile(cmd, outputkey, false); - File parametersFile = getFile(cmd, paramFile, true); - String presetName = getPresetName(cmd); - MsaWS msaws = connect(hostname, service); - Preset preset = null; - if (presetName != null) { - preset = getPreset(msaws, presetName); - } - List> customOptions = null; - if (parametersFile != null) { - List prms = loadParameters(parametersFile); - customOptions = processParameters(prms, msaws.getRunnerOptions()); - } - Alignment alignment = null; - if (inputFile != null) { - alignment = align(inputFile, msaws, preset, customOptions); - OutputStream outStream = null; - if (outFile != null) { - outStream = getOutStream(outFile); - } else { - // this stream is going to be closed later which is fine as - // std.out will not be - outStream = System.out; - } - writeOut(outStream, alignment); - // stream is closed in the method no need to close it here - } + /** + * Connects to a web service by the host and the service name + * + * @param + * 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("Conecting to JABAWS version 2 service"); + if (isV2service(wse)) { + serv = service.getService(url, V2_QUALIFIED_SERVICE_NAME); + } + } + JABAService serviceIF = service.getInterface(serv); + log.log(Level.FINE, "Connected successfully!"); - boolean listParameters = listParameters(cmd); - if (listParameters) { - System.out.println(getParametersList(msaws)); - } - boolean listPreset = listPresets(cmd); - if (listPreset) { - System.out.println(getPresetList(msaws)); - } - boolean listLimits = listLimits(cmd); - if (listLimits) { - System.out.println(getLimits(msaws)); + return serviceIF; } - log.fine("Disconnecting..."); - ((Closeable) msaws).close(); - log.fine("Disconnected successfully!"); - } - /** - * Load parameters from file - * - * @throws IOException - */ - List loadParameters(File paramsfile) throws IOException { - assert paramsfile != null && paramsfile.exists(); - BufferedReader reader = new BufferedReader(new FileReader(paramsfile)); - String line = null; - ArrayList params = new ArrayList(); - while ((line = reader.readLine()) != null) { - line = line.trim(); - if (line.length() == 0) - continue; - params.add(line); + 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; } - return params; - } - /** - * Converts options supplied via parameters file into {@code Option} objects - * - * @param - * web service type - * @param params - * @param options - * @return List of Options of type T - */ - List> processParameters(List params, - RunnerConfig options) { - List> chosenOptions = new ArrayList>(); - for (String param : params) { - String oname = null; - if (isParameter(param)) { - oname = this.getParamName(param); - } else { - oname = param; - } - Option o = options.getArgumentByOptionName(oname); - if (o == null) { - System.out.println("WARN ignoring unsuppoted parameter: " - + oname); - continue; - } - if (isParameter(param)) { + /** + * Align sequences from the file using MsaWS + * + * @param + * web service type e.g. Clustal + * @param file + * to write the resulting alignment to + * @param msaws + * MsaWS required + * @param preset + * Preset to use optional + * @param customOptions + * file which contains new line separated list of options + * @return Alignment + */ + static Alignment align(File file, MsaWS msaws, Preset preset, + List> customOptions) { + FileInputStream instream = null; + List fastalist = null; + Alignment alignment = null; try { - o.setValue(getParamValue(param)); + instream = new FileInputStream(file); + 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!"); + } + if (customOptions != null) { + jobId = msaws.customAlign(fastalist, customOptions); + } else if (preset != null) { + jobId = msaws.presetAlign(fastalist, preset); + } else { + jobId = msaws.align(fastalist); + } + Thread.sleep(1000); + 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:"); + e.printStackTrace(); + } catch (JobSubmissionException e) { + 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:"); + e.printStackTrace(); + } catch (InterruptedException ignored) { + // ignore and propagate an interruption + Thread.currentThread().interrupt(); } catch (WrongParameterException e) { - System.out - .println("Problem setting value for the parameter: " - + param); - e.printStackTrace(); + e.printStackTrace(); + } finally { + if (instream != null) { + try { + instream.close(); + } catch (IOException ignored) { + // ignore + } + } } - } - chosenOptions.add(o); + return alignment; } - return chosenOptions; - } - String getParamName(String fullName) { - assert isParameter(fullName); - return fullName.substring(0, fullName.indexOf(pseparator)); - } + /** + * 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"); - String getParamValue(String fullName) { - assert isParameter(fullName); - return fullName.substring(fullName.indexOf(pseparator) + 1); - } + System.out.println(); + System.out.println("OPTIONS (only for use with -i action):"); - boolean isParameter(String param) { - return param.contains(pseparator); - } + 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!"); - OutputStream getOutStream(File file) { - assert file != null && file.exists(); - try { - return new FileOutputStream(file); - } catch (FileNotFoundException e) { - e.printStackTrace(); + System.exit(exitStatus); } - return null; - } - /** - * Outputs clustal formatted alignment into the file represented by the - * outStream - * - * @param outStream - * @param align - * the alignment to output - */ - void writeOut(OutputStream outStream, Alignment align) { - try { - ClustalAlignmentUtil.writeClustalAlignment(outStream, align); - } catch (IOException e) { - System.err - .println("Problems writing output file! Stack trace is below: "); - e.printStackTrace(); - } finally { - if (outStream != null) { - try { - outStream.close(); - } catch (IOException ignored) { - // e.printStackTrace(); + /** + * Starts command line client, if no parameter are supported print help. Two + * parameters are required for successfull call the JWS2 host name and a + * service name. + * + * @param args + * Usage: -h=host_and_context + * -s=serviceName ACTION [OPTIONS] + * + * -h= - a full URL to the JWS2 web server + * including context path e.g. http://10.31.1.159:8080/ws + * + * -s= - one of [MafftWS, MuscleWS, ClustalWS, + * TcoffeeWS, ProbconsWS] ACTIONS: + * + * -i= - full path to fasta formatted sequence file, + * from which to align sequences + * + * -parameters - lists parameters supported by web service + * + * -presets - lists presets supported by web service + * + * -limits - lists web services limits Please note that if input + * file is specified other actions are ignored + * + * OPTIONS: (only for use with -i action): + * + * -r= - name of the preset to use + * + * -o= - full path to the file where to write an + * alignment -f= - the name of the file with + * the list of parameters to use. 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! + * + */ + public static void main(String[] args) { + + if (args == null) { + printUsage(1); + } + if (args.length < 2) { + System.out.println("Host and service names are required!"); + printUsage(1); } - } - } - } - - /** - * Connects to a web service by the host and the service name - * - * @param - * web service type - * @param host - * @param service - * @return MsaWS - * @throws WebServiceException - */ - public static MsaWS connect(String host, Services service) - throws WebServiceException { - URL url = null; - log.log(Level.FINE, "Attempt to connect..."); - try { - url = new URL(host + "/" + service.toString() + "?wsdl"); - } catch (MalformedURLException e) { - e.printStackTrace(); - // ignore as the host name is already verified - } - QName qname = new QName(qualifiedServiceName, service.toString()); - Service serv = Service.create(url, qname); - MsaWS msaws = serv.getPort(new QName(qualifiedServiceName, service - + "Port"), MsaWS.class); - log.log(Level.FINE, "Connected successfully!"); - return msaws; - } - - /** - * Align sequences from the file using MsaWS - * - * @param - * web service type e.g. Clustal - * @param file - * to write the resulting alignment to - * @param msaws - * MsaWS required - * @param preset - * Preset to use optional - * @param customOptions - * file which contains new line separated list of options - * @return Alignment - */ - 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); - 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 = msaws.customAlign(fastalist, customOptions); - } else if (preset != null) { - jobId = msaws.presetAlign(fastalist, preset); - } else { - jobId = msaws.align(fastalist); - } - Thread.sleep(1000); - 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:"); - e.printStackTrace(); - } catch (JobSubmissionException e) { - 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:"); - 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 + new Jws2Client(args); + } catch (IOException e) { + log.log(Level.SEVERE, "IOException in client! " + e.getMessage(), + e.getCause()); + System.err.println("Cannot write output file! Stack trace: "); + e.printStackTrace(); } - } - } - return alignment; - } - - /** - * Returns a list of options supported by web service - * - * @param - * web service type - * @param msaws - * web service proxy - * @return List of options supported by a web service - */ - List> getParametersList(MsaWS msaws) { - assert msaws != null; - return msaws.getRunnerOptions().getArguments(); - } - - /** - * Returns an objects from which the list of presets supported by web - * service can be obtained - * - * @param - * web service type - * @param msaws - * web service proxy - * @return PresetManager, object which operates on presets - */ - PresetManager getPresetList(MsaWS msaws) { - assert msaws != null; - PresetManager presetman = msaws.getPresets(); - return presetman; - } - - /** - * Returns a list of limits supported by web service Each limit correspond - * to a particular preset. - * - * @param - * web service type - * @param msaws - * web service proxy - * @return List of limits supported by a web service - */ - List> getLimits(MsaWS msaws) { - assert msaws != null; - LimitsManager lmanger = msaws.getLimits(); - - return lmanger != null ? lmanger.getLimits() : null; - } - - /** - * 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.exit(exitStatus); - } - - /** - * Starts command line client, if no parameter are supported print help. Two - * parameters are required for successfull call the JWS2 host name and a - * service name. - * - * @param args - * Usage: -h=host_and_context - * -s=serviceName ACTION [OPTIONS] - * - * -h= - a full URL to the JWS2 web server - * including context path e.g. http://10.31.1.159:8080/ws - * - * -s= - one of [MafftWS, MuscleWS, ClustalWS, - * TcoffeeWS, ProbconsWS] ACTIONS: - * - * -i= - full path to fasta formatted sequence file, - * from which to align sequences - * - * -parameters - lists parameters supported by web service - * - * -presets - lists presets supported by web service - * - * -limits - lists web services limits Please note that if input - * file is specified other actions are ignored - * - * OPTIONS: (only for use with -i action): - * - * -r= - name of the preset to use - * - * -o= - full path to the file where to write an - * alignment -f= - the name of the file with - * the list of parameters to use. 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! - * - */ - public static void main(String[] args) { - - if (args == null) { - printUsage(1); - } - if (args.length < 2) { - System.out.println("Host and service names are required!"); - printUsage(1); - } - - try { - new Jws2Client(args); - } catch (IOException e) { - log.log(Level.SEVERE, "IOException in client! " + e.getMessage(), e - .getCause()); - System.err.println("Cannot write output file! Stack trace: "); - e.printStackTrace(); } - } } diff --git a/webservices/compbio/ws/client/MetadataHelper.java b/webservices/compbio/ws/client/MetadataHelper.java new file mode 100644 index 0000000..d69c3c1 --- /dev/null +++ b/webservices/compbio/ws/client/MetadataHelper.java @@ -0,0 +1,145 @@ +package compbio.ws.client; + +import static compbio.ws.client.Constraints.pseparator; + +import java.util.ArrayList; +import java.util.List; + +import compbio.data.msa.Metadata; +import compbio.metadata.Limit; +import compbio.metadata.LimitsManager; +import compbio.metadata.Option; +import compbio.metadata.Preset; +import compbio.metadata.PresetManager; +import compbio.metadata.RunnerConfig; +import compbio.metadata.WrongParameterException; + +public class MetadataHelper { + + /** + * Returns a list of options supported by web service + * + * @param + * web service type + * @param msaws + * web service proxy + * @return List of options supported by a web service + */ + static List> getParametersList(Metadata msaws) { + assert msaws != null; + return msaws.getRunnerOptions().getArguments(); + } + + /** + * Returns an objects from which the list of presets supported by web + * service can be obtained + * + * @param + * web service type + * @param msaws + * web service proxy + * @return PresetManager, object which operates on presets + */ + static PresetManager getPresetList(Metadata msaws) { + assert msaws != null; + PresetManager presetman = msaws.getPresets(); + return presetman; + } + + /** + * Returns a list of limits supported by web service Each limit correspond + * to a particular preset. + * + * @param + * web service type + * @param msaws + * web service proxy + * @return List of limits supported by a web service + */ + static List> getLimits(Metadata msaws) { + assert msaws != null; + LimitsManager lmanger = msaws.getLimits(); + + return lmanger != null ? lmanger.getLimits() : null; + } + + /** + * Returns {@code Preset} by its name + * + * @see Preset + * @param + * @param msaws + * @param presetName + * @return Return a Preset by its optionName + */ + static Preset getPreset(Metadata msaws, String presetName) { + assert presetName != null; + PresetManager presets = MetadataHelper.getPresetList(msaws); + if (presets == null) { + System.out + .println("No presets are supported by the service! Ignoring -r directive!"); + return null; + } + Preset pre = presets.getPresetByName(presetName); + if (pre == null) { + System.out.println("Cannot find preset: " + presetName + + " WARN: ignoring -r directive!"); + } + return pre; + } + + /** + * Converts options supplied via parameters file into {@code Option} objects + * + * @param + * web service type + * @param params + * @param options + * @return List of Options of type T + */ + static List> processParameters(List params, + RunnerConfig options) { + List> chosenOptions = new ArrayList>(); + for (String param : params) { + String oname = null; + if (isParameter(param)) { + oname = getParamName(param); + } else { + oname = param; + } + Option o = options.getArgumentByOptionName(oname); + if (o == null) { + System.out.println("WARN ignoring unsuppoted parameter: " + + oname); + continue; + } + if (isParameter(param)) { + try { + o.setValue(getParamValue(param)); + } catch (WrongParameterException e) { + System.out + .println("Problem setting value for the parameter: " + + param); + e.printStackTrace(); + } + } + chosenOptions.add(o); + } + return chosenOptions; + } + + static String getParamName(String fullName) { + assert isParameter(fullName); + return fullName.substring(0, fullName.indexOf(pseparator)); + } + + static String getParamValue(String fullName) { + assert isParameter(fullName); + return fullName.substring(fullName.indexOf(pseparator) + 1); + } + + static boolean isParameter(String param) { + return param.contains(pseparator); + } + +} diff --git a/webservices/compbio/ws/client/Services.java b/webservices/compbio/ws/client/Services.java index 21b8bbc..95a0a89 100644 --- a/webservices/compbio/ws/client/Services.java +++ b/webservices/compbio/ws/client/Services.java @@ -18,35 +18,74 @@ package compbio.ws.client; +import java.net.URL; + +import javax.xml.namespace.QName; +import javax.xml.ws.Service; + +import compbio.data.msa.Annotation; +import compbio.data.msa.JABAService; +import compbio.data.msa.MsaWS; + /** - * List of web services currently supported by JABAWS version 1 + * List of web services currently supported by JABAWS version 2 * */ public enum Services { - MafftWS, MuscleWS, ClustalWS, TcoffeeWS, ProbconsWS; + MafftWS, MuscleWS, ClustalWS, TcoffeeWS, ProbconsWS, AAConWS; - public static Services getService(String servName) { - servName = servName.trim().toLowerCase(); - if (servName.equalsIgnoreCase(MafftWS.toString())) { - return MafftWS; - } - if (servName.equalsIgnoreCase(ClustalWS.toString())) { - return ClustalWS; + public static Services getService(String servName) { + servName = servName.trim().toLowerCase(); + if (servName.equalsIgnoreCase(MafftWS.toString())) { + return MafftWS; + } + if (servName.equalsIgnoreCase(ClustalWS.toString())) { + return ClustalWS; + } + if (servName.equalsIgnoreCase(TcoffeeWS.toString())) { + return TcoffeeWS; + } + if (servName.equalsIgnoreCase(MuscleWS.toString())) { + return MuscleWS; + } + if (servName.equalsIgnoreCase(ProbconsWS.toString())) { + return ProbconsWS; + } + if (servName.equalsIgnoreCase(AAConWS.toString())) { + return AAConWS; + } + return null; } - if (servName.equalsIgnoreCase(TcoffeeWS.toString())) { - return TcoffeeWS; - } - if (servName.equalsIgnoreCase(MuscleWS.toString())) { - return MuscleWS; - } - if (servName.equalsIgnoreCase(ProbconsWS.toString())) { - return ProbconsWS; + + Service getService(URL url, String sqname) { + QName qname = new QName(sqname, this.toString()); + return Service.create(url, qname); } - return null; - } - public static Services getService(Class wsImplClass) { - return getService(wsImplClass.getSimpleName()); - } + JABAService getInterface(Service service) { + assert service != null; + QName portName = new QName(service.getServiceName().getNamespaceURI(), + this.toString() + "Port"); + + switch (this) { + case AAConWS : + + return service.getPort(portName, Annotation.class); + + // deliberate leaking + case ClustalWS : + case MafftWS : + case MuscleWS : + case ProbconsWS : + case TcoffeeWS : + // TODO remove + System.out.println("Qname from serv: " + portName); + + return service.getPort(portName, MsaWS.class); + + default : + throw new RuntimeException("Should never happened!"); + } + } } \ No newline at end of file diff --git a/webservices/compbio/ws/client/WSTester.java b/webservices/compbio/ws/client/WSTester.java index ddd6742..544ecad 100644 --- a/webservices/compbio/ws/client/WSTester.java +++ b/webservices/compbio/ws/client/WSTester.java @@ -18,6 +18,10 @@ package compbio.ws.client; +import static compbio.ws.client.Constraints.hostkey; +import static compbio.ws.client.Constraints.pseparator; +import static compbio.ws.client.Constraints.servicekey; + import java.io.ByteArrayInputStream; import java.io.Closeable; import java.io.IOException; @@ -85,18 +89,17 @@ public class WSTester { * Prints usage */ static void printUsage() { - System.out.println("Usage: " - + Jws2Client.hostkey + Jws2Client.pseparator - + "host_and_context " + "<" + Jws2Client.servicekey - + Jws2Client.pseparator + "serviceName>"); + System.out.println("Usage: " + hostkey + + pseparator + "host_and_context " + "<" + servicekey + + pseparator + "serviceName>"); System.out.println(); System.out - .println(Jws2Client.hostkey - + Jws2Client.pseparator + .println(hostkey + + pseparator + " - a full URL to the JABAWS web server including context path e.g. http://10.31.1.159:8080/ws"); System.out - .println(Jws2Client.servicekey - + Jws2Client.pseparator + .println(servicekey + + pseparator + " - optional if unspecified all services are tested otherwise one of " + Arrays.toString(Services.values())); System.out.println(); @@ -270,7 +273,7 @@ public class WSTester { try { System.out.print("Connecting to service " + service + " on " + host + " ... "); - msaws = Jws2Client.connect(host, service); + msaws = (MsaWS) Jws2Client.connect(host, service); System.out.println(OK); } catch (WebServiceException e) { System.out.println(FAILED); @@ -297,8 +300,8 @@ public class WSTester { printUsage(); System.exit(0); } - String host = Jws2Client.getHost(args); - String serviceName = Jws2Client.getServiceName(args); + String host = CmdHelper.getHost(args); + String serviceName = CmdHelper.getServiceName(args); if (!Jws2Client.validURL(host)) { System.out .println(" parameter is not provided or is incorrect!"); diff --git a/webservices/compbio/ws/server/AAConWS.java b/webservices/compbio/ws/server/AAConWS.java index 27b85f3..e77c878 100644 --- a/webservices/compbio/ws/server/AAConWS.java +++ b/webservices/compbio/ws/server/AAConWS.java @@ -65,7 +65,7 @@ public class AAConWS implements Annotation { ConfiguredExecutable aacon = (ConfiguredExecutable) asyncEngine .getResults(jobId); HashSet mas = aacon.getResults(); - log.info(jobId + " getConservation : " + mas); + log.trace(jobId + " getConservation : " + mas); return mas; } /*