comparison variant_effect_predictor/Bio/EnsEMBL/DBSQL/TranslationAdaptor.pm @ 0:1f6dce3d34e0

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