EnsEMBL/DBSQL/RNAProductAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 184 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 12 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 266 CC 20 SCAR 32.8
CC
20
Cov
0%
store
90.0
_obj_from_sth
20.0
fetch_all_by_external_name
12.0
CRAP 26.5
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
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
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
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.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::DBSQL::RNAProductAdaptor - Provides a means to fetch and store
34 RNAProduct objects from a database.
35  
36 =head1 DESCRIPTION
37  
38 This adaptor provides a means to retrieve and store
39 Bio::EnsEMBL::RNAProduct objects from/in a database.
40  
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
44 RNAProduct.
45  
46 =head1 SYNOPSIS
47  
48 use Bio::EnsEMBL::Registry;
49  
50 Bio::EnsEMBL::Registry->load_registry_from_db(
51 -host => 'ensembldb.ensembl.org',
52 -user => 'anonymous'
53 );
54  
55 $rnaproduct_adaptor =
56 Bio::EnsEMBL::Registry->get_adaptor( "human", "core",
57 "rnaproduct" );
58  
59 ...
60  
61 =head1 METHODS
62  
63 =cut
64  
65 package Bio::EnsEMBL::DBSQL::RNAProductAdaptor;
66 $Bio::EnsEMBL::DBSQL::RNAProductAdaptor::VERSION = '114.0.0_50'; # TRIAL
67 $Bio::EnsEMBL::DBSQL::RNAProductAdaptor::VERSION = '114.0.050';
68 use strict;
69 use warnings;
70  
71 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
72 use Bio::EnsEMBL::MicroRNA;
73 use Bio::EnsEMBL::RNAProduct;
74 use Bio::EnsEMBL::Utils::Exception qw( throw warning );
75 use Bio::EnsEMBL::Utils::Scalar qw( assert_ref );
76  
77  
78 use parent qw( Bio::EnsEMBL::DBSQL::BaseAdaptor );
79  
80  
81  
82 =head2 fetch_all_by_Transcript
83  
84 Arg [1] : Bio::EnsEMBL::Transcript $transcript
85 Example : $rps = $rnaproduct_adaptor->fetch_by_Transcript($transcript);
86 Description: Retrieves RNAProducts via their associated transcript.
87 If no RNAProducts are found, an empty list is returned.
88 Returntype : arrayref of Bio::EnsEMBL::RNAProducts
89 Exceptions : throw on incorrect argument
90 Caller : Transcript
91 Status : Stable
92  
93 =cut
94  
95 sub fetch_all_by_Transcript {
96 my ($self, $transcript) = @_;
97  
98 assert_ref($transcript, 'Bio::EnsEMBL::Transcript');
99  
100 return $self->_fetch_direct_query(['rp.transcript_id', $transcript->dbID(), SQL_INTEGER]);
101 }
102  
103  
104 =head2 fetch_all_by_external_name
105  
106 Arg [1] : String $external_name
107 An external identifier of the RNAProduct to be obtained
108 Arg [2] : (optional) String $external_db_name
109 The name of the external database from which the
110 identifier originates.
111 Arg [3] : Boolean override. Force SQL regex matching for users
112 who really do want to find all 'NM%'
113 Example : my @rnaproducts =
114 @{ $rp_a->fetch_all_by_external_name('MIMAT0000416') };
115 my @more_rnaproducts =
116 @{ $rp_a->fetch_all_by_external_name('hsa-miR-1-__') };
117 Description: Retrieves all RNAProducts which are associated with
118 an external identifier such as a GO term, miRBase
119 identifer, etc. Usually there will only be a single
120 RNAProduct returned in the list reference, but not
121 always. If no RNAProducts with the external identifier
122 are found, a reference to an empty list is returned.
123 SQL wildcards % and _ are supported in the $external_name
124 but their use is somewhat restricted for performance reasons.
125 Users that really do want % and _ in the first three characters
126 should use argument 3 to prevent optimisations
127 Returntype : listref of Bio::EnsEMBL::RNAProduct
128 Exceptions : none
129 Caller : general
130 Status : Stable
131  
132 =cut
133  
134 sub fetch_all_by_external_name {
135 my ($self, $external_name, $external_db_name, $override) = @_;
136  
137 my $entry_adaptor = $self->db->get_DBEntryAdaptor();
138  
139 my @ids = $entry_adaptor->list_rnaproduct_ids_by_extids($external_name,
140 $external_db_name,
141 $override);
142  
143 my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
144  
145 my @reference;
146 my @non_reference;
147 foreach my $id (@ids) {
148 my $transcript = $transcript_adaptor->fetch_by_rnaproduct_id($id);
149  
150 if ( defined($transcript) ) {
151 my $rnaproduct = $self->fetch_by_dbID($id);
152 if ( $transcript->slice()->is_reference() ) {
153 push(@reference, $rnaproduct);
154 }
155 else {
156 push(@non_reference, $rnaproduct);
157 }
158 }
159 }
160  
161 return [@reference, @non_reference];
162 }
163  
164  
165 =head2 fetch_all_by_type
166  
167 Arg [1] : string $type_code
168 Example : $rps = $rp_a->fetch_all_by_type('miRNA');
169 Description: Retrieves RNAProducts via their type (e.g. miRNA, circRNA).
170 If no matching RNAProducts are found, an empty list is
171 returned.
172 Returntype : arrayref of Bio::EnsEMBL::RNAProducts
173 Exceptions : throws if type code is undefined
174 Caller : ?
175 Status : In Development
176  
177 =cut
178  
179 sub fetch_all_by_type {
180 my ($self, $type_code) = @_;
181  
182 if ( !defined $type_code ) {
183 throw("type code argument is required");
184 }
185  
186 return ($self->_fetch_direct_query(['pt.code', $type_code, SQL_VARCHAR]));
187 }
188  
189  
190 =head2 fetch_by_dbID
191  
192 Arg [1] : int $dbID
193 The internal identifier of the RNAProduct to obtain
194 Example : $rnaproduct = $rnaproduct_adaptor->fetch_by_dbID(1234);
195 Description: This fetches a RNAProduct object via its internal id.
196 This is only debatably useful since RNAProducts do
197 not make much sense outside of the context of their
198 Transcript. Consider using fetch_by_Transcript instead.
199 Returntype : Bio::EnsEMBL::RNAProduct, or undef if the RNAProduct is not
200 found.
201 Caller : ?
202 Status : Stable
203  
204 =cut
205  
206 sub fetch_by_dbID {
207 my ($self, $dbID) = @_;
208  
209 if ( !defined $dbID ) {
210 throw("dbID argument is required");
211 }
212  
213 return ($self->_fetch_direct_query(['rp.rnaproduct_id', $dbID, SQL_INTEGER]))->[0];
214 }
215  
216  
217 =head2 fetch_by_stable_id
218  
219 Arg [1] : string $stable_id
220 The stable identifier of the RNAProduct to obtain
221 Example : $rnaproduct = $rnaproduct_adaptor->fetch_by_stable_id("ENSS00001");
222 Description: This fetches a RNAProduct object via its stable id.
223 Returntype : Bio::EnsEMBL::RNAProduct, or undef if the RNAProduct is not
224 found.
225 Caller : ?
226 Status : Stable
227  
228 =cut
229  
230 sub fetch_by_stable_id {
231 my ($self, $stable_id) = @_;
232  
233 if ( !defined $stable_id ) {
234 throw("stable id argument is required");
235 }
236  
237 return ($self->_fetch_direct_query(['rp.stable_id', $stable_id, SQL_VARCHAR]))->[0];
238 }
239  
240  
241 =head2 list_dbIDs
242  
243 Arg [1] : none
244 Example : @rnaproduct_ids = @{$rnaproduct_adaptor->list_dbIDs()};
245 Description: Gets an array of internal ids for all RNAProducts in the current db
246 Returntype : list of ints
247 Exceptions : none
248 Caller : ?
249 Status : Stable
250  
251 =cut
252  
253 sub list_dbIDs {
254 my ($self) = @_;
255  
256 return $self->_list_dbIDs("rnaproduct");
257 }
258  
259  
260 =head2 remove
261  
262 Arg [1] : Bio::EnsEMBL::RNAProduct $rnaproduct
263 The RNAProduct to be removed from the database
264 Example : $rpID = $rp_adaptor->remove($rnaproduct, $transcript->dbID());
265 Description: Removes a RNAProduct, along with all associated information
266 from the database.
267 Returntype : none
268 Exceptions : throw on incorrect arguments
269 Caller : ?
270 Status : Stable
271  
272 =cut
273  
274 sub remove {
275 my ($self, $rnaproduct) = @_;
276  
277 if (!ref($rnaproduct) || !$rnaproduct->isa('Bio::EnsEMBL::RNAProduct')) {
278 throw("$rnaproduct is not a EnsEMBL rnaproduct");
279 }
280  
281 my $db = $self->db();
282  
283 # Do nothing if the object is not stored to begin with
284 if (!$rnaproduct->is_stored($db)) {
285 return;
286 }
287  
288 # Remove xrefs
289 my $dbe_adaptor = $db->get_DBEntryAdaptor();
290 for my $dbe (@{ $rnaproduct->get_all_DBEntries() }) {
291 $dbe_adaptor->remove_from_object($dbe, $rnaproduct, 'RNAProduct');
292 }
293  
294 # Remove attributes
295 my $attr_adaptor = $db->get_AttributeAdaptor();
296 $attr_adaptor->remove_from_RNAProduct($rnaproduct);
297  
298 # Remove rnaproduct itself
299 my $sth = $self->prepare("DELETE FROM rnaproduct WHERE rnaproduct_id = ?");
300 $sth->bind_param(1, $rnaproduct->dbID(), SQL_INTEGER);
301 $sth->execute();
302 $sth->finish();
303  
304 # Mark the object as local
305 $rnaproduct->dbID(undef);
306 $rnaproduct->adaptor(undef);
307  
308 return;
309 }
310  
311  
312 =head2 store
313  
314 Arg [1] : Bio::EnsEMBL::RNAProduct $rnaproduct
315 The RNAProduct to be written to the database
316 Arg [2] : Int $transcript_dbID
317 The identifier of the transcript that this RNAProduct is
318 associated with
319 Example : $rpID = $rp_adaptor->store($rnaproduct, $transcript->dbID());
320 Description: Stores a RNAProduct in the database and returns the new
321 internal identifier for the stored RNAProduct.
322 Returntype : Int
323 Exceptions : throw on incorrect arguments
324 Caller : general
325 Status : Stable
326  
327 =cut
328  
329 sub store {
330 my ($self, $rnaproduct, $transcript_dbID) = @_;
331  
332 if (!ref($rnaproduct) || !$rnaproduct->isa('Bio::EnsEMBL::RNAProduct')) {
333 throw("$rnaproduct is not a EnsEMBL rnaproduct - not storing");
334 }
335  
336 my $db = $self->db();
337  
338 # Avoid creating duplicate entries
339 if ($rnaproduct->is_stored($db)) {
340 return $rnaproduct->dbID();
341 }
342  
343 my ( $start_exon_dbID, $end_exon_dbID );
344 if ( $rnaproduct->start_Exon() ) {
345 $start_exon_dbID = $rnaproduct->start_Exon()->dbID();
346 }
347 if ( $rnaproduct->end_Exon() ) {
348 $end_exon_dbID = $rnaproduct->end_Exon()->dbID();
349 }
350  
351 # Store rnaproduct
352  
353 my @columns = qw{ transcript_id seq_start start_exon_id seq_end end_exon_id };
354  
355 my @canned_columns = ( 'rnaproduct_type_id' );
356 my @canned_values
357 = ( '(SELECT rnaproduct_type_id FROM rnaproduct_type WHERE code=?)' );
358  
359 if (defined($rnaproduct->stable_id())) {
360 push @columns, 'stable_id', 'version';
361  
362 my $created = $db->dbc->from_seconds_to_date($rnaproduct->created_date());
363 if ($created) {
364 push @canned_columns, 'created_date';
365 push @canned_values, $created;
366 }
367  
368 my $modified = $db->dbc->from_seconds_to_date($rnaproduct->modified_date());
369 if ($modified) {
370 push @canned_columns, 'modified_date';
371 push @canned_values, $modified;
372 }
373 }
374  
375 my $column_string = join(', ', @columns, @canned_columns);
376 my $value_string = join(', ', (q{?}) x @columns, @canned_values);
377 my $store_rnaproduct_sql
378 = "INSERT INTO rnaproduct ( $column_string ) VALUES ( $value_string )";
379 my $rp_st = $self->prepare($store_rnaproduct_sql);
380  
381 $rp_st->bind_param( 1, $transcript_dbID, SQL_INTEGER);
382 $rp_st->bind_param( 2, $rnaproduct->start(), SQL_INTEGER);
383 $rp_st->bind_param( 3, $start_exon_dbID, SQL_INTEGER);
384 $rp_st->bind_param( 4, $rnaproduct->end(), SQL_INTEGER);
385 $rp_st->bind_param( 5, $end_exon_dbID, SQL_INTEGER);
386 if (defined($rnaproduct->stable_id())) {
387 $rp_st->bind_param(6, $rnaproduct->stable_id(), SQL_VARCHAR);
388 $rp_st->bind_param(7, $rnaproduct->version(), SQL_INTEGER);
389 }
390 $rp_st->bind_param( 8, $rnaproduct->type_code(), SQL_VARCHAR);
391 $rp_st->execute();
392 $rp_st->finish();
393  
394 # Retrieve the newly assigned dbID
395 my $rp_dbID = $self->last_insert_id('rnaproduct_id', undef, 'rnaproduct');
396  
397 # Store attributes
398 $rnaproduct->synchronise_attributes();
399 my $attr_adaptor = $db->get_AttributeAdaptor();
400 $attr_adaptor->store_on_RNAProduct($rp_dbID,
401 $rnaproduct->get_all_Attributes());
402  
403 # Store xrefs
404 my $dbe_adaptor = $db->get_DBEntryAdaptor();
405 for my $dbe (@{ $rnaproduct->get_all_DBEntries() }) {
406 $dbe_adaptor->store($dbe, $rp_dbID, 'RNAProduct', 1);
407 }
408  
409 # Link the rnaproduct object to its data row
410 $rnaproduct->dbID($rp_dbID);
411 $rnaproduct->adaptor($self);
412  
413 return $rp_dbID;
414 }
415  
416  
417 =head2 _list_dbIDs
418  
419 Arg[1] : String $table
420 Arg[2] : String $column
421 Example : $rnaproduct_adaptor->_list_dbIDs('rnaproduct', 'rnaproduct_id');
422 Description : Local reimplementation to ensure multi-species RNAProducts
423 are limited to their species alone
424 Returntype : ArrayRef of specified IDs
425 Caller : Internal
426 Status : Unstable
427  
428 =cut
429  
430 sub _list_dbIDs {
431 my ($self, $table, $column) = @_;
432 my $ids;
433 if($self->is_multispecies()) {
434 $column ||= "${table}_id";
435 my $sql = <<"SQL";
436 SELECT `rp`.`${column}` FROM rnaproduct rp
437 JOIN transcript t USING (transcript_id)
438 JOIN seq_region sr USING (seq_region_id)
439 JOIN coord_system cs USING (coord_system_id)
440 WHERE cs.species_id = ?
441 SQL
442 return $self->dbc()->sql_helper()->execute_simple(-SQL => $sql, -PARAMS => [$self->species_id()]);
443 }
444 else {
445 $ids = $self->SUPER::_list_dbIDs($table, $column);
446 }
447 return $ids;
448 }
449  
450  
451 =head2 _fetch_direct_query
452 Arg [1] : reference to an array consisting of:
453 - the name of the column to use in the WHERE clause,
454 - the value fields from that column are to be equal to,
455 - the data type of that column (e.g. SQL_INTEGER)
456 Description: PRIVATE internal method shared between public fetch methods
457 in order to avoid duplication of SQL-query logic. NOT SAFE
458 to be handed directly to users because in its current form
459 it can be trivially exploited to inject arbitrary SQL.
460 Returntype : ArrayRef of either Bio::EnsEMBL::RNAProducts or undefs
461 Exceptions : throws if RNAProduct type is absent or unknown
462 Caller : internal
463 Status : At Risk (In Development)
464  
465 =cut
466  
467 sub _fetch_direct_query {
468 my ($self, $where_args) = @_;
469  
470 my $rp_created_date =
471 $self->db()->dbc()->from_date_to_seconds('created_date');
472 my $rp_modified_date =
473 $self->db()->dbc()->from_date_to_seconds('modified_date');
474  
475 my $sql_template = <<'SQL';
476 SELECT rp.rnaproduct_id, pt.code, rp.transcript_id, rp.seq_start,
477 rp.start_exon_id, rp.seq_end, rp.end_exon_id, rp.stable_id, rp.version,
478 %s, %s
479 FROM rnaproduct rp
480 JOIN rnaproduct_type pt ON rp.rnaproduct_type_id = pt.rnaproduct_type_id
481 WHERE %s = ?
482 SQL
483 my $sql = sprintf( $sql_template,
484 $rp_created_date, $rp_modified_date, $where_args->[0] );
485 my $sth = $self->prepare($sql);
486 $sth->bind_param(1, $where_args->[1], $where_args->[2]);
487 $sth->execute();
488 my $results = $self->_obj_from_sth($sth);
489 $sth->finish();
490  
491 return $results;
492 }
493  
494  
495 =head2 _obj_from_sth
496 Arg [1] : DBI statement handle
497 Description: PRIVATE internal method shared between public SQL-query
498 methods in order to avoid duplication of object-creation
499 logic.
500 Returntype : ArrayRef of either Bio::EnsEMBL::RNAProducts or undefs
501 Exceptions : throws if RNAProduct type is absent or unknown
502 Caller : internal
503 Status : At Risk (In Development)
504  
505 =cut
506  
507 sub _obj_from_sth {
508 my ($self, $sth) = @_;
509 my @return_data;
510  
511 my $sql_data = $sth->fetchall_arrayref();
512 my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
513 while (my $row_ref = shift @{$sql_data}) {
514 my ($rnaproduct_id, $type_code, $transcript_id, $seq_start, $start_exon_id,
515 $seq_end, $end_exon_id, $stable_id, $version, $created_date,
516 $modified_date) = @{$row_ref};
517  
518 if (!defined($rnaproduct_id)) {
519 push @return_data, undef;
520 next;
521 }
522  
523 my $exon_adaptor = $self->db()->get_ExonAdaptor();
524 my ( $start_exon, $end_exon );
525 if (defined $start_exon_id ) {
526 $start_exon = $exon_adaptor->fetch_by_dbID( $start_exon_id );
527 }
528 if (defined $end_exon_id ) {
529 $end_exon = $exon_adaptor->fetch_by_dbID( $end_exon_id );
530 }
531  
532 my $class_name = Bio::EnsEMBL::Utils::RNAProductTypeMapper::mapper()
533 ->type_code_to_class($type_code);
534 my $rnaproduct = $class_name->new_fast( {
535 'dbID' => $rnaproduct_id,
536 'type_code' => $type_code,
537 'adaptor' => $self,
538 'start' => $seq_start,
539 'end' => $seq_end,
540 'start_exon' => $start_exon,
541 'end_exon' => $end_exon,
542 'stable_id' => $stable_id,
543 'version' => $version,
544 'created_date' => $created_date || undef,
545 'modified_date' => $modified_date || undef,
546 } );
547  
548 my $transcript = $transcript_adaptor->fetch_by_dbID($transcript_id);
549 $rnaproduct->transcript($transcript);
550  
551 push @return_data, $rnaproduct;
552 }
553  
554 return \@return_data;
555 }
556  
557  
558 =head2 _tables
559  
560 Arg [1] : none
561 Description: PROTECTED implementation of superclass abstract method.
562 Returns the names, aliases of the tables to use for queries.
563 Returntype : list of listrefs of strings
564 Exceptions : none
565 Caller : internal
566 Status : Stable
567  
568 =cut
569  
570 # This method IS used, at the superclass level
571 sub _tables { ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
572 return (['rnaproduct', 'rp']);
573 }
574  
575  
576 1;