1 #summary forester Tutorial and Examples
3 = forester Tutorial and Examples =
4 <wiki:toc max_depth="3" />
10 This contains documentation, tutorials, and examples for [https://sites.google.com/site/cmzmasek/home/software/forester/ forester].
12 Documentation for [https://sites.google.com/site/cmzmasek/home/software/archaeopteryx/ Archaeopteryx] can be found [https://sites.google.com/site/cmzmasek/home/software/archaeopteryx/documentation here].
14 *All examples require jar-file "forester.jar" to be in the class-path.*
16 Download: http://code.google.com/p/forester/downloads/list
18 Author: [https://sites.google.com/site/cmzmasek/ Christian Zmasek], Sanford-Burnham Medical Research Institute
21 Copyright (C) 2013 Christian M Zmasek. All rights reserved.
25 = Reading and writing of phylogenetic trees =
34 import java.io.IOException;
36 import org.forester.io.parsers.PhylogenyParser;
37 import org.forester.io.parsers.util.ParserUtils;
38 import org.forester.io.writers.PhylogenyWriter;
39 import org.forester.phylogeny.Phylogeny;
40 import org.forester.phylogeny.PhylogenyMethods;
41 import org.forester.util.ForesterUtil;
43 public class Example {
45 public static void main( final String[] args ) {
46 // Reading-in of (a) tree(s) from a file.
47 final File treefile = new File( "/path/to/tree.xml" );
48 PhylogenyParser parser = null;
50 parser = ParserUtils.createParserDependingOnFileType( treefile, true );
52 catch ( final IOException e ) {
55 Phylogeny[] phys = null;
57 phys = PhylogenyMethods.readPhylogenies( parser, treefile );
59 catch ( final IOException e ) {
62 // Writing trees to a file.
63 final File outfile = new File( "/path/to/out_tree.xml" );
65 final PhylogenyWriter writer = new PhylogenyWriter();
66 writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
68 catch ( final Exception e ) {
78 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
86 import java.io.IOException;
88 import org.forester.archaeopteryx.Archaeopteryx;
89 import org.forester.io.parsers.util.ParserUtils;
90 import org.forester.io.parsers.PhylogenyParser;
91 import org.forester.phylogeny.Phylogeny;
92 import org.forester.phylogeny.PhylogenyMethods;
94 public class Example {
96 public static void main( final String[] args ) {
97 // Reading-in of (a) tree(s) from a file.
98 final File treefile = new File( "/path/to/tree.xml" );
99 PhylogenyParser parser = null;
101 parser = ParserUtils.createParserDependingOnFileType( treefile, true );
103 catch ( final IOException e ) {
106 Phylogeny[] phys = null;
108 phys = PhylogenyMethods.readPhylogenies( parser, treefile );
110 catch ( final IOException e ) {
113 // Display of the tree(s) with Archaeopteryx.
114 Archaeopteryx.createApplication( phys );
123 = Creating a new tree and displaying it with Archaeopteryx =
130 import org.forester.archaeopteryx.Archaeopteryx;
131 import org.forester.phylogeny.Phylogeny;
132 import org.forester.phylogeny.PhylogenyNode;
134 public class Example {
136 public static void main( final String[] args ) {
137 // Creating a new rooted tree with two external nodes.
138 final Phylogeny phy = new Phylogeny();
139 final PhylogenyNode root = new PhylogenyNode();
140 final PhylogenyNode d1 = new PhylogenyNode();
141 final PhylogenyNode d2 = new PhylogenyNode();
142 root.setName( "root" );
143 d1.setName( "descendant 1" );
144 d2.setName( "descendant 2" );
145 root.addAsChild( d1 );
146 root.addAsChild( d2 );
148 phy.setRooted( true );
149 // Displaying the newly created tree with Archaeopteryx.
150 Archaeopteryx.createApplication( phy );
161 = Using iterators to visit tree nodes in certain orders =
167 import org.forester.phylogeny.Phylogeny;
168 import org.forester.phylogeny.PhylogenyNode;
169 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
171 public class Example {
173 public static void main( final String[] args ) {
174 // Creating a new rooted tree with four external nodes.
175 final Phylogeny phy = new Phylogeny();
176 final PhylogenyNode root = new PhylogenyNode();
177 final PhylogenyNode d1 = new PhylogenyNode();
178 final PhylogenyNode d2 = new PhylogenyNode();
179 final PhylogenyNode d11 = new PhylogenyNode();
180 final PhylogenyNode d12 = new PhylogenyNode();
181 root.setName( "root" );
184 d11.setName( "1-1" );
185 d12.setName( "1-2" );
186 root.addAsChild( d1 );
187 root.addAsChild( d2 );
188 d2.addAsChild( d11 );
189 d2.addAsChild( d12 );
191 phy.setRooted( true );
192 // Using a variety of iterators to visit the nodes of the newly created tree.
193 System.out.println( "post-order:" );
194 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
195 System.out.println( it.next().getName() );
197 System.out.println( "pre-order:" );
198 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
199 System.out.println( it.next().getName() );
201 System.out.println( "level-order:" );
202 for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
203 System.out.println( it.next().getName() );
205 System.out.println( "external nodes only:" );
206 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
207 System.out.println( it.next().getName() );
222 = Creating a basic gene tree (with branch lengths) =
229 import org.forester.archaeopteryx.Archaeopteryx;
230 import org.forester.phylogeny.Phylogeny;
231 import org.forester.phylogeny.PhylogenyNode;
232 import org.forester.phylogeny.data.Event;
233 import org.forester.phylogeny.data.Sequence;
234 import org.forester.phylogeny.data.Taxonomy;
236 public class Example {
238 public static void main( final String[] args ) {
239 // Creating a new rooted tree with two external nodes.
240 final Phylogeny phy = new Phylogeny();
241 final PhylogenyNode root = new PhylogenyNode();
242 final PhylogenyNode d1 = new PhylogenyNode();
243 final PhylogenyNode d2 = new PhylogenyNode();
244 // Setting of distances.
245 d1.setDistanceToParent( 1.2 );
246 d2.setDistanceToParent( 2.4 );
247 // Adding species information.
248 final Taxonomy t1 = new Taxonomy();
249 t1.setScientificName( "Nematostella vectensis" );
250 d1.getNodeData().addTaxonomy( t1 );
251 final Taxonomy t2 = new Taxonomy();
252 t2.setScientificName( "Monosiga brevicollis" );
253 d2.getNodeData().addTaxonomy( t2 );
254 // Adding gene names.
255 final Sequence s1 = new Sequence();
256 s1.setName( "Bcl-2" );
257 d1.getNodeData().addSequence( s1 );
258 final Sequence s2 = new Sequence();
259 s2.setName( "Bcl-2" );
260 d2.getNodeData().addSequence( s2 );
261 // Root is a speciation.
262 final Event ev = new Event();
263 ev.setSpeciations( 1 );
264 ev.setDuplications( 0 );
265 root.getNodeData().setEvent( ev );
266 // Putting the tree together.
267 root.addAsChild( d1 );
268 root.addAsChild( d2 );
270 phy.setRooted( true );
271 // Displaying the newly created tree with Archaeopteryx.
272 Archaeopteryx.createApplication( phy );
278 = Writing a phylogenetic tree to a graphics file (e.g. png, jpg) =
284 import java.awt.Color;
286 import java.io.IOException;
288 import org.forester.archaeopteryx.AptxUtil;
289 import org.forester.archaeopteryx.AptxUtil.GraphicsExportType;
290 import org.forester.archaeopteryx.Configuration;
291 import org.forester.archaeopteryx.Options;
292 import org.forester.archaeopteryx.TreeColorSet;
294 public class phylo2graphics {
296 public static void main( final String[] args ) {
298 final Configuration config = new Configuration();
299 // Could also read a configuration file with:
300 // Configuration config = new Configuration("my_configuration_file.txt", false, false, false);
301 config.putDisplayColors( TreeColorSet.BACKGROUND, new Color( 255, 255, 255 ) );
302 config.putDisplayColors( TreeColorSet.BRANCH, new Color( 0, 0, 0 ) );
303 config.putDisplayColors( TreeColorSet.TAXONOMY, new Color( 0, 0, 0 ) );
304 config.setPhylogenyGraphicsType( Options.PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
305 AptxUtil.writePhylogenyToGraphicsFile( new File( "my_tree.xml" ),
306 new File( "my_tree_graphics.png" ),
309 GraphicsExportType.PNG,
311 // If the tree 'phy' already exists, can also use this:
312 AptxUtil.writePhylogenyToGraphicsFile( phy,
313 new File( "out.png" ),
316 GraphicsExportType.PNG,
319 catch ( final IOException e ) {
327 = Setting node/branch colors of a phylogenetic tree and writing it to a graphics file =
333 import java.awt.Color;
335 import java.io.IOException;
336 import java.util.HashMap;
337 import java.util.Map;
339 import org.forester.archaeopteryx.AptxUtil;
340 import org.forester.archaeopteryx.AptxUtil.GraphicsExportType;
341 import org.forester.archaeopteryx.Configuration;
342 import org.forester.archaeopteryx.Options;
343 import org.forester.archaeopteryx.TreeColorSet;
344 import org.forester.io.parsers.PhylogenyParser;
345 import org.forester.io.parsers.util.ParserUtils;
346 import org.forester.phylogeny.Phylogeny;
347 import org.forester.phylogeny.PhylogenyMethods;
348 import org.forester.phylogeny.PhylogenyNode;
349 import org.forester.phylogeny.data.BranchColor;
350 import org.forester.phylogeny.data.BranchWidth;
351 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
353 public class phylo2coloredgraphics {
355 public static void main( final String[] args ) {
357 // Reading-in of a tree from a file.
358 final File treefile = new File( "my_tree.nh" );
359 final PhylogenyParser parser = ParserUtils.createParserDependingOnFileType( treefile, true );
360 final Phylogeny phy = PhylogenyMethods.readPhylogenies( parser, treefile )[ 0 ];
361 // Creating a node name -> color map.
362 final Map<String, Color> colors = new HashMap<String, Color>();
363 colors.put( "Primates", new Color( 255, 255, 0 ) );
364 colors.put( "PANTR", new Color( 255, 0, 255 ) );
365 colors.put( "HUMAN", new Color( 255, 0, 0 ) );
366 colors.put( "RAT", new Color( 155, 0, 0 ) );
367 colors.put( "MOUSE", new Color( 55, 155, 0 ) );
368 colors.put( "CAVPO", new Color( 155, 155, 0 ) );
369 colors.put( "LOTGI", new Color( 155, 155, 255 ) );
371 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
372 final PhylogenyNode n = it.next();
373 if ( colors.containsKey( n.getName() ) ) {
374 n.getBranchData().setBranchColor( new BranchColor( colors.get( n.getName() ) ) );
375 // To make colored subtrees thicker:
376 n.getBranchData().setBranchWidth( new BranchWidth( 4 ) );
379 // Setting up a configuration object.
380 final Configuration config = new Configuration();
381 config.putDisplayColors( TreeColorSet.BACKGROUND, new Color( 255, 255, 255 ) );
382 config.putDisplayColors( TreeColorSet.BRANCH, new Color( 0, 0, 0 ) );
383 config.putDisplayColors( TreeColorSet.TAXONOMY, new Color( 0, 0, 0 ) );
384 config.setPhylogenyGraphicsType( Options.PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
385 config.setTaxonomyColorize( false );
386 config.setColorizeBranches( true );
387 config.setUseBranchesWidths( true );
388 config.setDisplayTaxonomyCode( false );
389 // Writing to a graphics file.
390 AptxUtil.writePhylogenyToGraphicsFile( phy,
391 new File( "out.png" ),
394 GraphicsExportType.PNG,
397 catch ( final IOException e ) {