X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=datamodel%2Fcompbio%2Fdata%2Fsequence%2FScore.java;h=98ba06a5947da4fe3359bbc0b5b6048612ef5f8e;hb=14a41b0acd5d808ae1cdd8ecc82c85b4d3329b72;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..98ba06a 100644 --- a/datamodel/compbio/data/sequence/Score.java +++ b/datamodel/compbio/data/sequence/Score.java @@ -1,19 +1,40 @@ package compbio.data.sequence; +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.Arrays; import java.util.List; +import java.util.Locale; +import java.util.Set; 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); + } + + private ConservationMethod method; private List scores; @@ -21,15 +42,35 @@ public class Score { // JaXB default constructor } - public Score(Method method, List scores) { + /** + * 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(ConservationMethod method, List scores) { this.method = method; - this.scores = scores; + this.scores = new ArrayList(scores); } - public Method getMethod() { + /** + * Returns the ConservationMethod + * + * @return the ConservationMethod + */ + public ConservationMethod getMethod() { return method; } + /** + * The column scores for the alignment + * + * @return the column scores for the alignment + */ public List getScores() { return scores; } @@ -74,4 +115,47 @@ public class Score { 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(); + } }