RNAProduct.pm 25.8 KB
Newer Older
1 2
=head1 LICENSE

Marek Szuba's avatar
Marek Szuba committed
3
Copyright [2018-2019] EMBL-European Bioinformatics Institute
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 39 40

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut


=head1 CONTACT

  Please email comments or questions to the public Ensembl
  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.

  Questions may also be sent to the Ensembl help desk at
  <http://www.ensembl.org/Help/Contact>.

=cut

=head1 NAME

Bio::EnsEMBL::RNAProduct - A class representing the mature RNA product
of a transcript

=head1 DESCRIPTION

TODO

=head1 SYNOPSIS

41 42 43
  my $rnaproduct = Bio::EnsEMBL::RNAProduct->new(
    -SEQ_START => 36,
    -SEQ_END   => 58
44 45
  );

46 47 48 49 50 51 52
  # Stable-ID setter
  $rnaproduct->stable_id('ENSM00090210');

  # Get start and end position in the precursor transcript
  my $start = $rnaproduct->start();
  my $end = $rnaproduct->end();

53 54 55 56 57 58 59
=cut


package Bio::EnsEMBL::RNAProduct;

use vars qw($AUTOLOAD);
use strict;
60
use warnings;
61 62 63

use Bio::EnsEMBL::Utils::Exception qw(throw warning );
use Bio::EnsEMBL::Utils::Argument qw( rearrange );
64
use Bio::EnsEMBL::Utils::RNAProductTypeMapper;
65 66 67 68 69 70 71 72 73 74
use Bio::EnsEMBL::Utils::Scalar qw( assert_ref wrap_array );
use Scalar::Util qw(weaken);

use Bio::EnsEMBL::Storable;

use parent qw(Bio::EnsEMBL::Storable);


=head2 new

75
  Arg [-SEQ_START]    : The offset in the Transcript indicating the start
76
                        position of the product sequence.
77
  Arg [-SEQ_END]      : The offset in the Transcript indicating the end
78
                        position of the product sequence.
Marek Szuba's avatar
Marek Szuba committed
79 80
  Arg [-START_EXON]   : The Exon object in which the rnaproduct starts
  Arg [-END_EXON]     : The Exon object in which the rnaproduct ends
81 82 83
  Arg [-STABLE_ID]    : The stable identifier for this RNAPRoduct
  Arg [-VERSION]      : The version of the stable identifier
  Arg [-DBID]         : The internal identifier of this RNAProduct
84
  Arg [-ADAPTOR]      : The RNAProductAdaptor for this RNAProduct
85 86 87 88 89 90 91 92 93
  Arg [-SEQ]          : Manually sets the nucleotide sequence of this
                        rnaproduct. May be useful if this rnaproduct is not
                        stored in a database.
  Arg [-CREATED_DATE] : the date the rnaproduct was created
  Arg [-MODIFIED_DATE]: the date the rnaproduct was modified
  Example    : my $rp = Bio::EnsEMBL::RNAProduct->new(
                 -SEQ_START => 36,
                 -SEQ_END   => 58
               );
94 95 96 97 98 99 100 101
  Description: Constructor.  Creates a new RNAProduct object
  Returntype : Bio::EnsEMBL::RNAProduct
  Exceptions : none
  Caller     : general
  Status     : In Development

=cut

102
sub new { ## no critic (Subroutines::RequireArgUnpacking)
103 104 105 106
  my $caller = shift;

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

107 108
  my $type_code = Bio::EnsEMBL::Utils::RNAProductTypeMapper::mapper()
    ->class_to_type_code($class);
109

Marek Szuba's avatar
Marek Szuba committed
110 111 112 113 114
  my ($seq_start, $seq_end, $start_exon, $end_exon, $stable_id, $version, $dbID,
      $adaptor, $seq, $created_date, $modified_date ) =
	rearrange(["SEQ_START", "SEQ_END", "START_EXON", "END_EXON",
                   "STABLE_ID", "VERSION", "DBID", "ADAPTOR", "SEQ",
                   "CREATED_DATE", "MODIFIED_DATE"], @_);
115

116 117 118
  # For consistency between stable_id() and stable_id_version()
  $stable_id //= '';

119 120 121
  # Default version
  $version //= 1;

122
  my $self = bless {
123 124
    'start'      => $seq_start,
    'end'        => $seq_end,
Marek Szuba's avatar
Marek Szuba committed
125 126
    'start_exon' => $start_exon,
    'end_exon'   => $end_exon,
127 128 129 130 131
    'stable_id'  => $stable_id,
    'version'    => $version,
    'dbID'       => $dbID,
    'seq'        => $seq,
    'created_date' => $created_date,
132
    'modified_date' => $modified_date,
133
    'type_code'  => $type_code,
134 135 136
  }, $class;

  $self->adaptor($adaptor);
137 138 139 140 141

  return $self;
}


