3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
31 package Bio::EnsEMBL::DBSQL::MetaCoordContainer;
47 my $self = $class->SUPER::new(@_);
50 # Retrieve the list of the coordinate systems that features are stored
55 @{ $self->db()->dnadb()->get_CoordSystemAdaptor->fetch_all() };
58 foreach my $cs (@coord_systems) { push( @cs_ids, $cs->dbID() ) }
60 my $sth = $self->prepare(
61 'SELECT mc.table_name, mc.coord_system_id, mc.max_length '
62 .
'FROM meta_coord mc '
63 .
'WHERE mc.coord_system_id in ('
64 . join(
',', @cs_ids )
69 while ( my ( $table_name, $cs_id, $max_length ) =
70 $sth->fetchrow_array() )
72 $table_name = lc($table_name);
74 $self->{
'_feature_cache'}->{$table_name} ||= [];
76 push( @{ $self->{
'_feature_cache'}->{$table_name} }, $cs_id );
77 $self->{
'_max_len_cache'}->{$cs_id}->{$table_name} = $max_length;
87 =head2 fetch_all_CoordSystems_by_feature_type
89 Arg [1] :
string $table - the name of the table to retrieve coord systems
90 for. E.g.
'gene',
'exon',
'dna_align_feature'
91 Example : @css = @{$mcc->fetch_all_CoordSystems_by_feature_type(
'gene')};
92 Description: This retrieves the list of coordinate systems that features
93 in a particular table are stored. It is used internally by
94 the API to perform queries to these tables and to ensure that
95 features are only stored in appropriate coordinate systems.
97 Exceptions :
throw if name argument not provided
98 Caller : BaseFeatureAdaptor
103 sub fetch_all_CoordSystems_by_feature_type {
105 my $table = lc(shift); #
case insensitive matching
107 throw(
'Name argument is required') unless $table;
109 if(!$self->{
'_feature_cache'}->{$table}) {
113 my @cs_ids = @{$self->{
'_feature_cache'}->{$table}};
116 my $csa = $self->db->get_CoordSystemAdaptor();
118 foreach my $cs_id (@cs_ids) {
119 my $cs = $csa->fetch_by_dbID($cs_id);
122 throw(
"meta_coord table refers to non-existant coord_system $cs_id");
125 push @coord_systems, $cs;
128 return \@coord_systems;
133 =head2 fetch_max_length_by_CoordSystem_feature_type
136 Arg [2] :
string $table
137 Example : $max_len = $mcc->fetch_max_length_by_CoordSystem_feature_type($cs,
'gene');
138 Description: Returns the maximum length of features of a given type in
139 a given coordinate system.
140 Returntype :
int or undef
141 Exceptions :
throw on incorrect argument
142 Caller : BaseFeatureAdaptor
148 sub fetch_max_length_by_CoordSystem_feature_type {
153 if(!ref($cs) || !$cs->isa(
'Bio::EnsEMBL::CoordSystem')) {
154 throw(
'Bio::EnsEMBL::CoordSystem argument expected');
157 throw(
"Table name argument is required") unless $table;
159 return $self->{
'_max_len_cache'}->{$cs->
dbID()}->{lc($table)};
164 =head2 add_feature_type
167 The coordinate system to associate with a feature table
168 Arg [2] :
string $table - the name of the table in which features of
169 a given coordinate system will be stored in
170 Arg [3] :
int $length
171 This length is used to update the max_length in the database
172 and the
internal cache.
173 Example : $csa->add_feature_table($chr_coord_system,
'gene');
174 Description: This
function tells the coordinate system adaptor that
175 features from a specified table will be stored in a certain
176 coordinate system. If
this information is not already stored
177 in the database it will be added.
180 Caller : BaseFeatureAdaptor
186 sub add_feature_type {
189 my $table = lc(shift);
191 if(!ref($cs) || !$cs->isa(
'Bio::EnsEMBL::CoordSystem')) {
192 throw(
'CoordSystem argument is required.');
196 throw(
'Table argument is required.');
199 my $cs_ids = $self->{
'_feature_cache'}->{$table} || [];
201 my ($exists) = grep {$cs->dbID() == $_} @$cs_ids;
203 if( !$self->{
'_max_len_cache'}->{$cs->dbID()}->{$table} ||
204 $self->{
'_max_len_cache'}->{$cs->dbID()}->{$table} < $length ) {
205 my $sth = $self->prepare(
'UPDATE meta_coord ' .
206 "SET max_length = $length " .
207 'WHERE coord_system_id = ? ' .
208 'AND table_name = ? '.
209 "AND (max_length<$length ".
210 "OR max_length is null)");
211 $sth->execute( $cs->dbID(), $table );
212 $self->{
'_max_len_cache'}->{$cs->dbID()}->{$table} = $length;
217 #store the new tablename -> coord system relationship in the db
218 #ignore failures b/c during the pipeline multiple processes may try
219 #to update this table and only the first will be successful
220 my $insert_ignore = $self->insert_ignore_clause();
221 my $sth = $self->prepare(qq{ ${insert_ignore} INTO meta_coord
222 (coord_system_id, table_name, max_length)
226 $sth->execute($cs->dbID, $table, $length );
228 #update the internal cache
229 $self->{
'_feature_cache'}->{$table} ||= [];
230 push @{$self->{
'_feature_cache'}->{$table}}, $cs->dbID();
231 $self->{
'_max_len_cache'}->{$cs->dbID()}->{$table} = $length;
237 =head2 remove_feature_type
240 The coordinate system to associate with a feature table
241 Arg [2] :
string $table - the name of the table in which features of
242 a given coordinate system are being removed
243 Example : $csa->remove_feature_table($chr_coord_system,
'gene');
244 Description: This
function tells the coordinate system adaptor that
245 features have been fully removed from a certain coordinate system.
246 Use with caution as it will cause issues
if there are still features
250 Caller : BaseFeatureAdaptor
255 sub remove_feature_type {
258 my $table = lc(shift);
260 if(!ref($cs) || !$cs->isa(
'Bio::EnsEMBL::CoordSystem')) {
261 throw(
'CoordSystem argument is required.');
265 throw(
'Table argument is required.');
268 my $cs_ids = $self->{
'_feature_cache'}->{$table} || [];
270 my ($exists) = grep {$cs->dbID() == $_} @$cs_ids;
272 my $sth = $self->prepare(
'DELETE FROM meta_coord ' .
273 'WHERE coord_system_id = ? ' .
274 'AND table_name = ? ');
275 $sth->execute( $cs->dbID(), $table );
276 delete $self->{
'_max_len_cache'}->{$cs->dbID()}->{$table};