X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=datamodel%2Fcompbio%2Fdata%2Fsequence%2FScore.java;h=c8cbfb4d38d34cd795eb2a109b5fb975a4acdd5e;hb=96ab3a201572c3ed7f9a27682bd0b6ca7edb34c7;hp=3416e78be70e629aa2ac968fbd357434979c3f16;hpb=27e1fc31d6e3cc48e5abddcc939f3c73b11fcd55;p=jabaws.git diff --git a/datamodel/compbio/data/sequence/Score.java b/datamodel/compbio/data/sequence/Score.java index 3416e78..c8cbfb4 100644 --- a/datamodel/compbio/data/sequence/Score.java +++ b/datamodel/compbio/data/sequence/Score.java @@ -1,57 +1,159 @@ +/* Copyright (c) 2011 Peter Troshin + * + * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0 + * + * This library is free software; you can redistribute it and/or modify it under the terms of the + * Apache License version 2 as published by the Apache Software Foundation + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache + * License for more details. + * + * A copy of the license is in apache_license.txt. It is also available here: + * @see: http://www.apache.org/licenses/LICENSE-2.0.txt + * + * Any republication or derived work distributed in source code form + * must include this copyright and license notice. + */ package compbio.data.sequence; -import java.util.Arrays; -import java.util.List; +import java.io.IOException; +import java.io.Writer; +import java.text.NumberFormat; +import java.util.ArrayList; +import java.util.Locale; +import java.util.TreeSet; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; 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 { +public class Score implements Comparable { - 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 = ""; + } + + /** + * 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) @@ -61,16 +163,76 @@ 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(TreeSet scores, Writer writer) + throws IOException { + if (writer == null) { + throw new NullPointerException("Writer must be provided!"); + } + for (Score score : scores) { + writer.write("#" + score.method + " "); + int count = score.ranges.size(); + for (Range range : score.ranges) { + count--; + writer.write(range.toString()); + if (count != 0) { + writer.write(", "); + } + } + for (Float scoreVal : score.scores) { + writer.write(NUMBER_FORMAT.format(scoreVal) + " "); + } + writer.write("\n"); + writer.flush(); + } + writer.flush(); + } + + @Override + public int compareTo(Score o) { + return this.method.compareTo(o.method); + } }