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

3
  Copyright (c) 1999-2010 The European Bioinformatics Institute and
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  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
  developers list at <ensembl-dev@ebi.ac.uk>.

  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;

55 56
use Bio::EnsEMBL::Utils::Exception qw( deprecate throw warning );
use Bio::EnsEMBL::Utils::Argument qw( rearrange );
Ewan Birney's avatar
Ewan Birney committed
57

58
use Bio::EnsEMBL::Storable;
Ewan Birney's avatar
Ewan Birney committed
59

60
@ISA = qw(Bio::EnsEMBL::Storable);
Ewan Birney's avatar
Ewan Birney committed
61 62


63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
=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.
78 79
  Arg [-CREATED_DATE]: the date the translation was created
  Arg [-MODIFIED_DATE]: the date the translation was modified
80 81 82 83 84 85 86 87 88
  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
89
  Status     : Stable
90 91 92

=cut

93
sub new {
Graham McVicker's avatar
Graham McVicker committed
94
  my $caller = shift;
95

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

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

  my $self = bless {
		    'start_exon' => $start_exon,
107 108 109 110 111 112 113
		    'end_exon'   => $end_exon,
		    'adaptor'    => $adaptor,
		    'dbID'       => $dbID,
		    'start'      => $seq_start,
		    'end'        => $seq_end,
		    'stable_id'  => $stable_id,
		    'version'    => $version,
114 115
		    'created_date' => $created_date,
		    'modified_date' => $modified_date,
116
        'seq'        => $seq
117 118 119
		   }, $class;

  return $self;
Ewan Birney's avatar
Ewan Birney committed
120 121
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
=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;
  return bless $hashref, $class;
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
sub transcript {
  my ( $self, $transcript ) = @_;

  if ( defined($transcript) ) {

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

  } elsif ( !defined( $self->{'transcript'} ) ) {
    my $adaptor = $self->{'adaptor'};
    if ( !defined($adaptor) ) {
      throw("Adaptor is not set.");
    }

    my $dbID = $self->{'dbID'};
    if ( !defined($dbID) ) {
      throw("dbID is not set.");
    }

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

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

Ewan Birney's avatar
Ewan Birney committed
166 167 168

=head2 start

Patrick Meidl's avatar
Patrick Meidl committed
169 170 171 172 173
  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.

174 175
               If you need genomic coordinates, use the genomic_start()
               method.
Patrick Meidl's avatar
Patrick Meidl committed
176 177 178 179
  Returntype : int
  Exceptions : none
  Caller     : general
  Status     : Stable
Ewan Birney's avatar
Ewan Birney committed
180 181 182 183 184 185 186

=cut

sub start{
   my $obj = shift;
   if( @_ ) {
      my $value = shift;
Laura Clarke's avatar
 
Laura Clarke committed
187
      
Ewan Birney's avatar
Ewan Birney committed
188 189 190 191 192 193 194
      $obj->{'start'} = $value;
    }
    return $obj->{'start'};

}


195 196
=head2 end

Patrick Meidl's avatar
Patrick Meidl committed
197 198 199 200
  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.
201

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

=cut

sub end {
   my $self = shift;
   if( @_ ) {
      my $value = shift;
Laura Clarke's avatar
 
Laura Clarke committed
215
      
216 217 218 219 220 221 222
      $self->{'end'} = $value;
    }
    return $self->{'end'};

}


Graham McVicker's avatar
Graham McVicker committed
223
=head2 start_Exon
224

Patrick Meidl's avatar
Patrick Meidl committed
225 226 227 228 229 230 231 232 233
  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
234 235 236

=cut

Graham McVicker's avatar
Graham McVicker committed
237
sub start_Exon {
238
   my $self = shift;
239

Ewan Birney's avatar
Ewan Birney committed
240 241
   if( @_ ) {
      my $value = shift;
242
      if( !ref $value || !$value->isa('Bio::EnsEMBL::Exon') ) {
243
         throw("Got to have an Exon object, not a $value");
244 245
      }
      $self->{'start_exon'} = $value;
Ewan Birney's avatar
Ewan Birney committed
246
    }
247
   return $self->{'start_exon'};
Ewan Birney's avatar
Ewan Birney committed
248 249 250
}


Graham McVicker's avatar
Graham McVicker committed
251
=head2 end_Exon
252

Patrick Meidl's avatar
Patrick Meidl committed
253 254 255 256 257 258 259 260 261
  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
262 263 264

=cut

Graham McVicker's avatar
Graham McVicker committed
265
sub end_Exon {
266
   my $self = shift;
Ewan Birney's avatar
Ewan Birney committed
267 268
   if( @_ ) {
      my $value = shift;
269
      if( !ref $value || !$value->isa('Bio::EnsEMBL::Exon') ) {
270
         throw("Got to have an Exon object, not a $value");
271 272
      }
      $self->{'end_exon'} = $value;
273
    } 
Ewan Birney's avatar
Ewan Birney committed
274

275
    return $self->{'end_exon'};
Ewan Birney's avatar
Ewan Birney committed
276 277
}

278 279
=head2 cdna_start

Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
280 281
    Arg  [1]    : (optional) Bio::EnsEMBL::Transcript $transcript
                  The transcript which this is a translation of.
282 283 284
    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
285 286 287
                  If no transcript is given, the method will use
                  TranscriptAdaptor->fetch_by_translation_id() to locate
                  the correct transcript.
288 289 290 291 292 293 294 295 296 297 298 299 300 301
    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 {
    my $self = shift;
    my ($transcript) = @_;

    if ( defined $transcript
         && (    !ref $transcript
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
302 303
              || !$transcript->isa('Bio::EnsEMBL::Transcript') ) )
    {
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
        throw("Argument is not a transcript");
    }

    if ( !exists $self->{'cdna_start'} ) {
        if ( !defined $transcript ) {
            # We were not given a transcript, get the transcript out of
            # the database.

            my $transcript_adaptor =
              $self->adaptor()->db()->get_TranscriptAdaptor();

            $transcript =
              $transcript_adaptor->fetch_by_translation_id(
                                                        $self->dbID() );
        }

        $self->{'cdna_start'} =
          $self->start_Exon()->cdna_coding_start($transcript);
    }

    return $self->{'cdna_start'};
} ## end sub cdna_start

=head2 cdna_end

Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
329 330
    Arg  [1]    : (optional) Bio::EnsEMBL::Transcript $transcript
                  The transcript which this is a translation of.
331 332 333
    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
334 335 336
                  If no transcript is given, the method will use
                  TranscriptAdaptor->fetch_by_translation_id() to locate
                  the correct transcript.
337 338 339 340 341 342 343 344 345 346 347 348 349 350
    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 {
    my $self = shift;
    my ($transcript) = @_;

    if ( defined $transcript
         && (    !ref $transcript
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
351 352
              || !$transcript->isa('Bio::EnsEMBL::Transcript') ) )
    {
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 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
        throw("Argument is not a transcript");
    }

    if ( !exists $self->{'cdna_end'} ) {
        if ( !defined $transcript ) {
            # We were not given a transcript, get the transcript out of
            # the database.

            my $transcript_adaptor =
              $self->adaptor()->db()->get_TranscriptAdaptor();

            $transcript =
              $transcript_adaptor->fetch_by_translation_id(
                                                        $self->dbID() );
        }

        $self->{'cdna_end'} =
          $self->end_Exon()->cdna_coding_end($transcript);
    }

    return $self->{'cdna_end'};
} ## end sub cdna_end

=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
434

435
=head2 version
436

Patrick Meidl's avatar
Patrick Meidl committed
437 438 439
  Arg [1]    : (optional) string $version - version to set
  Example    : $translation->version(2);
  Description: Getter/setter for attribute version
440 441 442
  Returntype : string
  Exceptions : none
  Caller     : general
443
  Status     : Stable
Ewan Birney's avatar
Ewan Birney committed
444 445 446

=cut

447
sub version {
448
   my $self = shift;
449 450
  $self->{'version'} = shift if( @_ );
  return $self->{'version'};
Ewan Birney's avatar
Ewan Birney committed
451
}
452 453


454
=head2 stable_id
Graham McVicker's avatar
Graham McVicker committed
455

Patrick Meidl's avatar
Patrick Meidl committed
456 457 458
  Arg [1]    : (optional) string $stable_id - stable ID to set
  Example    : $translation->stable_id('ENSP0059890');
  Description: Getter/setter for attribute stable_id
459 460 461
  Returntype : string
  Exceptions : none
  Caller     : general
462
  Status     : Stable
463 464 465

=cut

466
sub stable_id {
467
   my $self = shift;
468 469
  $self->{'stable_id'} = shift if( @_ );
  return $self->{'stable_id'};
470 471
}

472 473
=head2 created_date

Patrick Meidl's avatar
Patrick Meidl committed
474 475 476
  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
477 478 479 480 481 482 483
  Returntype : string
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut 

484 485 486 487 488 489 490
sub created_date {
  my $self = shift;
  $self->{'created_date'} = shift if ( @_ );
  return $self->{'created_date'};
}


491 492
=head2 modified_date

Patrick Meidl's avatar
Patrick Meidl committed
493 494 495
  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
496 497 498 499 500 501 502
  Returntype : string
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut 

503 504 505 506 507 508
sub modified_date {
  my $self = shift;
  $self->{'modified_date'} = shift if ( @_ );
  return $self->{'modified_date'};
}

509

Graham McVicker's avatar
Graham McVicker committed
510

511 512
=head2 transform

Patrick Meidl's avatar
Patrick Meidl committed
513 514 515 516
  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.
517
  Returntype: none
Patrick Meidl's avatar
Patrick Meidl committed
518 519
  Exceptions : none
  Caller     : Transcript->transform() 
520
  Status     : Stable
521 522 523 524 525 526 527

=cut

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

Graham McVicker's avatar
Graham McVicker committed
528 529
  my $start_exon = $self->start_Exon();
  my $end_exon = $self->end_Exon();
530

Arne Stabenau's avatar
Arne Stabenau committed
531 532
  if ( exists $href_exons->{$start_exon} ) {
    $self->start_Exon($href_exons->{$start_exon});
533 534
  } else {
    # do nothing, the start exon wasnt mapped
535 536
  }

Arne Stabenau's avatar
Arne Stabenau committed
537 538
  if ( exists $href_exons->{$end_exon} ) {
    $self->end_Exon($href_exons->{$end_exon});
539 540
  } else { 
    # do nothing, the end exon wasnt mapped
541 542 543
  }
}

544 545 546

=head2 get_all_DBEntries

Patrick Meidl's avatar
Patrick Meidl committed
547 548
  Arg [1]    : (optional) $ex_db_exp - external db name
  Example    : @dbentries = @{$translation->get_all_DBEntries()};
549 550 551 552 553 554 555 556 557
  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 : list reference to Bio::EnsEMBL::DBEntry objects
  Exceptions : none
  Caller     : get_all_DBLinks, TranslationAdaptor::store
558
  Status     : Stable
559 560 561 562 563

=cut

sub get_all_DBEntries {
  my $self = shift;
564
  my $ex_db_exp = shift;
Ian Longden's avatar
Ian Longden committed
565
  my $ex_db_type = shift;
566 567 568 569 570 571

  my $cache_name = "dbentries";

  if(defined($ex_db_exp)){
    $cache_name .= $ex_db_exp;
  }
Ian Longden's avatar
Ian Longden committed
572 573
  if(defined($ex_db_type)){
    $cache_name .= $ex_db_type;
574
  }
575

Patrick Meidl's avatar
Patrick Meidl committed
576
  # if not cached, retrieve all of the xrefs for this gene
577
  if(!defined $self->{$cache_name}) {
578 579
    my $adaptor = $self->adaptor();
    my $dbID    = $self->dbID();
Graham McVicker's avatar
Graham McVicker committed
580 581

    return [] if(!$adaptor || !$dbID);
582
    $self->{$cache_name} =
Ian Longden's avatar
Ian Longden committed
583
      $self->adaptor->db->get_DBEntryAdaptor->fetch_all_by_Translation($self, $ex_db_exp, $ex_db_type);
584 585
  }

586
  $self->{$cache_name} ||= [];
587

588
  return $self->{$cache_name};
589 590 591 592 593 594 595
}


=head2 add_DBEntry

  Arg [1]    : Bio::EnsEMBL::DBEntry $dbe
               The dbEntry to be added
Patrick Meidl's avatar
Patrick Meidl committed
596 597 598 599
  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).
600 601 602
  Returntype : none
  Exceptions : thrown on incorrect argument type
  Caller     : general
603
  Status     : Stable
604 605 606 607 608 609 610 611

=cut

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

  unless($dbe && ref($dbe) && $dbe->isa('Bio::EnsEMBL::DBEntry')) {
612
    throw('Expected DBEntry argument');
613 614 615 616 617 618 619
  }

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


Stephen Keenan's avatar
Stephen Keenan committed
620 621
=head2 get_all_DBLinks

622 623 624
  Arg [1]    : see get_all_DBEntries
  Example    : see get_all_DBEntries
  Description: This is here for consistancy with the Transcript and Gene 
Patrick Meidl's avatar
Patrick Meidl committed
625
               classes. It is a synonym for the get_all_DBEntries method.
626 627 628
  Returntype : see get_all_DBEntries
  Exceptions : none
  Caller     : general
629
  Status     : Stable
Stephen Keenan's avatar
Stephen Keenan committed
630 631 632 633 634

=cut

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

638

639 640 641 642 643 644 645 646 647 648
=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.
649 650 651

               ProteinFeatures are lazy-loaded from the database unless they
               added manually to the Translation or had already been loaded.
652 653 654
  Returntype : Bio::EnsEMBL::ProteinFeature
  Exceptions : none
  Caller     : general
655
  Status     : Stable
656 657 658 659 660 661 662 663 664 665

=cut

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

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

    return [] if (!$adaptor || !$dbID);
668 669 670 671 672 673 674 675 676

    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) {
677
        $name = lc($f->analysis->logic_name());
678
      } else {
679 680
        warning("ProteinFeature has no attached analysis\n");
        $name = '';
681 682 683 684 685 686
      }
      $hash{$name} ||= [];
      push @{$hash{$name}}, $f;
    }
  }

