6123fa91da0a44d0110280e65d8f540a1c1b79d3
[jalview.git] / wiki / forester.wiki
1 = forester Tutorial and Examples =
2 <wiki:toc max_depth="3" />
3
4 = Introduction =
5
6 Under development!
7
8 This contains documentation, tutorials, and examples for [http://www.phylosoft.org/forester/ forester].
9
10 Documentation for [http://www.phylosoft.org/archaeopteryx/ Archaeopteryx] can be found [Archaeopteryx here].
11
12 *All examples require jar-file "forester.jar" to be in the class-path.*
13
14 Download: http://code.google.com/p/forester/downloads/list
15
16 Author: [http://www.cmzmasek.net/ Christian M Zmasek], Sanford-Burnham Medical Research Institute
17
18  
19 Copyright (C) 2012 Christian M Zmasek. All rights reserved.
20
21
22
23 = Reading and writing of phylogenetic trees =
24
25
26
27 {{{
28
29 package examples;
30
31 import java.io.File;
32 import java.io.IOException;
33
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;
40
41 public class Example {
42
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;
47         try {
48             parser = ParserUtils.createParserDependingOnFileType( treefile, true );
49         }
50         catch ( final IOException e ) {
51             e.printStackTrace();
52         }
53         Phylogeny[] phys = null;
54         try {
55             phys = PhylogenyMethods.readPhylogenies( parser, treefile );
56         }
57         catch ( final IOException e ) {
58             e.printStackTrace();
59         }
60         // Writing trees to a file.
61         final File outfile = new File( "/path/to/out_tree.xml" );
62         try {
63             final PhylogenyWriter writer = new PhylogenyWriter();
64             writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
65         }
66         catch ( final Exception e ) {
67             e.printStackTrace();
68         }
69     }
70 }
71
72 }}}
73
74
75
76 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
77
78
79 {{{
80
81 package examples;
82
83 import java.io.File;
84 import java.io.IOException;
85
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;
91
92 public class Example {
93
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;
98         try {
99             parser = ParserUtils.createParserDependingOnFileType( treefile, true );
100         }
101         catch ( final IOException e ) {
102             e.printStackTrace();
103         }
104         Phylogeny[] phys = null;
105         try {
106             phys = PhylogenyMethods.readPhylogenies( parser, treefile );
107         }
108         catch ( final IOException e ) {
109             e.printStackTrace();
110         }
111         // Display of the tree(s) with Archaeopteryx.
112         Archaeopteryx.createApplication( phys );
113     }
114 }
115
116 }}}
117
118
119
120
121 = Creating a new tree and displaying it with Archaeopteryx =
122
123
124 {{{
125
126 package examples;
127
128 import org.forester.archaeopteryx.Archaeopteryx;
129 import org.forester.phylogeny.Phylogeny;
130 import org.forester.phylogeny.PhylogenyNode;
131
132 public class Example {
133
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 );
145         phy.setRoot( root );
146         phy.setRooted( true );
147         // Displaying the newly created tree with Archaeopteryx.
148         Archaeopteryx.createApplication( phy );
149     }
150 }
151
152 }}}
153
154
155
156
157
158
159 = Using iterators to visit tree nodes in certain orders =
160
161 {{{
162
163 package examples;
164
165 import org.forester.phylogeny.Phylogeny;
166 import org.forester.phylogeny.PhylogenyNode;
167 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
168
169 public class Example {
170
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" );
180         d1.setName( "1" );
181         d2.setName( "2" );
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 );
188         phy.setRoot( root );
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() );
194         }
195         System.out.println( "pre-order:" );
196         for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
197             System.out.println( it.next().getName() );
198         }
199         System.out.println( "level-order:" );
200         for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
201             System.out.println( it.next().getName() );
202         }
203         System.out.println( "external nodes only:" );
204         for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
205             System.out.println( it.next().getName() );
206         }
207     }
208 }
209
210 }}}
211
212
213
214
215
216
217
218
219
220 = Creating a basic gene tree (with branch lengths) =
221
222
223 {{{
224
225 package examples;
226
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;
233
234 public class Example {
235
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 );
267         phy.setRoot( root );
268         phy.setRooted( true );
269         // Displaying the newly created tree with Archaeopteryx.
270         Archaeopteryx.createApplication( phy );
271     }
272 }
273
274 }}}
275
276 = Writing a phylogenetic tree to a graphics file (e.g. png, jpg) =
277
278 {{{
279
280 package examples;
281
282 import java.awt.Color;
283 import java.io.File;
284 import java.io.IOException;
285
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;
291
292 public class phylo2graphics {
293
294     public static void main( final String[] args ) {
295         try {
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" ),
305                                                    1000,
306                                                    1000,
307                                                    GraphicsExportType.PNG,
308                                                    config );
309             // If the tree 'phy' already exists, can also use this:
310             AptxUtil.writePhylogenyToGraphicsFile( phy,
311                                                    new File( "out.png" ),
312                                                    1000,
313                                                    1000,
314                                                    GraphicsExportType.PNG,
315                                                    config );
316         }
317         catch ( final IOException e ) {
318             e.printStackTrace();
319         }
320     }
321 }
322
323 }}}
324
325 = Setting node/branch colors of a phylogenetic tree and writing it to a graphics file =
326
327 {{{
328
329 package examples;
330
331 import java.awt.Color;
332 import java.io.File;
333 import java.io.IOException;
334 import java.util.HashMap;
335 import java.util.Map;
336
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;
350
351 public class phylo2coloredgraphics {
352
353     public static void main( final String[] args ) {
354         try {
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 ) );
368             // Setting colors.
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 ) );
375                 }
376             }
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" ),
390                                                    1300,
391                                                    1300,
392                                                    GraphicsExportType.PNG,
393                                                    config );
394         }
395         catch ( final IOException e ) {
396             e.printStackTrace();
397         }
398     }
399 }
400
401 }}}