1 = forester Tutorial and Examples =
2 <wiki:toc max_depth="3" />
8 This contains documentation, tutorials, and examples for [http://www.phylosoft.org/forester/ forester].
10 Documentation for [http://www.phylosoft.org/archaeopteryx/ Archaeopteryx] can be found [Archaeopteryx here].
12 *All examples require jar-file "forester.jar" to be in the class-path.*
14 Download: http://code.google.com/p/forester/downloads/list
16 Author: [http://www.cmzmasek.net/ Christian M Zmasek], Sanford-Burnham Medical Research Institute
19 Copyright (C) 2012 Christian M Zmasek. All rights reserved.
23 = Reading and writing of phylogenetic trees =
32 import java.io.IOException;
34 import org.forester.io.parsers.PhylogenyParser;
35 import org.forester.io.parsers.util.ParserUtils;
36 import org.forester.io.writers.PhylogenyWriter;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.PhylogenyMethods;
39 import org.forester.util.ForesterUtil;
41 public class Example {
43 public static void main( final String[] args ) {
44 // Reading-in of (a) tree(s) from a file.
45 final File treefile = new File( "/path/to/tree.xml" );
46 PhylogenyParser parser = null;
48 parser = ParserUtils.createParserDependingOnFileType( treefile, true );
50 catch ( final IOException e ) {
53 Phylogeny[] phys = null;
55 phys = PhylogenyMethods.readPhylogenies( parser, treefile );
57 catch ( final IOException e ) {
60 // Writing trees to a file.
61 final File outfile = new File( "/path/to/out_tree.xml" );
63 final PhylogenyWriter writer = new PhylogenyWriter();
64 writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
66 catch ( final Exception e ) {
76 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
84 import java.io.IOException;
86 import org.forester.archaeopteryx.Archaeopteryx;
87 import org.forester.io.parsers.util.ParserUtils;
88 import org.forester.io.parsers.PhylogenyParser;
89 import org.forester.phylogeny.Phylogeny;
90 import org.forester.phylogeny.PhylogenyMethods;
92 public class Example {
94 public static void main( final String[] args ) {
95 // Reading-in of (a) tree(s) from a file.
96 final File treefile = new File( "/path/to/tree.xml" );
97 PhylogenyParser parser = null;
99 parser = ParserUtils.createParserDependingOnFileType( treefile, true );
101 catch ( final IOException e ) {
104 Phylogeny[] phys = null;
106 phys = PhylogenyMethods.readPhylogenies( parser, treefile );
108 catch ( final IOException e ) {
111 // Display of the tree(s) with Archaeopteryx.
112 Archaeopteryx.createApplication( phys );
121 = Creating a new tree and displaying it with Archaeopteryx =
128 import org.forester.archaeopteryx.Archaeopteryx;
129 import org.forester.phylogeny.Phylogeny;
130 import org.forester.phylogeny.PhylogenyNode;
132 public class Example {
134 public static void main( final String[] args ) {
135 // Creating a new rooted tree with two external nodes.
136 final Phylogeny phy = new Phylogeny();
137 final PhylogenyNode root = new PhylogenyNode();
138 final PhylogenyNode d1 = new PhylogenyNode();
139 final PhylogenyNode d2 = new PhylogenyNode();
140 root.setName( "root" );
141 d1.setName( "descendant 1" );
142 d2.setName( "descendant 2" );
143 root.addAsChild( d1 );
144 root.addAsChild( d2 );
146 phy.setRooted( true );
147 // Displaying the newly created tree with Archaeopteryx.
148 Archaeopteryx.createApplication( phy );
159 = Using iterators to visit tree nodes in certain orders =
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) =
227 import org.forester.archaeopteryx.Archaeopteryx;
228 import org.forester.phylogeny.Phylogeny;
229 import org.forester.phylogeny.PhylogenyNode;
230 import org.forester.phylogeny.data.Event;
231 import org.forester.phylogeny.data.Sequence;
232 import org.forester.phylogeny.data.Taxonomy;
234 public class Example {
236 public static void main( final String[] args ) {
237 // Creating a new rooted tree with two external nodes.
238 final Phylogeny phy = new Phylogeny();
239 final PhylogenyNode root = new PhylogenyNode();
240 final PhylogenyNode d1 = new PhylogenyNode();
241 final PhylogenyNode d2 = new PhylogenyNode();
242 // Setting of distances.
243 d1.setDistanceToParent( 1.2 );
244 d2.setDistanceToParent( 2.4 );
245 // Adding species information.
246 final Taxonomy t1 = new Taxonomy();
247 t1.setScientificName( "Nematostella vectensis" );
248 d1.getNodeData().addTaxonomy( t1 );
249 final Taxonomy t2 = new Taxonomy();
250 t2.setScientificName( "Monosiga brevicollis" );
251 d2.getNodeData().addTaxonomy( t2 );
252 // Adding gene names.
253 final Sequence s1 = new Sequence();
254 s1.setName( "Bcl-2" );
255 d1.getNodeData().addSequence( s1 );
256 final Sequence s2 = new Sequence();
257 s2.setName( "Bcl-2" );
258 d2.getNodeData().addSequence( s2 );
259 // Root is a speciation.
260 final Event ev = new Event();
261 ev.setSpeciations( 1 );
262 ev.setDuplications( 0 );
263 root.getNodeData().setEvent( ev );
264 // Putting the tree together.
265 root.addAsChild( d1 );
266 root.addAsChild( d2 );
268 phy.setRooted( true );
269 // Displaying the newly created tree with Archaeopteryx.
270 Archaeopteryx.createApplication( phy );
276 = Writing a phylogenetic tree to a graphics file (e.g. png, jpg) =
282 import java.awt.Color;
284 import java.io.IOException;
286 import org.forester.archaeopteryx.AptxUtil;
287 import org.forester.archaeopteryx.AptxUtil.GraphicsExportType;
288 import org.forester.archaeopteryx.Configuration;
289 import org.forester.archaeopteryx.Options;
290 import org.forester.archaeopteryx.TreeColorSet;
292 public class phylo2graphics {
294 public static void main( final String[] args ) {
296 final Configuration config = new Configuration();
297 // Could also read a configuration file with:
298 // Configuration config = new Configuration("my_configuration_file.txt", false, false, false);
299 config.putDisplayColors( TreeColorSet.BACKGROUND, new Color( 255, 255, 255 ) );
300 config.putDisplayColors( TreeColorSet.BRANCH, new Color( 0, 0, 0 ) );
301 config.putDisplayColors( TreeColorSet.TAXONOMY, new Color( 0, 0, 0 ) );
302 config.setPhylogenyGraphicsType( Options.PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
303 AptxUtil.writePhylogenyToGraphicsFile( new File( "my_tree.xml" ),
304 new File( "my_tree_graphics.png" ),
307 GraphicsExportType.PNG,
309 // If the tree 'phy' already exists, can also use this:
310 AptxUtil.writePhylogenyToGraphicsFile( phy,
311 new File( "out.png" ),
314 GraphicsExportType.PNG,
317 catch ( final IOException e ) {
325 = Setting node/branch colors of a phylogenetic tree and writing it to a graphics file =
331 import java.awt.Color;
333 import java.io.IOException;
334 import java.util.HashMap;
335 import java.util.Map;
337 import org.forester.archaeopteryx.AptxUtil;
338 import org.forester.archaeopteryx.AptxUtil.GraphicsExportType;
339 import org.forester.archaeopteryx.Configuration;
340 import org.forester.archaeopteryx.Options;
341 import org.forester.archaeopteryx.TreeColorSet;
342 import org.forester.io.parsers.PhylogenyParser;
343 import org.forester.io.parsers.util.ParserUtils;
344 import org.forester.phylogeny.Phylogeny;
345 import org.forester.phylogeny.PhylogenyMethods;
346 import org.forester.phylogeny.PhylogenyNode;
347 import org.forester.phylogeny.data.BranchColor;
348 import org.forester.phylogeny.data.BranchWidth;
349 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
351 public class phylo2coloredgraphics {
353 public static void main( final String[] args ) {
355 // Reading-in of a tree from a file.
356 final File treefile = new File( "my_tree.nh" );
357 final PhylogenyParser parser = ParserUtils.createParserDependingOnFileType( treefile, true );
358 final Phylogeny phy = PhylogenyMethods.readPhylogenies( parser, treefile )[ 0 ];
359 // Creating a node name -> color map.
360 final Map<String, Color> colors = new HashMap<String, Color>();
361 colors.put( "Primates", new Color( 255, 255, 0 ) );
362 colors.put( "PANTR", new Color( 255, 0, 255 ) );
363 colors.put( "HUMAN", new Color( 255, 0, 0 ) );
364 colors.put( "RAT", new Color( 155, 0, 0 ) );
365 colors.put( "MOUSE", new Color( 55, 155, 0 ) );
366 colors.put( "CAVPO", new Color( 155, 155, 0 ) );
367 colors.put( "LOTGI", new Color( 155, 155, 255 ) );
369 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
370 final PhylogenyNode n = it.next();
371 if ( colors.containsKey( n.getName() ) ) {
372 n.getBranchData().setBranchColor( new BranchColor( colors.get( n.getName() ) ) );
373 // To make colored subtrees thicker:
374 n.getBranchData().setBranchWidth( new BranchWidth( 4 ) );
377 // Setting up a configuration object.
378 final Configuration config = new Configuration();
379 config.putDisplayColors( TreeColorSet.BACKGROUND, new Color( 255, 255, 255 ) );
380 config.putDisplayColors( TreeColorSet.BRANCH, new Color( 0, 0, 0 ) );
381 config.putDisplayColors( TreeColorSet.TAXONOMY, new Color( 0, 0, 0 ) );
382 config.setPhylogenyGraphicsType( Options.PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
383 config.setTaxonomyColorize( false );
384 config.setColorizeBranches( true );
385 config.setUseBranchesWidths( true );
386 config.setDisplayTaxonomyCode( false );
387 // Writing to a graphics file.
388 AptxUtil.writePhylogenyToGraphicsFile( phy,
389 new File( "out.png" ),
392 GraphicsExportType.PNG,
395 catch ( final IOException e ) {