2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
23 import jalview.api.analysis.ScoreModelI;
24 import jalview.api.analysis.SimilarityParamsI;
25 import jalview.datamodel.SequenceNode;
26 import jalview.viewmodel.AlignmentViewport;
29 * This class implements distance calculations used in constructing a Average
30 * Distance tree (also known as UPGMA)
32 public class AverageDistanceTree extends TreeBuilder
39 * @param scoreParameters
41 public AverageDistanceTree(AlignmentViewport av, ScoreModelI sm,
42 SimilarityParamsI scoreParameters)
44 super(av, sm, scoreParameters);
48 * Calculates and saves the distance between the combination of cluster(i) and
49 * cluster(j) and all other clusters. An average of the distances from
50 * cluster(i) and cluster(j) is calculated, weighted by the sizes of each
57 protected void findClusterDistance(int i, int j)
59 int noi = clusters.elementAt(i).cardinality();
60 int noj = clusters.elementAt(j).cardinality();
62 // New distances from cluster i to others
63 double[] newdist = new double[noseqs];
65 for (int l = 0; l < noseqs; l++)
67 if ((l != i) && (l != j))
69 newdist[l] = ((distances.getValue(i, l) * noi)
70 + (distances.getValue(j, l) * noj)) / (noi + noj);
78 for (int ii = 0; ii < noseqs; ii++)
80 distances.setValue(i, ii, newdist[ii]);
81 distances.setValue(ii, i, newdist[ii]);
89 protected double findMinDistance()
91 double min = Double.MAX_VALUE;
93 for (int i = 0; i < (noseqs - 1); i++)
95 for (int j = i + 1; j < noseqs; j++)
97 if (!done.get(i) && !done.get(j))
99 if (distances.getValue(i, j) < min)
104 min = distances.getValue(i, j);
116 protected void findNewDistances(SequenceNode nodei, SequenceNode nodej,
122 SequenceNode sni = nodei;
123 SequenceNode snj = nodej;
128 sni = (SequenceNode) sni.left();
134 snj = (SequenceNode) snj.left();
137 nodei.dist = ((dist / 2) - ih);
138 nodej.dist = ((dist / 2) - jh);