From 623aa3495a44b7cecd16f33bce4e9a1d5e2de22a Mon Sep 17 00:00:00 2001
From: Monika Komorowska <mk8@sanger.ac.uk>
Date: Wed, 5 Oct 2011 14:57:42 +0000
Subject: [PATCH] Backing out stable_id merging changes until databases are
 patched

---
 .../EnsEMBL/DBSQL/ArchiveStableIdAdaptor.pm   |   5 +-
 modules/Bio/EnsEMBL/DBSQL/BaseAdaptor.pm      |   1 -
 modules/Bio/EnsEMBL/DBSQL/ExonAdaptor.pm      |  68 +++++++-----
 modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm      |  92 +++++++++------
 modules/Bio/EnsEMBL/DBSQL/OperonAdaptor.pm    |  84 +++++++++-----
 .../EnsEMBL/DBSQL/OperonTranscriptAdaptor.pm  |  72 +++++++-----
 .../Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm    | 105 ++++++++++--------
 modules/Bio/EnsEMBL/UnmappedObject.pm         |   2 +-
 8 files changed, 259 insertions(+), 170 deletions(-)

diff --git a/modules/Bio/EnsEMBL/DBSQL/ArchiveStableIdAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/ArchiveStableIdAdaptor.pm
index a424187a85..e056bf6064 100644
--- a/modules/Bio/EnsEMBL/DBSQL/ArchiveStableIdAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/ArchiveStableIdAdaptor.pm
@@ -1022,7 +1022,7 @@ sub add_all_current_to_history {
 
   # get current stable IDs from db
   my $sql = qq(
-    SELECT stable_id, version FROM ${type}
+    SELECT stable_id, version FROM ${type}_stable_id
     WHERE stable_id IN ('$id_string')
   );
   my $sth = $self->prepare($sql);
@@ -1442,7 +1442,7 @@ sub lookup_current {
   }
 
   my $sql = qq(
-    SELECT version FROM ${type}
+    SELECT version FROM ${type}_stable_id
     WHERE stable_id = ?
   );
   my $sth = $self->prepare($sql);
@@ -1510,4 +1510,3 @@ sub _resolve_type {
 
 
 1;
-
diff --git a/modules/Bio/EnsEMBL/DBSQL/BaseAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/BaseAdaptor.pm
index 7675e5269d..fb833745a7 100755
--- a/modules/Bio/EnsEMBL/DBSQL/BaseAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/BaseAdaptor.pm
@@ -788,5 +788,4 @@ sub get_dumped_data {
     return eval ($data);
 }
 
-
 1;
diff --git a/modules/Bio/EnsEMBL/DBSQL/ExonAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/ExonAdaptor.pm
index 5273c2c271..e0e3ed5f37 100644
--- a/modules/Bio/EnsEMBL/DBSQL/ExonAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/ExonAdaptor.pm
@@ -69,7 +69,7 @@ sub _tables {
     return @{ $self->{'tables'} };
   }
 
-  return ( [ 'exon', 'e' ] );
+  return ( [ 'exon', 'e' ], [ 'exon_stable_id', 'esi' ] );
 }
 
 
@@ -95,11 +95,14 @@ sub _columns {
     'e.exon_id',        'e.seq_region_id',     'e.seq_region_start',
     'e.seq_region_end', 'e.seq_region_strand', 'e.phase',
     'e.end_phase',      'e.is_current',        'e.is_constitutive',
-    'e.stable_id',      'e.version',           $created_date,
+    'esi.stable_id',    'esi.version',         $created_date,
     $modified_date
   );
 }
 
+sub _left_join {
+  return ( [ 'exon_stable_id', "esi.exon_id = e.exon_id" ] );
+}
 
 
 # _final_clause
