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.datamodel.AlignmentView;
24 import jalview.datamodel.BinaryNode;
25 import jalview.datamodel.NodeTransformI;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceI;
28 import jalview.datamodel.SequenceNode;
29 import jalview.io.NewickFile;
31 import java.util.ArrayList;
32 import java.util.Enumeration;
33 import java.util.List;
34 import java.util.Vector;
37 * A model of a tree, either computed by Jalview or loaded from a file or other
40 public class TreeModel
43 SequenceI[] sequences;
46 * SequenceData is a string representation of what the user
47 * sees. The display may contain hidden columns.
49 private AlignmentView seqData;
61 Vector<SequenceNode> node;
63 boolean hasDistances = true; // normal case for jalview trees
65 boolean hasBootstrap = false; // normal case for jalview trees
67 private boolean hasRootDistance = true;
70 * Create a new TreeModel object with leaves associated with sequences in
71 * seqs, and (optionally) original alignment data represented by Cigar strings
80 public TreeModel(SequenceI[] seqs, AlignmentView odata,
83 this(seqs, treefile.getTree(), treefile.HasDistances(),
84 treefile.HasBootstrap(), treefile.HasRootDistance());
87 associateLeavesToSequences(seqs);
91 * Constructor given a calculated tree
95 public TreeModel(TreeBuilder tree)
97 this(tree.getSequences(), tree.getTopNode(), tree.hasDistances(),
98 tree.hasBootstrap(), tree.hasRootDistance());
99 seqData = tree.getOriginalData();
103 * Constructor given sequences, root node and tree property flags
111 public TreeModel(SequenceI[] seqs, SequenceNode root, boolean hasDist,
112 boolean hasBoot, boolean hasRootDist)
114 this.sequences = seqs;
117 hasDistances = hasDist;
118 hasBootstrap = hasBoot;
119 hasRootDistance = hasRootDist;
121 maxheight = findHeight(top);
127 public void associateLeavesToSequences(SequenceI[] seqs)
129 SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs);
131 Vector<SequenceNode> leaves = findLeaves(top);
134 int namesleft = seqs.length;
139 Vector<SequenceI> one2many = new Vector<SequenceI>();
140 // int countOne2Many = 0;
141 while (i < leaves.size())
143 j = leaves.elementAt(i++);
144 realnam = j.getName();
149 nam = algnIds.findIdMatch(realnam);
155 if (one2many.contains(nam))
158 // if (jalview.bin.Cache.log.isDebugEnabled())
159 // jalview.bin.Cache.log.debug("One 2 many relationship for
164 one2many.addElement(nam);
170 j.setElement(new Sequence(realnam, "THISISAPLACEHLDER"));
171 j.setPlaceholder(true);
174 // if (jalview.bin.Cache.log.isDebugEnabled() && countOne2Many>0) {
175 // jalview.bin.Cache.log.debug("There were "+countOne2Many+" alignment
176 // sequence ids (out of "+one2many.size()+" unique ids) linked to two or
183 * Generate a string representation of the Tree
185 * @return Newick File with all tree data available
187 public String print()
189 NewickFile fout = new NewickFile(getTopNode());
191 return fout.print(hasBootstrap(), hasDistances(), hasRootDistance()); // output
201 * used when the alignment associated to a tree has changed.
204 * Sequence set to be associated with tree nodes
206 public void updatePlaceHolders(List<SequenceI> list)
208 Vector<SequenceNode> leaves = findLeaves(top);
210 int sz = leaves.size();
211 SequenceIdMatcher seqmatcher = null;
216 SequenceNode leaf = leaves.elementAt(i++);
218 if (list.contains(leaf.element()))
220 leaf.setPlaceholder(false);
224 if (seqmatcher == null)
226 // Only create this the first time we need it
227 SequenceI[] seqs = new SequenceI[list.size()];
229 for (int j = 0; j < seqs.length; j++)
231 seqs[j] = list.get(j);
234 seqmatcher = new SequenceIdMatcher(seqs);
237 SequenceI nam = seqmatcher.findIdMatch(leaf.getName());
241 if (!leaf.isPlaceholder())
243 // remapping the node to a new sequenceI - should remove any refs to
245 // TODO - make many sequenceI to one leaf mappings possible!
248 leaf.setPlaceholder(false);
249 leaf.setElement(nam);
253 if (!leaf.isPlaceholder())
255 // Construct a new placeholder sequence object for this leaf
257 new Sequence(leaf.getName(), "THISISAPLACEHLDER"));
259 leaf.setPlaceholder(true);
267 * rename any nodes according to their associated sequence. This will modify
268 * the tree's metadata! (ie the original NewickFile or newly generated
269 * BinaryTree's label data)
271 public void renameAssociatedNodes()
273 applyToNodes(new NodeTransformI()
277 public void transform(BinaryNode nd)
279 Object el = nd.element();
280 if (el != null && el instanceof SequenceI)
282 nd.setName(((SequenceI) el).getName());
289 * Search for leaf nodes below (or at) the given node
292 * root node to search from
296 public Vector<SequenceNode> findLeaves(SequenceNode nd)
298 Vector<SequenceNode> leaves = new Vector<SequenceNode>();
299 findLeaves(nd, leaves);
304 * Search for leaf nodes.
307 * root node to search from
309 * Vector of leaves to add leaf node objects too.
311 * @return Vector of leaf nodes on binary tree
313 Vector<SequenceNode> findLeaves(SequenceNode nd,
314 Vector<SequenceNode> leaves)
321 if ((nd.left() == null) && (nd.right() == null)) // Interior node
324 leaves.addElement(nd);
331 * TODO: Identify internal nodes... if (node.isSequenceLabel()) {
332 * leaves.addElement(node); }
334 findLeaves((SequenceNode) nd.left(), leaves);
335 findLeaves((SequenceNode) nd.right(), leaves);
342 * printNode is mainly for debugging purposes.
347 void printNode(SequenceNode nd)
354 if ((nd.left() == null) && (nd.right() == null))
356 System.out.println("Leaf = " + ((SequenceI) nd.element()).getName());
357 System.out.println("Dist " + nd.dist);
358 System.out.println("Boot " + nd.getBootstrap());
362 System.out.println("Dist " + nd.dist);
363 printNode((SequenceNode) nd.left());
364 printNode((SequenceNode) nd.right());
371 * @return DOCUMENT ME!
373 public double getMaxHeight()
379 * Makes a list of groups, where each group is represented by a node whose
380 * height (distance from the root node), as a fraction of the height of the
381 * whole tree, is greater than the given threshold. This corresponds to
382 * selecting the nodes immediately to the right of a vertical line
383 * partitioning the tree (if the tree is drawn with root to the left). Each
384 * such node represents a group that contains all of the sequences linked to
385 * the child leaf nodes.
390 public List<SequenceNode> groupNodes(float threshold)
392 List<SequenceNode> groups = new ArrayList<SequenceNode>();
393 _groupNodes(groups, getTopNode(), threshold);
397 protected void _groupNodes(List<SequenceNode> groups, SequenceNode nd,
405 if ((nd.height / maxheight) > threshold)
411 _groupNodes(groups, (SequenceNode) nd.left(), threshold);
412 _groupNodes(groups, (SequenceNode) nd.right(), threshold);
422 * @return DOCUMENT ME!
424 public double findHeight(SequenceNode nd)
431 if ((nd.left() == null) && (nd.right() == null))
433 nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
435 if (nd.height > maxheight)
446 if (nd.parent() != null)
448 nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
453 nd.height = (float) 0.0;
456 maxheight = findHeight((SequenceNode) (nd.left()));
457 maxheight = findHeight((SequenceNode) (nd.right()));
469 void printN(SequenceNode nd)
476 if ((nd.left() != null) && (nd.right() != null))
478 printN((SequenceNode) nd.left());
479 printN((SequenceNode) nd.right());
483 System.out.println(" name = " + ((SequenceI) nd.element()).getName());
487 " dist = " + nd.dist + " " + nd.count + " " + nd.height);
496 public void reCount(SequenceNode nd)
500 // _lylimit = this.node.size();
504 // private long _lycount = 0, _lylimit = 0;
512 void _reCount(SequenceNode nd)
514 // if (_lycount<_lylimit)
516 // System.err.println("Warning: depth of _recount greater than number of
525 if ((nd.left() != null) && (nd.right() != null))
528 _reCount((SequenceNode) nd.left());
529 _reCount((SequenceNode) nd.right());
531 SequenceNode l = (SequenceNode) nd.left();
532 SequenceNode r = (SequenceNode) nd.right();
534 nd.count = l.count + r.count;
535 nd.ycount = (l.ycount + r.ycount) / 2;
540 nd.ycount = ycount++;
551 public void swapNodes(SequenceNode nd)
558 SequenceNode tmp = (SequenceNode) nd.left();
560 nd.setLeft(nd.right());
572 void changeDirection(SequenceNode nd, SequenceNode dir)
579 if (nd.parent() != top)
581 changeDirection((SequenceNode) nd.parent(), nd);
583 SequenceNode tmp = (SequenceNode) nd.parent();
585 if (dir == nd.left())
590 else if (dir == nd.right())
598 if (dir == nd.left())
600 nd.setParent(nd.left());
602 if (top.left() == nd)
604 nd.setRight(top.right());
608 nd.setRight(top.left());
613 nd.setParent(nd.right());
615 if (top.left() == nd)
617 nd.setLeft(top.right());
621 nd.setLeft(top.left());
630 * @return DOCUMENT ME!
632 public SequenceNode getTopNode()
639 * @return true if tree has real distances
641 public boolean hasDistances()
648 * @return true if tree has real bootstrap values
650 public boolean hasBootstrap()
655 public boolean hasRootDistance()
657 return hasRootDistance;
661 * apply the given transform to all the nodes in the tree.
663 * @param nodeTransformI
665 public void applyToNodes(NodeTransformI nodeTransformI)
667 for (Enumeration<SequenceNode> nodes = node.elements(); nodes
668 .hasMoreElements(); nodeTransformI
669 .transform(nodes.nextElement()))
675 public AlignmentView getOriginalData()