ArchiveStableIdAdaptor.pm 44.6 KB
Newer Older
1 2
=head1 LICENSE

3
  Copyright (c) 1999-2013 The European Bioinformatics Institute and
4 5 6 7 8 9 10 11 12 13
  Genome Research Limited.  All rights reserved.

  This software is distributed under a modified Apache license.
  For license details, please see

    http://www.ensembl.org/info/about/code_licence.html

=head1 CONTACT

  Please email comments or questions to the public Ensembl
14
  developers list at <dev@ensembl.org>.
15 16 17 18 19

  Questions may also be sent to the Ensembl help desk at
  <helpdesk@ensembl.org>.

=cut
20 21 22

=head1 NAME

Monika Komorowska's avatar
Monika Komorowska committed
23
Bio::EnsEMBL::ArchiveStableIdAdaptor
24 25 26

=head1 SYNOPSIS

Monika Komorowska's avatar
Monika Komorowska committed
27
  my $registry = "Bio::EnsEMBL::Registry";
28

Monika Komorowska's avatar
Monika Komorowska committed
29 30
  my $archiveStableIdAdaptor =
    $registry->get_adaptor( 'Human', 'Core', 'ArchiveStableId' );
31

Monika Komorowska's avatar
Monika Komorowska committed
32
  my $stable_id = 'ENSG00000068990';
33

Monika Komorowska's avatar
Monika Komorowska committed
34
  my $arch_id = $archiveStableIdAdaptor->fetch_by_stable_id($stable_id);
35

Monika Komorowska's avatar
Monika Komorowska committed
36 37 38 39 40 41 42 43
  print("Latest incarnation of this stable ID:\n");
  printf( "  Stable ID: %s.%d\n",
    $arch_id->stable_id(), $arch_id->version() );
  print("  Release: "
      . $arch_id->release() . " ("
      . $arch_id->assembly() . ", "
      . $arch_id->db_name()
      . ")\n" );
44

Monika Komorowska's avatar
Monika Komorowska committed
45
  print "\nStable ID history:\n\n";
46

Monika Komorowska's avatar
Monika Komorowska committed
47 48 49
  my $history =
    $archiveStableIdAdaptor->fetch_history_tree_by_stable_id(
    $stable_id);
50

Monika Komorowska's avatar
Monika Komorowska committed
51 52 53 54 55 56 57 58
  foreach my $a ( @{ $history->get_all_ArchiveStableIds } ) {
    printf( "  Stable ID: %s.%d\n", $a->stable_id(), $a->version() );
    print("  Release: "
        . $a->release() . " ("
        . $a->assembly() . ", "
        . $a->db_name()
        . ")\n\n" );
  }
59

Monika Komorowska's avatar
Monika Komorowska committed
60
=head1 DESCRIPTION
61

Monika Komorowska's avatar
Monika Komorowska committed
62 63
ArchiveStableIdAdaptor does all SQL to create ArchiveStableIds and works
of
64

Monika Komorowska's avatar
Monika Komorowska committed
65 66 67 68
  stable_id_event
  mapping_session
  peptite_archive
  gene_archive
69

Monika Komorowska's avatar
Monika Komorowska committed
70
tables inside the core database.
71

Monika Komorowska's avatar
Monika Komorowska committed
72
This whole module has a status of At Risk as it is under development.
73

Monika Komorowska's avatar
Monika Komorowska committed
74
=head1 METHODS
75

Monika Komorowska's avatar
Monika Komorowska committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  fetch_by_stable_id
  fetch_by_stable_id_version
  fetch_by_stable_id_dbname
  fetch_all_by_archive_id
  fetch_predecessors_by_archive_id
  fetch_successors_by_archive_id
  fetch_history_tree_by_stable_id
  add_all_current_to_history
  list_dbnames
  previous_dbname
  next_dbname
  get_peptide
  get_current_release
  get_current_assembly

=head1 RELATED MODULES

  Bio::EnsEMBL::ArchiveStableId
  Bio::EnsEMBL::StableIdEvent
  Bio::EnsEMBL::StableIdHistoryTree
96

Monika Komorowska's avatar
Monika Komorowska committed
97
=head1 METHODS
98

Monika Komorowska's avatar
Monika Komorowska committed
99
=cut
100

Monika Komorowska's avatar
Monika Komorowska committed
101
package Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor;
102

Monika Komorowska's avatar
Monika Komorowska committed
103 104 105
use strict;
use warnings;
no warnings qw(uninitialized);
106

Monika Komorowska's avatar
Monika Komorowska committed
107 108
use Bio::EnsEMBL::DBSQL::BaseAdaptor;
our @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
109

Monika Komorowska's avatar
Monika Komorowska committed
110 111 112 113
use Bio::EnsEMBL::ArchiveStableId;
use Bio::EnsEMBL::StableIdEvent;
use Bio::EnsEMBL::StableIdHistoryTree;
use Bio::EnsEMBL::Utils::Exception qw(deprecate warning throw);
114

Monika Komorowska's avatar
Monika Komorowska committed
115 116
use constant MAX_ROWS => 30;
use constant NUM_HIGH_SCORERS => 20;
117

118

Monika Komorowska's avatar
Monika Komorowska committed
119
=head2 fetch_by_stable_id
120

Monika Komorowska's avatar
Monika Komorowska committed
121 122 123 124 125 126 127 128 129 130
  Arg [1]     : string $stable_id
  Arg [2]     : (optional) string $type
  Example     : none
  Description : Retrives an ArchiveStableId that is the latest incarnation of
                given stable_id.
  Returntype  : Bio::EnsEMBL::ArchiveStableId or undef if not in database
  Exceptions  : none
  Caller      : general
  Status      : At Risk
              : under development
131

Monika Komorowska's avatar
Monika Komorowska committed
132
=cut
Patrick Meidl's avatar
Patrick Meidl committed
133

