tidied up system.out messages and moved many to stderr.
[jalview.git] / src / jalview / io / NewickFile.java
index 997903c..85ab084 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,16 +61,26 @@ public class NewickFile extends FileParse
     }
 
     root = new SequenceNode();
+    SequenceNode realroot = null;
     SequenceNode c = root;
+
     int d = -1;
     int cp = 0;
     int flen = nf.length();
+
     String Error = null;
     String nodename = null;
-    float distance=-99999;
-    int bootstrap=-99999;
+
+    float DefDistance = (float) 0.00001; // @param Default distance for a node - very very small
+    int DefBootstrap = 0; // @param Default bootstrap for a node
+
+    float distance=DefDistance;
+    int bootstrap=DefBootstrap;
+
     boolean ascending = false; // flag indicating that we are leaving the current node
+
     com.stevesoft.pat.Regex majorsyms = new com.stevesoft.pat.Regex("[(\\['),;]");
+
     while (majorsyms.searchFrom(nf, cp) && Error==null) {
       int fcp = majorsyms.matchedFrom();
       switch (nf.charAt(fcp)) {
@@ -99,7 +112,7 @@ public class NewickFile extends FileParse
           d++;
           if (c.right() == null)
           {
-            c.setRight(new SequenceNode(null, c, 0, null));
+            c.setRight(new SequenceNode(null, c, null, DefDistance, DefBootstrap, false));
             c = (SequenceNode) c.right();
           }
           else
@@ -111,12 +124,15 @@ public class NewickFile extends FileParse
               tmpn.SetChildren(c.left(), c.right());
               c.setRight(tmpn);
             }
-            c.setLeft(new SequenceNode(null, c, 0, null));
+            c.setLeft(new SequenceNode(null, c, null, DefDistance, DefBootstrap, false));
             c = (SequenceNode) c.left();
           }
+          if (realroot==null) {
+            realroot = c;
+          }
           nodename = null;
-          distance = -99999;
-          bootstrap = -99999;
+          distance = DefDistance;
+          bootstrap = DefBootstrap;
           cp = fcp + 1;
           break;
 
@@ -163,7 +179,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 +209,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,15 +231,19 @@ 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
           {
             // Find a place to put the leaf
-            SequenceNode newnode = new SequenceNode(null, c,
-                (HasDistances) ? distance : 0,
-                nodename);
-
-            newnode.setBootstrap(HasBootstrap ? bootstrap : 0);
+            SequenceNode newnode =
+            new SequenceNode(null, c,
+                             nodename,
+                             (HasDistances) ? distance : DefDistance,
+                             (HasBootstrap) ? bootstrap : DefBootstrap,
+                             false);
 
             if (c.right() == null)
             {
@@ -271,8 +293,8 @@ public class NewickFile extends FileParse
           }
           // Reset new node properties to obvious fakes
           nodename = null;
-          distance = -99999;
-          bootstrap = -99999;
+          distance = DefDistance;
+          bootstrap = DefBootstrap;
 
           cp=fcp+1;
       }
@@ -283,6 +305,10 @@ public class NewickFile extends FileParse
     }
 
     root = (SequenceNode) root.right().detach(); // remove the imaginary root.
+    if (!RootHasDistance) {
+      root.dist = 0;
+    }
+
   }
 
   public NewickFile(SequenceNode newtree) {
@@ -482,7 +508,7 @@ public class NewickFile extends FileParse
   }
   catch (java.io.IOException e)
   {
-    System.out.println("Exception\n" + e);
+    System.err.println("Exception\n" + e);
     e.printStackTrace();
   }
 }