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
280 * reverseRange(true) converts maximum similarity to zero distance
282 MatrixI result = ((SimilarityScoreModelI) sm).findSimilarities(
283 seqData, scoreOptions);
284 result.reverseRange(true);
290 noClus = clusters.size();
301 void findMaxDist(SequenceNode nd)
308 if ((nd.left() == null) && (nd.right() == null))
310 double dist = nd.dist;
312 if (dist > maxDistValue)
320 findMaxDist((SequenceNode) nd.left());
321 findMaxDist((SequenceNode) nd.right());
333 * @return DOCUMENT ME!
335 protected double findr(int i, int j)
339 for (int k = 0; k < noseqs; k++)
341 if ((k != i) && (k != j) && (!done.get(k)))
343 tmp = tmp + distances.getValue(i, k);
349 tmp = tmp / (noClus - 2);
355 protected void init(AlignmentView seqView, int start, int end)
357 this.node = new Vector<SequenceNode>();
360 this.seqData = seqView;
364 SeqCigar[] seqs = new SeqCigar[sequences.length];
365 for (int i = 0; i < sequences.length; i++)
367 seqs[i] = new SeqCigar(sequences[i], start, end);
369 CigarArray sdata = new CigarArray(seqs);
370 sdata.addOperation(CigarArray.M, end - start + 1);
371 this.seqData = new AlignmentView(sdata, start);
375 * count the non-null sequences
381 for (SequenceI seq : sequences)
391 * Merges cluster(j) to cluster(i) and recalculates cluster and node distances
396 void joinClusters(final int i, final int j)
398 double dist = distances.getValue(i, j);
403 findClusterDistance(i, j);
405 SequenceNode sn = new SequenceNode();
407 sn.setLeft((node.elementAt(i)));
408 sn.setRight((node.elementAt(j)));
410 SequenceNode tmpi = (node.elementAt(i));
411 SequenceNode tmpj = (node.elementAt(j));
413 findNewDistances(tmpi, tmpj, dist);
418 node.setElementAt(sn, i);
421 * move the members of cluster(j) to cluster(i)
422 * and mark cluster j as out of the game
424 clusters.get(i).or(clusters.get(j));
425 clusters.get(j).clear();
429 protected abstract void findNewDistances(SequenceNode tmpi, SequenceNode tmpj,
433 * Calculates and saves the distance between the combination of cluster(i) and
434 * cluster(j) and all other clusters. The form of the calculation depends on
435 * the tree clustering method being used.
440 protected abstract void findClusterDistance(int i, int j);
443 * Start by making a cluster for each individual sequence
447 clusters = new Vector<BitSet>();
449 for (int i = 0; i < noseqs; i++)
451 SequenceNode sn = new SequenceNode();
453 sn.setElement(sequences[i]);
454 sn.setName(sequences[i].getName());
456 BitSet bs = new BitSet();
458 clusters.addElement(bs);