| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::SeqFeature::FeaturePair | ||
| 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::FeaturePair - hold pair feature information e.g. blast hits | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | my $feat = FAST::Bio::SeqFeature::FeaturePair->new(-feature1 => $f1, | ||
| 21 | -feature2 => $f2, | ||
| 22 | ); | ||
| 23 | |||
| 24 | # FAST::Bio::SeqFeatureI methods can be used | ||
| 25 | |||
| 26 | my $start = $feat->start; | ||
| 27 | my $end = $feat->end; | ||
| 28 | |||
| 29 | # FAST::Bio::FeaturePair methods can be used | ||
| 30 | my $hstart = $feat->hstart; | ||
| 31 | my $hend = $feat->hend; | ||
| 32 | |||
| 33 | my $feature1 = $feat->feature1; # returns feature1 object | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | A sequence feature object where the feature is itself a feature on | ||
| 38 | another sequence - e.g. a blast hit where residues 1-40 of a protein | ||
| 39 | sequence SW:HBA_HUMAN has hit to bases 100 - 220 on a genomic sequence | ||
| 40 | HS120G22. The genomic sequence coordinates are used to create one | ||
| 41 | sequence feature $f1 and the protein coordinates are used to create | ||
| 42 | feature $f2. A FeaturePair object can then be made | ||
| 43 | |||
| 44 | my $fp = FAST::Bio::SeqFeature::FeaturePair->new(-feature1 => $f1, # genomic | ||
| 45 | -feature2 => $f2, # protein | ||
| 46 | ); | ||
| 47 | |||
| 48 | This object can be used as a standard FAST::Bio::SeqFeatureI in which case | ||
| 49 | |||
| 50 | my $gstart = $fp->start # returns start coord on feature1 - genomic seq. | ||
| 51 | my $gend = $fp->end # returns end coord on feature1. | ||
| 52 | |||
| 53 | In general standard FAST::Bio::SeqFeatureI method calls return information | ||
| 54 | in feature1. | ||
| 55 | |||
| 56 | Data in the feature 2 object are generally obtained using the standard | ||
| 57 | methods prefixed by h (for hit!) | ||
| 58 | |||
| 59 | my $pstart = $fp->hstart # returns start coord on feature2 = protein seq. | ||
| 60 | my $pend = $fp->hend # returns end coord on feature2. | ||
| 61 | |||
| 62 | If you wish to swap feature1 and feature2 around : | ||
| 63 | |||
| 64 | $feat->invert | ||
| 65 | |||
| 66 | so... | ||
| 67 | |||
| 68 | $feat->start # etc. returns data in $feature2 object | ||
| 69 | |||
| 70 | |||
| 71 | No sub_SeqFeatures or tags can be stored in this object directly. Any | ||
| 72 | features or tags are expected to be stored in the contained objects | ||
| 73 | feature1, and feature2. | ||
| 74 | |||
| 75 | =head1 CONTACT | ||
| 76 | |||
| 77 | Ewan Birney E<lt>birney@sanger.ac.ukE<gt> | ||
| 78 | |||
| 79 | =head1 APPENDIX | ||
| 80 | |||
| 81 | The rest of the documentation details each of the object | ||
| 82 | methods. Internal methods are usually preceded with a _ | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | |||
| 87 | # Let the code begin... | ||
| 88 | |||
| 89 | |||
| 90 | package FAST::Bio::SeqFeature::FeaturePair; | ||
| 91 | use vars qw($AUTOLOAD); | ||
| 92 | use strict; | ||
| 93 | |||
| 94 | use FAST::Bio::SeqFeatureI; | ||
| 95 | use FAST::Bio::Factory::ObjectFactory; | ||
| 96 | |||
| 97 | use base qw(FAST::Bio::SeqFeature::Generic); | ||
| 98 | |||
| 99 | =head2 new | ||
| 100 | |||
| 101 | Title : new | ||
| 102 | Usage : | ||
| 103 | Function: Constructor for this module. Accepts the following parameters: | ||
| 104 | |||
| 105 | -feature1 FAST::Bio::SeqFeatureI-compliant object | ||
| 106 | -feature2 FAST::Bio::SeqFeatureI-compliant object | ||
| 107 | -feature_factory FAST::Bio::Factory::ObjectFactoryI compliant | ||
| 108 | object to be used when feature1 and/or feature2 | ||
| 109 | are accessed without explicitly set before. This | ||
| 110 | is mostly useful for derived classes who want to | ||
| 111 | set their preferred class for feature objects. | ||
| 112 | |||
| 113 | Example : | ||
| 114 | Returns : | ||
| 115 | Args : see above | ||
| 116 | |||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub new { | ||
| 121 | my ($class, @args) = @_; | ||
| 122 | |||
| 123 | # | ||
| 124 | # We've got a certain problem here that somewhat relates to chicken and | ||
| 125 | # eggs. The problem is, we override a lot of SeqFeatureI methods here | ||
| 126 | # to delegate them to either feature1 or feature2. If we pass along | ||
| 127 | # those attributes right away, we need feature1 or feature2 or the feature | ||
| 128 | # factory in place, or there is no way around the dreaded default, which | ||
| 129 | # is ugly too (as it necessitates subsequent copying if you wanted a | ||
| 130 | # different feature object class). | ||
| 131 | # | ||
| 132 | # So I decided to go with the lesser of two evils here: we need to assume | ||
| 133 | # here that we can set all attributes through set_attributes(), which we | ||
| 134 | # assume is no different from setting them through the constructor. This | ||
| 135 | # gives us a window to set the feature objects and the factory, such that | ||
| 136 | # any derived class doesn't have to worry about this any more. | ||
| 137 | # | ||
| 138 | # I'm happy to hear a better solution, but I think this one isn't so bad. | ||
| 139 | # | ||
| 140 | my $self = $class->SUPER::new(); | ||
| 141 | my ($feature1,$feature2,$featfact) = | ||
| 142 | $self->_rearrange([qw(FEATURE1 | ||
| 143 | FEATURE2 | ||
| 144 | FEATURE_FACTORY | ||
| 145 | )],@args); | ||
| 146 | |||
| 147 | $self->_register_for_cleanup(\&cleanup_fp); | ||
| 148 | # initialize the feature object factory if not provided | ||
| 149 | if(! $featfact) { | ||
| 150 | $featfact = FAST::Bio::Factory::ObjectFactory->new( | ||
| 151 | -type => "FAST::Bio::SeqFeature::Generic", | ||
| 152 | -interface => "FAST::Bio::SeqFeatureI"); | ||
| 153 | } | ||
| 154 | $self->feature_factory($featfact); | ||
| 155 | # Store the features in the object | ||
| 156 | $feature1 && $self->feature1($feature1); | ||
| 157 | $feature2 && $self->feature2($feature2); | ||
| 158 | |||
| 159 | # OK. Now we're setup to store all the attributes, and they'll go right | ||
| 160 | # away into the right objects. | ||
| 161 | $self->set_attributes(@args); | ||
| 162 | |||
| 163 | # done - we hope | ||
| 164 | return $self; | ||
| 165 | } | ||
| 166 | |||
| 167 | =head2 feature1 | ||
| 168 | |||
| 169 | Title : feature1 | ||
| 170 | Usage : $f = $featpair->feature1 | ||
| 171 | $featpair->feature1($feature) | ||
| 172 | Function: Get/set for the query feature | ||
| 173 | Returns : FAST::Bio::SeqFeatureI | ||
| 174 | Args : FAST::Bio::SeqFeatureI | ||
| 175 | |||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub feature1 { | ||
| 180 | my ($self,$arg) = @_; | ||
| 181 | if ( defined($arg) || !defined $self->{'feature1'} ) { | ||
| 182 | $self->throw("internal error: feature factory not set!") | ||
| 183 | unless $self->feature_factory; | ||
| 184 | $arg = $self->feature_factory->create_object() unless( defined $arg); | ||
| 185 | $self->throw("Argument [$arg] must be a FAST::Bio::SeqFeatureI") | ||
| 186 | unless (ref($arg) && $arg->isa("FAST::Bio::SeqFeatureI")); | ||
| 187 | $self->{'feature1'} = $arg; | ||
| 188 | } | ||
| 189 | return $self->{'feature1'}; | ||
| 190 | } | ||
| 191 | |||
| 192 | =head2 feature2 | ||
| 193 | |||
| 194 | Title : feature2 | ||
| 195 | Usage : $f = $featpair->feature2 | ||
| 196 | $featpair->feature2($feature) | ||
| 197 | Function: Get/set for the hit feature | ||
| 198 | Returns : FAST::Bio::SeqFeatureI | ||
| 199 | Args : FAST::Bio::SeqFeatureI | ||
| 200 | |||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | sub feature2 { | ||
| 205 | my ($self,$arg) = @_; | ||
| 206 | |||
| 207 | if ( defined($arg) || ! defined $self->{'feature2'}) { | ||
| 208 | $self->throw("internal error: feature factory not set!") | ||
| 209 | unless $self->feature_factory; | ||
| 210 | $arg = $self->feature_factory->create_object() unless( defined $arg); | ||
| 211 | $self->throw("Argument [$arg] must be a FAST::Bio::SeqFeatureI") | ||
| 212 | unless (ref($arg) && $arg->isa("FAST::Bio::SeqFeatureI")); | ||
| 213 | $self->{'feature2'} = $arg; | ||
| 214 | } | ||
| 215 | return $self->{'feature2'}; | ||
| 216 | } | ||
| 217 | |||
| 218 | =head2 start | ||
| 219 | |||
| 220 | Title : start | ||
| 221 | Usage : $start = $featpair->start | ||
| 222 | $featpair->start(20) | ||
| 223 | Function: Get/set on the start coordinate of feature1 | ||
| 224 | Returns : integer | ||
| 225 | Args : [optional] beginning of feature | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | sub start { | ||
| 230 | return shift->feature1->start(@_); | ||
| 231 | } | ||
| 232 | |||
| 233 | =head2 end | ||
| 234 | |||
| 235 | Title : end | ||
| 236 | Usage : $end = $featpair->end | ||
| 237 | $featpair->end($end) | ||
| 238 | Function: get/set on the end coordinate of feature1 | ||
| 239 | Returns : integer | ||
| 240 | Args : [optional] ending point of feature | ||
| 241 | |||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub end{ | ||
| 246 | return shift->feature1->end(@_); | ||
| 247 | } | ||
| 248 | |||
| 249 | =head2 strand | ||
| 250 | |||
| 251 | Title : strand | ||
| 252 | Usage : $strand = $feat->strand() | ||
| 253 | $feat->strand($strand) | ||
| 254 | Function: get/set on strand information, being 1,-1 or 0 | ||
| 255 | Returns : -1,1 or 0 | ||
| 256 | Args : [optional] strand information to set | ||
| 257 | |||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | sub strand{ | ||
| 262 | return shift->feature1->strand(@_); | ||
| 263 | } | ||
| 264 | |||
| 265 | =head2 location | ||
| 266 | |||
| 267 | Title : location | ||
| 268 | Usage : $location = $featpair->location | ||
| 269 | $featpair->location($location) | ||
| 270 | Function: Get/set location object (using feature1) | ||
| 271 | Returns : FAST::Bio::LocationI object | ||
| 272 | Args : [optional] LocationI to store | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | sub location { | ||
| 277 | return shift->feature1->location(@_); | ||
| 278 | } | ||
| 279 | |||
| 280 | =head2 score | ||
| 281 | |||
| 282 | Title : score | ||
| 283 | Usage : $score = $feat->score() | ||
| 284 | $feat->score($score) | ||
| 285 | Function: get/set on score information | ||
| 286 | Returns : float | ||
| 287 | Args : none if get, the new value if set | ||
| 288 | |||
| 289 | |||
| 290 | =cut | ||
| 291 | |||
| 292 | sub score { | ||
| 293 | return shift->feature1->score(@_); | ||
| 294 | } | ||
| 295 | |||
| 296 | =head2 frame | ||
| 297 | |||
| 298 | Title : frame | ||
| 299 | Usage : $frame = $feat->frame() | ||
| 300 | $feat->frame($frame) | ||
| 301 | Function: get/set on frame information | ||
| 302 | Returns : 0,1,2 | ||
| 303 | Args : none if get, the new value if set | ||
| 304 | |||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | sub frame { | ||
| 309 | return shift->feature1->frame(@_); | ||
| 310 | } | ||
| 311 | |||
| 312 | =head2 primary_tag | ||
| 313 | |||
| 314 | Title : primary_tag | ||
| 315 | Usage : $ptag = $featpair->primary_tag | ||
| 316 | Function: get/set on the primary_tag of feature1 | ||
| 317 | Returns : 0,1,2 | ||
| 318 | Args : none if get, the new value if set | ||
| 319 | |||
| 320 | |||
| 321 | =cut | ||
| 322 | |||
| 323 | sub primary_tag{ | ||
| 324 | return shift->feature1->primary_tag(@_); | ||
| 325 | } | ||
| 326 | |||
| 327 | =head2 source_tag | ||
| 328 | |||
| 329 | Title : source_tag | ||
| 330 | Usage : $tag = $feat->source_tag() | ||
| 331 | $feat->source_tag('genscan'); | ||
| 332 | Function: Returns the source tag for a feature, | ||
| 333 | eg, 'genscan' | ||
| 334 | Returns : a string | ||
| 335 | Args : none | ||
| 336 | |||
| 337 | |||
| 338 | =cut | ||
| 339 | |||
| 340 | sub source_tag{ | ||
| 341 | return shift->feature1->source_tag(@_); | ||
| 342 | } | ||
| 343 | |||
| 344 | =head2 seqname | ||
| 345 | |||
| 346 | Title : seqname | ||
| 347 | Usage : $obj->seq_id($newval) | ||
| 348 | Function: There are many cases when you make a feature that you | ||
| 349 | do know the sequence name, but do not know its actual | ||
| 350 | sequence. This is an attribute such that you can store | ||
| 351 | the seqname. | ||
| 352 | |||
| 353 | This attribute should *not* be used in GFF dumping, as | ||
| 354 | that should come from the collection in which the seq | ||
| 355 | feature was found. | ||
| 356 | Returns : value of seqname | ||
| 357 | Args : newvalue (optional) | ||
| 358 | |||
| 359 | |||
| 360 | =cut | ||
| 361 | |||
| 362 | sub seq_id{ | ||
| 363 | return shift->feature1->seq_id(@_); | ||
| 364 | } | ||
| 365 | |||
| 366 | =head2 hseqname | ||
| 367 | |||
| 368 | Title : hseqname | ||
| 369 | Usage : $featpair->hseqname($newval) | ||
| 370 | Function: Get/set method for the name of | ||
| 371 | feature2. | ||
| 372 | Returns : value of $feature2->seq_id | ||
| 373 | Args : newvalue (optional) | ||
| 374 | |||
| 375 | |||
| 376 | =cut | ||
| 377 | |||
| 378 | sub hseq_id { | ||
| 379 | return shift->feature2->seq_id(@_); | ||
| 380 | } | ||
| 381 | |||
| 382 | |||
| 383 | =head2 hstart | ||
| 384 | |||
| 385 | Title : hstart | ||
| 386 | Usage : $start = $featpair->hstart | ||
| 387 | $featpair->hstart(20) | ||
| 388 | Function: Get/set on the start coordinate of feature2 | ||
| 389 | Returns : integer | ||
| 390 | Args : none | ||
| 391 | |||
| 392 | =cut | ||
| 393 | |||
| 394 | sub hstart { | ||
| 395 | return shift->feature2->start(@_); | ||
| 396 | } | ||
| 397 | |||
| 398 | =head2 hend | ||
| 399 | |||
| 400 | Title : hend | ||
| 401 | Usage : $end = $featpair->hend | ||
| 402 | $featpair->hend($end) | ||
| 403 | Function: get/set on the end coordinate of feature2 | ||
| 404 | Returns : integer | ||
| 405 | Args : none | ||
| 406 | |||
| 407 | |||
| 408 | =cut | ||
| 409 | |||
| 410 | sub hend{ | ||
| 411 | return shift->feature2->end(@_); | ||
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | =head2 hstrand | ||
| 416 | |||
| 417 | Title : hstrand | ||
| 418 | Usage : $strand = $feat->strand() | ||
| 419 | $feat->strand($strand) | ||
| 420 | Function: get/set on strand information, being 1,-1 or 0 | ||
| 421 | Returns : -1,1 or 0 | ||
| 422 | Args : none | ||
| 423 | |||
| 424 | |||
| 425 | =cut | ||
| 426 | |||
| 427 | sub hstrand{ | ||
| 428 | return shift->feature2->strand(@_); | ||
| 429 | } | ||
| 430 | |||
| 431 | =head2 hscore | ||
| 432 | |||
| 433 | Title : hscore | ||
| 434 | Usage : $score = $feat->score() | ||
| 435 | $feat->score($score) | ||
| 436 | Function: get/set on score information | ||
| 437 | Returns : float | ||
| 438 | Args : none if get, the new value if set | ||
| 439 | |||
| 440 | |||
| 441 | =cut | ||
| 442 | |||
| 443 | sub hscore { | ||
| 444 | return shift->feature2->score(@_); | ||
| 445 | } | ||
| 446 | |||
| 447 | =head2 hframe | ||
| 448 | |||
| 449 | Title : hframe | ||
| 450 | Usage : $frame = $feat->frame() | ||
| 451 | $feat->frame($frame) | ||
| 452 | Function: get/set on frame information | ||
| 453 | Returns : 0,1,2 | ||
| 454 | Args : none if get, the new value if set | ||
| 455 | |||
| 456 | |||
| 457 | =cut | ||
| 458 | |||
| 459 | sub hframe { | ||
| 460 | return shift->feature2->frame(@_); | ||
| 461 | } | ||
| 462 | |||
| 463 | =head2 hprimary_tag | ||
| 464 | |||
| 465 | Title : hprimary_tag | ||
| 466 | Usage : $ptag = $featpair->hprimary_tag | ||
| 467 | Function: Get/set on the primary_tag of feature2 | ||
| 468 | Returns : 0,1,2 | ||
| 469 | Args : none if get, the new value if set | ||
| 470 | |||
| 471 | |||
| 472 | =cut | ||
| 473 | |||
| 474 | sub hprimary_tag{ | ||
| 475 | return shift->feature2->primary_tag(@_); | ||
| 476 | } | ||
| 477 | |||
| 478 | =head2 hsource_tag | ||
| 479 | |||
| 480 | Title : hsource_tag | ||
| 481 | Usage : $tag = $feat->hsource_tag() | ||
| 482 | $feat->source_tag('genscan'); | ||
| 483 | Function: Returns the source tag for a feature, | ||
| 484 | eg, 'genscan' | ||
| 485 | Returns : a string | ||
| 486 | Args : none | ||
| 487 | |||
| 488 | |||
| 489 | =cut | ||
| 490 | |||
| 491 | sub hsource_tag{ | ||
| 492 | return shift->feature2->source_tag(@_); | ||
| 493 | } | ||
| 494 | |||
| 495 | =head2 invert | ||
| 496 | |||
| 497 | Title : invert | ||
| 498 | Usage : $tag = $feat->invert | ||
| 499 | Function: Swaps feature1 and feature2 around | ||
| 500 | Returns : Nothing | ||
| 501 | Args : none | ||
| 502 | |||
| 503 | |||
| 504 | =cut | ||
| 505 | |||
| 506 | sub invert { | ||
| 507 | my ($self) = @_; | ||
| 508 | |||
| 509 | my $tmp = $self->feature1; | ||
| 510 | |||
| 511 | $self->feature1($self->feature2); | ||
| 512 | $self->feature2($tmp); | ||
| 513 | return; | ||
| 514 | } | ||
| 515 | |||
| 516 | =head2 feature_factory | ||
| 517 | |||
| 518 | Title : feature_factory | ||
| 519 | Usage : $obj->feature_factory($newval) | ||
| 520 | Function: Get/set the feature object factory for this feature pair. | ||
| 521 | |||
| 522 | The feature object factory will be used to create a feature | ||
| 523 | object if feature1() or feature2() is called in get mode | ||
| 524 | without having been set before. | ||
| 525 | |||
| 526 | The default is an instance of FAST::Bio::Factory::ObjectFactory | ||
| 527 | and hence allows the type to be changed dynamically at any | ||
| 528 | time. | ||
| 529 | |||
| 530 | Example : | ||
| 531 | Returns : The feature object factory in use (a | ||
| 532 | FAST::Bio::Factory::ObjectFactoryI compliant object) | ||
| 533 | Args : on set, a FAST::Bio::Factory::ObjectFactoryI compliant object | ||
| 534 | |||
| 535 | |||
| 536 | =cut | ||
| 537 | |||
| 538 | sub feature_factory{ | ||
| 539 | my $self = shift; | ||
| 540 | |||
| 541 | return $self->{'feature_factory'} = shift if @_; | ||
| 542 | return $self->{'feature_factory'}; | ||
| 543 | } | ||
| 544 | |||
| 545 | ################################################################# | ||
| 546 | # aliases for backwards compatibility # | ||
| 547 | ################################################################# | ||
| 548 | |||
| 549 | # seqname() is already aliased in Generic.pm, and we overwrite seq_id | ||
| 550 | |||
| 551 | sub hseqname { | ||
| 552 | my $self = shift; | ||
| 553 | $self->warn("SeqFeatureI::seqname() is deprecated. Please use seq_id() instead."); | ||
| 554 | return $self->hseq_id(@_); | ||
| 555 | } | ||
| 556 | |||
| 557 | sub cleanup_fp { | ||
| 558 | my $self = shift; | ||
| 559 | $self->{'feature1'} = $self->{'feature2'} = undef; | ||
| 560 | } | ||
| 561 | 1; |