0
|
1 =head1 LICENSE
|
|
2
|
|
3 Copyright (c) 1999-2012 The European Bioinformatics Institute and
|
|
4 Genome Research Limited. All rights reserved.
|
|
5
|
|
6 This software is distributed under a modified Apache license.
|
|
7 For license details, please see
|
|
8
|
|
9 http://www.ensembl.org/info/about/code_licence.html
|
|
10
|
|
11 =head1 CONTACT
|
|
12
|
|
13 Please email comments or questions to the public Ensembl
|
|
14 developers list at <dev@ensembl.org>.
|
|
15
|
|
16 Questions may also be sent to the Ensembl help desk at
|
|
17 <helpdesk@ensembl.org>.
|
|
18
|
|
19 =cut
|
|
20
|
|
21 package Bio::EnsEMBL::DBSQL::MetaCoordContainer;
|
|
22
|
|
23 use strict;
|
|
24 use warnings;
|
|
25
|
|
26 use vars qw(@ISA);
|
|
27 use strict;
|
|
28
|
|
29 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
|
|
30 use Bio::EnsEMBL::Utils::Exception;
|
|
31
|
|
32 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
|
|
33
|
|
34 sub new {
|
|
35 my $class = shift;
|
|
36
|
|
37 my $self = $class->SUPER::new(@_);
|
|
38
|
|
39 #
|
|
40 # Retrieve the list of the coordinate systems that features are stored
|
|
41 # in and cache them.
|
|
42 #
|
|
43
|
|
44 my @coord_systems =
|
|
45 @{ $self->db()->dnadb()->get_CoordSystemAdaptor->fetch_all() };
|
|
46
|
|
47 my @cs_ids;
|
|
48 foreach my $cs (@coord_systems) { push( @cs_ids, $cs->dbID() ) }
|
|
49
|
|
50 my $sth = $self->prepare(
|
|
51 'SELECT mc.table_name, mc.coord_system_id, mc.max_length '
|
|
52 . 'FROM meta_coord mc '
|
|
53 . 'WHERE mc.coord_system_id in ('
|
|
54 . join( ',', @cs_ids )
|
|
55 . ')' );
|
|
56
|
|
57 $sth->execute();
|
|
58
|
|
59 while ( my ( $table_name, $cs_id, $max_length ) =
|
|
60 $sth->fetchrow_array() )
|
|
61 {
|
|
62 $table_name = lc($table_name);
|
|
63
|
|
64 $self->{'_feature_cache'}->{$table_name} ||= [];
|
|
65
|
|
66 push( @{ $self->{'_feature_cache'}->{$table_name} }, $cs_id );
|
|
67 $self->{'_max_len_cache'}->{$cs_id}->{$table_name} = $max_length;
|
|
68 }
|
|
69 $sth->finish();
|
|
70
|
|
71 return $self;
|
|
72 } ## end sub new
|
|
73
|
|
74
|
|
75
|
|
76
|
|
77 =head2 fetch_all_CoordSystems_by_feature_type
|
|
78
|
|
79 Arg [1] : string $table - the name of the table to retrieve coord systems
|
|
80 for. E.g. 'gene', 'exon', 'dna_align_feature'
|
|
81 Example : @css = @{$mcc->fetch_all_CoordSystems_by_feature_type('gene')};
|
|
82 Description: This retrieves the list of coordinate systems that features
|
|
83 in a particular table are stored. It is used internally by
|
|
84 the API to perform queries to these tables and to ensure that
|
|
85 features are only stored in appropriate coordinate systems.
|
|
86 Returntype : listref of Bio::EnsEMBL::CoordSystem objects
|
|
87 Exceptions : throw if name argument not provided
|
|
88 Caller : BaseFeatureAdaptor
|
|
89 Status : Stable
|
|
90
|
|
91 =cut
|
|
92
|
|
93 sub fetch_all_CoordSystems_by_feature_type {
|
|
94 my $self = shift;
|
|
95 my $table = lc(shift); #case insensitive matching
|
|
96
|
|
97 throw('Name argument is required') unless $table;
|
|
98
|
|
99 if(!$self->{'_feature_cache'}->{$table}) {
|
|
100 return [];
|
|
101 }
|
|
102
|
|
103 my @cs_ids = @{$self->{'_feature_cache'}->{$table}};
|
|
104 my @coord_systems;
|
|
105
|
|
106 my $csa = $self->db->get_CoordSystemAdaptor();
|
|
107
|
|
108 foreach my $cs_id (@cs_ids) {
|
|
109 my $cs = $csa->fetch_by_dbID($cs_id);
|
|
110
|
|
111 if(!$cs) {
|
|
112 throw("meta_coord table refers to non-existant coord_system $cs_id");
|
|
113 }
|
|
114
|
|
115 push @coord_systems, $cs;
|
|
116 }
|
|
117
|
|
118 return \@coord_systems;
|
|
119 }
|
|
120
|
|
121
|
|
122
|
|
123 =head2 fetch_max_length_by_CoordSystem_feature_type
|
|
124
|
|
125 Arg [1] : Bio::EnsEMBL::CoordSystem $cs
|
|
126 Arg [2] : string $table
|
|
127 Example : $max_len = $mcc->fetch_max_length_by_CoordSystem_feature_type($cs,'gene');
|
|
128 Description: Returns the maximum length of features of a given type in
|
|
129 a given coordinate system.
|
|
130 Returntype : int or undef
|
|
131 Exceptions : throw on incorrect argument
|
|
132 Caller : BaseFeatureAdaptor
|
|
133 Status : Stable
|
|
134
|
|
135 =cut
|
|
136
|
|
137
|
|
138 sub fetch_max_length_by_CoordSystem_feature_type {
|
|
139 my $self = shift;
|
|
140 my $cs = shift;
|
|
141 my $table = shift;
|
|
142
|
|
143 if(!ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) {
|
|
144 throw('Bio::EnsEMBL::CoordSystem argument expected');
|
|
145 }
|
|
146
|
|
147 throw("Table name argument is required") unless $table;
|
|
148
|
|
149 return $self->{'_max_len_cache'}->{$cs->dbID()}->{lc($table)};
|
|
150 }
|
|
151
|
|
152
|
|
153
|
|
154 =head2 add_feature_type
|
|
155
|
|
156 Arg [1] : Bio::EnsEMBL::CoordSystem $cs
|
|
157 The coordinate system to associate with a feature table
|
|
158 Arg [2] : string $table - the name of the table in which features of
|
|
159 a given coordinate system will be stored in
|
|
160 Arg [3] : int $length
|
|
161 This length is used to update the max_length in the database
|
|
162 and the internal cache.
|
|
163 Example : $csa->add_feature_table($chr_coord_system, 'gene');
|
|
164 Description: This function tells the coordinate system adaptor that
|
|
165 features from a specified table will be stored in a certain
|
|
166 coordinate system. If this information is not already stored
|
|
167 in the database it will be added.
|
|
168 Returntype : none
|
|
169 Exceptions : none
|
|
170 Caller : BaseFeatureAdaptor
|
|
171 Status : Stable
|
|
172
|
|
173 =cut
|
|
174
|
|
175
|
|
176 sub add_feature_type {
|
|
177 my $self = shift;
|
|
178 my $cs = shift;
|
|
179 my $table = lc(shift);
|
|
180 my $length = shift;
|
|
181 if(!ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) {
|
|
182 throw('CoordSystem argument is required.');
|
|
183 }
|
|
184
|
|
185 if(!$table) {
|
|
186 throw('Table argument is required.');
|
|
187 }
|
|
188
|
|
189 my $cs_ids = $self->{'_feature_cache'}->{$table} || [];
|
|
190
|
|
191 my ($exists) = grep {$cs->dbID() == $_} @$cs_ids;
|
|
192 if( $exists ) {
|
|
193 if( !$self->{'_max_len_cache'}->{$cs->dbID()}->{$table} ||
|
|
194 $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} < $length ) {
|
|
195 my $sth = $self->prepare('UPDATE meta_coord ' .
|
|
196 "SET max_length = $length " .
|
|
197 'WHERE coord_system_id = ? ' .
|
|
198 'AND table_name = ? '.
|
|
199 "AND (max_length<$length ".
|
|
200 "OR max_length is null)");
|
|
201 $sth->execute( $cs->dbID(), $table );
|
|
202 $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} = $length;
|
|
203 }
|
|
204 return;
|
|
205 }
|
|
206
|
|
207 #store the new tablename -> coord system relationship in the db
|
|
208 #ignore failures b/c during the pipeline multiple processes may try
|
|
209 #to update this table and only the first will be successful
|
|
210 my $sth = $self->prepare('INSERT IGNORE INTO meta_coord ' .
|
|
211 'SET coord_system_id = ?, ' .
|
|
212 'table_name = ?, ' .
|
|
213 'max_length = ? '
|
|
214 );
|
|
215
|
|
216 $sth->execute($cs->dbID, $table, $length );
|
|
217
|
|
218 #update the internal cache
|
|
219 $self->{'_feature_cache'}->{$table} ||= [];
|
|
220 push @{$self->{'_feature_cache'}->{$table}}, $cs->dbID();
|
|
221 $self->{'_max_len_cache'}->{$cs->dbID()}->{$table} = $length;
|
|
222
|
|
223 return;
|
|
224 }
|
|
225
|
|
226
|
|
227 1;
|