Feature.pm 33.7 KB
Newer Older
1
# Ensembl module for Bio::EnsEMBL::Feature
2
#
3
# Copyright (c) 2003 Ensembl
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#


=head1 NAME

Bio::EnsEMBL::Feature - Ensembl specific sequence feature.

=head1 SYNOPSIS

    my $feat = new Bio::EnsEMBL::Feature(-start   => 100,
                                       -end     => 220,
                                       -strand  => -1,
                                       -slice   => $slice
                                       -analysis => $analysis
                                      );

    my $start  = $feat->start;
    my $end    = $feat->end;
    my $strand = $feat->strand;

    #move the feature to the chromosomal coordinate system
    $feature = $feature->transform('chromosome');

    #move the feature to a different slice (possibly on another coord system)
    $feature = $feature->transfer($new_slice);

    #project the feature onto another coordinate system possibly across
    #boundaries:
    @projection = @{$feature->project('contig')};

    #change the start, end, and strand of the feature in place
    $feature->move($new_start, $new_end, $new_strand);

=head1 DESCRIPTION

39
This is the Base feature class from which all Ensembl features inherit.  It
40 41 42 43 44
provides a bare minimum functionality that all features require.  It basically
describes a location on a sequence in an arbitrary coordinate system.

=head1 CONTACT

45
Post questions to the Ensembl development list: ensembl-dev@ebi.ac.uk
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

=head1 METHODS

=cut


use strict;
use warnings;

package Bio::EnsEMBL::Feature;

use Bio::EnsEMBL::Storable;
use Bio::EnsEMBL::Utils::Argument qw(rearrange);
use Bio::EnsEMBL::Utils::Exception qw(throw deprecate warning);
use Bio::EnsEMBL::Slice;
use vars qw(@ISA);

@ISA = qw(Bio::EnsEMBL::Storable);


=head2 new

  Arg [-SLICE]: Bio::EnsEMBL::SLice - Represents the sequence that this
                feature is on. The coordinates of the created feature are
                relative to the start of the slice.
  Arg [-START]: The start coordinate of this feature relative to the start
                of the slice it is sitting on.  Coordinates start at 1 and
                are inclusive.
  Arg [-END]  : The end coordinate of this feature relative to the start of
                the slice it is sitting on.  Coordinates start at 1 and are
                inclusive.
  Arg [-STRAND]: The orientation of this feature.  Valid values are 1,-1,0.
  Arg [-SEQNAME] : A seqname to be used instead of the default name of the 
                of the slice.  Useful for features that do not have an 
                attached slice such as protein features.
81 82
  Arg [-dbID]   : (optional) internal database id
  Arg [-ADAPTOR]: (optional) Bio::EnsEMBL::DBSQL::BaseAdaptor
83 84 85 86 87 88 89 90
  Example    : $feature = Bio::EnsEMBL::Feature->new(-start    => 1, 
                                                     -end      => 100,
                                                     -strand   => 1,
                                                     -slice    => $slice,
                                                     -analysis => $analysis);
  Description: Constructs a new Bio::EnsEMBL::Feature.  Generally subclasses
               of this method are instantiated, rather than this class itself.
  Returntype : Bio::EnsEMBL::Feature
91
  Exceptions : Thrown on invalid -SLICE, -ANALYSIS, -STRAND ,-ADAPTOR arguments
92
  Caller     : general, subclass constructors
93
  Status     : Stable
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

=cut


sub new {
  my $caller = shift;

  my $class = ref($caller) || $caller;

  my($start, $end, $strand, $slice, $analysis,$seqname, $dbID, $adaptor) =
    rearrange(['START','END','STRAND','SLICE','ANALYSIS', 'SEQNAME',
               'DBID', 'ADAPTOR'], @_);

  if($slice) {
    if(!ref($slice) || !$slice->isa('Bio::EnsEMBL::Slice')) {
      throw('-SLICE argument must be a Bio::EnsEMBL::Slice not '.$slice);
    }
  }

  if($analysis) {
    if(!ref($analysis) || !$analysis->isa('Bio::EnsEMBL::Analysis')) {
      throw('-ANALYSIS argument must be a Bio::EnsEMBL::Analysis not '.
            $analysis);
    }
  }

  if(defined($strand)) {
    if(!($strand == 1) && !($strand == -1) && !($strand == 0)) {
      throw('-STRAND argument must be 1, -1, or 0');
    }
  }

  if(defined($start) && defined($end)) {
127 128
    if($end+1 < $start) {
      throw('Start must be less than or equal to end+1');
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    }
  }

  return bless({'start'    => $start,
                'end'      => $end,
                'strand'   => $strand,
                'slice'    => $slice,
                'analysis' => $analysis,
                'adaptor'  => $adaptor,
                'seqname'  => $seqname,
                'dbID'     => $dbID}, $class);
}



