1 package compbio.data.sequence;
\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
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
18 import compbio.util.annotation.Immutable;
\r
21 * A value class for AACon annotation results storage. The objects of this type
\r
27 @XmlAccessorType(XmlAccessType.FIELD)
\r
29 public class Score {
\r
32 static final NumberFormat NUMBER_FORMAT = NumberFormat
\r
33 .getNumberInstance(Locale.UK);
\r
35 NUMBER_FORMAT.setGroupingUsed(false);
\r
36 NUMBER_FORMAT.setMaximumFractionDigits(3);
\r
39 private Enum<?> method;
\r
40 // private String method;
\r
43 private TreeSet<Range> ranges = new TreeSet<Range>();
\r
46 private ArrayList<Float> scores = new ArrayList<Float>(0);
\r
49 // JaXB default constructor
\r
53 * Instantiate the Score
\r
56 * the ConservationMethod with which {@code scores} were
\r
59 * the actual conservation values for each column of the
\r
62 public Score(Enum<?> method, ArrayList<Float> scores) {
\r
63 this.method = method;
\r
64 this.scores = new ArrayList<Float>(scores);
\r
69 * the ConservationMethod with which {@code scores} were
\r
72 * the actual conservation values for each column of the
\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
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
84 public Score(Enum<?> method, TreeSet<Range> ranges) {
\r
85 this.method = method;
\r
86 this.ranges = ranges;
\r
89 public Score(Enum<?> method, float[] scores) {
\r
90 this.method = method;
\r
91 this.scores = toList(scores);
\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
102 * Returns the ConservationMethod
\r
104 * @return the ConservationMethod
\r
106 public Enum<?> getMethod() {
\r
111 * The column scores for the alignment
\r
113 * @return the column scores for the alignment
\r
115 public ArrayList<Float> getScores() {
\r
120 * Return Ranges if any Collections.EMPTY_SET otherwise
\r
124 public TreeSet<Range> getRanges() {
\r
128 public void setRanges(TreeSet<Range> ranges) {
\r
129 this.ranges = ranges;
\r
133 public String toString() {
\r
134 return "Score [method=" + method + ", ranges=" + ranges + ", scores="
\r
139 public int hashCode() {
\r
140 final int prime = 7;
\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
149 public boolean equals(Object obj) {
\r
154 if (getClass() != obj.getClass())
\r
156 Score other = (Score) obj;
\r
157 if (method == null) {
\r
158 if (other.method != null)
\r
160 } else if (!method.equals(other.method))
\r
162 if (ranges == null) {
\r
163 if (other.ranges != null)
\r
165 } else if (!ranges.equals(other.ranges))
\r
167 if (scores == null) {
\r
168 if (other.scores != null)
\r
170 } else if (!scores.equals(other.scores))
\r
176 * Outputs the List of Score objects into the Output stream. The output
\r
177 * format is as follows:
\r
181 * #MethodName <space separated list of values>
\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
190 * The maximum precision for values is 3 digits, but can be less.
\r
193 * the list of scores to 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
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
206 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
\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
213 writer.write("\n");
\r