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" to be in the class-path.
25 import java.io.IOException;
27 import org.forester.io.parsers.PhylogenyParser;
28 import org.forester.io.writers.PhylogenyWriter;
29 import org.forester.phylogeny.Phylogeny;
30 import org.forester.util.ForesterUtil;
32 public class Example {
34 public static void main( final String[] args ) {
35 // Reading-in of (a) tree(s) from a file.
36 final File treefile = new File( "/path/to/tree.xml" );
37 PhylogenyParser parser = null;
39 parser = ForesterUtil.createParserDependingOnFileType( treefile, true );
41 catch ( final IOException e ) {
44 Phylogeny[] phys = null;
46 phys = ForesterUtil.readPhylogenies( parser, treefile );
48 catch ( final IOException e ) {
51 // Writing trees to a file.
52 final File outfile = new File( "/path/to/out_tree.xml" );
54 final PhylogenyWriter writer = new PhylogenyWriter();
55 writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
57 catch ( final Exception e ) {
67 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
69 This needs file "forester.jar" to be in the class-path.
76 import java.io.IOException;
78 import org.forester.archaeopteryx.Archaeopteryx;
79 import org.forester.io.parsers.PhylogenyParser;
80 import org.forester.phylogeny.Phylogeny;
81 import org.forester.util.ForesterUtil;
83 public class Example {
85 public static void main( final String[] args ) {
86 // Reading-in of (a) tree(s) from a file.
87 final File treefile = new File( "/path/to/tree.xml" );
88 PhylogenyParser parser = null;
90 parser = ForesterUtil.createParserDependingOnFileType( treefile, true );
92 catch ( final IOException e ) {
95 Phylogeny[] phys = null;
97 phys = ForesterUtil.readPhylogenies( parser, treefile );
99 catch ( final IOException e ) {
102 // Display of the tree(s) with Archaeopteryx.
103 Archaeopteryx.createApplication( phys );
112 = Creating a new tree and displaying it with Archaeopteryx =
114 This needs file "forester.jar" to be in the class-path.
120 import org.forester.archaeopteryx.Archaeopteryx;
121 import org.forester.phylogeny.Phylogeny;
122 import org.forester.phylogeny.PhylogenyNode;
124 public class Example {
126 public static void main( final String[] args ) {
127 // Creating a new rooted tree with two external nodes.
128 final Phylogeny phy = new Phylogeny();
129 final PhylogenyNode root = new PhylogenyNode();
130 final PhylogenyNode d1 = new PhylogenyNode();
131 final PhylogenyNode d2 = new PhylogenyNode();
132 root.setName( "root" );
133 d1.setName( "descendant 1" );
134 d2.setName( "descendant 2" );
135 root.addAsChild( d1 );
136 root.addAsChild( d2 );
138 phy.setRooted( true );
139 // Displaying the newly created tree with Archaeopteryx.
140 Archaeopteryx.createApplication( phy );
151 = Using iterators to visit tree nodes in certain orders =
153 This needs file "forester.jar" to be in the class-path.
159 import org.forester.phylogeny.Phylogeny;
160 import org.forester.phylogeny.PhylogenyNode;
161 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
163 public class Example {
165 public static void main( final String[] args ) {
166 // Creating a new rooted tree with four external nodes.
167 final Phylogeny phy = new Phylogeny();
168 final PhylogenyNode root = new PhylogenyNode();
169 final PhylogenyNode d1 = new PhylogenyNode();
170 final PhylogenyNode d2 = new PhylogenyNode();
171 final PhylogenyNode d11 = new PhylogenyNode();
172 final PhylogenyNode d12 = new PhylogenyNode();
173 root.setName( "root" );
176 d11.setName( "1-1" );
177 d12.setName( "1-2" );
178 root.addAsChild( d1 );
179 root.addAsChild( d2 );
180 d2.addAsChild( d11 );
181 d2.addAsChild( d12 );
183 phy.setRooted( true );
184 // Using a variety of iterators to visit the nodes of the newly created tree.
185 System.out.println( "post-order:" );
186 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
187 System.out.println( it.next().getName() );
189 System.out.println( "pre-order:" );
190 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
191 System.out.println( it.next().getName() );
193 System.out.println( "level-order:" );
194 for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
195 System.out.println( it.next().getName() );
197 System.out.println( "external nodes only:" );
198 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
199 System.out.println( it.next().getName() );
214 = Creating a basic gene tree (with branch lengths) =
216 This needs file "forester.jar" to be in the class-path.
222 import org.forester.archaeopteryx.Archaeopteryx;
223 import org.forester.phylogeny.Phylogeny;
224 import org.forester.phylogeny.PhylogenyNode;
225 import org.forester.phylogeny.data.Sequence;
226 import org.forester.phylogeny.data.Taxonomy;
228 public class Example {
230 public static void main( final String[] args ) {
231 // Creating a new rooted tree with two external nodes.
232 final Phylogeny phy = new Phylogeny();
233 final PhylogenyNode root = new PhylogenyNode();
234 final PhylogenyNode d1 = new PhylogenyNode();
235 final PhylogenyNode d2 = new PhylogenyNode();
236 // Setting of distances.
237 d1.setDistanceToParent( 1.2 );
238 d2.setDistanceToParent( 2.4 );
239 // Adding species information.
240 final Taxonomy t1 = new Taxonomy();
241 t1.setScientificName( "Nematostella vectensis" );
242 d1.getNodeData().addTaxonomy( t1 );
243 final Taxonomy t2 = new Taxonomy();
244 t2.setScientificName( "Monosiga brevicollis" );
245 d2.getNodeData().addTaxonomy( t2 );
246 // Adding gene names.
247 final Sequence s1 = new Sequence();
248 s1.setName( "Bcl-2" );
249 d1.getNodeData().addSequence( s1 );
250 final Sequence s2 = new Sequence();
251 s2.setName( "Bcl-2" );
252 d2.getNodeData().addSequence( s2 );
253 // Root is a speciation.
254 final Event ev = new Event();
255 ev.setSpeciations( 1 );
256 ev.setDuplications( 0 );
257 root.getNodeData().setEvent( ev );
258 // Putting the tree together.
259 root.addAsChild( d1 );
260 root.addAsChild( d2 );
262 phy.setRooted( true );
263 // Displaying the newly created tree with Archaeopteryx.
264 Archaeopteryx.createApplication( phy );