Fixes to test cases
[jabaws.git] / webservices / compbio / ws / client / AAConClient.java
index 5fa373d..4619c60 100644 (file)
@@ -1,6 +1,6 @@
-/* Copyright (c) 2009 Peter Troshin\r
+/* Copyright (c) 2010 Peter Troshin\r
  *  \r
- *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
+ *  Amino Acid Conservation Web Service client @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
@@ -29,14 +29,11 @@ import static compbio.ws.client.Constraints.pseparator;
 \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
@@ -44,10 +41,11 @@ import javax.xml.namespace.QName;
 import javax.xml.ws.Service;\r
 import javax.xml.ws.WebServiceException;\r
 \r
-import compbio.data.msa.Annotation;\r
+import compbio.data.msa.SequenceAnnotation;\r
 import compbio.data.sequence.FastaSequence;\r
-import compbio.data.sequence.Score;\r
+import compbio.data.sequence.ScoreManager;\r
 import compbio.data.sequence.SequenceUtil;\r
+import compbio.data.sequence.UnknownFileFormatException;\r
 import compbio.metadata.JobSubmissionException;\r
 import compbio.metadata.Option;\r
 import compbio.metadata.Preset;\r
@@ -70,57 +68,40 @@ public class AAConClient {
        private static final Logger log = Logger.getLogger(AAConClient.class\r
                        .getCanonicalName());\r
 \r
+       /**\r
+        * The fully qualified web service namespace\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
+        * Web service host\r
+        */\r
+       static final String hostname = "http://www.compbio.dundee.ac.uk/aacon";\r
+       // static final String hostname = "http://localhost:8080/jabaws";\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
+        * Web service name\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
+       static final String serviceName = "AAConWS";\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
+        *             if the system cannot read/write from/into the file system\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
+               SequenceAnnotation<AAConWS> msaws = connect();\r
                Preset<AAConWS> preset = null;\r
                if (presetName != null) {\r
                        preset = MetadataHelper.getPreset(msaws, presetName);\r
@@ -131,9 +112,9 @@ public class AAConClient {
                        customOptions = MetadataHelper.processParameters(prms,\r
                                        msaws.getRunnerOptions());\r
                }\r
-               Set<Score> result = null;\r
+               ScoreManager result = null;\r
                if (inputFile != null) {\r
-                       System.out.println("calc conserv!");\r
+                       System.out.println("Calculating conservation...");\r
                        result = analize(inputFile, msaws, preset, customOptions);\r
                        OutputStream outStream = null;\r
                        if (outFile != null) {\r
@@ -165,16 +146,15 @@ public class AAConClient {
        }\r
 \r
        /**\r
-        * Outputs clustal formatted alignment into the file represented by the\r
-        * outStream\r
+        * Outputs AAcon results into the file represented by the outStream\r
         * \r
         * @param outStream\r
-        * @param align\r
-        *            the alignment to output\r
+        * @param result\r
+        *            the AACon scores to output\r
         */\r
-       static void writeOut(OutputStream outStream, Set<Score> result) {\r
+       static void writeOut(OutputStream outStream, ScoreManager result) {\r
                try {\r
-                       Score.write(result, outStream);\r
+                       result.writeOut(outStream);\r
                } catch (IOException e) {\r
                        System.err\r
                                        .println("Problems writing output file! Stack trace is below: ");\r
@@ -191,60 +171,57 @@ public class AAConClient {
        }\r
 \r
        /**\r
-        * Connects to a web service by the host and the service name\r
+        * Connects to a AACon web service by the host and the service name\r
+        * \r
         * \r
-        * @param T\r
-        *            web service type\r
-        * @param host\r
-        * @param service\r
-        * @return MsaWS<T>\r
+        * @return {@link AlignmentAnnotation}\r
         * @throws WebServiceException\r
+        *             if cannot connect to a web service\r
         */\r
-       public static Annotation<AAConWS> connect(String host, Services service)\r
+       public static SequenceAnnotation<AAConWS> connect()\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
+                       url = new URL(hostname + "/" + "AAConWS" + "?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 qname = new QName(QUALIFIED_SERVICE_NAME, "AAConWS");\r
+               Service serv = Service.create(url, qname);\r
                QName portName = new QName(QUALIFIED_SERVICE_NAME, "AAConWS" + "Port");\r
 \r
-               Annotation<AAConWS> serviceIF = serv\r
-                               .getPort(portName, Annotation.class);\r
+               @SuppressWarnings("unchecked")\r
+               SequenceAnnotation<AAConWS> serviceIF = serv.getPort(portName,\r
+                               SequenceAnnotation.class);\r
 \r
                log.log(Level.FINE, "Connected successfully!");\r
                return serviceIF;\r
        }\r
 \r
        /**\r
-        * Align sequences from the file using MsaWS\r
+        * Calculate conservation for sequences loaded from the file\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
+        *            a web service proxy\r
+        * @param file\r
+        *            the file to read the results from\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
+        *            the list of options\r
+        * @return Set<Score> the conservation scores\r
+        * @throws UnknownFileFormatException\r
         */\r
-       static <AAConWS> Set<Score> analize(File file, Annotation<AAConWS> wsproxy,\r
+       static ScoreManager analize(File file, SequenceAnnotation<AAConWS> wsproxy,\r
                        Preset<AAConWS> preset, List<Option<AAConWS>> customOptions) {\r
-               FileInputStream instream = null;\r
+\r
                List<FastaSequence> fastalist = null;\r
-               Set<Score> scores = null;\r
+               ScoreManager scores = null;\r
                try {\r
-                       instream = new FileInputStream(file);\r
-                       fastalist = SequenceUtil.readFasta(instream);\r
-                       instream.close();\r
+                       fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());\r
+\r
                        String jobId = null;\r
                        if (customOptions != null && preset != null) {\r
                                System.out\r
@@ -279,21 +256,22 @@ public class AAConClient {
                        // ignore and propagate an interruption\r
                        Thread.currentThread().interrupt();\r
                } catch (WrongParameterException e) {\r
+                       System.err\r
+                                       .println("Exception while parsing the web method input parameters. "\r
+                                                       + "Exception details are below:");\r
+                       e.printStackTrace();\r
+               } catch (UnknownFileFormatException e) {\r
+                       System.err\r
+                                       .println("Exception while attempting to read the input file "\r
+                                                       + "Exception details are below:");\r
+                       System.out.println(e.getMessage());\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
+        * Prints AAConClient usage information to standard out\r
         * \r
         * @param exitStatus\r
         */\r
@@ -335,9 +313,7 @@ public class AAConClient {
        }\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
+        * Starts command line client, if no parameters are supplied prints help.\r
         * \r
         * @param args\r
         *            Usage: <Class or Jar file name> ACTION [OPTIONS]\r