=head2 start

  Arg [1]    : (optional) int $start
               The start of this feature relative to the start of the slice
               that it is on.
  Example    : $start = $feat->start()
  Description: Getter/Setter for the start of this feature relative to the 
               start of the slice it is on.  Note that negative values, or
               values exceeding the length of the slice are permitted.
               Start must be less than or equal to the end regardless of the 
               strand. Coordinate values start at 1 and are inclusive.
  Returntype : int
  Exceptions : none
  Caller     : general
158
  Status     : Stable
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

=cut

sub start {
  my $self = shift;
  $self->{'start'} = shift if(@_);
  return $self->{'start'};
}




=head2 end

  Arg [1]    : (optional) int $end
  Example    : $end = $feat->end();
  Description: Getter/Setter for the end of this feature relative to the
               start of the slice that it is on.  Note that negative values,
               of values exceeding the length of the slice are permitted.  End
               must be greater than or equal to start regardless of the strand.
               Coordinate values start at 1 and are inclusive.
  Returntype : int
  Exceptions : none
  Caller     : general
183
  Status     : Stable
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

=cut

sub end {
  my $self = shift;
  $self->{'end'} = shift if(@_);
  return $self->{'end'};
}




=head2 strand

  Arg [1]    : (optional) int $strand
  Example    : $feat->strand(-1);
  Description: Getter/Setter for the strand of this feature relative to the
               slice it is on.  0 is an unknown or non-applicable strand.  
               -1 is the reverse (negative) strand and 1 is the forward 
               (positive) strand.  No other values are permitted.
  Returntype : int
  Exceptions : thrown if an invalid strand argument is passed
  Caller     : general
207
  Status     : Stable
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

=cut

sub strand {
  my $self = shift;

  if(@_) {
    my $strand = shift || 0;
    if(defined($strand) && $strand != 0 && $strand != 1 && $strand != -1) {
      throw('strand argument must be 0, -1 or 1');
    }

    $self->{'strand'} = $strand;
  }

  return $self->{'strand'};
}



=head2 move

  Arg [1]    : int start
  Arg [2]    : int end
  Arg [3]    : (optional) int strand
  Example    : None
  Description: Sets the start, end and strand in one call rather than in 
               3 seperate calls to the start(), end() and strand() methods.
               This is for convenience and for speed when this needs to be
               done within a tight loop.
  Returntype : none
  Exceptions : Thrown is invalid arguments are provided
  Caller     : general
241
  Status     : Stable
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

=cut

sub move {
  my $self = shift;

  throw('start and end arguments are required') if(@_ < 2);

  my $start  = shift;
  my $end    = shift;
  my $strand = shift;

  if(defined($start) && defined($end) && $end < $start) {
    throw('start must be less than or equal to end');
  }
  if(defined($strand) && $strand != 0 && $strand != -1 && $strand != 1) {
    throw('strand must be 0, -1 or 1');
  }

  $self->{'start'} = $start;
  $self->{'end'} = $end;
  $self->{'strand'} = $strand if(defined($strand));
}



=head2 length

  Arg [1]    : none
  Example    : $length = $feat->length();
  Description: Returns the length of this feature
  Returntype : int
  Exceptions : none
  Caller     : general
276
  Status     : Stable
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

=cut

sub length {
  my $self = shift;
  return $self->{'end'} - $self->{'start'} + 1;
}




=head2 analysis

  Arg [1]    : (optional) Bio::EnsEMBL::Analysis $analysis
  Example    : $feature->analysis(new Bio::EnsEMBL::Analysis(...))
  Description: Getter/Setter for the analysis that is associated with 
               this feature.  The analysis describes how this feature 
               was derived.
  Returntype : Bio::EnsEMBL::Analysis
  Exceptions : thrown if an invalid argument is passed
  Caller     : general
298
  Status     : Stable
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

=cut

sub analysis {
  my $self = shift;

  if(@_) {
    my $an = shift;
    if(defined($an) && (!ref($an) || !$an->isa('Bio::EnsEMBL::Analysis'))) {
      throw('analysis argument must be a Bio::EnsEMBL::Analysis');
    }
    $self->{'analysis'} = $an;
  }

  return $self->{'analysis'};
}



