Bio-EnsEMBL

 view release on metacpan or  search on metacpan

lib/Bio/EnsEMBL/DBSQL/RNAProductAdaptor.pm  view on Meta::CPAN

  # Avoid creating duplicate entries
  if ($rnaproduct->is_stored($db)) {
    return $rnaproduct->dbID();
  }

  my ( $start_exon_dbID, $end_exon_dbID );
  if ( $rnaproduct->start_Exon() ) {
    $start_exon_dbID = $rnaproduct->start_Exon()->dbID();
  }
  if ( $rnaproduct->end_Exon() ) {
    $end_exon_dbID = $rnaproduct->end_Exon()->dbID();
  }

  # Store rnaproduct

  my @columns = qw{ transcript_id seq_start start_exon_id seq_end end_exon_id };

  my @canned_columns = ( 'rnaproduct_type_id' );
  my @canned_values
    = ( '(SELECT rnaproduct_type_id FROM rnaproduct_type WHERE code=?)' );

  if (defined($rnaproduct->stable_id())) {
    push @columns, 'stable_id', 'version';

    my $created = $db->dbc->from_seconds_to_date($rnaproduct->created_date());
    if ($created) {
      push @canned_columns, 'created_date';
      push @canned_values,  $created;
    }

    my $modified = $db->dbc->from_seconds_to_date($rnaproduct->modified_date());
    if ($modified) {
      push @canned_columns, 'modified_date';
      push @canned_values,  $modified;
    }
  }

  my $column_string = join(', ', @columns, @canned_columns);
  my $value_string  = join(', ', (q{?}) x @columns, @canned_values);
  my $store_rnaproduct_sql
    = "INSERT INTO rnaproduct ( $column_string ) VALUES ( $value_string )";
  my $rp_st = $self->prepare($store_rnaproduct_sql);

  $rp_st->bind_param(  1, $transcript_dbID,         SQL_INTEGER);
  $rp_st->bind_param(  2, $rnaproduct->start(),     SQL_INTEGER);
  $rp_st->bind_param(  3, $start_exon_dbID,         SQL_INTEGER);
  $rp_st->bind_param(  4, $rnaproduct->end(),       SQL_INTEGER);
  $rp_st->bind_param(  5, $end_exon_dbID,           SQL_INTEGER);
  if (defined($rnaproduct->stable_id())) {
    $rp_st->bind_param(6, $rnaproduct->stable_id(), SQL_VARCHAR);
    $rp_st->bind_param(7, $rnaproduct->version(),   SQL_INTEGER);
  }
  $rp_st->bind_param(  8, $rnaproduct->type_code(), SQL_VARCHAR);
  $rp_st->execute();
  $rp_st->finish();

  # Retrieve the newly assigned dbID
  my $rp_dbID = $self->last_insert_id('rnaproduct_id', undef, 'rnaproduct');

  # Store attributes
  $rnaproduct->synchronise_attributes();
  my $attr_adaptor = $db->get_AttributeAdaptor();
  $attr_adaptor->store_on_RNAProduct($rp_dbID,
                                     $rnaproduct->get_all_Attributes());

  # Store xrefs
  my $dbe_adaptor = $db->get_DBEntryAdaptor();
  for my $dbe (@{ $rnaproduct->get_all_DBEntries() }) {
    $dbe_adaptor->store($dbe, $rp_dbID, 'RNAProduct', 1);
  }

  # Link the rnaproduct object to its data row
  $rnaproduct->dbID($rp_dbID);
  $rnaproduct->adaptor($self);

  return $rp_dbID;
}


=head2 _list_dbIDs

  Arg[1]      : String $table
  Arg[2]      : String $column
  Example     : $rnaproduct_adaptor->_list_dbIDs('rnaproduct', 'rnaproduct_id');
  Description : Local reimplementation to ensure multi-species RNAProducts
                are limited to their species alone
  Returntype  : ArrayRef of specified IDs
  Caller      : Internal
  Status      : Unstable

=cut

sub _list_dbIDs {
  my ($self, $table, $column) = @_;
  my $ids;
  if($self->is_multispecies()) {
    $column ||= "${table}_id";
    my $sql = <<"SQL";
SELECT `rp`.`${column}` FROM rnaproduct rp
JOIN transcript t USING (transcript_id)
JOIN seq_region sr USING (seq_region_id)
JOIN coord_system cs USING (coord_system_id)
WHERE cs.species_id = ?
SQL
    return $self->dbc()->sql_helper()->execute_simple(-SQL => $sql, -PARAMS => [$self->species_id()]);
  }
  else {
    $ids = $self->SUPER::_list_dbIDs($table, $column);
  }
  return $ids;
}


=head2 _fetch_direct_query
  Arg [1]    : reference to an array consisting of:
                - the name of the column to use in the WHERE clause,
                - the value fields from that column are to be equal to,
                - the data type of that column (e.g. SQL_INTEGER)
  Description: PRIVATE internal method shared between public fetch methods
               in order to avoid duplication of SQL-query logic. NOT SAFE
               to be handed directly to users because in its current form



( run in 1.115 second using v1.01-cache-2.11-cpan-437f7b0c052 )