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
;
80 use vars
qw($Default_Source);
83 use Bio::SeqFeature::Generic;
86 use Bio::Seq::SeqFactory;
87 use Bio::Annotation::Collection;
88 use Bio::Annotation::Comment;
89 use Bio::Annotation::Reference;
90 use Bio::Annotation::DBLink;
92 use base qw(Bio::SeqIO XML::SAX::Base);
94 $Default_Source = 'BSML';
98 $self->SUPER::_initialize
(@_);
99 $self->{'_parser'} = XML
::SAX
::ParserFactory
->parser('Handler' => $self);
100 if( ! defined $self->sequence_factory ) {
101 $self->sequence_factory(Bio
::Seq
::SeqFactory
->new
102 (-verbose
=> $self->verbose(),
103 -type
=> 'Bio::Seq::RichSeq'));
115 Usage : my $bioSeqObj = $stream->next_seq
116 Function: Retrieves the next sequence from a SeqIO::bsml stream.
117 Returns : A reference to a Bio::Seq::RichSeq object
124 if( @
{$self->{'_seendata'}->{'_seqs'} || []} ||
126 return shift @
{$self->{'_seendata'}->{'_seqs'}};
128 $self->{'_parser'}->parse_file($self->_fh);
129 return shift @
{$self->{'_seendata'}->{'_seqs'}};
132 # XML::SAX::Base methods
135 my ($self,$doc) = @_;
136 $self->{'_seendata'} = {'_seqs' => [],
139 $self->SUPER::start_document
($doc);
143 my ($self,$doc) = @_;
144 $self->SUPER::end_document
($doc);
149 my ($self,$ele) = @_;
150 my $name = uc($ele->{'LocalName'});
151 my $attr = $ele->{'Attributes'};
152 my $seqid = defined $self->{'_seendata'}->{'_seqs'}->[-1] ?
153 $self->{'_seendata'}->{'_seqs'}->[-1]->display_id : undef;
154 for my $k ( keys %$attr ) {
155 $attr->{uc $k} = $attr->{$k};
158 if( $name eq 'BSML' ) {
160 } elsif( $name eq 'DEFINITIONS' ) {
161 } elsif( $name eq 'SEQUENCES' ) {
163 } elsif( $name eq 'SEQUENCE' ) {
165 $desc,$length,$topology,
166 $mol) = map { $attr->{'{}'.$_}->{'Value'} } qw(ID IC-ACCKEY
171 push @
{$self->{'_seendata'}->{'_seqs'}},
172 $self->sequence_factory->create
175 -accession_number
=> $acc,
176 -description
=> $desc,
178 -is_circular
=> ($topology =~ /^linear$/i) ?
0 : 1,
182 } elsif( $name eq 'FEATURE-TABLES' ) {
183 } elsif( $name eq 'ATTRIBUTE' ) {
184 my $curseq = $self->{'_seendata'}->{'_seqs'}->[-1];
185 my ($name,$content) = map { $attr->{'{}'.$_}->{'Value'} } qw(NAME CONTENT);
186 if($name =~ /^version$/i ) {
188 if($content =~ /^[^\.]+\.(\d+)/) {
190 } else { $version = $content }
191 $curseq->seq_version($version);
192 } elsif( $name eq 'organism-species') {
193 my ($genus,$species,$subsp) = split(/\s+/,$content,3);
194 $curseq->species(Bio
::Species
->new(-sub_species
=> $subsp,
197 } elsif( $name eq 'organism-classification' ) {
198 my (@class) =(split(/\s*;\s*/,$content),$curseq->species->species);
199 $curseq->species->classification([reverse @class]);
200 } elsif( $name eq 'database-xref' ) {
201 my ($db,$id) = split(/:/,$content);
202 $curseq->annotation->add_Annotation('dblink',
203 Bio
::Annotation
::DBLink
->new
206 } elsif( $name eq 'date-created' ||
207 $name eq 'date-last-updated' ) {
208 $curseq->add_date($content);
210 } elsif( $name eq 'FEATURE' ) {
211 my ($id,$class,$type,$title,$display_auto)
212 = map { $attr->{'{}'.$_}->{'Value'} } qw(ID CLASS VALUE-TYPE
215 push @
{$self->{'_seendata'}->{'_feats'}},
216 Bio
::SeqFeature
::Generic
->new
217 ( -seq_id
=> $self->{'_seendata'}->{'_seqs'}->[-1]->display_id,
218 -source_tag
=> $Default_Source,
219 -primary_tag
=> $type,
220 -tag
=> {'ID' => $id,
223 } elsif( $name eq 'QUALIFIER') {
224 my ($type,$value) = map { $attr->{'{}'.$_}->{'Value'} } qw(VALUE-TYPE
226 my $curfeat = $self->{'_seendata'}->{'_feats'}->[-1];
227 $curfeat->add_tag_value($type,$value);
228 } elsif( $name eq 'INTERVAL-LOC' ) {
229 my $curfeat = $self->{'_seendata'}->{'_feats'}->[-1];
230 my ($start,$end,$strand) =
231 map { $attr->{'{}'.$_}->{'Value'} } qw(STARTPOS
235 $curfeat->start($start);
237 $curfeat->strand(-1) if($strand);
238 } elsif( $name eq 'REFERENCE' ) {
239 push @
{$self->{'_seendata'}->{'_annot'}},
240 Bio
::Annotation
::Reference
->new();
243 push @
{$self->{'_state'}}, $name;
244 $self->SUPER::start_element
($ele);
248 my ($self,$ele) = @_;
249 pop @
{$self->{'_state'}};
250 my $name = uc $ele->{'LocalName'};
251 my $curseq = $self->{'_seendata'}->{'_seqs'}->[-1];
252 if( $name eq 'REFERENCE') {
253 my $ref = pop @
{$self->{'_seendata'}->{'_annot'}};
254 $curseq->annotation->add_Annotation('reference',$ref);
255 } elsif( $name eq 'FEATURE' ) {
256 my $feat = pop @
{$self->{'_seendata'}->{'_feats'}};
257 $curseq->add_SeqFeature($feat);
259 $self->SUPER::end_element
($ele);
263 my ($self,$data) = @_;
264 if( ! @
{$self->{'_state'}} ) {
265 $self->warn("Calling characters with no previous start_element call. Ignoring data");
267 my $curseq = $self->{'_seendata'}->{'_seqs'}->[-1];
268 my $curfeat = $self->{'_seendata'}->{'_feats'}->[-1];
269 my $curannot = $self->{'_seendata'}->{'_annot'}->[-1];
270 my $name = $self->{'_state'}->[-1];
271 if( $name eq 'REFAUTHORS' ) {
272 $curannot->authors($data->{'Data'});
273 } elsif( $name eq 'REFTITLE') {
274 $curannot->title($data->{'Data'});
275 } elsif( $name eq 'REFJOURNAL') {
276 $curannot->location($data->{'Data'});
277 } elsif( $name eq 'SEQ-DATA') {
278 $data->{'Data'} =~ s/\s+//g;
279 $curseq->seq($data->{'Data'});
282 $self->SUPER::characters
($data);