=head2 slice

  Arg [1]    : (optional) Bio::EnsEMBL::Slice $slice
  Example    : $seqname = $feature->slice()->name();
  Description: Getter/Setter for the Slice that is associated with this 
               feature.  The slice represents the underlying sequence that this
               feature is on.  Note that this method call is analagous to the
               old SeqFeature methods contig(), entire_seq(), attach_seq(),
               etc.
  Returntype : Bio::EnsEMBL::Slice
  Exceptions : thrown if an invalid argument is passed
  Caller     : general
330
  Status     : Stable
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

=cut

sub slice {
  my $self = shift;

  if(@_) {
    my $sl = shift;
    if(defined($sl) && (!ref($sl) || !$sl->isa('Bio::EnsEMBL::Slice'))) {
      throw('slice argument must be a Bio::EnsEMBL::Slice');
    }

    $self->{'slice'} = $sl;
  }

  return $self->{'slice'};
}


=head2 transform

  Arg [1]    : string $coord_system
               The coord system to transform this feature to.
  Arg [2]    : string $version (optional)
               The version of the coord system to transform this feature to.
  Example    : $feature = $feature->transform('contig');
               next if(!defined($feature));
  Description: Returns a copy of this feature, but converted to a different
               coordinate system. The converted feature will be placed on a
               slice which spans an entire sequence region of the new
               coordinate system. If the requested coordinate system is the
               same coordinate system it is simply placed on a slice which
               spans the entire seq_region (as opposed to the original slice
               which may have only partially covered the seq_region).

               If a feature spans a boundary in the new coordinate system,
               undef is returned instead.

               For example, transforming an exon in contig coordinates to one 
               in chromosomal coodinates will place the exon on a slice of an 
               entire chromosome.
  Returntype : Bio::EnsEMBL::Feature (or undef)
  Exceptions : thrown if an invalid coordinate system is provided
374
               warning if Feature is not attached to a slice
375
  Caller     : general, transfer()
376
  Status     : Stable
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

=cut

sub transform {
  my $self = shift;
  my $cs_name = shift;
  my $cs_version = shift;
  #
  # For backwards compatibility check if the arguments are old style args
  #
  if(!$cs_name || ref($cs_name)) {
    deprecate('Calling transform without a coord system name is deprecated.');
    return $self->_deprecated_transform($cs_name);
  }

  my $slice = $self->{'slice'};

  if(!$slice) {
395 396 397 398 399 400 401 402
    warning("Feature cannot be transformed without attached slice.");
    return undef;
  }

  if(!$slice->adaptor()) {
    warning("Feature cannot be transformed without adaptor on" .
            " attached slice.");
    return undef;
403 404 405 406 407 408 409
  }

  #use db from slice since this feature may not yet be stored in a database
  my $db = $slice->adaptor->db();
  my $cs = $db->get_CoordSystemAdaptor->fetch_by_name($cs_name, $cs_version);
  my $current_cs = $slice->coord_system();

410 411 412 413 414 415
  if(!$current_cs) {
    warning("Feature cannot be transformed without CoordSystem on " .
            "attached slice.");
    return undef;
  }

416 417 418 419 420
  if(!$cs) {
    throw("Cannot transform to unknown coordinate system " .
          "[$cs_name $cs_version]\n");
  }

421 422 423 424
  # if feature is already in the requested coordinate system, we can just
  # return a copy
  if( $cs->equals( $current_cs ) && $slice->start() == 1 &&
      $slice->strand() == 1 ) {
425 426 427 428 429 430 431
    my $new_feature;
    %$new_feature = %$self;
    bless $new_feature, ref $self;
    return $new_feature;
  }

  my $projection = $self->project( $cs_name, $cs_version );
432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
  if( @$projection != 1 ) {
    return undef;
  } else {
    my $p_slice = $projection->[0]->[2];
    my $slice_adaptor = $db->get_SliceAdaptor;
    $slice = $slice_adaptor->fetch_by_region($p_slice->coord_system()->name(),
					     $p_slice->seq_region_name(),
					     undef, #start
					     undef, #end
					     1, #strand
					     $p_slice->coord_system()->version);

    my $new_feature;
    %$new_feature = %$self;
    bless $new_feature, ref $self;
    $new_feature->{'start'}  = $p_slice->start();
    $new_feature->{'end'}    = $p_slice->end();
450 451
    $new_feature->{'strand'} =
      ($self->{'strand'} == 0) ? 0 : $p_slice->strand();
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    $new_feature->{'slice'}  = $slice;
    return $new_feature;
  }
}