Patrick Meidl's avatar
Patrick Meidl committed
687
  # a specific type of protein feature was requested
688 689 690 691 692
  if(defined($logic_name)) {
    $logic_name = lc($logic_name);
    return $self->{'protein_features'}->{$logic_name} || [];
  }

693
  my @features = ();
694

Patrick Meidl's avatar
Patrick Meidl committed
695
  # all protein features were requested
696 697 698 699 700 701 702 703 704 705 706 707 708 709
  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
710 711
               'pfscan', 'scanprosite', 'superfamily', 'pfam', 'prints',
               'smart', 'pirsf', 'tigrfam'.
712 713 714
  Returntype : listref of Bio::EnsEMBL::ProteinFeatures
  Exceptions : none
  Caller     : webcode (protview)
715
  Status     : Stable
716 717 718 719 720 721 722 723

=cut

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

 my @features;

724 725 726
 my @types = ('pfscan',      #profile (prosite or pfam motifs)
              'scanprosite', #prosite
              'superfamily',
727
              'pfam',
728 729 730
              'smart',
              'tigrfam',
              'pirsf',
731 732 733 734 735 736 737 738 739
              'prints');

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

 return \@features;
}

740

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
=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;
}


772 773 774 775
=head2 display_id

  Example    : print $translation->display_id();
  Description: This method returns a string that is considered to be
