MetaCoordContainer.pm 5.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use strict;
use warnings;

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

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(@_);

  #
23
24
  # Retrieve the list of the coordinate systems that features are stored
  # in and cache them.
25
  #
26
27
28
29
30
31
32
33

  my $sth = $self->prepare(
              'SELECT mc.table_name, mc.coord_system_id, mc.max_length '
                . 'FROM meta_coord mc, coord_system cs '
                . 'WHERE mc.coord_system_id = cs.coord_system_id '
                . 'AND cs.species_id = ?' );

  $sth->bind_param( 1, $self->species_id(), SQL_INTEGER );
34
35
  $sth->execute();

36
37
38
39
40
41
42
  while ( my ( $table_name, $cs_id, $max_length ) =
          $sth->fetchrow_array() )
  {
    $self->{'_feature_cache'}->{ lc($table_name) } ||= [];
    push @{ $self->{'_feature_cache'}->{ lc($table_name) } }, $cs_id;
    $self->{'_max_len_cache'}->{$cs_id}->{ lc($table_name) } =
      $max_length;
43
44
45
46
  }
  $sth->finish();

  return $self;
47
} ## end sub new
48
49
50
51
52
53
54
55
56
57
58
59
60
61




=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
62
  Exceptions : throw if name argument not provided
63
  Caller     : BaseFeatureAdaptor
64
  Status     : Stable
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

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



98
99
100
101
102
103
104
105
106
107
108
=head2 fetch_max_length_by_CoordSystem_feature_type

  Arg [1]    : Bio::EnsEMBL::CoordSystem $cs
  Arg [2]    : string $table
  Example    : $max_len = 
                $mcc->fetch_max_length_by_CoordSystem_feature_type($cs,'gene');
  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
109
  Status     : Stable
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

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


129
130
131
132
133
134
135

=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
136
137
138
  Arg [3]    : int $length
               This length is used to update the max_length in the database
               and the internal cache. 
139
140
141
142
143
144
145
146
  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
147
  Status     : Stable
148
149
150
151
152
153
154
155

=cut


sub add_feature_type {
  my $self = shift;
  my $cs   = shift;
  my $table = lc(shift);
156
  my $length = shift;
157
158
159
160
161
162
163
164
165
166
167
  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;
168
  if( $exists ) {
Laura Clarke's avatar
 
Laura Clarke committed
169
170
    if( !$self->{'_max_len_cache'}->{$cs->dbID()}->{$table} ||
        $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} < $length ) {
171
      my $sth = $self->prepare('UPDATE meta_coord ' .
Laura Clarke's avatar
 
Laura Clarke committed
172
173
174
175
                               "SET max_length = $length " .
                               'WHERE coord_system_id = ? ' .
                               'AND table_name = ? '.
                               "AND (max_length<$length ".
176
                               "OR max_length is null)");
177
178
179
180
181
      $sth->execute( $cs->dbID(), $table );
      $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} = $length;
    }
    return;
  }
182
183
184
185
186
187

  #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 = ?, ' .
188
189
190
                                  'table_name = ?, ' .
			   'max_length = ? ' 
			  );
191

192
  $sth->execute($cs->dbID, $table, $length );
193
194
195
196

  #update the internal cache
  $self->{'_feature_cache'}->{$table} ||= [];
  push @{$self->{'_feature_cache'}->{$table}}, $cs->dbID();
197
  $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} = $length;
198
199
200
201
202
203

  return;
}


1;