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.bin.Cache;
24 import jalview.datamodel.AlignmentView;
25 import jalview.datamodel.BinaryNode;
26 import jalview.datamodel.NodeTransformI;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceI;
29 import jalview.datamodel.SequenceNode;
30 import jalview.io.NewickFile;
32 import java.util.ArrayList;
33 import java.util.Enumeration;
34 import java.util.List;
35 import java.util.Vector;
38 * A model of a tree, either computed by Jalview or loaded from a file or other
41 public class TreeModel
44 SequenceI[] sequences;
47 * SequenceData is a string representation of what the user
48 * sees. The display may contain hidden columns.
50 private AlignmentView seqData;
62 Vector<SequenceNode> node;
64 boolean hasDistances = true; // normal case for jalview trees
66 boolean hasBootstrap = false; // normal case for jalview trees
68 private boolean hasRootDistance = true;
71 * Create a new TreeModel object with leaves associated with sequences in
72 * seqs, and (optionally) original alignment data represented by Cigar strings
81 public TreeModel(SequenceI[] seqs, AlignmentView odata,
84 this(seqs, treefile.getTree(), treefile.HasDistances(),
85 treefile.HasBootstrap(), treefile.HasRootDistance());
88 associateLeavesToSequences(seqs);
92 * Constructor given a calculated tree
96 public TreeModel(TreeBuilder tree)
98 this(tree.getSequences(), tree.getTopNode(), tree.hasDistances(),
99 tree.hasBootstrap(), tree.hasRootDistance());
100 seqData = tree.getOriginalData();
104 * Constructor given sequences, root node and tree property flags
112 public TreeModel(SequenceI[] seqs, SequenceNode root, boolean hasDist,
113 boolean hasBoot, boolean hasRootDist)
115 this.sequences = seqs;
118 hasDistances = hasDist;
119 hasBootstrap = hasBoot;
120 hasRootDistance = hasRootDist;
122 maxheight = findHeight(top);
128 public void associateLeavesToSequences(SequenceI[] seqs)
130 SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs);
132 Vector<SequenceNode> leaves = findLeaves(top);
135 int namesleft = seqs.length;
140 Vector<SequenceI> one2many = new Vector<SequenceI>();
141 // int countOne2Many = 0;
142 while (i < leaves.size())
144 j = leaves.elementAt(i++);
145 realnam = j.getName();
150 nam = algnIds.findIdMatch(realnam);
156 if (one2many.contains(nam))
159 // if (Cache.isDebugEnabled())
160 // Cache.debug("One 2 many relationship for
165 one2many.addElement(nam);
171 j.setElement(new Sequence(realnam, "THISISAPLACEHLDER"));
172 j.setPlaceholder(true);
175 // if (Cache.isDebugEnabled() && countOne2Many>0) {
176 // Cache.debug("There were "+countOne2Many+" alignment
177 // sequence ids (out of "+one2many.size()+" unique ids) linked to two or
184 * Generate a string representation of the Tree
186 * @return Newick File with all tree data available
188 public String print()
190 NewickFile fout = new NewickFile(getTopNode());
192 return fout.print(hasBootstrap(), hasDistances(), hasRootDistance()); // output
202 * used when the alignment associated to a tree has changed.
205 * Sequence set to be associated with tree nodes
207 public void updatePlaceHolders(List<SequenceI> list)
209 Vector<SequenceNode> leaves = findLeaves(top);
211 int sz = leaves.size();
212 SequenceIdMatcher seqmatcher = null;
217 SequenceNode leaf = leaves.elementAt(i++);
219 if (list.contains(leaf.element()))
221 leaf.setPlaceholder(false);
225 if (seqmatcher == null)
227 // Only create this the first time we need it
228 SequenceI[] seqs = new SequenceI[list.size()];
230 for (int j = 0; j < seqs.length; j++)
232 seqs[j] = list.get(j);
235 seqmatcher = new SequenceIdMatcher(seqs);
238 SequenceI nam = seqmatcher.findIdMatch(leaf.getName());
242 if (!leaf.isPlaceholder())
244 // remapping the node to a new sequenceI - should remove any refs to
246 // TODO - make many sequenceI to one leaf mappings possible!
249 leaf.setPlaceholder(false);
250 leaf.setElement(nam);
254 if (!leaf.isPlaceholder())
256 // Construct a new placeholder sequence object for this leaf
258 new Sequence(leaf.getName(), "THISISAPLACEHLDER"));
260 leaf.setPlaceholder(true);
268 * rename any nodes according to their associated sequence. This will modify
269 * the tree's metadata! (ie the original NewickFile or newly generated
270 * BinaryTree's label data)
272 public void renameAssociatedNodes()
274 applyToNodes(new NodeTransformI()
278 public void transform(BinaryNode nd)
280 Object el = nd.element();
281 if (el != null && el instanceof SequenceI)
283 nd.setName(((SequenceI) el).getName());
290 * Search for leaf nodes below (or at) the given node
293 * root node to search from
297 public Vector<SequenceNode> findLeaves(SequenceNode nd)
299 Vector<SequenceNode> leaves = new Vector<SequenceNode>();
300 findLeaves(nd, leaves);
305 * Search for leaf nodes.
308 * root node to search from
310 * Vector of leaves to add leaf node objects too.
312 * @return Vector of leaf nodes on binary tree
314 Vector<SequenceNode> findLeaves(SequenceNode nd,
315 Vector<SequenceNode> leaves)
322 if ((nd.left() == null) && (nd.right() == null)) // Interior node
325 leaves.addElement(nd);
332 * TODO: Identify internal nodes... if (node.isSequenceLabel()) {
333 * leaves.addElement(node); }
335 findLeaves((SequenceNode) nd.left(), leaves);
336 findLeaves((SequenceNode) nd.right(), leaves);
343 * printNode is mainly for debugging purposes.
348 void printNode(SequenceNode nd)
355 if ((nd.left() == null) && (nd.right() == null))
357 System.out.println("Leaf = " + ((SequenceI) nd.element()).getName());
358 System.out.println("Dist " + nd.dist);
359 System.out.println("Boot " + nd.getBootstrap());
363 System.out.println("Dist " + nd.dist);
364 printNode((SequenceNode) nd.left());
365 printNode((SequenceNode) nd.right());
372 * @return DOCUMENT ME!
374 public double getMaxHeight()
380 * Makes a list of groups, where each group is represented by a node whose
381 * height (distance from the root node), as a fraction of the height of the
382 * whole tree, is greater than the given threshold. This corresponds to
383 * selecting the nodes immediately to the right of a vertical line
384 * partitioning the tree (if the tree is drawn with root to the left). Each
385 * such node represents a group that contains all of the sequences linked to
386 * the child leaf nodes.
391 public List<SequenceNode> groupNodes(float threshold)
393 List<SequenceNode> groups = new ArrayList<SequenceNode>();
394 _groupNodes(groups, getTopNode(), threshold);
398 protected void _groupNodes(List<SequenceNode> groups, SequenceNode nd,
406 if ((nd.height / maxheight) > threshold)
412 _groupNodes(groups, (SequenceNode) nd.left(), threshold);
413 _groupNodes(groups, (SequenceNode) nd.right(), threshold);
423 * @return DOCUMENT ME!
425 public double findHeight(SequenceNode nd)
432 if ((nd.left() == null) && (nd.right() == null))
434 nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
436 if (nd.height > maxheight)
447 if (nd.parent() != null)
449 nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
454 nd.height = (float) 0.0;
457 maxheight = findHeight((SequenceNode) (nd.left()));
458 maxheight = findHeight((SequenceNode) (nd.right()));
470 void printN(SequenceNode nd)
477 if ((nd.left() != null) && (nd.right() != null))
479 printN((SequenceNode) nd.left());
480 printN((SequenceNode) nd.right());
484 System.out.println(" name = " + ((SequenceI) nd.element()).getName());
488 " dist = " + nd.dist + " " + nd.count + " " + nd.height);
497 public void reCount(SequenceNode nd)
501 // _lylimit = this.node.size();
505 // private long _lycount = 0, _lylimit = 0;
513 void _reCount(SequenceNode nd)
515 // if (_lycount<_lylimit)
517 // System.err.println("Warning: depth of _recount greater than number of
526 if ((nd.left() != null) && (nd.right() != null))
529 _reCount((SequenceNode) nd.left());
530 _reCount((SequenceNode) nd.right());
532 SequenceNode l = (SequenceNode) nd.left();
533 SequenceNode r = (SequenceNode) nd.right();
535 nd.count = l.count + r.count;
536 nd.ycount = (l.ycount + r.ycount) / 2;
541 nd.ycount = ycount++;
552 public void swapNodes(SequenceNode nd)
559 SequenceNode tmp = (SequenceNode) nd.left();
561 nd.setLeft(nd.right());
573 void changeDirection(SequenceNode nd, SequenceNode dir)
580 if (nd.parent() != top)
582 changeDirection((SequenceNode) nd.parent(), nd);
584 SequenceNode tmp = (SequenceNode) nd.parent();
586 if (dir == nd.left())
591 else if (dir == nd.right())
599 if (dir == nd.left())
601 nd.setParent(nd.left());
603 if (top.left() == nd)
605 nd.setRight(top.right());
609 nd.setRight(top.left());
614 nd.setParent(nd.right());
616 if (top.left() == nd)
618 nd.setLeft(top.right());
622 nd.setLeft(top.left());
631 * @return DOCUMENT ME!
633 public SequenceNode getTopNode()
640 * @return true if tree has real distances
642 public boolean hasDistances()
649 * @return true if tree has real bootstrap values
651 public boolean hasBootstrap()
656 public boolean hasRootDistance()
658 return hasRootDistance;
662 * apply the given transform to all the nodes in the tree.
664 * @param nodeTransformI
666 public void applyToNodes(NodeTransformI nodeTransformI)
668 for (Enumeration<SequenceNode> nodes = node.elements(); nodes
669 .hasMoreElements(); nodeTransformI
670 .transform(nodes.nextElement()))
676 public AlignmentView getOriginalData()