// TODO: Implement Basic NHX tag parsing and preservation\r
// TODO: http://evolution.genetics.wustl.edu/eddy/forester/NHX.html\r
// TODO: Extended SequenceNodeI to hold parsed NHX strings\r
-package uk.ac.vamsas.objects.utils;\r
+package uk.ac.vamsas.objects.utils.trees;\r
\r
import java.io.*;\r
+import java.util.Enumeration;\r
+import java.util.Hashtable;\r
+import java.util.Vector;\r
import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
import uk.ac.vamsas.client.Vobject;\r
import uk.ac.vamsas.client.VorbaId;\r
import uk.ac.vamsas.objects.core.SequenceType;\r
+import uk.ac.vamsas.objects.core.Treenode;\r
+import uk.ac.vamsas.objects.core.Vref;\r
\r
/**\r
* DOCUMENT ME!\r
e.printStackTrace();\r
}\r
}\r
+ /**\r
+ * Search for leaf nodes.\r
+ *\r
+ * @param node root node to search from\r
+ * @param leaves Vector of leaves to add leaf node objects too.\r
+ *\r
+ * @return Vector of leaf nodes on binary tree\r
+ */\r
+ public Vector findLeaves(SequenceNode node, Vector leaves)\r
+ { \r
+ if (node == null)\r
+ {\r
+ return leaves;\r
+ }\r
+\r
+ if ( (node.left() == null) && (node.right() == null)) // Interior node detection\r
+ {\r
+ leaves.addElement(node);\r
+\r
+ return leaves;\r
+ }\r
+ else\r
+ {\r
+/* TODO: Identify internal nodes... if (node.isSequenceLabel())\r
+ {\r
+ leaves.addElement(node);\r
+ }*/\r
+ findLeaves( (SequenceNode) node.left(), leaves);\r
+ findLeaves( (SequenceNode) node.right(), leaves);\r
+ }\r
+\r
+ return leaves;\r
+ }\r
+\r
+ /**\r
+ * make tree node vector from a newick tree structure with associated vamsas objects\r
+ * @param ntree\r
+ * @return\r
+ */\r
+ public Treenode[] makeTreeNodes() { // NJTree ntree) {\r
+ Vector leaves = new Vector();\r
+ findLeaves(root, leaves);\r
+ Vector tnv = new Vector();\r
+ Enumeration l = leaves.elements();\r
+ int i=0;\r
+ Hashtable nodespecs = new Hashtable();\r
+ while (l.hasMoreElements())\r
+ {\r
+ BinaryNode tnode = (BinaryNode) l.nextElement();\r
+ if (tnode instanceof SequenceNode)\r
+ {\r
+ if (!((SequenceNode) tnode).isPlaceholder())\r
+ {\r
+ Object assocseq = ((SequenceNode) tnode).element();\r
+ if (assocseq instanceof Vobject)\r
+ {\r
+ Vobject vobj = (Vobject) assocseq;\r
+ if (vobj!=null)\r
+ {\r
+ Treenode node = new Treenode();\r
+ node.setNodespec(makeNodeSpec(nodespecs, tnode));\r
+ node.setName(tnode.getName());\r
+ Vref vr = new Vref();\r
+ vr.addRefs(vobj);\r
+ node.addVref(vr);\r
+ tnv.addElement(node);\r
+ }\r
+ else\r
+ {\r
+ System.err.println("WARNING: Unassociated treeNode "+tnode.element().toString()+" "\r
+ +((tnode.getName()!=null) ? " label "+tnode.getName() : ""));\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ if (tnv.size()>0)\r
+ {\r
+ Treenode[] tn = new Treenode[tnv.size()];\r
+ tnv.copyInto(tn); \r
+ return tn;\r
+ }\r
+ return new Treenode[] {};\r
+ }\r
+ private String makeNodeSpec(Hashtable nodespecs, BinaryNode tnode)\r
+ { \r
+ String nname = new String(tnode.getName());\r
+ Integer nindx = (Integer) nodespecs.get(nname);\r
+ if (nindx==null)\r
+ {\r
+ nindx = new Integer(1);\r
+ }\r
+ nname = nindx.toString()+" "+nname;\r
+ return nname;\r
+ }\r
+ /**\r
+ * call to match up Treenode specs to NJTree parsed from document object.\r
+ * \r
+ * @param nodespec\r
+ * @param leaves\r
+ * as returned from NJTree.findLeaves( .., ..) ..\r
+ * @return\r
+ */\r
+ private BinaryNode findNodeSpec(String nodespec, Vector leaves)\r
+ {\r
+ int occurence=-1;\r
+ String nspec = nodespec.substring(nodespec.indexOf(' ')+1);\r
+ String oval = nodespec.substring(0, nodespec.indexOf(' '));\r
+ try {\r
+ occurence = new Integer(oval).intValue();\r
+ }\r
+ catch (Exception e)\r
+ {\r
+ System.err.println("Invalid nodespec '"+nodespec+"'");\r
+ return null;\r
+ }\r
+ BinaryNode bn = null;\r
+ \r
+ int nocc = 0;\r
+ Enumeration en = leaves.elements();\r
+ while (en.hasMoreElements() && nocc<occurence)\r
+ {\r
+ bn = (BinaryNode) en.nextElement();\r
+ if (bn instanceof SequenceNode && bn.getName().equals(nspec))\r
+ {\r
+ --occurence;\r
+ } else \r
+ bn=null;\r
+ }\r
+ return bn;\r
+ }\r
+\r
}\r