tag fourth (and hopefully last) alpha
[bioperl-live.git] / branch-1-6 / t / Tree / Tree.t
blob5f2209bdae5b14124c7d32923c602bb68d0b0bc6
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
6 BEGIN { 
7     use lib '.';
8     use Bio::Root::Test;
9     
10 #/maj    test_begin(-tests => 60);
11     test_begin(-tests => 62);   
12     use_ok('Bio::TreeIO');
15 my $verbose = test_debug();
17 my $treeio = Bio::TreeIO->new(-verbose => $verbose,
18                              -format => 'nhx',
19                              -file   => test_input_file('test.nhx'));
20 my $tree = $treeio->next_tree;
22 # tests for tags
23 ok ! $tree->has_tag('test');
24 is $tree->add_tag_value('test','a'), 1;
25 ok $tree->has_tag('test');
26 is $tree->add_tag_value('test','b'), 2;
27 my @tags = $tree->get_tag_values('test');
28 is scalar @tags, 2;
29 is scalar $tree->get_tag_values('test'), 'a', 'retrieve the first value';
30 is $tree->remove_tag('test2'), 0;
31 is $tree->remove_tag('test'), 1;
32 ok ! $tree->has_tag('test');
33 is $tree->set_tag_value('test',('a','b','c')), 3;
34 is $tree->remove_all_tags(), undef;
35 ok ! $tree->has_tag('test');
38 my @nodes = $tree->find_node('ADH2');
39 is(@nodes, 2,'Number of nodes that have ADH2 as name');
41 if( $verbose ) {
42     $treeio = Bio::TreeIO->new(-verbose => $verbose,
43                               -format => 'nhx',
44                               );
45     $treeio->write_tree($tree);
46     print "nodes are: \n",
47     join(", ", map {  $_->id . ":". (defined $_->branch_length ? 
48                                      $_->branch_length : '' ) } @nodes), "\n";
51 $treeio = Bio::TreeIO->new(-format => 'newick',
52                           -file   => test_input_file('test.nh'));
53 $tree = $treeio->next_tree;
56 if( $verbose ) { 
57     my $out = Bio::TreeIO->new(-format => 'tabtree');
58     
59     $out->write_tree($tree);
62 my @hADH = ( $tree->find_node('hADH1'),
63              $tree->find_node('hADH2') );
64 my ($n4) = $tree->find_node('yADH4');
66 is($tree->is_monophyletic(-nodes    => \@hADH,
67                           -outgroup => $n4),1,'Test Monophyly');
69 my @mixgroup = ( $tree->find_node('hADH1'),
70                  $tree->find_node('yADH2'),
71                  $tree->find_node('yADH3'),
72                  );
74 my ($iADHX) = $tree->find_node('iADHX');
76 # test height
77 is($iADHX->height, 0,'Height');
78 is($iADHX->depth,0.22,'Depth');
79 isnt( $tree->is_monophyletic(-nodes   => \@mixgroup,
80                             -outgroup=> $iADHX),1, 'non-monophyletic group');
82 # binary tree?
83 is $tree->is_binary, 0, 'not a binary tree';
84 is scalar $tree->get_nodes, 12, '12 nodes';
85 $tree->verbose(-1);
86 $tree->force_binary;
87 is $tree->is_binary, 1, 'after force_binary() it is';
88 is scalar $tree->get_nodes, 17, 'and there are more nodes (17)';
90 my $in = Bio::TreeIO->new(-format => 'newick',
91                          -fh     => \*DATA);
92 $tree = $in->next_tree;
93 my ($a,$b,$c,$d) = ( $tree->find_node('A'),
94                      $tree->find_node('B'),
95                      $tree->find_node('C'),
96                      $tree->find_node('D'));
98 is($tree->is_monophyletic(-nodes => [$b,$c],
99                           -outgroup => $d),1, 'B,C are Monophyletic');
101 is($tree->is_monophyletic(-nodes => [$b,$a],
102                           -outgroup => $d),1,'A,B are Monophyletic');
104 $tree = $in->next_tree;
105 my ($e,$f,$i);
106 ($a,$b,$c,$d,$e,$f,$i) = ( $tree->find_node('A'),
107                            $tree->find_node('B'),
108                            $tree->find_node('C'),
109                            $tree->find_node('D'),
110                            $tree->find_node('E'),
111                            $tree->find_node('F'),
112                            $tree->find_node('I'),
113                            );
114 isnt( $tree->is_monophyletic(-nodes => [$b,$f],
115                             -outgroup => $d),1,'B,F are not Monophyletic' );
117 is($tree->is_monophyletic(-nodes => [$b,$a],
118                           -outgroup => $f),1, 'A,B are Monophyletic');
120 # test for paraphyly
122 isnt(  $tree->is_paraphyletic(-nodes => [$a,$b,$c],
123                            -outgroup => $d), 1,'A,B,C are not Monophyletic w D as outgroup');
125 is(  $tree->is_paraphyletic(-nodes => [$a,$f,$e],
126                            -outgroup => $i), 1, 'A,F,E are monophyletic with I as outgroup');
129 # test for rerooting the tree
130 my $out = Bio::TreeIO->new(-format => 'newick', 
131                            -fh => \*STDERR, 
132                            -noclose => 1);
133 $tree = $in->next_tree;
134 $tree->verbose( -1 ) unless $verbose;
135 my $node_cnt_orig = scalar($tree->get_nodes);
136 # reroot on an internal node: should work fine
137 $a = $tree->find_node('A');
138 # removing node_count checks because re-rooting can change the
139 # number of internal nodes (if it is done correctly)
140 my $total_length_orig = $tree->total_branch_length;
141 is $tree->total_branch_length, $tree->subtree_length, 
142     "subtree_length() without attributes is an alias to total_branch_lenght()";
143 cmp_ok($total_length_orig, '>',$tree->subtree_length($a->ancestor), 
144        'Length of the tree is larger that lenght of a subtree');
145 $out->write_tree($tree) if $verbose;
146 is($tree->reroot($a),1, 'Can re-root with A as outgroup');
147 $out->write_tree($tree) if $verbose;
148 is($node_cnt_orig, scalar($tree->get_nodes), 'Count the number of nodes');
149 my $total_length_new = $tree->total_branch_length;
150 my $eps = 0.001 * $total_length_new;    # tolerance for checking length
151 warn("orig total len ", $total_length_orig, "\n") if $verbose;
152 warn("new  total len ", $tree->total_branch_length,"\n") if $verbose;
153 # according to retree in phylip these branch lengths actually get larger
154 # go figure...
155 # this should be fixed now/maj
156 ok(($total_length_orig >= $tree->total_branch_length - $eps) &&
157    ($total_length_orig <= $tree->total_branch_length + $eps),'same length');
159 # prob with below: rerooted tree on node A at line 146; so $a IS root
160 #/maj is($tree->get_root_node, $a->ancestor, "Root node is A's ancestor");
161 is($tree->get_root_node, $a, "Root node is A");
163 # former test expected the old behavior of reroot; here is the new
164 # test/maj
165 my $desc = ($a->each_Descendent)[0];
166 my $newroot = $desc->create_node_on_branch(-FRACTION=>0.5, -ANNOT=>{id=>'newroot'});
167 $tree->reroot($newroot);
168 is($tree->get_root_node, $a->ancestor, "Root node is A's ancestor");
170 # try to reroot on an internal, will result in there being 1 less node
171 # Rerooting should be an invariant operation with respect to node number!/maj
172 # the test show that it now is, because the secret removal of nodes 
173 # no longer occurs
175 $a = $tree->find_node('C')->ancestor;
176 $out->write_tree($tree) if $verbose;
177 is($tree->reroot($a),1, "Can reroot with C's ancsestor");
178 $out->write_tree($tree) if $verbose;
179 #/maj is($node_cnt_orig, scalar($tree->get_nodes), 'Check to see that node count is correct after an internal node was removed after this re-rooting');
180 # but we did add a new node at line 166, so
181 is($node_cnt_orig+1, scalar($tree->get_nodes), 'Node count correct');
182 warn("orig total len ", $total_length_orig, "\n") if $verbose;
183 warn("new  total len ", $tree->total_branch_length,"\n") if $verbose;
184 cmp_ok($total_length_orig, '>=', $tree->total_branch_length - $eps, 
185        'Total original branch length is what it is supposed to be');
186 # branch length should also be invariant w/r to rerooting...
187 cmp_ok($total_length_orig, '<=',$tree->total_branch_length + $eps, 
188        'Updated total branch length after the reroot');
189 # again, we rerooted ON THE NODE, so $a IS the root./maj
190 is($tree->get_root_node, $a, 'Make sure root is really what we asked for');
192 # try to reroot on new root: should fail
193 #/maj  $a = $tree->get_root_node;
194 isnt( $tree->reroot($a),1, 'Testing for failed re-rerooting');
196 # try a more realistic tree
197 $tree = $in->next_tree;
198 $a = $tree->find_node('VV');
199 $node_cnt_orig = scalar($tree->get_nodes);
200 $total_length_orig = $tree->total_branch_length;
201 $out->write_tree($tree) if $verbose;
202 is($tree->reroot($a),1, 'Test that rooting succeeded'); #mod /maj
203 $out->write_tree($tree) if $verbose;
204 # node number should be invariant after reroot/maj
205 is($node_cnt_orig, scalar($tree->get_nodes), 'Test that re-rooted tree has proper number of nodes after re-rooting'); #mod /maj
206 $total_length_new = $tree->total_branch_length;
207 $eps = 0.001 * $total_length_new;    # tolerance for checking length
208 cmp_ok($total_length_orig, '>=', $tree->total_branch_length - $eps, 'Branch length before rerooting');
209 cmp_ok($total_length_orig, '<=', $tree->total_branch_length + $eps, 
210        'Branch length after rerooting');
211 is($tree->get_root_node, $a,'Root is really the ancestor we asked for'); #mod /maj
213 # BFS and DFS search testing
214 $treeio = Bio::TreeIO->new(-verbose => $verbose,
215                              -format => 'newick',
216                              -file   => test_input_file('test.nh'));
217 $tree = $treeio->next_tree;
218 my ($ct,$n) = (0);
219 my $let = ord('A');
220 for $n (  $tree->get_leaf_nodes ) {
221     $n->id(chr($let++));
224 for $n ( grep {! $_->is_Leaf } $tree->get_nodes ) {
225     $n->id($ct++);
227 # enable for debugging
228 Bio::TreeIO->new(-format => 'newick')->write_tree($tree) if( $verbose );
230 my $BFSorder = join(",", map { $_->id } ( $tree->get_nodes(-order => 'b')));
231 is($BFSorder, '0,1,3,2,C,D,E,F,G,H,A,B', 'BFS traversal order');
232 my $DFSorder = join(",", map { $_->id } ( $tree->get_nodes(-order => 'd')));
233 is($DFSorder, '0,1,2,A,B,C,D,3,E,F,G,H', 'DFS travfersal order');
236 # test some Bio::Tree::TreeFunctionI methods
237 #find_node tested extensively already
238 $tree->remove_Node('H');
239 $DFSorder = join(",", map { $_->id } ( $tree->get_nodes(-order => 'd')));
240 is($DFSorder, '0,1,2,A,B,C,D,3,E,F,G', 'DFS traversal after removing H');
241 #get_lineage_nodes tested during get_lca
242 $tree->splice(-remove_id => 'G');
243 $DFSorder = join(",", map { $_->id } ( $tree->get_nodes(-order => 'd')));
244 is($DFSorder, '0,1,2,A,B,C,D,3,E,F', 'DFS traversal after removing G');
245 $tree->splice(-remove_id => [('E', 'F')], -keep_id => 'F');
246 $DFSorder = join(",", map { $_->id } ( $tree->get_nodes(-order => 'd')));
247 # the node '3' is not explicitly removed, so it should still be there
248 # I suspect that it disappeared before was due to the previously
249 # automatic removal of internal degree 2 nodes../maj
250 is($DFSorder, '0,1,2,A,B,C,D,3,F', 'DFS traversal after removing E');
251 $tree->splice(-keep_id => [qw(0 1 2 A B C D)]);
252 $DFSorder = join(",", map { $_->id } ( $tree->get_nodes(-order => 'd')));
253 is($DFSorder, '0,1,2,A,B,C,D', 'DFS after removing all but 0,1,2,A,B,C,D');
254 #get_lca, merge_lineage, contract_linear_paths tested in in Taxonomy.t
257 # try out the id to bootstrap copy method
258 $treeio = Bio::TreeIO->new(-format => 'newick',
259                            -file   => test_input_file('bootstrap.tre'));
260 $tree = $treeio->next_tree;
261 my ($test_node) = $tree->find_node(-id => 'A');
262 is($test_node->ancestor->id, 90,'Testing bootstrap copy');
263 is($test_node->ancestor->ancestor->id, '25','Testing bootstrap copy');
264 $tree->move_id_to_bootstrap;
265 is($test_node->ancestor->id, '','Testing bootstrap copy');
266 is($test_node->ancestor->bootstrap, '90', 'Testing bootstrap copy');
267 is($test_node->ancestor->ancestor->id, '', 'Testing bootstrap copy');
268 is($test_node->ancestor->ancestor->bootstrap, '25', 'Testing bootstrap copy');
270 # change TreeIO to parse 
271 $treeio = Bio::TreeIO->new(-format => 'newick',
272                            -file   => test_input_file('bootstrap.tre'),
273                            -internal_node_id => 'bootstrap');
274 $tree = $treeio->next_tree;
275 ($test_node) = $tree->find_node(-id => 'A');
276 is($test_node->ancestor->id, '','Testing auto-boostrap copy during parse');
277 is($test_node->ancestor->ancestor->id, '',
278    'Testing auto-boostrap copy during parse');
279 is($test_node->ancestor->bootstrap, '90',
280    'Testing auto-boostrap copy during parse');
281 is($test_node->ancestor->ancestor->bootstrap, '25', 
282    'Testing auto-boostrap copy during parse');
286 __DATA__
287 (D,(C,(A,B)));
288 (I,((D,(C,(A,B)x)y),(E,(F,G))));
289 (((A:0.3,B:2.1):0.45,C:0.7),D:4);
290 (A:0.031162,((((((B:0.022910,C:0.002796):0.010713,(D:0.015277,E:0.020484):0.005336):0.005588,((F:0.013293,(G:0.018374,H:0.003108):0.005318):0.006047,I:0.014607):0.001677):0.004196,(((((J:0.003307,K:0.001523):0.011884,L:0.006960):0.006514,((M:0.001683,N:0.000100):0.002226,O:0.007085):0.014649):0.008004,P:0.037422):0.005201,(Q:0.000805,R:0.000100):0.015280):0.005736):0.004612,S:0.042283):0.017979,(T:0.006883,U:0.016655):0.040226):0.014239,((((((V:0.000726,W:0.000100):0.028490,((((X:0.011182,Y:0.001407):0.005293,Z:0.011175):0.004701,AA:0.007825):0.016256,BB:0.029618):0.008146):0.004279,CC:0.035012):0.060215,((((((DD:0.014933,(EE:0.008148,FF:0.000100):0.015458):0.003891,GG:0.010996):0.001489,(HH:0.000100,II:0.000100):0.054265):0.003253,JJ:0.019722):0.013796,((KK:0.001960,LL:0.004924):0.013034,MM:0.010071):0.043273):0.011912,(NN:0.031543,OO:0.018307):0.059182):0.026517):0.011087,((PP:0.000100,QQ:0.002916):0.067214,(RR:0.064486,SS:0.013444):0.011613):0.050846):0.015644,((TT:0.000100,UU:0.009287):0.072710,(VV:0.009242,WW:0.009690):0.035346):0.042993):0.060365);