Bio::Tools::CodonTable and Bio::Tools::IUPAC: use our and drop BEGIN blocks.
[bioperl-live.git] / lib / Bio / Tools / GFF.pm
blobf7a6a46ecb29c4aaebf3b8724415deeb6c7e82b7
2 # BioPerl module for Bio::Tools::GFF
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by the Bioperl core team
8 # Copyright Matthew Pocock
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
14 =head1 NAME
16 Bio::Tools::GFF - A Bio::SeqAnalysisParserI compliant GFF format parser
18 =head1 SYNOPSIS
20 use Bio::Tools::GFF;
22 # specify input via -fh or -file
23 my $gffio = Bio::Tools::GFF->new(-fh => \*STDIN, -gff_version => 2);
24 my $feature;
25 # loop over the input stream
26 while($feature = $gffio->next_feature()) {
27 # do something with feature
29 $gffio->close();
31 # you can also obtain a GFF parser as a SeqAnalasisParserI in
32 # HT analysis pipelines (see Bio::SeqAnalysisParserI and
33 # Bio::Factory::SeqAnalysisParserFactory)
34 my $factory = Bio::Factory::SeqAnalysisParserFactory->new();
35 my $parser = $factory->get_parser(-input => \*STDIN, -method => "gff");
36 while($feature = $parser->next_feature()) {
37 # do something with feature
40 =head1 DESCRIPTION
42 This class provides a simple GFF parser and writer. In the sense of a
43 SeqAnalysisParser, it parses an input file or stream into SeqFeatureI
44 objects, but is not in any way specific to a particular analysis
45 program and the output that program produces.
47 That is, if you can get your analysis program spit out GFF, here is
48 your result parser.
50 =head1 GFF3 AND SEQUENCE DATA
52 GFF3 supports sequence data; see
54 http://www.sequenceontology.org/gff3.shtml
56 There are a number of ways to deal with this -
58 If you call
60 $gffio->ignore_sequence(1)
62 prior to parsing the sequence data is ignored; this is useful if you
63 just want the features. It avoids the memory overhead in building and
64 caching sequences
66 Alternatively, you can call either
68 $gffio->get_seqs()
72 $gffio->seq_id_by_h()
74 At the B<end> of parsing to get either a list or hashref of Bio::Seq
75 objects (see the documentation for each of these methods)
77 Note that these objects will not have the features attached - you have
78 to do this yourself, OR call
80 $gffio->features_attached_to_seqs(1)
82 PRIOR to parsing; this will ensure that the Seqs have the features
83 attached; ie you will then be able to call
85 $seq->get_SeqFeatures();
87 And use Bio::SeqIO methods
89 Note that auto-attaching the features to seqs will incur a higher
90 memory overhead as the features must be cached until the sequence data
91 is found
93 =head1 TODO
95 Make a Bio::SeqIO class specifically for GFF3 with sequence data
97 =head1 FEEDBACK
99 =head2 Mailing Lists
101 User feedback is an integral part of the evolution of this and other
102 Bioperl modules. Send your comments and suggestions preferably to one
103 of the Bioperl mailing lists. Your participation is much appreciated.
105 bioperl-l@bioperl.org - General discussion
106 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
108 =head2 Support
110 Please direct usage questions or support issues to the mailing list:
112 I<bioperl-l@bioperl.org>
114 rather than to the module maintainer directly. Many experienced and
115 reponsive experts will be able look at the problem and quickly
116 address it. Please include a thorough description of the problem
117 with code and data examples if at all possible.
119 =head2 Reporting Bugs
121 Report bugs to the Bioperl bug tracking system to help us keep track
122 the bugs and their resolution. Bug reports can be submitted the web:
124 https://github.com/bioperl/bioperl-live/issues
126 =head1 AUTHOR - Matthew Pocock
128 Email mrp-at-sanger.ac.uk
130 =head1 CONTRIBUTORS
132 Jason Stajich, jason-at-biperl-dot-org
133 Chris Mungall, cjm-at-fruitfly-dot-org
134 Steffen Grossmann [SG], grossman at molgen.mpg.de
135 Malcolm Cook, mec-at-stowers-institute.org
137 =head1 APPENDIX
139 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
141 =cut
143 # Let the code begin...
145 package Bio::Tools::GFF;
147 use vars qw($HAS_HTML_ENTITIES);
148 use strict;
150 use Bio::Seq::SeqFactory;
151 use Bio::LocatableSeq;
152 use Bio::SeqFeature::Generic;
154 use base qw(Bio::Root::Root Bio::SeqAnalysisParserI Bio::Root::IO);
156 my $i = 0;
157 my %GFF3_ID_Tags = map { $_ => $i++ } qw(ID Parent Target);
159 # for skipping data that may be represented elsewhere; currently, this is
160 # only the score
161 my %SKIPPED_TAGS = map { $_ => 1 } qw(score);
164 =head2 new
166 Title : new
167 Usage : my $parser = Bio::Tools::GFF->new(-gff_version => 2,
168 -file => "filename.gff");
170 my $writer = Bio::Tools::GFF->new(-gff_version => 3,
171 -file => ">filename.gff3");
172 Function: Creates a new instance. Recognized named parameters are -file, -fh,
173 and -gff_version.
174 Returns : a new object
175 Args : named parameters
176 -gff_version => [1,2,3]
178 =cut
180 { # make a class variable such that we can generate unique ID's over
181 # a session, no matter how many instances of GFF.pm we make
182 # since these have to be unique within the scope of a GFF file.
184 my $gff3_featureID = 0;
186 sub _incrementGFF3ID {
187 my ($self) = @_;
188 return ++ $gff3_featureID;
193 sub new {
194 my ($class, @args) = @_;
195 my $self = $class->SUPER::new(@args);
197 my ($gff_version, $noparse) = $self->_rearrange([qw(GFF_VERSION NOPARSE)],@args);
199 # initialize IO
200 $self->_initialize_io(@args);
201 $self->_parse_header() unless $noparse;
203 $gff_version ||= 2;
204 if( ! $self->gff_version($gff_version) ) {
205 $self->throw("Can't build a GFF object with the unknown version ".
206 $gff_version);
208 $self->{'_first'} = 1;
209 return $self;
213 =head2 _parse_header
215 Title : _parse_header
216 Usage : $gffio->_parse_header()
217 Function: used to turn parse GFF header lines. currently
218 produces Bio::LocatableSeq objects from ##sequence-region
219 lines
220 Returns : 1 on success
221 Args : none
224 =cut
226 sub _parse_header{
227 my ($self) = @_;
229 my @unhandled;
230 local $^W = 0; # hide warnings when we try and parse from a file opened
231 # for writing - there isn't really a better way to do
232 # AFAIK - cannot detech if a FH is read or write.
233 while(my $line = $self->_readline()){
234 my $handled = 0;
235 next if /^\s+$/;
236 if($line =~ /^\#\#sequence-region\s+(\S+)\s+(\S+)\s+(\S+)\s*/){
237 my($seqid,$start,$end) = ($1,$2,$3);
238 push @{ $self->{'segments'} }, Bio::LocatableSeq->new(
239 -id => unescape($seqid),
240 -start => $start,
241 -end => $end,
242 -length => ($end - $start + 1), ## make the length explicit
244 $handled = 1;
245 } elsif($line =~ /^(\#\#feature-ontology)/) {
246 #to be implemented
247 $self->warn("$1 header tag parsing unimplemented");
248 } elsif($line =~ /^(\#\#attribute-ontology)/) {
249 #to be implemented
250 $self->warn("$1 header tag parsing unimplemented");
251 } elsif($line =~ /^(\#\#source-ontology)/) {
252 #to be implemented
253 $self->warn("$1 header tag parsing unimplemented");
254 } elsif($line =~ /^(\#\#\#)/) {
255 #to be implemented
256 $self->warn("$1 header tag parsing unimplemented");
257 } elsif($line =~ /^(\#\#FASTA)/) {
258 # initial ##FASTA is optional - artemis does not use it
259 $line = $self->_readline();
260 if ($line !~ /^\>(\S+)/) {
261 $self->throw("##FASTA directive must be followed by fasta header, not: $line");
265 if ($line =~ /^\>(.*)/) {
266 # seq data can be at header or footer
267 my $seq = $self->_parse_sequence($line);
268 if ($seq) {
269 $self->_seq_by_id_h->{$seq->primary_id} = $seq;
274 if(!$handled){
275 push @unhandled, $line;
278 #looks like the header is over!
279 last unless $line =~ /^\#/;
282 foreach my $line (@unhandled){
283 $self->_pushback($line);
286 return 1;
289 sub _parse_sequence {
290 my ($self, $line) = @_;
292 if ($line =~ /^\>(.*)/) {
294 my $seqid = $1;
295 $seqid =~ s/\s+$//;
296 my $desc = '';
297 if ($seqid =~ /(\S+)\s+(.*)/) {
298 ($seqid, $desc) = ($1,$2);
300 my $res = '';
301 while (my $line = $self->_readline) {
302 if ($line =~ /^\#/) {
303 last;
305 if ($line =~ /^\>/) {
306 $self->_pushback($line);
307 last;
309 $line =~ s/\s//g;
310 $res .= $line;
312 return if $self->ignore_sequence;
314 my $seqfactory = Bio::Seq::SeqFactory->new('Bio::Seq');
315 my $seq = $seqfactory->create(-seq=>$res,
316 -id=>$seqid,
317 -desc=>$desc);
318 $seq->accession_number($seqid);
319 if ($self->features_attached_to_seqs) {
320 my @feats =
321 @{$self->_feature_idx_by_seq_id->{$seqid}};
322 $seq->add_SeqFeature($_) foreach @feats;
323 @{$self->_feature_idx_by_seq_id->{$seqid}} = ();
325 return $seq;
327 else {
328 $self->throw("expected fasta header, not: $line");
333 =head2 next_segment
335 Title : next_segment
336 Usage : my $seq = $gffio->next_segment;
337 Function: Returns a Bio::LocatableSeq object corresponding to a
338 GFF "##sequence-region" header line.
339 Example :
340 Returns : A Bio::LocatableSeq object, or undef if
341 there are no more sequences.
342 Args : none
345 =cut
347 sub next_segment{
348 my ($self,@args) = @_;
349 return shift @{ $self->{'segments'} } if defined $self->{'segments'};
350 return;
354 =head2 next_feature
356 Title : next_feature
357 Usage : $seqfeature = $gffio->next_feature();
358 Function: Returns the next feature available in the input file or stream, or
359 undef if there are no more features.
360 Example :
361 Returns : A Bio::SeqFeatureI implementing object, or undef if there are no
362 more features.
363 Args : none
365 =cut
367 sub next_feature {
368 my ($self) = @_;
370 my $gff_string;
372 # be graceful about empty lines or comments, and make sure we return undef
373 # if the input's consumed
374 while(($gff_string = $self->_readline()) && defined($gff_string)) {
375 if ($gff_string =~ /^\#\#\#/) {
376 # all forward refs have been seen; TODO
378 next if($gff_string =~ /^\#/ || $gff_string =~ /^\s*$/ ||
379 $gff_string =~ m{^//});
381 while ($gff_string =~ /^\>(.+)/) {
382 # fasta can be in header or footer
383 my $seq = $self->_parse_sequence($gff_string);
384 if ($seq) {
385 $self->_seq_by_id_h->{$seq->primary_id} = $seq;
386 $gff_string = $self->_readline;
387 last unless $gff_string;
390 last;
392 return unless $gff_string;
394 my $feat = Bio::SeqFeature::Generic->new();
395 $self->from_gff_string($feat, $gff_string);
397 if ($self->features_attached_to_seqs) {
398 push(@{$self->_feature_idx_by_seq_id->{$feat->seq_id}},
399 $feat);
402 return $feat;
405 sub _feature_idx_by_seq_id {
406 my $self = shift;
407 $self->{__feature_idx_by_seq_id} = shift if @_;
408 $self->{__feature_idx_by_seq_id} = {}
409 unless $self->{__feature_idx_by_seq_id};
410 return $self->{__feature_idx_by_seq_id};
414 =head2 from_gff_string
416 Title : from_gff_string
417 Usage : $gff->from_gff_string($feature, $gff_string);
418 Function: Sets properties of a SeqFeatureI object from a GFF-formatted
419 string. Interpretation of the string depends on the version
420 that has been specified at initialization.
422 This method is used by next_feature(). It actually dispatches to
423 one of the version-specific (private) methods.
424 Example :
425 Returns : void
426 Args : A Bio::SeqFeatureI implementing object to be initialized
427 The GFF-formatted string to initialize it from
429 =cut
431 sub from_gff_string {
432 my ($self, $feat, $gff_string) = @_;
434 if($self->gff_version() == 1) {
435 return $self->_from_gff1_string($feat, $gff_string);
436 } elsif( $self->gff_version() == 3 ) {
437 return $self->_from_gff3_string($feat, $gff_string);
438 } else {
439 return $self->_from_gff2_string($feat, $gff_string);
444 =head2 _from_gff1_string
446 Title : _from_gff1_string
447 Usage :
448 Function:
449 Example :
450 Returns : void
451 Args : A Bio::SeqFeatureI implementing object to be initialized
452 The GFF-formatted string to initialize it from
454 =cut
456 sub _from_gff1_string {
457 my ($gff, $feat, $string) = @_;
458 chomp $string;
459 my ($seqname, $source, $primary, $start, $end, $score,
460 $strand, $frame, @group) = split(/\t/, $string);
462 if ( !defined $frame ) {
463 $feat->throw("[$string] does not look like GFF to me");
465 $frame = 0 unless( $frame =~ /^\d+$/);
466 $feat->seq_id($seqname);
467 $feat->source_tag($source);
468 $feat->primary_tag($primary);
469 $feat->start($start);
470 $feat->end($end);
471 $feat->frame($frame);
472 if ( $score eq '.' ) {
473 #$feat->score(undef);
474 } else {
475 $feat->score($score);
477 if ( $strand eq '-' ) { $feat->strand(-1); }
478 if ( $strand eq '+' ) { $feat->strand(1); }
479 if ( $strand eq '.' ) { $feat->strand(0); }
480 foreach my $g ( @group ) {
481 if ( $g =~ /(\S+)=(\S+)/ ) {
482 my $tag = $1;
483 my $value = $2;
484 $feat->add_tag_value($1, $2);
485 } else {
486 $feat->add_tag_value('group', $g);
492 =head2 _from_gff2_string
494 Title : _from_gff2_string
495 Usage :
496 Function:
497 Example :
498 Returns : void
499 Args : A Bio::SeqFeatureI implementing object to be initialized
500 The GFF2-formatted string to initialize it from
503 =cut
505 sub _from_gff2_string {
506 my ($gff, $feat, $string) = @_;
507 chomp($string);
509 # according to the Sanger website, GFF2 should be single-tab
510 # separated elements, and the free-text at the end should contain
511 # text-translated tab symbols but no "real" tabs, so splitting on
512 # \t is safe, and $attribs gets the entire attributes field to be
513 # parsed later
515 # sendu: but the tag value pair can (should?) be separated by a tab. The
516 # 'no tabs' thing seems to apply only to the free text that is allowed for
517 # the value
519 my ($seqname, $source, $primary, $start,
520 $end, $score, $strand, $frame, @attribs) = split(/\t+/, $string);
521 my $attribs = join ' ', @attribs;
523 if ( !defined $frame ) {
524 $feat->throw("[$string] does not look like GFF2 to me");
526 $feat->seq_id($seqname);
527 $feat->source_tag($source);
528 $feat->primary_tag($primary);
529 $feat->start($start);
530 $feat->end($end);
531 $feat->frame($frame);
532 if ( $score eq '.' ) {
533 # $feat->score(undef);
534 } else {
535 $feat->score($score);
537 if ( $strand eq '-' ) { $feat->strand(-1); }
538 if ( $strand eq '+' ) { $feat->strand(1); }
539 if ( $strand eq '.' ) { $feat->strand(0); }
542 # <Begin Inefficient Code from Mark Wilkinson>
543 # this routine is necessay to allow the presence of semicolons in
544 # quoted text Semicolons are the delimiting character for new
545 # tag/value attributes. it is more or less a "state" machine, with
546 # the "quoted" flag going up and down as we pass thorugh quotes to
547 # distinguish free-text semicolon and hash symbols from GFF control
548 # characters
550 my $flag = 0; # this could be changed to a bit and just be twiddled
551 my @parsed;
553 # run through each character one at a time and check it
554 # NOTE: changed to foreach loop which is more efficient in perl
555 # --jasons
556 for my $a ( split //, $attribs ) {
557 # flag up on entering quoted text, down on leaving it
558 if( $a eq '"') { $flag = ( $flag == 0 ) ? 1:0 }
559 elsif( $a eq ';' && $flag ) { $a = "INSERT_SEMICOLON_HERE"}
560 elsif( $a eq '#' && ! $flag ) { last }
561 push @parsed, $a;
563 $attribs = join "", @parsed; # rejoin into a single string
565 # <End Inefficient Code>
566 # Please feel free to fix this and make it more "perlish"
568 my @key_vals = split /;/, $attribs; # attributes are semicolon-delimited
570 foreach my $pair ( @key_vals ) {
571 # replace semicolons that were removed from free-text above.
572 $pair =~ s/INSERT_SEMICOLON_HERE/;/g;
574 # separate the key from the value
575 my ($blank, $key, $values) = split /^\s*([\w\d]+)\s/, $pair;
577 if( defined $values ) {
578 my @values;
579 # free text is quoted, so match each free-text block
580 # and remove it from the $values string
581 while ($values =~ s/"(.*?)"//){
582 # and push it on to the list of values (tags may have
583 # more than one value... and the value may be undef)
584 push @values, $1;
587 # and what is left over should be space-separated
588 # non-free-text values
590 my @othervals = split /\s+/, $values;
591 foreach my $othervalue(@othervals){
592 # get rid of any empty strings which might
593 # result from the split
594 if (CORE::length($othervalue) > 0) {push @values, $othervalue}
597 foreach my $value(@values){
598 $feat->add_tag_value($key, $value);
605 sub _from_gff3_string {
606 my ($gff, $feat, $string) = @_;
607 chomp($string);
609 # according to the now nearly final GFF3 spec, columns should
610 # be tab separated, allowing unescaped spaces to occur in
611 # column 9
613 my ($seqname, $source, $primary, $start, $end,
614 $score, $strand, $frame, $groups) = split(/\t/, $string);
616 if ( ! defined $frame ) {
617 $feat->throw("[$string] does not look like GFF3 to me");
619 $feat->seq_id($seqname);
620 $feat->source_tag($source);
621 $feat->primary_tag($primary);
622 $feat->start($start);
623 $feat->end($end);
624 $feat->frame($frame);
625 if ( $score eq '.' ) {
626 #$feat->score(undef);
627 } else {
628 $feat->score($score);
630 if ( $strand eq '-' ) { $feat->strand(-1); }
631 if ( $strand eq '+' ) { $feat->strand(1); }
632 if ( $strand eq '.' ) { $feat->strand(0); }
633 my @groups = split(/\s*;\s*/, $groups);
635 for my $group (@groups) {
636 my ($tag,$value) = split /=/,$group;
637 $tag = unescape($tag);
638 my @values = map {unescape($_)} split /,/,$value;
639 for my $v ( @values ) { $feat->add_tag_value($tag,$v); }
643 # taken from Bio::DB::GFF
644 sub unescape {
645 my $v = shift;
646 $v =~ s/%([0-9a-fA-F]{2})/chr hex($1)/ge;
647 return $v;
651 =head2 write_feature
653 Title : write_feature
654 Usage : $gffio->write_feature($feature);
655 Function: Writes the specified SeqFeatureI object in GFF format to the stream
656 associated with this instance.
657 Returns : none
658 Args : An array of Bio::SeqFeatureI implementing objects to be serialized
660 =cut
662 sub write_feature {
663 my ($self, @features) = @_;
664 return unless @features;
665 if( $self->{'_first'} && $self->gff_version() == 3 ) {
666 $self->_print("##gff-version 3\n");
668 $self->{'_first'} = 0;
669 foreach my $feature ( @features ) {
670 $self->_print($self->gff_string($feature)."\n");
675 =head2 gff_string
677 Title : gff_string
678 Usage : $gffstr = $gffio->gff_string($feature);
679 Function: Obtain the GFF-formatted representation of a SeqFeatureI object.
680 The formatting depends on the version specified at initialization.
682 This method is used by write_feature(). It actually dispatches to
683 one of the version-specific (private) methods.
684 Example :
685 Returns : A GFF-formatted string representation of the SeqFeature
686 Args : A Bio::SeqFeatureI implementing object to be GFF-stringified
688 =cut
690 sub gff_string{
691 my ($self, $feature) = @_;
693 if($self->gff_version() == 1) {
694 return $self->_gff1_string($feature);
695 } elsif( $self->gff_version() == 3 ) {
696 return $self->_gff3_string($feature);
697 } elsif( $self->gff_version() == 2.5 ) {
698 return $self->_gff25_string($feature);
699 } else {
700 return $self->_gff2_string($feature);
705 =head2 _gff1_string
707 Title : _gff1_string
708 Usage : $gffstr = $gffio->_gff1_string
709 Function:
710 Example :
711 Returns : A GFF1-formatted string representation of the SeqFeature
712 Args : A Bio::SeqFeatureI implementing object to be GFF-stringified
714 =cut
716 sub _gff1_string{
717 my ($gff, $feat) = @_;
718 my ($str,$score,$frame,$name,$strand);
720 if( $feat->can('score') ) {
721 $score = $feat->score();
723 $score = '.' unless defined $score;
725 if( $feat->can('frame') ) {
726 $frame = $feat->frame();
728 $frame = '.' unless defined $frame;
730 $strand = $feat->strand();
731 if(! $strand) {
732 $strand = ".";
733 } elsif( $strand == 1 ) {
734 $strand = '+';
735 } elsif ( $feat->strand == -1 ) {
736 $strand = '-';
739 if( $feat->can('seqname') ) {
740 $name = $feat->seq_id();
742 $name = 'SEQ' if ! length($name);
744 $str = join("\t",
745 $name,
746 $feat->source_tag,
747 $feat->primary_tag,
748 $feat->start,
749 $feat->end,
750 $score,
751 $strand,
752 $frame);
754 foreach my $tag ( $feat->get_all_tags ) {
755 next if exists $SKIPPED_TAGS{$tag};
756 foreach my $value ( $feat->get_tag_values($tag) ) {
757 $str .= " $tag=$value" if $value;
761 return $str;
765 =head2 _gff2_string
767 Title : _gff2_string
768 Usage : $gffstr = $gffio->_gff2_string
769 Function:
770 Example :
771 Returns : A GFF2-formatted string representation of the SeqFeature
772 Args : A Bio::SeqFeatureI implementing object to be GFF2-stringified
774 =cut
776 sub _gff2_string{
777 my ($gff, $origfeat) = @_;
778 my $feat;
779 if ($origfeat->isa('Bio::SeqFeature::FeaturePair')){
780 $feat = $origfeat->feature2;
781 } else {
782 $feat = $origfeat;
784 my ($str1, $str2,$score,$frame,$name,$strand);
786 if( $feat->can('score') ) {
787 $score = $feat->score();
789 $score = '.' unless defined $score;
791 if( $feat->can('frame') ) {
792 $frame = $feat->frame();
794 $frame = '.' unless defined $frame;
796 $strand = $feat->strand();
797 if(! $strand) {
798 $strand = ".";
799 } elsif( $strand == 1 ) {
800 $strand = '+';
801 } elsif ( $feat->strand == -1 ) {
802 $strand = '-';
805 if( $feat->can('seqname') ) {
806 $name = $feat->seq_id();
808 $name = 'SEQ' if ! length($name);
810 $str1 = join("\t",
811 $name,
812 $feat->source_tag(),
813 $feat->primary_tag(),
814 $feat->start(),
815 $feat->end(),
816 $score,
817 $strand,
818 $frame);
819 # the routine below is the only modification I made to the original
820 # ->gff_string routine (above) as on November 17th, 2000, the
821 # Sanger webpage describing GFF2 format reads: "From version 2
822 # onwards, the attribute field must have a tag value structure
823 # following the syntax used within objects in a .ace file,
824 # flattened onto one line by semicolon separators. Tags must be
825 # standard identifiers ([A-Za-z][A-Za-z0-9_]*). Free text values
826 # must be quoted with double quotes".
827 # MW
829 my @group;
831 foreach my $tag ( $feat->get_all_tags ) {
832 next if exists $SKIPPED_TAGS{$tag};
833 my @v;
834 foreach my $value ( $feat->get_tag_values($tag) ) {
835 unless( defined $value && length($value) ) {
836 # quote anything other than valid tag/value characters
837 $value = '""';
838 } elsif ($value =~ /[^A-Za-z0-9_]/){
839 # substitute tab and newline chars by their UNIX equivalent
840 $value =~ s/\t/\\t/g;
841 $value =~ s/\n/\\n/g;
842 $value = '"' . $value . '" ';
844 push @v, $value;
845 # for this tag (allowed in GFF2 and .ace format)
847 push @group, "$tag ".join(" ", @v);
850 $str2 .= join(' ; ', @group);
851 # Add Target information for Feature Pairs
852 if( ! $feat->has_tag('Target') && # This is a bad hack IMHO
853 ! $feat->has_tag('Group') &&
854 $origfeat->isa('Bio::SeqFeature::FeaturePair') ) {
855 $str2 = sprintf("Target %s %d %d", $origfeat->feature1->seq_id,
856 ( $origfeat->feature1->strand < 0 ?
857 ( $origfeat->feature1->end,
858 $origfeat->feature1->start) :
859 ( $origfeat->feature1->start,
860 $origfeat->feature1->end)
861 )) . ($str2?" ; ".$str2:""); # need to put Target information before other tag/value pairs - mw
863 return $str1."\t".$str2;
867 =head2 _gff25_string
869 Title : _gff25_string
870 Usage : $gffstr = $gffio->_gff2_string
871 Function: To get a format of GFF that is peculiar to Gbrowse/Bio::DB::GFF
872 Example :
873 Returns : A GFF2.5-formatted string representation of the SeqFeature
874 Args : A Bio::SeqFeatureI implementing object to be GFF2.5-stringified
876 =cut
878 sub _gff25_string {
879 my ($gff, $origfeat) = @_;
880 my $feat;
881 if ($origfeat->isa('Bio::SeqFeature::FeaturePair')){
882 $feat = $origfeat->feature2;
883 } else {
884 $feat = $origfeat;
886 my ($str1, $str2,$score,$frame,$name,$strand);
888 if( $feat->can('score') ) {
889 $score = $feat->score();
891 $score = '.' unless defined $score;
893 if( $feat->can('frame') ) {
894 $frame = $feat->frame();
896 $frame = '.' unless defined $frame;
898 $strand = $feat->strand();
899 if(! $strand) {
900 $strand = ".";
901 } elsif( $strand == 1 ) {
902 $strand = '+';
903 } elsif ( $feat->strand == -1 ) {
904 $strand = '-';
907 if( $feat->can('seqname') ) {
908 $name = $feat->seq_id();
910 $name = 'SEQ' if ! length($name);
912 $str1 = join("\t",
913 $name,
914 $feat->source_tag(),
915 $feat->primary_tag(),
916 $feat->start(),
917 $feat->end(),
918 $score,
919 $strand,
920 $frame);
922 my @all_tags = $feat->all_tags;
923 my @group; my @firstgroup;
924 if (@all_tags) { # only play this game if it is worth playing...
925 foreach my $tag ( @all_tags ) {
926 my @v;
927 foreach my $value ( $feat->get_tag_values($tag) ) {
928 next if exists $SKIPPED_TAGS{$tag};
929 unless( defined $value && length($value) ) {
930 $value = '""';
931 } elsif ($value =~ /[^A-Za-z0-9_]/){
932 $value =~ s/\t/\\t/g; # substitute tab and newline
933 # characters
934 $value =~ s/\n/\\n/g; # to their UNIX equivalents
935 $value = '"' . $value . '" ';
936 } # if the value contains
937 # anything other than valid
938 # tag/value characters, then
939 # quote it
940 push @v, $value;
941 # for this tag (allowed in GFF2 and .ace format)
943 if (($tag eq 'Group') || ($tag eq 'Target')){ # hopefully we won't get both...
944 push @firstgroup, "$tag ".join(" ", @v);
945 } else {
946 push @group, "$tag ".join(" ", @v);
950 $str2 = join(' ; ', (@firstgroup, @group));
951 # Add Target information for Feature Pairs
952 if( ! $feat->has_tag('Target') && # This is a bad hack IMHO
953 ! $feat->has_tag('Group') &&
954 $origfeat->isa('Bio::SeqFeature::FeaturePair') ) {
955 $str2 = sprintf("Target %s ; tstart %d ; tend %d", $origfeat->feature1->seq_id,
956 ( $origfeat->feature1->strand < 0 ?
957 ( $origfeat->feature1->end,
958 $origfeat->feature1->start) :
959 ( $origfeat->feature1->start,
960 $origfeat->feature1->end)
961 )) . ($str2?" ; ".$str2:""); # need to put the target info before other tag/value pairs - mw
963 return $str1 . "\t". $str2;
967 =head2 _gff3_string
969 Title : _gff3_string
970 Usage : $gffstr = $gffio->_gff3_string
971 Function:
972 Example :
973 Returns : A GFF3-formatted string representation of the SeqFeature
974 Args : A Bio::SeqFeatureI implementing object to be GFF3-stringified
976 =cut
978 sub _gff3_string {
979 my ($gff, $origfeat) = @_;
980 my $feat;
981 if ($origfeat->isa('Bio::SeqFeature::FeaturePair')){
982 $feat = $origfeat->feature2;
983 } else {
984 $feat = $origfeat;
987 my $ID = $gff->_incrementGFF3ID();
989 my ($score,$frame,$name,$strand);
991 if( $feat->can('score') ) {
992 $score = $feat->score();
994 $score = '.' unless defined $score;
996 if( $feat->can('frame') ) {
997 $frame = $feat->frame();
999 $frame = '1' unless defined $frame;
1001 $strand = $feat->strand();
1003 if(! $strand) {
1004 $strand = ".";
1005 } elsif( $strand == 1 ) {
1006 $strand = '+';
1007 } elsif ( $feat->strand == -1 ) {
1008 $strand = '-';
1011 if( $feat->can('seqname') ) {
1012 $name = $feat->seq_id();
1014 $name = 'SEQ' if ! length($name);
1016 my @groups;
1018 # force leading ID and Parent tags
1019 my @all_tags = grep { ! exists $GFF3_ID_Tags{$_} } $feat->all_tags;
1020 for my $t ( sort { $GFF3_ID_Tags{$b} <=> $GFF3_ID_Tags{$a} }
1021 keys %GFF3_ID_Tags ) {
1022 unshift @all_tags, $t if $feat->has_tag($t);
1025 for my $tag ( @all_tags ) {
1026 next if exists $SKIPPED_TAGS{$tag};
1027 # next if $tag eq 'Target';
1028 if ($tag eq 'Target' && ! $origfeat->isa('Bio::SeqFeature::FeaturePair')){
1029 my @values = $feat->get_tag_values($tag);
1030 if(scalar(@values) > 1){ # How is it possible that Target is has a value list ??
1031 # simple Target,start,stop
1032 my ($target_id, $b,$e,$strand) = $feat->get_tag_values($tag);
1033 next unless(defined($e) && defined($b) && $target_id);
1034 ($b,$e)= ($e,$b) if(defined $strand && $strand<0);
1035 #if we have the strand we will print it
1036 if($strand){ push @groups, sprintf("Target=%s %d %d %s", $target_id,$b,$e,$strand); }
1037 else{ push @groups, sprintf("Target=%s %d %d", $target_id,$b,$e); }
1038 next;
1042 my $valuestr;
1043 # a string which will hold one or more values
1044 # for this tag, with quoted free text and
1045 # space-separated individual values.
1046 my @v;
1047 for my $value ( $feat->get_tag_values($tag) ) {
1048 if( defined $value && length($value) ) {
1049 #$value =~ tr/ /+/; #spaces are allowed now
1050 if ( ref $value eq 'Bio::Annotation::Comment') {
1051 $value = $value->text;
1054 if ($value =~ /[^a-zA-Z0-9\,\;\=\.:\%\^\*\$\@\!\+\_\?\-]/) {
1055 $value =~ s/\t/\\t/g; # substitute tab and newline
1056 # characters
1057 $value =~ s/\n/\\n/g; # to their UNIX equivalents
1059 # Unescaped quotes are not allowed in GFF3
1060 # $value = '"' . $value . '"';
1062 $value =~ s/([\t\n\r%&\=;,])/sprintf("%%%X",ord($1))/ge;
1063 } else {
1064 # if it is completely empty, then just make empty double quotes
1065 $value = '""';
1067 push @v, $value;
1069 # can we figure out how to improve this?
1070 $tag = lcfirst($tag) unless ( $tag =~
1071 /^(ID|Name|Alias|Parent|Gap|Target|Derives_from|Note|Dbxref|Ontology_term)$/);
1073 push @groups, "$tag=".join(",",@v);
1075 # Add Target information for Feature Pairs
1076 if( $feat->has_tag('Target') &&
1077 ! $feat->has_tag('Group') &&
1078 $origfeat->isa('Bio::SeqFeature::FeaturePair') ) {
1080 my $target_id = $origfeat->feature1->seq_id;
1081 $target_id =~ s/([\t\n\r%&\=;,])/sprintf("%%%X",ord($1))/ge;
1083 push @groups, sprintf("Target=%s %d %d",
1084 $target_id,
1085 ( $origfeat->feature1->strand < 0 ?
1086 ( $origfeat->feature1->end,
1087 $origfeat->feature1->start) :
1088 ( $origfeat->feature1->start,
1089 $origfeat->feature1->end)
1093 # unshift @groups, "ID=autogenerated$ID" unless ($feat->has_tag('ID'));
1094 if ( $feat->can('name') && defined($feat->name) ) {
1095 # such as might be for Bio::DB::SeqFeature
1096 unshift @groups, 'Name=' . $feat->name;
1099 my $gff_string = "";
1100 if ($feat->location->isa("Bio::Location::SplitLocationI")) {
1101 my @locs = $feat->location->each_Location;
1102 foreach my $loc (@locs) {
1103 $gff_string .= join("\t",
1104 $name,
1105 $feat->source_tag() || '.',
1106 $feat->primary_tag(),
1107 $loc->start(),
1108 $loc->end(),
1109 $score,
1110 $strand,
1111 $frame,
1112 join(';', @groups)) . "\n";
1114 chop $gff_string;
1115 return $gff_string;
1116 } else {
1117 $gff_string = join("\t",
1118 $name,
1119 $feat->source_tag() || '.',
1120 $feat->primary_tag(),
1121 $feat->start(),
1122 $feat->end(),
1123 $score,
1124 $strand,
1125 $frame,
1126 join(';', @groups));
1128 return $gff_string;
1132 =head2 gff_version
1134 Title : _gff_version
1135 Usage : $gffversion = $gffio->gff_version
1136 Function:
1137 Example :
1138 Returns : The GFF version this parser will accept and emit.
1139 Args : none
1141 =cut
1143 sub gff_version {
1144 my ($self, $value) = @_;
1145 if(defined $value && grep {$value == $_ } ( 1, 2, 2.5, 3)) {
1146 $self->{'GFF_VERSION'} = $value;
1148 return $self->{'GFF_VERSION'};
1152 # Make filehandles
1154 =head2 newFh
1156 Title : newFh
1157 Usage : $fh = Bio::Tools::GFF->newFh(-file=>$filename,-format=>'Format')
1158 Function: does a new() followed by an fh()
1159 Example : $fh = Bio::Tools::GFF->newFh(-file=>$filename,-format=>'Format')
1160 $feature = <$fh>; # read a feature object
1161 print $fh $feature; # write a feature object
1162 Returns : filehandle tied to the Bio::Tools::GFF class
1163 Args :
1165 =cut
1167 sub newFh {
1168 my $class = shift;
1169 return unless my $self = $class->new(@_);
1170 return $self->fh;
1174 =head2 fh
1176 Title : fh
1177 Usage : $obj->fh
1178 Function:
1179 Example : $fh = $obj->fh; # make a tied filehandle
1180 $feature = <$fh>; # read a feature object
1181 print $fh $feature; # write a feature object
1182 Returns : filehandle tied to Bio::Tools::GFF class
1183 Args : none
1185 =cut
1188 sub fh {
1189 my $self = shift;
1190 my $class = ref($self) || $self;
1191 my $s = Symbol::gensym;
1192 tie $$s,$class,$self;
1193 return $s;
1196 # This accessor is used for accessing the Bio::Seq objects from a GFF3
1197 # file; if the file you are using has no sequence data you can ignore
1198 # this accessor
1200 # This accessor returns a hash reference containing Bio::Seq objects,
1201 # indexed by Bio::Seq->primary_id
1203 sub _seq_by_id_h {
1204 my $self = shift;
1206 return $self->{'_seq_by_id_h'} = shift if @_;
1207 $self->{'_seq_by_id_h'} = {}
1208 unless $self->{'_seq_by_id_h'};
1209 return $self->{'_seq_by_id_h'};
1213 =head2 get_seqs
1215 Title : get_seqs
1216 Usage :
1217 Function: Returns all Bio::Seq objects populated by GFF3 file
1218 Example :
1219 Returns :
1220 Args :
1222 =cut
1224 sub get_seqs {
1225 my ($self,@args) = @_;
1226 return values %{$self->_seq_by_id_h};
1230 =head2 features_attached_to_seqs
1232 Title : features_attached_to_seqs
1233 Usage : $obj->features_attached_to_seqs(1);
1234 Function: For use with GFF3 containing sequence only
1236 Setting this B<before> parsing ensures that all Bio::Seq object
1237 created will have the appropriate features added to them
1239 defaults to false (off)
1241 Note that this mode will incur higher memory usage because features
1242 will have to be cached until the relevant feature comes along
1244 Example :
1245 Returns : value of features_attached_to_seqs (a boolean)
1246 Args : on set, new value (a boolean, optional)
1249 =cut
1251 sub features_attached_to_seqs{
1252 my $self = shift;
1254 return $self->{'_features_attached_to_seqs'} = shift if @_;
1255 return $self->{'_features_attached_to_seqs'};
1259 =head2 ignore_sequence
1261 Title : ignore_sequence
1262 Usage : $obj->ignore_sequence(1);
1263 Function: For use with GFF3 containing sequence only
1265 Setting this B<before> parsing means that all sequence data will be
1266 ignored
1268 Example :
1269 Returns : value of ignore_sequence (a boolean)
1270 Args : on set, new value (a boolean, optional)
1272 =cut
1274 sub ignore_sequence{
1275 my $self = shift;
1277 return $self->{'_ignore_sequence'} = shift if @_;
1278 return $self->{'_ignore_sequence'};
1282 sub DESTROY {
1283 my $self = shift;
1284 $self->close();
1287 sub TIEHANDLE {
1288 my ($class,$val) = @_;
1289 return bless {'gffio' => $val}, $class;
1292 sub READLINE {
1293 my $self = shift;
1294 return $self->{'gffio'}->next_feature() || undef unless wantarray;
1295 my (@list, $obj);
1296 push @list, $obj while $obj = $self->{'gffio'}->next_feature();
1297 return @list;
1300 sub PRINT {
1301 my $self = shift;
1302 $self->{'gffio'}->write_feature(@_);