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 =
80 import java.io.IOException;
82 import org.forester.archaeopteryx.Archaeopteryx;
83 import org.forester.io.parsers.util.ParserUtils;
84 import org.forester.io.parsers.PhylogenyParser;
85 import org.forester.phylogeny.Phylogeny;
86 import org.forester.phylogeny.PhylogenyMethods;
88 public class Example {
90 public static void main( final String[] args ) {
91 // Reading-in of (a) tree(s) from a file.
92 final File treefile = new File( "/path/to/tree.xml" );
93 PhylogenyParser parser = null;
95 parser = ParserUtils.createParserDependingOnFileType( treefile, true );
97 catch ( final IOException e ) {
100 Phylogeny[] phys = null;
102 phys = PhylogenyMethods.readPhylogenies( parser, treefile );
104 catch ( final IOException e ) {
107 // Display of the tree(s) with Archaeopteryx.
108 Archaeopteryx.createApplication( phys );
117 = Creating a new tree and displaying it with Archaeopteryx =
124 import org.forester.archaeopteryx.Archaeopteryx;
125 import org.forester.phylogeny.Phylogeny;
126 import org.forester.phylogeny.PhylogenyNode;
128 public class Example {
130 public static void main( final String[] args ) {
131 // Creating a new rooted tree with two external nodes.
132 final Phylogeny phy = new Phylogeny();
133 final PhylogenyNode root = new PhylogenyNode();
134 final PhylogenyNode d1 = new PhylogenyNode();
135 final PhylogenyNode d2 = new PhylogenyNode();
136 root.setName( "root" );
137 d1.setName( "descendant 1" );
138 d2.setName( "descendant 2" );
139 root.addAsChild( d1 );
140 root.addAsChild( d2 );
142 phy.setRooted( true );
143 // Displaying the newly created tree with Archaeopteryx.
144 Archaeopteryx.createApplication( phy );
155 = Using iterators to visit tree nodes in certain orders =
161 import org.forester.phylogeny.Phylogeny;
162 import org.forester.phylogeny.PhylogenyNode;
163 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
165 public class Example {
167 public static void main( final String[] args ) {
168 // Creating a new rooted tree with four external nodes.
169 final Phylogeny phy = new Phylogeny();
170 final PhylogenyNode root = new PhylogenyNode();
171 final PhylogenyNode d1 = new PhylogenyNode();
172 final PhylogenyNode d2 = new PhylogenyNode();
173 final PhylogenyNode d11 = new PhylogenyNode();
174 final PhylogenyNode d12 = new PhylogenyNode();
175 root.setName( "root" );
178 d11.setName( "1-1" );
179 d12.setName( "1-2" );
180 root.addAsChild( d1 );
181 root.addAsChild( d2 );
182 d2.addAsChild( d11 );
183 d2.addAsChild( d12 );
185 phy.setRooted( true );
186 // Using a variety of iterators to visit the nodes of the newly created tree.
187 System.out.println( "post-order:" );
188 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
189 System.out.println( it.next().getName() );
191 System.out.println( "pre-order:" );
192 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
193 System.out.println( it.next().getName() );
195 System.out.println( "level-order:" );
196 for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
197 System.out.println( it.next().getName() );
199 System.out.println( "external nodes only:" );
200 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
201 System.out.println( it.next().getName() );
216 = Creating a basic gene tree (with branch lengths) =
223 import org.forester.archaeopteryx.Archaeopteryx;
224 import org.forester.phylogeny.Phylogeny;
225 import org.forester.phylogeny.PhylogenyNode;
226 import org.forester.phylogeny.data.Event;
227 import org.forester.phylogeny.data.Sequence;
228 import org.forester.phylogeny.data.Taxonomy;
230 public class Example {
232 public static void main( final String[] args ) {
233 // Creating a new rooted tree with two external nodes.
234 final Phylogeny phy = new Phylogeny();
235 final PhylogenyNode root = new PhylogenyNode();
236 final PhylogenyNode d1 = new PhylogenyNode();
237 final PhylogenyNode d2 = new PhylogenyNode();
238 // Setting of distances.
239 d1.setDistanceToParent( 1.2 );
240 d2.setDistanceToParent( 2.4 );
241 // Adding species information.
242 final Taxonomy t1 = new Taxonomy();
243 t1.setScientificName( "Nematostella vectensis" );
244 d1.getNodeData().addTaxonomy( t1 );
245 final Taxonomy t2 = new Taxonomy();
246 t2.setScientificName( "Monosiga brevicollis" );
247 d2.getNodeData().addTaxonomy( t2 );
248 // Adding gene names.
249 final Sequence s1 = new Sequence();
250 s1.setName( "Bcl-2" );
251 d1.getNodeData().addSequence( s1 );
252 final Sequence s2 = new Sequence();
253 s2.setName( "Bcl-2" );
254 d2.getNodeData().addSequence( s2 );
255 // Root is a speciation.
256 final Event ev = new Event();
257 ev.setSpeciations( 1 );
258 ev.setDuplications( 0 );
259 root.getNodeData().setEvent( ev );
260 // Putting the tree together.
261 root.addAsChild( d1 );
262 root.addAsChild( d2 );
264 phy.setRooted( true );
265 // Displaying the newly created tree with Archaeopteryx.
266 Archaeopteryx.createApplication( phy );