@@ -142,7 +145,7 @@ sub fetch_all {
 sub fetch_by_stable_id {
   my ($self, $stable_id) = @_;
 
-  my $constraint = "e.stable_id = ? AND e.is_current = 1";
+  my $constraint = "esi.stable_id = ? AND e.is_current = 1";
 
   $self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
   my ($exon) = @{ $self->generic_fetch($constraint) };
@@ -169,7 +172,7 @@ sub fetch_by_stable_id {
 sub fetch_all_versions_by_stable_id {
   my ($self, $stable_id) = @_;
 
-  my $constraint = "e.stable_id = ?";
+  my $constraint = "esi.stable_id = ?";
 
   $self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
 
@@ -297,19 +300,9 @@ sub store {
   my $exon_sql = q{
     INSERT into exon ( seq_region_id, seq_region_start,
 		       seq_region_end, seq_region_strand, phase,
-		       end_phase, is_current, is_constitutive                      
+		       end_phase, is_current, is_constitutive )
+    VALUES ( ?,?,?,?,?,?,?,? )
   };
-  if ( defined($exon->stable_id) ) {
-      my $created = $self->db->dbc->from_seconds_to_date($exon->created_date());
-      my $modified = $self->db->dbc->from_seconds_to_date($exon->modified_date());
-      $exon_sql .= ", stable_id, version, created_date, modified_date) VALUES ( ?,?,?,?,?,?,?,?,?,?,". $created . ",". $modified ." )";
-     
-  } else {
-      $exon_sql .= q{
-         ) VALUES ( ?,?,?,?,?,?,?,?)
-      };
-  }
-
 
   my $exonst = $self->prepare($exon_sql);
 
@@ -329,16 +322,32 @@ sub store {
   $exonst->bind_param( 7, $is_current,      SQL_TINYINT );
   $exonst->bind_param( 8, $is_constitutive, SQL_TINYINT );
 
-  if ( defined($exon->stable_id) ) {
-
-     $exonst->bind_param( 9, $exon->stable_id, SQL_VARCHAR );
-     my $version = ($exon->version()) ? $exon->version() : 1;
-     $exonst->bind_param( 10, $version, SQL_INTEGER ); 
-  }
-
   $exonst->execute();
   $exonId = $exonst->{'mysql_insertid'};
 
+  #store any stable_id information
+  if ($exon->stable_id && $exon->version()) {
+
+    my $statement = 
+      "INSERT INTO exon_stable_id " .
+	"SET version = ?, " .
+          "stable_id = ?, " .
+	    "exon_id = ?, ";
+  
+    $statement .= "created_date = " .
+      $self->db->dbc->from_seconds_to_date($exon->created_date()) . ",";
+    $statement .= "modified_date = " .
+      $self->db->dbc->from_seconds_to_date($exon->modified_date()) ;
+
+    my $sth = $self->prepare( $statement );
+
+    $sth->bind_param(1,( $exon->version || 1 ),SQL_INTEGER);
+    $sth->bind_param(2,$exon->stable_id,SQL_VARCHAR);
+    $sth->bind_param(3,$exonId,SQL_INTEGER);
+
+    $sth->execute();
+  }
+
   # Now the supporting evidence
   my $esf_adaptor = $db->get_SupportingFeatureAdaptor;
   $esf_adaptor->store($exonId, $exon->get_all_supporting_features);
@@ -452,6 +461,12 @@ sub remove {
   $sth->execute();
   $sth->finish();
 
+  # delete the exon stable identifier
+
+  $sth = $self->prepare( "DELETE FROM exon_stable_id WHERE exon_id = ?" );
+  $sth->bind_param(1, $exon->dbID, SQL_INTEGER);
+  $sth->execute();
+  $sth->finish();
 
   # delete the exon
 
@@ -502,7 +517,7 @@ sub list_dbIDs {
 sub list_stable_ids {
    my ($self) = @_;
 
-   return $self->_list_dbIDs("exon", "stable_id");
+   return $self->_list_dbIDs("exon_stable_id", "stable_id");
 }
 
 #_objs_from_sth
@@ -780,12 +795,11 @@ sub get_stable_entry_info {
     #$self->throw("can't fetch stable info with no dbID");
     return;
   }
-
   my $created_date = $self->db->dbc->from_date_to_seconds("created_date");
   my $modified_date = $self->db->dbc->from_date_to_seconds("modified_date");
   my $sth = $self->prepare("SELECT stable_id, " . $created_date . ",
                                    " . $modified_date . ", version 
-                            FROM   exon
+                            FROM   exon_stable_id 
                             WHERE  exon_id = ");
 
   $sth->bind_param(1, $exon->dbID, SQL_INTEGER);
@@ -868,5 +882,3 @@ sub fetch_all_by_gene_id {
 
 
 1;
-
-
diff --git a/modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm
index 0af1f92ca4..6e8f8cc056 100644
--- a/modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm
@@ -85,6 +85,7 @@ use vars '@ISA';
 sub _tables {
   return (
     [ 'gene',           'g' ],
+    [ 'gene_stable_id', 'gsi' ],
     [ 'xref',           'x' ],
     [ 'external_db',    'exdb' ] );
 }
@@ -104,9 +105,9 @@ sub _columns {
   my ($self) = @_;
 
   my $created_date =
-    $self->db()->dbc()->from_date_to_seconds("g.created_date");
+    $self->db()->dbc()->from_date_to_seconds("gsi.created_date");
   my $modified_date =
-    $self->db()->dbc()->from_date_to_seconds("g.modified_date");
+    $self->db()->dbc()->from_date_to_seconds("gsi.modified_date");
 
   return (
     'g.gene_id',                 'g.seq_region_id',
@@ -116,7 +117,7 @@ sub _columns {
     'g.description',             'g.status',
     'g.source',                  'g.is_current',
     'g.canonical_transcript_id', 'g.canonical_annotation',
-    'g.stable_id',               'g.version',
+    'gsi.stable_id',             'gsi.version',
     $created_date,               $modified_date,
     'x.display_label',           'x.dbprimary_acc',
     'x.description',             'x.version',
@@ -129,6 +130,7 @@ sub _columns {
 
 sub _left_join {
   return (
+    [ 'gene_stable_id', "gsi.gene_id = g.gene_id" ],
     [ 'xref',           "x.xref_id = g.display_xref_id" ],
     [ 'external_db',    "exdb.external_db_id = x.external_db_id" ] );
 }
@@ -167,7 +169,7 @@ sub list_dbIDs {
 sub list_stable_ids {
    my ($self) = @_;
 
-   return $self->_list_dbIDs("gene", "stable_id");
+   return $self->_list_dbIDs("gene_stable_id", "stable_id");
 }
 
 
@@ -225,7 +227,7 @@ sub fetch_by_display_label {
 sub fetch_by_stable_id {
   my ($self, $stable_id) = @_;
 
-  my $constraint = "g.stable_id = ? AND g.is_current = 1";
+  my $constraint = "gsi.stable_id = ? AND g.is_current = 1";
   $self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
   my ($gene) = @{ $self->generic_fetch($constraint) };
 
@@ -308,7 +310,7 @@ sub fetch_all {
 sub fetch_all_versions_by_stable_id {
   my ($self, $stable_id) = @_;
 
-  my $constraint = "g.stable_id = ?";
+  my $constraint = "gsi.stable_id = ?";
   $self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
   return $self->generic_fetch($constraint);
 }
@@ -339,10 +341,12 @@ sub fetch_by_exon_stable_id {
       SELECT t.gene_id
         FROM transcript as t,
              exon_transcript as et,
-             exon as e
+             exon as e,
+             exon_stable_id as esi
        WHERE t.transcript_id = et.transcript_id 
+         AND et.exon_id = esi.exon_id 
          AND et.exon_id = e.exon_id
-         AND e.stable_id = ?
+         AND esi.stable_id = ?
          AND e.is_current = 1
   );
 
@@ -696,10 +700,11 @@ sub fetch_by_transcript_stable_id {
     my ($self, $trans_stable_id) = @_;
 
     my $sth = $self->prepare(qq(
-        SELECT  gene_id
-	FROM	transcript
-        WHERE   stable_id = ?
-        AND     is_current = 1
+        SELECT  tr.gene_id
+	FROM	transcript tr, transcript_stable_id tcl
+        WHERE   tcl.stable_id = ?
+        AND     tr.transcript_id = tcl.transcript_id
+        AND     tr.is_current = 1
     ));
 
     $sth->bind_param(1, $trans_stable_id, SQL_VARCHAR);
@@ -735,8 +740,10 @@ sub fetch_by_translation_stable_id {
     my $sth = $self->prepare(qq(
         SELECT  tr.gene_id
 	FROM    transcript tr,
-                translation tl
-	WHERE   tl.stable_id = ?
+                translation tl,
+		translation_stable_id as trs
+	WHERE   trs.stable_id = ?
+	AND     trs.translation_id = tl.translation_id
         AND     tr.transcript_id = tl.transcript_id
         AND     tr.is_current = 1
     ));
@@ -1149,14 +1156,6 @@ sub store {
                canonical_transcript_id = ?,
                canonical_annotation = ?
   );
-
-  if (defined($gene->stable_id)) {
-      my $created = $self->db->dbc->from_seconds_to_date($gene->created_date());
-      my $modified = $self->db->dbc->from_seconds_to_date($gene->modified_date());
-      $store_gene_sql .= ", stable_id = ?, version = ?, created_date = " . $created . " , modified_date = " . $modified;
-
-  }
-
   # column status is used from schema version 34 onwards (before it was
   # confidence)
 
@@ -1178,18 +1177,32 @@ sub store {
 
   $sth->bind_param( 12, $gene->canonical_annotation(), SQL_VARCHAR );
 
-  if ( defined($gene->stable_id) ) {
-
-     $sth->bind_param( 13, $gene->stable_id, SQL_VARCHAR );
-     my $version = ($gene->version()) ? $gene->version() : 1;
-     $sth->bind_param( 14, $version, SQL_INTEGER );
-  }
-
   $sth->execute();
   $sth->finish();
 
   my $gene_dbID = $sth->{'mysql_insertid'};
 
+  # store stable ids if they are available
+  if ( defined( $gene->stable_id() ) ) {
+    my $statement = sprintf(
+      "INSERT INTO gene_stable_id SET "
+        . "gene_id = ?, "
+        . "stable_id = ?, "
+        . "version = ?, "
+        . "created_date = %s, "
+        . "modified_date = %s",
+      $self->db()->dbc()->from_seconds_to_date( $gene->created_date() ),
+      $self->db()->dbc()->from_seconds_to_date( $gene->modified_date() )
+    );
+
+    $sth = $self->prepare($statement);
+    $sth->bind_param( 1, $gene_dbID,         SQL_INTEGER );
+    $sth->bind_param( 2, $gene->stable_id(), SQL_VARCHAR );
+    $sth->bind_param( 3, ($gene->version() || 1),   SQL_INTEGER );
+    $sth->execute();
+    $sth->finish();
+  }
+
   # store the dbentries associated with this gene
   my $dbEntryAdaptor = $db->get_DBEntryAdaptor();
 
@@ -1256,7 +1269,6 @@ sub store {
     $sth->bind_param( 2, $gene_dbID, SQL_INTEGER );
 
     $sth->execute();
-    $sth->finish();
   }
 
   # update gene to point to display xref if it is set
@@ -1269,7 +1281,7 @@ sub store {
     }
 
     if(defined($dxref_id)) {
-      my $sth = $self->prepare
+      $sth = $self->prepare
         ("UPDATE gene SET display_xref_id = ? WHERE gene_id = ?");
       $sth->bind_param(1, $dxref_id, SQL_INTEGER);
       $sth->bind_param(2, $gene_dbID, SQL_INTEGER);
@@ -1360,6 +1372,14 @@ sub remove {
     $transcriptAdaptor->remove($trans);
   }
 
+  # remove the gene stable identifier
+
+  $sth =
+    $self->prepare("DELETE FROM gene_stable_id WHERE gene_id = ? ");
+  $sth->bind_param( 1, $gene->dbID, SQL_INTEGER );
+  $sth->execute();
+  $sth->finish();
+
   # remove any unconventional transcript associations involving this gene
 
   $sth =
@@ -1412,9 +1432,9 @@ sub get_Interpro_by_geneid {
             protein_feature pf,
             interpro i,
             xref x,
-            gene g
-  WHERE     g.stable_id = ?
-    AND     t.gene_id = g.gene_id
+            gene_stable_id gsi
+  WHERE     gsi.stable_id = ?
+    AND     t.gene_id = gsi.gene_id
     AND     t.is_current = 1
     AND     tl.transcript_id = t.transcript_id
     AND     tl.translation_id = pf.translation_id
@@ -2281,7 +2301,7 @@ sub get_stable_entry_info {
 
   my $sth = $self->prepare("SELECT stable_id, " . $created_date . "," .
                                    $modified_date . ", version 
-                            FROM gene 
+                            FROM gene_stable_id 
                             WHERE gene_id = ?");
 
   $sth->bind_param(1, $gene->dbID, SQL_INTEGER);
@@ -2313,5 +2333,3 @@ sub fetch_all_by_DBEntry {
 
 
 1;
-
-
diff --git a/modules/Bio/EnsEMBL/DBSQL/OperonAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/OperonAdaptor.pm
index c2472e5087..1ab406b4d8 100644
--- a/modules/Bio/EnsEMBL/DBSQL/OperonAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/OperonAdaptor.pm
@@ -1,4 +1,3 @@
-
 =head1 LICENSE
 
   Copyright (c) 1999-2011 The European Bioinformatics Institute and
@@ -63,7 +62,7 @@ use vars '@ISA';
 #  Status     : Stable
 
 sub _tables {
-	return ( [ 'operon', 'o' ] );
+	return ( [ 'operon', 'o' ], [ 'operon_stable_id', 'osi' ] );
 }
 
 # _columns
@@ -80,16 +79,20 @@ sub _columns {
 	my ($self) = @_;
 
 	my $created_date =
-	  $self->db()->dbc()->from_date_to_seconds("o.created_date");
+	  $self->db()->dbc()->from_date_to_seconds("osi.created_date");
 	my $modified_date =
-	  $self->db()->dbc()->from_date_to_seconds("o.modified_date");
+	  $self->db()->dbc()->from_date_to_seconds("osi.modified_date");
 
 	return ( 'o.operon_id',      'o.seq_region_id',     'o.seq_region_start',
 			 'o.seq_region_end', 'o.seq_region_strand', 'o.display_label',
-			 'o.analysis_id',    'o.stable_id',       'o.version',
+			 'o.analysis_id',    'osi.stable_id',       'osi.version',
 			 $created_date,      $modified_date );
 }
 
+sub _left_join {
+	return ( [ 'operon_stable_id', "osi.operon_id = o.operon_id" ] );
+}
+
 =head2 list_dbIDs 
 
   Example    : @operon_ids = @{$operon_adaptor->list_dbIDs()};
@@ -122,7 +125,7 @@ sub list_dbIDs {
 sub list_stable_ids {
 	my ($self) = @_;
 
-	return $self->_list_dbIDs( "operon", "stable_id" );
+	return $self->_list_dbIDs( "operon_stable_id", "stable_id" );
 }
 
 sub list_seq_region_ids {
@@ -177,7 +180,7 @@ sub fetch_by_name {
 sub fetch_by_stable_id {
 	my ( $self, $stable_id ) = @_;
 
-	my $constraint = "o.stable_id = ?";
+	my $constraint = "osi.stable_id = ? AND o.is_current = 1";
 	$self->bind_param_generic_fetch( $stable_id, SQL_VARCHAR );
 	my ($operon) = @{ $self->generic_fetch($constraint) };
 
@@ -221,7 +224,7 @@ sub fetch_all {
 sub fetch_all_versions_by_stable_id {
 	my ( $self, $stable_id ) = @_;
 
-	my $constraint = "o.stable_id = ?";
+	my $constraint = "osi.stable_id = ?";
 	$self->bind_param_generic_fetch( $stable_id, SQL_VARCHAR );
 	return $self->generic_fetch($constraint);
 }
@@ -395,11 +398,11 @@ sub fetch_by_operon_transcript_id {
 	return $operon;
 }
 
-=head2 fetch_by_operon_transcript_stable_id
+=head2 fetch_by_transcript_stable_id
 
   Arg [1]    : string $trans_stable_id
                transcript stable ID whose operon should be retrieved
-  Example    : my $operon = $operon_adaptor->fetch_by_operon_transcript_stable_id
+  Example    : my $operon = $operon_adaptor->fetch_by_transcript_stable_id
                  ('ENST0000234');
   Description: Retrieves a operon from the database via the stable ID of one of
                its transcripts
@@ -415,9 +418,11 @@ sub fetch_by_operon_transcript_stable_id {
 
 	my $sth = $self->prepare(
 		qq(
-        SELECT  operon_id
-	FROM	operon_transcript
-        WHERE   stable_id = ?
+        SELECT  tr.operon_id
+	FROM	operon_transcript tr, operon_transcript_stable_id tcl
+        WHERE   tcl.stable_id = ?
+        AND     tr.operon_transcript_stable_id = tcl.operon_transcript_stable_id
+        AND     tr.is_current = 1
     ) );
 
 	$sth->bind_param( 1, $trans_stable_id, SQL_VARCHAR );
@@ -490,14 +495,7 @@ sub store {
                display_label = ?,
                analysis_id = ?
   );
-	
-	if ( defined($operon->stable_id()) ) {
-	    my $created = $self->db->dbc->from_seconds_to_date($operon->created_date());
-	    my $modified = $self->db->dbc->from_seconds_to_date($operon->modified_date());
-	    $store_operon_sql .= ", stable_id = ?, version = ?, created_date = " . $created . ",modified_date = " . $modified;
-	}
-
-        # column status is used from schema version 34 onwards (before it was
+	# column status is used from schema version 34 onwards (before it was
 	# confidence)
 
 	my $sth = $self->prepare($store_operon_sql);
@@ -508,12 +506,6 @@ sub store {
 	$sth->bind_param( 5, $operon->display_label(), SQL_VARCHAR );
 	$sth->bind_param( 6, $analysis_id,             SQL_INTEGER );
 
-	if ( defined($operon->stable_id()) ) {
-	    $sth->bind_param( 7, $operon->stable_id(), SQL_VARCHAR );
-	    my $version = ($operon->version()) ? $operon->version() : 1;
-	    $sth->bind_param( 8, $version, SQL_INTEGER ); 
-	}
-
 	$sth->execute();
 	$sth->finish();
 
@@ -528,6 +520,29 @@ sub store {
 		}
 	}
 
+	# store stable ids if they are available
+	if ( defined( $operon->stable_id() ) ) {
+		my $statement = sprintf( "INSERT INTO operon_stable_id SET "
+								   . "operon_id = ?, "
+								   . "stable_id = ?, "
+								   . "version = ?, "
+								   . "created_date = %s, "
+								   . "modified_date = %s",
+								 $self->db()->dbc()->from_seconds_to_date(
+														 $operon->created_date()
+								 ),
+								 $self->db()->dbc()->from_seconds_to_date(
+														$operon->modified_date()
+								 ) );
+
+		$sth = $self->prepare($statement);
+		$sth->bind_param( 1, $operon_dbID,         SQL_INTEGER );
+		$sth->bind_param( 2, $operon->stable_id(), SQL_VARCHAR );
+		$sth->bind_param( 3, $operon->version(),   SQL_INTEGER );
+		$sth->execute();
+		$sth->finish();
+	}
+
 	# store the dbentries associated with this operon
 	my $dbEntryAdaptor = $db->get_DBEntryAdaptor();
 
@@ -589,15 +604,27 @@ sub remove {
 		$dbe_adaptor->remove_from_object( $dbe, $operon, 'Operon' );
 	}
 
+	#	# remove the attributes associated with this transcript
+	#	my $attrib_adaptor = $self->db->get_AttributeAdaptor;
+	#	$attrib_adaptor->remove_from_Operon($operon);
+
 	# remove all of the transcripts associated with this operon
 	my $transcriptAdaptor = $self->db->get_OperonTranscriptAdaptor();
 	foreach my $trans ( @{ $operon->get_all_OperonTranscripts() } ) {
 		$transcriptAdaptor->remove($trans);
 	}
 
+	# remove the operon stable identifier
+
+	my $sth =
+	  $self->prepare("DELETE FROM operon_stable_id WHERE operon_id = ? ");
+	$sth->bind_param( 1, $operon->dbID, SQL_INTEGER );
+	$sth->execute();
+	$sth->finish();
+
 	# remove this operon from the database
 
-	my $sth = $self->prepare("DELETE FROM operon WHERE operon_id = ? ");
+	$sth = $self->prepare("DELETE FROM operon WHERE operon_id = ? ");
 	$sth->bind_param( 1, $operon->dbID, SQL_INTEGER );
 	$sth->execute();
 	$sth->finish();
@@ -779,4 +806,3 @@ sub _objs_from_sth {
 } ## end sub _objs_from_sth
 
 1;
-
diff --git a/modules/Bio/EnsEMBL/DBSQL/OperonTranscriptAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/OperonTranscriptAdaptor.pm
index 34c2184a8a..e6c2091de8 100644
--- a/modules/Bio/EnsEMBL/DBSQL/OperonTranscriptAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/OperonTranscriptAdaptor.pm
@@ -1,4 +1,3 @@
-
 =head1 LICENSE
 
   Copyright (c) 1999-2011 The European Bioinformatics Institute and
@@ -67,7 +66,8 @@ use vars '@ISA';
 #  Status     : Stable
 
 sub _tables {
-	return ( [ 'operon_transcript', 'o' ] );
+	return ( [ 'operon_transcript', 'o' ],
+			 [ 'operon_transcript_stable_id', 'osi' ] );
 }
 
 # _columns
@@ -84,18 +84,23 @@ sub _columns {
 	my ($self) = @_;
 
 	my $created_date =
-	  $self->db()->dbc()->from_date_to_seconds("o.created_date");
+	  $self->db()->dbc()->from_date_to_seconds("osi.created_date");
 	my $modified_date =
-	  $self->db()->dbc()->from_date_to_seconds("o.modified_date");
+	  $self->db()->dbc()->from_date_to_seconds("osi.modified_date");
 
 	return ( 'o.operon_transcript_id', 'o.seq_region_id',
 			 'o.seq_region_start',     'o.seq_region_end',
 			 'o.seq_region_strand',    'o.display_label',
-			 'o.analysis_id',          'o.stable_id',
-			 'o.version',            $created_date,
+			 'o.analysis_id',          'osi.stable_id',
+			 'osi.version',            $created_date,
 			 $modified_date );
 }
 
+sub _left_join {
+	return ( [ 'operon_transcript_stable_id',
+			   "osi.operon_transcript_id = o.operon_transcript_id" ] );
+}
+
 =head2 list_dbIDs
 
   Example    : @ot_ids = @{$ot_adaptor->list_dbIDs()};
@@ -111,7 +116,7 @@ sub _columns {
 sub list_dbIDs {
 	my ( $self, $ordered ) = @_;
 
-	return $self->_list_dbIDs( "operon_transcript", undef, $ordered );
+	return $self->_list_dbIDs( "operon", undef, $ordered );
 }
 
 =head2 list_stable_ids
@@ -128,7 +133,7 @@ sub list_dbIDs {
 sub list_stable_ids {
 	my ($self) = @_;
 
-	return $self->_list_dbIDs( "operon_transcript", "stable_id" );
+	return $self->_list_dbIDs( "operon_stable_id", "stable_id" );
 }
 
 sub list_seq_region_ids {
@@ -158,7 +163,7 @@ sub list_seq_region_ids {
 sub fetch_by_stable_id {
 	my ( $self, $stable_id ) = @_;
 
-	my $constraint = "o.stable_id = ?";
+	my $constraint = "osi.stable_id = ? AND o.is_current = 1";
 	$self->bind_param_generic_fetch( $stable_id, SQL_VARCHAR );
 	my ($operon_transcript) = @{ $self->generic_fetch($constraint) };
 
@@ -226,7 +231,7 @@ sub fetch_all {
 sub fetch_all_versions_by_stable_id {
 	my ( $self, $stable_id ) = @_;
 
-	my $constraint = "o.stable_id = ?";
+	my $constraint = "osi.stable_id = ?";
 	$self->bind_param_generic_fetch( $stable_id, SQL_VARCHAR );
 	return $self->generic_fetch($constraint);
 }
@@ -545,14 +550,6 @@ sub store {
                operon_id = ?,
                analysis_id =?
   );
-
-	if ( defined($operon_transcript->stable_id()) ) {
-	    my $created = $self->db->dbc->from_seconds_to_date($operon_transcript->created_date());
-	    my $modified = $self->db->dbc->from_seconds_to_date($operon_transcript->modified_date());
-	    $store_operon_transcript_sql .= ", stable_id = ?, version = ?, created_date = " . $created . ",modified_date = " . $modified;
-	}
-	
-
 	# column status is used from schema version 34 onwards (before it was
 	# confidence)
 
@@ -565,17 +562,34 @@ sub store {
 	$sth->bind_param( 6, $operon_id,                          SQL_INTEGER );
 	$sth->bind_param( 7, $analysis_id,                        SQL_INTEGER );
 
-	if ( defined($operon_transcript->stable_id()) ) {
-	    $sth->bind_param( 8, $operon_transcript->stable_id(), SQL_VARCHAR );
-	    my $version = ($operon_transcript->version()) ? $operon_transcript->version() : 1;
-	    $sth->bind_param( 9, $version, SQL_INTEGER ); 
-	}
-
 	$sth->execute();
 	$sth->finish();
 
 	my $operon_transcript_dbID = $sth->{'mysql_insertid'};
 
+	# store stable ids if they are available
+	if ( defined( $operon_transcript->stable_id() ) ) {
+		my $statement = sprintf( "INSERT INTO operon_transcript_stable_id SET "
+								   . "operon_transcript_id = ?, "
+								   . "stable_id = ?, "
+								   . "version = ?, "
+								   . "created_date = %s, "
+								   . "modified_date = %s",
+								 $self->db()->dbc()->from_seconds_to_date(
+											  $operon_transcript->created_date()
+								 ),
+								 $self->db()->dbc()->from_seconds_to_date(
+											 $operon_transcript->modified_date()
+								 ) );
+
+		$sth = $self->prepare($statement);
+		$sth->bind_param( 1, $operon_transcript_dbID,         SQL_INTEGER );
+		$sth->bind_param( 2, $operon_transcript->stable_id(), SQL_VARCHAR );
+		$sth->bind_param( 3, $operon_transcript->version(),   SQL_INTEGER );
+		$sth->execute();
+		$sth->finish();
+	}
+
 	# store the dbentries associated with this gene
 	my $dbEntryAdaptor = $db->get_DBEntryAdaptor();
 
@@ -676,8 +690,15 @@ sub remove {
 	#	my $attrib_adaptor = $self->db->get_AttributeAdaptor;
 	#	$attrib_adaptor->remove_from_OperonTranscript($operon_transcript);
 
-	# remove from the database
+	# remove the stable identifier
 	my $sth = $self->prepare(
+"DELETE FROM operon_transcript_stable_id WHERE operon_transcript_id = ? " );
+	$sth->bind_param( 1, $operon_transcript->dbID, SQL_INTEGER );
+	$sth->execute();
+	$sth->finish();
+
+	# remove from the database
+	$sth = $self->prepare(
 			   "DELETE FROM operon_transcript WHERE operon_transcript_id = ? ");
 	$sth->bind_param( 1, $operon_transcript->dbID, SQL_INTEGER );
 	$sth->execute();
@@ -861,4 +882,3 @@ sub _objs_from_sth {
 } ## end sub _objs_from_sth
 
 1;
-
diff --git a/modules/Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm
index bf2369ac02..0f6f9dcc02 100644
--- a/modules/Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm
@@ -85,6 +85,7 @@ use vars qw(@ISA);
 sub _tables {
   return (
     [ 'transcript',           't' ],
+    [ 'transcript_stable_id', 'tsi' ],
     [ 'xref',                 'x' ],
     [ 'external_db',          'exdb' ] );
 }
@@ -112,7 +113,7 @@ sub _columns {
     't.seq_region_start',  't.seq_region_end',
     't.seq_region_strand', 't.analysis_id',
     't.gene_id',           't.is_current',
-    't.stable_id',         't.version',
+    'tsi.stable_id',       'tsi.version',
     $created_date,         $modified_date,
     't.description',       't.biotype',
     't.status',            'exdb.db_name',
@@ -126,6 +127,7 @@ sub _columns {
 
 sub _left_join {
   return (
+    [ 'transcript_stable_id', "tsi.transcript_id = t.transcript_id" ],
     [ 'xref',                 "x.xref_id = t.display_xref_id" ],
     [ 'external_db',          "exdb.external_db_id = x.external_db_id" ]
   );
@@ -148,7 +150,7 @@ sub _left_join {
 sub fetch_by_stable_id {
   my ($self, $stable_id) = @_;
 
-  my $constraint = "t.stable_id = ? AND t.is_current = 1";
+  my $constraint = "tsi.stable_id = ? AND t.is_current = 1";
 
   $self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
 
@@ -184,7 +186,7 @@ sub fetch_all {
 sub fetch_all_versions_by_stable_id {
   my ($self, $stable_id) = @_;
 
-  my $constraint = "t.stable_id = ?";
+  my $constraint = "tsi.stable_id = ?";
 
   $self->bind_param_generic_fetch($stable_id,SQL_VARCHAR);
 
@@ -213,9 +215,11 @@ sub fetch_by_translation_stable_id {
 
   my $sth = $self->prepare(qq(
       SELECT t.transcript_id
-      FROM   translation tl,
-             transcript t
-      WHERE  tl.stable_id = ?
+      FROM   translation_stable_id tsi,
+             transcript t,
+             translation tl
+      WHERE  tsi.stable_id = ?
+      AND    tl.translation_id = tsi.translation_id
       AND    tl.transcript_id = t.transcript_id
       AND    t.is_current = 1
   ));
@@ -662,10 +666,10 @@ sub fetch_all_by_exon_stable_id {
 
   my $sth = $self->prepare(qq(
       SELECT t.transcript_id 
-      FROM exon_transcript et, exon e, transcript t
-      WHERE e.exon_id = et.exon_id
+      FROM exon_transcript et, exon_stable_id esi, transcript t
+      WHERE esi.exon_id = et.exon_id
       AND et.transcript_id = t.transcript_id
-      AND e.stable_id = ?
+      AND esi.stable_id = ?
       AND t.is_current = 1
   ));
   
@@ -777,30 +781,16 @@ sub store {
   #
   # Store transcript
   #
-  my $store_transcript_sql = qq(
-      INSERT INTO transcript 
-        SET gene_id = ?, 
-            analysis_id = ?, 
-            seq_region_id = ?, 
-            seq_region_start = ?,
-            seq_region_end = ?, 
-            seq_region_strand = ?, 
-            biotype = ?, 
-            status = ?, 
-            description = ?,
-            is_current = ?, 
-            canonical_translation_id = ? 
-  );
-
-  if ( defined( $transcript->stable_id() ) ) {
-
-      my $created = $self->db->dbc->from_seconds_to_date($transcript->created_date());
-      my $modified = $self->db->dbc->from_seconds_to_date($transcript->modified_date());
-      $store_transcript_sql .= ", stable_id = ?, version = ?, created_date = " . $created . " , modified_date = " . $modified;
+  my $tst = $self->prepare(
+    qq(
+      INSERT INTO transcript (
+        gene_id, analysis_id, seq_region_id, seq_region_start,
+        seq_region_end, seq_region_strand, biotype, status, description,
+        is_current, canonical_translation_id
+      )
+      VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+  ) );
 
-  }
-
-  my $tst = $self->prepare($store_transcript_sql);
   $tst->bind_param( 1,  $gene_dbID,                 SQL_INTEGER );
   $tst->bind_param( 2,  $new_analysis_id,           SQL_INTEGER );
   $tst->bind_param( 3,  $seq_region_id,             SQL_INTEGER );
@@ -815,14 +805,6 @@ sub store {
   # If the transcript has a translation, this is updated later:
   $tst->bind_param( 11, undef, SQL_INTEGER );
 
-  if ( defined( $transcript->stable_id() ) ) {
-
-    $tst->bind_param( 12, $transcript->stable_id(), SQL_VARCHAR );
-    my $version = ($transcript->version()) ? $transcript->version() : 1;
-    $tst->bind_param( 13, $version,                 SQL_INTEGER );
-  }
-
-
   $tst->execute();
   $tst->finish();
 
@@ -1006,6 +988,33 @@ sub store {
 
   $etst->finish();
 
+  #
+  # Store stable_id
+  #
+  if ( defined( $transcript->stable_id() ) ) {
+
+    my $statement = "INSERT INTO transcript_stable_id "
+      . "SET transcript_id = ?, stable_id = ?, version = ?, ";
+
+    $statement .=
+      "created_date = "
+      . $self->db()->dbc()
+      ->from_seconds_to_date( $transcript->created_date() ) . ",";
+
+    $statement .=
+      "modified_date = "
+      . $self->db()->dbc()
+      ->from_seconds_to_date( $transcript->modified_date() );
+
+    my $sth = $self->prepare($statement);
+    my $version = ($transcript->version()) ? $transcript->version() : 1;
+    $sth->bind_param( 1, $transc_dbID,             SQL_INTEGER );
+    $sth->bind_param( 2, $transcript->stable_id(), SQL_VARCHAR );
+    $sth->bind_param( 3, $version,                 SQL_INTEGER );
+    $sth->execute();
+    $sth->finish();
+  } ## end if ( defined( $transcript...))
+
   # Now the supporting evidence
   my $tsf_adaptor = $db->get_TranscriptSupportingFeatureAdaptor();
   $tsf_adaptor->store( $transc_dbID,
@@ -1046,16 +1055,18 @@ sub get_Interpro_by_transid {
 
    my $sth = $self->prepare(qq(
       SELECT  STRAIGHT_JOIN i.interpro_ac, x.description
-      FROM    transcript t,
+      FROM    transcript_stable_id tsi,
+              transcript t,
               translation tl,
               protein_feature pf,
 	      interpro i,
               xref x
-      WHERE   t.stable_id = ?
-      AND     tl.transcript_id = t.transcript_id
+      WHERE   tsi.stable_id = ?
+      AND     tl.transcript_id = tsi.transcript_id
       AND     tl.translation_id = pf.translation_id
       AND     i.id = pf.hit_name
       AND     i.interpro_ac = x.dbprimary_acc
+      AND     tsi.transcript_id = t.transcript_id
       AND     t.is_current = 1
   ));
 
@@ -1220,6 +1231,10 @@ sub remove {
                              WHERE transcript_id = ?" );
   $sth->bind_param(1, $transcript->dbID, SQL_INTEGER);
   $sth->execute();
+  $sth = $self->prepare( "DELETE FROM transcript_stable_id
+                          WHERE transcript_id = ?" );
+  $sth->bind_param(1, $transcript->dbID, SQL_INTEGER);
+  $sth->execute();
   $sth->finish();
 
 
@@ -1336,7 +1351,7 @@ sub list_dbIDs {
 sub list_stable_ids {
    my ($self) = @_;
 
-   return $self->_list_dbIDs("transcript", "stable_id");
+   return $self->_list_dbIDs("transcript_stable_id", "stable_id");
 }
 
 
@@ -1816,7 +1831,7 @@ sub get_stable_entry_info {
 
   my $sth = $self->prepare(qq(
       SELECT stable_id, version 
-      FROM   transcript
+      FROM   transcript_stable_id 
       WHERE  transcript_id = ?
   ));
                             
diff --git a/modules/Bio/EnsEMBL/UnmappedObject.pm b/modules/Bio/EnsEMBL/UnmappedObject.pm
index bc4ed1577a..827fac51dc 100644
--- a/modules/Bio/EnsEMBL/UnmappedObject.pm
+++ b/modules/Bio/EnsEMBL/UnmappedObject.pm
@@ -328,7 +328,7 @@ sub stable_id{
   
   my $handle = $self->adaptor;
   if(defined($handle)){
-    my $sql = "select stable_id from ".lc($self->{'ensembl_object_type'})." where ".
+    my $sql = "select stable_id from ".lc($self->{'ensembl_object_type'})."_stable_id where ".
       lc($self->{'ensembl_object_type'})."_id = ".
 	$self->{'ensembl_id'};
     my $sth = $handle->prepare($sql);
-- 
GitLab