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

3
  Copyright (c) 1999-2013 The European Bioinformatics Institute and
4
5
6
7
8
9
10
11
12
13
  Genome Research Limited.  All rights reserved.

  This software is distributed under a modified Apache license.
  For license details, please see

    http://www.ensembl.org/info/about/code_licence.html

=head1 CONTACT

  Please email comments or questions to the public Ensembl
14
  developers list at <dev@ensembl.org>.
15
16
17
18
19

  Questions may also be sent to the Ensembl help desk at
  <helpdesk@ensembl.org>.

=cut
20
21
22

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

23
24
25
use strict;
use warnings;

26
27
28
29
30
31
32
33
34
35
36
37
38
39
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(@_);

  #
40
41
  # Retrieve the list of the coordinate systems that features are stored
  # in and cache them.
42
  #
43

44
45
46
47
48
49
  my @coord_systems =
    @{ $self->db()->dnadb()->get_CoordSystemAdaptor->fetch_all() };

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

50
51
  my $sth = $self->prepare(
              'SELECT mc.table_name, mc.coord_system_id, mc.max_length '
52
53
54
55
                . 'FROM meta_coord mc '
                . 'WHERE mc.coord_system_id in ('
                . join( ',', @cs_ids )
                . ')' );
56

57
58
  $sth->execute();

59
60
61
  while ( my ( $table_name, $cs_id, $max_length ) =
          $sth->fetchrow_array() )
  {
62
63
64
65
66
67
    $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;
68
69
70
71
  }
  $sth->finish();

  return $self;
72
} ## end sub new
73
74
75
76
77
78
79
80
81
82
83
84
85
86




=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
87
  Exceptions : throw if name argument not provided
88
  Caller     : BaseFeatureAdaptor
89
  Status     : Stable
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

=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;
}



123
124
125
126
=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
127
  Example    : $max_len = $mcc->fetch_max_length_by_CoordSystem_feature_type($cs,'gene');
128
129
130
131
132
  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
133
  Status     : Stable
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

=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)};
}


153
154
155
156
157
158
159

=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
160
161
162
  Arg [3]    : int $length
               This length is used to update the max_length in the database
               and the internal cache. 
163
164
165
166
167
168
169
170
  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
171
  Status     : Stable
172
173
174
175
176
177
178
179

=cut


sub add_feature_type {
  my $self = shift;
  my $cs   = shift;
  my $table = lc(shift);
180
  my $length = shift;
181
182
183
184
185
186
187
188
189
190
191
  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;
192
  if( $exists ) {
Laura Clarke's avatar
 
Laura Clarke committed
193
194
    if( !$self->{'_max_len_cache'}->{$cs->dbID()}->{$table} ||
        $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} < $length ) {
195
      my $sth = $self->prepare('UPDATE meta_coord ' .
Laura Clarke's avatar
 
Laura Clarke committed
196
197
198
199
                               "SET max_length = $length " .
                               'WHERE coord_system_id = ? ' .
                               'AND table_name = ? '.
                               "AND (max_length<$length ".
200
                               "OR max_length is null)");
201
202
203
204
205
      $sth->execute( $cs->dbID(), $table );
      $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} = $length;
    }
    return;
  }
206
207
208
209
210
211

  #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 = ?, ' .
212
213
214
                                  'table_name = ?, ' .
			   'max_length = ? ' 
			  );
215

216
  $sth->execute($cs->dbID, $table, $length );
217
218
219
220

  #update the internal cache
  $self->{'_feature_cache'}->{$table} ||= [];
  push @{$self->{'_feature_cache'}->{$table}}, $cs->dbID();
221
  $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} = $length;
222
223
224
225
226
227

  return;
}


1;