X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FTreeModel.java;h=ce79a650c4b8b8e6f781a3caef8ef6d4d6ebe048;hb=789c97dc0fa8df08248c779694ffaaf5e19262f2;hp=2f8d7883f40d2fe37f1b875eb7026b8da517af8a;hpb=be27d7b3cadbfb5c488066034c48b1735799b429;p=jalview.git diff --git a/src/jalview/analysis/TreeModel.java b/src/jalview/analysis/TreeModel.java index 2f8d788..ce79a65 100644 --- a/src/jalview/analysis/TreeModel.java +++ b/src/jalview/analysis/TreeModel.java @@ -46,11 +46,11 @@ public class TreeModel * SequenceData is a string representation of what the user * sees. The display may contain hidden columns. */ - public AlignmentView seqData; + private AlignmentView seqData; int noseqs; - SequenceNode top; + BinaryNode top; double maxDistValue; @@ -58,7 +58,7 @@ public class TreeModel int ycount; - Vector node; + Vector node; boolean hasDistances = true; // normal case for jalview trees @@ -68,7 +68,7 @@ public class TreeModel /** * Create a new TreeModel object with leaves associated with sequences in - * seqs, and original alignment data represented by Cigar strings + * seqs, and (optionally) original alignment data represented by Cigar strings * * @param seqs * SequenceI[] @@ -77,24 +77,12 @@ public class TreeModel * @param treefile * NewickFile */ - public TreeModel(SequenceI[] seqs, AlignmentView odata, NewickFile treefile) + public TreeModel(SequenceI[] seqs, AlignmentView odata, + NewickFile treefile) { - this(seqs, treefile); + this(seqs, treefile.getTree(), treefile.HasDistances(), + treefile.HasBootstrap(), treefile.HasRootDistance()); seqData = odata; - } - - /** - * Creates a new TreeModel object from a tree from an external source - * - * @param seqs - * SequenceI which should be associated with leafs of treefile - * @param treefile - * A parsed tree - */ - public TreeModel(SequenceI[] seqs, NewickFile treefile) - { - this(seqs, treefile.getTree(), treefile.HasDistances(), treefile - .HasBootstrap(), treefile.HasRootDistance()); associateLeavesToSequences(seqs); } @@ -106,8 +94,9 @@ public class TreeModel */ public TreeModel(TreeBuilder tree) { - this(tree.getSequences(), tree.getTopNode(), tree.hasDistances(), tree - .hasBootstrap(), tree.hasRootDistance()); + this(tree.getSequences(), tree.getTopNode(), tree.hasDistances(), + tree.hasBootstrap(), tree.hasRootDistance()); + seqData = tree.getOriginalData(); } /** @@ -119,7 +108,7 @@ public class TreeModel * @param hasBoot * @param hasRootDist */ - public TreeModel(SequenceI[] seqs, SequenceNode root, boolean hasDist, + public TreeModel(SequenceI[] seqs, BinaryNode root, boolean hasDist, boolean hasBoot, boolean hasRootDist) { this.sequences = seqs; @@ -139,7 +128,7 @@ public class TreeModel { SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs); - Vector leaves = findLeaves(top); + Vector leaves = findLeaves(top); int i = 0; int namesleft = seqs.length; @@ -151,7 +140,8 @@ public class TreeModel // int countOne2Many = 0; while (i < leaves.size()) { - j = leaves.elementAt(i++); + // TODO - decide if we get rid of the polymorphism here ? + j = (SequenceNode) leaves.elementAt(i++); realnam = j.getName(); nam = null; @@ -166,8 +156,8 @@ public class TreeModel if (one2many.contains(nam)) { // countOne2Many++; - // if (jalview.bin.Cache.log.isDebugEnabled()) - // jalview.bin.Cache.log.debug("One 2 many relationship for + // if (Cache.isDebugEnabled()) + // Cache.debug("One 2 many relationship for // "+nam.getName()); } else @@ -182,8 +172,8 @@ public class TreeModel j.setPlaceholder(true); } } - // if (jalview.bin.Cache.log.isDebugEnabled() && countOne2Many>0) { - // jalview.bin.Cache.log.debug("There were "+countOne2Many+" alignment + // if (Cache.isDebugEnabled() && countOne2Many>0) { + // Cache.debug("There were "+countOne2Many+" alignment // sequence ids (out of "+one2many.size()+" unique ids) linked to two or // more leaves."); // } @@ -199,8 +189,12 @@ public class TreeModel { NewickFile fout = new NewickFile(getTopNode()); - return fout.print(hasBootstrap(), hasDistances(), - hasRootDistance()); // output all data available for tree + return fout.print(hasBootstrap(), hasDistances(), hasRootDistance()); // output + // all + // data + // available + // for + // tree } /** @@ -212,7 +206,7 @@ public class TreeModel */ public void updatePlaceHolders(List list) { - Vector leaves = findLeaves(top); + Vector leaves = findLeaves(top); int sz = leaves.size(); SequenceIdMatcher seqmatcher = null; @@ -220,7 +214,7 @@ public class TreeModel while (i < sz) { - SequenceNode leaf = leaves.elementAt(i++); + SequenceNode leaf = (SequenceNode) leaves.elementAt(i++); if (list.contains(leaf.element())) { @@ -260,8 +254,8 @@ public class TreeModel if (!leaf.isPlaceholder()) { // Construct a new placeholder sequence object for this leaf - leaf.setElement(new Sequence(leaf.getName(), - "THISISAPLACEHLDER")); + leaf.setElement( + new Sequence(leaf.getName(), "THISISAPLACEHLDER")); } leaf.setPlaceholder(true); @@ -295,15 +289,15 @@ public class TreeModel /** * Search for leaf nodes below (or at) the given node * - * @param nd + * @param top2 * root node to search from * * @return */ - public Vector findLeaves(SequenceNode nd) + public Vector findLeaves(BinaryNode top2) { - Vector leaves = new Vector(); - findLeaves(nd, leaves); + Vector leaves = new Vector(); + findLeaves(top2, leaves); return leaves; } @@ -317,8 +311,7 @@ public class TreeModel * * @return Vector of leaf nodes on binary tree */ - Vector findLeaves(SequenceNode nd, - Vector leaves) + Vector findLeaves(BinaryNode nd, Vector leaves) { if (nd == null) { @@ -338,8 +331,8 @@ public class TreeModel * TODO: Identify internal nodes... if (node.isSequenceLabel()) { * leaves.addElement(node); } */ - findLeaves((SequenceNode) nd.left(), leaves); - findLeaves((SequenceNode) nd.right(), leaves); + findLeaves(nd.left(), leaves); + findLeaves(nd.right(), leaves); } return leaves; @@ -351,7 +344,7 @@ public class TreeModel * @param nd * SequenceNode */ - void printNode(SequenceNode nd) + void printNode(BinaryNode nd) { if (nd == null) { @@ -360,15 +353,16 @@ public class TreeModel if ((nd.left() == null) && (nd.right() == null)) { - System.out.println("Leaf = " + ((SequenceI) nd.element()).getName()); - System.out.println("Dist " + nd.dist); - System.out.println("Boot " + nd.getBootstrap()); + // TODO FIX FOR COLUMN TREES + jalview.bin.Console.outPrintln("Leaf = " + ((SequenceI) nd.element()).getName()); + jalview.bin.Console.outPrintln("Dist " + nd.dist); + jalview.bin.Console.outPrintln("Boot " + nd.getBootstrap()); } else { - System.out.println("Dist " + nd.dist); - printNode((SequenceNode) nd.left()); - printNode((SequenceNode) nd.right()); + jalview.bin.Console.outPrintln("Dist " + nd.dist); + printNode((BinaryNode) nd.left()); + printNode((BinaryNode) nd.right()); } } @@ -394,14 +388,14 @@ public class TreeModel * @param threshold * @see #getGroups() */ - public List groupNodes(float threshold) + public List groupNodes(float threshold) { - List groups = new ArrayList(); + List groups = new ArrayList(); _groupNodes(groups, getTopNode(), threshold); return groups; } - protected void _groupNodes(List groups, SequenceNode nd, + protected void _groupNodes(List groups, BinaryNode nd, float threshold) { if (nd == null) @@ -415,8 +409,8 @@ public class TreeModel } else { - _groupNodes(groups, (SequenceNode) nd.left(), threshold); - _groupNodes(groups, (SequenceNode) nd.right(), threshold); + _groupNodes(groups, nd.left(), threshold); + _groupNodes(groups, nd.right(), threshold); } } @@ -428,7 +422,7 @@ public class TreeModel * * @return DOCUMENT ME! */ - public double findHeight(SequenceNode nd) + public double findHeight(BinaryNode nd) { if (nd == null) { @@ -437,7 +431,7 @@ public class TreeModel if ((nd.left() == null) && (nd.right() == null)) { - nd.height = ((SequenceNode) nd.parent()).height + nd.dist; + nd.height = nd.parent().height + nd.dist; if (nd.height > maxheight) { @@ -452,7 +446,7 @@ public class TreeModel { if (nd.parent() != null) { - nd.height = ((SequenceNode) nd.parent()).height + nd.dist; + nd.height = nd.parent().height + nd.dist; } else { @@ -460,53 +454,20 @@ public class TreeModel nd.height = (float) 0.0; } - maxheight = findHeight((SequenceNode) (nd.left())); - maxheight = findHeight((SequenceNode) (nd.right())); + maxheight = findHeight((BinaryNode) (nd.left())); + maxheight = findHeight((BinaryNode) (nd.right())); } return maxheight; } /** - * - * @return true if original sequence data can be recovered - */ - public boolean hasOriginalSequenceData() - { - return seqData != null; - } - - /** - * Returns original alignment data used for calculation - or null where not - * available. - * - * @return null or cut'n'pasteable alignment - */ - public String printOriginalSequenceData(char gapChar) - { - if (seqData == null) - { - return null; - } - - StringBuffer sb = new StringBuffer(); - String[] seqdatas = seqData.getSequenceStrings(gapChar); - for (int i = 0; i < seqdatas.length; i++) - { - sb.append(new jalview.util.Format("%-" + 15 + "s").form(sequences[i] - .getName())); - sb.append(" " + seqdatas[i] + "\n"); - } - return sb.toString(); - } - - /** * DOCUMENT ME! * * @param nd * DOCUMENT ME! */ - void printN(SequenceNode nd) + void printN(BinaryNode nd) { if (nd == null) { @@ -515,16 +476,16 @@ public class TreeModel if ((nd.left() != null) && (nd.right() != null)) { - printN((SequenceNode) nd.left()); - printN((SequenceNode) nd.right()); + printN((BinaryNode) nd.left()); + printN((BinaryNode) nd.right()); } else { - System.out.println(" name = " + ((SequenceI) nd.element()).getName()); + jalview.bin.Console.outPrintln(" name = " + ((SequenceI) nd.element()).getName()); } - System.out.println(" dist = " + nd.dist + " " + nd.count + " " - + nd.height); + jalview.bin.Console.outPrintln( + " dist = " + nd.dist + " " + nd.count + " " + nd.height); } /** @@ -533,7 +494,7 @@ public class TreeModel * @param nd * DOCUMENT ME! */ - public void reCount(SequenceNode nd) + public void reCount(BinaryNode nd) { ycount = 0; // _lycount = 0; @@ -549,11 +510,12 @@ public class TreeModel * @param nd * DOCUMENT ME! */ - void _reCount(SequenceNode nd) + void _reCount(BinaryNode nd) { // if (_lycount<_lylimit) // { - // System.err.println("Warning: depth of _recount greater than number of nodes."); + // jalview.bin.Console.errPrintln("Warning: depth of _recount greater than number of + // nodes."); // } if (nd == null) { @@ -564,11 +526,11 @@ public class TreeModel if ((nd.left() != null) && (nd.right() != null)) { - _reCount((SequenceNode) nd.left()); - _reCount((SequenceNode) nd.right()); + _reCount((BinaryNode) nd.left()); + _reCount((BinaryNode) nd.right()); - SequenceNode l = (SequenceNode) nd.left(); - SequenceNode r = (SequenceNode) nd.right(); + BinaryNode l = (BinaryNode) nd.left(); + BinaryNode r = (BinaryNode) nd.right(); nd.count = l.count + r.count; nd.ycount = (l.ycount + r.ycount) / 2; @@ -587,14 +549,14 @@ public class TreeModel * @param nd * DOCUMENT ME! */ - public void swapNodes(SequenceNode nd) + public void swapNodes(BinaryNode nd) { if (nd == null) { return; } - SequenceNode tmp = (SequenceNode) nd.left(); + BinaryNode tmp = (BinaryNode) nd.left(); nd.setLeft(nd.right()); nd.setRight(tmp); @@ -608,7 +570,7 @@ public class TreeModel * @param dir * DOCUMENT ME! */ - void changeDirection(SequenceNode nd, SequenceNode dir) + void changeDirection(BinaryNode nd, BinaryNode dir) { if (nd == null) { @@ -617,9 +579,9 @@ public class TreeModel if (nd.parent() != top) { - changeDirection((SequenceNode) nd.parent(), nd); + changeDirection((BinaryNode) nd.parent(), nd); - SequenceNode tmp = (SequenceNode) nd.parent(); + BinaryNode tmp = (BinaryNode) nd.parent(); if (dir == nd.left()) { @@ -668,7 +630,7 @@ public class TreeModel * * @return DOCUMENT ME! */ - public SequenceNode getTopNode() + public BinaryNode getTopNode() { return top; } @@ -703,11 +665,16 @@ public class TreeModel */ public void applyToNodes(NodeTransformI nodeTransformI) { - for (Enumeration nodes = node.elements(); nodes - .hasMoreElements(); nodeTransformI.transform(nodes - .nextElement())) + for (Enumeration nodes = node.elements(); nodes + .hasMoreElements(); nodeTransformI + .transform(nodes.nextElement())) { ; } } + + public AlignmentView getOriginalData() + { + return seqData; + } }