From c5b324d5bb93fd421347f87ffe59bd6efe076d1e Mon Sep 17 00:00:00 2001 From: jprocter Date: Tue, 21 Aug 2007 16:02:54 +0000 Subject: [PATCH] refactored and new methods for making and processing treenode vector for parsed newick string git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@439 be28352e-c001-0410-b1a7-c7978e42abec --- .../ac/vamsas/objects/utils/trees/NewickFile.java | 454 +++----------------- 1 file changed, 59 insertions(+), 395 deletions(-) diff --git a/src/uk/ac/vamsas/objects/utils/trees/NewickFile.java b/src/uk/ac/vamsas/objects/utils/trees/NewickFile.java index db62ef9..2e1e893 100644 --- a/src/uk/ac/vamsas/objects/utils/trees/NewickFile.java +++ b/src/uk/ac/vamsas/objects/utils/trees/NewickFile.java @@ -1,4 +1,6 @@ /* + * originally from + * * Jalview - A Sequence Alignment Editor and Viewer * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * @@ -25,6 +27,7 @@ // TODO: Extended SequenceNodeI to hold parsed NHX strings package uk.ac.vamsas.objects.utils.trees; + import java.io.*; import java.util.Enumeration; import java.util.Hashtable; @@ -33,8 +36,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import uk.ac.vamsas.client.Vobject; -import uk.ac.vamsas.client.VorbaId; -import uk.ac.vamsas.objects.core.SequenceType; import uk.ac.vamsas.objects.core.Treenode; import uk.ac.vamsas.objects.core.Vref; @@ -45,395 +46,6 @@ import uk.ac.vamsas.objects.core.Vref; * @version $Revision: 1.12 $ */ public class NewickFile { - public class BinaryNode { - VorbaId element; - - String name; - - BinaryNode left; - - BinaryNode right; - - BinaryNode parent; - - /** DOCUMENT ME!! */ - public int bootstrap; - - /** - * Creates a new BinaryNode object. - */ - public BinaryNode() { - left = right = parent = null; - bootstrap = 0; - } - - /** - * Creates a new BinaryNode object. - * - * @param element - * DOCUMENT ME! - * @param parent - * DOCUMENT ME! - * @param name - * DOCUMENT ME! - */ - public BinaryNode(VorbaId element, BinaryNode parent, String name) { - this.element = element; - this.parent = parent; - this.name = name; - - left = right = null; - } - - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public VorbaId element() { - return element; - } - - /** - * DOCUMENT ME! - * - * @param v - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public VorbaId setElement(VorbaId v) { - return element = v; - } - - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public BinaryNode left() { - return left; - } - - /** - * DOCUMENT ME! - * - * @param n - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public BinaryNode setLeft(BinaryNode n) { - return left = n; - } - - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public BinaryNode right() { - return right; - } - - /** - * DOCUMENT ME! - * - * @param n - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public BinaryNode setRight(BinaryNode n) { - return right = n; - } - - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public BinaryNode parent() { - return parent; - } - - /** - * DOCUMENT ME! - * - * @param n - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public BinaryNode setParent(BinaryNode n) { - return parent = n; - } - - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public boolean isLeaf() { - return (left == null) && (right == null); - } - - /** - * attaches FIRST and SECOND node arguments as the LEFT and RIGHT children - * of this node (removing any old references) a null parameter DOES NOT mean - * that the pointer to the corresponding child node is set to NULL - you - * should use setChild(null), or detach() for this. - * - */ - public void SetChildren(BinaryNode leftchild, BinaryNode rightchild) { - if (leftchild != null) { - this.setLeft(leftchild); - leftchild.detach(); - leftchild.setParent(this); - } - - if (rightchild != null) { - this.setRight(rightchild); - rightchild.detach(); - rightchild.setParent(this); - } - } - - /** - * Detaches the node from the binary tree, along with all its child nodes. - * - * @return BinaryNode The detached node. - */ - public BinaryNode detach() { - if (this.parent != null) { - if (this.parent.left == this) { - this.parent.left = null; - } else { - if (this.parent.right == this) { - this.parent.right = null; - } - } - } - - this.parent = null; - - return this; - } - - /** - * Traverses up through the tree until a node with a free leftchild is - * discovered. - * - * @return BinaryNode - */ - public BinaryNode ascendLeft() { - BinaryNode c = this; - - do { - c = c.parent(); - } while ((c != null) && (c.left() != null) && !c.left().isLeaf()); - - return c; - } - - /** - * Traverses up through the tree until a node with a free rightchild is - * discovered. Jalview builds trees by descent on the left, so this may be - * unused. - * - * @return BinaryNode - */ - public BinaryNode ascendRight() { - BinaryNode c = this; - - do { - c = c.parent(); - } while ((c != null) && (c.right() != null) && !c.right().isLeaf()); - - return c; - } - - /** - * DOCUMENT ME! - * - * @param name - * DOCUMENT ME! - */ - public void setName(String name) { - this.name = name; - } - - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public String getName() { - return this.name; - } - - /** - * DOCUMENT ME! - * - * @param boot - * DOCUMENT ME! - */ - public void setBootstrap(int boot) { - this.bootstrap = boot; - } - - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public int getBootstrap() { - return bootstrap; - } - /** - * - * @return unquoted string of form VorbaId|v|node name string - */ - public String getNewickNodeName() { - if (element!=null || name!=null) - { - return ((element!=null) ? element.getId()+"|v|" : "") - + ((name == null) ? "" : name); - } - return ""; - } - public boolean parseNodeNameString(uk.ac.vamsas.client.ClientDocument binder) - { - - if (element==null && name!=null && name.indexOf("|v|")>-1) - { - element = binder.getObject(null).getVorbaId(); // TODO: fix THIS ! name.substring(0, name.indexOf("|v"))); - - } - return false; - } - } - - public class SequenceNode extends BinaryNode { - /** DOCUMENT ME!! */ - public float dist; - - /** DOCUMENT ME!! */ - public boolean dummy = false; - - private boolean placeholder = false; - - /** - * Creates a new SequenceNode object. - */ - public SequenceNode() { - super(); - } - - /** - * Creates a new SequenceNode object. - * - * @param val - * DOCUMENT ME! - * @param parent - * DOCUMENT ME! - * @param dist - * DOCUMENT ME! - * @param name - * DOCUMENT ME! - */ - public SequenceNode(VorbaId val, SequenceNode parent, float dist, String name) { - super(val, parent, name); - this.dist = dist; - } - public SequenceNode(Vobject val, SequenceNode parent, float dist, String name) { - super(val.getVorbaId(), parent, name); - this.dist = dist; - } - - /** - * Creates a new SequenceNode object. - * - * @param val - * DOCUMENT ME! - * @param parent - * DOCUMENT ME! - * @param name - * DOCUMENT ME! - * @param dist - * DOCUMENT ME! - * @param bootstrap - * DOCUMENT ME! - * @param dummy - * DOCUMENT ME! - */ - public SequenceNode(Vobject val, SequenceNode parent, String name, - float dist, int bootstrap, boolean dummy) { - super(val.getVorbaId(), parent, name); - this.dist = dist; - this.bootstrap = bootstrap; - this.dummy = dummy; - } - - /** - * @param dummy - * true if node is created for the representation of polytomous - * trees - */ - public boolean isDummy() { - return dummy; - } - - /* - * @param placeholder is true if the sequence refered to in the element node - * is not actually present in the associated alignment - */ - public boolean isPlaceholder() { - return placeholder; - } - - /** - * DOCUMENT ME! - * - * @param newstate - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public boolean setDummy(boolean newstate) { - boolean oldstate = dummy; - dummy = newstate; - - return oldstate; - } - - /** - * DOCUMENT ME! - * - * @param Placeholder - * DOCUMENT ME! - */ - public void setPlaceholder(boolean Placeholder) { - this.placeholder = Placeholder; - } - - /** - * ascends the tree but doesn't stop until a non-dummy node is discovered. - * This will probably break if the tree is a mixture of BinaryNodes and - * SequenceNodes. - */ - public SequenceNode AscendTree() { - SequenceNode c = this; - - do { - c = (SequenceNode) c.parent(); - } while ((c != null) && c.dummy); - - return c; - } - - } - SequenceNode root; private boolean HasBootstrap = false; @@ -1018,8 +630,9 @@ public class NewickFile { * @return DOCUMENT ME! */ private String printNodeField(SequenceNode c) { - return c.getNewickNodeName() - + ((HasBootstrap) ? ((c.getBootstrap() > -1) ? (" " + c.getBootstrap()) + return //c.getNewickNodeName() + ((c.getName() == null) ? "" : nodeName(c.getName())) + + ((HasBootstrap) ? ((c.getBootstrap() > -1) ? (" " + c.getBootstrap()) : "") : "") + ((HasDistances) ? (":" + c.dist) : ""); } @@ -1175,12 +788,11 @@ public class NewickFile { * @param ntree * @return */ - public Treenode[] makeTreeNodes() { // NJTree ntree) { + public Treenode[] makeTreeNodes() { Vector leaves = new Vector(); findLeaves(root, leaves); Vector tnv = new Vector(); Enumeration l = leaves.elements(); - int i=0; Hashtable nodespecs = new Hashtable(); while (l.hasMoreElements()) { @@ -1267,5 +879,57 @@ public class NewickFile { } return bn; } + /** + * + * re-decorate the newick node representation with the VorbaId of an object mapped by its corresponding TreeNode. + * Note: this only takes the first object in the first set of references. + * @param tn + * @return vector of mappings { treenode, SequenceNode, Vobject for VorbaId on sequence node } + */ + public Vector attachTreeMap(Treenode[] tn) + { + if (root!=null || tn==null) + return null; + Vector leaves = new Vector(); + Vector nodemap=new Vector(); + findLeaves(root, leaves); + int sz = tn.length; + int i = 0; + + while (i < sz) + { + Treenode node = tn[i++]; + BinaryNode mappednode = findNodeSpec(node.getNodespec(),leaves); + if (mappednode!=null && mappednode instanceof SequenceNode) { + SequenceNode leaf = (SequenceNode) leaves.elementAt(i++); + // check if we can make the specified association + Vobject noderef = null; + int vrf=0,refv=0; + while (noderef==null && vrf