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 java.util.ArrayList;
24 import java.util.BitSet;
25 import java.util.List;
26 import java.util.Vector;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.BinaryNode;
30 import jalview.datamodel.ContactListI;
31 import jalview.datamodel.ContactMatrixI;
32 import jalview.math.Matrix;
33 import jalview.viewmodel.AlignmentViewport;
36 * This class implements distance calculations used in constructing a Average
37 * Distance tree (also known as UPGMA)
39 public class AverageDistanceEngine extends TreeEngine
43 AlignmentAnnotation aa;
45 * compute cosine distance matrix for a given contact matrix and create a UPGMA tree
48 public AverageDistanceEngine(AlignmentViewport av, AlignmentAnnotation aa, ContactMatrixI cm)
53 node = new Vector<BinaryNode>();
54 clusters = new Vector<BitSet>();
55 distances = new Matrix(new double[cm.getWidth()][cm.getWidth()]);
58 for (int i=0;i<cm.getWidth();i++)
60 // init the tree engine node for this column
61 BinaryNode cnode = new BinaryNode();
62 cnode.setElement(Integer.valueOf(i));
64 node.addElement(cnode);
65 BitSet bs = new BitSet();
67 clusters.addElement(bs);
69 // compute distance matrix element
70 ContactListI ith=cm.getContactList(i);
73 ContactListI jth = cm.getContactList(j);
75 for (int indx=0;indx<cm.getHeight();indx++)
77 prd+=ith.getContactAt(indx)*jth.getContactAt(indx);
79 distances.setValue(i, j, prd);
80 distances.setValue(j, i, prd);
84 noClus = clusters.size();
89 * Calculates and saves the distance between the combination of cluster(i) and
90 * cluster(j) and all other clusters. An average of the distances from
91 * cluster(i) and cluster(j) is calculated, weighted by the sizes of each
98 protected void findClusterDistance(int i, int j)
100 int noi = clusters.elementAt(i).cardinality();
101 int noj = clusters.elementAt(j).cardinality();
103 // New distances from cluster i to others
104 double[] newdist = new double[noseqs];
106 for (int l = 0; l < noseqs; l++)
108 if ((l != i) && (l != j))
110 newdist[l] = ((distances.getValue(i, l) * noi)
111 + (distances.getValue(j, l) * noj)) / (noi + noj);
119 for (int ii = 0; ii < noseqs; ii++)
121 distances.setValue(i, ii, newdist[ii]);
122 distances.setValue(ii, i, newdist[ii]);
130 protected double findMinDistance()
132 double min = Double.MAX_VALUE;
134 for (int i = 0; i < (noseqs - 1); i++)
136 for (int j = i + 1; j < noseqs; j++)
138 if (!done.get(i) && !done.get(j))
140 if (distances.getValue(i, j) < min)
145 min = distances.getValue(i, j);
157 protected void findNewDistances(BinaryNode nodei, BinaryNode nodej,
163 BinaryNode sni = nodei;
164 BinaryNode snj = nodej;
169 sni = (BinaryNode) sni.left();
175 snj = (BinaryNode) snj.left();
178 nodei.dist = ((dist / 2) - ih);
179 nodej.dist = ((dist / 2) - jh);
182 * not the right place - OH WELL!
186 * Makes a list of groups, where each group is represented by a node whose
187 * height (distance from the root node), as a fraction of the height of the
188 * whole tree, is greater than the given threshold. This corresponds to
189 * selecting the nodes immediately to the right of a vertical line
190 * partitioning the tree (if the tree is drawn with root to the left). Each
191 * such node represents a group that contains all of the sequences linked to
192 * the child leaf nodes.
197 public List<BinaryNode> groupNodes(float threshold)
199 List<BinaryNode> groups = new ArrayList<BinaryNode>();
200 _groupNodes(groups, getTopNode(), threshold);
204 protected void _groupNodes(List<BinaryNode> groups, BinaryNode nd,
212 if ((nd.height / maxheight) > threshold)
218 _groupNodes(groups, nd.left(), threshold);
219 _groupNodes(groups, nd.right(), threshold);
229 * @return DOCUMENT ME!
231 public double findHeight(BinaryNode nd)
238 if ((nd.left() == null) && (nd.right() == null))
240 nd.height = ((BinaryNode) nd.parent()).height + nd.dist;
242 if (nd.height > maxheight)
253 if (nd.parent() != null)
255 nd.height = ((BinaryNode) nd.parent()).height + nd.dist;
260 nd.height = (float) 0.0;
263 maxheight = findHeight((BinaryNode) (nd.left()));
264 maxheight = findHeight((BinaryNode) (nd.right()));
272 * Search for leaf nodes below (or at) the given node
275 * root node to search from
279 public Vector<BinaryNode> findLeaves(BinaryNode top2)
281 Vector<BinaryNode> leaves = new Vector<BinaryNode>();
282 findLeaves(top2, leaves);
287 * Search for leaf nodes.
290 * root node to search from
292 * Vector of leaves to add leaf node objects too.
294 * @return Vector of leaf nodes on binary tree
296 Vector<BinaryNode> findLeaves(BinaryNode nd,
297 Vector<BinaryNode> leaves)
304 if ((nd.left() == null) && (nd.right() == null)) // Interior node
307 leaves.addElement(nd);
314 * TODO: Identify internal nodes... if (node.isSequenceLabel()) {
315 * leaves.addElement(node); }
317 findLeaves( nd.left(), leaves);
318 findLeaves( nd.right(), leaves);