EnsEMBL/DBSQL/ArchiveStableIdAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 493 Branchbran 0.0% 0 / 130 Conditioncond 0.0% 0 / 51 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 730 CC 66 SCAR 35.1
CC
66
Cov
0%
fetch_history_tree_by_stable_id
90.0
_resolve_type
90.0
_fetch_by_stable_id
42.0
CRAP 33.3
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::ArchiveStableIdAdaptor
34  
35 =head1 SYNOPSIS
36  
37 my $registry = "Bio::EnsEMBL::Registry";
38  
39 my $archiveStableIdAdaptor =
40 $registry->get_adaptor( 'Human', 'Core', 'ArchiveStableId' );
41  
42 my $stable_id = 'ENSG00000068990';
43  
44 my $arch_id = $archiveStableIdAdaptor->fetch_by_stable_id($stable_id);
45  
46 print("Latest incarnation of this stable ID:\n");
47 printf( " Stable ID: %s.%d\n",
48 $arch_id->stable_id(), $arch_id->version() );
49 print(" Release: "
50 . $arch_id->release() . " ("
51 . $arch_id->assembly() . ", "
52 . $arch_id->db_name()
53 . ")\n" );
54  
55 print "\nStable ID history:\n\n";
56  
57 my $history =
58 $archiveStableIdAdaptor->fetch_history_tree_by_stable_id(
59 $stable_id);
60  
61 foreach my $a ( @{ $history->get_all_ArchiveStableIds } ) {
62 printf( " Stable ID: %s.%d\n", $a->stable_id(), $a->version() );
63 print(" Release: "
64 . $a->release() . " ("
65 . $a->assembly() . ", "
66 . $a->db_name()
67 . ")\n\n" );
68 }
69  
70 =head1 DESCRIPTION
71  
72 ArchiveStableIdAdaptor does all SQL to create ArchiveStableIds and works
73 of
74  
75 stable_id_event
76 mapping_session
77 peptite_archive
78 gene_archive
79  
80 tables inside the core database.
81  
82 This whole module has a status of At Risk as it is under development.
83  
84 =head1 METHODS
85  
86 fetch_by_stable_id
87 fetch_by_stable_id_version
88 fetch_by_stable_id_dbname
89 fetch_all_by_archive_id
90 fetch_predecessors_by_archive_id
91 fetch_successors_by_archive_id
92 fetch_history_tree_by_stable_id
93 add_all_current_to_history
94 list_dbnames
95 previous_dbname
96 next_dbname
97 get_peptide
98 get_current_release
99 get_current_assembly
100  
101 =head1 RELATED MODULES
102  
103 Bio::EnsEMBL::ArchiveStableId
104 Bio::EnsEMBL::StableIdEvent
105 Bio::EnsEMBL::StableIdHistoryTree
106  
107 =head1 METHODS
108  
109 =cut
110  
111 package Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor;
112 $Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor::VERSION = '114.0.0_50'; # TRIAL
113 $Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor::VERSION = '114.0.050';
114 use strict;
115 use warnings;
116 no warnings qw(uninitialized);
117  
118 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
119 our @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
120  
121 use Bio::EnsEMBL::ArchiveStableId;
122 use Bio::EnsEMBL::StableIdEvent;
123 use Bio::EnsEMBL::StableIdHistoryTree;
124 use Bio::EnsEMBL::Utils::Exception qw(warning throw);
125  
126 use constant MAX_ROWS => 30;
127 use constant NUM_HIGH_SCORERS => 20;
128  
129  
130 =head2 fetch_by_stable_id
131  
132 Arg [1] : string $stable_id
133 Arg [2] : (optional) string $type
134 Example : none
135 Description : Retrives an ArchiveStableId that is the latest incarnation of
136 given stable_id. If the lookup fails, attempts to check for a
137 version id delimited by a period (.) and lookup again using the
138 version id.
139 Returntype : Bio::EnsEMBL::ArchiveStableId or undef if not in database
140 Exceptions : none
141 Caller : general
142 Status : At Risk
143 : under development
144  
145 =cut
146  
147 sub fetch_by_stable_id {
148 my $self = shift;
149 my $stable_id = shift;
150  
151 my $arch_id = $self->_fetch_by_stable_id($stable_id, @_);
152  
153 # If we didn't get anything back, desperately try to see if there's
154 # a version number in the stable_id
155 if(!defined($arch_id)) {
156 my $vindex = rindex($stable_id, '.');
157 if ($vindex !~ /^[0-9]{1,5}$/) { return $arch_id; }
158 $arch_id = $self->fetch_by_stable_id_version(substr($stable_id,0,$vindex),
159 substr($stable_id,$vindex+1),
160 @_);
161 }
162  
163 return $arch_id;
164 }
165  
166 =head2 _fetch_by_stable_id
167  
168 Arg [1] : string $stable_id
169 Arg [2] : (optional) string $type
170 Example : none
171 Description : Retrives an ArchiveStableId that is the latest incarnation of
172 given stable_id. Helper function to fetch_by_stable_id, should
173 not be directly called.
174 Returntype : Bio::EnsEMBL::ArchiveStableId or undef if not in database
175 Exceptions : none
176 Caller : general
177 Status : At Risk
178 : under development
179  
180 =cut
181  
182 sub _fetch_by_stable_id {
183 my $self = shift;
184 my $stable_id = shift;
185
186 my $arch_id = Bio::EnsEMBL::ArchiveStableId->new(
187 -stable_id => $stable_id,
188 -adaptor => $self
189 );
190  
191 @_ ? $arch_id->type(shift) : $self->_resolve_type($arch_id);
192  
193 if ($self->lookup_current($arch_id)) {
194  
195 # stable ID is in current release
196 $arch_id->version($arch_id->current_version);
197 $arch_id->db_name($self->dbc->dbname);
198 $arch_id->release($self->get_current_release);
199 $arch_id->assembly($self->get_current_assembly);
200
201 } else {
202  
203 # look for latest version of this stable id
204 my $extra_sql = defined($arch_id->{'type'}) ?
205 " AND sie.type = '@{[lc($arch_id->{'type'})]}'" : '';
206  
207 my $r = $self->_fetch_archive_id($stable_id, $extra_sql, $extra_sql);
208  
209 if ($r->{'new_stable_id'} and $r->{'new_stable_id'} eq $stable_id) {
210 # latest event is a self event, use new_* data
211 $arch_id->version($r->{'new_version'});
212 $arch_id->release($r->{'new_release'});
213 $arch_id->assembly($r->{'new_assembly'});
214 $arch_id->db_name($r->{'new_db_name'});
215 } else {
216 # latest event is a deletion event (or mapping to other ID; this clause
217 # is only used to cope with buggy data where deletion events are
218 # missing), use old_* data
219 $arch_id->version($r->{'old_version'});
220 $arch_id->release($r->{'old_release'});
221 $arch_id->assembly($r->{'old_assembly'});
222 $arch_id->db_name($r->{'old_db_name'});
223 }
224  
225 $arch_id->type(ucfirst(lc($r->{'type'})));
226 }
227
228 if (! defined $arch_id->db_name) {
229 # couldn't find stable ID in archive or current db
230 return undef;
231 }
232  
233 $arch_id->is_latest(1);
234  
235 return $arch_id;
236 }
237  
238  
239 =head2 fetch_by_stable_id_version
240  
241 Arg [1] : string $stable_id
242 Arg [2] : int $version
243 Example : none
244 Description : Retrieve an ArchiveStableId with given version and stable ID.
245 Returntype : Bio::EnsEMBL::ArchiveStableId
246 Exceptions : none
247 Caller : general
248 Status : At Risk
249 : under development
250  
251 =cut
252  
253 sub fetch_by_stable_id_version {
254 my $self = shift;
255 my $stable_id = shift;
256 my $version = shift;
257  
258 my $arch_id = Bio::EnsEMBL::ArchiveStableId->new(
259 -stable_id => $stable_id,
260 -version => $version,
261 -adaptor => $self
262 );
263  
264 if ($version !~ /^[0-9]{1,5}$/) {
265 throw("$version is not valid, should be a small int");
266 }
267
268 @_ ? $arch_id->type(shift) : $self->_resolve_type($arch_id);
269  
270 if ($self->lookup_current($arch_id) && $arch_id->is_current) {
271  
272 # this version is the current one
273 $arch_id->db_name($self->dbc->dbname);
274 $arch_id->release($self->get_current_release);
275 $arch_id->assembly($self->get_current_assembly);
276
277 } else {
278  
279 # find latest release this stable ID version is found in archive
280 my $extra_sql1 = qq(AND sie.old_version = "$version");
281 my $extra_sql2 = qq(AND sie.new_version = "$version");
282 my $r = $self->_fetch_archive_id($stable_id, $extra_sql1, $extra_sql2);
283  
284 if ($r->{'new_stable_id'} and $r->{'new_stable_id'} eq $stable_id
285 and $r->{'new_version'} == $version) {
286 # latest event is a self event, use new_* data
287 $arch_id->release($r->{'new_release'});
288 $arch_id->assembly($r->{'new_assembly'});
289 $arch_id->db_name($r->{'new_db_name'});
290 } else {
291 # latest event is a deletion event (or mapping to other ID; this clause
292 # is only used to cope with buggy data where deletion events are
293 # missing), use old_* data
294 $arch_id->release($r->{'old_release'});
295 $arch_id->assembly($r->{'old_assembly'});
296 $arch_id->db_name($r->{'old_db_name'});
297 }
298  
299 $arch_id->type(ucfirst(lc($r->{'type'})));
300 }
301
302 if (! defined $arch_id->db_name) {
303 # couldn't find stable ID version in archive or current release
304 return undef;
305 }
306  
307 return $arch_id;
308 }
309  
310  
311 =head2 fetch_by_stable_id_dbname
312  
313 Arg [1] : string $stable_id
314 Arg [2] : string $db_name
315 Example : none
316 Description : Create an ArchiveStableId from given arguments.
317 Returntype : Bio::EnsEMBL::ArchiveStableId or undef if not in database
318 Exceptions : none
319 Caller : general
320 Status : At Risk
321 : under development
322  
323 =cut
324  
325 sub fetch_by_stable_id_dbname {
326 my $self = shift;
327 my $stable_id = shift;
328 my $db_name = shift;
329
330 my $arch_id = Bio::EnsEMBL::ArchiveStableId->new(
331 -stable_id => $stable_id,
332 -db_name => $db_name,
333 -adaptor => $self
334 );
335
336 @_ ? $arch_id->type(shift) : $self->_resolve_type($arch_id);
337  
338 if ($self->lookup_current($arch_id) and $db_name eq $self->dbc->dbname) {
339  
340 # this version is the current one
341 $arch_id->version($arch_id->current_version);
342 $arch_id->release($self->get_current_release);
343 $arch_id->assembly($self->get_current_assembly);
344
345 } else {
346  
347 # find version for this dbname in the stable ID archive
348 my $extra_sql = defined($arch_id->{'type'}) ?
349 " AND sie.type = '@{[lc($arch_id->{'type'})]}'" : '';
350 my $extra_sql1 = $extra_sql . qq( AND ms.old_db_name = "$db_name");
351 my $extra_sql2 = $extra_sql . qq( AND ms.new_db_name = "$db_name");
352 my $r = $self->_fetch_archive_id($stable_id, $extra_sql1, $extra_sql2);
353  
354 if ($r->{'new_stable_id'} and $r->{'new_stable_id'} eq $stable_id
355 and $r->{'new_db_name'} eq $db_name) {
356  
357 # latest event is a self event, use new_* data
358 $arch_id->release($r->{'new_release'});
359 $arch_id->assembly($r->{'new_assembly'});
360 $arch_id->version($r->{'new_version'});
361 } else {
362 # latest event is a deletion event (or mapping to other ID; this clause
363 # is only used to cope with buggy data where deletion events are
364 # missing), use old_* data
365 $arch_id->release($r->{'old_release'});
366 $arch_id->assembly($r->{'old_assembly'});
367 $arch_id->version($r->{'old_version'});
368 }
369  
370 $arch_id->type(ucfirst(lc($r->{'type'})));
371 }
372
373 if (! defined $arch_id->version ) {
374 # couldn't find stable ID version in archive or current release
375 return undef;
376 }
377  
378 return $arch_id;
379 }
380  
381 #
382 # Helper method to do fetch ArchiveStableId from db.
383 # Used by fetch_by_stable_id(), fetch_by_stable_id_version() and
384 # fetch_by_stable_id_dbname().
385 # Returns hashref as returned by DBI::sth::fetchrow_hashref
386 #
387 sub _fetch_archive_id {
388 my $self = shift;
389 my $stable_id = shift;
390 my $extra_sql1 = shift;
391 my $extra_sql2 = shift;
392  
393 # using a UNION is much faster in this query than somthing like
394 # "... AND (sie.old_stable_id = ? OR sie.new_stable_id = ?)"
395 my $sql = qq(
396 SELECT * FROM stable_id_event sie, mapping_session ms
397 WHERE sie.mapping_session_id = ms.mapping_session_id
398 AND sie.old_stable_id = ?
399 $extra_sql1
400 UNION
401 SELECT * FROM stable_id_event sie, mapping_session ms
402 WHERE sie.mapping_session_id = ms.mapping_session_id
403 AND sie.new_stable_id = ?
404 $extra_sql2
405 ORDER BY created DESC, score DESC
406 LIMIT 1
407 );
408  
409 my $sth = $self->prepare($sql);
410 $sth->execute($stable_id,$stable_id);
411 my $r = $sth->fetchrow_hashref;
412 $sth->finish;
413  
414 return $r;
415 }
416  
417  
418 =head2 fetch_all_by_archive_id
419  
420 Arg [1] : Bio::EnsEMBL::ArchiveStableId $archive_id
421 Arg [2] : String $return_type - type of ArchiveStableId to fetch
422 Example : my $arch_id = $arch_adaptor->fetch_by_stable_id('ENSG0001');
423 my @archived_transcripts =
424 $arch_adaptor->fetch_all_by_archive_id($arch_id, 'Transcript');
425 Description : Given a ArchiveStableId it retrieves associated ArchiveStableIds
426 of specified type (e.g. retrieve transcripts for genes or vice
427 versa).
428  
429 See also fetch_associated_archived() for a different approach to
430 retrieve this data.
431 Returntype : listref Bio::EnsEMBL::ArchiveStableId
432 Exceptions : none
433 Caller : Bio::EnsEMBL::ArchiveStableId->get_all_gene_archive_ids,
434 get_all_transcript_archive_ids, get_all_translation_archive_ids
435 Status : At Risk
436 : under development
437  
438 =cut
439  
440 sub fetch_all_by_archive_id {
441 my $self = shift;
442 my $archive_id = shift;
443 my $return_type = shift;
444  
445 my @result = ();
446 my $lc_self_type = lc($archive_id->type);
447 my $lc_return_type = lc($return_type);
448  
449 my $sql = qq(
450 SELECT
451 ga.${lc_return_type}_stable_id,
452 ga.${lc_return_type}_version,
453 m.old_db_name,
454 m.old_release,
455 m.old_assembly
456 FROM gene_archive ga, mapping_session m
457 WHERE ga.${lc_self_type}_stable_id = ?
458 AND ga.${lc_self_type}_version = ?
459 AND ga.mapping_session_id = m.mapping_session_id
460 );
461
462 my $sth = $self->prepare($sql);
463 $sth->bind_param(1, $archive_id->stable_id, SQL_VARCHAR);
464 $sth->bind_param(2, $archive_id->version, SQL_SMALLINT);
465 $sth->execute;
466
467 my ($stable_id, $version, $db_name, $release, $assembly);
468 $sth->bind_columns(\$stable_id, \$version, \$db_name, \$release, \$assembly);
469  
470 while ($sth->fetch) {
471 my $new_arch_id = Bio::EnsEMBL::ArchiveStableId->new(
472 -stable_id => $stable_id,
473 -version => $version,
474 -db_name => $db_name,
475 -release => $release,
476 -assembly => $assembly,
477 -type => $return_type,
478 -adaptor => $self
479 );
480  
481 push( @result, $new_arch_id );
482 }
483  
484 $sth->finish();
485 return \@result;
486 }
487  
488  
489 =head2 fetch_associated_archived
490  
491 Arg[1] : Bio::EnsEMBL::ArchiveStableId $arch_id -
492 the ArchiveStableId to fetch associated archived IDs for
493 Example : my ($arch_gene, $arch_tr, $arch_tl, $pep_seq) =
494 @{ $archive_adaptor->fetch_associated_archived($arch_id) };
495 Description : Fetches associated archived stable IDs from the db for a given
496 ArchiveStableId (version is taken into account).
497 Return type : Listref of
498 ArchiveStableId archived gene
499 ArchiveStableId archived transcript
500 (optional) ArchiveStableId archived translation
501 (optional) peptide sequence
502 Exceptions : thrown on missing or wrong argument
503 thrown if ArchiveStableID has no type
504 Caller : Bio::EnsEMBL::ArchiveStableId->get_all_associated_archived()
505 Status : At Risk
506 : under development
507  
508 =cut
509  
510 sub fetch_associated_archived {
511 my $self = shift;
512 my $arch_id = shift;
513  
514 throw("Need a Bio::EnsEMBL::ArchiveStableId") unless ($arch_id
515 and ref($arch_id) and $arch_id->isa('Bio::EnsEMBL::ArchiveStableId'));
516  
517 my $type = $arch_id->type();
518  
519 if ( !defined($type) ) {
520 throw("Can't deduce ArchiveStableId type.");
521 }
522  
523 $type = lc($type);
524  
525 my $sql = qq(
526 SELECT ga.gene_stable_id,
527 ga.gene_version,
528 ga.transcript_stable_id,
529 ga.transcript_version,
530 ga.translation_stable_id,
531 ga.translation_version,
532 pa.peptide_seq,
533 ms.old_release,
534 ms.old_assembly,
535 ms.old_db_name
536 FROM (mapping_session ms, gene_archive ga)
537 LEFT JOIN peptide_archive pa
538 ON ga.peptide_archive_id = pa.peptide_archive_id
539 WHERE ga.mapping_session_id = ms.mapping_session_id
540 AND ga.${type}_stable_id = ?
541 AND ga.${type}_version = ?
542 );
543  
544 my $sth = $self->prepare($sql);
545 $sth->bind_param(1, $arch_id->stable_id, SQL_VARCHAR);
546 $sth->bind_param(2, $arch_id->version, SQL_SMALLINT);
547 $sth->execute;
548  
549 my @result = ();
550  
551 while (my $r = $sth->fetchrow_hashref) {
552  
553 my @row = ();
554  
555 # create ArchiveStableIds genes, transcripts and translations
556 push @row, Bio::EnsEMBL::ArchiveStableId->new(
557 -stable_id => $r->{'gene_stable_id'},
558 -version => $r->{'gene_version'},
559 -db_name => $r->{'old_db_name'},
560 -release => $r->{'old_release'},
561 -assembly => $r->{'old_assembly'},
562 -type => 'Gene',
563 -adaptor => $self
564 );
565
566 push @row, Bio::EnsEMBL::ArchiveStableId->new(
567 -stable_id => $r->{'transcript_stable_id'},
568 -version => $r->{'transcript_version'},
569 -db_name => $r->{'old_db_name'},
570 -release => $r->{'old_release'},
571 -assembly => $r->{'old_assembly'},
572 -type => 'Transcript',
573 -adaptor => $self
574 );
575  
576 if ($r->{'translation_stable_id'}) {
577 push @row, Bio::EnsEMBL::ArchiveStableId->new(
578 -stable_id => $r->{'translation_stable_id'},
579 -version => $r->{'translation_version'},
580 -db_name => $r->{'old_db_name'},
581 -release => $r->{'old_release'},
582 -assembly => $r->{'old_assembly'},
583 -type => 'Translation',
584 -adaptor => $self
585 );
586  
587 # push peptide sequence onto result list
588 push @row, $r->{'peptide_seq'};
589 }
590
591 push @result, \@row;
592 }
593  
594 return \@result;
595 }
596  
597  
598 =head2 fetch_predecessors_by_archive_id
599  
600 Arg [1] : Bio::EnsEMBL::ArchiveStableId
601 Example : none
602 Description : Retrieve a list of ArchiveStableIds that were mapped to the
603 given one. This method goes back only one level, to retrieve
604 a full predecessor history use fetch_predecessor_history, or
605 ideally fetch_history_tree_by_stable_id for the complete
606 history network.
607 Returntype : listref Bio::EnsEMBL::ArchiveStableId
608 Exceptions : none
609 Caller : Bio::EnsEMBL::ArchiveStableId->get_all_predecessors
610 Status : At Risk
611 : under development
612  
613 =cut
614  
615 sub fetch_predecessors_by_archive_id {
616 my $self = shift;
617 my $arch_id = shift;
618
619 my @result;
620
621 if( ! ( defined $arch_id->stable_id() &&
622 defined $arch_id->db_name() )) {
623 throw( "Need db_name for predecessor retrieval" );
624 }
625  
626 my $sql = qq(
627 SELECT
628 sie.old_stable_id,
629 sie.old_version,
630 sie.type,
631 m.old_db_name,
632 m.old_release,
633 m.old_assembly
634 FROM mapping_session m, stable_id_event sie
635 WHERE sie.mapping_session_id = m.mapping_session_id
636 AND sie.new_stable_id = ?
637 AND m.new_db_name = ?
638 );
639  
640 my $sth = $self->prepare($sql);
641 $sth->bind_param(1, $arch_id->stable_id, SQL_VARCHAR);
642 $sth->bind_param(2, $arch_id->db_name, SQL_VARCHAR);
643 $sth->execute();
644
645 my ($old_stable_id, $old_version, $type, $old_db_name, $old_release, $old_assembly);
646 $sth->bind_columns(\$old_stable_id, \$old_version, \$type, \$old_db_name, \$old_release, \$old_assembly);
647
648 while ($sth->fetch) {
649 if (defined $old_stable_id) {
650 my $old_arch_id = Bio::EnsEMBL::ArchiveStableId->new(
651 -stable_id => $old_stable_id,
652 -version => $old_version,
653 -db_name => $old_db_name,
654 -release => $old_release,
655 -assembly => $old_assembly,
656 -type => $type,
657 -adaptor => $self
658 );
659 push( @result, $old_arch_id );
660 }
661 }
662 $sth->finish();
663  
664 # if you didn't find any predecessors, there might be a gap in the
665 # mapping_session history (i.e. databases in mapping_session don't chain). To
666 # bridge the gap, look in the previous mapping_session for identical
667 # stable_id.version
668 unless (@result) {
669  
670 $sql = qq(
671 SELECT
672 sie.new_stable_id,
673 sie.new_version,
674 sie.type,
675 m.new_db_name,
676 m.new_release,
677 m.new_assembly
678 FROM mapping_session m, stable_id_event sie
679 WHERE sie.mapping_session_id = m.mapping_session_id
680 AND sie.new_stable_id = ?
681 AND m.new_db_name = ?
682 );
683  
684 $sth = $self->prepare($sql);
685  
686 my $curr_dbname = $arch_id->db_name;
687
688 PREV:
689 while (my $prev_dbname = $self->previous_dbname($curr_dbname)) {
690
691 $sth->bind_param(1,$arch_id->stable_id, SQL_VARCHAR);
692 $sth->bind_param(2,$prev_dbname, SQL_VARCHAR);
693 $sth->execute();
694
695 $sth->bind_columns(\$old_stable_id, \$old_version, \$type, \$old_db_name, \$old_release, \$old_assembly);
696
697 while( $sth->fetch() ) {
698 if (defined $old_stable_id) {
699 my $old_arch_id = Bio::EnsEMBL::ArchiveStableId->new(
700 -stable_id => $old_stable_id,
701 -version => $old_version,
702 -db_name => $old_db_name,
703 -release => $old_release,
704 -assembly => $old_assembly,
705 -type => $type,
706 -adaptor => $self
707 );
708 push( @result, $old_arch_id );
709  
710 last PREV;
711 }
712 }
713  
714 $curr_dbname = $prev_dbname;
715  
716 }
717
718 $sth->finish();
719 }
720  
721 return \@result;
722 }
723  
724  
725 =head2 fetch_successors_by_archive_id
726  
727 Arg [1] : Bio::EnsEMBL::ArchiveStableId
728 Example : none
729 Description : Retrieve a list of ArchiveStableIds that the given one was
730 mapped to. This method goes forward only one level, to retrieve
731 a full successor history use fetch_successor_history, or
732 ideally fetch_history_tree_by_stable_id for the complete
733 history network.
734 Returntype : listref Bio::EnsEMBL::ArchiveStableId
735 Exceptions : none
736 Caller : Bio::EnsEMBL::ArchiveStableId->get_all_successors
737 Status : At Risk
738 : under development
739  
740 =cut
741  
742 sub fetch_successors_by_archive_id {
743 my $self = shift;
744 my $arch_id = shift;
745 my @result;
746  
747
748 if( ! ( defined $arch_id->stable_id() &&
749 defined $arch_id->db_name() )) {
750 throw( "Need db_name for successor retrieval" );
751 }
752  
753 my $sql = qq(
754 SELECT
755 sie.new_stable_id,
756 sie.new_version,
757 sie.type,
758 m.new_db_name,
759 m.new_release,
760 m.new_assembly
761 FROM mapping_session m, stable_id_event sie
762 WHERE sie.mapping_session_id = m.mapping_session_id
763 AND sie.old_stable_id = ?
764 AND m.old_db_name = ?
765 );
766  
767 my $sth = $self->prepare( $sql );
768 $sth->bind_param(1,$arch_id->stable_id,SQL_VARCHAR);
769 $sth->bind_param(2,$arch_id->db_name,SQL_VARCHAR);
770 $sth->execute();
771
772 my ($new_stable_id, $new_version, $type, $new_db_name, $new_release, $new_assembly);
773 $sth->bind_columns(\$new_stable_id, \$new_version, \$type, \$new_db_name, \$new_release, \$new_assembly);
774
775 while( $sth->fetch() ) {
776 if( defined $new_stable_id ) {
777 my $new_arch_id = Bio::EnsEMBL::ArchiveStableId->new(
778 -stable_id => $new_stable_id,
779 -version => $new_version,
780 -db_name => $new_db_name,
781 -release => $new_release,
782 -assembly => $new_assembly,
783 -type => $type,
784 -adaptor => $self
785 );
786
787 push( @result, $new_arch_id );
788 }
789 }
790 $sth->finish();
791
792 # if you didn't find any successors, there might be a gap in the
793 # mapping_session history (i.e. databases in mapping_session don't chain). To
794 # bridge the gap, look in the next mapping_session for identical
795 # stable_id.version
796 unless (@result) {
797  
798 $sql = qq(
799 SELECT
800 sie.old_stable_id,
801 sie.old_version,
802 sie.type,
803 m.old_db_name,
804 m.old_release,
805 m.old_assembly
806 FROM mapping_session m, stable_id_event sie
807 WHERE sie.mapping_session_id = m.mapping_session_id
808 AND sie.old_stable_id = ?
809 AND m.old_db_name = ?
810 );
811  
812 $sth = $self->prepare($sql);
813  
814 my $curr_dbname = $arch_id->db_name;
815
816 NEXTDB:
817 while (my $next_dbname = $self->next_dbname($curr_dbname)) {
818  
819 $sth->bind_param(1, $arch_id->stable_id, SQL_VARCHAR);
820 $sth->bind_param(2, $next_dbname, SQL_VARCHAR);
821 $sth->execute();
822
823 $sth->bind_columns(\$new_stable_id, \$new_version, \$type, \$new_db_name, \$new_release, \$new_assembly);
824
825 while( $sth->fetch() ) {
826 if (defined $new_stable_id) {
827 my $new_arch_id = Bio::EnsEMBL::ArchiveStableId->new(
828 -stable_id => $new_stable_id,
829 -version => $new_version,
830 -db_name => $new_db_name,
831 -release => $new_release,
832 -assembly => $new_assembly,
833 -type => $type,
834 -adaptor => $self
835 );
836
837 push( @result, $new_arch_id );
838  
839 last NEXTDB;
840 }
841 }
842
843 $curr_dbname = $next_dbname;
844  
845 }
846  
847 $sth->finish();
848 }
849  
850 return \@result;
851 }
852  
853  
854  
855 =head2 fetch_history_tree_by_stable_id
856  
857 Arg [1] : String $stable_id - the stable ID to fetch the history tree for
858 Arg [2] : (optional) Int $num_high_scorers
859 number of mappings per stable ID allowed when filtering
860 Arg [3] : (optional) Int $max_rows
861 maximum number of stable IDs in history tree (used for
862 filtering)
863 Arg [4] : (optional) Float $time_limit
864 Optimise tree normally runs until it hits a minimised state
865 but this can take a very long time. Therefore you can
866 opt to bail out of the optimisation early. Specify the
867 time in seconds. Floating point values are supported should you
868 require sub-second limits
869 Example : my $history = $archive_adaptor->fetch_history_tree_by_stable_id(
870 'ENSG00023747897');
871 Description : Returns the history tree for a given stable ID. This will
872 include a network of all stable IDs it is related to. The
873 method will try to return a minimal (sparse) set of nodes
874 (ArchiveStableIds) and links (StableIdEvents) by removing any
875 redundant entries and consolidating mapping events so that only
876 changes are recorded.
877 Return type : Bio::EnsEMBL::StableIdHistoryTree
878 Exceptions : thrown on missing argument
879 Caller : Bio::EnsEMBL::ArchiveStableId::get_history_tree, general
880 Status : At Risk
881 : under development
882  
883 =cut
884  
885 sub fetch_history_tree_by_stable_id {
886 my ($self, $stable_id, $num_high_scorers, $max_rows, $time_limit) = @_;
887  
888 throw("Expecting a stable ID argument.") unless $stable_id;
889  
890 $num_high_scorers ||= NUM_HIGH_SCORERS;
891 $max_rows ||= MAX_ROWS;
892  
893 # using a UNION is much faster in this query than somthing like
894 # "... AND (sie.old_stable_id = ?) OR (sie.new_stable_id = ?)"
895 #
896 # SQLite uses the fully qualified column name as the key in
897 # fetchrow_hashref() when there's a UNION, hence the need to
898 # avoid table names qualifiers in the column lists.
899 my $sql = qq(
900 SELECT old_stable_id, old_version,
901 old_db_name, old_release, old_assembly,
902 new_stable_id, new_version,
903 new_db_name, new_release, new_assembly,
904 type, score
905 FROM stable_id_event sie, mapping_session ms
906 WHERE sie.mapping_session_id = ms.mapping_session_id
907 AND sie.old_stable_id = ?
908 UNION
909 SELECT old_stable_id, old_version,
910 old_db_name, old_release, old_assembly,
911 new_stable_id, new_version,
912 new_db_name, new_release, new_assembly,
913 type, score
914 FROM stable_id_event sie, mapping_session ms
915 WHERE sie.mapping_session_id = ms.mapping_session_id
916 AND sie.new_stable_id = ?
917 );
918
919 my $sth = $self->prepare($sql);
920  
921 my $history = Bio::EnsEMBL::StableIdHistoryTree->new(
922 -CURRENT_DBNAME => $self->dbc->dbname,
923 -CURRENT_RELEASE => $self->get_current_release,
924 -CURRENT_ASSEMBLY => $self->get_current_assembly,
925 );
926  
927 # remember stable IDs you need to do and those that are done. Initialise the
928 # former hash with the focus stable ID
929 my %do = ($stable_id => 1);
930 my %done;
931  
932 # while we got someting to do
933 while (my ($id) = keys(%do)) {
934  
935 # if we already have more than MAX_ROWS stable IDs in this tree, we can't
936 # build the full tree. Return undef.
937 if (scalar(keys(%done)) > $max_rows) {
938 # warning("Too many related stable IDs (".scalar(keys(%done)).") to draw a history tree.");
939 $history->is_incomplete(1);
940 $sth->finish;
941 last;
942 }
943  
944 # mark this stable ID as done
945 delete $do{$id};
946 $done{$id} = 1;
947  
948 # fetch all stable IDs related to this one from the database
949 $sth->bind_param(1, $id, SQL_VARCHAR);
950 $sth->bind_param(2, $id, SQL_VARCHAR);
951 $sth->execute;
952  
953 my @events;
954  
955 while (my $r = $sth->fetchrow_hashref) {
956
957 #
958 # create old and new ArchiveStableIds and a StableIdEvent to link them
959 # add all of these to the history tree
960 #
961 my ($old_id, $new_id);
962  
963 if ($r->{'old_stable_id'}) {
964 $old_id = Bio::EnsEMBL::ArchiveStableId->new(
965 -stable_id => $r->{'old_stable_id'},
966 -version => $r->{'old_version'},
967 -db_name => $r->{'old_db_name'},
968 -release => $r->{'old_release'},
969 -assembly => $r->{'old_assembly'},
970 -type => $r->{'type'},
971 -adaptor => $self
972 );
973 }
974
975 if ($r->{'new_stable_id'}) {
976 $new_id = Bio::EnsEMBL::ArchiveStableId->new(
977 -stable_id => $r->{'new_stable_id'},
978 -version => $r->{'new_version'},
979 -db_name => $r->{'new_db_name'},
980 -release => $r->{'new_release'},
981 -assembly => $r->{'new_assembly'},
982 -type => $r->{'type'},
983 -adaptor => $self
984 );
985 }
986  
987 my $event = Bio::EnsEMBL::StableIdEvent->new(
988 -old_id => $old_id,
989 -new_id => $new_id,
990 -score => $r->{'score'}
991 );
992  
993 push @events, $event;
994  
995 }
996  
997 # filter out low-scoring events; the number of highest scoring events
998 # returned is defined by NUM_HIGH_SCORERS
999 my @others;
1000  
1001 foreach my $event (@events) {
1002
1003 my $old_id = $event->old_ArchiveStableId;
1004 my $new_id = $event->new_ArchiveStableId;
1005
1006 # creation, deletion and mapping-to-self events are added to the history
1007 # tree directly
1008 if (!$old_id || !$new_id || ($old_id->stable_id eq $new_id->stable_id)) {
1009 $history->add_StableIdEvents($event);
1010 } else {
1011 push @others, $event;
1012 }
1013
1014 }
1015  
1016 #if (scalar(@others) > $num_high_scorers) {
1017 # warn "Filtering ".(scalar(@others) - $num_high_scorers).
1018 # " low-scoring events.\n";
1019 #}
1020  
1021 my $k = 0;
1022 foreach my $event (sort { $b->score <=> $a->score } @others) {
1023 $history->add_StableIdEvents($event);
1024
1025 # mark stable IDs as todo if appropriate
1026 $do{$event->old_ArchiveStableId->stable_id} = 1
1027 unless $done{$event->old_ArchiveStableId->stable_id};
1028 $do{$event->new_ArchiveStableId->stable_id} = 1
1029 unless $done{$event->new_ArchiveStableId->stable_id};
1030
1031 last if (++$k == $num_high_scorers);
1032 }
1033
1034 }
1035  
1036 $sth->finish;
1037
1038 # try to consolidate the tree (remove redundant nodes, bridge gaps)
1039 $history->consolidate_tree;
1040  
1041 # now add ArchiveStableIds for current Ids not found in the archive
1042 $self->add_all_current_to_history($history);
1043
1044 # calculate grid coordinates for the sorted tree; this will also try to
1045 # untangle the tree
1046 $history->calculate_coords($time_limit);
1047
1048 return $history;
1049 }
1050  
1051  
1052 =head2 add_all_current_to_history
1053  
1054 Arg[1] : Bio::EnsEMBL::StableIdHistoryTree $history -
1055 the StableIdHistoryTree object to add the current IDs to
1056 Description : This method adds the current versions of all stable IDs found
1057 in a StableIdHistoryTree object to the tree, by creating
1058 appropriate Events for the stable IDs found in the *_stable_id
1059 tables. This is a helper method for
1060 fetch_history_tree_by_stable_id(), see there for more
1061 documentation.
1062 Return type : none (passed-in object is manipulated)
1063 Exceptions : thrown on missing or wrong argument
1064 Caller : internal
1065 Status : At Risk
1066 : under development
1067  
1068 =cut
1069  
1070 sub add_all_current_to_history {
1071 my $self = shift;
1072 my $history = shift;
1073  
1074 unless ($history and $history->isa('Bio::EnsEMBL::StableIdHistoryTree')) {
1075 throw("Need a Bio::EnsEMBL::StableIdHistoryTree.");
1076 }
1077  
1078 my @ids = @{ $history->get_unique_stable_ids };
1079 my $id_string = join("', '", @ids);
1080
1081 my $tmp_id = Bio::EnsEMBL::ArchiveStableId->new(-stable_id => $ids[0]);
1082 my $type = lc($self->_resolve_type($tmp_id));
1083 return unless ($type);
1084  
1085 # get current stable IDs from db
1086 my $sql = qq(
1087 SELECT stable_id, version FROM ${type}
1088 WHERE stable_id IN ('$id_string')
1089 );
1090 my $sth = $self->prepare($sql);
1091 $sth->execute;
1092  
1093 while (my ($stable_id, $version) = $sth->fetchrow_array) {
1094  
1095 my $new_id = Bio::EnsEMBL::ArchiveStableId->new(
1096 -stable_id => $stable_id,
1097 -version => $version,
1098 -current_version => $version,
1099 -db_name => $self->dbc->dbname,
1100 -release => $self->get_current_release,
1101 -assembly => $self->get_current_assembly,
1102 -type => $type,
1103 -adaptor => $self
1104 );
1105  
1106 my $event = $history->get_latest_StableIdEvent($new_id);
1107 next unless ($event);
1108  
1109 if ($event->old_ArchiveStableId and
1110 $event->old_ArchiveStableId->stable_id eq $stable_id) {
1111
1112 # latest event was a self event
1113 # update it with current stable ID and add to tree
1114 $event->new_ArchiveStableId($new_id);
1115  
1116 } else {
1117  
1118 # latest event was a non-self event
1119 # create a new event where the old_id is the new_id from latest
1120 my $new_event = Bio::EnsEMBL::StableIdEvent->new(
1121 -old_id => $event->new_ArchiveStableId,
1122 -new_id => $new_id,
1123 -score => $event->score,
1124 );
1125 $history->add_StableIdEvents($new_event);
1126 }
1127
1128 }
1129  
1130 # refresh node cache
1131 $history->flush_ArchiveStableIds;
1132 $history->add_ArchiveStableIds_for_events;
1133 }
1134  
1135  
1136 =head2 fetch_successor_history
1137  
1138 Arg [1] : Bio::EnsEMBL::ArchiveStableId $arch_id
1139 Example : none
1140 Description : Gives back a list of archive stable ids which are successors in
1141 the stable_id_event tree of the given stable_id. Might well be
1142 empty.
1143
1144 This method is valid, but in most cases you will rather
1145 want to use fetch_history_tree_by_stable_id().
1146 Returntype : listref Bio::EnsEMBL::ArchiveStableId
1147 Since every ArchiveStableId knows about it's successors, this is
1148 a linked tree.
1149 Exceptions : none
1150 Caller : webcode for archive
1151 Status : At Risk
1152 : under development
1153  
1154 =cut
1155  
1156 sub fetch_successor_history {
1157 my $self = shift;
1158 my $arch_id = shift;
1159  
1160 my $current_db_name = $self->list_dbnames->[0];
1161 my $dbname = $arch_id->db_name;
1162  
1163 if ($dbname eq $current_db_name) {
1164 return [$arch_id];
1165 }
1166  
1167 my $old = [];
1168 my @result = ();
1169
1170 push @$old, $arch_id;
1171  
1172 while ($dbname ne $current_db_name) {
1173 my $new = [];
1174 while (my $asi = (shift @$old)) {
1175 push @$new, @{ $asi->get_all_successors };
1176 }
1177  
1178 if (@$new) {
1179 $dbname = $new->[0]->db_name;
1180 } else {
1181 last;
1182 }
1183  
1184 # filter duplicates
1185 my %unique = map { join(":", $_->stable_id, $_->version, $_->release) =>
1186 $_ } @$new;
1187 @$new = values %unique;
1188
1189 @$old = @$new;
1190 push @result, @$new;
1191 }
1192  
1193 return \@result;
1194 }
1195  
1196  
1197 =head2 fetch_predecessor_history
1198  
1199 Arg [1] : Bio::EnsEMBL::ArchiveStableId $arch_id
1200 Example : none
1201 Description : Gives back a list of archive stable ids which are predecessors
1202 in the stable_id_event tree of the given stable_id. Might well
1203 be empty.
1204
1205 This method is valid, but in most cases you will rather
1206 want to use fetch_history_tree_by_stable_id().
1207 Returntype : listref Bio::EnsEMBL::ArchiveStableId
1208 Since every ArchiveStableId knows about it's successors, this is
1209 a linked tree.
1210 Exceptions : none
1211 Caller : webcode for archive
1212 Status : At Risk
1213 : under development
1214  
1215 =cut
1216  
1217 sub fetch_predecessor_history {
1218 my $self = shift;
1219 my $arch_id = shift;
1220  
1221 my $oldest_db_name = $self->list_dbnames->[-1];
1222 my $dbname = $arch_id->db_name;
1223  
1224 if ($dbname eq $oldest_db_name) {
1225 return [$arch_id];
1226 }
1227  
1228 my $old = [];
1229 my @result = ();
1230  
1231 push @$old, $arch_id;
1232  
1233 while ($dbname ne $oldest_db_name) {
1234 my $new = [];
1235 while (my $asi = (shift @$old)) {
1236 push @$new, @{ $asi->get_all_predecessors };
1237 }
1238  
1239 if( @$new ) {
1240 $dbname = $new->[0]->db_name;
1241 } else {
1242 last;
1243 }
1244
1245 # filter duplicates
1246 my %unique = map { join(":", $_->stable_id, $_->version, $_->release) =>
1247 $_ } @$new;
1248 @$new = values %unique;
1249
1250 @$old = @$new;
1251 push @result, @$new;
1252 }
1253  
1254 return \@result;
1255 }
1256  
1257  
1258 =head2 fetch_stable_id_event
1259  
1260 Arg [1] : Bio::EnsEMBL::ArchiveStableId $arch_id
1261 Arg [2] : stable_id
1262 Example : my $archive = $archive_stable_id_adaptor->fetch_by_stable_id($id);
1263 my $event = $archive_stable_id_adaptor($archive, $id2);
1264 Description : Gives back the event that links an archive stable id
1265 to a specific stable id
1266
1267 Returntype : Bio::EnsEMBL::StableIdEvent
1268 Undef if no event was found
1269 Exceptions : none
1270 Caller : general
1271 Status : At Risk
1272 : under development
1273  
1274 =cut
1275  
1276  
1277 sub fetch_stable_id_event {
1278 my $self = shift;
1279 my $arch_id = shift;
1280 my $stable_id = shift;
1281  
1282 my $event;
1283  
1284 my $sql = qq(
1285 SELECT sie.old_stable_id, sie.old_version, sie.new_stable_id, sie.new_version, sie.type, sie.score,
1286 ms.old_db_name, ms.new_db_name, ms.old_release, ms.new_release, ms.old_assembly, ms.new_assembly
1287 FROM stable_id_event sie, mapping_session ms
1288 WHERE ms.mapping_session_id = sie.mapping_session_id
1289 AND (old_stable_id = ? AND ms.old_db_name = ? AND old_release = ? AND old_assembly = ? AND new_stable_id = ?)
1290 OR (new_stable_id = ? AND ms.new_db_name = ? AND new_release = ? AND new_assembly = ? AND old_stable_id = ?)
1291 );
1292  
1293 my $sth = $self->prepare($sql);
1294 $sth->bind_param(1, $arch_id->stable_id, SQL_VARCHAR);
1295 $sth->bind_param(2, $arch_id->db_name, SQL_VARCHAR);
1296 $sth->bind_param(3, $arch_id->release, SQL_INTEGER);
1297 $sth->bind_param(4, $arch_id->assembly, SQL_VARCHAR);
1298 $sth->bind_param(5, $stable_id, SQL_VARCHAR);
1299 $sth->bind_param(6, $arch_id->stable_id, SQL_VARCHAR);
1300 $sth->bind_param(7, $arch_id->db_name, SQL_VARCHAR);
1301 $sth->bind_param(8, $arch_id->release, SQL_INTEGER);
1302 $sth->bind_param(9, $arch_id->assembly, SQL_VARCHAR);
1303 $sth->bind_param(10, $stable_id, SQL_VARCHAR);
1304 $sth->execute();
1305  
1306 my ($old_stable_id, $old_version, $new_stable_id, $new_version, $type, $score);
1307 my ($old_db_name, $new_db_name, $old_release, $new_release, $old_assembly, $new_assembly);
1308 $sth->bind_columns(\$old_stable_id, \$old_version, \$new_stable_id, \$new_version, \$type, \$score,
1309 \$old_db_name, \$new_db_name, \$old_release, \$new_release, \$old_assembly, \$new_assembly);
1310  
1311 while ($sth->fetch) {
1312 if ($new_stable_id eq $stable_id) {
1313  
1314 my $alt_id = Bio::EnsEMBL::ArchiveStableId->new(
1315 -stable_id => $new_stable_id,
1316 -version => $new_version,
1317 -db_name => $new_db_name,
1318 -release => $new_release,
1319 -assembly => $new_assembly,
1320 -type => $type,
1321 -adaptor => $self
1322 );
1323  
1324 $event = Bio::EnsEMBL::StableIdEvent->new(
1325 -old_id => $arch_id,
1326 -new_id => $alt_id,
1327 -score => $score
1328 );
1329  
1330 } elsif ($old_stable_id eq $stable_id) {
1331  
1332 my $alt_id = Bio::EnsEMBL::ArchiveStableId->new(
1333 -stable_id => $old_stable_id,
1334 -version => $old_version,
1335 -db_name => $old_db_name,
1336 -release => $old_release,
1337 -assembly => $old_assembly,
1338 -type => $type,
1339 -adaptor => $self
1340 );
1341  
1342 $event = Bio::EnsEMBL::StableIdEvent->new(
1343 -old_id => $alt_id,
1344 -new_id => $arch_id,
1345 -score => $score
1346 );
1347  
1348 }
1349 }
1350 $sth->finish();
1351  
1352 return $event;
1353 }
1354  
1355  
1356 =head2 list_dbnames
1357  
1358 Args : none
1359 Example : none
1360 Description : A list of available database names from the latest (current) to
1361 the oldest (ordered).
1362 Returntype : listref of strings
1363 Exceptions : none
1364 Caller : general
1365 Status : At Risk
1366 : under development
1367  
1368 =cut
1369  
1370 sub list_dbnames {
1371 my $self = shift;
1372
1373 if( ! defined $self->{'dbnames'} ) {
1374  
1375 my $sql = qq(
1376 SELECT old_db_name, new_db_name
1377 FROM mapping_session
1378 ORDER BY created DESC
1379 );
1380 my $sth = $self->prepare( $sql );
1381 $sth->execute();
1382 my ( $old_db_name, $new_db_name );
1383
1384 my @dbnames = ();
1385 my %seen;
1386  
1387 $sth->bind_columns( \$old_db_name, \$new_db_name );
1388  
1389 while( $sth->fetch() ) {
1390 # this code now can deal with non-chaining mapping sessions
1391 push(@{ $self->{'dbnames'} }, $new_db_name) unless ($seen{$new_db_name});
1392 $seen{$new_db_name} = 1;
1393  
1394 push(@{ $self->{'dbnames'} }, $old_db_name) unless ($seen{$old_db_name});
1395 $seen{$old_db_name} = 1;
1396 }
1397  
1398 $sth->finish();
1399
1400 }
1401  
1402 return $self->{'dbnames'};
1403 }
1404  
1405  
1406 =head2 previous_dbname
1407  
1408 Arg[1] : String $dbname - focus db name
1409 Example : my $prev_db = $self->previous_dbname($curr_db);
1410 Description : Returns the name of the next oldest database which has mapping
1411 session information.
1412 Return type : String (or undef if not available)
1413 Exceptions : none
1414 Caller : general
1415 Status : At Risk
1416  
1417 =cut
1418  
1419 sub previous_dbname {
1420 my $self = shift;
1421 my $dbname = shift;
1422  
1423 my $curr_idx = $self->_dbname_index($dbname);
1424 my @dbnames = @{ $self->list_dbnames };
1425  
1426 if ($curr_idx == @dbnames) {
1427 # this is the oldest dbname, so no previous one available
1428 return undef;
1429 } else {
1430 return $dbnames[$curr_idx+1];
1431 }
1432 }
1433  
1434  
1435 =head2 next_dbname
1436  
1437 Arg[1] : String $dbname - focus db name
1438 Example : my $prev_db = $self->next_dbname($curr_db);
1439 Description : Returns the name of the next newest database which has mapping
1440 session information.
1441 Return type : String (or undef if not available)
1442 Exceptions : none
1443 Caller : general
1444 Status : At Risk
1445  
1446 =cut
1447  
1448 sub next_dbname {
1449 my $self = shift;
1450 my $dbname = shift;
1451  
1452 my $curr_idx = $self->_dbname_index($dbname);
1453 my @dbnames = @{ $self->list_dbnames };
1454  
1455 if ($curr_idx == 0) {
1456 # this is the latest dbname, so no next one available
1457 return undef;
1458 } else {
1459 return $dbnames[$curr_idx-1];
1460 }
1461 }
1462  
1463  
1464 #
1465 # helper method to return the array index of a database in the ordered list of
1466 # available databases (as returned by list_dbnames()
1467 #
1468 sub _dbname_index {
1469 my $self = shift;
1470 my $dbname = shift;
1471  
1472 my @dbnames = @{ $self->list_dbnames };
1473  
1474 for (my $i = 0; $i < @dbnames; $i++) {
1475 if ($dbnames[$i] eq $dbname) {
1476 return $i;
1477 }
1478 }
1479 }
1480  
1481  
1482 =head2 get_peptide
1483  
1484 Arg [1] : Bio::EnsEMBL::ArchiveStableId $arch_id
1485 Example : none
1486 Description : Retrieves the peptide string for given ArchiveStableId. If its
1487 not a peptide or not in the database returns undef.
1488 Returntype : string or undef
1489 Exceptions : none
1490 Caller : Bio::EnsEMBL::ArchiveStableId->get_peptide, general
1491 Status : At Risk
1492 : under development
1493  
1494 =cut
1495  
1496 sub get_peptide {
1497 my $self = shift;
1498 my $arch_id = shift;
1499  
1500 if ( lc( $arch_id->type() ) ne 'translation' ) {
1501 return undef;
1502 }
1503  
1504 my $sql = qq(
1505 SELECT pa.peptide_seq
1506 FROM peptide_archive pa, gene_archive ga
1507 WHERE ga.translation_stable_id = ?
1508 AND ga.translation_version = ?
1509 AND ga.peptide_archive_id = pa.peptide_archive_id
1510 );
1511  
1512 my $sth = $self->prepare($sql);
1513 $sth->bind_param( 1, $arch_id->stable_id, SQL_VARCHAR );
1514 $sth->bind_param( 2, $arch_id->version, SQL_SMALLINT );
1515 $sth->execute();
1516  
1517 my ($peptide_seq) = $sth->fetchrow_array();
1518 $sth->finish();
1519  
1520 return $peptide_seq;
1521 } ## end sub get_peptide
1522  
1523  
1524 =head2 get_current_release
1525  
1526 Example : my $current_release = $archive_adaptor->get_current_release;
1527 Description : Returns the current release number (as found in the meta table).
1528 Return type : Int
1529 Exceptions : none
1530 Caller : general
1531 Status : At Risk
1532 : under development
1533  
1534 =cut
1535  
1536 sub get_current_release {
1537 my $self = shift;
1538  
1539 unless ($self->{'current_release'}) {
1540 my $mca = $self->db->get_MetaContainer;
1541 my ($release) = @{ $mca->list_value_by_key('schema_version') };
1542 $self->{'current_release'} = $release;
1543 }
1544  
1545 return $self->{'current_release'};
1546 }
1547  
1548  
1549 =head2 get_current_assembly
1550  
1551 Example : my $current_assembly = $archive_adaptor->get_current_assembly;
1552 Description : Returns the current assembly version (as found in the meta
1553 table).
1554 Return type : String
1555 Exceptions : none
1556 Caller : general
1557 Status : At Risk
1558 : under development
1559  
1560 =cut
1561  
1562 sub get_current_assembly {
1563 my $self = shift;
1564  
1565 unless ($self->{'current_assembly'}) {
1566 my $mca = $self->db->get_MetaContainer;
1567 my ($assembly) = @{ $mca->list_value_by_key('assembly.default') };
1568 $self->{'current_assembly'} = $assembly;
1569 }
1570  
1571 return $self->{'current_assembly'};
1572 }
1573  
1574  
1575 =head2 lookup_current
1576  
1577 Arg[1] : Bio::EnsEMBL::ArchiveStableId $arch_id -
1578 the stalbe ID to find the current version for
1579 Example : if ($self->lookup_version($arch_id) {
1580 $arch_id->version($arch_id->current_version);
1581 $arch_id->db_name($self->dbc->dbname);
1582 Description : Look in [gene|transcript|translation]_stable_id if you can find
1583 a current version for this stable ID. Set
1584 ArchiveStableId->current_version if found.
1585 Return type : Boolean (TRUE if current version found, else FALSE)
1586 Exceptions : none
1587 Caller : general
1588 Status : At Risk
1589 : under development
1590  
1591 =cut
1592  
1593 sub lookup_current {
1594 my $self = shift;
1595 my $arch_id = shift;
1596  
1597 my $type = lc( $arch_id->type );
1598  
1599 unless ($type) {
1600 warning("Can't lookup current version without a type.");
1601 return 0;
1602 }
1603  
1604 my $sql = qq(
1605 SELECT version FROM ${type}
1606 WHERE stable_id = ?
1607 );
1608 my $sth = $self->prepare($sql);
1609 $sth->execute( $arch_id->stable_id );
1610 my ($version) = $sth->fetchrow_array;
1611 $sth->finish;
1612  
1613 if ($version) {
1614 $arch_id->current_version($version);
1615 return 1;
1616 }
1617  
1618 # didn't find a current version
1619 return 0;
1620 } ## end sub lookup_current
1621  
1622  
1623 # infer type from stable ID format
1624 sub _resolve_type {
1625 my $self = shift;
1626 my $arch_id = shift;
1627
1628 my $stable_id = $arch_id->stable_id();
1629 my $id_type;
1630  
1631 # first, try to infer type from stable ID format
1632 #
1633 # Anopheles IDs
1634 if ($stable_id =~ /^AGAP.*/) {
1635 if ($stable_id =~ /.*-RA/) {
1636 $id_type = "Transcript";
1637 } elsif ($stable_id =~ /.*-PA/) {
1638 $id_type = "Translation";
1639 } else {
1640 $id_type = "Gene";
1641 }
1642  
1643 # standard Ensembl IDs
1644 } elsif ($stable_id =~ /.*G\d+(\.\d+)?$/) {
1645 $id_type = "Gene";
1646 } elsif ($stable_id =~ /.*T\d+(\.\d+)?$/) {
1647 $id_type = "Transcript";
1648 } elsif ($stable_id =~ /.*P\d+(\.\d+)?$/) {
1649 $id_type = "Translation";
1650 } elsif ($stable_id =~ /.*E\d+(\.\d+)?$/) {
1651 $id_type = "Exon";
1652  
1653 # if guessing fails, look in db
1654 } else {
1655 my $sql = qq(
1656 SELECT type from stable_id_event
1657 WHERE old_stable_id = ?
1658 OR new_stable_id = ?
1659 );
1660 my $sth = $self->prepare($sql);
1661 $sth->execute($stable_id, $stable_id);
1662 ($id_type) = $sth->fetchrow_array;
1663 $sth->finish;
1664 }
1665  
1666 warning("Couldn't resolve stable ID type.") unless ($id_type);
1667
1668 $arch_id->type($id_type);
1669 }
1670  
1671  
1672 1;
1673