33be88834d8cb50c4fd2c06d3858cad7e1f6b5dd
[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 Author: [http://www.cmzmasek.net/ Christian M Zmasek], Sanford-Burnham Medical Research Institute
11
12  
13 Copyright (C) 2011 Christian M Zmasek. All rights reserved.
14
15
16 = Reading and writing of phylogenetic trees =
17
18 This needs file "forester.jar" to be in the class-path.
19
20 {{{
21
22 package examples;
23
24 import java.io.File;
25 import java.io.IOException;
26
27 import org.forester.io.parsers.PhylogenyParser;
28 import org.forester.io.writers.PhylogenyWriter;
29 import org.forester.phylogeny.Phylogeny;
30 import org.forester.util.ForesterUtil;
31
32 public class Example {
33
34     public static void main( final String[] args ) {
35         // Reading-in of (a) tree(s) from a file.
36         final File treefile = new File( "/path/to/tree.xml" );
37         PhylogenyParser parser = null;
38         try {
39             parser = ForesterUtil.createParserDependingOnFileType( treefile, true );
40         }
41         catch ( final IOException e ) {
42             e.printStackTrace();
43         }
44         Phylogeny[] phys = null;
45         try {
46             phys = ForesterUtil.readPhylogenies( parser, treefile );
47         }
48         catch ( final IOException e ) {
49             e.printStackTrace();
50         }
51         // Writing trees to a file.
52         final File outfile = new File( "/path/to/out_tree.xml" );
53         try {
54             final PhylogenyWriter writer = new PhylogenyWriter();
55             writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
56         }
57         catch ( final Exception e ) {
58             e.printStackTrace();
59         }
60     }
61 }
62
63 }}}
64
65
66
67 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
68
69 This needs file "forester.jar" to be in the class-path.
70
71 {{{
72
73 package examples;
74
75 import java.io.File;
76 import java.io.IOException;
77
78 import org.forester.archaeopteryx.Archaeopteryx;
79 import org.forester.io.parsers.PhylogenyParser;
80 import org.forester.phylogeny.Phylogeny;
81 import org.forester.util.ForesterUtil;
82
83 public class Example {
84
85     public static void main( final String[] args ) {
86         // Reading-in of (a) tree(s) from a file.
87         final File treefile = new File( "/path/to/tree.xml" );
88         PhylogenyParser parser = null;
89         try {
90             parser = ForesterUtil.createParserDependingOnFileType( treefile, true );
91         }
92         catch ( final IOException e ) {
93             e.printStackTrace();
94         }
95         Phylogeny[] phys = null;
96         try {
97             phys = ForesterUtil.readPhylogenies( parser, treefile );
98         }
99         catch ( final IOException e ) {
100             e.printStackTrace();
101         }
102         // Display of the tree(s) with Archaeopteryx.
103         Archaeopteryx.createApplication( phys );
104     }
105 }
106
107 }}}
108
109
110
111
112 = Creating a new tree and displaying it with Archaeopteryx =
113
114 This needs file "forester.jar" to be in the class-path.
115
116 {{{
117
118 package examples;
119
120 import org.forester.archaeopteryx.Archaeopteryx;
121 import org.forester.phylogeny.Phylogeny;
122 import org.forester.phylogeny.PhylogenyNode;
123
124 public class Example {
125
126     public static void main( final String[] args ) {
127         // Creating a new rooted tree with two external nodes.
128         final Phylogeny phy = new Phylogeny();
129         final PhylogenyNode root = new PhylogenyNode();
130         final PhylogenyNode d1 = new PhylogenyNode();
131         final PhylogenyNode d2 = new PhylogenyNode();
132         root.setName( "root" );
133         d1.setName( "descendant 1" );
134         d2.setName( "descendant 2" );
135         root.addAsChild( d1 );
136         root.addAsChild( d2 );
137         phy.setRoot( root );
138         phy.setRooted( true );
139         // Displaying the newly created tree with Archaeopteryx.
140         Archaeopteryx.createApplication( phy );
141     }
142 }
143
144 }}}
145
146
147
148
149
150
151 = Using iterators to visit tree nodes in certain orders =
152
153 This needs file "forester.jar" to be in the class-path.
154
155 {{{
156
157 package examples;
158
159 import org.forester.phylogeny.Phylogeny;
160 import org.forester.phylogeny.PhylogenyNode;
161 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
162
163 public class Example {
164
165     public static void main( final String[] args ) {
166         // Creating a new rooted tree with four external nodes.
167         final Phylogeny phy = new Phylogeny();
168         final PhylogenyNode root = new PhylogenyNode();
169         final PhylogenyNode d1 = new PhylogenyNode();
170         final PhylogenyNode d2 = new PhylogenyNode();
171         final PhylogenyNode d11 = new PhylogenyNode();
172         final PhylogenyNode d12 = new PhylogenyNode();
173         root.setName( "root" );
174         d1.setName( "1" );
175         d2.setName( "2" );
176         d11.setName( "1-1" );
177         d12.setName( "1-2" );
178         root.addAsChild( d1 );
179         root.addAsChild( d2 );
180         d2.addAsChild( d11 );
181         d2.addAsChild( d12 );
182         phy.setRoot( root );
183         phy.setRooted( true );
184         // Using a variety of iterators to visit the nodes of the newly created tree.
185         System.out.println( "post-order:" );
186         for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
187             System.out.println( it.next().getName() );
188         }
189         System.out.println( "pre-order:" );
190         for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
191             System.out.println( it.next().getName() );
192         }
193         System.out.println( "level-order:" );
194         for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
195             System.out.println( it.next().getName() );
196         }
197         System.out.println( "external nodes only:" );
198         for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
199             System.out.println( it.next().getName() );
200         }
201     }
202 }
203
204 }}}
205
206
207
208
209
210
211
212
213
214 = Creating a basic gene tree (with branch lengths) =
215
216 This needs file "forester.jar" to be in the class-path.
217
218 {{{
219
220 package examples;
221
222 import org.forester.archaeopteryx.Archaeopteryx;
223 import org.forester.phylogeny.Phylogeny;
224 import org.forester.phylogeny.PhylogenyNode;
225 import org.forester.phylogeny.data.Sequence;
226 import org.forester.phylogeny.data.Taxonomy;
227
228 public class Example {
229
230     public static void main( final String[] args ) {
231         // Creating a new rooted tree with two external nodes.
232         final Phylogeny phy = new Phylogeny();
233         final PhylogenyNode root = new PhylogenyNode();
234         final PhylogenyNode d1 = new PhylogenyNode();
235         final PhylogenyNode d2 = new PhylogenyNode();
236         // Setting of distances.
237         d1.setDistanceToParent( 1.2 );
238         d2.setDistanceToParent( 2.4 );
239         // Adding species information.
240         final Taxonomy t1 = new Taxonomy();
241         t1.setScientificName( "Nematostella vectensis" );
242         d1.getNodeData().addTaxonomy( t1 );
243         final Taxonomy t2 = new Taxonomy();
244         t2.setScientificName( "Monosiga brevicollis" );
245         d2.getNodeData().addTaxonomy( t2 );
246         // Adding gene names.
247         final Sequence s1 = new Sequence();
248         s1.setName( "Bcl-2" );
249         d1.getNodeData().addSequence( s1 );
250         final Sequence s2 = new Sequence();
251         s2.setName( "Bcl-2" );
252         d2.getNodeData().addSequence( s2 );
253         // Root is a speciation.
254         final Event ev = new Event();
255         ev.setSpeciations( 1 );
256         ev.setDuplications( 0 );
257         root.getNodeData().setEvent( ev );
258         // Putting the tree together.
259         root.addAsChild( d1 );
260         root.addAsChild( d2 );
261         phy.setRoot( root );
262         phy.setRooted( true );
263         // Displaying the newly created tree with Archaeopteryx.
264         Archaeopteryx.createApplication( phy );
265     }
266 }
267
268 }}}