| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::Location::Atomic | ||
| 3 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 4 | # | ||
| 5 | # Cared for by Jason Stajich <jason@bioperl.org> | ||
| 6 | # | ||
| 7 | # Copyright Jason Stajich | ||
| 8 | # | ||
| 9 | # You may distribute this module under the same terms as perl itself | ||
| 10 | # POD documentation - main docs before the code | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | FAST::Bio::Location::Atomic - Implementation of a Atomic Location on a Sequence | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use FAST::Bio::Location::Atomic; | ||
| 19 | |||
| 20 | my $location = FAST::Bio::Location::Atomic->new(-start => 1, -end => 100, | ||
| 21 | -strand => 1 ); | ||
| 22 | |||
| 23 | if( $location->strand == -1 ) { | ||
| 24 | printf "complement(%d..%d)\n", $location->start, $location->end; | ||
| 25 | } else { | ||
| 26 | printf "%d..%d\n", $location->start, $location->end; | ||
| 27 | } | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | This is an implementation of FAST::Bio::LocationI to manage simple location | ||
| 32 | information on a Sequence. | ||
| 33 | |||
| 34 | =head1 FEEDBACK | ||
| 35 | |||
| 36 | User feedback is an integral part of the evolution of this and other | ||
| 37 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 38 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 39 | |||
| 40 | bioperl-l@bioperl.org - General discussion | ||
| 41 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 42 | |||
| 43 | =head2 Support | ||
| 44 | |||
| 45 | Please direct usage questions or support issues to the mailing list: | ||
| 46 | |||
| 47 | I<bioperl-l@bioperl.org> | ||
| 48 | |||
| 49 | rather than to the module maintainer directly. Many experienced and | ||
| 50 | reponsive experts will be able look at the problem and quickly | ||
| 51 | address it. Please include a thorough description of the problem | ||
| 52 | with code and data examples if at all possible. | ||
| 53 | |||
| 54 | =head2 Reporting Bugs | ||
| 55 | |||
| 56 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 57 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 58 | web: | ||
| 59 | |||
| 60 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 61 | |||
| 62 | =head1 AUTHOR - Jason Stajich | ||
| 63 | |||
| 64 | Email jason-at-bioperl-dot-org | ||
| 65 | |||
| 66 | =head1 APPENDIX | ||
| 67 | |||
| 68 | The rest of the documentation details each of the object | ||
| 69 | methods. Internal methods are usually preceded with a _ | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | # Let the code begin... | ||
| 74 | |||
| 75 | |||
| 76 | package FAST::Bio::Location::Atomic; | ||
| 77 | use strict; | ||
| 78 | |||
| 79 | use FAST::Bio::Location::WidestCoordPolicy; | ||
| 80 | |||
| 81 | use base qw(FAST::Bio::Root::Root FAST::Bio::LocationI); | ||
| 82 | |||
| 83 | our $coord_policy = FAST::Bio::Location::WidestCoordPolicy->new(); | ||
| 84 | |||
| 85 | sub new { | ||
| 86 | my ($class, @args) = @_; | ||
| 87 | $class = ref $class || $class; | ||
| 88 | my $self = {}; | ||
| 89 | # This is for the case when we've done something like this | ||
| 90 | # get a 2 features from somewhere (like FAST::Bio::Tools::GFF) | ||
| 91 | # Do | ||
| 92 | # my $location = $f1->location->union($f2->location); | ||
| 93 | # We get an error without the following code which | ||
| 94 | # explictly loads the FAST::Bio::Location::Simple class | ||
| 95 | unless( $class->can('start') ) { | ||
| 96 | eval { FAST::Bio::Root::Root->_load_module($class) }; | ||
| 97 | if ( $@ ) { | ||
| 98 | FAST::Bio::Root::Root->throw("$class cannot be found\nException $@"); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | bless $self,$class; | ||
| 102 | |||
| 103 | my ($v,$start,$end,$strand,$seqid) = $self->_rearrange([qw(VERBOSE | ||
| 104 | START | ||
| 105 | END | ||
| 106 | STRAND | ||
| 107 | SEQ_ID)],@args); | ||
| 108 | defined $v && $self->verbose($v); | ||
| 109 | defined $strand && $self->strand($strand); | ||
| 110 | |||
| 111 | defined $start && $self->start($start); | ||
| 112 | defined $end && $self->end($end); | ||
| 113 | if( defined $self->start && defined $self->end && | ||
| 114 | $self->start > $self->end && $self->strand != -1 ) { | ||
| 115 | $self->warn("When building a location, start ($start) is expected to be less than end ($end), ". | ||
| 116 | "however it was not. Switching start and end and setting strand to -1"); | ||
| 117 | |||
| 118 | $self->strand(-1); | ||
| 119 | my $e = $self->end; | ||
| 120 | my $s = $self->start; | ||
| 121 | $self->start($e); | ||
| 122 | $self->end($s); | ||
| 123 | } | ||
| 124 | $seqid && $self->seq_id($seqid); | ||
| 125 | |||
| 126 | return $self; | ||
| 127 | } | ||
| 128 | |||
| 129 | =head2 start | ||
| 130 | |||
| 131 | Title : start | ||
| 132 | Usage : $start = $loc->start(); | ||
| 133 | Function: get/set the start of this range | ||
| 134 | Returns : the start of this range | ||
| 135 | Args : optionaly allows the start to be set | ||
| 136 | : using $loc->start($start) | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | sub start { | ||
| 141 | my ($self, $value) = @_; | ||
| 142 | $self->min_start($value) if( defined $value ); | ||
| 143 | return $self->SUPER::start(); | ||
| 144 | } | ||
| 145 | |||
| 146 | =head2 end | ||
| 147 | |||
| 148 | Title : end | ||
| 149 | Usage : $end = $loc->end(); | ||
| 150 | Function: get/set the end of this range | ||
| 151 | Returns : the end of this range | ||
| 152 | Args : optionaly allows the end to be set | ||
| 153 | : using $loc->end($start) | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | sub end { | ||
| 158 | my ($self, $value) = @_; | ||
| 159 | |||
| 160 | $self->min_end($value) if( defined $value ); | ||
| 161 | return $self->SUPER::end(); | ||
| 162 | } | ||
| 163 | |||
| 164 | =head2 strand | ||
| 165 | |||
| 166 | Title : strand | ||
| 167 | Usage : $strand = $loc->strand(); | ||
| 168 | Function: get/set the strand of this range | ||
| 169 | Returns : the strandidness (-1, 0, +1) | ||
| 170 | Args : optionaly allows the strand to be set | ||
| 171 | : using $loc->strand($strand) | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | sub strand { | ||
| 176 | my $self = shift; | ||
| 177 | |||
| 178 | if ( @_ ) { | ||
| 179 | my $value = shift; | ||
| 180 | if ( defined($value) ) { | ||
| 181 | if ( $value eq '+' ) { $value = 1; } | ||
| 182 | elsif ( $value eq '-' ) { $value = -1; } | ||
| 183 | elsif ( $value eq '.' ) { $value = 0; } | ||
| 184 | elsif ( $value != -1 && $value != 1 && $value != 0 ) { | ||
| 185 | $self->throw("$value is not a valid strand info"); | ||
| 186 | } | ||
| 187 | $self->{'_strand'} = $value; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | # do not pretend the strand has been set if in fact it wasn't | ||
| 191 | return $self->{'_strand'}; | ||
| 192 | #return $self->{'_strand'} || 0; | ||
| 193 | } | ||
| 194 | |||
| 195 | =head2 flip_strand | ||
| 196 | |||
| 197 | Title : flip_strand | ||
| 198 | Usage : $location->flip_strand(); | ||
| 199 | Function: Flip-flop a strand to the opposite | ||
| 200 | Returns : None | ||
| 201 | Args : None | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | |||
| 206 | sub flip_strand { | ||
| 207 | my $self= shift; | ||
| 208 | $self->strand($self->strand * -1); | ||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | =head2 seq_id | ||
| 213 | |||
| 214 | Title : seq_id | ||
| 215 | Usage : my $seqid = $location->seq_id(); | ||
| 216 | Function: Get/Set seq_id that location refers to | ||
| 217 | Returns : seq_id (a string) | ||
| 218 | Args : [optional] seq_id value to set | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | |||
| 223 | sub seq_id { | ||
| 224 | my ($self, $seqid) = @_; | ||
| 225 | if( defined $seqid ) { | ||
| 226 | $self->{'_seqid'} = $seqid; | ||
| 227 | } | ||
| 228 | return $self->{'_seqid'}; | ||
| 229 | } | ||
| 230 | |||
| 231 | =head2 length | ||
| 232 | |||
| 233 | Title : length | ||
| 234 | Usage : $len = $loc->length(); | ||
| 235 | Function: get the length in the coordinate space this location spans | ||
| 236 | Example : | ||
| 237 | Returns : an integer | ||
| 238 | Args : none | ||
| 239 | |||
| 240 | |||
| 241 | =cut | ||
| 242 | |||
| 243 | sub length { | ||
| 244 | my ($self) = @_; | ||
| 245 | return abs($self->end() - $self->start()) + 1; | ||
| 246 | } | ||
| 247 | |||
| 248 | =head2 min_start | ||
| 249 | |||
| 250 | Title : min_start | ||
| 251 | Usage : my $minstart = $location->min_start(); | ||
| 252 | Function: Get minimum starting location of feature startpoint | ||
| 253 | Returns : integer or undef if no minimum starting point. | ||
| 254 | Args : none | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | sub min_start { | ||
| 259 | my ($self,$value) = @_; | ||
| 260 | |||
| 261 | if(defined($value)) { | ||
| 262 | $self->{'_start'} = $value; | ||
| 263 | } | ||
| 264 | return $self->{'_start'}; | ||
| 265 | } | ||
| 266 | |||
| 267 | =head2 max_start | ||
| 268 | |||
| 269 | Title : max_start | ||
| 270 | Usage : my $maxstart = $location->max_start(); | ||
| 271 | Function: Get maximum starting location of feature startpoint. | ||
| 272 | |||
| 273 | In this implementation this is exactly the same as min_start(). | ||
| 274 | |||
| 275 | Returns : integer or undef if no maximum starting point. | ||
| 276 | Args : none | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | sub max_start { | ||
| 281 | my ($self,@args) = @_; | ||
| 282 | return $self->min_start(@args); | ||
| 283 | } | ||
| 284 | |||
| 285 | =head2 start_pos_type | ||
| 286 | |||
| 287 | Title : start_pos_type | ||
| 288 | Usage : my $start_pos_type = $location->start_pos_type(); | ||
| 289 | Function: Get start position type (ie <,>, ^). | ||
| 290 | |||
| 291 | In this implementation this will always be 'EXACT'. | ||
| 292 | |||
| 293 | Returns : type of position coded as text | ||
| 294 | ('BEFORE', 'AFTER', 'EXACT','WITHIN', 'BETWEEN') | ||
| 295 | Args : none | ||
| 296 | |||
| 297 | =cut | ||
| 298 | |||
| 299 | sub start_pos_type { | ||
| 300 | my($self) = @_; | ||
| 301 | return 'EXACT'; | ||
| 302 | } | ||
| 303 | |||
| 304 | =head2 min_end | ||
| 305 | |||
| 306 | Title : min_end | ||
| 307 | Usage : my $minend = $location->min_end(); | ||
| 308 | Function: Get minimum ending location of feature endpoint | ||
| 309 | Returns : integer or undef if no minimum ending point. | ||
| 310 | Args : none | ||
| 311 | |||
| 312 | =cut | ||
| 313 | |||
| 314 | sub min_end { | ||
| 315 | my($self,$value) = @_; | ||
| 316 | |||
| 317 | if(defined($value)) { | ||
| 318 | $self->{'_end'} = $value; | ||
| 319 | } | ||
| 320 | return $self->{'_end'}; | ||
| 321 | } | ||
| 322 | |||
| 323 | =head2 max_end | ||
| 324 | |||
| 325 | Title : max_end | ||
| 326 | Usage : my $maxend = $location->max_end(); | ||
| 327 | Function: Get maximum ending location of feature endpoint | ||
| 328 | |||
| 329 | In this implementation this is exactly the same as min_end(). | ||
| 330 | |||
| 331 | Returns : integer or undef if no maximum ending point. | ||
| 332 | Args : none | ||
| 333 | |||
| 334 | =cut | ||
| 335 | |||
| 336 | sub max_end { | ||
| 337 | my($self,@args) = @_; | ||
| 338 | return $self->min_end(@args); | ||
| 339 | } | ||
| 340 | |||
| 341 | =head2 end_pos_type | ||
| 342 | |||
| 343 | Title : end_pos_type | ||
| 344 | Usage : my $end_pos_type = $location->end_pos_type(); | ||
| 345 | Function: Get end position type (ie <,>, ^) | ||
| 346 | |||
| 347 | In this implementation this will always be 'EXACT'. | ||
| 348 | |||
| 349 | Returns : type of position coded as text | ||
| 350 | ('BEFORE', 'AFTER', 'EXACT','WITHIN', 'BETWEEN') | ||
| 351 | Args : none | ||
| 352 | |||
| 353 | =cut | ||
| 354 | |||
| 355 | sub end_pos_type { | ||
| 356 | my($self) = @_; | ||
| 357 | return 'EXACT'; | ||
| 358 | } | ||
| 359 | |||
| 360 | =head2 location_type | ||
| 361 | |||
| 362 | Title : location_type | ||
| 363 | Usage : my $location_type = $location->location_type(); | ||
| 364 | Function: Get location type encoded as text | ||
| 365 | Returns : string ('EXACT', 'WITHIN', 'IN-BETWEEN') | ||
| 366 | Args : none | ||
| 367 | |||
| 368 | =cut | ||
| 369 | |||
| 370 | sub location_type { | ||
| 371 | my ($self) = @_; | ||
| 372 | return 'EXACT'; | ||
| 373 | } | ||
| 374 | |||
| 375 | =head2 is_remote | ||
| 376 | |||
| 377 | Title : is_remote | ||
| 378 | Usage : $is_remote_loc = $loc->is_remote() | ||
| 379 | Function: Whether or not a location is a remote location. | ||
| 380 | |||
| 381 | A location is said to be remote if it is on a different | ||
| 382 | 'object' than the object which 'has' this | ||
| 383 | location. Typically, features on a sequence will sometimes | ||
| 384 | have a remote location, which means that the location of | ||
| 385 | the feature is on a different sequence than the one that is | ||
| 386 | attached to the feature. In such a case, $loc->seq_id will | ||
| 387 | be different from $feat->seq_id (usually they will be the | ||
| 388 | same). | ||
| 389 | |||
| 390 | While this may sound weird, it reflects the location of the | ||
| 391 | kind of AL445212.9:83662..166657 which can be found in GenBank/EMBL | ||
| 392 | feature tables. | ||
| 393 | |||
| 394 | Example : | ||
| 395 | Returns : TRUE if the location is a remote location, and FALSE otherwise | ||
| 396 | Args : Value to set to | ||
| 397 | |||
| 398 | =cut | ||
| 399 | |||
| 400 | sub is_remote { | ||
| 401 | my $self = shift; | ||
| 402 | if( @_ ) { | ||
| 403 | my $value = shift; | ||
| 404 | $self->{'is_remote'} = $value; | ||
| 405 | } | ||
| 406 | return $self->{'is_remote'}; | ||
| 407 | } | ||
| 408 | |||
| 409 | =head2 each_Location | ||
| 410 | |||
| 411 | Title : each_Location | ||
| 412 | Usage : @locations = $locObject->each_Location($order); | ||
| 413 | Function: Conserved function call across Location:: modules - will | ||
| 414 | return an array containing the component Location(s) in | ||
| 415 | that object, regardless if the calling object is itself a | ||
| 416 | single location or one containing sublocations. | ||
| 417 | Returns : an array of FAST::Bio::LocationI implementing objects - for | ||
| 418 | Simple locations, the return value is just itself. | ||
| 419 | Args : | ||
| 420 | |||
| 421 | =cut | ||
| 422 | |||
| 423 | sub each_Location { | ||
| 424 | my ($self) = @_; | ||
| 425 | return ($self); | ||
| 426 | } | ||
| 427 | |||
| 428 | =head2 to_FTstring | ||
| 429 | |||
| 430 | Title : to_FTstring | ||
| 431 | Usage : my $locstr = $location->to_FTstring() | ||
| 432 | Function: returns the FeatureTable string of this location | ||
| 433 | Returns : string | ||
| 434 | Args : none | ||
| 435 | |||
| 436 | =cut | ||
| 437 | |||
| 438 | sub to_FTstring { | ||
| 439 | my($self) = @_; | ||
| 440 | if( $self->start == $self->end ) { | ||
| 441 | return $self->start; | ||
| 442 | } | ||
| 443 | my $str = $self->start . ".." . $self->end; | ||
| 444 | if( $self->strand == -1 ) { | ||
| 445 | $str = sprintf("complement(%s)", $str); | ||
| 446 | } | ||
| 447 | return $str; | ||
| 448 | } | ||
| 449 | |||
| 450 | =head2 valid_Location | ||
| 451 | |||
| 452 | Title : valid_Location | ||
| 453 | Usage : if ($location->valid_location) {...}; | ||
| 454 | Function: boolean method to determine whether location is considered valid | ||
| 455 | (has minimum requirements for Simple implementation) | ||
| 456 | Returns : Boolean value: true if location is valid, false otherwise | ||
| 457 | Args : none | ||
| 458 | |||
| 459 | =cut | ||
| 460 | |||
| 461 | sub valid_Location { | ||
| 462 | my ($self) = @_; | ||
| 463 | return 1 if $self->{'_start'} && $self->{'_end'}; | ||
| 464 | return 0; | ||
| 465 | } | ||
| 466 | |||
| 467 | =head2 coordinate_policy | ||
| 468 | |||
| 469 | Title : coordinate_policy | ||
| 470 | Usage : $policy = $location->coordinate_policy(); | ||
| 471 | $location->coordinate_policy($mypolicy); # set may not be possible | ||
| 472 | Function: Get the coordinate computing policy employed by this object. | ||
| 473 | |||
| 474 | See L<FAST::Bio::Location::CoordinatePolicyI> for documentation | ||
| 475 | about the policy object and its use. | ||
| 476 | |||
| 477 | The interface *does not* require implementing classes to | ||
| 478 | accept setting of a different policy. The implementation | ||
| 479 | provided here does, however, allow to do so. | ||
| 480 | |||
| 481 | Implementors of this interface are expected to initialize | ||
| 482 | every new instance with a | ||
| 483 | L<FAST::Bio::Location::CoordinatePolicyI> object. The | ||
| 484 | implementation provided here will return a default policy | ||
| 485 | object if none has been set yet. To change this default | ||
| 486 | policy object call this method as a class method with an | ||
| 487 | appropriate argument. Note that in this case only | ||
| 488 | subsequently created Location objects will be affected. | ||
| 489 | |||
| 490 | Returns : A L<FAST::Bio::Location::CoordinatePolicyI> implementing object. | ||
| 491 | Args : On set, a L<FAST::Bio::Location::CoordinatePolicyI> implementing object. | ||
| 492 | |||
| 493 | See L<FAST::Bio::Location::CoordinatePolicyI> for more information | ||
| 494 | |||
| 495 | |||
| 496 | =cut | ||
| 497 | |||
| 498 | sub coordinate_policy { | ||
| 499 | my ($self, $policy) = @_; | ||
| 500 | |||
| 501 | if(defined($policy)) { | ||
| 502 | if(! $policy->isa('FAST::Bio::Location::CoordinatePolicyI')) { | ||
| 503 | $self->throw("Object of class ".ref($policy)." does not implement". | ||
| 504 | " FAST::Bio::Location::CoordinatePolicyI"); | ||
| 505 | } | ||
| 506 | if(ref($self)) { | ||
| 507 | $self->{'_coordpolicy'} = $policy; | ||
| 508 | } else { | ||
| 509 | # called as class method | ||
| 510 | $coord_policy = $policy; | ||
| 511 | } | ||
| 512 | } | ||
| 513 | return (ref($self) && exists($self->{'_coordpolicy'}) ? | ||
| 514 | $self->{'_coordpolicy'} : $coord_policy); | ||
| 515 | } | ||
| 516 | |||
| 517 | =head2 trunc | ||
| 518 | |||
| 519 | Title : trunc | ||
| 520 | Usage : $trunc_location = $location->trunc($start, $end, $relative_ori); | ||
| 521 | Function: To truncate a location and keep annotations and features | ||
| 522 | within the truncated segment intact. | ||
| 523 | |||
| 524 | This might do things differently where the truncation | ||
| 525 | splits the location in half. | ||
| 526 | CAVEAT : As yet, this is an untested and unannounced method. Use | ||
| 527 | with caution! | ||
| 528 | Returns : A L<FAST::Bio::Location::Atomic> object. | ||
| 529 | Args : The start and end position for the trunction, and the relative | ||
| 530 | orientation. | ||
| 531 | |||
| 532 | =cut | ||
| 533 | |||
| 534 | sub trunc { | ||
| 535 | my ($self,$start,$end,$relative_ori) = @_; | ||
| 536 | |||
| 537 | my $newstart = $self->start - $start+1; | ||
| 538 | my $newend = $self->end - $start+1; | ||
| 539 | my $newstrand = $relative_ori * $self->strand; | ||
| 540 | |||
| 541 | my $out; | ||
| 542 | if( $newstart < 1 || $newend > ($end-$start+1) ) { | ||
| 543 | $out = FAST::Bio::Location::Atomic->new(); | ||
| 544 | $out->start($self->start); | ||
| 545 | $out->end($self->end); | ||
| 546 | $out->strand($self->strand); | ||
| 547 | $out->seq_id($self->seqid); | ||
| 548 | $out->is_remote(1); | ||
| 549 | } else { | ||
| 550 | $out = FAST::Bio::Location::Atomic->new(); | ||
| 551 | $out->start($newstart); | ||
| 552 | $out->end($newend); | ||
| 553 | $out->strand($newstrand); | ||
| 554 | $out->seq_id(); | ||
| 555 | } | ||
| 556 | |||
| 557 | return $out; | ||
| 558 | } | ||
| 559 | |||
| 560 | 1; | ||
| 561 |