Debian Codegraph @

src/dr/evolution/distance/DistanceMatrix.java

raw symbols SCIP
1 /*
2 * DistanceMatrix.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * storage for pairwise distance matrices.<p>
38 *
39 * @author Andrew Rambaut
40 * @author Alexei Drummond
41 * @version $Id: DistanceMatrix.java,v 1.23 2005/07/11 14:06:25 rambaut Exp $
42 */
43 public class DistanceMatrix extends Matrix.AbstractMatrix implements TaxonList {
44
45 public static final double MAX_DISTANCE = 1000.0;
46
47 /**
48 * constructor
49 */
50 public DistanceMatrix() {
51 super();
52 }
53
54 /**
55 * constructor taking a dimension
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 * constructor taking a pattern source
67 */
68 public DistanceMatrix(PatternList patterns) {
69 super();
70 setPatterns(patterns);
71 }
72
73 /**
74 * set the pattern source
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 * @return the number of rows
86 */
87 public int getRowCount() {
88 return dimension;
89 }
90
91 /**
92 * @return the number of columns
93 */
94 public int getColumnCount() {
95 return dimension;
96 }
97
98 /**
99 * @return an element
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 * set an element - this overwrites any existing elements
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 * Calculate the distances
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 * Calculate a pairwise distance
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 // sumDistance += dataType.getObservedDistance(state1, state2) * weight;
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 * Returns the mean pairwise distance of this matrix
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 // TaxonList IMPLEMENTATION
210 // **************************************************************
211
212 /**
213 * @return a count of the number of taxa in the list.
214 */
215 public int getTaxonCount() {
216 return taxa.getTaxonCount();
217 }
218
219 /**
220 * @return the ith taxon.
221 */
222 public Taxon getTaxon(int taxonIndex) {
223 return taxa.getTaxon(taxonIndex);
224 }
225
226 /**
227 * @return the ID of the ith taxon.
228 */
229 public String getTaxonId(int taxonIndex) {
230 return taxa.getTaxonId(taxonIndex);
231 }
232
233 /**
234 * returns the index of the taxon with the given id.
235 */
236 public int getTaxonIndex(String id) {
237 return taxa.getTaxonIndex(id);
238 }
239
240 /**
241 * returns the index of the given taxon.
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() { /* do nothing */ }
269 };
270 }
271
272 /**
273 * @param taxonIndex the index of the taxon whose attribute is being fetched.
274 * @param name the name of the attribute of interest.
275 * @return an object representing the named attributed for the given taxon.
276 */
277 public Object getTaxonAttribute(int taxonIndex, String name) {
278 return taxa.getTaxonAttribute(taxonIndex, name);
279 }
280
281 // **************************************************************
282 // Identifiable IMPLEMENTATION
283 // **************************************************************
284
285 protected String id = null;
286
287 /**
288 * @return the id.
289 */
290 public String getId() {
291 return id;
292 }
293
294 /**
295 * Sets the id.
296 */
297 public void setId(String id) {
298 this.id = id;
299 }
300
301 //
302 // Private stuff
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 }