cb719e63007adaaf5026c95625904f366100aa36
[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.parsers.util.ParserUtils;
29 import org.forester.io.writers.PhylogenyWriter;
30 import org.forester.phylogeny.Phylogeny;
31 import org.forester.phylogeny.PhylogenyMethods;
32 import org.forester.util.ForesterUtil;
33
34 public class Example {
35
36     public static void main( final String[] args ) {
37         // Reading-in of (a) tree(s) from a file.
38         final File treefile = new File( "/path/to/tree.xml" );
39         PhylogenyParser parser = null;
40         try {
41             parser = ParserUtils.createParserDependingOnFileType( treefile, true );
42         }
43         catch ( final IOException e ) {
44             e.printStackTrace();
45         }
46         Phylogeny[] phys = null;
47         try {
48             phys = PhylogenyMethods.readPhylogenies( parser, treefile );
49         }
50         catch ( final IOException e ) {
51             e.printStackTrace();
52         }
53         // Writing trees to a file.
54         final File outfile = new File( "/path/to/out_tree.xml" );
55         try {
56             final PhylogenyWriter writer = new PhylogenyWriter();
57             writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
58         }
59         catch ( final Exception e ) {
60             e.printStackTrace();
61         }
62     }
63 }
64
65 }}}
66
67
68
69 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
70
71 This needs file "forester.jar" to be in the class-path.
72
73 {{{
74
75 package examples;
76
77 import java.io.File;
78 import java.io.IOException;
79
80 import org.forester.archaeopteryx.Archaeopteryx;
81 import org.forester.io.parsers.util.ParserUtils;
82 import org.forester.io.parsers.PhylogenyParser;
83 import org.forester.phylogeny.Phylogeny;
84 import org.forester.phylogeny.PhylogenyMethods;
85
86 public class Example {
87
88     public static void main( final String[] args ) {
89         // Reading-in of (a) tree(s) from a file.
90         final File treefile = new File( "/path/to/tree.xml" );
91         PhylogenyParser parser = null;
92         try {
93             parser = ParserUtils.createParserDependingOnFileType( treefile, true );
94         }
95         catch ( final IOException e ) {
96             e.printStackTrace();
97         }
98         Phylogeny[] phys = null;
99         try {
100             phys = PhylogenyMethods.readPhylogenies( parser, treefile );
101         }
102         catch ( final IOException e ) {
103             e.printStackTrace();
104         }
105         // Display of the tree(s) with Archaeopteryx.
106         Archaeopteryx.createApplication( phys );
107     }
108 }
109
110 }}}
111
112
113
114
115 = Creating a new tree and displaying it with Archaeopteryx =
116
117 This needs file "forester.jar" to be in the class-path.
118
119 {{{
120
121 package examples;
122
123 import org.forester.archaeopteryx.Archaeopteryx;
124 import org.forester.phylogeny.Phylogeny;
125 import org.forester.phylogeny.PhylogenyNode;
126
127 public class Example {
128
129     public static void main( final String[] args ) {
130         // Creating a new rooted tree with two external nodes.
131         final Phylogeny phy = new Phylogeny();
132         final PhylogenyNode root = new PhylogenyNode();
133         final PhylogenyNode d1 = new PhylogenyNode();
134         final PhylogenyNode d2 = new PhylogenyNode();
135         root.setName( "root" );
136         d1.setName( "descendant 1" );
137         d2.setName( "descendant 2" );
138         root.addAsChild( d1 );
139         root.addAsChild( d2 );
140         phy.setRoot( root );
141         phy.setRooted( true );
142         // Displaying the newly created tree with Archaeopteryx.
143         Archaeopteryx.createApplication( phy );
144     }
145 }
146
147 }}}
148
149
150
151
152
153
154 = Using iterators to visit tree nodes in certain orders =
155
156 This needs file "forester.jar" to be in the class-path.
157
158 {{{
159
160 package examples;
161
162 import org.forester.phylogeny.Phylogeny;
163 import org.forester.phylogeny.PhylogenyNode;
164 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
165
166 public class Example {
167
168     public static void main( final String[] args ) {
169         // Creating a new rooted tree with four external nodes.
170         final Phylogeny phy = new Phylogeny();
171         final PhylogenyNode root = new PhylogenyNode();
172         final PhylogenyNode d1 = new PhylogenyNode();
173         final PhylogenyNode d2 = new PhylogenyNode();
174         final PhylogenyNode d11 = new PhylogenyNode();
175         final PhylogenyNode d12 = new PhylogenyNode();
176         root.setName( "root" );
177         d1.setName( "1" );
178         d2.setName( "2" );
179         d11.setName( "1-1" );
180         d12.setName( "1-2" );
181         root.addAsChild( d1 );
182         root.addAsChild( d2 );
183         d2.addAsChild( d11 );
184         d2.addAsChild( d12 );
185         phy.setRoot( root );
186         phy.setRooted( true );
187         // Using a variety of iterators to visit the nodes of the newly created tree.
188         System.out.println( "post-order:" );
189         for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
190             System.out.println( it.next().getName() );
191         }
192         System.out.println( "pre-order:" );
193         for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
194             System.out.println( it.next().getName() );
195         }
196         System.out.println( "level-order:" );
197         for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
198             System.out.println( it.next().getName() );
199         }
200         System.out.println( "external nodes only:" );
201         for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
202             System.out.println( it.next().getName() );
203         }
204     }
205 }
206
207 }}}
208
209
210
211
212
213
214
215
216
217 = Creating a basic gene tree (with branch lengths) =
218
219 This needs file "forester.jar" to be in the class-path.
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 }}}