| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::SeqFeature::Generic | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Ewan Birney <birney@sanger.ac.uk> | ||
| 7 | # | ||
| 8 | # Copyright Ewan Birney | ||
| 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::SeqFeature::Generic - Generic SeqFeature | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | $feat = FAST::Bio::SeqFeature::Generic->new( | ||
| 21 | -start => 10, | ||
| 22 | -end => 100, | ||
| 23 | -strand => -1, | ||
| 24 | -primary => 'repeat', # -primary_tag is a synonym | ||
| 25 | -source_tag => 'repeatmasker', | ||
| 26 | -display_name => 'alu family', | ||
| 27 | -score => 1000, | ||
| 28 | -tag => { new => 1, | ||
| 29 | author => 'someone', | ||
| 30 | sillytag => 'this is silly!' } ); | ||
| 31 | |||
| 32 | $feat = FAST::Bio::SeqFeature::Generic->new( -gff_string => $string ); | ||
| 33 | # if you want explicitly GFF1 | ||
| 34 | $feat = FAST::Bio::SeqFeature::Generic->new( -gff1_string => $string ); | ||
| 35 | |||
| 36 | # add it to an annotated sequence | ||
| 37 | |||
| 38 | $annseq->add_SeqFeature($feat); | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | FAST::Bio::SeqFeature::Generic is a generic implementation for the | ||
| 43 | FAST::Bio::SeqFeatureI interface, providing a simple object to provide all | ||
| 44 | the information for a feature on a sequence. | ||
| 45 | |||
| 46 | For many Features, this is all you will need to use (for example, this | ||
| 47 | is fine for Repeats in DNA sequence or Domains in protein | ||
| 48 | sequence). For other features, which have more structure, this is a | ||
| 49 | good base class to extend using inheritence to have new things: this | ||
| 50 | is what is done in the L<FAST::Bio::SeqFeature::Gene>, | ||
| 51 | L<FAST::Bio::SeqFeature::Transcript> and L<FAST::Bio::SeqFeature::Exon>, which provide | ||
| 52 | well coordinated classes to represent genes on DNA sequence (for | ||
| 53 | example, you can get the protein sequence out from a transcript | ||
| 54 | class). | ||
| 55 | |||
| 56 | For many Features, you want to add some piece of information, for | ||
| 57 | example a common one is that this feature is 'new' whereas other | ||
| 58 | features are 'old'. The tag system, which here is implemented using a | ||
| 59 | hash can be used here. You can use the tag system to extend the | ||
| 60 | L<FAST::Bio::SeqFeature::Generic> programmatically: that is, you know that you have | ||
| 61 | read in more information into the tag 'mytag' which you can then | ||
| 62 | retrieve. This means you do not need to know how to write inherited | ||
| 63 | Perl to provide more complex information on a feature, and/or, if you | ||
| 64 | do know but you do not want to write a new class every time you need | ||
| 65 | some extra piece of information, you can use the tag system to easily | ||
| 66 | store and then retrieve information. | ||
| 67 | |||
| 68 | The tag system can be written in/out of GFF format, and also into EMBL | ||
| 69 | format via the L<FAST::Bio::SeqIO> system | ||
| 70 | |||
| 71 | =head1 Implemented Interfaces | ||
| 72 | |||
| 73 | This class implementes the following interfaces. | ||
| 74 | |||
| 75 | =over 4 | ||
| 76 | |||
| 77 | =item L<FAST::Bio::SeqFeatureI> | ||
| 78 | |||
| 79 | Note that this includes implementing FAST::Bio::RangeI. | ||
| 80 | |||
| 81 | =item L<FAST::Bio::AnnotatableI> | ||
| 82 | |||
| 83 | =item L<FAST::Bio::FeatureHolderI> | ||
| 84 | |||
| 85 | Features held by a feature are essentially sub-features. | ||
| 86 | |||
| 87 | =back | ||
| 88 | |||
| 89 | =head1 FEEDBACK | ||
| 90 | |||
| 91 | =head2 Mailing Lists | ||
| 92 | |||
| 93 | User feedback is an integral part of the evolution of this and other | ||
| 94 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 95 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 96 | |||
| 97 | bioperl-l@bioperl.org - General discussion | ||
| 98 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 99 | |||
| 100 | =head2 Support | ||
| 101 | |||
| 102 | Please direct usage questions or support issues to the mailing list: | ||
| 103 | |||
| 104 | I<bioperl-l@bioperl.org> | ||
| 105 | |||
| 106 | rather than to the module maintainer directly. Many experienced and | ||
| 107 | reponsive experts will be able look at the problem and quickly | ||
| 108 | address it. Please include a thorough description of the problem | ||
| 109 | with code and data examples if at all possible. | ||
| 110 | |||
| 111 | =head2 Reporting Bugs | ||
| 112 | |||
| 113 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 114 | the bugs and their resolution. Bug reports can be submitted via | ||
| 115 | the web: | ||
| 116 | |||
| 117 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 118 | |||
| 119 | =head1 AUTHOR - Ewan Birney | ||
| 120 | |||
| 121 | Ewan Birney E<lt>birney@sanger.ac.ukE<gt> | ||
| 122 | |||
| 123 | =head1 DEVELOPERS | ||
| 124 | |||
| 125 | This class has been written with an eye out for inheritance. The fields | ||
| 126 | the actual object hash are: | ||
| 127 | |||
| 128 | _gsf_tag_hash = reference to a hash for the tags | ||
| 129 | _gsf_sub_array = reference to an array for subfeatures | ||
| 130 | |||
| 131 | =head1 APPENDIX | ||
| 132 | |||
| 133 | The rest of the documentation details each of the object | ||
| 134 | methods. Internal methods are usually preceded with a _ | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | |||
| 139 | # Let the code begin... | ||
| 140 | |||
| 141 | |||
| 142 | package FAST::Bio::SeqFeature::Generic; | ||
| 143 | use strict; | ||
| 144 | |||
| 145 | use FAST::Bio::Annotation::Collection; | ||
| 146 | use FAST::Bio::Location::Simple; | ||
| 147 | use FAST::Bio::Location::Split; | ||
| 148 | use FAST::Bio::Tools::GFF; | ||
| 149 | #use Tie::IxHash; | ||
| 150 | |||
| 151 | use base qw(FAST::Bio::Root::Root FAST::Bio::SeqFeatureI FAST::Bio::FeatureHolderI FAST::Bio::AnnotatableI); | ||
| 152 | |||
| 153 | sub new { | ||
| 154 | my ( $caller, @args) = @_; | ||
| 155 | my ($self) = $caller->SUPER::new(@args); | ||
| 156 | $self->_register_for_cleanup(\&cleanup_generic); | ||
| 157 | $self->{'_parse_h'} = {}; | ||
| 158 | $self->{'_gsf_tag_hash'} = {}; | ||
| 159 | |||
| 160 | # bulk-set attributes | ||
| 161 | $self->set_attributes(@args); | ||
| 162 | |||
| 163 | # done - we hope | ||
| 164 | return $self; | ||
| 165 | } | ||
| 166 | |||
| 167 | =head2 set_attributes | ||
| 168 | |||
| 169 | Title : set_attributes | ||
| 170 | Usage : | ||
| 171 | Function: Sets a whole array of parameters at once. | ||
| 172 | Example : | ||
| 173 | Returns : none | ||
| 174 | Args : Named parameters, in the form as they would otherwise be passed | ||
| 175 | to new(). Currently recognized are: | ||
| 176 | |||
| 177 | -start start position | ||
| 178 | -end end position | ||
| 179 | -strand strand | ||
| 180 | -phase the phase of the feature (0..2) | ||
| 181 | -primary_tag primary tag | ||
| 182 | -primary (synonym for -primary_tag) | ||
| 183 | -source source tag | ||
| 184 | -frame frame | ||
| 185 | -score score value | ||
| 186 | -tag a reference to a tag/value hash | ||
| 187 | -gff_string GFF v.2 string to initialize from | ||
| 188 | -gff1_string GFF v.1 string to initialize from | ||
| 189 | -seq_id the display name of the sequence | ||
| 190 | -annotation the AnnotationCollectionI object | ||
| 191 | -location the LocationI object | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | sub set_attributes { | ||
| 196 | my ($self,@args) = @_; | ||
| 197 | my ($start, $end, $strand, $primary_tag, $source_tag, $primary, | ||
| 198 | $source, $frame, $score, $tag, $gff_string, $gff1_string, | ||
| 199 | $seqname, $seqid, $annot, $location,$display_name, $pid,$phase) = | ||
| 200 | $self->_rearrange([qw(START | ||
| 201 | END | ||
| 202 | STRAND | ||
| 203 | PRIMARY_TAG | ||
| 204 | SOURCE_TAG | ||
| 205 | PRIMARY | ||
| 206 | SOURCE | ||
| 207 | FRAME | ||
| 208 | SCORE | ||
| 209 | TAG | ||
| 210 | GFF_STRING | ||
| 211 | GFF1_STRING | ||
| 212 | SEQNAME | ||
| 213 | SEQ_ID | ||
| 214 | ANNOTATION | ||
| 215 | LOCATION | ||
| 216 | DISPLAY_NAME | ||
| 217 | PRIMARY_ID | ||
| 218 | PHASE | ||
| 219 | )], @args); | ||
| 220 | $location && $self->location($location); | ||
| 221 | $gff_string && $self->_from_gff_string($gff_string); | ||
| 222 | $gff1_string && do { | ||
| 223 | $self->gff_format(FAST::Bio::Tools::GFF->new('-gff_version' => 1)); | ||
| 224 | $self->_from_gff_stream($gff1_string); | ||
| 225 | }; | ||
| 226 | |||
| 227 | $pid && $self->primary_id($pid); | ||
| 228 | $primary_tag && $self->primary_tag($primary_tag); | ||
| 229 | $source_tag && $self->source_tag($source_tag); | ||
| 230 | $primary && $self->primary_tag($primary); | ||
| 231 | $source && $self->source_tag($source); | ||
| 232 | defined $start && $self->start($start); | ||
| 233 | defined $end && $self->end($end); | ||
| 234 | defined $strand && $self->strand($strand); | ||
| 235 | defined $frame && $self->frame($frame); | ||
| 236 | defined $display_name && $self->display_name($display_name); | ||
| 237 | defined $score && $self->score($score); | ||
| 238 | $annot && $self->annotation($annot); | ||
| 239 | if($seqname) { | ||
| 240 | $self->warn("-seqname is deprecated. Please use -seq_id instead."); | ||
| 241 | $seqid = $seqname unless $seqid; | ||
| 242 | } | ||
| 243 | $seqid && $self->seq_id($seqid); | ||
| 244 | $tag && do { | ||
| 245 | foreach my $t ( keys %$tag ) { | ||
| 246 | $self->add_tag_value($t, UNIVERSAL::isa($tag->{$t}, "ARRAY") ? @{$tag->{$t}} : $tag->{$t}); | ||
| 247 | } | ||
| 248 | }; | ||
| 249 | defined $phase && $self->phase($phase); | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | =head2 direct_new | ||
| 254 | |||
| 255 | Title : direct_new | ||
| 256 | Usage : my $obj = FAST::Bio::SeqFeature::Generic->direct_new | ||
| 257 | Function: create a blessed hash - for performance improvement in | ||
| 258 | object creation | ||
| 259 | Returns : FAST::Bio::SeqFeature::Generic object | ||
| 260 | Args : none | ||
| 261 | |||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | sub direct_new { | ||
| 266 | my ( $class) = @_; | ||
| 267 | my ($self) = {}; | ||
| 268 | |||
| 269 | bless $self,$class; | ||
| 270 | |||
| 271 | return $self; | ||
| 272 | } | ||
| 273 | |||
| 274 | =head2 location | ||
| 275 | |||
| 276 | Title : location | ||
| 277 | Usage : my $location = $seqfeature->location() | ||
| 278 | Function: returns a location object suitable for identifying location | ||
| 279 | of feature on sequence or parent feature | ||
| 280 | Returns : FAST::Bio::LocationI object | ||
| 281 | Args : [optional] FAST::Bio::LocationI object to set the value to. | ||
| 282 | |||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | sub location { | ||
| 287 | my($self, $value ) = @_; | ||
| 288 | |||
| 289 | if (defined($value)) { | ||
| 290 | unless (ref($value) and $value->isa('FAST::Bio::LocationI')) { | ||
| 291 | $self->throw("object $value pretends to be a location but ". | ||
| 292 | "does not implement FAST::Bio::LocationI"); | ||
| 293 | } | ||
| 294 | $self->{'_location'} = $value; | ||
| 295 | } | ||
| 296 | elsif (! $self->{'_location'}) { | ||
| 297 | # guarantees a real location object is returned every time | ||
| 298 | $self->{'_location'} = FAST::Bio::Location::Simple->new(); | ||
| 299 | } | ||
| 300 | return $self->{'_location'}; | ||
| 301 | } | ||
| 302 | |||
| 303 | |||
| 304 | =head2 start | ||
| 305 | |||
| 306 | Title : start | ||
| 307 | Usage : $start = $feat->start | ||
| 308 | $feat->start(20) | ||
| 309 | Function: Get/set on the start coordinate of the feature | ||
| 310 | Returns : integer | ||
| 311 | Args : none | ||
| 312 | |||
| 313 | |||
| 314 | =cut | ||
| 315 | |||
| 316 | sub start { | ||
| 317 | my ($self,$value) = @_; | ||
| 318 | return $self->location->start($value); | ||
| 319 | } | ||
| 320 | |||
| 321 | =head2 end | ||
| 322 | |||
| 323 | Title : end | ||
| 324 | Usage : $end = $feat->end | ||
| 325 | $feat->end($end) | ||
| 326 | Function: get/set on the end coordinate of the feature | ||
| 327 | Returns : integer | ||
| 328 | Args : none | ||
| 329 | |||
| 330 | |||
| 331 | =cut | ||
| 332 | |||
| 333 | sub end { | ||
| 334 | my ($self,$value) = @_; | ||
| 335 | return $self->location->end($value); | ||
| 336 | } | ||
| 337 | |||
| 338 | =head2 length | ||
| 339 | |||
| 340 | Title : length | ||
| 341 | Usage : my $len = $feature->length | ||
| 342 | Function: Get the feature length computed as | ||
| 343 | $feat->end - $feat->start + 1 | ||
| 344 | Returns : integer | ||
| 345 | Args : none | ||
| 346 | |||
| 347 | |||
| 348 | =cut | ||
| 349 | |||
| 350 | sub length { | ||
| 351 | my $self = shift; | ||
| 352 | return $self->end - $self->start() + 1; | ||
| 353 | } | ||
| 354 | |||
| 355 | =head2 strand | ||
| 356 | |||
| 357 | Title : strand | ||
| 358 | Usage : $strand = $feat->strand() | ||
| 359 | $feat->strand($strand) | ||
| 360 | Function: get/set on strand information, being 1,-1 or 0 | ||
| 361 | Returns : -1,1 or 0 | ||
| 362 | Args : none | ||
| 363 | |||
| 364 | |||
| 365 | =cut | ||
| 366 | |||
| 367 | sub strand { | ||
| 368 | my $self = shift; | ||
| 369 | return $self->location->strand(@_); | ||
| 370 | } | ||
| 371 | |||
| 372 | =head2 score | ||
| 373 | |||
| 374 | Title : score | ||
| 375 | Usage : $score = $feat->score() | ||
| 376 | $feat->score($score) | ||
| 377 | Function: get/set on score information | ||
| 378 | Returns : float | ||
| 379 | Args : none if get, the new value if set | ||
| 380 | |||
| 381 | |||
| 382 | =cut | ||
| 383 | |||
| 384 | sub score { | ||
| 385 | my $self = shift; | ||
| 386 | |||
| 387 | if (@_) { | ||
| 388 | my $value = shift; | ||
| 389 | |||
| 390 | if ( defined $value && $value && $value !~ /^[A-Za-z]+$/ && | ||
| 391 | $value !~ /^[+-]?\d+\.?\d*(e-\d+)?/ and $value != 0) { | ||
| 392 | $self->throw(-class=>'FAST::Bio::Root::BadParameter', | ||
| 393 | -text=>"'$value' is not a valid score", | ||
| 394 | -value=>$value); | ||
| 395 | } | ||
| 396 | if ($self->has_tag('score')) { | ||
| 397 | $self->warn("Removing score value(s)"); | ||
| 398 | $self->remove_tag('score'); | ||
| 399 | } | ||
| 400 | $self->add_tag_value('score',$value); | ||
| 401 | } | ||
| 402 | my ($score) = $self->has_tag('score') ? $self->get_tag_values('score') : undef; | ||
| 403 | return $score; | ||
| 404 | } | ||
| 405 | |||
| 406 | =head2 frame | ||
| 407 | |||
| 408 | Title : frame | ||
| 409 | Usage : $frame = $feat->frame() | ||
| 410 | $feat->frame($frame) | ||
| 411 | Function: get/set on frame information | ||
| 412 | Returns : 0,1,2, '.' | ||
| 413 | Args : none if get, the new value if set | ||
| 414 | |||
| 415 | |||
| 416 | =cut | ||
| 417 | |||
| 418 | sub frame { | ||
| 419 | my $self = shift; | ||
| 420 | |||
| 421 | if ( @_ ) { | ||
| 422 | my $value = shift; | ||
| 423 | if ( defined $value && | ||
| 424 | $value !~ /^[0-2.]$/ ) { | ||
| 425 | $self->throw("'$value' is not a valid frame"); | ||
| 426 | } | ||
| 427 | if( defined $value && $value eq '.' ) { $value = '.' } | ||
| 428 | return $self->{'_gsf_frame'} = $value; | ||
| 429 | } | ||
| 430 | return $self->{'_gsf_frame'}; | ||
| 431 | } | ||
| 432 | |||
| 433 | =head2 primary_tag | ||
| 434 | |||
| 435 | Title : primary_tag | ||
| 436 | Usage : $tag = $feat->primary_tag() | ||
| 437 | $feat->primary_tag('exon') | ||
| 438 | Function: get/set on the primary tag for a feature, | ||
| 439 | eg 'exon' | ||
| 440 | Returns : a string | ||
| 441 | Args : none | ||
| 442 | |||
| 443 | |||
| 444 | =cut | ||
| 445 | |||
| 446 | sub primary_tag { | ||
| 447 | my $self = shift; | ||
| 448 | return $self->{'_primary_tag'} = shift if @_; | ||
| 449 | return $self->{'_primary_tag'}; | ||
| 450 | } | ||
| 451 | |||
| 452 | =head2 source_tag | ||
| 453 | |||
| 454 | Title : source_tag | ||
| 455 | Usage : $tag = $feat->source_tag() | ||
| 456 | $feat->source_tag('genscan'); | ||
| 457 | Function: Returns the source tag for a feature, | ||
| 458 | eg, 'genscan' | ||
| 459 | Returns : a string | ||
| 460 | Args : none | ||
| 461 | |||
| 462 | |||
| 463 | =cut | ||
| 464 | |||
| 465 | sub source_tag { | ||
| 466 | my $self = shift; | ||
| 467 | return $self->{'_source_tag'} = shift if @_; | ||
| 468 | return $self->{'_source_tag'}; | ||
| 469 | } | ||
| 470 | |||
| 471 | =head2 has_tag | ||
| 472 | |||
| 473 | Title : has_tag | ||
| 474 | Usage : $value = $self->has_tag('some_tag') | ||
| 475 | Function: Tests wether a feature contaings a tag | ||
| 476 | Returns : TRUE if the SeqFeature has the tag, | ||
| 477 | and FALSE otherwise. | ||
| 478 | Args : The name of a tag | ||
| 479 | |||
| 480 | |||
| 481 | =cut | ||
| 482 | |||
| 483 | sub has_tag { | ||
| 484 | my ($self, $tag) = @_; | ||
| 485 | return exists $_[0]->{'_gsf_tag_hash'}->{$tag}; | ||
| 486 | } | ||
| 487 | |||
| 488 | =head2 add_tag_value | ||
| 489 | |||
| 490 | Title : add_tag_value | ||
| 491 | Usage : $self->add_tag_value('note',"this is a note"); | ||
| 492 | Returns : TRUE on success | ||
| 493 | Args : tag (string) and one or more values (any scalar(s)) | ||
| 494 | |||
| 495 | |||
| 496 | =cut | ||
| 497 | |||
| 498 | sub add_tag_value { | ||
| 499 | my $self = shift; | ||
| 500 | my $tag = shift; | ||
| 501 | $self->{'_gsf_tag_hash'}->{$tag} ||= []; | ||
| 502 | push(@{$self->{'_gsf_tag_hash'}->{$tag}},@_); | ||
| 503 | } | ||
| 504 | |||
| 505 | |||
| 506 | =head2 get_tag_values | ||
| 507 | |||
| 508 | Title : get_tag_values | ||
| 509 | Usage : @values = $gsf->get_tag_values('note'); | ||
| 510 | Function: Returns a list of all the values stored | ||
| 511 | under a particular tag. | ||
| 512 | Returns : A list of scalars | ||
| 513 | Args : The name of the tag | ||
| 514 | |||
| 515 | |||
| 516 | =cut | ||
| 517 | |||
| 518 | sub get_tag_values { | ||
| 519 | my ($self, $tag) = @_; | ||
| 520 | |||
| 521 | if( ! defined $tag ) { return (); } | ||
| 522 | if ( ! exists $self->{'_gsf_tag_hash'}->{$tag} ) { | ||
| 523 | $self->throw("asking for tag value that does not exist $tag"); | ||
| 524 | } | ||
| 525 | return @{$self->{'_gsf_tag_hash'}->{$tag}}; | ||
| 526 | } | ||
| 527 | |||
| 528 | |||
| 529 | =head2 get_all_tags | ||
| 530 | |||
| 531 | Title : get_all_tags | ||
| 532 | Usage : @tags = $feat->get_all_tags() | ||
| 533 | Function: Get a list of all the tags in a feature | ||
| 534 | Returns : An array of tag names | ||
| 535 | Args : none | ||
| 536 | |||
| 537 | # added a sort so that tags will be returned in a predictable order | ||
| 538 | # I still think we should be able to specify a sort function | ||
| 539 | # to the object at some point | ||
| 540 | # -js | ||
| 541 | |||
| 542 | =cut | ||
| 543 | |||
| 544 | sub get_all_tags { | ||
| 545 | my ($self, @args) = @_; | ||
| 546 | return sort keys %{ $self->{'_gsf_tag_hash'}}; | ||
| 547 | } | ||
| 548 | |||
| 549 | =head2 remove_tag | ||
| 550 | |||
| 551 | Title : remove_tag | ||
| 552 | Usage : $feat->remove_tag('some_tag') | ||
| 553 | Function: removes a tag from this feature | ||
| 554 | Returns : the array of values for this tag before removing it | ||
| 555 | Args : tag (string) | ||
| 556 | |||
| 557 | |||
| 558 | =cut | ||
| 559 | |||
| 560 | sub remove_tag { | ||
| 561 | my ($self, $tag) = @_; | ||
| 562 | |||
| 563 | if ( ! exists $self->{'_gsf_tag_hash'}->{$tag} ) { | ||
| 564 | $self->throw("trying to remove a tag that does not exist: $tag"); | ||
| 565 | } | ||
| 566 | my @vals = @{$self->{'_gsf_tag_hash'}->{$tag}}; | ||
| 567 | delete $self->{'_gsf_tag_hash'}->{$tag}; | ||
| 568 | return @vals; | ||
| 569 | } | ||
| 570 | |||
| 571 | =head2 attach_seq | ||
| 572 | |||
| 573 | Title : attach_seq | ||
| 574 | Usage : $sf->attach_seq($seq) | ||
| 575 | Function: Attaches a FAST::Bio::Seq object to this feature. This | ||
| 576 | FAST::Bio::Seq object is for the *entire* sequence: ie | ||
| 577 | from 1 to 10000 | ||
| 578 | Example : | ||
| 579 | Returns : TRUE on success | ||
| 580 | Args : a FAST::Bio::PrimarySeqI compliant object | ||
| 581 | |||
| 582 | |||
| 583 | =cut | ||
| 584 | |||
| 585 | sub attach_seq { | ||
| 586 | my ($self, $seq) = @_; | ||
| 587 | |||
| 588 | if ( ! ($seq && ref($seq) && $seq->isa("FAST::Bio::PrimarySeqI")) ) { | ||
| 589 | $self->throw("Must attach FAST::Bio::PrimarySeqI objects to SeqFeatures"); | ||
| 590 | } | ||
| 591 | |||
| 592 | $self->{'_gsf_seq'} = $seq; | ||
| 593 | |||
| 594 | # attach to sub features if they want it | ||
| 595 | foreach ( $self->sub_SeqFeature() ) { | ||
| 596 | $_->attach_seq($seq); | ||
| 597 | } | ||
| 598 | return 1; | ||
| 599 | } | ||
| 600 | |||
| 601 | =head2 seq | ||
| 602 | |||
| 603 | Title : seq | ||
| 604 | Usage : $tseq = $sf->seq() | ||
| 605 | Function: returns the truncated sequence (if there) for this | ||
| 606 | Example : | ||
| 607 | Returns : sub seq (a FAST::Bio::PrimarySeqI compliant object) on attached sequence | ||
| 608 | bounded by start & end, or undef if there is no sequence attached | ||
| 609 | Args : none | ||
| 610 | |||
| 611 | |||
| 612 | =cut | ||
| 613 | |||
| 614 | sub seq { | ||
| 615 | my ($self, $arg) = @_; | ||
| 616 | |||
| 617 | if ( defined $arg ) { | ||
| 618 | $self->throw("Calling SeqFeature::Generic->seq with an argument. You probably want attach_seq"); | ||
| 619 | } | ||
| 620 | |||
| 621 | if ( ! exists $self->{'_gsf_seq'} ) { | ||
| 622 | return; | ||
| 623 | } | ||
| 624 | |||
| 625 | # assumming our seq object is sensible, it should not have to yank | ||
| 626 | # the entire sequence out here. | ||
| 627 | |||
| 628 | my $seq = $self->{'_gsf_seq'}->trunc($self->start(), $self->end()); | ||
| 629 | |||
| 630 | |||
| 631 | if ( defined $self->strand && | ||
| 632 | $self->strand == -1 ) { | ||
| 633 | |||
| 634 | # ok. this does not work well (?) | ||
| 635 | #print STDERR "Before revcom", $seq->str, "\n"; | ||
| 636 | $seq = $seq->revcom; | ||
| 637 | #print STDERR "After revcom", $seq->str, "\n"; | ||
| 638 | } | ||
| 639 | |||
| 640 | return $seq; | ||
| 641 | } | ||
| 642 | |||
| 643 | =head2 entire_seq | ||
| 644 | |||
| 645 | Title : entire_seq | ||
| 646 | Usage : $whole_seq = $sf->entire_seq() | ||
| 647 | Function: gives the entire sequence that this seqfeature is attached to | ||
| 648 | Example : | ||
| 649 | Returns : a FAST::Bio::PrimarySeqI compliant object, or undef if there is no | ||
| 650 | sequence attached | ||
| 651 | Args : | ||
| 652 | |||
| 653 | |||
| 654 | =cut | ||
| 655 | |||
| 656 | sub entire_seq { | ||
| 657 | return shift->{'_gsf_seq'}; | ||
| 658 | } | ||
| 659 | |||
| 660 | |||
| 661 | =head2 seq_id | ||
| 662 | |||
| 663 | Title : seq_id | ||
| 664 | Usage : $obj->seq_id($newval) | ||
| 665 | Function: There are many cases when you make a feature that you | ||
| 666 | do know the sequence name, but do not know its actual | ||
| 667 | sequence. This is an attribute such that you can store | ||
| 668 | the ID (e.g., display_id) of the sequence. | ||
| 669 | |||
| 670 | This attribute should *not* be used in GFF dumping, as | ||
| 671 | that should come from the collection in which the seq | ||
| 672 | feature was found. | ||
| 673 | Returns : value of seq_id | ||
| 674 | Args : newvalue (optional) | ||
| 675 | |||
| 676 | |||
| 677 | =cut | ||
| 678 | |||
| 679 | sub seq_id { | ||
| 680 | my $obj = shift; | ||
| 681 | return $obj->{'_gsf_seq_id'} = shift if @_; | ||
| 682 | return $obj->{'_gsf_seq_id'}; | ||
| 683 | } | ||
| 684 | |||
| 685 | =head2 display_name | ||
| 686 | |||
| 687 | Title : display_name | ||
| 688 | Usage : $featname = $obj->display_name | ||
| 689 | Function: Implements the display_name() method, which is a human-readable | ||
| 690 | name for the feature. | ||
| 691 | Returns : value of display_name (a string) | ||
| 692 | Args : Optionally, on set the new value or undef | ||
| 693 | |||
| 694 | =cut | ||
| 695 | |||
| 696 | sub display_name{ | ||
| 697 | my $self = shift; | ||
| 698 | return $self->{'display_name'} = shift if @_; | ||
| 699 | return $self->{'display_name'}; | ||
| 700 | } | ||
| 701 | |||
| 702 | =head1 Methods for implementing FAST::Bio::AnnotatableI | ||
| 703 | |||
| 704 | =cut | ||
| 705 | |||
| 706 | =head2 annotation | ||
| 707 | |||
| 708 | Title : annotation | ||
| 709 | Usage : $obj->annotation($annot_obj) | ||
| 710 | Function: Get/set the annotation collection object for annotating this | ||
| 711 | feature. | ||
| 712 | |||
| 713 | Example : | ||
| 714 | Returns : A FAST::Bio::AnnotationCollectionI object | ||
| 715 | Args : newvalue (optional) | ||
| 716 | |||
| 717 | |||
| 718 | =cut | ||
| 719 | |||
| 720 | sub annotation { | ||
| 721 | my ($obj,$value) = @_; | ||
| 722 | |||
| 723 | # we are smart if someone references the object and there hasn't been | ||
| 724 | # one set yet | ||
| 725 | if(defined $value || ! defined $obj->{'annotation'} ) { | ||
| 726 | $value = FAST::Bio::Annotation::Collection->new() unless ( defined $value ); | ||
| 727 | $obj->{'annotation'} = $value; | ||
| 728 | } | ||
| 729 | return $obj->{'annotation'}; | ||
| 730 | } | ||
| 731 | |||
| 732 | =head1 Methods to implement FAST::Bio::FeatureHolderI | ||
| 733 | |||
| 734 | This includes methods for retrieving, adding, and removing | ||
| 735 | features. Since this is already a feature, features held by this | ||
| 736 | feature holder are essentially sub-features. | ||
| 737 | |||
| 738 | =cut | ||
| 739 | |||
| 740 | =head2 get_SeqFeatures | ||
| 741 | |||
| 742 | Title : get_SeqFeatures | ||
| 743 | Usage : @feats = $feat->get_SeqFeatures(); | ||
| 744 | Function: Returns an array of sub Sequence Features | ||
| 745 | Returns : An array | ||
| 746 | Args : none | ||
| 747 | |||
| 748 | |||
| 749 | =cut | ||
| 750 | |||
| 751 | sub get_SeqFeatures { | ||
| 752 | return @{ shift->{'_gsf_sub_array'} || []}; | ||
| 753 | } | ||
| 754 | |||
| 755 | =head2 add_SeqFeature | ||
| 756 | |||
| 757 | Title : add_SeqFeature | ||
| 758 | Usage : $feat->add_SeqFeature($subfeat); | ||
| 759 | $feat->add_SeqFeature($subfeat,'EXPAND') | ||
| 760 | Function: Adds a SeqFeature into the subSeqFeature array. | ||
| 761 | With no 'EXPAND' qualifer, subfeat will be tested | ||
| 762 | as to whether it lies inside the parent, and throw | ||
| 763 | an exception if not. | ||
| 764 | |||
| 765 | If EXPAND is used, the parent's start/end/strand will | ||
| 766 | be adjusted so that it grows to accommodate the new | ||
| 767 | subFeature | ||
| 768 | Returns : nothing | ||
| 769 | Args : An object which has the SeqFeatureI interface | ||
| 770 | |||
| 771 | |||
| 772 | =cut | ||
| 773 | |||
| 774 | #' | ||
| 775 | sub add_SeqFeature{ | ||
| 776 | my ($self,$feat,$expand) = @_; | ||
| 777 | unless( defined $feat ) { | ||
| 778 | $self->warn("Called add_SeqFeature with no feature, ignoring"); | ||
| 779 | return; | ||
| 780 | } | ||
| 781 | if ( !$feat->isa('FAST::Bio::SeqFeatureI') ) { | ||
| 782 | $self->warn("$feat does not implement FAST::Bio::SeqFeatureI. Will add it anyway, but beware..."); | ||
| 783 | } | ||
| 784 | |||
| 785 | if($expand && ($expand eq 'EXPAND')) { | ||
| 786 | $self->_expand_region($feat); | ||
| 787 | } else { | ||
| 788 | if ( !$self->contains($feat) ) { | ||
| 789 | $self->throw("$feat is not contained within parent feature, and expansion is not valid"); | ||
| 790 | } | ||
| 791 | } | ||
| 792 | |||
| 793 | $self->{'_gsf_sub_array'} = [] unless exists($self->{'_gsf_sub_array'}); | ||
| 794 | push(@{$self->{'_gsf_sub_array'}},$feat); | ||
| 795 | |||
| 796 | } | ||
| 797 | |||
| 798 | =head2 remove_SeqFeatures | ||
| 799 | |||
| 800 | Title : remove_SeqFeatures | ||
| 801 | Usage : $sf->remove_SeqFeatures | ||
| 802 | Function: Removes all SeqFeatures | ||
| 803 | |||
| 804 | If you want to remove only a subset of features then remove that | ||
| 805 | subset from the returned array, and add back the rest. | ||
| 806 | Example : | ||
| 807 | Returns : The array of FAST::Bio::SeqFeatureI implementing features that was | ||
| 808 | deleted. | ||
| 809 | Args : none | ||
| 810 | |||
| 811 | |||
| 812 | =cut | ||
| 813 | |||
| 814 | sub remove_SeqFeatures { | ||
| 815 | my ($self) = @_; | ||
| 816 | |||
| 817 | my @subfeats = @{$self->{'_gsf_sub_array'} || []}; | ||
| 818 | $self->{'_gsf_sub_array'} = []; # zap the array implicitly. | ||
| 819 | return @subfeats; | ||
| 820 | } | ||
| 821 | |||
| 822 | =head1 GFF-related methods | ||
| 823 | |||
| 824 | =cut | ||
| 825 | |||
| 826 | =head2 gff_format | ||
| 827 | |||
| 828 | Title : gff_format | ||
| 829 | Usage : # get: | ||
| 830 | $gffio = $feature->gff_format(); | ||
| 831 | # set (change the default version of GFF2): | ||
| 832 | $feature->gff_format(FAST::Bio::Tools::GFF->new(-gff_version => 1)); | ||
| 833 | Function: Get/set the GFF format interpreter. This object is supposed to | ||
| 834 | format and parse GFF. See FAST::Bio::Tools::GFF for the interface. | ||
| 835 | |||
| 836 | If this method is called as class method, the default for all | ||
| 837 | newly created instances will be changed. Otherwise only this | ||
| 838 | instance will be affected. | ||
| 839 | Example : | ||
| 840 | Returns : a FAST::Bio::Tools::GFF compliant object | ||
| 841 | Args : On set, an instance of FAST::Bio::Tools::GFF or a derived object. | ||
| 842 | |||
| 843 | |||
| 844 | =cut | ||
| 845 | |||
| 846 | sub gff_format { | ||
| 847 | my ($self, $gffio) = @_; | ||
| 848 | |||
| 849 | if(defined($gffio)) { | ||
| 850 | if(ref($self)) { | ||
| 851 | $self->{'_gffio'} = $gffio; | ||
| 852 | } else { | ||
| 853 | $FAST::Bio::SeqFeatureI::static_gff_formatter = $gffio; | ||
| 854 | } | ||
| 855 | } | ||
| 856 | return (ref($self) && exists($self->{'_gffio'}) ? | ||
| 857 | $self->{'_gffio'} : $self->_static_gff_formatter); | ||
| 858 | } | ||
| 859 | |||
| 860 | =head2 gff_string | ||
| 861 | |||
| 862 | Title : gff_string | ||
| 863 | Usage : $str = $feat->gff_string; | ||
| 864 | $str = $feat->gff_string($gff_formatter); | ||
| 865 | Function: Provides the feature information in GFF format. | ||
| 866 | |||
| 867 | We override this here from FAST::Bio::SeqFeatureI in order to use the | ||
| 868 | formatter returned by gff_format(). | ||
| 869 | |||
| 870 | Returns : A string | ||
| 871 | Args : Optionally, an object implementing gff_string(). | ||
| 872 | |||
| 873 | |||
| 874 | =cut | ||
| 875 | |||
| 876 | sub gff_string{ | ||
| 877 | my ($self,$formatter) = @_; | ||
| 878 | |||
| 879 | $formatter = $self->gff_format() unless $formatter; | ||
| 880 | return $formatter->gff_string($self); | ||
| 881 | } | ||
| 882 | |||
| 883 | =head2 slurp_gff_file | ||
| 884 | |||
| 885 | Title : slurp_file | ||
| 886 | Usage : @features = FAST::Bio::SeqFeature::Generic::slurp_gff_file(\*FILE); | ||
| 887 | Function: Sneaky function to load an entire file as in memory objects. | ||
| 888 | Beware of big files. | ||
| 889 | |||
| 890 | This method is deprecated. Use FAST::Bio::Tools::GFF instead, which can | ||
| 891 | also handle large files. | ||
| 892 | |||
| 893 | Example : | ||
| 894 | Returns : | ||
| 895 | Args : | ||
| 896 | |||
| 897 | =cut | ||
| 898 | |||
| 899 | sub slurp_gff_file { | ||
| 900 | my ($f) = @_; | ||
| 901 | my @out; | ||
| 902 | if ( !defined $f ) { | ||
| 903 | FAST::Bio::Root::Root->throw("Must have a filehandle"); | ||
| 904 | } | ||
| 905 | |||
| 906 | FAST::Bio::Root::Root->deprecated( -message => "deprecated method slurp_gff_file() called in FAST::Bio::SeqFeature::Generic. Use FAST::Bio::Tools::GFF instead.", | ||
| 907 | -warn_version => '1.005', | ||
| 908 | -throw_version => '1.007', | ||
| 909 | ); | ||
| 910 | |||
| 911 | while(<$f>) { | ||
| 912 | my $sf = FAST::Bio::SeqFeature::Generic->new('-gff_string' => $_); | ||
| 913 | push(@out, $sf); | ||
| 914 | } | ||
| 915 | |||
| 916 | return @out; | ||
| 917 | } | ||
| 918 | |||
| 919 | =head2 _from_gff_string | ||
| 920 | |||
| 921 | Title : _from_gff_string | ||
| 922 | Usage : | ||
| 923 | Function: Set feature properties from GFF string. | ||
| 924 | |||
| 925 | This method uses the object returned by gff_format() for the | ||
| 926 | actual interpretation of the string. Set a different GFF format | ||
| 927 | interpreter first if you need a specific version, like GFF1. (The | ||
| 928 | default is GFF2.) | ||
| 929 | Example : | ||
| 930 | Returns : | ||
| 931 | Args : a GFF-formatted string | ||
| 932 | |||
| 933 | |||
| 934 | =cut | ||
| 935 | |||
| 936 | sub _from_gff_string { | ||
| 937 | my ($self, $string) = @_; | ||
| 938 | |||
| 939 | $self->gff_format()->from_gff_string($self, $string); | ||
| 940 | } | ||
| 941 | |||
| 942 | |||
| 943 | =head2 _expand_region | ||
| 944 | |||
| 945 | Title : _expand_region | ||
| 946 | Usage : $self->_expand_region($feature); | ||
| 947 | Function: Expand the total region covered by this feature to | ||
| 948 | accommodate for the given feature. | ||
| 949 | |||
| 950 | May be called whenever any kind of subfeature is added to this | ||
| 951 | feature. add_sub_SeqFeature() already does this. | ||
| 952 | Returns : | ||
| 953 | Args : A FAST::Bio::SeqFeatureI implementing object. | ||
| 954 | |||
| 955 | |||
| 956 | =cut | ||
| 957 | |||
| 958 | sub _expand_region { | ||
| 959 | my ($self, $feat) = @_; | ||
| 960 | if(! $feat->isa('FAST::Bio::SeqFeatureI')) { | ||
| 961 | $self->warn("$feat does not implement FAST::Bio::SeqFeatureI"); | ||
| 962 | } | ||
| 963 | # if this doesn't have start set - forget it! | ||
| 964 | # changed to reflect sanity checks for LocationI | ||
| 965 | if(!$self->location->valid_Location) { | ||
| 966 | $self->start($feat->start); | ||
| 967 | $self->end($feat->end); | ||
| 968 | $self->strand($feat->strand) unless $self->strand; | ||
| 969 | } else { | ||
| 970 | my ($start,$end,$strand) = $self->union($feat); | ||
| 971 | $self->start($start); | ||
| 972 | $self->end($end); | ||
| 973 | $self->strand($strand); | ||
| 974 | } | ||
| 975 | } | ||
| 976 | |||
| 977 | =head2 _parse | ||
| 978 | |||
| 979 | Title : _parse | ||
| 980 | Usage : | ||
| 981 | Function: Parsing hints | ||
| 982 | Example : | ||
| 983 | Returns : | ||
| 984 | Args : | ||
| 985 | |||
| 986 | |||
| 987 | =cut | ||
| 988 | |||
| 989 | sub _parse { | ||
| 990 | my ($self) = @_; | ||
| 991 | return $self->{'_parse_h'}; | ||
| 992 | } | ||
| 993 | |||
| 994 | =head2 _tag_value | ||
| 995 | |||
| 996 | Title : _tag_value | ||
| 997 | Usage : | ||
| 998 | Function: For internal use only. Convenience method for those tags that | ||
| 999 | may only have a single value. | ||
| 1000 | Returns : The first value under the given tag as a scalar (string) | ||
| 1001 | Args : The tag as a string. Optionally, the value on set. | ||
| 1002 | |||
| 1003 | |||
| 1004 | =cut | ||
| 1005 | |||
| 1006 | sub _tag_value { | ||
| 1007 | my $self = shift; | ||
| 1008 | my $tag = shift; | ||
| 1009 | |||
| 1010 | if(@_ || (! $self->has_tag($tag))) { | ||
| 1011 | $self->remove_tag($tag) if($self->has_tag($tag)); | ||
| 1012 | $self->add_tag_value($tag, @_); | ||
| 1013 | } | ||
| 1014 | return ($self->get_tag_values($tag))[0]; | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | ####################################################################### | ||
| 1018 | # aliases for methods that changed their names in an attempt to make # | ||
| 1019 | # bioperl names more consistent # | ||
| 1020 | ####################################################################### | ||
| 1021 | |||
| 1022 | sub seqname { | ||
| 1023 | my $self = shift; | ||
| 1024 | $self->warn("SeqFeatureI::seqname() is deprecated. Please use seq_id() instead."); | ||
| 1025 | return $self->seq_id(@_); | ||
| 1026 | } | ||
| 1027 | |||
| 1028 | sub display_id { | ||
| 1029 | my $self = shift; | ||
| 1030 | $self->warn("SeqFeatureI::display_id() is deprecated. Please use display_name() instead."); | ||
| 1031 | return $self->display_name(@_); | ||
| 1032 | } | ||
| 1033 | |||
| 1034 | # # this is towards consistent naming | ||
| 1035 | sub each_tag_value { return shift->get_tag_values(@_); } | ||
| 1036 | sub all_tags { return shift->get_all_tags(@_); } | ||
| 1037 | |||
| 1038 | # we revamped the feature containing property to implementing | ||
| 1039 | # FAST::Bio::FeatureHolderI | ||
| 1040 | *sub_SeqFeature = \&get_SeqFeatures; | ||
| 1041 | *add_sub_SeqFeature = \&add_SeqFeature; | ||
| 1042 | *flush_sub_SeqFeatures = \&remove_SeqFeatures; | ||
| 1043 | # this one is because of inconsistent naming ... | ||
| 1044 | *flush_sub_SeqFeature = \&remove_SeqFeatures; | ||
| 1045 | |||
| 1046 | |||
| 1047 | sub cleanup_generic { | ||
| 1048 | my $self = shift; | ||
| 1049 | foreach my $f ( @{$self->{'_gsf_sub_array'} || []} ) { | ||
| 1050 | $f = undef; | ||
| 1051 | } | ||
| 1052 | $self->{'_gsf_seq'} = undef; | ||
| 1053 | foreach my $t ( keys %{$self->{'_gsf_tag_hash'} } ) { | ||
| 1054 | $self->{'_gsf_tag_hash'}->{$t} = undef; | ||
| 1055 | delete($self->{'_gsf_tag_hash'}->{$t}); # bug 1720 fix | ||
| 1056 | } | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | 1; |