Translation.pm 32.4 KB
Newer Older
1 2
=head1 LICENSE

Andy Yates's avatar
Andy Yates committed
3
  Copyright (c) 1999-2012 The European Bioinformatics Institute and
4 5 6 7 8 9 10 11 12 13
  Genome Research Limited.  All rights reserved.

  This software is distributed under a modified Apache license.
  For license details, please see

    http://www.ensembl.org/info/about/code_licence.html

=head1 CONTACT

  Please email comments or questions to the public Ensembl
14
  developers list at <dev@ensembl.org>.
15 16 17 18 19

  Questions may also be sent to the Ensembl help desk at
  <helpdesk@ensembl.org>.

=cut
Ewan Birney's avatar
Ewan Birney committed
20 21 22

=head1 NAME

23 24
Bio::EnsEMBL::Translation - A class representing the translation of a
transcript
Ewan Birney's avatar
Ewan Birney committed
25 26 27

=head1 SYNOPSIS

Patrick Meidl's avatar
Patrick Meidl committed
28 29 30 31 32 33 34 35 36 37 38 39
  my $translation = Bio::EnsEMBL::Translation->new(
    -START_EXON => $exon1,
    -END_EXON   => $exon2,
    -SEQ_START  => 98,
    -SEQ_END    => 39
  );

  # stable ID setter
  $translation->stable_id('ENSP00053458');

  # get start and end position in start/end exons
  my $start = $translation->start;
40
  my $end   = $translation->end;
41

Ewan Birney's avatar
Ewan Birney committed
42 43
=head1 DESCRIPTION

44
A Translation object defines the CDS and UTR regions of a Transcript
45
through the use of start_Exon/end_Exon, and start/end attributes.
Ewan Birney's avatar
Ewan Birney committed
46 47 48 49

=cut


50 51
package Bio::EnsEMBL::Translation;

Ewan Birney's avatar
Ewan Birney committed
52 53 54
use vars qw($AUTOLOAD @ISA);
use strict;

Monika Komorowska's avatar
Monika Komorowska committed
55
use Scalar::Util qw(weaken isweak);
56

57 58
use Bio::EnsEMBL::Utils::Exception qw( deprecate throw warning );
use Bio::EnsEMBL::Utils::Argument qw( rearrange );
59
use Bio::EnsEMBL::Utils::Scalar qw( assert_ref );
Ewan Birney's avatar
Ewan Birney committed
60

61
use Bio::EnsEMBL::Storable;
Ewan Birney's avatar
Ewan Birney committed
62

63
@ISA = qw(Bio::EnsEMBL::Storable);
Ewan Birney's avatar
Ewan Birney committed
64 65


66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
=head2 new

  Arg [-START_EXON] : The Exon object in which the translation (CDS) starts
  Arg [-END_EXON]   : The Exon object in which the translation (CDS) ends
  Arg [-SEQ_START]  : The offset in the start_Exon indicating the start
                      position of the CDS.
  Arg [-SEQ_END]    : The offset in the end_Exon indicating the end
                      position of the CDS.
  Arg [-STABLE_ID]  : The stable identifier for this Translation
  Arg [-VERSION]    : The version of the stable identifier
  Arg [-DBID]       : The internal identifier of this Translation
  Arg [-ADAPTOR]    : The TranslationAdaptor for this Translation
  Arg [-SEQ]        : Manually sets the peptide sequence of this translation.
                      May be useful if this translation is not stored in
                      a database.
81 82
  Arg [-CREATED_DATE]: the date the translation was created
  Arg [-MODIFIED_DATE]: the date the translation was modified
83 84 85 86 87 88 89 90 91
  Example    : my $tl = Bio::EnsEMBL::Translation->new
                   (-START_EXON => $ex1,
                    -END_EXON   => $ex2,
                    -SEQ_START  => 98,
                    -SEQ_END    => 39);
  Description: Constructor.  Creates a new Translation object
  Returntype : Bio::EnsEMBL::Translation
  Exceptions : none
  Caller     : general
92
  Status     : Stable
93 94 95

=cut