Monika Komorowska's avatar
Monika Komorowska committed
134 135 136 137 138 139 140 141
sub fetch_by_stable_id {
  my $self = shift;
  my $stable_id = shift;
  
  my $arch_id = Bio::EnsEMBL::ArchiveStableId->new( 
     -stable_id => $stable_id,
     -adaptor => $self
  );
Patrick Meidl's avatar
Patrick Meidl committed
142

Monika Komorowska's avatar
Monika Komorowska committed
143
  @_ ? $arch_id->type(shift) : $self->_resolve_type($arch_id);
Patrick Meidl's avatar
Patrick Meidl committed
144

Monika Komorowska's avatar
Monika Komorowska committed
145
  if ($self->lookup_current($arch_id)) {
Patrick Meidl's avatar
Patrick Meidl committed
146

Monika Komorowska's avatar
Monika Komorowska committed
147 148 149 150 151 152 153
    # stable ID is in current release
    $arch_id->version($arch_id->current_version);
    $arch_id->db_name($self->dbc->dbname);
    $arch_id->release($self->get_current_release);
    $arch_id->assembly($self->get_current_assembly);
  
  } else {
Patrick Meidl's avatar
Patrick Meidl committed
154

Monika Komorowska's avatar
Monika Komorowska committed
155 156 157
    # look for latest version of this stable id
    my $extra_sql = defined($arch_id->{'type'}) ?
      " AND sie.type = '@{[lc($arch_id->{'type'})]}'" : '';
158

Monika Komorowska's avatar
Monika Komorowska committed
159
    my $r = $self->_fetch_archive_id($stable_id, $extra_sql, $extra_sql);
160

Monika Komorowska's avatar
Monika Komorowska committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    if ($r->{'new_stable_id'} and $r->{'new_stable_id'} eq $stable_id) {
      # latest event is a self event, use new_* data
      $arch_id->version($r->{'new_version'});
      $arch_id->release($r->{'new_release'});
      $arch_id->assembly($r->{'new_assembly'});
      $arch_id->db_name($r->{'new_db_name'});
    } else {
      # latest event is a deletion event (or mapping to other ID; this clause
      # is only used to cope with buggy data where deletion events are
      # missing), use old_* data
      $arch_id->version($r->{'old_version'});
      $arch_id->release($r->{'old_release'});
      $arch_id->assembly($r->{'old_assembly'});
      $arch_id->db_name($r->{'old_db_name'});
    }
Monika Komorowska's avatar
Monika Komorowska committed
176

Monika Komorowska's avatar
Monika Komorowska committed
177
    $arch_id->type(ucfirst(lc($r->{'type'})));
178
  }
Monika Komorowska's avatar
Monika Komorowska committed
179 180 181 182
  
  if (! defined $arch_id->db_name) {
    # couldn't find stable ID in archive or current db
    return undef;
Monika Komorowska's avatar
Monika Komorowska committed
183
  }
Patrick Meidl's avatar
Patrick Meidl committed
184

Monika Komorowska's avatar
Monika Komorowska committed
185 186 187
  $arch_id->is_latest(1);

  return $arch_id;
188 189 190
}


Monika Komorowska's avatar
Monika Komorowska committed
191
=head2 fetch_by_stable_id_version
192

Monika Komorowska's avatar
Monika Komorowska committed
193 194 195 196 197 198 199 200 201
  Arg [1]     : string $stable_id
  Arg [2]     : int $version
  Example     : none
  Description : Retrieve an ArchiveStableId with given version and stable ID.
  Returntype  : Bio::EnsEMBL::ArchiveStableId 
  Exceptions  : none
  Caller      : general
  Status      : At Risk
              : under development
202 203 204

=cut

Monika Komorowska's avatar
Monika Komorowska committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
sub fetch_by_stable_id_version {
  my $self = shift;
  my $stable_id = shift;
  my $version = shift;

  my $arch_id = Bio::EnsEMBL::ArchiveStableId->new( 
     -stable_id => $stable_id,
     -version => $version,
     -adaptor => $self
  );
  
  @_ ? $arch_id->type(shift) : $self->_resolve_type($arch_id);

  if ($self->lookup_current($arch_id) && $arch_id->is_current) {

    # this version is the current one
    $arch_id->db_name($self->dbc->dbname);
    $arch_id->release($self->get_current_release);
    $arch_id->assembly($self->get_current_assembly);
  
  } else {
Patrick Meidl's avatar
Patrick Meidl committed
226

Monika Komorowska's avatar
Monika Komorowska committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    # find latest release this stable ID version is found in archive
    my $extra_sql1 = qq(AND sie.old_version = "$version");
    my $extra_sql2 = qq(AND sie.new_version = "$version");
    my $r = $self->_fetch_archive_id($stable_id, $extra_sql1, $extra_sql2);

    if ($r->{'new_stable_id'} and $r->{'new_stable_id'} eq $stable_id
        and $r->{'new_version'} == $version) {
      # latest event is a self event, use new_* data
      $arch_id->release($r->{'new_release'});
      $arch_id->assembly($r->{'new_assembly'});
      $arch_id->db_name($r->{'new_db_name'});
    } else {
      # latest event is a deletion event (or mapping to other ID; this clause
      # is only used to cope with buggy data where deletion events are
      # missing), use old_* data
      $arch_id->release($r->{'old_release'});
      $arch_id->assembly($r->{'old_assembly'});
      $arch_id->db_name($r->{'old_db_name'});
    }
Patrick Meidl's avatar
Patrick Meidl committed
246

Monika Komorowska's avatar
Monika Komorowska committed
247 248 249 250 251 252 253 254 255
    $arch_id->type(ucfirst(lc($r->{'type'})));
  }
  
  if (! defined $arch_id->db_name) {
    # couldn't find stable ID version in archive or current release
    return undef;
  }

  return $arch_id;
256 257 258
}


Monika Komorowska's avatar
Monika Komorowska committed
259
=head2 fetch_by_stable_id_dbname
260

Monika Komorowska's avatar
Monika Komorowska committed
261 262 263 264 265 266 267 268 269
  Arg [1]     : string $stable_id
  Arg [2]     : string $db_name
  Example     : none
  Description : Create an ArchiveStableId from given arguments.
  Returntype  : Bio::EnsEMBL::ArchiveStableId or undef if not in database
  Exceptions  : none
  Caller      : general
  Status      : At Risk
              : under development
270 271 272

=cut

