translation.t 9.83 KB
Newer Older
Magali Ruffier's avatar
Magali Ruffier committed
1
# Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Tiago Grego's avatar
Tiago Grego committed
2
# Copyright [2016-2019] EMBL-European Bioinformatics Institute
3 4 5 6 7 8 9 10 11 12 13 14 15
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#      http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

16 17 18
use strict;
use warnings;

19
use Bio::EnsEMBL::Test::TestUtils;
Alistair Rust's avatar
Alistair Rust committed
20 21
use Bio::EnsEMBL::Translation;
use Bio::EnsEMBL::Exon;
22
use Bio::EnsEMBL::StopCodonReadthroughEdit;
Alistair Rust's avatar
Alistair Rust committed
23

24
use Test::More;
25
use Test::Warnings;
Alistair Rust's avatar
Alistair Rust committed
26 27

my $loaded = 0;
28
END { print "not ok 1\n" unless $loaded; }
Alistair Rust's avatar
Alistair Rust committed
29

30 31
#turn on/off debug prints:
our $verbose = 0;
Alistair Rust's avatar
Alistair Rust committed
32

33
use Bio::EnsEMBL::Test::MultiTestDB;
Alistair Rust's avatar
Alistair Rust committed
34

35
my $multi = Bio::EnsEMBL::Test::MultiTestDB->new();
Alistair Rust's avatar
Alistair Rust committed
36 37 38 39 40

$loaded = 1;

ok(1);

41
my $db = $multi->get_DBAdaptor('core');
Alistair Rust's avatar
Alistair Rust committed
42 43 44 45 46

my $t = Bio::EnsEMBL::Translation->new();

ok($t);

47 48 49
ok(test_getter_setter($t, "stable_id",     1));
ok(test_getter_setter($t, "created_date",  time()));
ok(test_getter_setter($t, "modified_date", time()));
50

51
ok(test_getter_setter($t, 'dbID', 3));
Alistair Rust's avatar
Alistair Rust committed
52

53 54
ok(test_getter_setter($t, 'start', 42));
ok(test_getter_setter($t, 'end',   50));
Alistair Rust's avatar
Alistair Rust committed
55

56 57
is($t->version, 1, 'Default translation version = 1');

Alistair Rust's avatar
Alistair Rust committed
58 59 60 61 62
my $exon = Bio::EnsEMBL::Exon->new();
$exon->start(10);
$exon->end(20);
$exon->strand(1);
$exon->phase(0);
63
$exon->end_phase(-1);
Alistair Rust's avatar
Alistair Rust committed
64 65 66 67 68 69

$t->start_Exon($exon);
ok($t);

$t->end_Exon($exon);
ok($t);
70

71 72 73 74
#
# Tests for the translation adaptor
##################################

75
my $ta  = $db->get_TranslationAdaptor();
76
my $ids = $ta->list_dbIDs();
77
is(@{$ids}, 26, 'We have 26 translation IDs');
78

79
my $stable_ids = $ta->list_stable_ids();
80
is(@{$stable_ids}, 26, 'We have 26 stable IDs');
81 82 83 84 85 86 87 88 89 90 91

my $tra = $db->get_TranscriptAdaptor();

my $transcript = $tra->fetch_by_stable_id('ENST00000201961');

#
# test fetch_by_Transcript
#
my $translation = $ta->fetch_by_Transcript($transcript);

ok($translation && $translation->stable_id eq 'ENSP00000201961');
92

93
my @date_time = gmtime($translation->created_date());
94
ok($date_time[3] == 6 && $date_time[4] == 11 && $date_time[5] == 104);
95

96
@date_time = gmtime($translation->modified_date());
97
ok($date_time[3] == 6 && $date_time[4] == 11 && $date_time[5] == 104);
98

99
ok($translation && $translation->start_Exon->stable_id eq 'ENSE00000661216');
100
ok($translation && $translation->end_Exon->stable_id   eq 'ENSE00000661212');
101 102 103 104 105 106 107 108 109 110 111 112 113

#
# test fetch_by_dbID
#
$translation = $ta->fetch_by_dbID(21734);
ok($translation && $translation->stable_id() eq 'ENSP00000201961');

#
# test fetch_by_stable_id
#
$translation = $ta->fetch_by_stable_id('ENSP00000201961');
ok($translation && $translation->dbID() == 21734);

