1
# $Id: swissdriver.pm 15212 2008-12-19 05:47:58Z cjfields $
3
# BioPerl module for Bio::SeqIO::swissdriver
5
# Cared for by Bioperl project bioperl-l(at)bioperl.org
7
# Copyright Chris Fields and contributors see AUTHORS section
9
# You may distribute this module under the same terms as perl itself
11
# POD documentation - main docs before the code
15
Bio::SeqIO::swissdriver - SwissProt/UniProt handler-based push parser
19
#It is probably best not to use this object directly, but
20
#rather go through the SeqIO handler:
22
$stream = Bio::SeqIO->new(-file => $filename,
23
-format => 'swissdriver');
25
while ( my $seq = $stream->next_seq() ) {
26
# do something with $seq
31
This object can transform Bio::Seq objects to and from UniProt flat file
32
databases. The key difference between this parser and the tried-and-true
33
Bio::SeqIO::swiss parser is this version separates the parsing and data
34
manipulation into a 'driver' method (next_seq) and separate object handlers
35
which deal with the data passed to it.
39
The main purpose of the driver routine, in this case next_seq(), is to carve out
40
the data into meaningful chunks which are passed along to relevant handlers (see
43
Each chunk of data in the has a NAME tag attached to it, similar to that for XML
44
parsing. This designates the type of data passed (annotation type or seqfeature)
45
and the handler to be called for processing the data.
51
User feedback is an integral part of the evolution of this and other
52
Bioperl modules. Send your comments and suggestions preferably to one
53
of the Bioperl mailing lists. Your participation is much appreciated.
55
bioperl-l@bioperl.org - General discussion
56
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
60
Report bugs to the Bioperl bug tracking system to help us keep track
61
the bugs and their resolution. Bug reports can be submitted via the web:
63
http://bugzilla.open-bio.org/
65
=head1 AUTHOR - Bioperl Project
67
bioperl-l at bioperl.org
71
The rest of the documentation details each of the object
72
methods. Internal methods are usually preceded with a _
76
# POD is at the end of the module
78
# Let the code begin...
80
# Let the code begin...
82
package Bio::SeqIO::swissdriver;
83
use vars qw(%FTQUAL_NO_QUOTE);
85
use Bio::SeqIO::Handler::GenericRichSeqHandler;
88
use base qw(Bio::SeqIO);
90
# signals to process what's in the hash prior to next round, maps ann => names
92
OC => 'CLASSIFICATION',
93
OH => 'HOST', # not currently handled, bundled with organism data for now
103
AS => 'ASSEMBLYINFO', # Third party annotation
107
# add specialized delimiters here for easier postprocessing
115
my($self,@args) = @_;
117
$self->SUPER::_initialize(@args);
118
my $handler = $self->_rearrange([qw(HANDLER)],@args);
119
# hash for functions for decoding keys.
120
$handler ? $self->seqhandler($handler) :
121
$self->seqhandler(Bio::SeqIO::Handler::GenericRichSeqHandler->new(
123
-verbose => $self->verbose,
124
-builder => $self->sequence_builder
126
if( ! defined $self->sequence_factory ) {
127
$self->sequence_factory(Bio::Seq::SeqFactory->new
128
(-verbose => $self->verbose(),
129
-type => 'Bio::Seq::RichSeq'));
136
Usage : $seq = $stream->next_seq()
137
Function: returns the next sequence in the stream
138
Returns : Bio::Seq object
145
my $hobj = $self->seqhandler;
147
# these contain values that need to carry over each round
148
my ($featkey, $qual, $annkey, $seqdata, $location);
153
while(defined(my $line = $self->_readline)) {
155
my ($ann, $data) = split(m{\s+}, $line, 2);
159
if ($data =~ m{^(\w+)\s+([\d\?\<]+)\s+([\d\?\>]+)(?:\s+?(\S.*))?}ox) {
160
# has location data and desc
162
$hobj->data_handler($seqdata);
165
($seqdata->{FEATURE_KEY}, my $loc1, my $loc2, $data) = ($1, $2, $3, $4);
166
$qual = 'description';
167
$seqdata->{$qual} = $data;
168
$seqdata->{NAME} = $ann;
169
$seqdata->{LOCATION} = "$loc1..$loc2" if defined $loc1;
171
} elsif ($data =~ m{^\s+/([^=]+)(?:=(.+))?}ox) {
173
($qual, $data) = ($1, $2 || '');
174
$ct = ($seqdata->{$qual}) ?
175
((ref($seqdata->{$qual})) ? scalar(@{ $seqdata->{$qual} }) : 1)
180
$seqdata->{$qual} .= ($seqdata->{$qual}) ?
183
if (!ref($seqdata->{$qual})) {
184
$seqdata->{$qual} = [$seqdata->{$qual}];
186
($seqdata->{$qual}->[$ct]) ?
187
($seqdata->{$qual}->[$ct] .= ' '.$data) :
188
($seqdata->{$qual}->[$ct] .= $data);
192
if ($ann ne $lastann) {
193
if (!$SEC{$ann} && $seqdata) {
194
$hobj->data_handler($seqdata);
195
# can't use undef here; it can lead to subtle mem leaks
198
$annkey = (!$SEC{$ann}) ? 'DATA' : # primary data
200
$seqdata->{'NAME'} = $ann if !$SEC{$ann};
202
last PARSER if $ann eq '//';
203
next PARSER if $ann eq 'SQ';
204
my $delim = $DELIM{$ann} || ' ';
205
$seqdata->{$annkey} .= ($seqdata->{$annkey}) ?
206
$delim.$data : $data;
210
# this should only be sequence (fingers crossed!)
212
while (defined ($line = $self->_readline)) {
213
if (index($line, '//') == 0) {
214
$data =~ tr{0-9 \n}{}d;
215
$seqdata->{DATA} = $data;
216
#$self->debug(Dumper($seqdata));
217
$hobj->data_handler($seqdata);
227
# some files have no // for the last file; this catches the last bit o' data
228
$hobj->data_handler($seqdata) if $seqdata;
229
return $hobj->build_sequence;
235
Usage : $stream->write_seq($seq)
236
Function: writes the $seq object (must be seq) to the stream
237
Returns : 1 for success and 0 for error
238
Args : array of 1 to n Bio::SeqI objects
243
shift->throw("Use Bio::SeqIO::swiss write_seq() for output");
244
# maybe make a Writer class as well????
250
Usage : $stream->seqhandler($handler)
251
Function: Get/Set teh Bio::Seq::HandlerBaseI object
252
Returns : Bio::Seq::HandlerBaseI
253
Args : Bio::Seq::HandlerBaseI
258
my ($self, $handler) = @_;
260
$self->throw("Not a Bio::HandlerBaseI") unless
261
ref($handler) && $handler->isa("Bio::HandlerBaseI");
262
$self->{'_seqhandler'} = $handler;
264
return $self->{'_seqhandler'};