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 protected ScoreModelI scoreModel;
78 protected SimilarityParamsI scoreParams;
80 private AlignmentView seqStrings; // redundant? (see seqData)
87 * @param scoreParameters
89 public TreeBuilder(AlignmentViewport av, ScoreModelI sm,
90 SimilarityParamsI scoreParameters)
93 boolean selview = av.getSelectionGroup() != null
94 && av.getSelectionGroup().getSize() > 1;
95 seqStrings = av.getAlignmentView(selview);
99 end = av.getAlignment().getWidth();
100 this.sequences = av.getAlignment().getSequencesArray();
104 start = av.getSelectionGroup().getStartRes();
105 end = av.getSelectionGroup().getEndRes() + 1;
106 this.sequences = av.getSelectionGroup()
107 .getSequencesInOrder(av.getAlignment());
110 init(seqStrings, start, end);
112 computeTree(sm, scoreParameters);
115 public SequenceI[] getSequences()
126 * @return DOCUMENT ME!
128 double findHeight(SequenceNode nd)
135 if ((nd.left() == null) && (nd.right() == null))
137 nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
139 if (nd.height > maxHeight)
150 if (nd.parent() != null)
152 nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
157 nd.height = (float) 0.0;
160 maxHeight = findHeight((SequenceNode) (nd.left()));
161 maxHeight = findHeight((SequenceNode) (nd.right()));
173 void reCount(SequenceNode nd)
177 // _lylimit = this.node.size();
187 void _reCount(SequenceNode nd)
189 // if (_lycount<_lylimit)
191 // System.err.println("Warning: depth of _recount greater than number of
200 if ((nd.left() != null) && (nd.right() != null))
203 _reCount((SequenceNode) nd.left());
204 _reCount((SequenceNode) nd.right());
206 SequenceNode l = (SequenceNode) nd.left();
207 SequenceNode r = (SequenceNode) nd.right();
209 nd.count = l.count + r.count;
210 nd.ycount = (l.ycount + r.ycount) / 2;
215 nd.ycount = ycount++;
223 * @return DOCUMENT ME!
225 public SequenceNode getTopNode()
232 * @return true if tree has real distances
234 public boolean hasDistances()
241 * @return true if tree has real bootstrap values
243 public boolean hasBootstrap()
248 public boolean hasRootDistance()
254 * Form clusters by grouping sub-clusters, starting from one sequence per
255 * cluster, and finishing when only two clusters remain
263 joinClusters(mini, minj);
268 int rightChild = done.nextClearBit(0);
269 int leftChild = done.nextClearBit(rightChild + 1);
271 joinClusters(leftChild, rightChild);
272 top = (node.elementAt(leftChild));
280 * Returns the minimum distance between two clusters, and also sets the
281 * indices of the clusters in fields mini and minj
285 protected abstract double findMinDistance();
288 * Calculates the tree using the given score model and parameters, and the
289 * configured tree type
291 * If the score model computes pairwise distance scores, then these are used
292 * directly to derive the tree
294 * If the score model computes similarity scores, then the range of the scores
295 * is reversed to give a distance measure, and this is used to derive the tree
298 * @param scoreOptions
300 protected void computeTree(ScoreModelI sm, SimilarityParamsI scoreOptions)
303 this.scoreModel = sm;
304 this.scoreParams = scoreOptions;
306 distances = scoreModel.findDistances(seqData, scoreParams);
310 noClus = clusters.size();
316 * Finds the node, at or below the given node, with the maximum distance, and
317 * saves the node and the distance value
321 void findMaxDist(SequenceNode nd)
328 if ((nd.left() == null) && (nd.right() == null))
330 double dist = nd.dist;
332 if (dist > maxDistValue)
340 findMaxDist((SequenceNode) nd.left());
341 findMaxDist((SequenceNode) nd.right());
346 * Calculates and returns r, whatever that is
353 protected double findr(int i, int j)
357 for (int k = 0; k < noseqs; k++)
359 if ((k != i) && (k != j) && (!done.get(k)))
361 tmp = tmp + distances.getValue(i, k);
367 tmp = tmp / (noClus - 2);
373 protected void init(AlignmentView seqView, int start, int end)
375 this.node = new Vector<>();
378 this.seqData = seqView;
382 SeqCigar[] seqs = new SeqCigar[sequences.length];
383 for (int i = 0; i < sequences.length; i++)
385 seqs[i] = new SeqCigar(sequences[i], start, end);
387 CigarArray sdata = new CigarArray(seqs);
388 sdata.addOperation(CigarArray.M, end - start + 1);
389 this.seqData = new AlignmentView(sdata, start);
393 * count the non-null sequences
399 for (SequenceI seq : sequences)
409 * Merges cluster(j) to cluster(i) and recalculates cluster and node distances
414 void joinClusters(final int i, final int j)
416 double dist = distances.getValue(i, j);
421 findClusterDistance(i, j);
423 SequenceNode sn = new SequenceNode();
425 sn.setLeft((node.elementAt(i)));
426 sn.setRight((node.elementAt(j)));
428 SequenceNode tmpi = (node.elementAt(i));
429 SequenceNode tmpj = (node.elementAt(j));
431 findNewDistances(tmpi, tmpj, dist);
436 node.setElementAt(sn, i);
439 * move the members of cluster(j) to cluster(i)
440 * and mark cluster j as out of the game
442 clusters.get(i).or(clusters.get(j));
443 clusters.get(j).clear();
448 * Computes and stores new distances for nodei and nodej, given the previous
449 * distance between them
451 protected abstract void findNewDistances(SequenceNode nodei,
452 SequenceNode nodej, double previousDistance);
455 * Calculates and saves the distance between the combination of cluster(i) and
456 * cluster(j) and all other clusters. The form of the calculation depends on
457 * the tree clustering method being used.
462 protected abstract void findClusterDistance(int i, int j);
465 * Start by making a cluster for each individual sequence
469 clusters = new Vector<>();
471 for (int i = 0; i < noseqs; i++)
473 SequenceNode sn = new SequenceNode();
475 sn.setElement(sequences[i]);
476 sn.setName(sequences[i].getName());
479 BitSet bs = new BitSet();
481 clusters.addElement(bs);
485 public AlignmentView getOriginalData()
490 public MatrixI getDistances()
495 public AlignmentView getSeqData()
500 public ScoreModelI getScoreModel()
505 public SimilarityParamsI getScoreParams()