=head2 transfer

  Arg [1]    : Bio::EnsEMBL::Slice $slice
               The slice to transfer this feature to
  Example    : $feature = $feature->transfer($slice);
               next if(!defined($feature));
  Description: Returns a copy of this feature which has been shifted onto
               another slice.

               If the new slice is in a different coordinate system the
               feature is transformed first and then placed on the slice.
               If the feature would be split across a coordinate system
               boundary or mapped to a gap undef is returned instead.
472 473 474 475 476

               If the feature cannot be placed on the provided slice because
               it maps to an entirely different location, undef is returned
               instead.

477
  Returntype : Bio::EnsEMBL::Feature (or undef)
478 479
  Exceptions : throw on incorrect argument
               throw if feature does not have attached slice
480
  Caller     : general, transform()
481
  Status     : Stable
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

=cut


sub transfer {
  my $self = shift;
  my $slice = shift;

  if(!$slice || !ref($slice) || !$slice->isa('Bio::EnsEMBL::Slice')) {
    throw('Slice argument is required');
  }

  #make a shallow copy of the feature to be transfered
  my $feature;
  %{$feature} = %{$self};
  bless $feature, ref($self);

499 500 501 502 503 504 505
  my $current_slice = $self->{'slice'};

  if(!$current_slice) {
    warning("Feature cannot be transfered without attached slice.");
    return undef;
  }

506 507 508 509 510 511 512 513 514 515
  my $cur_cs = $current_slice->coord_system();
  my $dest_cs = $slice->coord_system();

  #if we are not in the same coord system a transformation step is needed first
  if(!$dest_cs->equals($cur_cs)) {
    $feature = $feature->transform($dest_cs->name, $dest_cs->version);
    return undef if(!defined($feature));
    $current_slice = $feature->{'slice'};
  }

516
  # feature went to entirely different seq_region
517
  if($current_slice->seq_region_name() ne $slice->seq_region_name()) {
518
    return undef;
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
  }

  #if the current feature positions are not relative to the start of the
  #seq region, convert them so they are
  my $cur_slice_start  = $current_slice->start();
  my $cur_slice_strand = $current_slice->strand();
  if($cur_slice_start != 1 || $cur_slice_strand != 1) {
    my $fstart = $feature->{'start'};
    my $fend   = $feature->{'end'};

    if($cur_slice_strand == 1) {
      $feature->{'start'} = $fstart + $cur_slice_start - 1;
      $feature->{'end'}   = $fend   + $cur_slice_start - 1;
    } else {
      my $cur_slice_end = $current_slice->end();
      $feature->{'start'}  = $cur_slice_end - $fend   + 1;
      $feature->{'end'}    = $cur_slice_end - $fstart + 1;
      $feature->{'strand'} *= -1;
    }
  }

  my $fstart = $feature->{'start'};
  my $fend   = $feature->{'end'};

  #convert to destination slice coords
  if($slice->strand == 1) {
    $feature->{'start'} = $fstart - $slice->start() + 1;
    $feature->{'end'}   = $fend   - $slice->start() + 1;
  } else {
    $feature->{'start'} = $slice->end() - $fend   + 1;
    $feature->{'end'}   = $slice->end() - $fstart + 1;
    $feature->{'strand'} *= -1;
  }

  $feature->{'slice'} = $slice;

  return $feature;
}



=head2 project

  Arg [1]    : string $name
               The name of the coordinate system to project this feature onto
  Arg [2]    : string $version (optional)
               The version of the coordinate system (such as 'NCBI34') to
               project this feature onto
  Example    :
    my $clone_projection = $feature->project('clone');

570 571 572 573
    foreach my $seg (@$clone_projection) {
      my $clone = $seg->to_Slice();
      print "Features current coords ", $seg->from_start, '-',
        $seg->from_end, " project onto clone coords " .
574
        $clone->seq_region_name, ':', $clone->start, '-', $clone->end,
575
        $clone->strand, "\n";
576 577 578
    }
  Description: Returns the results of 'projecting' this feature onto another
               coordinate system.  This is useful to see where a feature
579
               would lie in a coordinate system in which it
580 581
               crosses a boundary.

582 583 584 585 586 587
               This method returns a reference to a list of
               Bio::EnsEMBL::ProjectionSegment objects.
               ProjectionSegments are blessed arrays and can also be used as
               triplets [from_start,from_end,to_Slice]. The from_start and
               from_end are the coordinates relative to the feature start.
               For example, if a feature is current 100-200bp on a slice
588
               then the triplets returned might be:
589 590
               [1,50,$slice1],
               [51,101,$slice2]
591

592 593
               The to_Slice is a slice spanning the region on the requested
               coordinate system that this feature projected to.
