--- /dev/null
+/* Copyright (c) 2009 Peter Troshin\r
+ * \r
+ * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0 \r
+ * \r
+ * This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ * Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ * License for more details.\r
+ * \r
+ * A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
+\r
+package compbio.ws.client;\r
+\r
+import static compbio.ws.client.Constraints.inputkey;\r
+import static compbio.ws.client.Constraints.limitList;\r
+import static compbio.ws.client.Constraints.outputkey;\r
+import static compbio.ws.client.Constraints.paramFile;\r
+import static compbio.ws.client.Constraints.paramList;\r
+import static compbio.ws.client.Constraints.presetList;\r
+import static compbio.ws.client.Constraints.presetkey;\r
+import static compbio.ws.client.Constraints.pseparator;\r
+\r
+import java.io.Closeable;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
+import java.util.Arrays;\r
+import java.util.List;\r
+import java.util.Set;\r
+import java.util.logging.Level;\r
+import java.util.logging.Logger;\r
+\r
+import javax.xml.namespace.QName;\r
+import javax.xml.ws.Service;\r
+import javax.xml.ws.WebServiceException;\r
+\r
+import compbio.data.msa.Annotation;\r
+import compbio.data.sequence.FastaSequence;\r
+import compbio.data.sequence.Score;\r
+import compbio.data.sequence.SequenceUtil;\r
+import compbio.metadata.JobSubmissionException;\r
+import compbio.metadata.Option;\r
+import compbio.metadata.Preset;\r
+import compbio.metadata.ResultNotAvailableException;\r
+import compbio.metadata.WrongParameterException;\r
+import compbio.ws.server.AAConWS;\r
+\r
+/**\r
+ * A command line client for AACon web service\r
+ * \r
+ * @author pvtroshin\r
+ * @version 1.0\r
+ */\r
+public class AAConClient {\r
+\r
+ /*\r
+ * Use java.util.Logger instead of log4j logger to reduce the size of the\r
+ * client package\r
+ */\r
+ private static final Logger log = Logger.getLogger(AAConClient.class\r
+ .getCanonicalName());\r
+\r
+ static final String QUALIFIED_SERVICE_NAME = "http://msa.data.compbio/01/12/2010/";\r
+\r
+ // static final String hostname = "http://www.compbio.dundee.ac.uk/aacon";\r
+ static final String hostname = "http://localhost:8080/jabaws";\r
+\r
+ static final String serviceName = "AAConWS";\r
+\r
+ /**\r
+ * Attempt to construct the URL object from the string\r
+ * \r
+ * @param urlstr\r
+ * @return true if it succeed false otherwise\r
+ */\r
+ public static boolean validURL(String urlstr) {\r
+ try {\r
+ if (urlstr == null || urlstr.trim().length() == 0) {\r
+ return false;\r
+ }\r
+ new URL(urlstr);\r
+ } catch (MalformedURLException e) {\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * Connects to the service and do the job as requested, if something goes\r
+ * wrong reports or/and prints usage help.\r
+ * \r
+ * @param <AAConWS>\r
+ * web service type\r
+ * @param cmd\r
+ * command line options\r
+ * @throws IOException\r
+ */\r
+ @SuppressWarnings("unchecked")\r
+ AAConClient(String[] cmd) throws IOException {\r
+\r
+ Services service = Services.getService(serviceName);\r
+ if (service == null) {\r
+ System.out.println("Service " + serviceName\r
+ + " is no supported! Valid values are: "\r
+ + Arrays.toString(Services.values()));\r
+ printUsage(1);\r
+ }\r
+ File inputFile = IOHelper.getFile(cmd, inputkey, true);\r
+ File outFile = IOHelper.getFile(cmd, outputkey, false);\r
+ File parametersFile = IOHelper.getFile(cmd, paramFile, true);\r
+ String presetName = CmdHelper.getPresetName(cmd);\r
+\r
+ Annotation<AAConWS> msaws = connect(hostname, service);\r
+ Preset<AAConWS> preset = null;\r
+ if (presetName != null) {\r
+ preset = MetadataHelper.getPreset(msaws, presetName);\r
+ }\r
+ List<Option<AAConWS>> customOptions = null;\r
+ if (parametersFile != null) {\r
+ List<String> prms = IOHelper.loadParameters(parametersFile);\r
+ customOptions = MetadataHelper.processParameters(prms,\r
+ msaws.getRunnerOptions());\r
+ }\r
+ Set<Score> result = null;\r
+ if (inputFile != null) {\r
+ System.out.println("calc conserv!");\r
+ result = analize(inputFile, msaws, preset, customOptions);\r
+ OutputStream outStream = null;\r
+ if (outFile != null) {\r
+ outStream = IOHelper.getOutStream(outFile);\r
+ } else {\r
+ // this stream is going to be closed later which is fine as\r
+ // std.out will not be\r
+ outStream = System.out;\r
+ }\r
+ writeOut(outStream, result);\r
+ // stream is closed in the method no need to close it here\r
+ }\r
+\r
+ boolean listParameters = CmdHelper.listParameters(cmd);\r
+ if (listParameters) {\r
+ System.out.println(MetadataHelper.getParametersList(msaws));\r
+ }\r
+ boolean listPreset = CmdHelper.listPresets(cmd);\r
+ if (listPreset) {\r
+ System.out.println(MetadataHelper.getPresetList(msaws));\r
+ }\r
+ boolean listLimits = CmdHelper.listLimits(cmd);\r
+ if (listLimits) {\r
+ System.out.println(MetadataHelper.getLimits(msaws));\r
+ }\r
+ log.fine("Disconnecting...");\r
+ ((Closeable) msaws).close();\r
+ log.fine("Disconnected successfully!");\r
+ }\r
+\r
+ /**\r
+ * Outputs clustal formatted alignment into the file represented by the\r
+ * outStream\r
+ * \r
+ * @param outStream\r
+ * @param align\r
+ * the alignment to output\r
+ */\r
+ static void writeOut(OutputStream outStream, Set<Score> result) {\r
+ try {\r
+ Score.write(result, outStream);\r
+ } catch (IOException e) {\r
+ System.err\r
+ .println("Problems writing output file! Stack trace is below: ");\r
+ e.printStackTrace();\r
+ } finally {\r
+ if (outStream != null) {\r
+ try {\r
+ outStream.close();\r
+ } catch (IOException ignored) {\r
+ // e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Connects to a web service by the host and the service name\r
+ * \r
+ * @param T\r
+ * web service type\r
+ * @param host\r
+ * @param service\r
+ * @return MsaWS<T>\r
+ * @throws WebServiceException\r
+ */\r
+ public static Annotation<AAConWS> connect(String host, Services service)\r
+ throws WebServiceException {\r
+ URL url = null;\r
+ log.log(Level.FINE, "Attempting to connect...");\r
+ try {\r
+ url = new URL(host + "/" + service.toString() + "?wsdl");\r
+ } catch (MalformedURLException e) {\r
+ e.printStackTrace();\r
+ // ignore as the host name is already verified\r
+ }\r
+ Service serv = service.getService(url, QUALIFIED_SERVICE_NAME);\r
+ @SuppressWarnings("unchecked")\r
+ QName portName = new QName(QUALIFIED_SERVICE_NAME, "AAConWS" + "Port");\r
+\r
+ Annotation<AAConWS> serviceIF = serv\r
+ .getPort(portName, Annotation.class);\r
+\r
+ log.log(Level.FINE, "Connected successfully!");\r
+ return serviceIF;\r
+ }\r
+\r
+ /**\r
+ * Align sequences from the file using MsaWS\r
+ * \r
+ * @param <T>\r
+ * web service type e.g. Clustal\r
+ * @param file\r
+ * to write the resulting alignment to\r
+ * @param wsproxy\r
+ * MsaWS required\r
+ * @param preset\r
+ * Preset to use optional\r
+ * @param customOptions\r
+ * file which contains new line separated list of options\r
+ * @return Alignment\r
+ */\r
+ static <AAConWS> Set<Score> analize(File file, Annotation<AAConWS> wsproxy,\r
+ Preset<AAConWS> preset, List<Option<AAConWS>> customOptions) {\r
+ FileInputStream instream = null;\r
+ List<FastaSequence> fastalist = null;\r
+ Set<Score> scores = null;\r
+ try {\r
+ instream = new FileInputStream(file);\r
+ fastalist = SequenceUtil.readFasta(instream);\r
+ instream.close();\r
+ String jobId = null;\r
+ if (customOptions != null && preset != null) {\r
+ System.out\r
+ .println("WARN: Parameters (-f) are defined together with a preset (-r) ignoring preset!");\r
+ }\r
+ if (customOptions != null) {\r
+ jobId = wsproxy.customAnalize(fastalist, customOptions);\r
+ } else if (preset != null) {\r
+ jobId = wsproxy.presetAnalize(fastalist, preset);\r
+ } else {\r
+ jobId = wsproxy.analize(fastalist);\r
+ }\r
+ Thread.sleep(1000);\r
+ scores = wsproxy.getAnnotation(jobId);\r
+\r
+ } catch (IOException e) {\r
+ System.err\r
+ .println("Exception while reading the input file. "\r
+ + "Check that the input file contains a list of fasta formatted sequences! "\r
+ + "Exception details are below:");\r
+ e.printStackTrace();\r
+ } catch (JobSubmissionException e) {\r
+ System.err\r
+ .println("Exception while submitting job to a web server. "\r
+ + "Exception details are below:");\r
+ e.printStackTrace();\r
+ } catch (ResultNotAvailableException e) {\r
+ System.err.println("Exception while waiting for results. "\r
+ + "Exception details are below:");\r
+ e.printStackTrace();\r
+ } catch (InterruptedException ignored) {\r
+ // ignore and propagate an interruption\r
+ Thread.currentThread().interrupt();\r
+ } catch (WrongParameterException e) {\r
+ e.printStackTrace();\r
+ } finally {\r
+ if (instream != null) {\r
+ try {\r
+ instream.close();\r
+ } catch (IOException ignored) {\r
+ // ignore\r
+ }\r
+ }\r
+ }\r
+ return scores;\r
+ }\r
+\r
+ /**\r
+ * Prints Jws2Client usage information to standard out\r
+ * \r
+ * @param exitStatus\r
+ */\r
+ static void printUsage(int exitStatus) {\r
+ System.out.println();\r
+ System.out.println("Usage: <Class or Jar file name> "\r
+ + " ACTION [OPTIONS] ");\r
+ System.out.println();\r
+ System.out.println("ACTIONS: ");\r
+ System.out\r
+ .println(inputkey\r
+ + pseparator\r
+ + "<inputFile> - full path to fasta or Clustal formatted alignment file ");\r
+ System.out.println(paramList\r
+ + " - lists parameters supported by web service");\r
+ System.out.println(presetList\r
+ + " - lists presets supported by web service");\r
+ System.out.println(limitList + " - lists web services limits");\r
+ System.out\r
+ .println("Please note that if input file is specified other actions are ignored");\r
+\r
+ System.out.println();\r
+ System.out.println("OPTIONS (only for use with -i action):");\r
+\r
+ System.out.println(presetkey + pseparator\r
+ + "<presetName> - name of the preset to use");\r
+ System.out\r
+ .println(outputkey\r
+ + pseparator\r
+ + "<outputFile> - full path to the file where to write the result");\r
+ System.out\r
+ .println("-f=<parameterInputFile> - the name of the file with the list of parameters to use.");\r
+ System.out\r
+ .println("Please note that -r and -f options cannot be used together. "\r
+ + "Conservation is calculated with either a preset or "\r
+ + "the parameters from the file, but not both!");\r
+\r
+ System.exit(exitStatus);\r
+ }\r
+\r
+ /**\r
+ * Starts command line client, if no parameter are supported print help. Two\r
+ * parameters are required for successfull call the JWS2 host name and a\r
+ * service name.\r
+ * \r
+ * @param args\r
+ * Usage: <Class or Jar file name> ACTION [OPTIONS]\r
+ * \r
+ * -i=<inputFile> - full path to fasta or Clustal formatted\r
+ * alignment file\r
+ * \r
+ * -parameters - lists parameters supported by web service\r
+ * \r
+ * -presets - lists presets supported by web service\r
+ * \r
+ * -limits - lists web services limits. Please note that if input\r
+ * file is specified other actions are ignored\r
+ * \r
+ * OPTIONS: (only for use with -i action):\r
+ * \r
+ * -r=<presetName> - name of the preset to use\r
+ * \r
+ * -o=<outputFile> - full path to the file where to write results\r
+ * -f=<parameterInputFile> - the name of the file with the list\r
+ * of parameters to use. Please note that -r and -f options\r
+ * cannot be used together. Conservation is calculated with\r
+ * either a preset or parameters from the file, but not both!\r
+ * \r
+ */\r
+ public static void main(String[] args) {\r
+\r
+ if (args == null) {\r
+ printUsage(1);\r
+ }\r
+ if (args.length < 1) {\r
+ System.out.println("No options is specified! ");\r
+ printUsage(1);\r
+ }\r
+\r
+ try {\r
+ new AAConClient(args);\r
+ } catch (IOException e) {\r
+ log.log(Level.SEVERE, "IOException in client! " + e.getMessage(),\r
+ e.getCause());\r
+ System.err.println("Cannot write output file! Stack trace: ");\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+}\r