FAST/Bio/SeqIO/tinyseq.pm untested

filter: Statementstmt 0.0% 0 / 121 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 227 CC 23 SCAR 24.5
CC
23
Cov
0%
_assign_identifier
30.0
_convert_seqtype
20.0
write_seq
12.0
CRAP 11.6
1 # BioPerl module for FAST::Bio::SeqIO::tinyseq
2 #
3 # Please direct questions and support issues to <bioperl-l@bioperl.org>
4 #
5 # Cared for by Donald Jackson, donald.jackson@bms.com
6 #
7 # Copyright Bristol-Myers Squibb
8 #
9 # You may distribute this module under the same terms as perl itself
10  
11 # POD documentation - main docs before the code
12  
13 =head1 NAME
14  
15 FAST::Bio::SeqIO::tinyseq - reading/writing sequences in NCBI TinySeq format
16  
17 =head1 SYNOPSIS
18  
19 Do not use this module directly; use the SeqIO handler system:
20  
21 $stream = FAST::Bio::SeqIO->new( -file => $filename, -format => 'tinyseq' );
22  
23 while ( my $seq = $stream->next_seq ) {
24 ....
25 }
26  
27 =head1 DESCRIPTION
28  
29 This object reads and writes FAST::Bio::Seq objects to and from TinySeq XML
30 format. A TinySeq is a lightweight XML file of sequence information,
31 analgous to FASTA format.
32  
33 See L<http://www.ncbi.nlm.nih.gov/dtd/NCBI_TSeq.mod.dtd> for the DTD.
34  
35 =head1 FEEDBACK
36  
37 =head2 Mailing Lists
38  
39 User feedback is an integral part of the evolution of this and other
40 Bioperl modules. Send your comments and suggestions preferably to
41 the Bioperl mailing list. Your participation is much appreciated.
42  
43 bioperl-l@bioperl.org - General discussion
44 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
45  
46 =head2 Support
47  
48 Please direct usage questions or support issues to the mailing list:
49  
50 I<bioperl-l@bioperl.org>
51  
52 rather than to the module maintainer directly. Many experienced and
53 reponsive experts will be able look at the problem and quickly
54 address it. Please include a thorough description of the problem
55 with code and data examples if at all possible.
56  
57 =head2 Reporting Bugs
58  
59 Report bugs to the Bioperl bug tracking system to help us keep track
60 of the bugs and their resolution. Bug reports can be submitted via
61 the web:
62  
63 https://redmine.open-bio.org/projects/bioperl/
64  
65 =head1 SEE ALSO
66  
67 L<FAST::Bio::SeqIO>, L<FAST::Bio::Seq>.
68  
69 =head1 AUTHOR
70  
71 Donald Jackson, E<lt>donald.jackson@bms.comE<gt>
72  
73 Parts of this module and the test script were patterned after Sheldon
74 McKay's L<FAST::Bio::SeqIO::game>. If it breaks, however, it's my fault not
75 his ;).
76  
77 =head1 APPENDIX
78  
79 The rest of the documentation details each of the object methods.
80 Internal methods are usually preceded with a _
81  
82 =cut
83  
84 package FAST::Bio::SeqIO::tinyseq;
85  
86 use strict;
87 use FAST::Bio::Seq::SeqFastaSpeedFactory;
88 use FAST::Bio::Species;
89 use FAST::Bio::SeqIO::tinyseq::tinyseqHandler;
90 use XML::Parser::PerlSAX;
91 use XML::Writer;
92  
93 use base qw(FAST::Bio::SeqIO);
94  
95 sub _initialize {
96 my ($self, @args) = @_;
97  
98 $self->SUPER::_initialize(@args);
99  
100 unless (defined $self->sequence_factory) {
101 $self->sequence_factory(FAST::Bio::Seq::SeqFastaSpeedFactory->new());
102 }
103  
104 $self->{'_species_objects'} = {};
105 $self->{_parsed} = 0;
106 }
107  
108 =head2 next_seq
109  
110 Title : next_seq
111 Usage : $seq = $stream->next_seq()
112 Function : returns the next sequence in the stream
113 Returns : FAST::Bio::Seq object
114 Args : NONE
115  
116 =cut
117  
118 sub next_seq {
119 my ($self) = @_;
120  
121 $self->_get_seqs() unless ($self->{_parsed});
122  
123 return shift @{$self->{_seqlist}};
124 }
125  
126 =head2 write_seq
127  
128 Title : write_seq
129 Usage : $seq = $stream->write_seq(@sequence_objects); undef $stream
130 Function : outputs one or more sequence objects as TinySeq XML
131 Returns : 1 on success
132 Args : one or more sequence objects as TinySeq XML
133  
134 Because the TSeq dtd includes closing tags after all sets are written,
135 the output will not be complete until the program terminates or the
136 object is forced out of scope (see close_writer()). May not perfectly
137 reproduce TSeq_sid element for all sequences
138  
139 =cut
140  
141 sub write_seq {
142 my ($self, @seqobjs) = @_;
143  
144 $self->throw('write_seq must be called with at least one FAST::Bio::SeqI or FAST::Bio::PrimarySeqI compliant object')
145 unless (@seqobjs and ( $seqobjs[0]->isa('FAST::Bio::SeqI') || $seqobjs[0]->isa('FAST::Bio::PrimarySeqI')));
146  
147 my $writer = $self->_get_writer;
148  
149 foreach my $seqobj (@seqobjs) {
150 my ($id_element, $id_value) = $self->_get_idstring($seqobj);
151 $writer->startTag('TSeq');
152 $writer->emptyTag('TSeq_seqtype', value => $self->_convert_seqtype($seqobj));
153 $writer->dataElement('TSeq_gi', $seqobj->primary_id || '');
154 $writer->dataElement($id_element, $id_value);
155 #$writer->dataElement('TSeq_orgname', $seqobj->taxid) if ($seqobj->can('taxid'); # just a placeholder
156 $writer->dataElement('TSeq_defline', $seqobj->desc);
157 $writer->dataElement('TSeq_length', $seqobj->length);
158 $writer->dataElement('TSeq_sequence', $seqobj->seq);
159  
160 if ($seqobj->can('species') && $seqobj->species) {
161 $self->_write_species($writer, $seqobj->species);
162 }
163  
164 $writer->endTag('TSeq');
165 }
166 1;
167 }
168  
169 =head2 _get_seqs
170  
171 Title : _get_seqs
172 Usage : Internal function - use next_seq() instead
173 Function : parses the XML and creates FAST::Bio::Seq objects
174 Returns : 1 on success
175 Args : NONE
176  
177 Currently stores all sequence objects into memory. I will work on do
178 more of a stream-based approach
179  
180 =cut
181  
182 sub _get_seqs {
183 my ($self) = @_;
184  
185 my $fh = $self->_fh;
186  
187 my $handler = FAST::Bio::SeqIO::tinyseq::tinyseqHandler->new();
188 my $parser = XML::Parser::PerlSAX->new( Handler => $handler );
189  
190 my @seqatts = $parser->parse( Source => { ByteStream => $fh });
191  
192 my $factory = $self->sequence_factory;
193  
194 $self->{_seqlist} ||= [];
195 foreach my $seqatt(@seqatts) {
196 foreach my $subatt(@$seqatt) { # why are there two hashes?
197 my $seqobj = $factory->create(%$subatt);
198 $self->_assign_identifier($seqobj, $subatt);
199  
200 if ($seqobj->can('species')) {
201 # my $class = [reverse(split(/ /, $subatt->{'-organism'}))];
202 # my $species = FAST::Bio::Species->new( -classification => $class,
203 # -ncbi_taxid => $subatt->{'-taxid'} );
204 my $species = $self->_get_species($subatt->{'-organism'}, $subatt->{'-taxid'});
205 $seqobj->species($species) if ($species);
206 }
207  
208 push(@{$self->{_seqlist}}, $seqobj);
209 }
210 }
211 $self->{_parsed} = 1;
212 }
213  
214 =head2 _get_species
215  
216 Title : _get_species
217 Usage : Internal function
218 Function : gets a FAST::Bio::Species object from cache or creates as needed
219 Returns : a FAST::Bio::Species object on success, undef on failure
220 Args : a classification string (eg 'Homo sapiens') and
221 a NCBI taxon id (optional)
222  
223 Objects are cached for parsing multiple sequence files.
224  
225 =cut
226  
227 sub _get_species {
228 my ($self, $orgname, $taxid) = @_;
229  
230 unless ($self->{'_species_objects'}->{$orgname}) {
231 my $species = $self->_create_species($orgname, $taxid);
232 $self->{'_species_objects'}->{$orgname} = $species;
233 }
234 return $self->{'_species_objects'}->{$orgname};
235 }
236  
237 =head2 _create_species
238  
239 Title : _create_species
240 Usage : Internal function
241 Function : creates a FAST::Bio::Species object
242 Returns : a FAST::Bio::Species object on success, undef on failure
243 Args : a classification string (eg 'Homo sapiens') and
244 a NCBI taxon id (optional)
245  
246 =cut
247  
248 sub _create_species {
249 my ($self, $orgname, $taxid) = @_;
250 return unless ($orgname); # not required in TinySeq dtd so don't throw an error
251  
252 my %params;
253 $params{'-classification'} = [reverse(split(/ /, $orgname))];
254 $params{'-ncbi_taxid'} = $taxid if ($taxid);
255  
256 my $species = FAST::Bio::Species->new(%params)
257 or return;
258  
259 return $species;
260 }
261  
262  
263 =head2 _assign_identifier
264  
265 Title : _assign_identifier
266 Usage : Internal function
267 Function : looks for sequence accession
268 Returns : 1 on success
269 Args : NONE
270  
271 NCBI puts refseq accessions in TSeq_sid, others in TSeq_accver.
272  
273 =cut
274  
275 sub _assign_identifier {
276 my ($self, $seqobj, $atts) = @_;
277 my ($accession, $version);
278  
279 if ($atts->{'-accver'}) {
280 ($accession, $version) = split(/\./, $atts->{'-accver'});;
281 }
282 elsif ($atts->{'-sid'}) {
283 my $sidstring =$atts->{'-sid'};
284 $sidstring =~ s/^.+?\|//;
285 $sidstring =~ s/\|[^\|]*//;
286 ($accession, $version) = split(/\./, $sidstring);;
287 }
288 else {
289 $self->throw('NO accession information found for this sequence');
290 }
291 $seqobj->accession_number($accession) if ($seqobj->can('accession_number'));
292 $seqobj->version($version) if ($seqobj->can('version'));
293  
294 }
295  
296 =head2 _convert_seqtype
297  
298 Title : _convert_seqtype
299 Usage : Internal function
300 Function : maps FAST::Bio::Seq::alphabet() values [dna/rna/protein] onto
301 TSeq_seqtype values [protein/nucleotide]
302  
303 =cut
304  
305 sub _convert_seqtype {
306 my ($self, $seqobj) = @_;
307  
308 return 'protein' if ($seqobj->alphabet eq 'protein');
309 return 'nucleotide' if ($seqobj->alphabet eq 'dna');
310 return 'nucleotide' if ($seqobj->alphabet eq 'rna');
311  
312 # if we get here there's a problem!
313 $self->throw("Alphabet not defined, can't assign type for $seqobj");
314 }
315  
316 =head2 _get_idstring
317  
318 Title : _get_idstring
319 Usage : Internal function
320 Function : parse accession and version info from TSeq_accver
321 or TSeq_sid
322  
323 =cut
324  
325 sub _get_idstring {
326 # NCBI puts refseq ids in TSeq_sid, others in TSeq_accver. No idea why.
327 my ($self, $seqobj) = @_;
328 my $accver = $seqobj->accession_number;
329 $accver .= '.' . $seqobj->version if ($seqobj->can('version') and $seqobj->version);
330 if ($accver =~ /^(NM_|NP_|XM_|XP_|NT_|NC_|NG_)/) {
331 return ('TSeq_sid', join('|', 'ref', $accver, ''));
332 }
333 else {
334 return ('TSeq_accver', $accver);
335 }
336 }
337  
338 =head2 _get_writer
339  
340 Title : _get_writer
341 Usage : Internal function
342 Function : instantiate XML::Writer object if needed,
343 output initial XML
344  
345 =cut
346  
347 sub _get_writer {
348 # initialize writer, start doc so write_seq can work one at a time
349 my ($self) = @_;
350  
351 unless ($self->{_writer}) {
352 my $fh = $self->_fh;
353 my $writer = XML::Writer->new(OUTPUT => $fh,
354 DATA_MODE => 1,
355 DATA_INDENT => 2,
356 NEWLINE => 1,
357 );
358 $writer->doctype('TSeqSet', '-//NCBI//NCBI TSeq/EN', 'http://www.ncbi.nlm.nih.gov/dtd/NCBI_TSeq.dtd');
359  
360 $writer->comment("Generated by FAST::Bio::SeqIO::tinyseq VERSION ".$FAST::Bio::SeqIO::tinyseq::VERSION);
361  
362 $writer->startTag('TSeqSet');
363  
364 $self->{_writer} = $writer;
365 }
366 return $self->{_writer};
367 }
368  
369 =head2 close_writer
370  
371 Title : close_writer
372 Usage : $self->close_writer()
373 Function : terminate XML output
374 Args : NONE
375 Returns : 1 on success
376  
377 Called automatically by DESTROY when object goes out of scope
378  
379 =cut
380  
381 sub close_writer {
382 # close out any dangling writer
383 my ($self) = @_;
384 if ($self->{_writer}) {
385 my $writer = $self->{_writer};
386 $writer->endTag('TSeqSet');
387 $writer->end;
388 undef $writer;
389 }
390 close($self->_fh) if ($self->_fh);
391 1;
392 }
393  
394 sub _write_species {
395 my ($self, $writer, $species) = @_;
396 $writer->dataElement('TSeq_orgname', $species->binomial);
397 $writer->dataElement('TSeq_taxid', $species->ncbi_taxid)
398 if($species->ncbi_taxid);
399 }
400  
401 sub DESTROY {
402 # primarily to close out a writer!
403 my ($self) = @_;
404 $self->close_writer;
405 undef $self;
406 }
407  
408 1;
409 __END__