More work to disorder prediction client & services.
[jabaws.git] / webservices / compbio / ws / client / Jws2Client.java
index 9da8abe..f3ca471 100644 (file)
@@ -33,7 +33,8 @@ import java.io.Closeable;
 import java.io.File;\r
 import java.io.FileInputStream;\r
 import java.io.IOException;\r
-import java.io.OutputStream;\r
+import java.io.PrintWriter;\r
+import java.io.Writer;\r
 import java.net.MalformedURLException;\r
 import java.net.URL;\r
 import java.util.Arrays;\r
@@ -41,15 +42,19 @@ import java.util.List;
 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.JABAService;\r
 import compbio.data.msa.Metadata;\r
 import compbio.data.msa.MsaWS;\r
+import compbio.data.msa.SequenceAnnotation;\r
 import compbio.data.sequence.Alignment;\r
 import compbio.data.sequence.FastaSequence;\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
@@ -65,8 +70,8 @@ import compbio.metadata.WrongParameterException;
 public class Jws2Client {\r
 \r
        /*\r
-        * Use java.util.Logger instead of log4j logger to reduce the size of the\r
-        * client package\r
+        * TODO Use java.util.Logger instead of log4j logger to reduce the size of\r
+        * the client package\r
         */\r
        private static final Logger log = Logger.getLogger(Jws2Client.class\r
                        .getCanonicalName());\r
@@ -147,23 +152,25 @@ public class Jws2Client {
                }\r
                Alignment alignment = null;\r
                if (inputFile != null) {\r
-                       if (service == Services.AAConWS) {\r
-                               System.out.println("calc conserv!");\r
+                       Writer writer = null;\r
+                       if (outFile != null) {\r
+                               writer = IOHelper.getWriter(outFile);\r
                        } else {\r
+                               // this stream is going to be closed later which is fine as\r
+                               // std.out will not be\r
+                               writer = new PrintWriter(System.out, true);\r
+                       }\r
+                       if (service.getServiceType() == SequenceAnnotation.class) {\r
+                               ScoreManager result = analize(inputFile,\r
+                                               ((SequenceAnnotation<T>) msaws), preset, customOptions);\r
 \r
+                               IOHelper.writeOut(writer, result);\r
+                       } else {\r
                                alignment = align(inputFile, (MsaWS<T>) msaws, preset,\r
                                                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
-                               IOHelper.writeOut(outStream, alignment);\r
-                               // stream is closed in the method no need to close it here\r
+                               IOHelper.writeOut(writer, alignment);\r
                        }\r
+                       writer.close();\r
                }\r
 \r
                boolean listParameters = CmdHelper.listParameters(cmd);\r
@@ -182,8 +189,104 @@ public class Jws2Client {
                ((Closeable) msaws).close();\r
                log.fine("Disconnected successfully!");\r
        }\r
+       /**\r
+        * Calculate conservation for sequences loaded from the file\r
+        * \r
+        * @param wsproxy\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
+        *            the list of options\r
+        * @return Set<Score> the conservation scores\r
+        * @throws UnknownFileFormatException\r
+        */\r
+       static <T> ScoreManager analize(List<FastaSequence> fastalist,\r
+                       SequenceAnnotation<T> wsproxy, Preset<T> preset,\r
+                       List<Option<T>> customOptions) {\r
+\r
+               ScoreManager scores = null;\r
+               try {\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
+                       System.out.println("\n\ncalling analise.........");\r
+                       Thread.sleep(1000);\r
+                       scores = wsproxy.getAnnotation(jobId);\r
+\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 e) {\r
+                       // ignore and propagate an interruption\r
+                       Thread.currentThread().interrupt();\r
+                       System.err.println("Exception while waiting for results. "\r
+                                       + "Exception details are below:");\r
+                       e.printStackTrace();\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
+               }\r
+               return scores;\r
+\r
+       }\r
 \r
        /**\r
+        * Calculate conservation for sequences loaded from the file\r
+        * \r
+        * @param wsproxy\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
+        *            the list of options\r
+        * @return Set<Score> the conservation scores\r
+        * @throws IOException\r
+        * @throws UnknownFileFormatException\r
+        */\r
+       static <T> ScoreManager analize(File file, SequenceAnnotation<T> wsproxy,\r
+                       Preset<T> preset, List<Option<T>> customOptions) {\r
+               List<FastaSequence> fastalist = null;\r
+               try {\r
+                       fastalist = SequenceUtil.openInputStream(file.getAbsolutePath());\r
+                       assert !fastalist.isEmpty() : "Input is empty!";\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 (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
+               }\r
+               return analize(fastalist, wsproxy, preset, customOptions);\r
+       }\r
+       /**\r
         * Connects to a web service by the host and the service name\r
         * \r
         * @param T\r
@@ -207,13 +310,18 @@ public class Jws2Client {
                try {\r
                        serv = service.getService(url, QUALIFIED_SERVICE_NAME);\r
                } catch (WebServiceException wse) {\r
-                       System.out.println("Conecting to JABAWS version 2 service");\r
+                       System.out.println("Connecting to JABAWS version 2 service");\r
                        if (isV2service(wse)) {\r
                                serv = service.getService(url, V2_QUALIFIED_SERVICE_NAME);\r
                        }\r
                }\r
+               if (serv == null) {\r
+                       System.err.println("Could not connect to " + url\r
+                                       + " the server is down?");\r
+                       // FIXME\r
+               }\r
                JABAService serviceIF = service.getInterface(serv);\r
-               log.log(Level.FINE, "Connected successfully!");\r
+               log.log(Level.INFO, "Connected successfully!");\r
 \r
                return serviceIF;\r
        }\r
@@ -229,6 +337,36 @@ public class Jws2Client {
                return false;\r
        }\r
 \r
+       public static compbio.data.msa.RegistryWS connectToRegistry(String host)\r
+                       throws WebServiceException {\r
+               URL url = null;\r
+               String service = "RegistryWS";\r
+               log.log(Level.FINE, "Attempting to connect...");\r
+\r
+               try {\r
+                       url = new URL(host + "/" + service + "?wsdl");\r
+               } catch (MalformedURLException e) {\r
+                       e.printStackTrace();\r
+                       // ignore as the host name is already verified\r
+               }\r
+               QName qname = new QName(V2_QUALIFIED_SERVICE_NAME, service);\r
+               Service serv = Service.create(url, qname);\r
+\r
+               if (serv == null) {\r
+                       System.err.println("Could not connect to " + url\r
+                                       + " the server is down?");\r
+               }\r
+\r
+               QName portName = new QName(serv.getServiceName().getNamespaceURI(),\r
+                               service + "Port");\r
+               compbio.data.msa.RegistryWS serviceIF = serv.getPort(portName,\r
+                               compbio.data.msa.RegistryWS.class);\r
+\r
+               log.log(Level.INFO, "Connected to " + service + " successfully!");\r
+\r
+               return serviceIF;\r
+       }\r
+\r
        /**\r
         * Align sequences from the file using MsaWS\r
         * \r
@@ -265,6 +403,7 @@ public class Jws2Client {
                        } else {\r
                                jobId = msaws.align(fastalist);\r
                        }\r
+                       System.out.println("\n\ncalling align.........");\r
                        Thread.sleep(1000);\r
                        alignment = msaws.getResult(jobId);\r
 \r
@@ -351,7 +490,7 @@ public class Jws2Client {
 \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
+        * parameters are required for successful call the JWS2 host name and a\r
         * service name.\r
         * \r
         * @param args\r