sync with trunk to r15684
[bioperl-live.git] / Bio / Matrix / IO / phylip.pm
blob8723aba2db5613b0b4cc56c5589304ac81cf5354
1 # $Id$
3 # BioPerl module for Bio::Matrix::IO::phylip
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Jason Stajich <jason-at-bioperl-dot.org>
9 # Copyright Jason Stajich
11 # You may distribute this module under the same terms as perl itself
13 # POD documentation - main docs before the code
15 =head1 NAME
17 Bio::Matrix::IO::phylip - A parser for PHYLIP distance matricies
19 =head1 SYNOPSIS
21 use Bio::Matrix::IO;
22 my $parser = Bio::Matrix::IO->new(-format => 'phylip'
23 -file => 't/data/phylipdist.out');
24 my $matrix = $parser->next_matrix;
26 =head1 DESCRIPTION
28 This is a parser for PHYLIP distance matrix output.
30 =head1 FEEDBACK
32 =head2 Mailing Lists
34 User feedback is an integral part of the evolution of this and other
35 Bioperl modules. Send your comments and suggestions preferably to
36 the Bioperl mailing list. Your participation is much appreciated.
38 bioperl-l@bioperl.org - General discussion
39 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
41 =head2 Support
43 Please direct usage questions or support issues to the mailing list:
45 L<bioperl-l@bioperl.org>
47 rather than to the module maintainer directly. Many experienced and
48 reponsive experts will be able look at the problem and quickly
49 address it. Please include a thorough description of the problem
50 with code and data examples if at all possible.
52 =head2 Reporting Bugs
54 Report bugs to the Bioperl bug tracking system to help us keep track
55 of the bugs and their resolution. Bug reports can be submitted via
56 the web:
58 http://bugzilla.open-bio.org/
60 =head1 AUTHOR - Jason Stajich
62 Email jason-at-bioperl-dot.org
64 =head1 APPENDIX
66 The rest of the documentation details each of the object methods.
67 Internal methods are usually preceded with a _
69 =cut
72 # Let the code begin...
75 package Bio::Matrix::IO::phylip;
76 use vars qw($DEFAULTPROGRAM);
77 use strict;
79 $DEFAULTPROGRAM = 'phylipdist';
81 use Bio::Matrix::PhylipDist;
83 use base qw(Bio::Matrix::IO);
85 =head2 new
87 Title : new
88 Usage : my $obj = Bio::Matrix::IO::phylip->new();
89 Function: Builds a new Bio::Matrix::IO::phylip object
90 Returns : an instance of Bio::Matrix::IO::phylip
91 Args :
94 =cut
96 sub new {
97 my($class,@args) = @_;
99 my $self = $class->SUPER::new(@args);
100 my ($prog) = $self->_rearrange([qw(PROGRAM)], @args);
101 $self->{'_program'} = $prog || $DEFAULTPROGRAM;
102 return $self;
106 =head2 next_matrix
108 Title : next_matrix
109 Usage : my $matrix = $parser->next_matrix
110 Function: Get the next result set from parser data
111 Returns : L<Bio::Matrix::PhylipDist>
112 Args : none
115 =cut
117 sub next_matrix {
118 my ($self) = @_;
119 my @names;
120 my @values;
121 my $entry;
122 my $size = 0;
123 while ($entry=$self->_readline) {
124 if($#names >=0 && $entry =~/^\s+\d+\n$/){
125 $self->_pushback($entry);
126 last;
127 } elsif($entry=~/^\s+(\d+)\n$/){
128 $size = $1;
129 next;
130 } elsif( $entry =~ s/^\s+(\-?\d+\.\d+)/$1/ ) {
131 my (@line) = split( /\s+/,$entry);
132 push @{$values[-1]}, @line;
133 next;
135 my ($n,@line) = split( /\s+/,$entry);
137 push @names, $n;
138 push @values, [@line];
140 if( scalar @names != $size ) {
141 $self->warn("The number of entries ".(scalar @names).
142 " is not the same $size");
144 $#names>=0 || return;
145 my %dist;
146 my $i=0;
147 foreach my $name(@names){
148 my $j=0;
149 foreach my $n(@names) {
150 $dist{$name}{$n} = [$i,$j];
151 $j++;
153 $i++;
155 my $matrix = Bio::Matrix::PhylipDist->new
156 (-matrix_name => $self->{'_program'},
157 -matrix => \%dist,
158 -names => \@names,
159 -values => \@values);
160 return $matrix;
163 =head2 write_matrix
165 Title : write_matrix
166 Usage : $matio->write_matrix($matrix)
167 Function: Write out a matrix in the phylip distance format
168 Returns : none
169 Args : L<Bio::Matrix::PhylipDist>
172 =cut
174 sub write_matrix {
175 my ($self,@matricies) = @_;
176 foreach my $matrix ( @matricies ) {
177 my @names = @{$matrix->names};
178 my @values = @{$matrix->_values};
179 my %matrix = %{$matrix->_matrix};
180 my $str;
181 $str.= (" "x 4). scalar(@names)."\n";
182 foreach my $name (@names){
183 my $newname = $name. (" " x (15-length($name)));
184 if( length($name) >= 15 ) { $newname .= " " }
185 $str.=$newname;
186 my $count = 0;
187 foreach my $n (@names){
188 my ($i,$j) = @{$matrix{$name}{$n}};
189 if($count < $#names){
190 $str.= $values[$i][$j]. " ";
192 else {
193 $str.= $values[$i][$j];
195 $count++;
197 $str.="\n";
199 $self->_print($str);