Tree io modifications, including support for polytomous trees by the
[jalview.git] / src / jalview / io / NewickFile.java
index 997903c..d0af8a7 100755 (executable)
@@ -14,6 +14,7 @@ public class NewickFile extends FileParse
   private boolean HasBootstrap = false;
   private boolean HasDistances = false;
   private boolean RootHasDistance = false;
+
   private String ErrorStringrange(String Error, String Er, int r, int p, String s) {
     return ((Error==null) ? "" : Error)
         + Er +
@@ -43,6 +44,8 @@ public class NewickFile extends FileParse
 
     super(inFile, type);
   }
+  // File IO Flags
+  boolean ReplaceUnderscores = false;
 
   public void parse() throws IOException
   {
@@ -58,6 +61,7 @@ public class NewickFile extends FileParse
     }
 
     root = new SequenceNode();
+    SequenceNode realroot = null;
     SequenceNode c = root;
     int d = -1;
     int cp = 0;
@@ -114,6 +118,9 @@ public class NewickFile extends FileParse
             c.setLeft(new SequenceNode(null, c, 0, null));
             c = (SequenceNode) c.left();
           }
+          if (realroot==null) {
+            realroot = c;
+          }
           nodename = null;
           distance = -99999;
           bootstrap = -99999;
@@ -163,7 +170,11 @@ public class NewickFile extends FileParse
           {
             if (nodename == null)
             {
-              nodename = uqnodename.stringMatched(1).replace('_', ' ');
+              if (ReplaceUnderscores) {
+                nodename = uqnodename.stringMatched(1).replace('_', ' ');
+              } else {
+                nodename = uqnodename.stringMatched(1);
+              }
             }
             else
             {
@@ -189,16 +200,14 @@ public class NewickFile extends FileParse
                                        cp + nbootstrap.matchedFrom(), nf);
             }
           }
-
+          boolean nodehasdistance=false;
           if (ndist.search(fstring))
           {
             try
             {
               distance = (new Float(ndist.stringMatched(1))).floatValue();
               HasDistances = true;
-              if (c.parent()==root) {
-                RootHasDistance = true;
-              }
+              nodehasdistance = true;
             }
             catch (Exception e)
             {
@@ -213,6 +222,9 @@ public class NewickFile extends FileParse
             c.setName(nodename);
             c.dist = (HasDistances) ? distance : 0;
             c.setBootstrap((HasBootstrap) ? bootstrap : 0);
+            if (c==realroot) {
+              RootHasDistance = nodehasdistance; // JBPNote This is really UGLY!!!
+            }
           }
           else
           {
@@ -283,6 +295,9 @@ public class NewickFile extends FileParse
     }
 
     root = (SequenceNode) root.right().detach(); // remove the imaginary root.
+    if (!RootHasDistance) {
+      root.dist = 0;
+    }
   }
 
   public NewickFile(SequenceNode newtree) {