Monika Komorowska's avatar
Monika Komorowska committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
sub fetch_by_stable_id_dbname {
  my $self = shift;
  my $stable_id = shift;
  my $db_name = shift;
  
  my $arch_id = Bio::EnsEMBL::ArchiveStableId->new( 
     -stable_id => $stable_id,
     -db_name => $db_name,
     -adaptor => $self
  );
  
  @_ ? $arch_id->type(shift) : $self->_resolve_type($arch_id);

  if ($self->lookup_current($arch_id) and $db_name eq $self->dbc->dbname) {

    # this version is the current one
    $arch_id->version($arch_id->current_version);
    $arch_id->release($self->get_current_release);
    $arch_id->assembly($self->get_current_assembly);
  
  } else {

    # find version for this dbname in the stable ID archive
    my $extra_sql = defined($arch_id->{'type'}) ?
      " AND sie.type = '@{[lc($arch_id->{'type'})]}'" : '';
    my $extra_sql1 = $extra_sql . qq( AND ms.old_db_name = "$db_name");
    my $extra_sql2 = $extra_sql . qq( AND ms.new_db_name = "$db_name");
    my $r = $self->_fetch_archive_id($stable_id, $extra_sql1, $extra_sql2);

    if ($r->{'new_stable_id'} and $r->{'new_stable_id'} eq $stable_id
        and $r->{'new_db_name'} eq $db_name) {

      # latest event is a self event, use new_* data
      $arch_id->release($r->{'new_release'});
      $arch_id->assembly($r->{'new_assembly'});
      $arch_id->version($r->{'new_version'});
    } else {
      # latest event is a deletion event (or mapping to other ID; this clause
      # is only used to cope with buggy data where deletion events are
      # missing), use old_* data
      $arch_id->release($r->{'old_release'});
      $arch_id->assembly($r->{'old_assembly'});
      $arch_id->version($r->{'old_version'});
    }
Patrick Meidl's avatar
Patrick Meidl committed
317

Monika Komorowska's avatar
Monika Komorowska committed
318 319 320 321 322 323
    $arch_id->type(ucfirst(lc($r->{'type'})));
  }
  
  if (! defined $arch_id->version ) {
    # couldn't find stable ID version in archive or current release
    return undef;
324 325
  }

Monika Komorowska's avatar
Monika Komorowska committed
326
  return $arch_id;
327 328
}

Monika Komorowska's avatar
Monika Komorowska committed
329 330 331 332 333 334 335 336 337 338 339
#
# Helper method to do fetch ArchiveStableId from db.
# Used by fetch_by_stable_id(), fetch_by_stable_id_version() and
# fetch_by_stable_id_dbname().
# Returns hashref as returned by DBI::sth::fetchrow_hashref
#
sub _fetch_archive_id {
  my $self = shift;
  my $stable_id = shift;
  my $extra_sql1 = shift;
  my $extra_sql2 = shift;
340

Monika Komorowska's avatar
Monika Komorowska committed
341 342 343 344 345 346 347 348 349 350 351 352
  # using a UNION is much faster in this query than somthing like
  # "... AND (sie.old_stable_id = ? OR sie.new_stable_id = ?)"
  my $sql = qq(
    (SELECT * FROM stable_id_event sie, mapping_session ms
    WHERE sie.mapping_session_id = ms.mapping_session_id
    AND sie.old_stable_id = ?
    $extra_sql1)
    UNION
    (SELECT * FROM stable_id_event sie, mapping_session ms
    WHERE sie.mapping_session_id = ms.mapping_session_id
    AND sie.new_stable_id = ?
    $extra_sql2)
353
    ORDER BY created DESC, score DESC
Monika Komorowska's avatar
Monika Komorowska committed
354 355
    LIMIT 1
  );
356

Monika Komorowska's avatar
Monika Komorowska committed
357 358 359 360
  my $sth = $self->prepare($sql);
  $sth->execute($stable_id,$stable_id);
  my $r = $sth->fetchrow_hashref;
  $sth->finish;
361

Monika Komorowska's avatar
Monika Komorowska committed
362 363
  return $r;
}  
364

365

Monika Komorowska's avatar
Monika Komorowska committed
366
=head2 fetch_all_by_archive_id
367

Monika Komorowska's avatar
Monika Komorowska committed
368 369 370 371 372 373 374 375
  Arg [1]     : Bio::EnsEMBL::ArchiveStableId $archive_id
  Arg [2]     : String $return_type - type of ArchiveStableId to fetch
  Example     : my $arch_id = $arch_adaptor->fetch_by_stable_id('ENSG0001');
                my @archived_transcripts =
                 $arch_adaptor->fetch_all_by_archive_id($arch_id, 'Transcript');
  Description : Given a ArchiveStableId it retrieves associated ArchiveStableIds
                of specified type (e.g. retrieve transcripts for genes or vice
                versa).
376

Monika Komorowska's avatar
Monika Komorowska committed
377 378 379 380 381 382 383 384
                See also fetch_associated_archived() for a different approach to
                retrieve this data.
  Returntype  : listref Bio::EnsEMBL::ArchiveStableId
  Exceptions  : none
  Caller      : Bio::EnsEMBL::ArchiveStableId->get_all_gene_archive_ids,
                get_all_transcript_archive_ids, get_all_translation_archive_ids
  Status      : At Risk
              : under development
385 386 387

=cut

Monika Komorowska's avatar
Monika Komorowska committed
388 389 390 391 392 393 394 395
sub fetch_all_by_archive_id {
  my $self = shift;
  my $archive_id = shift;
  my $return_type = shift;

  my @result = ();
  my $lc_self_type = lc($archive_id->type);
  my $lc_return_type = lc($return_type);
396

Monika Komorowska's avatar
Monika Komorowska committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
  my $sql = qq(
    SELECT
          ga.${lc_return_type}_stable_id,
          ga.${lc_return_type}_version,
          m.old_db_name,
          m.old_release,
          m.old_assembly
    FROM  gene_archive ga, mapping_session m
    WHERE ga.${lc_self_type}_stable_id = ?
    AND   ga.${lc_self_type}_version = ?
    AND   ga.mapping_session_id = m.mapping_session_id
  );
  
  my $sth = $self->prepare($sql);
  $sth->bind_param(1, $archive_id->stable_id, SQL_VARCHAR);
  $sth->bind_param(2, $archive_id->version, SQL_SMALLINT);
  $sth->execute;
  
  my ($stable_id, $version, $db_name, $release, $assembly);
  $sth->bind_columns(\$stable_id, \$version, \$db_name, \$release, \$assembly);

  while ($sth->fetch) {
    my $new_arch_id = Bio::EnsEMBL::ArchiveStableId->new(
       -stable_id => $stable_id,
       -version => $version,
       -db_name => $db_name,
       -release => $release,
       -assembly => $assembly,
       -type => $return_type,
       -adaptor => $self
    );

    push( @result, $new_arch_id );
430 431
  }

Monika Komorowska's avatar
Monika Komorowska committed
432 433
  $sth->finish();
  return \@result;
434 435 436
}


Monika Komorowska's avatar
Monika Komorowska committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
=head2 fetch_associated_archived 

  Arg[1]      : Bio::EnsEMBL::ArchiveStableId $arch_id -
                the ArchiveStableId to fetch associated archived IDs for
  Example     : my ($arch_gene, $arch_tr, $arch_tl, $pep_seq) =
                  @{ $archive_adaptor->fetch_associated_archived($arch_id) };
  Description : Fetches associated archived stable IDs from the db for a given
                ArchiveStableId (version is taken into account).
  Return type : Listref of
                  ArchiveStableId archived gene
                  ArchiveStableId archived transcript
                  (optional) ArchiveStableId archived translation
                  (optional) peptide sequence
  Exceptions  : thrown on missing or wrong argument
                thrown if ArchiveStableID has no type
  Caller      : Bio::EnsEMBL::ArchiveStableId->get_all_associated_archived()
  Status      : At Risk
              : under development
