EnsEMBL/DBSQL/TranslationAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 304 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 492 CC 47 SCAR 39.3
CC
47
Cov
0%
fetch_all_by_Transcript_list
156.0
store
90.0
fetch_all_alternative_by_Transcript
30.0
CRAP 50.8
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::DBSQL::TranslationAdaptor - Provides a means to fetch and store
34 Translation objects from a database.
35  
36 =head1 DESCRIPTION
37  
38 This adaptor provides a means to retrieve and store
39 Bio::EnsEMBL::Translation objects from/in a database.
40  
41 Translation objects only truly make sense in the context of their
42 transcripts so the recommended means to retrieve Translations is
43 by retrieving the Transcript object first, and then fetching the
44 Translation.
45  
46 =head1 SYNOPSIS
47  
48 use Bio::EnsEMBL::Registry;
49  
50 Bio::EnsEMBL::Registry->load_registry_from_db(
51 -host => 'ensembldb.ensembl.org',
52 -user => 'anonymous'
53 );
54  
55 $transcript_adaptor =
56 Bio::EnsEMBL::Registry->get_adaptor( "human", "core",
57 "transcript" );
58  
59 $translation_adaptor =
60 Bio::EnsEMBL::Registry->get_adaptor( "human", "core",
61 "translation" );
62  
63 my $transcript = $transcript_adaptor->fetch_by_dbID(131243);
64 my $translation =
65 $translation_adaptor->fetch_by_Transcript($transcript);
66  
67 print("Translation Start Site: "
68 . $translation->start_Exon()->stable_id() . " "
69 . $translation->start()
70 . "\n" );
71 print("Translation Stop: "
72 . $translation->end_Exon()->stable_id() . " "
73 . $translation->end() );
74  
75 =head1 METHODS
76  
77 =cut
78  
79 package Bio::EnsEMBL::DBSQL::TranslationAdaptor;
80 $Bio::EnsEMBL::DBSQL::TranslationAdaptor::VERSION = '114.0.0';
81 use vars qw(@ISA);
82 use strict;
83  
84 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
85 use Bio::EnsEMBL::Translation;
86 use Bio::EnsEMBL::Utils::Exception qw( throw warning );
87 use Bio::EnsEMBL::Utils::Scalar qw( assert_ref );
88  
89  
90 @ISA = qw( Bio::EnsEMBL::DBSQL::BaseAdaptor );
91  
92 =head2 fetch_all_alternative_by_Transcript
93  
94 Arg [1] : Bio::EnsEMBL::Transcript $transcript
95 Example :
96  
97 @tl = @{
98 $translation_adaptor->fetch_all_alternative_by_Transcript(
99 $transcript)
100 };
101  
102 Description: Retrieves all alternative translations associated with a
103 particular transcript. If no alternative translation is
104 found, a reference to an empty list is returned.
105  
106 Returntype : listref of Bio::EnsEMBL::Translation
107 Exceptions : throw on incorrect argument
108 Caller : Transcript
109 Status : Stable
110  
111 =cut
112  
113 sub fetch_all_alternative_by_Transcript {
114 my ( $self, $transcript ) = @_;
115  
116 assert_ref($transcript, 'Bio::EnsEMBL::Transcript');
117  
118 my $tl_created_date =
119 $self->db()->dbc()->from_date_to_seconds('tl.created_date');
120 my $tl_modified_date =
121 $self->db()->dbc()->from_date_to_seconds('tl.modified_date');
122  
123 my $sql =
124 sprintf( "SELECT tl.translation_id, tl.start_exon_id, "
125 . "tl.end_exon_id, tl.seq_start, tl.seq_end, "
126 . "tl.stable_id, tl.version, %s, %s "
127 . "FROM translation tl "
128 . "JOIN transcript t "
129 . "ON (t.transcript_id = tl.transcript_id) "
130 . "WHERE tl.transcript_id = ? "
131 . "AND tl.translation_id != t.canonical_translation_id",
132 $tl_created_date, $tl_modified_date );
133  
134 my $transcript_id = $transcript->dbID();
135 my $sth = $self->prepare($sql);
136 $sth->bind_param( 1, $transcript_id, SQL_INTEGER );
137  
138 $sth->execute();
139  
140 my (
141 $translation_id, $start_exon_id, $end_exon_id,
142 $seq_start, $seq_end, $stable_id,
143 $version, $created_date, $modified_date
144 );
145  
146 $sth->bind_columns(
147 \(
148 $translation_id, $start_exon_id, $end_exon_id,
149 $seq_start, $seq_end, $stable_id,
150 $version, $created_date, $modified_date
151 ) );
152  
153 # Get all alternative translations.
154 my $translations = [];
155 while ( $sth->fetch() ) {
156 if ( !defined($translation_id) ) { next }
157  
158 my ( $start_exon, $end_exon );
159  
160 # this will load all the exons whenever we load the translation
161 # but I guess thats ok ....
162  
163 foreach my $exon ( @{ $transcript->get_all_Exons() } ) {
164 if ( $exon->dbID() == $start_exon_id ) { $start_exon = $exon }
165 if ( $exon->dbID() == $end_exon_id ) { $end_exon = $exon }
166 }
167  
168 if ( !( defined($start_exon) && defined($end_exon) ) ) {
169 throw(
170 sprintf(
171 "Could not find start or end exon in transcript_id=%d\n",
172 $transcript->dbID() ) );
173 }
174  
175 my $translation =
176 Bio::EnsEMBL::Translation->new_fast( {
177 'dbID' => $translation_id,
178 'adaptor' => $self,
179 'start' => $seq_start,
180 'end' => $seq_end,
181 'start_exon' => $start_exon,
182 'end_exon' => $end_exon,
183 'stable_id' => $stable_id,
184 'version' => $version,
185 'created_date' => $created_date || undef,
186 'modified_date' => $modified_date || undef,
187 } );
188  
189 $translation->transcript($transcript);
190  
191 push( @{$translations}, $translation );
192  
193 } ## end while ( $sth->fetch() )
194  
195 return $translations;
196 } ## end sub fetch_all_by_Transcript
197  
198 =head2 fetch_by_Transcript
199  
200 Arg [1] : Bio::EnsEMBL::Transcript $transcript
201 Example : $tl = $translation_adaptor->fetch_by_Transcript($transcript);
202 Description: Retrieves a Translation via its associated transcript.
203 If the Translation is not found, undef is returned.
204 Returntype : Bio::EnsEMBL::Translation
205 Exceptions : throw on incorrect argument
206 Caller : Transcript
207 Status : Stable
208  
209 =cut
210  
211 sub fetch_by_Transcript {
212 my ( $self, $transcript ) = @_;
213  
214 assert_ref( $transcript, 'Bio::EnsEMBL::Transcript' );
215  
216 my $tl_created_date =
217 $self->db()->dbc()->from_date_to_seconds('tl.created_date');
218 my $tl_modified_date =
219 $self->db()->dbc()->from_date_to_seconds('tl.modified_date');
220  
221 my $sql =
222 sprintf( "SELECT tl.translation_id, tl.start_exon_id, "
223 . "tl.end_exon_id, tl.seq_start, tl.seq_end, "
224 . "tl.stable_id, tl.version, %s, %s "
225 . "FROM translation tl "
226 . "JOIN transcript tr "
227 . "ON (tl.translation_id = tr.canonical_translation_id) "
228 . "WHERE tr.transcript_id = ?",
229 $tl_created_date, $tl_modified_date );
230  
231 my $transcript_id = $transcript->dbID();
232 my $sth = $self->prepare($sql);
233 $sth->bind_param( 1, $transcript_id, SQL_INTEGER );
234  
235 $sth->execute();
236  
237 my (
238 $translation_id, $start_exon_id, $end_exon_id,
239 $seq_start, $seq_end, $stable_id,
240 $version, $created_date, $modified_date
241 ) = $sth->fetchrow_array();
242 $sth->finish();
243  
244 if ( !defined($translation_id) ) { return undef }
245  
246 my ( $start_exon, $end_exon );
247  
248 # this will load all the exons whenever we load the translation
249 # but I guess thats ok ....
250  
251 foreach my $exon ( @{ $transcript->get_all_Exons() } ) {
252 if ( $exon->dbID() == $start_exon_id ) { $start_exon = $exon }
253 if ( $exon->dbID() == $end_exon_id ) { $end_exon = $exon }
254 }
255  
256 if ( !( defined($start_exon) && defined($end_exon) ) ) {
257 throw(
258 sprintf( "Could not find start or end exon in transcript_id=%d\n",
259 $transcript->dbID() ) );
260 }
261  
262 my $translation =
263 Bio::EnsEMBL::Translation->new_fast( {
264 'dbID' => $translation_id,
265 'adaptor' => $self,
266 'start' => $seq_start,
267 'end' => $seq_end,
268 'start_exon' => $start_exon,
269 'end_exon' => $end_exon,
270 'stable_id' => $stable_id,
271 'version' => $version,
272 'created_date' => $created_date || undef,
273 'modified_date' => $modified_date || undef,
274 } );
275  
276 $translation->transcript($transcript);
277  
278 return $translation;
279 } ## end sub fetch_by_Transcript
280  
281  
282  
283 =head2 fetch_all_by_external_name
284  
285 Arg [1] : string $external_name
286 The external identifier for the translation(s) to be
287 obtained.
288 Arg [2] : (optional) string $external_db_name
289 The name of the external database from which the
290 identifier originates.
291 Arg [3] : Boolean override. Force SQL regex matching for users
292 who really do want to find all 'NM%'
293 Example : my @translations =
294 @{ $trl_adaptor->fetch_all_by_external_name('BRCA2') };
295 my @many_translations =
296 @{ $trl_adaptor->fetch_all_by_external_name('BRCA%') };
297 Description: Retrieves a list of translations fetched via an
298 external identifier. Note that this may not be a
299 particularly useful method, because translations
300 do not make much sense out of the context of
301 their transcript. It may be better to use the
302 TranscriptAdaptor::fetch_all_by_external_name instead.
303 SQL wildcards % and _ are supported in the $external_name
304 but their use is somewhat restricted for performance reasons.
305 Users that really do want % and _ in the first three characters
306 should use argument 3 to prevent optimisations
307 Returntype : reference to a list of Translations
308 Exceptions : none
309 Caller : general
310 Status : Medium Risk
311  
312 =cut
313  
314 sub fetch_all_by_external_name {
315 my ( $self, $external_name, $external_db_name, $override ) = @_;
316  
317 my $entry_adaptor = $self->db->get_DBEntryAdaptor();
318  
319 my @ids = $entry_adaptor->list_translation_ids_by_extids(
320 $external_name, $external_db_name, $override );
321  
322 my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
323  
324 my @reference;
325 my @non_reference;
326 foreach my $id (@ids) {
327 my $transcript = $transcript_adaptor->fetch_by_translation_id($id);
328  
329 if ( defined($transcript) ) {
330 my $translation = $self->fetch_by_Transcript($transcript);
331 if($transcript->slice()->is_reference()) {
332 push(@reference, $translation);
333 }
334 else {
335 push(@non_reference, $translation);
336 }
337 }
338 }
339  
340 return [@reference, @non_reference];
341 }
342  
343 =head2 fetch_all_by_GOTerm
344  
345 Arg [1] : Bio::EnsEMBL::OntologyTerm
346 The GO term for which translations should be fetched.
347  
348 Example: @translations = @{
349 $translation_adaptor->fetch_all_by_GOTerm(
350 $go_adaptor->fetch_by_accession('GO:0030326') ) };
351  
352 Description : Retrieves a list of translations that are
353 associated with the given GO term, or with any of
354 its descendent GO terms.
355  
356 Return type : listref of Bio::EnsEMBL::Translation
357 Exceptions : Throws of argument is not a GO term
358 Caller : general
359 Status : Stable
360  
361 =cut
362  
363 sub fetch_all_by_GOTerm {
364 my ( $self, $term ) = @_;
365  
366 assert_ref( $term, 'Bio::EnsEMBL::OntologyTerm' );
367 if ( $term->ontology() ne 'GO' ) {
368 throw('Argument is not a GO term');
369 }
370  
371 my $entryAdaptor = $self->db->get_DBEntryAdaptor();
372  
373 my %unique_dbIDs;
374 foreach my $accession ( map { $_->accession() }
375 ( $term, @{ $term->descendants() } ) )
376 {
377 my @ids =
378 $entryAdaptor->list_translation_ids_by_extids( $accession, 'GO' );
379 foreach my $dbID (@ids) { $unique_dbIDs{$dbID} = 1 }
380 }
381  
382 my @result;
383 if ( scalar( keys(%unique_dbIDs) ) > 0 ) {
384 my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
385  
386 foreach my $dbID ( sort { $a <=> $b } keys(%unique_dbIDs) ) {
387 my $transcript =
388 $transcript_adaptor->fetch_by_translation_id($dbID);
389 if ( defined($transcript) ) {
390 push( @result, $self->fetch_by_Transcript($transcript) );
391 }
392 }
393 }
394  
395 return \@result;
396 } ## end sub fetch_all_by_GOTerm
397  
398 =head2 fetch_all_by_GOTerm_accession
399  
400 Arg [1] : String
401 The GO term accession for which genes should be
402 fetched.
403  
404 Example :
405  
406 @genes =
407 @{ $gene_adaptor->fetch_all_by_GOTerm_accession('GO:0030326') };
408  
409 Description : Retrieves a list of genes that are associated with
410 the given GO term, or with any of its descendent
411 GO terms. The genes returned are in their native
412 coordinate system, i.e. in the coordinate system
413 in which they are stored in the database. If
414 another coordinate system is required then the
415 Gene::transfer or Gene::transform method can be
416 used.
417  
418 Return type : listref of Bio::EnsEMBL::Gene
419 Exceptions : Throws of argument is not a GO term accession
420 Caller : general
421 Status : Stable
422  
423 =cut
424  
425 sub fetch_all_by_GOTerm_accession {
426 my ( $self, $accession ) = @_;
427  
428 if ( $accession !~ /^GO:/ ) {
429 throw('Argument is not a GO term accession');
430 }
431  
432 my $goAdaptor =
433 Bio::EnsEMBL::Registry->get_adaptor( 'Multi', 'Ontology',
434 'OntologyTerm' );
435  
436 my $term = $goAdaptor->fetch_by_accession($accession);
437  
438 return $self->fetch_all_by_GOTerm($term);
439 }
440  
441 =head2 store
442  
443 Arg [1] : Bio::EnsEMBL::Translation $translation
444 The translation object to be stored in the database
445 Example : $transl_id = $translation_adaptor->store($translation);
446 Description: Stores a translation object in the database
447 Returntype : int - the new dbID of the stored translation
448 Exceptions : thrown if the dbID of the start_Exon or end_Exon is not
449 defined.
450 thrown if only partial stable id information is present (e.g.
451 identifier but not version number)
452 Caller : Transcript::store
453 Status : Stable
454  
455 =cut
456  
457 sub store {
458 my ( $self, $translation, $transcript_id ) = @_;
459
460 my $start_exon = $translation->start_Exon();
461 my $end_exon = $translation->end_Exon();
462
463 if(!$start_exon) {
464 throw("Translation must define a start_Exon to be stored.");
465 }
466
467 if(!$end_exon) {
468 throw("Translation must define an end_Exon to be stored.");
469 }
470
471 if(!$start_exon->dbID) {
472 throw("start_Exon must have a dbID for Translation to be stored.");
473 }
474  
475 if(!$end_exon->dbID) {
476 throw("end_Exon must have a dbID for Translation to be stored.");
477 }
478  
479 my @columns = qw(
480 seq_start
481 start_exon_id
482 seq_end
483 end_exon_id
484 transcript_id
485 );
486  
487 my @canned_columns;
488 my @canned_values;
489  
490 if (defined($translation->stable_id)) {
491 push @columns, 'stable_id', 'version';
492  
493 my $created = $self->db->dbc->from_seconds_to_date($translation->created_date());
494 my $modified = $self->db->dbc->from_seconds_to_date($translation->modified_date());
495  
496 if ($created) {
497 push @canned_columns, 'created_date';
498 push @canned_values, $created;
499 }
500 if ($modified) {
501 push @canned_columns, 'modified_date';
502 push @canned_values, $modified;
503 }
504 }
505  
506 my $columns = join(', ', @columns, @canned_columns);
507 my $values = join(', ', ('?') x @columns, @canned_values);
508 my $store_translation_sql = qq(
509 INSERT INTO translation ( $columns ) VALUES ( $values )
510 );
511  
512 my $sth = $self->prepare($store_translation_sql);
513 $sth->bind_param(1,$translation->start,SQL_INTEGER);
514 $sth->bind_param(2,$translation->start_Exon->dbID,SQL_INTEGER);
515 $sth->bind_param(3,$translation->end,SQL_INTEGER);
516 $sth->bind_param(4,$translation->end_Exon->dbID,SQL_INTEGER);
517 $sth->bind_param(5,$transcript_id,SQL_INTEGER);
518  
519  
520 if (defined($translation->stable_id)) {
521
522 $sth->bind_param(6, $translation->stable_id,SQL_VARCHAR);
523 $sth->bind_param(7, $translation->version,SQL_VARCHAR);
524 }
525  
526 $sth->execute();
527
528 my $transl_dbID = $self->last_insert_id('translation_id', undef, 'translation');
529  
530 #
531 # store object xref mappings to translations
532 #
533
534 my $dbEntryAdaptor = $self->db()->get_DBEntryAdaptor();
535 # store each of the xrefs for this translation
536 foreach my $dbl ( @{$translation->get_all_DBEntries} ) {
537 $dbEntryAdaptor->store( $dbl, $transl_dbID, "Translation", 1 );
538 }
539  
540 #storing the protein features associated with the translation
541 my $pfadaptor = $self->db->get_ProteinFeatureAdaptor();
542 foreach my $pf(@{$translation->get_all_ProteinFeatures}){
543 $pfadaptor->store($pf, $transl_dbID);
544 }
545  
546 $translation->get_all_Attributes();
547  
548 # store any translation attributes that are defined
549 my $attr_adaptor = $self->db->get_AttributeAdaptor();
550 $attr_adaptor->store_on_Translation($transl_dbID,
551 $translation->get_all_Attributes());
552  
553 $translation->dbID($transl_dbID);
554 $translation->adaptor($self);
555  
556 return $transl_dbID;
557 }
558  
559  
560  
561 =head2 remove
562  
563 Arg [1] : Bio::EnsEMBL::Translation $translation
564 Example : $translation_adaptor->remove($translation);
565 Description: Removes a translation completely from the database, and all
566 associated information including protein features etc.
567 Returntype : none
568 Exceptions : throw on incorrect arguments
569 warning if translation is not in this database
570 Caller : TranscriptAdaptor::remove
571 Status : Stable
572  
573 =cut
574  
575 sub remove {
576 my $self = shift;
577 my $translation = shift;
578  
579 if(!ref($translation) || !$translation->isa('Bio::EnsEMBL::Translation')) {
580 throw("Bio::EnsEMBL::Translation argument expected.");
581 }
582  
583 if( !$translation->is_stored($self->db()) ) {
584 warning("Cannot remove translation " . $translation->dbID() .
585 ". Is not stored in this database.");
586 return;
587 }
588  
589 # remove athe attributes associated with this translation
590 my $attrib_adp = $self->db->get_AttributeAdaptor;
591 $attrib_adp->remove_from_Translation($translation);
592  
593 #remove all transcripts links to translation
594 my $sth = $self->prepare
595 ("UPDATE transcript SET canonical_translation_id = NULL WHERE canonical_translation_id = ?");
596 $sth->bind_param(1,$translation->dbID,SQL_INTEGER);
597 $sth->execute();
598 $sth->finish();
599  
600 # remove all xref associations to this translation
601 my $dbe_adaptor = $self->db()->get_DBEntryAdaptor();
602 foreach my $dbe (@{$translation->get_all_DBEntries()}) {
603 $dbe_adaptor->remove_from_object($dbe, $translation, 'Translation');
604 }
605  
606 # remove all protein_features on this translation
607 $sth = $self->prepare
608 ("DELETE FROM protein_feature WHERE translation_id = ?");
609 $sth->bind_param(1,$translation->dbID,SQL_INTEGER);
610 $sth->execute();
611 $sth->finish();
612  
613 # remove the translation itself
614  
615 $sth = $self->prepare("DELETE FROM translation WHERE translation_id = ?" );
616 $sth->bind_param(1,$translation->dbID,SQL_INTEGER);
617 $sth->execute();
618 $sth->finish();
619  
620 $translation->dbID( undef );
621 $translation->adaptor(undef);
622  
623 return
624 }
625  
626  
627 =head2 list_dbIDs
628  
629 Arg [1] : none
630 Example : @translation_ids = @{$translation_adaptor->list_dbIDs()};
631 Description: Gets an array of internal ids for all translations in the current db
632 Returntype : list of ints
633 Exceptions : none
634 Caller : ?
635 Status : Stable
636  
637 =cut
638  
639 sub list_dbIDs {
640 my ($self) = @_;
641  
642 return $self->_list_dbIDs("translation");
643 }
644  
645  
646 =head2 list_stable_ids
647  
648 Arg [1] : none
649 Example : @transl_stable_ids = @{$transl_adaptor->list_stable_dbIDs()};
650 Description: Gets an array of stable ids for all translations in the current
651 db
652 Returntype : reference to a list of strings
653 Exceptions : none
654 Caller : general
655 Status : Stable
656  
657 =cut
658  
659 sub list_stable_ids {
660 my ($self) = @_;
661  
662 return $self->_list_dbIDs("translation", "stable_id");
663 }
664  
665 =head2 _list_dbIDs
666  
667 Arg[1] : String $table
668 Arg[2] : String $column
669 Example : $transl_adaptor->_list_dbIDs('translation','translation_id');
670 Description : Local reimplementation to ensure multi-species translations
671 are limited to their species alone
672 Returntype : ArrayRef of specified IDs
673 Caller : Internal
674 Status : Unstable
675 =cut
676  
677 sub _list_dbIDs {
678 my ($self, $table, $column) = @_;
679 my $ids;
680 if($self->is_multispecies()) {
681 $column ||= "${table}_id";
682 my $sql = <<SQL;
683 select `tr`.`${column}`
684 from translation tr
685 join transcript t using (transcript_id)
686 join seq_region sr using (seq_region_id)
687 join coord_system cs using (coord_system_id)
688 where cs.species_id =?
689 SQL
690 return $self->dbc()->sql_helper()->execute_simple(-SQL => $sql, -PARAMS => [$self->species_id()]);
691 }
692 else {
693 $ids = $self->SUPER::_list_dbIDs($table, $column);
694 }
695 return $ids;
696 }
697  
698  
699  
700 =head2 fetch_by_dbID
701  
702 Arg [1] : int $dbID
703 The internal identifier of the Translation to obtain
704 Example : $translation = $translation_adaptor->fetch_by_dbID(1234);
705 Description: This fetches a Translation object via its internal id.
706 This is only debatably useful since translations do
707 not make much sense outside of the context of their
708 Transcript. Consider using fetch_by_Transcript instead.
709 Returntype : Bio::EnsEMBL::Translation, or undef if the translation is not
710 found.
711 Exceptions : warning if an additional (old style) Transcript argument is
712 provided
713 Caller : ?
714 Status : Stable
715  
716 =cut
717  
718 sub fetch_by_dbID {
719 my ( $self, $dbID, $transcript ) = @_;
720  
721 if ( !defined($dbID) ) {
722 throw("dbID argument is required");
723 }
724  
725 my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
726 $transcript = $transcript_adaptor->fetch_by_translation_id($dbID);
727  
728 if ( defined($transcript) ) {
729 my $translation = $self->fetch_by_Transcript($transcript);
730  
731 if ( defined($translation) && $translation->dbID()==$dbID ) {
732 return $translation;
733 }
734  
735 my @alt_translations =
736 @{ $self->fetch_all_alternative_by_Transcript($transcript) };
737  
738 foreach my $alt_translation (@alt_translations) {
739 if ( $alt_translation->dbID() == $dbID ) {
740 return $alt_translation;
741 }
742 }
743 }
744  
745 return undef;
746 } ## end sub fetch_by_dbID
747  
748  
749 =head2 fetch_by_stable_id
750  
751 Arg [1] : string $stable_id
752 The stable identifier of the Translation to obtain
753 Example : $translation = $translation_adaptor->fetch_by_stable_id("ENSP00001");
754 Description: This fetches a Translation object via its stable id.
755 This is only debatably useful since translations do
756 not make much sense outside of the context of their
757 Transcript. Consider using fetch_by_Transcript instead.
758 Returntype : Bio::EnsEMBL::Translation or undef if the translation is not
759 found.
760 Exceptions : warning if an additional (old style) Transcript argument is
761 provided
762 Caller : ?
763 Status : Stable
764  
765 =cut
766  
767 sub fetch_by_stable_id {
768 my ($self,$stable_id) = @_;
769  
770 if(!$stable_id) {
771 throw("stable id argument is required");
772 }
773  
774 my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
775 my $transcript =
776 $transcript_adaptor->fetch_by_translation_stable_id($stable_id);
777  
778 return undef if(!$transcript);
779  
780 return $self->fetch_by_Transcript($transcript);
781 }
782  
783 =head2 fetch_by_stable_id_version
784  
785 Arg [1] : String $id
786 The stable ID of the gene to retrieve
787 Arg [2] : Integer $version
788 The version of the stable_id to retrieve
789 Example : $gene = $gene_adaptor->fetch_by_stable_id('ENSG00000148944', 14);
790 Description: Retrieves a gene object from the database via its stable id and version.
791 The gene will be retrieved in its native coordinate system (i.e.
792 in the coordinate system it is stored in the database). It may
793 be converted to a different coordinate system through a call to
794 transform() or transfer(). If the gene or exon is not found
795 undef is returned instead.
796 Returntype : Bio::EnsEMBL::Gene or undef
797 Exceptions : if we cant get the gene in given coord system
798 Caller : general
799 Status : Stable
800  
801 =cut
802  
803 sub fetch_by_stable_id_version {
804 my ($self,$stable_id, $version) = @_;
805  
806 if(!$stable_id) {
807 throw("stable id argument is required");
808 }
809  
810 # Enforce that version be numeric
811 return unless($version =~ /^\d+$/);
812  
813 my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
814 my $transcript =
815 $transcript_adaptor->fetch_by_translation_stable_id_version($stable_id, $version);
816  
817 return if(!$transcript);
818  
819 return $self->fetch_by_Transcript($transcript);
820 }
821  
822 =head2 fetch_all_by_Transcript_list
823  
824 Arg [1] : reference to list of Bio::EnsEMBL::Transcripts $transcripts
825 The list of $transcripts to obtain Translation object for.
826 Example : @translations = @{$tla->fetch_all_by_Transcript_list([$t1,$t2]);
827 Description: Fetches all translations associated with the list of transcripts
828 passed to this method. The passed transcripts will also have
829 their translation set by this method.
830 Returntype : Reference to list of Bio::EnsEMBL::Translations
831 Exceptions : None
832 Caller : general
833 Status : Stable
834  
835 =cut
836  
837 sub fetch_all_by_Transcript_list {
838 my ($self,$transcripts) = @_;
839  
840 if(!defined($transcripts) || ref($transcripts) ne 'ARRAY') {
841 throw("reference to list of Transcripts argument is required");
842 }
843  
844 return [] if(!@$transcripts);
845  
846 my %trans_hash = map {$_->dbID() => $_} @$transcripts;
847 my @id_list = keys %trans_hash;
848  
849 my @out;
850  
851 # mysql is faster and we ensure that we do not exceed the max query size by
852 # splitting large queries into smaller queries of 200 ids
853 my $max_size = 200;
854  
855 my ( $transcript_id, $translation_id, $start_exon_id, $end_exon_id,
856 $seq_start, $seq_end, $stable_id, $version,
857 $created_date, $modified_date );
858  
859 my %ex_hash;
860  
861 while(@id_list) {
862 my @ids;
863 if(@id_list > $max_size) {
864 @ids = splice(@id_list, 0, $max_size);
865 } else {
866 @ids = splice(@id_list, 0);
867 }
868  
869 my $id_str;
870 if(@ids > 1) {
871 $id_str = " IN (" . join(',', @ids). ")";
872 } else {
873 $id_str = " = " . $ids[0];
874 }
875
876 my $canonical_lookup = $self->dbc()->sql_helper()->execute_into_hash(
877 -SQL => 'SELECT transcript_id, canonical_translation_id FROM transcript WHERE transcript_id '.$id_str
878 );
879  
880 my $created_date = $self->db->dbc->from_date_to_seconds("tl.created_date");
881 my $modified_date = $self->db->dbc->from_date_to_seconds("tl.modified_date");
882  
883 my $sth = $self->prepare
884 ("SELECT tl.transcript_id, tl.translation_id, tl.start_exon_id,
885 tl.end_exon_id, tl.seq_start, tl.seq_end,
886 tl.stable_id, tl.version, " . $created_date . "," .
887 $modified_date .
888 " FROM translation tl
889 WHERE tl.transcript_id $id_str");
890  
891 $sth->execute();
892  
893 $sth->bind_columns( \$transcript_id, \$translation_id, \$start_exon_id, \$end_exon_id,
894 \$seq_start, \$seq_end, \$stable_id, \$version,
895 \$created_date, \$modified_date );
896  
897 while($sth->fetch()) {
898 my ($start_exon, $end_exon);
899  
900 # this will load all the exons whenever we load the translation
901 # but I guess thats ok ....
902  
903 my $tr = $trans_hash{$transcript_id};
904  
905 foreach my $exon (@{$tr->get_all_Exons()}) {
906 if(!$start_exon && $exon->dbID() == $start_exon_id ) {
907 $start_exon = $exon;
908 last if($end_exon);
909 }
910  
911 if(!$end_exon && $exon->dbID() == $end_exon_id ) {
912 $end_exon = $exon;
913 last if($start_exon);
914 }
915 }
916  
917 unless($start_exon && $end_exon) {
918 throw("Could not find start or end exon in transcript\n");
919 }
920  
921 my $tl = Bio::EnsEMBL::Translation->new
922 (-dbID => $translation_id,
923 -seq_start => $seq_start,
924 -seq_end => $seq_end,
925 -start_exon => $start_exon,
926 -end_exon => $end_exon,
927 -stable_id => $stable_id,
928 -version => $version,
929 -created_date => $created_date || undef,
930 -modified_date => $modified_date || undef);
931  
932 # Calling the new method will set $tl->version to '1' if $version is not defined.
933 # But if the version in the database is NULL, $version will be undef; and so we
934 # need to override the default version of '1', and set it back to undef.
935 $tl->{version} = undef unless defined $version;
936
937 $tl->adaptor($self);
938 my $canonical_translation_id = $canonical_lookup->{$transcript_id};
939 $tr->translation($tl) if $translation_id == $canonical_translation_id;
940  
941 push @out, $tl;
942 }
943 }
944  
945 return \@out;
946 }
947  
948  
949 =head2 fetch_all
950  
951 Example : $translations = $translation_adaptor->fetch_all();
952 Description : Retrieves all canonical and alternative translations
953 stored in the database.
954 Returntype : listref of Bio::EnsEMBL::Translation
955 Caller : general
956 Status : At Risk
957  
958 =cut
959  
960 sub fetch_all {
961 my ($self) = @_;
962 my $transcript_adaptor = $self->db()->get_TranscriptAdaptor();
963  
964 my @translations;
965 foreach my $transcript (@{$transcript_adaptor->fetch_all}) {
966 my $translation = $self->fetch_by_Transcript($transcript);
967 if ($translation) {
968 push @translations, $translation;
969 }
970 foreach my $alt_translation (@{$self->fetch_all_alternative_by_Transcript($transcript)}) {
971 push @translations, $alt_translation;
972 }
973 }
974 return \@translations;
975 }
976  
977  
978 # _tables
979 # Arg [1] : none
980 # Description: PROTECTED implementation of superclass abstract method.
981 # Returns the names, aliases of the tables to use for queries.
982 # Returntype : list of listrefs of strings
983 # Exceptions : none
984 # Caller : internal
985 # Status : Stable
986  
987 sub _tables {
988 return (['translation', 'tl']);
989 }
990  
991 1;