X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FNewickFile.java;h=de33266c4fabc49d577428abe890ee1b388cfab7;hb=4e66fcb33a56766f244dab68bc2087db29829efd;hp=c1ef5b62112f0fe1f75562931a901ca77719e518;hpb=efc31b4a8d5cee63555586804a2b79c06bdb5a14;p=jalview.git diff --git a/src/jalview/io/NewickFile.java b/src/jalview/io/NewickFile.java index c1ef5b6..de33266 100755 --- a/src/jalview/io/NewickFile.java +++ b/src/jalview/io/NewickFile.java @@ -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 +/* +* 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 */ // NewickFile.java // Tree I/O // http://evolution.genetics.washington.edu/phylip/newick_doc.html +// TODO: Implement Basic NHX tag parsing and preservation +// TODO: http://evolution.genetics.wustl.edu/eddy/forester/NHX.html +// TODO: Extended SequenceNodeI to hold parsed NHX strings package jalview.io; import jalview.datamodel.*; import java.io.*; -import java.util.*; - /** * DOCUMENT ME! @@ -168,6 +169,10 @@ public class NewickFile extends FileParse return HasDistances; } + public boolean HasRootDistance() + { + return RootHasDistance; + } /** * DOCUMENT ME! * @@ -196,12 +201,12 @@ public class NewickFile extends FileParse int d = -1; int cp = 0; - int flen = nf.length(); + //int flen = nf.length(); String Error = null; String nodename = null; - float DefDistance = (float) 0.00001; // @param Default distance for a node - very very small + float DefDistance = (float) 0.001; // @param Default distance for a node - very very small int DefBootstrap = 0; // @param Default bootstrap for a node float distance = DefDistance; @@ -325,7 +330,7 @@ public class NewickFile extends FileParse com.stevesoft.pat.Regex nbootstrap = new com.stevesoft.pat.Regex( "\\S+([0-9+]+)\\S*:"); com.stevesoft.pat.Regex ndist = new com.stevesoft.pat.Regex( - ":([-0-9.+]+)"); + ":([-0-9Ee.+]+)"); if (uqnodename.search(fstring) && ((uqnodename.matchedFrom(1) == 0) || @@ -390,12 +395,13 @@ public class NewickFile extends FileParse { // Write node info here c.setName(nodename); - c.dist = (HasDistances) ? distance : 0; - c.setBootstrap((HasBootstrap) ? bootstrap : 0); - + // Trees without distances still need a render distance + c.dist = (HasDistances) ? distance : DefDistance; + // be consistent for internal bootstrap defaults too + c.setBootstrap((HasBootstrap) ? bootstrap : DefBootstrap); if (c == realroot) { - RootHasDistance = nodehasdistance; // JBPNote This is really UGLY!!! + RootHasDistance = nodehasdistance; // JBPNote This is really UGLY!!! Ensure root node gets its given distance } } else @@ -418,8 +424,9 @@ public class NewickFile extends FileParse else { // Insert a dummy node for polytomy + // dummy nodes have distances SequenceNode newdummy = new SequenceNode(null, c, - null, 0, 0, true); + null, (HasDistances ? 0 : DefDistance), 0, true); newdummy.SetChildren(c.left(), newnode); c.setLeft(newdummy); } @@ -483,7 +490,7 @@ public class NewickFile extends FileParse if (!RootHasDistance) { - root.dist = 0; + root.dist = (HasDistances) ? 0 : DefDistance; } } @@ -498,9 +505,10 @@ public class NewickFile extends FileParse } /** - * DOCUMENT ME! + * Generate a newick format tree according to internal flags + * for bootstraps, distances and root distances. * - * @return DOCUMENT ME! + * @return new hampshire tree in a single line */ public String print() { @@ -514,11 +522,14 @@ public class NewickFile extends FileParse } /** - * DOCUMENT ME! * - * @param withbootstraps DOCUMENT ME! * - * @return DOCUMENT ME! + * Generate a newick format tree according to internal flags + * for distances and root distances and user specificied writing of + * bootstraps. + * @param withbootstraps controls if bootstrap values are explicitly written. + * + * @return new hampshire tree in a single line */ public String print(boolean withbootstraps) { @@ -535,12 +546,14 @@ public class NewickFile extends FileParse } /** - * DOCUMENT ME! * - * @param withbootstraps DOCUMENT ME! - * @param withdists DOCUMENT ME! + * Generate newick format tree according to internal flags + * for writing root node distances. * - * @return DOCUMENT ME! + * @param withbootstraps explicitly write bootstrap values + * @param withdists explicitly write distances + * + * @return new hampshire tree in a single line */ public String print(boolean withbootstraps, boolean withdists) { @@ -557,13 +570,13 @@ public class NewickFile extends FileParse } /** - * DOCUMENT ME! + * Generate newick format tree according to user specified flags * - * @param withbootstraps DOCUMENT ME! - * @param withdists DOCUMENT ME! - * @param printRootInfo DOCUMENT ME! + * @param withbootstraps explicitly write bootstrap values + * @param withdists explicitly write distances + * @param printRootInfo explicitly write root distance * - * @return DOCUMENT ME! + * @return new hampshire tree in a single line */ public String print(boolean withbootstraps, boolean withdists, boolean printRootInfo) @@ -701,8 +714,12 @@ public class NewickFile extends FileParse { if (c.isDummy()) { - _print(tf, (SequenceNode) c.right()); _print(tf, (SequenceNode) c.left()); + if (c.left() != null) + { + tf.append(","); + } + _print(tf, (SequenceNode) c.right()); } else { @@ -726,6 +743,11 @@ public class NewickFile extends FileParse { try { + if (args==null || args.length!=1) { + System.err.println("Takes one argument - file name of a newick tree file."); + System.exit(0); + } + File fn = new File(args[0]); StringBuffer newickfile = new StringBuffer();