Bio::SeqIO
gbdriver
Toolbar
Summary
Bio::SeqIO::gbdriver - GenBank handler-based push parser
Package variables
Privates (from "my" definitions)
%FTQUAL_NO_QUOTE = map {$_ => 1} qw( anticodon citation codon codon_start cons_splice direction evidence label mod_base number rpt_type rpt_unit transl_except transl_table usedin )
%POSTPROCESS_DATA = map {$_ => 1} qw (FEATURES)
Included modules
Inherit
Synopsis
#It is probably best not to use this object directly, but
#rather go through the SeqIO handler:
$stream = Bio::SeqIO->new(-file => $filename,
-format => 'gbdriver');
while ( my $seq = $stream->next_seq() ) {
# do something with $seq
}
Description
This object can transform Bio::Seq objects to and from GenBank flat file
databases. The key difference between this parser and the tried-and-true
Bio::SeqIO::genbank parser is this version separates the parsing and data
manipulation into a 'driver' method (next_seq) and separate object handlers
which deal with the data passed to it.
The main purpose of the driver routine, in this case next_seq(), is to carve out
the data into meaningful chunks which are passed along to relevant handlers (see
below).
Each chunk of data in the has a NAME tag attached to it, similar to that for XML
parsing. This designates the type of data passed (annotation type or seqfeature)
and the handler to be called for processing the data.
For GenBank annotations, the data is divided up and passed along to handlers
according to whether the data is tagged with a field name (i.e. LOCUS) and
whether the field name represents 'primary' annotation (in this case, is present
at the beginning of the line, such as REFERENCE). If the field is primary, it is
assigned to the NAME tag. Field names which aren't primary (have at least 2
spaces before the name, like ORGANISM) are appended to the preceding primary
field name as additional tags.
For feature table data each new feature name signals the beginning of a new
chunk of data. 'FEATURES' is attached to NAME, the feature key ('CDS', 'gene',
etc) is attached as the PRIMARY_ID, and the location is assigned to it's own tag
name (LOCATION). Feature qualifiers are added as additional keys, with multiple
keys included in an array.
Once a particular event occurs (new primary tag, sequence, end of record), the
data is passed along to be processed by a handler or (if no handler is defined)
tossed away.
Internally, the hash ref for a representative annotation (here a REFERENCE)
looks like this:
$VAR1 = {
'JOURNAL' => 'Unpublished (2003)',
'TITLE' => 'The DNA sequence of Homo sapiens',
'NAME' => 'REFERENCE',
'REFERENCE' => '1 (bases 1 to 10001)',
'AUTHORS' => 'International Human Genome Sequencing Consortium.'
};
and a SeqFeature as this:
$VAR1 = {
'db_xref' => [
'GeneID:127086',
'InterimID:127086'
],
'LOCATION' => 'complement(3024..6641)',
'NAME' => 'FEATURES',
'FEATURE_KEY' => 'gene',
'gene' => 'LOC127086',
'note' => 'Derived by automated computational analysis using
gene prediction method: GNOMON.'
};
Note that any driver implementation would suffice as long as it fulfilled the
requirements above.
Methods
Methods description
Title : next_seq Usage : $seq = $stream->next_seq() Function: returns the next sequence in the stream Returns : Bio::Seq object Args : |
Title : write_seq Usage : $stream->write_seq($seq) Function: writes the $seq object (must be seq) to the stream Returns : 1 for success and 0 for error Args : array of 1 to n Bio::SeqI objects |
Title : seqhandler Usage : $stream->seqhandler($handler) Function: Get/Set teh Bio::Seq::HandlerBaseI object Returns : Bio::Seq::HandlerBaseI Args : Bio::Seq::HandlerBaseI |
Methods code
sub _initialize
{ my($self,@args) = @_;
$self->SUPER::_initialize(@args);
my $handler = $self->_rearrange([qw(HANDLER)],@args);
$handler ? $self->seqhandler($handler) :
$self->seqhandler(Bio::SeqIO::Handler::GenericRichSeqHandler->new(
-format => 'genbank',
-verbose => $self->verbose,
-builder => $self->sequence_builder
));
if( ! defined $self->sequence_factory ) {
$self->sequence_factory(Bio::Seq::SeqFactory->new
(-verbose => $self->verbose(),
-type => 'Bio::Seq::RichSeq'));
}} |
sub next_seq
{ my $self = shift;
local($/) = "\n";
my ($ann, $data, $annkey);
my $endrec = my $seenfeat = 0;
my $seqdata;
my $seenlocus;
my $hobj = $self->seqhandler;
my $handlers = $self->seqhandler->handler_methods;
PARSER:
while (defined(my $line = $self->_readline)) {
next if $line =~ m{^\s*$};
if ($ann && $ann eq 'ORIGIN') {
SEQ:
while (defined($line)) {
last SEQ if index($line,'//') == 0;
$seqdata->{DATA} .= uc $line;
$line = $self->_readline;
}
$seqdata->{DATA} =~ tr{0-9 \n}{}d;
}
$endrec = 1 if (index($line,'//')==0);
if ($line =~ m{^(\s{0,5})(\w+)\s+(.*)$}ox || $endrec) {
($ann, $data) = ($2, $3);
unless ($seenlocus) {
$self->throw("No LOCUS found. Not GenBank in my book!")
if ($ann ne 'LOCUS');
$seenlocus = 1;
}
my $len = length($1 || '');
$annkey = ($len == 0 || $len > 4) ? 'DATA' : $ann;
if (($annkey eq 'DATA') && $seqdata) {
chomp $seqdata->{DATA};
if ($seqdata->{NAME} eq 'FEATURES') {
$self->_process_features($seqdata)
}
$hobj->data_handler($seqdata);
last PARSER if $endrec;
$seqdata = undef;
}
$seqdata->{NAME} = ($len == 0) ? $ann : ($len > 4 ) ? 'FEATURES': $seqdata->{NAME}; if ($seqdata->{NAME} eq 'FEATURES') {
$seqdata->{FEATURE_KEY} = $ann;
}
next PARSER if $ann eq 'ORIGIN';
} else {
($data = $line) =~ s{^\s+}{};
chomp $data;
}
my $delim = ($seqdata && $seqdata->{NAME} eq 'FEATURES') ? "\n" : ' ';
$seqdata->{$annkey} .= ($seqdata->{$annkey}) ? $delim.$data : $data;
}
return $hobj->build_sequence;} |
sub next_chunk
{ my $self = shift;
local($/) = "\n";
my ($ann, $data, $annkey);
my $endrec = my $seenfeat = 0;
my $seqdata;
my $seenlocus;
my $hobj = $self->seqhandler;
PARSER:
while (defined(my $line = $self->_readline)) {
next if $line =~ m{^\s*$};
if ($ann && $ann eq 'ORIGIN') {
SEQ:
while (defined($line)) {
last SEQ if index($line,'//') == 0;
$seqdata->{DATA} .= uc $line;
$line = $self->_readline;
}
$seqdata->{DATA} =~ tr{0-9 \n}{}d;
}
$endrec = 1 if (index($line,'//')==0);
if ($line =~ m{^(\s{0,5})(\w+)\s+(.*)$}ox || $endrec) {
($ann, $data) = ($2, $3);
unless ($seenlocus) {
$self->throw("No LOCUS found. Not GenBank in my book!")
if ($ann ne 'LOCUS');
$seenlocus = 1;
}
my $len = length($1 || '');
$annkey = ($len == 0 || $len > 4) ? 'DATA' : $ann;
if (($annkey eq 'DATA') && $seqdata) {
chomp $seqdata->{DATA};
if ($seqdata->{NAME} eq 'FEATURES') {
$self->_process_features($seqdata)
}
$hobj->data_handler($seqdata);
last PARSER if $endrec;
$seqdata = undef;
}
$seqdata->{NAME} = ($len == 0) ? $ann : ($len > 4 ) ? 'FEATURES': $seqdata->{NAME}; if ($seqdata->{NAME} eq 'FEATURES') {
$seqdata->{FEATURE_KEY} = $ann;
}
next PARSER if $ann eq 'ORIGIN';
} else {
($data = $line) =~ s{^\s+}{};
chomp $data;
}
my $delim = ($seqdata && $seqdata->{NAME} eq 'FEATURES') ? "\n" : ' ';
$seqdata->{$annkey} .= ($seqdata->{$annkey}) ? $delim.$data : $data;
}} |
sub write_seq
{ shift->throw("Use Bio::SeqIO::genbank for output");
} |
sub seqhandler
{ my ($self, $handler) = @_;
if ($handler) {
$self->throw("Not a Bio::HandlerBaseI") unless
ref($handler) && $handler->isa("Bio::HandlerBaseI");
$self->{'_seqhandler'} = $handler;
}
return $self->{'_seqhandler'};
}
} |
| _process_features | description | prev | next | Top |
sub _process_features
{ my ($self, $seqdata) = @_;
my @ftlines = split m{\n}, $seqdata->{DATA};
delete $seqdata->{DATA};
map { s{"}{}g } @ftlines;
my $qual = 'LOCATION';
my $ct = 0;
for my $qualdata (@ftlines) {
if ($qualdata =~ m{^/([^=]+)=?(.+)?}) {
($qual, $qualdata) = ($1, $2);
$qualdata ||= ''; $ct = (exists $seqdata->{$qual}) ?
((ref($seqdata->{$qual})) ? scalar(@{ $seqdata->{$qual} }) : 1)
: 0 ;
}
my $delim = ($qual eq 'translation' || exists $FTQUAL_NO_QUOTE{$qual}) ?
'' : ' ';
if ($ct == 0) {
(exists $seqdata->{$qual}) ? ($seqdata->{$qual}.= $delim.$qualdata || '') :
($seqdata->{$qual} .= $qualdata || '');
} else {
if (!ref($seqdata->{$qual})) {
$seqdata->{$qual} = [$seqdata->{$qual}];
}
(exists $seqdata->{$qual}->[$ct]) ? (($seqdata->{$qual}->[$ct]) .= $delim.$qualdata) :
(($seqdata->{$qual}->[$ct]) .= $qualdata);
}
}
}
1;
__END__} |
General documentation
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to one
of the Bioperl mailing lists. Your participation is much appreciated.
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
Please direct usage questions or support issues to the mailing list:
bioperl-l@bioperl.org
rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution. Bug reports can be submitted via the web:
https://redmine.open-bio.org/projects/bioperl/
| AUTHOR - Bioperl Project | Top |
bioperl-l at bioperl.org
Original author Elia Stupka, elia -at- tigem.it
Ewan Birney birney at ebi.ac.uk
Jason Stajich jason at bioperl.org
Chris Mungall cjm at fruitfly.bdgp.berkeley.edu
Lincoln Stein lstein at cshl.org
Heikki Lehvaslaiho, heikki at ebi.ac.uk
Hilmar Lapp, hlapp at gmx.net
Donald G. Jackson, donald.jackson at bms.com
James Wasmuth, james.wasmuth at ed.ac.uk
Brian Osborne, bosborne at alum.mit.edu
The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _