[bug 2714]
[bioperl-live.git] / Bio / Matrix / IO.pm
blobe2c48f412a52bee5f1bd84f116a32347ca5d39f7
1 # $Id$
3 # BioPerl module for Bio::Matrix::IO
5 # Cared for by Jason Stajich <jason-at-bioperl-dot-org>
7 # Copyright Jason Stajich
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
13 =head1 NAME
15 Bio::Matrix::IO - A factory for Matrix parsing
17 =head1 SYNOPSIS
19 use Bio::Matrix::IO;
20 my $parser = Bio::Matrix::IO->new(-format => 'scoring',
21 -file => 'BLOSUMN50');
23 my $matrix = $parser->next_matrix;
25 =head1 DESCRIPTION
27 This is a general factory framework for writing parsers for Matricies.
28 This includes parsing output from distance output like PHYLIP's
29 ProtDist. Additionally it should be possible to fit parsers for PWM
30 and PSSMs once their Matrix objects are written.
32 =head1 FEEDBACK
34 =head2 Mailing Lists
36 User feedback is an integral part of the evolution of this and other
37 Bioperl modules. Send your comments and suggestions preferably to
38 the Bioperl mailing list. Your participation is much appreciated.
40 bioperl-l@bioperl.org - General discussion
41 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
43 =head2 Reporting Bugs
45 Report bugs to the Bioperl bug tracking system to help us keep track
46 of the bugs and their resolution. Bug reports can be submitted via
47 the web:
49 http://bugzilla.open-bio.org/
51 =head1 AUTHOR - Jason Stajich
53 Email jason-at-bioperl-dot-org
55 =head1 APPENDIX
57 The rest of the documentation details each of the object methods.
58 Internal methods are usually preceded with a _
60 =cut
63 # Let the code begin...
66 package Bio::Matrix::IO;
67 use strict;
70 use base qw(Bio::Root::IO);
72 =head2 new
74 Title : new
75 Usage : my $obj = Bio::Matrix::IO->new();
76 Function: Builds a new Bio::Matrix::IO object
77 Returns : an instance of Bio::Matrix::IO
78 Args :
81 =cut
83 sub new {
84 my($caller,@args) = @_;
85 my $class = ref($caller) || $caller;
87 # or do we want to call SUPER on an object if $caller is an
88 # object?
89 if( $class =~ /Bio::Matrix::IO::(\S+)/ ) {
90 my ($self) = $class->SUPER::new(@args);
91 $self->_initialize(@args);
92 return $self;
93 } else {
95 my %param = @args;
96 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
97 my $format = $param{'-format'} ||
98 $class->_guess_format( $param{'-file'} || $ARGV[0] ) ||
99 'scoring';
100 $format = "\L$format"; # normalize capitalization to lower case
102 # normalize capitalization
103 return unless( $class->_load_format_module($format) );
104 return "Bio::Matrix::IO::$format"->new(@args);
108 =head2 newFh
110 Title : newFh
111 Usage : $fh = Bio::Matrix::IO->newFh(-file=>$filename,-format=>'Format')
112 Function: does a new() followed by an fh()
113 Example : $fh = Bio::Matrix::IO->newFh(-file=>$filename,-format=>'Format')
114 $matrix = <$fh>; # read a matrix object
115 print $fh $matrix; # write a matrix object
116 Returns : filehandle tied to the Bio::SeqIO::Fh class
117 Args :
119 =cut
121 sub newFh {
122 my $class = shift;
123 return unless my $self = $class->new(@_);
124 return $self->fh;
127 =head2 fh
129 Title : fh
130 Usage : $obj->fh
131 Function: Get a filehandle type access to the matrix parser
132 Example : $fh = $obj->fh; # make a tied filehandle
133 $matrix = <$fh>; # read a matrix object
134 print $fh $matrix; # write a matrix object
135 Returns : filehandle tied to Bio::Matrix::IO class
136 Args : none
138 =cut
141 sub fh {
142 my $self = shift;
143 my $class = ref($self) || $self;
144 my $s = Symbol::gensym;
145 tie $$s,$class,$self;
146 return $s;
150 =head2 next_matrix
152 Title : next_matrix
153 Usage : my $matrix = $matixio->next_matrix;
154 Function: Parse the next matrix from the data stream
155 Returns : L<Bio::Matrix::MatrixI> type object or undef when finished
156 Args : none
159 =cut
161 sub next_matrix{
162 my ($self) = @_;
163 $self->throw_not_implemented();
166 =head2 write_matrix
168 Title : write_matrix
169 Usage : $io->write_matrix($matrix)
170 Function: Writes a matrix out to the data stream
171 Returns : none
172 Args : Array of Bio::Matrix::MatrixI object
173 - note that not all matricies can be converted to
174 each format, beware with mixing matrix types and output formats
176 =cut
178 sub write_matrix{
179 my ($self) = @_;
180 $self->throw_not_implemented();
183 sub _initialize {
184 my ($self,@args) = @_;
185 $self->_initialize_io(@args);
188 =head2 _load_format_module
190 Title : _load_format_module
191 Usage : *INTERNAL Matrix::IO stuff*
192 Function: Loads up (like use) a module at run time on demand
194 =cut
196 sub _load_format_module {
197 my ($self,$format) = @_;
198 my $module = "Bio::Matrix::IO::" . $format;
199 my $ok;
201 eval {
202 $ok = $self->_load_module($module);
204 if ( $@ ) {
205 print STDERR <<END;
206 $self: $format cannot be found
207 Exception $@
208 For more information about the Matrix::IO system please see the
209 Matrix::IO docs. This includes ways of checking for formats at
210 compile time, not run time
214 return $ok;
218 =head2 _guess_format
220 Title : _guess_format
221 Usage : $obj->_guess_format($filename)
222 Returns : guessed format of filename (lower case)
223 Args : filename
225 =cut
227 sub _guess_format {
228 my $class = shift;
229 return unless $_ = shift;
230 return 'scoring' if /BLOSUM|PAM$/i;
231 return 'phylip' if /\.dist$/i;
234 sub DESTROY {
235 my $self = shift;
236 $self->close();
239 sub TIEHANDLE {
240 my $class = shift;
241 return bless {'matrixio' => shift},$class;
244 sub READLINE {
245 my $self = shift;
246 return $self->{'matrixio'}->next_tree() unless wantarray;
247 my (@list,$obj);
248 push @list,$obj while $obj = $self->{'treeio'}->next_tree();
249 return @list;
252 sub PRINT {
253 my $self = shift;
254 $self->{'matrixio'}->write_tree(@_);