Graham McVicker's avatar
Graham McVicker committed
594

595 596 597
               If the feature projects entirely into a gap then a reference to
               an empty list is returned.

598 599
  Returntype : list reference of Bio::EnsEMBL::ProjectionSegments
               which can also be used as [$start,$end,$slice] triplets
600
  Exceptions : slice does not have an adaptor
601
  Caller     : general
602
  Status     : Stable
603 604 605 606 607 608 609 610 611 612 613

=cut

sub project {
  my $self = shift;
  my $cs_name = shift;
  my $cs_version = shift;

  my $slice = $self->{'slice'};

  if(!$slice) {
614 615
    warning("Feature cannot be projected without attached slice.");
    return [];
616 617
  }

618

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  #get an adaptor from the attached slice because this feature may not yet
  #be stored and may not have its own adaptor
  my $slice_adaptor = $slice->adaptor();

  if(!$slice_adaptor) {
    throw("Cannot project feature because associated slice does not have an " .
          " adaptor");
  }

  my $strand = $self->strand() * $slice->strand();
  #fetch by feature always gives back forward strand slice:
  $slice = $slice_adaptor->fetch_by_Feature($self);
  $slice = $slice->invert if($strand == -1);
  return $slice->project($cs_name, $cs_version);
}



=head2 seqname

  Arg [1]    : (optional) $seqname
  Example    : $seqname = $feat->seqname();
  Description: Getter/Setter for the name of the sequence that this feature
               is on. Normally you can get away with not setting this value
               and it will default to the name of the slice on which this
               feature is on.  It is useful to set this value on features which
               do not ordinarily sit on features such as ProteinFeatures which
               sit on peptides.
  Returntype : string
  Exceptions : none
  Caller     : general
650
  Status     : Stable
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

=cut

sub seqname {
  my $self = shift;
  
  if(@_) {
    $self->{'seqname'} = shift;
  }

  if(!$self->{'seqname'} && $self->slice()) {
    return $self->slice->name();
  }

  return $self->{'seqname'};
}




=head2 display_id

  Arg [1]    : none
  Example    : print $f->display_id();
  Description: This method returns a string that is considered to be
               the 'display' identifier.  It is overridden by subclasses to
               return an appropriate value for objects of that particular 
               class.  If no appropriate display id is available an empty
               string is returned instead.
  Returntype : string
  Exceptions : none
  Caller     : web drawing code
683
  Status     : Stable
684 685 686 687 688 689 690 691 692

=cut

sub display_id {
  my $self = shift;
  return '';
}


Arne Stabenau's avatar
Arne Stabenau committed
693 694 695 696 697 698 699
=head2 feature_Slice

  Args       : none
  Example    : $slice = $feature->feature_Slice()
  Description: This is a convenience method to return a slice that covers the
               Area of this feature. The feature start will be at 1 on it, and
               it will have the length of this feature.
700 701 702
  Returntype : Bio::EnsEMBL::Slice or undef if this feature has no attached
               Slice.
  Exceptions : warning if Feature does not have attached slice.
Arne Stabenau's avatar
Arne Stabenau committed
703
  Caller     : web drawing code
704
  Status     : Stable
Arne Stabenau's avatar
Arne Stabenau committed
705 706 707 708 709

=cut

sub feature_Slice {
  my $self = shift;
710

Arne Stabenau's avatar
Arne Stabenau committed
711 712
  my $slice = $self->slice();

713 714 715 716 717
  if(!$slice) {
    warning('Cannot obtain Feature_Slice for feature without attached slice');
    return undef;
  }

Arne Stabenau's avatar
Arne Stabenau committed
718 719 720 721 722 723 724 725 726 727 728 729 730
  return Bio::EnsEMBL::Slice->new
    (-seq_region_name   => $slice->seq_region_name,
     -seq_region_length => $slice->seq_region_length,
     -coord_system      => $slice->coord_system,
     -start             => $self->seq_region_start(),
     -end               => $self->seq_region_end(),
     -strand            => $self->seq_region_strand(),
     -adaptor           => $slice->adaptor());

  
}


731 732 733 734 735 736 737 738 739
=head2 seq_region_name

  Arg [1]    : none
  Example    : print $feature->seq_region_name();
  Description: Gets the name of the seq_region which this feature is on.
               Returns undef if this Feature is not on a slice.
  Returntype : string or undef
  Exceptions : none
  Caller     : general
740
  Status     : Stable
741 742 743 744 745 746 747 748 749 750 751

=cut

