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