applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / objects / utils / trees / NewickFile.java
index d6f5f57..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
@@ -222,9 +222,9 @@ public class NewickFile {
   }\r
 \r
   /**\r
-   * call this to convert the newick string into a binary node linked tree\r
-   * Note: this is automatically called by the constructors, so you normally\r
-   * wouldn't need to use this.\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
@@ -271,9 +271,9 @@ public class NewickFile {
 \r
     Matcher mjsyms = majorsyms.matcher(nf);\r
     char schar;\r
-    int nextcp=0;\r
+    int nextcp = 0;\r
     int ncp = cp;\r
-     while (mjsyms.find(cp) && (Error == null)) {\r
+    while (mjsyms.find(cp) && (Error == null)) {\r
       int fcp = mjsyms.start();\r
 \r
       switch (schar = nf.charAt(fcp)) {\r
@@ -339,29 +339,32 @@ public class NewickFile {
               "Wayward semicolon (depth=" + d + ")", 7, fcp, nf);\r
         }\r
 \r
-      // Skip Comment or structured/extended NH format info\r
+        // Skip Comment or structured/extended NH format info\r
         if (schar == '[') {\r
-          if ((nextcp=nf.indexOf(']', fcp)) > -1) {\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 beginning of node info\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 from substring between ncp and node separator\r
+\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
-        {\r
-          int cstart=fstring.indexOf('[');\r
-          int cend=fstring.indexOf(']');\r
-          String comment =  fstring.substring(cstart+1,cend); // TODO: put this somewhere ?\r
-          fstring = fstring.substring(0, cstart)+fstring.substring(cend+1);\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
@@ -384,14 +387,12 @@ public class NewickFile {
         Matcher nbootstrap = Pattern.compile("\\s*([+0-9]+)\\s*:.*").matcher(\r
             fstring);\r
 \r
-        if (nbootstrap.matches())\r
-        {\r
-          if (nodename!=null && nbootstrap.group(1).equals(nodename)) \r
-          {\r
-              nodename=null; // empty nodename - only bootstrap value\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) || nbootstrap.start(1)>=uqnodename.end(1))\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
@@ -401,7 +402,7 @@ public class NewickFile {
             }\r
           }\r
         }\r
-        \r
+\r
         Matcher ndist = Pattern.compile(".*:([-0-9Ee.+]+)").matcher(fstring);\r
         boolean nodehasdistance = false;\r
 \r
@@ -515,7 +516,8 @@ public class NewickFile {
   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
@@ -641,7 +643,7 @@ public class NewickFile {
   private String nodeName(String name) {\r
     if (NodeSafeName[0].matcher(name).find()) {\r
       return QuoteChar + NodeSafeName[1].matcher(name).replaceAll("''")\r
-          + QuoteChar; // quite \r
+          + QuoteChar; // quite\r
     } else {\r
       return NodeSafeName[2].matcher(name).replaceAll("_"); // whitespace\r
     }\r
@@ -656,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.getName()==null) ? " " : "") + 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
@@ -673,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.getName()!=null ? " " : "") + 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
@@ -790,10 +795,12 @@ public class NewickFile {
 \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
@@ -801,16 +808,17 @@ public class NewickFile {
       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
     } else {\r
-      /*  TODO: Identify internal nodes...    if (node.isSequenceLabel())\r
-       {\r
-       leaves.addElement(node);\r
-       }*/\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
@@ -819,7 +827,9 @@ public class NewickFile {
   }\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 Treenode definitions for nodes with associated objects\r
    */\r
@@ -828,8 +838,10 @@ public class NewickFile {
   }\r
 \r
   /**\r
-   * make treenode vector for a parsed tree with/out leaf node associations \r
-   * @param ignoreplaceholders if true means only associated nodes are returned\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(boolean ignoreplaceholders) {\r
@@ -915,11 +927,14 @@ public class NewickFile {
   }\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
     if (root != null || tn == null)\r