EnsEMBL/DBSQL/ArchiveStableIdAdaptor.pm untested

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