2.08, not 2.07
[jalview.git] / src / jalview / io / NewickFile.java
index c1ef5b6..de33266 100755 (executable)
@@ -1,33 +1,34 @@
-/*
-* Jalview - A Sequence Alignment Editor and Viewer
-* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
-*
-* This program is free software; you can redistribute it and/or
-* modify it under the terms of the GNU General Public License
-* as published by the Free Software Foundation; either version 2
-* of the License, or (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program; if not, write to the Free Software
-* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+/*\r
+* Jalview - A Sequence Alignment Editor and Viewer\r
+* Copyright (C) 2005 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
+* 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
 */\r
 \r
 // NewickFile.java\r
 // Tree I/O\r
 // http://evolution.genetics.washington.edu/phylip/newick_doc.html\r
+// 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 jalview.io;\r
 \r
 import jalview.datamodel.*;\r
 \r
 import java.io.*;\r
 \r
-import java.util.*;\r
-\r
 \r
 /**\r
  * DOCUMENT ME!\r
@@ -168,6 +169,10 @@ public class NewickFile extends FileParse
         return HasDistances;\r
     }\r
 \r
+    public boolean HasRootDistance()\r
+    {\r
+        return RootHasDistance;\r
+    }\r
     /**\r
      * DOCUMENT ME!\r
      *\r
@@ -196,12 +201,12 @@ public class NewickFile extends FileParse
 \r
         int d = -1;\r
         int cp = 0;\r
-        int flen = nf.length();\r
+        //int flen = nf.length();\r
 \r
         String Error = null;\r
         String nodename = null;\r
 \r
-        float DefDistance = (float) 0.00001; // @param Default distance for a node - very very small\r
+        float DefDistance = (float) 0.001; // @param Default distance for a node - very very small\r
         int DefBootstrap = 0; // @param Default bootstrap for a node\r
 \r
         float distance = DefDistance;\r
@@ -325,7 +330,7 @@ public class NewickFile extends FileParse
                 com.stevesoft.pat.Regex nbootstrap = new com.stevesoft.pat.Regex(\r
                         "\\S+([0-9+]+)\\S*:");\r
                 com.stevesoft.pat.Regex ndist = new com.stevesoft.pat.Regex(\r
-                        ":([-0-9.+]+)");\r
+                        ":([-0-9Ee.+]+)");\r
 \r
                 if (uqnodename.search(fstring) &&\r
                         ((uqnodename.matchedFrom(1) == 0) ||\r
@@ -390,12 +395,13 @@ public class NewickFile extends FileParse
                 {\r
                     // Write node info here\r
                     c.setName(nodename);\r
-                    c.dist = (HasDistances) ? distance : 0;\r
-                    c.setBootstrap((HasBootstrap) ? bootstrap : 0);\r
-\r
+                    // Trees without distances still need a render distance\r
+                    c.dist = (HasDistances) ? distance : DefDistance;\r
+                    // be consistent for internal bootstrap defaults too\r
+                    c.setBootstrap((HasBootstrap) ? bootstrap : DefBootstrap);\r
                     if (c == realroot)\r
                     {\r
-                        RootHasDistance = nodehasdistance; // JBPNote This is really UGLY!!!\r
+                        RootHasDistance = nodehasdistance; // JBPNote This is really UGLY!!! Ensure root node gets its given distance\r
                     }\r
                 }\r
                 else\r
@@ -418,8 +424,9 @@ public class NewickFile extends FileParse
                         else\r
                         {\r
                             // Insert a dummy node for polytomy\r
+                            // dummy nodes have distances\r
                             SequenceNode newdummy = new SequenceNode(null, c,\r
-                                    null, 0, 0, true);\r
+                                    null, (HasDistances ? 0 : DefDistance), 0, true);\r
                             newdummy.SetChildren(c.left(), newnode);\r
                             c.setLeft(newdummy);\r
                         }\r
@@ -483,7 +490,7 @@ public class NewickFile extends FileParse
 \r
         if (!RootHasDistance)\r
         {\r
-            root.dist = 0;\r
+            root.dist = (HasDistances) ? 0 : DefDistance;\r
         }\r
     }\r
 \r
@@ -498,9 +505,10 @@ public class NewickFile extends FileParse
     }\r
 \r
     /**\r
-     * DOCUMENT ME!\r
+     * Generate a newick format tree according to internal flags\r
+     * for bootstraps, distances and root distances.\r
      *\r
-     * @return DOCUMENT ME!\r
+     * @return new hampshire tree in a single line\r
      */\r
     public String print()\r
     {\r
@@ -514,11 +522,14 @@ public class NewickFile extends FileParse
     }\r
 \r
     /**\r
-     * DOCUMENT ME!\r
      *\r
-     * @param withbootstraps DOCUMENT ME!\r
      *\r
-     * @return DOCUMENT ME!\r
+     * Generate a newick format tree according to internal flags\r
+     * for distances and root distances and user specificied writing of\r
+     * bootstraps.\r
+     * @param withbootstraps controls if bootstrap values are explicitly written.\r
+     *\r
+     * @return new hampshire tree in a single line\r
      */\r
     public String print(boolean withbootstraps)\r
     {\r
@@ -535,12 +546,14 @@ public class NewickFile extends FileParse
     }\r
 \r
     /**\r
-     * DOCUMENT ME!\r
      *\r
-     * @param withbootstraps DOCUMENT ME!\r
-     * @param withdists DOCUMENT ME!\r
+     * Generate newick format tree according to internal flags\r
+     * for writing root node distances.\r
      *\r
-     * @return DOCUMENT ME!\r
+     * @param withbootstraps explicitly write bootstrap values\r
+     * @param withdists explicitly write distances\r
+     *\r
+     * @return new hampshire tree in a single line\r
      */\r
     public String print(boolean withbootstraps, boolean withdists)\r
     {\r
@@ -557,13 +570,13 @@ public class NewickFile extends FileParse
     }\r
 \r
     /**\r
-     * DOCUMENT ME!\r
+     * Generate newick format tree according to user specified flags\r
      *\r
-     * @param withbootstraps DOCUMENT ME!\r
-     * @param withdists DOCUMENT ME!\r
-     * @param printRootInfo DOCUMENT ME!\r
+     * @param withbootstraps explicitly write bootstrap values\r
+     * @param withdists explicitly write distances\r
+     * @param printRootInfo explicitly write root distance\r
      *\r
-     * @return DOCUMENT ME!\r
+     * @return new hampshire tree in a single line\r
      */\r
     public String print(boolean withbootstraps, boolean withdists,\r
         boolean printRootInfo)\r
@@ -701,8 +714,12 @@ public class NewickFile extends FileParse
             {\r
                 if (c.isDummy())\r
                 {\r
-                    _print(tf, (SequenceNode) c.right());\r
                     _print(tf, (SequenceNode) c.left());\r
+                    if (c.left() != null)\r
+                    {\r
+                      tf.append(",");\r
+                    }\r
+                    _print(tf, (SequenceNode) c.right());\r
                 }\r
                 else\r
                 {\r
@@ -726,6 +743,11 @@ public class NewickFile extends FileParse
     {\r
         try\r
         {\r
+            if (args==null || args.length!=1) {\r
+              System.err.println("Takes one argument - file name of a newick tree file.");\r
+              System.exit(0);\r
+            }\r
+\r
             File fn = new File(args[0]);\r
 \r
             StringBuffer newickfile = new StringBuffer();\r