455 456 457

=cut

Monika Komorowska's avatar
Monika Komorowska committed
458 459 460 461 462 463
sub fetch_associated_archived {
  my $self = shift;
  my $arch_id = shift;

  throw("Need a Bio::EnsEMBL::ArchiveStableId") unless ($arch_id
    and ref($arch_id) and $arch_id->isa('Bio::EnsEMBL::ArchiveStableId'));
464

Monika Komorowska's avatar
Monika Komorowska committed
465 466 467 468
  my $type = $arch_id->type();

  if ( !defined($type) ) {
    throw("Can't deduce ArchiveStableId type.");
469 470
  }

Monika Komorowska's avatar
Monika Komorowska committed
471
  $type = lc($type);
472

Monika Komorowska's avatar
Monika Komorowska committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
  my $sql = qq(
    SELECT  ga.gene_stable_id,
            ga.gene_version,
            ga.transcript_stable_id,
            ga.transcript_version,
            ga.translation_stable_id,
            ga.translation_version,
            pa.peptide_seq,
            ms.old_release,
            ms.old_assembly,
            ms.old_db_name
    FROM (mapping_session ms, gene_archive ga)
    LEFT JOIN peptide_archive pa
      ON ga.peptide_archive_id = pa.peptide_archive_id
    WHERE ga.mapping_session_id = ms.mapping_session_id
    AND ga.${type}_stable_id = ?
    AND ga.${type}_version = ?
  );
491

Monika Komorowska's avatar
Monika Komorowska committed
492 493 494 495
  my $sth = $self->prepare($sql);
  $sth->bind_param(1, $arch_id->stable_id, SQL_VARCHAR);
  $sth->bind_param(2, $arch_id->version, SQL_SMALLINT);
  $sth->execute;
496

Monika Komorowska's avatar
Monika Komorowska committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
  my @result = ();

  while (my $r = $sth->fetchrow_hashref) {

    my @row = ();

    # create ArchiveStableIds genes, transcripts and translations
    push @row, Bio::EnsEMBL::ArchiveStableId->new(
       -stable_id => $r->{'gene_stable_id'},
       -version => $r->{'gene_version'},
       -db_name => $r->{'old_db_name'},
       -release => $r->{'old_release'},
       -assembly => $r->{'old_assembly'},
       -type => 'Gene',
       -adaptor => $self
    );
    
    push @row, Bio::EnsEMBL::ArchiveStableId->new(
       -stable_id => $r->{'transcript_stable_id'},
       -version => $r->{'transcript_version'},
       -db_name => $r->{'old_db_name'},
       -release => $r->{'old_release'},
       -assembly => $r->{'old_assembly'},
       -type => 'Transcript',
       -adaptor => $self
    );

    if ($r->{'translation_stable_id'}) {
      push @row, Bio::EnsEMBL::ArchiveStableId->new(
         -stable_id => $r->{'translation_stable_id'},
         -version => $r->{'translation_version'},
         -db_name => $r->{'old_db_name'},
         -release => $r->{'old_release'},
         -assembly => $r->{'old_assembly'},
         -type => 'Translation',
         -adaptor => $self
      );

      # push peptide sequence onto result list
      push @row, $r->{'peptide_seq'};
    }
    
    push @result, \@row;
  }
541

Monika Komorowska's avatar
Monika Komorowska committed
542 543
  return \@result;
}
544 545


Monika Komorowska's avatar
Monika Komorowska committed
546
=head2 fetch_predecessors_by_archive_id
547

Monika Komorowska's avatar
Monika Komorowska committed
548 549 550 551 552 553 554 555 556 557 558 559
  Arg [1]     : Bio::EnsEMBL::ArchiveStableId
  Example     : none
  Description : Retrieve a list of ArchiveStableIds that were mapped to the 
                given one. This method goes back only one level, to retrieve
                a full predecessor history use fetch_predecessor_history, or 
                ideally fetch_history_tree_by_stable_id for the complete
                history network.
  Returntype  : listref Bio::EnsEMBL::ArchiveStableId
  Exceptions  : none
  Caller      : Bio::EnsEMBL::ArchiveStableId->get_all_predecessors
  Status      : At Risk
              : under development
560

Monika Komorowska's avatar
Monika Komorowska committed
561
=cut
562

Monika Komorowska's avatar
Monika Komorowska committed
563 564 565 566 567 568 569 570 571
sub fetch_predecessors_by_archive_id {
  my $self = shift;
  my $arch_id = shift;
  
  my @result;
  
  if( ! ( defined $arch_id->stable_id() &&
	  defined $arch_id->db_name() )) {
    throw( "Need db_name for predecessor retrieval" );
572 573
  }

Monika Komorowska's avatar
Monika Komorowska committed
574 575 576 577 578 579 580 581 582 583 584 585 586
  my $sql = qq(
    SELECT
          sie.old_stable_id,
          sie.old_version,
          sie.type,
          m.old_db_name,
          m.old_release,
          m.old_assembly
    FROM  mapping_session m, stable_id_event sie
    WHERE sie.mapping_session_id = m.mapping_session_id
    AND   sie.new_stable_id = ?
    AND   m.new_db_name = ?	
  );
587

588
  my $sth = $self->prepare($sql);
Monika Komorowska's avatar
Monika Komorowska committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
  $sth->bind_param(1, $arch_id->stable_id, SQL_VARCHAR);
  $sth->bind_param(2, $arch_id->db_name, SQL_VARCHAR);
  $sth->execute();
  
  my ($old_stable_id, $old_version, $type, $old_db_name, $old_release, $old_assembly);
  $sth->bind_columns(\$old_stable_id, \$old_version, \$type, \$old_db_name, \$old_release, \$old_assembly);
  
  while ($sth->fetch) {
    if (defined $old_stable_id) {
      my $old_arch_id = Bio::EnsEMBL::ArchiveStableId->new( 
	 -stable_id => $old_stable_id,
	 -version => $old_version,
	 -db_name => $old_db_name,
         -release => $old_release,
         -assembly => $old_assembly,
         -type => $type,
	 -adaptor => $self
      );
      push( @result, $old_arch_id );
    }
609
  }
Monika Komorowska's avatar
Monika Komorowska committed
610
  $sth->finish();
611

Monika Komorowska's avatar
Monika Komorowska committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
  # if you didn't find any predecessors, there might be a gap in the
  # mapping_session history (i.e. databases in mapping_session don't chain). To
  # bridge the gap, look in the previous mapping_session for identical
  # stable_id.version
  unless (@result) {

    $sql = qq(
      SELECT
            sie.new_stable_id,
            sie.new_version,
            sie.type,
            m.new_db_name,
            m.new_release,
            m.new_assembly
      FROM  mapping_session m, stable_id_event sie
      WHERE sie.mapping_session_id = m.mapping_session_id
      AND   sie.new_stable_id = ?
      AND   m.new_db_name = ?	
    );

    $sth = $self->prepare($sql);

    my $curr_dbname = $arch_id->db_name;
    
    PREV:
    while (my $prev_dbname = $self->previous_dbname($curr_dbname)) {
    
      $sth->bind_param(1,$arch_id->stable_id, SQL_VARCHAR);
      $sth->bind_param(2,$prev_dbname, SQL_VARCHAR);
      $sth->execute();
      
      $sth->bind_columns(\$old_stable_id, \$old_version, \$type, \$old_db_name, \$old_release, \$old_assembly);
      
      while( $sth->fetch() ) {
        if (defined $old_stable_id) {
          my $old_arch_id = Bio::EnsEMBL::ArchiveStableId->new( 
             -stable_id => $old_stable_id,
             -version => $old_version,
             -db_name => $old_db_name,
             -release => $old_release,
             -assembly => $old_assembly,
             -type => $type,
             -adaptor => $self
          );
          push( @result, $old_arch_id );

          last PREV;
        }
      }
661

Monika Komorowska's avatar
Monika Komorowska committed
662
      $curr_dbname = $prev_dbname;
663

Monika Komorowska's avatar
Monika Komorowska committed
664 665 666
    }
      
    $sth->finish();
667 668
  }

Monika Komorowska's avatar
Monika Komorowska committed
669 670 671
  return \@result;
}

