| 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::IdMapping::Cache - a cache to hold data objects used by the | ||
| 34 | IdMapping application | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | =head1 METHODS | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | |||
| 43 | package Bio::EnsEMBL::IdMapping::Cache; | ||
| 44 | $Bio::EnsEMBL::IdMapping::Cache::VERSION = '114.0.0_50'; # TRIAL | ||
| 45 | $Bio::EnsEMBL::IdMapping::Cache::VERSION = '114.0.050'; | ||
| 46 | use strict; | ||
| 47 | use warnings; | ||
| 48 | no warnings 'uninitialized'; | ||
| 49 | |||
| 50 | use Bio::EnsEMBL::Utils::Argument qw(rearrange); | ||
| 51 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 52 | use Bio::EnsEMBL::Utils::ScriptUtils qw(parse_bytes path_append); | ||
| 53 | use Bio::EnsEMBL::Utils::Scalar qw(assert_ref); | ||
| 54 | use Bio::EnsEMBL::IdMapping::TinyGene; | ||
| 55 | use Bio::EnsEMBL::IdMapping::TinyTranscript; | ||
| 56 | use Bio::EnsEMBL::IdMapping::TinyTranslation; | ||
| 57 | use Bio::EnsEMBL::IdMapping::TinyExon; | ||
| 58 | use Bio::EnsEMBL::DBSQL::DBAdaptor; | ||
| 59 | use Storable qw(nstore retrieve); | ||
| 60 | use Digest::MD5 qw(md5_hex); | ||
| 61 | |||
| 62 | # define available cache names here | ||
| 63 | my @cache_names = qw( | ||
| 64 | exons_by_id | ||
| 65 | transcripts_by_id | ||
| 66 | transcripts_by_exon_id | ||
| 67 | translations_by_id | ||
| 68 | genes_by_id | ||
| 69 | genes_by_transcript_id | ||
| 70 | ); | ||
| 71 | |||
| 72 | |||
| 73 | =head2 new | ||
| 74 | |||
| 75 | Arg [LOGGER]: Bio::EnsEMBL::Utils::Logger $logger - a logger object | ||
| 76 | Arg [CONF] : Bio::EnsEMBL::Utils::ConfParser $conf - a configuration object | ||
| 77 | Example : my $cache = Bio::EnsEMBL::IdMapping::Cache->new( | ||
| 78 | -LOGGER => $logger, | ||
| 79 | -CONF => $conf, | ||
| 80 | ); | ||
| 81 | Description : constructor | ||
| 82 | Return type : Bio::EnsEMBL::IdMapping::Cache object | ||
| 83 | Exceptions : thrown on wrong or missing arguments | ||
| 84 | Caller : general | ||
| 85 | Status : At Risk | ||
| 86 | : under development | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub new { | ||
| 91 | my $caller = shift; | ||
| 92 | my $class = ref($caller) || $caller; | ||
| 93 | |||
| 94 | my ($logger, $conf, $load_instance) = | ||
| 95 | rearrange(['LOGGER', 'CONF', 'LOAD_INSTANCE'], @_); | ||
| 96 | |||
| 97 | unless ($logger->isa('Bio::EnsEMBL::Utils::Logger')) { | ||
| 98 | throw("You must provide a Bio::EnsEMBL::Utils::Logger for logging."); | ||
| 99 | } | ||
| 100 | |||
| 101 | unless ($conf->isa('Bio::EnsEMBL::Utils::ConfParser')) { | ||
| 102 | throw("You must provide configuration as a Bio::EnsEMBL::Utils::ConfParser object."); | ||
| 103 | } | ||
| 104 | |||
| 105 | my $self = {}; | ||
| 106 | bless ($self, $class); | ||
| 107 | |||
| 108 | # initialise | ||
| 109 | $self->logger($logger); | ||
| 110 | $self->conf($conf); | ||
| 111 | |||
| 112 | if ($load_instance) { | ||
| 113 | $self->read_instance_from_file; | ||
| 114 | } | ||
| 115 | |||
| 116 | return $self; | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | =head2 build_cache_by_slice | ||
| 121 | |||
| 122 | Arg[1] : String $dbtype - db type (source|target) | ||
| 123 | Arg[2] : String $slice_name - the name of a slice (format as returned by | ||
| 124 | Bio::EnsEMBL::Slice->name) | ||
| 125 | Example : my ($num_genes, $filesize) = $cache->build_cache_by_slice( | ||
| 126 | 'source', 'chromosome:NCBI36:X:1:1000000:-1'); | ||
| 127 | Description : Builds a cache of genes, transcripts, translations and exons | ||
| 128 | needed by the IdMapping application and serialises the resulting | ||
| 129 | cache object to a file, one slice at a time. | ||
| 130 | Return type : list of the number of genes processed and the size of the | ||
| 131 | serialised cache file | ||
| 132 | Exceptions : thrown on invalid slice name | ||
| 133 | Caller : general | ||
| 134 | Status : At Risk | ||
| 135 | : under development | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub build_cache_by_slice { | ||
| 140 | my $self = shift; | ||
| 141 | my $dbtype = shift; | ||
| 142 | my $slice_name = shift; | ||
| 143 | |||
| 144 | # set cache method (required for loading cache later) | ||
| 145 | $self->cache_method('BY_SEQ_REGION'); | ||
| 146 | |||
| 147 | my $dba = $self->get_DBAdaptor($dbtype); | ||
| 148 | my $sa = $dba->get_SliceAdaptor; | ||
| 149 | |||
| 150 | my $slice = $sa->fetch_by_name($slice_name); | ||
| 151 | unless ($slice) { | ||
| 152 | throw("Could not retrieve slice $slice_name."); | ||
| 153 | } | ||
| 154 | |||
| 155 | my $genes = $slice->get_all_Genes( undef, undef, 1 ); | ||
| 156 | |||
| 157 | # find common coord_system | ||
| 158 | my $common_cs_found = $self->find_common_coord_systems; | ||
| 159 | |||
| 160 | # find out whether native coord_system is a common coord_system. | ||
| 161 | # if so, you don't need to project. | ||
| 162 | # also don't project if no common coord_system present | ||
| 163 | my $need_project = 1; | ||
| 164 | |||
| 165 | my $csid = join( ':', | ||
| 166 | $slice->coord_system_name, | ||
| 167 | $slice->coord_system->version ); | ||
| 168 | |||
| 169 | if ( $self->is_common_cs($csid) or !$self->highest_common_cs ) { | ||
| 170 | $need_project = 0; | ||
| 171 | } | ||
| 172 | |||
| 173 | # build cache | ||
| 174 | my $type = "$dbtype.$slice_name"; | ||
| 175 | my $num_genes = | ||
| 176 | $self->build_cache_from_genes( $type, $genes, $need_project ); | ||
| 177 | undef $genes; | ||
| 178 | |||
| 179 | # write cache to file, then flush cache to reclaim memory | ||
| 180 | my $size = $self->write_all_to_file($type); | ||
| 181 | |||
| 182 | return $num_genes, $size; | ||
| 183 | } ## end sub build_cache_by_slice | ||
| 184 | |||
| 185 | |||
| 186 | =head2 build_cache_all | ||
| 187 | |||
| 188 | Arg[1] : String $dbtype - db type (source|target) | ||
| 189 | Example : my ($num_genes, $filesize) = $cache->build_cache_all('source'); | ||
| 190 | Description : Builds a cache of genes, transcripts, translations and exons | ||
| 191 | needed by the IdMapping application and serialises the | ||
| 192 | resulting cache object to a file. All genes across the genome | ||
| 193 | are processed in one go. This method should be used when | ||
| 194 | build_cache_by_seq_region can't be used due to a large number | ||
| 195 | of toplevel seq_regions (e.g. 2x genomes). | ||
| 196 | Return type : list of the number of genes processed and the size of the | ||
| 197 | serialised cache file | ||
| 198 | Exceptions : thrown on invalid slice name | ||
| 199 | Caller : general | ||
| 200 | Status : At Risk | ||
| 201 | : under development | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub build_cache_all { | ||
| 206 | my $self = shift; | ||
| 207 | my $dbtype = shift; | ||
| 208 | |||
| 209 | # set cache method (required for loading cache later) | ||
| 210 | $self->cache_method('ALL'); | ||
| 211 | |||
| 212 | my $dba = $self->get_DBAdaptor($dbtype); | ||
| 213 | my $ga = $dba->get_GeneAdaptor; | ||
| 214 | |||
| 215 | my $genes = $ga->fetch_all; | ||
| 216 | |||
| 217 | # find common coord_system | ||
| 218 | my $common_cs_found = $self->find_common_coord_systems; | ||
| 219 | |||
| 220 | # Build cache. Setting $need_project to 'CHECK' will cause | ||
| 221 | # build_cache_from_genes() to check the coordinate system for each | ||
| 222 | # gene. | ||
| 223 | my $type = "$dbtype.ALL"; | ||
| 224 | my $need_project = 'CHECK'; | ||
| 225 | my $num_genes = | ||
| 226 | $self->build_cache_from_genes( $type, $genes, $need_project ); | ||
| 227 | |||
| 228 | undef $genes; | ||
| 229 | |||
| 230 | # write cache to file, then flush cache to reclaim memory | ||
| 231 | my $size = $self->write_all_to_file($type); | ||
| 232 | |||
| 233 | return $num_genes, $size; | ||
| 234 | } | ||
| 235 | |||
| 236 | |||
| 237 | =head2 build_cache_from_genes | ||
| 238 | |||
| 239 | Arg[1] : String $type - cache type | ||
| 240 | Arg[2] : Listref of Bio::EnsEMBL::Genes $genes - genes to build cache | ||
| 241 | from | ||
| 242 | Arg[3] : Boolean $need_project - indicate if we need to project exons to | ||
| 243 | common coordinate system | ||
| 244 | Example : $cache->build_cache_from_genes( | ||
| 245 | 'source.chromosome:NCBI36:X:1:100000:1', \@genes); | ||
| 246 | Description : Builds the cache by fetching transcripts, translations and exons | ||
| 247 | for a list of genes from the database, and creating lightweight | ||
| 248 | Bio::EnsEMBL::IdMapping::TinyFeature objects containing only the | ||
| 249 | data needed by the IdMapping application. These objects are | ||
| 250 | attached to a name cache in this cache object. Exons only need | ||
| 251 | to be projected to a commond coordinate system if their native | ||
| 252 | coordinate system isn't common to source and target assembly | ||
| 253 | itself. | ||
| 254 | Return type : int - number of genes after filtering | ||
| 255 | Exceptions : thrown on wrong or missing arguments | ||
| 256 | Caller : internal | ||
| 257 | Status : At Risk | ||
| 258 | : under development | ||
| 259 | |||
| 260 | =cut | ||
| 261 | |||
| 262 | sub build_cache_from_genes { | ||
| 263 | my $self = shift; | ||
| 264 | my $type = shift; | ||
| 265 | my $genes = shift; | ||
| 266 | my $need_project = shift; | ||
| 267 | |||
| 268 | throw("You must provide a type.") unless $type; | ||
| 269 | throw("You must provide a listref of genes.") | ||
| 270 | unless ( ref($genes) eq 'ARRAY' ); | ||
| 271 | |||
| 272 | # biotype filter | ||
| 273 | if ( $self->conf()->param('biotypes') || | ||
| 274 | $self->conf()->param('biotypes_include') || | ||
| 275 | $self->conf()->param('biotypes_exclude') ) | ||
| 276 | { | ||
| 277 | $genes = $self->filter_biotypes($genes); | ||
| 278 | } | ||
| 279 | my $num_genes = scalar(@$genes); | ||
| 280 | |||
| 281 | # initialise cache for the given type. | ||
| 282 | $self->{'cache'}->{$type} = {}; | ||
| 283 | |||
| 284 | #my $i = 0; | ||
| 285 | #my $num_genes = scalar(@$genes); | ||
| 286 | #my $progress_id = $self->logger->init_progress($num_genes); | ||
| 287 | |||
| 288 | # loop over genes sorted by gene location. | ||
| 289 | # the sort will hopefully improve assembly mapper cache performance and | ||
| 290 | # therefore speed up exon sequence retrieval | ||
| 291 | foreach my $gene ( sort { $a->start <=> $b->start } @$genes ) { | ||
| 292 | #$self->logger->log_progressbar($progress_id, ++$i, 2); | ||
| 293 | #$self->logger->log_progress($num_genes, ++$i, 20, 3, 1); | ||
| 294 | |||
| 295 | if ( $need_project eq 'CHECK' ) { | ||
| 296 | # find out whether native coord_system is a common coord_system. | ||
| 297 | # if so, you don't need to project. | ||
| 298 | # also don't project if no common coord_system present | ||
| 299 | if ( $self->highest_common_cs ) { | ||
| 300 | my $csid = join( ':', | ||
| 301 | $gene->slice->coord_system_name, | ||
| 302 | $gene->slice->coord_system->version ); | ||
| 303 | if ( $self->is_common_cs($csid) ) { | ||
| 304 | $need_project = 0; | ||
| 305 | } | ||
| 306 | } | ||
| 307 | else { | ||
| 308 | $need_project = 0; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | # create lightweigt gene | ||
| 313 | my $lgene = | ||
| 314 | Bio::EnsEMBL::IdMapping::TinyGene->new_fast( [ | ||
| 315 | $gene->dbID, $gene->stable_id, | ||
| 316 | $gene->version, $gene->created_date, | ||
| 317 | $gene->modified_date, $gene->start, | ||
| 318 | $gene->end, $gene->strand, | ||
| 319 | $gene->slice->seq_region_name, $gene->biotype, | ||
| 320 | $gene->analysis->logic_name, | ||
| 321 | ] ); | ||
| 322 | |||
| 323 | # build gene caches | ||
| 324 | $self->add( 'genes_by_id', $type, $gene->dbID, $lgene ); | ||
| 325 | |||
| 326 | # transcripts | ||
| 327 | foreach my $tr ( @{ $gene->get_all_Transcripts } ) { | ||
| 328 | my $ltr = | ||
| 329 | Bio::EnsEMBL::IdMapping::TinyTranscript->new_fast( [ | ||
| 330 | $tr->dbID, $tr->stable_id, | ||
| 331 | $tr->version, $tr->created_date, | ||
| 332 | $tr->modified_date, $tr->start, | ||
| 333 | $tr->end, $tr->strand, | ||
| 334 | $tr->length, md5_hex( $tr->spliced_seq ), | ||
| 335 | ] ); | ||
| 336 | |||
| 337 | $ltr->biotype( $tr->biotype() ); | ||
| 338 | $ltr->seq_region_name( $tr->slice->seq_region_name() ); | ||
| 339 | $lgene->add_Transcript($ltr); | ||
| 340 | |||
| 341 | # build transcript caches | ||
| 342 | $self->add( 'transcripts_by_id', $type, $tr->dbID, $ltr ); | ||
| 343 | $self->add( 'genes_by_transcript_id', $type, $tr->dbID, $lgene ); | ||
| 344 | |||
| 345 | # translation (if there is one) | ||
| 346 | if ( my $tl = $tr->translation ) { | ||
| 347 | my $ltl = | ||
| 348 | Bio::EnsEMBL::IdMapping::TinyTranslation->new_fast( [ | ||
| 349 | $tl->dbID, $tl->stable_id, | ||
| 350 | $tl->version, $tl->created_date, | ||
| 351 | $tl->modified_date, $tr->dbID, | ||
| 352 | $tr->translate->seq, | ||
| 353 | ] ); | ||
| 354 | |||
| 355 | $ltr->add_Translation($ltl); | ||
| 356 | |||
| 357 | $self->add( 'translations_by_id', $type, $tl->dbID, $ltl ); | ||
| 358 | |||
| 359 | undef $tl; | ||
| 360 | } | ||
| 361 | |||
| 362 | # exons | ||
| 363 | foreach my $exon ( @{ $tr->get_all_Exons } ) { | ||
| 364 | my $lexon = | ||
| 365 | Bio::EnsEMBL::IdMapping::TinyExon->new_fast( [ | ||
| 366 | $exon->dbID, | ||
| 367 | $exon->stable_id, | ||
| 368 | $exon->version, | ||
| 369 | $exon->created_date, | ||
| 370 | $exon->modified_date, | ||
| 371 | $exon->start, | ||
| 372 | $exon->end, | ||
| 373 | $exon->strand, | ||
| 374 | $exon->slice->seq_region_name, | ||
| 375 | $exon->slice->coord_system_name, | ||
| 376 | $exon->slice->coord_system->version, | ||
| 377 | $exon->slice->subseq( $exon->start, $exon->end, | ||
| 378 | $exon->strand ), | ||
| 379 | $exon->phase, | ||
| 380 | $need_project, ] ); | ||
| 381 | |||
| 382 | # get coordinates in common coordinate system if needed | ||
| 383 | if ($need_project) { | ||
| 384 | my @seg = @{ | ||
| 385 | $exon->project( $self->highest_common_cs, | ||
| 386 | $self->highest_common_cs_version ) }; | ||
| 387 | |||
| 388 | if ( scalar(@seg) == 1 ) { | ||
| 389 | my $sl = $seg[0]->to_Slice; | ||
| 390 | $lexon->common_start( $sl->start ); | ||
| 391 | $lexon->common_end( $sl->end ); | ||
| 392 | $lexon->common_strand( $sl->strand ); | ||
| 393 | $lexon->common_sr_name( $sl->seq_region_name ); | ||
| 394 | } | ||
| 395 | } | ||
| 396 | |||
| 397 | $ltr->add_Exon($lexon); | ||
| 398 | |||
| 399 | $self->add( 'exons_by_id', $type, $exon->dbID, $lexon ); | ||
| 400 | $self->add_list( 'transcripts_by_exon_id', | ||
| 401 | $type, $exon->dbID, $ltr ); | ||
| 402 | |||
| 403 | undef $exon; | ||
| 404 | } ## end foreach my $exon ( @{ $tr->get_all_Exons...}) | ||
| 405 | |||
| 406 | undef $tr; | ||
| 407 | } ## end foreach my $tr ( @{ $gene->get_all_Transcripts...}) | ||
| 408 | |||
| 409 | undef $gene; | ||
| 410 | } ## end foreach my $gene ( sort { $a...}) | ||
| 411 | |||
| 412 | return $num_genes; | ||
| 413 | } ## end sub build_cache_from_genes | ||
| 414 | |||
| 415 | |||
| 416 | =head2 filter_biotypes | ||
| 417 | |||
| 418 | Arg[1] : Listref of Bio::EnsEMBL::Genes $genes - the genes to filter | ||
| 419 | Example : my @filtered = @{ $cache->filter_biotypes(\@genes) }; | ||
| 420 | |||
| 421 | Description : Filters a list of genes by biotype. Biotypes are | ||
| 422 | taken from the IdMapping configuration parameter | ||
| 423 | 'biotypes_include' or 'biotypes_exclude'. | ||
| 424 | |||
| 425 | If the configuration parameter 'biotypes_exclude' is | ||
| 426 | defined, then rather than returning the genes whose | ||
| 427 | biotype is listed in the configuration parameter | ||
| 428 | 'biotypes_include' the method will return the genes | ||
| 429 | whose biotype is *not* listed in the 'biotypes_exclude' | ||
| 430 | configuration parameter. | ||
| 431 | |||
| 432 | It is an error to define both these configuration | ||
| 433 | parameters. | ||
| 434 | |||
| 435 | The old parameter 'biotypes' is equivalent to | ||
| 436 | 'biotypes_include'. | ||
| 437 | |||
| 438 | Return type : Listref of Bio::EnsEMBL::Genes (or empty list) | ||
| 439 | Exceptions : none | ||
| 440 | Caller : internal | ||
| 441 | Status : At Risk | ||
| 442 | : under development | ||
| 443 | |||
| 444 | =cut | ||
| 445 | |||
| 446 | sub filter_biotypes { | ||
| 447 | my ( $self, $genes ) = @_; | ||
| 448 | |||
| 449 | my @filtered; | ||
| 450 | my @biotypes; | ||
| 451 | my $opt_reverse; | ||
| 452 | |||
| 453 | if ( defined( $self->conf()->param('biotypes_include') ) || | ||
| 454 | defined( $self->conf()->param('biotypes') ) ) | ||
| 455 | { | ||
| 456 | if ( defined( $self->conf()->param('biotypes_exclude') ) ) { | ||
| 457 | $self->logger() | ||
| 458 | ->error( "You may not use both " . | ||
| 459 | "'biotypes_include' and 'biotypes_exclude' " . | ||
| 460 | "in the configuration" ); | ||
| 461 | } | ||
| 462 | |||
| 463 | if ( defined( $self->conf()->param('biotypes_include') ) ) { | ||
| 464 | @biotypes = $self->conf()->param('biotypes_include'); | ||
| 465 | } | ||
| 466 | else { | ||
| 467 | @biotypes = $self->conf()->param('biotypes'); | ||
| 468 | } | ||
| 469 | $opt_reverse = 0; | ||
| 470 | } | ||
| 471 | else { | ||
| 472 | @biotypes = $self->conf()->param('biotypes_exclude'); | ||
| 473 | $opt_reverse = 1; | ||
| 474 | } | ||
| 475 | |||
| 476 | foreach my $gene ( @{$genes} ) { | ||
| 477 | my $keep_gene; | ||
| 478 | |||
| 479 | foreach my $biotype (@biotypes) { | ||
| 480 | if ( $gene->biotype() eq $biotype ) { | ||
| 481 | if ($opt_reverse) { $keep_gene = 0 } | ||
| 482 | else { $keep_gene = 1 } | ||
| 483 | last; | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | if ( defined($keep_gene) ) { | ||
| 488 | if ($keep_gene) { | ||
| 489 | push( @filtered, $gene ); | ||
| 490 | } | ||
| 491 | } | ||
| 492 | elsif ($opt_reverse) { | ||
| 493 | push( @filtered, $gene ); | ||
| 494 | } | ||
| 495 | } | ||
| 496 | |||
| 497 | return \@filtered; | ||
| 498 | } ## end sub filter_biotypes | ||
| 499 | |||
| 500 | |||
| 501 | =head2 add | ||
| 502 | |||
| 503 | Arg[1] : String $name - a cache name (e.g. 'genes_by_id') | ||
| 504 | Arg[2] : String type - a cache type (e.g. "source.$slice_name") | ||
| 505 | Arg[3] : String $key - key of this entry (e.g. a gene dbID) | ||
| 506 | Arg[4] : Bio::EnsEMBL::IdMappping::TinyFeature $val - value to cache | ||
| 507 | Example : $cache->add('genes_by_id', | ||
| 508 | 'source.chromosome:NCBI36:X:1:1000000:1', '1234', $tiny_gene); | ||
| 509 | Description : Adds a TinyFeature object to a named cache. | ||
| 510 | Return type : Bio::EnsEMBL::IdMapping::TinyFeature | ||
| 511 | Exceptions : thrown on wrong or missing arguments | ||
| 512 | Caller : internal | ||
| 513 | Status : At Risk | ||
| 514 | : under development | ||
| 515 | |||
| 516 | =cut | ||
| 517 | |||
| 518 | sub add { | ||
| 519 | my $self = shift; | ||
| 520 | my $name = shift; | ||
| 521 | my $type = shift; | ||
| 522 | my $key = shift; | ||
| 523 | my $val = shift; | ||
| 524 | |||
| 525 | throw("You must provide a cache name (e.g. genes_by_id.") unless $name; | ||
| 526 | throw("You must provide a cache type.") unless $type; | ||
| 527 | throw("You must provide a cache key (e.g. a gene dbID).") unless $key; | ||
| 528 | |||
| 529 | $self->{'cache'}->{$type}->{$name}->{$key} = $val; | ||
| 530 | |||
| 531 | return $self->{'cache'}->{$type}->{$name}->{$key}; | ||
| 532 | } | ||
| 533 | |||
| 534 | =head2 add_list | ||
| 535 | |||
| 536 | Arg[1] : String $name - a cache name (e.g. 'genes_by_id') | ||
| 537 | Arg[2] : String type - a cache type (e.g. "source.$slice_name") | ||
| 538 | Arg[3] : String $key - key of this entry (e.g. a gene dbID) | ||
| 539 | Arg[4] : List of Bio::EnsEMBL::IdMappping::TinyFeature @val - values | ||
| 540 | to cache | ||
| 541 | Example : $cache->add_list('transcripts_by_exon_id', | ||
| 542 | 'source.chromosome:NCBI36:X:1:1000000:1', '1234', | ||
| 543 | $tiny_transcript1, $tiny_transcript2); | ||
| 544 | Description : Adds a list of TinyFeature objects to a named cache. | ||
| 545 | Return type : Listref of Bio::EnsEMBL::IdMapping::TinyFeature objects | ||
| 546 | Exceptions : thrown on wrong or missing arguments | ||
| 547 | Caller : internal | ||
| 548 | Status : At Risk | ||
| 549 | : under development | ||
| 550 | |||
| 551 | =cut | ||
| 552 | |||
| 553 | sub add_list { | ||
| 554 | my $self = shift; | ||
| 555 | my $name = shift; | ||
| 556 | my $type = shift; | ||
| 557 | my $key = shift; | ||
| 558 | my @vals = @_; | ||
| 559 | |||
| 560 | throw("You must provide a cache name (e.g. genes_by_id.") unless $name; | ||
| 561 | throw("You must provide a cache type.") unless $type; | ||
| 562 | throw("You must provide a cache key (e.g. a gene dbID).") unless $key; | ||
| 563 | |||
| 564 | push @{ $self->{'cache'}->{$type}->{$name}->{$key} }, @vals; | ||
| 565 | |||
| 566 | return $self->{'cache'}->{$type}->{$name}->{$key}; | ||
| 567 | } | ||
| 568 | |||
| 569 | sub get_by_key { | ||
| 570 | my $self = shift; | ||
| 571 | my $name = shift; | ||
| 572 | my $type = shift; | ||
| 573 | my $key = shift; | ||
| 574 | |||
| 575 | throw("You must provide a cache name (e.g. genes_by_id.") unless $name; | ||
| 576 | throw("You must provide a cache type.") unless $type; | ||
| 577 | throw("You must provide a cache key (e.g. a gene dbID).") unless $key; | ||
| 578 | |||
| 579 | # transparently load cache from file unless already loaded | ||
| 580 | unless ($self->{'instance'}->{'loaded'}->{"$type"}) { | ||
| 581 | $self->read_and_merge($type); | ||
| 582 | } | ||
| 583 | |||
| 584 | return $self->{'cache'}->{$type}->{$name}->{$key}; | ||
| 585 | } | ||
| 586 | |||
| 587 | sub get_by_name { | ||
| 588 | my $self = shift; | ||
| 589 | my $name = shift; | ||
| 590 | my $type = shift; | ||
| 591 | |||
| 592 | throw("You must provide a cache name (e.g. genes_by_id.") unless $name; | ||
| 593 | throw("You must provide a cache type.") unless $type; | ||
| 594 | |||
| 595 | # transparently load cache from file unless already loaded | ||
| 596 | unless ($self->{'instance'}->{'loaded'}->{$type}) { | ||
| 597 | $self->read_and_merge($type); | ||
| 598 | } | ||
| 599 | |||
| 600 | return $self->{'cache'}->{$type}->{$name} || {}; | ||
| 601 | } | ||
| 602 | |||
| 603 | |||
| 604 | sub get_count_by_name { | ||
| 605 | my $self = shift; | ||
| 606 | my $name = shift; | ||
| 607 | my $type = shift; | ||
| 608 | |||
| 609 | throw("You must provide a cache name (e.g. genes_by_id.") unless $name; | ||
| 610 | throw("You must provide a cache type.") unless $type; | ||
| 611 | |||
| 612 | # transparently load cache from file unless already loaded | ||
| 613 | unless ($self->{'instance'}->{'loaded'}->{$type}) { | ||
| 614 | $self->read_and_merge($type); | ||
| 615 | } | ||
| 616 | |||
| 617 | return scalar(keys %{ $self->get_by_name($name, $type) }); | ||
| 618 | } | ||
| 619 | |||
| 620 | |||
| 621 | sub find_common_coord_systems { | ||
| 622 | my $self = shift; | ||
| 623 | |||
| 624 | # get adaptors for source db | ||
| 625 | my $s_dba = $self->get_DBAdaptor('source'); | ||
| 626 | my $s_csa = $s_dba->get_CoordSystemAdaptor; | ||
| 627 | my $s_sa = $s_dba->get_SliceAdaptor; | ||
| 628 | |||
| 629 | # get adaptors for target db | ||
| 630 | my $t_dba = $self->get_DBAdaptor('target'); | ||
| 631 | my $t_csa = $t_dba->get_CoordSystemAdaptor; | ||
| 632 | my $t_sa = $t_dba->get_SliceAdaptor; | ||
| 633 | |||
| 634 | # find common coord_systems | ||
| 635 | my @s_coord_systems = @{ $s_csa->fetch_all }; | ||
| 636 | my @t_coord_systems = @{ $t_csa->fetch_all }; | ||
| 637 | my $found_highest = 0; | ||
| 638 | |||
| 639 | SOURCE: | ||
| 640 | foreach my $s_cs (@s_coord_systems) { | ||
| 641 | if ( !$s_cs->is_default() ) { next SOURCE } | ||
| 642 | |||
| 643 | TARGET: | ||
| 644 | foreach my $t_cs (@t_coord_systems) { | ||
| 645 | if ( !$t_cs->is_default() ) { next TARGET } | ||
| 646 | |||
| 647 | if ( $s_cs->name eq $t_cs->name ) { | ||
| 648 | |||
| 649 | # test for identical coord_system version | ||
| 650 | if ( $s_cs->version and ( $s_cs->version ne $t_cs->version ) ) { | ||
| 651 | next TARGET; | ||
| 652 | } | ||
| 653 | |||
| 654 | # test for at least 50% identical seq_regions | ||
| 655 | if ( $self->seq_regions_compatible( $s_cs, $s_sa, $t_sa ) ) { | ||
| 656 | $self->add_common_cs($s_cs); | ||
| 657 | |||
| 658 | unless ($found_highest) { | ||
| 659 | $self->highest_common_cs( $s_cs->name ); | ||
| 660 | $self->highest_common_cs_version( $s_cs->version ); | ||
| 661 | } | ||
| 662 | |||
| 663 | $found_highest = 1; | ||
| 664 | |||
| 665 | next SOURCE; | ||
| 666 | } | ||
| 667 | } | ||
| 668 | } ## end foreach my $t_cs (@t_coord_systems) | ||
| 669 | } ## end foreach my $s_cs (@s_coord_systems) | ||
| 670 | |||
| 671 | return $found_highest; | ||
| 672 | } ## end sub find_common_coord_systems | ||
| 673 | |||
| 674 | |||
| 675 | sub seq_regions_compatible { | ||
| 676 | my $self = shift; | ||
| 677 | my $cs = shift; | ||
| 678 | my $s_sa = shift; | ||
| 679 | my $t_sa = shift; | ||
| 680 | |||
| 681 | unless ($cs and $cs->isa('Bio::EnsEMBL::CoordSystem')) { | ||
| 682 | throw('You must provide a CoordSystem'); | ||
| 683 | } | ||
| 684 | |||
| 685 | unless ($s_sa and $t_sa and $s_sa->isa('Bio::EnsEMBL::DBSQL::SliceAdaptor') | ||
| 686 | and $t_sa->isa('Bio::EnsEMBL::DBSQL::SliceAdaptor')) { | ||
| 687 | throw('You must provide a source and target SliceAdaptor'); | ||
| 688 | } | ||
| 689 | |||
| 690 | my %sr_match; | ||
| 691 | my $equal = 0; | ||
| 692 | |||
| 693 | my $s_seq_regions = $s_sa->fetch_all($cs->name, $cs->version); | ||
| 694 | my $t_seq_regions = $t_sa->fetch_all($cs->name, $cs->version); | ||
| 695 | |||
| 696 | # sanity check to prevent divison by zero | ||
| 697 | my $s_count = scalar(@$s_seq_regions); | ||
| 698 | my $t_count = scalar(@$t_seq_regions); | ||
| 699 | return(0) if ($s_count == 0 or $t_count == 0); | ||
| 700 | |||
| 701 | foreach my $s_sr (@$s_seq_regions) { | ||
| 702 | $sr_match{$s_sr->seq_region_name} = $s_sr->length; | ||
| 703 | } | ||
| 704 | |||
| 705 | foreach my $t_sr (@$t_seq_regions) { | ||
| 706 | if (exists($sr_match{$t_sr->seq_region_name})) { | ||
| 707 | $equal++; | ||
| 708 | |||
| 709 | # return false if we have a region with same name but different length | ||
| 710 | return(0) unless ($sr_match{$t_sr->seq_region_name} == $t_sr->length); | ||
| 711 | } | ||
| 712 | } | ||
| 713 | |||
| 714 | if ($equal/$s_count > 0.5 and $equal/$t_count > 0.5) { | ||
| 715 | return(1); | ||
| 716 | } else { | ||
| 717 | $self->logger->info("Only $equal seq_regions identical for ".$cs->name." ".$cs->version."\n"); | ||
| 718 | return(0); | ||
| 719 | } | ||
| 720 | |||
| 721 | } | ||
| 722 | |||
| 723 | |||
| 724 | sub check_db_connection { | ||
| 725 | my $self = shift; | ||
| 726 | my $dbtype = shift; | ||
| 727 | |||
| 728 | my $err = 0; | ||
| 729 | |||
| 730 | eval { | ||
| 731 | my $dba = $self->get_DBAdaptor($dbtype); | ||
| 732 | $dba->dbc->connect; | ||
| 733 | }; | ||
| 734 | |||
| 735 | if ($@) { | ||
| 736 | $self->logger->warning("Can't connect to $dbtype db: $@\n"); | ||
| 737 | $err++; | ||
| 738 | } else { | ||
| 739 | $self->logger->debug("Connection to $dbtype db ok.\n"); | ||
| 740 | $self->{'_db_conn_ok'}->{$dbtype} = 1; | ||
| 741 | } | ||
| 742 | |||
| 743 | return $err; | ||
| 744 | } | ||
| 745 | |||
| 746 | |||
| 747 | sub check_db_read_permissions { | ||
| 748 | my $self = shift; | ||
| 749 | my $dbtype = shift; | ||
| 750 | |||
| 751 | # skip this check if db connection failed (this prevents re-throwing | ||
| 752 | # exceptions). | ||
| 753 | return 1 unless ($self->{'_db_conn_ok'}->{$dbtype}); | ||
| 754 | |||
| 755 | my $err = 0; | ||
| 756 | my %privs = %{ $self->get_db_privs($dbtype) }; | ||
| 757 | |||
| 758 | unless ($privs{'SELECT'} or $privs{'ALL PRIVILEGES'}) { | ||
| 759 | $self->logger->warning("User doesn't have read permission on $dbtype db.\n"); | ||
| 760 | $err++; | ||
| 761 | } else { | ||
| 762 | $self->logger->debug("Read permission on $dbtype db ok.\n"); | ||
| 763 | } | ||
| 764 | |||
| 765 | return $err; | ||
| 766 | } | ||
| 767 | |||
| 768 | |||
| 769 | sub check_db_write_permissions { | ||
| 770 | my $self = shift; | ||
| 771 | my $dbtype = shift; | ||
| 772 | |||
| 773 | # skip this check if db connection failed (this prevents re-throwing | ||
| 774 | # exceptions). | ||
| 775 | return 1 unless ($self->{'_db_conn_ok'}->{$dbtype}); | ||
| 776 | |||
| 777 | my $err = 0; | ||
| 778 | |||
| 779 | unless ($self->do_upload) { | ||
| 780 | $self->logger->debug("No uploads, so write permission on $dbtype db not required.\n"); | ||
| 781 | return $err; | ||
| 782 | } | ||
| 783 | |||
| 784 | my %privs = %{ $self->get_db_privs($dbtype) }; | ||
| 785 | |||
| 786 | unless ($privs{'INSERT'} or $privs{'ALL PRIVILEGES'}) { | ||
| 787 | $self->logger->warning("User doesn't have write permission on $dbtype db.\n"); | ||
| 788 | $err++; | ||
| 789 | } else { | ||
| 790 | $self->logger->debug("Write permission on $dbtype db ok.\n"); | ||
| 791 | } | ||
| 792 | |||
| 793 | return $err; | ||
| 794 | } | ||
| 795 | |||
| 796 | |||
| 797 | sub do_upload { | ||
| 798 | my $self = shift; | ||
| 799 | |||
| 800 | if ($self->conf->param('dry_run') or | ||
| 801 | ! ($self->conf->param('upload_events') or | ||
| 802 | $self->conf->param('upload_stable_ids') or | ||
| 803 | $self->conf->param('upload_archive'))) { | ||
| 804 | return 0; | ||
| 805 | } else { | ||
| 806 | return 1; | ||
| 807 | } | ||
| 808 | } | ||
| 809 | |||
| 810 | |||
| 811 | sub get_db_privs { | ||
| 812 | my ( $self, $dbtype ) = @_; | ||
| 813 | |||
| 814 | my %privs = (); | ||
| 815 | my $rs; | ||
| 816 | |||
| 817 | # get privileges from mysql db | ||
| 818 | eval { | ||
| 819 | my $dbc = $self->get_DBAdaptor($dbtype)->dbc(); | ||
| 820 | my $sql = qq(SHOW GRANTS FOR ) . $dbc->username(); | ||
| 821 | my $sth = $dbc->prepare($sql); | ||
| 822 | $sth->execute(); | ||
| 823 | $rs = $sth->fetchall_arrayref(); | ||
| 824 | #$sth->finish(); | ||
| 825 | }; | ||
| 826 | |||
| 827 | if ($@) { | ||
| 828 | $self->logger->warning( | ||
| 829 | "Error obtaining privileges from $dbtype db: $@\n"); | ||
| 830 | return {}; | ||
| 831 | } | ||
| 832 | |||
| 833 | # parse the output | ||
| 834 | foreach my $r ( map { $_->[0] } @{$rs} ) { | ||
| 835 | $r =~ s/GRANT (.*) ON .*/$1/i; | ||
| 836 | foreach my $p ( split( ',', $r ) ) { | ||
| 837 | # trim leading and trailing whitespace | ||
| 838 | $p =~ s/^\s+//; | ||
| 839 | $p =~ s/\s+$//; | ||
| 840 | $privs{ uc($p) } = 1; | ||
| 841 | } | ||
| 842 | } | ||
| 843 | |||
| 844 | return \%privs; | ||
| 845 | } ## end sub get_db_privs | ||
| 846 | |||
| 847 | |||
| 848 | sub check_empty_tables { | ||
| 849 | my $self = shift; | ||
| 850 | my $dbtype = shift; | ||
| 851 | |||
| 852 | # skip this check if db connection failed (this prevents re-throwing | ||
| 853 | # exceptions). | ||
| 854 | return 1 unless ($self->{'_db_conn_ok'}->{$dbtype}); | ||
| 855 | |||
| 856 | my $err = 0; | ||
| 857 | my $c = 0; | ||
| 858 | |||
| 859 | if ($self->conf->param('no_check_empty_tables') or !$self->do_upload) { | ||
| 860 | $self->logger->debug("Won't check for empty stable ID and archive tables in $dbtype db.\n"); | ||
| 861 | return $err; | ||
| 862 | } | ||
| 863 | |||
| 864 | eval { | ||
| 865 | my @tables = | ||
| 866 | qw( | ||
| 867 | gene_stable_id | ||
| 868 | transcript_stable_id | ||
| 869 | translation_stable_id | ||
| 870 | exon_stable_id | ||
| 871 | stable_id_event | ||
| 872 | mapping_session | ||
| 873 | gene_archive | ||
| 874 | peptide_archive | ||
| 875 | ); | ||
| 876 | |||
| 877 | my $dba = $self->get_DBAdaptor($dbtype); | ||
| 878 | foreach my $table (@tables) { | ||
| 879 | if ( $table =~ /^([^_]+)_stable_id/ ) { | ||
| 880 | $table = $1; | ||
| 881 | if ( $c = | ||
| 882 | $self->fetch_value_from_db( | ||
| 883 | $dba, | ||
| 884 | "SELECT COUNT(*) FROM $table WHERE stable_id IS NOT NULL" | ||
| 885 | ) ) | ||
| 886 | { | ||
| 887 | $self->logger->warning( | ||
| 888 | "$table table in $dbtype db has $c stable IDs.\n"); | ||
| 889 | $err++; | ||
| 890 | } | ||
| 891 | } | ||
| 892 | else { | ||
| 893 | if ( $c = | ||
| 894 | $self->fetch_value_from_db( | ||
| 895 | $dba, "SELECT COUNT(*) FROM $table" | ||
| 896 | ) ) | ||
| 897 | { | ||
| 898 | $self->logger->warning( | ||
| 899 | "$table table in $dbtype db has $c entries.\n"); | ||
| 900 | $err++; | ||
| 901 | } | ||
| 902 | } | ||
| 903 | } ## end foreach my $table (@tables) | ||
| 904 | }; | ||
| 905 | |||
| 906 | if ($@) { | ||
| 907 | $self->logger->warning( | ||
| 908 | "Error retrieving stable ID and archive table row counts from $dbtype db: $@\n" | ||
| 909 | ); | ||
| 910 | $err++; | ||
| 911 | } | ||
| 912 | elsif ( !$err ) { | ||
| 913 | $self->logger->debug( | ||
| 914 | "All stable ID and archive tables in $dbtype db are empty.\n"); | ||
| 915 | } | ||
| 916 | return $err; | ||
| 917 | } | ||
| 918 | |||
| 919 | |||
| 920 | sub check_sequence { | ||
| 921 | my ( $self, $dbtype ) = @_; | ||
| 922 | |||
| 923 | # skip this check if db connection failed (this prevents re-throwing | ||
| 924 | # exceptions). | ||
| 925 | return 1 unless ( $self->{'_db_conn_ok'}->{$dbtype} ); | ||
| 926 | |||
| 927 | my $err = 0; | ||
| 928 | my $c = 0; | ||
| 929 | |||
| 930 | eval { | ||
| 931 | my $dba = $self->get_DBAdaptor($dbtype); | ||
| 932 | unless ( $c = | ||
| 933 | $self->fetch_value_from_db( | ||
| 934 | $dba->dnadb(), "SELECT COUNT(*) FROM dna" | ||
| 935 | ) ) | ||
| 936 | { | ||
| 937 | $err++; | ||
| 938 | } | ||
| 939 | }; | ||
| 940 | |||
| 941 | if ($@) { | ||
| 942 | $self->logger->warning( "Error retrieving dna table row count " | ||
| 943 | . "from $dbtype database: $@\n" ); | ||
| 944 | $err++; | ||
| 945 | } elsif ($err) { | ||
| 946 | $self->logger->warning("No sequence found in $dbtype database.\n"); | ||
| 947 | } else { | ||
| 948 | $self->logger->debug( | ||
| 949 | ucfirst($dbtype) . " db has sequence ($c entries).\n" ); | ||
| 950 | } | ||
| 951 | |||
| 952 | return $err; | ||
| 953 | } ## end sub check_sequence | ||
| 954 | |||
| 955 | |||
| 956 | sub check_meta_entries { | ||
| 957 | my $self = shift; | ||
| 958 | my $dbtype = shift; | ||
| 959 | |||
| 960 | # skip this check if db connection failed (this prevents re-throwing | ||
| 961 | # exceptions). | ||
| 962 | return 1 unless ($self->{'_db_conn_ok'}->{$dbtype}); | ||
| 963 | |||
| 964 | my $err = 0; | ||
| 965 | my $assembly_default; | ||
| 966 | my $schema_version; | ||
| 967 | |||
| 968 | eval { | ||
| 969 | my $dba = $self->get_DBAdaptor($dbtype); | ||
| 970 | $assembly_default = $self->fetch_value_from_db($dba, | ||
| 971 | qq(SELECT meta_value FROM meta WHERE meta_key = 'assembly.default')); | ||
| 972 | $schema_version = $self->fetch_value_from_db($dba, | ||
| 973 | qq(SELECT meta_value FROM meta WHERE meta_key = 'schema_version')); | ||
| 974 | }; | ||
| 975 | |||
| 976 | if ($@) { | ||
| 977 | $self->logger->warning("Error retrieving dna table row count from $dbtype db: $@\n"); | ||
| 978 | return ++$err; | ||
| 979 | } | ||
| 980 | |||
| 981 | unless ($assembly_default) { | ||
| 982 | $self->logger->warning("No meta.assembly.default value found in $dbtype db.\n"); | ||
| 983 | $err++; | ||
| 984 | } else { | ||
| 985 | $self->logger->debug("meta.assembly.default value found ($assembly_default).\n"); | ||
| 986 | } | ||
| 987 | |||
| 988 | unless ($schema_version) { | ||
| 989 | $self->logger->warning("No meta.schema_version value found in $dbtype db.\n"); | ||
| 990 | $err++; | ||
| 991 | } else { | ||
| 992 | $self->logger->debug("meta.schema_version value found ($schema_version).\n"); | ||
| 993 | } | ||
| 994 | |||
| 995 | return $err; | ||
| 996 | } | ||
| 997 | |||
| 998 | |||
| 999 | sub fetch_value_from_db { | ||
| 1000 | my ( $self, $dba, $sql ) = @_; | ||
| 1001 | |||
| 1002 | assert_ref( $dba, 'Bio::EnsEMBL::DBSQL::DBAdaptor' ); | ||
| 1003 | |||
| 1004 | if ( !defined($sql) ) { | ||
| 1005 | throw("Need an SQL statement to execute.\n"); | ||
| 1006 | } | ||
| 1007 | |||
| 1008 | my $sth = $dba->dbc->prepare($sql); | ||
| 1009 | $sth->execute(); | ||
| 1010 | |||
| 1011 | my ($c) = $sth->fetchrow_array; | ||
| 1012 | return $c; | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | sub get_DBAdaptor { | ||
| 1016 | my ( $self, $prefix ) = @_; | ||
| 1017 | |||
| 1018 | unless ( $self->{'_dba'}->{$prefix} ) { | ||
| 1019 | # connect to database | ||
| 1020 | my $dba = | ||
| 1021 | new Bio::EnsEMBL::DBSQL::DBAdaptor( | ||
| 1022 | -host => $self->conf->param("${prefix}host"), | ||
| 1023 | -port => $self->conf->param("${prefix}port"), | ||
| 1024 | -user => $self->conf->param("${prefix}user"), | ||
| 1025 | -pass => $self->conf->param("${prefix}pass"), | ||
| 1026 | -dbname => $self->conf->param("${prefix}dbname"), | ||
| 1027 | -group => $prefix, ); | ||
| 1028 | |||
| 1029 | if ( !defined( $self->conf->param("${prefix}host_dna") ) ) { | ||
| 1030 | # explicitely set the dnadb to itself - by default the Registry | ||
| 1031 | # assumes a group 'core' for this now | ||
| 1032 | $dba->dnadb($dba); | ||
| 1033 | } else { | ||
| 1034 | my $dna_dba = | ||
| 1035 | new Bio::EnsEMBL::DBSQL::DBAdaptor( | ||
| 1036 | -host => $self->conf->param("${prefix}host_dna"), | ||
| 1037 | -port => $self->conf->param("${prefix}port_dna"), | ||
| 1038 | -user => $self->conf->param("${prefix}user_dna"), | ||
| 1039 | -pass => $self->conf->param("${prefix}pass_dna"), | ||
| 1040 | -dbname => $self->conf->param("${prefix}dbname_dna"), | ||
| 1041 | -group => $prefix, ); | ||
| 1042 | $dba->dnadb($dna_dba); | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | $self->{'_dba'}->{$prefix} = $dba; | ||
| 1046 | } ## end unless ( $self->{'_dba'}->...) | ||
| 1047 | |||
| 1048 | return $self->{'_dba'}->{$prefix}; | ||
| 1049 | } ## end sub get_DBAdaptor | ||
| 1050 | |||
| 1051 | |||
| 1052 | sub get_production_DBAdaptor() { | ||
| 1053 | my ($self) = @_; | ||
| 1054 | my $dba = new Bio::EnsEMBL::DBSQL::DBAdaptor( | ||
| 1055 | -host => $self->conf->param("productionhost"), | ||
| 1056 | -port => $self->conf->param("productionport"), | ||
| 1057 | -user => $self->conf->param("productionuser"), | ||
| 1058 | -pass => $self->conf->param("productionpass"), | ||
| 1059 | -dbname => $self->conf->param("productiondbname")); | ||
| 1060 | return $dba; | ||
| 1061 | } | ||
| 1062 | |||
| 1063 | |||
| 1064 | sub cache_file_exists { | ||
| 1065 | my $self = shift; | ||
| 1066 | my $type = shift; | ||
| 1067 | |||
| 1068 | throw("You must provide a cache type.") unless $type; | ||
| 1069 | |||
| 1070 | my $cache_file = $self->cache_file($type); | ||
| 1071 | |||
| 1072 | if (-e $cache_file) { | ||
| 1073 | $self->logger->info("Cache file found for $type.\n", 2); | ||
| 1074 | $self->logger->debug("Will read from $cache_file.\n", 2); | ||
| 1075 | return 1; | ||
| 1076 | } else { | ||
| 1077 | $self->logger->info("No cache file found for $type.\n", 2); | ||
| 1078 | $self->logger->info("Will build cache from db.\n", 2); | ||
| 1079 | return 0; | ||
| 1080 | } | ||
| 1081 | } | ||
| 1082 | |||
| 1083 | |||
| 1084 | sub cache_file { | ||
| 1085 | my $self = shift; | ||
| 1086 | my $type = shift; | ||
| 1087 | |||
| 1088 | throw("You must provide a cache type.") unless $type; | ||
| 1089 | |||
| 1090 | return $self->dump_path."/$type.object_cache.ser"; | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | |||
| 1094 | sub instance_file { | ||
| 1095 | my $self = shift; | ||
| 1096 | |||
| 1097 | return $self->dump_path."/cache_instance.ser"; | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | |||
| 1101 | sub dump_path { | ||
| 1102 | my $self = shift; | ||
| 1103 | |||
| 1104 | $self->{'dump_path'} ||= path_append($self->conf->param('basedir'), 'cache'); | ||
| 1105 | |||
| 1106 | return $self->{'dump_path'}; | ||
| 1107 | } | ||
| 1108 | |||
| 1109 | |||
| 1110 | sub write_all_to_file { | ||
| 1111 | my $self = shift; | ||
| 1112 | my $type = shift; | ||
| 1113 | |||
| 1114 | throw("You must provide a cache type.") unless $type; | ||
| 1115 | |||
| 1116 | my $size = 0; | ||
| 1117 | $size += $self->write_to_file($type); | ||
| 1118 | $size += $self->write_instance_to_file; | ||
| 1119 | |||
| 1120 | return parse_bytes($size); | ||
| 1121 | } | ||
| 1122 | |||
| 1123 | |||
| 1124 | sub write_to_file { | ||
| 1125 | my $self = shift; | ||
| 1126 | my $type = shift; | ||
| 1127 | |||
| 1128 | throw("You must provide a cache type.") unless $type; | ||
| 1129 | |||
| 1130 | unless ($self->{'cache'}->{$type}) { | ||
| 1131 | $self->logger->warning("No features found in $type. Won't write cache file.\n"); | ||
| 1132 | return; | ||
| 1133 | } | ||
| 1134 | |||
| 1135 | my $cache_file = $self->cache_file($type); | ||
| 1136 | |||
| 1137 | eval { nstore($self->{'cache'}->{$type}, $cache_file) }; | ||
| 1138 | if ($@) { | ||
| 1139 | throw("Unable to store $cache_file: $@\n"); | ||
| 1140 | } | ||
| 1141 | |||
| 1142 | my $size = -s $cache_file; | ||
| 1143 | return $size; | ||
| 1144 | } | ||
| 1145 | |||
| 1146 | |||
| 1147 | sub write_instance_to_file { | ||
| 1148 | my $self = shift; | ||
| 1149 | |||
| 1150 | my $instance_file = $self->instance_file; | ||
| 1151 | |||
| 1152 | eval { nstore($self->{'instance'}, $instance_file) }; | ||
| 1153 | if ($@) { | ||
| 1154 | throw("Unable to store $instance_file: $@\n"); | ||
| 1155 | } | ||
| 1156 | |||
| 1157 | my $size = -s $instance_file; | ||
| 1158 | return $size; | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | |||
| 1162 | sub read_from_file { | ||
| 1163 | my $self = shift; | ||
| 1164 | my $type = shift; | ||
| 1165 | |||
| 1166 | throw("You must provide a cache type.") unless $type; | ||
| 1167 | |||
| 1168 | my $cache_file = $self->cache_file($type); | ||
| 1169 | |||
| 1170 | if (-s $cache_file) { | ||
| 1171 | |||
| 1172 | #$self->logger->info("Reading cache from file...\n", 0, 'stamped'); | ||
| 1173 | #$self->logger->info("Cache file $cache_file.\n", 1); | ||
| 1174 | eval { $self->{'cache'}->{$type} = retrieve($cache_file); }; | ||
| 1175 | if ($@) { | ||
| 1176 | throw("Unable to retrieve cache: $@"); | ||
| 1177 | } | ||
| 1178 | #$self->logger->info("Done.\n", 0, 'stamped'); | ||
| 1179 | |||
| 1180 | } else { | ||
| 1181 | $self->logger->warning("Cache file $cache_file not found or empty.\n"); | ||
| 1182 | } | ||
| 1183 | |||
| 1184 | |||
| 1185 | return $self->{'cache'}->{$type}; | ||
| 1186 | } | ||
| 1187 | |||
| 1188 | |||
| 1189 | sub read_and_merge { | ||
| 1190 | my $self = shift; | ||
| 1191 | my $dbtype = shift; | ||
| 1192 | |||
| 1193 | unless ($dbtype eq 'source' or $dbtype eq 'target') { | ||
| 1194 | throw("Db type must be 'source' or 'target'."); | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | # read cache from single or multiple files, depending on caching strategy | ||
| 1198 | my $cache_method = $self->cache_method; | ||
| 1199 | if ($cache_method eq 'ALL') { | ||
| 1200 | $self->read_from_file("$dbtype.ALL"); | ||
| 1201 | } elsif ($cache_method eq 'BY_SEQ_REGION') { | ||
| 1202 | foreach my $slice_name (@{ $self->slice_names($dbtype) }) { | ||
| 1203 | $self->read_from_file("$dbtype.$slice_name"); | ||
| 1204 | } | ||
| 1205 | } else { | ||
| 1206 | throw("Unknown cache method: $cache_method."); | ||
| 1207 | } | ||
| 1208 | |||
| 1209 | $self->merge($dbtype); | ||
| 1210 | |||
| 1211 | # flag as being loaded | ||
| 1212 | $self->{'instance'}->{'loaded'}->{$dbtype} = 1; | ||
| 1213 | } | ||
| 1214 | |||
| 1215 | |||
| 1216 | sub merge { | ||
| 1217 | my $self = shift; | ||
| 1218 | my $dbtype = shift; | ||
| 1219 | |||
| 1220 | unless ($dbtype eq 'source' or $dbtype eq 'target') { | ||
| 1221 | throw("Db type must be 'source' or 'target'."); | ||
| 1222 | } | ||
| 1223 | |||
| 1224 | foreach my $type (keys %{ $self->{'cache'} || {} }) { | ||
| 1225 | next unless ($type =~ /^$dbtype/); | ||
| 1226 | |||
| 1227 | foreach my $name (keys %{ $self->{'cache'}->{$type} || {} }) { | ||
| 1228 | |||
| 1229 | foreach my $key (keys %{ $self->{'cache'}->{$type}->{$name} || {} }) { | ||
| 1230 | if (defined $self->{'cache'}->{$dbtype}->{$name}->{$key}) { | ||
| 1231 | # warning("Duplicate key in cache: $name|$dbtype|$key. Skipping.\n"); | ||
| 1232 | } else { | ||
| 1233 | $self->{'cache'}->{$dbtype}->{$name}->{$key} = | ||
| 1234 | $self->{'cache'}->{$type}->{$name}->{$key}; | ||
| 1235 | } | ||
| 1236 | |||
| 1237 | delete $self->{'cache'}->{$type}->{$name}->{$key}; | ||
| 1238 | } | ||
| 1239 | |||
| 1240 | delete $self->{'cache'}->{$type}->{$name}; | ||
| 1241 | } | ||
| 1242 | |||
| 1243 | delete $self->{'cache'}->{$type}; | ||
| 1244 | |||
| 1245 | } | ||
| 1246 | } | ||
| 1247 | |||
| 1248 | |||
| 1249 | sub read_instance_from_file { | ||
| 1250 | my $self = shift; | ||
| 1251 | |||
| 1252 | my $instance_file = $self->instance_file; | ||
| 1253 | |||
| 1254 | unless (-s $instance_file) { | ||
| 1255 | throw("No valid cache instance file found at $instance_file."); | ||
| 1256 | } | ||
| 1257 | |||
| 1258 | eval { $self->{'instance'} = retrieve($instance_file); }; | ||
| 1259 | if ($@) { | ||
| 1260 | throw("Unable to retrieve cache instance: $@"); | ||
| 1261 | } | ||
| 1262 | |||
| 1263 | return $self->{'instance'}; | ||
| 1264 | } | ||
| 1265 | |||
| 1266 | |||
| 1267 | sub slice_names { | ||
| 1268 | my $self = shift; | ||
| 1269 | my $dbtype = shift; | ||
| 1270 | |||
| 1271 | throw("You must provide a db type (source|target).") unless $dbtype; | ||
| 1272 | |||
| 1273 | my $dba = $self->get_DBAdaptor($dbtype); | ||
| 1274 | my $sa = $dba->get_SliceAdaptor; | ||
| 1275 | |||
| 1276 | my @slice_names = (); | ||
| 1277 | |||
| 1278 | if ( $self->conf->param('chromosomes') ) { | ||
| 1279 | # Fetch the specified chromosomes. | ||
| 1280 | foreach my $chr ( $self->conf->param('chromosomes') ) { | ||
| 1281 | my $slice = $sa->fetch_by_region( 'chromosome', $chr ); | ||
| 1282 | push @slice_names, $slice->name; | ||
| 1283 | } | ||
| 1284 | |||
| 1285 | } | ||
| 1286 | elsif ( $self->conf->param('region') ) { | ||
| 1287 | # Fetch the slices on the specified regions. Don't use | ||
| 1288 | # SliceAdaptor->fetch_by_name() since this will fail if assembly | ||
| 1289 | # versions are different for source and target db. | ||
| 1290 | my ( $cs, $version, $name, $start, $end, $strand ) = | ||
| 1291 | split( /:/, $self->conf->param('region') ); | ||
| 1292 | |||
| 1293 | my $slice = $sa->fetch_by_region( $cs, $name, $start, $end ); | ||
| 1294 | |||
| 1295 | push @slice_names, $slice->name; | ||
| 1296 | |||
| 1297 | } | ||
| 1298 | else { | ||
| 1299 | # Fetch all slices that have genes on them. | ||
| 1300 | my $ga = $dba->get_GeneAdaptor; | ||
| 1301 | my $sa = $dba->get_SliceAdaptor; | ||
| 1302 | |||
| 1303 | foreach my $srid ( @{ $ga->list_seq_region_ids } ) { | ||
| 1304 | my $slice = $sa->fetch_by_seq_region_id($srid); | ||
| 1305 | my $slices = $sa->fetch_by_region_unique( $slice->coord_system_name(), $slice->seq_region_name() ); | ||
| 1306 | |||
| 1307 | push( @slice_names, map { $_->name() } @{$slices} ); | ||
| 1308 | } | ||
| 1309 | } | ||
| 1310 | |||
| 1311 | return \@slice_names; | ||
| 1312 | } ## end sub slice_names | ||
| 1313 | |||
| 1314 | |||
| 1315 | sub logger { | ||
| 1316 | my $self = shift; | ||
| 1317 | $self->{'logger'} = shift if (@_); | ||
| 1318 | return $self->{'logger'}; | ||
| 1319 | } | ||
| 1320 | |||
| 1321 | sub conf { | ||
| 1322 | my $self = shift; | ||
| 1323 | $self->{'conf'} = shift if (@_); | ||
| 1324 | return $self->{'conf'}; | ||
| 1325 | } | ||
| 1326 | |||
| 1327 | |||
| 1328 | sub cache_method { | ||
| 1329 | my $self = shift; | ||
| 1330 | $self->{'instance'}->{'cache_method'} = shift if (@_); | ||
| 1331 | return $self->{'instance'}->{'cache_method'}; | ||
| 1332 | } | ||
| 1333 | |||
| 1334 | |||
| 1335 | sub highest_common_cs { | ||
| 1336 | my $self = shift; | ||
| 1337 | $self->{'instance'}->{'hccs'} = shift if (@_); | ||
| 1338 | return $self->{'instance'}->{'hccs'}; | ||
| 1339 | } | ||
| 1340 | |||
| 1341 | |||
| 1342 | sub highest_common_cs_version { | ||
| 1343 | my $self = shift; | ||
| 1344 | $self->{'instance'}->{'hccsv'} = shift if (@_); | ||
| 1345 | return $self->{'instance'}->{'hccsv'}; | ||
| 1346 | } | ||
| 1347 | |||
| 1348 | |||
| 1349 | sub add_common_cs { | ||
| 1350 | my $self = shift; | ||
| 1351 | my $cs = shift; | ||
| 1352 | |||
| 1353 | unless ($cs and $cs->isa('Bio::EnsEMBL::CoordSystem')) { | ||
| 1354 | throw('You must provide a CoordSystem'); | ||
| 1355 | } | ||
| 1356 | |||
| 1357 | my $csid = join(':', $cs->name, $cs->version); | ||
| 1358 | |||
| 1359 | $self->{'instance'}->{'ccs'}->{$csid} = 1; | ||
| 1360 | } | ||
| 1361 | |||
| 1362 | |||
| 1363 | sub is_common_cs { | ||
| 1364 | my $self = shift; | ||
| 1365 | my $csid = shift; | ||
| 1366 | |||
| 1367 | return $self->{'instance'}->{'ccs'}->{$csid}; | ||
| 1368 | } | ||
| 1369 | |||
| 1370 | |||
| 1371 | 1; | ||
| 1372 |