EnsEMBL/DBSQL/SimpleFeatureAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 139 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 30 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 248 CC 33 SCAR 63.1
CC
33
Cov
0%
_objs_from_sth
812.0
store
42.0
_tables
2.0
CRAP 547.7
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 =head1 NAME
32  
33 Bio::EnsEMBL::DBSQL::SimpleFeatureAdaptor
34  
35 =head1 SYNOPSIS
36  
37 my $registry = 'Bio::EnsEMBL::Registry';
38  
39 $registry->
40 load_registry_from_db( ...
41  
42 my $sfa =
43 $registry->get_adaptor('homo sapiens', 'core', 'SimpleFeature');
44  
45 print ref($sfa), "\n";
46  
47 my $sf_aref =
48 $sfa->fetch_all;
49  
50 print scalar @$sf_aref, "\n";
51  
52 =head1 DESCRIPTION
53  
54 Simple Feature Adaptor - database access for simple features
55  
56 =head1 METHODS
57  
58 =cut
59  
60 package Bio::EnsEMBL::DBSQL::SimpleFeatureAdaptor;
61 $Bio::EnsEMBL::DBSQL::SimpleFeatureAdaptor::VERSION = '114.0.0_50'; # TRIAL
62 $Bio::EnsEMBL::DBSQL::SimpleFeatureAdaptor::VERSION = '114.0.050';
63 use vars qw(@ISA);
64 use strict;
65  
66 use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
67 use Bio::EnsEMBL::SimpleFeature;
68 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
69  
70 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor);
71  
72  
73 =head2 store
74  
75 Arg [1] : list of Bio::EnsEMBL::SimpleFeatures @sf
76 the simple features to store in the database
77 Example : $simple_feature_adaptor->store(@simple_feats);
78 Description: Stores a list of simple feature objects in the database
79 Returntype : none
80 Exceptions : thrown if @sf is not defined, if any of the features do not
81 have an attached slice.
82 or if any elements of @sf are not Bio::EnsEMBL::SimpleFeatures
83 Caller : general
84 Status : Stable
85  
86 =cut
87  
88 sub store{
89 my ($self,@sf) = @_;
90  
91 if( scalar(@sf) == 0 ) {
92 throw("Must call store with list of SimpleFeatures");
93 }
94  
95 my $sth = $self->prepare
96 ("INSERT INTO simple_feature (seq_region_id, seq_region_start, " .
97 "seq_region_end, seq_region_strand, " .
98 "display_label, analysis_id, score) " .
99 "VALUES (?,?,?,?,?,?,?)");
100  
101 my $db = $self->db();
102 my $analysis_adaptor = $db->get_AnalysisAdaptor();
103  
104 FEATURE: foreach my $sf ( @sf ) {
105  
106 if( !ref $sf || !$sf->isa("Bio::EnsEMBL::SimpleFeature") ) {
107 throw("SimpleFeature must be an Ensembl SimpleFeature, " .
108 "not a [".ref($sf)."]");
109 }
110  
111 if($sf->is_stored($db)) {
112 warning("SimpleFeature [".$sf->dbID."] is already stored" .
113 " in this database.");
114 next FEATURE;
115 }
116  
117 if(!defined($sf->analysis)) {
118 throw("An analysis must be attached to the features to be stored.");
119 }
120  
121 #store the analysis if it has not been stored yet
122 if(!$sf->analysis->is_stored($db)) {
123 $analysis_adaptor->store($sf->analysis());
124 }
125  
126 my $original = $sf;
127 my $seq_region_id;
128 ($sf, $seq_region_id) = $self->_pre_store($sf);
129  
130 $sth->bind_param(1,$seq_region_id,SQL_INTEGER);
131 $sth->bind_param(2,$sf->start,SQL_INTEGER);
132 $sth->bind_param(3,$sf->end,SQL_INTEGER);
133 $sth->bind_param(4,$sf->strand,SQL_TINYINT);
134 $sth->bind_param(5,$sf->display_label,SQL_VARCHAR);
135 $sth->bind_param(6,$sf->analysis->dbID,SQL_INTEGER);
136 $sth->bind_param(7,$sf->score,SQL_DOUBLE);
137  
138 $sth->execute();
139  
140 $original->dbID($self->last_insert_id('simple_feature_id', undef, 'simple_feature'));
141 $original->adaptor($self);
142 }
143 }
144  
145  
146 =head2 _tables
147  
148 Arg [1] : none
149 Example : none
150 Description: PROTECTED implementation of superclass abstract method
151 returns the names, aliases of the tables to use for queries
152 Returntype : list of listrefs of strings
153 Exceptions : none
154 Caller : internal
155 Status : Stable
156  
157 =cut
158  
159 sub _tables {
160 my $self = shift;
161
162 return ['simple_feature', 'sf'];
163 }
164  
165  
166 =head2 _columns
167  
168 Arg [1] : none
169 Example : none
170 Description: PROTECTED implementation of superclass abstract method
171 returns a list of columns to use for queries
172 Returntype : list of strings
173 Exceptions : none
174 Caller : internal
175 Status : Stable
176  
177 =cut
178  
179 sub _columns {
180 my $self = shift;
181  
182 return qw( sf.simple_feature_id
183 sf.seq_region_id sf.seq_region_start sf.seq_region_end
184 sf.seq_region_strand sf.display_label sf.analysis_id sf.score );
185 }
186  
187  
188 =head2 _objs_from_sth
189  
190 Arg [1] : hash reference $hashref
191 Example : none
192 Description: PROTECTED implementation of superclass abstract method.
193 creates SimpleFeatures from an executed DBI statement handle.
194 Returntype : list reference to Bio::EnsEMBL::SimpleFeature objects
195 Exceptions : none
196 Caller : internal
197 Status : Stable
198  
199 =cut
200  
201 sub _objs_from_sth {
202 my ($self, $sth, $mapper, $dest_slice) = @_;
203  
204 #
205 # This code is ugly because an attempt has been made to remove as many
206 # function calls as possible for speed purposes. Thus many caches and
207 # a fair bit of gymnastics is used.
208 #
209  
210 my $sa = $self->db()->get_SliceAdaptor();
211 my $aa = $self->db()->get_AnalysisAdaptor();
212  
213 my @features;
214 my %analysis_hash;
215 my %slice_hash;
216 my %sr_name_hash;
217 my %sr_cs_hash;
218
219 my(
220 $simple_feature_id, $seq_region_id, $seq_region_start,
221 $seq_region_end, $seq_region_strand, $display_label,
222 $analysis_id, $score);
223  
224 $sth->bind_columns(\(
225 $simple_feature_id, $seq_region_id, $seq_region_start,
226 $seq_region_end, $seq_region_strand, $display_label,
227 $analysis_id, $score));
228
229 my $dest_slice_start;
230 my $dest_slice_end;
231 my $dest_slice_strand;
232 my $dest_slice_length;
233 my $dest_slice_cs;
234 my $dest_slice_sr_name;
235 my $dest_slice_sr_id;
236 my $asma;
237  
238 if ($dest_slice) {
239 $dest_slice_start = $dest_slice->start();
240 $dest_slice_end = $dest_slice->end();
241 $dest_slice_strand = $dest_slice->strand();
242 $dest_slice_length = $dest_slice->length();
243 $dest_slice_cs = $dest_slice->coord_system();
244 $dest_slice_sr_name = $dest_slice->seq_region_name();
245 $dest_slice_sr_id = $dest_slice->get_seq_region_id();
246 $asma = $self->db->get_AssemblyMapperAdaptor();
247 }
248  
249 FEATURE: while($sth->fetch()) {
250  
251 #get the analysis object
252 my $analysis = $analysis_hash{$analysis_id} ||= $aa->fetch_by_dbID($analysis_id);
253 $analysis_hash{$analysis_id} = $analysis;
254  
255 #need to get the internal_seq_region, if present
256 $seq_region_id = $self->get_seq_region_id_internal($seq_region_id);
257 my $slice = $slice_hash{"ID:".$seq_region_id};
258  
259 if (!$slice) {
260 $slice = $sa->fetch_by_seq_region_id($seq_region_id);
261 $slice_hash{"ID:".$seq_region_id} = $slice;
262 $sr_name_hash{$seq_region_id} = $slice->seq_region_name();
263 $sr_cs_hash{$seq_region_id} = $slice->coord_system();
264 }
265  
266 #obtain a mapper if none was defined, but a dest_seq_region was
267 if(!$mapper && $dest_slice && !$dest_slice_cs->equals($slice->coord_system)) {
268 $mapper = $asma->fetch_by_CoordSystems($dest_slice_cs, $slice->coord_system);
269 }
270  
271 my $sr_name = $sr_name_hash{$seq_region_id};
272 my $sr_cs = $sr_cs_hash{$seq_region_id};
273  
274 #
275 # remap the feature coordinates to another coord system
276 # if a mapper was provided
277 #
278  
279 if ($mapper) {
280  
281 if (defined $dest_slice && $mapper->isa('Bio::EnsEMBL::ChainedAssemblyMapper') ) {
282 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
283 $mapper->map($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs, 1, $dest_slice);
284  
285 } else {
286 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
287 $mapper->fastmap($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs);
288 }
289  
290 #skip features that map to gaps or coord system boundaries
291 next FEATURE if (!defined($seq_region_id));
292  
293 #get a slice in the coord system we just mapped to
294 $slice = $slice_hash{"ID:".$seq_region_id} ||= $sa->fetch_by_seq_region_id($seq_region_id);
295 }
296  
297 #
298 # If a destination slice was provided convert the coords.
299 #
300 if (defined($dest_slice)) {
301 my $seq_region_len = $dest_slice->seq_region_length();
302  
303 if ( $dest_slice_strand == 1 ) {
304 $seq_region_start = $seq_region_start - $dest_slice_start + 1;
305 $seq_region_end = $seq_region_end - $dest_slice_start + 1;
306  
307 if ( $dest_slice->is_circular ) {
308 # Handle circular chromosomes.
309  
310 if ( $seq_region_start > $seq_region_end ) {
311 # Looking at a feature overlapping the chromosome origin.
312  
313 if ( $seq_region_end > $dest_slice_start ) {
314 # Looking at the region in the beginning of the chromosome
315 $seq_region_start -= $seq_region_len;
316 }
317 if ( $seq_region_end < 0 ) {
318 $seq_region_end += $seq_region_len;
319 }
320 } else {
321 if ($dest_slice_start > $dest_slice_end && $seq_region_end < 0) {
322 # Looking at the region overlapping the chromosome
323 # origin and a feature which is at the beginning of the
324 # chromosome.
325 $seq_region_start += $seq_region_len;
326 $seq_region_end += $seq_region_len;
327 }
328 }
329 }
330 } else {
331  
332 my $start = $dest_slice_end - $seq_region_end + 1;
333 my $end = $dest_slice_end - $seq_region_start + 1;
334  
335 if ($dest_slice->is_circular()) {
336  
337 if ($dest_slice_start > $dest_slice_end) {
338 # slice spans origin or replication
339  
340 if ($seq_region_start >= $dest_slice_start) {
341 $end += $seq_region_len;
342 $start += $seq_region_len if $seq_region_end > $dest_slice_start;
343  
344 } elsif ($seq_region_start <= $dest_slice_end) {
345 # do nothing
346 } elsif ($seq_region_end >= $dest_slice_start) {
347 $start += $seq_region_len;
348 $end += $seq_region_len;
349  
350 } elsif ($seq_region_end <= $dest_slice_end) {
351 $end += $seq_region_len if $end < 0;
352  
353 } elsif ($seq_region_start > $seq_region_end) {
354 $end += $seq_region_len;
355 }
356  
357 } else {
358  
359 if ($seq_region_start <= $dest_slice_end and $seq_region_end >= $dest_slice_start) {
360 # do nothing
361 } elsif ($seq_region_start > $seq_region_end) {
362 if ($seq_region_start <= $dest_slice_end) {
363 $start -= $seq_region_len;
364 } elsif ($seq_region_end >= $dest_slice_start) {
365 $end += $seq_region_len;
366 }
367 }
368 }
369 }
370  
371 $seq_region_start = $start;
372 $seq_region_end = $end;
373 $seq_region_strand *= -1;
374  
375 } ## end else [ if ( $dest_slice_strand...)]
376  
377 # Throw away features off the end of the requested slice or on
378 # different seq_region.
379 if ($seq_region_end < 1
380 || $seq_region_start > $dest_slice_length
381 || ($dest_slice_sr_id != $seq_region_id)) {
382 next FEATURE;
383 }
384 $slice = $dest_slice;
385 }
386  
387 push( @features,
388 $self->_create_feature_fast(
389 'Bio::EnsEMBL::SimpleFeature', {
390 'start' => $seq_region_start,
391 'end' => $seq_region_end,
392 'strand' => $seq_region_strand,
393 'slice' => $slice,
394 'analysis' => $analysis,
395 'adaptor' => $self,
396 'dbID' => $simple_feature_id,
397 'display_label' => $display_label,
398 'score' => $score
399 } ) );
400  
401 }
402  
403 return \@features;
404 }
405  
406  
407 =head2 list_dbIDs
408  
409 Arg [1] : none
410 Example : @feature_ids = @{$simple_feature_adaptor->list_dbIDs()};
411 Description: Gets an array of internal ids for all simple features in the current db
412 Arg[1] : <optional> int. not 0 for the ids to be sorted by the seq_region.
413 Returntype : list of ints
414 Exceptions : none
415 Caller : ?
416 Status : Stable
417  
418 =cut
419  
420 sub list_dbIDs {
421 my ($self, $ordered) = @_;
422  
423 return $self->_list_dbIDs("simple_feature", undef, $ordered);
424 }
425  
426 1;