Finally, the new web services are working
[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.Locale;\r
10 import java.util.Set;\r
11 import java.util.TreeSet;\r
12 \r
13 import javax.xml.bind.annotation.XmlAccessType;\r
14 import javax.xml.bind.annotation.XmlAccessorType;\r
15 \r
16 import compbio.util.annotation.Immutable;\r
17 \r
18 /**\r
19  * A value class for AACon annotation results storage. The objects of this type\r
20  * are immutable\r
21  * \r
22  * @author pvtroshin\r
23  * \r
24  */\r
25 @XmlAccessorType(XmlAccessType.FIELD)\r
26 @Immutable\r
27 public class Score {\r
28 \r
29         static final NumberFormat NUMBER_FORMAT = NumberFormat\r
30                         .getNumberInstance(Locale.UK);\r
31         static {\r
32                 NUMBER_FORMAT.setGroupingUsed(false);\r
33                 NUMBER_FORMAT.setMaximumFractionDigits(3);\r
34         }\r
35         // This should be Enum<?> but JAXB cannot serialize it.\r
36         private final String method;\r
37 \r
38         private TreeSet<Range> ranges = new TreeSet<Range>();\r
39 \r
40         private ArrayList<Float> scores = new ArrayList<Float>(0);\r
41 \r
42         private Score() {\r
43                 // JaXB default constructor\r
44                 method = null;\r
45         }\r
46 \r
47         /**\r
48          * Instantiate the Score\r
49          * \r
50          * @param method\r
51          *            the ConservationMethod with which {@code scores} were\r
52          *            calculated\r
53          * @param scores\r
54          *            the actual conservation values for each column of the\r
55          *            alignment\r
56          */\r
57         public Score(Enum<?> method, ArrayList<Float> scores) {\r
58                 this.method = method.toString();\r
59                 this.scores = new ArrayList<Float>(scores);\r
60         }\r
61 \r
62         /**\r
63          * @param method\r
64          *            the ConservationMethod with which {@code scores} were\r
65          *            calculated\r
66          * @param scores\r
67          *            the actual conservation values for each column of the\r
68          *            alignment\r
69          * @param ranges\r
70          *            The set of ranges i.e. parts of the sequence with specific\r
71          *            function, usually can be calculated based on scores\r
72          */\r
73         public Score(Enum<?> method, ArrayList<Float> scores, TreeSet<Range> ranges) {\r
74                 this.method = method.toString();\r
75                 this.ranges = ranges;\r
76                 this.scores = scores;\r
77         }\r
78 \r
79         public Score(Enum<?> method, TreeSet<Range> ranges) {\r
80                 this.method = method.toString();\r
81                 this.ranges = ranges;\r
82         }\r
83 \r
84         public Score(Enum<?> method, float[] scores) {\r
85                 this.method = method.toString();\r
86                 this.scores = toList(scores);\r
87         }\r
88 \r
89         private ArrayList<Float> toList(float[] values) {\r
90                 ArrayList<Float> vlist = new ArrayList<Float>();\r
91                 for (float v : values) {\r
92                         vlist.add(new Float(v));\r
93                 }\r
94                 return vlist;\r
95         }\r
96         /**\r
97          * Returns the ConservationMethod\r
98          * \r
99          * @return the ConservationMethod\r
100          */\r
101         public String getMethod() {\r
102                 return method;\r
103         }\r
104 \r
105         /**\r
106          * The column scores for the alignment\r
107          * \r
108          * @return the column scores for the alignment\r
109          */\r
110         public ArrayList<Float> getScores() {\r
111                 return scores;\r
112         }\r
113 \r
114         /**\r
115          * Return Ranges if any Collections.EMPTY_SET otherwise\r
116          * \r
117          * @return\r
118          */\r
119         public TreeSet<Range> getRanges() {\r
120                 return ranges;\r
121         }\r
122 \r
123         public void setRanges(TreeSet<Range> ranges) {\r
124                 this.ranges = ranges;\r
125         }\r
126 \r
127         @Override\r
128         public String toString() {\r
129                 return "Score [method=" + method + ", ranges=" + ranges + ", scores="\r
130                                 + scores + "]";\r
131         }\r
132 \r
133         @Override\r
134         public int hashCode() {\r
135                 final int prime = 7;\r
136                 int result = 1;\r
137                 result = prime * result + ((method == null) ? 0 : method.hashCode());\r
138                 result = prime * result + ((ranges == null) ? 0 : ranges.hashCode());\r
139                 result = prime * result + ((scores == null) ? 0 : scores.hashCode());\r
140                 return result;\r
141         }\r
142 \r
143         @Override\r
144         public boolean equals(Object obj) {\r
145                 if (this == obj)\r
146                         return true;\r
147                 if (obj == null)\r
148                         return false;\r
149                 if (getClass() != obj.getClass())\r
150                         return false;\r
151                 Score other = (Score) obj;\r
152                 if (method == null) {\r
153                         if (other.method != null)\r
154                                 return false;\r
155                 } else if (!method.equals(other.method))\r
156                         return false;\r
157                 if (ranges == null) {\r
158                         if (other.ranges != null)\r
159                                 return false;\r
160                 } else if (!ranges.equals(other.ranges))\r
161                         return false;\r
162                 if (scores == null) {\r
163                         if (other.scores != null)\r
164                                 return false;\r
165                 } else if (!scores.equals(other.scores))\r
166                         return false;\r
167                 return true;\r
168         }\r
169 \r
170         /**\r
171          * Outputs the List of Score objects into the Output stream. The output\r
172          * format is as follows:\r
173          * \r
174          * <pre>\r
175          * {@code\r
176          * #MethodName <space separated list of values>\r
177          *        \r
178          * For example:\r
179          *       \r
180          * #KABAT 0.2 0.3 0.2 0 0.645 0.333 1 1 0 0\r
181          * #SMERFS 0.645 0.333 1 1 0 0 0.2 0.3 0.2 0\r
182          * }\r
183          * </pre>\r
184          * \r
185          * The maximum precision for values is 3 digits, but can be less.\r
186          * \r
187          * @param scores\r
188          *            the list of scores to output\r
189          * @param output\r
190          *            the stream to output the data to\r
191          * @throws IOException\r
192          *             if the OutputStream cannot be written into\r
193          * @throws NullPointerException\r
194          *             if the output stream is null\r
195          */\r
196         public static void write(Set<Score> scores, OutputStream output)\r
197                         throws IOException {\r
198                 if (output == null) {\r
199                         throw new NullPointerException("OutputStream must be provided!");\r
200                 }\r
201                 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(\r
202                                 output));\r
203                 for (Score score : scores) {\r
204                         writer.write("#" + score.method + " ");\r
205                         for (Float scoreVal : score.getScores()) {\r
206                                 writer.write(NUMBER_FORMAT.format(scoreVal) + " ");\r
207                         }\r
208                         writer.write("\n");\r
209                 }\r
210                 writer.flush();\r
211         }\r
212 }\r