inprogress
[jalview.git] / forester / java / src / org / forester / io / writers / PhylogenyWriter.java
index 512921e..6aee097 100644 (file)
@@ -21,7 +21,7 @@
 // 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,7 +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.PhylogenyNodeI.NH_CONVERSION_SUPPORT_VALUE_STYLE;
+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;
@@ -72,7 +72,6 @@ public final class PhylogenyWriter {
     private PhylogenyNode               _root;
     private boolean                     _has_next;
     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;
@@ -202,10 +201,6 @@ public final class PhylogenyWriter {
         return _saw_comma;
     }
 
-    private boolean isSimpleNH() {
-        return _simple_nh;
-    }
-
     private boolean isWriteDistanceToParentInNH() {
         return _nh_write_distance_to_parent;
     }
@@ -307,10 +302,6 @@ public final class PhylogenyWriter {
         _saw_comma = saw_comma;
     }
 
-    private void setSimpleNH( final boolean simple_nh ) {
-        _simple_nh = simple_nh;
-    }
-
     private void setStack( final Stack<PostOrderStackObject> stack ) {
         _stack = stack;
     }
@@ -324,62 +315,53 @@ public final class PhylogenyWriter {
     }
 
     public void toNewHampshire( final List<Phylogeny> trees,
-                                final boolean simple_nh,
                                 final boolean write_distance_to_parent,
                                 final File out_file,
                                 final String separator ) throws IOException {
         final Iterator<Phylogeny> it = trees.iterator();
         final StringBuffer sb = new StringBuffer();
         while ( it.hasNext() ) {
-            sb.append( toNewHampshire( it.next(), simple_nh, write_distance_to_parent ) );
+            sb.append( toNewHampshire( it.next(), write_distance_to_parent ) );
             sb.append( separator );
         }
         writeToFile( sb, out_file );
     }
 
     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 {
+    public StringBuffer toNewHampshire( final Phylogeny tree, final boolean nh_write_distance_to_parent )
+            throws IOException {
         setOutputFormt( FORMAT.NH );
-        setSimpleNH( simple_nh );
         setWriteDistanceToParentInNH( nh_write_distance_to_parent );
         return getOutput( tree );
     }
 
-    public void toNewHampshire( final Phylogeny tree,
-                                final boolean simple_nh,
-                                final boolean write_distance_to_parent,
-                                final File out_file ) throws IOException {
-        writeToFile( toNewHampshire( tree, simple_nh, write_distance_to_parent ), out_file );
+    public void toNewHampshire( final Phylogeny tree, final boolean write_distance_to_parent, final File out_file )
+            throws IOException {
+        writeToFile( toNewHampshire( tree, 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 );
+        writeToFile( toNewHampshire( tree, write_distance_to_parent, svs ), out_file );
     }
 
     public void toNewHampshire( final Phylogeny[] trees,
-                                final boolean simple_nh,
                                 final boolean write_distance_to_parent,
                                 final File out_file,
                                 final String separator ) throws IOException {
         final StringBuffer sb = new StringBuffer();
         for( final Phylogeny element : trees ) {
-            sb.append( toNewHampshire( element, simple_nh, write_distance_to_parent ) );
+            sb.append( toNewHampshire( element, write_distance_to_parent ) );
             sb.append( separator );
         }
         writeToFile( sb, out_file );
@@ -575,9 +557,7 @@ public final class PhylogenyWriter {
             getBuffer().append( node.toNewHampshireX() );
         }
         else if ( getOutputFormt() == FORMAT.NH ) {
-            getBuffer().append( node.toNewHampshire( isSimpleNH(),
-                                                     isWriteDistanceToParentInNH(),
-                                                     getNhConversionSupportStyle() ) );
+            getBuffer().append( node.toNewHampshire( isWriteDistanceToParentInNH(), getNhConversionSupportStyle() ) );
         }
     }
 
@@ -622,44 +602,24 @@ public final class PhylogenyWriter {
         }
         if ( tree != null ) {
             reset( writer, tree );
-            boolean rerootable = true;
             String unit = "";
             String type = "";
-            String rooted = "false";
-            if ( tree.isRooted() ) {
-                rooted = "true";
-            }
-            if ( !tree.isRerootable() ) {
-                rerootable = false;
-            }
             if ( !ForesterUtil.isEmpty( tree.getDistanceUnit() ) ) {
                 unit = tree.getDistanceUnit();
             }
             if ( !ForesterUtil.isEmpty( tree.getType() ) ) {
                 type = tree.getType();
             }
-            if ( rerootable ) {
-                PhylogenyDataUtil.appendOpen( writer,
-                                              PhyloXmlMapping.PHYLOGENY,
-                                              PhyloXmlMapping.PHYLOGENY_IS_ROOTED_ATTR,
-                                              rooted,
-                                              PhyloXmlMapping.PHYLOGENY_BRANCHLENGTH_UNIT_ATTR,
-                                              unit,
-                                              PhyloXmlMapping.PHYLOGENY_TYPE_ATTR,
-                                              type );
-            }
-            else {
-                PhylogenyDataUtil.appendOpen( writer,
-                                              PhyloXmlMapping.PHYLOGENY,
-                                              PhyloXmlMapping.PHYLOGENY_IS_ROOTED_ATTR,
-                                              rooted,
-                                              PhyloXmlMapping.PHYLOGENY_BRANCHLENGTH_UNIT_ATTR,
-                                              unit,
-                                              PhyloXmlMapping.PHYLOGENY_TYPE_ATTR,
-                                              type,
-                                              PhyloXmlMapping.PHYLOGENY_IS_REROOTABLE_ATTR,
-                                              "false" );
-            }
+            PhylogenyDataUtil.appendOpen( writer,
+                                          PhyloXmlMapping.PHYLOGENY,
+                                          PhyloXmlMapping.PHYLOGENY_IS_ROOTED_ATTR,
+                                          tree.isRooted() + "",
+                                          PhyloXmlMapping.PHYLOGENY_BRANCHLENGTH_UNIT_ATTR,
+                                          unit,
+                                          PhyloXmlMapping.PHYLOGENY_TYPE_ATTR,
+                                          type,
+                                          PhyloXmlMapping.PHYLOGENY_IS_REROOTABLE_ATTR,
+                                          tree.isRerootable() + "" );
             appendPhylogenyLevelPhyloXml( writer, tree );
             while ( isHasNext() ) {
                 next();
@@ -728,19 +688,19 @@ public final class PhylogenyWriter {
                 else if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getCommonName() ) ) {
                     data = node.getNodeData().getTaxonomy().getCommonName();
                 }
-                else if ( node.getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
-                    data = node.getNodeData().getTaxonomy().getTaxonomyCode();
-                }
             }
             else if ( node.getNodeData().isHasSequence() ) {
                 if ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getName() ) ) {
                     data = node.getNodeData().getSequence().getName();
                 }
+                else if ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getSymbol() ) ) {
+                    data = node.getNodeData().getSequence().getSymbol();
+                }
+                else if ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getGeneName() ) ) {
+                    data = node.getNodeData().getSequence().getGeneName();
+                }
             }
-            if ( data.length() > 0 ) {
-                data = data.replaceAll( " ", "_" );
-            }
-            writer.write( data );
+            writer.write( ForesterUtil.santitizeStringForNH( data ).toString() );
         }
         writer.write( ";" );
         writer.write( ForesterUtil.LINE_SEPARATOR );
@@ -774,7 +734,7 @@ public final class PhylogenyWriter {
             else {
                 writer.write( "[&U]" );
             }
-            writer.write( phylogeny.toNewHampshire( false, svs ) );
+            writer.write( phylogeny.toNewHampshire( svs ) );
             writer.write( ForesterUtil.LINE_SEPARATOR );
             i++;
         }