From ba048bdd71d38ad45b484e7ff4c3f8b65d34c6ee Mon Sep 17 00:00:00 2001 From: jprocter Date: Fri, 18 Nov 2005 16:07:41 +0000 Subject: [PATCH] Fixed bug http://dragonfly.compbio.dundee.ac.uk/mantis/view.php?id=9326 for viewing trees without explicit distances. made sure trees output from treeviewer contain only as much information (distance, bootstraps, etc) as was read in from original tree file (or algorithm). --- src/jalview/analysis/NJTree.java | 43 +++++++++++++--- src/jalview/gui/TreePanel.java | 4 +- src/jalview/io/NewickFile.java | 101 +++++++++++++++++++++++--------------- 3 files changed, 98 insertions(+), 50 deletions(-) diff --git a/src/jalview/analysis/NJTree.java b/src/jalview/analysis/NJTree.java index 46a2ced..99592f2 100755 --- a/src/jalview/analysis/NJTree.java +++ b/src/jalview/analysis/NJTree.java @@ -18,16 +18,13 @@ */ package jalview.analysis; -import jalview.datamodel.*; - -import jalview.io.NewickFile; - -import jalview.schemes.ResidueProperties; +import java.util.*; +import jalview.datamodel.*; +import jalview.io.*; +import jalview.schemes.*; import jalview.util.*; -import java.util.*; - /** * DOCUMENT ME! @@ -60,6 +57,10 @@ public class NJTree Object leaves = null; int start; int end; + boolean hasDistances = true; // normal case for jalview trees + boolean hasBootstrap = false; // normal case for jalview trees + + private boolean hasRootDistance = true; /** * Creates a new NJTree object. @@ -81,6 +82,11 @@ public class NJTree public NJTree(SequenceI[] seqs, NewickFile treefile) { top = treefile.getTree(); + + hasDistances = treefile.HasDistances(); + hasBootstrap = treefile.HasBootstrap(); + hasRootDistance = treefile.HasRootDistance(); + maxheight = findHeight(top); SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs); @@ -375,7 +381,7 @@ public class NJTree public void findNewNJDistances(SequenceNode tmpi, SequenceNode tmpj, float dist) { - + tmpi.dist = ((dist + ri) - rj) / 2; tmpj.dist = (dist - tmpi.dist); @@ -1134,6 +1140,27 @@ public class NJTree { return top; } + /** + * + * @return true if tree has real distances + */ + public boolean isHasDistances() { + return hasDistances; + } + + /** + * + * @return true if tree has real bootstrap values + */ + public boolean isHasBootstrap() { + return hasBootstrap; + } + + public boolean isHasRootDistance() + { + return hasRootDistance; + } + } diff --git a/src/jalview/gui/TreePanel.java b/src/jalview/gui/TreePanel.java index 04be0fd..9c1e42e 100755 --- a/src/jalview/gui/TreePanel.java +++ b/src/jalview/gui/TreePanel.java @@ -236,7 +236,7 @@ public class TreePanel extends GTreePanel Desktop.addInternalFrame(cap, buffer.toString(), 500, 100); jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode()); - cap.setText(fout.print(false, true)); + cap.setText(fout.print(tree.isHasBootstrap(), tree.isHasDistances(), tree.isHasRootDistance())); } /** @@ -263,7 +263,7 @@ public class TreePanel extends GTreePanel try { jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode()); - String output = fout.print(false, true); // distances only + String output = fout.print(tree.isHasBootstrap(), tree.isHasDistances(), tree.isHasRootDistance()); java.io.PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter( choice)); out.println(output); diff --git a/src/jalview/io/NewickFile.java b/src/jalview/io/NewickFile.java index d696605..077faa0 100755 --- a/src/jalview/io/NewickFile.java +++ b/src/jalview/io/NewickFile.java @@ -1,20 +1,20 @@ -/* -* 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 @@ -166,6 +166,10 @@ public class NewickFile extends FileParse return HasDistances; } + public boolean HasRootDistance() + { + return RootHasDistance; + } /** * DOCUMENT ME! * @@ -199,7 +203,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 +327,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 +392,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 +421,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 +487,7 @@ public class NewickFile extends FileParse if (!RootHasDistance) { - root.dist = 0; + root.dist = (HasDistances) ? 0 : DefDistance; } } @@ -496,9 +502,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 +519,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 +543,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 +567,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 +711,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 +740,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(); -- 1.7.10.2