applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / objects / utils / trees / NewickFile.java
index 2e1e893..d38c99a 100644 (file)
@@ -1,24 +1,24 @@
 /*\r
- * originally from \r
+ * This file is part of the Vamsas Client version 0.1. \r
+ * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
+ *  Andrew Waterhouse and Dominik Lindner.\r
  * \r
- * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
- *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
+ * Earlier versions have also been incorporated into Jalview version 2.4 \r
+ * since 2008, and TOPALi version 2 since 2007.\r
+ * \r
+ * The Vamsas Client is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU Lesser General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *  \r
+ * The Vamsas Client is distributed in the hope that it will be useful,\r
  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
+ * GNU Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public License\r
+ * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
  */\r
-\r
 // NewickFile.java\r
 // Tree I/O\r
 // http://evolution.genetics.washington.edu/phylip/newick_doc.html\r
@@ -27,7 +27,6 @@
 // TODO: Extended SequenceNodeI to hold parsed NHX strings\r
 package uk.ac.vamsas.objects.utils.trees;\r
 \r
-\r
 import java.io.*;\r
 import java.util.Enumeration;\r
 import java.util.Hashtable;\r
@@ -55,14 +54,14 @@ public class NewickFile {
   private boolean RootHasDistance = false;\r
 \r
   // File IO Flags\r
-  boolean ReplaceUnderscores = false;\r
+  boolean ReplaceUnderscores = true;\r
 \r
-  boolean printRootInfo = false;\r
+  boolean printRootInfo = true; // in case root has anything to preserve\r
 \r
   private Pattern[] NodeSafeName = new Pattern[] {\r
       Pattern.compile("[\\[,:'()]"), // test for requiring quotes\r
       Pattern.compile("'"), // escaping quote characters\r
-      Pattern.compile("/w") // unqoted whitespace transformation\r
+      Pattern.compile("\\s") // unqoted whitespace transformation\r
   };\r
 \r
   char QuoteChar = '\'';\r
@@ -70,23 +69,26 @@ public class NewickFile {
   String newickFile = null;\r
 \r
   /**\r
-   * Creates a new NewickFile object.\r
+   * Creates a new NewickFile object\r
    * \r
    * @param inStr\r
-   *          DOCUMENT ME!\r
+   *          Newick style tree string\r
    * \r
    * @throws IOException\r
-   *           DOCUMENT ME!\r
+   *           if string is not a valid newick file\r
    */\r
   public NewickFile(String inStr) throws IOException {\r
     newickFile = inStr;\r
     parse();\r
   }\r
+\r
   public NewickFile(File inFile) throws IOException {\r
-    errormessage = "Problem's reading file "+inFile;\r
-    dataIn = new java.io.BufferedReader(new InputStreamReader(new java.io.FileInputStream(inFile)));\r
+    errormessage = "Problem's reading file " + inFile;\r
+    dataIn = new java.io.BufferedReader(new InputStreamReader(\r
+        new java.io.FileInputStream(inFile)));\r
     parse();\r
   }\r
+\r
   /**\r
    * Creates a new NewickFile object.\r
    * \r
@@ -196,34 +198,40 @@ public class NewickFile {
   public boolean HasRootDistance() {\r
     return RootHasDistance;\r
   }\r
+\r
   /*\r
    * hacked out of jalview code\r
    */\r
   boolean error;\r
+\r
   String errormessage;\r
-  java.io.BufferedReader dataIn=null;\r
+\r
+  java.io.BufferedReader dataIn = null;\r
+\r
   public String nextLine() throws IOException {\r
-    if (dataIn==null)\r
-    {\r
+    if (dataIn == null && newickFile == null)\r
+      throw new IOException(\r
+          "IMPLEMENTATION ERROR: NewickFile has not been initialised for reading a newick string.");\r
+    if (dataIn == null) {\r
       dataIn = new BufferedReader(new StringReader(newickFile));\r
-      error=false;\r
+      error = false;\r
     }\r
-    else\r
-      throw new IOException("IMPLEMENTATION ERROR: NewickFile has not been initialised for reading a newick string.");\r
     if (!error)\r
       return dataIn.readLine();\r
     throw new IOException("Invalid Source Stream:" + errormessage);\r
   }\r
+\r
   /**\r
-   * call this to convert the newick string into a binary node linked tree\r
+   * call this to convert the newick string into a binary node linked tree Note:\r
+   * this is automatically called by the constructors, so you normally wouldn't\r
+   * need to use this.\r
    * \r
    * @throws IOException\r
    *           if the newick string cannot be parsed.\r
    */\r
   public void parse() throws IOException {\r
     String nf;\r
-    if (newickFile==null)\r
-    {\r
+    if (newickFile == null) {\r
       // fill nf with complete tree file\r
 \r
       StringBuffer file = new StringBuffer();\r
@@ -233,11 +241,9 @@ public class NewickFile {
       }\r
 \r
       nf = file.toString();\r
-    } else\r
-    {\r
+    } else {\r
       nf = newickFile;\r
     }\r
-    \r
 \r
     root = new SequenceNode();\r
 \r
@@ -252,37 +258,25 @@ public class NewickFile {
     String nodename = null;\r
 \r
     float DefDistance = (float) 0.001; // @param Default distance for a node -\r
-                                        // very very small\r
-    int DefBootstrap = 0; // @param Default bootstrap for a node\r
+    // very very small\r
+    int DefBootstrap = -1; // @param Default bootstrap for a node\r
 \r
     float distance = DefDistance;\r
     int bootstrap = DefBootstrap;\r
 \r
     boolean ascending = false; // flag indicating that we are leaving the\r
-                                // current node\r
+    // current node\r
 \r
-    Pattern majorsyms = Pattern.compile(\r
-        "[(\\['),;]");\r
+    Pattern majorsyms = Pattern.compile("[(\\['),;]");\r
 \r
     Matcher mjsyms = majorsyms.matcher(nf);\r
+    char schar;\r
+    int nextcp = 0;\r
+    int ncp = cp;\r
     while (mjsyms.find(cp) && (Error == null)) {\r
       int fcp = mjsyms.start();\r
 \r
-      switch (nf.charAt(fcp)) {\r
-      case '[': // Comment or structured/extended NH format info\r
-\r
-\r
-        if (nf.indexOf(']',fcp)>-1) {\r
-          // Skip the comment field\r
-          cp = nf.indexOf(']',fcp);\r
-        } else {\r
-          Error = ErrorStringrange(Error, "Unterminated comment", 3, fcp, nf);\r
-        }\r
-\r
-        ;\r
-\r
-        break;\r
-\r
+      switch (schar = nf.charAt(fcp)) {\r
       case '(':\r
 \r
         // ascending should not be set\r
@@ -338,19 +332,42 @@ public class NewickFile {
 \r
         break;\r
 \r
-      case ';':\r
-\r
-        if (d != -1) {\r
+      default:\r
+        // Reached termininating root node label.\r
+        if (schar == ';' && d != -1) {\r
           Error = ErrorStringrange(Error,\r
               "Wayward semicolon (depth=" + d + ")", 7, fcp, nf);\r
         }\r
 \r
-        // cp advanced at the end of default\r
-      default:\r
+        // Skip Comment or structured/extended NH format info\r
+        if (schar == '[') {\r
+          if ((nextcp = nf.indexOf(']', fcp)) > -1) {\r
+            // verified that comment is properly terminated.\r
+            // now skip the comment field\r
+            nextcp++;\r
+            break; // go and search for the next node separator, leaving ncp at\r
+                   // beginning of node info\r
+          } else {\r
+            Error = ErrorStringrange(Error, "Unterminated comment", 3, fcp, nf);\r
+            nextcp = 0;\r
+            break;\r
+          }\r
+        }\r
 \r
-        // Parse simpler field strings\r
-        String fstring = nf.substring(cp, fcp);\r
-        Matcher uqnodename = Pattern.compile("\\b([^' :;\\](),]+)").matcher(fstring);\r
+        // Parse simpler field strings from substring between ncp and node\r
+        // separator\r
+        String fstring = nf.substring(ncp, fcp);\r
+        // extract any comments from the nodeinfo.\r
+        while (fstring.indexOf(']') > -1) {\r
+          int cstart = fstring.indexOf('[');\r
+          int cend = fstring.indexOf(']');\r
+          String comment = fstring.substring(cstart + 1, cend); // TODO: put\r
+                                                                // this\r
+                                                                // somewhere ?\r
+          fstring = fstring.substring(0, cstart) + fstring.substring(cend + 1);\r
+        }\r
+        Matcher uqnodename = Pattern.compile("^([^' :;\\](),]+).*").matcher(\r
+            fstring);\r
         if (uqnodename.matches()\r
             && ((uqnodename.start(1) == 0) || (fstring.charAt(uqnodename\r
                 .start(1) - 1) != ':'))) // JBPNote HACK!\r
@@ -366,23 +383,27 @@ public class NewickFile {
                 "File has broken algorithm - overwritten nodename", 10, fcp, nf);\r
           }\r
         }\r
-        \r
-        Matcher nbootstrap = Pattern.compile("\\S+([0-9+]+)\\S*:").matcher(fstring);\r
 \r
+        Matcher nbootstrap = Pattern.compile("\\s*([+0-9]+)\\s*:.*").matcher(\r
+            fstring);\r
 \r
-        if (nbootstrap.matches()\r
-            && (nbootstrap.start(1) > uqnodename.end(1))) {\r
-          try {\r
-            bootstrap = (new Integer(nbootstrap.group(1))).intValue();\r
-            HasBootstrap = true;\r
-          } catch (Exception e) {\r
-            Error = ErrorStringrange(Error, "Can't parse bootstrap value", 4,\r
-                cp + nbootstrap.start(0), nf);\r
+        if (nbootstrap.matches()) {\r
+          if (nodename != null && nbootstrap.group(1).equals(nodename)) {\r
+            nodename = null; // empty nodename - only bootstrap value\r
+          }\r
+          if ((nodename == null || nodename.length() == 0)\r
+              || nbootstrap.start(1) >= uqnodename.end(1)) {\r
+            try {\r
+              bootstrap = (new Integer(nbootstrap.group(1))).intValue();\r
+              HasBootstrap = true;\r
+            } catch (Exception e) {\r
+              Error = ErrorStringrange(Error, "Can't parse bootstrap value", 4,\r
+                  ncp + nbootstrap.start(0), nf);\r
+            }\r
           }\r
         }\r
 \r
-        Matcher ndist = Pattern.compile(\r
-        ":([-0-9Ee.+]+)").matcher(fstring);\r
+        Matcher ndist = Pattern.compile(".*:([-0-9Ee.+]+)").matcher(fstring);\r
         boolean nodehasdistance = false;\r
 \r
         if (ndist.matches()) {\r
@@ -392,7 +413,7 @@ public class NewickFile {
             nodehasdistance = true;\r
           } catch (Exception e) {\r
             Error = ErrorStringrange(Error, "Can't parse node distance value",\r
-                7, cp + ndist.start(0), nf);\r
+                7, ncp + ndist.start(0), nf);\r
           }\r
         }\r
 \r
@@ -405,8 +426,8 @@ public class NewickFile {
           c.setBootstrap((HasBootstrap) ? bootstrap : DefBootstrap);\r
           if (c == realroot) {\r
             RootHasDistance = nodehasdistance; // JBPNote This is really\r
-                                                // UGLY!!! Ensure root node gets\r
-                                                // its given distance\r
+            // UGLY!!! Ensure root node gets\r
+            // its given distance\r
           }\r
         } else {\r
           // Find a place to put the leaf\r
@@ -456,16 +477,19 @@ public class NewickFile {
               }\r
             }\r
           }\r
-\r
-          // else : We do nothing if ';' is encountered.\r
         }\r
 \r
         // Reset new node properties to obvious fakes\r
         nodename = null;\r
         distance = DefDistance;\r
         bootstrap = DefBootstrap;\r
-\r
-        cp = fcp + 1;\r
+      }\r
+      // Advance character pointers if necessary\r
+      if (nextcp == 0) {\r
+        ncp = cp = fcp + 1;\r
+      } else {\r
+        cp = nextcp;\r
+        nextcp = 0;\r
       }\r
     }\r
 \r
@@ -488,12 +512,15 @@ public class NewickFile {
   public SequenceNode getTree() {\r
     return root;\r
   }\r
-  public uk.ac.vamsas.objects.core.Treenode[] matchTreeNodeNames(String[] names, Vobject[] boundObjects)\r
-  {\r
+\r
+  public uk.ac.vamsas.objects.core.Treenode[] matchTreeNodeNames(\r
+      String[] names, Vobject[] boundObjects) {\r
     // todo!\r
-    // also - need to reconstruct a names object id mapping (or BInaryNode) mapping for the parsed tree file\r
+    // also - need to reconstruct a names object id mapping (or BInaryNode)\r
+    // mapping for the parsed tree file\r
     return null;\r
   }\r
+\r
   /**\r
    * Generate a newick format tree according to internal flags for bootstraps,\r
    * distances and root distances.\r
@@ -615,7 +642,8 @@ public class NewickFile {
    */\r
   private String nodeName(String name) {\r
     if (NodeSafeName[0].matcher(name).find()) {\r
-      return QuoteChar + NodeSafeName[1].matcher(name).replaceAll("''") + QuoteChar; // quite \r
+      return QuoteChar + NodeSafeName[1].matcher(name).replaceAll("''")\r
+          + QuoteChar; // quite\r
     } else {\r
       return NodeSafeName[2].matcher(name).replaceAll("_"); // whitespace\r
     }\r
@@ -630,10 +658,12 @@ public class NewickFile {
    * @return DOCUMENT ME!\r
    */\r
   private String printNodeField(SequenceNode c) {\r
-    return //c.getNewickNodeName()\r
+    return // c.getNewickNodeName()\r
     ((c.getName() == null) ? "" : nodeName(c.getName()))\r
-      + ((HasBootstrap) ? ((c.getBootstrap() > -1) ? (" " + c.getBootstrap())\r
-            : "") : "") + ((HasDistances) ? (":" + c.dist) : "");\r
+        + ((HasBootstrap) ? ((c.getBootstrap() > -1) ? (((c.getName() == null) ? " "\r
+            : "") + c.getBootstrap())\r
+            : "")\r
+            : "") + ((HasDistances) ? (":" + c.dist) : "");\r
   }\r
 \r
   /**\r
@@ -647,9 +677,10 @@ public class NewickFile {
   private String printRootField(SequenceNode root) {\r
     return (printRootInfo) ? (((root.getName() == null) ? "" : nodeName(root\r
         .getName()))\r
-        + ((HasBootstrap) ? ((root.getBootstrap() > -1) ? (" " + root\r
-            .getBootstrap()) : "") : "") + ((RootHasDistance) ? (":" + root.dist)\r
-        : ""))\r
+        + ((HasBootstrap) ? ((root.getBootstrap() > -1) ? ((root.getName() != null ? " "\r
+            : "") + root.getBootstrap())\r
+            : "")\r
+            : "") + ((RootHasDistance) ? (":" + root.dist) : ""))\r
         : "";\r
   }\r
 \r
@@ -729,7 +760,9 @@ public class NewickFile {
       // trf.parse();\r
       System.out.println("Original file :\n");\r
 \r
-      System.out.println(Pattern.compile("\n+").matcher(newickfile.toString()).replaceAll("") + "\n");\r
+      System.out.println(Pattern.compile("\n+").matcher(newickfile.toString())\r
+          .replaceAll("")\r
+          + "\n");\r
 \r
       System.out.println("Parsed file.\n");\r
       System.out.println("Default output type for original input.\n");\r
@@ -744,69 +777,87 @@ public class NewickFile {
       System.out.println(trf.print(false, false));\r
       System.out.println("With bootstraps and with distances.\n");\r
       System.out.println(trf.print(true, true));\r
+      System.out.println("leaves.\n");\r
+      Vector lvs = new Vector();\r
+      trf.findLeaves(trf.root, lvs);\r
+      Enumeration lv = lvs.elements();\r
+      while (lv.hasMoreElements()) {\r
+        BinaryNode leave = (BinaryNode) lv.nextElement();\r
+        if (leave.getName() != null) {\r
+          System.out.println("Node:'" + leave.getName() + "'");\r
+        }\r
+      }\r
     } catch (java.io.IOException e) {\r
       System.err.println("Exception\n" + e);\r
       e.printStackTrace();\r
     }\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
+   * \r
+   * @param node\r
+   *          root node to search from\r
+   * @param leaves\r
+   *          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
+  public Vector findLeaves(SequenceNode node, Vector leaves) {\r
+    if (node == null) {\r
       return leaves;\r
     }\r
 \r
-    if ( (node.left() == null) && (node.right() == null)) // Interior node detection\r
+    if ((node.left() == null) && (node.right() == null)) // Interior node\r
+                                                         // 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
+    } else {\r
+      /*\r
+       * TODO: Identify internal nodes... if (node.isSequenceLabel()) {\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
+   * make tree node vector from a newick tree structure with associated vamsas\r
+   * objects\r
+   * \r
    * @param ntree\r
-   * @return\r
+   * @return Treenode definitions for nodes with associated objects\r
+   */\r
+  public Treenode[] makeTreeNodes() {\r
+    return makeTreeNodes(true);\r
+  }\r
+\r
+  /**\r
+   * make treenode vector for a parsed tree with/out leaf node associations\r
+   * \r
+   * @param ignoreplaceholders\r
+   *          if true means only associated nodes are returned\r
+   * @return treenode vector for associated or all leaves\r
    */\r
-  public Treenode[] makeTreeNodes() { \r
+  public Treenode[] makeTreeNodes(boolean ignoreplaceholders) {\r
     Vector leaves = new Vector();\r
     findLeaves(root, leaves);\r
     Vector tnv = new Vector();\r
     Enumeration l = leaves.elements();\r
     Hashtable nodespecs = new Hashtable();\r
-    while (l.hasMoreElements())\r
-    {\r
+    while (l.hasMoreElements()) {\r
       BinaryNode tnode = (BinaryNode) l.nextElement();\r
-      if (tnode instanceof SequenceNode)\r
-      {\r
-        if (!((SequenceNode) tnode).isPlaceholder())\r
-        {\r
+      if (tnode instanceof SequenceNode) {\r
+        if (!(ignoreplaceholders && ((SequenceNode) tnode).isPlaceholder())) {\r
           Object assocseq = ((SequenceNode) tnode).element();\r
-          if (assocseq instanceof Vobject)\r
-          {\r
+          if (assocseq instanceof Vobject) {\r
             Vobject vobj = (Vobject) assocseq;\r
-            if (vobj!=null)\r
-            {\r
+            if (vobj != null) {\r
               Treenode node = new Treenode();\r
               node.setNodespec(makeNodeSpec(nodespecs, tnode));\r
               node.setName(tnode.getName());\r
@@ -814,35 +865,35 @@ public class NewickFile {
               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
+            } else {\r
+              System.err.println("WARNING: Unassociated treeNode "\r
+                  + tnode.element().toString()\r
+                  + " "\r
+                  + ((tnode.getName() != null) ? " label " + tnode.getName()\r
+                      : ""));\r
             }\r
           }\r
         }\r
       }\r
     }\r
-    if (tnv.size()>0)\r
-    {\r
+    if (tnv.size() > 0) {\r
       Treenode[] tn = new Treenode[tnv.size()];\r
-      tnv.copyInto(tn);  \r
+      tnv.copyInto(tn);\r
       return tn;\r
     }\r
     return new Treenode[] {};\r
   }\r
-  private String makeNodeSpec(Hashtable nodespecs, BinaryNode tnode)\r
-  { \r
+\r
+  private String makeNodeSpec(Hashtable nodespecs, BinaryNode tnode) {\r
     String nname = new String(tnode.getName());\r
     Integer nindx = (Integer) nodespecs.get(nname);\r
-    if (nindx==null)\r
-    {\r
+    if (nindx == null) {\r
       nindx = new Integer(1);\r
     }\r
-    nname = nindx.toString()+" "+nname;\r
+    nname = nindx.toString() + " " + nname;\r
     return nname;\r
   }\r
+\r
   /**\r
    * call to match up Treenode specs to NJTree parsed from document object.\r
    * \r
@@ -851,85 +902,78 @@ public class NewickFile {
    *          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
+  private BinaryNode findNodeSpec(String nodespec, Vector leaves) {\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
+    } catch (Exception e) {\r
+      System.err.println("Invalid nodespec '" + nodespec + "'");\r
       return null;\r
     }\r
     BinaryNode bn = null;\r
-    \r
+\r
     int nocc = 0;\r
     Enumeration en = leaves.elements();\r
-    while (en.hasMoreElements() && nocc<occurence)\r
-    {\r
+    while (en.hasMoreElements() && nocc < occurence) {\r
       bn = (BinaryNode) en.nextElement();\r
-      if (bn instanceof SequenceNode && bn.getName().equals(nspec))\r
-      {\r
-         --occurence;\r
-      } else \r
-        bn=null;\r
+      if (bn instanceof SequenceNode && bn.getName().equals(nspec)) {\r
+        --occurence;\r
+      } else\r
+        bn = null;\r
     }\r
     return bn;\r
   }\r
+\r
   /**\r
-   *\r
-   * re-decorate the newick node representation with the VorbaId of an object mapped by its corresponding TreeNode. \r
-   * Note: this only takes the first object in the first set of references.\r
+   * \r
+   * re-decorate the newick node representation with the VorbaId of an object\r
+   * mapped by its corresponding TreeNode. Note: this only takes the first\r
+   * object in the first set of references.\r
+   * \r
    * @param tn\r
-   * @return vector of mappings { treenode, SequenceNode, Vobject for VorbaId on sequence node }\r
+   * @return vector of mappings { treenode, SequenceNode, Vobject for VorbaId on\r
+   *         sequence node }\r
    */\r
-  public Vector attachTreeMap(Treenode[] tn)\r
-  {\r
-  if (root!=null || tn==null)\r
-    return null;\r
-  Vector leaves = new Vector();\r
-  Vector nodemap=new Vector();\r
-  findLeaves(root, leaves);\r
-  int sz = tn.length;\r
-  int i = 0;\r
-  \r
-  while (i < sz)\r
-  {\r
-    Treenode node = tn[i++];\r
-    BinaryNode mappednode = findNodeSpec(node.getNodespec(),leaves);\r
-    if (mappednode!=null && mappednode instanceof SequenceNode) {\r
-      SequenceNode leaf = (SequenceNode) leaves.elementAt(i++);\r
-      // check if we can make the specified association\r
-      Vobject noderef = null;\r
-      int vrf=0,refv=0;\r
-      while (noderef==null && vrf<node.getVrefCount())\r
-      {\r
-        if (refv<node.getVref(vrf).getRefsCount())\r
-        {\r
-          Object ref = node.getVref(vrf).getRefs(refv++);\r
-          if (ref instanceof Vobject)\r
-          {\r
-            noderef = (Vobject) ref;\r
+  public Vector attachTreeMap(Treenode[] tn) {\r
+    if (root != null || tn == null)\r
+      return null;\r
+    Vector leaves = new Vector();\r
+    Vector nodemap = new Vector();\r
+    findLeaves(root, leaves);\r
+    int sz = tn.length;\r
+    int i = 0;\r
+\r
+    while (i < sz) {\r
+      Treenode node = tn[i++];\r
+      BinaryNode mappednode = findNodeSpec(node.getNodespec(), leaves);\r
+      if (mappednode != null && mappednode instanceof SequenceNode) {\r
+        SequenceNode leaf = (SequenceNode) leaves.elementAt(i++);\r
+        // check if we can make the specified association\r
+        Vobject noderef = null;\r
+        int vrf = 0, refv = 0;\r
+        while (noderef == null && vrf < node.getVrefCount()) {\r
+          if (refv < node.getVref(vrf).getRefsCount()) {\r
+            Object ref = node.getVref(vrf).getRefs(refv++);\r
+            if (ref instanceof Vobject) {\r
+              noderef = (Vobject) ref;\r
+            }\r
+          } else {\r
+            refv = 0;\r
+            vrf++;\r
           }\r
+        }\r
+        if (noderef != null) {\r
+          nodemap.addElement(new Object[] { node, leaf, noderef });\r
+          leaf.setElement(noderef);\r
+          leaf.setPlaceholder(false);\r
         } else {\r
-          refv=0;\r
-          vrf++;\r
+          leaf.setPlaceholder(true);\r
         }\r
       }\r
-      if (noderef!=null)\r
-      {\r
-        nodemap.addElement(new Object[] { node, leaf, noderef });\r
-        leaf.setElement(noderef);\r
-        leaf.setPlaceholder(false);\r
-      } else {\r
-        leaf.setPlaceholder(true);\r
-      }\r
     }\r
+    return nodemap;\r
   }\r
-  return nodemap;\r
-}\r
 \r
 }\r