EnsEMBL/DBSQL/AttributeAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 301 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 15 Subroutinesub 0.0% 0 / 45 Podpod 0.0% 0 / 38 totaltotal 0.0% 0 / 463 CC 33 SCAR 22.1
CC
33
Cov
0%
remove_from_Object
30.0
store_on_Slice
20.0
fetch_all_by_Object
20.0
CRAP 9.1
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  
22 =head1 CONTACT
23  
24 Please email comments or questions to the public Ensembl
25 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
26  
27 Questions may also be sent to the Ensembl help desk at
28 <http://www.ensembl.org/Help/Contact>.
29  
30 =cut
31  
32 =head1 NAME
33  
34 Bio::EnsEMBL::DBSQL::AttributeAdaptor - Provides database interaction for
35 Bio::EnsEMBL::Attribute objects.
36  
37  
38 =head1 SYNOPSIS
39  
40 # $db is a Bio::EnsEMBL::DBSQL::DBAdaptor object:
41 $attribute_adaptor = $db->get_AttributeAdaptor();
42  
43 $attributes = $attribute_adaptor->fetch_all_by_MiscFeature($feature);
44  
45 $attributes = $attribute_adaptor->fetch_all_by_Slice($slice);
46  
47 $attribute_adaptor->store_on_Slice( $slice, \@attributes );
48  
49 $attribute_adaptor->store_on_MiscFeature( $misc_feature,
50 \@attributes )
51  
52 =head1 DESCRIPTION
53  
54 =head1 METHODS
55  
56 =cut
57  
58 package Bio::EnsEMBL::DBSQL::AttributeAdaptor;
59 $Bio::EnsEMBL::DBSQL::AttributeAdaptor::VERSION = '114.0.0_50'; # TRIAL
60 $Bio::EnsEMBL::DBSQL::AttributeAdaptor::VERSION = '114.0.050';
61 use strict;
62 use warnings;
63  
64 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
65 use Bio::EnsEMBL::Attribute;
66  
67 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
68 use Bio::EnsEMBL::Utils::Scalar qw(assert_ref);
69  
70 use vars qw(@ISA);
71  
72 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
73  
74 =head2 new
75  
76 Arg [...] : Superclass args. See Bio::EnsEMBL::DBSQL::BaseAdaptor
77 Description: Instantiates a Bio::EnsEMBL::DBSQL::AttributeAdaptor
78 Returntype : Bio::EnsEMBL::AttributeAdaptor
79 Exceptions : none
80 Caller : DBAdaptor
81 Status : Stable
82  
83 =cut
84  
85 sub new {
86 my $class = shift;
87  
88 my $self = $class->SUPER::new(@_);
89  
90 # cache creation could go here
91 return $self;
92 }
93  
94  
95  
96 ## Storing methods
97  
98 ## Batch store
99  
100 sub store_batch_on_Object {
101 my ($self, $table, $attributes, $batch_size) = @_;
102 # $attributes is a hashref where the key is the object ID
103 # and the value is an array ref of Attribute objects
104  
105 # maintain a hash of attrib type IDs by code so we don't have to keep looking them up...
106 my $attrib_type_ids = {};
107  
108 # create an arrayref of the values to store
109 my $rows = [];
110 $batch_size ||= scalar(values(%$attributes));
111  
112 while (my ($obj_id, $attribs) = each %{$attributes}) {
113 for my $attrib (@{$attribs}) {
114  
115 my $attrib_type_id = $attrib_type_ids->{$attrib->code()};
116 if (!defined $attrib_type_id) {
117 $attrib_type_id = $self->_store_type($attrib);
118 $attrib_type_ids->{$attrib->code()} = $attrib_type_id;
119 }
120  
121 push @$rows, sprintf('(%d, %d, %s)', $obj_id, $attrib_type_id, $self->dbc()->db_handle()->quote($attrib->value()));
122  
123 if (scalar(@$rows) == $batch_size) {
124 $rows = $self->_store_batch_rows($table, $rows);
125 }
126 }
127 }
128  
129 $rows = $self->_store_batch_rows($table, $rows);
130  
131 return;
132 }
133  
134 sub _store_batch_rows {
135 my ($self, $table, $rows) = @_;
136 if (scalar(@$rows) > 0) {
137 $self->dbc()->sql_helper()->execute_update(-SQL => 'INSERT INTO ' . $table . '_attrib(' . $table . '_id, attrib_type_id, value) VALUES ' . join(',', @$rows));
138 }
139 return [];
140 }
141  
142  
143 sub store_batch_on_MiscAttrib {
144 my ($self, $attributes, $batch_size) = @_;
145  
146 $self->store_batch_on_Object('misc_feature', $attributes, $batch_size);
147  
148 return;
149 }
150  
151 sub store_batch_on_Slice {
152 my ($self, $attributes, $batch_size) = @_;
153  
154 $self->store_batch_on_Object('seq_region', $attributes, $batch_size);
155  
156 return;
157 }
158  
159 sub store_batch_on_Gene {
160 my ($self, $attributes, $batch_size) = @_;
161  
162 $self->store_batch_on_Object('gene', $attributes, $batch_size);
163  
164 return;
165 }
166  
167 sub store_batch_on_Transcript {
168 my ($self, $attributes, $batch_size) = @_;
169  
170 $self->store_batch_on_Object('transcript', $attributes, $batch_size);
171  
172 return;
173 }
174  
175 sub store_batch_on_Translation {
176 my ($self, $attributes, $batch_size) = @_;
177  
178 $self->store_batch_on_Object('translation', $attributes, $batch_size);
179  
180 return;
181 }
182  
183 sub store_batch_on_DnaDnaAlignFeature {
184 my ($self, $attributes, $batch_size) = @_;
185  
186 $self->store_batch_on_Object('dna_align_feature', $attributes, $batch_size);
187  
188 return;
189 }
190  
191  
192 ## Single store
193  
194 sub store_on_Object {
195 my ($self, $object_id, $attributes, $table, $type) = @_;
196  
197 my $db = $self->db();
198 if (!defined $type) {
199 $type = $table;
200 }
201  
202 my $insert_ignore = $self->insert_ignore_clause();
203 my $sth = $self->prepare( "${insert_ignore} INTO ${table}_attrib (${type}_id, attrib_type_id, value)" .
204 "VALUES (?, ?, ?)" );
205  
206 for my $attrib ( @$attributes ) {
207 if(!ref($attrib) && $attrib->isa('Bio::EnsEMBL::Attribute')) {
208 throw("Reference to list of Bio::EnsEMBL::Attribute objects " .
209 "argument expected.");
210 }
211  
212 my $atid = $self->_store_type( $attrib );
213 $sth->bind_param(1,$object_id,SQL_INTEGER);
214 $sth->bind_param(2,$atid,SQL_INTEGER);
215 $sth->bind_param(3,$attrib->value,SQL_VARCHAR);
216 $sth->execute();
217 }
218  
219 return;
220 }
221  
222  
223 sub store_on_MiscFeature {
224 my ($self, $object, $attributes) = @_;
225  
226 assert_ref( $object, 'Bio::EnsEMBL::MiscFeature');
227  
228 my $object_id = $object->dbID();
229 $self->store_on_Object($object_id, $attributes, 'misc', 'misc_feature');
230  
231 return;
232 }
233  
234 sub store_on_Slice {
235 my ($self, $object, $attributes) = @_;
236  
237 assert_ref( $object, 'Bio::EnsEMBL::Slice');
238  
239 my $object_id = $object->get_seq_region_id();
240 $self->store_on_Object($object_id, $attributes, 'seq_region');
241  
242 my $undef_circular_cache = 0;
243 for my $attrib ( @$attributes ) {
244 if ((defined $attrib->code) and ($attrib->code eq 'circular_seq')) {
245 $undef_circular_cache = 1;
246 }
247 }
248  
249 if ($undef_circular_cache) {
250 #the slice is circular
251 $object->{'circular'} = 1;
252 my $slice_adaptor = $object->adaptor();
253 #undefine slice adaptor->is_circular and the circular slice cache
254 if (defined $slice_adaptor) {
255 $slice_adaptor->{'is_circular'} = undef;
256 $slice_adaptor->{'circular_sr_id_cache'} = {};
257 }
258 }
259  
260 return;
261 }
262  
263 sub store_on_Gene {
264 my ($self, $object, $attributes) = @_;
265  
266 my $object_id;
267 if (!ref($object)){
268 $object_id = $object;
269 }
270 else {
271 $object_id = $object->dbID;
272 }
273  
274 $self->store_on_Object($object_id, $attributes, 'gene');
275
276 return;
277 }
278  
279 sub store_on_Transcript {
280 my ($self, $object, $attributes) = @_;
281  
282 my $object_id;
283 if (!ref($object)){
284 $object_id = $object;
285 }
286 else {
287 $object_id = $object->dbID;
288 }
289  
290 $self->store_on_Object($object_id, $attributes, 'transcript');
291  
292 return;
293 }
294  
295 sub store_on_RNAProduct {
296 my ($self, $object, $attributes) = @_;
297  
298 my $object_id;
299 if (!ref($object)) {
300 $object_id = $object;
301 }
302 else {
303 $object_id = $object->dbID();
304 }
305  
306 $self->store_on_Object($object_id, $attributes, 'rnaproduct');
307  
308 return;
309 }
310  
311 sub store_on_Translation {
312 my ($self, $object, $attributes) = @_;
313  
314 my $object_id;
315 if (!ref($object)){
316 $object_id = $object;
317 }
318 else {
319 $object_id = $object->dbID;
320 }
321  
322 $self->store_on_Object($object_id, $attributes, 'translation');
323  
324 return;
325 }
326  
327 sub store_on_DnaDnaAlignFeature {
328 my ($self, $object, $attributes) = @_;
329  
330 my $object_id;
331 if (!ref($object)){
332 $object_id = $object;
333 }
334 else {
335 $object_id = $object->dbID;
336 }
337  
338 $self->store_on_Object($object_id, $attributes, 'dna_align_feature');
339  
340 return;
341 }
342  
343  
344 ## Remove methods
345  
346 sub remove_from_Object {
347 my ($self, $object_id, $table, $code, $type) = @_;
348  
349 my $db = $self->db();
350
351 if(!defined($object_id)) {
352 throw("$table must have dbID.");
353 }
354 if (!defined($type)) {
355 $type = $table;
356 }
357
358 my $sth;
359 if (defined($code)) {
360 if ($db->dbc->driver() eq 'mysql') {
361 $sth = $self->prepare("DELETE a FROM " . $table . "_attrib a, attrib_type at " .
362 "WHERE a.attrib_type_id = at.attrib_type_id AND ".
363 "a." . $type . "_id = ? AND ".
364 "at.code like ?");
365 } else {
366 $sth = $self->prepare(qq{DELETE FROM ${table}_attrib
367 WHERE ${type}_id = ? AND
368 attrib_type_id IN
369 (SELECT attrib_type_id
370 FROM attrib_type
371 WHERE code LIKE ? ) });
372 }
373 $sth->bind_param(1,$object_id,SQL_INTEGER);
374 $sth->bind_param(2,$code,SQL_VARCHAR);
375 }
376 else{
377 $sth = $self->prepare("DELETE FROM " . $table . "_attrib " .
378 "WHERE " . $type . "_id = ?");
379 $sth->bind_param(1,$object_id,SQL_INTEGER);
380 }
381 $sth->execute();
382 $sth->finish();
383
384 return;
385  
386 }
387  
388  
389 sub remove_from_MiscFeature {
390 my ($self, $object, $code) = @_;
391  
392 assert_ref($object, 'Bio::EnsEMBL::MiscFeature');
393  
394 my $object_id = $object->dbID();
395 $self->remove_from_Object($object_id, 'misc', $code, 'misc_feature');
396  
397 return;
398  
399 }
400  
401 sub remove_from_Slice {
402 my ($self, $object, $code) = @_;
403  
404 assert_ref($object, 'Bio::EnsEMBL::Slice');
405  
406 my $object_id = $object->get_seq_region_id();
407 $self->remove_from_Object($object_id, 'seq_region', $code);
408  
409 return;
410  
411 }
412  
413 sub remove_from_Gene {
414 my ($self, $object, $code) = @_;
415  
416 assert_ref($object, 'Bio::EnsEMBL::Gene');
417  
418 my $object_id = $object->dbID();
419 $self->remove_from_Object($object_id, 'gene', $code);
420  
421 return;
422  
423 }
424  
425 sub remove_from_Transcript {
426 my ($self, $object, $code) = @_;
427  
428 assert_ref($object, 'Bio::EnsEMBL::Transcript');
429  
430 my $object_id = $object->dbID();
431 $self->remove_from_Object($object_id, 'transcript', $code);
432
433 return;
434  
435 }
436  
437 sub remove_from_RNAProduct {
438 my ($self, $object, $code) = @_;
439  
440 assert_ref($object, 'Bio::EnsEMBL::RNAProduct');
441  
442 my $object_id = $object->dbID();
443 $self->remove_from_Object($object_id, 'rnaproduct', $code);
444  
445 return;
446  
447 }
448  
449 sub remove_from_Translation {
450 my ($self, $object, $code) = @_;
451  
452 assert_ref($object, 'Bio::EnsEMBL::Translation');
453  
454 my $object_id = $object->dbID();
455 $self->remove_from_Object($object_id, 'translation', $code);
456
457 return;
458  
459 }
460  
461 sub remove_from_DnaDnaAlignFeature {
462 my ($self, $object, $code) = @_;
463  
464 assert_ref($object, 'Bio::EnsEMBL::DnaDnaAlignFeature');
465  
466 my $object_id = $object->dbID();
467 $self->remove_from_Object($object_id, 'dna_align_feature', $code);
468
469 return;
470  
471 }
472  
473  
474  
475 ## Fetch methods
476  
477 sub fetch_all {
478 throw("Use of method fetch_all not supported for attributes");
479 }
480  
481 sub fetch_by_code {
482 my ($self, $code) = @_;
483  
484 my $sql = "SELECT attrib_type_id, code, name, description " .
485 "FROM attrib_type
486 WHERE code = '$code' ";
487 my $sth = $self->prepare($sql);
488 $sth->execute();
489  
490 my ($attrib_type_id, $name, $desc);
491 $sth->bind_columns(\$attrib_type_id, \$code, \$name, \$desc);
492  
493 my @results = $sth->fetchrow_array;
494 $sth->finish();
495  
496 return \@results;
497 }
498  
499  
500  
501 sub fetch_all_by_Object {
502 my ($self, $object_id, $table, $code, $type) = @_;
503  
504 if (!defined $type) {
505 $type = $table;
506 }
507  
508 my $sql = "SELECT at.code, at.name, at.description, a.value " .
509 "FROM " . $table . "_attrib a, attrib_type at " .
510 "WHERE at.attrib_type_id = a.attrib_type_id ";
511  
512 if (defined($code)) {
513 $sql .= 'AND at.code like "' . $code . '" ';
514 }
515 if (defined($object_id)) {
516 $sql .= "AND a." .$type . "_id = " . $object_id;
517 }
518  
519 my $sth = $self->prepare($sql);
520 $sth->execute();
521 my $results = $self->_obj_from_sth($sth);
522 $sth->finish();
523  
524 return $results;
525 }
526  
527  
528 sub fetch_all_by_MiscFeature {
529 my ($self, $object, $code) = @_;
530  
531 my $object_id;
532  
533 if (defined($object)) {
534 assert_ref($object, 'Bio::EnsEMBL::MiscFeature');
535 $object_id = $object->dbID();
536 }
537  
538 my $results = $self->fetch_all_by_Object($object_id, 'misc', $code, 'misc_feature');
539  
540 return $results;
541  
542 }
543  
544 sub fetch_all_by_Slice {
545 my ($self, $object, $code) = @_;
546  
547 my $object_id;
548  
549 if (defined($object)) {
550 assert_ref($object, 'Bio::EnsEMBL::Slice');
551 $object_id = $object->get_seq_region_id();
552 }
553  
554 my $results = $self->fetch_all_by_Object($object_id, 'seq_region', $code);
555  
556 return $results;
557  
558 }
559  
560 sub fetch_all_by_Gene {
561 my ($self, $object, $code) = @_;
562  
563 my $object_id;
564  
565 if (defined($object)) {
566 assert_ref($object, 'Bio::EnsEMBL::Gene');
567 $object_id = $object->dbID();
568 }
569  
570 my $results = $self->fetch_all_by_Object($object_id, 'gene', $code);
571  
572 return $results;
573  
574 }
575  
576 sub fetch_all_by_Transcript {
577 my ($self, $object, $code) = @_;
578  
579 my $object_id;
580  
581 if (defined($object)) {
582 assert_ref($object, 'Bio::EnsEMBL::Transcript');
583 $object_id = $object->dbID();
584 }
585  
586 my $results = $self->fetch_all_by_Object($object_id, 'transcript', $code);
587  
588 return $results;
589  
590 }
591  
592 sub fetch_all_by_Translation {
593 my ($self, $object, $code) = @_;
594  
595 my $object_id;
596  
597 if (defined($object)) {
598 assert_ref($object, 'Bio::EnsEMBL::Translation');
599 $object_id = $object->dbID();
600 }
601  
602 my $results = $self->fetch_all_by_Object($object_id, 'translation', $code);
603
604 return $results;
605  
606 }
607  
608 sub fetch_all_by_RNAProduct {
609 my ($self, $object, $code) = @_;
610  
611 my $object_id;
612  
613 if (defined($object)) {
614 assert_ref($object, 'Bio::EnsEMBL::RNAProduct');
615 $object_id = $object->dbID();
616 }
617  
618 my $results = $self->fetch_all_by_Object($object_id, 'rnaproduct', $code);
619  
620 return $results;
621  
622 }
623  
624 sub fetch_all_by_DnaDnaAlignFeature {
625 my ($self, $object, $code) = @_;
626  
627 my $object_id;
628  
629 if (defined($object)) {
630 assert_ref($object, 'Bio::EnsEMBL::DnaDnaAlignFeature');
631 $object_id = $object->dbID();
632 }
633  
634 my $results = $self->fetch_all_by_Object($object_id, 'dna_align_feature', $code);
635
636 return $results;
637  
638 }
639  
640  
641  
642  
643 #
644 # _id_check
645 #
646 # backwards compatibility check:
647 # check if $ensID is an object; if so, return $obj->dbID
648 #
649  
650 sub _id_check {
651 my $self = shift;
652 my $ensID = shift;
653  
654 if ($ensID =~ /^\d+$/) {
655 return $ensID;
656  
657 } elsif ( ref($ensID) eq 'Bio::EnsEMBL::Gene'
658 or ref($ensID) eq 'Bio::EnsEMBL::Transcript'
659 or ref($ensID) eq 'Bio::EnsEMBL::Translation')
660 {
661  
662 warning("You should pass a dbID rather than an ensembl object to store the attribute on");
663  
664 if ($ensID->dbID) {
665 return $ensID->dbID;
666 } else {
667 throw("Ensembl object " . $ensID->display_id . " doesn't have a dbID, can't store attribute");
668 }
669  
670 } else {
671 throw("Invalid dbID");
672 }
673  
674 } ## end sub _id_check
675  
676 # _store_type
677  
678 sub _store_type {
679 my $self = shift;
680 my $attrib = shift;
681  
682 my $insert_ignore = $self->insert_ignore_clause();
683 my $sth1 = $self->prepare
684 ("${insert_ignore} INTO attrib_type (code, name, description) values (?, ?, ?)" );
685  
686  
687 $sth1->bind_param(1, $attrib->code, SQL_VARCHAR);
688 $sth1->bind_param(2, $attrib->name, SQL_VARCHAR);
689 $sth1->bind_param(3, $attrib->description, SQL_LONGVARCHAR);
690  
691 my $rows_inserted = $sth1->execute();
692  
693 my $atid = $self->last_insert_id('attrib_type_id', undef, 'attrib_type');
694  
695 if ($rows_inserted == 0) {
696 # the insert failed because the code is already stored
697 my $sth2 = $self->prepare("SELECT attrib_type_id FROM attrib_type " . "WHERE code = ?");
698 $sth2->bind_param(1, $attrib->code, SQL_VARCHAR);
699 $sth2->execute();
700 ($atid) = $sth2->fetchrow_array();
701  
702 $sth2->finish();
703  
704 if (!$atid) {
705 throw("Could not store or fetch attrib_type code [" . $attrib->code . "]\n" . "Wrong database user/permissions?");
706 }
707 }
708  
709 $sth1->finish();
710  
711 return $atid;
712 } ## end sub _store_type
713  
714 sub _obj_from_sth {
715 my $self = shift;
716 my $sth = shift;
717  
718 my ($code, $name, $desc, $value);
719 $sth->bind_columns(\$code, \$name, \$desc, \$value);
720  
721 my @results;
722 while ($sth->fetch()) {
723 push @results,
724 Bio::EnsEMBL::Attribute->new_fast({'code' => $code,
725 'name' => $name,
726 'description' => $desc,
727 'value' => $value});
728 }
729  
730 return \@results;
731 }
732  
733 1;
734