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