672

Monika Komorowska's avatar
Monika Komorowska committed
673
=head2 fetch_successors_by_archive_id
674

Monika Komorowska's avatar
Monika Komorowska committed
675 676 677 678 679 680 681 682 683 684 685 686
  Arg [1]     : Bio::EnsEMBL::ArchiveStableId
  Example     : none
  Description : Retrieve a list of ArchiveStableIds that the given one was 
                mapped to. This method goes forward only one level, to retrieve
                a full successor history use fetch_successor_history, or 
                ideally fetch_history_tree_by_stable_id for the complete
                history network.
  Returntype  : listref Bio::EnsEMBL::ArchiveStableId
  Exceptions  : none
  Caller      : Bio::EnsEMBL::ArchiveStableId->get_all_successors
  Status      : At Risk
              : under development
687

Monika Komorowska's avatar
Monika Komorowska committed
688
=cut
689

Monika Komorowska's avatar
Monika Komorowska committed
690
sub fetch_successors_by_archive_id {
691
  my $self = shift;
Monika Komorowska's avatar
Monika Komorowska committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
  my $arch_id = shift;
  my @result;

  
  if( ! ( defined $arch_id->stable_id() &&
	  defined $arch_id->db_name() )) {
    throw( "Need db_name for successor retrieval" );
  }

  my $sql = qq(
    SELECT
          sie.new_stable_id,
          sie.new_version,
          sie.type,
          m.new_db_name,
          m.new_release,
          m.new_assembly
    FROM  mapping_session m, stable_id_event sie
    WHERE sie.mapping_session_id = m.mapping_session_id
    AND   sie.old_stable_id = ?
    AND   m.old_db_name = ?	
  );

  my $sth = $self->prepare( $sql );
  $sth->bind_param(1,$arch_id->stable_id,SQL_VARCHAR);
  $sth->bind_param(2,$arch_id->db_name,SQL_VARCHAR);
  $sth->execute();
  
  my ($new_stable_id, $new_version, $type, $new_db_name, $new_release, $new_assembly);
  $sth->bind_columns(\$new_stable_id, \$new_version, \$type, \$new_db_name, \$new_release, \$new_assembly);
  
  while( $sth->fetch() ) {
    if( defined $new_stable_id ) {
      my $new_arch_id = Bio::EnsEMBL::ArchiveStableId->new( 
	 -stable_id => $new_stable_id,
	 -version => $new_version,
	 -db_name => $new_db_name,
         -release => $new_release,
         -assembly => $new_assembly,
         -type => $type,
	 -adaptor => $self
      );
        
      push( @result, $new_arch_id );
    }
  }
  $sth->finish();
  
  # if you didn't find any successors, there might be a gap in the
  # mapping_session history (i.e. databases in mapping_session don't chain). To
  # bridge the gap, look in the next mapping_session for identical
  # stable_id.version
  unless (@result) {

    $sql = qq(
      SELECT
            sie.old_stable_id,
            sie.old_version,
            sie.type,
            m.old_db_name,
            m.old_release,
            m.old_assembly
      FROM  mapping_session m, stable_id_event sie
      WHERE sie.mapping_session_id = m.mapping_session_id
      AND   sie.old_stable_id = ?
      AND   m.old_db_name = ?	
    );

    $sth = $self->prepare($sql);

    my $curr_dbname = $arch_id->db_name;
    
    NEXTDB:
    while (my $next_dbname = $self->next_dbname($curr_dbname)) {

      $sth->bind_param(1, $arch_id->stable_id, SQL_VARCHAR);
      $sth->bind_param(2, $next_dbname, SQL_VARCHAR);
      $sth->execute();
      
      $sth->bind_columns(\$new_stable_id, \$new_version, \$type, \$new_db_name, \$new_release, \$new_assembly);
      
      while( $sth->fetch() ) {
        if (defined $new_stable_id) {
          my $new_arch_id = Bio::EnsEMBL::ArchiveStableId->new( 
             -stable_id => $new_stable_id,
             -version => $new_version,
             -db_name => $new_db_name,
             -release => $new_release,
             -assembly => $new_assembly,
             -type => $type,
             -adaptor => $self
          );
            
          push( @result, $new_arch_id );

          last NEXTDB;
        }
      }
      
      $curr_dbname = $next_dbname;

    }

    $sth->finish();
796 797
  }

Monika Komorowska's avatar
Monika Komorowska committed
798
  return \@result;
799 800 801
}


802

Monika Komorowska's avatar
Monika Komorowska committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
=head2 fetch_history_tree_by_stable_id

  Arg[1]      : String $stable_id - the stable ID to fetch the history tree for
  Arg[2]      : (optional) Int $num_high_scorers
                number of mappings per stable ID allowed when filtering
  Arg[3]      : (optional) Int $max_rows
                maximum number of stable IDs in history tree (used for
                filtering)
  Example     : my $history = $archive_adaptor->fetch_history_tree_by_stable_id(
                  'ENSG00023747897');
  Description : Returns the history tree for a given stable ID. This will
                include a network of all stable IDs it is related to. The
                method will try to return a minimal (sparse) set of nodes
                (ArchiveStableIds) and links (StableIdEvents) by removing any
                redundant entries and consolidating mapping events so that only
                changes are recorded.
  Return type : Bio::EnsEMBL::StableIdHistoryTree
  Exceptions  : thrown on missing argument
  Caller      : Bio::EnsEMBL::ArchiveStableId::get_history_tree, general
  Status      : At Risk
              : under development
