2 # BioPerl module for Bio::SeqIO::tigrxml
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Jason Stajich <jason-at-bioperl-dot-org>
8 # Copyright Jason Stajich
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
16 Bio::SeqIO::tigrxml - Parse TIGR (new) XML
21 my $in = Bio::SeqIO->new(-format => 'tigrcoordset',
24 while( my $seq = $in->next_seq ) {
30 This is a parser for TIGR Coordset XML for their in-progress
37 User feedback is an integral part of the evolution of this and other
38 Bioperl modules. Send your comments and suggestions preferably to
39 the Bioperl mailing list. Your participation is much appreciated.
41 bioperl-l@bioperl.org - General discussion
42 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
46 Please direct usage questions or support issues to the mailing list:
48 I<bioperl-l@bioperl.org>
50 rather than to the module maintainer directly. Many experienced and
51 reponsive experts will be able look at the problem and quickly
52 address it. Please include a thorough description of the problem
53 with code and data examples if at all possible.
57 Report bugs to the Bioperl bug tracking system to help us keep track
58 of the bugs and their resolution. Bug reports can be submitted via the
61 https://github.com/bioperl/bioperl-live/issues
63 =head1 AUTHOR - Jason Stajich
65 Email jason-at-bioperl-dot-org
69 The rest of the documentation details each of the object methods.
70 Internal methods are usually preceded with a _
75 # Let the code begin...
78 package Bio
::SeqIO
::tigrxml
;
79 use vars
qw($Default_Source);
84 use Bio::Seq::SeqFactory;
86 use Bio::SeqFeature::Generic;
87 use Bio::Annotation::Reference;
88 use Bio::Annotation::Comment;
89 use Bio::Annotation::DBLink;
90 use List::Util qw(min max);
92 use base
qw(Bio::SeqIO XML::SAX::Base);
95 $Default_Source = 'TIGR';
99 $self->SUPER::_initialize
(@_);
100 $self->{'_parser'} = XML
::SAX
::ParserFactory
->parser('Handler' => $self);
101 if( ! defined $self->sequence_factory ) {
102 $self->sequence_factory(Bio
::Seq
::SeqFactory
->new
103 (-verbose
=> $self->verbose(),
104 -type
=> 'Bio::Seq::RichSeq'));
111 if( @
{$self->{'_seendata'}->{'_seqs'} || []} ||
113 return shift @
{$self->{'_seendata'}->{'_seqs'}};
115 $self->{'_parser'}->parse_file($self->_fh);
116 return shift @
{$self->{'_seendata'}->{'_seqs'}};
119 # XML::SAX::Base methods
122 my ($self,$doc) = @_;
123 $self->{'_seendata'} = {'_seqs' => [],
126 $self->SUPER::start_document
($doc);
130 my ($self,$doc) = @_;
131 $self->SUPER::end_document
($doc);
135 my ($self,$ele) = @_;
137 my $name = uc $ele->{'LocalName'};
138 my $attr = $ele->{'Attributes'};
139 my $seqid = defined $self->{'_seendata'}->{'_seqs'}->[-1] ?
140 $self->{'_seendata'}->{'_seqs'}->[-1]->display_id : undef;
142 # we're going to try and be SO-nice here
143 if( $name eq 'ASSEMBLY' ) { # New sequence
144 my ($len) = $attr->{'{}COORDS'}->{'Value'} =~ /\d+\-(\d+)/;
145 push @
{$self->{'_seendata'}->{'_seqs'}},
146 $self->sequence_factory->create
148 -display_id
=> $attr->{'{}ASMBL_ID'}->{'Value'},
151 } elsif( $name eq 'HEADER' ) {
152 } elsif( $name eq 'CLONE_NAME' ) {
153 } elsif( $name eq 'ORGANISM' ) {
154 } elsif( $name eq 'AUTHOR_LIST' ) {
155 $self->{'_seendata'}->{'_authors'} = [];
156 } elsif( $name eq 'TU' ) { # gene feature
157 my ($s,$e) = ($attr->{'{}COORDS'}->{'Value'} =~ /(\d+)\-(\d+)/);
160 ($s,$e,$strand) = ( $e,$s,-1);
162 my $fname = $attr->{'{}FEAT_NAME'}->{'Value'};
163 my $f = Bio
::SeqFeature
::Generic
->new
168 -primary_tag
=> 'gene', # what does this really map to?
169 -source_tag
=> $Default_Source,
171 'Note' => $attr->{'{}COM_NAME'}->{'Value'},
173 'locus' => $attr->{'{}LOCUS'}->{'Value'},
174 'pub_locus' => $attr->{'{}PUB_LOCUS'}->{'Value'},
175 'alt_locus' => $attr->{'{}ALT_LOCUS'}->{'Value'},
176 'pub_comment' => $attr->{'{}PUB_COMMENT'}->{'Value'},
179 push @
{$self->{'_seendata'}->{'_feats'}}, $f;
180 # add this feature to the current sequence
181 $self->{'_seendata'}->{'_seqs'}->[-1]->add_SeqFeature($f);
182 } elsif( $name eq 'MODEL' ) { # mRNA/transcript
184 $self->{'_seendata'}->{"five_prime_UTR"}= undef;
185 $self->{'_seendata'}->{"three_prime_UTR"} = undef;
186 my ($s,$e) = ($attr->{'{}COORDS'}->{'Value'} =~ /(\d+)\-(\d+)/);
189 ($s,$e,$strand) = ( $e,$s,-1);
191 my $parent = $self->{'_seendata'}->{'_feats'}->[-1];
192 my ($parentid) = $parent->get_tag_values('ID');
193 my $f = Bio
::SeqFeature
::Generic
->new
194 (-primary_tag
=> 'transcript',
195 -source_tag
=> $Default_Source,
196 -start
=> $s, # we use parent start/stop because 'MODEL' means CDS start/stop
197 -end
=> $e, # but we want to reflect
201 'ID' => $attr->{'{}FEAT_NAME'}->{'Value'},
202 'Parent' => $parentid,
203 'Note' => $attr->{'{}COMMENT'}->{'Value'},
205 $parent->add_SeqFeature($f);
206 push @
{$self->{'_seendata'}->{'_feats'}}, $f;
207 $self->{'_seendata'}->{'_seqs'}->[-1]->add_SeqFeature($f);
208 } elsif( $name eq 'EXON' ) { # exon feature
209 my ($s,$e) = ($attr->{'{}COORDS'}->{'Value'} =~ /(\d+)\-(\d+)/);
212 ($s,$e,$strand) = ( $e,$s,-1);
214 my $parent = $self->{'_seendata'}->{'_feats'}->[-1];
216 my ($parentid) = $parent->get_tag_values('ID');
218 my $f = Bio
::SeqFeature
::Generic
->new
219 (-primary_tag
=> 'exon',
220 -source_tag
=> $Default_Source,
226 'ID' => $attr->{'{}FEAT_NAME'}->{'Value'},
227 'Parent' => $parentid,
229 $parent->add_SeqFeature($f,'EXPAND');
230 $self->{'_seendata'}->{'_seqs'}->[-1]->add_SeqFeature($f);
231 # we'll still just add exons to the transcript
232 } elsif( $name eq 'PROTEIN_SEQ' ) {
234 } elsif( $name eq 'CDS' ) {
235 # CDS will be the translation of the transcript
236 my ($s,$e) = ($attr->{'{}COORDS'}->{'Value'} =~ /(\d+)\-(\d+)/);
239 ($s,$e,$strand) = ( $e,$s,-1);
241 my $parent = $self->{'_seendata'}->{'_feats'}->[-1];
242 my ($parentid) = $parent->get_tag_values('ID');
243 $self->assert($parent->primary_tag eq 'transcript', 'Testing for primary tag equivalent to mRNA');
244 $self->assert($parent->strand == $strand || abs($s-$e) == 0, 'Testing that parent feature and current feature strand are equal '. $parentid. ' '.$attr->{'{}FEAT_NAME'}->{'Value'});
245 my $f = Bio
::SeqFeature
::Generic
->new
246 (-primary_tag
=> 'CDS',
247 -source_tag
=> $Default_Source,
251 -strand
=> $parent->strand,
253 'ID' => $attr->{'{}FEAT_NAME'}->{'Value'},
254 'Parent' => $parentid, # should be the mRNA
256 $parent->add_SeqFeature($f);
257 $self->{'_seendata'}->{'_seqs'}->[-1]->add_SeqFeature($f);
258 } elsif( $name eq 'RNA-EXON' ) {
260 my ($s,$e) = ($attr->{'{}COORDS'}->{'Value'} =~ /(\d+)\-(\d+)/);
263 ($s,$e,$strand) = ( $e,$s,-1);
265 my $parent = $self->{'_seendata'}->{'_feats'}->[-1];
266 my ($parentid) = $parent->get_tag_values('ID');
267 my $f = Bio
::SeqFeature
::Generic
->new
268 (-primary_tag
=> 'tRNA_exon', # tRNA_exon?
269 -source_tag
=> $Default_Source,
275 'ID' => $attr->{'{}FEAT_NAME'}->{'Value'},
276 'Parent' => $parentid,
279 $parent->add_SeqFeature($f);
280 $self->{'_seendata'}->{'_seqs'}->[-1]->add_SeqFeature($f);
281 } elsif( $name eq 'PRE-TRNA' ) { # tRNA gene
282 my ($s,$e) = ( $attr->{'{}COORDS'}->{'Value'} =~/(\d+)\-(\d+)/);
285 ($s,$e,$strand) = ( $e,$s,-1);
287 my $f = Bio
::SeqFeature
::Generic
->new
288 ( -primary_tag
=> 'tRNA_coding_gene',
289 -source_tag
=> $Default_Source,
294 -tag
=> {'ID' => $attr->{'{}FEAT_NAME'}->{'Value'},
297 push @
{$self->{'_seendata'}->{'_feats'}}, $f;
298 $self->{'_seendata'}->{'_seqs'}->[-1]->add_SeqFeature($f);
299 } elsif( $name eq 'TRNA' ) { # tRNA transcript
300 my ($s,$e) = ($attr->{'{}COORDS'}->{'Value'} =~ /(\d+)\-(\d+)/);
303 ($s,$e,$strand) = ( $e,$s,-1);
305 my $parent = $self->{'_seendata'}->{'_feats'}->[-1];
306 my ($parentid) = $parent->get_tag_values('ID');
307 my $f = Bio
::SeqFeature
::Generic
->new
308 (-primary_tag
=> 'tRNA_primary_transcript',
309 -source_tag
=> $Default_Source,
315 'ID' => $attr->{'{}FEAT_NAME'}->{'Value'},
316 'Parent' => $parentid,
317 'Note' => $attr->{'{}COM_NAME'}->{'Value'},
318 'anticodon' => $attr->{'{}ANTICODON'}->{'Value'},
319 'pub_locus' => $attr->{'{}PUB_LOCUS'}->{'Value'},
322 $parent->add_SeqFeature($f);
323 push @
{$self->{'_seendata'}->{'_feats'}}, $f;
324 $self->{'_seendata'}->{'_seqs'}->[-1]->add_SeqFeature($f);
325 } elsif( $name eq 'REPEAT_LIST' ) {
326 } elsif( $name eq 'REPEAT' ) {
327 my ($s,$e) = ($attr->{'{}COORDS'}->{'Value'} =~ /(\d+)\-(\d+)/);
330 ($s,$e,$strand) = ( $e,$s,-1);
332 my $f = Bio
::SeqFeature
::Generic
->new
333 (-primary_tag
=> 'simple_repeat',
334 -source_tag
=> $Default_Source,
340 'ID' => $attr->{'{}FEAT_NAME'}->{'Value'},
343 push @
{$self->{'_seendata'}->{'_feats'}}, $f;
344 $self->{'_seendata'}->{'_seqs'}->[-1]->add_SeqFeature($f);
345 } elsif ( $name eq 'AUTHOR' ) {
346 } elsif( $name eq 'GB_DESCRIPTION' ) {
348 } elsif( $name eq 'GB_COMMENT' ) {
349 } elsif( $name eq 'LINEAGE' ) {
352 $self->warn("Unknown element $name, ignored\n");
354 push @
{$self->{'_state'}}, $name;
355 $self->SUPER::start_element
($ele);
359 my ($self,$ele) = @_;
360 pop @
{$self->{'_state'}};
361 my $name = $ele->{'LocalName'};
362 my $curseq = $self->{'_seendata'}->{'_seqs'}->[-1];
363 if( $name eq 'AUTHOR_LIST' ) {
364 if( $curseq->can('annotation') ) {
365 $curseq->annotation->add_Annotation
366 ('reference',Bio
::Annotation
::Reference
->new
367 (-authors
=> join(',',@
{$self->{'_seendata'}->{'_authors'}}))
370 $self->{'_seendata'}->{'_authors'} = [];
371 } elsif( $name eq 'ASSEMBLY' ) {
372 if( @
{$self->{'_seendata'}->{'_feats'} || []} ) {
373 $self->warn("Leftover features which were not finished!");
375 $self->debug("end element for ASSEMBLY ". $curseq->display_id. "\n");
376 } elsif( $name eq 'TU' ||
377 $name eq 'TRNA' || $name eq 'PRE-TRNA' ||
378 $name eq 'REPEAT' ) {
379 pop @
{$self->{'_seendata'}->{'_feats'}};
380 } elsif( $name eq 'MODEL' ) {
381 # This is all to for adding UTRs
383 my $model = pop @
{$self->{'_seendata'}->{'_feats'}};
384 my $curseq = $self->{'_seendata'}->{'_seqs'}->[-1];
385 # sort smallest to largest, don't forget about
387 my ($parentid) = $model->get_tag_values('Parent');
389 my @features = $model->get_SeqFeatures();
390 my @exons = sort { $a->start <=> $b->start }
391 grep { $_->primary_tag eq 'exon' } @features;
393 my @cdsexons = sort { $a->start <=> $b->start }
394 grep { $_->primary_tag eq 'CDS' } @features;
396 # look at the exons, find those which come after the model start
397 my $cdsexon = shift @cdsexons;
398 my $exon = shift @exons; # first exon
399 if( ! defined $cdsexon ) {
400 $self->warn( "no CDS exons $parentid!");
402 } elsif( ! defined $exon ) {
403 $self->warn("no exons $parentid!" );
407 while( defined $exon && $exon->start < $cdsexon->start ) {
408 my ($pid) = $exon->get_tag_values('Parent');
409 $self->debug("LeftPhase: tu-id $parentid mrna-id $pid exon is ".
410 $exon->location->to_FTstring.
411 " CDSexon is ".$cdsexon->location->to_FTstring."\n");
413 my $utr = Bio
::SeqFeature
::Generic
->new
414 (-seq_id
=> $exon->seq_id,
415 -strand
=> $exon->strand,
416 -primary_tag
=> $exon->strand > 0 ?
"five_prime_UTR" : "three_prime_UTR",
417 -source_tag
=> $Default_Source,
419 'ID' => "$pid.UTR".$utrct++,
423 if( $utr->primary_tag eq 'five_prime_UTR' ) {
425 $ne = min
( $exon->end, $cdsexon->start - 1);
427 $ne = min
( $exon->end, $cdsexon->start - 1);
430 $utr->start($ns); $utr->end($ne);
431 $model->add_SeqFeature($utr);
432 $curseq->add_SeqFeature($utr);
433 $exon = shift @exons;
435 @exons = sort { $a->start <=> $b->start }
436 grep {$_->primary_tag eq 'exon' } @features;
437 @cdsexons = sort { $a->start <=> $b->start }
438 grep { $_->primary_tag eq 'CDS' } @features;
440 $cdsexon = pop @cdsexons;
442 if( ! defined $cdsexon ) {
443 $self->warn( "no CDS exons $parentid!");
445 } elsif( ! defined $exon ) {
446 $self->warn("no exons $parentid!" );
450 while( defined $exon &&$exon->end > $cdsexon->end ) {
451 my ($pid) = $exon->get_tag_values('Parent');
452 $self->debug("RightPhase: tu-id $parentid mrna-id $pid exon is ".
453 $exon->location->to_FTstring.
454 " CDSexon is ".$cdsexon->location->to_FTstring."\n");
456 my $utr = Bio
::SeqFeature
::Generic
->new
457 (-seq_id
=> $exon->seq_id,
458 -strand
=> $exon->strand,
459 -primary_tag
=> $exon->strand < 0 ?
"five_prime_UTR" : "three_prime_UTR",
460 -source_tag
=> $Default_Source,
463 'ID' => "$pid.UTR".$utrct++,
467 if( $utr->primary_tag eq 'three_prime_UTR' ) {
468 $ns = max
( $exon->start, $cdsexon->end + 1);
471 $ns = $cdsexon->end+1;
472 $ne = max
( $exon->end, $cdsexon->start + 1);
474 $utr->start($ns); $utr->end($ne);
476 $model->add_SeqFeature($utr);
477 $curseq->add_SeqFeature($utr);
481 $self->SUPER::end_element
($ele);
485 my ($self,$data) = @_;
486 if( ! @
{$self->{'_state'}} ) {
487 $self->warn("Calling characters with no previous start_element call. Ignoring data");
489 my $curseq = $self->{'_seendata'}->{'_seqs'}->[-1];
490 my $curfeat = $self->{'_seendata'}->{'_feats'}->[-1];
491 my $name = $self->{'_state'}->[-1];
492 if( defined $curseq ) {
493 if( $name eq 'CLONE_NAME' ) {
494 $self->debug("Clone name is ",$data->{'Data'}, "\n");
495 $curseq->display_id($data->{'Data'});
496 } elsif( $name eq 'ORGANISM' ) {
497 my ($genus,$species,$subspec) = split(/\s+/,$data->{Data
},3);
498 $curseq->species(Bio
::Species
->new(
501 -sub_species
=> $species));
502 } elsif( $name eq 'LINEAGE' ) {
503 $curseq->species->classification(
505 $curseq->species->species,
506 $curseq->species->genus,
507 reverse (map { s/^\s+//;
509 split /[;\.]+/,$data->{'Data'} ),
512 } elsif( $name eq 'AUTHOR' ) {
513 push @
{$self->{'_seendata'}->{'_authors'}}, $data->{'Data'};
516 if( defined $curfeat ) {
517 if( $name eq 'EXON' ) { # exon feature
518 } elsif( $name eq 'RNA-EXON' ) {
520 } elsif( $name eq 'PROTEIN_SEQ' ) {
521 $curfeat->add_tag_value('translation',$data->{'Data'});
522 } elsif( $name eq 'CDS' ) {
523 } elsif( $name eq 'PRE-TRNA' ) { # tRNA gene
524 } elsif( $name eq 'TRNA' ) { # tRNA transcript
525 } elsif( $name eq 'REPEAT_LIST' ) {
526 } elsif( $name eq 'REPEAT' ) {
527 $curfeat->add_tag_value('Note',$data->{'Data'});
528 } elsif( $name eq 'GB_COMMENT' ) {
529 $curseq->annotation->add_Annotation
531 Bio
::Annotation
::Comment
->new(-text
=> $data->{'Data'}));
532 } elsif( $name eq 'GB_DESCRIPTION' ) {
533 $curseq->description($data->{'Data'});
537 $self->SUPER::characters
($data);
542 my ($self,$test,$msg) = @_;
543 $self->throw($msg) unless $test;