Remove still unsupported methods of RegistryWS
[jabaws.git] / webservices / compbio / ws / client / Jws2Client.java
index 3322b35..cf0cf23 100644 (file)
@@ -25,6 +25,7 @@ import static compbio.ws.client.Constraints.paramFile;
 import java.io.Closeable;\r
 import java.io.File;\r
 import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\r
 import java.io.IOException;\r
 import java.io.PrintWriter;\r
 import java.io.Writer;\r
@@ -48,13 +49,18 @@ import compbio.data.msa.MsaWS;
 import compbio.data.msa.RegistryWS;\r
 import compbio.data.msa.SequenceAnnotation;\r
 import compbio.data.sequence.Alignment;\r
+import compbio.data.sequence.AlignmentMetadata;\r
+import compbio.data.sequence.JpredAlignment;\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.JobStatus;\r
 import compbio.metadata.Option;\r
+import compbio.metadata.Limit;\r
 import compbio.metadata.Preset;\r
+import compbio.metadata.PresetManager;\r
 import compbio.metadata.ResultNotAvailableException;\r
 import compbio.metadata.WrongParameterException;\r
 import compbio.util.FileUtil;\r
@@ -67,7 +73,7 @@ import compbio.util.FileUtil;
  */\r
 public class Jws2Client {\r
 \r
-       /*\r
+       /**\r
         * Use java.util.Logger instead of log4j logger to reduce the size of the client package\r
         */\r
        private static final Logger log = Logger.getLogger(Jws2Client.class.getCanonicalName());\r
@@ -100,7 +106,9 @@ public class Jws2Client {
         *            command line options\r
         * @throws IOException\r
         */\r
+       @SuppressWarnings("unchecked")\r
        <T> Jws2Client(String[] cmd) throws IOException {\r
+               \r
 \r
                String hostname = CmdHelper.getHost(cmd);\r
                if (hostname == null) {\r
@@ -160,27 +168,52 @@ public class Jws2Client {
                        if (outfile != null) {\r
                                writer = IOHelper.getWriter(outfile);\r
                        } else {\r
-                               // this stream is going to be closed later which is fine as std.out will not be\r
                                writer = new PrintWriter(System.out, true);\r
                        }\r
                        if (service.getServiceType() == SequenceAnnotation.class) {\r
                                ScoreManager result = analize(infile, ((SequenceAnnotation<T>) thews), preset, customOptions);\r
                                IOHelper.writeOut(writer, result);\r
+                               writer.close();\r
                        } else if (service.getServiceType() == MsaWS.class) {\r
                                Alignment alignment = align(infile, (MsaWS<T>) thews, preset, customOptions);\r
-                               IOHelper.writeOut(writer, alignment);\r
+                               if (serviceName.equalsIgnoreCase("JpredWS")) {\r
+                                       writer.close();\r
+                                       JpredAlignment jpred = (JpredAlignment)alignment;\r
+                                       if (outfile != null) {\r
+                                               FileOutputStream fout = new FileOutputStream(outfile);\r
+                                               SequenceUtil.writeFastaKeepTheStream(fout, jpred.getJpredSequences(), 60);\r
+                                               fout.close();\r
+                                       } else {\r
+                                               SequenceUtil.writeFasta(System.out, jpred.getJpredSequences());\r
+                                       }\r
+                               } else {\r
+                                       IOHelper.writeOut(writer, alignment);\r
+                                       writer.close();\r
+                               }\r
+                               AlignmentMetadata md = alignment.getMetadata();\r
+                               System.out.println("Output has been prepared with " + md.getProgram());\r
                        }\r
-                       writer.close();\r
                }\r
 \r
                if (CmdHelper.listParameters(cmd)) {\r
-                       System.out.println(MetadataHelper.getParametersList(thews));\r
+                       List<Option<T>> opts = MetadataHelper.getParametersList(thews, hostname);\r
+                       for (Option<T> o : opts) {\r
+                               System.out.println("##############################################################################\n" + o.toString());\r
+                       }\r
                }\r
                if (CmdHelper.listPresets(cmd)) {\r
-                       System.out.println(MetadataHelper.getPresetList(thews));\r
+                       PresetManager<T> psm = MetadataHelper.getPresetList(thews);\r
+                       if (null != psm) {\r
+                               System.out.print(psm);\r
+                       } else {\r
+                               System.out.println("No presets are defined for the Web service");\r
+                       }\r
                }\r
                if (CmdHelper.listLimits(cmd)) {\r
-                       System.out.println(MetadataHelper.getLimits(thews));\r
+                       List<Limit<T>> lims = MetadataHelper.getLimits(thews);\r
+                       for (Limit<T> l : lims) {\r
+                               System.out.println("##############################################################################\n" + l.toString());\r
+                       }\r
                }\r
                log.fine("Disconnecting...");\r
                ((Closeable) thews).close();\r
@@ -251,7 +284,7 @@ public class Jws2Client {
                        e.printStackTrace();\r
                        // ignore as the host name is already verified\r
                }\r
-               QName qname = new QName(JABAService.V3_SERVICE_NAMESPACE, service);\r
+               QName qname = new QName(JABAService.V2_SERVICE_NAMESPACE, service);\r
                Service serv = Service.create(url, qname);\r
 \r
                if (serv == null) {\r
@@ -288,24 +321,28 @@ public class Jws2Client {
 \r
        private static void listAllServices(String hostname) throws WebServiceException, IOException {\r
                RegistryWS registry = connectToRegistry(hostname);\r
-               Set<Services> func_services = Collections.EMPTY_SET;\r
-               Set<Services> nonfunc_services = Collections.EMPTY_SET;\r
+               Set<Services> func_services = Collections.emptySet();\r
+               //Set<Services> nonfunc_services = Collections.emptySet();\r
                if (registry != null) {\r
                        func_services = registry.getSupportedServices();\r
-                       nonfunc_services = registry.getNonoperatedServices();\r
+                       //nonfunc_services = registry.getNonoperatedServices();\r
                        FileUtil.closeSilently(((Closeable) registry));\r
                } else {\r
                        System.out.println("Failed to connect to the registry! ");\r
                        return;\r
                }\r
                if (!func_services.isEmpty()) {\r
-                       String mess = "\n\rAvailable services: ";\r
+                       System.out.println("There are " + func_services.size() + " services at " + hostname + ":");\r
+                       String mess = "\n\rThe list:\n";\r
                        System.out.println(mess + Services.toString(func_services));\r
                }\r
+               /*\r
                if (!nonfunc_services.isEmpty()) {\r
-                       String mess = "Non-available services (internal tests failed): ";\r
+                       System.out.println("There are " + nonfunc_services.size() + " non-available services at " + hostname + ":");\r
+                       String mess = "The list (internal tests failed): ";\r
                        System.out.println(mess + Services.toString(nonfunc_services));\r
                }\r
+               */\r
        }\r
 \r
        /**\r
@@ -338,6 +375,8 @@ public class Jws2Client {
                        }\r
                        System.out.println("\n\rcalling predictor.........");\r
                        Thread.sleep(100);\r
+                       JobStatus status = wsproxy.getJobStatus(jobId);\r
+                       System.out.println("\njob " + jobId + " status: " + status);\r
                        scores = wsproxy.getAnnotation(jobId);\r
                } catch (JobSubmissionException e) {\r
                        System.err.println("Exception while submitting job to a web server. Exception details are below:");\r
@@ -409,11 +448,10 @@ public class Jws2Client {
        static <T> Alignment align(File file, MsaWS<T> msaws, Preset<T> preset,\r
                        List<Option<T>> customOptions) {\r
                FileInputStream instream = null;\r
-               List<FastaSequence> fastalist = null;\r
                Alignment alignment = null;\r
                try {\r
                        instream = new FileInputStream(file);\r
-                       fastalist = SequenceUtil.readFasta(instream);\r
+                       List<FastaSequence> fastalist = SequenceUtil.readFasta(instream);\r
                        instream.close();\r
                        String jobId = null;\r
                        if (customOptions != null && preset != null) {\r
@@ -426,22 +464,23 @@ public class Jws2Client {
                        } else {\r
                                jobId = msaws.align(fastalist);\r
                        }\r
-                       System.out.println("\ncalling aligner.........");\r
-                       Thread.sleep(1000);\r
+                       System.out.println("\ncalling program.........");\r
+                       long startTime = System.nanoTime();\r
+                       while (JobStatus.RUNNING == msaws.getJobStatus(jobId)) {\r
+                               Thread.sleep(1000);\r
+                               long endTime = System.nanoTime();\r
+                               System.out.println("job " + jobId + " time executing: "+ (endTime - startTime) / 1000000 +" msec, status: " + msaws.getJobStatus(jobId));\r
+                       }\r
                        alignment = msaws.getResult(jobId);\r
-\r
                } catch (IOException e) {\r
-                       System.err.println("Exception while reading the input file. "\r
-                                               + "Check that the input file contains a list of fasta formatted sequences! "\r
+                       System.err.println("Exception while reading the input file. Check that the input file is a FASTA file! "\r
                                                + "Exception details are below:");\r
                        e.printStackTrace();\r
                } catch (JobSubmissionException e) {\r
-                       System.err.println("Exception while submitting job to a web server. "\r
-                                               + "Exception details are below:");\r
+                       System.err.println("Exception while submitting job to a web server. 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
+                       System.err.println("Exception while waiting for results. Exception details are below:");\r
                        e.printStackTrace();\r
                } catch (InterruptedException ignored) {\r
                        // ignore and propagate an interruption\r
@@ -459,7 +498,7 @@ public class Jws2Client {
                }\r
                return alignment;\r
        }\r
-\r
+       \r
        /**\r
         * Starts command line client, if no parameter are supported print help. Two\r
         * parameters are required for successful call the JWS2 host name and a\r
@@ -497,7 +536,6 @@ public class Jws2Client {
         * \r
         */\r
        public static void main(String[] args) {\r
-\r
                if (args == null) {\r
                        System.out.println(Constraints.help_text);\r
                        System.exit(1);\r
@@ -507,7 +545,6 @@ public class Jws2Client {
                        System.out.println(Constraints.help_text);\r
                        System.exit(1);\r
                }\r
-\r
                try {\r
                        new Jws2Client(args);\r
                } catch (IOException e) {\r