776 777 778
               the 'display' identifier. For translations this is (depending on
               availability and in this order) the stable Id, the dbID or an
               empty string.
779 780 781
  Returntype : string
  Exceptions : none
  Caller     : web drawing code
782
  Status     : Stable
783 784 785 786 787

=cut

sub display_id {
  my $self = shift;
788
  return $self->{'stable_id'} || $self->dbID || '';
789 790 791
}


792 793 794 795 796 797 798 799
=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.)
800
  Status     : Stable
801 802 803

=cut

Web Admin's avatar
Web Admin committed
804
sub length {
805 806
  my $self = shift;
  my $seq = $self->seq();
Web Admin's avatar
testing  
Web Admin committed
807

808
  return ($seq) ? CORE::length($seq) : 0;
809 810 811 812 813 814 815 816 817
}


=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
818
               was set via the constructor.
819 820 821 822
  Returntype : string
  Exceptions : warning if the sequence is not set and cannot be retrieved from
               the database.
  Caller     : webcode (protview etc.)
823
  Status     : Stable
824 825 826 827

=cut

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

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

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

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

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

838 839 840
    my $canonical_translation = $transcript->translation();
    my $is_alternative =
      ( $canonical_translation->stable_id() ne $self->stable_id() );
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
841

842 843 844 845
    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