Matthew Laird's avatar
Matthew Laird committed
114 115 116 117 118 119 120 121 122 123
$translation->stable_id_version('ENSP00000201962.4');
is($translation->stable_id, 'ENSP00000201962', 'Stable id set with stable_id_version');
is($translation->version, 4, 'Version set with stable_id_version');
is($translation->stable_id_version, 'ENSP00000201962.4', 'Stable id and version from stable_id_version');

$translation->stable_id_version('ENSP00000201963');
is($translation->stable_id, 'ENSP00000201963', 'Stable id set with stable_id_version');
is($translation->version, undef, 'Version undef from stable_id_version');
is($translation->stable_id_version, 'ENSP00000201963', 'Stable id and no version from stable_id_version');

Matthew Laird's avatar
Matthew Laird committed
124 125 126 127 128 129
$translation = $ta->fetch_by_stable_id('ENSP00000201961.1');
ok($translation && $translation->dbID() == 21734, 'fetch_by_stable_id with version');

$translation = $ta->fetch_by_stable_id('ENSP00000201961.1a');
ok(!defined($translation), 'fetch_by_stable_id with bad version');

Matthew Laird's avatar
Matthew Laird committed
130 131 132 133 134 135
$translation = $ta->fetch_by_stable_id_version('ENSP00000201961', 1);
ok($translation && $translation->dbID() == 21734, 'fetch_by_stable_id_version');

$translation = $ta->fetch_by_stable_id_version('ENSP00000201961', '1a');
ok(!defined($translation), 'fetch_by_stable_id_version with bad version');

136 137 138 139 140 141 142 143 144 145 146
#
# test fetch_by_external_name
#
($translation) = @{$ta->fetch_all_by_external_name('CAC33959')};
ok($translation && $translation->dbID() == 21716);

#
# test get_all_ProteinFeatures
#

my @protein_features = @{$translation->get_all_ProteinFeatures()};
147
debug("Got " . scalar(@protein_features) . " protein features.");
148 149 150 151 152 153
ok(@protein_features == 3);

#
# test get_all_DomainFeatures
#
my @domain_features = @{$translation->get_all_DomainFeatures()};
154
is(@domain_features, 3, "Got 3 domain features");
155

156 157
ok($translation->display_id eq $translation->stable_id);

158 159 160 161 162 163
#
# test that when manually attaching ProteinFeatures they are not loaded from
# the db
#
$translation->{'protein_features'} = undef;

164
my $pfa             = $translation->adaptor->db->get_ProteinFeatureAdaptor;
165 166
my $protein_feature = $pfa->fetch_by_dbID(27374);
$translation->add_ProteinFeature($protein_feature);
167
ok(@{$translation->get_all_ProteinFeatures} == 1);
168 169 170

# reset ProteinFeature cache
$translation->{'protein_features'} = undef;
171 172 173 174 175 176 177 178 179 180 181

#
# test length() and seq()
#
my $seq = $translation->seq();
debug("Seq = $seq");
ok($seq);

debug("Lenth = " . $translation->length());
ok(length($seq) == $translation->length());

182 183 184 185
#
# test remove method
#

186
$multi->save('core', 'translation', 'protein_feature', 'object_xref', 'identity_xref', 'ontology_xref', 'meta_coord');
187

188
my $tl_count    = count_rows($db, 'translation');
189 190 191 192 193 194 195 196 197 198 199 200
my $pfeat_count = count_rows($db, 'protein_feature');

my $pfeat_minus = @{$translation->get_all_ProteinFeatures()};

$ta->remove($translation);

ok(!defined($translation->dbID));
ok(!defined($translation->adaptor()));

ok(count_rows($db, 'translation') == $tl_count - 1);
ok(count_rows($db, 'protein_feature') == $pfeat_count - $pfeat_minus);

201 202 203 204
#
# Attribute handling for selenocystein
#

205
my $tr = $tra->fetch_by_stable_id("ENST00000217347");
206

207 208
$tr->edits_enabled(1);

209 210 211 212 213 214 215
my $sc = Bio::EnsEMBL::SeqEdit->new(
  -START   => 2,
  -END     => 2,
  -ALT_SEQ => 'U',
  -CODE    => '_selenocysteine',
  -NAME    => 'Selenocysteine'
);
216

217
$tr->translation->add_Attributes($sc->get_Attribute());
218 219 220 221

$sc->start(3);
$sc->end(3);

222
$tr->translation->add_Attributes($sc->get_Attribute());
223 224 225 226

$sc->start(4);
$sc->end(4);

227
$tr->translation->add_Attributes($sc->get_Attribute());
228 229 230

my $tlseq = $tr->translate->seq();

