| 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::StableIdHistoryTree - object representing a stable ID history tree | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | my $registry = "Bio::EnsEMBL::Registry"; | ||
| 38 | my $archiveStableIdAdaptor = | ||
| 39 | $registry->get_adaptor( 'human', 'core', 'ArchiveStableId' ); | ||
| 40 | |||
| 41 | my $stable_id = 'ENSG00000068990'; | ||
| 42 | my $history = | ||
| 43 | $archiveStableIdAdaptor->fetch_history_tree_by_stable_id('ENSG01'); | ||
| 44 | |||
| 45 | print "Unique stable IDs in this tree:\n"; | ||
| 46 | print join( ", ", @{ $history->get_unique_stable_ids } ), "\n"; | ||
| 47 | |||
| 48 | print "\nReleases in this tree:\n"; | ||
| 49 | print join( ", ", @{ $history->get_release_display_names } ), "\n"; | ||
| 50 | |||
| 51 | print "\nCoordinates of nodes in the tree:\n\n"; | ||
| 52 | foreach my $a ( @{ $history->get_all_ArchiveStableIds } ) { | ||
| 53 | print " Stable ID: " . $a->stable_id . "." . $a->version . "\n"; | ||
| 54 | print " Release: " | ||
| 55 | . $a->release . " (" | ||
| 56 | . $a->assembly . ", " | ||
| 57 | . $a->db_name . ")\n"; | ||
| 58 | print " coords: " | ||
| 59 | . join( ', ', @{ $history->coords_by_ArchiveStableId($a) } ) | ||
| 60 | . "\n\n"; | ||
| 61 | } | ||
| 62 | |||
| 63 | =head1 DESCRIPTION | ||
| 64 | |||
| 65 | This object represents a stable ID history tree graph. | ||
| 66 | |||
| 67 | The graph is implemented as a collection of nodes (ArchiveStableId | ||
| 68 | objects) and links (StableIdEvent objects) which have positions | ||
| 69 | on an (x,y) grid. The x axis is used for releases, the y axis for | ||
| 70 | stable_ids. The idea is to create a plot similar to this (the numbers | ||
| 71 | shown on the nodes are the stable ID versions): | ||
| 72 | |||
| 73 | ENSG001 1-------------- 2-- | ||
| 74 | \ | ||
| 75 | ENSG003 1-----1 | ||
| 76 | / | ||
| 77 | ENSG002 1-------2---------- | ||
| 78 | |||
| 79 | 38 39 40 41 42 | ||
| 80 | |||
| 81 | The grid coordinates of the ArchiveStableId objects in this example | ||
| 82 | would be (note that coordinates are zero-based): | ||
| 83 | |||
| 84 | ENSG001.1 (0, 0) | ||
| 85 | ENSG001.2 (2, 0) | ||
| 86 | ENSG003.1 (release 41) (3, 1) | ||
| 87 | ENSG003.1 (release 42) (4, 1) | ||
| 88 | ENSG002.1 (0, 2) | ||
| 89 | ENSG002.2 (1, 2) | ||
| 90 | |||
| 91 | The tree will only contain those nodes which had a change in the stable | ||
| 92 | ID version. Therefore, in the above example, in release 39 ENSG001 was | ||
| 93 | present and had version 1 (but will not be drawn there, to unclutter the | ||
| 94 | output). | ||
| 95 | |||
| 96 | The grid positions will be calculated by the API and will try to | ||
| 97 | untangle the tree (i.e. try to avoid overlapping lines). | ||
| 98 | |||
| 99 | =head1 METHODS | ||
| 100 | |||
| 101 | new | ||
| 102 | add_ArchiveStableIds | ||
| 103 | add_ArchiveStableIds_for_events | ||
| 104 | remove_ArchiveStableId | ||
| 105 | flush_ArchiveStableIds | ||
| 106 | add_StableIdEvents | ||
| 107 | remove_StableIdEvent | ||
| 108 | flush_StableIdEvents | ||
| 109 | get_all_ArchiveStableIds | ||
| 110 | get_all_StableIdEvents | ||
| 111 | get_latest_StableIdEvent | ||
| 112 | get_release_display_names | ||
| 113 | get_release_db_names | ||
| 114 | get_unique_stable_ids | ||
| 115 | optimise_tree | ||
| 116 | coords_by_ArchiveStableId | ||
| 117 | calculate_coords | ||
| 118 | consolidate_tree | ||
| 119 | reset_tree | ||
| 120 | current_dbname | ||
| 121 | current_release | ||
| 122 | current_assembly | ||
| 123 | is_incomplete | ||
| 124 | |||
| 125 | =head1 RELATED MODULES | ||
| 126 | |||
| 127 | Bio::EnsEMBL::ArchiveStableId | ||
| 128 | Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor | ||
| 129 | Bio::EnsEMBL::StableIdEvent | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | package Bio::EnsEMBL::StableIdHistoryTree; | ||
| 134 | $Bio::EnsEMBL::StableIdHistoryTree::VERSION = '114.0.0_50'; # TRIAL | ||
| 135 | $Bio::EnsEMBL::StableIdHistoryTree::VERSION = '114.0.050'; | ||
| 136 | use strict; | ||
| 137 | use warnings; | ||
| 138 | no warnings 'uninitialized'; | ||
| 139 | |||
| 140 | use Bio::EnsEMBL::Utils::Argument qw(rearrange); | ||
| 141 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 142 | use Time::HiRes; | ||
| 143 | |||
| 144 | |||
| 145 | =head2 new | ||
| 146 | |||
| 147 | Arg [CURRENT_DBNAME] : (optional) name of current db | ||
| 148 | Arg [CURRENT_RELEASE] : (optional) current release number | ||
| 149 | Arg [CURRENT_ASSEMBLY] : (optional) current assembly name | ||
| 150 | Example : my $history = Bio::EnsEMBL::StableIdHistoryTree->new; | ||
| 151 | Description : object constructor | ||
| 152 | Return type : Bio::EnsEMBL::StableIdHistoryTree | ||
| 153 | Exceptions : none | ||
| 154 | Caller : general | ||
| 155 | Status : At Risk | ||
| 156 | : under development | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | sub new { | ||
| 161 | my $caller = shift; | ||
| 162 | my $class = ref($caller) || $caller; | ||
| 163 | |||
| 164 | my $self = {}; | ||
| 165 | bless $self, $class; | ||
| 166 | |||
| 167 | my ($current_dbname, $current_release, $current_assembly) = | ||
| 168 | rearrange([qw( CURRENT_DBNAME CURRENT_RELEASE CURRENT_ASSEMBLY )], @_ ); | ||
| 169 | |||
| 170 | # initialise | ||
| 171 | $self->{'current_dbname'} = $current_dbname; | ||
| 172 | $self->{'current_release'} = $current_release; | ||
| 173 | $self->{'current_assembly'} = $current_assembly; | ||
| 174 | |||
| 175 | return $self; | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | =head2 add_ArchiveStableIds | ||
| 180 | |||
| 181 | Arg[1..n] : Bio::EnsEMBL::ArchiveStableId's @archive_ids | ||
| 182 | The ArchiveStableIds to add to the the history tree | ||
| 183 | Example : my $archive_id = $archiveStableIdAdaptor->fetch_by_stable_id( | ||
| 184 | 'ENSG00024808'); | ||
| 185 | $history->add_ArchiveStableId($archive_id); | ||
| 186 | Description : Adds ArchiveStableIds (nodes) to the history tree. No | ||
| 187 | calculation of grid coordinates is done at this point, you need | ||
| 188 | to initiate this manually with calculate_coords(). | ||
| 189 | ArchiveStableIds are only added once for each release (to avoid | ||
| 190 | duplicates). | ||
| 191 | Return type : none | ||
| 192 | Exceptions : thrown on invalid or missing argument | ||
| 193 | Caller : Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor::fetch_history_by_stable_id, general | ||
| 194 | Status : At Risk | ||
| 195 | : under development | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub add_ArchiveStableIds { | ||
| 200 | my ($self, @archive_ids) = @_; | ||
| 201 | |||
| 202 | throw("You must provide one or more Bio::EnsEMBL::ArchiveStableIds to add.") | ||
| 203 | unless (@archive_ids); | ||
| 204 | |||
| 205 | foreach my $archive_id (@archive_ids) { | ||
| 206 | throw("Bio::EnsEMBL::ArchiveStableId object expected.") | ||
| 207 | unless (ref($archive_id) && | ||
| 208 | $archive_id->isa('Bio::EnsEMBL::ArchiveStableId')); | ||
| 209 | |||
| 210 | $self->{'nodes'}->{$self->_node_id($archive_id)} = $archive_id; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | =head2 add_ArchiveStableIds_for_events | ||
| 216 | |||
| 217 | Example : my $history = Bio::EnsEMBL::StableIdHistoryTree->new; | ||
| 218 | $history->add_StableIdEvents($event1, $event2); | ||
| 219 | $history->add_ArchiveStableIds_for_events; | ||
| 220 | Description : Convenience method that adds all ArchiveStableIds for all | ||
| 221 | StableIdEvents attached to this object to the tree. | ||
| 222 | Return type : none | ||
| 223 | Exceptions : none | ||
| 224 | Caller : Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor::fetch_history_by_stable_id, general | ||
| 225 | Status : At Risk | ||
| 226 | : under development | ||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | sub add_ArchiveStableIds_for_events { | ||
| 231 | my $self = shift; | ||
| 232 | |||
| 233 | foreach my $event (@{ $self->get_all_StableIdEvents }) { | ||
| 234 | if ($event->old_ArchiveStableId) { | ||
| 235 | $self->add_ArchiveStableIds($event->old_ArchiveStableId); | ||
| 236 | } | ||
| 237 | if ($event->new_ArchiveStableId) { | ||
| 238 | $self->add_ArchiveStableIds($event->new_ArchiveStableId); | ||
| 239 | } | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | =head2 remove_ArchiveStableId | ||
| 245 | |||
| 246 | Arg[1] : Bio::EnsEMBL::ArchiveStableId $archive_id | ||
| 247 | the ArchiveStableId to remove from the tree | ||
| 248 | Example : $history->remove_ArchiveStableId($archive_id); | ||
| 249 | Description : Removes an ArchiveStableId from the tree. | ||
| 250 | Return type : none | ||
| 251 | Exceptions : thrown on missing or invalid argument | ||
| 252 | Caller : Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor::fetch_history_by_stable_id, general | ||
| 253 | Status : At Risk | ||
| 254 | : under development | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | sub remove_ArchiveStableId { | ||
| 259 | my ($self, $archive_id) = @_; | ||
| 260 | |||
| 261 | throw("Bio::EnsEMBL::ArchiveStableId object expected.") | ||
| 262 | unless ($archive_id && ref($archive_id) && | ||
| 263 | $archive_id->isa('Bio::EnsEMBL::ArchiveStableId')); | ||
| 264 | |||
| 265 | my %nodes = %{ $self->{'nodes'} }; | ||
| 266 | delete $nodes{$self->_node_id($archive_id)}; | ||
| 267 | $self->{'nodes'} = \%nodes; | ||
| 268 | } | ||
| 269 | |||
| 270 | |||
| 271 | =head2 flush_ArchiveStableIds | ||
| 272 | |||
| 273 | Example : $history->flush_ArchiveStableIds; | ||
| 274 | Description : Remove all ArchiveStableIds from the tree. | ||
| 275 | Return type : none | ||
| 276 | Exceptions : none | ||
| 277 | Caller : general | ||
| 278 | Status : At Risk | ||
| 279 | : under development | ||
| 280 | |||
| 281 | =cut | ||
| 282 | |||
| 283 | sub flush_ArchiveStableIds { | ||
| 284 | my $self = shift; | ||
| 285 | $self->{'nodes'} = undef; | ||
| 286 | } | ||
| 287 | |||
| 288 | |||
| 289 | # | ||
| 290 | # generate a unique node identifier | ||
| 291 | # | ||
| 292 | sub _node_id { | ||
| 293 | my ($self, $archive_id) = @_; | ||
| 294 | return $archive_id->stable_id . ':' . $archive_id->db_name; | ||
| 295 | } | ||
| 296 | |||
| 297 | |||
| 298 | =head2 add_StableIdEvents | ||
| 299 | |||
| 300 | Arg[1..n] : Bio::EnsEMBL::StableIdEvent's @events | ||
| 301 | The StableIdEvents to add to the the history tree | ||
| 302 | Example : $history->add_StableIdEvents($event); | ||
| 303 | Description : Adds StableIdEvents (links) to the history tree. Note that | ||
| 304 | ArchiveStableIds attached to the StableIdEvent aren't added to | ||
| 305 | the tree automatically, you'll need to call | ||
| 306 | add_ArchiveStableIds_for_events later. | ||
| 307 | Return type : none | ||
| 308 | Exceptions : thrown on invalid or missing argument | ||
| 309 | Caller : Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor::fetch_history_by_stable_id, general | ||
| 310 | Status : At Risk | ||
| 311 | : under development | ||
| 312 | |||
| 313 | =cut | ||
| 314 | |||
| 315 | sub add_StableIdEvents { | ||
| 316 | my ($self, @events) = @_; | ||
| 317 | |||
| 318 | throw("You must provide one or more Bio::EnsEMBL::StableIdsEvents to add.") | ||
| 319 | unless (@events); | ||
| 320 | |||
| 321 | foreach my $event (@events) { | ||
| 322 | throw("Bio::EnsEMBL::StableIdEvent object expected.") | ||
| 323 | unless ($event->isa('Bio::EnsEMBL::StableIdEvent')); | ||
| 324 | |||
| 325 | $self->{'links'}->{$self->_link_id($event)} = $event; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | |||
| 330 | =head2 remove_StableIdEvent | ||
| 331 | |||
| 332 | Arg[1] : Bio::EnsEMBL::StableIdEvent $event | ||
| 333 | the StableIdEvent to remove from the tree | ||
| 334 | Example : $history->remove_StableIdEvent($event); | ||
| 335 | Description : Removes a StableIdEvent from the tree. | ||
| 336 | Return type : none | ||
| 337 | Exceptions : thrown on missing or invalid arguments | ||
| 338 | Caller : Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor::fetch_history_by_stable_id, general | ||
| 339 | Status : At Risk | ||
| 340 | : under development | ||
| 341 | |||
| 342 | =cut | ||
| 343 | |||
| 344 | sub remove_StableIdEvent { | ||
| 345 | my ($self, $event) = @_; | ||
| 346 | |||
| 347 | throw("Bio::EnsEMBL::StableIdEvent object expected.") unless | ||
| 348 | ($event && ref($event) && $event->isa('Bio::EnsEMBL::StableIdEvent')); | ||
| 349 | |||
| 350 | my %links = %{ $self->{'links'} }; | ||
| 351 | delete $links{$self->_link_id($event)}; | ||
| 352 | $self->{'links'} = \%links; | ||
| 353 | } | ||
| 354 | |||
| 355 | |||
| 356 | =head2 flush_StableIdEvents | ||
| 357 | |||
| 358 | Example : $history->flush_StableIdEvents; | ||
| 359 | Description : Removes all StableIdEvents from the tree. | ||
| 360 | Return type : none | ||
| 361 | Exceptions : none | ||
| 362 | Caller : general | ||
| 363 | Status : At Risk | ||
| 364 | : under development | ||
| 365 | |||
| 366 | =cut | ||
| 367 | |||
| 368 | sub flush_StableIdEvents { | ||
| 369 | my $self = shift; | ||
| 370 | $self->{'links'} = undef; | ||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | # | ||
| 375 | # generate a unique link identifier | ||
| 376 | # | ||
| 377 | sub _link_id { | ||
| 378 | my ($self, $event) = @_; | ||
| 379 | |||
| 380 | my ($old_id, $old_db_name, $new_id, $new_db_name); | ||
| 381 | if ($event->old_ArchiveStableId) { | ||
| 382 | $old_id = $event->old_ArchiveStableId->stable_id; | ||
| 383 | $old_db_name = $event->old_ArchiveStableId->db_name; | ||
| 384 | } | ||
| 385 | if ($event->new_ArchiveStableId) { | ||
| 386 | $new_id = $event->new_ArchiveStableId->stable_id; | ||
| 387 | $new_db_name = $event->new_ArchiveStableId->db_name; | ||
| 388 | } | ||
| 389 | |||
| 390 | return join(':', $old_id, $old_db_name, $new_id, $new_db_name); | ||
| 391 | } | ||
| 392 | |||
| 393 | |||
| 394 | =head2 get_all_ArchiveStableIds | ||
| 395 | |||
| 396 | Example : foreach my $arch_id (@{ $history->get_all_ArchiveStableIds }) { | ||
| 397 | print $arch_id->stable_id, '.', $arch_id->version, "\n"; | ||
| 398 | } | ||
| 399 | Description : Gets all ArchiveStableIds (nodes) in this tree. | ||
| 400 | Return type : Arrayref of Bio::EnsEMBL::ArchiveStableId objects | ||
| 401 | Exceptions : none | ||
| 402 | Caller : general | ||
| 403 | Status : At Risk | ||
| 404 | : under development | ||
| 405 | |||
| 406 | =cut | ||
| 407 | |||
| 408 | sub get_all_ArchiveStableIds { | ||
| 409 | my $self = shift; | ||
| 410 | return [ values %{ $self->{'nodes'} } ]; | ||
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | =head2 get_all_current_ArchiveStableIds | ||
| 415 | |||
| 416 | Example : foreach my $arch_id (@{ $history->get_all_current_ArchiveStableIds }) { | ||
| 417 | print $arch_id->stable_id, '.', $arch_id->version, "\n"; | ||
| 418 | } | ||
| 419 | Description : Convenience method to get all current ArchiveStableIds in this | ||
| 420 | tree. | ||
| 421 | |||
| 422 | Note that no lazy loading of "current" status is done at that | ||
| 423 | stage; as long as you retrieve your StableIdHistoryTree object | ||
| 424 | from ArchiveStableIdAdaptor, you'll get the right answer. In | ||
| 425 | other use cases, if you want to make sure you really get all | ||
| 426 | current stable IDs, loop over the result of | ||
| 427 | get_all_ArchiveStableIds() and call | ||
| 428 | ArchiveStableId->current_version() on all of them. | ||
| 429 | Return type : Arrayref of Bio::EnsEMBL::ArchiveStableId objects | ||
| 430 | Exceptions : none | ||
| 431 | Caller : general | ||
| 432 | Status : At Risk | ||
| 433 | : under development | ||
| 434 | |||
| 435 | =cut | ||
| 436 | |||
| 437 | sub get_all_current_ArchiveStableIds { | ||
| 438 | my $self = shift; | ||
| 439 | |||
| 440 | my @current = (); | ||
| 441 | |||
| 442 | foreach my $arch_id (@{ $self->get_all_ArchiveStableIds }) { | ||
| 443 | push @current, $arch_id if ($arch_id->is_current); | ||
| 444 | } | ||
| 445 | |||
| 446 | return \@current; | ||
| 447 | } | ||
| 448 | |||
| 449 | |||
| 450 | =head2 get_all_StableIdEvents | ||
| 451 | |||
| 452 | Example : foreach my $event (@{ $history->get_all_StableIdsEvents }) { | ||
| 453 | print "Old stable ID: ", | ||
| 454 | ($event->get_attribute('old', 'stable_id') or 'none'), "\n"; | ||
| 455 | print "New stable ID: ", | ||
| 456 | ($event->get_attribute('new', 'stable_id') or 'none'), "\n"; | ||
| 457 | print "Mapping score: ", $event->score, "\n"; | ||
| 458 | } | ||
| 459 | Description : Gets all StableIdsEvents (links) in this tree. | ||
| 460 | Return type : Arrayref of Bio::EnsEMBL::StableIdEvent objects | ||
| 461 | Exceptions : none | ||
| 462 | Caller : general | ||
| 463 | Status : At Risk | ||
| 464 | : under development | ||
| 465 | |||
| 466 | =cut | ||
| 467 | |||
| 468 | sub get_all_StableIdEvents { | ||
| 469 | my $self = shift; | ||
| 470 | return [ values %{ $self->{'links'} } ]; | ||
| 471 | } | ||
| 472 | |||
| 473 | |||
| 474 | =head2 get_latest_StableIdEvent | ||
| 475 | |||
| 476 | Arg[1] : Bio::EnsEMBL::ArchiveStableId $arch_id - the stable ID to get | ||
| 477 | the latest Event for | ||
| 478 | Example : my $arch_id = Bio::EnsEMBL::ArchiveStableId->new( | ||
| 479 | -stable_id => 'ENSG00001' | ||
| 480 | ); | ||
| 481 | my $event = $history->get_latest_Event($arch_id); | ||
| 482 | Description : Returns the latest StableIdEvent found in the tree where a given | ||
| 483 | stable ID is the new stable ID. If more than one is found (e.g. | ||
| 484 | in a merge scenario in the latest mapping), preference is given | ||
| 485 | to self-events. | ||
| 486 | Return type : Bio::EnsEMBL::StableIdEvent | ||
| 487 | Exceptions : thrown on missing or wrong argument | ||
| 488 | Caller : Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor::add_all_current_to_history, general | ||
| 489 | Status : At Risk | ||
| 490 | : under development | ||
| 491 | |||
| 492 | =cut | ||
| 493 | |||
| 494 | sub get_latest_StableIdEvent { | ||
| 495 | my $self = shift; | ||
| 496 | my $arch_id = shift; | ||
| 497 | |||
| 498 | unless ($arch_id and $arch_id->isa('Bio::EnsEMBL::ArchiveStableId')) { | ||
| 499 | throw("Need a Bio::EnsEMBL::ArchiveStableId."); | ||
| 500 | } | ||
| 501 | |||
| 502 | my @all_events = @{ $self->get_all_StableIdEvents }; | ||
| 503 | my @self_events = (); | ||
| 504 | |||
| 505 | while (my $event = shift(@all_events)) { | ||
| 506 | if ($event->new_ArchiveStableId and | ||
| 507 | $event->new_ArchiveStableId->stable_id eq $arch_id->stable_id) { | ||
| 508 | push @self_events, $event; | ||
| 509 | } | ||
| 510 | } | ||
| 511 | |||
| 512 | my @sorted = sort { $b->new_ArchiveStableId->release <=> | ||
| 513 | $a->new_ArchiveStableId->release } @self_events; | ||
| 514 | |||
| 515 | # give priority to self events | ||
| 516 | my $latest; | ||
| 517 | while ($latest = shift @sorted) { | ||
| 518 | last if (($latest->old_ArchiveStableId and | ||
| 519 | $latest->old_ArchiveStableId->stable_id eq $arch_id->stable_id) | ||
| 520 | or !$latest->old_ArchiveStableId); | ||
| 521 | } | ||
| 522 | |||
| 523 | return $latest; | ||
| 524 | } | ||
| 525 | |||
| 526 | |||
| 527 | =head2 get_release_display_names | ||
| 528 | |||
| 529 | Example : print "Unique release display_names in this tree:\n" | ||
| 530 | foreach my $name (@{ $history->get_release_display_names }) { | ||
| 531 | print " $name\n"; | ||
| 532 | } | ||
| 533 | Description : Returns a chronologically sorted list of unique release | ||
| 534 | display_names in this tree. | ||
| 535 | |||
| 536 | This method can be used to determine the number of columns when | ||
| 537 | plotting the history tree. | ||
| 538 | Return type : Arrayref of strings. | ||
| 539 | Exceptions : none | ||
| 540 | Caller : general | ||
| 541 | Status : At Risk | ||
| 542 | : under development | ||
| 543 | |||
| 544 | =cut | ||
| 545 | |||
| 546 | sub get_release_display_names { | ||
| 547 | my $self = shift; | ||
| 548 | |||
| 549 | my @display_names = map { $_->[1] } @{ $self->_sort_releases }; | ||
| 550 | |||
| 551 | return \@display_names; | ||
| 552 | } | ||
| 553 | |||
| 554 | |||
| 555 | =head2 get_release_db_names | ||
| 556 | |||
| 557 | Example : print "Unique release db_names in this tree:\n" | ||
| 558 | foreach my $name (@{ $history->get_release_db_names }) { | ||
| 559 | print " $name\n"; | ||
| 560 | } | ||
| 561 | Description : Returns a chronologically sorted list of unique release | ||
| 562 | db_names in this tree. | ||
| 563 | Return type : Arrayref of strings. | ||
| 564 | Exceptions : none | ||
| 565 | Caller : general | ||
| 566 | Status : At Risk | ||
| 567 | : under development | ||
| 568 | |||
| 569 | =cut | ||
| 570 | |||
| 571 | sub get_release_db_names { | ||
| 572 | my $self = shift; | ||
| 573 | |||
| 574 | my @db_names = map { $_->[0] } @{ $self->_sort_releases }; | ||
| 575 | |||
| 576 | return \@db_names; | ||
| 577 | } | ||
| 578 | |||
| 579 | |||
| 580 | # | ||
| 581 | # Create a chronologically sorted list of releases. | ||
| 582 | # | ||
| 583 | # Return type : Arrayref of arrayrefs (db_name, release) | ||
| 584 | # | ||
| 585 | sub _sort_releases { | ||
| 586 | my $self = shift; | ||
| 587 | |||
| 588 | unless ($self->{'sorted_tree'}->{'releases'}) { | ||
| 589 | |||
| 590 | my %unique = (); | ||
| 591 | |||
| 592 | foreach my $archive_id (@{ $self->get_all_ArchiveStableIds }) { | ||
| 593 | $unique{join(':', $archive_id->db_name, $archive_id->release)} = 1; | ||
| 594 | } | ||
| 595 | |||
| 596 | # sort releases by release number, then db_name; this should get them into | ||
| 597 | # chronological order | ||
| 598 | my @releases = sort { $a->[1] <=> $b->[1] || $a->[0] cmp $b->[0] } | ||
| 599 | map { [ split(/:/, $_) ] } keys(%unique); | ||
| 600 | |||
| 601 | $self->{'sorted_tree'}->{'releases'} = \@releases; | ||
| 602 | |||
| 603 | } | ||
| 604 | |||
| 605 | return $self->{'sorted_tree'}->{'releases'}; | ||
| 606 | } | ||
| 607 | |||
| 608 | |||
| 609 | =head2 get_unique_stable_ids | ||
| 610 | |||
| 611 | Example : print "Unique stable IDs in this tree:\n" | ||
| 612 | foreach my $id (@{ $history->get_unique_stable_ids }) { | ||
| 613 | print " $id\n"; | ||
| 614 | } | ||
| 615 | Description : Returns a list of unique stable IDs in this tree. Version is not | ||
| 616 | taken into account here. This method can be used to determine | ||
| 617 | the number of rows when plotting the history with each stable ID | ||
| 618 | occupying one line. | ||
| 619 | |||
| 620 | Sort algorithm will depend on what was chosen when the sorted | ||
| 621 | tree was generated. This ranges from a simple alphanumeric sort | ||
| 622 | to algorithms trying to untangle the history tree. If no | ||
| 623 | pre-sorted data is found, an alphanumerically sorted list will | ||
| 624 | be returned by default. | ||
| 625 | Return type : Arrayref of strings. | ||
| 626 | Exceptions : none | ||
| 627 | Caller : general | ||
| 628 | Status : At Risk | ||
| 629 | : under development | ||
| 630 | |||
| 631 | =cut | ||
| 632 | |||
| 633 | sub get_unique_stable_ids { | ||
| 634 | my $self = shift; | ||
| 635 | |||
| 636 | unless ($self->{'sorted_tree'}->{'stable_ids'}) { | ||
| 637 | $self->{'sorted_tree'}->{'stable_ids'} = $self->_sort_stable_ids; | ||
| 638 | } | ||
| 639 | |||
| 640 | return $self->{'sorted_tree'}->{'stable_ids'}; | ||
| 641 | } | ||
| 642 | |||
| 643 | |||
| 644 | # | ||
| 645 | # Returns a list of stable IDs in this history tree, sorted alphabetically. | ||
| 646 | # This is the simplest sort function used and doesn't try to untangle the tree. | ||
| 647 | # | ||
| 648 | # Return type : Arrayref | ||
| 649 | # | ||
| 650 | sub _sort_stable_ids { | ||
| 651 | my $self = shift; | ||
| 652 | my %unique = map { $_->stable_id => 1 } @{ $self->get_all_ArchiveStableIds }; | ||
| 653 | return [sort keys %unique]; | ||
| 654 | } | ||
| 655 | |||
| 656 | |||
| 657 | =head2 optimise_tree | ||
| 658 | |||
| 659 | Arg [1] : (optional) Float $time_limit | ||
| 660 | Optimise tree normally runs until it hits a minimised state | ||
| 661 | but this can take a very long time. Therefore you can | ||
| 662 | opt to bail out of the optimisation early. Specify the | ||
| 663 | time in seconds. Floating point values are supported should you | ||
| 664 | require sub-second limits | ||
| 665 | Example : $history->optimise_tree; | ||
| 666 | Description : This method sorts the history tree so that the number of | ||
| 667 | overlapping branches is minimised (thus "untangling" the tree). | ||
| 668 | |||
| 669 | It uses a clustering algorithm for this which iteratively moves | ||
| 670 | the nodes with the largest vertical distance next to each other | ||
| 671 | and looking for a mininum in total branch length. This might not | ||
| 672 | produce the overall optimum but usually converges on a local | ||
| 673 | optimum very quickly. | ||
| 674 | Return type : none | ||
| 675 | Exceptions : none | ||
| 676 | Caller : calculate_coords | ||
| 677 | Status : At Risk | ||
| 678 | : under development | ||
| 679 | |||
| 680 | =cut | ||
| 681 | |||
| 682 | sub optimise_tree { | ||
| 683 | my $self = shift; | ||
| 684 | my $time_limit = shift; | ||
| 685 | |||
| 686 | # get all non-self events | ||
| 687 | my @links; | ||
| 688 | foreach my $event (@{ $self->get_all_StableIdEvents }) { | ||
| 689 | next unless ($event->old_ArchiveStableId and $event->new_ArchiveStableId); | ||
| 690 | my $old_id = $event->old_ArchiveStableId->stable_id; | ||
| 691 | my $new_id = $event->new_ArchiveStableId->stable_id; | ||
| 692 | push @links, [$old_id, $new_id] if ($old_id ne $new_id); | ||
| 693 | } | ||
| 694 | |||
| 695 | # get initial list of sorted unique stable IDs and put them into a position | ||
| 696 | # lookup hash | ||
| 697 | my $i = 0; | ||
| 698 | my %pos = map { $_ => $i++ } @{ $self->_sort_stable_ids }; | ||
| 699 | |||
| 700 | my $opt_length; | ||
| 701 | my $successive_fails = 0; | ||
| 702 | my $k = 0; | ||
| 703 | my %seen; | ||
| 704 | |||
| 705 | # for debug purposes: | ||
| 706 | # find the number of permutations for the given number of stable IDs | ||
| 707 | my $fact = $self->_factorial(scalar(keys %pos)); | ||
| 708 | |||
| 709 | my $starting_time = Time::HiRes::time(); | ||
| 710 | |||
| 711 | OPT: | ||
| 712 | while ($successive_fails < 100) { | ||
| 713 | |||
| 714 | if(defined $time_limit) { | ||
| 715 | my $current_time = Time::HiRes::time(); | ||
| 716 | my $diff = $current_time - $starting_time; | ||
| 717 | last OPT if $diff > $time_limit; | ||
| 718 | } | ||
| 719 | |||
| 720 | # sort links by vertical distance | ||
| 721 | #warn "sorting\n"; | ||
| 722 | $self->_sort_links(\@links, \%pos); | ||
| 723 | |||
| 724 | # loop over sorted links | ||
| 725 | SORTED: | ||
| 726 | foreach my $link (@links) { | ||
| 727 | |||
| 728 | #warn " trying ".join('-', @$link)."\n"; | ||
| 729 | |||
| 730 | $k++; | ||
| 731 | |||
| 732 | # remember last sort order | ||
| 733 | my %last = %pos; | ||
| 734 | |||
| 735 | #my $this_order = join(':', sort { $pos{$a} <=> $pos{$b} } keys %pos); | ||
| 736 | #warn " before $this_order\n"; | ||
| 737 | |||
| 738 | # try both to move bottom node next to top node's current position and | ||
| 739 | # top node next to bottom node's position - one of the methods might give | ||
| 740 | # you better results | ||
| 741 | DIRECT: | ||
| 742 | foreach my $direction (qw(up down)) { | ||
| 743 | |||
| 744 | # move the nodes next to each other | ||
| 745 | $self->_move_nodes($link, \%pos, $direction); | ||
| 746 | |||
| 747 | # next if we've seen this sort order before | ||
| 748 | my $new_order = join(':', sort { $pos{$a} <=> $pos{$b} } keys %pos); | ||
| 749 | #warn " after ($direction) $new_order\n"; | ||
| 750 | if ($seen{$new_order}) { | ||
| 751 | #warn " seen\n"; | ||
| 752 | %pos = %last; | ||
| 753 | next DIRECT; | ||
| 754 | } | ||
| 755 | $seen{$new_order} = 1; | ||
| 756 | |||
| 757 | # calculate total link length for this sort order | ||
| 758 | my $total_length = $self->_total_link_length(\@links, \%pos); | ||
| 759 | |||
| 760 | if (!$opt_length or $total_length < $opt_length) { | ||
| 761 | #warn " better ($total_length/$opt_length)\n"; | ||
| 762 | $opt_length = $total_length; | ||
| 763 | $successive_fails = 0; | ||
| 764 | next OPT; | ||
| 765 | } else { | ||
| 766 | #warn " worse ($total_length/$opt_length)\n"; | ||
| 767 | %pos = %last; | ||
| 768 | $successive_fails++; | ||
| 769 | } | ||
| 770 | } | ||
| 771 | |||
| 772 | } | ||
| 773 | |||
| 774 | last OPT; | ||
| 775 | |||
| 776 | } | ||
| 777 | |||
| 778 | #warn "Needed $k tries (of $fact) to find optimal tree.\n"; | ||
| 779 | |||
| 780 | my @best = sort { $pos{$a} <=> $pos{$b} } keys %pos; | ||
| 781 | $self->{'sorted_tree'}->{'stable_ids'} = \@best; | ||
| 782 | } | ||
| 783 | |||
| 784 | |||
| 785 | # | ||
| 786 | # find the number of permutations for a give array size. | ||
| 787 | # used for debugging code (compare implemented algorithm to looping over all | ||
| 788 | # possible permutations). | ||
| 789 | # | ||
| 790 | sub _factorial { | ||
| 791 | my ($self, $n) = @_; | ||
| 792 | my $s = 1; | ||
| 793 | $s *= $n-- while $n > 0; | ||
| 794 | return $s; | ||
| 795 | } | ||
| 796 | |||
| 797 | |||
| 798 | # | ||
| 799 | # sort links by vertical distance | ||
| 800 | # | ||
| 801 | sub _sort_links { | ||
| 802 | my ($self, $links, $pos) = @_; | ||
| 803 | |||
| 804 | my @lookup; | ||
| 805 | |||
| 806 | foreach my $link (@$links) { | ||
| 807 | my $dist = $pos->{$link->[0]} - $pos->{$link->[1]}; | ||
| 808 | $dist = -$dist if ($dist < 0); | ||
| 809 | push @lookup, [$dist, $link]; | ||
| 810 | #warn " $dist ".join(' ', @$link)."\n"; | ||
| 811 | } | ||
| 812 | |||
| 813 | @$links = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @lookup; | ||
| 814 | } | ||
| 815 | |||
| 816 | |||
| 817 | # | ||
| 818 | # make two nodes adjacent by moving the second node next to the first node | ||
| 819 | # all other node coordinates are adjusted accordingly | ||
| 820 | # | ||
| 821 | sub _move_nodes { | ||
| 822 | my ($self, $link, $pos, $direction) = @_; | ||
| 823 | |||
| 824 | my $first_pos = $pos->{$link->[0]}; | ||
| 825 | my $second_pos = $pos->{$link->[1]}; | ||
| 826 | |||
| 827 | # swap positions if necessary | ||
| 828 | if ($first_pos > $second_pos) { | ||
| 829 | my $tmp = $second_pos; | ||
| 830 | $second_pos = $first_pos; | ||
| 831 | $first_pos = $tmp; | ||
| 832 | } | ||
| 833 | #warn " $first_pos:$second_pos\n"; | ||
| 834 | |||
| 835 | foreach my $p (keys %$pos) { | ||
| 836 | |||
| 837 | my $val = $pos->{$p}; | ||
| 838 | |||
| 839 | #warn " $p $val\n"; | ||
| 840 | if ($direction eq 'up') { | ||
| 841 | if ($val > $first_pos and $val < $second_pos) { | ||
| 842 | $val++; | ||
| 843 | } elsif ($val == $second_pos) { | ||
| 844 | $val = $first_pos + 1; | ||
| 845 | } | ||
| 846 | } else { | ||
| 847 | if ($val > $first_pos and $val < $second_pos) { | ||
| 848 | $val--; | ||
| 849 | } elsif ($val == $first_pos) { | ||
| 850 | $val = $second_pos - 1; | ||
| 851 | } | ||
| 852 | } | ||
| 853 | |||
| 854 | #warn " $p $val\n"; | ||
| 855 | $pos->{$p} = $val; | ||
| 856 | #warn "\n"; | ||
| 857 | } | ||
| 858 | } | ||
| 859 | |||
| 860 | |||
| 861 | # | ||
| 862 | # calculate the total link (vertical distance) length based on this sort order | ||
| 863 | # | ||
| 864 | sub _total_link_length { | ||
| 865 | my ($self, $links, $pos) = @_; | ||
| 866 | |||
| 867 | my $total_length; | ||
| 868 | |||
| 869 | foreach my $link (@$links) { | ||
| 870 | my $length = $pos->{$link->[0]} - $pos->{$link->[1]}; | ||
| 871 | $length = -$length if ($length < 0); | ||
| 872 | $total_length += $length; | ||
| 873 | } | ||
| 874 | |||
| 875 | return $total_length; | ||
| 876 | } | ||
| 877 | |||
| 878 | |||
| 879 | =head2 coords_by_ArchiveStableId | ||
| 880 | |||
| 881 | Arg[1] : Bio::EnsEMBL::ArchiveStableId $archive_id | ||
| 882 | The ArchiveStableId to get tree grid coordinates for | ||
| 883 | Example : my ($x, $y) = | ||
| 884 | @{ $history->coords_by_ArchiveStableId($archive_id) }; | ||
| 885 | print $archive_id->stable_id, " coords: $x, $y\n"; | ||
| 886 | Description : Returns the coordinates of an ArchiveStableId in the history | ||
| 887 | tree grid. If the ArchiveStableId isn't found in this tree, an | ||
| 888 | empty list is returned. | ||
| 889 | |||
| 890 | Coordinates are zero-based (i.e. the top leftmost element in | ||
| 891 | the grid has coordinates [0, 0], not [1, 1]). This is to | ||
| 892 | facilitate using them to create a matrix as a two-dimensional | ||
| 893 | array of arrays. | ||
| 894 | Return type : Arrayref (x coordinate, y coordinate) | ||
| 895 | Exceptions : thrown on wrong argument type | ||
| 896 | Caller : general | ||
| 897 | Status : At Risk | ||
| 898 | : under development | ||
| 899 | |||
| 900 | =cut | ||
| 901 | |||
| 902 | sub coords_by_ArchiveStableId { | ||
| 903 | my ($self, $archive_id) = @_; | ||
| 904 | |||
| 905 | throw("Bio::EnsEMBL::ArchiveStableId object expected.") | ||
| 906 | unless ($archive_id and ref($archive_id) and | ||
| 907 | $archive_id->isa('Bio::EnsEMBL::ArchiveStableId')); | ||
| 908 | |||
| 909 | return $self->{'sorted_tree'}->{'coords'}->{$self->_node_id($archive_id)} | ||
| 910 | || []; | ||
| 911 | } | ||
| 912 | |||
| 913 | |||
| 914 | =head2 calculate_coords | ||
| 915 | |||
| 916 | Arg [1] : (optional) Float $time_limit | ||
| 917 | Optimise tree normally runs until it hits a minimised state | ||
| 918 | but this can take a very long time. Therefore you can | ||
| 919 | opt to bail out of the optimisation early. Specify the | ||
| 920 | time in seconds. Floating point values are supported should you | ||
| 921 | require sub-second limits | ||
| 922 | Example : $history->calculate_coords; | ||
| 923 | Description : Pre-calculates the grid coordinates of all nodes in the tree. | ||
| 924 | Return type : none | ||
| 925 | Exceptions : none | ||
| 926 | Caller : ArchiveStableIdAdaptor::fetch_history_by_stable_id | ||
| 927 | Status : At Risk | ||
| 928 | : under development | ||
| 929 | |||
| 930 | =cut | ||
| 931 | |||
| 932 | sub calculate_coords { | ||
| 933 | my $self = shift; | ||
| 934 | my $time_limit = shift; | ||
| 935 | |||
| 936 | # reset any previous tree cordinate calculations | ||
| 937 | $self->reset_tree; | ||
| 938 | |||
| 939 | # the "master" information for the sorted tree is stored as the sorted lists | ||
| 940 | # of releases (x) and stable IDs (y). Sort them now. | ||
| 941 | my $db_names = $self->get_release_db_names; | ||
| 942 | |||
| 943 | # untangle tree by sorting stable IDs appropriately | ||
| 944 | $self->optimise_tree($time_limit); | ||
| 945 | my $stable_ids = $self->get_unique_stable_ids; | ||
| 946 | |||
| 947 | # for performance reasons, additionally store coordinates in a lookup hash | ||
| 948 | foreach my $archive_id (@{ $self->get_all_ArchiveStableIds }) { | ||
| 949 | |||
| 950 | # coordinates are positions in the sorted lists | ||
| 951 | my $x = $self->_index_of($archive_id->db_name, $db_names); | ||
| 952 | my $y = $self->_index_of($archive_id->stable_id, $stable_ids); | ||
| 953 | |||
| 954 | $self->{'sorted_tree'}->{'coords'}->{$self->_node_id($archive_id)} = | ||
| 955 | [ $x, $y ]; | ||
| 956 | } | ||
| 957 | } | ||
| 958 | |||
| 959 | # | ||
| 960 | # Description : Returns the index of an element in an array | ||
| 961 | # Example : my @array = (a, b, c); | ||
| 962 | # my $i = _index_of('b', \@array); # will return 1 | ||
| 963 | # Return type : Int (or undef if element is not found in array) | ||
| 964 | # | ||
| 965 | sub _index_of { | ||
| 966 | my ($self, $element, $arrayref) = @_; | ||
| 967 | |||
| 968 | throw("Expecting arrayref argument.") unless (ref($arrayref) eq 'ARRAY'); | ||
| 969 | |||
| 970 | my @array = @$arrayref; | ||
| 971 | |||
| 972 | while (my $e = pop(@array)) { | ||
| 973 | return scalar(@array) if ($e eq $element); | ||
| 974 | } | ||
| 975 | |||
| 976 | return undef; | ||
| 977 | } | ||
| 978 | |||
| 979 | |||
| 980 | =head2 consolidate_tree | ||
| 981 | |||
| 982 | Example : $history->consolidate_tree; | ||
| 983 | Description : Consolidate the history tree. This means removing nodes where | ||
| 984 | there wasn't a change and bridging gaps in the history. The end | ||
| 985 | result will be a sparse tree which only contains the necessary | ||
| 986 | information. | ||
| 987 | Return type : none | ||
| 988 | Exceptions : none | ||
| 989 | Caller : ArchiveStableIdAdaptor->fetch_history_tree_by_stable_id | ||
| 990 | Status : At Risk | ||
| 991 | : under development | ||
| 992 | |||
| 993 | =cut | ||
| 994 | |||
| 995 | sub consolidate_tree { | ||
| 996 | my $self = shift; | ||
| 997 | |||
| 998 | # | ||
| 999 | # get all self-events and creations/deletions and sort them (by stable ID and | ||
| 1000 | # chronologically) | ||
| 1001 | # | ||
| 1002 | my @event_lookup; | ||
| 1003 | |||
| 1004 | foreach my $event (@{ $self->get_all_StableIdEvents }) { | ||
| 1005 | |||
| 1006 | my $old_id = $event->old_ArchiveStableId; | ||
| 1007 | my $new_id = $event->new_ArchiveStableId; | ||
| 1008 | |||
| 1009 | if (!$old_id or !$new_id or ($old_id->stable_id eq $new_id->stable_id)) { | ||
| 1010 | if ($old_id) { | ||
| 1011 | push @event_lookup, [$old_id->stable_id, $old_id->release, | ||
| 1012 | $old_id->db_name, $event]; | ||
| 1013 | } else { | ||
| 1014 | push @event_lookup, [$new_id->stable_id, $new_id->release - 1, | ||
| 1015 | $new_id->db_name, $event]; | ||
| 1016 | } | ||
| 1017 | } | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | my @self_events = map { $_->[3] } | ||
| 1021 | sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] || $a->[2] cmp $b->[2] } | ||
| 1022 | @event_lookup; | ||
| 1023 | |||
| 1024 | # | ||
| 1025 | # consolidate tree | ||
| 1026 | # | ||
| 1027 | my $last = shift(@self_events); | ||
| 1028 | |||
| 1029 | while (my $event = shift(@self_events)) { | ||
| 1030 | |||
| 1031 | my $lo = $last->old_ArchiveStableId; | ||
| 1032 | my $ln = $last->new_ArchiveStableId; | ||
| 1033 | my $eo = $event->old_ArchiveStableId; | ||
| 1034 | my $en = $event->new_ArchiveStableId; | ||
| 1035 | |||
| 1036 | if ($lo and $eo and $en and $lo->stable_id eq $eo->stable_id | ||
| 1037 | and $lo->version eq $eo->version) { | ||
| 1038 | |||
| 1039 | # this removes redundant nodes and connects the remaining nodes: | ||
| 1040 | # | ||
| 1041 | # o--o--o -> o-----o | ||
| 1042 | # 1 1 1 1 1 | ||
| 1043 | |||
| 1044 | #warn 'A: '.$last->ident_string.' | '.$event->ident_string."\n"; | ||
| 1045 | |||
| 1046 | $self->remove_StableIdEvent($last); | ||
| 1047 | $self->remove_StableIdEvent($event); | ||
| 1048 | |||
| 1049 | $event->old_ArchiveStableId($lo); | ||
| 1050 | |||
| 1051 | $self->add_StableIdEvents($event); | ||
| 1052 | |||
| 1053 | } elsif ($ln and $eo and $ln->db_name ne $eo->db_name | ||
| 1054 | and $ln->stable_id eq $eo->stable_id and $ln->version eq $eo->version) { | ||
| 1055 | |||
| 1056 | # try to brigde gaps | ||
| 1057 | |||
| 1058 | if ($en) { | ||
| 1059 | |||
| 1060 | # o--o o--o -> o--o-----o | ||
| 1061 | # 1 2 2 2 1 2 2 | ||
| 1062 | # | ||
| 1063 | # o o--o -> o-----o | ||
| 1064 | # 1 1 1 1 1 | ||
| 1065 | |||
| 1066 | #warn 'X: '.$last->ident_string.' | '.$event->ident_string."\n"; | ||
| 1067 | |||
| 1068 | $self->remove_StableIdEvent($event); | ||
| 1069 | $event->old_ArchiveStableId($ln); | ||
| 1070 | $self->add_StableIdEvents($event); | ||
| 1071 | |||
| 1072 | } elsif ($lo) { | ||
| 1073 | |||
| 1074 | # there's a deletion event, deal with it differently | ||
| 1075 | |||
| 1076 | if ($lo->version eq $ln->version) { | ||
| 1077 | |||
| 1078 | # o--o o -> o-----o | ||
| 1079 | # 1 1 1 1 1 | ||
| 1080 | |||
| 1081 | #warn 'Y: '.$last->ident_string.' | '.$event->ident_string."\n"; | ||
| 1082 | |||
| 1083 | $self->remove_StableIdEvent($last); | ||
| 1084 | $last->new_ArchiveStableId($eo); | ||
| 1085 | $self->add_StableIdEvents($last); | ||
| 1086 | |||
| 1087 | } else { | ||
| 1088 | |||
| 1089 | # o--o o -> o--o--o | ||
| 1090 | # 1 2 2 1 2 2 | ||
| 1091 | |||
| 1092 | #warn 'Z: '.$last->ident_string.' | '.$event->ident_string."\n"; | ||
| 1093 | |||
| 1094 | $self->remove_StableIdEvent($event); | ||
| 1095 | $event->old_ArchiveStableId($ln); | ||
| 1096 | $event->new_ArchiveStableId($eo); | ||
| 1097 | $self->add_StableIdEvents($event); | ||
| 1098 | |||
| 1099 | } | ||
| 1100 | |||
| 1101 | } else { | ||
| 1102 | |||
| 1103 | # creation followed by deletion in next mapping | ||
| 1104 | # | ||
| 1105 | # o o -> o--o | ||
| 1106 | # 1 1 1 1 | ||
| 1107 | |||
| 1108 | #warn 'Q: '.$last->ident_string.' | '.$event->ident_string."\n"; | ||
| 1109 | |||
| 1110 | $self->remove_StableIdEvent($last); | ||
| 1111 | $self->remove_StableIdEvent($event); | ||
| 1112 | $event->old_ArchiveStableId($ln); | ||
| 1113 | $event->new_ArchiveStableId($eo); | ||
| 1114 | $self->add_StableIdEvents($event); | ||
| 1115 | |||
| 1116 | } | ||
| 1117 | |||
| 1118 | } else { | ||
| 1119 | #warn 'C: '.$last->ident_string.' | '.$event->ident_string."\n"; | ||
| 1120 | } | ||
| 1121 | |||
| 1122 | $last = $event; | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | # now add ArchiveStableIds of the remaining events to the tree | ||
| 1126 | $self->add_ArchiveStableIds_for_events; | ||
| 1127 | } | ||
| 1128 | |||
| 1129 | |||
| 1130 | =head2 reset_tree | ||
| 1131 | |||
| 1132 | Example : $history->reset_tree; | ||
| 1133 | Description : Resets all pre-calculated tree grid data. Mostly used internally | ||
| 1134 | by methods that modify the tree. | ||
| 1135 | Return type : none | ||
| 1136 | Exceptions : none | ||
| 1137 | Caller : internal | ||
| 1138 | Status : At Risk | ||
| 1139 | : under development | ||
| 1140 | |||
| 1141 | =cut | ||
| 1142 | |||
| 1143 | sub reset_tree { | ||
| 1144 | my $self = shift; | ||
| 1145 | $self->{'sorted_tree'} = undef; | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | |||
| 1149 | =head2 current_dbname | ||
| 1150 | |||
| 1151 | Arg[1] : (optional) String $dbname - the dbname to set | ||
| 1152 | Example : my $dbname = $history->current_dbname; | ||
| 1153 | Description : Getter/setter for current dbname. | ||
| 1154 | Return type : String | ||
| 1155 | Exceptions : none | ||
| 1156 | Caller : general | ||
| 1157 | Status : At Risk | ||
| 1158 | : under development | ||
| 1159 | |||
| 1160 | =cut | ||
| 1161 | |||
| 1162 | sub current_dbname { | ||
| 1163 | my $self = shift; | ||
| 1164 | $self->{'current_dbname'} = shift if (@_); | ||
| 1165 | return $self->{'current_dbname'}; | ||
| 1166 | } | ||
| 1167 | |||
| 1168 | |||
| 1169 | =head2 current_release | ||
| 1170 | |||
| 1171 | Arg[1] : (optional) Int $release - the release to set | ||
| 1172 | Example : my $release = $history->current_release; | ||
| 1173 | Description : Getter/setter for current release. | ||
| 1174 | Return type : Int | ||
| 1175 | Exceptions : none | ||
| 1176 | Caller : general | ||
| 1177 | Status : At Risk | ||
| 1178 | : under development | ||
| 1179 | |||
| 1180 | =cut | ||
| 1181 | |||
| 1182 | sub current_release { | ||
| 1183 | my $self = shift; | ||
| 1184 | $self->{'current_release'} = shift if (@_); | ||
| 1185 | return $self->{'current_release'}; | ||
| 1186 | } | ||
| 1187 | |||
| 1188 | |||
| 1189 | =head2 current_assembly | ||
| 1190 | |||
| 1191 | Arg[1] : (optional) String $assembly - the assembly to set | ||
| 1192 | Example : my $assembly = $history->current_assembly; | ||
| 1193 | Description : Getter/setter for current assembly. | ||
| 1194 | Return type : String | ||
| 1195 | Exceptions : none | ||
| 1196 | Caller : general | ||
| 1197 | Status : At Risk | ||
| 1198 | : under development | ||
| 1199 | |||
| 1200 | =cut | ||
| 1201 | |||
| 1202 | sub current_assembly { | ||
| 1203 | my $self = shift; | ||
| 1204 | $self->{'current_assembly'} = shift if (@_); | ||
| 1205 | return $self->{'current_assembly'}; | ||
| 1206 | } | ||
| 1207 | |||
| 1208 | |||
| 1209 | =head2 is_incomplete | ||
| 1210 | |||
| 1211 | Arg[1] : (optional) Boolean $incomplete | ||
| 1212 | Example : if ($history->is_incomplete) { | ||
| 1213 | print "Returned tree is incomplete due to too many mappings | ||
| 1214 | in the database.\n"; | ||
| 1215 | } | ||
| 1216 | Description : Getter/setter for incomplete flag. This is used by | ||
| 1217 | ArchiveStableIdAdaptor to indicate that it finished building | ||
| 1218 | the tree prematurely due to too many mappins in the db and can | ||
| 1219 | be used by applications to print warning messages. | ||
| 1220 | Return type : Boolean | ||
| 1221 | Exceptions : none | ||
| 1222 | Caller : general | ||
| 1223 | Status : At Risk | ||
| 1224 | : under development | ||
| 1225 | |||
| 1226 | =cut | ||
| 1227 | |||
| 1228 | sub is_incomplete { | ||
| 1229 | my $self = shift; | ||
| 1230 | $self->{'incomplete'} = shift if (@_); | ||
| 1231 | return $self->{'incomplete'}; | ||
| 1232 | } | ||
| 1233 | |||
| 1234 | |||
| 1235 | 1; | ||
| 1236 |