1 <wiki:toc max_depth="3" />
7 Documentation, tutorial, and examples for [http://www.phylosoft.org/forester/ forester].
9 Author: [http://www.cmzmasek.net/ Christian M Zmasek], Sanford-Burnham Medical Research Institute
12 Copyright (C) 2011 Christian M Zmasek. All rights reserved.
15 = Reading and writing of phylogenetic trees =
17 This needs file "forester.jar" to be in the class-path.
24 import java.io.IOException;
26 import org.forester.io.parsers.PhylogenyParser;
27 import org.forester.io.writers.PhylogenyWriter;
28 import org.forester.phylogeny.Phylogeny;
29 import org.forester.util.ForesterUtil;
31 public class Example {
33 public static void main( final String[] args ) {
34 // Reading-in of (a) tree(s) from a file.
35 final File treefile = new File( "/path/to/tree.xml" );
36 PhylogenyParser parser = null;
38 parser = ForesterUtil.createParserDependingOnFileType( treefile, true );
40 catch ( final IOException e ) {
43 Phylogeny[] phys = null;
45 phys = ForesterUtil.readPhylogenies( parser, treefile );
47 catch ( final IOException e ) {
50 // Writing trees to a file.
51 final File outfile = new File( "/path/to/out_tree.xml" );
53 final PhylogenyWriter writer = new PhylogenyWriter();
54 writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
56 catch ( final Exception e ) {
66 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
68 This needs file "forester.jar" to be in the class-path.
75 import java.io.IOException;
77 import org.forester.archaeopteryx.Archaeopteryx;
78 import org.forester.io.parsers.PhylogenyParser;
79 import org.forester.phylogeny.Phylogeny;
80 import org.forester.util.ForesterUtil;
82 public class Example {
84 public static void main( final String[] args ) {
85 // Reading-in of (a) tree(s) from a file.
86 final File treefile = new File( "/path/to/tree.xml" );
87 PhylogenyParser parser = null;
89 parser = ForesterUtil.createParserDependingOnFileType( treefile, true );
91 catch ( final IOException e ) {
94 Phylogeny[] phys = null;
96 phys = ForesterUtil.readPhylogenies( parser, treefile );
98 catch ( final IOException e ) {
101 // Display of the tree(s) with Archaeopteryx.
102 Archaeopteryx.createApplication( phys );
111 = Creating a new tree and displaying it with Archaeopteryx =
113 This needs file "forester.jar" to be in the class-path.
119 import org.forester.archaeopteryx.Archaeopteryx;
120 import org.forester.phylogeny.Phylogeny;
121 import org.forester.phylogeny.PhylogenyNode;
123 public class Example {
125 public static void main( final String[] args ) {
126 // Creating a new rooted tree with two external nodes.
127 final Phylogeny phy = new Phylogeny();
128 final PhylogenyNode root = new PhylogenyNode();
129 final PhylogenyNode d1 = new PhylogenyNode();
130 final PhylogenyNode d2 = new PhylogenyNode();
131 root.setName( "root" );
132 d1.setName( "descendant 1" );
133 d2.setName( "descendant 2" );
134 root.addAsChild( d1 );
135 root.addAsChild( d2 );
137 phy.setRooted( true );
138 // Displaying the newly created tree with Archaeopteryx.
139 Archaeopteryx.createApplication( phy );
150 = Using iterators to visit tree nodes in certain orders =
152 This needs file "forester.jar" to be in the class-path.
158 import org.forester.phylogeny.Phylogeny;
159 import org.forester.phylogeny.PhylogenyNode;
160 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
162 public class Example {
164 public static void main( final String[] args ) {
165 // Creating a new rooted tree with four external nodes.
166 final Phylogeny phy = new Phylogeny();
167 final PhylogenyNode root = new PhylogenyNode();
168 final PhylogenyNode d1 = new PhylogenyNode();
169 final PhylogenyNode d2 = new PhylogenyNode();
170 final PhylogenyNode d11 = new PhylogenyNode();
171 final PhylogenyNode d12 = new PhylogenyNode();
172 root.setName( "root" );
175 d11.setName( "1-1" );
176 d12.setName( "1-2" );
177 root.addAsChild( d1 );
178 root.addAsChild( d2 );
179 d2.addAsChild( d11 );
180 d2.addAsChild( d12 );
182 phy.setRooted( true );
183 // Using a variety of iterators to visit the nodes of the newly created tree.
184 System.out.println( "post-order:" );
185 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
186 System.out.println( it.next().getName() );
188 System.out.println( "pre-order:" );
189 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
190 System.out.println( it.next().getName() );
192 System.out.println( "level-order:" );
193 for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
194 System.out.println( it.next().getName() );
196 System.out.println( "external nodes only:" );
197 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
198 System.out.println( it.next().getName() );
213 = Creating a basic gene tree (with branch lengths) =
215 This needs file "forester.jar" to be in the class-path.
221 import org.forester.archaeopteryx.Archaeopteryx;
222 import org.forester.phylogeny.Phylogeny;
223 import org.forester.phylogeny.PhylogenyNode;
224 import org.forester.phylogeny.data.Sequence;
225 import org.forester.phylogeny.data.Taxonomy;
227 public class Example {
229 public static void main( final String[] args ) {
230 // Creating a new rooted tree with two external nodes.
231 final Phylogeny phy = new Phylogeny();
232 final PhylogenyNode root = new PhylogenyNode();
233 final PhylogenyNode d1 = new PhylogenyNode();
234 final PhylogenyNode d2 = new PhylogenyNode();
235 // Setting of distances.
236 d1.setDistanceToParent( 1.2 );
237 d2.setDistanceToParent( 2.4 );
238 // Adding species information.
239 final Taxonomy t1 = new Taxonomy();
240 t1.setScientificName( "Nematostella vectensis" );
241 d1.getNodeData().addTaxonomy( t1 );
242 final Taxonomy t2 = new Taxonomy();
243 t2.setScientificName( "Monosiga brevicollis" );
244 d2.getNodeData().addTaxonomy( t2 );
245 // Adding gene names.
246 final Sequence s1 = new Sequence();
247 s1.setName( "Bcl-2" );
248 d1.getNodeData().addSequence( s1 );
249 final Sequence s2 = new Sequence();
250 s2.setName( "Bcl-2" );
251 d2.getNodeData().addSequence( s2 );
252 // Root is a speciation.
253 final Event ev = new Event();
254 ev.setSpeciations( 1 );
255 ev.setDuplications( 0 );
256 root.getNodeData().setEvent( ev );
257 // Putting the tree together.
258 root.addAsChild( d1 );
259 root.addAsChild( d2 );
261 phy.setRooted( true );
262 // Displaying the newly created tree with Archaeopteryx.
263 Archaeopteryx.createApplication( phy );