sub seq_region_name {
  my $self = shift;
  my $slice = $self->{'slice'};

  return ($slice) ? $slice->seq_region_name() : undef;
}


752 753 754 755 756 757 758 759 760
=head2 seq_region_length

  Arg [1]    : none
  Example    : print $feature->seq_region_length();
  Description: Returns the length of the seq_region which this feature is on 
               Returns undef if this Feature is not on a slice.
  Returntype : unsigned int or undef
  Exceptions : none
  Caller     : general
761
  Status     : Stable
762 763 764 765 766 767 768 769 770 771 772 773

=cut


sub seq_region_length {
  my $self = shift;
  my $slice = $self->{'slice'};

  return ($slice) ? $slice->seq_region_length() : undef;
}


774 775 776 777 778 779 780 781 782 783
=head2 seq_region_strand

  Arg [1]    : none
  Example    : print $feature->seq_region_strand();
  Description: Returns the strand of the seq_region which this feature is on 
               (i.e. feature_strand * slice_strand)
               Returns undef if this Feature is not on a slice.
  Returntype : 1,0,-1 or undef
  Exceptions : none
  Caller     : general
784
  Status     : Stable
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

=cut


sub seq_region_strand {
  my $self = shift;
  my $slice = $self->{'slice'};

  return ($slice) ? $slice->strand() * $self->{'strand'} : undef;
}


=head2 seq_region_start

  Arg [1]    : none
  Example    : print $feature->seq_region_start();
  Description: Convenience method which returns the absolute start of this
               feature on the seq_region, as opposed to the relative (slice) 
               position.

               Returns undef if this feature is not on a slice.
  Returntype : int or undef
  Exceptions : none
  Caller     : general
809
  Status     : Stable
810 811 812 813 814 815 816 817 818 819

=cut

sub seq_region_start {
  my $self = shift;
  my $slice = $self->{'slice'};

  return undef if(!$slice);

  if($slice->strand == 1) {
820
    return undef if(!defined($self->{'start'}));
821 822
    return $slice->start() + $self->{'start'} - 1;
  } else {
823
    return undef if(!defined($self->{'end'}));
824 825 826 827 828 829 830 831 832 833
    return $slice->end() - $self->{'end'} + 1;
  }
}


=head2 seq_region_end

  Arg [1]    : none
  Example    : print $feature->seq_region_end();
  Description: Convenience method which returns the absolute end of this
834
               feature on the seq_region, as opposed to the relative (slice)
835 836 837 838 839 840
               position.

               Returns undef if this feature is not on a slice.
  Returntype : int or undef
  Exceptions : none
  Caller     : general
841
  Status     : Stable
842 843 844 845 846 847 848 849 850 851

=cut

sub seq_region_end {
  my $self = shift;
  my $slice = $self->{'slice'};

  return undef if(!$slice);

  if($slice->strand == 1) {
852
    return undef if(!defined($self->{'end'}));
853 854
    return $slice->start() + $self->{'end'} - 1;
  } else {
855
    return undef if(!defined($self->{'start'}));
856 857 858 859 860
    return $slice->end() - $self->{'start'} + 1;
  }
}


861 862 863 864 865 866
=head2 coord_system_name

  Arg [1]    : none
  Example    : print $feature->coord_system_name()
  Description: Gets the name of the coord_system which this feature is on.
               Returns undef if this Feature is not on a slice.
867 868 869 870
  Returntype : string or undef
  Exceptions : none
  Caller     : general
  Status     : Stable
871 872 873 874 875 876 877 878 879

=cut

sub coord_system_name {
  my $self = shift;
  my $slice = $self->{'slice'};
  return ($slice) ? $slice->coord_system_name() : undef;
}

Arne Stabenau's avatar
Arne Stabenau committed
880 881 882 883 884 885 886 887

=head2 seq

  Args       : none
  Example    : my $dna_sequence = $simple_feature->seq();
  Description: Returns the dna sequence from the attached slice and 
               attached database that overlaps with this feature.
               Returns undef if there is no slice or no database.
888
               Returns undef if this feature is unstranded (i.e. strand=0).
Arne Stabenau's avatar
Arne Stabenau committed
889
  Returntype : undef or string
890
  Exceptions : warning if this feature is not stranded
Arne Stabenau's avatar
Arne Stabenau committed
891
  Caller     : general
892
  Status     : Stable
Arne Stabenau's avatar
Arne Stabenau committed
893 894 895 896 897 898 899 900 901 902 903

=cut