846

847 848 849 850
      $transcript->translation($self);
    }

    my $seq = $transcript->translate();
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
851 852 853
    if ( defined($seq) ) {
      $self->{'seq'} = $seq->seq();
    }
854

855 856 857 858 859 860 861
    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
862 863 864

  if ( !defined( $self->{'seq'} ) ) {
    return '';    # Empty string
865
  }
Andreas Kusalananda Kähäri's avatar
Andreas Kusalananda Kähäri committed
866 867 868 869

  return $self->{'seq'};

} ## end sub seq
870

Patrick Meidl's avatar
Patrick Meidl committed
871

872 873 874 875
=head2 get_all_Attributes

  Arg [1]    : optional string $attrib_code
               The code of the attribute type to retrieve values for.
876
  Example    : ($sc_attr) = @{$tl->get_all_Attributes('_selenocysteine')};
877
               @tl_attributes = @{$translation->get_all_Attributes()};
878 879
  Description: Gets a list of Attributes of this translation.
               Optionally just get Attrubutes for given code.
880
               Recognized attribute "_selenocysteine"
881 882 883 884
  Returntype : listref Bio::EnsEMBL::Attribute
  Exceptions : warning if translation does not have attached adaptor and 
               attempts lazy load.
  Caller     : general, modify_translation
