3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
6 Licensed under the Apache License, Version 2.0 (the
"License");
7 you may not use
this file except in compliance with the License.
8 You may obtain a copy of the License at
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an
"AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License
for the specific language governing permissions and
16 limitations under the License.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
34 RNAProduct objects from a database.
38 This adaptor provides a means to retrieve and store
41 RNAProduct objects only truly make sense in the context of their
42 transcripts so the recommended means to retrieve RNAProducts is
43 by retrieving the
Transcript object first, and then fetching the
51 -host =>
'ensembldb.ensembl.org',
65 package Bio::EnsEMBL::DBSQL::RNAProductAdaptor;
81 =head2 fetch_all_by_Transcript
84 Example : $rps = $rnaproduct_adaptor->fetch_by_Transcript($transcript);
85 Description: Retrieves RNAProducts via their associated
transcript.
86 If no RNAProducts are found, an empty list is returned.
87 Returntype : arrayref of Bio::EnsEMBL::RNAProducts
88 Exceptions :
throw on incorrect argument
94 sub fetch_all_by_Transcript {
95 my ($self, $transcript) = @_;
97 assert_ref($transcript,
'Bio::EnsEMBL::Transcript');
99 return $self->_fetch_direct_query([
'rp.transcript_id', $transcript->dbID(), SQL_INTEGER]);
103 =head2 fetch_all_by_external_name
105 Arg [1] : String $external_name
106 An external identifier of the
RNAProduct to be obtained
107 Arg [2] : (optional) String $external_db_name
108 The name of the external database from which the
109 identifier originates.
110 Arg [3] : Boolean
override. Force SQL regex matching
for users
111 who really
do want to find all
'NM%'
112 Example : my @rnaproducts =
113 @{ $rp_a->fetch_all_by_external_name(
'MIMAT0000416') };
114 my @more_rnaproducts =
115 @{ $rp_a->fetch_all_by_external_name(
'hsa-miR-1-__') };
116 Description: Retrieves all RNAProducts which are associated with
117 an external identifier such as a GO term, miRBase
118 identifer, etc. Usually there will only be a single
119 RNAProduct returned in the list reference, but not
120 always. If no RNAProducts with the external identifier
121 are found, a reference to an empty list is returned.
122 SQL wildcards % and _ are supported in the $external_name
123 but their use is somewhat restricted
for performance reasons.
124 Users that really
do want % and _ in the first three characters
125 should use argument 3 to prevent optimisations
133 sub fetch_all_by_external_name {
134 my ($self, $external_name, $external_db_name, $override) = @_;
136 my $entry_adaptor = $self->db->get_DBEntryAdaptor();
138 my @ids = $entry_adaptor->list_rnaproduct_ids_by_extids($external_name,
142 my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
146 foreach my $id (@ids) {
147 my $transcript = $transcript_adaptor->fetch_by_rnaproduct_id($id);
149 if ( defined($transcript) ) {
150 my $rnaproduct = $self->fetch_by_dbID($id);
151 if ( $transcript->slice()->is_reference() ) {
152 push(@reference, $rnaproduct);
155 push(@non_reference, $rnaproduct);
160 return [@reference, @non_reference];
164 =head2 fetch_all_by_type
166 Arg [1] :
string $type_code
167 Example : $rps = $rp_a->fetch_all_by_type(
'miRNA');
168 Description: Retrieves RNAProducts via their type (e.g. miRNA, circRNA).
169 If no matching RNAProducts are found, an empty list is
171 Returntype : arrayref of Bio::EnsEMBL::RNAProducts
172 Exceptions :
throws if type code is undefined
174 Status : In Development
178 sub fetch_all_by_type {
179 my ($self, $type_code) = @_;
181 if ( !defined $type_code ) {
182 throw(
"type code argument is required");
185 return ($self->_fetch_direct_query([
'pt.code', $type_code, SQL_VARCHAR]));
192 The
internal identifier of the RNAProduct to obtain
193 Example : $rnaproduct = $rnaproduct_adaptor->fetch_by_dbID(1234);
194 Description: This fetches a RNAProduct
object via its
internal id.
195 This is only debatably useful since RNAProducts
do
196 not make much sense outside of the context of their
197 Transcript. Consider
using fetch_by_Transcript instead.
206 my ($self, $dbID) = @_;
208 if ( !defined $dbID ) {
209 throw(
"dbID argument is required");
212 return ($self->_fetch_direct_query([
'rp.rnaproduct_id', $dbID, SQL_INTEGER]))->[0];
216 =head2 fetch_by_stable_id
218 Arg [1] :
string $stable_id
219 The stable identifier of the RNAProduct to obtain
220 Example : $rnaproduct = $rnaproduct_adaptor->fetch_by_stable_id(
"ENSS00001");
221 Description: This fetches a RNAProduct
object via its stable
id.
229 sub fetch_by_stable_id {
230 my ($self, $stable_id) = @_;
232 if ( !defined $stable_id ) {
233 throw(
"stable id argument is required");
236 return ($self->_fetch_direct_query([
'rp.stable_id', $stable_id, SQL_VARCHAR]))->[0];
243 Example : @rnaproduct_ids = @{$rnaproduct_adaptor->list_dbIDs()};
244 Description: Gets an array of
internal ids
for all RNAProducts in the current db
245 Returntype : list of ints
255 return $self->_list_dbIDs(
"rnaproduct");
262 The RNAProduct to be removed from the database
263 Example : $rpID = $rp_adaptor->remove($rnaproduct, $transcript->dbID());
264 Description: Removes a RNAProduct, along with all associated information
267 Exceptions :
throw on incorrect arguments
274 my ($self, $rnaproduct) = @_;
276 if (!ref($rnaproduct) || !$rnaproduct->isa(
'Bio::EnsEMBL::RNAProduct')) {
277 throw(
"$rnaproduct is not a EnsEMBL rnaproduct");
280 my $db = $self->db();
282 # Do nothing if the object is not stored to begin with
283 if (!$rnaproduct->is_stored($db)) {
288 my $dbe_adaptor = $db->get_DBEntryAdaptor();
289 for my $dbe (@{ $rnaproduct->get_all_DBEntries() }) {
290 $dbe_adaptor->remove_from_object($dbe, $rnaproduct,
'RNAProduct');
294 my $attr_adaptor = $db->get_AttributeAdaptor();
295 $attr_adaptor->remove_from_RNAProduct($rnaproduct);
297 # Remove rnaproduct itself
298 my $sth = $self->prepare(
"DELETE FROM rnaproduct WHERE rnaproduct_id = ?");
299 $sth->bind_param(1, $rnaproduct->dbID(), SQL_INTEGER);
303 # Mark the object as local
304 $rnaproduct->
dbID(undef);
305 $rnaproduct->adaptor(undef);
314 The RNAProduct to be written to the database
315 Arg [2] : Int $transcript_dbID
316 The identifier of the
transcript that
this RNAProduct is
318 Example : $rpID = $rp_adaptor->store($rnaproduct, $transcript->dbID());
319 Description: Stores a RNAProduct in the database and returns the
new
320 internal identifier
for the stored RNAProduct.
322 Exceptions :
throw on incorrect arguments
329 my ($self, $rnaproduct, $transcript_dbID) = @_;
331 if (!ref($rnaproduct) || !$rnaproduct->isa(
'Bio::EnsEMBL::RNAProduct')) {
332 throw(
"$rnaproduct is not a EnsEMBL rnaproduct - not storing");
335 my $db = $self->db();
337 # Avoid creating duplicate entries
338 if ($rnaproduct->is_stored($db)) {
339 return $rnaproduct->dbID();
342 my ( $start_exon_dbID, $end_exon_dbID );
343 if ( $rnaproduct->start_Exon() ) {
344 $start_exon_dbID = $rnaproduct->start_Exon()->dbID();
346 if ( $rnaproduct->end_Exon() ) {
347 $end_exon_dbID = $rnaproduct->end_Exon()->dbID();
352 my @columns = qw{ transcript_id seq_start start_exon_id seq_end end_exon_id };
354 my @canned_columns = (
'rnaproduct_type_id' );
356 = (
'(SELECT rnaproduct_type_id FROM rnaproduct_type WHERE code=?)' );
358 if (defined($rnaproduct->stable_id())) {
359 push @columns,
'stable_id',
'version';
361 my $created = $db->dbc->from_seconds_to_date($rnaproduct->created_date());
363 push @canned_columns,
'created_date';
364 push @canned_values, $created;
367 my $modified = $db->dbc->from_seconds_to_date($rnaproduct->modified_date());
369 push @canned_columns,
'modified_date';
370 push @canned_values, $modified;
374 my $column_string = join(
', ', @columns, @canned_columns);
375 my $value_string = join(
', ', (q{?}) x @columns, @canned_values);
376 my $store_rnaproduct_sql
377 =
"INSERT INTO rnaproduct ( $column_string ) VALUES ( $value_string )";
378 my $rp_st = $self->prepare($store_rnaproduct_sql);
380 $rp_st->bind_param( 1, $transcript_dbID, SQL_INTEGER);
381 $rp_st->bind_param( 2, $rnaproduct->start(), SQL_INTEGER);
382 $rp_st->bind_param( 3, $start_exon_dbID, SQL_INTEGER);
383 $rp_st->bind_param( 4, $rnaproduct->end(), SQL_INTEGER);
384 $rp_st->bind_param( 5, $end_exon_dbID, SQL_INTEGER);
385 if (defined($rnaproduct->stable_id())) {
386 $rp_st->bind_param(6, $rnaproduct->stable_id(), SQL_VARCHAR);
387 $rp_st->bind_param(7, $rnaproduct->version(), SQL_INTEGER);
389 $rp_st->bind_param( 8, $rnaproduct->type_code(), SQL_VARCHAR);
393 # Retrieve the newly assigned dbID
394 my $rp_dbID = $self->last_insert_id(
'rnaproduct_id', undef,
'rnaproduct');
397 $rnaproduct->synchronise_attributes();
398 my $attr_adaptor = $db->get_AttributeAdaptor();
399 $attr_adaptor->store_on_RNAProduct($rp_dbID,
400 $rnaproduct->get_all_Attributes());
403 my $dbe_adaptor = $db->get_DBEntryAdaptor();
404 for my $dbe (@{ $rnaproduct->get_all_DBEntries() }) {
405 $dbe_adaptor->store($dbe, $rp_dbID,
'RNAProduct', 1);
408 # Link the rnaproduct object to its data row
409 $rnaproduct->dbID($rp_dbID);
410 $rnaproduct->adaptor($self);
418 Arg[1] : String $table
419 Arg[2] : String $column
420 Example : $rnaproduct_adaptor->_list_dbIDs(
'rnaproduct',
'rnaproduct_id');
421 Description : Local reimplementation to ensure multi-species RNAProducts
422 are limited to their species alone
423 Returntype : ArrayRef of specified IDs
430 my ($self, $table, $column) = @_;
432 if($self->is_multispecies()) {
433 $column ||=
"${table}_id";
435 SELECT `rp`.`${column}` FROM rnaproduct rp
437 JOIN seq_region sr USING (seq_region_id)
438 JOIN coord_system cs USING (coord_system_id)
439 WHERE cs.species_id = ?
441 return $self->dbc()->sql_helper()->execute_simple(-SQL => $sql, -PARAMS => [$self->species_id()]);
444 $ids = $self->SUPER::_list_dbIDs($table, $column);
450 =head2 _fetch_direct_query
451 Arg [1] : reference to an array consisting of:
452 - the name of the column to use in the WHERE clause,
453 - the value fields from that column are to be equal to,
454 - the data type of that column (e.g. SQL_INTEGER)
455 Description: PRIVATE
internal method shared between
public fetch methods
456 in order to avoid duplication of SQL-query logic. NOT SAFE
457 to be handed directly to users because in its current form
458 it can be trivially exploited to inject arbitrary SQL.
459 Returntype : ArrayRef of either Bio::EnsEMBL::RNAProducts or undefs
460 Exceptions :
throws if RNAProduct type is absent or unknown
462 Status : At Risk (In Development)
466 sub _fetch_direct_query {
467 my ($self, $where_args) = @_;
469 my $rp_created_date =
470 $self->db()->dbc()->from_date_to_seconds(
'created_date');
471 my $rp_modified_date =
472 $self->db()->dbc()->from_date_to_seconds(
'modified_date');
474 my $sql_template = <<
'SQL';
475 SELECT rp.rnaproduct_id, pt.code, rp.transcript_id, rp.seq_start,
476 rp.start_exon_id, rp.seq_end, rp.end_exon_id, rp.stable_id, rp.version,
479 JOIN rnaproduct_type pt ON rp.rnaproduct_type_id = pt.rnaproduct_type_id
482 my $sql = sprintf( $sql_template,
483 $rp_created_date, $rp_modified_date, $where_args->[0] );
484 my $sth = $self->prepare($sql);
485 $sth->bind_param(1, $where_args->[1], $where_args->[2]);
487 my $results = $self->_obj_from_sth($sth);
495 Arg [1] : DBI statement handle
496 Description: PRIVATE
internal method shared between
public SQL-query
497 methods in order to avoid duplication of
object-creation
499 Returntype : ArrayRef of either Bio::EnsEMBL::RNAProducts or undefs
500 Exceptions :
throws if RNAProduct type is absent or unknown
502 Status : At Risk (In Development)
507 my ($self, $sth) = @_;
510 my $sql_data = $sth->fetchall_arrayref();
511 my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
512 while (my $row_ref = shift @{$sql_data}) {
513 my ($rnaproduct_id, $type_code, $transcript_id, $seq_start, $start_exon_id,
514 $seq_end, $end_exon_id, $stable_id, $version, $created_date,
515 $modified_date) = @{$row_ref};
517 if (!defined($rnaproduct_id)) {
518 push @return_data, undef;
522 my $exon_adaptor = $self->db()->get_ExonAdaptor();
523 my ( $start_exon, $end_exon );
524 if (defined $start_exon_id ) {
525 $start_exon = $exon_adaptor->fetch_by_dbID( $start_exon_id );
527 if (defined $end_exon_id ) {
528 $end_exon = $exon_adaptor->fetch_by_dbID( $end_exon_id );
533 my $rnaproduct = $class_name->new_fast( {
534 'dbID' => $rnaproduct_id,
535 'type_code' => $type_code,
537 'start' => $seq_start,
539 'start_exon' => $start_exon,
540 'end_exon' => $end_exon,
541 'stable_id' => $stable_id,
542 'version' => $version,
543 'created_date' => $created_date || undef,
544 'modified_date' => $modified_date || undef,
547 my $transcript = $transcript_adaptor->fetch_by_dbID($transcript_id);
548 $rnaproduct->transcript($transcript);
550 push @return_data, $rnaproduct;
553 return \@return_data;
560 Description: PROTECTED implementation of superclass
abstract method.
561 Returns the names, aliases of the tables to use
for queries.
562 Returntype : list of listrefs of strings
569 # This method IS used, at the superclass level
570 sub _tables { ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
571 return ([
'rnaproduct',
'rp']);