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://github.com/bioperl/bioperl-live/issues
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
;
110 use base
qw(Bio::Root::Root Bio::Matrix::MatrixI);
115 Usage : my $family = Bio::Matrix::PhylipDist->new(-file=>"protdist.out",
116 -program=>"protdist");
117 Function: Constructor for PhylipDist Object
118 Returns : L<Bio::Matrix::PhylipDist>
123 my ($class,@args) = @_;
124 my $self = $class->SUPER::new
(@args);
125 my ($matrix,$values, $names,
127 $matid) = $self->_rearrange([qw(MATRIX
135 ($matrix && $values && $names) ||
136 $self->throw("Need matrix, values, and names fields all provided!");
138 $program && $self->matrix_name($program) if defined $program;
140 $self->_matrix($matrix) if ref($matrix) =~ /HASH/i;
141 $self->_values($values) if ref($values) =~ /ARRAY/i;
142 $self->names($names) if ref($names) =~ /ARRAY/i;
144 $self->matrix_name($matname) if defined $matname;
145 $self->matrix_id ($matid) if defined $matid;
153 Usage : $matrix->get_entry();
154 Function: returns a particular entry
156 Arguments: string id1, string id2
161 my ($self,$row,$column) = @_;
162 $row && $column || $self->throw("Need at least 2 ids");
163 my %matrix = %{$self->_matrix};
164 my @values = @
{$self->_values};
165 if(ref $matrix{$row}{$column}){
166 my ($i,$j) = @
{$matrix{$row}{$column}};
167 return $values[$i][$j];
176 Usage : $matrix->get_row('ALPHA');
177 Function: returns a particular row
178 Returns : an array of float
179 Arguments: string id1
184 my ($self,$row) = @_;
185 $row || $self->throw("Need at least a row id");
187 my %matrix = %{$self->_matrix};
188 my @values = @
{$self->_values};
189 my @names = @
{$self->names};
190 $matrix{$row} || return;
191 my ($val) = values %{$matrix{$row}};
192 my $row_pointer = $val->[0];
193 my $index = scalar(@names)-1;
194 return @
{$values[$row_pointer]}[0..$index];
200 Usage : $matrix->get_column('ALPHA');
201 Function: returns a particular column
202 Returns : an array of floats
203 Arguments: string id1
208 my ($self,$column) = @_;
209 $column || $self->throw("Need at least a column id");
211 my %matrix = %{$self->_matrix};
212 my @values = @
{$self->_values};
213 my @names = @
{$self->names};
214 $matrix{$column} || return ();
215 my ($val) = values %{$matrix{$column}};
216 my $row_pointer = $val->[0];
218 for(my $i=0; $i < scalar(@names); $i++) {
219 push @ret, $values[$i][$row_pointer];
227 Usage : $matrix->get_diagonal();
228 Function: returns the diagonal of the matrix
229 Returns : an array of float
230 Arguments: string id1
236 my %matrix = %{$self->_matrix};
237 my @values = @
{$self->_values};
239 foreach my $name (@
{$self->names}){
240 my ($i,$j) = @
{$matrix{$name}{$name}};
241 push @return,$values[$i][$j];
249 Usage : $matrix->print_matrix();
250 Function: returns a string of the matrix in phylip format
258 my @names = @
{$self->names};
259 my @values = @
{$self->_values};
260 my %matrix = %{$self->_matrix};
262 $str.= (" "x
4). scalar(@names)."\n";
263 foreach my $name (@names){
264 my $newname = $name. (" " x
(15-length($name)));
265 if( length($name) >= 15 ) { $newname .= " " }
268 foreach my $n (@names) {
269 my ($i,$j) = @
{$matrix{$name}{$n}};
270 if($count < $#names){
271 $str .= $values[$i][$j]. " ";
274 if( ! defined $values[$i][$j] ) {
275 $self->debug("no value for $i,$j cell\n");
277 $str .= $values[$i][$j];
290 Usage : $matrix->_matrix();
291 Function: get/set for hash reference of the pointers
293 Returns : hash reference
294 Arguments: hash reference
299 my ($self,$val) = @_;
301 $self->{'_matrix'} = $val;
303 return $self->{'_matrix'};
310 Usage : $matrix->names();
311 Function: get/set for array ref of names of sequences
312 Returns : an array reference
313 Arguments: an array reference
318 my ($self,$val) = @_;
320 $self->{'_names'} = $val;
322 return $self->{'_names'};
328 Usage : $matrix->program();
329 Function: get/set for the program name generating this
338 return $self->matrix_name(@_);
344 Usage : $matrix->_values();
345 Function: get/set for array ref of the matrix containing
347 Returns : an array reference
348 Arguments: an array reference
353 my ($self,$val) = @_;
355 $self->{'_values'} = $val;
357 return $self->{'_values'};
361 =head1 L<Bio::Matrix::MatrixI> implementation
367 Usage : my $id = $matrix->matrix_id
368 Function: Get/Set the matrix ID
369 Returns : scalar value
370 Args : [optional] new id value to store
377 return $self->{'_matid'} = shift if @_;
378 return $self->{'_matid'};
386 Usage : my $name = $matrix->matrix_name();
387 Function: Get/Set the matrix name
388 Returns : scalar value
389 Args : [optional] new matrix name value
396 return $self->{'_matname'} = shift if @_;
397 return $self->{'_matname'};
402 Title : column_header
403 Usage : my $name = $matrix->column_header(0)
404 Function: Gets the column header for a particular column number
412 my ($self,$num) = @_;
413 my @coln = $self->column_names;
421 Usage : my $name = $matrix->row_header(0)
422 Function: Gets the row header for a particular row number
430 my ($self,$num) = @_;
431 my @rown = $self->row_names;
434 =head2 column_num_for_name
436 Title : column_num_for_name
437 Usage : my $num = $matrix->column_num_for_name($name)
438 Function: Gets the column number for a particular column name
445 sub column_num_for_name
{
446 my ($self,$name) = @_;
448 foreach my $n ( $self->column_names ) {
449 return $ct if $n eq $name;
455 =head2 row_num_for_name
457 Title : row_num_for_name
458 Usage : my $num = $matrix->row_num_for_name($name)
459 Function: Gets the row number for a particular row name
466 sub row_num_for_name
{
467 my ($self,$name) = @_;
469 foreach my $n ( $self->row_names ) {
470 return $ct if $n eq $name;
478 Usage : my $rowcount = $matrix->num_rows;
479 Function: Get the number of rows
486 sub num_rows
{ return scalar @
{shift->names} }
491 Usage : my $colcount = $matrix->num_columns
492 Function: Get the number of columns
500 return scalar @
{shift->names};
506 Usage : my @rows = $matrix->row_names
507 Function: The names of all the rows
508 Returns : array in array context, arrayref in scalar context
514 sub row_names
{ return @
{shift->names} }
519 Usage : my @columns = $matrix->column_names
520 Function: The names of all the columns
521 Returns : array in array context, arrayref in scalar context
527 sub column_names
{ return @
{shift->names} }