| 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 |
package dr.evolution.distance; |
| 27 |
|
| 28 |
import dr.evolution.alignment.PatternList; |
| 29 |
import dr.evolution.datatype.DataType; |
| 30 |
import dr.evolution.util.Taxon; |
| 31 |
import dr.evolution.util.TaxonList; |
| 32 |
import dr.matrix.Matrix; |
| 33 |
|
| 34 |
import java.util.*; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class DistanceMatrix extends Matrix.AbstractMatrix implements TaxonList { |
| 44 |
|
| 45 |
public static final double MAX_DISTANCE = 1000.0; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public DistanceMatrix() { |
| 51 |
super(); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public DistanceMatrix(TaxonList taxa) { |
| 58 |
super(); |
| 59 |
this.taxa = taxa; |
| 60 |
dimension = taxa.getTaxonCount(); |
| 61 |
distances = new double[dimension][dimension]; |
| 62 |
distancesKnown = true; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public DistanceMatrix(PatternList patterns) { |
| 69 |
super(); |
| 70 |
setPatterns(patterns); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public void setPatterns(PatternList patterns) { |
| 77 |
this.taxa = patterns; |
| 78 |
this.patterns = patterns; |
| 79 |
dimension = patterns.getTaxonCount(); |
| 80 |
dataType = patterns.getDataType(); |
| 81 |
distancesKnown = false; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public int getRowCount() { |
| 88 |
return dimension; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public int getColumnCount() { |
| 95 |
return dimension; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public double getElement(int row, int column) { |
| 102 |
|
| 103 |
if (!distancesKnown) { |
| 104 |
calculateDistances(); |
| 105 |
} |
| 106 |
|
| 107 |
return distances[row][column]; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public void setElement(int row, int column, double value) { |
| 114 |
|
| 115 |
if (!distancesKnown) { |
| 116 |
calculateDistances(); |
| 117 |
} |
| 118 |
|
| 119 |
distances[row][column] = value; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public void calculateDistances() { |
| 126 |
distances = new double[dimension][dimension]; |
| 127 |
|
| 128 |
for (int i = 0; i < dimension; i++) { |
| 129 |
for (int j = i + 1; j < dimension; j++) { |
| 130 |
distances[i][j] = calculatePairwiseDistance(i, j); |
| 131 |
distances[j][i] = distances[i][j]; |
| 132 |
} |
| 133 |
distances[i][i] = 0.0; |
| 134 |
} |
| 135 |
|
| 136 |
distancesKnown = true; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
protected double calculatePairwiseDistance(int taxon1, int taxon2) { |
| 143 |
int state1, state2; |
| 144 |
|
| 145 |
int n = patterns.getPatternCount(); |
| 146 |
double weight, distance; |
| 147 |
double sumDistance = 0.0; |
| 148 |
double sumWeight = 0.0; |
| 149 |
|
| 150 |
int[] pattern; |
| 151 |
|
| 152 |
for (int i = 0; i < n; i++) { |
| 153 |
pattern = patterns.getPattern(i); |
| 154 |
|
| 155 |
state1 = pattern[taxon1]; |
| 156 |
state2 = pattern[taxon2]; |
| 157 |
|
| 158 |
weight = patterns.getPatternWeight(i); |
| 159 |
|
| 160 |
if (!dataType.isAmbiguousState(state1) && !dataType.isAmbiguousState(state2) && |
| 161 |
state1 != state2) { |
| 162 |
sumDistance += weight; |
| 163 |
} |
| 164 |
sumWeight += weight; |
| 165 |
} |
| 166 |
|
| 167 |
distance = sumDistance / sumWeight; |
| 168 |
|
| 169 |
return distance; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public double getMeanDistance() { |
| 177 |
if (!distancesKnown) { |
| 178 |
calculateDistances(); |
| 179 |
} |
| 180 |
|
| 181 |
double dist = 0.0; |
| 182 |
int count = 0; |
| 183 |
for (int i = 0; i < dimension; i++) { |
| 184 |
for (int j = 0; j < dimension; j++) { |
| 185 |
if (i != j) { |
| 186 |
dist += distances[i][j]; |
| 187 |
count += 1; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
return dist / (double) count; |
| 192 |
} |
| 193 |
|
| 194 |
public String toString() { |
| 195 |
try { |
| 196 |
double[] dists = getUpperTriangle(); |
| 197 |
StringBuffer buffer = new StringBuffer(String.valueOf(dists[0])); |
| 198 |
|
| 199 |
for (int i = 1; i < dists.length; i++) { |
| 200 |
buffer.append(", ").append(String.valueOf(dists[i])); |
| 201 |
} |
| 202 |
return buffer.toString(); |
| 203 |
} catch (Matrix.NotSquareException e) { |
| 204 |
return e.toString(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public int getTaxonCount() { |
| 216 |
return taxa.getTaxonCount(); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public Taxon getTaxon(int taxonIndex) { |
| 223 |
return taxa.getTaxon(taxonIndex); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public String getTaxonId(int taxonIndex) { |
| 230 |
return taxa.getTaxonId(taxonIndex); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public int getTaxonIndex(String id) { |
| 237 |
return taxa.getTaxonIndex(id); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public int getTaxonIndex(Taxon taxon) { |
| 244 |
return taxa.getTaxonIndex(taxon); |
| 245 |
} |
| 246 |
|
| 247 |
public List<Taxon> asList() { |
| 248 |
List<Taxon> taxa = new ArrayList<Taxon>(); |
| 249 |
for (int i = 0, n = getTaxonCount(); i < n; i++) { |
| 250 |
taxa.add(getTaxon(i)); |
| 251 |
} |
| 252 |
return taxa; |
| 253 |
} |
| 254 |
|
| 255 |
public Iterator<Taxon> iterator() { |
| 256 |
return new Iterator<Taxon>() { |
| 257 |
private int index = -1; |
| 258 |
|
| 259 |
public boolean hasNext() { |
| 260 |
return index < getTaxonCount() - 1; |
| 261 |
} |
| 262 |
|
| 263 |
public Taxon next() { |
| 264 |
index ++; |
| 265 |
return getTaxon(index); |
| 266 |
} |
| 267 |
|
| 268 |
public void remove() { } |
| 269 |
}; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public Object getTaxonAttribute(int taxonIndex, String name) { |
| 278 |
return taxa.getTaxonAttribute(taxonIndex, name); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
protected String id = null; |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public String getId() { |
| 291 |
return id; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public void setId(String id) { |
| 298 |
this.id = id; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
protected DataType dataType = null; |
| 306 |
int dimension = 0; |
| 307 |
boolean distancesKnown; |
| 308 |
private double[][] distances = null; |
| 309 |
protected PatternList patterns = null; |
| 310 |
private TaxonList taxa = null; |
| 311 |
} |