Moving a method from AACon to SequenceUtil
authorpvtroshin <pvtroshin@e3abac25-378b-4346-85de-24260fe3988d>
Thu, 23 Dec 2010 14:29:57 +0000 (14:29 +0000)
committerpvtroshin <pvtroshin@e3abac25-378b-4346-85de-24260fe3988d>
Thu, 23 Dec 2010 14:29:57 +0000 (14:29 +0000)
git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@3567 e3abac25-378b-4346-85de-24260fe3988d

binaries/aaconservation.jar
datamodel/compbio/data/sequence/SequenceUtil.java
webservices/compbio/ws/client/AAConClient.java

index e16b9a8..5f76eab 100644 (file)
Binary files a/binaries/aaconservation.jar and b/binaries/aaconservation.jar differ
index 20628a1..f061ac7 100644 (file)
@@ -483,7 +483,8 @@ public final class SequenceUtil {
                        assert spacePos > 0 : "Space is expected as delimited between method "\r
                                        + "name and values!";\r
                        String methodLine = line.substring(0, spacePos);\r
-                       ConservationMethod method = ConservationMethod.getMethod(methodLine);\r
+                       ConservationMethod method = ConservationMethod\r
+                                       .getMethod(methodLine);\r
                        assert method != null : "Method " + methodLine\r
                                        + " is not recognized! ";\r
                        Scanner valuesScanner = new Scanner(line.substring(spacePos));\r
@@ -497,4 +498,37 @@ public final class SequenceUtil {
                return annotations;\r
        }\r
 \r
+       /**\r
+        * Reads and parses Fasta or Clustal formatted file into a list of\r
+        * FastaSequence objects\r
+        * \r
+        * @param inFilePath\r
+        *            the path to the input file\r
+        * @throws IOException\r
+        *             if the file denoted by inFilePath cannot be read\r
+        * @throws UnknownFileFormatException\r
+        *             if the inFilePath points to the file which format cannot be\r
+        *             recognised\r
+        * @return the List of FastaSequence objects\r
+        * \r
+        */\r
+       public static List<FastaSequence> openInputStream(String inFilePath)\r
+                       throws IOException, UnknownFileFormatException {\r
+\r
+               // This stream gets closed in isValidClustalFile method\r
+               InputStream inStrForValidation = new FileInputStream(inFilePath);\r
+               // This stream is closed in the calling methods\r
+               InputStream inStr = new FileInputStream(inFilePath);\r
+               List<FastaSequence> fastaSeqs = null;\r
+               if (ClustalAlignmentUtil.isValidClustalFile(inStrForValidation)) {\r
+                       Alignment al = ClustalAlignmentUtil.readClustalFile(inStr);\r
+                       // alignment cannot be null see\r
+                       // ClustalAlignmentUtil.readClustalFile(inStr);\r
+                       fastaSeqs = al.getSequences();\r
+               } else {\r
+                       fastaSeqs = SequenceUtil.readFasta(inStr);\r
+               }\r
+               return fastaSeqs;\r
+       }\r
+\r
 }\r
index 5fa373d..34fc751 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,12 +29,10 @@ 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
@@ -48,6 +46,7 @@ import compbio.data.msa.Annotation;
 import compbio.data.sequence.FastaSequence;\r
 import compbio.data.sequence.Score;\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 +69,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
+               Annotation<AAConWS> msaws = connect();\r
                Preset<AAConWS> preset = null;\r
                if (presetName != null) {\r
                        preset = MetadataHelper.getPreset(msaws, presetName);\r
@@ -133,7 +115,7 @@ public class AAConClient {
                }\r
                Set<Score> 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,12 +147,11 @@ 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
                try {\r
@@ -191,29 +172,27 @@ 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 Annotation}\r
         * @throws WebServiceException\r
+        *             if cannot connect to a web service\r
         */\r
-       public static Annotation<AAConWS> connect(String host, Services service)\r
-                       throws WebServiceException {\r
+       public static Annotation<AAConWS> connect() 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
+               @SuppressWarnings("unchecked")\r
                Annotation<AAConWS> serviceIF = serv\r
                                .getPort(portName, Annotation.class);\r
 \r
@@ -222,29 +201,27 @@ public class AAConClient {
        }\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 Set<Score> analize(File file, Annotation<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
                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