From: pvtroshin Date: Thu, 23 Dec 2010 12:28:47 +0000 (+0000) Subject: AAConWS separate client to be removed later X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=6695d8f2418a044b900e9a23dfc3bd04e0f7c222;p=jabaws.git AAConWS separate client to be removed later git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@3565 e3abac25-378b-4346-85de-24260fe3988d --- diff --git a/build.xml b/build.xml index 05137d6..5aab7b1 100644 --- a/build.xml +++ b/build.xml @@ -10,6 +10,7 @@ + @@ -190,6 +191,27 @@ + + + Jar file: AAConWS client jar + + + + + + + + + + + + + + + + + + diff --git a/webservices/compbio/data/msa/jaxws/GetAnnotation.java b/webservices/compbio/data/msa/jaxws/GetAnnotation.java new file mode 100644 index 0000000..e938ec9 --- /dev/null +++ b/webservices/compbio/data/msa/jaxws/GetAnnotation.java @@ -0,0 +1,36 @@ + +package compbio.data.msa.jaxws; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + +@XmlRootElement(name = "getAnnotation", namespace = "http://msa.data.compbio/01/12/2010/") +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "getAnnotation", namespace = "http://msa.data.compbio/01/12/2010/") +public class GetAnnotation { + + @XmlElement(name = "jobId", namespace = "") + private String jobId; + + /** + * + * @return + * returns String + */ + public String getJobId() { + return this.jobId; + } + + /** + * + * @param jobId + * the value for the jobId property + */ + public void setJobId(String jobId) { + this.jobId = jobId; + } + +} diff --git a/webservices/compbio/data/msa/jaxws/GetAnnotationResponse.java b/webservices/compbio/data/msa/jaxws/GetAnnotationResponse.java new file mode 100644 index 0000000..c9d0d5b --- /dev/null +++ b/webservices/compbio/data/msa/jaxws/GetAnnotationResponse.java @@ -0,0 +1,37 @@ + +package compbio.data.msa.jaxws; + +import java.util.HashSet; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + +@XmlRootElement(name = "getAnnotationResponse", namespace = "http://msa.data.compbio/01/12/2010/") +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "getAnnotationResponse", namespace = "http://msa.data.compbio/01/12/2010/") +public class GetAnnotationResponse { + + @XmlElement(name = "return", namespace = "") + private HashSet _return; + + /** + * + * @return + * returns HashSet + */ + public HashSet getReturn() { + return this._return; + } + + /** + * + * @param _return + * the value for the _return property + */ + public void setReturn(HashSet _return) { + this._return = _return; + } + +} diff --git a/webservices/compbio/ws/client/AAConClient.java b/webservices/compbio/ws/client/AAConClient.java new file mode 100644 index 0000000..5fa373d --- /dev/null +++ b/webservices/compbio/ws/client/AAConClient.java @@ -0,0 +1,385 @@ +/* Copyright (c) 2009 Peter Troshin + * + * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.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 + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache + * License for more details. + * + * A copy of the license is in apache_license.txt. It is also available here: + * @see: http://www.apache.org/licenses/LICENSE-2.0.txt + * + * Any republication or derived work distributed in source code form + * must include this copyright and license notice. + */ + +package compbio.ws.client; + +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 java.io.Closeable; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +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.Annotation; +import compbio.data.sequence.FastaSequence; +import compbio.data.sequence.Score; +import compbio.data.sequence.SequenceUtil; +import compbio.metadata.JobSubmissionException; +import compbio.metadata.Option; +import compbio.metadata.Preset; +import compbio.metadata.ResultNotAvailableException; +import compbio.metadata.WrongParameterException; +import compbio.ws.server.AAConWS; + +/** + * A command line client for AACon web service + * + * @author pvtroshin + * @version 1.0 + */ +public class AAConClient { + + /* + * Use java.util.Logger instead of log4j logger to reduce the size of the + * client package + */ + private static final Logger log = Logger.getLogger(AAConClient.class + .getCanonicalName()); + + static final String QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/12/2010/"; + + // static final String hostname = "http://www.compbio.dundee.ac.uk/aacon"; + static final String hostname = "http://localhost:8080/jabaws"; + + static final String serviceName = "AAConWS"; + + /** + * 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; + } + + /** + * 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 + */ + @SuppressWarnings("unchecked") + AAConClient(String[] cmd) throws IOException { + + 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); + + Annotation msaws = 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()); + } + Set result = null; + if (inputFile != null) { + System.out.println("calc conserv!"); + result = analize(inputFile, 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; + } + writeOut(outStream, result); + // stream is closed in the method no need to close it here + } + + 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!"); + } + + /** + * Outputs clustal formatted alignment into the file represented by the + * outStream + * + * @param outStream + * @param align + * the alignment to output + */ + static void writeOut(OutputStream outStream, Set result) { + try { + Score.write(result, outStream); + } 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(); + } + } + } + } + + /** + * 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 Annotation 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 = service.getService(url, QUALIFIED_SERVICE_NAME); + @SuppressWarnings("unchecked") + QName portName = new QName(QUALIFIED_SERVICE_NAME, "AAConWS" + "Port"); + + Annotation serviceIF = serv + .getPort(portName, Annotation.class); + + log.log(Level.FINE, "Connected successfully!"); + return serviceIF; + } + + /** + * Align sequences from the file using MsaWS + * + * @param + * web service type e.g. Clustal + * @param file + * to write the resulting alignment to + * @param wsproxy + * MsaWS required + * @param preset + * Preset to use optional + * @param customOptions + * file which contains new line separated list of options + * @return Alignment + */ + static Set analize(File file, Annotation wsproxy, + Preset preset, List> customOptions) { + FileInputStream instream = null; + List fastalist = null; + Set scores = 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 = wsproxy.customAnalize(fastalist, customOptions); + } else if (preset != null) { + jobId = wsproxy.presetAnalize(fastalist, preset); + } else { + jobId = wsproxy.analize(fastalist); + } + Thread.sleep(1000); + scores = wsproxy.getAnnotation(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 + } + } + } + return scores; + } + + /** + * Prints Jws2Client usage information to standard out + * + * @param exitStatus + */ + static void printUsage(int exitStatus) { + System.out.println(); + System.out.println("Usage: " + + " ACTION [OPTIONS] "); + System.out.println(); + System.out.println("ACTIONS: "); + System.out + .println(inputkey + + pseparator + + " - full path to fasta or Clustal formatted alignment file "); + 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 the result"); + 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. " + + "Conservation is calculated with either a preset or " + + "the 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: ACTION [OPTIONS] + * + * -i= - full path to fasta or Clustal formatted + * alignment file + * + * -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 results + * -f= - the name of the file with the list + * of parameters to use. Please note that -r and -f options + * cannot be used together. Conservation is calculated with + * either a preset or parameters from the file, but not both! + * + */ + public static void main(String[] args) { + + if (args == null) { + printUsage(1); + } + if (args.length < 1) { + System.out.println("No options is specified! "); + printUsage(1); + } + + try { + new AAConClient(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(); + } + } +}