X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=datamodel%2Fcompbio%2Fdata%2Fsequence%2FScore.java;h=ec4cadc4a5e2a8650e1376bd4f681aad1b5f64d8;hb=ba2d4e920e433bd080f448248e7dacade69a27a0;hp=b61c74e4f17703a680c4279149a58ec34c3dc531;hpb=7e625752237ee07e815bfb28048bb0b1193b45c5;p=jabaws.git diff --git a/datamodel/compbio/data/sequence/Score.java b/datamodel/compbio/data/sequence/Score.java index b61c74e..ec4cadc 100644 --- a/datamodel/compbio/data/sequence/Score.java +++ b/datamodel/compbio/data/sequence/Score.java @@ -1,19 +1,31 @@ +/* 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.io.BufferedWriter; import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; +import java.io.Writer; 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 javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlTransient; import compbio.util.annotation.Immutable; @@ -26,27 +38,24 @@ import compbio.util.annotation.Immutable; */ @XmlAccessorType(XmlAccessType.FIELD) @Immutable -public class Score { +public class Score implements Comparable { - @XmlTransient 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 Enum method; - // private String method; - - @XmlElement private TreeSet ranges = new TreeSet(); - @XmlElement private ArrayList scores = new ArrayList(0); private Score() { // JaXB default constructor + method = ""; } /** @@ -60,7 +69,7 @@ public class Score { * alignment */ public Score(Enum method, ArrayList scores) { - this.method = method; + this.method = method.toString(); this.scores = new ArrayList(scores); } @@ -76,18 +85,18 @@ public class Score { * function, usually can be calculated based on scores */ public Score(Enum method, ArrayList scores, TreeSet ranges) { - this.method = method; + this.method = method.toString(); this.ranges = ranges; this.scores = scores; } public Score(Enum method, TreeSet ranges) { - this.method = method; + this.method = method.toString(); this.ranges = ranges; } public Score(Enum method, float[] scores) { - this.method = method; + this.method = method.toString(); this.scores = toList(scores); } @@ -103,7 +112,7 @@ public class Score { * * @return the ConservationMethod */ - public Enum getMethod() { + public String getMethod() { return method; } @@ -119,7 +128,7 @@ public class Score { /** * Return Ranges if any Collections.EMPTY_SET otherwise * - * @return + * @return ordered set of Range */ public TreeSet getRanges() { return ranges; @@ -191,27 +200,38 @@ public class Score { * * @param scores * the list of scores to output - * @param output - * the stream to output the data to + * @param writer * @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) + public static void write(TreeSet scores, Writer writer) throws IOException { - if (output == null) { - throw new NullPointerException("OutputStream must be provided!"); + if (writer == null) { + throw new NullPointerException("Writer must be provided!"); } - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( - output)); for (Score score : scores) { writer.write("#" + score.method + " "); - for (Float scoreVal : score.getScores()) { + 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); + } }