885
  Status     : Stable
886 887 888 889 890 891 892 893 894 895 896 897 898

=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 [];
    }

899 900
    my $aa = $self->adaptor->db->get_AttributeAdaptor();
    $self->{'attributes'} = $aa->fetch_all_by_Translation( $self );
901 902 903
  }

  if( defined $attrib_code ) {
904
    my @results = grep { uc($_->code()) eq uc($attrib_code) }
905 906 907 908 909 910 911 912 913 914
    @{$self->{'attributes'}};
    return \@results;
  } else {
    return $self->{'attributes'};
  }
}


=head2 add_Attributes

Patrick Meidl's avatar
Patrick Meidl committed
915 916
  Arg [1..N] : Bio::EnsEMBL::Attribute $attribute
               Attributes to add.
917
  Example    : $translation->add_Attributes($selenocysteine_attribute);
918
  Description: Adds an Attribute to the Translation. Usefull to 
919
               do _selenocysteine.
920 921
               If you add an attribute before you retrieve any from database, 
               lazy load will be disabled.
922
  Returntype : none
923
  Exceptions : throw on incorrect arguments
924
  Caller     : general
925
  Status     : Stable
926 927 928 929 930 931 932 933 934 935 936 937 938

=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" )) {
939
      throw( "Argument to add_Attribute must be a Bio::EnsEMBL::Attribute" );
940 941
    }
    push( @{$self->{'attributes'}}, $attrib );
942
    $self->{seq}=undef;
943 944 945
  }
}

Patrick Meidl's avatar
Patrick Meidl committed
946

947
=head2 get_all_SeqEdits
948

949 950 951 952 953 954
  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()
955
  Status     : Stable
956 957 958

=cut

