MetaCoordContainer.pm 7.65 KB
Newer Older
1 2
=head1 LICENSE

3
Copyright [1999-2013] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
4

5 6 7 8 9 10 11 12 13 14 15 16 17
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut
18 19 20 21 22


=head1 CONTACT

  Please email comments or questions to the public Ensembl
Magali Ruffier's avatar
Magali Ruffier committed
23
  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
24 25

  Questions may also be sent to the Ensembl help desk at
Magali Ruffier's avatar
Magali Ruffier committed
26
  <http://www.ensembl.org/Help/Contact>.
27 28

=cut
29 30 31

package Bio::EnsEMBL::DBSQL::MetaCoordContainer;

32 33 34
use strict;
use warnings;

35 36 37 38 39 40 41 42 43 44 45 46 47 48
use vars qw(@ISA);
use strict;

use Bio::EnsEMBL::DBSQL::BaseAdaptor;
use Bio::EnsEMBL::Utils::Exception;

@ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);

sub new {
  my $class = shift;

  my $self = $class->SUPER::new(@_);

  #
49 50
  # Retrieve the list of the coordinate systems that features are stored
  # in and cache them.
51
  #
52

53 54 55 56 57 58
  my @coord_systems =
    @{ $self->db()->dnadb()->get_CoordSystemAdaptor->fetch_all() };

  my @cs_ids;
  foreach my $cs (@coord_systems) { push( @cs_ids, $cs->dbID() ) }

59 60
  my $sth = $self->prepare(
              'SELECT mc.table_name, mc.coord_system_id, mc.max_length '
61 62 63 64
                . 'FROM meta_coord mc '
                . 'WHERE mc.coord_system_id in ('
                . join( ',', @cs_ids )
                . ')' );
65

66 67
  $sth->execute();

68 69 70
  while ( my ( $table_name, $cs_id, $max_length ) =
          $sth->fetchrow_array() )
  {
71 72 73 74 75 76
    $table_name = lc($table_name);

    $self->{'_feature_cache'}->{$table_name} ||= [];

    push( @{ $self->{'_feature_cache'}->{$table_name} }, $cs_id );
    $self->{'_max_len_cache'}->{$cs_id}->{$table_name} = $max_length;
77 78 79 80
  }
  $sth->finish();

  return $self;
81
} ## end sub new
82 83 84 85 86 87 88 89 90 91 92 93 94 95




=head2 fetch_all_CoordSystems_by_feature_type

  Arg [1]    : string $table - the name of the table to retrieve coord systems
               for.  E.g. 'gene', 'exon', 'dna_align_feature'
  Example    : @css = @{$mcc->fetch_all_CoordSystems_by_feature_type('gene')};
  Description: This retrieves the list of coordinate systems that features
               in a particular table are stored.  It is used internally by
               the API to perform queries to these tables and to ensure that
               features are only stored in appropriate coordinate systems.
  Returntype : listref of Bio::EnsEMBL::CoordSystem objects
96
  Exceptions : throw if name argument not provided
97
  Caller     : BaseFeatureAdaptor
98
  Status     : Stable
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

=cut

sub fetch_all_CoordSystems_by_feature_type {
  my $self = shift;
  my $table = lc(shift); #case insensitive matching

  throw('Name argument is required') unless $table;

  if(!$self->{'_feature_cache'}->{$table}) {
    return [];
  }

  my @cs_ids = @{$self->{'_feature_cache'}->{$table}};
  my @coord_systems;

  my $csa = $self->db->get_CoordSystemAdaptor();

  foreach my $cs_id (@cs_ids) {
    my $cs = $csa->fetch_by_dbID($cs_id);

    if(!$cs) {
      throw("meta_coord table refers to non-existant coord_system $cs_id");
    }

    push @coord_systems, $cs;
  }

  return \@coord_systems;
}



132 133 134 135
=head2 fetch_max_length_by_CoordSystem_feature_type

  Arg [1]    : Bio::EnsEMBL::CoordSystem $cs
  Arg [2]    : string $table
Kieron Taylor's avatar
Kieron Taylor committed
136
  Example    : $max_len = $mcc->fetch_max_length_by_CoordSystem_feature_type($cs,'gene');
