EnsEMBL/DBSQL/DnaAlignFeatureAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 187 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 36 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 316 CC 38 SCAR 62.7
CC
38
Cov
0%
_objs_from_sth
870.0
store
72.0
remove
12.0
CRAP 527.8
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::DnaAlignFeatureAdaptor - Adaptor for DnaAlignFeatures
34  
35 =head1 SYNOPSIS
36  
37 $dafa = $registry->get_adaptor( 'Human', 'Core', 'DnaAlignFeature' );
38  
39 my @features = @{ $dafa->fetch_all_by_Slice($slice) };
40  
41 $dafa->store(@features);
42  
43 =head1 DESCRIPTION
44  
45 This is an adaptor responsible for the retrieval and storage of
46 DnaDnaAlignFeatures from the database. This adaptor inherits most of its
47 functionality from the BaseAlignFeatureAdaptor and BaseFeatureAdaptor
48 superclasses.
49  
50 =head1 METHODS
51  
52 =cut
53  
54  
55 package Bio::EnsEMBL::DBSQL::DnaAlignFeatureAdaptor;
56 $Bio::EnsEMBL::DBSQL::DnaAlignFeatureAdaptor::VERSION = '114.0.0_50'; # TRIAL
57 $Bio::EnsEMBL::DBSQL::DnaAlignFeatureAdaptor::VERSION = '114.0.050';
58 use vars qw(@ISA);
59 use strict;
60 use Bio::EnsEMBL::DnaDnaAlignFeature;
61 use Bio::EnsEMBL::DBSQL::BaseAlignFeatureAdaptor;
62 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
63  
64 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAlignFeatureAdaptor);
65  
66  
67 =head2 _tables
68  
69 Args : none
70 Example : @tabs = $self->_tables
71 Description: PROTECTED implementation of the abstract method inherited from
72 BaseFeatureAdaptor. Returns list of [tablename, alias] pairs
73 Returntype : list of listrefs of strings
74 Exceptions : none
75 Caller : Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor::generic_fetch
76 Status : Stable
77  
78 =cut
79  
80 sub _tables {
81 my $self = shift;
82  
83 return (['dna_align_feature', 'daf'],['external_db','exdb']);
84 }
85  
86  
87 sub _left_join{
88 return (['external_db',"exdb.external_db_id = daf.external_db_id"]);
89 }
90  
91 =head2 _columns
92  
93 Args : none
94 Example : @columns = $self->_columns
95 Description: PROTECTED implementation of abstract superclass method.
96 Returns a list of columns that are needed for object creation.
97 Returntype : list of strings
98 Exceptions : none
99 Caller : Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor::generic_fetch
100 Status : Stable
101  
102 =cut
103  
104 sub _columns {
105 my $self = shift;
106  
107 #warning, implementation of _objs_from_sth method depends on order of list
108 return qw(daf.dna_align_feature_id
109 daf.seq_region_id
110 daf.analysis_id
111 daf.seq_region_start
112 daf.seq_region_end
113 daf.seq_region_strand
114 daf.hit_start
115 daf.hit_end
116 daf.hit_name
117 daf.hit_strand
118 daf.cigar_line
119 daf.evalue
120 daf.perc_ident
121 daf.score
122 daf.external_db_id
123 daf.hcoverage
124 daf.align_type
125 exdb.db_name
126 exdb.db_display_name);
127 }
128  
129  
130 =head2 store
131  
132 Arg [1] : list of Bio::EnsEMBL::DnaAlignFeatures @feats
133 the features to store in the database
134 Example : $dna_align_feature_adaptor->store(@features);
135 Description: Stores a list of DnaAlignFeatures in the database
136 Returntype : none
137 Exceptions : throw if any of the provided features cannot be stored
138 which may occur if:
139 * The feature does not have an associate Slice
140 * The feature does not have an associated analysis
141 * The Slice the feature is associated with is on a seq_region
142 unknown to this database
143 A warning is given if:
144 * The feature has already been stored in this db
145 Caller : Pipeline
146 Status : Stable
147  
148 =cut
149  
150 sub store {
151 my ( $self, @feats ) = @_;
152  
153 throw("Must call store with features") if ( scalar(@feats) == 0 );
154  
155 my @tabs = $self->_tables;
156 my ($tablename) = @{ $tabs[0] };
157  
158 my $db = $self->db();
159 my $analysis_adaptor = $db->get_AnalysisAdaptor();
160  
161 my $sth = $self->prepare(
162 "INSERT INTO $tablename (seq_region_id, seq_region_start,
163 seq_region_end, seq_region_strand,
164 hit_start, hit_end, hit_strand, hit_name,
165 cigar_line, analysis_id, score, evalue,
166 perc_ident, external_db_id, hcoverage, align_type)
167 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)" # 16 arguments, external_data removed and align_type added
168 );
169  
170 FEATURE:
171 foreach my $feat (@feats) {
172 if ( !ref $feat || !$feat->isa("Bio::EnsEMBL::DnaDnaAlignFeature") )
173 {
174 throw("feature must be a Bio::EnsEMBL::DnaDnaAlignFeature, not a [". ref($feat). "]." );
175 }
176  
177 if ( $feat->is_stored($db) ) {
178 warning( "DnaDnaAlignFeature [".$feat->dbID()."] is already stored in this database." );
179 next FEATURE;
180 }
181  
182 my $hstart = $feat->hstart();
183 my $hend = $feat->hend();
184 my $hstrand = $feat->hstrand();
185 $self->_check_start_end_strand( $hstart, $hend, $hstrand, $feat->hslice );
186  
187 my $cigar_string = $feat->cigar_string();
188 if ( !$cigar_string ) {
189 $cigar_string = $feat->length() . 'M';
190 warning( "DnaDnaAlignFeature does not define a cigar_string.\n"
191 . "Assuming ungapped block with cigar_line=$cigar_string ." );
192 }
193 my $align_type = $feat->align_type();
194  
195 my $hseqname = $feat->hseqname();
196 if ( !$hseqname ) {
197 throw("DnaDnaAlignFeature must define an hseqname.");
198 }
199  
200 if ( !defined( $feat->analysis ) ) {
201 throw(
202 "An analysis must be attached to the features to be stored.");
203 }
204  
205 #store the analysis if it has not been stored yet
206 if ( !$feat->analysis->is_stored($db) ) {
207 $analysis_adaptor->store( $feat->analysis() );
208 }
209  
210 my $original = $feat;
211 my $seq_region_id;
212 ( $feat, $seq_region_id ) = $self->_pre_store($feat);
213
214 $sth->bind_param( 1, $seq_region_id, SQL_INTEGER );
215 $sth->bind_param( 2, $feat->start, SQL_INTEGER );
216 $sth->bind_param( 3, $feat->end, SQL_INTEGER );
217 $sth->bind_param( 4, $feat->strand, SQL_TINYINT );
218 $sth->bind_param( 5, $hstart, SQL_INTEGER );
219 $sth->bind_param( 6, $hend, SQL_INTEGER );
220 $sth->bind_param( 7, $hstrand, SQL_TINYINT );
221 $sth->bind_param( 8, $hseqname, SQL_VARCHAR );
222 $sth->bind_param( 9, $cigar_string, SQL_LONGVARCHAR );
223 $sth->bind_param( 10, $feat->analysis->dbID, SQL_INTEGER );
224 $sth->bind_param( 11, $feat->score, SQL_DOUBLE );
225 $sth->bind_param( 12, $feat->p_value, SQL_DOUBLE );
226 $sth->bind_param( 13, $feat->percent_id, SQL_FLOAT );
227 $sth->bind_param( 14, $feat->external_db_id, SQL_INTEGER );
228 $sth->bind_param( 15, $feat->hcoverage, SQL_DOUBLE );
229 $sth->bind_param( 16, $feat->align_type, SQL_VARCHAR);
230 $sth->execute();
231  
232 my $dbId = $self->last_insert_id("${tablename}_id", undef, $tablename);
233  
234 # store attributes if there are any
235 my $attr_adaptor = $db->get_AttributeAdaptor();
236 $attr_adaptor->store_on_DnaDnaAlignFeature($dbId, $feat->get_all_Attributes);
237  
238 $original->dbID( $dbId );
239 $original->adaptor($self);
240 } ## end foreach my $feat (@feats)
241  
242 $sth->finish();
243 } ## end sub store
244  
245  
246 =head2 remove
247  
248 Arg [1] : A feature $feature
249 Example : $feature_adaptor->remove($feature);
250 Description: This removes a feature from the database. The table the
251 feature is removed from is defined by the abstract method
252 _tablename, and the primary key of the table is assumed
253 to be _tablename() . '_id'. The feature argument must
254 be an object implementing the dbID method, and for the
255 feature to be removed from the database a dbID value must
256 be returned.
257 Returntype : none
258 Exceptions : thrown if $feature arg does not implement dbID(), or if
259 $feature->dbID is not a true value
260 Caller : general
261 Status : Stable
262  
263 =cut
264  
265  
266 sub remove {
267 my ($self, $feature) = @_;
268  
269 if(!$feature || !ref($feature) || !$feature->isa('Bio::EnsEMBL::Feature')) {
270 throw('Feature argument is required');
271 }
272  
273 if(!$feature->is_stored($self->db)) {
274 throw("This feature is not stored in this database");
275 }
276  
277 my @tabs = $self->_tables;
278 my ($table) = @{$tabs[0]};
279  
280 my $sth = $self->prepare("DELETE FROM $table WHERE ${table}_id = ?");
281 $sth->bind_param(1,$feature->dbID,SQL_INTEGER);
282 $sth->execute();
283  
284 # remove the attributes associated with this feature
285 my $attrib_adaptor = $self->db->get_AttributeAdaptor;
286 $attrib_adaptor->remove_from_DnaDnaAlignFeature($feature);
287  
288 #unset the feature dbID ad adaptor
289 $feature->dbID(undef);
290 $feature->adaptor(undef);
291  
292 return;
293 }
294  
295  
296 =head2 _objs_from_sth
297  
298 Arg [1] : DBI statement handle $sth
299 an exectuted DBI statement handle generated by selecting
300 the columns specified by _columns() from the table specified
301 by _table()
302 Example : @dna_dna_align_feats = $self->_obj_from_hashref
303 Description: PROTECTED implementation of superclass abstract method.
304 Creates DnaDnaAlignFeature objects from a DBI hashref
305 Returntype : listref of Bio::EnsEMBL::DnaDnaAlignFeatures
306 Exceptions : none
307 Caller : Bio::EnsEMBL::BaseFeatureAdaptor::generic_fetch
308 Status : Stable
309  
310 =cut
311  
312 sub _objs_from_sth {
313 my ( $self, $sth, $mapper, $dest_slice ) = @_;
314  
315 #
316 # This code is ugly because an attempt has been made to remove as many
317 # function calls as possible for speed purposes. Thus many caches and
318 # a fair bit of gymnastics is used.
319 #
320  
321 # In case of userdata we need the features on the dest_slice. In case
322 # of get_all_supporting_features dest_slice is not provided.
323 my $sa = ( $dest_slice
324 ? $dest_slice->adaptor()
325 : $self->db()->get_SliceAdaptor() );
326 my $aa = $self->db->get_AnalysisAdaptor();
327  
328 my @features;
329 my %analysis_hash;
330 my %slice_hash;
331 my %sr_name_hash;
332 my %sr_cs_hash;
333  
334 my ( $dna_align_feature_id, $seq_region_id,
335 $analysis_id, $seq_region_start,
336 $seq_region_end, $seq_region_strand,
337 $hit_start, $hit_end,
338 $hit_name, $hit_strand,
339 $cigar_line, $evalue,
340 $perc_ident, $score,
341 $external_db_id, $hcoverage,
342 $align_type,
343 $external_db_name, $external_display_db_name );
344  
345 $sth->bind_columns( \( $dna_align_feature_id, $seq_region_id,
346 $analysis_id, $seq_region_start,
347 $seq_region_end, $seq_region_strand,
348 $hit_start, $hit_end,
349 $hit_name, $hit_strand,
350 $cigar_line, $evalue,
351 $perc_ident, $score,
352 $external_db_id, $hcoverage,
353 $align_type,
354 $external_db_name, $external_display_db_name )
355 );
356  
357 my $dest_slice_start;
358 my $dest_slice_end;
359 my $dest_slice_strand;
360 my $dest_slice_length;
361 my $dest_slice_cs;
362 my $dest_slice_sr_name;
363 my $dest_slice_sr_id;
364 my $asma;
365  
366 if ($dest_slice) {
367 $dest_slice_start = $dest_slice->start();
368 $dest_slice_end = $dest_slice->end();
369 $dest_slice_strand = $dest_slice->strand();
370 $dest_slice_length = $dest_slice->length();
371 $dest_slice_cs = $dest_slice->coord_system();
372 $dest_slice_sr_name = $dest_slice->seq_region_name();
373 $dest_slice_sr_id = $dest_slice->get_seq_region_id();
374 $asma = $self->db->get_AssemblyMapperAdaptor();
375 }
376  
377 FEATURE: while($sth->fetch()) {
378  
379 #get the analysis object
380 my $analysis = $analysis_hash{$analysis_id} ||= $aa->fetch_by_dbID($analysis_id);
381 $analysis_hash{$analysis_id} = $analysis;
382  
383 #need to get the internal_seq_region, if present
384 $seq_region_id = $self->get_seq_region_id_internal($seq_region_id);
385 my $slice = $slice_hash{"ID:".$seq_region_id};
386  
387 if (!$slice) {
388 $slice = $sa->fetch_by_seq_region_id($seq_region_id);
389 $slice_hash{"ID:".$seq_region_id} = $slice;
390 $sr_name_hash{$seq_region_id} = $slice->seq_region_name();
391 $sr_cs_hash{$seq_region_id} = $slice->coord_system();
392 }
393  
394 #obtain a mapper if none was defined, but a dest_seq_region was
395 if(!$mapper && $dest_slice && !$dest_slice_cs->equals($slice->coord_system)) {
396 $mapper = $asma->fetch_by_CoordSystems($dest_slice_cs, $slice->coord_system);
397 }
398  
399 my $sr_name = $sr_name_hash{$seq_region_id};
400 my $sr_cs = $sr_cs_hash{$seq_region_id};
401  
402 #
403 # remap the feature coordinates to another coord system
404 # if a mapper was provided
405 #
406  
407 if ($mapper) {
408  
409 if (defined $dest_slice && $mapper->isa('Bio::EnsEMBL::ChainedAssemblyMapper') ) {
410 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
411 $mapper->map($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs, 1, $dest_slice);
412  
413 } else {
414 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
415 $mapper->fastmap($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs);
416 }
417  
418 #skip features that map to gaps or coord system boundaries
419 next FEATURE if (!defined($seq_region_id));
420  
421 #get a slice in the coord system we just mapped to
422 $slice = $slice_hash{"ID:".$seq_region_id} ||= $sa->fetch_by_seq_region_id($seq_region_id);
423 }
424  
425 #
426 # If a destination slice was provided convert the coords.
427 #
428 if (defined($dest_slice)) {
429 my $seq_region_len = $dest_slice->seq_region_length();
430  
431 if ( $dest_slice_strand == 1 ) {
432 $seq_region_start = $seq_region_start - $dest_slice_start + 1;
433 $seq_region_end = $seq_region_end - $dest_slice_start + 1;
434  
435 if ( $dest_slice->is_circular ) {
436 # Handle circular chromosomes.
437  
438 if ( $seq_region_start > $seq_region_end ) {
439 # Looking at a feature overlapping the chromosome origin.
440  
441 if ( $seq_region_end > $dest_slice_start ) {
442 # Looking at the region in the beginning of the chromosome
443 $seq_region_start -= $seq_region_len;
444 }
445 if ( $seq_region_end < 0 ) {
446 $seq_region_end += $seq_region_len;
447 }
448 } else {
449 if ($dest_slice_start > $dest_slice_end && $seq_region_end < 0) {
450 # Looking at the region overlapping the chromosome
451 # origin and a feature which is at the beginning of the
452 # chromosome.
453 $seq_region_start += $seq_region_len;
454 $seq_region_end += $seq_region_len;
455 }
456 }
457 }
458 } else {
459  
460 my $start = $dest_slice_end - $seq_region_end + 1;
461 my $end = $dest_slice_end - $seq_region_start + 1;
462  
463 if ($dest_slice->is_circular()) {
464  
465 if ($dest_slice_start > $dest_slice_end) {
466 # slice spans origin or replication
467  
468 if ($seq_region_start >= $dest_slice_start) {
469 $end += $seq_region_len;
470 $start += $seq_region_len if $seq_region_end > $dest_slice_start;
471  
472 } elsif ($seq_region_start <= $dest_slice_end) {
473 # do nothing
474 } elsif ($seq_region_end >= $dest_slice_start) {
475 $start += $seq_region_len;
476 $end += $seq_region_len;
477  
478 } elsif ($seq_region_end <= $dest_slice_end) {
479 $end += $seq_region_len if $end < 0;
480  
481 } elsif ($seq_region_start > $seq_region_end) {
482 $end += $seq_region_len;
483 }
484  
485 } else {
486  
487 if ($seq_region_start <= $dest_slice_end and $seq_region_end >= $dest_slice_start) {
488 # do nothing
489 } elsif ($seq_region_start > $seq_region_end) {
490 if ($seq_region_start <= $dest_slice_end) {
491 $start -= $seq_region_len;
492 } elsif ($seq_region_end >= $dest_slice_start) {
493 $end += $seq_region_len;
494 }
495 }
496 }
497 }
498  
499 $seq_region_start = $start;
500 $seq_region_end = $end;
501 $seq_region_strand *= -1;
502  
503 } ## end else [ if ( $dest_slice_strand...)]
504  
505 # Throw away features off the end of the requested slice or on
506 # different seq_region.
507 if ($seq_region_end < 1
508 || $seq_region_start > $dest_slice_length
509 || ($dest_slice_sr_id != $seq_region_id)) {
510 next FEATURE;
511 }
512 $slice = $dest_slice;
513 }
514  
515 # Finally, create the new DnaAlignFeature.
516 push( @features,
517 $self->_create_feature_fast(
518 'Bio::EnsEMBL::DnaDnaAlignFeature', {
519 'slice' => $slice,
520 'start' => $seq_region_start,
521 'end' => $seq_region_end,
522 'strand' => $seq_region_strand,
523 'hseqname' => $hit_name,
524 'hstart' => $hit_start,
525 'hend' => $hit_end,
526 'hstrand' => $hit_strand,
527 'score' => $score,
528 'p_value' => $evalue,
529 'percent_id' => $perc_ident,
530 'cigar_string' => $cigar_line,
531 'analysis' => $analysis,
532 'adaptor' => $self,
533 'dbID' => $dna_align_feature_id,
534 'external_db_id' => $external_db_id,
535 'hcoverage' => $hcoverage,
536 'align_type' => $align_type,
537 'dbname' => $external_db_name,
538 'db_display_name' => $external_display_db_name
539 } ) );
540  
541 } ## end while ( $sth->fetch() )
542  
543 return \@features;
544 } ## end sub _objs_from_sth
545  
546 =head2 list_dbIDs
547  
548 Arg [1] : none
549 Example : @feature_ids = @{$dna_align_feature_adaptor->list_dbIDs()};
550 Description: Gets an array of internal ids for all dna align features in
551 the current db
552 Arg[1] : <optional> int. not 0 for the ids to be sorted by the seq_region.
553 Returntype : list of ints
554 Exceptions : none
555 Caller : ?
556 Status : Stable
557  
558 =cut
559  
560 sub list_dbIDs {
561 my ($self, $ordered) = @_;
562  
563 return $self->_list_dbIDs("dna_align_feature",undef, $ordered);
564 }
565  
566 1;
567  
568