From 96880e9f3097f7e35df6074adf02be08fdd2573c Mon Sep 17 00:00:00 2001
From: Andrew Yates <ayates@ebi.ac.uk>
Date: Fri, 2 Nov 2012 17:25:14 +0000
Subject: [PATCH] [ENSCORESW-302]. TranslationAdaptor did not deal with
 multispecies databases properly when querying for IDs.

---
 .../Bio/EnsEMBL/DBSQL/TranslationAdaptor.pm   |  33 ++++
 modules/t/translation.t                       | 155 +++++++++---------
 2 files changed, 106 insertions(+), 82 deletions(-)

diff --git a/modules/Bio/EnsEMBL/DBSQL/TranslationAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/TranslationAdaptor.pm
index 61c4dcecdc..93003aefba 100644
--- a/modules/Bio/EnsEMBL/DBSQL/TranslationAdaptor.pm
+++ b/modules/Bio/EnsEMBL/DBSQL/TranslationAdaptor.pm
@@ -624,6 +624,39 @@ sub list_stable_ids {
    return $self->_list_dbIDs("translation", "stable_id");
 }
 
+=head2 _list_dbIDs
+
+  Arg[1]      : String $table
+  Arg[2]      : String $column
+  Example     : $transl_adaptor->_list_dbIDs('translation','translation_id');
+  Description : Local reimplementation to ensure multi-species translations
+                are limited to their species alone
+  Returntype  : ArrayRef of specified IDs
+  Caller      : Internal
+  Status      : Unstable
+=cut
+
+sub _list_dbIDs {
+  my ($self, $table, $column) = @_;
+  my $ids;
+  if($self->is_multispecies()) {
+    $column ||= "${table}_id";
+    my $sql = <<SQL;
+select `tr`.`${column}` 
+from translation tr
+join transcript t using (transcript_id)
+join seq_region sr using (seq_region_id)
+join coord_system cs using (coord_system_id)
+where cs.species_id =?
+SQL
+    return $self->dbc()->sql_helper()->execute_simple(-SQL => $sql, -PARAMS => [$self->species_id()]);
+  }
+  else {
+    $ids = $self->SUPER::_list_dbIDs($table, $column);
+  }
+  return $ids;
+}
+
 
 
 =head2 fetch_by_dbID
diff --git a/modules/t/translation.t b/modules/t/translation.t
index 06b66581ad..0d7ce5cfdb 100644
--- a/modules/t/translation.t
+++ b/modules/t/translation.t
@@ -5,14 +5,10 @@ use Bio::EnsEMBL::Test::TestUtils;
 use Bio::EnsEMBL::Translation;
 use Bio::EnsEMBL::Exon;
 
-BEGIN {
-    $| = 1;
-    use Test;
-    plan tests => 41;
-}
+use Test::More;
 
 my $loaded = 0;
-END {print "not ok 1\n" unless $loaded;}
+END { print "not ok 1\n" unless $loaded; }
 
 #turn on/off debug prints:
 our $verbose = 0;
@@ -25,27 +21,27 @@ $loaded = 1;
 
 ok(1);
 
-my $db = $multi->get_DBAdaptor( 'core' );
+my $db = $multi->get_DBAdaptor('core');
 
 my $t = Bio::EnsEMBL::Translation->new();
 
 ok($t);
 
-ok( test_getter_setter( $t, "stable_id", 1 ));
-ok( test_getter_setter( $t, "created_date", time() ));
-ok( test_getter_setter( $t, "modified_date", time() ));
+ok(test_getter_setter($t, "stable_id",     1));
+ok(test_getter_setter($t, "created_date",  time()));
+ok(test_getter_setter($t, "modified_date", time()));
 
-ok(test_getter_setter($t,'dbID',3));
+ok(test_getter_setter($t, 'dbID', 3));
 
-ok(test_getter_setter($t,'start',42));
-ok(test_getter_setter($t,'end',50));
+ok(test_getter_setter($t, 'start', 42));
+ok(test_getter_setter($t, 'end',   50));
 
 my $exon = Bio::EnsEMBL::Exon->new();
 $exon->start(10);
 $exon->end(20);
 $exon->strand(1);
 $exon->phase(0);
-$exon->end_phase( -1 );
+$exon->end_phase(-1);
 
 $t->start_Exon($exon);
 ok($t);
@@ -53,18 +49,16 @@ ok($t);
 $t->end_Exon($exon);
 ok($t);
 
-
 #
 # Tests for the translation adaptor
 ##################################
 
-my $ta = $db->get_TranslationAdaptor();
+my $ta  = $db->get_TranslationAdaptor();
 my $ids = $ta->list_dbIDs();
-ok (@{$ids});
+is(@{$ids}, 25, 'We have 25 translation IDs');
 
 my $stable_ids = $ta->list_stable_ids();
-ok (@{$stable_ids});
-
+is(@{$stable_ids}, 25, 'We have 25 stable IDs');
 
 my $tra = $db->get_TranscriptAdaptor();
 
