10e4c3a20ec5853ef5542e7d9646be170c5d4427
[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 Author: [http://www.cmzmasek.net/ Christian M Zmasek], Sanford-Burnham Medical Research Institute
13
14  
15 Copyright (C) 2011 Christian M Zmasek. All rights reserved.
16
17
18
19 = Reading and writing of phylogenetic trees =
20
21
22
23 {{{
24
25 package examples;
26
27 import java.io.File;
28 import java.io.IOException;
29
30 import org.forester.io.parsers.PhylogenyParser;
31 import org.forester.io.parsers.util.ParserUtils;
32 import org.forester.io.writers.PhylogenyWriter;
33 import org.forester.phylogeny.Phylogeny;
34 import org.forester.phylogeny.PhylogenyMethods;
35 import org.forester.util.ForesterUtil;
36
37 public class Example {
38
39     public static void main( final String[] args ) {
40         // Reading-in of (a) tree(s) from a file.
41         final File treefile = new File( "/path/to/tree.xml" );
42         PhylogenyParser parser = null;
43         try {
44             parser = ParserUtils.createParserDependingOnFileType( treefile, true );
45         }
46         catch ( final IOException e ) {
47             e.printStackTrace();
48         }
49         Phylogeny[] phys = null;
50         try {
51             phys = PhylogenyMethods.readPhylogenies( parser, treefile );
52         }
53         catch ( final IOException e ) {
54             e.printStackTrace();
55         }
56         // Writing trees to a file.
57         final File outfile = new File( "/path/to/out_tree.xml" );
58         try {
59             final PhylogenyWriter writer = new PhylogenyWriter();
60             writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
61         }
62         catch ( final Exception e ) {
63             e.printStackTrace();
64         }
65     }
66 }
67
68 }}}
69
70
71
72 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
73
74 This needs file "forester.jar" to be in the class-path.
75
76 {{{
77
78 package examples;
79
80 import java.io.File;
81 import java.io.IOException;
82
83 import org.forester.archaeopteryx.Archaeopteryx;
84 import org.forester.io.parsers.util.ParserUtils;
85 import org.forester.io.parsers.PhylogenyParser;
86 import org.forester.phylogeny.Phylogeny;
87 import org.forester.phylogeny.PhylogenyMethods;
88
89 public class Example {
90
91     public static void main( final String[] args ) {
92         // Reading-in of (a) tree(s) from a file.
93         final File treefile = new File( "/path/to/tree.xml" );
94         PhylogenyParser parser = null;
95         try {
96             parser = ParserUtils.createParserDependingOnFileType( treefile, true );
97         }
98         catch ( final IOException e ) {
99             e.printStackTrace();
100         }
101         Phylogeny[] phys = null;
102         try {
103             phys = PhylogenyMethods.readPhylogenies( parser, treefile );
104         }
105         catch ( final IOException e ) {
106             e.printStackTrace();
107         }
108         // Display of the tree(s) with Archaeopteryx.
109         Archaeopteryx.createApplication( phys );
110     }
111 }
112
113 }}}
114
115
116
117
118 = Creating a new tree and displaying it with Archaeopteryx =
119
120 This needs file "forester.jar" to be in the class-path.
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 This needs file "forester.jar" to be in the class-path.
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 This needs file "forester.jar" to be in the class-path.
223
224 {{{
225
226 package examples;
227
228 import org.forester.archaeopteryx.Archaeopteryx;
229 import org.forester.phylogeny.Phylogeny;
230 import org.forester.phylogeny.PhylogenyNode;
231 import org.forester.phylogeny.data.Event;
232 import org.forester.phylogeny.data.Sequence;
233 import org.forester.phylogeny.data.Taxonomy;
234
235 public class Example {
236
237     public static void main( final String[] args ) {
238         // Creating a new rooted tree with two external nodes.
239         final Phylogeny phy = new Phylogeny();
240         final PhylogenyNode root = new PhylogenyNode();
241         final PhylogenyNode d1 = new PhylogenyNode();
242         final PhylogenyNode d2 = new PhylogenyNode();
243         // Setting of distances.
244         d1.setDistanceToParent( 1.2 );
245         d2.setDistanceToParent( 2.4 );
246         // Adding species information.
247         final Taxonomy t1 = new Taxonomy();
248         t1.setScientificName( "Nematostella vectensis" );
249         d1.getNodeData().addTaxonomy( t1 );
250         final Taxonomy t2 = new Taxonomy();
251         t2.setScientificName( "Monosiga brevicollis" );
252         d2.getNodeData().addTaxonomy( t2 );
253         // Adding gene names.
254         final Sequence s1 = new Sequence();
255         s1.setName( "Bcl-2" );
256         d1.getNodeData().addSequence( s1 );
257         final Sequence s2 = new Sequence();
258         s2.setName( "Bcl-2" );
259         d2.getNodeData().addSequence( s2 );
260         // Root is a speciation.
261         final Event ev = new Event();
262         ev.setSpeciations( 1 );
263         ev.setDuplications( 0 );
264         root.getNodeData().setEvent( ev );
265         // Putting the tree together.
266         root.addAsChild( d1 );
267         root.addAsChild( d2 );
268         phy.setRoot( root );
269         phy.setRooted( true );
270         // Displaying the newly created tree with Archaeopteryx.
271         Archaeopteryx.createApplication( phy );
272     }
273 }
274
275 }}}