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 Phylogeny phy,
486 final int phyloxml_level,
487 final File out_file ) throws IOException {
488 final Writer writer = new BufferedWriter( new PrintWriter( out_file ) );
489 toPhyloXML( writer, phy, phyloxml_level );
494 public void toPhyloXML( final Writer writer,
495 final List<Phylogeny> trees,
496 final int phyloxml_level,
497 final String separator ) throws IOException {
498 writePhyloXmlStart( writer );
499 final Iterator<Phylogeny> it = trees.iterator();
500 while ( it.hasNext() ) {
501 toPhyloXMLNoPhyloXmlSource( writer, it.next(), phyloxml_level );
502 writer.write( separator );
504 writePhyloXmlEnd( writer );
507 public void toPhyloXML( final Writer writer, final Phylogeny tree, final int phyloxml_level ) throws IOException {
508 setPhyloXmlLevel( phyloxml_level );
509 setOutputFormt( FORMAT.PHYLO_XML );
510 writePhyloXmlStart( writer );
511 writeOutput( writer, tree );
512 writePhyloXmlEnd( writer );
515 public void toPhyloXML( final Writer writer,
516 final Phylogeny[] trees,
517 final int phyloxml_level,
518 final String separator ) throws IOException {
519 writePhyloXmlStart( writer );
520 for( final Phylogeny phylogeny : trees ) {
521 toPhyloXMLNoPhyloXmlSource( writer, phylogeny, phyloxml_level );
522 writer.write( separator );
524 writePhyloXmlEnd( writer );
527 private void toPhyloXMLNoPhyloXmlSource( final Writer writer, final Phylogeny tree, final int phyloxml_level )
529 setPhyloXmlLevel( phyloxml_level );
530 setOutputFormt( FORMAT.PHYLO_XML );
531 writeOutput( writer, tree );
534 private void writeCladeSeparator() {
536 if ( ( getOutputFormt() == FORMAT.NHX ) || ( getOutputFormt() == FORMAT.NH ) ) {
537 getBuffer().append( "," );
541 private void writeCloseClade() throws IOException {
543 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
544 getWriter().write( ForesterUtil.LINE_SEPARATOR );
545 if ( isIndentPhyloxml() ) {
546 getWriter().write( createIndentation().toString() );
548 PhylogenyDataUtil.appendClose( getWriter(), PhyloXmlMapping.CLADE );
550 else if ( ( getOutputFormt() == FORMAT.NHX ) || ( getOutputFormt() == FORMAT.NH ) ) {
551 getBuffer().append( ")" );
555 private void writeNode( final PhylogenyNode node, final StringBuffer indentation ) throws IOException {
556 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
557 if ( node.isExternal() ) {
558 getWriter().write( ForesterUtil.LINE_SEPARATOR );
559 if ( indentation != null ) {
560 getWriter().write( indentation.toString() );
562 PhylogenyDataUtil.appendOpen( getWriter(), PhyloXmlMapping.CLADE );
564 PhyloXmlNodeWriter.toPhyloXml( getWriter(),
567 indentation != null ? indentation.toString() : "" );
568 if ( node.isExternal() ) {
569 getWriter().write( ForesterUtil.LINE_SEPARATOR );
570 if ( indentation != null ) {
571 getWriter().write( indentation.toString() );
573 PhylogenyDataUtil.appendClose( getWriter(), PhyloXmlMapping.CLADE );
576 else if ( getOutputFormt() == FORMAT.NHX ) {
577 getBuffer().append( node.toNewHampshireX() );
579 else if ( getOutputFormt() == FORMAT.NH ) {
580 getBuffer().append( node.toNewHampshire( isSimpleNH(),
581 isWriteDistanceToParentInNH(),
582 getNhConversionSupportStyle() ) );
586 private NH_CONVERSION_SUPPORT_VALUE_STYLE getNhConversionSupportStyle() {
587 return _nh_conversion_support_style;
590 private void setNhConversionSupportStyle( final NH_CONVERSION_SUPPORT_VALUE_STYLE nh_conversion_support_style ) {
591 _nh_conversion_support_style = nh_conversion_support_style;
594 private void writeOpenClade( final PhylogenyNode node ) throws IOException {
595 if ( !isSawComma() ) {
596 if ( !node.isRoot() && node.isFirstChildNode() ) {
599 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
600 getWriter().write( ForesterUtil.LINE_SEPARATOR );
601 if ( isIndentPhyloxml() ) {
602 getWriter().write( createIndentation().toString() );
604 if ( node.isCollapse() ) {
605 PhylogenyDataUtil.appendOpen( getWriter(),
606 PhyloXmlMapping.CLADE,
607 PhyloXmlMapping.NODE_COLLAPSE,
611 PhylogenyDataUtil.appendOpen( getWriter(), PhyloXmlMapping.CLADE );
614 else if ( ( getOutputFormt() == FORMAT.NHX ) || ( getOutputFormt() == FORMAT.NH ) ) {
615 getBuffer().append( "(" );
618 setSawComma( false );
621 private void writeOutput( final Writer writer, final Phylogeny tree ) throws IOException {
622 if ( getOutputFormt() != FORMAT.PHYLO_XML ) {
623 throw new RuntimeException( "method inappropriately called" );
625 if ( tree != null ) {
626 reset( writer, tree );
627 boolean rerootable = true;
630 String rooted = "false";
631 if ( tree.isRooted() ) {
634 if ( !tree.isRerootable() ) {
637 if ( !ForesterUtil.isEmpty( tree.getDistanceUnit() ) ) {
638 unit = tree.getDistanceUnit();
640 if ( !ForesterUtil.isEmpty( tree.getType() ) ) {
641 type = tree.getType();
644 PhylogenyDataUtil.appendOpen( writer,
645 PhyloXmlMapping.PHYLOGENY,
646 PhyloXmlMapping.PHYLOGENY_IS_ROOTED_ATTR,
648 PhyloXmlMapping.PHYLOGENY_BRANCHLENGTH_UNIT_ATTR,
650 PhyloXmlMapping.PHYLOGENY_TYPE_ATTR,
654 PhylogenyDataUtil.appendOpen( writer,
655 PhyloXmlMapping.PHYLOGENY,
656 PhyloXmlMapping.PHYLOGENY_IS_ROOTED_ATTR,
658 PhyloXmlMapping.PHYLOGENY_BRANCHLENGTH_UNIT_ATTR,
660 PhyloXmlMapping.PHYLOGENY_TYPE_ATTR,
662 PhyloXmlMapping.PHYLOGENY_IS_REROOTABLE_ATTR,
665 appendPhylogenyLevelPhyloXml( writer, tree );
666 while ( isHasNext() ) {
669 writer.write( ForesterUtil.LINE_SEPARATOR );
670 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.PHYLOGENY );
674 private void writeToFile( final StringBuffer sb, final File out_file ) throws IOException {
675 if ( out_file.exists() ) {
676 throw new IOException( "attempt to overwrite existing file \"" + out_file.getAbsolutePath() + "\"" );
678 final PrintWriter out = new PrintWriter( new FileWriter( out_file ), true );
679 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
680 out.print( PHYLO_XML_VERSION_ENCODING_LINE );
681 out.print( ForesterUtil.LINE_SEPARATOR );
682 out.print( PHYLO_XML_NAMESPACE_LINE );
683 out.print( ForesterUtil.LINE_SEPARATOR );
686 if ( getOutputFormt() == FORMAT.PHYLO_XML ) {
687 out.print( ForesterUtil.LINE_SEPARATOR );
688 out.print( PHYLO_XML_END );
694 public static PhylogenyWriter createPhylogenyWriter() {
695 return new PhylogenyWriter();
698 private static void writeNexusStart( final Writer writer ) throws IOException {
699 writer.write( NexusConstants.NEXUS );
700 writer.write( ForesterUtil.LINE_SEPARATOR );
703 public static void writeNexusTaxaBlock( final Writer writer, final Phylogeny tree ) throws IOException {
704 writer.write( NexusConstants.BEGIN_TAXA );
705 writer.write( ForesterUtil.LINE_SEPARATOR );
707 writer.write( NexusConstants.DIMENSIONS );
709 writer.write( NexusConstants.NTAX );
711 writer.write( String.valueOf( tree.getNumberOfExternalNodes() ) );
713 writer.write( ForesterUtil.LINE_SEPARATOR );
715 writer.write( NexusConstants.TAXLABELS );
716 for( final PhylogenyNodeIterator it = tree.iteratorExternalForward(); it.hasNext(); ) {
717 final PhylogenyNode node = it.next();
720 if ( !ForesterUtil.isEmpty( node.getName() ) ) {
721 data = node.getName();
723 else if ( node.getNodeData().isHasTaxonomy() ) {
724 if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
725 data = node.getNodeData().getTaxonomy().getTaxonomyCode();
727 else if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
728 data = node.getNodeData().getTaxonomy().getScientificName();
730 else if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getCommonName() ) ) {
731 data = node.getNodeData().getTaxonomy().getCommonName();
733 else if ( node.getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
734 data = node.getNodeData().getTaxonomy().getTaxonomyCode();
737 else if ( node.getNodeData().isHasSequence() ) {
738 if ( !ForesterUtil.isEmpty( node.getNodeData().getSequence().getName() ) ) {
739 data = node.getNodeData().getSequence().getName();
742 if ( data.length() > 0 ) {
743 data = data.replaceAll( " ", "_" );
745 writer.write( data );
748 writer.write( ForesterUtil.LINE_SEPARATOR );
749 writer.write( NexusConstants.END );
750 writer.write( ForesterUtil.LINE_SEPARATOR );
753 public static void writeNexusTreesBlock( final Writer writer,
754 final List<Phylogeny> trees,
755 final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) throws IOException {
756 writer.write( NexusConstants.BEGIN_TREES );
757 writer.write( ForesterUtil.LINE_SEPARATOR );
759 for( final Phylogeny phylogeny : trees ) {
761 writer.write( NexusConstants.TREE );
763 if ( !ForesterUtil.isEmpty( phylogeny.getName() ) ) {
764 writer.write( "\'" );
765 writer.write( phylogeny.getName() );
766 writer.write( "\'" );
769 writer.write( "tree" );
770 writer.write( String.valueOf( i ) );
773 if ( phylogeny.isRooted() ) {
774 writer.write( "[&R]" );
777 writer.write( "[&U]" );
779 writer.write( phylogeny.toNewHampshire( false, svs ) );
780 writer.write( ForesterUtil.LINE_SEPARATOR );
783 writer.write( NexusConstants.END );
784 writer.write( ForesterUtil.LINE_SEPARATOR );
787 private static void writePhyloXmlEnd( final Writer writer ) throws IOException {
788 writer.write( ForesterUtil.LINE_SEPARATOR );
789 writer.write( PhylogenyWriter.PHYLO_XML_END );
792 private static void writePhyloXmlStart( final Writer writer ) throws IOException {
793 writer.write( PhylogenyWriter.PHYLO_XML_VERSION_ENCODING_LINE );
794 writer.write( ForesterUtil.LINE_SEPARATOR );
795 writer.write( PhylogenyWriter.PHYLO_XML_NAMESPACE_LINE );
796 writer.write( ForesterUtil.LINE_SEPARATOR );
799 public static enum FORMAT {
800 NH, NHX, PHYLO_XML, NEXUS;