Write javadoc for new datamodel classes , remove some incomplete staff
[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\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 \r
36         private ConservationMethod method;\r
37 \r
38         private List<Float> scores;\r
39 \r
40         private Score() {\r
41                 // JaXB default constructor\r
42         }\r
43 \r
44         /**\r
45          * Instantiate the Score\r
46          * \r
47          * @param method\r
48          *            the ConservationMethod with which {@code scores} were\r
49          *            calculated\r
50          * @param scores\r
51          *            the actual conservation values for each column of the\r
52          *            alignment\r
53          */\r
54         public Score(ConservationMethod method, List<Float> scores) {\r
55                 this.method = method;\r
56                 this.scores = new ArrayList<Float>(scores);\r
57         }\r
58 \r
59         /**\r
60          * Returns the ConservationMethod\r
61          * \r
62          * @return the ConservationMethod\r
63          */\r
64         public ConservationMethod getMethod() {\r
65                 return method;\r
66         }\r
67 \r
68         /**\r
69          * The column scores for the alignment\r
70          * \r
71          * @return the column scores for the alignment\r
72          */\r
73         public List<Float> getScores() {\r
74                 return scores;\r
75         }\r
76 \r
77         @Override\r
78         public String toString() {\r
79                 return "Score [method=" + method + ", scores=" + scores + "]";\r
80         }\r
81 \r
82         @Override\r
83         public int hashCode() {\r
84                 final int prime = 31;\r
85                 int result = 1;\r
86                 result = prime * result + ((method == null) ? 0 : method.hashCode());\r
87                 result = prime * result + ((scores == null) ? 0 : scores.hashCode());\r
88                 return result;\r
89         }\r
90 \r
91         /*\r
92          * TODO test ! (non-Javadoc)\r
93          * \r
94          * @see java.lang.Object#equals(java.lang.Object)\r
95          */\r
96         @Override\r
97         public boolean equals(Object obj) {\r
98                 if (this == obj)\r
99                         return true;\r
100                 if (obj == null)\r
101                         return false;\r
102                 if (getClass() != obj.getClass())\r
103                         return false;\r
104                 Score other = (Score) obj;\r
105                 if (method != other.method)\r
106                         return false;\r
107                 if (scores == other.scores) {\r
108                         return true;\r
109                 }\r
110                 if (scores == null) {\r
111                         return false;\r
112                 }\r
113                 if (!Arrays.deepEquals(scores.toArray(), other.scores.toArray()))\r
114                         return false;\r
115                 return true;\r
116         }\r
117 \r
118         /**\r
119          * Outputs the List of Score objects into the Output stream. The output\r
120          * format is as follows:\r
121          * \r
122          * <pre>\r
123          *        #MethodName <space separated list of values>\r
124          *        \r
125          * For example:\r
126          *       \r
127          *        #KABAT 0.2 0.3 0.2 0 0.645 0.333 1 1 0 0\r
128          *    #SMERFS 0.645 0.333 1 1 0 0 0.2 0.3 0.2 0\r
129          * </pre>\r
130          * \r
131          * The maximum precision for values is 3 digits, but can be less.\r
132          * \r
133          * @param scores\r
134          *            the list of scores to output\r
135          * @param output\r
136          *            the stream to output the data to\r
137          * @throws IOException\r
138          *             if the OutputStream cannot be written into\r
139          * @throws NullPointerException\r
140          *             if the output stream is null\r
141          */\r
142         public static void write(Set<Score> scores, OutputStream output)\r
143                         throws IOException {\r
144                 if (output == null) {\r
145                         throw new NullPointerException("OutputStream must be provided!");\r
146                 }\r
147                 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(\r
148                                 output));\r
149                 for (Score score : scores) {\r
150                         writer.write("#" + score.method + " ");\r
151                         for (Float scoreVal : score.getScores()) {\r
152                                 writer.write(NUMBER_FORMAT.format(scoreVal) + " ");\r
153                         }\r
154                         writer.write("\n");\r
155                 }\r
156                 writer.flush();\r
157         }\r
158 }\r