~ubuntu-branches/ubuntu/oneiric/bioperl/oneiric

« back to all changes in this revision

Viewing changes to Bio/Restriction/IO.pm

  • Committer: Bazaar Package Importer
  • Author(s): Matt Hope
  • Date: 2004-04-18 14:24:11 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040418142411-gr92uexquw4w8liq
Tags: 1.4-1
* New upstream release
* Examples and working code are installed by default to usr/bin,
  this has been moved to usr/share/doc/bioperl/bin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# $Id: IO.pm,v 1.4 2003/12/15 11:50:38 heikki Exp $
 
2
#
 
3
# BioPerl module for Bio::Restriction::IO
 
4
#
 
5
# Cared for by Rob Edwards <redwards@utmem.edu>
 
6
#
 
7
# Copyright Rob Edwards
 
8
#
 
9
# You may distribute this module under the same terms as perl itself
 
10
#
 
11
# POD documentation - main docs before the code
 
12
 
 
13
=head1 NAME
 
14
 
 
15
Bio::Restriction::IO - Handler for sequence variation IO Formats
 
16
 
 
17
=head1 SYNOPSIS
 
18
 
 
19
    use Bio::Restriction::IO;
 
20
 
 
21
    $in  = Bio::Restriction::IO->new(-file => "inputfilename" ,
 
22
                                     -format => 'withrefm');
 
23
    $out = Bio::Restriction::IO->new(-file => ">outputfilename" ,
 
24
                                     -format => 'bairoch');
 
25
    my $res = $in->read; # a Bio::Restriction::EnzymeCollection
 
26
    $out->write($res);
 
27
 
 
28
  # or
 
29
 
 
30
  #    use Bio::Restriction::IO;
 
31
  #
 
32
  #    #input file format can be read from the file extension (dat|xml)
 
33
  #    $in  = Bio::Restriction::IO->newFh(-file => "inputfilename");
 
34
  #    $out = Bio::Restriction::IO->newFh('-format' => 'xml');
 
35
  #
 
36
  #    # World's shortest flat<->xml format converter:
 
37
  #    print $out $_ while <$in>;
 
38
 
 
39
=head1 DESCRIPTION
 
40
 
 
41
Bio::Restriction::IO is a handler module for the formats in the
 
42
Restriction IO set (eg, Bio::Restriction::IO::XXX). It is the
 
43
officially sanctioned way of getting at the format objects, which most
 
44
people should use.
 
45
 
 
46
The structure, conventions and most of the code is inherited from
 
47
L<Bio::SeqIO> module. The main difference is that instead of using
 
48
methods next_seq and write_seq, you drop '_seq' from the method names.
 
49
 
 
50
Also, instead of dealing only with individual Bio::Restriction::Enzyme
 
51
objects, write() will go through all enzymes from a
 
52
Bio::Restriction::EnzymeCollection objects and read() will slurp in all
 
53
enzymes into a Collection.
 
54
 
 
55
For more details, see documentation in Bio::SeqIO.
 
56
 
 
57
=head1 TO DO
 
58
 
 
59
This inherits from SeqIOfor convinience sake. Get rid of it by copying
 
60
relevant methods in. Bio::SeqIO has too many sequence IO specific
 
61
tweeks in it.
 
62
 
 
63
=head1 SEE ALSO
 
64
 
 
65
L<Bio::SeqIO>, 
 
66
L<Bio::Restriction::Enzyme>, 
 
67
L<Bio::Restriction::EnzymeCollection>
 
68
 
 
69
=head1 FEEDBACK
 
70
 
 
71
=head2 Mailing Lists
 
72
 
 
73
User feedback is an integral part of the evolution of this and other
 
74
Bioperl modules. Send your comments and suggestions preferably to the
 
75
Bioperl mailing lists Your participation is much appreciated.
 
76
 
 
77
  bioperl-l@bioperl.org                     - General discussion
 
78
  http://bio.perl.org/MailList.html         - About the mailing lists
 
79
 
 
80
=head2 Reporting Bugs
 
81
 
 
82
report bugs to the Bioperl bug tracking system to help us keep track
 
83
 the bugs and their resolution.  Bug reports can be submitted via
 
84
 email or the web:
 
85
 
 
86
  bioperl-bugs@bio.perl.org
 
87
  http://bugzilla.bioperl.org/
 
88
 
 
89
=head1 AUTHOR
 
90
 
 
91
Rob Edwards, redwards@utmem.edu
 
92
 
 
93
=head1 CONTRIBUTORS
 
94
 
 
95
Heikki Lehvaslaiho, heikki@ebi.ac.uk
 
96
 
 
97
=head1 APPENDIX
 
98
 
 
99
The rest of the documentation details each of the object
 
100
methods. Internal methods are usually preceded with a _
 
101
 
 
102
=cut
 
103
 
 
104
# Let the code begin...
 
