1 # BioPerl module for Bio::SeqIO::bsml_sax
3 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 # Cared for by Jason Stajich
10 Bio::SeqIO::bsml_sax - BSML sequence input/output stream using SAX
14 It is probably best not to use this object directly, but rather go
15 through the SeqIO handler system. To read a BSML file:
17 $stream = Bio::SeqIO->new( -file => $filename, -format => 'bsml');
19 while ( my $bioSeqObj = $stream->next_seq() ) {
20 # do something with $bioSeqObj
23 To write a Seq object to the current file handle in BSML XML format:
25 $stream->write_seq( -seq => $seqObj);
27 If instead you would like a XML::DOM object containing the BSML, use:
29 my $newXmlObject = $stream->to_bsml( -seq => $seqObj);
33 In addition to parts of the Bio:: hierarchy, this module uses:
39 This object can transform Bio::Seq objects to and from BSML (XML)
46 User feedback is an integral part of the evolution of this and other
47 Bioperl modules. Send your comments and suggestions preferably to one
48 of the Bioperl mailing lists. Your participation is much appreciated.
50 bioperl-l@bioperl.org - General discussion
51 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
55 Please direct usage questions or support issues to the mailing list:
57 I<bioperl-l@bioperl.org>
59 rather than to the module maintainer directly. Many experienced and
60 reponsive experts will be able look at the problem and quickly
61 address it. Please include a thorough description of the problem
62 with code and data examples if at all possible.
66 Report bugs to the Bioperl bug tracking system to help us keep track
67 the bugs and their resolution. Bug reports can be submitted via the
70 https://github.com/bioperl/bioperl-live/issues
72 =head1 AUTHOR - Jason Stajich
74 Email jason-at-bioperl-dot-org
78 package Bio
::SeqIO
::bsml_sax
;
79 use vars
qw($Default_Source);
82 use Bio::SeqFeature::Generic;
85 use Bio::Seq::SeqFactory;
86 use Bio::Annotation::Collection;
87 use Bio::Annotation::Comment;
88 use Bio::Annotation::Reference;
89 use Bio::Annotation::DBLink;
91 use base qw(Bio::SeqIO XML::SAX::Base);
93 $Default_Source = 'BSML';
97 $self->SUPER::_initialize
(@_);
98 $self->{'_parser'} = XML
::SAX
::ParserFactory
->parser('Handler' => $self);
99 if( ! defined $self->sequence_factory ) {
100 $self->sequence_factory(Bio
::Seq
::SeqFactory
->new
101 (-verbose
=> $self->verbose(),
102 -type
=> 'Bio::Seq::RichSeq'));
114 Usage : my $bioSeqObj = $stream->next_seq
115 Function: Retrieves the next sequence from a SeqIO::bsml stream.
116 Returns : A reference to a Bio::Seq::RichSeq object
123 if( @
{$self->{'_seendata'}->{'_seqs'} || []} ||
125 return shift @
{$self->{'_seendata'}->{'_seqs'}};
127 $self->{'_parser'}->parse_file($self->_fh);
128 return shift @
{$self->{'_seendata'}->{'_seqs'}};
131 # XML::SAX::Base methods
134 my ($self,$doc) = @_;
135 $self->{'_seendata'} = {'_seqs' => [],
138 $self->SUPER::start_document
($doc);
142 my ($self,$doc) = @_;
143 $self->SUPER::end_document
($doc);
148 my ($self,$ele) = @_;
149 my $name = uc($ele->{'LocalName'});
150 my $attr = $ele->{'Attributes'};
151 my $seqid = defined $self->{'_seendata'}->{'_seqs'}->[-1] ?
152 $self->{'_seendata'}->{'_seqs'}->[-1]->display_id : undef;
153 for my $k ( keys %$attr ) {
154 $attr->{uc $k} = $attr->{$k};
157 if( $name eq 'BSML' ) {
159 } elsif( $name eq 'DEFINITIONS' ) {
160 } elsif( $name eq 'SEQUENCES' ) {
162 } elsif( $name eq 'SEQUENCE' ) {
164 $desc,$length,$topology,
165 $mol) = map { $attr->{'{}'.$_}->{'Value'} } qw(ID IC-ACCKEY
170 push @
{$self->{'_seendata'}->{'_seqs'}},
171 $self->sequence_factory->create
174 -accession_number
=> $acc,
175 -description
=> $desc,
177 -is_circular
=> ($topology =~ /^linear$/i) ?
0 : 1,
181 } elsif( $name eq 'FEATURE-TABLES' ) {
182 } elsif( $name eq 'ATTRIBUTE' ) {
183 my $curseq = $self->{'_seendata'}->{'_seqs'}->[-1];
184 my ($name,$content) = map { $attr->{'{}'.$_}->{'Value'} } qw(NAME CONTENT);
185 if($name =~ /^version$/i ) {
187 if($content =~ /^[^\.]+\.(\d+)/) {
189 } else { $version = $content }
190 $curseq->seq_version($version);
191 } elsif( $name eq 'organism-species') {
192 my ($genus,$species,$subsp) = split(/\s+/,$content,3);
193 $curseq->species(Bio
::Species
->new(-sub_species
=> $subsp,
196 } elsif( $name eq 'organism-classification' ) {
197 my (@class) =(split(/\s*;\s*/,$content),$curseq->species->species);
198 $curseq->species->classification([reverse @class]);
199 } elsif( $name eq 'database-xref' ) {
200 my ($db,$id) = split(/:/,$content);
201 $curseq->annotation->add_Annotation('dblink',
202 Bio
::Annotation
::DBLink
->new
205 } elsif( $name eq 'date-created' ||
206 $name eq 'date-last-updated' ) {
207 $curseq->add_date($content);
209 } elsif( $name eq 'FEATURE' ) {
210 my ($id,$class,$type,$title,$display_auto)
211 = map { $attr->{'{}'.$_}->{'Value'} } qw(ID CLASS VALUE-TYPE
214 push @
{$self->{'_seendata'}->{'_feats'}},
215 Bio
::SeqFeature
::Generic
->new
216 ( -seq_id
=> $self->{'_seendata'}->{'_seqs'}->[-1]->display_id,
217 -source_tag
=> $Default_Source,
218 -primary_tag
=> $type,
219 -tag
=> {'ID' => $id,
222 } elsif( $name eq 'QUALIFIER') {
223 my ($type,$value) = map { $attr->{'{}'.$_}->{'Value'} } qw(VALUE-TYPE
225 my $curfeat = $self->{'_seendata'}->{'_feats'}->[-1];
226 $curfeat->add_tag_value($type,$value);
227 } elsif( $name eq 'INTERVAL-LOC' ) {
228 my $curfeat = $self->{'_seendata'}->{'_feats'}->[-1];
229 my ($start,$end,$strand) =
230 map { $attr->{'{}'.$_}->{'Value'} } qw(STARTPOS
234 $curfeat->start($start);
236 $curfeat->strand(-1) if($strand);
237 } elsif( $name eq 'REFERENCE' ) {
238 push @
{$self->{'_seendata'}->{'_annot'}},
239 Bio
::Annotation
::Reference
->new();
242 push @
{$self->{'_state'}}, $name;
243 $self->SUPER::start_element
($ele);
247 my ($self,$ele) = @_;
248 pop @
{$self->{'_state'}};
249 my $name = uc $ele->{'LocalName'};
250 my $curseq = $self->{'_seendata'}->{'_seqs'}->[-1];
251 if( $name eq 'REFERENCE') {
252 my $ref = pop @
{$self->{'_seendata'}->{'_annot'}};
253 $curseq->annotation->add_Annotation('reference',$ref);
254 } elsif( $name eq 'FEATURE' ) {
255 my $feat = pop @
{$self->{'_seendata'}->{'_feats'}};
256 $curseq->add_SeqFeature($feat);
258 $self->SUPER::end_element
($ele);
262 my ($self,$data) = @_;
263 if( ! @
{$self->{'_state'}} ) {
264 $self->warn("Calling characters with no previous start_element call. Ignoring data");
266 my $curseq = $self->{'_seendata'}->{'_seqs'}->[-1];
267 my $curfeat = $self->{'_seendata'}->{'_feats'}->[-1];
268 my $curannot = $self->{'_seendata'}->{'_annot'}->[-1];
269 my $name = $self->{'_state'}->[-1];
270 if( $name eq 'REFAUTHORS' ) {
271 $curannot->authors($data->{'Data'});
272 } elsif( $name eq 'REFTITLE') {
273 $curannot->title($data->{'Data'});
274 } elsif( $name eq 'REFJOURNAL') {
275 $curannot->location($data->{'Data'});
276 } elsif( $name eq 'SEQ-DATA') {
277 $data->{'Data'} =~ s/\s+//g;
278 $curseq->seq($data->{'Data'});
281 $self->SUPER::characters
($data);