Add tests for memory leaks and weaken for Issue #81
[bioperl-live.git] / t / Tree / TreeStatistics.t
blob2544933e2d7bf07ee7d9d8b51221472e8d681d1e
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id: RandomTreeFactory.t 11525 2007-06-27 10:16:38Z sendu $
4 use strict;
6 BEGIN { 
7     use lib '.';
8     use Bio::Root::Test;
9     
10     test_begin(-tests => 42);
12     use_ok('Bio::TreeIO');
13     use_ok('Bio::Tree::Statistics');
17 use Data::Dumper;
18 my $in = Bio::TreeIO->new(-format => 'nexus',
19                           -file   => test_input_file('traittree.nexus'));
20 my $tree = $in->next_tree;
21 my $node = $tree->find_node(-id => 'N14');
24 my $stats = Bio::Tree::Statistics->new();
25 is $stats->cherries($tree), 8, 'cherries';
26 is $stats->cherries($tree, $node), 4, 'cherries';
28 # traits
29 my $key = $tree->add_trait(test_input_file('traits.tab'), 4);
30 is $key, undef, 'read traits'; # exceeded column number
32 $key = $tree->add_trait(test_input_file('traits.tab'), 2, 1);
33 is $key, 'disp'; # one leaf has a missing trait value, but ignore it
35 $key = $tree->add_trait(test_input_file('traits.tab'), 3);
36 is $key, 'intermediate';
38 is $stats->ps($tree, $key), 4, 'parsimony score';
39 is $stats->ps($tree, $key, $node), 1, 'subtree parsimony score';
41 my $node_i = $tree->find_node(-id => 'N10');
42 my @values = sort $node_i->get_tag_values('ps_trait');
43 ok eq_set (\@values, ['red', 'blue']), 'ps value';
45 is $stats->fitch_down($tree), 1, 'fitch_down';
46 is $node_i->get_tag_values('ps_trait'), 'red', 'ps value after fitch_down';
50 $node_i = $tree->find_node(-id => '2'); # leaf
51 is $stats->persistence($tree, $node_i), 1, 'persistence of a leaf';
53 $node_i = $tree->find_node(-id => 'N1');
54 is $stats->persistence($tree, $node_i), 1, 'persistence of an internal node value ';
56 $node_i = $tree->find_node(-id => 'N13');
57 is $stats->persistence($tree, $node_i), 3,  'persistence of an internal node value';
59 $node_i = $tree->find_node(-id => 'N6');
60 is $stats->persistence($tree, $node_i), 2,  'persistence of an internal node value';
62 my $value;
64 $node_i = $tree->find_node(-id => '1');
65 is $stats->count_subclusters($tree, $node_i), 0,  'leaf node: number of clusters = 0 ';
67 $node_i = $tree->find_node(-id => 'N3');
68 is $stats->count_subclusters($tree, $node_i), 1,  'number of clusters ';
70 $node_i = $tree->find_node(-id => 'N14');
71 is $stats->count_subclusters($tree, $node_i), 1,  'number of clusters ';
73 $node_i = $tree->find_node(-id => 'N7');
74 is $stats->count_subclusters($tree, $node_i), 2,  'number of clusters ';
78 $node_i = $tree->find_node(-id => 'N12');
79 is $stats->count_leaves($tree, $node_i), 2,  'number of leaves in phylotype ';
81 $node_i = $tree->find_node(-id => 'N13');
82 is $stats->count_leaves($tree, $node_i), 4,  'number of leaves in phylotype ';
84 $node_i = $tree->find_node(-id => 'N14');
85 is $stats->count_leaves($tree, $node_i), 6,  'number of leaves in phylotype ';
87 $node_i = $tree->find_node(-id => 'N7');
88 is $stats->count_leaves($tree, $node_i), 6,  'number of leaves in phylotype ';
92 $node_i = $tree->find_node(-id => 'N4');
93 is $stats->phylotype_length($tree, $node_i), 1,  'phylotype length';
95 $node_i = $tree->find_node(-id => 'N6');
96 is $stats->phylotype_length($tree, $node_i), 5,  'phylotype length';
98 $node_i = $tree->find_node(-id => 'N7');
99 is $stats->phylotype_length($tree, $node_i), 12,  'phylotype length';
101 $node_i = $tree->find_node(-id => 'N13');
102 is $stats->phylotype_length($tree, $node_i), 6,  'phylotype length';
104 $node_i = $tree->find_node(-id => 'N14');
105 is $stats->phylotype_length($tree, $node_i), 11,  'phylotype length';
108 $node_i = $tree->find_node(-id => 'N4');
109 is $stats->sum_of_leaf_distances($tree, $node_i), 1,  'sum of leaf distances';
111 $node_i = $tree->find_node(-id => 'N6');
112 is $stats->sum_of_leaf_distances($tree, $node_i), 6,  'sum of leaf distances';
114 $node_i = $tree->find_node(-id => 'N7');
115 is $stats->sum_of_leaf_distances($tree, $node_i), 18,  'sum of leaf distances';
117 $node_i = $tree->find_node(-id => 'N13');
118 is $stats->sum_of_leaf_distances($tree, $node_i), 8,  'sum of leaf distances';
120 $node_i = $tree->find_node(-id => 'N14');
121 is $stats->sum_of_leaf_distances($tree, $node_i), 18,  'sum of leaf distances';
125 is sprintf ("%.3f", $stats->genetic_diversity($tree, $node_i)), '3.000',  'genetic diversity'; 
127 is sprintf ("%.3f", $stats->statratio($tree, $node_i)), '0.333',  'separation'; 
130 is $stats->ai($tree, $key), 0.628906, 'association index';
131 is $stats->ai($tree, $key, $node), 0.062500, 'subtree association index';
133 my $mc = $stats->mc($tree, $key);
134 is ($mc->{blue}, 2, 'monophyletic clade size');
135 is ($mc->{red}, 4, 'monophyletic clade size');
136 $node = $tree->find_node(-id => 'N10');
137 $mc = $stats->mc($tree, $key, $node);
138 is ($mc->{blue}, 2, 'monophyletic clade size');
139 is ($mc->{red}, 2, 'monophyletic clade size');