105
 
 
106
package Bio::Restriction::IO;
 
107
 
 
108
use strict;
 
109
use vars qw(@ISA %FORMAT);
 
110
use Bio::SeqIO;
 
111
@ISA = 'Bio::SeqIO';
 
112
 
 
113
%FORMAT = (
 
114
            'itype2'    => 'itype2',
 
115
            '8'         => 'itype2',
 
116
            'withrefm'  => 'withrefm',
 
117
            '31'        => 'withrefm',
 
118
            'base'      => 'base',
 
119
            '0'         => 'base',
 
120
            'bairoch'   => 'bairoch',
 
121
            '19'        => 'bairoch',
 
122
            'macvector' => 'bairoch',
 
123
            'vectorNTI' => 'bairoch'
 
124
);
 
125
 
 
126
=head2 new
 
127
 
 
128
 Title   : new
 
129
 Usage   : $stream = Bio::Restriction::IO->new(-file => $filename,
 
130
                                               -format => 'Format')
 
131
 Function: Returns a new seqstream
 
132
 Returns : A Bio::Restriction::IO::Handler initialised with
 
133
           the appropriate format
 
134
 Args    : -file => $filename
 
135
           -format => format
 
136
           -fh => filehandle to attach to
 
137
 
 
138
=cut
 
139
 
 
140
sub new {
 
141
   my ($class, %param) = @_;
 
142
   my ($format);
 
143
 
 
144
   @param{ map { lc $_ } keys %param } = values %param;  # lowercase keys
 
145
 
 
146
   $format = $FORMAT{$param{'-format'}} if defined $param{'-format'};
 
147
   $format ||= $class->_guess_format( $param{-file} || $ARGV[0] )
 
148
             || 'base';
 
149
   $format = "\L$format"; # normalize capitalization to lower case
 
150
 
 
151
   return undef unless $class->_load_format_module($format);
 
152
   return "Bio::Restriction::IO::$format"->new(%param);
 
153
}
 
154
 
 
155
 
 
156
sub _load_format_module {
 
157
  my ($class, $format) = @_;
 
158
  my $module = "Bio::Restriction::IO::" . $format;
 
159
  my $ok;
 
160
  eval {
 
161
      $ok = $class->_load_module($module);
 
162
  };
 
163
  if ( $@ ) {
 
164
    print STDERR <<END;
 
165
$class: $format cannot be found
 
166
Exception $@
 
167
For more information about the IO system please see the IO docs.
 
168
This includes ways of checking for formats at compile time, not run time
 
169
END
 
170
  ;
 
171
  }
 
172
  return $ok;
 
173
}
 
174
 
 
175
=head2 read
 
176
 
 
177
 Title   : read
 
178
 Usage   : $renzs = $stream->read
 
179
 Function: reads all the restrction enzymes from the stream
 
180
 Returns : a Bio::Restriction::EnzymeCollection object
 
181
 Args    :
 
182
 
 
183
=cut
 
184
 
 
185
sub read {
 
186
   my ($self, $seq) = @_;
 
187
   $self->throw("Not implemented");
 
188
}
 
189
 
 
190
sub next {
 
191
   my ($self, $seq) = @_;
 
192
   $self->throw("Not implemented");
 
193
}
 
194
 
 
195
sub next_seq {
 
196
   my ($self, $seq) = @_;
 
197
   $self->throw("Not implemented");
 
198
}
 
199
 
 
200
=head2 write
 
201
 
 
202
 Title   : write
 
203
 Usage   : $stream->write($seq)
 
204
 Function: writes the $seq object into the stream
 
205
 Returns : 1 for success and 0 for error
 
206
 Args    : Bio::Restriction::EnzymeCOllection object
 
207
 
 
208
=cut
 
209
 
 
210
sub write {
 
211
    my ($self, $seq) = @_;
 
212
    $self->throw("Sorry, you cannot write to a generic ".
 
213
                 "Bio::Restricion::IO object.");
 
214
}
 
215
 
 
216
sub write_seq {
 
217
   my ($self, $seq) = @_;
 
218
   $self->warn("These are not sequence objects. ".
 
219
               "Use method 'write' instead of 'write_seq'.");
 
220
   $self->write($seq);
 
221
}
 
222
 
 
223
=head2 _guess_format
 
224
 
 
225
 Title   : _guess_format
 
226
 Usage   : $obj->_guess_format($filename)
 
227
 Function:
 
228
 Example :
 
229
 Returns : guessed format of filename (lower case)
 
230
 Args    :
 
231
 
 
232
=cut
 
233
 
 
234
sub _guess_format {
 
235
   my $class = shift;
 
236
   return  unless $_ = shift;
 
237
   return 'flat'     if /\.dat$/i;
 
238
   return 'xml'     if /\.xml$/i;
 
239
}
 
240
 
 
241
 
 
242
1;