25708bfa727fead6b2f44d7a56b84f1fb7ad7a88
[jalview.git] / wiki / forester.wiki
1 = Introduction =
2
3 Under development!
4
5 Documentation, tutorial, and examples for [http://www.phylosoft.org/forester/ forester].
6
7 Author: [http://www.cmzmasek.net/ Christian M Zmasek], Sanford-Burnham Medical Research Institute
8
9  
10 Copyright (C) 2011 Christian M Zmasek. All rights reserved.
11
12
13 = Reading and writing of phylogenetic trees =
14
15 This needs file "forester.jar" to be in the class-path.
16
17 {{{
18
19 package examples;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import org.forester.io.parsers.PhylogenyParser;
25 import org.forester.io.writers.PhylogenyWriter;
26 import org.forester.phylogeny.Phylogeny;
27 import org.forester.util.ForesterUtil;
28
29 public class Example4 {
30
31     public static void main( final String[] args ) {
32         // Reading-in of (a) tree(s) from a file.
33         final File treefile = new File( "/path/to/tree.xml" );
34         PhylogenyParser parser = null;
35         try {
36             parser = ForesterUtil.createParserDependingOnFileType( treefile, true );
37         }
38         catch ( final IOException e ) {
39             e.printStackTrace();
40         }
41         Phylogeny[] phys = null;
42         try {
43             phys = ForesterUtil.readPhylogenies( parser, treefile );
44         }
45         catch ( final IOException e ) {
46             e.printStackTrace();
47         }
48         // Writing trees to a file.
49         final File outfile = new File( "/path/to/out_tree.xml" );
50         try {
51             final PhylogenyWriter writer = new PhylogenyWriter();
52             writer.toPhyloXML( phys, 0, outfile, ForesterUtil.LINE_SEPARATOR );
53         }
54         catch ( final Exception e ) {
55             e.printStackTrace();
56         }
57     }
58 }
59
60 }}}
61
62
63
64 = Reading of phylogenetic trees and displaying them with Archaeopteryx =
65
66 This needs file "forester.jar" to be in the class-path.
67
68 {{{
69
70 package examples;
71
72 import java.io.File;
73 import java.io.IOException;
74
75 import org.forester.archaeopteryx.Archaeopteryx;
76 import org.forester.io.parsers.PhylogenyParser;
77 import org.forester.phylogeny.Phylogeny;
78 import org.forester.util.ForesterUtil;
79
80 public class Example1 {
81
82     public static void main( final String[] args ) {
83         // Reading-in of (a) tree(s) from a file.
84         final File treefile = new File( "/path/to/tree.xml" );
85         PhylogenyParser parser = null;
86         try {
87             parser = ForesterUtil.createParserDependingOnFileType( treefile, true );
88         }
89         catch ( final IOException e ) {
90             e.printStackTrace();
91         }
92         Phylogeny[] phys = null;
93         try {
94             phys = ForesterUtil.readPhylogenies( parser, treefile );
95         }
96         catch ( final IOException e ) {
97             e.printStackTrace();
98         }
99         // Display of the tree(s) with Archaeopteryx.
100         Archaeopteryx.createApplication( phys );
101     }
102 }
103
104 }}}
105
106
107
108
109 = Creating a new tree and displaying it with Archaeopteryx =
110
111 This needs file "forester.jar" to be in the class-path.
112
113 {{{
114
115 package examples;
116
117 import org.forester.archaeopteryx.Archaeopteryx;
118 import org.forester.phylogeny.Phylogeny;
119 import org.forester.phylogeny.PhylogenyNode;
120
121 public class Example2 {
122
123     public static void main( final String[] args ) {
124         // Creating a new rooted tree with two external nodes.
125         final Phylogeny phy = new Phylogeny();
126         final PhylogenyNode root = new PhylogenyNode();
127         final PhylogenyNode d1 = new PhylogenyNode();
128         final PhylogenyNode d2 = new PhylogenyNode();
129         root.setName( "root" );
130         d1.setName( "descendant 1" );
131         d2.setName( "descendant 2" );
132         root.addAsChild( d1 );
133         root.addAsChild( d2 );
134         phy.setRoot( root );
135         phy.setRooted( true );
136         // Displaying the newly created tree with Archaeopteryx.
137         Archaeopteryx.createApplication( phy );
138     }
139 }
140
141 }}}
142
143
144
145
146
147
148 = Using iterators to visit tree nodes in certain orders=
149
150 This needs file "forester.jar" to be in the class-path.
151
152 {{{
153
154 package examples;
155
156 import org.forester.phylogeny.Phylogeny;
157 import org.forester.phylogeny.PhylogenyNode;
158 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
159
160 public class Example3 {
161
162     public static void main( final String[] args ) {
163         // Creating a new rooted tree with four external nodes.
164         final Phylogeny phy = new Phylogeny();
165         final PhylogenyNode root = new PhylogenyNode();
166         final PhylogenyNode d1 = new PhylogenyNode();
167         final PhylogenyNode d2 = new PhylogenyNode();
168         final PhylogenyNode d11 = new PhylogenyNode();
169         final PhylogenyNode d12 = new PhylogenyNode();
170         root.setName( "root" );
171         d1.setName( "1" );
172         d2.setName( "2" );
173         d11.setName( "1-1" );
174         d12.setName( "1-2" );
175         root.addAsChild( d1 );
176         root.addAsChild( d2 );
177         d2.addAsChild( d11 );
178         d2.addAsChild( d12 );
179         phy.setRoot( root );
180         phy.setRooted( true );
181         // Using a variety of iterators to visit the nodes of the newly created tree.
182         System.out.println( "post-order:" );
183         for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
184             System.out.println( it.next().getName() );
185         }
186         System.out.println( "pre-order:" );
187         for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
188             System.out.println( it.next().getName() );
189         }
190         System.out.println( "level-order:" );
191         for( final PhylogenyNodeIterator it = phy.iteratorLevelOrder(); it.hasNext(); ) {
192             System.out.println( it.next().getName() );
193         }
194         System.out.println( "external nodes only:" );
195         for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
196             System.out.println( it.next().getName() );
197         }
198     }
199 }
200
201 }}}