142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
=head2 add_Attributes

  Arg [1..N] : Bio::EnsEMBL::Attribute $attribute
               Attributes to add.
  Example    : $rnaproduct->add_Attributes($selenocysteine_attribute);
  Description: Adds an Attribute to the RNAProduct.
               If you add an attribute before you retrieve any from database,
               lazy load will be disabled.
  Returntype : none
  Exceptions : throw on incorrect arguments
  Caller     : general
  Status     : Stable

=cut

sub add_Attributes {
  my ($self, @attribs) = @_;

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

  for my $attrib (@attribs) {
    if (! $attrib->isa("Bio::EnsEMBL::Attribute")) {
      throw("Argument to add_Attribute must be a Bio::EnsEMBL::Attribute");
    }
    push (@{$self->{'attributes'}}, $attrib);
    # FIXME: why is this here???
    $self->{seq}=undef;
  }

  return;
}


177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
=head2 add_DBEntry

  Arg [1]    : Bio::EnsEMBL::DBEntry $dbe
               The dbEntry to be added
  Example    : $rnaproduct->add_DBEntry($xref);
  Description: Associates a DBEntry with this rnaproduct. Note that adding
               DBEntries will prevent future lazy-loading of DBEntries for this
               rnaproduct (see get_all_DBEntries).
  Returntype : none
  Exceptions : thrown on incorrect argument type
  Caller     : general
  Status     : Stable

=cut

sub add_DBEntry {
  my ($self, $dbe) = @_;

  if (!$dbe || !ref($dbe) || !$dbe->isa('Bio::EnsEMBL::DBEntry')) {
    throw('Expected DBEntry argument');
  }

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

  return;
203 204 205
}


206 207 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 241 242 243 244 245
=head2 cdna_end

    Example     : $rnaproduct_cdna_end = $rnaproduct->cdna_end();
    Description : Returns the end position of the rnaproduct in cDNA
                  coordinates.
                  Since rnaproducts do not span multiple exons, this is
                  simply an alias for end().
    Return type : Integer
    Caller      : General
    Status      : Stable

=cut

sub cdna_end {
  my $self = shift;

  return $self->end();
}


=head2 cdna_start

    Example     : $rnaproduct_cdna_start = $rnaproduct->cdna_start();
    Description : Returns the start position of the rnaproduct in cDNA
                  coordinates.
                  Since rnaproducts do not span multiple exons, this is
                  simply an alias for start().
    Return type : Integer
    Caller      : General
    Status      : Stable

=cut

sub cdna_start {
  my $self = shift;

  return $self->start();
}


246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
=head2 created_date

  Arg [1]    : (optional) string $created_date - created date to set
  Example    : $rnaproduct->created_date('2007-01-10 20:52:00');
  Description: Getter/setter for attribute created_date
  Returntype : string
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut

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


265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
=head2 display_id

  Example    : print $rnaproduct->display_id();
  Description: This method returns a string that is considered to be
               the 'display' identifier. For rnaproducts this is (depending on
               availability and in this order) the stable ID, the dbID or an
               empty string.
  Returntype : string
  Exceptions : none
  Caller     : web drawing code
  Status     : Stable

=cut

sub display_id {
  my $self = shift;
  return $self->stable_id() || $self->dbID() || '';
}


285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
=head2 end

  Arg [1]    : (optional) int $end - end position to set
  Example    : $rnaproduct->end(39);
  Description: Getter/setter for the value of end, which is a position within
               the precursor Transcript.
  Returntype : int
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut

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


