New code update
[jabaws.git] / webservices / compbio / ws / client / WSTester.java
index bae843b..5450a1d 100644 (file)
@@ -28,9 +28,13 @@ import java.io.Closeable;
 import java.io.IOException;\r
 import java.io.PrintWriter;\r
 import java.net.ConnectException;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
 import java.util.Arrays;\r
 import java.util.List;\r
+import java.util.logging.Level;\r
 \r
+import javax.xml.ws.Service;\r
 import javax.xml.ws.WebServiceException;\r
 \r
 import compbio.data.msa.JABAService;\r
@@ -72,7 +76,16 @@ public class WSTester {
        public static final String fastaAlignment = \r
                        ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV--------\n"\r
                +   ">Bar\nASDAAPEH------------PGIALWLHALE-DAGQAEAAA---AYTRAHQLLPEEPYITAQLLNAVA\n";\r
-\r
+       public static final String fastaRNAAlignment = \r
+                       ">Foo\nC-UUGCGUUAAUGAGAACAGAAACG-UAAA--CUAUAA-CCUAG-G-GGUUUCUGUUGGAUGGUUG----GCAAC\n"\r
+               +   ">Bar\nG-UGGCGCUUAUGACGCAGUUGUCU-UAAA-CUCGAAC--UCGA-GCGGGCAAUUGCUGAU-UACGAUUAACCAC\n";\r
+\r
+       public static final String clustalRNAAlignment = \r
+                       "CLUSTAL\n" +\r
+                       "Foo             C-UUGCGUUAAUGAGAACAGAAACG-UAAA--CUAUAA-CCUAG-G-GGUUUCUGUUGGA\n" +\r
+                       "Bar             G-UGGCGCUUAUGACGCAGUUGUCU-UAAA-CUCGAAC--UCGA-GCGGGCAAUUGCUGA\n" +\r
+                       "Foo             UGGUUG----GCAAC\n" +\r
+                       "Bar             U-UACGAUUAACCAC";\r
        /**\r
         * Status strings\r
         */\r
@@ -113,6 +126,20 @@ public class WSTester {
                }\r
                return null;\r
        }\r
+       /**\r
+        * Converting input to a form accepted by WS\r
+        * \r
+        * @return List of FastaSequence records with aligned RNA sequences\r
+        */\r
+       private static List<FastaSequence> loadRNAAlignment() {\r
+               try {\r
+                       return SequenceUtil.readFasta(new ByteArrayInputStream(fastaRNAAlignment.getBytes()));\r
+               } catch (IOException ignored) {\r
+                       // Should not happen as a source is not a external stream\r
+                       ignored.printStackTrace();\r
+               }\r
+               return null;\r
+       }\r
 \r
        private final PrintWriter writer;\r
        private final String hostname;\r
@@ -239,11 +266,13 @@ public class WSTester {
 \r
        private <T> boolean testSequenceAnnotationWS(\r
                        SequenceAnnotation<T> wservice, Services service) throws Exception {\r
-               writer.print("Calling analyse.........");\r
+               writer.print("Calling annotation test.........");\r
 \r
                List<FastaSequence> input = loadSeqs(2);\r
-               if (service == Services.AAConWS) {\r
+               if (service == Services.AAConWS ) {\r
                        input = loadAlignment();\r
+               } else if (service == Services.RNAalifoldWS) {\r
+                       input = loadRNAAlignment();\r
                }\r
                boolean success = testDefaultAnalyse(input, wservice, null, null);\r
 \r
@@ -285,8 +314,7 @@ public class WSTester {
 \r
        private void reportException(Exception e) {\r
                writer.println(FAILED);\r
-               writer.println("Exception while waiting for results. "\r
-                               + "Exception details are below:");\r
+               writer.println("Exception while waiting for results. Exception details are below:");\r
                writer.println(e.getLocalizedMessage());\r
                e.printStackTrace(writer);\r
        }\r
@@ -343,7 +371,7 @@ public class WSTester {
                String taskId;\r
 \r
                if (service == Services.JpredWS) {\r
-                       taskId = msaws.align(loadSeqs(1));\r
+                       taskId = msaws.align(loadAlignment());\r
                } else {\r
                        taskId = msaws.align(loadSeqs(2));\r
                }\r
@@ -410,6 +438,44 @@ public class WSTester {
                }\r
 \r
        }\r
+       \r
+       /**\r
+        * Connects to a web service by the host and the service name web service type\r
+        * \r
+        * @param host\r
+        *            the fully qualified name of JABAWS server including JABAWS\r
+        *            context name e.g\r
+        *            http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba\r
+        * @param service\r
+        *            the name of the JABAWS service to connect to\r
+        * @return JABAService<T>\r
+        * @throws WebServiceException\r
+        * @throws ConnectException\r
+        *             if fails to connect to the service on the host\r
+        */\r
+       private JABAService connect(String host, Services service)\r
+                       throws WebServiceException, ConnectException {\r
+               URL url = null;\r
+               System.out.println ("Attempting to connect with " + service.toString() + "...");\r
+               try {\r
+                       url = new URL(host + "/" + service.toString() + "?wsdl");\r
+                       System.out.println ("URL: " + url.toString());\r
+               } catch (MalformedURLException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               Service serv = null;\r
+               try {\r
+                       serv = service.getService(url, service.getServiceNamespace());\r
+               } catch (WebServiceException wse) {\r
+                       wse.printStackTrace();\r
+               }\r
+               if (serv == null) {\r
+                       throw new ConnectException("Could not connect to " + url + ". Is the server down?");\r
+               }\r
+               JABAService srv = service.getInterface(serv);\r
+               System.out.println ("Connected successfully!");\r
+               return srv;\r
+       }\r
 \r
        /**\r
         * Test JABA web service\r
@@ -422,7 +488,7 @@ public class WSTester {
         */\r
        public boolean checkService(Services service) throws ConnectException,\r
                        WebServiceException {\r
-               JABAService ws = Jws2Client.connect(hostname, service);\r
+               JABAService ws = connect(hostname, service);\r
                if (ws == null) {\r
                        String line = "Cannot estabilish the connection to host " + hostname + " with service ";\r
                        writer.println(line + service.toString());\r