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