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 Download: http://code.google.com/p/forester/downloads/list
14 Author: [http://www.cmzmasek.net/ Christian M Zmasek], Sanford-Burnham Medical Research Institute
17 Copyright (C) 2011 Christian M Zmasek. All rights reserved.
21 = Reading and writing of phylogenetic trees =
30 import java.io.IOException;
32 import org.forester.io.parsers.PhylogenyParser;
33 import org.forester.io.parsers.util.ParserUtils;
34 import org.forester.io.writers.PhylogenyWriter;
35 import org.forester.phylogeny.Phylogeny;
36 import org.forester.phylogeny.PhylogenyMethods;
37 import org.forester.util.ForesterUtil;
39 public class Example {
41 public static void main( final String[] args ) {
42 // Reading-in of (a) tree(s) from a file.
43 final File treefile = new File( "/path/to/tree.xml" );
44 PhylogenyParser parser = null;
46 parser = ParserUtils.createParserDependingOnFileType( treefile, true );
48 catch ( final IOException e ) {
51 Phylogeny[] phys = null;
53 phys = PhylogenyMethods.readPhylogenies( parser, treefile );
55 catch ( final IOException e ) {
58 // Writing trees to a file.
59 final File outfile = new File( "/path/to/out_tree.xml" );
61 final PhylogenyWriter writer = new PhylogenyWriter();
62 writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
64 catch ( final Exception e ) {
74 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
82 import java.io.IOException;
84 import org.forester.archaeopteryx.Archaeopteryx;
85 import org.forester.io.parsers.util.ParserUtils;
86 import org.forester.io.parsers.PhylogenyParser;
87 import org.forester.phylogeny.Phylogeny;
88 import org.forester.phylogeny.PhylogenyMethods;
90 public class Example {
92 public static void main( final String[] args ) {
93 // Reading-in of (a) tree(s) from a file.
94 final File treefile = new File( "/path/to/tree.xml" );
95 PhylogenyParser parser = null;
97 parser = ParserUtils.createParserDependingOnFileType( treefile, true );
99 catch ( final IOException e ) {
102 Phylogeny[] phys = null;
104 phys = PhylogenyMethods.readPhylogenies( parser, treefile );
106 catch ( final IOException e ) {
109 // Display of the tree(s) with Archaeopteryx.
110 Archaeopteryx.createApplication( phys );
119 = Creating a new tree and displaying it with Archaeopteryx =
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 =
163 import org.forester.phylogeny.Phylogeny;
164 import org.forester.phylogeny.PhylogenyNode;
165 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
167 public class Example {
169 public static void main( final String[] args ) {
170 // Creating a new rooted tree with four external nodes.
171 final Phylogeny phy = new Phylogeny();
172 final PhylogenyNode root = new PhylogenyNode();
173 final PhylogenyNode d1 = new PhylogenyNode();
174 final PhylogenyNode d2 = new PhylogenyNode();
175 final PhylogenyNode d11 = new PhylogenyNode();
176 final PhylogenyNode d12 = new PhylogenyNode();
177 root.setName( "root" );
180 d11.setName( "1-1" );
181 d12.setName( "1-2" );
182 root.addAsChild( d1 );
183 root.addAsChild( d2 );
184 d2.addAsChild( d11 );
185 d2.addAsChild( d12 );
187 phy.setRooted( true );
188 // Using a variety of iterators to visit the nodes of the newly created tree.
189 System.out.println( "post-order:" );
190 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
191 System.out.println( it.next().getName() );
193 System.out.println( "pre-order:" );
194 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
195 System.out.println( it.next().getName() );
197 System.out.println( "level-order:" );
198 for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
199 System.out.println( it.next().getName() );
201 System.out.println( "external nodes only:" );
202 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
203 System.out.println( it.next().getName() );
218 = Creating a basic gene tree (with branch lengths) =
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 );
278 import java.awt.Color;
280 import java.io.IOException;
282 import org.forester.archaeopteryx.AptxUtil;
283 import org.forester.archaeopteryx.AptxUtil.GraphicsExportType;
284 import org.forester.archaeopteryx.Configuration;
285 import org.forester.archaeopteryx.Options;
286 import org.forester.archaeopteryx.TreeColorSet;
288 public class phylo2graphics {
290 public static void main(final String[] args) {
292 Configuration config = new Configuration();
293 // Could also read a configuration file with:
294 // Configuration config = new Configuration("my_configuration_file.txt", false, false, false);
296 config.putDisplayColors(TreeColorSet.BACKGROUND, new Color(255, 255, 255));
297 config.putDisplayColors(TreeColorSet.BRANCH, new Color(0, 0, 0));
298 config.putDisplayColors(TreeColorSet.TAXONOMY, new Color(0, 0, 0));
299 config.setPhylogenyGraphicsType(Options.PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR);
300 AptxUtil.writePhylogenyToGraphicsFileNonInteractive(new File(
301 "/home/czmasek/tol_117_TEST.xml"), new File(
302 "/home/czmasek/tol_117_TEST_.png"), 1000, 1000,
303 GraphicsExportType.PNG, config);
305 catch (IOException e) {