1 /* Copyright (c) 2011 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.data.sequence;
\r
20 import java.io.IOException;
\r
21 import java.io.Writer;
\r
22 import java.text.NumberFormat;
\r
23 import java.util.ArrayList;
\r
24 import java.util.Locale;
\r
25 import java.util.TreeSet;
\r
27 import javax.xml.bind.annotation.XmlAccessType;
\r
28 import javax.xml.bind.annotation.XmlAccessorType;
\r
30 import compbio.util.annotation.Immutable;
\r
33 * A value class for AACon annotation results storage. The objects of this type
\r
39 @XmlAccessorType(XmlAccessType.FIELD)
\r
41 public class Score implements Comparable<Score> {
\r
43 static final NumberFormat NUMBER_FORMAT = NumberFormat
\r
44 .getNumberInstance(Locale.UK);
\r
46 NUMBER_FORMAT.setGroupingUsed(false);
\r
47 NUMBER_FORMAT.setMaximumFractionDigits(3);
\r
49 // This should be Enum<?> but JAXB cannot serialize it.
\r
50 private final String method;
\r
52 private TreeSet<Range> ranges = new TreeSet<Range>();
\r
54 private ArrayList<Float> scores = new ArrayList<Float>(0);
\r
57 // JaXB default constructor
\r
62 * Instantiate the Score
\r
65 * the ConservationMethod with which {@code scores} were
\r
68 * the actual conservation values for each column of the
\r
71 public Score(Enum<?> method, ArrayList<Float> scores) {
\r
72 this.method = method.toString();
\r
73 this.scores = new ArrayList<Float>(scores);
\r
78 * the ConservationMethod with which {@code scores} were
\r
81 * the actual conservation values for each column of the
\r
84 * The set of ranges i.e. parts of the sequence with specific
\r
85 * function, usually can be calculated based on scores
\r
87 public Score(Enum<?> method, ArrayList<Float> scores, TreeSet<Range> ranges) {
\r
88 this.method = method.toString();
\r
89 this.ranges = ranges;
\r
90 this.scores = scores;
\r
93 public Score(Enum<?> method, TreeSet<Range> ranges) {
\r
94 this.method = method.toString();
\r
95 this.ranges = ranges;
\r
98 public Score(Enum<?> method, float[] scores) {
\r
99 this.method = method.toString();
\r
100 this.scores = toList(scores);
\r
103 private ArrayList<Float> toList(float[] values) {
\r
104 ArrayList<Float> vlist = new ArrayList<Float>();
\r
105 for (float v : values) {
\r
106 vlist.add(new Float(v));
\r
111 * Returns the ConservationMethod
\r
113 * @return the ConservationMethod
\r
115 public String getMethod() {
\r
120 * The column scores for the alignment
\r
122 * @return the column scores for the alignment
\r
124 public ArrayList<Float> getScores() {
\r
129 * Return Ranges if any Collections.EMPTY_SET otherwise
\r
131 * @return ordered set of Range
\r
133 public TreeSet<Range> getRanges() {
\r
137 public void setRanges(TreeSet<Range> ranges) {
\r
138 this.ranges = ranges;
\r
142 public String toString() {
\r
143 return "Score [method=" + method + ", ranges=" + ranges + ", scores="
\r
148 public int hashCode() {
\r
149 final int prime = 7;
\r
151 result = prime * result + ((method == null) ? 0 : method.hashCode());
\r
152 result = prime * result + ((ranges == null) ? 0 : ranges.hashCode());
\r
153 result = prime * result + ((scores == null) ? 0 : scores.hashCode());
\r
158 public boolean equals(Object obj) {
\r
163 if (getClass() != obj.getClass())
\r
165 Score other = (Score) obj;
\r
166 if (method == null) {
\r
167 if (other.method != null)
\r
169 } else if (!method.equals(other.method))
\r
171 if (ranges == null) {
\r
172 if (other.ranges != null)
\r
174 } else if (!ranges.equals(other.ranges))
\r
176 if (scores == null) {
\r
177 if (other.scores != null)
\r
179 } else if (!scores.equals(other.scores))
\r
185 * Outputs the List of Score objects into the Output stream. The output
\r
186 * format is as follows:
\r
190 * #MethodName <space separated list of values>
\r
194 * #KABAT 0.2 0.3 0.2 0 0.645 0.333 1 1 0 0
\r
195 * #SMERFS 0.645 0.333 1 1 0 0 0.2 0.3 0.2 0
\r
199 * The maximum precision for values is 3 digits, but can be less.
\r
202 * the list of scores to output
\r
204 * @throws IOException
\r
205 * if the OutputStream cannot be written into
\r
206 * @throws NullPointerException
\r
207 * if the output stream is null
\r
209 public static void write(TreeSet<Score> scores, Writer writer)
\r
210 throws IOException {
\r
211 if (writer == null) {
\r
212 throw new NullPointerException("Writer must be provided!");
\r
214 for (Score score : scores) {
\r
215 writer.write("#" + score.method + " ");
\r
216 int count = score.ranges.size();
\r
217 for (Range range : score.ranges) {
\r
219 writer.write(range.toString());
\r
221 writer.write(", ");
\r
224 for (Float scoreVal : score.scores) {
\r
225 writer.write(NUMBER_FORMAT.format(scoreVal) + " ");
\r
227 writer.write("\n");
\r
234 public int compareTo(Score o) {
\r
235 return this.method.compareTo(o.method);
\r