EnsEMBL/DBSQL/OperonAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 318 Branchbran 0.0% 0 / 110 Conditioncond 0.0% 0 / 70 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 540 CC 56 SCAR 58.1
CC
56
Cov
0%
_objs_from_sth
812.0
fetch_all_by_Slice
156.0
store
156.0
CRAP 332.5
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  
22 =head1 CONTACT
23  
24 Please email comments or questions to the public Ensembl
25 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
26  
27 Questions may also be sent to the Ensembl help desk at
28 <http://www.ensembl.org/Help/Contact>.
29  
30 =cut
31  
32 =head1 NAME
33  
34 Bio::EnsEMBL::DBSQL::OperonAdaptor - Database adaptor for the retrieval and
35 storage of Operon objects
36  
37 =head1 SYNOPSIS
38  
39 my $operon_adaptor = Bio::EnsEMBL::DBSQL::OperonAdaptor->new($dba);
40 $operon_adaptor->store($operon);
41 my $operon2 = $operon_adaptor->fetch_by_dbID( $operon->dbID() );
42  
43 =head1 DESCRIPTION
44  
45 This is a database aware adaptor for the retrieval and storage of operon
46 objects.
47  
48 =head1 METHODS
49  
50 =cut
51  
52 package Bio::EnsEMBL::DBSQL::OperonAdaptor;
53 $Bio::EnsEMBL::DBSQL::OperonAdaptor::VERSION = '114.0.0_50'; # TRIAL
54 $Bio::EnsEMBL::DBSQL::OperonAdaptor::VERSION = '114.0.050';
55 use strict;
56  
57 use Bio::EnsEMBL::Utils::Exception qw( throw warning );
58 use Bio::EnsEMBL::Utils::Scalar qw( assert_ref );
59 use Bio::EnsEMBL::DBSQL::SliceAdaptor;
60 use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
61 use Bio::EnsEMBL::DBSQL::DBAdaptor;
62 use Bio::EnsEMBL::Operon;
63  
64 use vars '@ISA';
65 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor);
66  
67 # _tables
68 # Arg [1] : 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 # Status : Stable
75  
76 sub _tables {
77 return ( [ 'operon', 'o' ] );
78 }
79  
80 # _columns
81 # Arg [1] : none
82 # Example : none
83 # Description: PROTECTED implementation of superclass abstract method.
84 # Returns a list of columns to use for queries.
85 # Returntype : list of strings
86 # Exceptions : none
87 # Caller : internal
88 # Status : Stable
89  
90 sub _columns {
91 my ($self) = @_;
92  
93 my $created_date =
94 $self->db()->dbc()->from_date_to_seconds("o.created_date");
95 my $modified_date =
96 $self->db()->dbc()->from_date_to_seconds("o.modified_date");
97  
98 return ( 'o.operon_id', 'o.seq_region_id', 'o.seq_region_start',
99 'o.seq_region_end', 'o.seq_region_strand', 'o.display_label',
100 'o.analysis_id', 'o.stable_id', 'o.version',
101 $created_date, $modified_date );
102 }
103  
104 =head2 list_dbIDs
105  
106 Example : @operon_ids = @{$operon_adaptor->list_dbIDs()};
107 Description: Gets an array of internal ids for all operons in the current db
108 Arg[1] : <optional> int. not 0 for the ids to be sorted by the seq_region.
109 Returntype : Listref of Ints
110 Exceptions : none
111 Caller : general
112 Status : Stable
113  
114 =cut
115  
116 sub list_dbIDs {
117 my ( $self, $ordered ) = @_;
118  
119 return $self->_list_dbIDs( "operon", undef, $ordered );
120 }
121  
122 =head2 list_stable_ids
123  
124 Example : @stable_operon_ids = @{$operon_adaptor->list_stable_ids()};
125 Description: Gets an listref of stable ids for all operons in the current db
126 Returntype : reference to a list of strings
127 Exceptions : none
128 Caller : general
129 Status : Stable
130  
131 =cut
132  
133 sub list_stable_ids {
134 my ($self) = @_;
135  
136 return $self->_list_dbIDs( "operon", "stable_id" );
137 }
138  
139 sub list_seq_region_ids {
140 my $self = shift;
141  
142 return $self->_list_seq_region_ids('operon');
143 }
144  
145 =head2 fetch_by_name
146  
147 Arg [1] : String $label - name of operon to fetch
148 Example : my $operon = $operonAdaptor->fetch_by_name("accBC");
149 Description: Returns the operon which has the given display label or undef if
150 there is none. If there are more than 1, only the first is
151 reported.
152 Returntype : Bio::EnsEMBL::Operon
153 Exceptions : none
154 Caller : general
155 Status : Stable
156  
157 =cut
158  
159 sub fetch_by_name {
160 my $self = shift;
161 my $label = shift;
162  
163 my $constraint = "o.display_label = ?";
164 $self->bind_param_generic_fetch( $label, SQL_VARCHAR );
165 my ($operon) = @{ $self->generic_fetch($constraint) };
166  
167 return $operon;
168 }
169  
170 =head2 fetch_by_stable_id
171  
172 Arg [1] : String $id
173 The stable ID of the operon to retrieve
174 Example : $operon = $operon_adaptor->fetch_by_stable_id('ENSG00000148944');
175 Description: Retrieves a operon object from the database via its stable id.
176 The operon will be retrieved in its native coordinate system (i.e.
177 in the coordinate system it is stored in the database). It may
178 be converted to a different coordinate system through a call to
179 transform() or transfer(). If the operon or exon is not found
180 undef is returned instead.
181 Returntype : Bio::EnsEMBL::Operon or undef
182 Exceptions : if we cant get the operon in given coord system
183 Caller : general
184 Status : Stable
185  
186 =cut
187  
188 sub fetch_by_stable_id {
189 my ( $self, $stable_id ) = @_;
190  
191 my $constraint = "o.stable_id = ?";
192 $self->bind_param_generic_fetch( $stable_id, SQL_VARCHAR );
193 my ($operon) = @{ $self->generic_fetch($constraint) };
194  
195 # If we didn't get anything back, desperately try to see if there's
196 # a version number in the stable_id
197 if(!defined($operon) && (my $vindex = rindex($stable_id, '.'))) {
198 $operon = $self->fetch_by_stable_id_version(substr($stable_id,0,$vindex),
199 substr($stable_id,$vindex+1));
200 }
201  
202 return $operon;
203 }
204  
205 =head2 fetch_all
206  
207 Example : $operons = $operon_adaptor->fetch_all();
208 Description : Similar to fetch_by_stable_id, but retrieves all
209 operons stored in the database.
210 Returntype : listref of Bio::EnsEMBL::Operon
211 Caller : general
212 Status : At Risk
213  
214 =cut
215  
216 =head2 fetch_by_stable_id_version
217  
218 Arg [1] : String $id
219 The stable ID of the operon to retrieve
220 Arg [2] : Integer $version
221 The version of the stable_id to retrieve
222 Example : $operon = $operon_adaptor->fetch_by_stable_id('16152-16153-4840', 2);
223 Description: Retrieves an operon object from the database via its stable id and version.
224 The operon will be retrieved in its native coordinate system (i.e.
225 in the coordinate system it is stored in the database). It may
226 be converted to a different coordinate system through a call to
227 transform() or transfer(). If the operon is not found
228 undef is returned instead.
229 Returntype : Bio::EnsEMBL::Operon or undef
230 Exceptions : if we cant get the operon in given coord system
231 Caller : general
232 Status : Stable
233  
234 =cut
235  
236 sub fetch_by_stable_id_version {
237 my ($self, $stable_id, $version) = @_;
238  
239 # Enforce that version be numeric
240 return unless($version =~ /^\d+$/);
241  
242 my $constraint = "o.stable_id = ? AND o.version = ?";
243 $self->bind_param_generic_fetch($stable_id, SQL_VARCHAR);
244 $self->bind_param_generic_fetch($version, SQL_INTEGER);
245 my ($operon) = @{$self->generic_fetch($constraint)};
246  
247 return $operon;
248 }
249  
250 sub fetch_all {
251 my ($self) = @_;
252  
253 my $constraint = '';
254 my @operons = @{ $self->generic_fetch($constraint) };
255 return \@operons;
256 }
257  
258 =head2 fetch_all_versions_by_stable_id
259  
260 Arg [1] : String $stable_id
261 The stable ID of the operon to retrieve
262 Example : $operon = $operon_adaptor->fetch_all_versions_by_stable_id
263 ('ENSG00000148944');
264 Description : Similar to fetch_by_stable_id, but retrieves all versions of a
265 operon stored in the database.
266 Returntype : listref of Bio::EnsEMBL::Operon
267 Exceptions : if we cant get the operon in given coord system
268 Caller : general
269 Status : At Risk
270  
271 =cut
272  
273 sub fetch_all_versions_by_stable_id {
274 my ( $self, $stable_id ) = @_;
275  
276 my $constraint = "o.stable_id = ?";
277 $self->bind_param_generic_fetch( $stable_id, SQL_VARCHAR );
278 return $self->generic_fetch($constraint);
279 }
280  
281 =head2 fetch_all_by_Slice
282  
283 Arg [1] : Bio::EnsEMBL::Slice $slice
284 The slice to fetch operons on.
285 Arg [2] : (optional) string $logic_name
286 the logic name of the type of features to obtain
287 Arg [3] : (optional) boolean $load_transcripts
288 if true, transcripts will be loaded immediately rather than
289 lazy loaded later.
290 Arg [4] : (optional) string $source
291 the source name of the features to obtain.
292 Arg [5] : (optional) string biotype
293 the biotype of the features to obtain.
294 Example : @operons = @{$operon_adaptor->fetch_all_by_Slice()};
295 Description: Overrides superclass method to optionally load transcripts
296 immediately rather than lazy-loading them later. This
297 is more efficient when there are a lot of operons whose
298 transcripts are going to be used.
299 Returntype : reference to list of operons
300 Exceptions : thrown if exon cannot be placed on transcript slice
301 Caller : Slice::get_all_operons
302 Status : Stable
303  
304 =cut
305  
306 sub fetch_all_by_Slice {
307 my ( $self, $slice, $logic_name, $load_transcripts ) = @_;
308  
309 my $constraint = '';
310 my $operons =
311 $self->SUPER::fetch_all_by_Slice_constraint( $slice, $constraint,
312 $logic_name );
313  
314 # If there are less than two operons, still do lazy-loading.
315 if ( !$load_transcripts || @$operons < 2 ) {
316 return $operons;
317 }
318  
319 # Preload all of the transcripts now, instead of lazy loading later,
320 # faster than one query per transcript.
321  
322 # First check if transcripts are already preloaded.
323 # FIXME: Should check all transcripts.
324 if ( exists( $operons->[0]->{'_operon_transcript_array'} ) ) {
325 return $operons;
326 }
327  
328 # Get extent of region spanned by transcripts.
329 my ($min_start, $max_end);
330 my $ext_slice;
331  
332 unless ($slice->is_circular()) {
333 foreach my $o (@$operons) {
334 if (!defined($min_start) || $o->seq_region_start() < $min_start) {
335 $min_start = $o->seq_region_start();
336 }
337 if (!defined($max_end) || $o->seq_region_end() > $max_end) {
338 $max_end = $o->seq_region_end();
339 }
340 }
341  
342 if ($min_start >= $slice->start() && $max_end <= $slice->end()) {
343 $ext_slice = $slice;
344 } else {
345 my $sa = $self->db()->get_SliceAdaptor();
346 $ext_slice = $sa->fetch_by_region($slice->coord_system->name(), $slice->seq_region_name(), $min_start, $max_end, $slice->strand(), $slice->coord_system->version());
347 }
348  
349 } else {
350 # feature might be crossing the origin of replication (i.e. seq_region_start > seq_region_end)
351 # the computation of min_start|end based on seq_region_start|end is not safe
352 # use feature start/end relative to the slice instead
353 my ($min_start_feature, $max_end_feature);
354 foreach my $o (@$operons) {
355 if (!defined($min_start) || ($o->start() >= 0 && $o->start() < $min_start)) {
356 $min_start = $o->start();
357 $min_start_feature = $o;
358 }
359 if (!defined($max_end) || ($o->end() >= 0 && $o->end() > $max_end)) {
360 $max_end = $o->end();
361 $max_end_feature = $o;
362 }
363 }
364
365 # now we can reassign min_start|end to seq_region_start|end of
366 # the feature which spans the largest region
367 $min_start = $min_start_feature->seq_region_start();
368 $max_end = $max_end_feature->seq_region_end();
369  
370 my $sa = $self->db()->get_SliceAdaptor();
371 $ext_slice =
372 $sa->fetch_by_region($slice->coord_system->name(),
373 $slice->seq_region_name(),
374 $min_start,
375 $max_end,
376 $slice->strand(),
377 $slice->coord_system->version());
378 }
379  
380  
381 # Associate transcript identifiers with operons.
382  
383 my %o_hash = map { $_->dbID => $_ } @{$operons};
384  
385 my $o_id_str = join( ',', keys(%o_hash) );
386  
387 my $sth =
388 $self->prepare( "SELECT operon_id, operon_transcript_id "
389 . "FROM operon_transcript "
390 . "WHERE operon_id IN ($o_id_str)" );
391  
392 $sth->execute();
393  
394 my ( $o_id, $tr_id );
395 $sth->bind_columns( \( $o_id, $tr_id ) );
396  
397 my %tr_o_hash;
398  
399 while ( $sth->fetch() ) {
400 $tr_o_hash{$tr_id} = $o_hash{$o_id};
401 }
402  
403 my $ta = $self->db()->get_OperonTranscriptAdaptor();
404 my $transcripts =
405 $ta->fetch_all_by_Slice( $ext_slice,
406 1, undef,
407 sprintf( "ot.operon_transcript_id IN (%s)",
408 join( ',',
409 sort { $a <=> $b }
410 keys(%tr_o_hash) ) ) );
411  
412 # Move transcripts onto operon slice, and add them to operons.
413 foreach my $tr ( @{$transcripts} ) {
414 if ( !exists( $tr_o_hash{ $tr->dbID() } ) ) {
415 next;
416 }
417  
418 my $new_tr;
419 if ( $slice != $ext_slice ) {
420 $new_tr = $tr->transfer($slice);
421 if ( !defined($new_tr) ) {
422 throw( "Unexpected. "
423 . "Transcript could not be transfered onto operon slice."
424 );
425 }
426 } else {
427 $new_tr = $tr;
428 }
429  
430 $tr_o_hash{ $tr->dbID() }->add_OperonTranscript($new_tr);
431 }
432  
433 return $operons;
434 } ## end sub fetch_all_by_Slice
435  
436 =head2 fetch_by_transcript_id
437  
438 Arg [1] : Int $trans_id
439 Unique database identifier for the transcript whose operon should
440 be retrieved. The operon is returned in its native coord
441 system (i.e. the coord_system it is stored in). If the coord
442 system needs to be changed, then tranform or transfer should
443 be called on the returned object. undef is returned if the
444 operon or transcript is not found in the database.
445 Example : $operon = $operon_adaptor->fetch_by_transcript_id(1241);
446 Description: Retrieves a operon from the database via the database identifier
447 of one of its transcripts.
448 Returntype : Bio::EnsEMBL::Operon
449 Exceptions : none
450 Caller : operonral
451 Status : Stable
452  
453 =cut
454  
455 sub fetch_by_operon_transcript_id {
456 my ( $self, $trans_id ) = @_;
457  
458 # this is a cheap SQL call
459 my $sth = $self->prepare(
460 qq(
461 SELECT tr.operon_id
462 FROM operon_transcript tr
463 WHERE tr.operon_transcript_id = ?
464 ) );
465  
466 $sth->bind_param( 1, $trans_id, SQL_INTEGER );
467 $sth->execute();
468  
469 my ($operonid) = $sth->fetchrow_array();
470  
471 $sth->finish();
472  
473 return undef if ( !defined $operonid );
474  
475 my $operon = $self->fetch_by_dbID($operonid);
476 return $operon;
477 }
478  
479 =head2 fetch_by_operon_transcript_stable_id
480  
481 Arg [1] : string $trans_stable_id
482 transcript stable ID whose operon should be retrieved
483 Example : my $operon = $operon_adaptor->fetch_by_operon_transcript_stable_id
484 ('ENST0000234');
485 Description: Retrieves a operon from the database via the stable ID of one of
486 its transcripts
487 Returntype : Bio::EnsEMBL::Operon
488 Exceptions : none
489 Caller : operonral
490 Status : Stable
491  
492 =cut
493  
494 sub fetch_by_operon_transcript_stable_id {
495 my ( $self, $trans_stable_id ) = @_;
496  
497 my $sth = $self->prepare(
498 qq(
499 SELECT operon_id
500 FROM operon_transcript
501 WHERE stable_id = ?
502 ) );
503  
504 $sth->bind_param( 1, $trans_stable_id, SQL_VARCHAR );
505 $sth->execute();
506  
507 my ($operonid) = $sth->fetchrow_array();
508 $sth->finish;
509  
510 return undef if ( !defined $operonid );
511  
512 my $operon = $self->fetch_by_dbID($operonid);
513 return $operon;
514 }
515  
516 sub fetch_by_operon_transcript {
517 my ( $self, $trans ) = @_;
518 assert_ref( $trans, 'Bio::EnsEMBL::OperonTranscript' );
519 $self->fetch_by_operon_transcript_id( $trans->dbID() );
520 }
521  
522 =head2 store
523  
524 Arg [1] : Bio::EnsEMBL::Operon $operon
525 The operon to store in the database
526 Arg [2] : ignore_release in xrefs [default 1] set to 0 to use release info
527 in external database references
528 Example : $operon_adaptor->store($operon);
529 Description: Stores a operon in the database.
530 Returntype : the database identifier (dbID) of the newly stored operon
531 Exceptions : thrown if the $operon is not a Bio::EnsEMBL::Operon or if
532 $operon does not have an analysis object
533 Caller : general
534 Status : Stable
535  
536 =cut
537  
538 sub store {
539 my ( $self, $operon, $ignore_release ) = @_;
540  
541 if ( !ref $operon || !$operon->isa('Bio::EnsEMBL::Operon') ) {
542 throw("Must store a operon object, not a $operon");
543 }
544  
545 my $db = $self->db();
546  
547 if ( $operon->is_stored($db) ) {
548 return $operon->dbID();
549 }
550 my $analysis = $operon->analysis();
551 throw("Operons must have an analysis object.") if(!defined($analysis));
552 my $analysis_id;
553 if ( $analysis->is_stored($db) ) {
554 $analysis_id = $analysis->dbID();
555 } else {
556 $analysis_id = $db->get_AnalysisAdaptor->store( $analysis );
557 }
558 # ensure coords are correct before storing
559 #$operon->recalculate_coordinates();
560  
561 my $seq_region_id;
562  
563 ( $operon, $seq_region_id ) = $self->_pre_store($operon);
564  
565 my @columns = qw(
566 seq_region_id
567 seq_region_start
568 seq_region_end
569 seq_region_strand
570 display_label
571 analysis_id
572 );
573 my @canned_columns;
574 my @canned_values;
575  
576 if ( defined($operon->stable_id()) ) {
577 push @columns, qw(
578 stable_id
579 version
580 );
581 my $created = $self->db->dbc->from_seconds_to_date($operon->created_date());
582 my $modified = $self->db->dbc->from_seconds_to_date($operon->modified_date());
583  
584 if ($created) {
585 push @canned_columns, 'created_date';
586 push @canned_values, $created;
587 }
588 if ($modified) {
589 push @canned_columns, 'modified_date';
590 push @canned_values, $modified;
591 }
592 }
593  
594 my $i_columns = join(', ', @columns, @canned_columns);
595 my $i_values = join(', ', (('?') x scalar(@columns)), @canned_values);
596 my $store_operon_sql = qq(
597 INSERT INTO operon ( ${i_columns} ) VALUES ( $i_values )
598 );
599  
600 my $sth = $self->prepare($store_operon_sql);
601 $sth->bind_param( 1, $seq_region_id, SQL_INTEGER );
602 $sth->bind_param( 2, $operon->start(), SQL_INTEGER );
603 $sth->bind_param( 3, $operon->end(), SQL_INTEGER );
604 $sth->bind_param( 4, $operon->strand(), SQL_TINYINT );
605 $sth->bind_param( 5, $operon->display_label(), SQL_VARCHAR );
606 $sth->bind_param( 6, $analysis_id, SQL_INTEGER );
607  
608 if ( defined($operon->stable_id()) ) {
609 $sth->bind_param( 7, $operon->stable_id(), SQL_VARCHAR );
610 my $version = ($operon->version()) ? $operon->version() : 1;
611 $sth->bind_param( 8, $version, SQL_INTEGER );
612 }
613 $sth->execute();
614 $sth->finish();
615  
616 my $operon_dbID = $self->last_insert_id('operon_id', undef, 'operon');
617  
618 my $transcripts = $operon->get_all_OperonTranscripts();
619  
620 if ( $transcripts && scalar @$transcripts ) {
621 my $transcript_adaptor = $db->get_OperonTranscriptAdaptor();
622 for my $transcript (@$transcripts) {
623 $transcript_adaptor->store( $transcript, $operon_dbID );
624 }
625 }
626  
627 # store the dbentries associated with this operon
628 my $dbEntryAdaptor = $db->get_DBEntryAdaptor();
629  
630 foreach my $dbe ( @{ $operon->get_all_DBEntries } ) {
631 $dbEntryAdaptor->store( $dbe, $operon_dbID, "Operon", $ignore_release );
632 }
633  
634 # store operon attributes if there are any
635 my $attrs = $operon->get_all_Attributes();
636 if ( $attrs && scalar @$attrs ) {
637 my $attr_adaptor = $db->get_AttributeAdaptor();
638 $attr_adaptor->store_on_Operon( $operon, $attrs );
639 }
640  
641 # set the adaptor and dbID on the original passed in operon not the
642 # transfered copy
643 $operon->adaptor($self);
644 $operon->dbID($operon_dbID);
645  
646 return $operon_dbID;
647 } ## end sub store
648  
649 =head2 remove
650  
651 Arg [1] : Bio::EnsEMBL::Operon $operon
652 the operon to remove from the database
653 Example : $operon_adaptor->remove($operon);
654 Description: Removes a operon completely from the database. All associated
655 transcripts, exons, stable_identifiers, descriptions, etc.
656 are removed as well. Use with caution!
657 Returntype : none
658 Exceptions : throw on incorrect arguments
659 warning if operon is not stored in this database
660 Caller : general
661 Status : Stable
662  
663 =cut
664  
665 sub remove {
666 my $self = shift;
667 my $operon = shift;
668  
669 if ( !ref($operon) || !$operon->isa('Bio::EnsEMBL::Operon') ) {
670 throw("Bio::EnsEMBL::Operon argument expected.");
671 }
672  
673 if ( !$operon->is_stored( $self->db() ) ) {
674 warning( "Cannot remove operon "
675 . $operon->dbID()
676 . ". Is not stored in "
677 . "this database." );
678 return;
679 }
680  
681 # remove all object xrefs associated with this operon
682  
683 my $dbe_adaptor = $self->db()->get_DBEntryAdaptor();
684 foreach my $dbe ( @{ $operon->get_all_DBEntries() } ) {
685 $dbe_adaptor->remove_from_object( $dbe, $operon, 'Operon' );
686 }
687  
688 # remove all of the transcripts associated with this operon
689 my $transcriptAdaptor = $self->db->get_OperonTranscriptAdaptor();
690 foreach my $trans ( @{ $operon->get_all_OperonTranscripts() } ) {
691 $transcriptAdaptor->remove($trans);
692 }
693  
694 # remove this operon from the database
695  
696 my $sth = $self->prepare("DELETE FROM operon WHERE operon_id = ? ");
697 $sth->bind_param( 1, $operon->dbID, SQL_INTEGER );
698 $sth->execute();
699 $sth->finish();
700  
701 # unset the operon identifier and adaptor thereby flagging it as unstored
702  
703 $operon->dbID(undef);
704 $operon->adaptor(undef);
705  
706 return;
707 } ## end sub remove
708  
709 # _objs_from_sth
710  
711 # Arg [1] : StatementHandle $sth
712 # Arg [2] : Bio::EnsEMBL::AssemblyMapper $mapper
713 # Arg [3] : Bio::EnsEMBL::Slice $dest_slice
714 # Description: PROTECTED implementation of abstract superclass method.
715 # responsible for the creation of Operons
716 # Returntype : listref of Bio::EnsEMBL::Operon in target coordinate system
717 # Exceptions : none
718 # Caller : internal
719 # Status : Stable
720  
721 sub _objs_from_sth {
722 my ($self, $sth, $mapper, $dest_slice) = @_;
723  
724 #
725 # This code is ugly because an attempt has been made to remove as many
726 # function calls as possible for speed purposes. Thus many caches and
727 # a fair bit of gymnastics is used.
728 #
729  
730 my $sa = $self->db()->get_SliceAdaptor();
731 my $aa = $self->db()->get_AnalysisAdaptor();
732  
733 my @operons;
734 my %analysis_hash;
735 my %slice_hash;
736 my %sr_name_hash;
737 my %sr_cs_hash;
738  
739 my (
740 $operon_id, $seq_region_id, $seq_region_start,
741 $seq_region_end, $seq_region_strand, $display_label,
742 $analysis_id, $stable_id, $version,
743 $created_date, $modified_date
744 );
745  
746 $sth->bind_columns( \(
747 $operon_id, $seq_region_id, $seq_region_start,
748 $seq_region_end, $seq_region_strand, $display_label,
749 $analysis_id, $stable_id, $version,
750 $created_date, $modified_date ));
751  
752 my $dest_slice_start;
753 my $dest_slice_end;
754 my $dest_slice_strand;
755 my $dest_slice_length;
756 my $dest_slice_cs;
757 my $dest_slice_sr_name;
758 my $dest_slice_sr_id;
759 my $asma;
760  
761 if ($dest_slice) {
762 $dest_slice_start = $dest_slice->start();
763 $dest_slice_end = $dest_slice->end();
764 $dest_slice_strand = $dest_slice->strand();
765 $dest_slice_length = $dest_slice->length();
766 $dest_slice_cs = $dest_slice->coord_system();
767 $dest_slice_sr_name = $dest_slice->seq_region_name();
768 $dest_slice_sr_id = $dest_slice->get_seq_region_id();
769 $asma = $self->db->get_AssemblyMapperAdaptor();
770 }
771  
772 OPERON: while ( $sth->fetch() ) {
773  
774 #get the analysis object
775 my $analysis = $analysis_hash{$analysis_id} ||= $aa->fetch_by_dbID($analysis_id);
776 $analysis_hash{$analysis_id} = $analysis;
777  
778 #need to get the internal_seq_region, if present
779 $seq_region_id = $self->get_seq_region_id_internal($seq_region_id);
780 my $slice = $slice_hash{"ID:".$seq_region_id};
781  
782 if (!$slice) {
783 $slice = $sa->fetch_by_seq_region_id($seq_region_id);
784 $slice_hash{"ID:".$seq_region_id} = $slice;
785 $sr_name_hash{$seq_region_id} = $slice->seq_region_name();
786 $sr_cs_hash{$seq_region_id} = $slice->coord_system();
787 }
788  
789 #obtain a mapper if none was defined, but a dest_seq_region was
790 if(!$mapper && $dest_slice && !$dest_slice_cs->equals($slice->coord_system)) {
791 $mapper = $asma->fetch_by_CoordSystems($dest_slice_cs, $slice->coord_system);
792 }
793  
794 my $sr_name = $sr_name_hash{$seq_region_id};
795 my $sr_cs = $sr_cs_hash{$seq_region_id};
796  
797 #
798 # remap the feature coordinates to another coord system
799 # if a mapper was provided
800 #
801  
802 if ($mapper) {
803  
804 if (defined $dest_slice && $mapper->isa('Bio::EnsEMBL::ChainedAssemblyMapper') ) {
805 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
806 $mapper->map($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs, 1, $dest_slice);
807  
808 } else {
809 ($seq_region_id, $seq_region_start, $seq_region_end, $seq_region_strand) =
810 $mapper->fastmap($sr_name, $seq_region_start, $seq_region_end, $seq_region_strand, $sr_cs);
811 }
812  
813 #skip features that map to gaps or coord system boundaries
814 next OPERON if (!defined($seq_region_id));
815  
816 #get a slice in the coord system we just mapped to
817 $slice = $slice_hash{"ID:".$seq_region_id} ||= $sa->fetch_by_seq_region_id($seq_region_id);
818 }
819  
820 #
821 # If a destination slice was provided convert the coords.
822 #
823 if (defined($dest_slice)) {
824 my $seq_region_len = $dest_slice->seq_region_length();
825  
826 if ( $dest_slice_strand == 1 ) {
827 $seq_region_start = $seq_region_start - $dest_slice_start + 1;
828 $seq_region_end = $seq_region_end - $dest_slice_start + 1;
829  
830 if ( $dest_slice->is_circular ) {
831 # Handle circular chromosomes.
832  
833 if ( $seq_region_start > $seq_region_end ) {
834 # Looking at a feature overlapping the chromosome origin.
835  
836 if ( $seq_region_end > $dest_slice_start ) {
837 # Looking at the region in the beginning of the chromosome
838 $seq_region_start -= $seq_region_len;
839 }
840 if ( $seq_region_end < 0 ) {
841 $seq_region_end += $seq_region_len;
842 }
843 } else {
844 if ($dest_slice_start > $dest_slice_end && $seq_region_end < 0) {
845 # Looking at the region overlapping the chromosome
846 # origin and a feature which is at the beginning of the
847 # chromosome.
848 $seq_region_start += $seq_region_len;
849 $seq_region_end += $seq_region_len;
850 }
851 }
852 }
853 } else {
854  
855 my $start = $dest_slice_end - $seq_region_end + 1;
856 my $end = $dest_slice_end - $seq_region_start + 1;
857  
858 if ($dest_slice->is_circular()) {
859  
860 if ($dest_slice_start > $dest_slice_end) {
861 # slice spans origin or replication
862  
863 if ($seq_region_start >= $dest_slice_start) {
864 $end += $seq_region_len;
865 $start += $seq_region_len if $seq_region_end > $dest_slice_start;
866  
867 } elsif ($seq_region_start <= $dest_slice_end) {
868 # do nothing
869 } elsif ($seq_region_end >= $dest_slice_start) {
870 $start += $seq_region_len;
871 $end += $seq_region_len;
872  
873 } elsif ($seq_region_end <= $dest_slice_end) {
874 $end += $seq_region_len if $end < 0;
875  
876 } elsif ($seq_region_start > $seq_region_end) {
877 $end += $seq_region_len;
878 }
879  
880 } else {
881  
882 if ($seq_region_start <= $dest_slice_end and $seq_region_end >= $dest_slice_start) {
883 # do nothing
884 } elsif ($seq_region_start > $seq_region_end) {
885 if ($seq_region_start <= $dest_slice_end) {
886 $start -= $seq_region_len;
887 } elsif ($seq_region_end >= $dest_slice_start) {
888 $end += $seq_region_len;
889 }
890 }
891 }
892 }
893  
894 $seq_region_start = $start;
895 $seq_region_end = $end;
896 $seq_region_strand *= -1;
897  
898 } ## end else [ if ( $dest_slice_strand...)]
899  
900 # Throw away features off the end of the requested slice or on
901 # different seq_region.
902 if ($seq_region_end < 1
903 || $seq_region_start > $dest_slice_length
904 || ($dest_slice_sr_id != $seq_region_id)) {
905 next OPERON;
906 }
907 $slice = $dest_slice;
908 } ## end if ($dest_slice)
909  
910 push( @operons,
911 Bio::EnsEMBL::Operon->new(
912 -START => $seq_region_start,
913 -END => $seq_region_end,
914 -STRAND => $seq_region_strand,
915 -SLICE => $slice,
916 -DISPLAY_LABEL => $display_label,
917 -ADAPTOR => $self,
918 -DBID => $operon_id,
919 -STABLE_ID => $stable_id,
920 -VERSION => $version,
921 -CREATED_DATE => $created_date || undef,
922 -MODIFIED_DATE => $modified_date || undef,
923 -ANALYSIS => $analysis ) );
924  
925 } ## end while ( $sth->fetch() )
926  
927 return \@operons;
928 } ## end sub _objs_from_sth
929  
930 1;
931