EnsEMBL/Storable.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 47 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 201 CC 18 SCAR 35.4
CC
18
Cov
0%
is_stored
90.0
get_all_DAS_Features
20.0
new
12.0
CRAP 34.4
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::Storable
34  
35 =head1 SYNOPSIS
36  
37 my $dbID = $storable_object->dbID();
38 my $adaptor = $storable_object->adaptor();
39 if ( $storable_object->is_stored($db_adaptor) ) { ... }
40  
41 =head1 DESCRIPTION
42  
43 This is a storable base class. All objects which are storable
44 in the database should inherit from this class. It provides two
45 getter/setters: dbID() adaptor(). And a is_stored() method that can be
46 used to determine if an object is already stored in a database.
47  
48 =cut
49  
50 use strict;
51 use warnings;
52  
53 package Bio::EnsEMBL::Storable;
54 $Bio::EnsEMBL::Storable::VERSION = '114.0.0_50'; # TRIAL
55 $Bio::EnsEMBL::Storable::VERSION = '114.0.050';
56  
57 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
58 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
59 use Bio::EnsEMBL::Utils::Scalar qw(assert_ref);
60 use Scalar::Util qw(weaken isweak);
61  
62 =head2 new
63  
64 Arg [-ADAPTOR] : Bio::EnsEMBL::DBSQL::BaseAdaptor
65 Arg [-dbID] : database internal id
66 Caller : internal calls
67 Description : create a new Storable object
68 Returntype : Bio::EnsEMBL::Storable
69 Exceptions : Adaptor not a Bio::EnsEMBL::DBSQL::BaseAdaptor
70 Status : Stable
71  
72 =cut
73  
74 sub new {
75 my $caller = shift;
76 my $class = ref($caller) || $caller;
77  
78 my ($adaptor, $dbID) = rearrange(['ADAPTOR', 'dbID'],@_);
79  
80 if($adaptor) {
81 if(!ref($adaptor) || !$adaptor->isa('Bio::EnsEMBL::DBSQL::BaseAdaptor')) {
82 throw('-ADAPTOR argument must be a Bio::EnsEMBL::DBSQL::BaseAdaptor');
83 }
84 }
85  
86 my $self = bless({'dbID' => $dbID}, $class);
87 $self->adaptor($adaptor);
88 return $self;
89 }
90  
91 =head2 new_fast
92  
93 Arg [1] : hashref to be blessed
94 Description: Construct a new Bio::EnsEMBL::Storable object using the hashref.
95 This is a very quick constructor that requires internal knowledge
96 of the class. This is used in speed critical sections of the code
97 where many objects need to be created quickly.
98 Exceptions : none
99 Returntype : Instance of Bio::EnsEMBL::Storable subclass
100 Caller : general, subclass constructors
101 Status : Stable
102  
103 =cut
104  
105  
106 sub new_fast {
107 my $class = shift;
108 my $hashref = shift;
109 my $self = bless $hashref, $class;
110 weaken($self->{adaptor}) if ( ! isweak($self->{adaptor}) );
111 return $self;
112 }
113  
114 =head2 dbID
115  
116 Arg [1] : int $dbID
117 Description: getter/setter for the database internal id
118 Returntype : int
119 Exceptions : none
120 Caller : general, set from adaptor on store
121 Status : Stable
122  
123 =cut
124  
125 sub dbID {
126 my $self = shift;
127 $self->{'dbID'} = shift if(@_);
128 return $self->{'dbID'};
129 }
130  
131  
132  
133 =head2 adaptor
134  
135 Arg [1] : Bio::EnsEMBL::DBSQL::BaseAdaptor $adaptor
136 Description: get/set for this objects Adaptor
137 Returntype : Bio::EnsEMBL::DBSQL::BaseAdaptor
138 Exceptions : none
139 Caller : general, set from adaptor on store
140 Status : Stable
141  
142 =cut
143  
144 sub adaptor {
145 my ($self, $adaptor) = @_;
146 if(scalar(@_) > 1) {
147 if(defined $adaptor) {
148 assert_ref($adaptor, 'Bio::EnsEMBL::DBSQL::BaseAdaptor', 'adaptor');
149 $self->{adaptor} = $adaptor;
150 weaken($self->{adaptor});
151 }
152 else {
153 $self->{adaptor} = undef;
154 }
155 }
156 return $self->{adaptor}
157 }
158  
159  
160  
161 =head2 is_stored
162  
163 Arg [1] : Bio::EnsEMBL::DBSQL::DBConnection
164 : or Bio::EnsEMBL::DBSQL::DBAdaptor
165 Example : do_something if($object->is_stored($db));
166 Description: Returns true if this object is stored in the provided database.
167 This works under the assumption that if the adaptor and dbID are
168 set and the database of the adaptor shares the port, dbname and
169 hostname with the provided database, this object is stored in
170 that database.
171 Returntype : 1 or 0
172 Exceptions : throw if dbID is set but adaptor is not
173 throw if adaptor is set but dbID is not
174 throw if incorrect argument is passed
175 Caller : store methods
176 Status : Stable
177  
178 =cut
179  
180 my $message_only_once =1;
181  
182 sub is_stored {
183 my $self = shift;
184 my $db = shift;
185  
186 if($db and $db->isa('Bio::EnsEMBL::DBSQL::DBAdaptor')) {
187 $db = $db->dbc();
188 }
189 if(!$db || !ref($db) || !$db->isa('Bio::EnsEMBL::DBSQL::DBConnection')) {
190 throw('db argument must be a Bio::EnsEMBL::DBSQL::DBConnection');
191 }
192  
193 my $adaptor = $self->{'adaptor'};
194 my $dbID = $self->{'dbID'};
195  
196 if($dbID && !$adaptor) {
197 if($message_only_once){
198 warning("Storable object has a dbID but not an adaptor.\n" .
199 'Storable objects must have neither OR both.');
200 $message_only_once = 0;
201 }
202 return 0;
203 }
204  
205 if($adaptor && !$dbID) {
206 if($message_only_once){
207 warning("Storable object has an adaptor but not a dbID.\n".
208 "Storable objects must have neither OR both.");
209 $message_only_once = 0;
210 }
211 return 0;
212 }
213  
214 return 0 if (!$adaptor && !$dbID);
215  
216 my $cur_db = $adaptor->dbc();
217  
218 #
219 # Databases are the same if they share the same port, host and username
220 #
221 if ( ($db->port()||'') eq ($cur_db->port()||'')
222 && ($db->host()||'') eq ($cur_db->host()||'')
223 && $db->dbname() eq $cur_db->dbname() )
224 {
225 return 1;
226 }
227  
228 return 0;
229 }
230  
231 sub get_all_DAS_Features{
232 my ($self, $slice) = @_;
233  
234 $self->{_das_features} ||= {}; # Cache
235 $self->{_das_styles} ||= {}; # Cache
236 $self->{_das_segments} ||= {}; # Cache
237 my %das_features;
238 my %das_styles;
239 my %das_segments;
240  
241 foreach my $dasfact( @{$self->get_all_DASFactories} ){
242 my $dsn = $dasfact->adaptor->dsn;
243 my $name = $dasfact->adaptor->name;
244 my $url = $dasfact->adaptor->url;
245  
246 # Construct a cache key : SOURCE_URL/TYPE
247 # Need the type to handle sources that serve multiple types of features
248  
249 my ($type) = ref($dasfact->adaptor->mapping) eq 'ARRAY' ? @{$dasfact->adaptor->mapping} : $dasfact->adaptor->mapping;
250 $type ||=$dasfact->adaptor->type;
251 my $key = join('/', $name, $type);
252  
253 if( $self->{_das_features}->{$key} ){ # Use cached
254 $das_features{$name} = $self->{_das_features}->{$key};
255 $das_styles{$name} = $self->{_das_styles}->{$key};
256 $das_segments{$name} = $self->{_das_segments}->{$key};
257 } else { # Get fresh data
258
259 my ($featref, $styleref, $segref) = ($type =~ /^ensembl_location/) ? ($dasfact->fetch_all_Features( $slice, $type )) : $dasfact->fetch_all_by_ID( $self );
260
261 $self->{_das_features}->{$key} = $featref;
262 $self->{_das_styles}->{$key} = $styleref;
263 $self->{_das_segments}->{$key} = $segref;
264 $das_features{$name} = $featref;
265 $das_styles{$name} = $styleref;
266 $das_segments{$name} = $segref;
267 }
268 }
269  
270 return (\%das_features, \%das_styles, \%das_segments);
271 }
272  
273 1;