ensembl-hive  2.8.1
MetaCoordContainer.pm
Go to the documentation of this file.
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 
33 use strict;
34 use warnings;
35 
36 use vars qw(@ISA);
37 use strict;
38 
41 
43 
44 sub new {
45  my $class = shift;
46 
47  my $self = $class->SUPER::new(@_);
48 
49  #
50  # Retrieve the list of the coordinate systems that features are stored
51  # in and cache them.
52  #
53 
54  my @coord_systems =
55  @{ $self->db()->dnadb()->get_CoordSystemAdaptor->fetch_all() };
56 
57  my @cs_ids;
58  foreach my $cs (@coord_systems) { push( @cs_ids, $cs->dbID() ) }
59 
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 )
65  . ')' );
66 
67  $sth->execute();
68 
69  while ( my ( $table_name, $cs_id, $max_length ) =
70  $sth->fetchrow_array() )
71  {
72  $table_name = lc($table_name);
73 
74  $self->{'_feature_cache'}->{$table_name} ||= [];
75 
76  push( @{ $self->{'_feature_cache'}->{$table_name} }, $cs_id );
77  $self->{'_max_len_cache'}->{$cs_id}->{$table_name} = $max_length;
78  }
79  $sth->finish();
80 
81  return $self;
82 } ## end sub new
83 
84 
85 
86 
87 =head2 fetch_all_CoordSystems_by_feature_type
88 
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.
96  Returntype : listref of Bio::EnsEMBL::CoordSystem objects
97  Exceptions : throw if name argument not provided
98  Caller : BaseFeatureAdaptor
99  Status : Stable
100 
101 =cut
102 
103 sub fetch_all_CoordSystems_by_feature_type {
104  my $self = shift;
105  my $table = lc(shift); #case insensitive matching
106 
107  throw('Name argument is required') unless $table;
108 
109  if(!$self->{'_feature_cache'}->{$table}) {
110  return [];
111  }
112 
113  my @cs_ids = @{$self->{'_feature_cache'}->{$table}};
114  my @coord_systems;
115 
116  my $csa = $self->db->get_CoordSystemAdaptor();
117 
118  foreach my $cs_id (@cs_ids) {
119  my $cs = $csa->fetch_by_dbID($cs_id);
120 
121  if(!$cs) {
122  throw("meta_coord table refers to non-existant coord_system $cs_id");
123  }
124 
125  push @coord_systems, $cs;
126  }
127 
128  return \@coord_systems;
129 }
130 
131 
132 
133 =head2 fetch_max_length_by_CoordSystem_feature_type
134 
135  Arg [1] : Bio::EnsEMBL::CoordSystem $cs
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
143  Status : Stable
144 
145 =cut
146 
147 
148 sub fetch_max_length_by_CoordSystem_feature_type {
149  my $self = shift;
150  my $cs = shift;
151  my $table = shift;
152 
153  if(!ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) {
154  throw('Bio::EnsEMBL::CoordSystem argument expected');
155  }
156 
157  throw("Table name argument is required") unless $table;
158 
159  return $self->{'_max_len_cache'}->{$cs->dbID()}->{lc($table)};
160 }
161 
162 
163 
164 =head2 add_feature_type
165 
166  Arg [1] : Bio::EnsEMBL::CoordSystem $cs
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.
178  Returntype : none
179  Exceptions : none
180  Caller : BaseFeatureAdaptor
181  Status : Stable
182 
183 =cut
184 
185 
186 sub add_feature_type {
187  my $self = shift;
188  my $cs = shift;
189  my $table = lc(shift);
190  my $length = shift;
191  if(!ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) {
192  throw('CoordSystem argument is required.');
193  }
194 
195  if(!$table) {
196  throw('Table argument is required.');
197  }
198 
199  my $cs_ids = $self->{'_feature_cache'}->{$table} || [];
200 
201  my ($exists) = grep {$cs->dbID() == $_} @$cs_ids;
202  if( $exists ) {
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;
213  }
214  return;
215  }
216 
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)
223  VALUES (?, ?, ?)
224  } );
225 
226  $sth->execute($cs->dbID, $table, $length );
227 
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;
232 
233  return;
234 }
235 
236 
237 =head2 remove_feature_type
238 
239  Arg [1] : Bio::EnsEMBL::CoordSystem $cs
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
247  left.
248  Returntype : none
249  Exceptions : none
250  Caller : BaseFeatureAdaptor
251  Status : Stable
252 
253 =cut
254 
255 sub remove_feature_type {
256  my $self = shift;
257  my $cs = shift;
258  my $table = lc(shift);
259  my $length = shift;
260  if(!ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) {
261  throw('CoordSystem argument is required.');
262  }
263 
264  if(!$table) {
265  throw('Table argument is required.');
266  }
267 
268  my $cs_ids = $self->{'_feature_cache'}->{$table} || [];
269 
270  my ($exists) = grep {$cs->dbID() == $_} @$cs_ids;
271  if( $exists ) {
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};
277  }
278 
279  return;
280 }
281 
282 
283 1;
Bio::EnsEMBL::Storable::dbID
public Int dbID()
Bio::EnsEMBL::CoordSystem
Definition: CoordSystem.pm:40
Bio::EnsEMBL::DBSQL::BaseAdaptor
Definition: BaseAdaptor.pm:71
Bio::EnsEMBL::Utils::Exception
Definition: Exception.pm:68