2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.io.writers;
28 import java.io.BufferedWriter;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.io.PrintWriter;
33 import java.io.StringWriter;
34 import java.io.Writer;
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Stack;
40 import org.forester.io.parsers.nexus.NexusConstants;
41 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
42 import org.forester.phylogeny.Phylogeny;
43 import org.forester.phylogeny.PhylogenyNode;
44 import org.forester.phylogeny.PhylogenyNodeI.NH_CONVERSION_SUPPORT_VALUE_STYLE;
45 import org.forester.phylogeny.data.PhylogenyDataUtil;
46 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
47 import org.forester.phylogeny.iterators.PostOrderStackObject;
48 import org.forester.util.ForesterConstants;
49 import org.forester.util.ForesterUtil;
51 public final class PhylogenyWriter {
53 public final static boolean INDENT_PHYLOXML_DEAFULT = true;
54 public final static String PHYLO_XML_INTENDATION_BASE = " ";
55 public final static String PHYLO_XML_VERSION_ENCODING_LINE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
56 public final static String PHYLO_XML_NAMESPACE_LINE = "<phyloxml xmlns:xsi=\""
57 + ForesterConstants.XML_SCHEMA_INSTANCE
58 + "\" xsi:schemaLocation=\""
59 + ForesterConstants.PHYLO_XML_LOCATION
61 + ForesterConstants.PHYLO_XML_LOCATION
63 + ForesterConstants.PHYLO_XML_VERSION
64 + "/" + ForesterConstants.PHYLO_XML_XSD
66 + ForesterConstants.PHYLO_XML_LOCATION
68 public final static String PHYLO_XML_END = "</phyloxml>";
69 private boolean _saw_comma;
70 private StringBuffer _buffer;
71 private Writer _writer;
72 private PhylogenyNode _root;
73 private boolean _has_next;
74 private Stack<PostOrderStackObject> _stack;
75 private boolean _simple_nh;
76 private boolean _nh_write_distance_to_parent;
77 NH_CONVERSION_SUPPORT_VALUE_STYLE _nh_conversion_support_style;
78 private boolean _indent_phyloxml;
79 private int _node_level;
80 private int _phyloxml_level;
81 private FORMAT _format;
83 public PhylogenyWriter() {
84 setIndentPhyloxml( INDENT_PHYLOXML_DEAFULT );
85 setNhConversionSupportStyle( NH_CONVERSION_SUPPORT_VALUE_STYLE.NONE );
88 private void appendPhylogenyLevelPhyloXml( final Writer writer, final Phylogeny tree ) throws IOException {
89 final String indentation = new String();
90 if ( !ForesterUtil.isEmpty( tree.getName() ) ) {
91 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.PHYLOGENY_NAME, tree.getName(), indentation );
93 if ( tree.getIdentifier() != null ) {
94 if ( ForesterUtil.isEmpty( tree.getIdentifier().getProvider() ) ) {
95 PhylogenyDataUtil.appendElement( writer,
96 PhyloXmlMapping.IDENTIFIER,
97 tree.getIdentifier().getValue(),
100 PhylogenyDataUtil.appendElement( writer,
101 PhyloXmlMapping.IDENTIFIER,
102 tree.getIdentifier().getValue(),
103 PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR,
104 tree.getIdentifier().getProvider(),
107 if ( !ForesterUtil.isEmpty( tree.getDescription() ) ) {
108 PhylogenyDataUtil.appendElement( writer,
109 PhyloXmlMapping.PHYLOGENY_DESCRIPTION,
110 tree.getDescription(),
113 if ( tree.getConfidence() != null ) {
114 if ( ForesterUtil.isEmpty( tree.getConfidence().getType() ) ) {
115 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.CONFIDENCE, tree.getConfidence().getValue()
118 PhylogenyDataUtil.appendElement( writer,
119 PhyloXmlMapping.CONFIDENCE,
120 tree.getConfidence().getValue() + "",
121 PhyloXmlMapping.CONFIDENCE_TYPE_ATTR,
122 tree.getConfidence().getType(),
127 private StringBuffer createIndentation() {
128 if ( !isIndentPhyloxml() ) {
131 final StringBuffer sb = new StringBuffer( getNodeLevel() * 2 );
132 for( int i = 0; i < getNodeLevel(); ++i ) {
133 sb.append( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
138 private void decreaseNodeLevel() {
142 private StringBuffer getBuffer() {
146 private int getNodeLevel() {
150 private StringBuffer getOutput( final Phylogeny tree ) throws IOException {
151 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
152 throw new RuntimeException( "method inappropriately called" );
154 if ( tree != null ) {
156 while ( isHasNext() ) {
159 if ( getOutputFormt() == FORMAT.NH ) {
160 getBuffer().append( ';' );
165 return new StringBuffer( 0 );
169 private FORMAT getOutputFormt() {
173 private int getPhyloXmlLevel() {
174 return _phyloxml_level;
177 private PhylogenyNode getRoot() {
181 private Stack<PostOrderStackObject> getStack() {
185 private Writer getWriter() {
189 private void increaseNodeLevel() {
193 private boolean isHasNext() {
197 private boolean isIndentPhyloxml() {
198 return _indent_phyloxml;
201 private boolean isSawComma() {
205 private boolean isSimpleNH() {
209 private boolean isWriteDistanceToParentInNH() {
210 return _nh_write_distance_to_parent;
213 private void next() throws IOException {
215 final PostOrderStackObject si = getStack().pop();
216 final PhylogenyNode node = si.getNode();
217 final int phase = si.getPhase();
218 if ( phase > node.getNumberOfDescendants() ) {
219 setHasNext( node != getRoot() );
220 if ( ( getOutputFormt() != FORMAT.PHYLO_XML ) || node.isExternal() ) {
221 if ( !node.isRoot() && node.isFirstChildNode() ) {
224 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
225 writeNode( node, createIndentation() );
228 writeNode( node, null );
231 if ( !node.isRoot() ) {
232 if ( !node.isLastChildNode() ) {
233 writeCladeSeparator();
242 getStack().push( new PostOrderStackObject( node, ( phase + 1 ) ) );
243 if ( node.isInternal() ) {
244 getStack().push( new PostOrderStackObject( node.getChildNode( phase - 1 ), 1 ) );
245 writeOpenClade( node );
246 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
248 writeNode( node, createIndentation() );
256 private void reset( final Phylogeny tree ) {
257 setBuffer( new StringBuffer() );
259 setSawComma( false );
261 setRoot( tree.getRoot() );
262 setStack( new Stack<PostOrderStackObject>() );
263 getStack().push( new PostOrderStackObject( tree.getRoot(), 1 ) );
267 private void reset( final Writer writer, final Phylogeny tree ) {
270 setSawComma( false );
272 setRoot( tree.getRoot() );
273 setStack( new Stack<PostOrderStackObject>() );
274 getStack().push( new PostOrderStackObject( tree.getRoot(), 1 ) );
278 private void setBuffer( final StringBuffer buffer ) {
282 private void setHasNext( final boolean has_next ) {
283 _has_next = has_next;
286 public void setIndentPhyloxml( final boolean indent_phyloxml ) {
287 _indent_phyloxml = indent_phyloxml;
290 private void setNodeLevel( final int level ) {
294 private void setOutputFormt( final FORMAT format ) {
298 private void setPhyloXmlLevel( final int phyloxml_level ) {
299 _phyloxml_level = phyloxml_level;
302 private void setRoot( final PhylogenyNode root ) {
306 private void setSawComma( final boolean saw_comma ) {
307 _saw_comma = saw_comma;
310 private void setSimpleNH( final boolean simple_nh ) {
311 _simple_nh = simple_nh;
314 private void setStack( final Stack<PostOrderStackObject> stack ) {
318 private void setWriteDistanceToParentInNH( final boolean nh_write_distance_to_parent ) {
319 _nh_write_distance_to_parent = nh_write_distance_to_parent;
322 private void setWriter( final Writer writer ) {
326 public void toNewHampshire( final List<Phylogeny> trees,
327 final boolean simple_nh,
328 final boolean write_distance_to_parent,
330 final String separator ) throws IOException {
331 final Iterator<Phylogeny> it = trees.iterator();
332 final StringBuffer sb = new StringBuffer();
333 while ( it.hasNext() ) {
334 sb.append( toNewHampshire( it.next(), simple_nh, write_distance_to_parent ) );
335 sb.append( separator );
337 writeToFile( sb, out_file );
340 public StringBuffer toNewHampshire( final Phylogeny tree,
341 final boolean simple_nh,
342 final boolean nh_write_distance_to_parent,
343 final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) throws IOException {
344 setOutputFormt( FORMAT.NH );
345 setNhConversionSupportStyle( svs );
346 setSimpleNH( simple_nh );
347 setWriteDistanceToParentInNH( nh_write_distance_to_parent );
348 return getOutput( tree );
351 public StringBuffer toNewHampshire( final Phylogeny tree,
352 final boolean simple_nh,
353 final boolean nh_write_distance_to_parent ) throws IOException {
354 setOutputFormt( FORMAT.NH );
355 setSimpleNH( simple_nh );
356 setWriteDistanceToParentInNH( nh_write_distance_to_parent );
357 return getOutput( tree );
360 public void toNewHampshire( final Phylogeny tree,
361 final boolean simple_nh,
362 final boolean write_distance_to_parent,
363 final File out_file ) throws IOException {
364 writeToFile( toNewHampshire( tree, simple_nh, write_distance_to_parent ), out_file );
367 public void toNewHampshire( final Phylogeny tree,
368 final boolean simple_nh,
369 final boolean write_distance_to_parent,
370 final NH_CONVERSION_SUPPORT_VALUE_STYLE svs,
371 final File out_file ) throws IOException {
372 writeToFile( toNewHampshire( tree, simple_nh, write_distance_to_parent, svs ), out_file );
375 public void toNewHampshire( final Phylogeny[] trees,
376 final boolean simple_nh,
377 final boolean write_distance_to_parent,
379 final String separator ) throws IOException {
380 final StringBuffer sb = new StringBuffer();
381 for( final Phylogeny element : trees ) {
382 sb.append( toNewHampshire( element, simple_nh, write_distance_to_parent ) );
383 sb.append( separator );
385 writeToFile( sb, out_file );
388 public void toNewHampshireX( final List<Phylogeny> trees, final File out_file, final String separator )
390 final Iterator<Phylogeny> it = trees.iterator();
391 final StringBuffer sb = new StringBuffer();
392 while ( it.hasNext() ) {
393 sb.append( toNewHampshireX( it.next() ) );
394 sb.append( separator );
396 writeToFile( sb, out_file );
399 public StringBuffer toNewHampshireX( final Phylogeny tree ) throws IOException {
400 setOutputFormt( FORMAT.NHX );
401 return getOutput( tree );
404 public void toNewHampshireX( final Phylogeny tree, final File out_file ) throws IOException {
405 writeToFile( toNewHampshireX( tree ), out_file );
408 public void toNewHampshireX( final Phylogeny[] trees, final File out_file, final String separator )
410 final StringBuffer sb = new StringBuffer();
411 for( final Phylogeny element : trees ) {
412 sb.append( toNewHampshireX( element ) );
413 sb.append( separator );
415 writeToFile( sb, out_file );
418 public void toNexus( final File out_file, final Phylogeny tree, final NH_CONVERSION_SUPPORT_VALUE_STYLE svs )
420 final Writer writer = new BufferedWriter( new PrintWriter( out_file ) );
421 final List<Phylogeny> trees = new ArrayList<Phylogeny>( 1 );
423 writeNexusStart( writer );
424 writeNexusTaxaBlock( writer, tree );
425 writeNexusTreesBlock( writer, trees, svs );
430 public StringBuffer toNexus( final Phylogeny tree, final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) throws IOException {
431 final StringWriter string_writer = new StringWriter();
432 final Writer writer = new BufferedWriter( string_writer );
433 final List<Phylogeny> trees = new ArrayList<Phylogeny>( 1 );
435 writeNexusStart( writer );
436 writeNexusTaxaBlock( writer, tree );
437 writeNexusTreesBlock( writer, trees, svs );
440 return string_writer.getBuffer();
443 public void toPhyloXML( final File out_file,
444 final List<Phylogeny> trees,
445 final int phyloxml_level,
446 final String separator ) throws IOException {
447 final Writer writer = new BufferedWriter( new PrintWriter( out_file ) );
448 toPhyloXML( writer, trees, phyloxml_level, separator );
453 public void toPhyloXML( final File out_file, final Phylogeny tree, final int phyloxml_level ) throws IOException {
454 final Writer writer = new BufferedWriter( new PrintWriter( out_file ) );
455 writePhyloXmlStart( writer );
456 toPhyloXMLNoPhyloXmlSource( writer, tree, phyloxml_level );
457 writePhyloXmlEnd( writer );
462 public StringBuffer toPhyloXML( final Phylogeny tree, final int phyloxml_level ) throws IOException {
463 final StringWriter string_writer = new StringWriter();
464 final Writer writer = new BufferedWriter( string_writer );
465 setPhyloXmlLevel( phyloxml_level );
466 setOutputFormt( FORMAT.PHYLO_XML );
467 writePhyloXmlStart( writer );
468 writeOutput( writer, tree );
469 writePhyloXmlEnd( writer );
472 return string_writer.getBuffer();
475 public void toPhyloXML( final Phylogeny[] trees,
476 final int phyloxml_level,
478 final String separator ) throws IOException {
479 final Writer writer = new BufferedWriter( new PrintWriter( out_file ) );
480 toPhyloXML( writer, trees, phyloxml_level, separator );
485 public void toPhyloXML( final Writer writer,
486 final List<Phylogeny> trees,
487 final int phyloxml_level,
488 final String separator ) throws IOException {
489 writePhyloXmlStart( writer );
490 final Iterator<Phylogeny> it = trees.iterator();
491 while ( it.hasNext() ) {
492 toPhyloXMLNoPhyloXmlSource( writer, it.next(), phyloxml_level );
493 writer.write( separator );
495 writePhyloXmlEnd( writer );
498 public void toPhyloXML( final Writer writer, final Phylogeny tree, final int phyloxml_level ) throws IOException {
499 setPhyloXmlLevel( phyloxml_level );
500 setOutputFormt( FORMAT.PHYLO_XML );
501 writePhyloXmlStart( writer );
502 writeOutput( writer, tree );
503 writePhyloXmlEnd( writer );
506 public void toPhyloXML( final Writer writer,
507 final Phylogeny[] trees,
508 final int phyloxml_level,
509 final String separator ) throws IOException {
510 writePhyloXmlStart( writer );
511 for( final Phylogeny phylogeny : trees ) {
512 toPhyloXMLNoPhyloXmlSource( writer, phylogeny, phyloxml_level );
513 writer.write( separator );
515 writePhyloXmlEnd( writer );
518 private void toPhyloXMLNoPhyloXmlSource( final Writer writer, final Phylogeny tree, final int phyloxml_level )
520 setPhyloXmlLevel( phyloxml_level );
521 setOutputFormt( FORMAT.PHYLO_XML );
522 writeOutput( writer, tree );
525 private void writeCladeSeparator() {
527 if ( ( getOutputFormt() == FORMAT.NHX ) || ( getOutputFormt() == FORMAT.NH ) ) {
528 getBuffer().append( "," );
532 private void writeCloseClade() throws IOException {
534 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
535 getWriter().write( ForesterUtil.LINE_SEPARATOR );
536 if ( isIndentPhyloxml() ) {
537 getWriter().write( createIndentation().toString() );
539 PhylogenyDataUtil.appendClose( getWriter(), PhyloXmlMapping.CLADE );
541 else if ( ( getOutputFormt() == FORMAT.NHX ) || ( getOutputFormt() == FORMAT.NH ) ) {
542 getBuffer().append( ")" );
546 private void writeNode( final PhylogenyNode node, final StringBuffer indentation ) throws IOException {
547 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
548 if ( node.isExternal() ) {
549 getWriter().write( ForesterUtil.LINE_SEPARATOR );
550 if ( indentation != null ) {
551 getWriter().write( indentation.toString() );
553 PhylogenyDataUtil.appendOpen( getWriter(), PhyloXmlMapping.CLADE );
555 PhyloXmlNodeWriter.toPhyloXml( getWriter(),
558 indentation != null ? indentation.toString() : "" );
559 if ( node.isExternal() ) {
560 getWriter().write( ForesterUtil.LINE_SEPARATOR );
561 if ( indentation != null ) {
562 getWriter().write( indentation.toString() );
564 PhylogenyDataUtil.appendClose( getWriter(), PhyloXmlMapping.CLADE );
567 else if ( getOutputFormt() == FORMAT.NHX ) {
568 getBuffer().append( node.toNewHampshireX() );
570 else if ( getOutputFormt() == FORMAT.NH ) {
571 getBuffer().append( node.toNewHampshire( isSimpleNH(),
572 isWriteDistanceToParentInNH(),
573 getNhConversionSupportStyle() ) );
577 private NH_CONVERSION_SUPPORT_VALUE_STYLE getNhConversionSupportStyle() {
578 return _nh_conversion_support_style;
581 private void setNhConversionSupportStyle( final NH_CONVERSION_SUPPORT_VALUE_STYLE nh_conversion_support_style ) {
582 _nh_conversion_support_style = nh_conversion_support_style;
585 private void writeOpenClade( final PhylogenyNode node ) throws IOException {
586 if ( !isSawComma() ) {
587 if ( !node.isRoot() && node.isFirstChildNode() ) {
590 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
591 getWriter().write( ForesterUtil.LINE_SEPARATOR );
592 if ( isIndentPhyloxml() ) {
593 getWriter().write( createIndentation().toString() );
595 if ( node.isCollapse() ) {
596 PhylogenyDataUtil.appendOpen( getWriter(),
597 PhyloXmlMapping.CLADE,
598 PhyloXmlMapping.NODE_COLLAPSE,
602 PhylogenyDataUtil.appendOpen( getWriter(), PhyloXmlMapping.CLADE );
605 else if ( ( getOutputFormt() == FORMAT.NHX ) || ( getOutputFormt() == FORMAT.NH ) ) {
606 getBuffer().append( "(" );
609 setSawComma( false );
612 private void writeOutput( final Writer writer, final Phylogeny tree ) throws IOException {
613 if ( getOutputFormt() != FORMAT.PHYLO_XML ) {
614 throw new RuntimeException( "method inappropriately called" );
616 if ( tree != null ) {
617 reset( writer, tree );
618 boolean rerootable = true;
621 String rooted = "false";
622 if ( tree.isRooted() ) {
625 if ( !tree.isRerootable() ) {
628 if ( !ForesterUtil.isEmpty( tree.getDistanceUnit() ) ) {
629 unit = tree.getDistanceUnit();
631 if ( !ForesterUtil.isEmpty( tree.getType() ) ) {
632 type = tree.getType();
635 PhylogenyDataUtil.appendOpen( writer,
636 PhyloXmlMapping.PHYLOGENY,
637 PhyloXmlMapping.PHYLOGENY_IS_ROOTED_ATTR,
639 PhyloXmlMapping.PHYLOGENY_BRANCHLENGTH_UNIT_ATTR,
641 PhyloXmlMapping.PHYLOGENY_TYPE_ATTR,
645 PhylogenyDataUtil.appendOpen( writer,
646 PhyloXmlMapping.PHYLOGENY,
647 PhyloXmlMapping.PHYLOGENY_IS_ROOTED_ATTR,
649 PhyloXmlMapping.PHYLOGENY_BRANCHLENGTH_UNIT_ATTR,
651 PhyloXmlMapping.PHYLOGENY_TYPE_ATTR,
653 PhyloXmlMapping.PHYLOGENY_IS_REROOTABLE_ATTR,
656 appendPhylogenyLevelPhyloXml( writer, tree );
657 while ( isHasNext() ) {
660 writer.write( ForesterUtil.LINE_SEPARATOR );
661 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.PHYLOGENY );
665 private void writeToFile( final StringBuffer sb, final File out_file ) throws IOException {
666 if ( out_file.exists() ) {
667 throw new IOException( "attempt to overwrite existing file \"" + out_file.getAbsolutePath() + "\"" );
669 final PrintWriter out = new PrintWriter( new FileWriter( out_file ), true );
670 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
671 out.print( PHYLO_XML_VERSION_ENCODING_LINE );
672 out.print( ForesterUtil.LINE_SEPARATOR );
673 out.print( PHYLO_XML_NAMESPACE_LINE );
674 out.print( ForesterUtil.LINE_SEPARATOR );
677 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
678 out.print( ForesterUtil.LINE_SEPARATOR );
679 out.print( PHYLO_XML_END );
685 public static PhylogenyWriter createPhylogenyWriter() {
686 return new PhylogenyWriter();
689 private static void writeNexusStart( final Writer writer ) throws IOException {
690 writer.write( NexusConstants.NEXUS );
691 writer.write( ForesterUtil.LINE_SEPARATOR );
694 public static void writeNexusTaxaBlock( final Writer writer, final Phylogeny tree ) throws IOException {
695 writer.write( NexusConstants.BEGIN_TAXA );
696 writer.write( ForesterUtil.LINE_SEPARATOR );
698 writer.write( NexusConstants.DIMENSIONS );
700 writer.write( NexusConstants.NTAX );
702 writer.write( String.valueOf( tree.getNumberOfExternalNodes() ) );
704 writer.write( ForesterUtil.LINE_SEPARATOR );
706 writer.write( NexusConstants.TAXLABELS );
707 for( final PhylogenyNodeIterator it = tree.iteratorExternalForward(); it.hasNext(); ) {
708 final PhylogenyNode node = it.next();
711 if ( !ForesterUtil.isEmpty( node.getName() ) ) {
712 data = node.getName();
714 else if ( node.getNodeData().isHasTaxonomy() ) {
715 if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
716 data = node.getNodeData().getTaxonomy().getTaxonomyCode();
718 else if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
719 data = node.getNodeData().getTaxonomy().getScientificName();
721 else if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getCommonName() ) ) {
722 data = node.getNodeData().getTaxonomy().getCommonName();
724 else if ( node.getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
725 data = node.getNodeData().getTaxonomy().getTaxonomyCode();
728 else if ( node.getNodeData().isHasSequence() ) {
729 if ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getName() ) ) {
730 data = node.getNodeData().getSequence().getName();
733 if ( data.length() > 0 ) {
734 data = data.replaceAll( " ", "_" );
736 writer.write( data );
739 writer.write( ForesterUtil.LINE_SEPARATOR );
740 writer.write( NexusConstants.END );
741 writer.write( ForesterUtil.LINE_SEPARATOR );
744 public static void writeNexusTreesBlock( final Writer writer,
745 final List<Phylogeny> trees,
746 final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) throws IOException {
747 writer.write( NexusConstants.BEGIN_TREES );
748 writer.write( ForesterUtil.LINE_SEPARATOR );
750 for( final Phylogeny phylogeny : trees ) {
752 writer.write( NexusConstants.TREE );
754 if ( !ForesterUtil.isEmpty( phylogeny.getName() ) ) {
755 writer.write( "\'" );
756 writer.write( phylogeny.getName() );
757 writer.write( "\'" );
760 writer.write( "tree" );
761 writer.write( String.valueOf( i ) );
764 if ( phylogeny.isRooted() ) {
765 writer.write( "[&R]" );
768 writer.write( "[&U]" );
770 writer.write( phylogeny.toNewHampshire( false, svs ) );
771 writer.write( ForesterUtil.LINE_SEPARATOR );
774 writer.write( NexusConstants.END );
775 writer.write( ForesterUtil.LINE_SEPARATOR );
778 private static void writePhyloXmlEnd( final Writer writer ) throws IOException {
779 writer.write( ForesterUtil.LINE_SEPARATOR );
780 writer.write( PhylogenyWriter.PHYLO_XML_END );
783 private static void writePhyloXmlStart( final Writer writer ) throws IOException {
784 writer.write( PhylogenyWriter.PHYLO_XML_VERSION_ENCODING_LINE );
785 writer.write( ForesterUtil.LINE_SEPARATOR );
786 writer.write( PhylogenyWriter.PHYLO_XML_NAMESPACE_LINE );
787 writer.write( ForesterUtil.LINE_SEPARATOR );
790 public static enum FORMAT {
791 NH, NHX, PHYLO_XML, NEXUS;