| 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 | =head1 SYNOPSIS | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | =head1 METHODS | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | package Bio::EnsEMBL::IdMapping::StableIdMapper; | ||
| 42 | $Bio::EnsEMBL::IdMapping::StableIdMapper::VERSION = '114.0.0_50'; # TRIAL | ||
| 43 | $Bio::EnsEMBL::IdMapping::StableIdMapper::VERSION = '114.0.050'; | ||
| 44 | use strict; | ||
| 45 | use warnings; | ||
| 46 | no warnings 'uninitialized'; | ||
| 47 | |||
| 48 | use Bio::EnsEMBL::IdMapping::BaseObject; | ||
| 49 | our @ISA = qw(Bio::EnsEMBL::IdMapping::BaseObject); | ||
| 50 | |||
| 51 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 52 | use Bio::EnsEMBL::Utils::ScriptUtils qw(inject path_append); | ||
| 53 | use Bio::EnsEMBL::IdMapping::ScoredMappingMatrix; | ||
| 54 | use POSIX qw(strftime); | ||
| 55 | |||
| 56 | |||
| 57 | # instance variables | ||
| 58 | my %debug_mappings; | ||
| 59 | |||
| 60 | |||
| 61 | sub new { | ||
| 62 | my $caller = shift; | ||
| 63 | my $class = ref($caller) || $caller; | ||
| 64 | my $self = $class->SUPER::new(@_); | ||
| 65 | |||
| 66 | # inject a StableIdGenerator | ||
| 67 | # | ||
| 68 | # If you write your own generators, make sure they extend | ||
| 69 | # Bio::EnsEMBL::Idmapping::BaseObject and additionally implement these three | ||
| 70 | # methods: initial_stable_id(), increment_stable_id() and calculate_version(). | ||
| 71 | my $stable_id_generator = $self->conf->param('plugin_stable_id_generator') || | ||
| 72 | 'Bio::EnsEMBL::IdMapping::StableIdGenerator::EnsemblGeneric'; | ||
| 73 | $self->logger->debug("Using $stable_id_generator to generate stable Ids.\n"); | ||
| 74 | inject($stable_id_generator); | ||
| 75 | |||
| 76 | # create a new StableIdGenerator object | ||
| 77 | my $generator_instance = $stable_id_generator->new( | ||
| 78 | -LOGGER => $self->logger, | ||
| 79 | -CONF => $self->conf, | ||
| 80 | -CACHE => $self->cache | ||
| 81 | ); | ||
| 82 | $self->stable_id_generator($generator_instance); | ||
| 83 | |||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | sub generate_mapping_session { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | # only run this method once | ||
| 92 | return if ($self->mapping_session_date); | ||
| 93 | |||
| 94 | $self->logger->info("Generating new mapping_session...\n"); | ||
| 95 | |||
| 96 | $self->mapping_session_date(time); | ||
| 97 | $self->mapping_session_date_fmt(strftime("%Y-%m-%d %T", | ||
| 98 | localtime($self->mapping_session_date))); | ||
| 99 | |||
| 100 | my $s_dba = $self->cache->get_DBAdaptor('source'); | ||
| 101 | my $s_dbh = $s_dba->dbc->db_handle; | ||
| 102 | my $t_dba = $self->cache->get_DBAdaptor('target'); | ||
| 103 | my $t_dbh = $t_dba->dbc->db_handle; | ||
| 104 | |||
| 105 | # check if mapping_session_id was manually set by the configuration | ||
| 106 | my $mapping_session_id = $self->conf->param('mapping_session_id'); | ||
| 107 | |||
| 108 | if ($mapping_session_id) { | ||
| 109 | |||
| 110 | $self->logger->debug("Using manually configured mapping_session_id $mapping_session_id\n", 1); | ||
| 111 | |||
| 112 | } else { | ||
| 113 | |||
| 114 | # calculate mapping_session_id from db | ||
| 115 | my $sql = qq(SELECT MAX(mapping_session_id) FROM mapping_session); | ||
| 116 | $mapping_session_id = $self->fetch_value_from_db($s_dbh, $sql); | ||
| 117 | |||
| 118 | unless ($mapping_session_id) { | ||
| 119 | $self->logger->debug("No previous mapping_session found.\n", 1); | ||
| 120 | } | ||
| 121 | |||
| 122 | # increment last mapping_session_id | ||
| 123 | $mapping_session_id++; | ||
| 124 | |||
| 125 | $self->logger->debug("Using mapping_session_id $mapping_session_id\n", 1); | ||
| 126 | } | ||
| 127 | |||
| 128 | $self->mapping_session_id($mapping_session_id); | ||
| 129 | |||
| 130 | # write old mapping_session table to a file | ||
| 131 | my $i; | ||
| 132 | my $fh = $self->get_filehandle('mapping_session.txt', 'tables'); | ||
| 133 | |||
| 134 | my $sth1 = $s_dbh->prepare("SELECT * FROM mapping_session"); | ||
| 135 | $sth1->execute; | ||
| 136 | |||
| 137 | while (my @row = $sth1->fetchrow_array) { | ||
| 138 | $i++; | ||
| 139 | print $fh join("\t", @row); | ||
| 140 | print $fh "\n"; | ||
| 141 | } | ||
| 142 | |||
| 143 | $sth1->finish; | ||
| 144 | |||
| 145 | # append the new mapping_session to the file | ||
| 146 | my $release_sql = qq( | ||
| 147 | SELECT meta_value FROM meta WHERE meta_key = 'schema_version' | ||
| 148 | ); | ||
| 149 | my $old_release = $self->fetch_value_from_db($s_dbh, $release_sql); | ||
| 150 | my $new_release = $self->fetch_value_from_db($t_dbh, $release_sql); | ||
| 151 | |||
| 152 | my $assembly_sql = qq( | ||
| 153 | SELECT meta_value FROM meta WHERE meta_key = 'assembly.default' | ||
| 154 | ); | ||
| 155 | my $old_assembly = $self->fetch_value_from_db($s_dbh, $assembly_sql); | ||
| 156 | my $new_assembly = $self->fetch_value_from_db($t_dbh, $assembly_sql); | ||
| 157 | |||
| 158 | unless ($old_release and $new_release and $old_assembly and $new_assembly) { | ||
| 159 | $self->logger->warning("Not all data for new mapping_session found:\n", 1); | ||
| 160 | $self->logger->info("old_release: $old_release, new_release: $new_release"); | ||
| 161 | $self->logger->info("old_assembly: $old_assembly, new_assembly $new_assembly\n", 2); | ||
| 162 | } | ||
| 163 | |||
| 164 | print $fh join("\t", | ||
| 165 | $mapping_session_id, | ||
| 166 | $self->conf->param('sourcedbname'), | ||
| 167 | $self->conf->param('targetdbname'), | ||
| 168 | $old_release, | ||
| 169 | $new_release, | ||
| 170 | $old_assembly, | ||
| 171 | $new_assembly, | ||
| 172 | $self->mapping_session_date_fmt); | ||
| 173 | |||
| 174 | print $fh "\n"; | ||
| 175 | close($fh); | ||
| 176 | |||
| 177 | $self->logger->info("Done writing ".++$i." mapping_session entries.\n\n"); | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | sub map_stable_ids { | ||
| 182 | my $self = shift; | ||
| 183 | my $mappings = shift; | ||
| 184 | my $type = shift; | ||
| 185 | |||
| 186 | unless ($mappings and | ||
| 187 | $mappings->isa('Bio::EnsEMBL::IdMapping::MappingList')) { | ||
| 188 | throw("Need a Bio::EnsEMBL::IdMapping::MappingList of ${type}s."); | ||
| 189 | } | ||
| 190 | |||
| 191 | # generate a new mapping_session and write all mapping_session data to a file | ||
| 192 | $self->generate_mapping_session; | ||
| 193 | |||
| 194 | $self->logger->info("== Stable ID mapping for $type...\n\n", 0, 'stamped'); | ||
| 195 | |||
| 196 | # check if there are any objects of this type at all | ||
| 197 | my %all_sources = %{ $self->cache->get_by_name("${type}s_by_id", 'source') }; | ||
| 198 | my %all_targets = %{ $self->cache->get_by_name("${type}s_by_id", 'target') }; | ||
| 199 | unless (scalar(keys %all_sources)) { | ||
| 200 | $self->logger->info("No cached ${type}s found.\n\n"); | ||
| 201 | return; | ||
| 202 | } | ||
| 203 | |||
| 204 | my %stats = map { $_ => 0 } | ||
| 205 | qw(mapped new lost); | ||
| 206 | |||
| 207 | # create some lookup hashes from the mappings | ||
| 208 | my %sources_mapped = (); | ||
| 209 | my %targets_mapped = (); | ||
| 210 | my %scores_by_target = (); | ||
| 211 | |||
| 212 | foreach my $e (@{ $mappings->get_all_Entries }) { | ||
| 213 | $sources_mapped{$e->source} = $e->target; | ||
| 214 | $targets_mapped{$e->target} = $e->source; | ||
| 215 | $scores_by_target{$e->target} = $e->score; | ||
| 216 | } | ||
| 217 | |||
| 218 | # determine starting stable ID for new assignments | ||
| 219 | my $new_stable_id = $self->stable_id_generator->initial_stable_id($type); | ||
| 220 | |||
| 221 | # | ||
| 222 | # assign mapped and new stable IDs | ||
| 223 | # | ||
| 224 | foreach my $tid (keys %all_targets) { | ||
| 225 | |||
| 226 | my $t_obj = $all_targets{$tid}; | ||
| 227 | |||
| 228 | # a mapping exists, assign stable ID accordingly | ||
| 229 | if (my $sid = $targets_mapped{$tid}) { | ||
| 230 | |||
| 231 | my $s_obj = $all_sources{$sid}; | ||
| 232 | |||
| 233 | # set target's stable ID and created_date | ||
| 234 | $t_obj->stable_id($s_obj->stable_id); | ||
| 235 | $t_obj->created_date($s_obj->created_date); | ||
| 236 | |||
| 237 | # calculate and set version | ||
| 238 | my $old_version = $s_obj->version(); | ||
| 239 | my $new_version = $self->stable_id_generator->calculate_version($s_obj, $t_obj) ; | ||
| 240 | $t_obj->version($new_version); | ||
| 241 | |||
| 242 | # change modified_date if version changed | ||
| 243 | if ($old_version == $new_version) { | ||
| 244 | $t_obj->modified_date($s_obj->modified_date); | ||
| 245 | } else { | ||
| 246 | $t_obj->modified_date($self->mapping_session_date); | ||
| 247 | # If version changed, score cannot be 1 | ||
| 248 | if ($scores_by_target{$tid} == 1) { | ||
| 249 | $scores_by_target{$tid} = 0.99; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | # create a stable_id_event entry (not for exons) | ||
| 254 | unless ( $type eq 'exon' ) { | ||
| 255 | # Only add events when something changed. | ||
| 256 | if ( !( $s_obj->stable_id eq $t_obj->stable_id && | ||
| 257 | $s_obj->version == $t_obj->version && | ||
| 258 | $scores_by_target{$tid} > 0.9999 ) ) | ||
| 259 | { | ||
| 260 | my $key = join( "\t", | ||
| 261 | $s_obj->stable_id, $s_obj->version, | ||
| 262 | $t_obj->stable_id, $t_obj->version, | ||
| 263 | $self->mapping_session_id, $type, | ||
| 264 | $scores_by_target{$tid} ); | ||
| 265 | $self->add_stable_id_event( 'new', $key ); | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | # add to debug hash | ||
| 270 | push @{ $debug_mappings{$type} }, [ $sid, $tid, $t_obj->stable_id ]; | ||
| 271 | |||
| 272 | # stats | ||
| 273 | $stats{'mapped'}++; | ||
| 274 | |||
| 275 | # no mapping was found, assign a new stable ID | ||
| 276 | } else { | ||
| 277 | |||
| 278 | $t_obj->stable_id($new_stable_id); | ||
| 279 | $t_obj->version(1); | ||
| 280 | $t_obj->created_date($self->mapping_session_date); | ||
| 281 | $t_obj->modified_date($self->mapping_session_date); | ||
| 282 | |||
| 283 | # create a stable_id_event entry (not for exons) | ||
| 284 | unless ($type eq 'exon') { | ||
| 285 | my $key = join("\t", | ||
| 286 | '\N', | ||
| 287 | 0, | ||
| 288 | $t_obj->stable_id, | ||
| 289 | $t_obj->version, | ||
| 290 | $self->mapping_session_id, | ||
| 291 | $type, | ||
| 292 | 0 | ||
| 293 | ); | ||
| 294 | $self->add_stable_id_event('new', $key); | ||
| 295 | } | ||
| 296 | |||
| 297 | # increment the stable Id (to be assigned to the next unmapped object) | ||
| 298 | $new_stable_id = $self->stable_id_generator->increment_stable_id( | ||
| 299 | $new_stable_id); | ||
| 300 | |||
| 301 | # stats | ||
| 302 | $stats{'new'}++; | ||
| 303 | |||
| 304 | } | ||
| 305 | |||
| 306 | } | ||
| 307 | |||
| 308 | # | ||
| 309 | # deletion events for lost sources | ||
| 310 | # | ||
| 311 | my $fh; | ||
| 312 | if ($type eq 'gene' or $type eq 'transcript') { | ||
| 313 | $fh = $self->get_filehandle("${type}s_lost.txt", 'debug'); | ||
| 314 | } | ||
| 315 | |||
| 316 | foreach my $sid (keys %all_sources) { | ||
| 317 | |||
| 318 | my $s_obj = $all_sources{$sid}; | ||
| 319 | |||
| 320 | # no mapping exists, add deletion event | ||
| 321 | unless ($sources_mapped{$sid}) { | ||
| 322 | unless ($type eq 'exon') { | ||
| 323 | my $key = join("\t", | ||
| 324 | $s_obj->stable_id, | ||
| 325 | $s_obj->version, | ||
| 326 | '\N', | ||
| 327 | 0, | ||
| 328 | $self->mapping_session_id, | ||
| 329 | $type, | ||
| 330 | 0 | ||
| 331 | ); | ||
| 332 | $self->add_stable_id_event('new', $key); | ||
| 333 | } | ||
| 334 | |||
| 335 | # stats | ||
| 336 | my $status; | ||
| 337 | $stats{'lost'}++; | ||
| 338 | |||
| 339 | # log lost genes and transcripts (for debug purposes) | ||
| 340 | # | ||
| 341 | # The Java app did this with a separate method | ||
| 342 | # (StableIdMapper.dumpLostGeneAndTranscripts()) which also claims to log | ||
| 343 | # losses due to merge. Since at that point this data isn't available yet | ||
| 344 | # the logging can be done much more efficient here | ||
| 345 | if ($type eq 'gene' or $type eq 'transcript') { | ||
| 346 | print $fh $s_obj->stable_id, "\t$status\n"; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | close($fh) if (defined($fh)); | ||
| 352 | |||
| 353 | # | ||
| 354 | # write stable IDs to file | ||
| 355 | # | ||
| 356 | $self->write_stable_ids_to_file($type, \%all_targets); | ||
| 357 | |||
| 358 | # also generate and write stats to file | ||
| 359 | $self->generate_mapping_stats($type, \%stats); | ||
| 360 | |||
| 361 | $self->logger->info("Done.\n\n"); | ||
| 362 | } | ||
| 363 | |||
| 364 | |||
| 365 | sub generate_similarity_events { | ||
| 366 | my ( $self, $mappings, $scores, $type ) = @_; | ||
| 367 | |||
| 368 | # argument checks | ||
| 369 | unless ( $mappings and | ||
| 370 | $mappings->isa('Bio::EnsEMBL::IdMapping::MappingList') ) | ||
| 371 | { | ||
| 372 | throw('Need a gene Bio::EnsEMBL::IdMapping::MappingList.'); | ||
| 373 | } | ||
| 374 | |||
| 375 | unless ( $scores and | ||
| 376 | $scores->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix') ) | ||
| 377 | { | ||
| 378 | throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 379 | } | ||
| 380 | |||
| 381 | throw("Need a type (gene|transcript|translation).") unless ($type); | ||
| 382 | |||
| 383 | my $mapped; | ||
| 384 | |||
| 385 | foreach my $e ( @{ $mappings->get_all_Entries } ) { | ||
| 386 | |||
| 387 | # create lookup hash for mapped sources and targets; we'll need this | ||
| 388 | # later | ||
| 389 | $mapped->{'source'}->{ $e->source } = 1; | ||
| 390 | $mapped->{'target'}->{ $e->target } = 1; | ||
| 391 | |||
| 392 | } ## end foreach my $e ( @{ $mappings...}) | ||
| 393 | |||
| 394 | # | ||
| 395 | # similarities for other entries | ||
| 396 | # | ||
| 397 | foreach my $dbtype ( keys %$mapped ) { | ||
| 398 | |||
| 399 | # note: $dbtype will be either 'source' or 'target' | ||
| 400 | my $m1 = "get_all_${dbtype}s"; | ||
| 401 | my $m2 = "get_Entries_for_${dbtype}"; | ||
| 402 | |||
| 403 | foreach my $id ( @{ $scores->$m1 } ) { | ||
| 404 | |||
| 405 | # skip if this is a mapped source/target | ||
| 406 | if ( $mapped->{$dbtype}->{$id} ) { next } | ||
| 407 | |||
| 408 | my @entries = | ||
| 409 | sort { $b->score <=> $a->score } @{ $scores->$m2($id) }; | ||
| 410 | |||
| 411 | unless (@entries) { next } | ||
| 412 | |||
| 413 | # skip if top score < 0.75 | ||
| 414 | my $top_score = $entries[0]->score; | ||
| 415 | if ( $top_score < 0.75 ) { next } | ||
| 416 | |||
| 417 | # add similarities for all entries within 5% of top scorer | ||
| 418 | while ( my $e = shift(@entries) ) { | ||
| 419 | |||
| 420 | if ( $mapped->{'source'}->{$e->source} ) { next ; } | ||
| 421 | if ( $mapped->{'target'}->{$e->target} ) { next ; } | ||
| 422 | |||
| 423 | if ( $e->score > ( $top_score*0.95 ) ) { | ||
| 424 | |||
| 425 | my $s_obj = | ||
| 426 | $self->cache->get_by_key( "${type}s_by_id", 'source', | ||
| 427 | $e->source ); | ||
| 428 | my $t_obj = | ||
| 429 | $self->cache->get_by_key( "${type}s_by_id", 'target', | ||
| 430 | $e->target ); | ||
| 431 | |||
| 432 | my $key = join( "\t", | ||
| 433 | $s_obj->stable_id, $s_obj->version, | ||
| 434 | $t_obj->stable_id, $t_obj->version, | ||
| 435 | $self->mapping_session_id, $type, | ||
| 436 | $e->score ); | ||
| 437 | $self->add_stable_id_event( 'similarity', $key ); | ||
| 438 | |||
| 439 | } | ||
| 440 | } | ||
| 441 | |||
| 442 | } ## end foreach my $id ( @{ $scores...}) | ||
| 443 | } ## end foreach my $dbtype ( keys %$mapped) | ||
| 444 | |||
| 445 | } ## end sub generate_similarity_events | ||
| 446 | |||
| 447 | |||
| 448 | sub filter_same_gene_transcript_similarities { | ||
| 449 | my $self = shift; | ||
| 450 | my $transcript_scores = shift; | ||
| 451 | |||
| 452 | # argument checks | ||
| 453 | unless ($transcript_scores and | ||
| 454 | $transcript_scores->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) { | ||
| 455 | throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix of transcripts.'); | ||
| 456 | } | ||
| 457 | |||
| 458 | # create a new matrix for the filtered entries | ||
| 459 | my $filtered_scores = Bio::EnsEMBL::IdMapping::ScoredMappingMatrix->new( | ||
| 460 | -DUMP_PATH => path_append($self->conf->param('basedir'), 'matrix'), | ||
| 461 | -CACHE_FILE => 'filtered_transcript_scores.ser', | ||
| 462 | ); | ||
| 463 | |||
| 464 | # lookup hash for all target transcripts | ||
| 465 | my %all_targets = map { $_->stable_id => 1 } | ||
| 466 | values %{ $self->cache->get_by_name("transcripts_by_id", 'target') }; | ||
| 467 | |||
| 468 | my $i = 0; | ||
| 469 | |||
| 470 | foreach my $e (@{ $transcript_scores->get_all_Entries }) { | ||
| 471 | |||
| 472 | my $s_tr = $self->cache->get_by_key('transcripts_by_id', 'source', | ||
| 473 | $e->source); | ||
| 474 | my $s_gene = $self->cache->get_by_key('genes_by_transcript_id', 'source', | ||
| 475 | $e->source); | ||
| 476 | my $t_gene = $self->cache->get_by_key('genes_by_transcript_id', 'target', | ||
| 477 | $e->target); | ||
| 478 | # workaround for caching issue: only gene objects in 'genes_by_id' cache | ||
| 479 | # have a stable ID assigned | ||
| 480 | #$t_gene = $self->cache->get_by_key('genes_by_id', 'target', $t_gene->id); | ||
| 481 | |||
| 482 | #$self->logger->debug("xxx ".join(":", $s_tr->stable_id, $s_gene->stable_id, | ||
| 483 | # $t_gene->stable_id)."\n"); | ||
| 484 | |||
| 485 | # skip if source and target transcript are in same gene, BUT keep events for | ||
| 486 | # deleted transcripts | ||
| 487 | if (($s_gene->stable_id eq $t_gene->stable_id) and | ||
| 488 | $all_targets{$s_tr->stable_id}) { | ||
| 489 | $i++; | ||
| 490 | next; | ||
| 491 | } | ||
| 492 | |||
| 493 | $filtered_scores->add_Entry($e); | ||
| 494 | } | ||
| 495 | |||
| 496 | $self->logger->debug("Skipped $i same gene transcript mappings.\n"); | ||
| 497 | |||
| 498 | return $filtered_scores; | ||
| 499 | } | ||
| 500 | |||
| 501 | |||
| 502 | sub generate_translation_similarity_events { | ||
| 503 | my $self = shift; | ||
| 504 | my $mappings = shift; | ||
| 505 | my $transcript_scores = shift; | ||
| 506 | |||
| 507 | # argument checks | ||
| 508 | unless ($mappings and | ||
| 509 | $mappings->isa('Bio::EnsEMBL::IdMapping::MappingList')) { | ||
| 510 | throw('Need a gene Bio::EnsEMBL::IdMapping::MappingList.'); | ||
| 511 | } | ||
| 512 | |||
| 513 | unless ($transcript_scores and | ||
| 514 | $transcript_scores->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) { | ||
| 515 | throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 516 | } | ||
| 517 | |||
| 518 | # create a fake translation scoring matrix | ||
| 519 | my $translation_scores = Bio::EnsEMBL::IdMapping::ScoredMappingMatrix->new( | ||
| 520 | -DUMP_PATH => path_append($self->conf->param('basedir'), 'matrix'), | ||
| 521 | -CACHE_FILE => 'translation_scores.ser', | ||
| 522 | ); | ||
| 523 | |||
| 524 | foreach my $e (@{ $transcript_scores->get_all_Entries }) { | ||
| 525 | |||
| 526 | my $s_tl = $self->cache->get_by_key('transcripts_by_id', 'source', | ||
| 527 | $e->source)->translation; | ||
| 528 | my $t_tl = $self->cache->get_by_key('transcripts_by_id', 'target', | ||
| 529 | $e->target)->translation; | ||
| 530 | |||
| 531 | # add an entry to the translation scoring matrix using the score of the | ||
| 532 | # corresponding transcripts | ||
| 533 | if ($s_tl and $t_tl) { | ||
| 534 | $translation_scores->add_score($s_tl->id, $t_tl->id, $e->score); | ||
| 535 | } | ||
| 536 | } | ||
| 537 | |||
| 538 | # now generate similarity events using this fake scoring matrix | ||
| 539 | $self->generate_similarity_events($mappings, $translation_scores, | ||
| 540 | 'translation'); | ||
| 541 | } | ||
| 542 | |||
| 543 | |||
| 544 | sub write_stable_ids_to_file { | ||
| 545 | my $self = shift; | ||
| 546 | my $type = shift; | ||
| 547 | my $all_targets = shift; | ||
| 548 | |||
| 549 | $self->logger->info("Writing ${type} stable IDs to file...\n"); | ||
| 550 | |||
| 551 | my $fh = $self->get_filehandle("${type}_stable_id.txt", 'tables'); | ||
| 552 | |||
| 553 | my @sorted_targets = map { $all_targets->{$_} } sort { $a <=> $b } | ||
| 554 | keys %$all_targets; | ||
| 555 | |||
| 556 | foreach my $obj (@sorted_targets) { | ||
| 557 | |||
| 558 | # check for missing created and modified dates | ||
| 559 | my $created_date = $obj->created_date; | ||
| 560 | unless ($created_date) { | ||
| 561 | #$self->logger->debug("Missing created_date for target ". | ||
| 562 | # $obj->to_string."\n", 1); | ||
| 563 | $created_date = $self->mapping_session_date; | ||
| 564 | } | ||
| 565 | |||
| 566 | my $modified_date = $obj->modified_date; | ||
| 567 | unless ($modified_date) { | ||
| 568 | #$self->logger->debug("Missing modified_date for target ". | ||
| 569 | # $obj->to_string."\n", 1); | ||
| 570 | $modified_date = $self->mapping_session_date; | ||
| 571 | } | ||
| 572 | |||
| 573 | my $row = join("\t", | ||
| 574 | $obj->id, | ||
| 575 | $obj->stable_id, | ||
| 576 | $obj->version, | ||
| 577 | strftime("%Y-%m-%d %T", localtime($created_date)), | ||
| 578 | strftime("%Y-%m-%d %T", localtime($modified_date)), | ||
| 579 | ); | ||
| 580 | |||
| 581 | print $fh "$row\n"; | ||
| 582 | } | ||
| 583 | |||
| 584 | close($fh); | ||
| 585 | |||
| 586 | $self->logger->info("Done writing ".scalar(@sorted_targets)." entries.\n\n"); | ||
| 587 | } | ||
| 588 | |||
| 589 | |||
| 590 | sub generate_mapping_stats { | ||
| 591 | my $self = shift; | ||
| 592 | my $type = shift; | ||
| 593 | my $stats = shift; | ||
| 594 | |||
| 595 | my $result = ucfirst($type)." mapping results:\n\n"; | ||
| 596 | |||
| 597 | my $fmt1 = "%-10s%-10s%-10s%-10s\n"; | ||
| 598 | my $fmt2 = "%-10s%6.0f %6.0f %4.2f%%\n"; | ||
| 599 | |||
| 600 | $result .= sprintf($fmt1, qw(TYPE MAPPED LOST PERCENTAGE)); | ||
| 601 | $result .= ('-'x40)."\n"; | ||
| 602 | |||
| 603 | my $mapped_total = $stats->{'mapped'}; | ||
| 604 | my $lost_total = $stats->{'lost'}; | ||
| 605 | |||
| 606 | $result .= sprintf($fmt2, 'total', $mapped_total, $lost_total, | ||
| 607 | $mapped_total/($mapped_total+$lost_total)*100); | ||
| 608 | |||
| 609 | # log result | ||
| 610 | $self->logger->info($result."\n"); | ||
| 611 | |||
| 612 | # write result to file | ||
| 613 | my $fh = $self->get_filehandle("${type}_mapping_stats.txt", 'stats'); | ||
| 614 | print $fh $result; | ||
| 615 | close($fh); | ||
| 616 | } | ||
| 617 | |||
| 618 | |||
| 619 | sub dump_debug_mappings { | ||
| 620 | my $self = shift; | ||
| 621 | |||
| 622 | foreach my $type (keys %debug_mappings) { | ||
| 623 | |||
| 624 | $self->logger->debug("Writing $type mappings to debug/${type}_mappings.txt...\n"); | ||
| 625 | |||
| 626 | my $fh = $self->get_filehandle("${type}_mappings.txt", 'debug'); | ||
| 627 | |||
| 628 | foreach my $row (@{ $debug_mappings{$type} }) { | ||
| 629 | print $fh join("\t", @$row); | ||
| 630 | print $fh "\n"; | ||
| 631 | } | ||
| 632 | |||
| 633 | close($fh); | ||
| 634 | |||
| 635 | $self->logger->debug("Done.\n"); | ||
| 636 | } | ||
| 637 | } | ||
| 638 | |||
| 639 | |||
| 640 | sub write_stable_id_events { | ||
| 641 | my $self = shift; | ||
| 642 | my $event_type = shift; | ||
| 643 | |||
| 644 | throw("Need an event type (new|similarity).") unless ($event_type); | ||
| 645 | |||
| 646 | $self->logger->debug("Writing $event_type stable_id_events to file...\n"); | ||
| 647 | |||
| 648 | my $fh = $self->get_filehandle("stable_id_event_${event_type}.txt", 'tables'); | ||
| 649 | my $i = 0; | ||
| 650 | |||
| 651 | foreach my $event (@{ $self->get_all_stable_id_events($event_type) }) { | ||
| 652 | print $fh "$event\n"; | ||
| 653 | $i++; | ||
| 654 | } | ||
| 655 | |||
| 656 | close($fh); | ||
| 657 | |||
| 658 | $self->logger->debug("Done writing $i entries.\n"); | ||
| 659 | } | ||
| 660 | |||
| 661 | |||
| 662 | sub add_stable_id_event { | ||
| 663 | my ($self, $type, $event) = @_; | ||
| 664 | |||
| 665 | # argument check | ||
| 666 | throw("Need an event type (new|similarity).") unless ($type); | ||
| 667 | |||
| 668 | $self->{'stable_id_events'}->{$type}->{$event} = 1; | ||
| 669 | } | ||
| 670 | |||
| 671 | |||
| 672 | sub get_all_stable_id_events { | ||
| 673 | my ($self, $type) = @_; | ||
| 674 | |||
| 675 | # argument check | ||
| 676 | throw("Need an event type (new|similarity).") unless ($type); | ||
| 677 | |||
| 678 | return [ keys %{ $self->{'stable_id_events'}->{$type} } ]; | ||
| 679 | } | ||
| 680 | |||
| 681 | |||
| 682 | sub mapping_session_id { | ||
| 683 | my $self = shift; | ||
| 684 | $self->{'_mapping_session_id'} = shift if (@_); | ||
| 685 | return $self->{'_mapping_session_id'}; | ||
| 686 | } | ||
| 687 | |||
| 688 | |||
| 689 | sub mapping_session_date { | ||
| 690 | my $self = shift; | ||
| 691 | $self->{'_mapping_session_date'} = shift if (@_); | ||
| 692 | return $self->{'_mapping_session_date'}; | ||
| 693 | } | ||
| 694 | |||
| 695 | |||
| 696 | sub mapping_session_date_fmt { | ||
| 697 | my $self = shift; | ||
| 698 | $self->{'_mapping_session_date_fmt'} = shift if (@_); | ||
| 699 | return $self->{'_mapping_session_date_fmt'}; | ||
| 700 | } | ||
| 701 | |||
| 702 | |||
| 703 | sub stable_id_generator { | ||
| 704 | my $self = shift; | ||
| 705 | $self->{'_stable_id_generator'} = shift if (@_); | ||
| 706 | return $self->{'_stable_id_generator'}; | ||
| 707 | } | ||
| 708 | |||
| 709 | |||
| 710 | 1; | ||
| 711 |