sub seq {
  my $self = shift;

  if( ! defined $self->{'slice'} ) {
    return undef;
  }

904 905 906 907 908 909 910
  if(!$self->strand()) {
    warning("Cannot retrieve sequence for unstranded feature.");
    return undef;
  }

  return $self->{'slice'}->subseq($self->start(), $self->end(),
                                  $self->strand());
Arne Stabenau's avatar
Arne Stabenau committed
911 912 913 914

}


Arne Stabenau's avatar
Arne Stabenau committed
915

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942

=head2 get_all_alt_locations

  Arg [1]    : none
  Example    : @features = @{$feature->get_all_alt_locations()};
               foreach $f (@features) {
                 print $f->slice->seq_region_name,' ',$f->start, $f->end,"\n";
               }

  Description: Retrieves shallow copies of this feature in its alternate
               locations.  A feature can be considered to have multiple
               locations when it sits on a alternative structural haplotype
               or when it is on a pseudo autosomal region.  Most features will
               just return a reference to an empty list though.
               The features returned by this method will be on a slice which
               covers the entire alternate region.

               Currently this method does not take into account alternate
               locations on the alternate locations (e.g. a reference
               sequence may have multiple alternate haplotypes.  Asking
               for alternate locations of a feature on one of the alternate
               haplotypes will give you back the reference location, but not
               locations on the other alternate haplotypes).

  Returntype : reference to list of features of the same type of this feature.
  Exceptions : none
  Caller     : general
943
  Status     : Stable
944 945 946 947 948

=cut

sub get_all_alt_locations {
  my $self = shift;
949
  my $return_all = shift || 0;
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964

  my $slice = $self->{'slice'} or return [];
  my $sa = $slice->adaptor() or return [];

  # get slice of entire region
  $slice = $sa->fetch_by_seq_region_id($slice->get_seq_region_id);

  my $axfa = $sa->db->get_AssemblyExceptionFeatureAdaptor();
  my $axfs = $axfa->fetch_all_by_Slice($slice);

  my (@haps, @alt);

  foreach my $axf (@$axfs) {
    if(uc($axf->type()) eq 'HAP') {
      push @haps, $axf;
965
    } elsif(uc($axf->type()) =~ 'PAR') {
966
      push @alt, $axf;
967
    } elsif( $axf->type() eq "HAP REF" ) {
968
      push @haps, $axf if $return_all > 0 ;
969
      # do nothing when you are on REF
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
    } else {
      warning("Unknown exception feature type ". $axf->type()."- ignoring.");
    }
  }

  # regions surrounding hap are those of interest, not hap itself
  # convert hap alt. exc. features to regions around haps instead
  foreach my $h (@haps) {
    my $haslice = $h->alternate_slice();
    my $hacs    = $haslice->coord_system();

    if($h->start() > 1 && $haslice->start() > 1) {
      my $aslice = $sa->fetch_by_region($hacs->name(),
                                        $haslice->seq_region_name(),
                                        1,
                                        $haslice->start()-1,
                                        $haslice->strand(),
                                        $hacs->version());

      push @alt, Bio::EnsEMBL::AssemblyExceptionFeature->new
        (-start  => 1,
         -end    => $h->start()-1,
         -alternate_slice => $aslice);
    }

    if($h->end() < $slice->seq_region_length() &&
       $haslice->end < $haslice->seq_region_length()) {
      my $aslice = $sa->fetch_by_region($hacs->name(),
                                        $haslice->seq_region_name(),
                                        $haslice->end()+1,
                                        $haslice->seq_region_length(),
                                        $haslice->strand(),
                                        $hacs->version());

      push @alt, Bio::EnsEMBL::AssemblyExceptionFeature->new
        (-start  => $h->end() + 1,
         -end    => $slice->seq_region_length(),
         -alternate_slice => $aslice);
    }
  }


  # check if exception regions contain our feature

  my @features;

  foreach my $axf (@alt) {
    # ignore other region if feature is not entirely on it
    next if($self->seq_region_start() < $axf->start() ||
            $self->seq_region_end()   > $axf->end());

    # quick shallow copy of the feature
    my $f;
    %$f = %$self;
    bless $f, ref($self);

    my $aslice = $axf->alternate_slice();

    # position feature on entire slice of other region
    $f->{'start'}  = $f->seq_region_start() - $axf->start() + $aslice->start();
    $f->{'end'}    = $f->seq_region_end()   - $axf->start() + $aslice->start();
    $f->{'strand'} *= $aslice->strand();

    $f->{'slice'} = $sa->fetch_by_seq_region_id($aslice->get_seq_region_id());

    push @features, $f;
  }

  return \@features;
}


