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 Example {
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 Example {
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 Example {
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 Example {
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() );
211 = Creating a basic gene tree (with branch lengths) =
213 This needs file "forester.jar" to be in the class-path.
219 import org.forester.archaeopteryx.Archaeopteryx;
220 import org.forester.phylogeny.Phylogeny;
221 import org.forester.phylogeny.PhylogenyNode;
222 import org.forester.phylogeny.data.Sequence;
223 import org.forester.phylogeny.data.Taxonomy;
225 public class Example {
227 public static void main( final String[] args ) {
228 // Creating a new rooted tree with two external nodes.
229 final Phylogeny phy = new Phylogeny();
230 final PhylogenyNode root = new PhylogenyNode();
231 final PhylogenyNode d1 = new PhylogenyNode();
232 final PhylogenyNode d2 = new PhylogenyNode();
233 // Setting of distances.
234 d1.setDistanceToParent( 1.2 );
235 d2.setDistanceToParent( 2.4 );
236 // Adding species information.
237 final Taxonomy t1 = new Taxonomy();
238 t1.setScientificName( "Nematostella vectensis" );
239 d1.getNodeData().addTaxonomy( t1 );
240 final Taxonomy t2 = new Taxonomy();
241 t2.setScientificName( "Monosiga brevicollis" );
242 d2.getNodeData().addTaxonomy( t2 );
243 // Adding gene names.
244 final Sequence s1 = new Sequence();
245 s1.setName( "Bcl-2" );
246 d1.getNodeData().addSequence( s1 );
247 final Sequence s2 = new Sequence();
248 s2.setName( "Bcl-2" );
249 d2.getNodeData().addSequence( s2 );
250 // Root is a speciation.
251 final Event ev = new Event();
252 ev.setSpeciations( 1 );
253 ev.setDuplications( 0 );
254 root.getNodeData().setEvent( ev );
255 // Putting the tree together.
256 root.addAsChild( d1 );
257 root.addAsChild( d2 );
259 phy.setRooted( true );
260 // Displaying the newly created tree with Archaeopteryx.
261 Archaeopteryx.createApplication( phy );