From c14e1a2a7ef043a0c6fd0b3c158758f38f8efaba Mon Sep 17 00:00:00 2001
From: Graham McVicker <mcvicker@sanger.ac.uk>
Date: Thu, 25 Mar 2004 13:17:29 +0000
Subject: [PATCH] Transcript::add_Exon now throws if exon could not be added
 b/c of overlap with other exon, rather than silently doing nothing

---
 modules/Bio/EnsEMBL/Transcript.pm | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/modules/Bio/EnsEMBL/Transcript.pm b/modules/Bio/EnsEMBL/Transcript.pm
index 838bb1145e..d4d6f2cee7 100755
--- a/modules/Bio/EnsEMBL/Transcript.pm
+++ b/modules/Bio/EnsEMBL/Transcript.pm
@@ -647,16 +647,20 @@ sub add_Exon{
 
   $self->{'_trans_exon_array'} ||= [];
 
+  my $was_added = 0;
+
   my $ea = $self->{'_trans_exon_array'};
   if( @$ea ) {
     if( $exon->strand() == 1 ) {
       if( $exon->start() > $ea->[$#$ea]->end() ) {
         push(@{$self->{'_trans_exon_array'}},$exon);
+        $was_added = 1;
       } else {
         # insert it at correct place
         for( my $i=0; $i <= $#$ea; $i++ ) {
           if( $exon->end() < $ea->[$i]->start() ) {
             splice( @$ea, $i, 0, $exon );
+            $was_added = 1;
             last;
           }
         }
@@ -664,11 +668,13 @@ sub add_Exon{
     } else {
       if( $exon->end() < $ea->[$#$ea]->start() ) {
         push(@{$self->{'_trans_exon_array'}},$exon);
+        $was_added = 1;
       } else {
         # insert it at correct place
         for( my $i=0; $i <= $#$ea; $i++ ) {
           if( $exon->start() > $ea->[$i]->end() ) {
             splice( @$ea, $i, 0, $exon );
+            $was_added = 1;
             last;
           }
         }
@@ -676,7 +682,26 @@ sub add_Exon{
     }
   } else {
     push( @$ea, $exon );
+    $was_added = 1;
   }
+
+  # sanity check:
+  if(!$was_added) {
+    # exon was not added because it has same end coord as start
+    # of another exon
+    my $all_str = '';
+    foreach my $e (@$ea) {
+      $all_str .= '  '.$e->start .'-'.$e->end.' ('.$e->strand.') ' .
+        ($e->stable_id || '') . "\n";
+    }
+    my $cur_str = '  '.$exon->start.'-'.$exon->end. ' ('.$exon->strand.') '.
+      ($exon->stable_id || '')."\n";
+    throw("Exon overlaps with other exon in same transcript.\n" .
+          "Transcript Exons:\n$all_str\n" .
+          "This Exon:\n$cur_str");
+    throw("Exon overlaps with other exon in same transcript.");
+  }
+
   # recalculate start, end, slice, strand
   $self->recalculate_coordinates();
 }
-- 
GitLab