824 825 826

=cut

Monika Komorowska's avatar
Monika Komorowska committed
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
sub fetch_history_tree_by_stable_id {
  my ($self, $stable_id, $num_high_scorers, $max_rows) = @_;

  throw("Expecting a stable ID argument.") unless $stable_id;

  $num_high_scorers ||= NUM_HIGH_SCORERS;
  $max_rows ||= MAX_ROWS;

  # using a UNION is much faster in this query than somthing like
  # "... AND (sie.old_stable_id = ?) OR (sie.new_stable_id = ?)"
  my $sql = qq(
    SELECT sie.old_stable_id, sie.old_version,
           ms.old_db_name, ms.old_release, ms.old_assembly,
           sie.new_stable_id, sie.new_version,
           ms.new_db_name, ms.new_release, ms.new_assembly,
           sie.type, sie.score
    FROM stable_id_event sie, mapping_session ms
    WHERE sie.mapping_session_id = ms.mapping_session_id
    AND sie.old_stable_id = ?
    UNION
    SELECT sie.old_stable_id, sie.old_version,
           ms.old_db_name, ms.old_release, ms.old_assembly,
           sie.new_stable_id, sie.new_version,
           ms.new_db_name, ms.new_release, ms.new_assembly,
           sie.type, sie.score
    FROM stable_id_event sie, mapping_session ms
    WHERE sie.mapping_session_id = ms.mapping_session_id
    AND sie.new_stable_id = ?
  );
  
  my $sth = $self->prepare($sql);

  my $history = Bio::EnsEMBL::StableIdHistoryTree->new(
      -CURRENT_DBNAME => $self->dbc->dbname,
      -CURRENT_RELEASE => $self->get_current_release,
      -CURRENT_ASSEMBLY => $self->get_current_assembly,
  );

  # remember stable IDs you need to do and those that are done. Initialise the
  # former hash with the focus stable ID
  my %do = ($stable_id => 1);
  my %done;

  # while we got someting to do
  while (my ($id) = keys(%do)) {

    # if we already have more than MAX_ROWS stable IDs in this tree, we can't
    # build the full tree. Return undef.
    if (scalar(keys(%done)) > $max_rows) {
      # warning("Too many related stable IDs (".scalar(keys(%done)).") to draw a history tree.");
      $history->is_incomplete(1);
      $sth->finish;
      last;
    }

    # mark this stable ID as done
    delete $do{$id};
    $done{$id} = 1;

    # fetch all stable IDs related to this one from the database
    $sth->bind_param(1, $id, SQL_VARCHAR);
    $sth->bind_param(2, $id, SQL_VARCHAR);
    $sth->execute;

    my @events;

    while (my $r = $sth->fetchrow_hashref) {
      
      #
      # create old and new ArchiveStableIds and a StableIdEvent to link them
      # add all of these to the history tree
      #
      my ($old_id, $new_id);

      if ($r->{'old_stable_id'}) {
        $old_id = Bio::EnsEMBL::ArchiveStableId->new(
          -stable_id => $r->{'old_stable_id'},
          -version => $r->{'old_version'},
          -db_name => $r->{'old_db_name'},
          -release => $r->{'old_release'},
          -assembly => $r->{'old_assembly'},
          -type => $r->{'type'},
          -adaptor => $self
        );
      }
       
      if ($r->{'new_stable_id'}) {
        $new_id = Bio::EnsEMBL::ArchiveStableId->new(
          -stable_id => $r->{'new_stable_id'},
          -version => $r->{'new_version'},
          -db_name => $r->{'new_db_name'},
          -release => $r->{'new_release'},
          -assembly => $r->{'new_assembly'},
          -type => $r->{'type'},
          -adaptor => $self
        );
      }

      my $event = Bio::EnsEMBL::StableIdEvent->new(
        -old_id => $old_id,
        -new_id => $new_id,
        -score => $r->{'score'}
      );

      push @events, $event;
932 933

    }
Monika Komorowska's avatar
Monika Komorowska committed
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951

    # filter out low-scoring events; the number of highest scoring events
    # returned is defined by NUM_HIGH_SCORERS
    my @others;

    foreach my $event (@events) {
      
      my $old_id = $event->old_ArchiveStableId;
      my $new_id = $event->new_ArchiveStableId;
      
      # creation, deletion and mapping-to-self events are added to the history
      # tree directly
      if (!$old_id || !$new_id || ($old_id->stable_id eq $new_id->stable_id)) {
        $history->add_StableIdEvents($event);
      } else {
        push @others, $event;
      }
      
952
    }
Monika Komorowska's avatar
Monika Komorowska committed
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969

    #if (scalar(@others) > $num_high_scorers) {
    #  warn "Filtering ".(scalar(@others) - $num_high_scorers).
    #    " low-scoring events.\n";
    #}

    my $k = 0;
    foreach my $event (sort { $b->score <=> $a->score } @others) {
      $history->add_StableIdEvents($event);
      
      # mark stable IDs as todo if appropriate
      $do{$event->old_ArchiveStableId->stable_id} = 1
        unless $done{$event->old_ArchiveStableId->stable_id};
      $do{$event->new_ArchiveStableId->stable_id} = 1
        unless $done{$event->new_ArchiveStableId->stable_id};
      
      last if (++$k == $num_high_scorers);
970
    }
Monika Komorowska's avatar
Monika Komorowska committed
971 972 973 974 975 976 977
    
  }

  $sth->finish;
  
  # try to consolidate the tree (remove redundant nodes, bridge gaps)
  $history->consolidate_tree;
978

Monika Komorowska's avatar
Monika Komorowska committed
979 980 981 982 983 984 985 986 987
  # now add ArchiveStableIds for current Ids not found in the archive
  $self->add_all_current_to_history($history);
  
  # calculate grid coordinates for the sorted tree; this will also try to
  # untangle the tree
  $history->calculate_coords;
  
  return $history;
}
988

Patrick Meidl's avatar
Patrick Meidl committed
989

Monika Komorowska's avatar
Monika Komorowska committed
990
=head2 add_all_current_to_history 
Patrick Meidl's avatar
Patrick Meidl committed
991

