1 = forester Tutorial and Examples =
2 <wiki:toc max_depth="3" />
8 Documentation, tutorial, and examples for [http://www.phylosoft.org/forester/ forester].
10 Author: [http://www.cmzmasek.net/ Christian M Zmasek], Sanford-Burnham Medical Research Institute
13 Copyright (C) 2011 Christian M Zmasek. All rights reserved.
16 = Reading and writing of phylogenetic trees =
18 *This needs file "forester.jar" (actually, as of now, it requires the development version "forester_dev.jar") to be in the class-path.*
25 import java.io.IOException;
27 import org.forester.io.parsers.PhylogenyParser;
28 import org.forester.io.parsers.util.ParserUtils;
29 import org.forester.io.writers.PhylogenyWriter;
30 import org.forester.phylogeny.Phylogeny;
31 import org.forester.phylogeny.PhylogenyMethods;
32 import org.forester.util.ForesterUtil;
34 public class Example {
36 public static void main( final String[] args ) {
37 // Reading-in of (a) tree(s) from a file.
38 final File treefile = new File( "/path/to/tree.xml" );
39 PhylogenyParser parser = null;
41 parser = ParserUtils.createParserDependingOnFileType( treefile, true );
43 catch ( final IOException e ) {
46 Phylogeny[] phys = null;
48 phys = PhylogenyMethods.readPhylogenies( parser, treefile );
50 catch ( final IOException e ) {
53 // Writing trees to a file.
54 final File outfile = new File( "/path/to/out_tree.xml" );
56 final PhylogenyWriter writer = new PhylogenyWriter();
57 writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
59 catch ( final Exception e ) {
69 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
71 This needs file "forester.jar" to be in the class-path.
78 import java.io.IOException;
80 import org.forester.archaeopteryx.Archaeopteryx;
81 import org.forester.io.parsers.util.ParserUtils;
82 import org.forester.io.parsers.PhylogenyParser;
83 import org.forester.phylogeny.Phylogeny;
84 import org.forester.phylogeny.PhylogenyMethods;
86 public class Example {
88 public static void main( final String[] args ) {
89 // Reading-in of (a) tree(s) from a file.
90 final File treefile = new File( "/path/to/tree.xml" );
91 PhylogenyParser parser = null;
93 parser = ParserUtils.createParserDependingOnFileType( treefile, true );
95 catch ( final IOException e ) {
98 Phylogeny[] phys = null;
100 phys = PhylogenyMethods.readPhylogenies( parser, treefile );
102 catch ( final IOException e ) {
105 // Display of the tree(s) with Archaeopteryx.
106 Archaeopteryx.createApplication( phys );
115 = Creating a new tree and displaying it with Archaeopteryx =
117 This needs file "forester.jar" to be in the class-path.
123 import org.forester.archaeopteryx.Archaeopteryx;
124 import org.forester.phylogeny.Phylogeny;
125 import org.forester.phylogeny.PhylogenyNode;
127 public class Example {
129 public static void main( final String[] args ) {
130 // Creating a new rooted tree with two external nodes.
131 final Phylogeny phy = new Phylogeny();
132 final PhylogenyNode root = new PhylogenyNode();
133 final PhylogenyNode d1 = new PhylogenyNode();
134 final PhylogenyNode d2 = new PhylogenyNode();
135 root.setName( "root" );
136 d1.setName( "descendant 1" );
137 d2.setName( "descendant 2" );
138 root.addAsChild( d1 );
139 root.addAsChild( d2 );
141 phy.setRooted( true );
142 // Displaying the newly created tree with Archaeopteryx.
143 Archaeopteryx.createApplication( phy );
154 = Using iterators to visit tree nodes in certain orders =
156 This needs file "forester.jar" to be in the class-path.
162 import org.forester.phylogeny.Phylogeny;
163 import org.forester.phylogeny.PhylogenyNode;
164 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
166 public class Example {
168 public static void main( final String[] args ) {
169 // Creating a new rooted tree with four external nodes.
170 final Phylogeny phy = new Phylogeny();
171 final PhylogenyNode root = new PhylogenyNode();
172 final PhylogenyNode d1 = new PhylogenyNode();
173 final PhylogenyNode d2 = new PhylogenyNode();
174 final PhylogenyNode d11 = new PhylogenyNode();
175 final PhylogenyNode d12 = new PhylogenyNode();
176 root.setName( "root" );
179 d11.setName( "1-1" );
180 d12.setName( "1-2" );
181 root.addAsChild( d1 );
182 root.addAsChild( d2 );
183 d2.addAsChild( d11 );
184 d2.addAsChild( d12 );
186 phy.setRooted( true );
187 // Using a variety of iterators to visit the nodes of the newly created tree.
188 System.out.println( "post-order:" );
189 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
190 System.out.println( it.next().getName() );
192 System.out.println( "pre-order:" );
193 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
194 System.out.println( it.next().getName() );
196 System.out.println( "level-order:" );
197 for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
198 System.out.println( it.next().getName() );
200 System.out.println( "external nodes only:" );
201 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
202 System.out.println( it.next().getName() );
217 = Creating a basic gene tree (with branch lengths) =
219 This needs file "forester.jar" to be in the class-path.
225 import org.forester.archaeopteryx.Archaeopteryx;
226 import org.forester.phylogeny.Phylogeny;
227 import org.forester.phylogeny.PhylogenyNode;
228 import org.forester.phylogeny.data.Event;
229 import org.forester.phylogeny.data.Sequence;
230 import org.forester.phylogeny.data.Taxonomy;
232 public class Example {
234 public static void main( final String[] args ) {
235 // Creating a new rooted tree with two external nodes.
236 final Phylogeny phy = new Phylogeny();
237 final PhylogenyNode root = new PhylogenyNode();
238 final PhylogenyNode d1 = new PhylogenyNode();
239 final PhylogenyNode d2 = new PhylogenyNode();
240 // Setting of distances.
241 d1.setDistanceToParent( 1.2 );
242 d2.setDistanceToParent( 2.4 );
243 // Adding species information.
244 final Taxonomy t1 = new Taxonomy();
245 t1.setScientificName( "Nematostella vectensis" );
246 d1.getNodeData().addTaxonomy( t1 );
247 final Taxonomy t2 = new Taxonomy();
248 t2.setScientificName( "Monosiga brevicollis" );
249 d2.getNodeData().addTaxonomy( t2 );
250 // Adding gene names.
251 final Sequence s1 = new Sequence();
252 s1.setName( "Bcl-2" );
253 d1.getNodeData().addSequence( s1 );
254 final Sequence s2 = new Sequence();
255 s2.setName( "Bcl-2" );
256 d2.getNodeData().addSequence( s2 );
257 // Root is a speciation.
258 final Event ev = new Event();
259 ev.setSpeciations( 1 );
260 ev.setDuplications( 0 );
261 root.getNodeData().setEvent( ev );
262 // Putting the tree together.
263 root.addAsChild( d1 );
264 root.addAsChild( d2 );
266 phy.setRooted( true );
267 // Displaying the newly created tree with Archaeopteryx.
268 Archaeopteryx.createApplication( phy );