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