-package jalview.datamodel;\r
-\r
-public class BinaryNode {\r
-\r
- Object element;\r
- String name;\r
- BinaryNode left;\r
- BinaryNode right;\r
- BinaryNode parent;\r
- public int bootstrap;\r
-\r
- public BinaryNode() {\r
- left = right = parent = null;\r
- }\r
-\r
- public BinaryNode(Object element, BinaryNode parent,String name) {\r
- this.element = element;\r
- this.parent = parent;\r
- this.name = name;\r
-\r
- left=right=null;\r
- }\r
-\r
- public Object element() {\r
- return element;\r
- }\r
-\r
- public Object setElement(Object v) {\r
- return element=v;\r
- }\r
-\r
- public BinaryNode left() {\r
- return left;\r
- }\r
-\r
- public BinaryNode setLeft(BinaryNode n) {\r
- return left=n;\r
- }\r
-\r
- public BinaryNode right() {\r
- return right;\r
- }\r
-\r
- public BinaryNode setRight(BinaryNode n) {\r
- return right=n;\r
- }\r
-\r
- public BinaryNode parent() {\r
- return parent;\r
- }\r
-\r
- public BinaryNode setParent(BinaryNode n) {\r
- return parent=n;\r
- }\r
-\r
- public boolean isLeaf() {\r
- return (left == null) && (right == null);\r
- }\r
-\r
- public void setName(String name) {\r
- this.name = name;\r
- }\r
- public String getName() {\r
- return this.name;\r
- }\r
- public void setBootstrap(int boot) {\r
- this.bootstrap = boot;\r
- }\r
- public int getBootstrap() {\r
- return bootstrap;\r
- }\r
-}\r
-\r
+package jalview.datamodel;
+
+public class BinaryNode
+{
+
+ Object element;
+ String name;
+ BinaryNode left;
+ BinaryNode right;
+ BinaryNode parent;
+ public int bootstrap;
+
+ public BinaryNode()
+ {
+ left = right = parent = null;
+ bootstrap = 0;
+ }
+
+ public BinaryNode(Object element, BinaryNode parent, String name)
+ {
+ this.element = element;
+ this.parent = parent;
+ this.name = name;
+
+ left = right = null;
+ }
+
+ public Object element()
+ {
+ return element;
+ }
+
+ public Object setElement(Object v)
+ {
+ return element = v;
+ }
+
+ public BinaryNode left()
+ {
+ return left;
+ }
+
+ public BinaryNode setLeft(BinaryNode n)
+ {
+ return left = n;
+ }
+
+ public BinaryNode right()
+ {
+ return right;
+ }
+
+ public BinaryNode setRight(BinaryNode n)
+ {
+ return right = n;
+ }
+
+ public BinaryNode parent()
+ {
+ return parent;
+ }
+
+ public BinaryNode setParent(BinaryNode n)
+ {
+ return parent = n;
+ }
+
+ 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;
+ }
+
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ public String getName()
+ {
+ return this.name;
+ }
+
+ public void setBootstrap(int boot)
+ {
+ this.bootstrap = boot;
+ }
+
+ public int getBootstrap()
+ {
+ return bootstrap;
+ }
+}
public float height;\r
public float ycount;\r
public Color color = Color.black;\r
-\r
+ public boolean dummy = false;\r
public SequenceNode() {\r
super();\r
}\r
- \r
+\r
public SequenceNode(Object val, SequenceNode parent, float dist,String name) {\r
super(val,parent,name);\r
this.dist = dist;\r
}\r
+ public SequenceNode(Object val, SequenceNode parent, String name, float dist, int bootstrap, boolean dummy) {\r
+ super(val,parent,name);\r
+ this.dist = dist;\r
+ this.bootstrap = bootstrap;\r
+ this.dummy = dummy;\r
+ }\r
+\r
+\r
+ /**\r
+ * @param dummy true if node is created for the representation of polytomous trees\r
+ */\r
+\r
+ public boolean isDummy() {\r
+ return dummy;\r
+ }\r
+ public boolean setDummy(boolean newstate) {\r
+ boolean oldstate = dummy;\r
+ dummy = newstate;\r
+ return oldstate;\r
+ }\r
+\r
+ /**\r
+ * ascends the tree but doesn't stop until a non-dummy node is discovered.\r
+ * This will probably break if the tree is a mixture of BinaryNodes and SequenceNodes.\r
+ */\r
+\r
+ public SequenceNode AscendTree() {\r
+ SequenceNode c = this;\r
+ do {\r
+ c = (SequenceNode) c.parent();\r
+ } while (c!=null && c.dummy);\r
+ return c;\r
+ }\r
}\r
private boolean HasBootstrap = false;
private boolean HasDistances = false;
private boolean RootHasDistance = false;
+
private String ErrorStringrange(String Error, String Er, int r, int p, String s) {
return ((Error==null) ? "" : Error)
+ Er +
super(inFile, type);
}
+ // File IO Flags
+ boolean ReplaceUnderscores = false;
public void parse() throws IOException
{
}
root = new SequenceNode();
+ SequenceNode realroot = null;
SequenceNode c = root;
int d = -1;
int cp = 0;
c.setLeft(new SequenceNode(null, c, 0, null));
c = (SequenceNode) c.left();
}
+ if (realroot==null) {
+ realroot = c;
+ }
nodename = null;
distance = -99999;
bootstrap = -99999;
{
if (nodename == null)
{
- nodename = uqnodename.stringMatched(1).replace('_', ' ');
+ if (ReplaceUnderscores) {
+ nodename = uqnodename.stringMatched(1).replace('_', ' ');
+ } else {
+ nodename = uqnodename.stringMatched(1);
+ }
}
else
{
cp + nbootstrap.matchedFrom(), nf);
}
}
-
+ boolean nodehasdistance=false;
if (ndist.search(fstring))
{
try
{
distance = (new Float(ndist.stringMatched(1))).floatValue();
HasDistances = true;
- if (c.parent()==root) {
- RootHasDistance = true;
- }
+ nodehasdistance = true;
}
catch (Exception e)
{
c.setName(nodename);
c.dist = (HasDistances) ? distance : 0;
c.setBootstrap((HasBootstrap) ? bootstrap : 0);
+ if (c==realroot) {
+ RootHasDistance = nodehasdistance; // JBPNote This is really UGLY!!!
+ }
}
else
{
}
root = (SequenceNode) root.right().detach(); // remove the imaginary root.
+ if (!RootHasDistance) {
+ root.dist = 0;
+ }
}
public NewickFile(SequenceNode newtree) {