@@ -77,15 +71,14 @@ my $translation = $ta->fetch_by_Transcript($transcript);
 
 ok($translation && $translation->stable_id eq 'ENSP00000201961');
 
-my @date_time = localtime( $translation->created_date());
-ok( $date_time[3] == 6 && $date_time[4] == 11 && $date_time[5] == 104 );
+my @date_time = localtime($translation->created_date());
+ok($date_time[3] == 6 && $date_time[4] == 11 && $date_time[5] == 104);
 
-@date_time = localtime( $translation->modified_date());
-ok( $date_time[3] == 6 && $date_time[4] == 11 && $date_time[5] == 104 );
+@date_time = localtime($translation->modified_date());
+ok($date_time[3] == 6 && $date_time[4] == 11 && $date_time[5] == 104);
 
 ok($translation && $translation->start_Exon->stable_id eq 'ENSE00000661216');
-ok($translation && $translation->end_Exon->stable_id eq 'ENSE00000661212');
-
+ok($translation && $translation->end_Exon->stable_id   eq 'ENSE00000661212');
 
 #
 # test fetch_by_dbID
@@ -93,7 +86,6 @@ ok($translation && $translation->end_Exon->stable_id eq 'ENSE00000661212');
 $translation = $ta->fetch_by_dbID(21734);
 ok($translation && $translation->stable_id() eq 'ENSP00000201961');
 
-
 #
 # test fetch_by_stable_id
 #
@@ -111,10 +103,9 @@ ok($translation && $translation->dbID() == 21716);
 #
 
 my @protein_features = @{$translation->get_all_ProteinFeatures()};
-debug("Got " . scalar(@protein_features) ." protein features.");
+debug("Got " . scalar(@protein_features) . " protein features.");
 ok(@protein_features == 3);
 
-
 #
 # test get_all_DomainFeatures
 #
@@ -130,15 +121,14 @@ ok($translation->display_id eq $translation->stable_id);
 #
 $translation->{'protein_features'} = undef;
 
-my $pfa = $translation->adaptor->db->get_ProteinFeatureAdaptor;
+my $pfa             = $translation->adaptor->db->get_ProteinFeatureAdaptor;
 my $protein_feature = $pfa->fetch_by_dbID(27374);
 $translation->add_ProteinFeature($protein_feature);
-ok(@{ $translation->get_all_ProteinFeatures } == 1);
+ok(@{$translation->get_all_ProteinFeatures} == 1);
 
 # reset ProteinFeature cache
 $translation->{'protein_features'} = undef;
 
-
 #
 # test length() and seq()
 #
@@ -149,19 +139,15 @@ ok($seq);
 debug("Lenth = " . $translation->length());
 ok(length($seq) == $translation->length());
 
-
 #
 # test remove method
 #
 
-$multi->save('core', 'translation',
-             'protein_feature', 'object_xref', 'identity_xref',
-             'ontology_xref');
+$multi->save('core', 'translation', 'protein_feature', 'object_xref', 'identity_xref', 'ontology_xref');
 
-my $tl_count = count_rows($db, 'translation');
+my $tl_count    = count_rows($db, 'translation');
 my $pfeat_count = count_rows($db, 'protein_feature');
 
-
 my $pfeat_minus = @{$translation->get_all_ProteinFeatures()};
 
 $ta->remove($translation);
@@ -176,78 +162,75 @@ ok(count_rows($db, 'protein_feature') == $pfeat_count - $pfeat_minus);
 # Attribute handling for selenocystein
 #
 
-my $tr = $tra->fetch_by_stable_id( "ENST00000217347" );
+my $tr = $tra->fetch_by_stable_id("ENST00000217347");
 
 $tr->edits_enabled(1);
 
-my $sc = Bio::EnsEMBL::SeqEdit->new(-START   => 2,
-                                    -END     => 2,
-                                    -ALT_SEQ => 'U',
-                                    -CODE    => '_selenocysteine',
-                                    -NAME    => 'Selenocysteine');
+my $sc = Bio::EnsEMBL::SeqEdit->new(
+  -START   => 2,
+  -END     => 2,
+  -ALT_SEQ => 'U',
+  -CODE    => '_selenocysteine',
+  -NAME    => 'Selenocysteine'
+);
 
-$tr->translation->add_Attributes( $sc->get_Attribute() );
+$tr->translation->add_Attributes($sc->get_Attribute());
 
 $sc->start(3);
 $sc->end(3);
 
-$tr->translation->add_Attributes( $sc->get_Attribute() );
+$tr->translation->add_Attributes($sc->get_Attribute());
 
 $sc->start(4);
 $sc->end(4);
 
-$tr->translation->add_Attributes( $sc->get_Attribute() );
+$tr->translation->add_Attributes($sc->get_Attribute());
 
 my $tlseq = $tr->translate->seq();
 
-debug( "UUU inserted: ".$tlseq );
-ok( $tlseq =~ /^.UUU/ );
+debug("UUU inserted: " . $tlseq);
+ok($tlseq =~ /^.UUU/);
 
 #
 # store and retrieve by lazy load
 #
 
