File: bsml_sax.pm

package info (click to toggle)
bioperl 1.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 50,136 kB
  • sloc: perl: 172,618; xml: 22,869; lisp: 2,034; sh: 1,984; makefile: 19
file content (284 lines) | stat: -rw-r--r-- 8,085 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# BioPerl module for Bio::SeqIO::bsml_sax
#
# Please direct questions and support issues to <bioperl-l@bioperl.org> 
#
# Cared for by Jason Stajich
#

=head1 NAME

Bio::SeqIO::bsml_sax - BSML sequence input/output stream using SAX

=head1 SYNOPSIS

 It is probably best not to use this object directly, but rather go
 through the SeqIO handler system. To read a BSML file:

    $stream = Bio::SeqIO->new( -file => $filename, -format => 'bsml');

    while ( my $bioSeqObj = $stream->next_seq() ) {
	# do something with $bioSeqObj
    }

 To write a Seq object to the current file handle in BSML XML format:

    $stream->write_seq( -seq => $seqObj);

 If instead you would like a XML::DOM object containing the BSML, use:

    my $newXmlObject = $stream->to_bsml( -seq => $seqObj);

=head1 DEPENDENCIES

 In addition to parts of the Bio:: hierarchy, this module uses:

 XML::SAX

=head1 DESCRIPTION

 This object can transform Bio::Seq objects to and from BSML (XML)
 flatfiles.

=head1 FEEDBACK

=head2 Mailing Lists

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

=head2 Support 

Please direct usage questions or support issues to the mailing list:

I<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.

=head2 Reporting Bugs

 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://github.com/bioperl/bioperl-live/issues

=head1 AUTHOR - Jason Stajich

Email jason-at-bioperl-dot-org

=cut

package Bio::SeqIO::bsml_sax;
use vars qw($Default_Source);
use strict;

use Bio::SeqFeature::Generic;
use Bio::Species;
use XML::SAX;
use Bio::Seq::SeqFactory;
use Bio::Annotation::Collection;
use Bio::Annotation::Comment;
use Bio::Annotation::Reference;
use Bio::Annotation::DBLink;

use base qw(Bio::SeqIO XML::SAX::Base);

$Default_Source = 'BSML';

sub _initialize {
    my ($self) = shift;
    $self->SUPER::_initialize(@_);
    $self->{'_parser'} = XML::SAX::ParserFactory->parser('Handler' => $self);
    if( ! defined $self->sequence_factory ) {
	$self->sequence_factory(Bio::Seq::SeqFactory->new
				(-verbose => $self->verbose(),
				 -type => 'Bio::Seq::RichSeq'));
    }
    return;
}

=head1 METHODS

=cut

=head2 next_seq

 Title   : next_seq
 Usage   : my $bioSeqObj = $stream->next_seq
 Function: Retrieves the next sequence from a SeqIO::bsml stream.
 Returns : A reference to a Bio::Seq::RichSeq object
 Args    :

=cut

sub next_seq {
    my $self = shift;
    if( @{$self->{'_seendata'}->{'_seqs'} || []} ||
	eof($self->_fh)) {
	return shift @{$self->{'_seendata'}->{'_seqs'}};
    }
    $self->{'_parser'}->parse_file($self->_fh);
    return shift @{$self->{'_seendata'}->{'_seqs'}};
}

# XML::SAX::Base methods

sub start_document {
    my ($self,$doc) = @_;
    $self->{'_seendata'} = {'_seqs'    => [],
			    '_authors' => [],
			    '_feats'   => [] };
    $self->SUPER::start_document($doc);
}

sub end_document {
    my ($self,$doc) = @_;
    $self->SUPER::end_document($doc);
}


