1 # BioPerl module for Bio::Matrix::PhylipDist
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Shawn Hoon <shawnh@fugu-sg.org>
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
16 Bio::Matrix::PhylipDist - A Phylip Distance Matrix object
20 use Bio::Tools::Phylo::Phylip::ProtDist;
21 my $dist = Bio::Tools::Phylo::Phylip::ProtDist->new(
22 -file=>"protdist.out",
23 -program=>"ProtDist");
25 my $dist = Bio::Tools::Phylo::Phylip::ProtDist->new(
27 -program=>"ProtDist");
31 my $distance_value = $dist->get_entry('ALPHA','BETA');
32 my @columns = $dist->get_column('ALPHA');
33 my @rows = $dist->get_row('BETA');
34 my @diagonal = $dist->get_diagonal();
36 #print the matrix in phylip numerical format
37 print $dist->print_matrix;
41 Simple object for holding Distance Matrices generated by the following Phylip programs:
47 It currently handles parsing of the matrix without the data output option.
50 Alpha 0.00000 4.23419 3.63330 6.20865 3.45431
51 Beta 4.23419 0.00000 3.49289 3.36540 4.29179
52 Gamma 3.63330 3.49289 0.00000 3.68733 5.84929
53 Delta 6.20865 3.36540 3.68733 0.00000 4.43345
54 Epsilon 3.45431 4.29179 5.84929 4.43345 0.00000
61 User feedback is an integral part of the evolution of this and other
62 Bioperl modules. Send your comments and suggestions preferably to one
63 of the Bioperl mailing lists. Your participation is much appreciated.
65 bioperl-l@bioperl.org - General discussion
66 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
70 Please direct usage questions or support issues to the mailing list:
72 I<bioperl-l@bioperl.org>
74 rather than to the module maintainer directly. Many experienced and
75 reponsive experts will be able look at the problem and quickly
76 address it. Please include a thorough description of the problem
77 with code and data examples if at all possible.
81 Report bugs to the Bioperl bug tracking system to help us keep track
82 the bugs and their resolution. Bug reports can be submitted via the
85 https://redmine.open-bio.org/projects/bioperl/
87 =head1 AUTHOR - Shawn Hoon
89 Email shawnh@fugu-sg.org
93 Jason Stajich, jason-at-bioperl-dot-org
98 The rest of the documentation details each of the object
99 methods. Internal methods are usually preceded with a "_".
103 # Let the code begin...
105 package Bio
::Matrix
::PhylipDist
;
109 use base
qw(Bio::Root::Root Bio::Matrix::MatrixI);
114 Usage : my $family = Bio::Matrix::PhylipDist->new(-file=>"protdist.out",
115 -program=>"protdist");
116 Function: Constructor for PhylipDist Object
117 Returns : L<Bio::Matrix::PhylipDist>
122 my ($class,@args) = @_;
123 my $self = $class->SUPER::new
(@args);
124 my ($matrix,$values, $names,
126 $matid) = $self->_rearrange([qw(MATRIX
134 ($matrix && $values && $names) ||
135 $self->throw("Need matrix, values, and names fields all provided!");
137 $program && $self->matrix_name($program) if defined $program;
139 $self->_matrix($matrix) if ref($matrix) =~ /HASH/i;
140 $self->_values($values) if ref($values) =~ /ARRAY/i;
141 $self->names($names) if ref($names) =~ /ARRAY/i;
143 $self->matrix_name($matname) if defined $matname;
144 $self->matrix_id ($matid) if defined $matid;
152 Usage : $matrix->get_entry();
153 Function: returns a particular entry
155 Arguments: string id1, string id2
160 my ($self,$row,$column) = @_;
161 $row && $column || $self->throw("Need at least 2 ids");
162 my %matrix = %{$self->_matrix};
163 my @values = @
{$self->_values};
164 if(ref $matrix{$row}{$column}){
165 my ($i,$j) = @
{$matrix{$row}{$column}};
166 return $values[$i][$j];
175 Usage : $matrix->get_row('ALPHA');
176 Function: returns a particular row
177 Returns : an array of float
178 Arguments: string id1
183 my ($self,$row) = @_;
184 $row || $self->throw("Need at least a row id");
186 my %matrix = %{$self->_matrix};
187 my @values = @
{$self->_values};
188 my @names = @
{$self->names};
189 $matrix{$row} || return;
190 my ($val) = values %{$matrix{$row}};
191 my $row_pointer = $val->[0];
192 my $index = scalar(@names)-1;
193 return @
{$values[$row_pointer]}[0..$index];
199 Usage : $matrix->get_column('ALPHA');
200 Function: returns a particular column
201 Returns : an array of floats
202 Arguments: string id1
207 my ($self,$column) = @_;
208 $column || $self->throw("Need at least a column id");
210 my %matrix = %{$self->_matrix};
211 my @values = @
{$self->_values};
212 my @names = @
{$self->names};
213 $matrix{$column} || return ();
214 my ($val) = values %{$matrix{$column}};
215 my $row_pointer = $val->[0];
217 for(my $i=0; $i < scalar(@names); $i++) {
218 push @ret, $values[$i][$row_pointer];
226 Usage : $matrix->get_diagonal();
227 Function: returns the diagonal of the matrix
228 Returns : an array of float
229 Arguments: string id1
235 my %matrix = %{$self->_matrix};
236 my @values = @
{$self->_values};
238 foreach my $name (@
{$self->names}){
239 my ($i,$j) = @
{$matrix{$name}{$name}};
240 push @return,$values[$i][$j];
248 Usage : $matrix->print_matrix();
249 Function: returns a string of the matrix in phylip format
257 my @names = @
{$self->names};
258 my @values = @
{$self->_values};
259 my %matrix = %{$self->_matrix};
261 $str.= (" "x
4). scalar(@names)."\n";
262 foreach my $name (@names){
263 my $newname = $name. (" " x
(15-length($name)));
264 if( length($name) >= 15 ) { $newname .= " " }
267 foreach my $n (@names) {
268 my ($i,$j) = @
{$matrix{$name}{$n}};
269 if($count < $#names){
270 $str .= $values[$i][$j]. " ";
273 if( ! defined $values[$i][$j] ) {
274 $self->debug("no value for $i,$j cell\n");
276 $str .= $values[$i][$j];
289 Usage : $matrix->_matrix();
290 Function: get/set for hash reference of the pointers
292 Returns : hash reference
293 Arguments: hash reference
298 my ($self,$val) = @_;
300 $self->{'_matrix'} = $val;
302 return $self->{'_matrix'};
309 Usage : $matrix->names();
310 Function: get/set for array ref of names of sequences
311 Returns : an array reference
312 Arguments: an array reference
317 my ($self,$val) = @_;
319 $self->{'_names'} = $val;
321 return $self->{'_names'};
327 Usage : $matrix->program();
328 Function: get/set for the program name generating this
337 return $self->matrix_name(@_);
343 Usage : $matrix->_values();
344 Function: get/set for array ref of the matrix containing
346 Returns : an array reference
347 Arguments: an array reference
352 my ($self,$val) = @_;
354 $self->{'_values'} = $val;
356 return $self->{'_values'};
360 =head1 L<Bio::Matrix::MatrixI> implementation
366 Usage : my $id = $matrix->matrix_id
367 Function: Get/Set the matrix ID
368 Returns : scalar value
369 Args : [optional] new id value to store
376 return $self->{'_matid'} = shift if @_;
377 return $self->{'_matid'};
385 Usage : my $name = $matrix->matrix_name();
386 Function: Get/Set the matrix name
387 Returns : scalar value
388 Args : [optional] new matrix name value
395 return $self->{'_matname'} = shift if @_;
396 return $self->{'_matname'};
401 Title : column_header
402 Usage : my $name = $matrix->column_header(0)
403 Function: Gets the column header for a particular column number
411 my ($self,$num) = @_;
412 my @coln = $self->column_names;
420 Usage : my $name = $matrix->row_header(0)
421 Function: Gets the row header for a particular row number
429 my ($self,$num) = @_;
430 my @rown = $self->row_names;
433 =head2 column_num_for_name
435 Title : column_num_for_name
436 Usage : my $num = $matrix->column_num_for_name($name)
437 Function: Gets the column number for a particular column name
444 sub column_num_for_name
{
445 my ($self,$name) = @_;
447 foreach my $n ( $self->column_names ) {
448 return $ct if $n eq $name;
454 =head2 row_num_for_name
456 Title : row_num_for_name
457 Usage : my $num = $matrix->row_num_for_name($name)
458 Function: Gets the row number for a particular row name
465 sub row_num_for_name
{
466 my ($self,$name) = @_;
468 foreach my $n ( $self->row_names ) {
469 return $ct if $n eq $name;
477 Usage : my $rowcount = $matrix->num_rows;
478 Function: Get the number of rows
485 sub num_rows
{ return scalar @
{shift->names} }
490 Usage : my $colcount = $matrix->num_columns
491 Function: Get the number of columns
499 return scalar @
{shift->names};
505 Usage : my @rows = $matrix->row_names
506 Function: The names of all the rows
507 Returns : array in array context, arrayref in scalar context
513 sub row_names
{ return @
{shift->names} }
518 Usage : my @columns = $matrix->column_names
519 Function: The names of all the columns
520 Returns : array in array context, arrayref in scalar context
526 sub column_names
{ return @
{shift->names} }