| 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::DBSQL::CoordSystemAdaptor | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | use Bio::EnsEMBL::Registry; | ||
| 38 | |||
| 39 | Bio::EnsEMBL::Registry->load_registry_from_db( | ||
| 40 | -host => 'ensembldb.ensembl.org', | ||
| 41 | -user => 'anonymous' | ||
| 42 | ); | ||
| 43 | |||
| 44 | $csa = Bio::EnsEMBL::Registry->get_adaptor( "human", "core", | ||
| 45 | "coordsystem" ); | ||
| 46 | |||
| 47 | # | ||
| 48 | # Get all coord systems in the database: | ||
| 49 | # | ||
| 50 | foreach my $cs ( @{ $csa->fetch_all() } ) { | ||
| 51 | print $cs->name, ' ', $cs->version, "\n"; | ||
| 52 | } | ||
| 53 | |||
| 54 | # | ||
| 55 | # Fetching by name: | ||
| 56 | # | ||
| 57 | |||
| 58 | # use the default version of coord_system 'chromosome' (e.g. NCBI33): | ||
| 59 | $cs = $csa->fetch_by_name('chromosome'); | ||
| 60 | |||
| 61 | # get an explicit version of coord_system 'chromosome': | ||
| 62 | $cs = $csa->fetch_by_name( 'chromsome', 'NCBI34' ); | ||
| 63 | |||
| 64 | # get all coord_systems of name 'chromosome': | ||
| 65 | foreach $cs ( @{ $csa->fetch_all_by_name('chromosome') } ) { | ||
| 66 | print $cs->name, ' ', $cs->version, "\n"; | ||
| 67 | } | ||
| 68 | |||
| 69 | # | ||
| 70 | # Fetching by rank: | ||
| 71 | # | ||
| 72 | $cs = $csa->fetch_by_rank(2); | ||
| 73 | |||
| 74 | # | ||
| 75 | # Fetching the pseudo coord system 'toplevel' | ||
| 76 | # | ||
| 77 | |||
| 78 | # Get the default top_level coord system: | ||
| 79 | $cs = $csa->fetch_top_level(); | ||
| 80 | |||
| 81 | # can also use an alias in fetch_by_name: | ||
| 82 | $cs = $csa->fetch_by_name('toplevel'); | ||
| 83 | |||
| 84 | # can also request toplevel using rank=0 | ||
| 85 | $cs = $csa->fetch_by_rank(0); | ||
| 86 | |||
| 87 | # | ||
| 88 | # Fetching by sequence level: | ||
| 89 | # | ||
| 90 | |||
| 91 | # Get the coord system which is used to store sequence: | ||
| 92 | $cs = $csa->fetch_sequence_level(); | ||
| 93 | |||
| 94 | # can also use an alias in fetch_by_name: | ||
| 95 | $cs = $csa->fetch_by_name('seqlevel'); | ||
| 96 | |||
| 97 | # | ||
| 98 | # Fetching by id | ||
| 99 | # | ||
| 100 | $cs = $csa->fetch_by_dbID(1); | ||
| 101 | |||
| 102 | |||
| 103 | =head1 DESCRIPTION | ||
| 104 | |||
| 105 | This adaptor allows the querying of information from the coordinate | ||
| 106 | system adaptor. | ||
| 107 | |||
| 108 | Note that many coordinate systems do not have a concept of a version | ||
| 109 | for the entire coordinate system (though they may have a per-sequence | ||
| 110 | version). The 'chromosome' coordinate system usually has a version | ||
| 111 | (i.e. the assembly version) but the clonal coordinate system does not | ||
| 112 | (despite having individual sequence versions). In the case where a | ||
| 113 | coordinate system does not have a version an empty string ('') is used | ||
| 114 | instead. | ||
| 115 | |||
| 116 | =head1 METHODS | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | package Bio::EnsEMBL::DBSQL::CoordSystemAdaptor; | ||
| 121 | $Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::VERSION = '114.0.0_50'; # TRIAL | ||
| 122 | $Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::VERSION = '114.0.050'; | ||
| 123 | use strict; | ||
| 124 | use warnings; | ||
| 125 | |||
| 126 | use Bio::EnsEMBL::DBSQL::BaseAdaptor; | ||
| 127 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 128 | use Bio::EnsEMBL::CoordSystem; | ||
| 129 | |||
| 130 | use vars qw(@ISA); | ||
| 131 | |||
| 132 | @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor); | ||
| 133 | |||
| 134 | |||
| 135 | =head2 new | ||
| 136 | |||
| 137 | Arg [1] : See BaseAdaptor for arguments (none specific to this | ||
| 138 | subclass) | ||
| 139 | Example : $cs = $db->get_CoordSystemAdaptor(); #better than new() | ||
| 140 | Description: Creates a new CoordSystem adaptor and caches the contents | ||
| 141 | of the coord_system table in memory. | ||
| 142 | Returntype : Bio::EnsEMBL::DBSQL::CoordSystemAdaptor | ||
| 143 | Exceptions : none | ||
| 144 | Caller : | ||
| 145 | Status : Stable | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | sub new { | ||
| 150 | my ( $proto, @args ) = @_; | ||
| 151 | |||
| 152 | my $class = ref($proto) || $proto; | ||
| 153 | my $self = $class->SUPER::new(@args); | ||
| 154 | |||
| 155 | # | ||
| 156 | # Cache the entire contents of the coord_system table cross-referenced | ||
| 157 | # by dbID and name. | ||
| 158 | # | ||
| 159 | |||
| 160 | # keyed on name, list of coord_system value | ||
| 161 | $self->{'_name_cache'} = {}; | ||
| 162 | |||
| 163 | # keyed on id, coord_system value | ||
| 164 | $self->{'_dbID_cache'} = {}; | ||
| 165 | |||
| 166 | # keyed on rank | ||
| 167 | $self->{'_rank_cache'} = {}; | ||
| 168 | |||
| 169 | # keyed on id, 1/undef values | ||
| 170 | $self->{'_is_sequence_level'} = {}; | ||
| 171 | $self->{'_is_default_version'} = {}; | ||
| 172 | |||
| 173 | #cache to store the seq_region_mapping information | ||
| 174 | #from internal->external | ||
| 175 | $self->{'_internal_seq_region_mapping'} = {}; | ||
| 176 | #from external->internal | ||
| 177 | $self->{'_external_seq_region_mapping'} = {}; | ||
| 178 | |||
| 179 | my $sth = $self->prepare( | ||
| 180 | 'SELECT `coord_system_id`, `name`, `rank`, `version`, `attrib` ' | ||
| 181 | . 'FROM coord_system ' | ||
| 182 | . 'WHERE species_id = ?' ); | ||
| 183 | |||
| 184 | $sth->bind_param( 1, $self->species_id(), SQL_INTEGER ); | ||
| 185 | $sth->execute(); | ||
| 186 | |||
| 187 | my ( $dbID, $name, $rank, $version, $attrib ); | ||
| 188 | $sth->bind_columns( \( $dbID, $name, $rank, $version, $attrib ) ); | ||
| 189 | |||
| 190 | while ( $sth->fetch() ) { | ||
| 191 | my $seq_lvl = 0; | ||
| 192 | my $default = 0; | ||
| 193 | |||
| 194 | if ( defined($attrib) ) { | ||
| 195 | foreach my $attrib ( split( ',', $attrib ) ) { | ||
| 196 | $self->{"_is_$attrib"}->{$dbID} = 1; | ||
| 197 | if ( $attrib eq 'sequence_level' ) { | ||
| 198 | $seq_lvl = 1; | ||
| 199 | } elsif ( $attrib eq 'default_version' ) { | ||
| 200 | $default = 1; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | my $cs = | ||
| 206 | Bio::EnsEMBL::CoordSystem->new( -DBID => $dbID, | ||
| 207 | -ADAPTOR => $self, | ||
| 208 | -NAME => $name, | ||
| 209 | -VERSION => $version, | ||
| 210 | -RANK => $rank, | ||
| 211 | -SEQUENCE_LEVEL => $seq_lvl, | ||
| 212 | -DEFAULT => $default ); | ||
| 213 | |||
| 214 | $self->{'_dbID_cache'}->{$dbID} = $cs; | ||
| 215 | |||
| 216 | $self->{'_name_cache'}->{ lc($name) } ||= []; | ||
| 217 | $self->{'_rank_cache'}->{$rank} = $cs; | ||
| 218 | |||
| 219 | push @{ $self->{'_name_cache'}->{ lc($name) } }, $cs; | ||
| 220 | |||
| 221 | } ## end while ( $sth->fetch() ) | ||
| 222 | $sth->finish(); | ||
| 223 | |||
| 224 | $self->_cache_mapping_paths(); | ||
| 225 | |||
| 226 | $self->_cache_seq_region_mapping(); | ||
| 227 | |||
| 228 | return $self; | ||
| 229 | } ## end sub new | ||
| 230 | |||
| 231 | sub _cache_seq_region_mapping { | ||
| 232 | # | ||
| 233 | # This cache will load the information from the seq_region_table, if | ||
| 234 | # any, to allow mapping between internal and external seq_region_id. | ||
| 235 | # | ||
| 236 | |||
| 237 | my ($self) = @_; | ||
| 238 | |||
| 239 | # For a given core database, will return the schema_build information. | ||
| 240 | my $schema_build = $self->db->_get_schema_build(); | ||
| 241 | |||
| 242 | # Prepare the query to get relation for the current database being | ||
| 243 | # used. | ||
| 244 | my $sql = qq( | ||
| 245 | SELECT s.internal_seq_region_id, | ||
| 246 | s.external_seq_region_id | ||
| 247 | FROM seq_region_mapping s, | ||
| 248 | mapping_set ms, | ||
| 249 | seq_region sr, | ||
| 250 | coord_system cs | ||
| 251 | WHERE ms.mapping_set_id = s.mapping_set_id | ||
| 252 | AND ms.internal_schema_build = ? | ||
| 253 | AND s.internal_seq_region_id = sr.seq_region_id | ||
| 254 | AND sr.coord_system_id = cs.coord_system_id | ||
| 255 | AND cs.species_id = ?); | ||
| 256 | |||
| 257 | my $sth = $self->prepare($sql); | ||
| 258 | |||
| 259 | $sth->bind_param( 1, $schema_build, SQL_VARCHAR ); | ||
| 260 | $sth->bind_param( 2, $self->species_id(), SQL_INTEGER ); | ||
| 261 | |||
| 262 | $sth->execute(); | ||
| 263 | |||
| 264 | # Load the cache: | ||
| 265 | foreach my $row ( @{ $sth->fetchall_arrayref() } ) { | ||
| 266 | # internal->external | ||
| 267 | $self->{'_internal_seq_region_mapping'}->{ $row->[0] } = $row->[1]; | ||
| 268 | # external->internal | ||
| 269 | $self->{'_external_seq_region_mapping'}->{ $row->[1] } = $row->[0]; | ||
| 270 | } | ||
| 271 | |||
| 272 | $sth->finish(); | ||
| 273 | |||
| 274 | } ## end sub _cache_seq_region_mapping | ||
| 275 | |||
| 276 | |||
| 277 | sub _cache_mapping_paths { | ||
| 278 | # Retrieve a list of available mappings from the meta table. This | ||
| 279 | # may eventually be moved a table of its own if this proves too | ||
| 280 | # cumbersome. | ||
| 281 | |||
| 282 | my ($self) = @_; | ||
| 283 | |||
| 284 | my %mapping_paths; | ||
| 285 | my $mc = $self->db()->get_MetaContainer(); | ||
| 286 | |||
| 287 | MAP_PATH: | ||
| 288 | foreach | ||
| 289 | my $map_path ( @{ $mc->list_value_by_key('assembly.mapping') } ) | ||
| 290 | { | ||
| 291 | my @cs_strings = split( /[|#]/, $map_path ); | ||
| 292 | |||
| 293 | if ( scalar(@cs_strings) < 2 ) { | ||
| 294 | warning( "Incorrectly formatted assembly.mapping value in meta " | ||
| 295 | . "table: $map_path" ); | ||
| 296 | next MAP_PATH; | ||
| 297 | } | ||
| 298 | |||
| 299 | my @coord_systems; | ||
| 300 | foreach my $cs_string (@cs_strings) { | ||
| 301 | my ( $name, $version ) = split( /:/, $cs_string ); | ||
| 302 | |||
| 303 | my $cs = $self->fetch_by_name( $name, $version ); | ||
| 304 | |||
| 305 | if ( !defined($cs) ) { | ||
| 306 | warning( "Unknown coordinate system specified in meta table " | ||
| 307 | . " assembly.mapping:\n $name:$version" ); | ||
| 308 | next MAP_PATH; | ||
| 309 | } | ||
| 310 | |||
| 311 | push( @coord_systems, $cs ); | ||
| 312 | } | ||
| 313 | |||
| 314 | # If the delimiter is a '#' we want a special case, multiple parts | ||
| 315 | # of the same component map to the same assembly part. As this | ||
| 316 | # looks like the "long" mapping, we just make the path a bit longer | ||
| 317 | # :-) | ||
| 318 | |||
| 319 | if ( index( $map_path, '#' ) != -1 && scalar(@coord_systems) == 2 ) | ||
| 320 | { | ||
| 321 | splice( @coord_systems, 1, 0, (undef) ); | ||
| 322 | } | ||
| 323 | |||
| 324 | my $cs1 = $coord_systems[0]; | ||
| 325 | my $cs2 = $coord_systems[$#coord_systems]; | ||
| 326 | |||
| 327 | my $key1 = $cs1->name(); | ||
| 328 | $key1 .= ':' . $cs1->version() if $cs1->version(); | ||
| 329 | my $key2 = $cs2->name(); | ||
| 330 | $key2 .= ':' . $cs2->version() if $cs2->version(); | ||
| 331 | |||
| 332 | if ( exists( $mapping_paths{"$key1|$key2"} ) ) { | ||
| 333 | warning( "Meta table specifies multiple mapping paths between " | ||
| 334 | . "coord systems $key1 and $key2.\n" | ||
| 335 | . "Choosing shorter path arbitrarily." ); | ||
| 336 | |||
| 337 | if ( scalar( @{ $mapping_paths{"$key1|$key2"} } ) < | ||
| 338 | scalar(@coord_systems) ) | ||
| 339 | { | ||
| 340 | next MAP_PATH; | ||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | $mapping_paths{"$key1|$key2"} = \@coord_systems; | ||
| 345 | } ## end foreach my $map_path ( @{ $mc... | ||
| 346 | |||
| 347 | # Create the pseudo coord system 'toplevel' and cache it so that only | ||
| 348 | # one of these is created for each database. | ||
| 349 | |||
| 350 | my $toplevel = | ||
| 351 | Bio::EnsEMBL::CoordSystem->new( -TOP_LEVEL => 1, | ||
| 352 | -NAME => 'toplevel', | ||
| 353 | -ADAPTOR => $self ); | ||
| 354 | |||
| 355 | $self->{'_top_level'} = $toplevel; | ||
| 356 | $self->{'_mapping_paths'} = \%mapping_paths; | ||
| 357 | |||
| 358 | return 1; | ||
| 359 | } ## end sub _cache_mapping_paths | ||
| 360 | |||
| 361 | =head2 fetch_all | ||
| 362 | |||
| 363 | Arg [1] : none | ||
| 364 | Example : foreach my $cs (@{$csa->fetch_all()}) { | ||
| 365 | print $cs->name(), ' ', $cs->version(), "\n"; | ||
| 366 | } | ||
| 367 | Description: Retrieves every coordinate system defined in the DB. | ||
| 368 | These will be returned in ascending order of rank. I.e. | ||
| 369 | The highest coordinate system with rank=1 would be first in the | ||
| 370 | array. | ||
| 371 | Returntype : listref of Bio::EnsEMBL::CoordSystems | ||
| 372 | Exceptions : none | ||
| 373 | Caller : general | ||
| 374 | Status : Stable | ||
| 375 | |||
| 376 | =cut | ||
| 377 | |||
| 378 | sub fetch_all { | ||
| 379 | my $self = shift; | ||
| 380 | |||
| 381 | my @coord_systems; | ||
| 382 | |||
| 383 | #order the array by rank in ascending order | ||
| 384 | foreach my $rank (sort {$a <=> $b} keys %{$self->{'_rank_cache'}}) { | ||
| 385 | push @coord_systems, $self->{'_rank_cache'}->{$rank}; | ||
| 386 | } | ||
| 387 | |||
| 388 | return \@coord_systems; | ||
| 389 | } | ||
| 390 | |||
| 391 | |||
| 392 | |||
| 393 | =head2 fetch_by_rank | ||
| 394 | |||
| 395 | Arg [1] : int $rank | ||
| 396 | Example : my $cs = $coord_sys_adaptor->fetch_by_rank(1); | ||
| 397 | Description: Retrieves a CoordinateSystem via its rank. 0 is a special | ||
| 398 | rank reserved for the pseudo coordinate system 'toplevel'. | ||
| 399 | undef is returned if no coordinate system of the specified rank | ||
| 400 | exists. | ||
| 401 | Returntype : Bio::EnsEMBL::CoordSystem | ||
| 402 | Exceptions : none | ||
| 403 | Caller : general | ||
| 404 | Status : Stable | ||
| 405 | |||
| 406 | =cut | ||
| 407 | |||
| 408 | sub fetch_by_rank { | ||
| 409 | my $self = shift; | ||
| 410 | my $rank = shift; | ||
| 411 | |||
| 412 | throw("Rank argument must be defined.") if(!defined($rank)); | ||
| 413 | throw("Rank argument must be a non-negative integer.") if($rank !~ /^\d+$/); | ||
| 414 | |||
| 415 | if($rank == 0) { | ||
| 416 | return $self->fetch_top_level(); | ||
| 417 | } | ||
| 418 | |||
| 419 | return $self->{'_rank_cache'}->{$rank}; | ||
| 420 | } | ||
| 421 | |||
| 422 | |||
| 423 | =head2 fetch_by_name | ||
| 424 | |||
| 425 | Arg [1] : string $name | ||
| 426 | The name of the coordinate system to retrieve. Alternatively | ||
| 427 | this may be an alias for a real coordinate system. Valid | ||
| 428 | aliases are 'toplevel' and 'seqlevel'. | ||
| 429 | Arg [2] : string $version (optional) | ||
| 430 | The version of the coordinate system to retrieve. If not | ||
| 431 | specified the default version will be used. | ||
| 432 | Example : $coord_sys = $csa->fetch_by_name('clone'); | ||
| 433 | $coord_sys = $csa->fetch_by_name('chromosome', 'NCBI33'); | ||
| 434 | # toplevel is an pseudo coord system representing the highest | ||
| 435 | # coord system in a given region | ||
| 436 | # such as the chromosome coordinate system | ||
| 437 | $coord_sys = $csa->fetch_by_name('toplevel'); | ||
| 438 | #seqlevel is an alias for the sequence level coordinate system | ||
| 439 | #such as the clone or contig coordinate system | ||
| 440 | $coord_sys = $csa->fetch_by_name('seqlevel'); | ||
| 441 | Description: Retrieves a coordinate system by its name | ||
| 442 | Returntype : Bio::EnsEMBL::CoordSystem | ||
| 443 | Exceptions : throw if no name argument provided | ||
| 444 | warning if no version provided and default does not exist | ||
| 445 | Caller : general | ||
| 446 | Status : Stable | ||
| 447 | |||
| 448 | =cut | ||
| 449 | |||
| 450 | sub fetch_by_name { | ||
| 451 | my $self = shift; | ||
| 452 | my $name = lc(shift); #case insensitve matching | ||
| 453 | my $version = shift; | ||
| 454 | |||
| 455 | throw('Name argument is required.') if(!$name); | ||
| 456 | |||
| 457 | $version = lc($version) if($version); | ||
| 458 | |||
| 459 | |||
| 460 | if($name eq 'seqlevel') { | ||
| 461 | return $self->fetch_sequence_level(); | ||
| 462 | } elsif($name eq 'toplevel') { | ||
| 463 | return $self->fetch_top_level($version); | ||
| 464 | } | ||
| 465 | |||
| 466 | if(!exists($self->{'_name_cache'}->{$name})) { | ||
| 467 | if($name =~ /top/) { | ||
| 468 | warning("Did you mean 'toplevel' coord system instead of '$name'?"); | ||
| 469 | } elsif($name =~ /seq/) { | ||
| 470 | warning("Did you mean 'seqlevel' coord system instead of '$name'?"); | ||
| 471 | } | ||
| 472 | return undef; | ||
| 473 | } | ||
| 474 | |||
| 475 | my @coord_systems = @{$self->{'_name_cache'}->{$name}}; | ||
| 476 | |||
| 477 | foreach my $cs (@coord_systems) { | ||
| 478 | if($version) { | ||
| 479 | return $cs if(lc($cs->version()) eq $version); | ||
| 480 | } elsif($self->{'_is_default_version'}->{$cs->dbID()}) { | ||
| 481 | return $cs; | ||
| 482 | } | ||
| 483 | } | ||
| 484 | |||
| 485 | if($version) { | ||
| 486 | #the specific version we were looking for was not found | ||
| 487 | return undef; | ||
| 488 | } | ||
| 489 | |||
| 490 | #didn't find a default, just take first one | ||
| 491 | my $cs = shift @coord_systems; | ||
| 492 | my $v = $cs->version(); | ||
| 493 | warning("No default version for coord_system [$name] exists. " . | ||
| 494 | "Using version [$v] arbitrarily"); | ||
| 495 | |||
| 496 | return $cs; | ||
| 497 | } | ||
| 498 | |||
| 499 | |||
| 500 | =head2 fetch_all_by_name | ||
| 501 | |||
| 502 | Arg [1] : string $name | ||
| 503 | The name of the coordinate system to retrieve. This can be | ||
| 504 | the name of an actual coordinate system or an alias for a | ||
| 505 | coordinate system. Valid aliases are 'toplevel' and 'seqlevel'. | ||
| 506 | Example : foreach my $cs (@{$csa->fetch_all_by_name('chromosome')}){ | ||
| 507 | print $cs->name(), ' ', $cs->version(); | ||
| 508 | } | ||
| 509 | Description: Retrieves all coordinate systems of a particular name | ||
| 510 | Returntype : listref of Bio::EnsEMBL::CoordSystem objects | ||
| 511 | Exceptions : throw if no name argument provided | ||
| 512 | Caller : general | ||
| 513 | Status : Stable | ||
| 514 | |||
| 515 | =cut | ||
| 516 | |||
| 517 | sub fetch_all_by_name { | ||
| 518 | my $self = shift; | ||
| 519 | my $name = lc(shift); #case insensitive matching | ||
| 520 | |||
| 521 | throw('Name argument is required') if(!$name); | ||
| 522 | |||
| 523 | if($name eq 'seqlevel') { | ||
| 524 | return [$self->fetch_sequence_level()]; | ||
| 525 | } elsif($name eq 'toplevel') { | ||
| 526 | return [$self->fetch_top_level()]; | ||
| 527 | } | ||
| 528 | |||
| 529 | return $self->{'_name_cache'}->{$name} || []; | ||
| 530 | } | ||
| 531 | |||
| 532 | =head2 fetch_all_by_version | ||
| 533 | |||
| 534 | Arg [1] : string $version | ||
| 535 | The version of the coordinate systems to retrieve. | ||
| 536 | Example : foreach my $cs (@{$csa->fetch_all_by_version('GRCh37')}){ | ||
| 537 | print $cs->name(), ' ', $cs->version(); | ||
| 538 | } | ||
| 539 | Description: Retrieves all coordinate systems of a particular version | ||
| 540 | Returntype : ArrayRef of Bio::EnsEMBL::CoordSystem objects | ||
| 541 | Exceptions : throw if no name argument provided | ||
| 542 | Caller : general | ||
| 543 | Status : Stable | ||
| 544 | |||
| 545 | =cut | ||
| 546 | |||
| 547 | sub fetch_all_by_version { | ||
| 548 | my ($self, $version) = @_; | ||
| 549 | throw "Version argument is required" if ! $version; | ||
| 550 | my @coord_systems; | ||
| 551 | |||
| 552 | foreach my $rank (sort {$a <=> $b} keys %{$self->{'_rank_cache'}}) { | ||
| 553 | if ($self->{'_rank_cache'}->{$rank}->version()) { | ||
| 554 | if ($self->{'_rank_cache'}->{$rank}->version() eq $version) { | ||
| 555 | push @coord_systems, $self->{'_rank_cache'}->{$rank}; | ||
| 556 | } | ||
| 557 | } else { | ||
| 558 | if (!$version) { | ||
| 559 | push @coord_systems, $self->{'_rank_cache'}->{$rank}; | ||
| 560 | } | ||
| 561 | } | ||
| 562 | } | ||
| 563 | return \@coord_systems; | ||
| 564 | } | ||
| 565 | |||
| 566 | =head2 fetch_by_dbID | ||
| 567 | |||
| 568 | Arg [1] : int dbID | ||
| 569 | Example : $cs = $csa->fetch_by_dbID(4); | ||
| 570 | Description: Retrieves a coord_system via its internal | ||
| 571 | identifier, or undef if no coordinate system with the provided | ||
| 572 | id exists. | ||
| 573 | Returntype : Bio::EnsEMBL::CoordSystem or undef | ||
| 574 | Exceptions : thrown if no coord_system exists for specified dbID | ||
| 575 | Caller : general | ||
| 576 | Status : Stable | ||
| 577 | |||
| 578 | =cut | ||
| 579 | |||
| 580 | sub fetch_by_dbID { | ||
| 581 | my $self = shift; | ||
| 582 | my $dbID = shift; | ||
| 583 | |||
| 584 | throw('dbID argument is required') if(!$dbID); | ||
| 585 | |||
| 586 | my $cs = $self->{'_dbID_cache'}->{$dbID}; | ||
| 587 | |||
| 588 | return undef if(!$cs); | ||
| 589 | |||
| 590 | return $cs; | ||
| 591 | } | ||
| 592 | |||
| 593 | |||
| 594 | |||
| 595 | =head2 fetch_top_level | ||
| 596 | |||
| 597 | Arg [1] : none | ||
| 598 | Example : $cs = $csa->fetch_top_level(); | ||
| 599 | Description: Retrieves the toplevel pseudo coordinate system. | ||
| 600 | Returntype : Bio::EnsEMBL::CoordSystem object | ||
| 601 | Exceptions : none | ||
| 602 | Caller : general | ||
| 603 | Status : Stable | ||
| 604 | |||
| 605 | =cut | ||
| 606 | |||
| 607 | sub fetch_top_level { | ||
| 608 | my $self = shift; | ||
| 609 | |||
| 610 | return $self->{'_top_level'}; | ||
| 611 | } | ||
| 612 | |||
| 613 | |||
| 614 | =head2 fetch_sequence_level | ||
| 615 | |||
| 616 | Arg [1] : none | ||
| 617 | Example : ($id, $name, $version) = $csa->fetch_sequence_level(); | ||
| 618 | Description: Retrieves the coordinate system at which sequence | ||
| 619 | is stored at. | ||
| 620 | Returntype : Bio::EnsEMBL::CoordSystem | ||
| 621 | Exceptions : throw if no sequence_level coord system exists at all | ||
| 622 | throw if multiple sequence_level coord systems exists | ||
| 623 | Caller : general | ||
| 624 | Status : Stable | ||
| 625 | |||
| 626 | =cut | ||
| 627 | |||
| 628 | sub fetch_sequence_level { | ||
| 629 | my $self = shift; | ||
| 630 | |||
| 631 | my @dbIDs = keys %{$self->{'_is_sequence_level'}}; | ||
| 632 | |||
| 633 | throw('No sequence_level coord_system is defined') if(!@dbIDs); | ||
| 634 | |||
| 635 | if(@dbIDs > 1) { | ||
| 636 | throw('Multiple sequence_level coord_systems are defined.' . | ||
| 637 | 'Only one is currently supported'); | ||
| 638 | } | ||
| 639 | |||
| 640 | return $self->{'_dbID_cache'}->{$dbIDs[0]}; | ||
| 641 | } | ||
| 642 | =head2 get_default_version | ||
| 643 | |||
| 644 | Arg [1] : none | ||
| 645 | Example : $version = $csa->get_default_version(); | ||
| 646 | Description: Retrieves the default version of the assembly | ||
| 647 | Returntype : String | ||
| 648 | Exceptions : throw if no default version is defined | ||
| 649 | Caller : general | ||
| 650 | Status : Stable | ||
| 651 | |||
| 652 | =cut | ||
| 653 | |||
| 654 | sub get_default_version { | ||
| 655 | my $self = shift; | ||
| 656 | |||
| 657 | my $version; | ||
| 658 | foreach my $dbID (keys %{$self->{'_is_default_version'}}) { | ||
| 659 | if ($self->{'_dbID_cache'}->{$dbID}->version) { | ||
| 660 | $version = $self->{'_dbID_cache'}->{$dbID}->version; | ||
| 661 | last; | ||
| 662 | } | ||
| 663 | } | ||
| 664 | |||
| 665 | return $version; | ||
| 666 | } | ||
| 667 | |||
| 668 | |||
| 669 | |||
| 670 | =head2 get_all_versions | ||
| 671 | |||
| 672 | Arg [1] : none | ||
| 673 | Example : @versions = $csa->get_all_versions(); | ||
| 674 | Description: Retrieves all the available versions of assemblies | ||
| 675 | Returntype : Listref of versions (strings) | ||
| 676 | Exceptions : throw if no version is defined | ||
| 677 | Caller : general | ||
| 678 | Status : Stable | ||
| 679 | |||
| 680 | =cut | ||
| 681 | |||
| 682 | sub get_all_versions { | ||
| 683 | my $self = shift; | ||
| 684 | |||
| 685 | my %hash_versions; | ||
| 686 | my @versions; | ||
| 687 | my $version; | ||
| 688 | foreach my $dbID (sort {$a <=> $b} keys %{$self->{'_rank_cache'}}) { | ||
| 689 | if ($self->{'_rank_cache'}->{$dbID}->version) { | ||
| 690 | $version = $self->{'_rank_cache'}->{$dbID}->version; | ||
| 691 | if (!$hash_versions{$version}) { | ||
| 692 | $hash_versions{$version} = 1; | ||
| 693 | push @versions, $version; | ||
| 694 | } | ||
| 695 | } | ||
| 696 | } | ||
| 697 | |||
| 698 | throw('No versions found') if(!scalar(@versions)); | ||
| 699 | |||
| 700 | return \@versions; | ||
| 701 | } | ||
| 702 | |||
| 703 | |||
| 704 | =head2 get_mapping_path | ||
| 705 | |||
| 706 | Arg [1] : Bio::EnsEMBL::CoordSystem $cs1 | ||
| 707 | Arg [2] : Bio::EnsEMBL::CoordSystem $cs2 | ||
| 708 | Example : foreach my $cs @{$csa->get_mapping_path($cs1,$cs2); | ||
| 709 | Description: Given two coordinate systems this will return a mapping path | ||
| 710 | between them if one has been defined. Allowed Mapping paths are | ||
| 711 | explicitly defined in the meta table. The following is an | ||
| 712 | example: | ||
| 713 | |||
| 714 | mysql> select * from meta where meta_key = 'assembly.mapping'; | ||
| 715 | +---------+------------------+--------------------------------------+ | ||
| 716 | | meta_id | meta_key | meta_value | | ||
| 717 | +---------+------------------+--------------------------------------+ | ||
| 718 | | 20 | assembly.mapping | chromosome:NCBI34|contig | | ||
| 719 | | 21 | assembly.mapping | clone|contig | | ||
| 720 | | 22 | assembly.mapping | supercontig|contig | | ||
| 721 | | 23 | assembly.mapping | chromosome:NCBI34|contig|clone | | ||
| 722 | | 24 | assembly.mapping | chromosome:NCBI34|contig|supercontig | | ||
| 723 | | 25 | assembly.mapping | supercontig|contig|clone | | ||
| 724 | +---------+------------------+--------------------------------------+ | ||
| 725 | |||
| 726 | For a one-step mapping path to be valid there needs to be | ||
| 727 | a relationship between the two coordinate systems defined in | ||
| 728 | the assembly table. Two step mapping paths work by building | ||
| 729 | on the one-step mapping paths which are already defined. | ||
| 730 | |||
| 731 | The first coordinate system in a one step mapping path must | ||
| 732 | be the assembled coordinate system and the second must be | ||
| 733 | the component. | ||
| 734 | |||
| 735 | Example of use: | ||
| 736 | my $cs1 = $cs_adaptor->fetch_by_name('contig'); | ||
| 737 | my $cs2 = $cs_adaptor->fetch_by_name('chromosome'); | ||
| 738 | |||
| 739 | my @path = @{$cs_adaptor->get_mapping_path($cs1,$cs2)}; | ||
| 740 | |||
| 741 | if(!@path) { | ||
| 742 | print "No mapping path."; | ||
| 743 | } | ||
| 744 | elsif(@path == 2) { | ||
| 745 | print "2 step mapping path."; | ||
| 746 | print "Assembled = " . $path[0]->name() . "\n"; | ||
| 747 | print "Component = " . $path[1]->name() . "\n"; | ||
| 748 | } else { | ||
| 749 | print "Multi step mapping path\n"; | ||
| 750 | } | ||
| 751 | |||
| 752 | Returntype : reference to a list of Bio::EnsEMBL::CoordSystem objects | ||
| 753 | |||
| 754 | Exceptions : none | ||
| 755 | Caller : general | ||
| 756 | Status : Stable | ||
| 757 | |||
| 758 | =cut | ||
| 759 | |||
| 760 | sub get_mapping_path { | ||
| 761 | my $self = shift; | ||
| 762 | my $cs1 = shift; | ||
| 763 | my $cs2 = shift; | ||
| 764 | |||
| 765 | if(!ref($cs1) || !ref($cs2) || | ||
| 766 | !$cs1->isa('Bio::EnsEMBL::CoordSystem') || | ||
| 767 | !$cs2->isa('Bio::EnsEMBL::CoordSystem')) { | ||
| 768 | throw('Two Bio::EnsEMBL::CoordSystem arguments expected.'); | ||
| 769 | } | ||
| 770 | |||
| 771 | my $key1 = $cs1->name(); | ||
| 772 | $key1 .= ':' . $cs1->version() if ($cs1->version()); | ||
| 773 | my $key2 = $cs2->name(); | ||
| 774 | $key2 .= ':' . $cs2->version() if ($cs2->version()); | ||
| 775 | |||
| 776 | my $path = $self->{'_mapping_paths'}->{"$key1|$key2"}; | ||
| 777 | |||
| 778 | return $path if($path); | ||
| 779 | |||
| 780 | $path = $self->{'_mapping_paths'}->{"$key2|$key1"}; | ||
| 781 | |||
| 782 | if(!$path) { | ||
| 783 | # No path was explicitly defined, but we might be able to guess a | ||
| 784 | # suitable path. We only guess for missing 2 step paths. | ||
| 785 | |||
| 786 | my %mid1; | ||
| 787 | my %mid2; | ||
| 788 | |||
| 789 | foreach my $path (values(%{$self->{'_mapping_paths'}})) { | ||
| 790 | next if(@$path != 2); | ||
| 791 | |||
| 792 | my $match = undef; | ||
| 793 | |||
| 794 | if($path->[0]->equals($cs1)) { | ||
| 795 | $match = 1; | ||
| 796 | } elsif($path->[1]->equals($cs1)) { | ||
| 797 | $match = 0; | ||
| 798 | } | ||
| 799 | |||
| 800 | if(defined($match)) { | ||
| 801 | my $mid = $path->[$match]; | ||
| 802 | my $midkey = $mid->name() . ':' . $mid->version(); | ||
| 803 | |||
| 804 | # is the same cs mapped to by other cs? | ||
| 805 | if($mid2{$midkey}) { | ||
| 806 | my $path = [$cs1,$mid,$cs2]; | ||
| 807 | $self->{'_mapping_paths'}->{"$key1|$key2"} = $path; | ||
| 808 | $key1 =~ s/\:$//; | ||
| 809 | $key2 =~ s/\:$//; | ||
| 810 | $midkey =~ s/\:$//; | ||
| 811 | warning("Using implicit mapping path between '$key1' and '$key2' " . | ||
| 812 | "coord systems.\n" . | ||
| 813 | "An explicit 'assembly.mapping' entry should be added " . | ||
| 814 | "to the meta table.\nExample: " . | ||
| 815 | "'$key1|$midkey|$key2'\n"); | ||
| 816 | return $path; | ||
| 817 | } else { | ||
| 818 | $mid1{$midkey} = $mid; | ||
| 819 | } | ||
| 820 | } | ||
| 821 | |||
| 822 | $match = undef; | ||
| 823 | |||
| 824 | if($path->[0]->equals($cs2)) { | ||
| 825 | $match = 1; | ||
| 826 | } elsif($path->[1]->equals($cs2)) { | ||
| 827 | $match = 0; | ||
| 828 | } | ||
| 829 | |||
| 830 | |||
| 831 | if(defined($match)) { | ||
| 832 | my $mid = $path->[$match]; | ||
| 833 | my $midkey = $mid->name() . ':' . $mid->version(); | ||
| 834 | |||
| 835 | # is the same cs mapped to by other cs? | ||
| 836 | if($mid1{$midkey}) { | ||
| 837 | my $path = [$cs2,$mid,$cs1]; | ||
| 838 | $self->{'_mapping_paths'}->{"$key2|$key1"} = $path; | ||
| 839 | |||
| 840 | $key1 =~ s/\:$//; | ||
| 841 | $key2 =~ s/\:$//; | ||
| 842 | $midkey =~ s/\:$//; | ||
| 843 | warning("Using implicit mapping path between '$key1' and '$key2' " . | ||
| 844 | "coord systems.\n" . | ||
| 845 | "An explicit 'assembly.mapping' entry should be added " . | ||
| 846 | "to the meta table.\nExample: " . | ||
| 847 | "'$key1|$midkey|$key2'\n"); | ||
| 848 | |||
| 849 | return $path; | ||
| 850 | } else { | ||
| 851 | $mid2{$midkey} = $mid; | ||
| 852 | } | ||
| 853 | } | ||
| 854 | } | ||
| 855 | } | ||
| 856 | |||
| 857 | return $path || []; | ||
| 858 | } | ||
| 859 | |||
| 860 | =head2 store_mapping_path | ||
| 861 | |||
| 862 | Arg [1] : Bio::EnsEMBL::CoordSystem $cs1 | ||
| 863 | Arg [2] : Bio::EnsEMBL::CoordSystem $cs2 | ||
| 864 | Arg [3..n] : Bio::EnsEMBL::CoordSystem $cs3..$csN | ||
| 865 | Example : my $pathref = $csa->store_mapping_path($cs1,$cs2); | ||
| 866 | Description: Given two or more coordinate systems this will store | ||
| 867 | mapping paths between them in the database. | ||
| 868 | |||
| 869 | For example, if $cs1 represents chrs of version | ||
| 870 | V1, $cs2 represents contigs, and $cs3 clones then, unless | ||
| 871 | they already exist, the following entries will be created | ||
| 872 | in the meta table; | ||
| 873 | +------------------+---------------------+ | ||
| 874 | | meta_key | meta_value | | ||
| 875 | +------------------+---------------------+ | ||
| 876 | | assembly.mapping | chr:V1|clone | | ||
| 877 | | assembly.mapping | clone|contig | | ||
| 878 | | assembly.mapping | chr:V1|clone|contig | | ||
| 879 | +------------------+---------------------+ | ||
| 880 | |||
| 881 | |||
| 882 | For a one-step mapping path to be valid there needs to be | ||
| 883 | a relationship between the two coordinate systems defined in | ||
| 884 | the assembly table. Two step mapping paths work by building | ||
| 885 | on the one-step mapping paths which are already defined. | ||
| 886 | |||
| 887 | The first coordinate system in a one step mapping path must | ||
| 888 | be the assembled coordinate system and the second must be | ||
| 889 | the component. | ||
| 890 | |||
| 891 | Returntype : reference to a list of lists of new meta_value mapping strings | ||
| 892 | created for assembly.mapping | ||
| 893 | Exceptions : CoordSystems with no rank/duplicated rank | ||
| 894 | Caller : general | ||
| 895 | Status : Experimental | ||
| 896 | |||
| 897 | =cut | ||
| 898 | |||
| 899 | sub store_mapping_path{ | ||
| 900 | my $self = shift; | ||
| 901 | my @csystems = @_; | ||
| 902 | |||
| 903 | # Validate and sort the args | ||
| 904 | my %seen_ranks; | ||
| 905 | @csystems >= 2 or throw('Need two or more CoordSystems'); | ||
| 906 | my $validate = sub{ | ||
| 907 | ref($_[0]) && $_[0]->isa('Bio::EnsEMBL::CoordSystem') or | ||
| 908 | throw('CoordSystem argument expected.'); | ||
| 909 | my $rank = $_[0]->rank || | ||
| 910 | throw('CoordSystem has no rank: '.$_[0]->name); | ||
| 911 | $seen_ranks{$rank} && | ||
| 912 | throw('CoordSystem '.$_[0]->name." shares rank $rank with ". | ||
| 913 | $seen_ranks{$rank}->name); | ||
| 914 | $seen_ranks{$rank} = $_[0]; | ||
| 915 | }; | ||
| 916 | @csystems = map{&{$validate}($_)} @csystems; | ||
| 917 | my ($key, @keys); | ||
| 918 | foreach my $cs (@csystems) { | ||
| 919 | $key = $cs->name(); | ||
| 920 | if ($cs->version()) { | ||
| 921 | $key .= ":" . $cs->version(); | ||
| 922 | } | ||
| 923 | push @keys, $key; | ||
| 924 | } | ||
| 925 | |||
| 926 | # For each pair in the sorted list, store in the DB | ||
| 927 | my $meta = $self->db->get_MetaContainer; | ||
| 928 | my @retlist; | ||
| 929 | for( my $i=1; $i<@keys; $i++ ){ | ||
| 930 | for( my $j=0; $j<(@keys-$i); $j++ ){ | ||
| 931 | my $mapping = join( "|", @keys ); | ||
| 932 | |||
| 933 | my $mapping_key = join( "|", @keys ); | ||
| 934 | # Skip existing | ||
| 935 | next if $self->{'_mapping_paths'}->{$mapping_key}; | ||
| 936 | |||
| 937 | # Update the database | ||
| 938 | $meta->store_key_value('assembly.mapping',$mapping); | ||
| 939 | push @retlist, $mapping; | ||
| 940 | } | ||
| 941 | } | ||
| 942 | |||
| 943 | if( @retlist ){ | ||
| 944 | # Update mapping path cache | ||
| 945 | $self->_cache_mapping_paths; | ||
| 946 | } | ||
| 947 | |||
| 948 | # Return the mappings that we have just created | ||
| 949 | return [@retlist]; | ||
| 950 | } | ||
| 951 | |||
| 952 | |||
| 953 | =head2 store_multiple_mapping_path | ||
| 954 | |||
| 955 | Arg [1] : Bio::EnsEMBL::CoordSystem $cs1 | ||
| 956 | Arg [2] : Bio::EnsEMBL::CoordSystem $cs2 | ||
| 957 | Arg [3..n] : Bio::EnsEMBL::CoordSystem $cs3..$csN | ||
| 958 | Example : my $pathref = $csa->store_multiple_mapping_path($cs1,$cs2); | ||
| 959 | Description: Given two or more coordinate systems this will store | ||
| 960 | multiple mapping paths between them in the database. | ||
| 961 | |||
| 962 | Works similarly to the store_mapping_path method | ||
| 963 | But will presume every coord system can be mapped in multiple | ||
| 964 | ways to the other coord systems | ||
| 965 | This is represented by the use of '#' instead of '|' | ||
| 966 | in the mapping key | ||
| 967 | |||
| 968 | Returntype : reference to a list of lists of new meta_value mapping strings | ||
| 969 | created for assembly.mapping | ||
| 970 | Exceptions : CoordSystems with no rank/duplicated rank | ||
| 971 | Caller : general | ||
| 972 | Status : Experimental | ||
| 973 | |||
| 974 | =cut | ||
| 975 | |||
| 976 | sub store_multiple_mapping_path{ | ||
| 977 | my $self = shift; | ||
| 978 | my @csystems = @_; | ||
| 979 | |||
| 980 | # Validate and sort the args | ||
| 981 | my %seen_ranks; | ||
| 982 | @csystems >= 2 or throw('Need two or more CoordSystems'); | ||
| 983 | my $validate = sub{ | ||
| 984 | ref($_[0]) && $_[0]->isa('Bio::EnsEMBL::CoordSystem') or | ||
| 985 | throw('CoordSystem argument expected.'); | ||
| 986 | my $rank = $_[0]->rank || | ||
| 987 | throw('CoordSystem has no rank: '.$_[0]->name); | ||
| 988 | $seen_ranks{$rank} && | ||
| 989 | throw('CoordSystem '.$_[0]->name." shares rank $rank with ". | ||
| 990 | $seen_ranks{$rank}->name); | ||
| 991 | $seen_ranks{$rank} = $_[0]; | ||
| 992 | }; | ||
| 993 | @csystems = map{&{$validate}($_)} @csystems; | ||
| 994 | my ($key, @keys); | ||
| 995 | foreach my $cs (@csystems) { | ||
| 996 | $key = $cs->name(); | ||
| 997 | if ($cs->version()) { | ||
| 998 | $key .= ":" . $cs->version(); | ||
| 999 | } | ||
| 1000 | push @keys, $key; | ||
| 1001 | } | ||
| 1002 | # For each pair in the sorted list, store in the DB | ||
| 1003 | my $meta = $self->db->get_MetaContainer; | ||
| 1004 | my @retlist; | ||
| 1005 | for( my $i=1; $i<@keys; $i++ ){ | ||
| 1006 | for( my $j=0; $j<(@keys-$i); $j++ ){ | ||
| 1007 | my $mapping = join( "#", @keys ); | ||
| 1008 | |||
| 1009 | my $mapping_key = join( "#", @keys ); | ||
| 1010 | # Skip existing | ||
| 1011 | next if $self->{'_mapping_paths'}->{$mapping_key}; | ||
| 1012 | |||
| 1013 | # Update the database | ||
| 1014 | $meta->store_key_value('assembly.mapping',$mapping) unless $meta->key_value_exists('assembly.mapping',$mapping); | ||
| 1015 | push @retlist, $mapping; | ||
| 1016 | } | ||
| 1017 | } | ||
| 1018 | |||
| 1019 | if( @retlist ){ | ||
| 1020 | # Update mapping path cache | ||
| 1021 | $self->_cache_mapping_paths; | ||
| 1022 | } | ||
| 1023 | |||
| 1024 | # Return the mappings that we have just created | ||
| 1025 | return [@retlist]; | ||
| 1026 | } | ||
| 1027 | |||
| 1028 | |||
| 1029 | |||
| 1030 | =head2 fetch_by_attrib | ||
| 1031 | |||
| 1032 | Arg [1] : string attrib | ||
| 1033 | Arg [2] : (optional) string version | ||
| 1034 | Example : $csa->fetch_by_attrib('default_version','NCBIM37'); | ||
| 1035 | Description: Retrieves a CoordSystem object from the database that have the specified | ||
| 1036 | attrib and version, if no version is specified, returns the default version | ||
| 1037 | Returntype : Bio::EnsEMBL::CoordSystem object | ||
| 1038 | Exceptions : throw when attrib not present | ||
| 1039 | Caller : general | ||
| 1040 | Status : Stable | ||
| 1041 | |||
| 1042 | =cut | ||
| 1043 | |||
| 1044 | sub fetch_by_attrib { | ||
| 1045 | my $self = shift; | ||
| 1046 | my $attrib = shift; | ||
| 1047 | my $version = shift; | ||
| 1048 | |||
| 1049 | $version = lc($version) if($version); | ||
| 1050 | |||
| 1051 | my @dbIDs = keys %{$self->{"_is_$attrib"}}; | ||
| 1052 | |||
| 1053 | throw("No $attrib coordinate system defined") if(!@dbIDs); | ||
| 1054 | |||
| 1055 | foreach my $dbID (@dbIDs) { | ||
| 1056 | my $cs = $self->{'_dbID_cache'}->{$dbID}; | ||
| 1057 | if($version) { | ||
| 1058 | return $cs if(lc($version) eq $cs->version()); | ||
| 1059 | } elsif($self->{'_is_default_version'}->{$dbID}) { | ||
| 1060 | return $cs; | ||
| 1061 | } | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | #specifically requested attrib system was not found | ||
| 1065 | if($version) { | ||
| 1066 | throw("$attrib coord_system with version [$version] does not exist"); | ||
| 1067 | } | ||
| 1068 | |||
| 1069 | #coordsystem with attrib exists but no default is defined: | ||
| 1070 | my $dbID = shift @dbIDs; | ||
| 1071 | my $cs = $self->{'_dbID_cache'}->{$dbID}; | ||
| 1072 | my $v = $cs->version(); | ||
| 1073 | warning("No default version for $attrib coord_system exists. " . | ||
| 1074 | "Using version [$v] arbitrarily"); | ||
| 1075 | |||
| 1076 | return $cs; | ||
| 1077 | } | ||
| 1078 | |||
| 1079 | |||
| 1080 | =head2 fetch_all_by_attrib | ||
| 1081 | |||
| 1082 | Arg [1] : string attrib | ||
| 1083 | Example : $csa->fetch_all_by_attrib('default_version'); | ||
| 1084 | Description: Retrieves all CoordSystem object from the database that have the specified | ||
| 1085 | attrib. | ||
| 1086 | Returntype : reference to a list of Bio::EnsEMBL::CoordSystem objects | ||
| 1087 | Exceptions : throw when attrib not present | ||
| 1088 | Caller : general | ||
| 1089 | Status : Stable | ||
| 1090 | |||
| 1091 | =cut | ||
| 1092 | |||
| 1093 | sub fetch_all_by_attrib { | ||
| 1094 | my $self = shift; | ||
| 1095 | my $attrib = shift; | ||
| 1096 | |||
| 1097 | my @coord_systems = (); | ||
| 1098 | foreach my $dbID (keys %{$self->{"_is_$attrib"}}) { | ||
| 1099 | push @coord_systems, $self->{"_dbID_cache"}->{$dbID}; | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | return \@coord_systems; | ||
| 1103 | } | ||
| 1104 | |||
| 1105 | =head2 store | ||
| 1106 | |||
| 1107 | Arg [1] : Bio::EnsEMBL::CoordSystem | ||
| 1108 | Example : $csa->store($coord_system); | ||
| 1109 | Description: Stores a CoordSystem object in the database. | ||
| 1110 | Returntype : none | ||
| 1111 | Exceptions : Warning if CoordSystem is already stored in this database. | ||
| 1112 | Caller : none | ||
| 1113 | Status : Stable | ||
| 1114 | |||
| 1115 | =cut | ||
| 1116 | |||
| 1117 | sub store { | ||
| 1118 | my $self = shift; | ||
| 1119 | my $cs = shift; | ||
| 1120 | |||
| 1121 | if(!$cs || !ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) { | ||
| 1122 | throw('CoordSystem argument expected.'); | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | my $db = $self->db(); | ||
| 1126 | my $name = $cs->name(); | ||
| 1127 | my $version = $cs->version(); | ||
| 1128 | my $rank = $cs->rank(); | ||
| 1129 | |||
| 1130 | my $seqlevel = $cs->is_sequence_level(); | ||
| 1131 | my $default = $cs->is_default(); | ||
| 1132 | |||
| 1133 | my $toplevel = $cs->is_top_level(); | ||
| 1134 | |||
| 1135 | if($toplevel) { | ||
| 1136 | throw("The toplevel CoordSystem cannot be stored"); | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | # | ||
| 1140 | # Do lots of sanity checking to prevent bad data from being entered | ||
| 1141 | # | ||
| 1142 | |||
| 1143 | if($cs->is_stored($db)) { | ||
| 1144 | warning("CoordSystem $name $version is already in db.\n"); | ||
| 1145 | return; | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | if($name eq 'toplevel' || $name eq 'seqlevel' || !$name) { | ||
| 1149 | throw("[$name] is not a valid name for a CoordSystem."); | ||
| 1150 | } | ||
| 1151 | |||
| 1152 | if($seqlevel && keys(%{$self->{'_is_sequence_level'}})) { | ||
| 1153 | throw("There can only be one sequence level CoordSystem."); | ||
| 1154 | } | ||
| 1155 | |||
| 1156 | if(exists $self->{'_name_cache'}->{lc($name)}) { | ||
| 1157 | my @coord_systems = @{$self->{'_name_cache'}->{lc($name)}}; | ||
| 1158 | foreach my $c (@coord_systems) { | ||
| 1159 | if(lc($c->version()) eq lc($version)) { | ||
| 1160 | warning("CoordSystem $name $version is already in db.\n"); | ||
| 1161 | return; | ||
| 1162 | } | ||
| 1163 | if($default && $self->{'_is_default_version'}->{$c->dbID()}) { | ||
| 1164 | throw("There can only be one default version of CoordSystem $name"); | ||
| 1165 | } | ||
| 1166 | } | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | if($rank !~ /^\d+$/) { | ||
| 1170 | throw("Rank attribute must be a positive integer not [$rank]"); | ||
| 1171 | } | ||
| 1172 | if($rank == 0) { | ||
| 1173 | throw("Only toplevel CoordSystem may have rank of 0."); | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | if(defined($self->{'_rank_cache'}->{$rank})) { | ||
| 1177 | throw("CoordSystem with rank [$rank] already exists."); | ||
| 1178 | } | ||
| 1179 | |||
| 1180 | my @attrib; | ||
| 1181 | |||
| 1182 | push @attrib, 'default_version' if($default); | ||
| 1183 | push @attrib, 'sequence_level' if($seqlevel); | ||
| 1184 | |||
| 1185 | my $attrib_str = (@attrib) ? join(',', @attrib) : undef; | ||
| 1186 | |||
| 1187 | # | ||
| 1188 | # store the coordinate system in the database | ||
| 1189 | # | ||
| 1190 | |||
| 1191 | my $sth = | ||
| 1192 | $db->dbc->prepare( 'INSERT INTO coord_system ' | ||
| 1193 | . '( `name`, `version`, `attrib`, `rank`, `species_id` ) ' | ||
| 1194 | . 'VALUES ( ?, ?, ?, ?, ? )' ); | ||
| 1195 | |||
| 1196 | $sth->bind_param( 1, $name, SQL_VARCHAR ); | ||
| 1197 | $sth->bind_param( 2, $version, SQL_VARCHAR ); | ||
| 1198 | $sth->bind_param( 3, $attrib_str, SQL_VARCHAR ); | ||
| 1199 | $sth->bind_param( 4, $rank, SQL_INTEGER ); | ||
| 1200 | $sth->bind_param( 5, $self->species_id(), SQL_INTEGER ); | ||
| 1201 | |||
| 1202 | $sth->execute(); | ||
| 1203 | my $dbID = $self->last_insert_id('coord_system_id', undef, 'coord_system'); | ||
| 1204 | $sth->finish(); | ||
| 1205 | |||
| 1206 | if(!$dbID) { | ||
| 1207 | throw("Did not get dbID from store of CoordSystem."); | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | $cs->dbID($dbID); | ||
| 1211 | $cs->adaptor($self); | ||
| 1212 | |||
| 1213 | # | ||
| 1214 | # update the internal caches that are used for fetching | ||
| 1215 | # | ||
| 1216 | $self->{'_is_default_version'}->{$dbID} = 1 if($default); | ||
| 1217 | $self->{'_is_sequence_level'}->{$dbID} = 1 if($seqlevel); | ||
| 1218 | |||
| 1219 | $self->{'_name_cache'}->{lc($name)} ||= []; | ||
| 1220 | push @{$self->{'_name_cache'}->{lc($name)}}, $cs; | ||
| 1221 | |||
| 1222 | $self->{'_dbID_cache'}->{$dbID} = $cs; | ||
| 1223 | $self->{'_rank_cache'}->{$rank} = $cs; | ||
| 1224 | |||
| 1225 | return $cs; | ||
| 1226 | } | ||
| 1227 | |||
| 1228 | =head2 remove | ||
| 1229 | |||
| 1230 | Arg [1] : Bio::EnsEMBL::CoordSystem | ||
| 1231 | Example : $csa->remove($coord_system); | ||
| 1232 | Description: Removes a CoordSystem object from the database. | ||
| 1233 | Returntype : none | ||
| 1234 | Exceptions : Warning if CoordSystem is not stored in this database. | ||
| 1235 | Caller : none | ||
| 1236 | Status : Stable | ||
| 1237 | |||
| 1238 | =cut | ||
| 1239 | |||
| 1240 | sub remove { | ||
| 1241 | my $self = shift; | ||
| 1242 | my $cs = shift; | ||
| 1243 | |||
| 1244 | if(!$cs || !ref($cs) || !$cs->isa('Bio::EnsEMBL::CoordSystem')) { | ||
| 1245 | throw('CoordSystem argument expected.'); | ||
| 1246 | } | ||
| 1247 | |||
| 1248 | my $db = $self->db(); | ||
| 1249 | my $name = $cs->name(); | ||
| 1250 | my $version = $cs->version(); | ||
| 1251 | my $dbID = $cs->dbID(); | ||
| 1252 | my $rank = $cs->rank(); | ||
| 1253 | |||
| 1254 | # | ||
| 1255 | # Do lots of sanity checking to prevent bad data from being entered | ||
| 1256 | # | ||
| 1257 | |||
| 1258 | if(!$cs->is_stored($db)) { | ||
| 1259 | warning("CoordSystem $name $version does not exist in db.\n"); | ||
| 1260 | return; | ||
| 1261 | } | ||
| 1262 | |||
| 1263 | if($name eq 'toplevel' || $name eq 'seqlevel' || !$name) { | ||
| 1264 | throw("[$name] is not a valid name for a CoordSystem."); | ||
| 1265 | } | ||
| 1266 | |||
| 1267 | # | ||
| 1268 | # remove the coordinate system from in the database | ||
| 1269 | # | ||
| 1270 | |||
| 1271 | my $sql = "DELETE FROM coord_system WHERE name = ?"; | ||
| 1272 | if ($version) { | ||
| 1273 | $sql .= " AND version = ?"; | ||
| 1274 | } | ||
| 1275 | |||
| 1276 | my $sth = $db->dbc->prepare($sql); | ||
| 1277 | |||
| 1278 | $sth->bind_param( 1, $name, SQL_VARCHAR ); | ||
| 1279 | $sth->bind_param( 2, $version, SQL_VARCHAR ) if $version; | ||
| 1280 | |||
| 1281 | $sth->execute(); | ||
| 1282 | $sth->finish(); | ||
| 1283 | |||
| 1284 | delete $self->{'_name_cache'}->{lc($name)}; | ||
| 1285 | |||
| 1286 | delete $self->{'_dbID_cache'}->{$dbID}; | ||
| 1287 | delete $self->{'_rank_cache'}->{$rank}; | ||
| 1288 | delete $self->{'_is_sequence_level'}->{$dbID}; | ||
| 1289 | delete $self->{'_is_default_version'}->{$dbID}; | ||
| 1290 | $cs->dbID(undef); | ||
| 1291 | $cs->adaptor(undef); | ||
| 1292 | |||
| 1293 | return $cs; | ||
| 1294 | } | ||
| 1295 | |||
| 1296 | 1; |