1 = forester Tutorial and Examples =
2 <wiki:toc max_depth="3" />
8 Documentation, tutorial, and examples for [http://www.phylosoft.org/forester/ forester].
10 *All examples require jar-file "forester.jar" (_actually, as of now, it requires the development version "forester_dev.jar"_) to be in the class-path.*
12 Author: [http://www.cmzmasek.net/ Christian M Zmasek], Sanford-Burnham Medical Research Institute
15 Copyright (C) 2011 Christian M Zmasek. All rights reserved.
19 = Reading and writing of phylogenetic trees =
28 import java.io.IOException;
30 import org.forester.io.parsers.PhylogenyParser;
31 import org.forester.io.parsers.util.ParserUtils;
32 import org.forester.io.writers.PhylogenyWriter;
33 import org.forester.phylogeny.Phylogeny;
34 import org.forester.phylogeny.PhylogenyMethods;
35 import org.forester.util.ForesterUtil;
37 public class Example {
39 public static void main( final String[] args ) {
40 // Reading-in of (a) tree(s) from a file.
41 final File treefile = new File( "/path/to/tree.xml" );
42 PhylogenyParser parser = null;
44 parser = ParserUtils.createParserDependingOnFileType( treefile, true );
46 catch ( final IOException e ) {
49 Phylogeny[] phys = null;
51 phys = PhylogenyMethods.readPhylogenies( parser, treefile );
53 catch ( final IOException e ) {
56 // Writing trees to a file.
57 final File outfile = new File( "/path/to/out_tree.xml" );
59 final PhylogenyWriter writer = new PhylogenyWriter();
60 writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
62 catch ( final Exception e ) {
72 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
74 This needs file "forester.jar" to be in the class-path.
81 import java.io.IOException;
83 import org.forester.archaeopteryx.Archaeopteryx;
84 import org.forester.io.parsers.util.ParserUtils;
85 import org.forester.io.parsers.PhylogenyParser;
86 import org.forester.phylogeny.Phylogeny;
87 import org.forester.phylogeny.PhylogenyMethods;
89 public class Example {
91 public static void main( final String[] args ) {
92 // Reading-in of (a) tree(s) from a file.
93 final File treefile = new File( "/path/to/tree.xml" );
94 PhylogenyParser parser = null;
96 parser = ParserUtils.createParserDependingOnFileType( treefile, true );
98 catch ( final IOException e ) {
101 Phylogeny[] phys = null;
103 phys = PhylogenyMethods.readPhylogenies( parser, treefile );
105 catch ( final IOException e ) {
108 // Display of the tree(s) with Archaeopteryx.
109 Archaeopteryx.createApplication( phys );
118 = Creating a new tree and displaying it with Archaeopteryx =
120 This needs file "forester.jar" to be in the class-path.
126 import org.forester.archaeopteryx.Archaeopteryx;
127 import org.forester.phylogeny.Phylogeny;
128 import org.forester.phylogeny.PhylogenyNode;
130 public class Example {
132 public static void main( final String[] args ) {
133 // Creating a new rooted tree with two external nodes.
134 final Phylogeny phy = new Phylogeny();
135 final PhylogenyNode root = new PhylogenyNode();
136 final PhylogenyNode d1 = new PhylogenyNode();
137 final PhylogenyNode d2 = new PhylogenyNode();
138 root.setName( "root" );
139 d1.setName( "descendant 1" );
140 d2.setName( "descendant 2" );
141 root.addAsChild( d1 );
142 root.addAsChild( d2 );
144 phy.setRooted( true );
145 // Displaying the newly created tree with Archaeopteryx.
146 Archaeopteryx.createApplication( phy );
157 = Using iterators to visit tree nodes in certain orders =
159 This needs file "forester.jar" to be in the class-path.
165 import org.forester.phylogeny.Phylogeny;
166 import org.forester.phylogeny.PhylogenyNode;
167 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
169 public class Example {
171 public static void main( final String[] args ) {
172 // Creating a new rooted tree with four external nodes.
173 final Phylogeny phy = new Phylogeny();
174 final PhylogenyNode root = new PhylogenyNode();
175 final PhylogenyNode d1 = new PhylogenyNode();
176 final PhylogenyNode d2 = new PhylogenyNode();
177 final PhylogenyNode d11 = new PhylogenyNode();
178 final PhylogenyNode d12 = new PhylogenyNode();
179 root.setName( "root" );
182 d11.setName( "1-1" );
183 d12.setName( "1-2" );
184 root.addAsChild( d1 );
185 root.addAsChild( d2 );
186 d2.addAsChild( d11 );
187 d2.addAsChild( d12 );
189 phy.setRooted( true );
190 // Using a variety of iterators to visit the nodes of the newly created tree.
191 System.out.println( "post-order:" );
192 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
193 System.out.println( it.next().getName() );
195 System.out.println( "pre-order:" );
196 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
197 System.out.println( it.next().getName() );
199 System.out.println( "level-order:" );
200 for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
201 System.out.println( it.next().getName() );
203 System.out.println( "external nodes only:" );
204 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
205 System.out.println( it.next().getName() );
220 = Creating a basic gene tree (with branch lengths) =
222 This needs file "forester.jar" to be in the class-path.
228 import org.forester.archaeopteryx.Archaeopteryx;
229 import org.forester.phylogeny.Phylogeny;
230 import org.forester.phylogeny.PhylogenyNode;
231 import org.forester.phylogeny.data.Event;
232 import org.forester.phylogeny.data.Sequence;
233 import org.forester.phylogeny.data.Taxonomy;
235 public class Example {
237 public static void main( final String[] args ) {
238 // Creating a new rooted tree with two external nodes.
239 final Phylogeny phy = new Phylogeny();
240 final PhylogenyNode root = new PhylogenyNode();
241 final PhylogenyNode d1 = new PhylogenyNode();
242 final PhylogenyNode d2 = new PhylogenyNode();
243 // Setting of distances.
244 d1.setDistanceToParent( 1.2 );
245 d2.setDistanceToParent( 2.4 );
246 // Adding species information.
247 final Taxonomy t1 = new Taxonomy();
248 t1.setScientificName( "Nematostella vectensis" );
249 d1.getNodeData().addTaxonomy( t1 );
250 final Taxonomy t2 = new Taxonomy();
251 t2.setScientificName( "Monosiga brevicollis" );
252 d2.getNodeData().addTaxonomy( t2 );
253 // Adding gene names.
254 final Sequence s1 = new Sequence();
255 s1.setName( "Bcl-2" );
256 d1.getNodeData().addSequence( s1 );
257 final Sequence s2 = new Sequence();
258 s2.setName( "Bcl-2" );
259 d2.getNodeData().addSequence( s2 );
260 // Root is a speciation.
261 final Event ev = new Event();
262 ev.setSpeciations( 1 );
263 ev.setDuplications( 0 );
264 root.getNodeData().setEvent( ev );
265 // Putting the tree together.
266 root.addAsChild( d1 );
267 root.addAsChild( d2 );
269 phy.setRooted( true );
270 // Displaying the newly created tree with Archaeopteryx.
271 Archaeopteryx.createApplication( phy );