Monika Komorowska's avatar
Monika Komorowska committed
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
  Arg[1]      : Bio::EnsEMBL::StableIdHistoryTree $history -
                the StableIdHistoryTree object to add the current IDs to
  Description : This method adds the current versions of all stable IDs found
                in a StableIdHistoryTree object to the tree, by creating
                appropriate Events for the stable IDs found in the *_stable_id
                tables. This is a helper method for
                fetch_history_tree_by_stable_id(), see there for more
                documentation.
  Return type : none (passed-in object is manipulated)
  Exceptions  : thrown on missing or wrong argument
  Caller      : internal
  Status      : At Risk
              : under development
Patrick Meidl's avatar
Patrick Meidl committed
1005

Monika Komorowska's avatar
Monika Komorowska committed
1006
=cut
Patrick Meidl's avatar
Patrick Meidl committed
1007

Monika Komorowska's avatar
Monika Komorowska committed
1008 1009 1010 1011 1012 1013 1014
sub add_all_current_to_history {
  my $self = shift;
  my $history = shift;

  unless ($history and $history->isa('Bio::EnsEMBL::StableIdHistoryTree')) {
    throw("Need a Bio::EnsEMBL::StableIdHistoryTree.");
  }
Patrick Meidl's avatar
Patrick Meidl committed
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024

  my @ids = @{ $history->get_unique_stable_ids };
  my $id_string = join("', '", @ids);
  
  my $tmp_id = Bio::EnsEMBL::ArchiveStableId->new(-stable_id => $ids[0]);
  my $type = lc($self->_resolve_type($tmp_id));
  return unless ($type);

  # get current stable IDs from db
  my $sql = qq(
Monika Komorowska's avatar
Monika Komorowska committed
1025
    SELECT stable_id, version FROM ${type}
Patrick Meidl's avatar
Patrick Meidl committed
1026 1027 1028 1029
    WHERE stable_id IN ('$id_string')
  );
  my $sth = $self->prepare($sql);
  $sth->execute;
Arne Stabenau's avatar
Arne Stabenau committed
1030

Monika Komorowska's avatar
Monika Komorowska committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
  while (my ($stable_id, $version) = $sth->fetchrow_array) {

    my $new_id = Bio::EnsEMBL::ArchiveStableId->new(
      -stable_id => $stable_id,
      -version => $version,
      -current_version => $version,
      -db_name => $self->dbc->dbname,
      -release => $self->get_current_release,
      -assembly => $self->get_current_assembly,
      -type => $type,
      -adaptor => $self
    );

    my $event = $history->get_latest_StableIdEvent($new_id);
    next unless ($event);

    if ($event->old_ArchiveStableId and
        $event->old_ArchiveStableId->stable_id eq $stable_id) {
      
      # latest event was a self event
      # update it with current stable ID and add to tree
      $event->new_ArchiveStableId($new_id);
1053

Monika Komorowska's avatar
Monika Komorowska committed
1054
    } else {
1055

Monika Komorowska's avatar
Monika Komorowska committed
1056 1057 1058 1059 1060 1061 1062 1063
      # latest event was a non-self event
      # create a new event where the old_id is the new_id from latest
      my $new_event = Bio::EnsEMBL::StableIdEvent->new(
        -old_id => $event->new_ArchiveStableId,
        -new_id => $new_id,
        -score => $event->score,
      );
      $history->add_StableIdEvents($new_event);
1064
    }
Monika Komorowska's avatar
Monika Komorowska committed
1065
    
1066 1067
  }

Monika Komorowska's avatar
Monika Komorowska committed
1068 1069 1070 1071
  # refresh node cache
  $history->flush_ArchiveStableIds;
  $history->add_ArchiveStableIds_for_events;
}
1072

1073

Monika Komorowska's avatar
Monika Komorowska committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
=head2 fetch_successor_history

  Arg [1]     : Bio::EnsEMBL::ArchiveStableId $arch_id
  Example     : none
  Description : Gives back a list of archive stable ids which are successors in
                the stable_id_event tree of the given stable_id. Might well be
                empty.
                
                This method isn't deprecated, but in most cases you will rather
                want to use fetch_history_tree_by_stable_id().
  Returntype  : listref Bio::EnsEMBL::ArchiveStableId
                Since every ArchiveStableId knows about it's successors, this is
                a linked tree.
  Exceptions  : none
  Caller      : webcode for archive
  Status      : At Risk
              : under development
1091

Monika Komorowska's avatar
Monika Komorowska committed
1092
=cut
1093

Monika Komorowska's avatar
Monika Komorowska committed
1094 1095 1096
sub fetch_successor_history {
  my $self = shift;
  my $arch_id = shift;
1097

Monika Komorowska's avatar
Monika Komorowska committed
1098 1099
  my $current_db_name = $self->list_dbnames->[0];
  my $dbname = $arch_id->db_name;
1100

Monika Komorowska's avatar
Monika Komorowska committed
1101 1102
  if ($dbname eq $current_db_name) {
    return [$arch_id];
Monika Komorowska's avatar
Monika Komorowska committed
1103
  }
1104

Monika Komorowska's avatar
Monika Komorowska committed
1105 1106 1107 1108
  my $old = [];
  my @result = ();
  
  push @$old, $arch_id;
1109

Monika Komorowska's avatar
Monika Komorowska committed
1110 1111 1112 1113 1114
  while ($dbname ne $current_db_name) {
    my $new = [];
    while (my $asi = (shift @$old)) {
      push @$new, @{ $asi->get_all_successors };
    }
1115

Monika Komorowska's avatar
Monika Komorowska committed
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    if (@$new) {
      $dbname = $new->[0]->db_name;
    } else {
      last;
    }

    # filter duplicates
    my %unique = map { join(":", $_->stable_id, $_->version, $_->release) =>
      $_ } @$new;
    @$new = values %unique;
    
    @$old = @$new;
    push @result, @$new;
1129 1130
  }

Monika Komorowska's avatar
Monika Komorowska committed
1131
  return \@result;
1132 1133
}

1134

Monika Komorowska's avatar
Monika Komorowska committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
=head2 fetch_predecessor_history

  Arg [1]     : Bio::EnsEMBL::ArchiveStableId $arch_id
  Example     : none
  Description : Gives back a list of archive stable ids which are predecessors
                in the stable_id_event tree of the given stable_id. Might well
                be empty.
                
                This method isn't deprecated, but in most cases you will rather
                want to use fetch_history_tree_by_stable_id().
  Returntype  : listref Bio::EnsEMBL::ArchiveStableId
                Since every ArchiveStableId knows about it's successors, this is
                a linked tree.
  Exceptions  : none
  Caller      : webcode for archive
  Status      : At Risk
              : under development
1152 1153 1154

=cut

