Remove Annotated and MuptiAnnotated Sequence use Score instead
[jabaws.git] / datamodel / compbio / data / sequence / Score.java
1 package compbio.data.sequence;\r
2 \r
3 import java.io.BufferedWriter;\r
4 import java.io.IOException;\r
5 import java.io.OutputStream;\r
6 import java.io.OutputStreamWriter;\r
7 import java.text.NumberFormat;\r
8 import java.util.ArrayList;\r
9 import java.util.Arrays;\r
10 import java.util.List;\r
11 import java.util.Locale;\r
12 import java.util.Set;\r
13 \r
14 import javax.xml.bind.annotation.XmlAccessType;\r
15 import javax.xml.bind.annotation.XmlAccessorType;\r
16 \r
17 import compbio.util.annotation.Immutable;\r
18 \r
19 /**\r
20  * A value class for AACon annotation results storage. The objects of this type\r
21  * are immutable\r
22  * \r
23  * @author pvtroshin\r
24  * \r
25  */\r
26 @XmlAccessorType(XmlAccessType.FIELD)\r
27 @Immutable\r
28 public class Score {\r
29 \r
30         static final NumberFormat NUMBER_FORMAT = NumberFormat\r
31                         .getNumberInstance(Locale.UK);\r
32         static {\r
33                 NUMBER_FORMAT.setGroupingUsed(false);\r
34                 NUMBER_FORMAT.setMaximumFractionDigits(3);\r
35         }\r
36 \r
37         private Enum<?> method;\r
38 \r
39         private List<Float> scores;\r
40 \r
41         private Score() {\r
42                 // JaXB default constructor\r
43         }\r
44 \r
45         /**\r
46          * Instantiate the Score\r
47          * \r
48          * @param method\r
49          *            the ConservationMethod with which {@code scores} were\r
50          *            calculated\r
51          * @param scores\r
52          *            the actual conservation values for each column of the\r
53          *            alignment\r
54          */\r
55         public Score(Enum<?> method, List<Float> scores) {\r
56                 this.method = method;\r
57                 this.scores = new ArrayList<Float>(scores);\r
58         }\r
59 \r
60         public Score(Enum<?> method, float[] scores) {\r
61                 this.method = method;\r
62                 this.scores = toList(scores);\r
63         }\r
64 \r
65         private List<Float> toList(float[] values) {\r
66                 List<Float> vlist = new ArrayList<Float>();\r
67                 for (float v : values) {\r
68                         vlist.add(new Float(v));\r
69                 }\r
70                 return vlist;\r
71         }\r
72         /**\r
73          * Returns the ConservationMethod\r
74          * \r
75          * @return the ConservationMethod\r
76          */\r
77         public Enum<?> getMethod() {\r
78                 return method;\r
79         }\r
80 \r
81         /**\r
82          * The column scores for the alignment\r
83          * \r
84          * @return the column scores for the alignment\r
85          */\r
86         public List<Float> getScores() {\r
87                 return scores;\r
88         }\r
89 \r
90         @Override\r
91         public String toString() {\r
92                 return "Score [method=" + method + ", scores=" + scores + "]";\r
93         }\r
94 \r
95         @Override\r
96         public int hashCode() {\r
97                 final int prime = 31;\r
98                 int result = 1;\r
99                 result = prime * result + ((method == null) ? 0 : method.hashCode());\r
100                 result = prime * result + ((scores == null) ? 0 : scores.hashCode());\r
101                 return result;\r
102         }\r
103 \r
104         /*\r
105          * TODO test ! (non-Javadoc)\r
106          * \r
107          * @see java.lang.Object#equals(java.lang.Object)\r
108          */\r
109         @Override\r
110         public boolean equals(Object obj) {\r
111                 if (this == obj)\r
112                         return true;\r
113                 if (obj == null)\r
114                         return false;\r
115                 if (getClass() != obj.getClass())\r
116                         return false;\r
117                 Score other = (Score) obj;\r
118                 if (method != other.method)\r
119                         return false;\r
120                 if (scores == other.scores) {\r
121                         return true;\r
122                 }\r
123                 if (scores == null) {\r
124                         return false;\r
125                 }\r
126                 if (!Arrays.deepEquals(scores.toArray(), other.scores.toArray()))\r
127                         return false;\r
128                 return true;\r
129         }\r
130 \r
131         /**\r
132          * Outputs the List of Score objects into the Output stream. The output\r
133          * format is as follows:\r
134          * \r
135          * <pre>\r
136          * {@code\r
137          * #MethodName <space separated list of values>\r
138          *        \r
139          * For example:\r
140          *       \r
141          * #KABAT 0.2 0.3 0.2 0 0.645 0.333 1 1 0 0\r
142          * #SMERFS 0.645 0.333 1 1 0 0 0.2 0.3 0.2 0\r
143          * }\r
144          * </pre>\r
145          * \r
146          * The maximum precision for values is 3 digits, but can be less.\r
147          * \r
148          * @param scores\r
149          *            the list of scores to output\r
150          * @param output\r
151          *            the stream to output the data to\r
152          * @throws IOException\r
153          *             if the OutputStream cannot be written into\r
154          * @throws NullPointerException\r
155          *             if the output stream is null\r
156          */\r
157         public static void write(Set<Score> scores, OutputStream output)\r
158                         throws IOException {\r
159                 if (output == null) {\r
160                         throw new NullPointerException("OutputStream must be provided!");\r
161                 }\r
162                 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(\r
163                                 output));\r
164                 for (Score score : scores) {\r
165                         writer.write("#" + score.method + " ");\r
166                         for (Float scoreVal : score.getScores()) {\r
167                                 writer.write(NUMBER_FORMAT.format(scoreVal) + " ");\r
168                         }\r
169                         writer.write("\n");\r
170                 }\r
171                 writer.flush();\r
172         }\r
173 }\r