Test requires networking
[bioperl-live.git] / t / Matrix / Matrix.t
blobadb4e61d98425537f50eb6b674bfc3e6883a9457
1 # -*-Perl-*- Test Harness script for Bioperl
2 # $Id$
4 use strict;
6 BEGIN {
7     use lib '.';
8     use Bio::Root::Test;
9     
10     test_begin(-tests => 77);
11         
12         use_ok('Bio::Matrix::Generic');
13         use_ok('Bio::Matrix::IO');
16 my $raw = [ [ 0, 10, 20],
17             [ 2, 17,  4],
18             [ 3,  4,  5] ];
20 my $matrix = Bio::Matrix::Generic->new(-values => $raw,
21                                       -matrix_id  => 'fakeid00',
22                                       -matrix_name=> 'matname',
23                                       -rownames   => [qw(A B C)],
24                                       -colnames   => [qw(D E F)] );
26 is($matrix->matrix_name, 'matname');
27 is($matrix->matrix_id,   'fakeid00');
28 is($matrix->entry('A','F'), $raw->[0]->[2]);
29 my @colE = $matrix->get_column('E');
30 is($colE[0], $raw->[0]->[1]);
31 is($colE[1], $raw->[1]->[1]);
32 is($colE[2], $raw->[2]->[1]);
34 my @rowC = $matrix->get_row('C');
35 is($rowC[0], $raw->[2]->[0]);
36 is($rowC[1], $raw->[2]->[1]);
37 is($rowC[2], $raw->[2]->[2]);
39 is($matrix->row_num_for_name('A'),0);
40 is($matrix->column_num_for_name('D'),0);
42 is($matrix->row_header(1),'B');
43 is($matrix->column_header(0),'D');
45 is($matrix->add_row(1, 'b', [qw(21 13 14)]),4);
46 is($matrix->add_column(2, 'f', [qw(71 81 14 3)]),4);
48 is($matrix->add_row(4, 'c', [qw(22 11 17)]),5);
49 is($matrix->remove_row(4),4);
51 is($matrix->add_column(4, 'g', [qw(11 10 100 71)]),5);
52 is($matrix->remove_column(4),4);
54 is($matrix->row_num_for_name('B'),2);
55 is($matrix->row_num_for_name('b'),1);
57 is($matrix->column_num_for_name('D'),0);
58 is($matrix->column_num_for_name('F'),3);
59 is($matrix->column_num_for_name('f'),2);
61 is($matrix->row_header(2),'B');
63 is($matrix->column_header(3),'F');
65 is($matrix->get_entry('b', 'f'), 81);
68 # read in a scoring matrix
70 my $io = Bio::Matrix::IO->new(-format => 'scoring',
71                               -file   => test_input_file('BLOSUM50'));
72 my $blosum_matrix = $io->next_matrix;
73 isa_ok($blosum_matrix,'Bio::Matrix::Scoring');
74 is($blosum_matrix->entropy, 0.4808);
75 is($blosum_matrix->expected_score, -0.3573);
76 is($blosum_matrix->scale, '1/3');
77 is($blosum_matrix->get_entry('*','A'), -5);
78 is($blosum_matrix->get_entry('V','Y'), -1);
79 is($blosum_matrix->get_entry('Y','V'), -1);
80 is($blosum_matrix->get_entry('L','I'), 2);
81 my @diag = $blosum_matrix->get_diagonal;
82 is($diag[2],7);
83 my @row = $blosum_matrix->get_row('D');
84 is($row[5], $blosum_matrix->get_entry('D','Q'));
85 is($blosum_matrix->num_rows,24);
86 is($blosum_matrix->num_columns,24);
88 $io = Bio::Matrix::IO->new(-format => 'scoring',
89                            -file   => test_input_file('PAM250'));
90 my $pam_matrix = $io->next_matrix;
91 isa_ok($pam_matrix, 'Bio::Matrix::Scoring');
92 is($pam_matrix->entropy, 0.354);
93 is($pam_matrix->expected_score, -0.844);
94 is($pam_matrix->scale, 'ln(2)/3');
95 is($pam_matrix->num_rows,24);
96 is($pam_matrix->get_entry('G','*'), -8);
97 is($pam_matrix->get_entry('V','Y'), -2);
98 is($pam_matrix->get_entry('Y','V'), -2);
99 is($pam_matrix->get_entry('L','I'), 2);
100 @diag = $pam_matrix->get_diagonal;
101 is($diag[2],2);
102 @row = $pam_matrix->get_row('D');
103 is($row[5], $pam_matrix->get_entry('D','Q'));
105 # test Phylip parsing
107 $io = Bio::Matrix::IO->new(-format  => 'phylip',
108                           -program => 'phylipdist',
109                           -file    => test_input_file('phylipdist.out'));
111 my $phy = $io->next_matrix;
112 is $phy->program, 'phylipdist';
113 is $phy->get_entry('Alpha','Beta'), '4.23419';
114 is $phy->get_entry('Gamma','Alpha'),'3.63330';
116 my @column =  $phy->get_column('Alpha');
117 is $column[0], '0.00000';
118 is $column[1], '4.23419';
119 is $column[2], '3.63330';
120 is $column[3], '6.20865';
121 is $column[4], '3.45431';
123 @row    = $phy->get_row('Gamma');
124 is $row[0], '3.63330';
125 is $row[1], '3.49289';
126 is $row[2], '0.00000';
127 is $row[3], '3.68733';
128 is $row[4], '5.84929';
130 @diag   = $phy->get_diagonal;
132 is $diag[0], '0.00000';
133 is $diag[1], '0.00000';
134 is $diag[2], '0.00000';
135 is $diag[3], '0.00000';
136 is $diag[4], '0.00000';
139 # test mlagan parsing
141 $io = Bio::Matrix::IO->new(-format => 'mlagan',
142                                                   -file   => test_input_file('nucmatrix.txt'));
144 my $mlag = $io->next_matrix;
145 is $mlag->get_entry('A', 'C'), -150;
146 is $mlag->get_entry('.', 'A'), 0;
147 is $mlag->gap_open, -300;
148 is $mlag->gap_continue, -25;
150 # test output round-trip
151 $mlag->entry('A', 'C', -149);
152 $mlag->gap_open(-150);
153 $mlag->gap_continue(-5);
155 my $out = test_output_file();
156 $io = Bio::Matrix::IO->new(-format => 'mlagan',
157                                                   -file   => ">$out");
158 $io->write_matrix($mlag);
160 $io = Bio::Matrix::IO->new(-format => 'mlagan',
161                                                   -file   => $out);
162 $mlag = $io->next_matrix;
163 is $mlag->get_entry('A', 'C'), -149;
164 is $mlag->gap_open, -150;
165 is $mlag->gap_continue, -5;