1 package jalview.analysis;
3 import jalview.api.analysis.DistanceScoreModelI;
4 import jalview.api.analysis.ScoreModelI;
5 import jalview.api.analysis.SimilarityParamsI;
6 import jalview.api.analysis.SimilarityScoreModelI;
7 import jalview.datamodel.AlignmentView;
8 import jalview.datamodel.CigarArray;
9 import jalview.datamodel.SeqCigar;
10 import jalview.datamodel.SequenceI;
11 import jalview.datamodel.SequenceNode;
12 import jalview.math.MatrixI;
13 import jalview.viewmodel.AlignmentViewport;
15 import java.util.BitSet;
16 import java.util.Vector;
18 public abstract class TreeBuilder
20 public static final String AVERAGE_DISTANCE = "AV";
22 public static final String NEIGHBOUR_JOINING = "NJ";
24 protected Vector<BitSet> clusters;
26 protected SequenceI[] sequences;
28 public AlignmentView seqData;
30 protected BitSet done;
36 protected MatrixI distances;
56 Vector<SequenceNode> node;
63 * @param scoreParameters
65 public TreeBuilder(AlignmentViewport av, ScoreModelI sm,
66 SimilarityParamsI scoreParameters)
69 boolean selview = av.getSelectionGroup() != null
70 && av.getSelectionGroup().getSize() > 1;
71 AlignmentView seqStrings = av.getAlignmentView(selview);
75 end = av.getAlignment().getWidth();
76 this.sequences = av.getAlignment().getSequencesArray();
80 start = av.getSelectionGroup().getStartRes();
81 end = av.getSelectionGroup().getEndRes() + 1;
82 this.sequences = av.getSelectionGroup().getSequencesInOrder(
86 init(seqStrings, start, end);
88 computeTree(sm, scoreParameters);
91 public SequenceI[] getSequences()
102 * @return DOCUMENT ME!
104 double findHeight(SequenceNode nd)
111 if ((nd.left() == null) && (nd.right() == null))
113 nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
115 if (nd.height > maxheight)
126 if (nd.parent() != null)
128 nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
133 nd.height = (float) 0.0;
136 maxheight = findHeight((SequenceNode) (nd.left()));
137 maxheight = findHeight((SequenceNode) (nd.right()));
149 void reCount(SequenceNode nd)
153 // _lylimit = this.node.size();
163 void _reCount(SequenceNode nd)
165 // if (_lycount<_lylimit)
167 // System.err.println("Warning: depth of _recount greater than number of nodes.");
175 if ((nd.left() != null) && (nd.right() != null))
178 _reCount((SequenceNode) nd.left());
179 _reCount((SequenceNode) nd.right());
181 SequenceNode l = (SequenceNode) nd.left();
182 SequenceNode r = (SequenceNode) nd.right();
184 nd.count = l.count + r.count;
185 nd.ycount = (l.ycount + r.ycount) / 2;
190 nd.ycount = ycount++;
198 * @return DOCUMENT ME!
200 public SequenceNode getTopNode()
207 * @return true if tree has real distances
209 public boolean hasDistances()
216 * @return true if tree has real bootstrap values
218 public boolean hasBootstrap()
223 public boolean hasRootDistance()
229 * Form clusters by grouping sub-clusters, starting from one sequence per
230 * cluster, and finishing when only two clusters remain
238 joinClusters(mini, minj);
243 int rightChild = done.nextClearBit(0);
244 int leftChild = done.nextClearBit(rightChild + 1);
246 joinClusters(leftChild, rightChild);
247 top = (node.elementAt(leftChild));
254 protected abstract double findMinDistance();
257 * Calculates the tree using the given score model and parameters, and the
258 * configured tree type
260 * If the score model computes pairwise distance scores, then these are used
261 * directly to derive the tree
263 * If the score model computes similarity scores, then the range of the scores
264 * is reversed to give a distance measure, and this is used to derive the tree
267 * @param scoreOptions
269 protected void computeTree(ScoreModelI sm, SimilarityParamsI scoreOptions)
271 if (sm instanceof DistanceScoreModelI)
273 distances = ((DistanceScoreModelI) sm).findDistances(seqData,
276 else if (sm instanceof SimilarityScoreModelI)
279 * compute similarity and invert it to give a distance measure
281 MatrixI result = ((SimilarityScoreModelI) sm).findSimilarities(
282 seqData, scoreOptions);
283 result.reverseRange(true);
289 noClus = clusters.size();
300 void findMaxDist(SequenceNode nd)
307 if ((nd.left() == null) && (nd.right() == null))
309 double dist = nd.dist;
311 if (dist > maxDistValue)
319 findMaxDist((SequenceNode) nd.left());
320 findMaxDist((SequenceNode) nd.right());
332 * @return DOCUMENT ME!
334 protected double findr(int i, int j)
338 for (int k = 0; k < noseqs; k++)
340 if ((k != i) && (k != j) && (!done.get(k)))
342 tmp = tmp + distances.getValue(i, k);
348 tmp = tmp / (noClus - 2);
354 protected void init(AlignmentView seqView, int start, int end)
356 this.node = new Vector<SequenceNode>();
359 this.seqData = seqView;
363 SeqCigar[] seqs = new SeqCigar[sequences.length];
364 for (int i = 0; i < sequences.length; i++)
366 seqs[i] = new SeqCigar(sequences[i], start, end);
368 CigarArray sdata = new CigarArray(seqs);
369 sdata.addOperation(CigarArray.M, end - start + 1);
370 this.seqData = new AlignmentView(sdata, start);
374 * count the non-null sequences
380 for (SequenceI seq : sequences)
390 * Merges cluster(j) to cluster(i) and recalculates cluster and node distances
395 void joinClusters(final int i, final int j)
397 double dist = distances.getValue(i, j);
402 findClusterDistance(i, j);
404 SequenceNode sn = new SequenceNode();
406 sn.setLeft((node.elementAt(i)));
407 sn.setRight((node.elementAt(j)));
409 SequenceNode tmpi = (node.elementAt(i));
410 SequenceNode tmpj = (node.elementAt(j));
412 findNewDistances(tmpi, tmpj, dist);
417 node.setElementAt(sn, i);
420 * move the members of cluster(j) to cluster(i)
421 * and mark cluster j as out of the game
423 clusters.get(i).or(clusters.get(j));
424 clusters.get(j).clear();
428 protected abstract void findNewDistances(SequenceNode tmpi, SequenceNode tmpj,
432 * Calculates and saves the distance between the combination of cluster(i) and
433 * cluster(j) and all other clusters. The form of the calculation depends on
434 * the tree clustering method being used.
439 protected abstract void findClusterDistance(int i, int j);
442 * Start by making a cluster for each individual sequence
446 clusters = new Vector<BitSet>();
448 for (int i = 0; i < noseqs; i++)
450 SequenceNode sn = new SequenceNode();
452 sn.setElement(sequences[i]);
453 sn.setName(sequences[i].getName());
455 BitSet bs = new BitSet();
457 clusters.addElement(bs);