From: jprocter Date: Tue, 21 Aug 2007 16:01:45 +0000 (+0000) Subject: refactored from newickfile X-Git-Tag: Release_0.2~82 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=b548b51490ddfed45bc07e067cf71b9ab5cac600;p=vamsas.git refactored from newickfile git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@438 be28352e-c001-0410-b1a7-c7978e42abec --- diff --git a/src/uk/ac/vamsas/objects/utils/trees/BinaryNode.java b/src/uk/ac/vamsas/objects/utils/trees/BinaryNode.java new file mode 100644 index 0000000..ca0739a --- /dev/null +++ b/src/uk/ac/vamsas/objects/utils/trees/BinaryNode.java @@ -0,0 +1,278 @@ +/** + * + */ +package uk.ac.vamsas.objects.utils.trees; + +import uk.ac.vamsas.client.Vobject; +import uk.ac.vamsas.client.VorbaId; + +public class BinaryNode { + Vobject 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 val + * DOCUMENT ME! + * @param parent + * DOCUMENT ME! + * @param name + * DOCUMENT ME! + */ + public BinaryNode(Vobject val, BinaryNode parent, String name) { + this.element = val; + this.parent = parent; + this.name = name; + + left = right = null; + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Vobject element() { + return element; + } + + /** + * DOCUMENT ME! + * + * @param v + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Vobject setElement(Vobject 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.getVorbaId().getId()+"|v|" : "") + + ((name == null) ? "" : name); + } + return ""; + } + + * note we probably don't need this now + + * 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; + }*/ +} \ No newline at end of file diff --git a/src/uk/ac/vamsas/objects/utils/trees/SequenceNode.java b/src/uk/ac/vamsas/objects/utils/trees/SequenceNode.java new file mode 100644 index 0000000..9f2a05d --- /dev/null +++ b/src/uk/ac/vamsas/objects/utils/trees/SequenceNode.java @@ -0,0 +1,123 @@ +/** + * + */ +package uk.ac.vamsas.objects.utils.trees; + +import uk.ac.vamsas.client.Vobject; +import uk.ac.vamsas.client.VorbaId; + +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(Vobject val, SequenceNode parent, float dist, String name) { + super(val, 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, 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 referred 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; + } + +} \ No newline at end of file