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;
58 private AlignmentView seqStrings;
65 * @param scoreParameters
67 public TreeBuilder(AlignmentViewport av, ScoreModelI sm,
68 SimilarityParamsI scoreParameters)
71 boolean selview = av.getSelectionGroup() != null
72 && av.getSelectionGroup().getSize() > 1;
73 seqStrings = av.getAlignmentView(selview);
77 end = av.getAlignment().getWidth();
78 this.sequences = av.getAlignment().getSequencesArray();
82 start = av.getSelectionGroup().getStartRes();
83 end = av.getSelectionGroup().getEndRes() + 1;
84 this.sequences = av.getSelectionGroup().getSequencesInOrder(
88 init(seqStrings, start, end);
90 computeTree(sm, scoreParameters);
93 public SequenceI[] getSequences()
104 * @return DOCUMENT ME!
106 double findHeight(SequenceNode nd)
113 if ((nd.left() == null) && (nd.right() == null))
115 nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
117 if (nd.height > maxheight)
128 if (nd.parent() != null)
130 nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
135 nd.height = (float) 0.0;
138 maxheight = findHeight((SequenceNode) (nd.left()));
139 maxheight = findHeight((SequenceNode) (nd.right()));
151 void reCount(SequenceNode nd)
155 // _lylimit = this.node.size();
165 void _reCount(SequenceNode nd)
167 // if (_lycount<_lylimit)
169 // System.err.println("Warning: depth of _recount greater than number of nodes.");
177 if ((nd.left() != null) && (nd.right() != null))
180 _reCount((SequenceNode) nd.left());
181 _reCount((SequenceNode) nd.right());
183 SequenceNode l = (SequenceNode) nd.left();
184 SequenceNode r = (SequenceNode) nd.right();
186 nd.count = l.count + r.count;
187 nd.ycount = (l.ycount + r.ycount) / 2;
192 nd.ycount = ycount++;
200 * @return DOCUMENT ME!
202 public SequenceNode getTopNode()
209 * @return true if tree has real distances
211 public boolean hasDistances()
218 * @return true if tree has real bootstrap values
220 public boolean hasBootstrap()
225 public boolean hasRootDistance()
231 * Form clusters by grouping sub-clusters, starting from one sequence per
232 * cluster, and finishing when only two clusters remain
240 joinClusters(mini, minj);
245 int rightChild = done.nextClearBit(0);
246 int leftChild = done.nextClearBit(rightChild + 1);
248 joinClusters(leftChild, rightChild);
249 top = (node.elementAt(leftChild));
256 protected abstract double findMinDistance();
259 * Calculates the tree using the given score model and parameters, and the
260 * configured tree type
262 * If the score model computes pairwise distance scores, then these are used
263 * directly to derive the tree
265 * If the score model computes similarity scores, then the range of the scores
266 * is reversed to give a distance measure, and this is used to derive the tree
269 * @param scoreOptions
271 protected void computeTree(ScoreModelI sm, SimilarityParamsI scoreOptions)
273 if (sm instanceof DistanceScoreModelI)
275 distances = ((DistanceScoreModelI) sm).findDistances(seqData,
278 else if (sm instanceof SimilarityScoreModelI)
281 * compute similarity and invert it to give a distance measure
282 * reverseRange(true) converts maximum similarity to zero distance
284 MatrixI result = ((SimilarityScoreModelI) sm).findSimilarities(
285 seqData, scoreOptions);
286 result.reverseRange(true);
292 noClus = clusters.size();
303 void findMaxDist(SequenceNode nd)
310 if ((nd.left() == null) && (nd.right() == null))
312 double dist = nd.dist;
314 if (dist > maxDistValue)
322 findMaxDist((SequenceNode) nd.left());
323 findMaxDist((SequenceNode) nd.right());
335 * @return DOCUMENT ME!
337 protected double findr(int i, int j)
341 for (int k = 0; k < noseqs; k++)
343 if ((k != i) && (k != j) && (!done.get(k)))
345 tmp = tmp + distances.getValue(i, k);
351 tmp = tmp / (noClus - 2);
357 protected void init(AlignmentView seqView, int start, int end)
359 this.node = new Vector<SequenceNode>();
362 this.seqData = seqView;
366 SeqCigar[] seqs = new SeqCigar[sequences.length];
367 for (int i = 0; i < sequences.length; i++)
369 seqs[i] = new SeqCigar(sequences[i], start, end);
371 CigarArray sdata = new CigarArray(seqs);
372 sdata.addOperation(CigarArray.M, end - start + 1);
373 this.seqData = new AlignmentView(sdata, start);
377 * count the non-null sequences
383 for (SequenceI seq : sequences)
393 * Merges cluster(j) to cluster(i) and recalculates cluster and node distances
398 void joinClusters(final int i, final int j)
400 double dist = distances.getValue(i, j);
405 findClusterDistance(i, j);
407 SequenceNode sn = new SequenceNode();
409 sn.setLeft((node.elementAt(i)));
410 sn.setRight((node.elementAt(j)));
412 SequenceNode tmpi = (node.elementAt(i));
413 SequenceNode tmpj = (node.elementAt(j));
415 findNewDistances(tmpi, tmpj, dist);
420 node.setElementAt(sn, i);
423 * move the members of cluster(j) to cluster(i)
424 * and mark cluster j as out of the game
426 clusters.get(i).or(clusters.get(j));
427 clusters.get(j).clear();
431 protected abstract void findNewDistances(SequenceNode tmpi, SequenceNode tmpj,
435 * Calculates and saves the distance between the combination of cluster(i) and
436 * cluster(j) and all other clusters. The form of the calculation depends on
437 * the tree clustering method being used.
442 protected abstract void findClusterDistance(int i, int j);
445 * Start by making a cluster for each individual sequence
449 clusters = new Vector<BitSet>();
451 for (int i = 0; i < noseqs; i++)
453 SequenceNode sn = new SequenceNode();
455 sn.setElement(sequences[i]);
456 sn.setName(sequences[i].getName());
458 BitSet bs = new BitSet();
460 clusters.addElement(bs);
464 public AlignmentView getOriginalData()