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 jalview.api.analysis.ScoreModelI;
24 import jalview.api.analysis.SimilarityParamsI;
25 import jalview.datamodel.AlignmentView;
26 import jalview.datamodel.CigarArray;
27 import jalview.datamodel.SeqCigar;
28 import jalview.datamodel.SequenceI;
29 import jalview.datamodel.SequenceNode;
30 import jalview.math.MatrixI;
31 import jalview.viewmodel.AlignmentViewport;
33 import java.util.BitSet;
34 import java.util.Vector;
36 public abstract class TreeBuilder
38 public static final String AVERAGE_DISTANCE = "AV";
40 public static final String NEIGHBOUR_JOINING = "NJ";
42 protected Vector<BitSet> clusters;
44 protected SequenceI[] sequences;
46 public AlignmentView seqData;
48 protected BitSet done;
54 protected MatrixI distances;
74 Vector<SequenceNode> node;
76 private AlignmentView seqStrings;
83 * @param scoreParameters
85 public TreeBuilder(AlignmentViewport av, ScoreModelI sm,
86 SimilarityParamsI scoreParameters)
89 boolean selview = av.getSelectionGroup() != null
90 && av.getSelectionGroup().getSize() > 1;
91 seqStrings = av.getAlignmentView(selview);
95 end = av.getAlignment().getWidth();
96 this.sequences = av.getAlignment().getSequencesArray();
100 start = av.getSelectionGroup().getStartRes();
101 end = av.getSelectionGroup().getEndRes() + 1;
102 this.sequences = av.getSelectionGroup()
103 .getSequencesInOrder(av.getAlignment());
106 init(seqStrings, start, end);
108 computeTree(sm, scoreParameters);
111 public SequenceI[] getSequences()
122 * @return DOCUMENT ME!
124 double findHeight(SequenceNode nd)
131 if ((nd.left() == null) && (nd.right() == null))
133 nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
135 if (nd.height > maxheight)
146 if (nd.parent() != null)
148 nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
153 nd.height = (float) 0.0;
156 maxheight = findHeight((SequenceNode) (nd.left()));
157 maxheight = findHeight((SequenceNode) (nd.right()));
169 void reCount(SequenceNode nd)
173 // _lylimit = this.node.size();
183 void _reCount(SequenceNode nd)
185 // if (_lycount<_lylimit)
187 // System.err.println("Warning: depth of _recount greater than number of
196 if ((nd.left() != null) && (nd.right() != null))
199 _reCount((SequenceNode) nd.left());
200 _reCount((SequenceNode) nd.right());
202 SequenceNode l = (SequenceNode) nd.left();
203 SequenceNode r = (SequenceNode) nd.right();
205 nd.count = l.count + r.count;
206 nd.ycount = (l.ycount + r.ycount) / 2;
211 nd.ycount = ycount++;
219 * @return DOCUMENT ME!
221 public SequenceNode getTopNode()
228 * @return true if tree has real distances
230 public boolean hasDistances()
237 * @return true if tree has real bootstrap values
239 public boolean hasBootstrap()
244 public boolean hasRootDistance()
250 * Form clusters by grouping sub-clusters, starting from one sequence per
251 * cluster, and finishing when only two clusters remain
259 joinClusters(mini, minj);
264 int rightChild = done.nextClearBit(0);
265 int leftChild = done.nextClearBit(rightChild + 1);
267 joinClusters(leftChild, rightChild);
268 top = (node.elementAt(leftChild));
276 * Returns the minimum distance between two clusters, and also sets the
277 * indices of the clusters in fields mini and minj
281 protected abstract double findMinDistance();
284 * Calculates the tree using the given score model and parameters, and the
285 * configured tree type
287 * If the score model computes pairwise distance scores, then these are used
288 * directly to derive the tree
290 * If the score model computes similarity scores, then the range of the scores
291 * is reversed to give a distance measure, and this is used to derive the tree
294 * @param scoreOptions
296 protected void computeTree(ScoreModelI sm, SimilarityParamsI scoreOptions)
298 distances = sm.findDistances(seqData, scoreOptions);
302 noClus = clusters.size();
308 * Finds the node, at or below the given node, with the maximum distance, and
309 * saves the node and the distance value
313 void findMaxDist(SequenceNode nd)
320 if ((nd.left() == null) && (nd.right() == null))
322 double dist = nd.dist;
324 if (dist > maxDistValue)
332 findMaxDist((SequenceNode) nd.left());
333 findMaxDist((SequenceNode) nd.right());
338 * Calculates and returns r, whatever that is
345 protected double findr(int i, int j)
349 for (int k = 0; k < noseqs; k++)
351 if ((k != i) && (k != j) && (!done.get(k)))
353 tmp = tmp + distances.getValue(i, k);
359 tmp = tmp / (noClus - 2);
365 protected void init(AlignmentView seqView, int start, int end)
367 this.node = new Vector<SequenceNode>();
370 this.seqData = seqView;
374 SeqCigar[] seqs = new SeqCigar[sequences.length];
375 for (int i = 0; i < sequences.length; i++)
377 seqs[i] = new SeqCigar(sequences[i], start, end);
379 CigarArray sdata = new CigarArray(seqs);
380 sdata.addOperation(CigarArray.M, end - start + 1);
381 this.seqData = new AlignmentView(sdata, start);
385 * count the non-null sequences
391 for (SequenceI seq : sequences)
401 * Merges cluster(j) to cluster(i) and recalculates cluster and node distances
406 void joinClusters(final int i, final int j)
408 double dist = distances.getValue(i, j);
413 findClusterDistance(i, j);
415 SequenceNode sn = new SequenceNode();
417 sn.setLeft((node.elementAt(i)));
418 sn.setRight((node.elementAt(j)));
420 SequenceNode tmpi = (node.elementAt(i));
421 SequenceNode tmpj = (node.elementAt(j));
423 findNewDistances(tmpi, tmpj, dist);
428 node.setElementAt(sn, i);
431 * move the members of cluster(j) to cluster(i)
432 * and mark cluster j as out of the game
434 clusters.get(i).or(clusters.get(j));
435 clusters.get(j).clear();
440 * Computes and stores new distances for nodei and nodej, given the previous
441 * distance between them
443 protected abstract void findNewDistances(SequenceNode nodei,
444 SequenceNode nodej, double previousDistance);
447 * Calculates and saves the distance between the combination of cluster(i) and
448 * cluster(j) and all other clusters. The form of the calculation depends on
449 * the tree clustering method being used.
454 protected abstract void findClusterDistance(int i, int j);
457 * Start by making a cluster for each individual sequence
461 clusters = new Vector<BitSet>();
463 for (int i = 0; i < noseqs; i++)
465 SequenceNode sn = new SequenceNode();
467 sn.setElement(sequences[i]);
468 sn.setName(sequences[i].getName());
470 BitSet bs = new BitSet();
472 clusters.addElement(bs);
476 public AlignmentView getOriginalData()