| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::Tools::GFF | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by the Bioperl core team | ||
| 7 | # | ||
| 8 | # Copyright Matthew Pocock | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | |||
| 12 | # POD documentation - main docs before the code | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | FAST::Bio::Tools::GFF - A FAST::Bio::SeqAnalysisParserI compliant GFF format parser | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use FAST::Bio::Tools::GFF; | ||
| 21 | |||
| 22 | # specify input via -fh or -file | ||
| 23 | my $gffio = FAST::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 | ||
| 28 | } | ||
| 29 | $gffio->close(); | ||
| 30 | |||
| 31 | # you can also obtain a GFF parser as a SeqAnalasisParserI in | ||
| 32 | # HT analysis pipelines (see FAST::Bio::SeqAnalysisParserI and | ||
| 33 | # FAST::Bio::Factory::SeqAnalysisParserFactory) | ||
| 34 | my $factory = FAST::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 | ||
| 38 | } | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 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. | ||
| 46 | |||
| 47 | That is, if you can get your analysis program spit out GFF, here is | ||
| 48 | your result parser. | ||
| 49 | |||
| 50 | =head1 GFF3 AND SEQUENCE DATA | ||
| 51 | |||
| 52 | GFF3 supports sequence data; see | ||
| 53 | |||
| 54 | http://www.sequenceontology.org/gff3.shtml | ||
| 55 | |||
| 56 | There are a number of ways to deal with this - | ||
| 57 | |||
| 58 | If you call | ||
| 59 | |||
| 60 | $gffio->ignore_sequence(1) | ||
| 61 | |||
| 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 | ||
| 65 | |||
| 66 | Alternatively, you can call either | ||
| 67 | |||
| 68 | $gffio->get_seqs() | ||
| 69 | |||
| 70 | Or | ||
| 71 | |||
| 72 | $gffio->seq_id_by_h() | ||
| 73 | |||
| 74 | At the B<end> of parsing to get either a list or hashref of FAST::Bio::Seq | ||
| 75 | objects (see the documentation for each of these methods) | ||
| 76 | |||
| 77 | Note that these objects will not have the features attached - you have | ||
| 78 | to do this yourself, OR call | ||
| 79 | |||
| 80 | $gffio->features_attached_to_seqs(1) | ||
| 81 | |||
| 82 | PRIOR to parsing; this will ensure that the Seqs have the features | ||
| 83 | attached; ie you will then be able to call | ||
| 84 | |||
| 85 | $seq->get_SeqFeatures(); | ||
| 86 | |||
| 87 | And use FAST::Bio::SeqIO methods | ||
| 88 | |||
| 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 | ||
| 92 | |||
| 93 | =head1 TODO | ||
| 94 | |||
| 95 | Make a FAST::Bio::SeqIO class specifically for GFF3 with sequence data | ||
| 96 | |||
| 97 | =head1 FEEDBACK | ||
| 98 | |||
| 99 | =head2 Mailing Lists | ||
| 100 | |||
| 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. | ||
| 104 | |||
| 105 | bioperl-l@bioperl.org - General discussion | ||
| 106 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 107 | |||
| 108 | =head2 Support | ||
| 109 | |||
| 110 | Please direct usage questions or support issues to the mailing list: | ||
| 111 | |||
| 112 | I<bioperl-l@bioperl.org> | ||
| 113 | |||
| 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. | ||
| 118 | |||
| 119 | =head2 Reporting Bugs | ||
| 120 | |||
| 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: | ||
| 123 | |||
| 124 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 125 | |||
| 126 | =head1 AUTHOR - Matthew Pocock | ||
| 127 | |||
| 128 | Email mrp-at-sanger.ac.uk | ||
| 129 | |||
| 130 | =head1 CONTRIBUTORS | ||
| 131 | |||
| 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 | ||
| 136 | |||
| 137 | =head1 APPENDIX | ||
| 138 | |||
| 139 | The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _ | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | # Let the code begin... | ||
| 144 | |||
| 145 | package FAST::Bio::Tools::GFF; | ||
| 146 | |||
| 147 | use vars qw($HAS_HTML_ENTITIES); | ||
| 148 | use strict; | ||
| 149 | |||
| 150 | use FAST::Bio::Seq::SeqFactory; | ||
| 151 | use FAST::Bio::LocatableSeq; | ||
| 152 | use FAST::Bio::SeqFeature::Generic; | ||
| 153 | |||
| 154 | use base qw(FAST::Bio::Root::Root FAST::Bio::SeqAnalysisParserI FAST::Bio::Root::IO); | ||
| 155 | |||
| 156 | my $i = 0; | ||
| 157 | my %GFF3_ID_Tags = map { $_ => $i++ } qw(ID Parent Target); | ||
| 158 | |||
| 159 | # for skipping data that may be represented elsewhere; currently, this is | ||
| 160 | # only the score | ||
| 161 | my %SKIPPED_TAGS = map { $_ => 1 } qw(score); | ||
| 162 | |||
| 163 | =head2 new | ||
| 164 | |||
| 165 | Title : new | ||
| 166 | Usage : my $parser = FAST::Bio::Tools::GFF->new(-gff_version => 2, | ||
| 167 | -file => "filename.gff"); | ||
| 168 | or | ||
| 169 | my $writer = FAST::Bio::Tools::GFF->new(-gff_version => 3, | ||
| 170 | -file => ">filename.gff3"); | ||
| 171 | Function: Creates a new instance. Recognized named parameters are -file, -fh, | ||
| 172 | and -gff_version. | ||
| 173 | Returns : a new object | ||
| 174 | Args : named parameters | ||
| 175 | -gff_version => [1,2,3] | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | |||
| 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. | ||
| 183 | |||
| 184 | my $gff3_featureID = 0; | ||
| 185 | |||
| 186 | sub _incrementGFF3ID { | ||
| 187 | my ($self) = @_; | ||
| 188 | return ++ $gff3_featureID; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | sub new { | ||
| 194 | my ($class, @args) = @_; | ||
| 195 | my $self = $class->SUPER::new(@args); | ||
| 196 | |||
| 197 | my ($gff_version, $noparse) = $self->_rearrange([qw(GFF_VERSION NOPARSE)],@args); | ||
| 198 | |||
| 199 | # initialize IO | ||
| 200 | $self->_initialize_io(@args); | ||
| 201 | $self->_parse_header() unless $noparse; | ||
| 202 | |||
| 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); | ||
| 207 | } | ||
| 208 | $self->{'_first'} = 1; | ||
| 209 | return $self; | ||
| 210 | } | ||
| 211 | |||
| 212 | =head2 _parse_header | ||
| 213 | |||
| 214 | Title : _parse_header | ||
| 215 | Usage : $gffio->_parse_header() | ||
| 216 | Function: used to turn parse GFF header lines. currently | ||
| 217 | produces FAST::Bio::LocatableSeq objects from ##sequence-region | ||
| 218 | lines | ||
| 219 | Returns : 1 on success | ||
| 220 | Args : none | ||
| 221 | |||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | sub _parse_header{ | ||
| 226 | my ($self) = @_; | ||
| 227 | |||
| 228 | my @unhandled; | ||
| 229 | local $^W = 0; # hide warnings when we try and parse from a file opened | ||
| 230 | # for writing - there isn't really a better way to do | ||
| 231 | # AFAIK - cannot detech if a FH is read or write. | ||
| 232 | while(my $line = $self->_readline()){ | ||
| 233 | my $handled = 0; | ||
| 234 | next if /^\s+$/; | ||
| 235 | if($line =~ /^\#\#sequence-region\s+(\S+)\s+(\S+)\s+(\S+)\s*/){ | ||
| 236 | my($seqid,$start,$end) = ($1,$2,$3); | ||
| 237 | push @{ $self->{'segments'} }, FAST::Bio::LocatableSeq->new | ||
| 238 | ( | ||
| 239 | -id => unescape($seqid), | ||
| 240 | -start => $start, | ||
| 241 | -end => $end, | ||
| 242 | -length => ($end - $start + 1), ## make the length explicit | ||
| 243 | ); | ||
| 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"); | ||
| 262 | } | ||
| 263 | } else { | ||
| 264 | } | ||
| 265 | |||
| 266 | if ($line =~ /^\>(.*)/) { | ||
| 267 | # seq data can be at header or footer | ||
| 268 | my $seq = $self->_parse_sequence($line); | ||
| 269 | if ($seq) { | ||
| 270 | $self->_seq_by_id_h->{$seq->primary_id} = $seq; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | |||
| 275 | if(!$handled){ | ||
| 276 | push @unhandled, $line | ||
| 277 | } | ||
| 278 | |||
| 279 | #looks like the header is over! | ||
| 280 | last unless $line =~ /^\#/; | ||
| 281 | } | ||
| 282 | |||
| 283 | foreach my $line (@unhandled){ | ||
| 284 | $self->_pushback($line); | ||
| 285 | } | ||
| 286 | |||
| 287 | return 1; | ||
| 288 | } | ||
| 289 | |||
| 290 | sub _parse_sequence { | ||
| 291 | my ($self, $line) = @_; | ||
| 292 | |||
| 293 | if ($line =~ /^\>(.*)/) { | ||
| 294 | |||
| 295 | my $seqid = $1; | ||
| 296 | $seqid =~ s/\s+$//; | ||
| 297 | my $desc = ''; | ||
| 298 | if ($seqid =~ /(\S+)\s+(.*)/) { | ||
| 299 | ($seqid, $desc) = ($1,$2); | ||
| 300 | } | ||
| 301 | my $res = ''; | ||
| 302 | while (my $line = $self->_readline) { | ||
| 303 | if ($line =~ /^\#/) { | ||
| 304 | last; | ||
| 305 | } | ||
| 306 | if ($line =~ /^\>/) { | ||
| 307 | $self->_pushback($line); | ||
| 308 | last; | ||
| 309 | } | ||
| 310 | $line =~ s/\s//g; | ||
| 311 | $res .= $line; | ||
| 312 | } | ||
| 313 | return if $self->ignore_sequence; | ||
| 314 | |||
| 315 | my $seqfactory = FAST::Bio::Seq::SeqFactory->new('FAST::Bio::Seq'); | ||
| 316 | my $seq = $seqfactory->create(-seq=>$res, | ||
| 317 | -id=>$seqid, | ||
| 318 | -desc=>$desc); | ||
| 319 | $seq->accession_number($seqid); | ||
| 320 | if ($self->features_attached_to_seqs) { | ||
| 321 | my @feats = | ||
| 322 | @{$self->_feature_idx_by_seq_id->{$seqid}}; | ||
| 323 | $seq->add_SeqFeature($_) foreach @feats; | ||
| 324 | @{$self->_feature_idx_by_seq_id->{$seqid}} = (); | ||
| 325 | } | ||
| 326 | return $seq; | ||
| 327 | } | ||
| 328 | else { | ||
| 329 | $self->throw("expected fasta header, not: $line"); | ||
| 330 | } | ||
| 331 | } | ||
| 332 | |||
| 333 | |||
| 334 | =head2 next_segment | ||
| 335 | |||
| 336 | Title : next_segment | ||
| 337 | Usage : my $seq = $gffio->next_segment; | ||
| 338 | Function: Returns a FAST::Bio::LocatableSeq object corresponding to a | ||
| 339 | GFF "##sequence-region" header line. | ||
| 340 | Example : | ||
| 341 | Returns : A FAST::Bio::LocatableSeq object, or undef if | ||
| 342 | there are no more sequences. | ||
| 343 | Args : none | ||
| 344 | |||
| 345 | |||
| 346 | =cut | ||
| 347 | |||
| 348 | sub next_segment{ | ||
| 349 | my ($self,@args) = @_; | ||
| 350 | return shift @{ $self->{'segments'} } if defined $self->{'segments'}; | ||
| 351 | return; | ||
| 352 | } | ||
| 353 | |||
| 354 | =head2 next_feature | ||
| 355 | |||
| 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 FAST::Bio::SeqFeatureI implementing object, or undef if there are no | ||
| 362 | more features. | ||
| 363 | Args : none | ||
| 364 | |||
| 365 | =cut | ||
| 366 | |||
| 367 | sub next_feature { | ||
| 368 | my ($self) = @_; | ||
| 369 | |||
| 370 | my $gff_string; | ||
| 371 | |||
| 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 | ||
| 377 | } | ||
| 378 | next if($gff_string =~ /^\#/ || $gff_string =~ /^\s*$/ || | ||
| 379 | $gff_string =~ m{^//}); | ||
| 380 | |||
| 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; | ||
| 388 | } | ||
| 389 | } | ||
| 390 | last; | ||
| 391 | } | ||
| 392 | return unless $gff_string; | ||
| 393 | |||
| 394 | my $feat = FAST::Bio::SeqFeature::Generic->new(); | ||
| 395 | $self->from_gff_string($feat, $gff_string); | ||
| 396 | |||
| 397 | if ($self->features_attached_to_seqs) { | ||
| 398 | push(@{$self->_feature_idx_by_seq_id->{$feat->seq_id}}, | ||
| 399 | $feat); | ||
| 400 | } | ||
| 401 | |||
| 402 | return $feat; | ||
| 403 | } | ||
| 404 | |||
| 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}; | ||
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | =head2 from_gff_string | ||
| 415 | |||
| 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. | ||
| 421 | |||
| 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 FAST::Bio::SeqFeatureI implementing object to be initialized | ||
| 427 | The GFF-formatted string to initialize it from | ||
| 428 | |||
| 429 | =cut | ||
| 430 | |||
| 431 | sub from_gff_string { | ||
| 432 | my ($self, $feat, $gff_string) = @_; | ||
| 433 | |||
| 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); | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | =head2 _from_gff1_string | ||
| 444 | |||
| 445 | Title : _from_gff1_string | ||
| 446 | Usage : | ||
| 447 | Function: | ||
| 448 | Example : | ||
| 449 | Returns : void | ||
| 450 | Args : A FAST::Bio::SeqFeatureI implementing object to be initialized | ||
| 451 | The GFF-formatted string to initialize it from | ||
| 452 | |||
| 453 | =cut | ||
| 454 | |||
| 455 | sub _from_gff1_string { | ||
| 456 | my ($gff, $feat, $string) = @_; | ||
| 457 | chomp $string; | ||
| 458 | my ($seqname, $source, $primary, $start, $end, $score, | ||
| 459 | $strand, $frame, @group) = split(/\t/, $string); | ||
| 460 | |||
| 461 | if ( !defined $frame ) { | ||
| 462 | $feat->throw("[$string] does not look like GFF to me"); | ||
| 463 | } | ||
| 464 | $frame = 0 unless( $frame =~ /^\d+$/); | ||
| 465 | $feat->seq_id($seqname); | ||
| 466 | $feat->source_tag($source); | ||
| 467 | $feat->primary_tag($primary); | ||
| 468 | $feat->start($start); | ||
| 469 | $feat->end($end); | ||
| 470 | $feat->frame($frame); | ||
| 471 | if ( $score eq '.' ) { | ||
| 472 | #$feat->score(undef); | ||
| 473 | } else { | ||
| 474 | $feat->score($score); | ||
| 475 | } | ||
| 476 | if ( $strand eq '-' ) { $feat->strand(-1); } | ||
| 477 | if ( $strand eq '+' ) { $feat->strand(1); } | ||
| 478 | if ( $strand eq '.' ) { $feat->strand(0); } | ||
| 479 | foreach my $g ( @group ) { | ||
| 480 | if ( $g =~ /(\S+)=(\S+)/ ) { | ||
| 481 | my $tag = $1; | ||
| 482 | my $value = $2; | ||
| 483 | $feat->add_tag_value($1, $2); | ||
| 484 | } else { | ||
| 485 | $feat->add_tag_value('group', $g); | ||
| 486 | } | ||
| 487 | } | ||
| 488 | } | ||
| 489 | |||
| 490 | =head2 _from_gff2_string | ||
| 491 | |||
| 492 | Title : _from_gff2_string | ||
| 493 | Usage : | ||
| 494 | Function: | ||
| 495 | Example : | ||
| 496 | Returns : void | ||
| 497 | Args : A FAST::Bio::SeqFeatureI implementing object to be initialized | ||
| 498 | The GFF2-formatted string to initialize it from | ||
| 499 | |||
| 500 | |||
| 501 | =cut | ||
| 502 | |||
| 503 | sub _from_gff2_string { | ||
| 504 | my ($gff, $feat, $string) = @_; | ||
| 505 | chomp($string); | ||
| 506 | |||
| 507 | # according to the Sanger website, GFF2 should be single-tab | ||
| 508 | # separated elements, and the free-text at the end should contain | ||
| 509 | # text-translated tab symbols but no "real" tabs, so splitting on | ||
| 510 | # \t is safe, and $attribs gets the entire attributes field to be | ||
| 511 | # parsed later | ||
| 512 | |||
| 513 | # sendu: but the tag value pair can (should?) be separated by a tab. The | ||
| 514 | # 'no tabs' thing seems to apply only to the free text that is allowed for | ||
| 515 | # the value | ||
| 516 | |||
| 517 | my ($seqname, $source, $primary, $start, | ||
| 518 | $end, $score, $strand, $frame, @attribs) = split(/\t+/, $string); | ||
| 519 | my $attribs = join ' ', @attribs; | ||
| 520 | |||
| 521 | if ( !defined $frame ) { | ||
| 522 | $feat->throw("[$string] does not look like GFF2 to me"); | ||
| 523 | } | ||
| 524 | $feat->seq_id($seqname); | ||
| 525 | $feat->source_tag($source); | ||
| 526 | $feat->primary_tag($primary); | ||
| 527 | $feat->start($start); | ||
| 528 | $feat->end($end); | ||
| 529 | $feat->frame($frame); | ||
| 530 | if ( $score eq '.' ) { | ||
| 531 | # $feat->score(undef); | ||
| 532 | } else { | ||
| 533 | $feat->score($score); | ||
| 534 | } | ||
| 535 | if ( $strand eq '-' ) { $feat->strand(-1); } | ||
| 536 | if ( $strand eq '+' ) { $feat->strand(1); } | ||
| 537 | if ( $strand eq '.' ) { $feat->strand(0); } | ||
| 538 | |||
| 539 | |||
| 540 | # <Begin Inefficient Code from Mark Wilkinson> | ||
| 541 | # this routine is necessay to allow the presence of semicolons in | ||
| 542 | # quoted text Semicolons are the delimiting character for new | ||
| 543 | # tag/value attributes. it is more or less a "state" machine, with | ||
| 544 | # the "quoted" flag going up and down as we pass thorugh quotes to | ||
| 545 | # distinguish free-text semicolon and hash symbols from GFF control | ||
| 546 | # characters | ||
| 547 | |||
| 548 | |||
| 549 | my $flag = 0; # this could be changed to a bit and just be twiddled | ||
| 550 | my @parsed; | ||
| 551 | |||
| 552 | # run through each character one at a time and check it | ||
| 553 | # NOTE: changed to foreach loop which is more efficient in perl | ||
| 554 | # --jasons | ||
| 555 | for my $a ( split //, $attribs ) { | ||
| 556 | # flag up on entering quoted text, down on leaving it | ||
| 557 | if( $a eq '"') { $flag = ( $flag == 0 ) ? 1:0 } | ||
| 558 | elsif( $a eq ';' && $flag ) { $a = "INSERT_SEMICOLON_HERE"} | ||
| 559 | elsif( $a eq '#' && ! $flag ) { last } | ||
| 560 | push @parsed, $a; | ||
| 561 | } | ||
| 562 | $attribs = join "", @parsed; # rejoin into a single string | ||
| 563 | |||
| 564 | # <End Inefficient Code> | ||
| 565 | # Please feel free to fix this and make it more "perlish" | ||
| 566 | |||
| 567 | my @key_vals = split /;/, $attribs; # attributes are semicolon-delimited | ||
| 568 | |||
| 569 | foreach my $pair ( @key_vals ) { | ||
| 570 | # replace semicolons that were removed from free-text above. | ||
| 571 | $pair =~ s/INSERT_SEMICOLON_HERE/;/g; | ||
| 572 | |||
| 573 | # separate the key from the value | ||
| 574 | my ($blank, $key, $values) = split /^\s*([\w\d]+)\s/, $pair; | ||
| 575 | |||
| 576 | |||
| 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; | ||
| 585 | } | ||
| 586 | |||
| 587 | # and what is left over should be space-separated | ||
| 588 | # non-free-text values | ||
| 589 | |||
| 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} | ||
| 595 | } | ||
| 596 | |||
| 597 | foreach my $value(@values){ | ||
| 598 | $feat->add_tag_value($key, $value); | ||
| 599 | } | ||
| 600 | } | ||
| 601 | } | ||
| 602 | } | ||
| 603 | |||
| 604 | |||
| 605 | sub _from_gff3_string { | ||
| 606 | my ($gff, $feat, $string) = @_; | ||
| 607 | chomp($string); | ||
| 608 | |||
| 609 | # according to the now nearly final GFF3 spec, columns should | ||
| 610 | # be tab separated, allowing unescaped spaces to occur in | ||
| 611 | # column 9 | ||
| 612 | |||
| 613 | my ($seqname, $source, $primary, $start, $end, | ||
| 614 | $score, $strand, $frame, $groups) = split(/\t/, $string); | ||
| 615 | |||
| 616 | if ( ! defined $frame ) { | ||
| 617 | $feat->throw("[$string] does not look like GFF3 to me"); | ||
| 618 | } | ||
| 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); | ||
| 629 | } | ||
| 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); | ||
| 634 | |||
| 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); } | ||
| 640 | } | ||
| 641 | } | ||
| 642 | |||
| 643 | # taken from FAST::Bio::DB::GFF | ||
| 644 | sub unescape { | ||
| 645 | my $v = shift; | ||
| 646 | $v =~ tr/+/ /; | ||
| 647 | $v =~ s/%([0-9a-fA-F]{2})/chr hex($1)/ge; | ||
| 648 | return $v; | ||
| 649 | } | ||
| 650 | |||
| 651 | =head2 write_feature | ||
| 652 | |||
| 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 FAST::Bio::SeqFeatureI implementing objects to be serialized | ||
| 659 | |||
| 660 | =cut | ||
| 661 | |||
| 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"); | ||
| 667 | } | ||
| 668 | $self->{'_first'} = 0; | ||
| 669 | foreach my $feature ( @features ) { | ||
| 670 | $self->_print($self->gff_string($feature)."\n"); | ||
| 671 | } | ||
| 672 | } | ||
| 673 | |||
| 674 | =head2 gff_string | ||
| 675 | |||
| 676 | Title : gff_string | ||
| 677 | Usage : $gffstr = $gffio->gff_string($feature); | ||
| 678 | Function: Obtain the GFF-formatted representation of a SeqFeatureI object. | ||
| 679 | The formatting depends on the version specified at initialization. | ||
| 680 | |||
| 681 | This method is used by write_feature(). It actually dispatches to | ||
| 682 | one of the version-specific (private) methods. | ||
| 683 | Example : | ||
| 684 | Returns : A GFF-formatted string representation of the SeqFeature | ||
| 685 | Args : A FAST::Bio::SeqFeatureI implementing object to be GFF-stringified | ||
| 686 | |||
| 687 | =cut | ||
| 688 | |||
| 689 | sub gff_string{ | ||
| 690 | my ($self, $feature) = @_; | ||
| 691 | |||
| 692 | if($self->gff_version() == 1) { | ||
| 693 | return $self->_gff1_string($feature); | ||
| 694 | } elsif( $self->gff_version() == 3 ) { | ||
| 695 | return $self->_gff3_string($feature); | ||
| 696 | } elsif( $self->gff_version() == 2.5 ) { | ||
| 697 | return $self->_gff25_string($feature); | ||
| 698 | } else { | ||
| 699 | return $self->_gff2_string($feature); | ||
| 700 | } | ||
| 701 | } | ||
| 702 | |||
| 703 | =head2 _gff1_string | ||
| 704 | |||
| 705 | Title : _gff1_string | ||
| 706 | Usage : $gffstr = $gffio->_gff1_string | ||
| 707 | Function: | ||
| 708 | Example : | ||
| 709 | Returns : A GFF1-formatted string representation of the SeqFeature | ||
| 710 | Args : A FAST::Bio::SeqFeatureI implementing object to be GFF-stringified | ||
| 711 | |||
| 712 | =cut | ||
| 713 | |||
| 714 | sub _gff1_string{ | ||
| 715 | my ($gff, $feat) = @_; | ||
| 716 | my ($str,$score,$frame,$name,$strand); | ||
| 717 | |||
| 718 | if( $feat->can('score') ) { | ||
| 719 | $score = $feat->score(); | ||
| 720 | } | ||
| 721 | $score = '.' unless defined $score; | ||
| 722 | |||
| 723 | if( $feat->can('frame') ) { | ||
| 724 | $frame = $feat->frame(); | ||
| 725 | } | ||
| 726 | $frame = '.' unless defined $frame; | ||
| 727 | |||
| 728 | $strand = $feat->strand(); | ||
| 729 | if(! $strand) { | ||
| 730 | $strand = "."; | ||
| 731 | } elsif( $strand == 1 ) { | ||
| 732 | $strand = '+'; | ||
| 733 | } elsif ( $feat->strand == -1 ) { | ||
| 734 | $strand = '-'; | ||
| 735 | } | ||
| 736 | |||
| 737 | if( $feat->can('seqname') ) { | ||
| 738 | $name = $feat->seq_id(); | ||
| 739 | $name ||= 'SEQ'; | ||
| 740 | } else { | ||
| 741 | $name = 'SEQ'; | ||
| 742 | } | ||
| 743 | |||
| 744 | |||
| 745 | $str = join("\t", | ||
| 746 | $name, | ||
| 747 | $feat->source_tag, | ||
| 748 | $feat->primary_tag, | ||
| 749 | $feat->start, | ||
| 750 | $feat->end, | ||
| 751 | $score, | ||
| 752 | $strand, | ||
| 753 | $frame); | ||
| 754 | |||
| 755 | foreach my $tag ( $feat->get_all_tags ) { | ||
| 756 | next if exists $SKIPPED_TAGS{$tag}; | ||
| 757 | foreach my $value ( $feat->get_tag_values($tag) ) { | ||
| 758 | $str .= " $tag=$value" if $value; | ||
| 759 | } | ||
| 760 | } | ||
| 761 | |||
| 762 | |||
| 763 | return $str; | ||
| 764 | } | ||
| 765 | |||
| 766 | =head2 _gff2_string | ||
| 767 | |||
| 768 | Title : _gff2_string | ||
| 769 | Usage : $gffstr = $gffio->_gff2_string | ||
| 770 | Function: | ||
| 771 | Example : | ||
| 772 | Returns : A GFF2-formatted string representation of the SeqFeature | ||
| 773 | Args : A FAST::Bio::SeqFeatureI implementing object to be GFF2-stringified | ||
| 774 | |||
| 775 | =cut | ||
| 776 | |||
| 777 | sub _gff2_string{ | ||
| 778 | my ($gff, $origfeat) = @_; | ||
| 779 | my $feat; | ||
| 780 | if ($origfeat->isa('FAST::Bio::SeqFeature::FeaturePair')){ | ||
| 781 | $feat = $origfeat->feature2; | ||
| 782 | } else { | ||
| 783 | $feat = $origfeat; | ||
| 784 | } | ||
| 785 | my ($str1, $str2,$score,$frame,$name,$strand); | ||
| 786 | |||
| 787 | if( $feat->can('score') ) { | ||
| 788 | $score = $feat->score(); | ||
| 789 | } | ||
| 790 | $score = '.' unless defined $score; | ||
| 791 | |||
| 792 | if( $feat->can('frame') ) { | ||
| 793 | $frame = $feat->frame(); | ||
| 794 | } | ||
| 795 | $frame = '.' unless defined $frame; | ||
| 796 | |||
| 797 | $strand = $feat->strand(); | ||
| 798 | if(! $strand) { | ||
| 799 | $strand = "."; | ||
| 800 | } elsif( $strand == 1 ) { | ||
| 801 | $strand = '+'; | ||
| 802 | } elsif ( $feat->strand == -1 ) { | ||
| 803 | $strand = '-'; | ||
| 804 | } | ||
| 805 | |||
| 806 | if( $feat->can('seqname') ) { | ||
| 807 | $name = $feat->seq_id(); | ||
| 808 | $name ||= 'SEQ'; | ||
| 809 | } else { | ||
| 810 | $name = 'SEQ'; | ||
| 811 | } | ||
| 812 | $str1 = join("\t", | ||
| 813 | $name, | ||
| 814 | $feat->source_tag(), | ||
| 815 | $feat->primary_tag(), | ||
| 816 | $feat->start(), | ||
| 817 | $feat->end(), | ||
| 818 | $score, | ||
| 819 | $strand, | ||
| 820 | $frame); | ||
| 821 | # the routine below is the only modification I made to the original | ||
| 822 | # ->gff_string routine (above) as on November 17th, 2000, the | ||
| 823 | # Sanger webpage describing GFF2 format reads: "From version 2 | ||
| 824 | # onwards, the attribute field must have a tag value structure | ||
| 825 | # following the syntax used within objects in a .ace file, | ||
| 826 | # flattened onto one line by semicolon separators. Tags must be | ||
| 827 | # standard identifiers ([A-Za-z][A-Za-z0-9_]*). Free text values | ||
| 828 | # must be quoted with double quotes". | ||
| 829 | |||
| 830 | # MW | ||
| 831 | |||
| 832 | |||
| 833 | my @all_tags = $feat->all_tags; | ||
| 834 | my @group; | ||
| 835 | if (@all_tags) { # only play this game if it is worth playing... | ||
| 836 | foreach my $tag ( @all_tags ) { | ||
| 837 | next if exists $SKIPPED_TAGS{$tag}; | ||
| 838 | my @v; | ||
| 839 | foreach my $value ( $feat->each_tag_value($tag) ) { | ||
| 840 | unless( defined $value && length($value) ) { | ||
| 841 | $value = '""'; | ||
| 842 | } elsif ($value =~ /[^A-Za-z0-9_]/){ | ||
| 843 | $value =~ s/\t/\\t/g; # substitute tab and newline | ||
| 844 | # characters | ||
| 845 | $value =~ s/\n/\\n/g; # to their UNIX equivalents | ||
| 846 | $value = '"' . $value . '" '; | ||
| 847 | } # if the value contains | ||
| 848 | # anything other than valid | ||
| 849 | # tag/value characters, then | ||
| 850 | # quote it | ||
| 851 | push @v, $value; | ||
| 852 | # for this tag (allowed in GFF2 and .ace format) | ||
| 853 | } | ||
| 854 | push @group, "$tag ".join(" ", @v); | ||
| 855 | } | ||
| 856 | } | ||
| 857 | $str2 .= join(' ; ', @group); | ||
| 858 | # Add Target information for Feature Pairs | ||
| 859 | if( ! $feat->has_tag('Target') && # This is a bad hack IMHO | ||
| 860 | ! $feat->has_tag('Group') && | ||
| 861 | $origfeat->isa('FAST::Bio::SeqFeature::FeaturePair') ) { | ||
| 862 | $str2 = sprintf("Target %s %d %d", $origfeat->feature1->seq_id, | ||
| 863 | ( $origfeat->feature1->strand < 0 ? | ||
| 864 | ( $origfeat->feature1->end, | ||
| 865 | $origfeat->feature1->start) : | ||
| 866 | ( $origfeat->feature1->start, | ||
| 867 | $origfeat->feature1->end) | ||
| 868 | )) . ($str2?" ; ".$str2:""); # need to put Target information before other tag/value pairs - mw | ||
| 869 | } | ||
| 870 | return $str1."\t".$str2; | ||
| 871 | } | ||
| 872 | |||
| 873 | |||
| 874 | |||
| 875 | =head2 _gff25_string | ||
| 876 | |||
| 877 | Title : _gff25_string | ||
| 878 | Usage : $gffstr = $gffio->_gff2_string | ||
| 879 | Function: To get a format of GFF that is peculiar to Gbrowse/FAST::Bio::DB::GFF | ||
| 880 | Example : | ||
| 881 | Returns : A GFF2.5-formatted string representation of the SeqFeature | ||
| 882 | Args : A FAST::Bio::SeqFeatureI implementing object to be GFF2.5-stringified | ||
| 883 | |||
| 884 | =cut | ||
| 885 | |||
| 886 | sub _gff25_string { | ||
| 887 | my ($gff, $origfeat) = @_; | ||
| 888 | my $feat; | ||
| 889 | if ($origfeat->isa('FAST::Bio::SeqFeature::FeaturePair')){ | ||
| 890 | $feat = $origfeat->feature2; | ||
| 891 | } else { | ||
| 892 | $feat = $origfeat; | ||
| 893 | } | ||
| 894 | my ($str1, $str2,$score,$frame,$name,$strand); | ||
| 895 | |||
| 896 | if( $feat->can('score') ) { | ||
| 897 | $score = $feat->score(); | ||
| 898 | } | ||
| 899 | $score = '.' unless defined $score; | ||
| 900 | |||
| 901 | if( $feat->can('frame') ) { | ||
| 902 | $frame = $feat->frame(); | ||
| 903 | } | ||
| 904 | $frame = '.' unless defined $frame; | ||
| 905 | |||
| 906 | $strand = $feat->strand(); | ||
| 907 | if(! $strand) { | ||
| 908 | $strand = "."; | ||
| 909 | } elsif( $strand == 1 ) { | ||
| 910 | $strand = '+'; | ||
| 911 | } elsif ( $feat->strand == -1 ) { | ||
| 912 | $strand = '-'; | ||
| 913 | } | ||
| 914 | |||
| 915 | if( $feat->can('seqname') ) { | ||
| 916 | $name = $feat->seq_id(); | ||
| 917 | $name ||= 'SEQ'; | ||
| 918 | } else { | ||
| 919 | $name = 'SEQ'; | ||
| 920 | } | ||
| 921 | $str1 = join("\t", | ||
| 922 | $name, | ||
| 923 | $feat->source_tag(), | ||
| 924 | $feat->primary_tag(), | ||
| 925 | $feat->start(), | ||
| 926 | $feat->end(), | ||
| 927 | $score, | ||
| 928 | $strand, | ||
| 929 | $frame); | ||
| 930 | |||
| 931 | my @all_tags = $feat->all_tags; | ||
| 932 | my @group; my @firstgroup; | ||
| 933 | if (@all_tags) { # only play this game if it is worth playing... | ||
| 934 | foreach my $tag ( @all_tags ) { | ||
| 935 | my @v; | ||
| 936 | foreach my $value ( $feat->each_tag_value($tag) ) { | ||
| 937 | next if exists $SKIPPED_TAGS{$tag}; | ||
| 938 | unless( defined $value && length($value) ) { | ||
| 939 | $value = '""'; | ||
| 940 | } elsif ($value =~ /[^A-Za-z0-9_]/){ | ||
| 941 | $value =~ s/\t/\\t/g; # substitute tab and newline | ||
| 942 | # characters | ||
| 943 | $value =~ s/\n/\\n/g; # to their UNIX equivalents | ||
| 944 | $value = '"' . $value . '" '; | ||
| 945 | } # if the value contains | ||
| 946 | # anything other than valid | ||
| 947 | # tag/value characters, then | ||
| 948 | # quote it | ||
| 949 | push @v, $value; | ||
| 950 | # for this tag (allowed in GFF2 and .ace format) | ||
| 951 | } | ||
| 952 | if (($tag eq 'Group') || ($tag eq 'Target')){ # hopefully we wont get both... | ||
| 953 | push @firstgroup, "$tag ".join(" ", @v); | ||
| 954 | } else { | ||
| 955 | push @group, "$tag ".join(" ", @v); | ||
| 956 | } | ||
| 957 | } | ||
| 958 | } | ||
| 959 | $str2 = join(' ; ', (@firstgroup, @group)); | ||
| 960 | # Add Target information for Feature Pairs | ||
| 961 | if( ! $feat->has_tag('Target') && # This is a bad hack IMHO | ||
| 962 | ! $feat->has_tag('Group') && | ||
| 963 | $origfeat->isa('FAST::Bio::SeqFeature::FeaturePair') ) { | ||
| 964 | $str2 = sprintf("Target %s ; tstart %d ; tend %d", $origfeat->feature1->seq_id, | ||
| 965 | ( $origfeat->feature1->strand < 0 ? | ||
| 966 | ( $origfeat->feature1->end, | ||
| 967 | $origfeat->feature1->start) : | ||
| 968 | ( $origfeat->feature1->start, | ||
| 969 | $origfeat->feature1->end) | ||
| 970 | )) . ($str2?" ; ".$str2:""); # need to put the target info before other tag/value pairs - mw | ||
| 971 | } | ||
| 972 | return $str1 . "\t". $str2; | ||
| 973 | } | ||
| 974 | |||
| 975 | |||
| 976 | =head2 _gff3_string | ||
| 977 | |||
| 978 | Title : _gff3_string | ||
| 979 | Usage : $gffstr = $gffio->_gff3_string | ||
| 980 | Function: | ||
| 981 | Example : | ||
| 982 | Returns : A GFF3-formatted string representation of the SeqFeature | ||
| 983 | Args : A FAST::Bio::SeqFeatureI implementing object to be GFF3-stringified | ||
| 984 | |||
| 985 | =cut | ||
| 986 | |||
| 987 | sub _gff3_string { | ||
| 988 | my ($gff, $origfeat) = @_; | ||
| 989 | my $feat; | ||
| 990 | if ($origfeat->isa('FAST::Bio::SeqFeature::FeaturePair')){ | ||
| 991 | $feat = $origfeat->feature2; | ||
| 992 | } else { | ||
| 993 | $feat = $origfeat; | ||
| 994 | } | ||
| 995 | |||
| 996 | my $ID = $gff->_incrementGFF3ID(); | ||
| 997 | |||
| 998 | my ($score,$frame,$name,$strand); | ||
| 999 | |||
| 1000 | if( $feat->can('score') ) { | ||
| 1001 | $score = $feat->score(); | ||
| 1002 | } | ||
| 1003 | $score = '.' unless defined $score; | ||
| 1004 | |||
| 1005 | if( $feat->can('frame') ) { | ||
| 1006 | $frame = $feat->frame(); | ||
| 1007 | } | ||
| 1008 | $frame = '1' unless defined $frame; | ||
| 1009 | |||
| 1010 | $strand = $feat->strand(); | ||
| 1011 | |||
| 1012 | if(! $strand) { | ||
| 1013 | $strand = "."; | ||
| 1014 | } elsif( $strand == 1 ) { | ||
| 1015 | $strand = '+'; | ||
| 1016 | } elsif ( $feat->strand == -1 ) { | ||
| 1017 | $strand = '-'; | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | if( $feat->can('seqname') ) { | ||
| 1021 | $name = $feat->seq_id(); | ||
| 1022 | $name ||= 'SEQ'; | ||
| 1023 | } else { | ||
| 1024 | $name = 'SEQ'; | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | my @groups; | ||
| 1028 | |||
| 1029 | # force leading ID and Parent tags | ||
| 1030 | my @all_tags = grep { ! exists $GFF3_ID_Tags{$_} } $feat->all_tags; | ||
| 1031 | for my $t ( sort { $GFF3_ID_Tags{$b} <=> $GFF3_ID_Tags{$a} } | ||
| 1032 | keys %GFF3_ID_Tags ) { | ||
| 1033 | unshift @all_tags, $t if $feat->has_tag($t); | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | for my $tag ( @all_tags ) { | ||
| 1037 | next if exists $SKIPPED_TAGS{$tag}; | ||
| 1038 | # next if $tag eq 'Target'; | ||
| 1039 | if ($tag eq 'Target' && ! $origfeat->isa('FAST::Bio::SeqFeature::FeaturePair')){ | ||
| 1040 | # simple Target,start,stop | ||
| 1041 | my($target_id, $b,$e,$strand) = $feat->get_tag_values($tag); | ||
| 1042 | next unless(defined($e) && defined($b) && $target_id); | ||
| 1043 | ($b,$e)= ($e,$b) if(defined $strand && $strand<0); | ||
| 1044 | $target_id =~ s/([\t\n\r%&\=;,])/sprintf("%%%X",ord($1))/ge; | ||
| 1045 | push @groups, sprintf("Target=%s %d %d", $target_id,$b,$e); | ||
| 1046 | next; | ||
| 1047 | } | ||
| 1048 | |||
| 1049 | my $valuestr; | ||
| 1050 | # a string which will hold one or more values | ||
| 1051 | # for this tag, with quoted free text and | ||
| 1052 | # space-separated individual values. | ||
| 1053 | my @v; | ||
| 1054 | for my $value ( $feat->each_tag_value($tag) ) { | ||
| 1055 | if( defined $value && length($value) ) { | ||
| 1056 | #$value =~ tr/ /+/; #spaces are allowed now | ||
| 1057 | if ( ref $value eq 'FAST::Bio::Annotation::Comment') { | ||
| 1058 | $value = $value->text; | ||
| 1059 | } | ||
| 1060 | |||
| 1061 | if ($value =~ /[^a-zA-Z0-9\,\;\=\.:\%\^\*\$\@\!\+\_\?\-]/) { | ||
| 1062 | $value =~ s/\t/\\t/g; # substitute tab and newline | ||
| 1063 | # characters | ||
| 1064 | $value =~ s/\n/\\n/g; # to their UNIX equivalents | ||
| 1065 | |||
| 1066 | # Unescaped quotes are not allowed in GFF3 | ||
| 1067 | # $value = '"' . $value . '"'; | ||
| 1068 | } | ||
| 1069 | $value =~ s/([\t\n\r%&\=;,])/sprintf("%%%X",ord($1))/ge; | ||
| 1070 | } else { | ||
| 1071 | # if it is completely empty, | ||
| 1072 | # then just make empty double | ||
| 1073 | # quotes | ||
| 1074 | $value = '""'; | ||
| 1075 | } | ||
| 1076 | push @v, $value; | ||
| 1077 | } | ||
| 1078 | # can we figure out how to improve this? | ||
| 1079 | $tag= lcfirst($tag) unless ($tag | ||
| 1080 | =~ /^(ID|Name|Alias|Parent|Gap|Target|Derives_from|Note|Dbxref|Ontology_term)$/); | ||
| 1081 | |||
| 1082 | push @groups, "$tag=".join(",",@v); | ||
| 1083 | } | ||
| 1084 | # Add Target information for Feature Pairs | ||
| 1085 | if( $feat->has_tag('Target') && | ||
| 1086 | ! $feat->has_tag('Group') && | ||
| 1087 | $origfeat->isa('FAST::Bio::SeqFeature::FeaturePair') ) { | ||
| 1088 | |||
| 1089 | my $target_id = $origfeat->feature1->seq_id; | ||
| 1090 | $target_id =~ s/([\t\n\r%&\=;,])/sprintf("%%%X",ord($1))/ge; | ||
| 1091 | |||
| 1092 | push @groups, sprintf("Target=%s %d %d", | ||
| 1093 | $target_id, | ||
| 1094 | ( $origfeat->feature1->strand < 0 ? | ||
| 1095 | ( $origfeat->feature1->end, | ||
| 1096 | $origfeat->feature1->start) : | ||
| 1097 | ( $origfeat->feature1->start, | ||
| 1098 | $origfeat->feature1->end) | ||
| 1099 | )); | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | # unshift @groups, "ID=autogenerated$ID" unless ($feat->has_tag('ID')); | ||
| 1103 | if ( $feat->can('name') && defined($feat->name) ) { | ||
| 1104 | # such as might be for FAST::Bio::DB::SeqFeature | ||
| 1105 | unshift @groups, 'Name=' . $feat->name; | ||
| 1106 | } | ||
| 1107 | |||
| 1108 | my $gff_string = ""; | ||
| 1109 | if ($feat->location->isa("FAST::Bio::Location::SplitLocationI")) { | ||
| 1110 | my @locs = $feat->location->each_Location; | ||
| 1111 | foreach my $loc (@locs) { | ||
| 1112 | $gff_string .= join("\t", | ||
| 1113 | $name, | ||
| 1114 | $feat->source_tag() || '.', | ||
| 1115 | $feat->primary_tag(), | ||
| 1116 | $loc->start(), | ||
| 1117 | $loc->end(), | ||
| 1118 | $score, | ||
| 1119 | $strand, | ||
| 1120 | $frame, | ||
| 1121 | join(';', @groups)) . "\n"; | ||
| 1122 | } | ||
| 1123 | chop $gff_string; | ||
| 1124 | return $gff_string; | ||
| 1125 | } else { | ||
| 1126 | $gff_string = join("\t", | ||
| 1127 | $name, | ||
| 1128 | $feat->source_tag() || '.', | ||
| 1129 | $feat->primary_tag(), | ||
| 1130 | $feat->start(), | ||
| 1131 | $feat->end(), | ||
| 1132 | $score, | ||
| 1133 | $strand, | ||
| 1134 | $frame, | ||
| 1135 | join(';', @groups)); | ||
| 1136 | } | ||
| 1137 | return $gff_string; | ||
| 1138 | } | ||
| 1139 | |||
| 1140 | =head2 gff_version | ||
| 1141 | |||
| 1142 | Title : _gff_version | ||
| 1143 | Usage : $gffversion = $gffio->gff_version | ||
| 1144 | Function: | ||
| 1145 | Example : | ||
| 1146 | Returns : The GFF version this parser will accept and emit. | ||
| 1147 | Args : none | ||
| 1148 | |||
| 1149 | =cut | ||
| 1150 | |||
| 1151 | sub gff_version { | ||
| 1152 | my ($self, $value) = @_; | ||
| 1153 | if(defined $value && grep {$value == $_ } ( 1, 2, 2.5, 3)) { | ||
| 1154 | $self->{'GFF_VERSION'} = $value; | ||
| 1155 | } | ||
| 1156 | return $self->{'GFF_VERSION'}; | ||
| 1157 | } | ||
| 1158 | |||
| 1159 | # Make filehandles | ||
| 1160 | |||
| 1161 | =head2 newFh | ||
| 1162 | |||
| 1163 | Title : newFh | ||
| 1164 | Usage : $fh = FAST::Bio::Tools::GFF->newFh(-file=>$filename,-format=>'Format') | ||
| 1165 | Function: does a new() followed by an fh() | ||
| 1166 | Example : $fh = FAST::Bio::Tools::GFF->newFh(-file=>$filename,-format=>'Format') | ||
| 1167 | $feature = <$fh>; # read a feature object | ||
| 1168 | print $fh $feature; # write a feature object | ||
| 1169 | Returns : filehandle tied to the FAST::Bio::Tools::GFF class | ||
| 1170 | Args : | ||
| 1171 | |||
| 1172 | |||
| 1173 | =cut | ||
| 1174 | |||
| 1175 | sub newFh { | ||
| 1176 | my $class = shift; | ||
| 1177 | return unless my $self = $class->new(@_); | ||
| 1178 | return $self->fh; | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | =head2 fh | ||
| 1182 | |||
| 1183 | Title : fh | ||
| 1184 | Usage : $obj->fh | ||
| 1185 | Function: | ||
| 1186 | Example : $fh = $obj->fh; # make a tied filehandle | ||
| 1187 | $feature = <$fh>; # read a feature object | ||
| 1188 | print $fh $feature; # write a feature object | ||
| 1189 | Returns : filehandle tied to FAST::Bio::Tools::GFF class | ||
| 1190 | Args : none | ||
| 1191 | |||
| 1192 | |||
| 1193 | =cut | ||
| 1194 | |||
| 1195 | |||
| 1196 | sub fh { | ||
| 1197 | my $self = shift; | ||
| 1198 | my $class = ref($self) || $self; | ||
| 1199 | my $s = Symbol::gensym; | ||
| 1200 | tie $$s,$class,$self; | ||
| 1201 | return $s; | ||
| 1202 | } | ||
| 1203 | |||
| 1204 | # This accessor is used for accessing the FAST::Bio::Seq objects from a GFF3 | ||
| 1205 | # file; if the file you are using has no sequence data you can ignore | ||
| 1206 | # this accessor | ||
| 1207 | |||
| 1208 | # This accessor returns a hash reference containing FAST::Bio::Seq objects, | ||
| 1209 | # indexed by FAST::Bio::Seq->primary_id | ||
| 1210 | |||
| 1211 | sub _seq_by_id_h { | ||
| 1212 | my $self = shift; | ||
| 1213 | |||
| 1214 | return $self->{'_seq_by_id_h'} = shift if @_; | ||
| 1215 | $self->{'_seq_by_id_h'} = {} | ||
| 1216 | unless $self->{'_seq_by_id_h'}; | ||
| 1217 | return $self->{'_seq_by_id_h'}; | ||
| 1218 | } | ||
| 1219 | |||
| 1220 | =head2 get_seqs | ||
| 1221 | |||
| 1222 | Title : get_seqs | ||
| 1223 | Usage : | ||
| 1224 | Function: Returns all FAST::Bio::Seq objects populated by GFF3 file | ||
| 1225 | Example : | ||
| 1226 | Returns : | ||
| 1227 | Args : | ||
| 1228 | |||
| 1229 | =cut | ||
| 1230 | |||
| 1231 | sub get_seqs { | ||
| 1232 | my ($self,@args) = @_; | ||
| 1233 | return values %{$self->_seq_by_id_h}; | ||
| 1234 | } | ||
| 1235 | |||
| 1236 | =head2 features_attached_to_seqs | ||
| 1237 | |||
| 1238 | Title : features_attached_to_seqs | ||
| 1239 | Usage : $obj->features_attached_to_seqs(1); | ||
| 1240 | Function: For use with GFF3 containg sequence only | ||
| 1241 | |||
| 1242 | Setting this B<before> parsing ensures that all FAST::Bio::Seq object | ||
| 1243 | created will have the appropriate features added to them | ||
| 1244 | |||
| 1245 | defaults to false (off) | ||
| 1246 | |||
| 1247 | Note that this mode will incur higher memory usage because features | ||
| 1248 | will have to be cached until the relevant feature comes along | ||
| 1249 | |||
| 1250 | Example : | ||
| 1251 | Returns : value of features_attached_to_seqs (a boolean) | ||
| 1252 | Args : on set, new value (a boolean, optional) | ||
| 1253 | |||
| 1254 | |||
| 1255 | =cut | ||
| 1256 | |||
| 1257 | sub features_attached_to_seqs{ | ||
| 1258 | my $self = shift; | ||
| 1259 | |||
| 1260 | return $self->{'_features_attached_to_seqs'} = shift if @_; | ||
| 1261 | return $self->{'_features_attached_to_seqs'}; | ||
| 1262 | } | ||
| 1263 | |||
| 1264 | =head2 ignore_sequence | ||
| 1265 | |||
| 1266 | Title : ignore_sequence | ||
| 1267 | Usage : $obj->ignore_sequence(1); | ||
| 1268 | Function: For use with GFF3 containg sequence only | ||
| 1269 | |||
| 1270 | Setting this B<before> parsing means that all sequence data will be | ||
| 1271 | ignored | ||
| 1272 | |||
| 1273 | Example : | ||
| 1274 | Returns : value of ignore_sequence (a boolean) | ||
| 1275 | Args : on set, new value (a boolean, optional) | ||
| 1276 | |||
| 1277 | =cut | ||
| 1278 | |||
| 1279 | sub ignore_sequence{ | ||
| 1280 | my $self = shift; | ||
| 1281 | |||
| 1282 | return $self->{'_ignore_sequence'} = shift if @_; | ||
| 1283 | return $self->{'_ignore_sequence'}; | ||
| 1284 | } | ||
| 1285 | |||
| 1286 | |||
| 1287 | sub DESTROY { | ||
| 1288 | my $self = shift; | ||
| 1289 | $self->close(); | ||
| 1290 | } | ||
| 1291 | |||
| 1292 | sub TIEHANDLE { | ||
| 1293 | my ($class,$val) = @_; | ||
| 1294 | return bless {'gffio' => $val}, $class; | ||
| 1295 | } | ||
| 1296 | |||
| 1297 | sub READLINE { | ||
| 1298 | my $self = shift; | ||
| 1299 | return $self->{'gffio'}->next_feature() unless wantarray; | ||
| 1300 | my (@list, $obj); | ||
| 1301 | push @list, $obj while $obj = $self->{'gffio'}->next_feature(); | ||
| 1302 | return @list; | ||
| 1303 | } | ||
| 1304 | |||
| 1305 | sub PRINT { | ||
| 1306 | my $self = shift; | ||
| 1307 | $self->{'gffio'}->write_feature(@_); | ||
| 1308 | } | ||
| 1309 | |||
| 1310 | 1; | ||
| 1311 |