1 package jalview.analysis;
3 import jalview.api.analysis.ScoreModelI;
4 import jalview.api.analysis.SimilarityParamsI;
5 import jalview.datamodel.AlignmentView;
6 import jalview.datamodel.CigarArray;
7 import jalview.datamodel.SeqCigar;
8 import jalview.datamodel.SequenceI;
9 import jalview.datamodel.SequenceNode;
10 import jalview.math.MatrixI;
11 import jalview.viewmodel.AlignmentViewport;
13 import java.util.BitSet;
14 import java.util.Vector;
16 public abstract class TreeBuilder
18 public static final String AVERAGE_DISTANCE = "AV";
20 public static final String NEIGHBOUR_JOINING = "NJ";
22 protected Vector<BitSet> clusters;
24 protected SequenceI[] sequences;
26 public AlignmentView seqData;
28 protected BitSet done;
34 protected MatrixI distances;
54 Vector<SequenceNode> node;
56 private AlignmentView seqStrings;
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 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));
255 * Returns the minimum distance between two clusters, and also sets the
256 * indices of the clusters in fields mini and minj
260 protected abstract double findMinDistance();
263 * Calculates the tree using the given score model and parameters, and the
264 * configured tree type
266 * If the score model computes pairwise distance scores, then these are used
267 * directly to derive the tree
269 * If the score model computes similarity scores, then the range of the scores
270 * is reversed to give a distance measure, and this is used to derive the tree
273 * @param scoreOptions
275 protected void computeTree(ScoreModelI sm, SimilarityParamsI scoreOptions)
277 distances = sm.findDistances(seqData, scoreOptions);
281 noClus = clusters.size();
287 * Finds the node, at or below the given node, with the maximum distance, and
288 * saves the node and the distance value
292 void findMaxDist(SequenceNode nd)
299 if ((nd.left() == null) && (nd.right() == null))
301 double dist = nd.dist;
303 if (dist > maxDistValue)
311 findMaxDist((SequenceNode) nd.left());
312 findMaxDist((SequenceNode) nd.right());
317 * Calculates and returns r, whatever that is
324 protected double findr(int i, int j)
328 for (int k = 0; k < noseqs; k++)
330 if ((k != i) && (k != j) && (!done.get(k)))
332 tmp = tmp + distances.getValue(i, k);
338 tmp = tmp / (noClus - 2);
344 protected void init(AlignmentView seqView, int start, int end)
346 this.node = new Vector<SequenceNode>();
349 this.seqData = seqView;
353 SeqCigar[] seqs = new SeqCigar[sequences.length];
354 for (int i = 0; i < sequences.length; i++)
356 seqs[i] = new SeqCigar(sequences[i], start, end);
358 CigarArray sdata = new CigarArray(seqs);
359 sdata.addOperation(CigarArray.M, end - start + 1);
360 this.seqData = new AlignmentView(sdata, start);
364 * count the non-null sequences
370 for (SequenceI seq : sequences)
380 * Merges cluster(j) to cluster(i) and recalculates cluster and node distances
385 void joinClusters(final int i, final int j)
387 double dist = distances.getValue(i, j);
392 findClusterDistance(i, j);
394 SequenceNode sn = new SequenceNode();
396 sn.setLeft((node.elementAt(i)));
397 sn.setRight((node.elementAt(j)));
399 SequenceNode tmpi = (node.elementAt(i));
400 SequenceNode tmpj = (node.elementAt(j));
402 findNewDistances(tmpi, tmpj, dist);
407 node.setElementAt(sn, i);
410 * move the members of cluster(j) to cluster(i)
411 * and mark cluster j as out of the game
413 clusters.get(i).or(clusters.get(j));
414 clusters.get(j).clear();
419 * Computes and stores new distances for nodei and nodej, given the previous
420 * distance between them
422 protected abstract void findNewDistances(SequenceNode nodei,
423 SequenceNode nodej, double previousDistance);
426 * Calculates and saves the distance between the combination of cluster(i) and
427 * cluster(j) and all other clusters. The form of the calculation depends on
428 * the tree clustering method being used.
433 protected abstract void findClusterDistance(int i, int j);
436 * Start by making a cluster for each individual sequence
440 clusters = new Vector<BitSet>();
442 for (int i = 0; i < noseqs; i++)
444 SequenceNode sn = new SequenceNode();
446 sn.setElement(sequences[i]);
447 sn.setName(sequences[i].getName());
449 BitSet bs = new BitSet();
451 clusters.addElement(bs);
455 public AlignmentView getOriginalData()