1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/* 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.IOException;
import java.io.Writer;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
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 implements Comparable<Score> {
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<Range> ranges = new TreeSet<Range>();
private ArrayList<Float> scores = new ArrayList<Float>(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<Float> scores) {
this.method = method.toString();
this.scores = new ArrayList<Float>(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<Float> scores, TreeSet<Range> ranges) {
this.method = method.toString();
this.ranges = ranges;
this.scores = scores;
}
public Score(Enum<?> method, TreeSet<Range> ranges) {
this.method = method.toString();
this.ranges = ranges;
}
public Score(Enum<?> method, float[] scores) {
this.method = method.toString();
this.scores = toList(scores);
}
private ArrayList<Float> toList(float[] values) {
ArrayList<Float> vlist = new ArrayList<Float>();
for (float v : values) {
vlist.add(new Float(v));
}
return vlist;
}
/**
* Returns the ConservationMethod
*
* @return the ConservationMethod
*/
public String getMethod() {
return method;
}
/**
* The column scores for the alignment
*
* @return the column scores for the alignment
*/
public ArrayList<Float> getScores() {
return scores;
}
/**
* Return Ranges if any Collections.EMPTY_SET otherwise
*
* @return ordered set of Range
*/
public TreeSet<Range> getRanges() {
return ranges;
}
public void setRanges(TreeSet<Range> 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:
*
* <pre>
* {@code
* #MethodName [comma separated list of ranges] <space separated list of values>
*
* 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
* #COILS 22-33, 44-56 0.121 3.212
* }
* </pre>
*
* The maximum precision for values is 3 digits, but can be less.
*
* @param scores
* the list of scores to output
* @param writer
* @throws IOException
* if the OutputStream cannot be written into
* @throws NullPointerException
* if the output stream is null
*/
public static void write(TreeSet<Score> 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();
}
// Old compareTo method
// @Override
// public int compareTo(Score o) {
// return this.method.compareTo(o.method);
// }
/* daniel messed with this method. While preserving the original
* ordering when the method Enumerations are different, additional
* constraints have been added on how equal Score objects must be
* to be considered equal.
*
* It is necessary to distinguish Score objects by their ranges in order
* to use a Set of Score objects to represent the alifold.out information
*
* Distinguishing score objects by their Scores has the result of ordering
* the base pair contacts so into descending probability.
*
* Should this be in a new extended Score class?
*/
@Override
public int compareTo(Score o) {
if (this.method.compareTo(o.method) != 0) {
return this.method.compareTo(o.method);
}
int pass;
pass = new Integer(this.scores.size()).compareTo(
new Integer(o.scores.size()));
if (pass != 0) return pass;
for (int i = 0; i < this.scores.size(); i++) {
pass = this.scores.get(i).compareTo(o.scores.get(i));
if (pass != 0) {
return pass*-1; // descending order
}
}
pass = new Integer(this.ranges.size()).compareTo(
new Integer(o.ranges.size()));
if (pass != 0) return pass;
Iterator<Range> thisRange = this.ranges.iterator();
Iterator<Range> oRange = o.ranges.iterator();
for (int i = 0; i < this.ranges.size(); i++) {
Range tR = thisRange.next();
Range oR = oRange.next();
if (tR.compareTo(oR) != 0) {
return tR.compareTo(oR);
}
}
return 0;
}
}
|