sub start_element {
    my ($self,$ele) = @_;
    my $name = uc($ele->{'LocalName'});
    my $attr = $ele->{'Attributes'};
    my $seqid = defined $self->{'_seendata'}->{'_seqs'}->[-1] ?
	$self->{'_seendata'}->{'_seqs'}->[-1]->display_id : undef;
    for my $k ( keys %$attr ) {
	$attr->{uc $k} = $attr->{$k};
	delete $attr->{$k};
    }
    if( $name eq 'BSML' ) {

    } elsif( $name eq 'DEFINITIONS' ) {
    } elsif( $name eq 'SEQUENCES' ) {

    } elsif( $name eq 'SEQUENCE' ) {
	my ($id,$acc,$title,
	    $desc,$length,$topology,
	    $mol) =  map { $attr->{'{}'.$_}->{'Value'} } qw(ID IC-ACCKEY
							    TITLE COMMENT
							    LENGTH
							    TOPOLOGY
							    MOLECULE);
	push @{$self->{'_seendata'}->{'_seqs'}},
	$self->sequence_factory->create
	    (
	     -display_id          => $id,
	     -accession_number    => $acc,
	     -description         => $desc,
	     -length              => $length,
	     -is_circular         => ($topology =~ /^linear$/i) ? 0 : 1,
	     -molecule            => $mol,
	     );

    } elsif( $name eq 'FEATURE-TABLES' ) {
    } elsif( $name eq 'ATTRIBUTE' ) {
	my $curseq = $self->{'_seendata'}->{'_seqs'}->[-1];
	my ($name,$content) = map { $attr->{'{}'.$_}->{'Value'} } qw(NAME CONTENT);
	if($name =~ /^version$/i ) {
	    my ($version);
	    if($content =~ /^[^\.]+\.(\d+)/) {
		$version = $1;
	    } else { $version = $content }
	    $curseq->seq_version($version);
	} elsif( $name eq 'organism-species') {
	    my ($genus,$species,$subsp) = split(/\s+/,$content,3);
	    $curseq->species(Bio::Species->new(-sub_species => $subsp,
					       -classification =>
					       [$species,$genus]));
	} elsif( $name eq 'organism-classification' ) {
	    my (@class) =(split(/\s*;\s*/,$content),$curseq->species->species);
	    $curseq->species->classification([reverse @class]);
	} elsif( $name eq 'database-xref' ) {
	    my ($db,$id) = split(/:/,$content);
	    $curseq->annotation->add_Annotation('dblink',
					      Bio::Annotation::DBLink->new
						( -database  => $db,
						  -primary_id=> $id));
	} elsif( $name eq 'date-created' ||
		 $name eq 'date-last-updated' ) {
	    $curseq->add_date($content);
	}
    } elsif( $name eq 'FEATURE' ) {
	my ($id,$class,$type,$title,$display_auto)
	    =  map { $attr->{'{}'.$_}->{'Value'} } qw(ID CLASS VALUE-TYPE
						      TITLE DISPLAY-AUTO);

	push @{$self->{'_seendata'}->{'_feats'}},
	Bio::SeqFeature::Generic->new
	    ( -seq_id      => $self->{'_seendata'}->{'_seqs'}->[-1]->display_id,
	      -source_tag  => $Default_Source,
	      -primary_tag => $type,
	      -tag => {'ID'    => $id,
		   });

    } elsif( $name eq 'QUALIFIER') {
	my ($type,$value) =  map { $attr->{'{}'.$_}->{'Value'} } qw(VALUE-TYPE
								    VALUE);
	my $curfeat = $self->{'_seendata'}->{'_feats'}->[-1];
	$curfeat->add_tag_value($type,$value);
    } elsif( $name eq 'INTERVAL-LOC' ) {
	my $curfeat = $self->{'_seendata'}->{'_feats'}->[-1];
	my ($start,$end,$strand) =
	    map { $attr->{'{}'.$_}->{'Value'} } qw(STARTPOS
						   ENDPOS
						   COMPLEMENT);

	$curfeat->start($start);
	$curfeat->end($end);
	$curfeat->strand(-1) if($strand);
    } elsif( $name eq 'REFERENCE' ) {
	push @{$self->{'_seendata'}->{'_annot'}},
	Bio::Annotation::Reference->new();
    }

    push @{$self->{'_state'}}, $name;
    $self->SUPER::start_element($ele);
}

sub end_element {
    my ($self,$ele) = @_;
    pop @{$self->{'_state'}};
    my $name = uc $ele->{'LocalName'};
    my $curseq = $self->{'_seendata'}->{'_seqs'}->[-1];
    if( $name eq 'REFERENCE') {
	my $ref = pop @{$self->{'_seendata'}->{'_annot'}};
	$curseq->annotation->add_Annotation('reference',$ref);
    } elsif( $name eq 'FEATURE' ) {
	my $feat = pop @{$self->{'_seendata'}->{'_feats'}};
	$curseq->add_SeqFeature($feat);
    }
    $self->SUPER::end_element($ele);
}

sub characters {
    my ($self,$data) = @_;
    if( ! @{$self->{'_state'}} ) {
	$self->warn("Calling characters with no previous start_element call. Ignoring data");
    } else {
	my $curseq = $self->{'_seendata'}->{'_seqs'}->[-1];
	my $curfeat = $self->{'_seendata'}->{'_feats'}->[-1];
	my $curannot = $self->{'_seendata'}->{'_annot'}->[-1];
	my $name = $self->{'_state'}->[-1];
	if( $name eq 'REFAUTHORS' ) {
	    $curannot->authors($data->{'Data'});
	} elsif( $name eq 'REFTITLE') {
	    $curannot->title($data->{'Data'});
	} elsif( $name eq 'REFJOURNAL') {
	    $curannot->location($data->{'Data'});
	} elsif( $name eq 'SEQ-DATA') {
	    $data->{'Data'} =~ s/\s+//g;
	    $curseq->seq($data->{'Data'});
	}
    }
    $self->SUPER::characters($data);
}

1;