Monika Komorowska's avatar
Monika Komorowska committed
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
sub fetch_predecessor_history {
  my $self = shift;
  my $arch_id = shift;

  my $oldest_db_name = $self->list_dbnames->[-1];
  my $dbname = $arch_id->db_name;

  if ($dbname eq $oldest_db_name) {
    return [$arch_id];
  }
1165

Monika Komorowska's avatar
Monika Komorowska committed
1166 1167
  my $old = [];
  my @result = ();
1168

Monika Komorowska's avatar
Monika Komorowska committed
1169
  push @$old, $arch_id;
1170

Monika Komorowska's avatar
Monika Komorowska committed
1171 1172 1173 1174 1175
  while ($dbname ne $oldest_db_name) {
    my $new = [];
    while (my $asi = (shift @$old)) {
      push @$new, @{ $asi->get_all_predecessors };
    }
1176

Monika Komorowska's avatar
Monika Komorowska committed
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    if( @$new ) {
      $dbname = $new->[0]->db_name;
    } else {
      last;
    }
    
    # filter duplicates
    my %unique = map { join(":", $_->stable_id, $_->version, $_->release) =>
      $_ } @$new;
    @$new = values %unique;
    
    @$old = @$new;
    push @result, @$new;
  }
1191

Monika Komorowska's avatar
Monika Komorowska committed
1192
  return \@result;
1193 1194 1195
}


1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
=head2 fetch_stable_id_event

  Arg [1]     : Bio::EnsEMBL::ArchiveStableId $arch_id
  Arg [2]     : stable_id
  Example     : my $archive = $archive_stable_id_adaptor->fetch_by_stable_id($id);
                my $event = $archive_stable_id_adaptor($archive, $id2);
  Description : Gives back the event that links an archive stable id
                to a specific stable id
                
  Returntype  : Bio::EnsEMBL::StableIdEvent
                Undef if no event was found
  Exceptions  : none
  Caller      : general
  Status      : At Risk
              : under development

=cut


sub fetch_stable_id_event {
  my $self = shift;
  my $arch_id = shift;
  my $stable_id = shift;

  my $event;

  my $sql = qq(
    SELECT sie.old_stable_id, sie.old_version, sie.new_stable_id, sie.new_version, sie.type, sie.score,
           ms.old_db_name, ms.new_db_name, ms.old_release, ms.new_release, ms.old_assembly, ms.new_assembly
    FROM stable_id_event sie, mapping_session ms
    WHERE ms.mapping_session_id = sie.mapping_session_id
    AND (old_stable_id = ? AND ms.old_db_name = ? AND old_release = ? AND old_assembly = ? AND new_stable_id = ?)
    OR (new_stable_id = ? AND ms.new_db_name = ? AND new_release = ? AND new_assembly = ? AND old_stable_id = ?)
  );

  my $sth = $self->prepare($sql);
  $sth->bind_param(1, $arch_id->stable_id, SQL_VARCHAR);
  $sth->bind_param(2, $arch_id->db_name, SQL_VARCHAR);
  $sth->bind_param(3, $arch_id->release, SQL_INTEGER);
  $sth->bind_param(4, $arch_id->assembly, SQL_VARCHAR);
  $sth->bind_param(5, $stable_id, SQL_VARCHAR);
  $sth->bind_param(6, $arch_id->stable_id, SQL_VARCHAR);
  $sth->bind_param(7, $arch_id->db_name, SQL_VARCHAR);
  $sth->bind_param(8, $arch_id->release, SQL_INTEGER);
  $sth->bind_param(9, $arch_id->assembly, SQL_VARCHAR);
  $sth->bind_param(10, $stable_id, SQL_VARCHAR);
  $sth->execute();

  my ($old_stable_id, $old_version, $new_stable_id, $new_version, $type, $score);
  my ($old_db_name, $new_db_name, $old_release, $new_release, $old_assembly, $new_assembly);
  $sth->bind_columns(\$old_stable_id, \$old_version, \$new_stable_id, \$new_version, \$type, \$score,
                     \$old_db_name, \$new_db_name, \$old_release, \$new_release, \$old_assembly, \$new_assembly);

  while ($sth->fetch) {
    if ($new_stable_id eq $stable_id) {

      my $alt_id = Bio::EnsEMBL::ArchiveStableId->new(
          -stable_id => $new_stable_id,
          -version => $new_version,
          -db_name => $new_db_name,
          -release => $new_release,
          -assembly => $new_assembly,
          -type => $type,
          -adaptor => $self
      );

      $event = Bio::EnsEMBL::StableIdEvent->new(
        -old_id => $arch_id,
        -new_id => $alt_id,
        -score => $score
      );

    } elsif ($old_stable_id eq $stable_id) {

      my $alt_id = Bio::EnsEMBL::ArchiveStableId->new(
          -stable_id => $old_stable_id,
          -version => $old_version,
          -db_name => $old_db_name,
          -release => $old_release,
          -assembly => $old_assembly,
          -type => $type,
          -adaptor => $self
      );

      $event = Bio::EnsEMBL::StableIdEvent->new(
        -old_id => $alt_id,
        -new_id => $arch_id,
        -score => $score
      );

    }
  }
  $sth->finish();

  return $event;
}


Monika Komorowska's avatar
Monika Komorowska committed
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
=head2 list_dbnames

  Args        : none
  Example     : none
  Description : A list of available database names from the latest (current) to
                the oldest (ordered).
  Returntype  : listref of strings
  Exceptions  : none
  Caller      : general
  Status      : At Risk
              : under development
1305

Monika Komorowska's avatar
Monika Komorowska committed
1306
=cut
1307

Monika Komorowska's avatar
Monika Komorowska committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
sub list_dbnames {
  my $self = shift;
  
  if( ! defined $self->{'dbnames'} ) {

    my $sql = qq(
      SELECT old_db_name, new_db_name
      FROM mapping_session
      ORDER BY created DESC
    );
    my $sth = $self->prepare( $sql );
    $sth->execute();
    my ( $old_db_name, $new_db_name );
    
    my @dbnames = ();
    my %seen;

    $sth->bind_columns( \$old_db_name, \$new_db_name );

    while( $sth->fetch() ) {
      # this code now can deal with non-chaining mapping sessions
      push(@{ $self->{'dbnames'} }, $new_db_name) unless ($seen{$new_db_name});
      $seen{$new_db_name} = 1;

      push(@{ $self->{'dbnames'} }, $old_db_name) unless ($seen{$old_db_name});
      $seen{$old_db_name} = 1;
    }
1335

Monika Komorowska's avatar
Monika Komorowska committed
1336 1337
    $sth->finish();
    
Monika Komorowska's avatar
Monika Komorowska committed
1338
  }