9eaf25f59a3931a8eca8d448772684adeb6c56e6
[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 Documentation, tutorial, and examples for [http://www.phylosoft.org/forester/ forester].
9
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.*
11
12 Download: http://code.google.com/p/forester/downloads/list
13
14 Author: [http://www.cmzmasek.net/ Christian M Zmasek], Sanford-Burnham Medical Research Institute
15
16  
17 Copyright (C) 2011 Christian M Zmasek. All rights reserved.
18
19
20
21 = Reading and writing of phylogenetic trees =
22
23
24
25 {{{
26
27 package examples;
28
29 import java.io.File;
30 import java.io.IOException;
31
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;
38
39 public class Example {
40
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;
45         try {
46             parser = ParserUtils.createParserDependingOnFileType( treefile, true );
47         }
48         catch ( final IOException e ) {
49             e.printStackTrace();
50         }
51         Phylogeny[] phys = null;
52         try {
53             phys = PhylogenyMethods.readPhylogenies( parser, treefile );
54         }
55         catch ( final IOException e ) {
56             e.printStackTrace();
57         }
58         // Writing trees to a file.
59         final File outfile = new File( "/path/to/out_tree.xml" );
60         try {
61             final PhylogenyWriter writer = new PhylogenyWriter();
62             writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
63         }
64         catch ( final Exception e ) {
65             e.printStackTrace();
66         }
67     }
68 }
69
70 }}}
71
72
73
74 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
75
76
77 {{{
78
79 package examples;
80
81 import java.io.File;
82 import java.io.IOException;
83
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;
89
90 public class Example {
91
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;
96         try {
97             parser = ParserUtils.createParserDependingOnFileType( treefile, true );
98         }
99         catch ( final IOException e ) {
100             e.printStackTrace();
101         }
102         Phylogeny[] phys = null;
103         try {
104             phys = PhylogenyMethods.readPhylogenies( parser, treefile );
105         }
106         catch ( final IOException e ) {
107             e.printStackTrace();
108         }
109         // Display of the tree(s) with Archaeopteryx.
110         Archaeopteryx.createApplication( phys );
111     }
112 }
113
114 }}}
115
116
117
118
119 = Creating a new tree and displaying it with Archaeopteryx =
120
121
122 {{{
123
124 package examples;
125
126 import org.forester.archaeopteryx.Archaeopteryx;
127 import org.forester.phylogeny.Phylogeny;
128 import org.forester.phylogeny.PhylogenyNode;
129
130 public class Example {
131
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 );
143         phy.setRoot( root );
144         phy.setRooted( true );
145         // Displaying the newly created tree with Archaeopteryx.
146         Archaeopteryx.createApplication( phy );
147     }
148 }
149
150 }}}
151
152
153
154
155
156
157 = Using iterators to visit tree nodes in certain orders =
158
159 {{{
160
161 package examples;
162
163 import org.forester.phylogeny.Phylogeny;
164 import org.forester.phylogeny.PhylogenyNode;
165 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
166
167 public class Example {
168
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" );
178         d1.setName( "1" );
179         d2.setName( "2" );
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 );
186         phy.setRoot( root );
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() );
192         }
193         System.out.println( "pre-order:" );
194         for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
195             System.out.println( it.next().getName() );
196         }
197         System.out.println( "level-order:" );
198         for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
199             System.out.println( it.next().getName() );
200         }
201         System.out.println( "external nodes only:" );
202         for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
203             System.out.println( it.next().getName() );
204         }
205     }
206 }
207
208 }}}
209
210
211
212
213
214
215
216
217
218 = Creating a basic gene tree (with branch lengths) =
219
220
221 {{{
222
223 package examples;
224
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;
231
232 public class Example {
233
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 );
265         phy.setRoot( root );
266         phy.setRooted( true );
267         // Displaying the newly created tree with Archaeopteryx.
268         Archaeopteryx.createApplication( phy );
269     }
270 }
271
272 }}}
273
274 = Writing a phylogenetic tree to a graphics file (e.g. png, jpg) =
275
276 {{{
277
278 package examples;
279
280 import java.awt.Color;
281 import java.io.File;
282 import java.io.IOException;
283
284 import org.forester.archaeopteryx.AptxUtil;
285 import org.forester.archaeopteryx.AptxUtil.GraphicsExportType;
286 import org.forester.archaeopteryx.Configuration;
287 import org.forester.archaeopteryx.Options;
288 import org.forester.archaeopteryx.TreeColorSet;
289
290 public class phylo2graphics {
291
292     public static void main( final String[] args ) {
293         try {
294             final Configuration config = new Configuration();
295             // Could also read a configuration file with:
296             // Configuration config = new Configuration("my_configuration_file.txt", false, false, false);
297             config.putDisplayColors( TreeColorSet.BACKGROUND, new Color( 255, 255, 255 ) );
298             config.putDisplayColors( TreeColorSet.BRANCH, new Color( 0, 0, 0 ) );
299             config.putDisplayColors( TreeColorSet.TAXONOMY, new Color( 0, 0, 0 ) );
300             config.setPhylogenyGraphicsType( Options.PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
301             AptxUtil.writePhylogenyToGraphicsFile( new File( "my_tree.xml" ),
302                                                    new File( "my_tree_graphics.png" ),
303                                                    1000,
304                                                    1000,
305                                                    GraphicsExportType.PNG,
306                                                    config );
307             // If the tree 'phy' already exists, can also use this:
308             AptxUtil.writePhylogenyToGraphicsFile( phy,
309                                                    new File( "out.png" ),
310                                                    1000,
311                                                    1000,
312                                                    GraphicsExportType.PNG,
313                                                    config );
314         }
315         catch ( final IOException e ) {
316             e.printStackTrace();
317         }
318     }
319 }
320
321 }}}
322
323 = Setting node/branch colors of a phylogenetic tree and writing it to a graphics file =
324
325 {{{
326
327 package examples;
328
329
330
331 }}}