137 138 139 140 141
  Description: Returns the maximum length of features of a given type in
               a given coordinate system.
  Returntype : int or undef
  Exceptions : throw on incorrect argument
  Caller     : BaseFeatureAdaptor
142
  Status     : Stable
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

=cut


sub fetch_max_length_by_CoordSystem_feature_type {
  my $self = shift;
  my $cs = shift;
  my $table = shift;

  if(!ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) {
    throw('Bio::EnsEMBL::CoordSystem argument expected');
  }

  throw("Table name argument is required") unless $table;

  return $self->{'_max_len_cache'}->{$cs->dbID()}->{lc($table)};
}


162 163 164 165 166 167 168

=head2 add_feature_type

  Arg [1]    : Bio::EnsEMBL::CoordSystem $cs
               The coordinate system to associate with a feature table
  Arg [2]    : string $table - the name of the table in which features of
               a given coordinate system will be stored in
169 170 171
  Arg [3]    : int $length
               This length is used to update the max_length in the database
               and the internal cache. 
172 173 174 175 176 177 178 179
  Example    : $csa->add_feature_table($chr_coord_system, 'gene');
  Description: This function tells the coordinate system adaptor that
               features from a specified table will be stored in a certain
               coordinate system.  If this information is not already stored
               in the database it will be added.
  Returntype : none
  Exceptions : none
  Caller     : BaseFeatureAdaptor
180
  Status     : Stable
181 182 183 184 185 186 187 188

=cut


sub add_feature_type {
  my $self = shift;
  my $cs   = shift;
  my $table = lc(shift);
189
  my $length = shift;
190
  my $remove = shift;
191 192 193 194 195 196 197 198 199 200 201
  if(!ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) {
    throw('CoordSystem argument is required.');
  }

  if(!$table) {
    throw('Table argument is required.');
  }

  my $cs_ids = $self->{'_feature_cache'}->{$table} || [];

  my ($exists) = grep {$cs->dbID() == $_} @$cs_ids;
202
  if( $exists ) {
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    my $sth;
    if ($remove) {
      delete $self->{'_max_len_cache'}->{$cs->dbID()}->{$table};
      if (!$self->{'_max_len_cache'}->{$cs->dbID()}->{$table}) {
        $sth = $self->prepare('DELETE FROM meta_coord ' .
                               "WHERE coord_system_id = ? " . 
                               "AND table_name = ?");
        $sth->execute( $cs->dbID(), $table );
      } elsif ($self->{'_max_len_cache'}->{$cs->dbID()}->{$table} < $length) {
        $sth = $self->prepare('UPDATE meta_coord ' .
                               "SET max_length = " . $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} . " " .
                               'WHERE coord_system_id = ? ' .
                               'AND table_name = ? '.
                               "AND (max_length<$length ".
                               "OR max_length is null)");
        $sth->execute( $cs->dbID(), $table );
      }
    } else {
      if( !$self->{'_max_len_cache'}->{$cs->dbID()}->{$table} ||
        $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} < $length) {
        $sth = $self->prepare('UPDATE meta_coord ' .
Laura Clarke's avatar
 
Laura Clarke committed
224 225 226 227
                               "SET max_length = $length " .
                               'WHERE coord_system_id = ? ' .
                               'AND table_name = ? '.
                               "AND (max_length<$length ".
228
                               "OR max_length is null)");
229 230 231
        $sth->execute( $cs->dbID(), $table );
        $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} = $length;
      }
232 233 234
    }
    return;
  }
235 236 237 238 239 240

  #store the new tablename -> coord system relationship in the db
  #ignore failures b/c during the pipeline multiple processes may try
  #to update this table and only the first will be successful
  my $sth = $self->prepare('INSERT IGNORE INTO meta_coord ' .
                              'SET coord_system_id = ?, ' .
241 242 243
                                  'table_name = ?, ' .
			   'max_length = ? ' 
			  );
244

245
  $sth->execute($cs->dbID, $table, $length );
246 247 248 249

  #update the internal cache
  $self->{'_feature_cache'}->{$table} ||= [];
  push @{$self->{'_feature_cache'}->{$table}}, $cs->dbID();
250
  $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} = $length;
251 252 253 254 255 256

  return;
}


1;