X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fio%2Fwriters%2FPhylogenyWriter.java;h=911ebb5d8437f54cf9ed3fc21018deb1bd5e8519;hb=665e671efec73fcb36a9aac45f119330f290fa81;hp=c443842db2bc10396454adf17eee56553f32f3c9;hpb=eee996a6476a1e3d84c07f8f690dcde3ff4b2ef5;p=jalview.git diff --git a/forester/java/src/org/forester/io/writers/PhylogenyWriter.java b/forester/java/src/org/forester/io/writers/PhylogenyWriter.java index c443842..911ebb5 100644 --- a/forester/java/src/org/forester/io/writers/PhylogenyWriter.java +++ b/forester/java/src/org/forester/io/writers/PhylogenyWriter.java @@ -21,13 +21,12 @@ // 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; import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; @@ -41,6 +40,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; @@ -53,17 +53,17 @@ public final class PhylogenyWriter { public final static String PHYLO_XML_INTENDATION_BASE = " "; public final static String PHYLO_XML_VERSION_ENCODING_LINE = ""; public final static String PHYLO_XML_NAMESPACE_LINE = ""; + + ForesterConstants.XML_SCHEMA_INSTANCE + + "\" xsi:schemaLocation=\"" + + ForesterConstants.PHYLO_XML_LOCATION + + " " + + ForesterConstants.PHYLO_XML_LOCATION + + "/" + + ForesterConstants.PHYLO_XML_VERSION + + "/" + ForesterConstants.PHYLO_XML_XSD + + "\" " + "xmlns=\"" + + ForesterConstants.PHYLO_XML_LOCATION + + "\">"; public final static String PHYLO_XML_END = ""; private boolean _saw_comma; private StringBuffer _buffer; @@ -71,8 +71,8 @@ public final class PhylogenyWriter { private PhylogenyNode _root; private boolean _has_next; private Stack _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 +80,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 { @@ -110,7 +111,7 @@ public final class PhylogenyWriter { if ( tree.getConfidence() != null ) { if ( ForesterUtil.isEmpty( tree.getConfidence().getType() ) ) { PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.CONFIDENCE, tree.getConfidence().getValue() - + "", indentation ); + + "", indentation ); } PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.CONFIDENCE, @@ -199,10 +200,6 @@ public final class PhylogenyWriter { return _saw_comma; } - private boolean isSimpleNH() { - return _simple_nh; - } - private boolean isWriteDistanceToParentInNH() { return _nh_write_distance_to_parent; } @@ -304,10 +301,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 stack ) { _stack = stack; } @@ -321,43 +314,53 @@ public final class PhylogenyWriter { } public void toNewHampshire( final List trees, - final boolean simple_nh, final boolean write_distance_to_parent, final File out_file, final String separator ) throws IOException { final Iterator 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 ) throws IOException { + final boolean nh_write_distance_to_parent, + final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) throws IOException { + setOutputFormt( FORMAT.NH ); + setNhConversionSupportStyle( svs ); + setWriteDistanceToParentInNH( nh_write_distance_to_parent ); + return getOutput( tree ); + } + + 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 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 ), 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 ); @@ -393,34 +396,26 @@ public final class PhylogenyWriter { writeToFile( sb, out_file ); } - public void toNexus( final File out_file, final List 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 { - final Writer writer = new BufferedWriter( new PrintWriter( out_file ) ); + 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, ForesterConstants.UTF_8 ) ); final List trees = new ArrayList( 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 trees = new ArrayList( 1 ); trees.add( tree ); writeNexusStart( writer ); writeNexusTaxaBlock( writer, tree ); - writeNexusTreesBlock( writer, trees ); + writeNexusTreesBlock( writer, trees, svs ); writer.flush(); writer.close(); return string_writer.getBuffer(); @@ -430,14 +425,14 @@ public final class PhylogenyWriter { final List trees, final int phyloxml_level, final String separator ) throws IOException { - final Writer writer = new BufferedWriter( new PrintWriter( out_file ) ); + final Writer writer = new BufferedWriter( new PrintWriter( out_file, ForesterConstants.UTF_8 ) ); toPhyloXML( writer, trees, phyloxml_level, separator ); writer.flush(); writer.close(); } public void toPhyloXML( final File out_file, final Phylogeny tree, final int phyloxml_level ) throws IOException { - final Writer writer = new BufferedWriter( new PrintWriter( out_file ) ); + final Writer writer = new BufferedWriter( new PrintWriter( out_file, ForesterConstants.UTF_8 ) ); writePhyloXmlStart( writer ); toPhyloXMLNoPhyloXmlSource( writer, tree, phyloxml_level ); writePhyloXmlEnd( writer ); @@ -468,6 +463,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 trees, final int phyloxml_level, @@ -538,12 +540,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 +556,18 @@ public final class PhylogenyWriter { getBuffer().append( node.toNewHampshireX() ); } else if ( getOutputFormt() == FORMAT.NH ) { - getBuffer().append( node.toNewHampshire( isSimpleNH(), isWriteDistanceToParentInNH() ) ); + getBuffer().append( node.toNewHampshire( 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 +578,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( "(" ); @@ -585,44 +601,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(); @@ -636,18 +632,8 @@ public final class PhylogenyWriter { if ( out_file.exists() ) { throw new IOException( "attempt to overwrite existing file \"" + out_file.getAbsolutePath() + "\"" ); } - final PrintWriter out = new PrintWriter( new FileWriter( out_file ), true ); - if ( getOutputFormt() == FORMAT.PHYLO_XML ) { - out.print( PHYLO_XML_VERSION_ENCODING_LINE ); - out.print( ForesterUtil.LINE_SEPARATOR ); - out.print( PHYLO_XML_NAMESPACE_LINE ); - out.print( ForesterUtil.LINE_SEPARATOR ); - } + final PrintWriter out = new PrintWriter( out_file, ForesterConstants.UTF_8 ); out.print( sb ); - if ( getOutputFormt() == FORMAT.PHYLO_XML ) { - out.print( ForesterUtil.LINE_SEPARATOR ); - out.print( PHYLO_XML_END ); - } out.flush(); out.close(); } @@ -691,19 +677,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 ); @@ -711,7 +697,9 @@ public final class PhylogenyWriter { writer.write( ForesterUtil.LINE_SEPARATOR ); } - public static void writeNexusTreesBlock( final Writer writer, final List trees ) throws IOException { + public static void writeNexusTreesBlock( final Writer writer, + final List 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 +723,7 @@ public final class PhylogenyWriter { else { writer.write( "[&U]" ); } - writer.write( phylogeny.toNewHampshire( false ) ); + writer.write( phylogeny.toNewHampshire( svs ) ); writer.write( ForesterUtil.LINE_SEPARATOR ); i++; } @@ -759,3 +747,7 @@ public final class PhylogenyWriter { NH, NHX, PHYLO_XML, NEXUS; } } + + + +