3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
33 Bio::EnsEMBL::ArchiveStableIdAdaptor
37 my $registry =
"Bio::EnsEMBL::Registry";
39 my $archiveStableIdAdaptor =
40 $registry->get_adaptor(
'Human',
'Core',
'ArchiveStableId' );
42 my $stable_id =
'ENSG00000068990';
44 my $arch_id = $archiveStableIdAdaptor->fetch_by_stable_id($stable_id);
46 print(
"Latest incarnation of this stable ID:\n");
47 printf(
" Stable ID: %s.%d\n",
48 $arch_id->stable_id(), $arch_id->version() );
50 . $arch_id->release() .
" ("
51 . $arch_id->assembly() .
", "
55 print
"\nStable ID history:\n\n";
58 $archiveStableIdAdaptor->fetch_history_tree_by_stable_id(
61 foreach my $a ( @{ $history->get_all_ArchiveStableIds } ) {
62 printf(
" Stable ID: %s.%d\n", $a->stable_id(), $a->version() );
64 . $a->release() .
" ("
65 . $a->assembly() .
", "
80 tables inside the core database.
82 This whole module has a status of At Risk as it is under development.
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
101 =head1 RELATED MODULES
111 package Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor;
115 no warnings qw(uninitialized);
125 use constant MAX_ROWS => 30;
126 use constant NUM_HIGH_SCORERS => 20;
129 =head2 fetch_by_stable_id
131 Arg [1] :
string $stable_id
132 Arg [2] : (optional)
string $type
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
146 sub fetch_by_stable_id {
148 my $stable_id = shift;
150 my $arch_id = $self->_fetch_by_stable_id($stable_id, @_);
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),
165 =head2 _fetch_by_stable_id
167 Arg [1] :
string $stable_id
168 Arg [2] : (optional)
string $type
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.
181 sub _fetch_by_stable_id {
183 my $stable_id = shift;
186 -stable_id => $stable_id,
190 @_ ? $arch_id->
type(shift) : $self->_resolve_type($arch_id);
192 if ($self->lookup_current($arch_id)) {
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);
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'})]}'" :
'';
206 my $r = $self->_fetch_archive_id($stable_id, $extra_sql, $extra_sql);
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'});
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'});
224 $arch_id->type(ucfirst(lc($r->{
'type'})));
227 if (! defined $arch_id->db_name) {
228 # couldn't find stable ID in archive or current db
232 $arch_id->is_latest(1);
238 =head2 fetch_by_stable_id_version
240 Arg [1] :
string $stable_id
241 Arg [2] :
int $version
243 Description : Retrieve an ArchiveStableId with given version and stable ID.
252 sub fetch_by_stable_id_version {
254 my $stable_id = shift;
258 -stable_id => $stable_id,
259 -version => $version,
263 if ($version !~ /^[0-9]{1,5}$/) {
264 throw(
"$version is not valid, should be a small int");
267 @_ ? $arch_id->
type(shift) : $self->_resolve_type($arch_id);
269 if ($self->lookup_current($arch_id) && $arch_id->is_current) {
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);
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);
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'});
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'});
298 $arch_id->type(ucfirst(lc($r->{
'type'})));
301 if (! defined $arch_id->db_name) {
302 # couldn't find stable ID version in archive or current release
310 =head2 fetch_by_stable_id_dbname
312 Arg [1] :
string $stable_id
313 Arg [2] :
string $db_name
315 Description : Create an ArchiveStableId from given arguments.
324 sub fetch_by_stable_id_dbname {
326 my $stable_id = shift;
330 -stable_id => $stable_id,
331 -db_name => $db_name,
335 @_ ? $arch_id->
type(shift) : $self->_resolve_type($arch_id);
337 if ($self->lookup_current($arch_id) and $db_name eq $self->dbc->dbname) {
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);
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);
353 if ($r->{
'new_stable_id'} and $r->{
'new_stable_id'} eq $stable_id
354 and $r->{
'new_db_name'} eq $db_name) {
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'});
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'});
369 $arch_id->type(ucfirst(lc($r->{
'type'})));
372 if (! defined $arch_id->version ) {
373 # couldn't find stable ID version in archive or current release
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
386 sub _fetch_archive_id {
388 my $stable_id = shift;
389 my $extra_sql1 = shift;
390 my $extra_sql2 = shift;
392 # using a UNION is much faster in this query than somthing like
393 # "... AND (sie.old_stable_id = ? OR sie.new_stable_id = ?)"
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 = ?
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 = ?
404 ORDER BY created DESC, score DESC
408 my $sth = $self->prepare($sql);
409 $sth->execute($stable_id,$stable_id);
410 my $r = $sth->fetchrow_hashref;
417 =head2 fetch_all_by_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
428 See also fetch_associated_archived() for a different approach to
430 Returntype : listref
Bio::
EnsEMBL::ArchiveStableId
432 Caller :
Bio::
EnsEMBL::ArchiveStableId->get_all_gene_archive_ids,
433 get_all_transcript_archive_ids, get_all_translation_archive_ids
439 sub fetch_all_by_archive_id {
441 my $archive_id = shift;
442 my $return_type = shift;
445 my $lc_self_type = lc($archive_id->type);
446 my $lc_return_type = lc($return_type);
450 ga.${lc_return_type}_stable_id,
451 ga.${lc_return_type}_version,
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
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);
466 my ($stable_id, $version, $db_name, $release, $assembly);
467 $sth->bind_columns(\$stable_id, \$version, \$db_name, \$release, \$assembly);
469 while ($sth->fetch) {
471 -stable_id => $stable_id,
472 -version => $version,
473 -db_name => $db_name,
474 -release => $release,
475 -assembly => $assembly,
476 -type => $return_type,
480 push( @result, $new_arch_id );
488 =head2 fetch_associated_archived
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
499 (optional) ArchiveStableId archived translation
500 (optional) peptide sequence
501 Exceptions : thrown on missing or wrong argument
502 thrown
if ArchiveStableID has no type
509 sub fetch_associated_archived {
513 throw(
"Need a Bio::EnsEMBL::ArchiveStableId") unless ($arch_id
514 and ref($arch_id) and $arch_id->isa(
'Bio::EnsEMBL::ArchiveStableId'));
516 my $type = $arch_id->type();
518 if ( !defined($type) ) {
519 throw(
"Can't deduce ArchiveStableId type.");
525 SELECT ga.gene_stable_id,
527 ga.transcript_stable_id,
528 ga.transcript_version,
529 ga.translation_stable_id,
530 ga.translation_version,
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 = ?
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);
550 while (my $r = $sth->fetchrow_hashref) {
554 # create ArchiveStableIds genes, transcripts and translations
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'},
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',
575 if ($r->{
'translation_stable_id'}) {
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',
586 # push peptide sequence onto result list
587 push @row, $r->{
'peptide_seq'};
597 =head2 fetch_predecessors_by_archive_id
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
614 sub fetch_predecessors_by_archive_id {
620 if( ! ( defined $arch_id->stable_id() &&
621 defined $arch_id->db_name() )) {
622 throw(
"Need db_name for predecessor retrieval" );
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 = ?
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);
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);
647 while ($sth->fetch) {
648 if (defined $old_stable_id) {
650 -stable_id => $old_stable_id,
651 -version => $old_version,
652 -db_name => $old_db_name,
653 -release => $old_release,
654 -assembly => $old_assembly,
658 push( @result, $old_arch_id );
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
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 = ?
683 $sth = $self->prepare($sql);
685 my $curr_dbname = $arch_id->db_name;
688 while (my $prev_dbname = $self->previous_dbname($curr_dbname)) {
690 $sth->bind_param(1,$arch_id->stable_id, SQL_VARCHAR);
691 $sth->bind_param(2,$prev_dbname, SQL_VARCHAR);
694 $sth->bind_columns(\$old_stable_id, \$old_version, \$type, \$old_db_name, \$old_release, \$old_assembly);
696 while( $sth->fetch() ) {
697 if (defined $old_stable_id) {
699 -stable_id => $old_stable_id,
700 -version => $old_version,
701 -db_name => $old_db_name,
702 -release => $old_release,
703 -assembly => $old_assembly,
707 push( @result, $old_arch_id );
713 $curr_dbname = $prev_dbname;
724 =head2 fetch_successors_by_archive_id
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
741 sub fetch_successors_by_archive_id {
747 if( ! ( defined $arch_id->stable_id() &&
748 defined $arch_id->db_name() )) {
749 throw(
"Need db_name for successor retrieval" );
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 = ?
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);
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);
774 while( $sth->fetch() ) {
775 if( defined $new_stable_id ) {
777 -stable_id => $new_stable_id,
778 -version => $new_version,
779 -db_name => $new_db_name,
780 -release => $new_release,
781 -assembly => $new_assembly,
786 push( @result, $new_arch_id );
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
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 = ?
811 $sth = $self->prepare($sql);
813 my $curr_dbname = $arch_id->db_name;
816 while (my $next_dbname = $self->next_dbname($curr_dbname)) {
818 $sth->bind_param(1, $arch_id->stable_id, SQL_VARCHAR);
819 $sth->bind_param(2, $next_dbname, SQL_VARCHAR);
822 $sth->bind_columns(\$new_stable_id, \$new_version, \$type, \$new_db_name, \$new_release, \$new_assembly);
824 while( $sth->fetch() ) {
825 if (defined $new_stable_id) {
827 -stable_id => $new_stable_id,
828 -version => $new_version,
829 -db_name => $new_db_name,
830 -release => $new_release,
831 -assembly => $new_assembly,
836 push( @result, $new_arch_id );
842 $curr_dbname = $next_dbname;
854 =head2 fetch_history_tree_by_stable_id
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
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(
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.
877 Exceptions : thrown on missing argument
884 sub fetch_history_tree_by_stable_id {
885 my ($self, $stable_id, $num_high_scorers, $max_rows, $time_limit) = @_;
887 throw(
"Expecting a stable ID argument.") unless $stable_id;
889 $num_high_scorers ||= NUM_HIGH_SCORERS;
890 $max_rows ||= MAX_ROWS;
892 # using a UNION is much faster in this query than somthing like
893 # "... AND (sie.old_stable_id = ?) OR (sie.new_stable_id = ?)"
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.
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,
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 = ?
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,
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 = ?
918 my $sth = $self->prepare($sql);
921 -CURRENT_DBNAME => $self->dbc->dbname,
922 -CURRENT_RELEASE => $self->get_current_release,
923 -CURRENT_ASSEMBLY => $self->get_current_assembly,
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);
931 # while we got someting to do
932 while (my ($id) = keys(%
do)) {
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.");
943 # mark this stable ID as done
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);
954 while (my $r = $sth->fetchrow_hashref) {
957 # create old and new ArchiveStableIds and a StableIdEvent to link them
958 # add all of these to the history tree
960 my ($old_id, $new_id);
962 if ($r->{
'old_stable_id'}) {
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'},
974 if ($r->{
'new_stable_id'}) {
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'},
989 -score => $r->{
'score'}
992 push @events, $event;
996 # filter out low-scoring events; the number of highest scoring events
997 # returned is defined by NUM_HIGH_SCORERS
1000 foreach my $event (@events) {
1003 my $new_id = $event->new_ArchiveStableId;
1005 # creation, deletion and mapping-to-self events are added to the history
1007 if (!$old_id || !$new_id || ($old_id->stable_id eq $new_id->stable_id)) {
1008 $history->add_StableIdEvents($event);
1010 push @others, $event;
1015 #if (scalar(@others) > $num_high_scorers) {
1016 # warn "Filtering ".(scalar(@others) - $num_high_scorers).
1017 # " low-scoring events.\n";
1021 foreach my $event (sort { $b->score <=> $a->score } @others) {
1022 $history->add_StableIdEvents($event);
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};
1030 last
if (++$k == $num_high_scorers);
1037 # try to consolidate the tree (remove redundant nodes, bridge gaps)
1038 $history->consolidate_tree;
1040 # now add ArchiveStableIds for current Ids not found in the archive
1041 $self->add_all_current_to_history($history);
1043 # calculate grid coordinates for the sorted tree; this will also try to
1045 $history->calculate_coords($time_limit);
1051 =head2 add_all_current_to_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
1061 Return type : none (passed-in
object is manipulated)
1062 Exceptions : thrown on missing or wrong argument
1069 sub add_all_current_to_history {
1071 my $history = shift;
1073 unless ($history and $history->isa(
'Bio::EnsEMBL::StableIdHistoryTree')) {
1074 throw(
"Need a Bio::EnsEMBL::StableIdHistoryTree.");
1078 my $id_string = join(
"', '", @ids);
1081 my $type = lc($self->_resolve_type($tmp_id));
1082 return unless ($type);
1084 # get current stable IDs from db
1086 SELECT stable_id, version FROM ${type}
1087 WHERE stable_id IN (
'$id_string')
1089 my $sth = $self->prepare($sql);
1092 while (my ($stable_id, $version) = $sth->fetchrow_array) {
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,
1105 my $event = $history->get_latest_StableIdEvent($new_id);
1106 next unless ($event);
1108 if ($event->old_ArchiveStableId and
1109 $event->old_ArchiveStableId->stable_id eq $stable_id) {
1111 # latest event was a self event
1112 # update it with current stable ID and add to tree
1113 $event->new_ArchiveStableId($new_id);
1117 # latest event was a non-self event
1118 # create a new event where the old_id is the new_id from latest
1120 -old_id => $event->new_ArchiveStableId,
1122 -score => $event->score,
1124 $history->add_StableIdEvents($new_event);
1129 # refresh node cache
1130 $history->flush_ArchiveStableIds;
1131 $history->add_ArchiveStableIds_for_events;
1135 =head2 fetch_successor_history
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
1143 This method is valid, but in most cases you will rather
1144 want to use fetch_history_tree_by_stable_id().
1146 Since every ArchiveStableId knows
about it
's successors, this is
1149 Caller : webcode for archive
1155 sub fetch_successor_history {
1157 my $arch_id = shift;
1159 my $current_db_name = $self->list_dbnames->[0];
1160 my $dbname = $arch_id->db_name;
1162 if ($dbname eq $current_db_name) {
1169 push @$old, $arch_id;
1171 while ($dbname ne $current_db_name) {
1173 while (my $asi = (shift @$old)) {
1174 push @$new, @{ $asi->get_all_successors };
1178 $dbname = $new->[0]->db_name;
1184 my %unique = map { join(":", $_->stable_id, $_->version, $_->release) =>
1186 @$new = values %unique;
1189 push @result, @$new;
1196 =head2 fetch_predecessor_history
1198 Arg [1] : Bio::EnsEMBL::ArchiveStableId $arch_id
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
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
1216 sub fetch_predecessor_history {
1218 my $arch_id = shift;
1220 my $oldest_db_name = $self->list_dbnames->[-1];
1221 my $dbname = $arch_id->db_name;
1223 if ($dbname eq $oldest_db_name) {
1230 push @$old, $arch_id;
1232 while ($dbname ne $oldest_db_name) {
1234 while (my $asi = (shift @$old)) {
1235 push @$new, @{ $asi->get_all_predecessors };
1239 $dbname = $new->[0]->db_name;
1245 my %unique =
map { join(
":", $_->stable_id, $_->version, $_->release) =>
1247 @$new = values %unique;
1250 push @result, @$new;
1257 =head2 fetch_stable_id_event
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
1267 Undef
if no
event was found
1276 sub fetch_stable_id_event {
1278 my $arch_id = shift;
1279 my $stable_id = shift;
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 = ?)
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);
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);
1310 while ($sth->fetch) {
1311 if ($new_stable_id eq $stable_id) {
1314 -stable_id => $new_stable_id,
1315 -version => $new_version,
1316 -db_name => $new_db_name,
1317 -release => $new_release,
1318 -assembly => $new_assembly,
1324 -old_id => $arch_id,
1329 } elsif ($old_stable_id eq $stable_id) {
1332 -stable_id => $old_stable_id,
1333 -version => $old_version,
1334 -db_name => $old_db_name,
1335 -release => $old_release,
1336 -assembly => $old_assembly,
1343 -new_id => $arch_id,
1359 Description : A list of available database names from the latest (current) to
1360 the oldest (ordered).
1361 Returntype : listref of strings
1372 if( ! defined $self->{
'dbnames'} ) {
1375 SELECT old_db_name, new_db_name
1376 FROM mapping_session
1377 ORDER BY created DESC
1379 my $sth = $self->prepare( $sql );
1381 my ( $old_db_name, $new_db_name );
1386 $sth->bind_columns( \$old_db_name, \$new_db_name );
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;
1393 push(@{ $self->{
'dbnames'} }, $old_db_name) unless ($seen{$old_db_name});
1394 $seen{$old_db_name} = 1;
1401 return $self->{
'dbnames'};
1405 =head2 previous_dbname
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)
1418 sub previous_dbname {
1422 my $curr_idx = $self->_dbname_index($dbname);
1423 my @dbnames = @{ $self->list_dbnames };
1425 if ($curr_idx == @dbnames) {
1426 # this is the oldest dbname, so no previous one available
1429 return $dbnames[$curr_idx+1];
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)
1451 my $curr_idx = $self->_dbname_index($dbname);
1452 my @dbnames = @{ $self->list_dbnames };
1454 if ($curr_idx == 0) {
1455 # this is the latest dbname, so no next one available
1458 return $dbnames[$curr_idx-1];
1464 # helper method to return the array index of a database in the ordered list of
1465 # available databases (as returned by list_dbnames()
1471 my @dbnames = @{ $self->list_dbnames };
1473 for (my $i = 0; $i < @dbnames; $i++) {
1474 if ($dbnames[$i] eq $dbname) {
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
1497 my $arch_id = shift;
1499 if ( lc( $arch_id->type() ) ne
'translation' ) {
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
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 );
1516 my ($peptide_seq) = $sth->fetchrow_array();
1519 return $peptide_seq;
1520 } ## end sub get_peptide
1523 =head2 get_current_release
1525 Example : my $current_release = $archive_adaptor->get_current_release;
1526 Description : Returns the current release number (as found in the meta table).
1535 sub get_current_release {
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;
1544 return $self->{
'current_release'};
1548 =head2 get_current_assembly
1550 Example : my $current_assembly = $archive_adaptor->get_current_assembly;
1551 Description : Returns the current assembly version (as found in the meta
1553 Return type : String
1561 sub get_current_assembly {
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;
1570 return $self->{
'current_assembly'};
1574 =head2 lookup_current
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)
1592 sub lookup_current {
1594 my $arch_id = shift;
1596 my $type = lc( $arch_id->type );
1599 warning(
"Can't lookup current version without a type.");
1604 SELECT version FROM ${type}
1607 my $sth = $self->prepare($sql);
1608 $sth->execute( $arch_id->stable_id );
1609 my ($version) = $sth->fetchrow_array;
1613 $arch_id->current_version($version);
1617 # didn
't find a current version
1619 } ## end sub lookup_current
1622 # infer type from stable ID format
1625 my $arch_id = shift;
1627 my $stable_id = $arch_id->stable_id();
1630 # first, try to infer type from stable ID format
1633 if ($stable_id =~ /^AGAP.*/) {
1634 if ($stable_id =~ /.*-RA/) {
1635 $id_type = "Transcript";
1636 } elsif ($stable_id =~ /.*-PA/) {
1637 $id_type = "Translation";
1642 # standard Ensembl IDs
1643 } elsif ($stable_id =~ /.*G\d+(\.\d+)?$/) {
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+)?$/) {
1652 # if guessing fails, look in db
1655 SELECT type from stable_id_event
1656 WHERE old_stable_id = ?
1657 OR new_stable_id = ?
1659 my $sth = $self->prepare($sql);
1660 $sth->execute($stable_id, $stable_id);
1661 ($id_type) = $sth->fetchrow_array;
1665 warning("Couldn't resolve stable ID type.
") unless ($id_type);
1667 $arch_id->type($id_type);