tag fourth (and hopefully last) alpha
[bioperl-live.git] / branch-1-6 / Bio / Restriction / IO.pm
blobd04e8b7a02a4479cc6fe37717bdbbe706f4ed73c
1 # $Id$
3 # BioPerl module for Bio::Restriction::IO
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Rob Edwards <redwards@utmem.edu>
9 # Copyright Rob Edwards
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::Restriction::IO - Handler for sequence variation IO Formats
19 =head1 SYNOPSIS
21 use Bio::Restriction::IO;
23 $in = Bio::Restriction::IO->new(-file => "inputfilename" ,
24 -format => 'withrefm');
25 my $res = $in->read; # a Bio::Restriction::EnzymeCollection
27 =head1 DESCRIPTION
29 L<Bio::Restriction::IO> is a handler module for the formats in the
30 Restriction IO set, e.g. C<Bio::Restriction::IO::xxx>. It is the
31 officially sanctioned way of getting at the format objects, which most
32 people should use.
34 The structure, conventions and most of the code is inherited from
35 L<Bio::SeqIO>. The main difference is that instead of using methods
36 C<next_seq>, you drop C<_seq> from the method name.
38 Also, instead of dealing only with individual L<Bio::Restriction::Enzyme>
39 objects, C<read()> will slurp in all enzymes into a
40 L<Bio::Restriction::EnzymeCollection> object.
42 For more details, see documentation in L<Bio::SeqIO>.
44 =head1 TO DO
46 At the moment, these can be use mainly to get a custom set if enzymes in
47 C<withrefm> or C<itype2> formats into L<Bio::Restriction::Enzyme> or
48 L<Bio::Restriction::EnzymeCollection> objects. Using C<bairoch> format is
49 highly experimental and is not recommmended at this time.
51 This class inherits from L<Bio::SeqIO> for convenience sake, though this should
52 inherit from L<Bio::Root::Root>. Get rid of L<Bio::SeqIO> inheritance by
53 copying relevant methods in.
55 C<write()> methods are currently not implemented for any format except C<base>.
56 Using C<write()> even with C<base> format is not recommended as it does not
57 support multicut/multisite enzyme output.
59 Should additional formats be supported (such as XML)?
61 =head1 SEE ALSO
63 L<Bio::SeqIO>,
64 L<Bio::Restriction::Enzyme>,
65 L<Bio::Restriction::EnzymeCollection>
67 =head1 FEEDBACK
69 =head2 Mailing Lists
71 User feedback is an integral part of the evolution of this and other
72 Bioperl modules. Send your comments and suggestions preferably to the
73 Bioperl mailing lists Your participation is much appreciated.
75 bioperl-l@bioperl.org - General discussion
76 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
78 =head2 Support
80 Please direct usage questions or support issues to the mailing list:
82 I<bioperl-l@bioperl.org>
84 rather than to the module maintainer directly. Many experienced and
85 reponsive experts will be able look at the problem and quickly
86 address it. Please include a thorough description of the problem
87 with code and data examples if at all possible.
89 =head2 Reporting Bugs
91 report bugs to the Bioperl bug tracking system to help us keep track
92 the bugs and their resolution. Bug reports can be submitted via the
93 web:
95 http://bugzilla.open-bio.org/
97 =head1 AUTHOR
99 Rob Edwards, redwards@utmem.edu
101 =head1 CONTRIBUTORS
103 Heikki Lehvaslaiho, heikki-at-bioperl-dot-org
105 =head1 APPENDIX
107 The rest of the documentation details each of the object
108 methods. Internal methods are usually preceded with a _
110 =cut
112 # Let the code begin...
114 package Bio::Restriction::IO;
116 use strict;
117 use vars qw(%FORMAT);
118 use base qw(Bio::SeqIO);
120 %FORMAT = (
121 'itype2' => 'itype2',
122 '8' => 'itype2',
123 'withrefm' => 'withrefm',
124 '31' => 'withrefm',
125 'base' => 'base',
126 '0' => 'base',
127 'bairoch' => 'bairoch',
128 '19' => 'bairoch',
129 'macvector' => 'bairoch',
130 'vectorNTI' => 'bairoch',
131 'neo' => 'prototype',
132 'prototype' => 'prototype'
135 =head2 new
137 Title : new
138 Usage : $stream = Bio::Restriction::IO->new(-file => $filename,
139 -format => 'Format')
140 Function: Returns a new seqstream
141 Returns : A Bio::Restriction::IO::Handler initialised with
142 the appropriate format
143 Args : -file => $filename
144 -format => format
145 -fh => filehandle to attach to
147 =cut
149 sub new {
150 my ($class, %param) = @_;
151 my ($format);
153 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
154 $format = $FORMAT{$param{'-format'}} if defined $param{'-format'};
155 $format ||= $class->_guess_format( $param{-file} || $ARGV[0] )
156 || 'base';
157 $format = "\L$format"; # normalize capitalization to lower case
159 return unless $class->_load_format_module($format);
160 return "Bio::Restriction::IO::$format"->new(%param);
164 sub _load_format_module {
165 my ($class, $format) = @_;
166 my $module = "Bio::Restriction::IO::" . $format;
167 my $ok;
168 eval {
169 $ok = $class->_load_module($module);
171 if ( $@ ) {
172 print STDERR <<END;
173 $class: $format cannot be found
174 Exception $@
175 For more information about the IO system please see the IO docs.
176 This includes ways of checking for formats at compile time, not run time
180 return $ok;
183 =head2 read
185 Title : read
186 Usage : $renzs = $stream->read
187 Function: reads all the restrction enzymes from the stream
188 Returns : a Bio::Restriction::EnzymeCollection object
189 Args :
191 =cut
193 sub read {
194 my ($self, $seq) = @_;
195 $self->throw_not_implemented();
198 sub next {
199 my ($self, $seq) = @_;
200 $self->throw_not_implemented();
203 sub next_seq {
204 my ($self, $seq) = @_;
205 $self->throw_not_implemented();
208 =head2 write
210 Title : write
211 Usage : $stream->write($seq)
212 Function: writes the $seq object into the stream
213 Returns : 1 for success and 0 for error
214 Args : Bio::Restriction::EnzymeCollection object
216 =cut
218 sub write {
219 my ($self, $seq) = @_;
220 $self->throw("Sorry, you cannot write to a generic ".
221 "Bio::Restricion::IO object.");
224 sub write_seq {
225 my ($self, $seq) = @_;
226 $self->warn("These are not sequence objects. ".
227 "Use method 'write' instead of 'write_seq'.");
228 $self->write($seq);
231 =head2 _guess_format
233 Title : _guess_format
234 Usage : $obj->_guess_format($filename)
235 Function:
236 Example :
237 Returns : guessed format of filename (lower case)
238 Args :
240 =cut
242 sub _guess_format {
243 my $class = shift;
244 return unless $_ = shift;
245 return 'flat' if /\.dat$/i;
246 return 'xml' if /\.xml$/i;