959
sub get_all_SeqEdits {
960 961
  my $self = shift;

962
  my @seqeds;
963

964 965
  my $attribs;
  
966
  my @edits = ('initial_met', '_selenocysteine', 'amino_acid_sub');
967 968 969 970
  

  foreach my $edit(@edits){
    $attribs = $self->get_all_Attributes($edit);
971

972 973 974 975
    # convert attributes to SeqEdit objects
    foreach my $a (@$attribs) {
      push @seqeds, Bio::EnsEMBL::SeqEdit->new(-ATTRIB => $a);
    }
976 977 978
  }
  return \@seqeds;
}
Patrick Meidl's avatar
Patrick Meidl committed
979
  
980 981 982

=head2 modify_translation

Patrick Meidl's avatar
Patrick Meidl committed
983
  Arg [1]    : Bio::Seq $peptide 
984 985
  Example    : my $seq = Bio::Seq->new(-SEQ => $dna)->translate();
               $translation->modify_translation($seq);
986
  Description: Applies sequence edits such as selenocysteines to the Bio::Seq 
987
               peptide thats passed in
988
  Returntype : Bio::Seq
Patrick Meidl's avatar
Patrick Meidl committed
989
  Exceptions : none
990
  Caller     : Bio::EnsEMBL::Transcript->translate
991
  Status     : Stable
992 993 994 995

=cut

sub modify_translation {
996
  my ( $self, $seq ) = @_;
997

998
  my @seqeds = @{ $self->get_all_SeqEdits() };
999

1000 1001 1002
  # Sort in reverse order to avoid complication of adjusting
  # downstream edits.
  @seqeds = sort { $b->start() <=> $a->start() } @seqeds;
1003

1004
  # Apply all edits.
1005 1006
  my $peptide = $seq->seq();
  foreach my $se (@seqeds) {
1007
    $se->apply_edit( \$peptide );
1008
  }
1009

1010
  $seq->seq($peptide);
1011 1012 1013 1014 1015

  return $seq;
}


1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
=head2 temporary_id

  Description: DEPRECATED This method should not be needed. Use dbID,
               stable_id or something else.

=cut

sub temporary_id {
   my $self = shift;
   deprecate( "I cant see what a temporary_id is good for, please use " .
               "dbID or stableID or\n try without an id." );
  $self->{'temporary_id'} = shift if( @_ );
  return $self->{'temporary_id'};
}


1032 1033 1034
=head2 get_all_DASFactories

  Function  : Retrieves a listref of registered DAS objects
Patrick Meidl's avatar
Patrick Meidl committed
1035 1036
  Returntype: DAS objects
  Exceptions: none
1037
  Caller    : webcode
Patrick Meidl's avatar
Patrick Meidl committed
1038 1039
  Example   : $dasref = $prot->get_all_DASFactories;
  Status    : Stable
1040 1041 1042 1043 1044 1045 1046 1047 1048

=cut

sub get_all_DASFactories {
   my $self = shift;
   return [ $self->adaptor()->db()->_each_DASFeatureFactory ];
}


1049 1050 1051 1052 1053 1054 1055 1056 1057
=head2 get_all_DAS_Features

  Example    : $features = $prot->get_all_DAS_Features;
  Description: Retreives a hash reference to a hash of DAS feature
               sets, keyed by the DNS, NOTE the values of this hash
               are an anonymous array containing:
                (1) a pointer to an array of features;
                (2) a pointer to the DAS stylesheet
  Returntype : hashref of Bio::SeqFeatures
Patrick Meidl's avatar
Patrick Meidl committed
1058
  Exceptions : none
1059
  Caller     : webcode
1060
  Status     : Stable
1061 1062 1063 1064

=cut

sub get_all_DAS_Features{
Patrick Meidl's avatar
Patrick Meidl committed
1065
  my $self = shift;
1066 1067 1068

  my $db = $self->adaptor->db;
  my $GeneAdaptor = $db->get_GeneAdaptor;
1069
  my $Gene = $GeneAdaptor->fetch_by_translation_stable_id($self->stable_id) || return;
1070
  my $slice = $Gene->feature_Slice;
1071 1072
 
  return $self->SUPER::get_all_DAS_Features($slice);
1073 1074
}

1075

1076
1;