From: jprocter Date: Tue, 21 Aug 2007 12:43:54 +0000 (+0000) Subject: newickfile class moved into its own package ready for refactoring public methods... X-Git-Tag: Release_0.2~83 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=322373e3ca9cef7843be21e01032cf9c0c9a5c12;p=vamsas.git newickfile class moved into its own package ready for refactoring public methods into Trees class. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@437 be28352e-c001-0410-b1a7-c7978e42abec --- diff --git a/src/uk/ac/vamsas/objects/utils/Trees.java b/src/uk/ac/vamsas/objects/utils/Trees.java new file mode 100644 index 0000000..45d4e99 --- /dev/null +++ b/src/uk/ac/vamsas/objects/utils/Trees.java @@ -0,0 +1,5 @@ +package uk.ac.vamsas.objects.utils; + +public class Trees { + +} diff --git a/src/uk/ac/vamsas/objects/utils/NewickFile.java b/src/uk/ac/vamsas/objects/utils/trees/NewickFile.java similarity index 84% rename from src/uk/ac/vamsas/objects/utils/NewickFile.java rename to src/uk/ac/vamsas/objects/utils/trees/NewickFile.java index 058bca9..db62ef9 100644 --- a/src/uk/ac/vamsas/objects/utils/NewickFile.java +++ b/src/uk/ac/vamsas/objects/utils/trees/NewickFile.java @@ -23,15 +23,20 @@ // TODO: Implement Basic NHX tag parsing and preservation // TODO: http://evolution.genetics.wustl.edu/eddy/forester/NHX.html // TODO: Extended SequenceNodeI to hold parsed NHX strings -package uk.ac.vamsas.objects.utils; +package uk.ac.vamsas.objects.utils.trees; import java.io.*; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Vector; 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; /** * DOCUMENT ME! @@ -1131,4 +1136,136 @@ public class NewickFile { e.printStackTrace(); } } + /** + * Search for leaf nodes. + * + * @param node root node to search from + * @param leaves Vector of leaves to add leaf node objects too. + * + * @return Vector of leaf nodes on binary tree + */ + public Vector findLeaves(SequenceNode node, Vector leaves) + { + if (node == null) + { + return leaves; + } + + if ( (node.left() == null) && (node.right() == null)) // Interior node detection + { + leaves.addElement(node); + + return leaves; + } + else + { +/* TODO: Identify internal nodes... if (node.isSequenceLabel()) + { + leaves.addElement(node); + }*/ + findLeaves( (SequenceNode) node.left(), leaves); + findLeaves( (SequenceNode) node.right(), leaves); + } + + return leaves; + } + + /** + * make tree node vector from a newick tree structure with associated vamsas objects + * @param ntree + * @return + */ + public Treenode[] makeTreeNodes() { // NJTree ntree) { + 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()) + { + BinaryNode tnode = (BinaryNode) l.nextElement(); + if (tnode instanceof SequenceNode) + { + if (!((SequenceNode) tnode).isPlaceholder()) + { + Object assocseq = ((SequenceNode) tnode).element(); + if (assocseq instanceof Vobject) + { + Vobject vobj = (Vobject) assocseq; + if (vobj!=null) + { + Treenode node = new Treenode(); + node.setNodespec(makeNodeSpec(nodespecs, tnode)); + node.setName(tnode.getName()); + Vref vr = new Vref(); + vr.addRefs(vobj); + node.addVref(vr); + tnv.addElement(node); + } + else + { + System.err.println("WARNING: Unassociated treeNode "+tnode.element().toString()+" " + +((tnode.getName()!=null) ? " label "+tnode.getName() : "")); + } + } + } + } + } + if (tnv.size()>0) + { + Treenode[] tn = new Treenode[tnv.size()]; + tnv.copyInto(tn); + return tn; + } + return new Treenode[] {}; + } + private String makeNodeSpec(Hashtable nodespecs, BinaryNode tnode) + { + String nname = new String(tnode.getName()); + Integer nindx = (Integer) nodespecs.get(nname); + if (nindx==null) + { + nindx = new Integer(1); + } + nname = nindx.toString()+" "+nname; + return nname; + } + /** + * call to match up Treenode specs to NJTree parsed from document object. + * + * @param nodespec + * @param leaves + * as returned from NJTree.findLeaves( .., ..) .. + * @return + */ + private BinaryNode findNodeSpec(String nodespec, Vector leaves) + { + int occurence=-1; + String nspec = nodespec.substring(nodespec.indexOf(' ')+1); + String oval = nodespec.substring(0, nodespec.indexOf(' ')); + try { + occurence = new Integer(oval).intValue(); + } + catch (Exception e) + { + System.err.println("Invalid nodespec '"+nodespec+"'"); + return null; + } + BinaryNode bn = null; + + int nocc = 0; + Enumeration en = leaves.elements(); + while (en.hasMoreElements() && nocc