EnsEMBL/DBSQL/MetaCoordContainer.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 20 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 162 CC 13 SCAR 27.5
CC
13
Cov
0%
add_feature_type
30.0
fetch_all_CoordSystems_by_feature_type
20.0
remove_feature_type
20.0
CRAP 15.7
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 package Bio::EnsEMBL::DBSQL::MetaCoordContainer;
32 $Bio::EnsEMBL::DBSQL::MetaCoordContainer::VERSION = '114.0.0_50'; # TRIAL
33 $Bio::EnsEMBL::DBSQL::MetaCoordContainer::VERSION = '114.0.050';
34 use strict;
35 use warnings;
36  
37 use vars qw(@ISA);
38 use strict;
39  
40 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
41 use Bio::EnsEMBL::Utils::Exception;
42  
43 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
44  
45 sub new {
46 my $class = shift;
47  
48 my $self = $class->SUPER::new(@_);
49  
50 #
51 # Retrieve the list of the coordinate systems that features are stored
52 # in and cache them.
53 #
54  
55 my @coord_systems =
56 @{ $self->db()->dnadb()->get_CoordSystemAdaptor->fetch_all() };
57  
58 my @cs_ids;
59 foreach my $cs (@coord_systems) { push( @cs_ids, $cs->dbID() ) }
60  
61 my $sth = $self->prepare(
62 'SELECT mc.table_name, mc.coord_system_id, mc.max_length '
63 . 'FROM meta_coord mc '
64 . 'WHERE mc.coord_system_id in ('
65 . join( ',', @cs_ids )
66 . ')' );
67  
68 $sth->execute();
69  
70 while ( my ( $table_name, $cs_id, $max_length ) =
71 $sth->fetchrow_array() )
72 {
73 $table_name = lc($table_name);
74  
75 $self->{'_feature_cache'}->{$table_name} ||= [];
76  
77 push( @{ $self->{'_feature_cache'}->{$table_name} }, $cs_id );
78 $self->{'_max_len_cache'}->{$cs_id}->{$table_name} = $max_length;
79 }
80 $sth->finish();
81  
82 return $self;
83 } ## end sub new
84  
85  
86  
87  
88 =head2 fetch_all_CoordSystems_by_feature_type
89  
90 Arg [1] : string $table - the name of the table to retrieve coord systems
91 for. E.g. 'gene', 'exon', 'dna_align_feature'
92 Example : @css = @{$mcc->fetch_all_CoordSystems_by_feature_type('gene')};
93 Description: This retrieves the list of coordinate systems that features
94 in a particular table are stored. It is used internally by
95 the API to perform queries to these tables and to ensure that
96 features are only stored in appropriate coordinate systems.
97 Returntype : listref of Bio::EnsEMBL::CoordSystem objects
98 Exceptions : throw if name argument not provided
99 Caller : BaseFeatureAdaptor
100 Status : Stable
101  
102 =cut
103  
104 sub fetch_all_CoordSystems_by_feature_type {
105 my $self = shift;
106 my $table = lc(shift); #case insensitive matching
107  
108 throw('Name argument is required') unless $table;
109  
110 if(!$self->{'_feature_cache'}->{$table}) {
111 return [];
112 }
113  
114 my @cs_ids = @{$self->{'_feature_cache'}->{$table}};
115 my @coord_systems;
116  
117 my $csa = $self->db->get_CoordSystemAdaptor();
118  
119 foreach my $cs_id (@cs_ids) {
120 my $cs = $csa->fetch_by_dbID($cs_id);
121  
122 if(!$cs) {
123 throw("meta_coord table refers to non-existant coord_system $cs_id");
124 }
125  
126 push @coord_systems, $cs;
127 }
128  
129 return \@coord_systems;
130 }
131  
132  
133  
134 =head2 fetch_max_length_by_CoordSystem_feature_type
135  
136 Arg [1] : Bio::EnsEMBL::CoordSystem $cs
137 Arg [2] : string $table
138 Example : $max_len = $mcc->fetch_max_length_by_CoordSystem_feature_type($cs,'gene');
139 Description: Returns the maximum length of features of a given type in
140 a given coordinate system.
141 Returntype : int or undef
142 Exceptions : throw on incorrect argument
143 Caller : BaseFeatureAdaptor
144 Status : Stable
145  
146 =cut
147  
148  
149 sub fetch_max_length_by_CoordSystem_feature_type {
150 my $self = shift;
151 my $cs = shift;
152 my $table = shift;
153  
154 if(!ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) {
155 throw('Bio::EnsEMBL::CoordSystem argument expected');
156 }
157  
158 throw("Table name argument is required") unless $table;
159  
160 return $self->{'_max_len_cache'}->{$cs->dbID()}->{lc($table)};
161 }
162  
163  
164  
165 =head2 add_feature_type
166  
167 Arg [1] : Bio::EnsEMBL::CoordSystem $cs
168 The coordinate system to associate with a feature table
169 Arg [2] : string $table - the name of the table in which features of
170 a given coordinate system will be stored in
171 Arg [3] : int $length
172 This length is used to update the max_length in the database
173 and the internal cache.
174 Example : $csa->add_feature_table($chr_coord_system, 'gene');
175 Description: This function tells the coordinate system adaptor that
176 features from a specified table will be stored in a certain
177 coordinate system. If this information is not already stored
178 in the database it will be added.
179 Returntype : none
180 Exceptions : none
181 Caller : BaseFeatureAdaptor
182 Status : Stable
183  
184 =cut
185  
186  
187 sub add_feature_type {
188 my $self = shift;
189 my $cs = shift;
190 my $table = lc(shift);
191 my $length = shift;
192 if(!ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) {
193 throw('CoordSystem argument is required.');
194 }
195  
196 if(!$table) {
197 throw('Table argument is required.');
198 }
199  
200 my $cs_ids = $self->{'_feature_cache'}->{$table} || [];
201  
202 my ($exists) = grep {$cs->dbID() == $_} @$cs_ids;
203 if( $exists ) {
204 if( !$self->{'_max_len_cache'}->{$cs->dbID()}->{$table} ||
205 $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} < $length ) {
206 my $sth = $self->prepare('UPDATE meta_coord ' .
207 "SET max_length = $length " .
208 'WHERE coord_system_id = ? ' .
209 'AND table_name = ? '.
210 "AND (max_length<$length ".
211 "OR max_length is null)");
212 $sth->execute( $cs->dbID(), $table );
213 $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} = $length;
214 }
215 return;
216 }
217  
218 #store the new tablename -> coord system relationship in the db
219 #ignore failures b/c during the pipeline multiple processes may try
220 #to update this table and only the first will be successful
221 my $insert_ignore = $self->insert_ignore_clause();
222 my $sth = $self->prepare(qq{ ${insert_ignore} INTO meta_coord
223 (coord_system_id, table_name, max_length)
224 VALUES (?, ?, ?)
225 } );
226  
227 $sth->execute($cs->dbID, $table, $length );
228  
229 #update the internal cache
230 $self->{'_feature_cache'}->{$table} ||= [];
231 push @{$self->{'_feature_cache'}->{$table}}, $cs->dbID();
232 $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} = $length;
233  
234 return;
235 }
236  
237  
238 =head2 remove_feature_type
239  
240 Arg [1] : Bio::EnsEMBL::CoordSystem $cs
241 The coordinate system to associate with a feature table
242 Arg [2] : string $table - the name of the table in which features of
243 a given coordinate system are being removed
244 Example : $csa->remove_feature_table($chr_coord_system, 'gene');
245 Description: This function tells the coordinate system adaptor that
246 features have been fully removed from a certain coordinate system.
247 Use with caution as it will cause issues if there are still features
248 left.
249 Returntype : none
250 Exceptions : none
251 Caller : BaseFeatureAdaptor
252 Status : Stable
253  
254 =cut
255  
256 sub remove_feature_type {
257 my $self = shift;
258 my $cs = shift;
259 my $table = lc(shift);
260 my $length = shift;
261 if(!ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) {
262 throw('CoordSystem argument is required.');
263 }
264  
265 if(!$table) {
266 throw('Table argument is required.');
267 }
268  
269 my $cs_ids = $self->{'_feature_cache'}->{$table} || [];
270  
271 my ($exists) = grep {$cs->dbID() == $_} @$cs_ids;
272 if( $exists ) {
273 my $sth = $self->prepare('DELETE FROM meta_coord ' .
274 'WHERE coord_system_id = ? ' .
275 'AND table_name = ? ');
276 $sth->execute( $cs->dbID(), $table );
277 delete $self->{'_max_len_cache'}->{$cs->dbID()}->{$table};
278 }
279  
280 return;
281 }
282  
283  
284 1;