1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
=head2 overlaps

  Arg [1]    : Bio::EnsEMBL::Feature $f
               The other feature you want to check overlap with this feature
               for.
  Description: This method does a range comparison of this features start and
               end and compares it with another features start and end. It will
               return true if these ranges overlap and the features are on the
               same seq_region.
  Returntype : TRUE if features overlap, FALSE if they don't
  Exceptions : warning if features are on different seq_regions
  Caller     : general
  Status     : At Risk

=cut

sub overlaps {
  my $self = shift;
  my $f = shift;

Patrick Meidl's avatar
Patrick Meidl committed
1062 1063 1064 1065 1066
  my $sr1_name = $self->seq_region_name;
  my $sr2_name = $f->seq_region_name;

  if (!$sr1_name or !$sr2_name or ($sr1_name ne $sr2_name)) {
    warning("Bio::EnsEMBL::Feature->overlaps(): features are on different seq regions (or not attached to a seq region at all).");
1067 1068 1069 1070 1071 1072 1073
    return undef;
  }
  
  return ($self->end >= $f->start and $self->start <= $f->end);
}


1074 1075


1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
##############################################
# Methods included for backwards compatibility
##############################################


# contig
#
# This method is included for backwards compatibility.
# Use slice() instead
#
sub contig {
  deprecate('Use slice() instead');
  slice(@_);
}



# sub_SeqFeature
#
# This method is only for genebuild backwards compatibility.
# Avoid using it if possible
#
sub sub_SeqFeature{
  my ($self) = @_;
  return @{$self->{'_gsf_sub_array'}} if($self->{'_gsf_sub_array'});
}

# add_sub_SeqFeature
#
# This method is only for genebuild backwards compatibility.
# Avoid using it if possible
#
sub add_sub_SeqFeature{
  my ($self,$feat,$expand) = @_;
Laura Clarke's avatar
 
Laura Clarke committed
1110
  my ($p, $f, $l) = caller;
1111 1112
  if( $expand eq 'EXPAND' ) {
    # if this doesn't have start/end set - forget it!
Laura Clarke's avatar
 
Laura Clarke committed
1113 1114
    if( ! $self->start && ! $self->end ) {
      
1115 1116 1117 1118 1119
      $self->start($feat->start());
      $self->end($feat->end());
      $self->strand($feat->strand);
    } else {
      if( $feat->start < $self->start ) {
Laura Clarke's avatar
 
Laura Clarke committed
1120
        $self->start($feat->start);
1121 1122 1123
      }

      if( $feat->end > $self->end ) {
Laura Clarke's avatar
 
Laura Clarke committed
1124
        $self->end($feat->end);
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
      }
    }
   } else {
     if($self->start > $feat->start || $self->end < $feat->end) {
       throw("$feat is not contained within parent feature, " .
             "and expansion is not valid");
     }
   }

   push(@{$self->{'_gsf_sub_array'}},$feat);
}

# flush_sub_SeqFeature
#
# This method is only for genebuild backwards compatibility.
# Avoid using it isf possible
#
sub flush_sub_SeqFeature {
  my ($self) = @_;
  $self->{'_gsf_sub_array'} = [];
}


sub _deprecated_transform {
  my $self = shift;
  my $arg = shift;

  if(!$arg) {
    warning("Calling transform() with no arguments is deprecated.\n".
          "A coordinate system name argument should be used instead.\n".
          "You probably wanted transform('seqlevel') or transform('contig').");
    return $self->transform('seqlevel');
  }

  if(ref($arg) eq 'Bio::EnsEMBL::Slice') {
    if($arg->{'empty'}) {
      warning("Calling transform with an empty slice is deprecated.\n" .
                "A coordinate system name argument should be used instead.\n".
                "You probably wanted transform('chromosome') or " .
                "transform('toplevel')");
      return $self->transform('toplevel');
    }
    warning("Calling transform with a slice is deprecated.\n" .
              "Use the transfer method instead");
    return $self->transfer($arg);
  }

  warning("Calling transform with a [".ref($arg)."] arg is no longer " .
          "(or never was) supported.  Doing nothing instead.");

  return $self;
}

# id
#
# This method is included for backwards compatibility only.
# Use hseqname or dbID or stable_id instead
#
sub id {
  my $self = shift;
  deprecate("id method is not used - use display_id instead");
  return $self->{'stable_id'} if($self->{'stable_id'});
  return $self->{'hseqname'} if($self->{'hseqname'});
  return $self->{'seqname'}  if($self->{'seqname'});
  return $self->{'dbID'};
}

1192

1193
1;