96
sub new {
Graham McVicker's avatar
Graham McVicker committed
97
  my $caller = shift;
98

Graham McVicker's avatar
Graham McVicker committed
99
  my $class = ref($caller) || $caller;
Ewan Birney's avatar
Ewan Birney committed
100

101
  my ( $start_exon, $end_exon, $seq_start, $seq_end,
102
       $stable_id, $version, $dbID, $adaptor, $seq,
103 104 105 106
       $created_date, $modified_date ) =
	   rearrange( [ "START_EXON", "END_EXON", "SEQ_START", "SEQ_END",
			"STABLE_ID", "VERSION", "DBID", "ADAPTOR",
			"SEQ", "CREATED_DATE", "MODIFIED_DATE" ], @_ );
107 108 109

  my $self = bless {
		    'start_exon' => $start_exon,
110 111 112 113 114 115
		    'end_exon'   => $end_exon,
		    'dbID'       => $dbID,
		    'start'      => $seq_start,
		    'end'        => $seq_end,
		    'stable_id'  => $stable_id,
		    'version'    => $version,
116 117
		    'created_date' => $created_date,
		    'modified_date' => $modified_date,
Monika Komorowska's avatar
Monika Komorowska committed
118
                    'seq'        => $seq
119 120
		   }, $class;

Monika Komorowska's avatar
Monika Komorowska committed
121 122
  $self->adaptor($adaptor);

123
  return $self;
Ewan Birney's avatar
Ewan Birney committed
124 125
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
=head2 new_fast

  Arg [1]    : hashref to be blessed
  Description: Construct a new Bio::EnsEMBL::Translation using the hashref.
  Exceptions : none
  Returntype : Bio::EnsEMBL::Translation
  Caller     : general, subclass constructors
  Status     : Stable

=cut


sub new_fast {
  my $class = shift;
  my $hashref = shift;
Monika Komorowska's avatar
Monika Komorowska committed
141 142 143
  my $self = bless $hashref, $class;
  weaken($self->{adaptor})  if ( ! isweak($self->{adaptor}) );
  return $self;
144 145
}

146 147 148 149 150 151 152
=head2 transcript

  Arg [1]       : Transcript object (optional)
  Description   : Sets or retrieves the transcript object associated
                  with this translation object.
  Exceptions    : Throws if there is no adaptor or no dbID defined for
                  the translation object.
153
  Returntype    : Bio::EnsEMBL::Transcript
154 155
=cut

156 157 158 159
sub transcript {
  my ( $self, $transcript ) = @_;

  if ( defined($transcript) ) {
160
    assert_ref( $transcript, 'Bio::EnsEMBL::Transcript' );
161 162 163

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

164
    weaken( $self->{'transcript'} );    # Avoid circular references.
165

166 167 168
  } elsif ( @_ > 1 ) {
    # Break connection to transcript.
    delete( $self->{'transcript'} );
169
  } elsif ( !defined( $self->{'transcript'} ) ) {
Monika Komorowska's avatar
Monika Komorowska committed
170
    my $adaptor = $self->adaptor;
171
    if ( !defined($adaptor) ) {
172
      throw(   "Adaptor is not set for translation, "
173
             . "can not fetch its transcript." );
174 175 176 177
    }

    my $dbID = $self->{'dbID'};
    if ( !defined($dbID) ) {
178 179
      throw(   "dbID is not set for translation, "
             . " can not fetch its transcript." );
180 181 182 183 184
    }

    $self->{'transcript'} =
      $adaptor->db()->get_TranscriptAdaptor()
      ->fetch_by_translation_id($dbID);
185

186 187 188 189
  # Do not weaken the reference if we had to get the transcript from the
  # database.  The user is probably working on translations directly,
  # not going through transcripts.
  #weaken( $self->{'transcript'} );    # Avoid circular references.
190 191 192
  }

  return $self->{'transcript'};
193
} ## end sub transcript
194

Ewan Birney's avatar
Ewan Birney committed
195 196 197

=head2 start

Patrick Meidl's avatar
Patrick Meidl committed
198 199 200 201 202
  Arg [1]    : (optional) int $start - start position to set
  Example    : $translation->start(17);
  Description: Getter/setter for the value of start, which is a position within
               the exon given by start_Exon.

203 204
               If you need genomic coordinates, use the genomic_start()
               method.
Patrick Meidl's avatar
Patrick Meidl committed
205 206 207 208
  Returntype : int
  Exceptions : none
  Caller     : general
  Status     : Stable
Ewan Birney's avatar
Ewan Birney committed
209 210 211 212 213 214 215

=cut

sub start{
   my $obj = shift;
   if( @_ ) {
      my $value = shift;
Laura Clarke's avatar
 
Laura Clarke committed
216
      
Ewan Birney's avatar
Ewan Birney committed
217 218 219 220 221 222 223
      $obj->{'start'} = $value;
    }
    return $obj->{'start'};

}


224 225
=head2 end

Patrick Meidl's avatar
Patrick Meidl committed
226 227 228 229
  Arg [1]    : (optional) int $end - end position to set
  Example    : $translation->end(8);
  Description: Getter/setter for the value of end, which is a position within
               the exon given by end_Exon.
230

231 232
               If you need genomic coordinates, use the genomic_end()
               method.
Patrick Meidl's avatar
Patrick Meidl committed
233 234 235 236
  Returntype : int
  Exceptions : none
  Caller     : general
  Status     : Stable
237 238 239 240 241 242 243

=cut

sub end {
   my $self = shift;
   if( @_ ) {
      my $value = shift;
Laura Clarke's avatar
 
Laura Clarke committed
244
      
245 246 247 248 249 250 251
      $self->{'end'} = $value;
    }
    return $self->{'end'};

}


Graham McVicker's avatar
Graham McVicker committed
252
=head2 start_Exon
253

Patrick Meidl's avatar
Patrick Meidl committed
254 255 256 257 258 259 260 261 262
  Arg [1]    : (optional) Bio::EnsEMBL::Exon - start exon to assign
  Example    : $translation->start_Exon($exon1);
  Description: Getter/setter for the value of start_Exon, which denotes the
               exon at which translation starts (and within this exon, at the
               position indicated by start, see above).
  Returntype : Bio::EnsEMBL::Exon
  Exceptions : thrown on wrong argument type
  Caller     : general
  Status     : Stable
Ewan Birney's avatar
Ewan Birney committed
263 264 265

=cut

Graham McVicker's avatar
Graham McVicker committed
266
sub start_Exon {
267
   my $self = shift;
268

Ewan Birney's avatar
Ewan Birney committed
269 270
   if( @_ ) {
      my $value = shift;
271
      if( !ref $value || !$value->isa('Bio::EnsEMBL::Exon') ) {
272
         throw("Got to have an Exon object, not a $value");
273 274
      }
      $self->{'start_exon'} = $value;
Ewan Birney's avatar
Ewan Birney committed
275
    }
276
   return $self->{'start_exon'};
Ewan Birney's avatar
Ewan Birney committed
277 278 279
}


Graham McVicker's avatar
Graham McVicker committed
280
=head2 end_Exon
281

Patrick Meidl's avatar
Patrick Meidl committed
282 283 284 285 286 287 288 289 290
  Arg [1]    : (optional) Bio::EnsEMBL::Exon - start exon to assign
  Example    : $translation->start_Exon($exon1);
  Description: Getter/setter for the value of end_Exon, which denotes the
               exon at which translation ends (and within this exon, at the
               position indicated by end, see above).
  Returntype : Bio::EnsEMBL::Exon
  Exceptions : thrown on wrong argument type
  Caller     : general
  Status     : Stable
Ewan Birney's avatar
Ewan Birney committed
291 292 293

=cut

Graham McVicker's avatar
Graham McVicker committed
294
sub end_Exon {
295
   my $self = shift;
Ewan Birney's avatar
Ewan Birney committed
296 297
   if( @_ ) {
      my $value = shift;
298
      if( !ref $value || !$value->isa('Bio::EnsEMBL::Exon') ) {
299
         throw("Got to have an Exon object, not a $value");
300 301
      }
      $self->{'end_exon'} = $value;
302
    } 
Ewan Birney's avatar
Ewan Birney committed
303

304
    return $self->{'end_exon'};
Ewan Birney's avatar
Ewan Birney committed
305 306
}

307 308
=head2 cdna_start

Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
309 310
    Arg  [1]    : (optional) Bio::EnsEMBL::Transcript $transcript
                  The transcript which this is a translation of.
311 312 313
    Example     : $translation_cdna_start = $translation->cdna_start();
    Description : Returns the start position of the translation in cDNA
                  coordinates.
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
314 315 316
                  If no transcript is given, the method will use
                  TranscriptAdaptor->fetch_by_translation_id() to locate
                  the correct transcript.
317 318 319 320 321 322 323 324 325
    Return type : Integer
    Exceptions  : Throws if the given (optional) argument is not a
                  transcript.
    Caller      : General
    Status      : At Risk (Under Development)

=cut

sub cdna_start {
326
  my ( $self, $transcript ) = @_;
327

328 329 330 331 332 333
  if ( defined($transcript)
       && (    !ref($transcript)
            || !$transcript->isa('Bio::EnsEMBL::Transcript') ) )
  {
    throw("Argument is not a transcript");
  }
334

335 336 337 338 339
  if ( !exists( $self->{'cdna_start'} ) ) {
    if ( !defined($transcript) ) {
      # We were not given a transcript, get the transcript out of
      # the database.
      $transcript = $self->transcript();
340 341
    }

342 343 344 345 346 347
    $self->{'cdna_start'} =
      $self->start_Exon()->cdna_coding_start($transcript);
  }

  return $self->{'cdna_start'};
}
348 349 350

=head2 cdna_end

Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
351 352
    Arg  [1]    : (optional) Bio::EnsEMBL::Transcript $transcript
                  The transcript which this is a translation of.
353 354 355
    Example     : $translation_cdna_end = $translation->cdna_end();
    Description : Returns the end position of the translation in cDNA
                  coordinates.
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
356 357 358
                  If no transcript is given, the method will use
                  TranscriptAdaptor->fetch_by_translation_id() to locate
                  the correct transcript.
359 360 361 362 363 364 365 366 367
    Return type : Integer
    Exceptions  : Throws if the given (optional) argument is not a
                  transcript.
    Caller      : General
    Status      : At Risk (Under Development)

=cut

sub cdna_end {
368
  my ( $self, $transcript ) = @_;
369

370 371 372 373 374 375
  if ( defined($transcript)
       && (    !ref($transcript)
            || !$transcript->isa('Bio::EnsEMBL::Transcript') ) )
  {
    throw("Argument is not a transcript");
  }
376

377 378 379 380 381
  if ( !exists( $self->{'cdna_end'} ) ) {
    if ( !defined($transcript) ) {
      # We were not given a transcript, get the transcript out of
      # the database.
      $transcript = $self->transcript();
382 383
    }

384 385 386 387 388 389
    $self->{'cdna_end'} =
      $self->end_Exon()->cdna_coding_end($transcript);
  }

  return $self->{'cdna_end'};
}
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

=head2 genomic_start

    Args        : None
    Example     : $translation_genomic_start =
                      $translation->genomic_start();
    Description : Returns the start position of the translation in
                  genomic coordinates on the forward strand.
    Return type : Integer
    Exceptions  : None
    Caller      : General
    Status      : At Risk (Under Development)

=cut

sub genomic_start {
    my $self = shift;

    if ( !exists $self->{'genomic_start'} ) {
        if ( $self->start_Exon()->strand() >= 0 ) {
            $self->{'genomic_start'} =
              $self->start_Exon()->start() + ( $self->start() - 1 );
        } else {
            $self->{'genomic_start'} =
              $self->end_Exon()->end() - ( $self->end() - 1 );
        }
    }

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

=head2 genomic_end

    Args        : None
    Example     : $translation_genomic_end = $translation->genomic_end();
    Description : Returns the end position of the translation in genomic
                  coordinates on the forward strand.
    Return type : Integer
    Exceptions  : None
    Caller      : General
    Status      : At Risk (Under Development)

=cut

sub genomic_end {
    my $self = shift;

    if ( !exists $self->{'genomic_end'} ) {
        if ( $self->end_Exon()->strand() >= 0 ) {
            $self->{'genomic_end'} =
              $self->end_Exon()->start() + ( $self->end() - 1 );
        } else {
            $self->{'genomic_end'} =
              $self->start_Exon()->end() - ( $self->start() - 1 );
        }
    }

    return $self->{'genomic_end'};
}
Ewan Birney's avatar
Ewan Birney committed
449

450
=head2 version
451

Patrick Meidl's avatar
Patrick Meidl committed
452 453 454
  Arg [1]    : (optional) string $version - version to set
  Example    : $translation->version(2);
  Description: Getter/setter for attribute version
455 456 457
  Returntype : string
  Exceptions : none
  Caller     : general
458
  Status     : Stable
Ewan Birney's avatar
Ewan Birney committed
459 460 461

=cut

462
sub version {
463
   my $self = shift;
464 465
  $self->{'version'} = shift if( @_ );
  return $self->{'version'};
Ewan Birney's avatar
Ewan Birney committed
466
}
467 468


469
=head2 stable_id
Graham McVicker's avatar
Graham McVicker committed
470

Patrick Meidl's avatar
Patrick Meidl committed
471 472 473
  Arg [1]    : (optional) string $stable_id - stable ID to set
  Example    : $translation->stable_id('ENSP0059890');
  Description: Getter/setter for attribute stable_id
474 475 476
  Returntype : string
  Exceptions : none
  Caller     : general
477
  Status     : Stable
478 479 480

=cut

481
sub stable_id {
482
   my $self = shift;
483 484
  $self->{'stable_id'} = shift if( @_ );
  return $self->{'stable_id'};
485 486
}

487 488
=head2 created_date

Patrick Meidl's avatar
Patrick Meidl committed
489 490 491
  Arg [1]    : (optional) string $created_date - created date to set
  Example    : $translation->created_date('2007-01-10 20:52:00');
  Description: Getter/setter for attribute created date
492 493 494 495 496 497 498
  Returntype : string
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut 

499 500 501 502 503 504 505
sub created_date {
  my $self = shift;
  $self->{'created_date'} = shift if ( @_ );
  return $self->{'created_date'};
}


506 507
=head2 modified_date

Patrick Meidl's avatar
Patrick Meidl committed
508 509 510
  Arg [1]    : (optional) string $modified_date - modification date to set
  Example    : $translation->modified_date('2007-01-10 20:52:00');
  Description: Getter/setter for attribute modified date
511 512 513 514 515 516 517
  Returntype : string
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut 

518 519 520 521 522 523
sub modified_date {
  my $self = shift;
  $self->{'modified_date'} = shift if ( @_ );
  return $self->{'modified_date'};
}

524

Graham McVicker's avatar
Graham McVicker committed
525

526 527
=head2 transform

Patrick Meidl's avatar
Patrick Meidl committed
528 529 530 531
  Arg [1]    : hashref $old_new_exon_map
               a hash that maps old to new exons for a whole gene
  Description: maps start end end exon according to mapping table.
              If an exon is not mapped, just keep the old one.
532
  Returntype : none
Patrick Meidl's avatar
Patrick Meidl committed
533 534
  Exceptions : none
  Caller     : Transcript->transform() 
535
  Status     : Stable
536 537 538 539 540 541 542

=cut

sub transform {
  my $self = shift;
  my $href_exons = shift;

Graham McVicker's avatar
Graham McVicker committed
543 544
  my $start_exon = $self->start_Exon();
  my $end_exon = $self->end_Exon();
545

Arne Stabenau's avatar
Arne Stabenau committed
546 547
  if ( exists $href_exons->{$start_exon} ) {
    $self->start_Exon($href_exons->{$start_exon});
548 549
  } else {
    # do nothing, the start exon wasnt mapped
550 551
  }

Arne Stabenau's avatar
Arne Stabenau committed
552 553
  if ( exists $href_exons->{$end_exon} ) {
    $self->end_Exon($href_exons->{$end_exon});
554 555
  } else { 
    # do nothing, the end exon wasnt mapped
556 557 558
  }
}

559 560 561

=head2 get_all_DBEntries

562 563 564 565 566 567 568 569 570 571 572 573 574 575
  Arg [1]    : (optional) String, external database name

  Arg [2]    : (optional) String, external_db type

  Example    : @dbentries = @{ $translation->get_all_DBEntries() };

  Description: Retrieves DBEntries (xrefs) for this translation.

               This method will attempt to lazy-load DBEntries
               from a database if an adaptor is available and no
               DBEntries are present on the translation (i.e. they
               have not already been added or loaded).

  Returntype : Listref to Bio::EnsEMBL::DBEntry objects
576
  Exceptions : none
577
  Caller     : TranslationAdaptor::store
578
  Status     : Stable
579 580 581 582

=cut

sub get_all_DBEntries {
583
  my ( $self, $ex_db_exp, $ex_db_type ) = @_;
584

585
  my $cache_name = 'dbentries';
586

587
  if ( defined($ex_db_exp) ) {
588 589
    $cache_name .= $ex_db_exp;
  }
590 591

  if ( defined($ex_db_type) ) {
Ian Longden's avatar
Ian Longden committed
592
    $cache_name .= $ex_db_type;
593
  }
594

595 596 597
  # if not cached, retrieve all of the xrefs for this translation
  if ( !defined( $self->{$cache_name} ) && defined( $self->adaptor() ) )
  {
598
    $self->{$cache_name} =
599 600
      $self->adaptor()->db()->get_DBEntryAdaptor()
      ->fetch_all_by_Translation( $self, $ex_db_exp, $ex_db_type );
601 602
  }

603
  $self->{$cache_name} ||= [];
604

605
  return $self->{$cache_name};
606
} ## end sub get_all_DBEntries
607

608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
=head2 get_all_object_xrefs

  Arg [1]    : (optional) String, external database name

  Arg [2]    : (optional) String, external_db type

  Example    : @oxrefs = @{ $translation->get_all_object_xrefs() };

  Description: Retrieves xrefs for this translation.

               This method will attempt to lazy-load xrefs from a
               database if an adaptor is available and no xrefs
               are present on the translation (i.e. they have not
               already been added or loaded).

623 624
                NB: This method is an alias for the
                    get_all_DBentries() method.
625 626 627 628 629 630 631

  Return type: Listref of Bio::EnsEMBL::DBEntry objects

  Status     : Stable

=cut

632 633 634 635
sub get_all_object_xrefs {
      my $self = shift;
            return $self->get_all_DBEntries(@_);
}
636 637 638 639 640

=head2 add_DBEntry

  Arg [1]    : Bio::EnsEMBL::DBEntry $dbe
               The dbEntry to be added
Patrick Meidl's avatar
Patrick Meidl committed
641 642 643 644
  Example    : $translation->add_DBEntry($xref);
  Description: Associates a DBEntry with this translation. Note that adding
               DBEntries will prevent future lazy-loading of DBEntries for this
               translation (see get_all_DBEntries).
645 646 647
  Returntype : none
  Exceptions : thrown on incorrect argument type
  Caller     : general
648
  Status     : Stable
649 650 651 652 653 654 655 656

=cut

sub add_DBEntry {
  my $self = shift;
  my $dbe = shift;

  unless($dbe && ref($dbe) && $dbe->isa('Bio::EnsEMBL::DBEntry')) {
657
    throw('Expected DBEntry argument');
658 659 660 661 662 663 664
  }

  $self->{'dbentries'} ||= [];
  push @{$self->{'dbentries'}}, $dbe;
}


Stephen Keenan's avatar
Stephen Keenan committed
665 666
=head2 get_all_DBLinks

667 668 669 670 671 672 673 674 675 676 677 678
  Arg [1]    : String database name (optional)
               SQL wildcard characters (_ and %) can be used to
               specify patterns.

  Example    :  my @dblinks = @{ $translation->get_all_DBLinks() };
                my @dblinks = @{ $translation->get_all_DBLinks('Uniprot%') };

  Description: This is here for consistancy with the Transcript
               and Gene classes.  It is a synonym for the
               get_all_DBEntries() method.

  Return type: Listref to Bio::EnsEMBL::DBEntry objects
679 680
  Exceptions : none
  Caller     : general
681
  Status     : Stable
Stephen Keenan's avatar
Stephen Keenan committed
682 683 684 685 686

=cut

sub get_all_DBLinks {
  my $self = shift;
687
  return $self->get_all_DBEntries(@_);
Stephen Keenan's avatar
Stephen Keenan committed
688 689
}

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
=head2 get_all_xrefs

  Arg [1]    : String database name (optional)
               SQL wildcard characters (_ and %) can be used to
               specify patterns.

  Example    : @xrefs = @{ $translation->get_all_xrefs() };
               @xrefs = @{ $translation->get_all_xrefs('Uniprot%') };

  Description: This method is here for consistancy with the Gene
               and Transcript classes.  It is an alias for the
               get_all_DBLinks() method, which in turn directly
               calls get_all_DBEntries().

  Return type: Listref of Bio::EnsEMBL::DBEntry objects

  Status     : Stable

=cut

710 711 712 713
sub get_all_xrefs {
  my $self = shift;
  return $self->get_all_DBLinks(@_);
}
714

715 716 717 718 719 720 721 722 723 724
=head2 get_all_ProteinFeatures

  Arg [1]    : (optional) string $logic_name
               The analysis logic_name of the features to retrieve.  If not
               specified, all features are retrieved instead.
  Example    : $features = $self->get_all_ProteinFeatures('PFam');
  Description: Retrieves all ProteinFeatures associated with this 
               Translation. If a logic_name is specified, only features with 
               that logic_name are returned.  If no logic_name is provided all
               associated protein_features are returned.
725 726 727

               ProteinFeatures are lazy-loaded from the database unless they
               added manually to the Translation or had already been loaded.
728 729 730
  Returntype : Bio::EnsEMBL::ProteinFeature
  Exceptions : none
  Caller     : general
731
  Status     : Stable
732 733 734 735 736 737 738 739 740 741

=cut

sub get_all_ProteinFeatures {
  my $self = shift;
  my $logic_name = shift;

  if(!$self->{'protein_features'}) {
    my $adaptor = $self->adaptor();
    my $dbID    = $self->dbID();
742 743

    return [] if (!$adaptor || !$dbID);
744 745 746 747 748 749 750 751 752

    my %hash;
    $self->{'protein_features'} = \%hash;

    my $pfa = $adaptor->db()->get_ProteinFeatureAdaptor();
    my $name;
    foreach my $f (@{$pfa->fetch_all_by_translation_id($dbID)}) {
      my $analysis = $f->analysis();
      if($analysis) {
753
        $name = lc($f->analysis->logic_name());
754
      } else {
755 756
        warning("ProteinFeature has no attached analysis\n");
        $name = '';
757 758 759 760 761 762
      }
      $hash{$name} ||= [];
      push @{$hash{$name}}, $f;
    }
  }

Patrick Meidl's avatar
Patrick Meidl committed
763
  # a specific type of protein feature was requested
764 765 766 767 768
  if(defined($logic_name)) {
    $logic_name = lc($logic_name);
    return $self->{'protein_features'}->{$logic_name} || [];
  }

769
  my @features = ();
770

Patrick Meidl's avatar
Patrick Meidl committed
771
  # all protein features were requested
772 773 774 775 776 777 778 779 780 781 782 783 784 785
  foreach my $type (keys %{$self->{'protein_features'}}) {
    push @features, @{$self->{'protein_features'}->{$type}};
  }

  return \@features;    
}


=head2 get_all_DomainFeatures

  Example    : @domain_feats = @{$translation->get_all_DomainFeatures};
  Description: A convenience method which retrieves all protein features
               that are considered to be 'Domain' features.  Features which
               are 'domain' features are those with analysis logic names:
Patrick Meidl's avatar
Patrick Meidl committed
786 787
               'pfscan', 'scanprosite', 'superfamily', 'pfam', 'prints',
               'smart', 'pirsf', 'tigrfam'.
788 789 790
  Returntype : listref of Bio::EnsEMBL::ProteinFeatures
  Exceptions : none
  Caller     : webcode (protview)
791
  Status     : Stable
792 793 794 795 796 797 798 799

=cut

sub get_all_DomainFeatures{
 my ($self) = @_;

 my @features;

800 801 802
 my @types = ('pfscan',      #profile (prosite or pfam motifs)
              'scanprosite', #prosite
              'superfamily',
803
              'pfam',
804 805 806
              'smart',
              'tigrfam',
              'pirsf',
807 808 809 810 811 812 813 814 815
              'prints');

 foreach my $type (@types) {
   push @features, @{$self->get_all_ProteinFeatures($type)};
 }

 return \@features;
}

816

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
=head2 add_ProteinFeature

  Arg [1]    : Bio::EnsEMBL::ProteinFeature $pf
               The ProteinFeature to be added
  Example    : $translation->add_ProteinFeature($pf);
  Description: Associates a ProteinFeature with this translation. Note that
               adding ProteinFeatures will prevent future lazy-loading of
               ProteinFeatures for this translation (see
               get_all_ProteinFeatures).
  Returntype : none
  Exceptions : thrown on incorrect argument type
  Caller     : general
  Status     : Stable

=cut

sub add_ProteinFeature {
  my $self = shift;
  my $pf = shift;

  unless ($pf && ref($pf) && $pf->isa('Bio::EnsEMBL::ProteinFeature')) {
    throw('Expected ProteinFeature argument');
  }

  my $analysis = $pf->analysis;
  throw("ProteinFeature has no attached Analysis.") unless $analysis;

  push @{ $self->{'protein_features'}->{$analysis->logic_name} }, $pf;
}


848 849 850 851
=head2 display_id

  Example    : print $translation->display_id();
  Description: This method returns a string that is considered to be
852 853 854
               the 'display' identifier. For translations this is (depending on
               availability and in this order) the stable Id, the dbID or an
               empty string.
855 856 857
  Returntype : string
  Exceptions : none
  Caller     : web drawing code
858
  Status     : Stable
859 860 861 862 863

=cut

sub display_id {
  my $self = shift;
864
  return $self->{'stable_id'} || $self->dbID || '';
865 866 867
}


868 869 870 871 872 873 874 875
=head2 length

  Example    : print "Peptide length =", $translation->length();
  Description: Retrieves the length of the peptide sequence (i.e. number of
               amino acids) represented by this Translation object.
  Returntype : int
  Exceptions : none
  Caller     : webcode (protview etc.)
876
  Status     : Stable
877 878 879

=cut

Web Admin's avatar
Web Admin committed
880
sub length {
881 882
  my $self = shift;
  my $seq = $self->seq();
Web Admin's avatar
testing  
Web Admin committed
883

884
  return ($seq) ? CORE::length($seq) : 0;
885 886 887 888 889 890 891 892 893
}


=head2 seq

  Example    : print $translation->seq();
  Description: Retrieves a string representation of the peptide sequence
               of this Translation.  This retrieves the transcript from the
               database and gets its sequence, or retrieves the sequence which
Patrick Meidl's avatar
Patrick Meidl committed
894
               was set via the constructor.
895 896 897 898
  Returntype : string
  Exceptions : warning if the sequence is not set and cannot be retrieved from
               the database.
  Caller     : webcode (protview etc.)
899
  Status     : Stable
900 901 902 903

=cut

sub seq {
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
904
  my ( $self, $sequence ) = @_;
905

Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
906
  if ( defined($sequence) ) {
907

Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
908
    $self->{'seq'} = $sequence;
909

Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
910
  } elsif ( !defined( $self->{'seq'} ) ) {
911

912
    my $transcript = $self->transcript();
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
913

914
    my $canonical_translation = $transcript->translation();
915
    my $is_alternative;
916 917 918
    if(!$canonical_translation) {
	throw "Transcript does not have a canonical translation";
    }
919 920 921
    if (    defined( $canonical_translation->stable_id() )
         && defined( $self->stable_id() ) )
    {
922
      # Try stable ID.
923 924
      $is_alternative =
        ( $canonical_translation->stable_id() ne $self->stable_id() );
925 926 927 928
    } elsif (    defined( $canonical_translation->dbID() )
              && defined( $self->dbID() ) )
    {
      # Try dbID.
929 930
      $is_alternative =
        ( $canonical_translation->dbID() != $self->dbID() );
931 932 933 934 935 936
    } else {
      # Resort to using geomic start/end coordinates.
      $is_alternative = ( ($canonical_translation->genomic_start() !=
                             $self->genomic_start() )
                           || ( $canonical_translation->genomic_end() !=
                                $self->genomic_end() ) );
937
    }
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
938

939 940 941 942
    if ($is_alternative) {
      # To deal with non-canonical (alternative) translations, subsitute
      # the canonical translation in the transcript with $self for a
      # while.
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
943

944 945 946 947
      $transcript->translation($self);
    }

    my $seq = $transcript->translate();
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
948 949 950
    if ( defined($seq) ) {
      $self->{'seq'} = $seq->seq();
    }
951

952 953 954 955 956 957 958
    if ($is_alternative) {
      # Reinstate the real canonical translation.

      $transcript->translation($canonical_translation);
    }

  } ## end elsif ( !defined( $self->...))
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
959 960 961

  if ( !defined( $self->{'seq'} ) ) {
    return '';    # Empty string
962
  }
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
963 964 965 966

  return $self->{'seq'};

} ## end sub seq
967

Patrick Meidl's avatar
Patrick Meidl committed
968

969 970 971 972
=head2 get_all_Attributes

  Arg [1]    : optional string $attrib_code
               The code of the attribute type to retrieve values for.
973
  Example    : ($sc_attr) = @{$tl->get_all_Attributes('_selenocysteine')};
974
               @tl_attributes = @{$translation->get_all_Attributes()};
975 976
  Description: Gets a list of Attributes of this translation.
               Optionally just get Attrubutes for given code.
977
               Recognized attribute "_selenocysteine"
978 979 980 981
  Returntype : listref Bio::EnsEMBL::Attribute
  Exceptions : warning if translation does not have attached adaptor and 
               attempts lazy load.
  Caller     : general, modify_translation
982
  Status     : Stable
983 984 985 986 987 988 989 990 991 992 993 994 995

=cut

sub get_all_Attributes {
  my $self = shift;
  my $attrib_code = shift;

  if( ! exists $self->{'attributes' } ) {
    if(!$self->adaptor() ) {
#      warning('Cannot get attributes without an adaptor.');
      return [];
    }

996 997
    my $aa = $self->adaptor->db->get_AttributeAdaptor();
    $self->{'attributes'} = $aa->fetch_all_by_Translation( $self );
998 999 1000
  }

  if( defined $attrib_code ) {
1001
    my @results = grep { uc($_->code()) eq uc($attrib_code) }
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    @{$self->{'attributes'}};
    return \@results;
  } else {
    return $self->{'attributes'};
  }
}


=head2 add_Attributes

Patrick Meidl's avatar
Patrick Meidl committed
1012 1013
  Arg [1..N] : Bio::EnsEMBL::Attribute $attribute
               Attributes to add.
1014
  Example    : $translation->add_Attributes($selenocysteine_attribute);
1015
  Description: Adds an Attribute to the Translation. Usefull to 
1016
               do _selenocysteine.
1017 1018
               If you add an attribute before you retrieve any from database, 
               lazy load will be disabled.
1019
  Returntype : none
1020
  Exceptions : throw on incorrect arguments
1021
  Caller     : general
1022
  Status     : Stable
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035

=cut

sub add_Attributes {
  my $self = shift;
  my @attribs = @_;

  if( ! exists $self->{'attributes'} ) {
    $self->{'attributes'} = [];
  }

  for my $attrib ( @attribs ) {
    if( ! $attrib->isa( "Bio::EnsEMBL::Attribute" )) {
1036
      throw( "Argument to add_Attribute must be a Bio::EnsEMBL::Attribute" );
1037 1038
    }
    push( @{$self->{'attributes'}}, $attrib );
1039
    $self->{seq}=undef;
1040 1041 1042
  }
}

Patrick Meidl's avatar
Patrick Meidl committed
1043

1044
=head2 get_all_SeqEdits
1045

1046 1047 1048 1049 1050 1051
  Example    : my @seqeds = @{$transcript->get_all_SeqEdits()};
  Description: Retrieves all post transcriptional sequence modifications for
               this transcript.
  Returntype : Bio::EnsEMBL::SeqEdit
  Exceptions : none
  Caller     : spliced_seq()
1052
  Status     : Stable
1053 1054 1055

=cut

1056
sub get_all_SeqEdits {
1057 1058
  my $self = shift;

1059
  my @seqeds;