EnsEMBL/DBSQL/RepeatFeatureAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 194 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 42 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 336 CC 41 SCAR 61.4
CC
41
Cov
0%
_objs_from_sth
812.0
store
156.0
fetch_all_by_Slice
12.0
CRAP 465.4
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::RepeatFeatureAdaptor
34  
35 =head1 SYNOPSIS
36  
37 $rfa = $database_adaptor->get_RepeatFeatureAdaptor();
38  
39 my $repeat = $rfa->fetch_by_dbID(1234);
40 my @repeats = @{ $rfa->fetch_all_by_Slice($slice) };
41  
42 =head1 DESCRIPTION
43  
44 This is an adaptor for the retrieval and storage of RepeatFeature
45 objects from the database. Most of the implementation is in the
46 superclass BaseFeatureAdaptor.
47  
48 =head1 METHODS
49  
50 =cut
51  
52 package Bio::EnsEMBL::DBSQL::RepeatFeatureAdaptor;
53 $Bio::EnsEMBL::DBSQL::RepeatFeatureAdaptor::VERSION = '114.0.0_50'; # TRIAL
54 $Bio::EnsEMBL::DBSQL::RepeatFeatureAdaptor::VERSION = '114.0.050';
55 use strict;
56 use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
57 use Bio::EnsEMBL::RepeatFeature;
58 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
59 use Bio::EnsEMBL::Utils::Scalar qw/wrap_array/;
60  
61 use vars qw(@ISA);
62  
63 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor);
64  
65  
66 =head2 fetch_all_by_Slice
67  
68 Arg [1] : Bio::EnsEMBL::Slice $slice
69 Arg [2] : (optional) string $logic_name
70 Limits RepeatFeatures obtained to those having an Analysis with
71 of the specified logic_name. If no logic name is specified
72 Repeats of all analysis types are retrieved.
73 Arg [3] : (optional) string/array $repeat_type
74 Limits RepeatFeatures obtained to those of specified
75 repeat_type
76 Example : @rfeats = @{$rfa->fetch_all_by_Slice($slice, undef, 'Type II Transposons')};
77 @rfeats = @{$rfa->fetch_all_by_Slice($slice, undef, ['Type II Transposons', 'RNA repeats'])};
78 Description: Retrieves repeat features overlapping the area designated by
79 the provided slice argument. Returned features will be in
80 in the same coordinate system as the provided slice and will
81 have coordinates relative to the slice start.
82 Returntype : reference to a list of Bio::EnsEMBL::RepeatFeatures.
83 Exceptions : throw on bad argument
84 Caller : Slice::get_all_RepeatFeatures
85 Status : Stable
86  
87 =cut
88  
89 sub fetch_all_by_Slice {
90 my $self = shift;
91 my $slice = shift;
92 my $logic_name = shift;
93 my $repeat_type = shift;
94  
95 my $constraint = '';
96  
97 # MySQL was optimising the query the incorrect way when joining to
98 # the repeat_consensus table on type
99 $self->_straight_join(1);
100  
101 if($repeat_type) {
102 my $rta = wrap_array($repeat_type);
103 if(scalar(@{$rta}) > 1) {
104 $constraint .= sprintf('rc.repeat_type IN (%s)', join(q{,}, map {"'${_}'"} @{$rta}));
105 }
106 else {
107 $constraint .= "rc.repeat_type = '${repeat_type}'";
108 }
109 }
110  
111 my $result =
112 $self->fetch_all_by_Slice_constraint($slice,$constraint,$logic_name);
113  
114  
115 $self->_straight_join(0);
116  
117 return $result;
118 }
119  
120  
121 # _tablename
122 #
123 # Arg [1] : none
124 # Example : none
125 # Description: PROTECTED Implementation of abstract superclass method to
126 # provide the name of the tables to query
127 # Returntype : string
128 # Exceptions : none
129 # Caller : internal
130  
131  
132 sub _tables {
133 my $self = shift;
134  
135 return (['repeat_feature', 'r'], ['repeat_consensus', 'rc']);
136 }
137  
138  
139 # _columns
140 #
141 # Arg [1] : none
142 # Example : none
143 # Description: PROTECTED Implementation of abstract superclass method to
144 # provide the name of the columns to query
145 # Returntype : list of strings
146 # Exceptions : none
147 # Caller : internal
148  
149 sub _columns {
150 my $self = shift;
151  
152 return qw (r.repeat_feature_id
153 r.seq_region_id
154 r.seq_region_start
155 r.seq_region_end
156 r.seq_region_strand
157 r.repeat_consensus_id
158 r.repeat_start
159 r.repeat_end
160 r.analysis_id
161 r.score
162 rc.repeat_name
163 rc.repeat_class
164 rc.repeat_type
165 rc.repeat_consensus);
166 }
167  
168  
169 # _default_where_clause
170 # Arg [1] : none
171 # Example : none
172 # Description: Overrides superclass method to provide an additional
173 # table joining constraint before the SQL query is performed.
174 # Returntype : string
175 # Exceptions : none
176 # Caller : generic_fetch
177 #
178  
179 sub _default_where_clause {
180 my $self = shift;
181  
182 return 'r.repeat_consensus_id = rc.repeat_consensus_id';
183 }
184  
185  
186  
187 # Description: PROTECTED implementation of abstract superclass method.
188 # responsible for the creation of RepeatFeatures from a
189 # hashref generated from an SQL query
190  
191 sub _objs_from_sth {
192 my ($self, $sth, $mapper, $dest_slice) = @_;
193  
194 #
195 # This code is ugly because an attempt has been made to remove as many
196 # function calls as possible for speed purposes. Thus many caches and
197 # a fair bit of gymnastics is used.
198 #
199  
200 my $rca = $self->db()->get_RepeatConsensusAdaptor();
201 my $sa = $self->db()->get_SliceAdaptor();
202 my $aa = $self->db()->get_AnalysisAdaptor();
203  
204 my @features;
205 my %rc_hash;
206 my %analysis_hash;
207 my %slice_hash;
208 my %sr_name_hash;
209 my %sr_cs_hash;
210  
211 my($repeat_feature_id, $seq_region_id, $seq_region_start, $seq_region_end,
212 $seq_region_strand, $repeat_consensus_id, $repeat_start, $repeat_end,
213 $analysis_id, $score, $repeat_name, $repeat_class, $repeat_type,
214 $repeat_consensus);
215  
216 $sth->bind_columns( \$repeat_feature_id, \$seq_region_id, \$seq_region_start,
217 \$seq_region_end, \$seq_region_strand,
218 \$repeat_consensus_id, \$repeat_start,\$repeat_end,
219 \$analysis_id, \$score, \$repeat_name, \$repeat_class,
220 \$repeat_type, \$repeat_consensus );
221  
222 my $dest_slice_start;
223 my $dest_slice_end;
224 my $dest_slice_strand;
225 my $dest_slice_length;
226 my $dest_slice_cs;
227 my $dest_slice_sr_name;
228 my $dest_slice_sr_id;
229 my $asma;
230  
231 if ($dest_slice) {
232 $dest_slice_start = $dest_slice->start();
233 $dest_slice_end = $dest_slice->end();
234 $dest_slice_strand = $dest_slice->strand();
235 $dest_slice_length = $dest_slice->length();
236 $dest_slice_cs = $dest_slice->coord_system();
237 $dest_slice_sr_name = $dest_slice->seq_region_name();
238 $dest_slice_sr_id = $dest_slice->get_seq_region_id();
239 $asma = $self->db->get_AssemblyMapperAdaptor();
240 }
241  
242 FEATURE: while($sth->fetch()) {
243 #create a repeat consensus object
244  
245 my $rc = $rc_hash{$repeat_consensus_id} ||=
246 Bio::EnsEMBL::RepeatConsensus->new_fast
247 ({'dbID' => $repeat_consensus_id,
248 'adaptor' => $rca,
249 'name' => $repeat_name,
250 'repeat_class' => $repeat_class,
251 'repeat_type' => $repeat_type,
252 'repeat_consensus' => $repeat_consensus,
253 'length' => length($repeat_consensus)});
254  
255 #get the analysis object
256 my $analysis = $analysis_hash{$analysis_id} ||= $aa->fetch_by_dbID($analysis_id);
257 $analysis_hash{$analysis_id} = $analysis;
258  
259 #need to get the internal_seq_region, if present
260 $seq_region_id = $self->get_seq_region_id_internal($seq_region_id);
261 my $slice = $slice_hash{"ID:".$seq_region_id};
262  
263 if (!$slice) {
264 $slice = $sa->fetch_by_seq_region_id($seq_region_id);
265 $slice_hash{"ID:".$seq_region_id} = $slice;
266 $sr_name_hash{$seq_region_id} = $slice->seq_region_name();
267 $sr_cs_hash{$seq_region_id} = $slice->coord_system();
268 }
269  
270 #obtain a mapper if none was defined, but a dest_seq_region was
271 if(!$mapper && $dest_slice && !$dest_slice_cs->equals($slice->coord_system)) {
272 $mapper = $asma->fetch_by_CoordSystems($dest_slice_cs, $slice->coord_system);
273 }
274  
275 my $sr_name = $sr_name_hash{$seq_region_id};
276 my $sr_cs = $sr_cs_hash{$seq_region_id};
277  
278 #
279 # remap the feature coordinates to another coord system
280 # if a mapper was provided
281 #
282  
283 if ($mapper) {
284  
285 if (defined $dest_slice && $mapper->isa('Bio::EnsEMBL::ChainedAssemblyMapper') ) {
286 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
287 $mapper->map($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs, 1, $dest_slice);
288  
289 } else {
290 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
291 $mapper->fastmap($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs);
292 }
293  
294 #skip features that map to gaps or coord system boundaries
295 next FEATURE if (!defined($seq_region_id));
296  
297 #get a slice in the coord system we just mapped to
298 $slice = $slice_hash{"ID:".$seq_region_id} ||= $sa->fetch_by_seq_region_id($seq_region_id);
299 }
300  
301 #
302 # If a destination slice was provided convert the coords.
303 #
304 if (defined($dest_slice)) {
305 my $seq_region_len = $dest_slice->seq_region_length();
306  
307 if ( $dest_slice_strand == 1 ) {
308 $seq_region_start = $seq_region_start - $dest_slice_start + 1;
309 $seq_region_end = $seq_region_end - $dest_slice_start + 1;
310  
311 if ( $dest_slice->is_circular ) {
312 # Handle circular chromosomes.
313  
314 if ( $seq_region_start > $seq_region_end ) {
315 # Looking at a feature overlapping the chromosome origin.
316  
317 if ( $seq_region_end > $dest_slice_start ) {
318 # Looking at the region in the beginning of the chromosome
319 $seq_region_start -= $seq_region_len;
320 }
321 if ( $seq_region_end < 0 ) {
322 $seq_region_end += $seq_region_len;
323 }
324 } else {
325 if ($dest_slice_start > $dest_slice_end && $seq_region_end < 0) {
326 # Looking at the region overlapping the chromosome
327 # origin and a feature which is at the beginning of the
328 # chromosome.
329 $seq_region_start += $seq_region_len;
330 $seq_region_end += $seq_region_len;
331 }
332 }
333 }
334 } else {
335  
336 my $start = $dest_slice_end - $seq_region_end + 1;
337 my $end = $dest_slice_end - $seq_region_start + 1;
338  
339 if ($dest_slice->is_circular()) {
340  
341 if ($dest_slice_start > $dest_slice_end) {
342 # slice spans origin or replication
343  
344 if ($seq_region_start >= $dest_slice_start) {
345 $end += $seq_region_len;
346 $start += $seq_region_len if $seq_region_end > $dest_slice_start;
347  
348 } elsif ($seq_region_start <= $dest_slice_end) {
349 # do nothing
350 } elsif ($seq_region_end >= $dest_slice_start) {
351 $start += $seq_region_len;
352 $end += $seq_region_len;
353  
354 } elsif ($seq_region_end <= $dest_slice_end) {
355 $end += $seq_region_len if $end < 0;
356  
357 } elsif ($seq_region_start > $seq_region_end) {
358 $end += $seq_region_len;
359 }
360  
361 } else {
362  
363 if ($seq_region_start <= $dest_slice_end and $seq_region_end >= $dest_slice_start) {
364 # do nothing
365 } elsif ($seq_region_start > $seq_region_end) {
366 if ($seq_region_start <= $dest_slice_end) {
367 $start -= $seq_region_len;
368 } elsif ($seq_region_end >= $dest_slice_start) {
369 $end += $seq_region_len;
370 }
371 }
372 }
373 }
374  
375 $seq_region_start = $start;
376 $seq_region_end = $end;
377 $seq_region_strand *= -1;
378  
379 } ## end else [ if ( $dest_slice_strand...)]
380  
381 # Throw away features off the end of the requested slice or on
382 # different seq_region.
383 if ($seq_region_end < 1
384 || $seq_region_start > $dest_slice_length
385 || ($dest_slice_sr_id != $seq_region_id)) {
386 next FEATURE;
387 }
388 $slice = $dest_slice;
389 }
390  
391 # Finally, create the new RepeatFeature.
392 push( @features,
393 $self->_create_feature_fast( 'Bio::EnsEMBL::RepeatFeature', {
394 'dbID' => $repeat_feature_id,
395 'analysis' => $analysis,
396 'start' => $seq_region_start,
397 'end' => $seq_region_end,
398 'strand' => $seq_region_strand,
399 'score' => $score,
400 'hstart' => $repeat_start,
401 'hend' => $repeat_end,
402 'repeat_consensus' => $rc,
403 'adaptor' => $self,
404 'slice' => $slice
405 } ) );
406  
407 }
408  
409 return \@features;
410 }
411  
412  
413 =head2 store
414  
415 Arg [1] : list of Bio::EnsEMBL::RepeatFeatures $repeat_feature_id
416 the list of repeat features to store in the database
417 Example : $repeat_feature_adaptor->store(@repeat_features);
418 Description: stores a repeat feature in the database
419 Returntype : none
420 Exceptions : if the repeat features do not have attached sequences
421 or if repeat_consensus are not present
422 Caller : general
423 Status : Stable
424  
425 =cut
426  
427 sub store {
428 my( $self, @repeats ) = @_;
429  
430 my $db = $self->db();
431 my $rca = $db->get_RepeatConsensusAdaptor();
432 my $sa = $db->get_SliceAdaptor();
433 my ($cons, $db_id);
434  
435 my $sth = $self->prepare(qq{
436 INSERT into repeat_feature( repeat_feature_id
437 , seq_region_id
438 , seq_region_start
439 , seq_region_end
440 , seq_region_strand
441 , repeat_consensus_id
442 , repeat_start
443 , repeat_end
444 , score
445 , analysis_id )
446 VALUES(NULL, ?,?,?,?,?,?,?,?,?)
447 });
448  
449 FEATURE: foreach my $rf (@repeats) {
450 if(!ref($rf) || !$rf->isa('Bio::EnsEMBL::RepeatFeature')) {
451 throw('Expected RepeatFeature argument not [' . ref($rf) .'].');
452 }
453  
454 if($rf->is_stored($db)) {
455 warning("RepeatFeature [".$rf->dbID."] is already stored in this DB.");
456 next FEATURE;
457 }
458  
459 my $cons = $rf->repeat_consensus();
460 throw("Must have a RepeatConsensus attached") if(!defined($cons));
461  
462 # for tandem repeats - simply store consensus and repeat
463 # one pair per hit. don't need to check consensi stored
464 # already. consensus has name and class set to 'trf'
465  
466 if ($cons->repeat_class eq 'trf') {
467  
468 # Look for matches already stored
469 my @match = @{$rca->fetch_all_by_class_seq('trf', $cons->repeat_consensus)};
470 if (@match) {
471 $cons->dbID($match[0]->dbID());
472 }
473 else {
474 $rca->store($cons);
475 }
476  
477 } elsif ($cons->repeat_class eq 'Simple_repeat') {
478  
479 my $rcon = $cons->name;
480 $rcon =~ s/\((\S+)\)n/$1/; # get repeat element
481 $cons->repeat_consensus($rcon);
482  
483 # Look for matches already stored
484 my $match = $rca->fetch_by_name_class($cons->name, 'Simple_repeat');
485 if ($match) {
486 $cons->dbID($match->dbID());
487 }
488 else {
489 $rca->store($cons);
490 }
491  
492 } else {
493  
494 # for other repeats - need to see if a consensus is stored already
495 if(!$cons->dbID) {
496 my $match = ($rca->fetch_by_name($cons->name));
497
498 if($match) {
499 #set the consensus dbID to be the same as the database one
500 $cons->dbID($match->dbID());
501 } else {
502 # if we don't match a consensus already stored create a fake one
503 # and set consensus to 'N' as null seq not allowed
504 # FIXME: not happy with this, but ho hum ...
505 warning("Can't find " . $cons->name . "\n");
506 $cons->repeat_consensus("N");
507 $rca->store($cons);
508 }
509 }
510
511 #if (@match > 1) {
512 #multiple consensi were matched
513 # $self->warn(@match . " consensi for " . $cons->name . "\n");
514 #}
515 }
516  
517 my $slice = $rf->slice();
518 if(!ref($slice) || !($slice->isa("Bio::EnsEMBL::Slice") or $slice->isa('Bio::EnsEMBL::LRGSlice'))) {
519 throw("RepeatFeature cannot be stored without an associated slice.");
520 }
521  
522 #store the analysis if it has not been stored yet
523 if(!$rf->analysis->is_stored($db)) {
524 $db->get_AnalysisAdaptor->store($rf->analysis());
525 }
526  
527 my $original = $rf;
528 my $seq_region_id;
529 ($rf, $seq_region_id) = $self->_pre_store($rf);
530  
531 $sth->bind_param(1,$seq_region_id,SQL_INTEGER);
532 $sth->bind_param(2,$rf->start,SQL_INTEGER);
533 $sth->bind_param(3,$rf->end,SQL_INTEGER);
534 $sth->bind_param(4,$rf->strand,SQL_TINYINT);
535 $sth->bind_param(5,$rf->repeat_consensus->dbID,SQL_INTEGER);
536 $sth->bind_param(6,$rf->hstart,SQL_INTEGER);
537 $sth->bind_param(7,$rf->hend,SQL_INTEGER);
538 $sth->bind_param(8,$rf->score,SQL_DOUBLE);
539 $sth->bind_param(9,$rf->analysis->dbID,SQL_INTEGER);
540  
541 $sth->execute();
542  
543 my $db_id = $self->last_insert_id('repeat_feature_id', undef, 'repeat_feature')
544 or throw("Didn't get an insertid from the INSERT statement");
545  
546 $original->dbID($db_id);
547 $original->adaptor($self);
548 }
549 }
550  
551  
552 =head2 list_dbIDs
553  
554 Arg [1] : none
555 Example : @feature_ids = @{$repeat_feature_adaptor->list_dbIDs()};
556 Description: Gets an array of internal ids for all repeat features in the current db
557 Arg[1] : <optional> int. not 0 for the ids to be sorted by the seq_region.
558 Returntype : list of ints
559 Exceptions : none
560 Caller : ?
561 Status : Stable
562  
563 =cut
564  
565 sub list_dbIDs {
566 my ($self, $ordered) = @_;
567  
568 return $self->_list_dbIDs("repeat_feature", undef, $ordered);
569 }
570  
571 1;
572  
573  
574  
575  
576