From 7e8d249067b6d59af1f2c1fcfe80b52f17de48de Mon Sep 17 00:00:00 2001
From: Andrew Yates <ayates@ebi.ac.uk>
Date: Thu, 23 Feb 2012 12:22:34 +0000
Subject: [PATCH] Made the circular slice code compartmentalised to just
 SliceAdaptor (making cache initalisation code easier) and we now only build
 circular slices when the seqregion it is based off is flagged as circular

---
 modules/Bio/EnsEMBL/DBSQL/SliceAdaptor.pm | 48 +++++++++++++++++------
 modules/Bio/EnsEMBL/Slice.pm              | 24 ++++--------
 2 files changed, 44 insertions(+), 28 deletions(-)

diff --git a/modules/Bio/EnsEMBL/DBSQL/SliceAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/SliceAdaptor.pm
index 652a9558cf..66dc9e76b4 100644
--- a/modules/Bio/EnsEMBL/DBSQL/SliceAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/SliceAdaptor.pm
@@ -398,18 +398,24 @@ sub fetch_by_region {
 
   if ( !defined($end) ) { $end = $length }
 
+  #If this was given then check if we've got a circular seq region otherwise
+  #let it fall through to the normal Slice method
   if ( $end + 1 < $start ) {
-    my $new_sl =
-      Bio::EnsEMBL::CircularSlice->new(
-                                   -COORD_SYSTEM    => $cs,
-                                   -SEQ_REGION_NAME => $seq_region_name,
-                                   -SEQ_REGION_LENGTH => $length,
-                                   -START             => $start,
-                                   -END               => $end,
-                                   -STRAND            => 1,
-                                   -ADAPTOR           => $self );
-
-    return $new_sl;
+    my $cs_id = $cs->dbID();
+    my $seq_region_id = $self->{'sr_name_cache'}->{"$seq_region_name:$cs_id"} = $arr->[0];
+    if($self->is_circular($seq_region_id)) {
+      my $new_sl =
+        Bio::EnsEMBL::CircularSlice->new(
+                                     -COORD_SYSTEM    => $cs,
+                                     -SEQ_REGION_NAME => $seq_region_name,
+                                     -SEQ_REGION_LENGTH => $length,
+                                     -START             => $start,
+                                     -END               => $end,
+                                     -STRAND            => 1,
+                                     -ADAPTOR           => $self );
+  
+      return $new_sl;
+    }
   }
 
   if ( defined( $self->{'lrg_region_test'} )
@@ -1048,6 +1054,26 @@ sub is_reference {
   return 1;
 }
 
+=head2 is_circular
+
+  Arg[1]      : int seq_region_id
+  Example     : my $circular = $slice_adptor->is_circular($seq_region_id);
+  Description : Indicates if the sequence region was circular or not
+  Returntype  : Boolean
+  
+=cut
+
+sub is_circular {
+  my ($self, $id) = @_;
+  
+  if (! defined $self->{is_circular}) {
+    $self->_build_circular_slice_cache();
+  }
+  
+  return 0 if $self->{is_circular} == 0;
+  return (exists $self->{circular_sr_id_cache}->{$id}) ? 1 : 0;
+}
+
 =head2 fetch_by_band
 
  Title   : fetch_by_band
diff --git a/modules/Bio/EnsEMBL/Slice.pm b/modules/Bio/EnsEMBL/Slice.pm
index 2961790e35..26eca280fa 100644
--- a/modules/Bio/EnsEMBL/Slice.pm
+++ b/modules/Bio/EnsEMBL/Slice.pm
@@ -532,29 +532,19 @@ sub is_toplevel {
   Description: Returns 1 if slice is a circular slice else 0
   Returntype : int
   Caller     : general
-  Status     : At Risk
+  Status     : Stable
 
 =cut
 
 sub is_circular {
   my ($self) = @_;
-
-  if ( !defined( $self->adaptor() ) ) { return 0 }
-
-  if ( !defined( $self->{'circular'} ) ) {
-    $self->{'circular'} = 0;  
-
-    if ( !defined($self->adaptor()->{'is_circular'}) ){   
-        $self->adaptor()->_build_circular_slice_cache();
-    } 
-
-    if ($self->adaptor()->{'is_circular'}) {
-	if ( exists($self->adaptor()->{'circular_sr_id_cache'}->{ $self->adaptor()->get_seq_region_id($self) } ) ) {
-		$self->{'circular'} = 1;
-	}
-    }
+  my $adaptor = $self->adaptor();
+  return 0 if ! defined $adaptor;
+  if (! exists $self->{'circular'}) {
+    my $id = $adaptor->get_seq_region_id($self);
+    $self->{circular} = $adaptor->is_circular($id);
   }
-  return $self->{'circular'};
+  return $self->{circular};
 }
 
 =head2 invert
-- 
GitLab