Adding updated javadoc
[jabaws.git] / datamodel / compbio / data / sequence / Score.java
index 0e49a08..98ba06a 100644 (file)
@@ -1,17 +1,39 @@
 package compbio.data.sequence;\r
 \r
+import java.io.BufferedWriter;\r
+import java.io.IOException;\r
+import java.io.OutputStream;\r
+import java.io.OutputStreamWriter;\r
+import java.text.NumberFormat;\r
+import java.util.ArrayList;\r
 import java.util.Arrays;\r
 import java.util.List;\r
+import java.util.Locale;\r
+import java.util.Set;\r
 \r
 import javax.xml.bind.annotation.XmlAccessType;\r
 import javax.xml.bind.annotation.XmlAccessorType;\r
 \r
 import compbio.util.annotation.Immutable;\r
 \r
+/**\r
+ * A value class for AACon annotation results storage. The objects of this type\r
+ * are immutable\r
+ * \r
+ * @author pvtroshin\r
+ * \r
+ */\r
 @XmlAccessorType(XmlAccessType.FIELD)\r
 @Immutable\r
 public class Score {\r
 \r
+       static final NumberFormat NUMBER_FORMAT = NumberFormat\r
+                       .getNumberInstance(Locale.UK);\r
+       static {\r
+               NUMBER_FORMAT.setGroupingUsed(false);\r
+               NUMBER_FORMAT.setMaximumFractionDigits(3);\r
+       }\r
+\r
        private ConservationMethod method;\r
 \r
        private List<Float> scores;\r
@@ -20,15 +42,35 @@ public class Score {
                // JaXB default constructor\r
        }\r
 \r
+       /**\r
+        * Instantiate the Score\r
+        * \r
+        * @param method\r
+        *            the ConservationMethod with which {@code scores} were\r
+        *            calculated\r
+        * @param scores\r
+        *            the actual conservation values for each column of the\r
+        *            alignment\r
+        */\r
        public Score(ConservationMethod method, List<Float> scores) {\r
                this.method = method;\r
-               this.scores = scores;\r
+               this.scores = new ArrayList<Float>(scores);\r
        }\r
 \r
+       /**\r
+        * Returns the ConservationMethod\r
+        * \r
+        * @return the ConservationMethod\r
+        */\r
        public ConservationMethod getMethod() {\r
                return method;\r
        }\r
 \r
+       /**\r
+        * The column scores for the alignment\r
+        * \r
+        * @return the column scores for the alignment\r
+        */\r
        public List<Float> getScores() {\r
                return scores;\r
        }\r
@@ -73,4 +115,47 @@ public class Score {
                        return false;\r
                return true;\r
        }\r
+\r
+       /**\r
+        * Outputs the List of Score objects into the Output stream. The output\r
+        * format is as follows:\r
+        * \r
+        * <pre>\r
+        * {@code\r
+        * #MethodName <space separated list of values>\r
+        *        \r
+        * For example:\r
+        *       \r
+        * #KABAT 0.2 0.3 0.2 0 0.645 0.333 1 1 0 0\r
+        * #SMERFS 0.645 0.333 1 1 0 0 0.2 0.3 0.2 0\r
+        * }\r
+        * </pre>\r
+        * \r
+        * The maximum precision for values is 3 digits, but can be less.\r
+        * \r
+        * @param scores\r
+        *            the list of scores to output\r
+        * @param output\r
+        *            the stream to output the data to\r
+        * @throws IOException\r
+        *             if the OutputStream cannot be written into\r
+        * @throws NullPointerException\r
+        *             if the output stream is null\r
+        */\r
+       public static void write(Set<Score> scores, OutputStream output)\r
+                       throws IOException {\r
+               if (output == null) {\r
+                       throw new NullPointerException("OutputStream must be provided!");\r
+               }\r
+               BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(\r
+                               output));\r
+               for (Score score : scores) {\r
+                       writer.write("#" + score.method + " ");\r
+                       for (Float scoreVal : score.getScores()) {\r
+                               writer.write(NUMBER_FORMAT.format(scoreVal) + " ");\r
+                       }\r
+                       writer.write("\n");\r
+               }\r
+               writer.flush();\r
+       }\r
 }\r