| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::Location::Simple | ||
| 3 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 4 | # | ||
| 5 | # Cared for by Heikki Lehvaslaiho <heikki-at-bioperl-dot-org> | ||
| 6 | # | ||
| 7 | # Copyright Heikki Lehvaslaiho | ||
| 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::Simple - Implementation of a Simple Location on a Sequence | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use FAST::Bio::Location::Simple; | ||
| 19 | |||
| 20 | my $location = FAST::Bio::Location::Simple->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 exact location | ||
| 32 | information on a Sequence: '22' or '12..15' or '16^17'. | ||
| 33 | |||
| 34 | You can test the type of the location using length() function () or | ||
| 35 | directly location_type() which can one of two values: 'EXACT' or | ||
| 36 | 'IN-BETWEEN'. | ||
| 37 | |||
| 38 | |||
| 39 | =head1 FEEDBACK | ||
| 40 | |||
| 41 | User feedback is an integral part of the evolution of this and other | ||
| 42 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 43 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 44 | |||
| 45 | bioperl-l@bioperl.org - General discussion | ||
| 46 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 47 | |||
| 48 | =head2 Support | ||
| 49 | |||
| 50 | Please direct usage questions or support issues to the mailing list: | ||
| 51 | |||
| 52 | I<bioperl-l@bioperl.org> | ||
| 53 | |||
| 54 | rather than to the module maintainer directly. Many experienced and | ||
| 55 | reponsive experts will be able look at the problem and quickly | ||
| 56 | address it. Please include a thorough description of the problem | ||
| 57 | with code and data examples if at all possible. | ||
| 58 | |||
| 59 | =head2 Reporting Bugs | ||
| 60 | |||
| 61 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 62 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 63 | web: | ||
| 64 | |||
| 65 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 66 | |||
| 67 | =head1 AUTHOR - Heikki Lehvaslaiho | ||
| 68 | |||
| 69 | Email heikki-at-bioperl-dot-org | ||
| 70 | |||
| 71 | =head1 APPENDIX | ||
| 72 | |||
| 73 | The rest of the documentation details each of the object | ||
| 74 | methods. Internal methods are usually preceded with a _ | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | # Let the code begin... | ||
| 79 | |||
| 80 | |||
| 81 | package FAST::Bio::Location::Simple; | ||
| 82 | use strict; | ||
| 83 | |||
| 84 | use base qw(FAST::Bio::Location::Atomic); | ||
| 85 | |||
| 86 | our %RANGEENCODE = ('\.\.' => 'EXACT', | ||
| 87 | '\^' => 'IN-BETWEEN' ); | ||
| 88 | |||
| 89 | our %RANGEDECODE = ('EXACT' => '..', | ||
| 90 | 'IN-BETWEEN' => '^' ); | ||
| 91 | |||
| 92 | sub new { | ||
| 93 | my ($class, @args) = @_; | ||
| 94 | my $self = $class->SUPER::new(@args); | ||
| 95 | |||
| 96 | my ($locationtype) = $self->_rearrange([qw(LOCATION_TYPE)],@args); | ||
| 97 | |||
| 98 | $locationtype && $self->location_type($locationtype); | ||
| 99 | |||
| 100 | return $self; | ||
| 101 | } | ||
| 102 | |||
| 103 | =head2 start | ||
| 104 | |||
| 105 | Title : start | ||
| 106 | Usage : $start = $loc->start(); | ||
| 107 | Function: get/set the start of this range | ||
| 108 | Returns : the start of this range | ||
| 109 | Args : optionaly allows the start to be set | ||
| 110 | : using $loc->start($start) | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub start { | ||
| 115 | my ($self, $value) = @_; | ||
| 116 | $self->{'_start'} = $value if defined $value ; | ||
| 117 | |||
| 118 | $self->throw("Only adjacent residues when location type ". | ||
| 119 | "is IN-BETWEEN. Not [". $self->{'_start'}. "] and [". | ||
| 120 | $self->{'_end'}. "]" ) | ||
| 121 | if defined $self->{'_start'} && defined $self->{'_end'} && | ||
| 122 | $self->location_type eq 'IN-BETWEEN' && | ||
| 123 | ($self->{'_end'} - 1 != $self->{'_start'}); | ||
| 124 | return $self->{'_start'}; | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | =head2 end | ||
| 129 | |||
| 130 | Title : end | ||
| 131 | Usage : $end = $loc->end(); | ||
| 132 | Function: get/set the end of this range | ||
| 133 | Returns : the end of this range | ||
| 134 | Args : optionaly allows the end to be set | ||
| 135 | : using $loc->end($start) | ||
| 136 | Note : If start is set but end is undefined, this now assumes that start | ||
| 137 | is the same as end but throws a warning (i.e. it assumes this is | ||
| 138 | a possible error). If start is undefined, this now throws an | ||
| 139 | exception. | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub end { | ||
| 144 | my ($self, $value) = @_; | ||
| 145 | |||
| 146 | $self->{'_end'} = $value if defined $value ; | ||
| 147 | |||
| 148 | #assume end is the same as start if not defined | ||
| 149 | if (!defined $self->{'_end'}) { | ||
| 150 | if (!defined $self->{'_start'}) { | ||
| 151 | $self->warn('Can not set FAST::Bio::Location::Simple::end() equal to start; start not set'); | ||
| 152 | return; | ||
| 153 | } | ||
| 154 | $self->warn('Setting end to equal start['. $self->{'_start'}. ']'); | ||
| 155 | $self->{'_end'} = $self->{'_start'}; | ||
| 156 | } | ||
| 157 | $self->throw("Only adjacent residues when location type ". | ||
| 158 | "is IN-BETWEEN. Not [". $self->{'_start'}. "] and [". | ||
| 159 | $self->{'_end'}. "]" ) | ||
| 160 | if defined $self->{'_start'} && defined $self->{'_end'} && | ||
| 161 | $self->location_type eq 'IN-BETWEEN' && | ||
| 162 | ($self->{'_end'} - 1 != $self->{'_start'}); | ||
| 163 | |||
| 164 | return $self->{'_end'}; | ||
| 165 | } | ||
| 166 | |||
| 167 | =head2 strand | ||
| 168 | |||
| 169 | Title : strand | ||
| 170 | Usage : $strand = $loc->strand(); | ||
| 171 | Function: get/set the strand of this range | ||
| 172 | Returns : the strandedness (-1, 0, +1) | ||
| 173 | Args : optionaly allows the strand to be set | ||
| 174 | : using $loc->strand($strand) | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | =head2 length | ||
| 179 | |||
| 180 | Title : length | ||
| 181 | Usage : $len = $loc->length(); | ||
| 182 | Function: get the length in the coordinate space this location spans | ||
| 183 | Example : | ||
| 184 | Returns : an integer | ||
| 185 | Args : none | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | sub length { | ||
| 190 | my ($self) = @_; | ||
| 191 | if ($self->location_type eq 'IN-BETWEEN' ) { | ||
| 192 | return 0; | ||
| 193 | } else { | ||
| 194 | return abs($self->end - $self->start) + 1; | ||
| 195 | } | ||
| 196 | |||
| 197 | } | ||
| 198 | |||
| 199 | =head2 min_start | ||
| 200 | |||
| 201 | Title : min_start | ||
| 202 | Usage : my $minstart = $location->min_start(); | ||
| 203 | Function: Get minimum starting location of feature startpoint | ||
| 204 | Returns : integer or undef if no minimum starting point. | ||
| 205 | Args : none | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | =head2 max_start | ||
| 210 | |||
| 211 | Title : max_start | ||
| 212 | Usage : my $maxstart = $location->max_start(); | ||
| 213 | Function: Get maximum starting location of feature startpoint. | ||
| 214 | |||
| 215 | In this implementation this is exactly the same as min_start(). | ||
| 216 | |||
| 217 | Returns : integer or undef if no maximum starting point. | ||
| 218 | Args : none | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | =head2 start_pos_type | ||
| 223 | |||
| 224 | Title : start_pos_type | ||
| 225 | Usage : my $start_pos_type = $location->start_pos_type(); | ||
| 226 | Function: Get start position type (ie <,>, ^). | ||
| 227 | |||
| 228 | Returns : type of position coded as text | ||
| 229 | ('BEFORE', 'AFTER', 'EXACT','WITHIN', 'BETWEEN') | ||
| 230 | Args : none | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | =head2 min_end | ||
| 235 | |||
| 236 | Title : min_end | ||
| 237 | Usage : my $minend = $location->min_end(); | ||
| 238 | Function: Get minimum ending location of feature endpoint | ||
| 239 | Returns : integer or undef if no minimum ending point. | ||
| 240 | Args : none | ||
| 241 | |||
| 242 | =cut | ||
| 243 | |||
| 244 | |||
| 245 | =head2 max_end | ||
| 246 | |||
| 247 | Title : max_end | ||
| 248 | Usage : my $maxend = $location->max_end(); | ||
| 249 | Function: Get maximum ending location of feature endpoint | ||
| 250 | |||
| 251 | In this implementation this is exactly the same as min_end(). | ||
| 252 | |||
| 253 | Returns : integer or undef if no maximum ending point. | ||
| 254 | Args : none | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | =head2 end_pos_type | ||
| 259 | |||
| 260 | Title : end_pos_type | ||
| 261 | Usage : my $end_pos_type = $location->end_pos_type(); | ||
| 262 | Function: Get end position type (ie <,>, ^) | ||
| 263 | |||
| 264 | Returns : type of position coded as text | ||
| 265 | ('BEFORE', 'AFTER', 'EXACT','WITHIN', 'BETWEEN') | ||
| 266 | Args : none | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | =head2 location_type | ||
| 271 | |||
| 272 | Title : location_type | ||
| 273 | Usage : my $location_type = $location->location_type(); | ||
| 274 | Function: Get location type encoded as text | ||
| 275 | Returns : string ('EXACT' or 'IN-BETWEEN') | ||
| 276 | Args : 'EXACT' or '..' or 'IN-BETWEEN' or '^' | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | sub location_type { | ||
| 281 | my ($self, $value) = @_; | ||
| 282 | |||
| 283 | if( defined $value || ! defined $self->{'_location_type'} ) { | ||
| 284 | $value = 'EXACT' unless defined $value; | ||
| 285 | $value = uc $value; | ||
| 286 | if (! defined $RANGEDECODE{$value}) { | ||
| 287 | $value = '\^' if $value eq '^'; | ||
| 288 | $value = '\.\.' if $value eq '..'; | ||
| 289 | $value = $RANGEENCODE{$value}; | ||
| 290 | } | ||
| 291 | $self->throw("Did not specify a valid location type. [$value] is no good") | ||
| 292 | unless defined $value; | ||
| 293 | $self->{'_location_type'} = $value; | ||
| 294 | } | ||
| 295 | $self->throw("Only adjacent residues when location type ". | ||
| 296 | "is IN-BETWEEN. Not [". $self->{'_start'}. "] and [". | ||
| 297 | $self->{'_end'}. "]" ) | ||
| 298 | if $self->{'_location_type'} eq 'IN-BETWEEN' && | ||
| 299 | defined $self->{'_start'} && | ||
| 300 | defined $self->{'_end'} && | ||
| 301 | ($self->{'_end'} - 1 != $self->{'_start'}); | ||
| 302 | |||
| 303 | return $self->{'_location_type'}; | ||
| 304 | } | ||
| 305 | |||
| 306 | =head2 is_remote | ||
| 307 | |||
| 308 | Title : is_remote | ||
| 309 | Usage : $is_remote_loc = $loc->is_remote() | ||
| 310 | Function: Whether or not a location is a remote location. | ||
| 311 | |||
| 312 | A location is said to be remote if it is on a different | ||
| 313 | 'object' than the object which 'has' this | ||
| 314 | location. Typically, features on a sequence will sometimes | ||
| 315 | have a remote location, which means that the location of | ||
| 316 | the feature is on a different sequence than the one that is | ||
| 317 | attached to the feature. In such a case, $loc->seq_id will | ||
| 318 | be different from $feat->seq_id (usually they will be the | ||
| 319 | same). | ||
| 320 | |||
| 321 | While this may sound weird, it reflects the location of the | ||
| 322 | kind of AL445212.9:83662..166657 which can be found in GenBank/EMBL | ||
| 323 | feature tables. | ||
| 324 | |||
| 325 | Example : | ||
| 326 | Returns : TRUE if the location is a remote location, and FALSE otherwise | ||
| 327 | Args : Value to set to | ||
| 328 | |||
| 329 | =cut | ||
| 330 | |||
| 331 | =head2 to_FTstring | ||
| 332 | |||
| 333 | Title : to_FTstring | ||
| 334 | Usage : my $locstr = $location->to_FTstring() | ||
| 335 | Function: returns the FeatureTable string of this location | ||
| 336 | Returns : string | ||
| 337 | Args : none | ||
| 338 | |||
| 339 | =cut | ||
| 340 | |||
| 341 | sub to_FTstring { | ||
| 342 | my($self) = @_; | ||
| 343 | |||
| 344 | my $str; | ||
| 345 | if( $self->start == $self->end ) { | ||
| 346 | $str = $self->start; | ||
| 347 | } else { | ||
| 348 | $str = $self->start . $RANGEDECODE{$self->location_type} . $self->end; | ||
| 349 | } | ||
| 350 | if($self->is_remote() && $self->seq_id()) { | ||
| 351 | $str = $self->seq_id() . ":" . $str; | ||
| 352 | } | ||
| 353 | if( defined $self->strand && | ||
| 354 | $self->strand == -1 ) { | ||
| 355 | $str = "complement(".$str.")"; | ||
| 356 | } | ||
| 357 | return $str; | ||
| 358 | } | ||
| 359 | |||
| 360 | =head2 valid_Location | ||
| 361 | |||
| 362 | Title : valid_Location | ||
| 363 | Usage : if ($location->valid_location) {...}; | ||
| 364 | Function: boolean method to determine whether location is considered valid | ||
| 365 | (has minimum requirements for Simple implementation) | ||
| 366 | Returns : Boolean value: true if location is valid, false otherwise | ||
| 367 | Args : none | ||
| 368 | |||
| 369 | =cut | ||
| 370 | |||
| 371 | # comments, not function added by jason | ||
| 372 | # | ||
| 373 | # trunc is untested, and as of now unannounced method for truncating a | ||
| 374 | # location. This is to eventually be part of the procedure to | ||
| 375 | # truncate a sequence with annotatioin and properly remap the location | ||
| 376 | # of all the features contained within the truncated segment. | ||
| 377 | |||
| 378 | # presumably this might do things a little differently for the case | ||
| 379 | # where the truncation splits the location in half | ||
| 380 | # | ||
| 381 | # in short- you probably don't want to use this method. | ||
| 382 | |||
| 383 | sub trunc { | ||
| 384 | my ($self,$start,$end,$relative_ori) = @_; | ||
| 385 | my $newstart = $self->start - $start+1; | ||
| 386 | my $newend = $self->end - $start+1; | ||
| 387 | my $newstrand = $relative_ori * $self->strand; | ||
| 388 | |||
| 389 | my $out; | ||
| 390 | if( $newstart < 1 || $newend > ($end-$start+1) ) { | ||
| 391 | $out = FAST::Bio::Location::Simple->new(); | ||
| 392 | $out->start($self->start); | ||
| 393 | $out->end($self->end); | ||
| 394 | $out->strand($self->strand); | ||
| 395 | $out->seq_id($self->seqid); | ||
| 396 | $out->is_remote(1); | ||
| 397 | } else { | ||
| 398 | $out = FAST::Bio::Location::Simple->new(); | ||
| 399 | $out->start($newstart); | ||
| 400 | $out->end($newend); | ||
| 401 | $out->strand($newstrand); | ||
| 402 | $out->seq_id(); | ||
| 403 | } | ||
| 404 | |||
| 405 | return $out; | ||
| 406 | } | ||
| 407 | |||
| 408 | 1; | ||
| 409 |