EnsEMBL/DBSQL/PredictionExonAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 161 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 36 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 288 CC 36 SCAR 61.7
CC
36
Cov
0%
_objs_from_sth
812.0
store
42.0
fetch_all_by_PredictionTranscript
12.0
CRAP 480.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::PredictionExonAdaptor - Performs database interaction for
34 PredictionExons.
35  
36 =head1 SYNOPSIS
37  
38 $pea = $database_adaptor->get_PredictionExonAdaptor();
39 $pexon = $pea->fetch_by_dbID();
40  
41 my $slice =
42 $database_adaptor->get_SliceAdaptor->fetch_by_region( 'X', 1, 1e6 );
43  
44 my @pexons = @{ $pea->fetch_all_by_Slice($slice) };
45  
46 =head1 METHODS
47  
48 =cut
49  
50 package Bio::EnsEMBL::DBSQL::PredictionExonAdaptor;
51 $Bio::EnsEMBL::DBSQL::PredictionExonAdaptor::VERSION = '114.0.0_50'; # TRIAL
52 $Bio::EnsEMBL::DBSQL::PredictionExonAdaptor::VERSION = '114.0.050';
53 use vars qw( @ISA );
54 use strict;
55  
56  
57 use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
58 use Bio::EnsEMBL::PredictionExon;
59 use Bio::EnsEMBL::Utils::Exception qw( warning throw );
60  
61  
62 @ISA = qw( Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor );
63  
64  
65 #_tables
66 #
67 # Arg [1] : none
68 # Example : none
69 # Description: PROTECTED implementation of superclass abstract method
70 # returns the names, aliases of the tables to use for queries
71 # Returntype : list of listrefs of strings
72 # Exceptions : none
73 # Caller : internal
74 #
75  
76 sub _tables {
77 return ([ 'prediction_exon', 'pe' ] );
78 }
79  
80  
81  
82 #_columns
83 #
84 # Arg [1] : none
85 # Example : none
86 # Description: PROTECTED implementation of superclass abstract method
87 # returns a list of columns to use for queries
88 # Returntype : list of strings
89 # Exceptions : none
90 # Caller : internal
91  
92 sub _columns {
93 my $self = shift;
94  
95 return qw( pe.prediction_exon_id
96 pe.seq_region_id
97 pe.seq_region_start
98 pe.seq_region_end
99 pe.seq_region_strand
100 pe.start_phase
101 pe.score
102 pe.p_value );
103 }
104  
105  
106 # _final_clause
107 #
108 # Arg [1] : none
109 # Example : none
110 # Description: PROTECTED implementation of superclass abstract method
111 # returns a default end for the SQL-query (ORDER BY)
112 # Returntype : string
113 # Exceptions : none
114 # Caller : internal
115  
116 sub _final_clause {
117 return "ORDER BY pe.prediction_transcript_id, pe.exon_rank";
118 }
119  
120  
121 =head2 fetch_all_by_PredictionTranscript
122  
123 Arg [1] : Bio::EnsEMBL::PredcitionTranscript $transcript
124 Example : none
125 Description: Retrieves all Exons for the Transcript in 5-3 order
126 Returntype : listref Bio::EnsEMBL::Exon on Transcript slice
127 Exceptions : throws if transcript does not have a slice
128 Caller : Transcript->get_all_Exons()
129 Status : Stable
130  
131 =cut
132  
133 sub fetch_all_by_PredictionTranscript {
134 my ( $self, $transcript ) = @_;
135 my $constraint = "pe.prediction_transcript_id = ".$transcript->dbID();
136  
137 # use 'keep_all' option to keep exons that are off end of slice
138  
139 my $tslice = $transcript->slice();
140 my $slice;
141  
142 if(!$tslice) {
143 throw("Transcript must have attached slice to retrieve exons.");
144 }
145  
146 # use a small slice the same size as the prediction transcript
147 $slice = $self->db->get_SliceAdaptor->fetch_by_Feature($transcript);
148  
149 my $exons = $self->fetch_all_by_Slice_constraint($slice, $constraint);
150  
151 # remap exon coordinates if necessary
152 if($slice->name() ne $tslice->name()) {
153 my @out;
154 foreach my $ex (@$exons) {
155 push @out, $ex->transfer($tslice);
156 }
157 $exons = \@out;
158 }
159  
160 return $exons;
161 }
162  
163  
164  
165 =head2 store
166  
167 Arg [1] : Bio::EnsEMBL::PredictionExon $exon
168 The exon to store in this database
169 Arg [2] : int $prediction_transcript_id
170 The internal identifier of the prediction exon that that this
171 exon is associated with.
172 Arg [3] : int $rank
173 The rank of the exon in the transcript (starting at 1)
174 Example : $pexon_adaptor->store($pexon, 1211, 2);
175 Description: Stores a PredictionExon in the database
176 Returntype : none
177 Exceptions : thrown if exon does not have a slice attached
178 or if $exon->start, $exon->end, $exon->strand, or $exon->phase
179 are not defined or if $exon is not a Bio::EnsEMBL::PredictionExon
180 Caller : general
181 Status : Stable
182  
183 =cut
184  
185 sub store {
186 my ( $self, $pexon, $pt_id, $rank ) = @_;
187  
188 if(!ref($pexon) || !$pexon->isa('Bio::EnsEMBL::PredictionExon') ) {
189 throw("Expected PredictionExon argument");
190 }
191  
192 throw("Expected PredictionTranscript id argument.") if(!$pt_id);
193 throw("Expected rank argument.") if(!$rank);
194  
195 my $db = $self->db();
196  
197 if($pexon->is_stored($db)) {
198 warning('PredictionExon is already stored in this DB.');
199 return $pexon->dbID();
200 }
201  
202 if( ! $pexon->start || ! $pexon->end ||
203 ! $pexon->strand || ! defined $pexon->phase ) {
204 throw("PredictionExon does not have all attributes to store.\n" .
205 "start, end, strand and phase attributes must be set.");
206 }
207  
208 #maintain reference to original passed-in prediction exon
209 my $original = $pexon;
210 my $seq_region_id;
211 ($pexon, $seq_region_id) = $self->_pre_store($pexon);
212  
213 my $sth = $db->dbc->prepare
214 ("INSERT into prediction_exon (prediction_transcript_id, exon_rank, " .
215 "seq_region_id, seq_region_start, seq_region_end, " .
216 "seq_region_strand, start_phase, score, p_value) " .
217 "VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ? )");
218  
219 $sth->bind_param(1,$pt_id,SQL_INTEGER);
220 $sth->bind_param(2,$rank,SQL_SMALLINT);
221 $sth->bind_param(3,$seq_region_id,SQL_INTEGER);
222 $sth->bind_param(4,$pexon->start,SQL_INTEGER);
223 $sth->bind_param(5,$pexon->end,SQL_INTEGER);
224 $sth->bind_param(6,$pexon->strand,SQL_TINYINT);
225 $sth->bind_param(7,$pexon->phase,SQL_TINYINT);
226 $sth->bind_param(8,$pexon->score,SQL_DOUBLE);
227 $sth->bind_param(9,$pexon->p_value,SQL_DOUBLE);
228  
229 $sth->execute();
230  
231 my $dbID = $self->last_insert_id('prediction_transcript_id', undef, 'prediction_transcript');
232  
233 #set the adaptor and dbID of the object they passed in
234 $original->dbID($dbID);
235 $original->adaptor($self);
236  
237 return $dbID;
238 }
239  
240  
241  
242 =head2 remove
243  
244 Arg [1] : Bio::EnsEMBL::PredictionExon $exon
245 the exon to remove from the database
246 Example : $exon_adaptor->remove($exon);
247 Description: Removes an exon from the database
248 Returntype : none
249 Exceptions : none
250 Caller : general
251 Status : Stable
252  
253 =cut
254  
255 sub remove {
256 my $self = shift;
257 my $pexon = shift;
258  
259 my $db = $self->db();
260  
261 if(!$pexon->is_stored($db)) {
262 warning('PredictionExon is not in this DB - not removing');
263 return undef;
264 }
265  
266 my $sth = $self->prepare(
267 "DELETE FROM prediction_exon WHERE prediction_exon_id = ?");
268 $sth->bind_param( 1, $pexon->dbID, SQL_INTEGER );
269 $sth->execute();
270  
271 $pexon->dbID(undef);
272 $pexon->adaptor(undef);
273 }
274  
275  
276  
277 =head2 list_dbIDs
278  
279 Arg [1] : none
280 Example : @exon_ids = @{$exon_adaptor->list_dbIDs()};
281 Description: Gets an array of internal ids for all exons in the current db
282 Arg[1] : <optional> int. not 0 for the ids to be sorted by the seq_region.
283 Returntype : list of ints
284 Exceptions : none
285 Caller : ?
286 Status : Stable
287  
288 =cut
289  
290 sub list_dbIDs {
291 my ($self,$ordered) = @_;
292  
293 return $self->_list_dbIDs("prediction_exon",undef, $ordered);
294 }
295  
296  
297  
298 #_objs_from_sth
299  
300 # Arg [1] : Hashreference $hashref
301 # Example : none
302 # Description: PROTECTED implementation of abstract superclass method.
303 # responsible for the creation of Genes
304 # Returntype : listref of Bio::EnsEMBL::Genes in target coordinate system
305 # Exceptions : none
306 # Caller : internal
307 #
308  
309 sub _objs_from_sth {
310 my ($self, $sth, $mapper, $dest_slice) = @_;
311  
312 #
313 # This code is ugly because an attempt has been made to remove as many
314 # function calls as possible for speed purposes. Thus many caches and
315 # a fair bit of gymnastics is used.
316 #
317 my $sa = $self->db()->get_SliceAdaptor();
318  
319 my @exons;
320 my %slice_hash;
321 my %sr_name_hash;
322 my %sr_cs_hash;
323  
324 my(
325 $prediction_exon_id, $seq_region_id,
326 $seq_region_start, $seq_region_end, $seq_region_strand,
327 $start_phase, $score, $p_value);
328  
329 $sth->bind_columns(\(
330 $prediction_exon_id, $seq_region_id,
331 $seq_region_start, $seq_region_end, $seq_region_strand,
332 $start_phase, $score, $p_value));
333  
334 my $dest_slice_start;
335 my $dest_slice_end;
336 my $dest_slice_strand;
337 my $dest_slice_length;
338 my $dest_slice_cs;
339 my $dest_slice_sr_name;
340 my $dest_slice_sr_id;
341 my $asma;
342
343 if ($dest_slice) {
344 $dest_slice_start = $dest_slice->start();
345 $dest_slice_end = $dest_slice->end();
346 $dest_slice_strand = $dest_slice->strand();
347 $dest_slice_length = $dest_slice->length();
348 $dest_slice_cs = $dest_slice->coord_system();
349 $dest_slice_sr_name = $dest_slice->seq_region_name();
350 $dest_slice_sr_id = $dest_slice->get_seq_region_id();
351 $asma = $self->db->get_AssemblyMapperAdaptor();
352 }
353  
354 FEATURE: while($sth->fetch()) {
355  
356 #need to get the internal_seq_region, if present
357 $seq_region_id = $self->get_seq_region_id_internal($seq_region_id);
358 my $slice = $slice_hash{"ID:".$seq_region_id};
359  
360 if (!$slice) {
361 $slice = $sa->fetch_by_seq_region_id($seq_region_id);
362 $slice_hash{"ID:".$seq_region_id} = $slice;
363 $sr_name_hash{$seq_region_id} = $slice->seq_region_name();
364 $sr_cs_hash{$seq_region_id} = $slice->coord_system();
365 }
366  
367 #obtain a mapper if none was defined, but a dest_seq_region was
368 if(!$mapper && $dest_slice && !$dest_slice_cs->equals($slice->coord_system)) {
369 $mapper = $asma->fetch_by_CoordSystems($dest_slice_cs, $slice->coord_system);
370 }
371  
372 my $sr_name = $sr_name_hash{$seq_region_id};
373 my $sr_cs = $sr_cs_hash{$seq_region_id};
374  
375 #
376 # remap the feature coordinates to another coord system
377 # if a mapper was provided
378 #
379  
380 if ($mapper) {
381  
382 if (defined $dest_slice && $mapper->isa('Bio::EnsEMBL::ChainedAssemblyMapper') ) {
383 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
384 $mapper->map($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs, 1, $dest_slice);
385  
386 } else {
387 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
388 $mapper->fastmap($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs);
389 }
390  
391 #skip features that map to gaps or coord system boundaries
392 next FEATURE if (!defined($seq_region_id));
393  
394 #get a slice in the coord system we just mapped to
395 $slice = $slice_hash{"ID:".$seq_region_id} ||= $sa->fetch_by_seq_region_id($seq_region_id);
396 }
397  
398 #
399 # If a destination slice was provided convert the coords.
400 #
401 if (defined($dest_slice)) {
402 my $seq_region_len = $dest_slice->seq_region_length();
403  
404 if ( $dest_slice_strand == 1 ) {
405 $seq_region_start = $seq_region_start - $dest_slice_start + 1;
406 $seq_region_end = $seq_region_end - $dest_slice_start + 1;
407  
408 if ( $dest_slice->is_circular ) {
409 # Handle circular chromosomes.
410  
411 if ( $seq_region_start > $seq_region_end ) {
412 # Looking at a feature overlapping the chromosome origin.
413  
414 if ( $seq_region_end > $dest_slice_start ) {
415 # Looking at the region in the beginning of the chromosome
416 $seq_region_start -= $seq_region_len;
417 }
418 if ( $seq_region_end < 0 ) {
419 $seq_region_end += $seq_region_len;
420 }
421 } else {
422 if ($dest_slice_start > $dest_slice_end && $seq_region_end < 0) {
423 # Looking at the region overlapping the chromosome
424 # origin and a feature which is at the beginning of the
425 # chromosome.
426 $seq_region_start += $seq_region_len;
427 $seq_region_end += $seq_region_len;
428 }
429 }
430 }
431 } else {
432  
433 my $start = $dest_slice_end - $seq_region_end + 1;
434 my $end = $dest_slice_end - $seq_region_start + 1;
435  
436 if ($dest_slice->is_circular()) {
437  
438 if ($dest_slice_start > $dest_slice_end) {
439 # slice spans origin or replication
440  
441 if ($seq_region_start >= $dest_slice_start) {
442 $end += $seq_region_len;
443 $start += $seq_region_len if $seq_region_end > $dest_slice_start;
444  
445 } elsif ($seq_region_start <= $dest_slice_end) {
446 # do nothing
447 } elsif ($seq_region_end >= $dest_slice_start) {
448 $start += $seq_region_len;
449 $end += $seq_region_len;
450  
451 } elsif ($seq_region_end <= $dest_slice_end) {
452 $end += $seq_region_len if $end < 0;
453  
454 } elsif ($seq_region_start > $seq_region_end) {
455 $end += $seq_region_len;
456 }
457  
458 } else {
459  
460 if ($seq_region_start <= $dest_slice_end and $seq_region_end >= $dest_slice_start) {
461 # do nothing
462 } elsif ($seq_region_start > $seq_region_end) {
463 if ($seq_region_start <= $dest_slice_end) {
464 $start -= $seq_region_len;
465 } elsif ($seq_region_end >= $dest_slice_start) {
466 $end += $seq_region_len;
467 }
468 }
469 }
470 }
471  
472 $seq_region_start = $start;
473 $seq_region_end = $end;
474 $seq_region_strand *= -1;
475  
476 } ## end else [ if ( $dest_slice_strand...)]
477  
478 # Throw away features off the end of the requested slice or on
479 # different seq_region.
480 if ($seq_region_end < 1
481 || $seq_region_start > $dest_slice_length
482 || ($dest_slice_sr_id != $seq_region_id)) {
483 next FEATURE;
484 }
485 $slice = $dest_slice;
486 }
487  
488 # Finally, create the new PredictionExon.
489 push( @exons,
490 $self->_create_feature( 'Bio::EnsEMBL::PredictionExon', {
491 '-start' => $seq_region_start,
492 '-end' => $seq_region_end,
493 '-strand' => $seq_region_strand,
494 '-adaptor' => $self,
495 '-slice' => $slice,
496 '-dbID' => $prediction_exon_id,
497 '-phase' => $start_phase,
498 '-score' => $score,
499 '-p_value' => $p_value
500 } ) );
501  
502 }
503  
504 return \@exons;
505 }
506  
507  
508 1;