moved to: https://sites.google.com/site/cmzmasek/home/software/forester
[jalview.git] / forester / java / src / org / forester / io / writers / PhylogenyWriter.java
index 630f057..454ca74 100644 (file)
@@ -5,7 +5,7 @@
 // Copyright (C) 2008-2009 Christian M. Zmasek
 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
 // All rights reserved
-// 
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 // Lesser General Public License for more details.
-// 
+//
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.io.writers;
 
@@ -41,6 +41,7 @@ import org.forester.io.parsers.nexus.NexusConstants;
 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyNode;
+import org.forester.phylogeny.PhylogenyNode.NH_CONVERSION_SUPPORT_VALUE_STYLE;
 import org.forester.phylogeny.data.PhylogenyDataUtil;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 import org.forester.phylogeny.iterators.PostOrderStackObject;
@@ -73,6 +74,7 @@ public final class PhylogenyWriter {
     private Stack<PostOrderStackObject> _stack;
     private boolean                     _simple_nh;
     private boolean                     _nh_write_distance_to_parent;
+    NH_CONVERSION_SUPPORT_VALUE_STYLE   _nh_conversion_support_style;
     private boolean                     _indent_phyloxml;
     private int                         _node_level;
     private int                         _phyloxml_level;
@@ -80,6 +82,7 @@ public final class PhylogenyWriter {
 
     public PhylogenyWriter() {
         setIndentPhyloxml( INDENT_PHYLOXML_DEAFULT );
+        setNhConversionSupportStyle( NH_CONVERSION_SUPPORT_VALUE_STYLE.NONE );
     }
 
     private void appendPhylogenyLevelPhyloXml( final Writer writer, final Phylogeny tree ) throws IOException {
@@ -336,6 +339,17 @@ public final class PhylogenyWriter {
 
     public StringBuffer toNewHampshire( final Phylogeny tree,
                                         final boolean simple_nh,
+                                        final boolean nh_write_distance_to_parent,
+                                        final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) throws IOException {
+        setOutputFormt( FORMAT.NH );
+        setNhConversionSupportStyle( svs );
+        setSimpleNH( simple_nh );
+        setWriteDistanceToParentInNH( nh_write_distance_to_parent );
+        return getOutput( tree );
+    }
+
+    public StringBuffer toNewHampshire( final Phylogeny tree,
+                                        final boolean simple_nh,
                                         final boolean nh_write_distance_to_parent ) throws IOException {
         setOutputFormt( FORMAT.NH );
         setSimpleNH( simple_nh );
@@ -350,6 +364,14 @@ public final class PhylogenyWriter {
         writeToFile( toNewHampshire( tree, simple_nh, write_distance_to_parent ), out_file );
     }
 
+    public void toNewHampshire( final Phylogeny tree,
+                                final boolean simple_nh,
+                                final boolean write_distance_to_parent,
+                                final NH_CONVERSION_SUPPORT_VALUE_STYLE svs,
+                                final File out_file ) throws IOException {
+        writeToFile( toNewHampshire( tree, simple_nh, write_distance_to_parent, svs ), out_file );
+    }
+
     public void toNewHampshire( final Phylogeny[] trees,
                                 final boolean simple_nh,
                                 final boolean write_distance_to_parent,
@@ -393,34 +415,26 @@ public final class PhylogenyWriter {
         writeToFile( sb, out_file );
     }
 
-    public void toNexus( final File out_file, final List<Phylogeny> trees ) throws IOException {
-        final Writer writer = new BufferedWriter( new PrintWriter( out_file ) );
-        writeNexusStart( writer );
-        writeNexusTaxaBlock( writer, trees.get( 0 ) );
-        writeNexusTreesBlock( writer, trees );
-        writer.flush();
-        writer.close();
-    }
-
-    public void toNexus( final File out_file, final Phylogeny tree ) throws IOException {
+    public void toNexus( final File out_file, final Phylogeny tree, final NH_CONVERSION_SUPPORT_VALUE_STYLE svs )
+            throws IOException {
         final Writer writer = new BufferedWriter( new PrintWriter( out_file ) );
         final List<Phylogeny> trees = new ArrayList<Phylogeny>( 1 );
         trees.add( tree );
         writeNexusStart( writer );
         writeNexusTaxaBlock( writer, tree );
-        writeNexusTreesBlock( writer, trees );
+        writeNexusTreesBlock( writer, trees, svs );
         writer.flush();
         writer.close();
     }
 
-    public StringBuffer toNexus( final Phylogeny tree ) throws IOException {
+    public StringBuffer toNexus( final Phylogeny tree, final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) throws IOException {
         final StringWriter string_writer = new StringWriter();
         final Writer writer = new BufferedWriter( string_writer );
         final List<Phylogeny> trees = new ArrayList<Phylogeny>( 1 );
         trees.add( tree );
         writeNexusStart( writer );
         writeNexusTaxaBlock( writer, tree );
-        writeNexusTreesBlock( writer, trees );
+        writeNexusTreesBlock( writer, trees, svs );
         writer.flush();
         writer.close();
         return string_writer.getBuffer();
@@ -468,6 +482,13 @@ public final class PhylogenyWriter {
         writer.close();
     }
 
+    public void toPhyloXML( final Phylogeny phy, final int phyloxml_level, final File out_file ) throws IOException {
+        final Writer writer = new BufferedWriter( new PrintWriter( out_file ) );
+        toPhyloXML( writer, phy, phyloxml_level );
+        writer.flush();
+        writer.close();
+    }
+
     public void toPhyloXML( final Writer writer,
                             final List<Phylogeny> trees,
                             final int phyloxml_level,
@@ -538,12 +559,10 @@ public final class PhylogenyWriter {
                 }
                 PhylogenyDataUtil.appendOpen( getWriter(), PhyloXmlMapping.CLADE );
             }
-            if ( indentation != null ) {
-                PhyloXmlNodeWriter.toPhyloXml( getWriter(), node, getPhyloXmlLevel(), indentation.toString() );
-            }
-            else {
-                PhyloXmlNodeWriter.toPhyloXml( getWriter(), node, getPhyloXmlLevel(), "" );
-            }
+            PhyloXmlNodeWriter.toPhyloXml( getWriter(),
+                                           node,
+                                           getPhyloXmlLevel(),
+                                           indentation != null ? indentation.toString() : "" );
             if ( node.isExternal() ) {
                 getWriter().write( ForesterUtil.LINE_SEPARATOR );
                 if ( indentation != null ) {
@@ -556,10 +575,20 @@ public final class PhylogenyWriter {
             getBuffer().append( node.toNewHampshireX() );
         }
         else if ( getOutputFormt() == FORMAT.NH ) {
-            getBuffer().append( node.toNewHampshire( isSimpleNH(), isWriteDistanceToParentInNH() ) );
+            getBuffer().append( node.toNewHampshire( isSimpleNH(),
+                                                     isWriteDistanceToParentInNH(),
+                                                     getNhConversionSupportStyle() ) );
         }
     }
 
+    private NH_CONVERSION_SUPPORT_VALUE_STYLE getNhConversionSupportStyle() {
+        return _nh_conversion_support_style;
+    }
+
+    private void setNhConversionSupportStyle( final NH_CONVERSION_SUPPORT_VALUE_STYLE nh_conversion_support_style ) {
+        _nh_conversion_support_style = nh_conversion_support_style;
+    }
+
     private void writeOpenClade( final PhylogenyNode node ) throws IOException {
         if ( !isSawComma() ) {
             if ( !node.isRoot() && node.isFirstChildNode() ) {
@@ -570,7 +599,15 @@ public final class PhylogenyWriter {
                 if ( isIndentPhyloxml() ) {
                     getWriter().write( createIndentation().toString() );
                 }
-                PhylogenyDataUtil.appendOpen( getWriter(), PhyloXmlMapping.CLADE );
+                if ( node.isCollapse() ) {
+                    PhylogenyDataUtil.appendOpen( getWriter(),
+                                                  PhyloXmlMapping.CLADE,
+                                                  PhyloXmlMapping.NODE_COLLAPSE,
+                                                  "true" );
+                }
+                else {
+                    PhylogenyDataUtil.appendOpen( getWriter(), PhyloXmlMapping.CLADE );
+                }
             }
             else if ( ( getOutputFormt() == FORMAT.NHX ) || ( getOutputFormt() == FORMAT.NH ) ) {
                 getBuffer().append( "(" );
@@ -711,7 +748,9 @@ public final class PhylogenyWriter {
         writer.write( ForesterUtil.LINE_SEPARATOR );
     }
 
-    public static void writeNexusTreesBlock( final Writer writer, final List<Phylogeny> trees ) throws IOException {
+    public static void writeNexusTreesBlock( final Writer writer,
+                                             final List<Phylogeny> trees,
+                                             final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) throws IOException {
         writer.write( NexusConstants.BEGIN_TREES );
         writer.write( ForesterUtil.LINE_SEPARATOR );
         int i = 1;
@@ -735,7 +774,7 @@ public final class PhylogenyWriter {
             else {
                 writer.write( "[&U]" );
             }
-            writer.write( phylogeny.toNewHampshire( false ) );
+            writer.write( phylogeny.toNewHampshire( false, svs ) );
             writer.write( ForesterUtil.LINE_SEPARATOR );
             i++;
         }