X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FNewickFile.java;h=de33266c4fabc49d577428abe890ee1b388cfab7;hb=b01c4edc00dbeedd6b53777310d4232a4104a9ec;hp=d6966055f5c4bddbf732b845cac00d56a131f774;hpb=f24dacb1da56fccf05d684e2f4899facec2aecf7;p=jalview.git diff --git a/src/jalview/io/NewickFile.java b/src/jalview/io/NewickFile.java index d696605..de33266 100755 --- a/src/jalview/io/NewickFile.java +++ b/src/jalview/io/NewickFile.java @@ -1,25 +1,28 @@ -/* -* 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.*; @@ -166,6 +169,10 @@ public class NewickFile extends FileParse return HasDistances; } + public boolean HasRootDistance() + { + return RootHasDistance; + } /** * DOCUMENT ME! * @@ -199,7 +206,7 @@ public class NewickFile extends FileParse 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; @@ -323,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) || @@ -388,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 @@ -416,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); } @@ -481,7 +490,7 @@ public class NewickFile extends FileParse if (!RootHasDistance) { - root.dist = 0; + root.dist = (HasDistances) ? 0 : DefDistance; } } @@ -496,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() { @@ -512,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) { @@ -533,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) { @@ -555,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) @@ -699,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 { @@ -724,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();