Mercurial > repos > mahtabm > ensembl
comparison variant_effect_predictor/Bio/EnsEMBL/Storable.pm @ 0:1f6dce3d34e0
Uploaded
author | mahtabm |
---|---|
date | Thu, 11 Apr 2013 02:01:53 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1f6dce3d34e0 |
---|---|
1 =head1 LICENSE | |
2 | |
3 Copyright (c) 1999-2012 The European Bioinformatics Institute and | |
4 Genome Research Limited. All rights reserved. | |
5 | |
6 This software is distributed under a modified Apache license. | |
7 For license details, please see | |
8 | |
9 http://www.ensembl.org/info/about/code_licence.html | |
10 | |
11 =head1 CONTACT | |
12 | |
13 Please email comments or questions to the public Ensembl | |
14 developers list at <dev@ensembl.org>. | |
15 | |
16 Questions may also be sent to the Ensembl help desk at | |
17 <helpdesk@ensembl.org>. | |
18 | |
19 =cut | |
20 | |
21 =head1 NAME | |
22 | |
23 Bio::EnsEMBL::Storable | |
24 | |
25 =head1 SYNOPSIS | |
26 | |
27 my $dbID = $storable_object->dbID(); | |
28 my $adaptor = $storable_object->adaptor(); | |
29 if ( $storable_object->is_stored($db_adaptor) ) { ... } | |
30 | |
31 =head1 DESCRIPTION | |
32 | |
33 This is a storable base class. All objects which are storable | |
34 in the database should inherit from this class. It provides two | |
35 getter/setters: dbID() adaptor(). And a is_stored() method that can be | |
36 used to determine if an object is already stored in a database. | |
37 | |
38 =cut | |
39 | |
40 use strict; | |
41 use warnings; | |
42 | |
43 package Bio::EnsEMBL::Storable; | |
44 | |
45 | |
46 use Bio::EnsEMBL::Utils::Exception qw(throw warning); | |
47 use Bio::EnsEMBL::Utils::Argument qw(rearrange); | |
48 use Bio::EnsEMBL::Utils::Scalar qw(assert_ref); | |
49 use Scalar::Util qw(weaken); | |
50 | |
51 =head2 new | |
52 | |
53 Arg [-ADAPTOR] : Bio::EnsEMBL::DBSQL::BaseAdaptor | |
54 Arg [-dbID] : database internal id | |
55 Caller : internal calls | |
56 Description : create a new Storable object | |
57 Returntype : Bio::EnsEMBL::Storable | |
58 Exceptions : Adaptor not a Bio::EnsEMBL::DBSQL::BaseAdaptor | |
59 Status : Stable | |
60 | |
61 =cut | |
62 | |
63 sub new { | |
64 my $caller = shift; | |
65 my $class = ref($caller) || $caller; | |
66 | |
67 my ($adaptor, $dbID) = rearrange(['ADAPTOR', 'dbID'],@_); | |
68 | |
69 if($adaptor) { | |
70 if(!ref($adaptor) || !$adaptor->isa('Bio::EnsEMBL::DBSQL::BaseAdaptor')) { | |
71 throw('-ADAPTOR argument must be a Bio::EnsEMBL::DBSQL::BaseAdaptor'); | |
72 } | |
73 } | |
74 | |
75 my $self = bless({'dbID' => $dbID}, $class); | |
76 $self->adaptor($adaptor); | |
77 return $self; | |
78 } | |
79 | |
80 | |
81 =head2 dbID | |
82 | |
83 Arg [1] : int $dbID | |
84 Description: getter/setter for the database internal id | |
85 Returntype : int | |
86 Exceptions : none | |
87 Caller : general, set from adaptor on store | |
88 Status : Stable | |
89 | |
90 =cut | |
91 | |
92 sub dbID { | |
93 my $self = shift; | |
94 $self->{'dbID'} = shift if(@_); | |
95 return $self->{'dbID'}; | |
96 } | |
97 | |
98 | |
99 | |
100 =head2 adaptor | |
101 | |
102 Arg [1] : Bio::EnsEMBL::DBSQL::BaseAdaptor $adaptor | |
103 Description: get/set for this objects Adaptor | |
104 Returntype : Bio::EnsEMBL::DBSQL::BaseAdaptor | |
105 Exceptions : none | |
106 Caller : general, set from adaptor on store | |
107 Status : Stable | |
108 | |
109 =cut | |
110 | |
111 sub adaptor { | |
112 my ($self, $adaptor) = @_; | |
113 if(scalar(@_) > 1) { | |
114 if(defined $adaptor) { | |
115 assert_ref($adaptor, 'Bio::EnsEMBL::DBSQL::BaseAdaptor', 'adaptor'); | |
116 $self->{adaptor} = $adaptor; | |
117 weaken($self->{adaptor}); | |
118 } | |
119 else { | |
120 $self->{adaptor} = undef; | |
121 } | |
122 } | |
123 return $self->{adaptor} | |
124 } | |
125 | |
126 | |
127 | |
128 =head2 is_stored | |
129 | |
130 Arg [1] : Bio::EnsEMBL::DBSQL::DBConnection | |
131 : or Bio::EnsEMBL::DBSQL::DBAdaptor | |
132 Example : do_something if($object->is_stored($db)); | |
133 Description: Returns true if this object is stored in the provided database. | |
134 This works under the assumption that if the adaptor and dbID are | |
135 set and the database of the adaptor shares the port, dbname and | |
136 hostname with the provided database, this object is stored in | |
137 that database. | |
138 Returntype : 1 or 0 | |
139 Exceptions : throw if dbID is set but adaptor is not | |
140 throw if adaptor is set but dbID is not | |
141 throw if incorrect argument is passed | |
142 Caller : store methods | |
143 Status : Stable | |
144 | |
145 =cut | |
146 | |
147 my $message_only_once =1; | |
148 | |
149 sub is_stored { | |
150 my $self = shift; | |
151 my $db = shift; | |
152 | |
153 if($db and $db->isa('Bio::EnsEMBL::DBSQL::DBAdaptor')) { | |
154 $db = $db->dbc(); | |
155 } | |
156 if(!$db || !ref($db) || !$db->isa('Bio::EnsEMBL::DBSQL::DBConnection')) { | |
157 throw('db argument must be a Bio::EnsEMBL::DBSQL::DBConnection'); | |
158 } | |
159 | |
160 my $adaptor = $self->{'adaptor'}; | |
161 my $dbID = $self->{'dbID'}; | |
162 | |
163 if($dbID && !$adaptor) { | |
164 if($message_only_once){ | |
165 warning("Storable object has a dbID but not an adaptor.\n" . | |
166 'Storable objects must have neither OR both.'); | |
167 $message_only_once = 0; | |
168 } | |
169 return 0; | |
170 } | |
171 | |
172 if($adaptor && !$dbID) { | |
173 if($message_only_once){ | |
174 warning("Storable object has an adaptor but not a dbID.\n". | |
175 "Storable objects must have neither OR both."); | |
176 $message_only_once = 0; | |
177 } | |
178 return 0; | |
179 } | |
180 | |
181 return 0 if (!$adaptor && !$dbID); | |
182 | |
183 my $cur_db = $adaptor->dbc(); | |
184 | |
185 # | |
186 # Databases are the same if they share the same port, host and username | |
187 # | |
188 if ( $db->port() eq $cur_db->port() | |
189 && $db->host() eq $cur_db->host() | |
190 && $db->dbname() eq $cur_db->dbname() ) | |
191 { | |
192 return 1; | |
193 } | |
194 | |
195 return 0; | |
196 } | |
197 | |
198 sub get_all_DAS_Features{ | |
199 my ($self, $slice) = @_; | |
200 | |
201 $self->{_das_features} ||= {}; # Cache | |
202 $self->{_das_styles} ||= {}; # Cache | |
203 $self->{_das_segments} ||= {}; # Cache | |
204 my %das_features; | |
205 my %das_styles; | |
206 my %das_segments; | |
207 | |
208 foreach my $dasfact( @{$self->get_all_DASFactories} ){ | |
209 my $dsn = $dasfact->adaptor->dsn; | |
210 my $name = $dasfact->adaptor->name; | |
211 my $url = $dasfact->adaptor->url; | |
212 | |
213 # Construct a cache key : SOURCE_URL/TYPE | |
214 # Need the type to handle sources that serve multiple types of features | |
215 | |
216 my ($type) = ref($dasfact->adaptor->mapping) eq 'ARRAY' ? @{$dasfact->adaptor->mapping} : $dasfact->adaptor->mapping; | |
217 $type ||=$dasfact->adaptor->type; | |
218 my $key = join('/', $name, $type); | |
219 | |
220 if( $self->{_das_features}->{$key} ){ # Use cached | |
221 $das_features{$name} = $self->{_das_features}->{$key}; | |
222 $das_styles{$name} = $self->{_das_styles}->{$key}; | |
223 $das_segments{$name} = $self->{_das_segments}->{$key}; | |
224 } else { # Get fresh data | |
225 | |
226 my ($featref, $styleref, $segref) = ($type =~ /^ensembl_location/) ? ($dasfact->fetch_all_Features( $slice, $type )) : $dasfact->fetch_all_by_ID( $self ); | |
227 | |
228 $self->{_das_features}->{$key} = $featref; | |
229 $self->{_das_styles}->{$key} = $styleref; | |
230 $self->{_das_segments}->{$key} = $segref; | |
231 $das_features{$name} = $featref; | |
232 $das_styles{$name} = $styleref; | |
233 $das_segments{$name} = $segref; | |
234 } | |
235 } | |
236 | |
237 return (\%das_features, \%das_styles, \%das_segments); | |
238 } | |
239 | |
240 1; |