Marek Szuba's avatar
Marek Szuba committed
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 330 331 332 333 334 335 336
=head2 end_Exon

  Arg [1]    : (optional) Bio::EnsEMBL::Exon || undef - start exon to assign
  Example    : $rnaproduct->end_Exon($exon1);
  Description: Getter/setter for the value of end_Exon, which denotes the
               exon at which rnaproduct ends.
  Returntype : Bio::EnsEMBL::Exon
  Exceptions : thrown on wrong argument type
  Caller     : general
  Status     : Stable

=cut

sub end_Exon {
  my ($self, $exon) = @_;

  if (defined($exon)) {

    # Normal setter
    assert_ref($exon, 'Bio::EnsEMBL::Exon');
    $self->{'end_exon'} = $exon;

  }
  elsif (@_ > 1) {
    # User has explicitly passed undef. Break connection to exon.
    delete( $self->{'end_exon'} );
  }

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


337 338 339 340 341 342 343 344 345
=head2 genomic_end

    Args        : None
    Example     : $rnaproduct_genomic_end = $rnaproduct->genomic_end();
    Description : Returns the end position of the rnaproduct in genomic
                  coordinates on the forward strand.
    Return type : Integer
    Exceptions  : None
    Caller      : General
346
    Status      : Stable
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 374 375 376 377

=cut

sub genomic_end {
  my $self = shift;

  if (!exists $self->{'genomic_end'}) {
    my $transcript = $self->transcript();

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

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


=head2 genomic_start

    Args        : None
    Example     : $rnaproduct_genomic_start = $rnaproduct->genomic_start();
    Description : Returns the start position of the rnaproduct in
                  genomic coordinates on the forward strand.
    Return type : Integer
    Exceptions  : None
    Caller      : General
378
    Status      : Stable
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

=cut

sub genomic_start {
  my $self = shift;

  if (!exists $self->{'genomic_start'}) {
    my $transcript = $self->transcript();

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

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


401 402 403 404 405 406 407 408 409
=head2 get_all_Attributes

  Arg [1]    : optional string $attrib_code
               The code of the attribute type to retrieve values for.
  Example    : ($n_attr) = @{$tl->get_all_Attributes('note')};
               @rp_attributes = @{$rnaproduct->get_all_Attributes()};
  Description: Gets a list of Attributes of this rnaproduct.
               Optionally just get Attributes for given code.
  Returntype : listref Bio::EnsEMBL::Attribute
410
  Exceptions : none
411 412 413 414 415 416 417 418
  Caller     : general
  Status     : Stable

=cut

sub get_all_Attributes {
  my ($self, $attrib_code) = @_;

419 420
  # if not cached, retrieve all of the attributes for this rnaproduct
  if (!defined($self->{'attributes'}) && defined($self->adaptor())) {
421 422 423 424 425 426 427 428 429 430 431 432 433 434
    my $aa = $self->adaptor->db->get_AttributeAdaptor();
    $self->{'attributes'} = $aa->fetch_all_by_RNAProduct($self);
  }

  if (defined $attrib_code) {
    my @results = grep { uc($_->code()) eq uc($attrib_code) }
      @{$self->{'attributes'}};
    return \@results;
  } else {
    return $self->{'attributes'};
  }
}


435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
=head2 get_all_DBEntries

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

  Arg [2]    : (optional) String, external_db type,
               ('ARRAY','ALT_TRANS','ALT_GENE','MISC','LIT','PRIMARY_DB_SYNONYM','ENSEMBL'),
               SQL wildcard characters (_ and %) can be used to
               specify patterns.

  Example    : my @dbentries = @{ $rnaproduct->get_all_DBEntries() };
               @dbentries = @{ $rnaproduct->get_all_DBEntries('Uniprot%') };
               @dbentries = @{ $rnaproduct->get_all_DBEntries('%', 'ENSEMBL') };

  Description: Retrieves DBEntries (xrefs) for this rnaproduct.

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

  Returntype : Listref to Bio::EnsEMBL::DBEntry objects
  Exceptions : none
  Caller     : ?
  Status     : Stable

=cut

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

  my $cache_name = 'dbentries';

  if (defined($ex_db_exp)) {
    $cache_name .= $ex_db_exp;
  }

  if (defined($ex_db_type)) {
    $cache_name .= $ex_db_type;
  }

  # if not cached, retrieve all of the xrefs for this rnaproduct
  if (!defined($self->{$cache_name}) && defined($self->adaptor())) {
    $self->{$cache_name} = $self->adaptor()->db()->get_DBEntryAdaptor()->
      fetch_all_by_RNAProduct( $self, $ex_db_exp, $ex_db_type );
  }

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

  return $self->{$cache_name};
}





=head2 get_all_DBLinks

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

  Arg [2]    : (optional) String, external database type, can be one of
               ('ARRAY','ALT_TRANS','ALT_GENE','MISC','LIT','PRIMARY_DB_SYNONYM','ENSEMBL'),
               SQL wildcard characters (_ and %) can be used to
               specify patterns.

  Example    :  my @dblinks = @{ $rnaproduct->get_all_DBLinks() };
                @dblinks = @{ $rnaproduct->get_all_DBLinks('mirbase%') };
                @dblinks = @{ $rnaproduct->get_all_DBLinks('%', 'ENSEMBL') };

  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
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut

518
sub get_all_DBLinks {  ## no critic (Subroutines::RequireArgUnpacking)
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
  my $self = shift;
  return $self->get_all_DBEntries(@_);
}


=head2 get_all_object_xrefs

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

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

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

  Description: Retrieves xrefs for this rnaproduct.

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

                NB: This method is an alias for the
                    get_all_DBentries() method.

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

  Status     : Stable

=cut

548
sub get_all_object_xrefs {  ## no critic (Subroutines::RequireArgUnpacking)
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
  my $self = shift;
  return $self->get_all_DBEntries(@_);
}


=head2 get_all_xrefs

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

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

  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

574
sub get_all_xrefs {  ## no critic (Subroutines::RequireArgUnpacking)
575 576 577 578 579
  my $self = shift;
  return $self->get_all_DBLinks(@_);
}


580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
=head2 modified_date

  Arg [1]    : (optional) string $modified_date - modification date to set
  Example    : $rnaproduct->modified_date('2007-01-10 20:52:00');
  Description: Getter/setter for attribute modified_date
  Returntype : string
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut

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


599 600 601 602 603 604 605 606 607 608 609 610
=head2 length

  Example    : print "RNA length =", $rnaproduct->length();
  Description: Retrieves the length of the nucleotide sequence represented
               by this RNAProduct object.
  Returntype : int
  Exceptions : none
  Caller     : webcode (protview etc.)
  Status     : Stable

=cut

611
sub length { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
612 613 614 615 616 617 618
  my $self = shift;
  my $seq = $self->seq();

  return ($seq) ? CORE::length($seq) : 0;
}


Marek Szuba's avatar
Marek Szuba committed
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 650 651
=head2 load

  Arg [1]       : Boolean $load_xrefs
                  Load (or don't load) xrefs.  Default is to load xrefs.
  Example       : $rnaproduct->load();
  Description   : The Ensembl API makes extensive use of
                  lazy-loading.  Under some circumstances (e.g.,
                  when copying genes between databases), all data of
                  an object needs to be fully loaded.  This method
                  loads the parts of the object that are usually
                  lazy-loaded.
  Returns       : none

=cut

sub load {
  my ($self, $load_xrefs) = @_;

  $load_xrefs = 1 unless defined($load_xrefs);

  $self->seq();

  $self->stable_id();
  $self->get_all_Attributes();

  if ($load_xrefs) {
    $self->get_all_DBEntries();
  }

  return;
}


652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
=head2 seq

  Example    : print $rnaproduct->seq();
  Description: Retrieves a string representation of the nucleotide sequence
               of this RNAProduct.  This retrieves the transcript from the
               database and gets its sequence, or retrieves the sequence which
               was set via the constructor/setter.
  Returntype : string
  Exceptions : warning if the sequence is not set and cannot be retrieved from
               the database.
  Caller     : webcode (protview etc.)
  Status     : Stable

=cut

sub seq {
  my ($self, $sequence) = @_;

  if (defined($sequence)) {

    $self->{'seq'} = $sequence;

  } elsif (!defined($self->{'seq'})) {

676 677 678 679 680
    my $tr_seq = $self->transcript()->seq();
    if ($tr_seq->length() <= 0) {
      throw('Got no or empty sequence from the database');
    }
    $self->{'seq'} = $tr_seq->subseq($self->{'start'}, $self->{'end'});
681 682 683 684 685 686 687 688

  }

  return '' unless defined($self->{'seq'});
  return $self->{'seq'};
}


689 690 691 692
=head2 stable_id

  Arg [1]    : (optional) string $stable_id - stable ID to set
  Example    : $rnaproduct->stable_id('ENSM00090210');
693 694 695
  Description: Getter/setter for attribute stable_id.
               Unlike stable_id_version(), setting a new stable ID does NOT
               reset the version number.
696 697 698 699 700 701 702 703 704 705 706 707 708 709
  Returntype : string
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut

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


710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
=head2 stable_id_version

  Arg [1]    : (optional) String - the stable ID with version to set
  Example    : $rnaproduct->stable_id("ENSM0059890.3");
  Description: Getter/setter for stable id with version for this rnaproduct.
               If the input string omits the version part, the version gets reset
               to undef; use stable_id() if you want to avoid this.
  Returntype : String
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut

sub stable_id_version {
  my $self = shift;

  if (my $stable_id = shift) {
    # If there is at least one embedded period assume everything
    # beyond the last one is the version number. This may not work for
    # some species, if you are worried about ambiguity use stable_id() +
    # version() explicitly.
    my $vindex = rindex($stable_id, '.');
    ($self->{stable_id},
     $self->{version}) = ($vindex > 0 ?
			  (substr($stable_id, 0, $vindex),
			   substr($stable_id, $vindex + 1)) :
			  $stable_id, undef
			 );
  }

  return $self->{stable_id} . ($self->{version} ? ".$self->{version}" : '');
}


745 746 747 748 749 750 751 752 753 754 755 756 757 758
=head2 start

  Arg [1]    : (optional) int $start - start position to set
  Example    : $rnaproduct->start(17);
  Description: Getter/setter for the value of start, which is a position within
               the precursor Transcript.
  Returntype : int
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut

sub start {
759 760 761
  my $self = shift;
  $self->{'start'} = shift if (@_);
  return $self->{'start'};
762 763 764
}


Marek Szuba's avatar
Marek Szuba committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
=head2 start_Exon

  Arg [1]    : (optional) Bio::EnsEMBL::Exon || undef - start exon to assign
  Example    : $rnaproduct->start_Exon($exon1);
  Description: Getter/setter for the value of start_Exon, which denotes the
               exon at which rnaproduct starts.
  Returntype : Bio::EnsEMBL::Exon
  Exceptions : thrown on wrong argument type
  Caller     : general
  Status     : Stable

=cut

sub start_Exon {
  my ($self, $exon) = @_;

  if (defined($exon)) {

    # Normal setter
    assert_ref($exon, 'Bio::EnsEMBL::Exon');
    $self->{'start_exon'} = $exon;

  }
  elsif (@_ > 1) {
    # User has explicitly passed undef. Break connection to exon.
    delete( $self->{'start_exon'} );
  }

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


797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
=head2 summary_as_hash

  Example       : $rnaproduct_summary = $rnaproduct->summary_as_hash();
  Description   : Retrieves a textual summary of this RNAProduct.
                  Not inherited from Feature.
  Returns       : hashref of arrays of descriptive strings
  Status        : Intended for internal use

=cut

sub summary_as_hash {
  my $self = shift;
  my %summary;
  my $id = $self->display_id;
  if ($self->version) {
    $id .= "." . $self->version;
  }
  $summary{'id'} = $id;
  $summary{'rnaproduct_id'} = $id;
  $summary{'genomic_start'} = $self->genomic_start;
  $summary{'genomic_end'} = $self->genomic_end;
  $summary{'length'} = $self->length;
  my $transcript = $self->transcript;
  $summary{'Parent'} = $transcript->display_id;
  return \%summary;
}


825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
=head2 synchronise_attributes

  Example       : $rnaproduct->synchronise_attributes();
  Description   : Some RNAProduct attributes, e.g. stem-loop arm in case
                  of MicroRNA, use a local cache of their value for
                  convenience. Unless the corresponding setters update both
                  the cache value and the attribute (which would defeat
                  the convenience thing), we have to make sure the former
                  get propagated to the latter before storing the object
                  in the database:
                   - if no corresponding attribute exists, create one;
                   - if there is one, update its value.
                  Class-specific maps of attributes to synchronise are
                  provided by
                  RNAProductTypeMapper::class_attribute_cache_map() .
  Returntype    : none
  Exceptions    : throws if the object contains multiple attributes with the
                  given code and the choice which one to update is
                  ambiguous.
  Caller        : RNAProductAdaptor
  Status        : At Risk (In Development)

=cut

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

  my $attribute_cache_map = Bio::EnsEMBL::Utils::RNAProductTypeMapper::mapper()
    ->class_attribute_cache_map(ref($self));

  while (my ($cache_key, $attr_code) = each %{$attribute_cache_map}) {
    my $existing_attributes = $self->get_all_Attributes($attr_code);
    my $n_existing_attrs = scalar @{$existing_attributes};
    if ($n_existing_attrs > 0) {
      # At the moment we do not support multiple occurrences of target
      # attributes at all
      if ($n_existing_attrs > 1) {
        throw("Object has multiple '$attr_code' attributes and we do not know"
              . " which one to update");
      }
      else {
        $existing_attributes->[0]->value($self->{$cache_key});
      }
    }
    else {
      # No corresponding attribute exists, most likely because we are
      # dealing with a newly created object which has never been pushed
      # to the database.
      $self->add_Attributes(Bio::EnsEMBL::Attribute->new(
        -CODE  => $attr_code,
        -VALUE => $self->{$cache_key},
      ));
    }
  }

  return;
}


884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
=head2 transcript

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

=cut

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

  if (defined($transcript)) {

    # Normal setter
    assert_ref($transcript, 'Bio::EnsEMBL::Transcript');
    $self->{'transcript'} = $transcript;
    weaken($self->{'transcript'});    # Avoid circular references.

  } elsif (@_ > 1) {

    # User has explicitly passed undef. Break connection to transcript.
    delete( $self->{'transcript'} );

  } elsif (!defined($self->{'transcript'})) {
911 912 913 914 915 916 917 918 919 920 921 922 923
    my $adaptor = $self->{'adaptor'};
    if (!defined($adaptor)) {
      throw("Adaptor not set for rnaproduct, cannot fetch its transcript");
    }

    my $dbID = $self->{'dbID'};
    if (!defined($dbID)) {
      throw("dbID not set for rnaproduct, cannot fetch its transcript.");
    }

    $self->{'transcript'} =
      $adaptor->db()->get_TranscriptAdaptor()
      ->fetch_by_rnaproduct_id($dbID);
924 925 926 927 928 929 930 931 932 933

    # Do not weaken the reference if we had to get the transcript from the
    # database. The user is probably working on rnaproducts directly,
    # not going through transcripts.
  }

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


934
=head2 type_code
935

936
  Example    : my $rp_type_code = $rnaproduct->type_code();
937
  Description: Getter for the RNAProduct type (e.g. miRNA, circRNA, ...).
938 939 940 941 942 943 944
               The type is expressed as human-readable code.
               This is somewhat redundant because similar information can
               be obtained simply by looking at the class of the object,
               indeed type_code is not meant to be modified independently
               of the class. However, there are certain use cases when the
               latter are more convenient than the former.
  Returntype : string
945 946 947 948 949 950
  Exceptions : none
  Caller     : ?
  Status     : In Development

=cut

951
sub type_code {
952
  my $self = shift;
953
  return $self->{'type_code'};
954 955 956
}


957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
=head2 version

  Arg [1]    : (optional) string $version - version to set
  Example    : $rnaproduct->version(2);
  Description: Getter/setter for attribute version
  Returntype : string
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut

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

1;