EnsEMBL/DBSQL/RNAProductAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 183 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 271 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';
67 use strict;
68 use warnings;
69  
70 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
71 use Bio::EnsEMBL::MicroRNA;
72 use Bio::EnsEMBL::RNAProduct;
73 use Bio::EnsEMBL::Utils::Exception qw( throw warning );
74 use Bio::EnsEMBL::Utils::Scalar qw( assert_ref );
75  
76  
77 use parent qw( Bio::EnsEMBL::DBSQL::BaseAdaptor );
78  
79  
80  
81 =head2 fetch_all_by_Transcript
82  
83 Arg [1] : Bio::EnsEMBL::Transcript $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
89 Caller : Transcript
90 Status : Stable
91  
92 =cut
93  
94 sub fetch_all_by_Transcript {
95 my ($self, $transcript) = @_;
96  
97 assert_ref($transcript, 'Bio::EnsEMBL::Transcript');
98  
99 return $self->_fetch_direct_query(['rp.transcript_id', $transcript->dbID(), SQL_INTEGER]);
100 }
101  
102  
103 =head2 fetch_all_by_external_name
104  
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
126 Returntype : listref of Bio::EnsEMBL::RNAProduct
127 Exceptions : none
128 Caller : general
129 Status : Stable
130  
131 =cut
132  
133 sub fetch_all_by_external_name {
134 my ($self, $external_name, $external_db_name, $override) = @_;
135  
136 my $entry_adaptor = $self->db->get_DBEntryAdaptor();
137  
138 my @ids = $entry_adaptor->list_rnaproduct_ids_by_extids($external_name,
139 $external_db_name,
140 $override);
141  
142 my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
143  
144 my @reference;
145 my @non_reference;
146 foreach my $id (@ids) {
147 my $transcript = $transcript_adaptor->fetch_by_rnaproduct_id($id);
148  
149 if ( defined($transcript) ) {
150 my $rnaproduct = $self->fetch_by_dbID($id);
151 if ( $transcript->slice()->is_reference() ) {
152 push(@reference, $rnaproduct);
153 }
154 else {
155 push(@non_reference, $rnaproduct);
156 }
157 }
158 }
159  
160 return [@reference, @non_reference];
161 }
162  
163  
164 =head2 fetch_all_by_type
165  
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
170 returned.
171 Returntype : arrayref of Bio::EnsEMBL::RNAProducts
172 Exceptions : throws if type code is undefined
173 Caller : ?
174 Status : In Development
175  
176 =cut
177  
178 sub fetch_all_by_type {
179 my ($self, $type_code) = @_;
180  
181 if ( !defined $type_code ) {
182 throw("type code argument is required");
183 }
184  
185 return ($self->_fetch_direct_query(['pt.code', $type_code, SQL_VARCHAR]));
186 }
187  
188  
189 =head2 fetch_by_dbID
190  
191 Arg [1] : int $dbID
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.
198 Returntype : Bio::EnsEMBL::RNAProduct, or undef if the RNAProduct is not
199 found.
200 Caller : ?
201 Status : Stable
202  
203 =cut
204  
205 sub fetch_by_dbID {
206 my ($self, $dbID) = @_;
207  
208 if ( !defined $dbID ) {
209 throw("dbID argument is required");
210 }
211  
212 return ($self->_fetch_direct_query(['rp.rnaproduct_id', $dbID, SQL_INTEGER]))->[0];
213 }
214  
215  
216 =head2 fetch_by_stable_id
217  
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.
222 Returntype : Bio::EnsEMBL::RNAProduct, or undef if the RNAProduct is not
223 found.
224 Caller : ?
225 Status : Stable
226  
227 =cut
228  
229 sub fetch_by_stable_id {
230 my ($self, $stable_id) = @_;
231  
232 if ( !defined $stable_id ) {
233 throw("stable id argument is required");
234 }
235  
236 return ($self->_fetch_direct_query(['rp.stable_id', $stable_id, SQL_VARCHAR]))->[0];
237 }
238  
239  
240 =head2 list_dbIDs
241  
242 Arg [1] : none
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
246 Exceptions : none
247 Caller : ?
248 Status : Stable
249  
250 =cut
251  
252 sub list_dbIDs {
253 my ($self) = @_;
254  
255 return $self->_list_dbIDs("rnaproduct");
256 }
257  
258  
259 =head2 remove
260  
261 Arg [1] : Bio::EnsEMBL::RNAProduct $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
265 from the database.
266 Returntype : none
267 Exceptions : throw on incorrect arguments
268 Caller : ?
269 Status : Stable
270  
271 =cut
272  
273 sub remove {
274 my ($self, $rnaproduct) = @_;
275  
276 if (!ref($rnaproduct) || !$rnaproduct->isa('Bio::EnsEMBL::RNAProduct')) {
277 throw("$rnaproduct is not a EnsEMBL rnaproduct");
278 }
279  
280 my $db = $self->db();
281  
282 # Do nothing if the object is not stored to begin with
283 if (!$rnaproduct->is_stored($db)) {
284 return;
285 }
286  
287 # Remove xrefs
288 my $dbe_adaptor = $db->get_DBEntryAdaptor();
289 for my $dbe (@{ $rnaproduct->get_all_DBEntries() }) {
290 $dbe_adaptor->remove_from_object($dbe, $rnaproduct, 'RNAProduct');
291 }
292  
293 # Remove attributes
294 my $attr_adaptor = $db->get_AttributeAdaptor();
295 $attr_adaptor->remove_from_RNAProduct($rnaproduct);
296  
297 # Remove rnaproduct itself
298 my $sth = $self->prepare("DELETE FROM rnaproduct WHERE rnaproduct_id = ?");
299 $sth->bind_param(1, $rnaproduct->dbID(), SQL_INTEGER);
300 $sth->execute();
301 $sth->finish();
302  
303 # Mark the object as local
304 $rnaproduct->dbID(undef);
305 $rnaproduct->adaptor(undef);
306  
307 return;
308 }
309  
310  
311 =head2 store
312  
313 Arg [1] : Bio::EnsEMBL::RNAProduct $rnaproduct
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
317 associated with
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.
321 Returntype : Int
322 Exceptions : throw on incorrect arguments
323 Caller : general
324 Status : Stable
325  
326 =cut
327  
328 sub store {
329 my ($self, $rnaproduct, $transcript_dbID) = @_;
330  
331 if (!ref($rnaproduct) || !$rnaproduct->isa('Bio::EnsEMBL::RNAProduct')) {
332 throw("$rnaproduct is not a EnsEMBL rnaproduct - not storing");
333 }
334  
335 my $db = $self->db();
336  
337 # Avoid creating duplicate entries
338 if ($rnaproduct->is_stored($db)) {
339 return $rnaproduct->dbID();
340 }
341  
342 my ( $start_exon_dbID, $end_exon_dbID );
343 if ( $rnaproduct->start_Exon() ) {
344 $start_exon_dbID = $rnaproduct->start_Exon()->dbID();
345 }
346 if ( $rnaproduct->end_Exon() ) {
347 $end_exon_dbID = $rnaproduct->end_Exon()->dbID();
348 }
349  
350 # Store rnaproduct
351  
352 my @columns = qw{ transcript_id seq_start start_exon_id seq_end end_exon_id };
353  
354 my @canned_columns = ( 'rnaproduct_type_id' );
355 my @canned_values
356 = ( '(SELECT rnaproduct_type_id FROM rnaproduct_type WHERE code=?)' );
357  
358 if (defined($rnaproduct->stable_id())) {
359 push @columns, 'stable_id', 'version';
360  
361 my $created = $db->dbc->from_seconds_to_date($rnaproduct->created_date());
362 if ($created) {
363 push @canned_columns, 'created_date';
364 push @canned_values, $created;
365 }
366  
367 my $modified = $db->dbc->from_seconds_to_date($rnaproduct->modified_date());
368 if ($modified) {
369 push @canned_columns, 'modified_date';
370 push @canned_values, $modified;
371 }
372 }
373  
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);
379  
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);
388 }
389 $rp_st->bind_param( 8, $rnaproduct->type_code(), SQL_VARCHAR);
390 $rp_st->execute();
391 $rp_st->finish();
392  
393 # Retrieve the newly assigned dbID
394 my $rp_dbID = $self->last_insert_id('rnaproduct_id', undef, 'rnaproduct');
395  
396 # Store attributes
397 $rnaproduct->synchronise_attributes();
398 my $attr_adaptor = $db->get_AttributeAdaptor();
399 $attr_adaptor->store_on_RNAProduct($rp_dbID,
400 $rnaproduct->get_all_Attributes());
401  
402 # Store xrefs
403 my $dbe_adaptor = $db->get_DBEntryAdaptor();
404 for my $dbe (@{ $rnaproduct->get_all_DBEntries() }) {
405 $dbe_adaptor->store($dbe, $rp_dbID, 'RNAProduct', 1);
406 }
407  
408 # Link the rnaproduct object to its data row
409 $rnaproduct->dbID($rp_dbID);
410 $rnaproduct->adaptor($self);
411  
412 return $rp_dbID;
413 }
414  
415  
416 =head2 _list_dbIDs
417  
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
424 Caller : Internal
425 Status : Unstable
426  
427 =cut
428  
429 sub _list_dbIDs {
430 my ($self, $table, $column) = @_;
431 my $ids;
432 if($self->is_multispecies()) {
433 $column ||= "${table}_id";
434 my $sql = <<"SQL";
435 SELECT `rp`.`${column}` FROM rnaproduct rp
436 JOIN transcript t USING (transcript_id)
437 JOIN seq_region sr USING (seq_region_id)
438 JOIN coord_system cs USING (coord_system_id)
439 WHERE cs.species_id = ?
440 SQL
441 return $self->dbc()->sql_helper()->execute_simple(-SQL => $sql, -PARAMS => [$self->species_id()]);
442 }
443 else {
444 $ids = $self->SUPER::_list_dbIDs($table, $column);
445 }
446 return $ids;
447 }
448  
449  
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
461 Caller : internal
462 Status : At Risk (In Development)
463  
464 =cut
465  
466 sub _fetch_direct_query {
467 my ($self, $where_args) = @_;
468  
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');
473  
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,
477 %s, %s
478 FROM rnaproduct rp
479 JOIN rnaproduct_type pt ON rp.rnaproduct_type_id = pt.rnaproduct_type_id
480 WHERE %s = ?
481 SQL
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]);
486 $sth->execute();
487 my $results = $self->_obj_from_sth($sth);
488 $sth->finish();
489  
490 return $results;
491 }
492  
493  
494 =head2 _obj_from_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
498 logic.
499 Returntype : ArrayRef of either Bio::EnsEMBL::RNAProducts or undefs
500 Exceptions : throws if RNAProduct type is absent or unknown
501 Caller : internal
502 Status : At Risk (In Development)
503  
504 =cut
505  
506 sub _obj_from_sth {
507 my ($self, $sth) = @_;
508 my @return_data;
509  
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};
516  
517 if (!defined($rnaproduct_id)) {
518 push @return_data, undef;
519 next;
520 }
521  
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 );
526 }
527 if (defined $end_exon_id ) {
528 $end_exon = $exon_adaptor->fetch_by_dbID( $end_exon_id );
529 }
530  
531 my $class_name = Bio::EnsEMBL::Utils::RNAProductTypeMapper::mapper()
532 ->type_code_to_class($type_code);
533 my $rnaproduct = $class_name->new_fast( {
534 'dbID' => $rnaproduct_id,
535 'type_code' => $type_code,
536 'adaptor' => $self,
537 'start' => $seq_start,
538 'end' => $seq_end,
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,
545 } );
546  
547 my $transcript = $transcript_adaptor->fetch_by_dbID($transcript_id);
548 $rnaproduct->transcript($transcript);
549  
550 push @return_data, $rnaproduct;
551 }
552  
553 return \@return_data;
554 }
555  
556  
557 =head2 _tables
558  
559 Arg [1] : none
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
563 Exceptions : none
564 Caller : internal
565 Status : Stable
566  
567 =cut
568  
569 # This method IS used, at the superclass level
570 sub _tables { ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
571 return (['rnaproduct', 'rp']);
572 }
573  
574  
575 1;