231 232
debug("UUU inserted: " . $tlseq);
ok($tlseq =~ /^.UUU/);
233 234 235 236 237

#
# store and retrieve by lazy load
#

238
$multi->hide("core", "translation_attrib");
239

240
my $tl          = $tr->translation();
241 242
my $attrAdaptor = $db->get_AttributeAdaptor();

243
$attrAdaptor->store_on_Translation($tl->dbID, $tl->get_all_Attributes);
244

245
$tr = $tra->fetch_by_stable_id("ENST00000217347");
246

247 248
$tr->edits_enabled(1);

249
$tlseq = $tr->translate->seq();
250
ok($tlseq =~ /^.UUU/);
251 252

$multi->restore();
253 254 255 256 257

#
# Check if this was not caching artefact
#  No selenos should occur here
#
258
$tr = $tra->fetch_by_stable_id("ENST00000217347");
259 260

$tlseq = $tr->translate->seq();
261
ok($tlseq !~ /^.UUU/);
262 263 264 265

# test the fetch_all_by_Transcript_list method
my $tr2 = $tra->fetch_by_stable_id('ENST00000252021');

266
my @tls = @{$ta->fetch_all_by_Transcript_list([$tr, $tr2])};
267 268

ok(@tls == 2);
269 270 271 272

# test that translation attribs are stored when translation is stored
# check that attributes are stored when transcript is stored

273
$tr = $tra->fetch_by_stable_id("ENST00000217347");
274 275 276 277 278 279 280 281

$tl = $tr->translation();

# unstore the translation so it can be stored again

$tl->adaptor(undef);
$tl->dbID(undef);

282
$multi->hide('core', 'transcript', 'translation_attrib', 'translation', 'meta_coord');
283 284 285

# add a couple of attributes to the translation

286 287 288 289 290 291 292
$sc = Bio::EnsEMBL::SeqEdit->new(
  -START   => 2,
  -END     => 2,
  -ALT_SEQ => 'U',
  -CODE    => '_selenocysteine',
  -NAME    => 'Selenocysteine'
);
293

294
$tl->add_Attributes($sc->get_Attribute());
295 296 297 298

$sc->start(3);
$sc->end(3);

299
$tl->add_Attributes($sc->get_Attribute());
300 301 302 303 304 305 306

$ta->store($tl, $tr->dbID());

ok(count_rows($db, 'translation_attrib') == 2);

$multi->restore('core');

307 308
# TEST 37-40: Tests for cdna_start(), cdna_end(), genomic_start(), and
# genomic_end().
309

310 311 312
$tr = $tra->fetch_by_stable_id('ENST00000246229');
$tl = $tr->translation();

313 314
ok($tl->cdna_start() == 203);
ok($tl->cdna_end() == 1690);
315

316 317
ok($tl->genomic_start() == 30572315);
ok($tl->genomic_end() == 30578038);
Monika Komorowska's avatar
Monika Komorowska committed
318

319 320 321 322 323 324 325 326
SKIP: {
  skip 'No registry support for SQLite yet', 1 if $db->dbc->driver() eq 'SQLite';

  #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');
}
Monika Komorowska's avatar
Monika Komorowska committed
327 328 329

#41

330 331 332 333 334 335 336 337 338 339 340 341 342
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);
}
Monika Komorowska's avatar
Monika Komorowska committed
343

344 345 346
# Test generic_count(), inherited method from BaseAdaptor
is($ta->generic_count(), @{$ta->list_dbIDs()}, "Number of features from generic_count is equal to the number of dbIDs from list_dbIDs");

347 348 349 350
#56-57
#
# Handling stop codon readthrough edit
#
351 352 353 354 355

my $tra_scrt = $db->get_TranscriptAdaptor();
my $tr_scrt = $tra_scrt->fetch_by_stable_id("ENST00000217347");
$tr_scrt->edits_enabled(1);

356
diag 'Before X insertion: ', explain($tr_scrt->translate->seq());
357

358
my $scrt = Bio::EnsEMBL::StopCodonReadthroughEdit->new(265);
359 360
$tr_scrt->translation->add_Attributes($scrt->get_Attribute());
my $tlseq_scrt = $tr_scrt->translate->seq();
361 362 363 364

diag 'After X insertion: ', explain($tlseq_scrt);
is($tlseq_scrt =~ /QEEXEE/, 1, 'X inserted');
is(length($tr_scrt->translate->seq()), length($tlseq_scrt), 'Length of the sequence pre and post edit is equal');
365

366
done_testing();