X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=datamodel%2Fcompbio%2Fdata%2Fsequence%2FScore.java;h=f28bd0d6a34934551efb015baaa8290939a63e28;hb=aca9c5503b812c9c96aebb408748dc15728f9ad7;hp=8a58db3e78075ce8a64458c2bd50c49d29990636;hpb=212bbd43c19f645cccef34a608dc001fb694833c;p=jabaws.git diff --git a/datamodel/compbio/data/sequence/Score.java b/datamodel/compbio/data/sequence/Score.java index 8a58db3..f28bd0d 100644 --- a/datamodel/compbio/data/sequence/Score.java +++ b/datamodel/compbio/data/sequence/Score.java @@ -1,58 +1,145 @@ package compbio.data.sequence; -import java.util.Arrays; -import java.util.List; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.text.NumberFormat; +import java.util.ArrayList; +import java.util.Locale; +import java.util.Set; +import java.util.TreeSet; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; -import compbio.conservation.Method; import compbio.util.annotation.Immutable; +/** + * A value class for AACon annotation results storage. The objects of this type + * are immutable + * + * @author pvtroshin + * + */ @XmlAccessorType(XmlAccessType.FIELD) @Immutable public class Score { - private Method method; + static final NumberFormat NUMBER_FORMAT = NumberFormat + .getNumberInstance(Locale.UK); + static { + NUMBER_FORMAT.setGroupingUsed(false); + NUMBER_FORMAT.setMaximumFractionDigits(3); + } + // This should be Enum but JAXB cannot serialize it. + private final String method; + + private TreeSet ranges = new TreeSet(); - private List scores; + private ArrayList scores = new ArrayList(0); private Score() { // JaXB default constructor + method = null; + } + + /** + * Instantiate the Score + * + * @param method + * the ConservationMethod with which {@code scores} were + * calculated + * @param scores + * the actual conservation values for each column of the + * alignment + */ + public Score(Enum method, ArrayList scores) { + this.method = method.toString(); + this.scores = new ArrayList(scores); } - public Score(Method method, List scores) { - this.method = method; + /** + * @param method + * the ConservationMethod with which {@code scores} were + * calculated + * @param scores + * the actual conservation values for each column of the + * alignment + * @param ranges + * The set of ranges i.e. parts of the sequence with specific + * function, usually can be calculated based on scores + */ + public Score(Enum method, ArrayList scores, TreeSet ranges) { + this.method = method.toString(); + this.ranges = ranges; this.scores = scores; } - public Method getMethod() { + public Score(Enum method, TreeSet ranges) { + this.method = method.toString(); + this.ranges = ranges; + } + + public Score(Enum method, float[] scores) { + this.method = method.toString(); + this.scores = toList(scores); + } + + private ArrayList toList(float[] values) { + ArrayList vlist = new ArrayList(); + for (float v : values) { + vlist.add(new Float(v)); + } + return vlist; + } + /** + * Returns the ConservationMethod + * + * @return the ConservationMethod + */ + public String getMethod() { return method; } - public List getScores() { + /** + * The column scores for the alignment + * + * @return the column scores for the alignment + */ + public ArrayList getScores() { return scores; } + /** + * Return Ranges if any Collections.EMPTY_SET otherwise + * + * @return + */ + public TreeSet getRanges() { + return ranges; + } + + public void setRanges(TreeSet ranges) { + this.ranges = ranges; + } + @Override public String toString() { - return "Score [method=" + method + ", scores=" + scores + "]"; + return "Score [method=" + method + ", ranges=" + ranges + ", scores=" + + scores + "]"; } @Override public int hashCode() { - final int prime = 31; + final int prime = 7; int result = 1; result = prime * result + ((method == null) ? 0 : method.hashCode()); + result = prime * result + ((ranges == null) ? 0 : ranges.hashCode()); result = prime * result + ((scores == null) ? 0 : scores.hashCode()); return result; } - /* - * TODO test ! (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ @Override public boolean equals(Object obj) { if (this == obj) @@ -62,16 +149,64 @@ public class Score { if (getClass() != obj.getClass()) return false; Score other = (Score) obj; - if (method != other.method) + if (method == null) { + if (other.method != null) + return false; + } else if (!method.equals(other.method)) return false; - if (scores == other.scores) { - return true; - } - if (scores == null) { + if (ranges == null) { + if (other.ranges != null) + return false; + } else if (!ranges.equals(other.ranges)) return false; - } - if (!Arrays.deepEquals(scores.toArray(), other.scores.toArray())) + if (scores == null) { + if (other.scores != null) + return false; + } else if (!scores.equals(other.scores)) return false; return true; } + + /** + * Outputs the List of Score objects into the Output stream. The output + * format is as follows: + * + *
+	 * {@code
+	 * #MethodName 
+	 * 	  
+	 * For example:
+	 * 	 
+	 * #KABAT 0.2 0.3 0.2 0 0.645 0.333 1 1 0 0
+	 * #SMERFS 0.645 0.333 1 1 0 0 0.2 0.3 0.2 0
+	 * }
+	 * 
+ * + * The maximum precision for values is 3 digits, but can be less. + * + * @param scores + * the list of scores to output + * @param output + * the stream to output the data to + * @throws IOException + * if the OutputStream cannot be written into + * @throws NullPointerException + * if the output stream is null + */ + public static void write(Set scores, OutputStream output) + throws IOException { + if (output == null) { + throw new NullPointerException("OutputStream must be provided!"); + } + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( + output)); + for (Score score : scores) { + writer.write("#" + score.method + " "); + for (Float scoreVal : score.getScores()) { + writer.write(NUMBER_FORMAT.format(scoreVal) + " "); + } + writer.write("\n"); + } + writer.flush(); + } }