-$multi->hide( "core", "translation_attrib" );
+$multi->hide("core", "translation_attrib");
 
-my $tl = $tr->translation();
+my $tl          = $tr->translation();
 my $attrAdaptor = $db->get_AttributeAdaptor();
 
 $attrAdaptor->store_on_Translation($tl->dbID, $tl->get_all_Attributes);
 
-$tr = $tra->fetch_by_stable_id( "ENST00000217347" );
+$tr = $tra->fetch_by_stable_id("ENST00000217347");
 
 $tr->edits_enabled(1);
 
 $tlseq = $tr->translate->seq();
-ok( $tlseq =~ /^.UUU/ );
+ok($tlseq =~ /^.UUU/);
 
 $multi->restore();
 
-
 #
 # Check if this was not caching artefact
 #  No selenos should occur here
 #
-$tr = $tra->fetch_by_stable_id( "ENST00000217347" );
+$tr = $tra->fetch_by_stable_id("ENST00000217347");
 
 $tlseq = $tr->translate->seq();
-ok( $tlseq !~ /^.UUU/ );
-
-
+ok($tlseq !~ /^.UUU/);
 
 # test the fetch_all_by_Transcript_list method
 my $tr2 = $tra->fetch_by_stable_id('ENST00000252021');
 
-my @tls = @{$ta->fetch_all_by_Transcript_list([$tr,$tr2])};
+my @tls = @{$ta->fetch_all_by_Transcript_list([$tr, $tr2])};
 
 ok(@tls == 2);
 
-
-
 # test that translation attribs are stored when translation is stored
 # check that attributes are stored when transcript is stored
 
-$tr = $tra->fetch_by_stable_id( "ENST00000217347" );
+$tr = $tra->fetch_by_stable_id("ENST00000217347");
 
 $tl = $tr->translation();
 
@@ -256,24 +239,24 @@ $tl = $tr->translation();
 $tl->adaptor(undef);
 $tl->dbID(undef);
 
-
 $multi->hide('core', 'transcript', 'translation_attrib', 'translation');
 
-
 # add a couple of attributes to the translation
 
-$sc = Bio::EnsEMBL::SeqEdit->new(-START   => 2,
-                                 -END     => 2,
-                                 -ALT_SEQ => 'U',
-                                 -CODE    => '_selenocysteine',
-                                 -NAME    => 'Selenocysteine');
+$sc = Bio::EnsEMBL::SeqEdit->new(
+  -START   => 2,
+  -END     => 2,
+  -ALT_SEQ => 'U',
+  -CODE    => '_selenocysteine',
+  -NAME    => 'Selenocysteine'
+);
 
-$tl->add_Attributes( $sc->get_Attribute() );
+$tl->add_Attributes($sc->get_Attribute());
 
 $sc->start(3);
 $sc->end(3);
 
-$tl->add_Attributes( $sc->get_Attribute() );
+$tl->add_Attributes($sc->get_Attribute());
 
 $ta->store($tl, $tr->dbID());
 
@@ -287,23 +270,31 @@ $multi->restore('core');
 $tr = $tra->fetch_by_stable_id('ENST00000246229');
 $tl = $tr->translation();
 
-ok( $tl->cdna_start() == 203 );
-ok( $tl->cdna_end() == 1690 );
+ok($tl->cdna_start() == 203);
+ok($tl->cdna_end() == 1690);
 
-ok( $tl->genomic_start() == 30572315 );
-ok( $tl->genomic_end() == 30578038 );
+ok($tl->genomic_start() == 30572315);
+ok($tl->genomic_end() == 30578038);
 
 #test the get_species_and_object_type method from the Registry
 my $registry = 'Bio::EnsEMBL::Registry';
-my ( $species, $object_type, $db_type ) = $registry->get_species_and_object_type('ENSP00000201961');
-ok( $species eq 'homo_sapiens' && $object_type eq 'Translation');
-
+my ($species, $object_type, $db_type) = $registry->get_species_and_object_type('ENSP00000201961');
+ok($species eq 'homo_sapiens' && $object_type eq 'Translation');
 
 #41
 
-my @alt_tls = @{
-      $ta->fetch_all_alternative_by_Transcript(
-                                                            $tr)
-      };
+my @alt_tls = @{$ta->fetch_all_alternative_by_Transcript($tr)};
+
+ok(!scalar(@alt_tls));
+
+# Test querying with a multispecies DBA
+{
+  $ta->is_multispecies(1);
+  $ta->species_id(2);
+  is(@{$ta->list_stable_ids()}, 0, 'Now a multi-species DBA of ID 2 so no translation stable IDs available');
+  is(@{$ta->list_dbIDs()}, 0, 'Now a multi-species DBA of ID 2 so no translation IDs available');
+  $ta->is_multispecies(0);
+  $ta->species_id(1);
+}
 
-ok(!scalar(@alt_tls))
+done_testing();
-- 
GitLab