5 Documentation, tutorial, and examples for [http://www.phylosoft.org/forester/ forester].
7 Author: [http://www.cmzmasek.net/ Christian M Zmasek], Sanford-Burnham Medical Research Institute
10 Copyright (C) 2011 Christian M Zmasek. All rights reserved.
13 = Reading and writing of phylogenetic trees =
15 This needs file "forester.jar" to be in the class-path.
22 import java.io.IOException;
24 import org.forester.io.parsers.PhylogenyParser;
25 import org.forester.io.writers.PhylogenyWriter;
26 import org.forester.phylogeny.Phylogeny;
27 import org.forester.util.ForesterUtil;
29 public class Example4 {
31 public static void main( final String[] args ) {
32 // Reading-in of (a) tree(s) from a file.
33 final File treefile = new File( "/path/to/tree.xml" );
34 PhylogenyParser parser = null;
36 parser = ForesterUtil.createParserDependingOnFileType( treefile, true );
38 catch ( final IOException e ) {
41 Phylogeny[] phys = null;
43 phys = ForesterUtil.readPhylogenies( parser, treefile );
45 catch ( final IOException e ) {
48 // Writing trees to a file.
49 final File outfile = new File( "/path/to/out_tree.xml" );
51 final PhylogenyWriter writer = new PhylogenyWriter();
52 writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
54 catch ( final Exception e ) {
64 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
66 This needs file "forester.jar" to be in the class-path.
73 import java.io.IOException;
75 import org.forester.archaeopteryx.Archaeopteryx;
76 import org.forester.io.parsers.PhylogenyParser;
77 import org.forester.phylogeny.Phylogeny;
78 import org.forester.util.ForesterUtil;
80 public class Example1 {
82 public static void main( final String[] args ) {
83 // Reading-in of (a) tree(s) from a file.
84 final File treefile = new File( "/path/to/tree.xml" );
85 PhylogenyParser parser = null;
87 parser = ForesterUtil.createParserDependingOnFileType( treefile, true );
89 catch ( final IOException e ) {
92 Phylogeny[] phys = null;
94 phys = ForesterUtil.readPhylogenies( parser, treefile );
96 catch ( final IOException e ) {
99 // Display of the tree(s) with Archaeopteryx.
100 Archaeopteryx.createApplication( phys );
109 = Creating a new tree and displaying it with Archaeopteryx =
111 This needs file "forester.jar" to be in the class-path.
117 import org.forester.archaeopteryx.Archaeopteryx;
118 import org.forester.phylogeny.Phylogeny;
119 import org.forester.phylogeny.PhylogenyNode;
121 public class Example2 {
123 public static void main( final String[] args ) {
124 // Creating a new rooted tree with two external nodes.
125 final Phylogeny phy = new Phylogeny();
126 final PhylogenyNode root = new PhylogenyNode();
127 final PhylogenyNode d1 = new PhylogenyNode();
128 final PhylogenyNode d2 = new PhylogenyNode();
129 root.setName( "root" );
130 d1.setName( "descendant 1" );
131 d2.setName( "descendant 2" );
132 root.addAsChild( d1 );
133 root.addAsChild( d2 );
135 phy.setRooted( true );
136 // Displaying the newly created tree with Archaeopteryx.
137 Archaeopteryx.createApplication( phy );
148 = Using iterators to visit tree nodes in certain orders=
150 This needs file "forester.jar" to be in the class-path.
156 import org.forester.phylogeny.Phylogeny;
157 import org.forester.phylogeny.PhylogenyNode;
158 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
160 public class Example3 {
162 public static void main( final String[] args ) {
163 // Creating a new rooted tree with four external nodes.
164 final Phylogeny phy = new Phylogeny();
165 final PhylogenyNode root = new PhylogenyNode();
166 final PhylogenyNode d1 = new PhylogenyNode();
167 final PhylogenyNode d2 = new PhylogenyNode();
168 final PhylogenyNode d11 = new PhylogenyNode();
169 final PhylogenyNode d12 = new PhylogenyNode();
170 root.setName( "root" );
173 d11.setName( "1-1" );
174 d12.setName( "1-2" );
175 root.addAsChild( d1 );
176 root.addAsChild( d2 );
177 d2.addAsChild( d11 );
178 d2.addAsChild( d12 );
180 phy.setRooted( true );
181 // Using a variety of iterators to visit the nodes of the newly created tree.
182 System.out.println( "post-order:" );
183 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
184 System.out.println( it.next().getName() );
186 System.out.println( "pre-order:" );
187 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
188 System.out.println( it.next().getName() );
190 System.out.println( "level-order:" );
191 for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
192 System.out.println( it.next().getName() );
194 System.out.println( "external nodes only:" );
195 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
196 System.out.println( it.next().getName() );