EnsEMBL/DBSQL/ProteinAlignFeatureAdaptor.pm untested

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