1f119e0b41e477b87d623e063e3889d0a1848b19
[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
75 {{{
76
77 package examples;
78
79 import java.io.File;
80 import java.io.IOException;
81
82 import org.forester.archaeopteryx.Archaeopteryx;
83 import org.forester.io.parsers.util.ParserUtils;
84 import org.forester.io.parsers.PhylogenyParser;
85 import org.forester.phylogeny.Phylogeny;
86 import org.forester.phylogeny.PhylogenyMethods;
87
88 public class Example {
89
90     public static void main( final String[] args ) {
91         // Reading-in of (a) tree(s) from a file.
92         final File treefile = new File( "/path/to/tree.xml" );
93         PhylogenyParser parser = null;
94         try {
95             parser = ParserUtils.createParserDependingOnFileType( treefile, true );
96         }
97         catch ( final IOException e ) {
98             e.printStackTrace();
99         }
100         Phylogeny[] phys = null;
101         try {
102             phys = PhylogenyMethods.readPhylogenies( parser, treefile );
103         }
104         catch ( final IOException e ) {
105             e.printStackTrace();
106         }
107         // Display of the tree(s) with Archaeopteryx.
108         Archaeopteryx.createApplication( phys );
109     }
110 }
111
112 }}}
113
114
115
116
117 = Creating a new tree and displaying it with Archaeopteryx =
118
119
120 {{{
121
122 package examples;
123
124 import org.forester.archaeopteryx.Archaeopteryx;
125 import org.forester.phylogeny.Phylogeny;
126 import org.forester.phylogeny.PhylogenyNode;
127
128 public class Example {
129
130     public static void main( final String[] args ) {
131         // Creating a new rooted tree with two external nodes.
132         final Phylogeny phy = new Phylogeny();
133         final PhylogenyNode root = new PhylogenyNode();
134         final PhylogenyNode d1 = new PhylogenyNode();
135         final PhylogenyNode d2 = new PhylogenyNode();
136         root.setName( "root" );
137         d1.setName( "descendant 1" );
138         d2.setName( "descendant 2" );
139         root.addAsChild( d1 );
140         root.addAsChild( d2 );
141         phy.setRoot( root );
142         phy.setRooted( true );
143         // Displaying the newly created tree with Archaeopteryx.
144         Archaeopteryx.createApplication( phy );
145     }
146 }
147
148 }}}
149
150
151
152
153
154
155 = Using iterators to visit tree nodes in certain orders =
156
157 {{{
158
159 package examples;
160
161 import org.forester.phylogeny.Phylogeny;
162 import org.forester.phylogeny.PhylogenyNode;
163 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
164
165 public class Example {
166
167     public static void main( final String[] args ) {
168         // Creating a new rooted tree with four external nodes.
169         final Phylogeny phy = new Phylogeny();
170         final PhylogenyNode root = new PhylogenyNode();
171         final PhylogenyNode d1 = new PhylogenyNode();
172         final PhylogenyNode d2 = new PhylogenyNode();
173         final PhylogenyNode d11 = new PhylogenyNode();
174         final PhylogenyNode d12 = new PhylogenyNode();
175         root.setName( "root" );
176         d1.setName( "1" );
177         d2.setName( "2" );
178         d11.setName( "1-1" );
179         d12.setName( "1-2" );
180         root.addAsChild( d1 );
181         root.addAsChild( d2 );
182         d2.addAsChild( d11 );
183         d2.addAsChild( d12 );
184         phy.setRoot( root );
185         phy.setRooted( true );
186         // Using a variety of iterators to visit the nodes of the newly created tree.
187         System.out.println( "post-order:" );
188         for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
189             System.out.println( it.next().getName() );
190         }
191         System.out.println( "pre-order:" );
192         for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
193             System.out.println( it.next().getName() );
194         }
195         System.out.println( "level-order:" );
196         for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
197             System.out.println( it.next().getName() );
198         }
199         System.out.println( "external nodes only:" );
200         for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
201             System.out.println( it.next().getName() );
202         }
203     }
204 }
205
206 }}}
207
208
209
210
211
212
213
214
215
216 = Creating a basic gene tree (with branch lengths) =
217
218
219 {{{
220
221 package examples;
222
223 import org.forester.archaeopteryx.Archaeopteryx;
224 import org.forester.phylogeny.Phylogeny;
225 import org.forester.phylogeny.PhylogenyNode;
226 import org.forester.phylogeny.data.Event;
227 import org.forester.phylogeny.data.Sequence;
228 import org.forester.phylogeny.data.Taxonomy;
229
230 public class Example {
231
232     public static void main( final String[] args ) {
233         // Creating a new rooted tree with two external nodes.
234         final Phylogeny phy = new Phylogeny();
235         final PhylogenyNode root = new PhylogenyNode();
236         final PhylogenyNode d1 = new PhylogenyNode();
237         final PhylogenyNode d2 = new PhylogenyNode();
238         // Setting of distances.
239         d1.setDistanceToParent( 1.2 );
240         d2.setDistanceToParent( 2.4 );
241         // Adding species information.
242         final Taxonomy t1 = new Taxonomy();
243         t1.setScientificName( "Nematostella vectensis" );
244         d1.getNodeData().addTaxonomy( t1 );
245         final Taxonomy t2 = new Taxonomy();
246         t2.setScientificName( "Monosiga brevicollis" );
247         d2.getNodeData().addTaxonomy( t2 );
248         // Adding gene names.
249         final Sequence s1 = new Sequence();
250         s1.setName( "Bcl-2" );
251         d1.getNodeData().addSequence( s1 );
252         final Sequence s2 = new Sequence();
253         s2.setName( "Bcl-2" );
254         d2.getNodeData().addSequence( s2 );
255         // Root is a speciation.
256         final Event ev = new Event();
257         ev.setSpeciations( 1 );
258         ev.setDuplications( 0 );
259         root.getNodeData().setEvent( ev );
260         // Putting the tree together.
261         root.addAsChild( d1 );
262         root.addAsChild( d2 );
263         phy.setRoot( root );
264         phy.setRooted( true );
265         // Displaying the newly created tree with Archaeopteryx.
266         Archaeopteryx.createApplication( phy );
267     }
268 }
269
270 }}}