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.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; /** * A value class for AACon annotation results storage. The objects of this type * are immutable * * @author pvtroshin * */ @XmlAccessorType(XmlAccessType.FIELD) @Immutable public class Score { @XmlTransient static final NumberFormat NUMBER_FORMAT = NumberFormat .getNumberInstance(Locale.UK); static { NUMBER_FORMAT.setGroupingUsed(false); NUMBER_FORMAT.setMaximumFractionDigits(3); } private Enum method; // private String method; @XmlElement private TreeSet ranges = new TreeSet(); @XmlElement private ArrayList scores = new ArrayList(0); private Score() { // JaXB default constructor } /** * 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; this.scores = new ArrayList(scores); } /** * @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; this.ranges = ranges; this.scores = scores; } public Score(Enum method, TreeSet ranges) { this.method = method; this.ranges = ranges; } public Score(Enum method, float[] scores) { this.method = method; 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 Enum getMethod() { return method; } /** * 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 + ", ranges=" + ranges + ", scores=" + scores + "]"; } @Override public int hashCode() { 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; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Score other = (Score) obj; if (method == null) { if (other.method != null) return false; } else if (!method.equals(other.method)) return false; if (ranges == null) { if (other.ranges != null) return false; } else if (!ranges.equals(other.ranges)) return false; 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(); } }