| 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 | Combines ExonScoreBuilder, ExonDirectMapper and ExonerateRunner from | ||
| 38 | Java application. | ||
| 39 | |||
| 40 | =head1 METHODS | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | package Bio::EnsEMBL::IdMapping::GeneScoreBuilder; | ||
| 45 | $Bio::EnsEMBL::IdMapping::GeneScoreBuilder::VERSION = '114.0.0_50'; # TRIAL | ||
| 46 | $Bio::EnsEMBL::IdMapping::GeneScoreBuilder::VERSION = '114.0.050'; | ||
| 47 | use strict; | ||
| 48 | use warnings; | ||
| 49 | no warnings 'uninitialized'; | ||
| 50 | |||
| 51 | use Bio::EnsEMBL::IdMapping::ScoreBuilder; | ||
| 52 | our @ISA = qw(Bio::EnsEMBL::IdMapping::ScoreBuilder); | ||
| 53 | |||
| 54 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 55 | use Bio::EnsEMBL::Utils::ScriptUtils qw(path_append); | ||
| 56 | use Bio::EnsEMBL::IdMapping::ScoredMappingMatrix; | ||
| 57 | |||
| 58 | |||
| 59 | sub score_genes { | ||
| 60 | my $self = shift; | ||
| 61 | my $transcript_matrix = shift; | ||
| 62 | |||
| 63 | unless ($transcript_matrix and | ||
| 64 | $transcript_matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) { | ||
| 65 | throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 66 | } | ||
| 67 | |||
| 68 | $self->logger->info("-- Scoring genes...\n\n", 0, 'stamped'); | ||
| 69 | |||
| 70 | # build scores based on transcript scores | ||
| 71 | my $matrix = $self->scores_from_transcript_scores($transcript_matrix); | ||
| 72 | |||
| 73 | # debug logging | ||
| 74 | if ($self->logger->loglevel eq 'debug') { | ||
| 75 | $matrix->log('gene', $self->conf->param('basedir')); | ||
| 76 | } | ||
| 77 | |||
| 78 | # log stats of combined matrix | ||
| 79 | my $fmt = "%-40s%10.0f\n"; | ||
| 80 | |||
| 81 | $self->logger->info("Scoring matrix:\n"); | ||
| 82 | |||
| 83 | $self->logger->info(sprintf($fmt, "Total source genes:", | ||
| 84 | $self->cache->get_count_by_name('genes_by_id', 'source')), 1); | ||
| 85 | |||
| 86 | $self->logger->info(sprintf($fmt, "Total target genes:", | ||
| 87 | $self->cache->get_count_by_name('genes_by_id', 'target')), 1); | ||
| 88 | |||
| 89 | $self->log_matrix_stats($matrix); | ||
| 90 | |||
| 91 | $self->logger->info("\nDone with gene scoring.\n\n"); | ||
| 92 | |||
| 93 | return $matrix; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub scores_from_transcript_scores { | ||
| 98 | my $self = shift; | ||
| 99 | my $transcript_matrix = shift; | ||
| 100 | |||
| 101 | unless ($transcript_matrix and | ||
| 102 | $transcript_matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) { | ||
| 103 | throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 104 | } | ||
| 105 | |||
| 106 | my $dump_path = path_append($self->conf->param('basedir'), 'matrix'); | ||
| 107 | |||
| 108 | my $matrix = Bio::EnsEMBL::IdMapping::ScoredMappingMatrix->new( | ||
| 109 | -DUMP_PATH => $dump_path, | ||
| 110 | -CACHE_FILE => 'gene_matrix.ser', | ||
| 111 | ); | ||
| 112 | |||
| 113 | my $gene_cache = $matrix->cache_file; | ||
| 114 | |||
| 115 | if (-s $gene_cache) { | ||
| 116 | |||
| 117 | # read from file | ||
| 118 | $self->logger->info("Reading gene scoring matrix from file...\n", 0, 'stamped'); | ||
| 119 | $self->logger->debug("Cache file $gene_cache.\n", 1); | ||
| 120 | $matrix->read_from_file; | ||
| 121 | $self->logger->info("Done.\n\n", 0, 'stamped'); | ||
| 122 | |||
| 123 | } else { | ||
| 124 | |||
| 125 | # build scoring matrix | ||
| 126 | $self->logger->info("No gene scoring matrix found. Will build new one.\n"); | ||
| 127 | |||
| 128 | $self->logger->info("Gene scoring...\n", 0, 'stamped'); | ||
| 129 | $matrix = $self->build_scores($matrix, $transcript_matrix); | ||
| 130 | $self->logger->info("Done.\n\n", 0, 'stamped'); | ||
| 131 | |||
| 132 | # write scoring matrix to file | ||
| 133 | $matrix->write_to_file; | ||
| 134 | |||
| 135 | } | ||
| 136 | |||
| 137 | return $matrix; | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | sub build_scores { | ||
| 142 | my $self = shift; | ||
| 143 | my $matrix = shift; | ||
| 144 | my $transcript_matrix = shift; | ||
| 145 | |||
| 146 | unless ($matrix and | ||
| 147 | $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) { | ||
| 148 | throw('Need a gene Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 149 | } | ||
| 150 | |||
| 151 | unless ($transcript_matrix and | ||
| 152 | $transcript_matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) { | ||
| 153 | throw('Need a transcript Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 154 | } | ||
| 155 | |||
| 156 | # first find out which source and target genes have scoring transcripts and | ||
| 157 | # build a "flag" matrix for these genes (all scores are 1) | ||
| 158 | $self->flag_matrix_from_transcript_scores($matrix, $transcript_matrix); | ||
| 159 | |||
| 160 | # now calculate the actual scores for the genes in the flag matrix | ||
| 161 | my $final_matrix = $self->score_matrix_from_flag_matrix($matrix, | ||
| 162 | $transcript_matrix); | ||
| 163 | |||
| 164 | return $final_matrix; | ||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | sub flag_matrix_from_transcript_scores { | ||
| 169 | my $self = shift; | ||
| 170 | my $matrix = shift; | ||
| 171 | my $transcript_matrix = shift; | ||
| 172 | |||
| 173 | unless ($matrix and | ||
| 174 | $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) { | ||
| 175 | throw('Need a gene Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 176 | } | ||
| 177 | |||
| 178 | unless ($transcript_matrix and | ||
| 179 | $transcript_matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) { | ||
| 180 | throw('Need a transcript Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 181 | } | ||
| 182 | |||
| 183 | # initialise progress logger | ||
| 184 | my $i; | ||
| 185 | my $num_entries = $transcript_matrix->get_entry_count; | ||
| 186 | my $progress_id = $self->logger->init_progress($num_entries, 100); | ||
| 187 | |||
| 188 | $self->logger->info("Creating flag matrix...\n", 1); | ||
| 189 | |||
| 190 | # for every transcript scoring matrix entry, make an entry in the gene flag | ||
| 191 | # matrix. | ||
| 192 | foreach my $entry (@{ $transcript_matrix->get_all_Entries }) { | ||
| 193 | |||
| 194 | $self->logger->log_progress($progress_id, ++$i, 1); | ||
| 195 | |||
| 196 | my $source_gene = $self->cache->get_by_key('genes_by_transcript_id', | ||
| 197 | 'source', $entry->source); | ||
| 198 | |||
| 199 | my $target_gene = $self->cache->get_by_key('genes_by_transcript_id', | ||
| 200 | 'target', $entry->target); | ||
| 201 | |||
| 202 | $matrix->add_score($source_gene->id, $target_gene->id, 1); | ||
| 203 | } | ||
| 204 | |||
| 205 | $self->logger->info("\n"); | ||
| 206 | |||
| 207 | return $matrix; | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | sub score_matrix_from_flag_matrix { | ||
| 212 | my $self = shift; | ||
| 213 | my $flag_matrix = shift; | ||
| 214 | my $transcript_matrix = shift; | ||
| 215 | my $simple_scoring = shift; | ||
| 216 | |||
| 217 | unless ( $flag_matrix and | ||
| 218 | $flag_matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix') ) | ||
| 219 | { | ||
| 220 | throw('Need a gene Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 221 | } | ||
| 222 | |||
| 223 | unless ( $transcript_matrix | ||
| 224 | and | ||
| 225 | $transcript_matrix->isa( | ||
| 226 | 'Bio::EnsEMBL::IdMapping::ScoredMappingMatrix') | ||
| 227 | ) | ||
| 228 | { | ||
| 229 | throw( | ||
| 230 | 'Need a transcript Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.' | ||
| 231 | ); | ||
| 232 | } | ||
| 233 | |||
| 234 | # create a new scoring matrix which will replace the flag matrix | ||
| 235 | my $matrix = | ||
| 236 | Bio::EnsEMBL::IdMapping::ScoredMappingMatrix->new( | ||
| 237 | -DUMP_PATH => $flag_matrix->dump_path, | ||
| 238 | -CACHE_FILE => $flag_matrix->cache_file_name, | ||
| 239 | ); | ||
| 240 | |||
| 241 | # initialise progress logger | ||
| 242 | my $i; | ||
| 243 | my $num_entries = $flag_matrix->get_entry_count; | ||
| 244 | my $progress_id = $self->logger->init_progress( $num_entries, 100 ); | ||
| 245 | |||
| 246 | $self->logger->info( "Creating score matrix from flag matrix...\n", | ||
| 247 | 1 ); | ||
| 248 | |||
| 249 | my $gene_score_threshold = | ||
| 250 | $self->conf()->param('gene_score_threshold') || 0; | ||
| 251 | |||
| 252 | # loop over flag matrix and do proper scoring for each entry | ||
| 253 | foreach my $entry ( @{ $flag_matrix->get_all_Entries } ) { | ||
| 254 | |||
| 255 | $self->logger->log_progress( $progress_id, ++$i, 1 ); | ||
| 256 | |||
| 257 | my $score = 0; | ||
| 258 | my $source_gene = | ||
| 259 | $self->cache->get_by_key( 'genes_by_id', 'source', | ||
| 260 | $entry->source ); | ||
| 261 | my $target_gene = | ||
| 262 | $self->cache->get_by_key( 'genes_by_id', 'target', | ||
| 263 | $entry->target ); | ||
| 264 | |||
| 265 | if ($simple_scoring) { | ||
| 266 | |||
| 267 | # simple scoring (used for rescoring purposes) | ||
| 268 | $score = | ||
| 269 | $self->simple_gene_gene_score( $source_gene, $target_gene, | ||
| 270 | $transcript_matrix ); | ||
| 271 | |||
| 272 | } | ||
| 273 | else { | ||
| 274 | |||
| 275 | # full scoring | ||
| 276 | $score = | ||
| 277 | $self->complex_gene_gene_score( $source_gene, $target_gene, | ||
| 278 | $transcript_matrix ); | ||
| 279 | } | ||
| 280 | |||
| 281 | if ( $score > $gene_score_threshold ) { | ||
| 282 | $matrix->add_score( $entry->source, $entry->target, $score ); | ||
| 283 | } | ||
| 284 | } ## end foreach my $entry ( @{ $flag_matrix...}) | ||
| 285 | |||
| 286 | $self->logger->info("\n"); | ||
| 287 | |||
| 288 | return $matrix; | ||
| 289 | } ## end sub score_matrix_from_flag_matrix | ||
| 290 | |||
| 291 | |||
| 292 | sub complex_gene_gene_score { | ||
| 293 | my $self = shift; | ||
| 294 | my $source_gene = shift; | ||
| 295 | my $target_gene = shift; | ||
| 296 | my $transcript_matrix = shift; | ||
| 297 | |||
| 298 | my $source_gene_score = 0; | ||
| 299 | my $target_gene_score = 0; | ||
| 300 | my $source_gene_accum_length = 0; # sum of all transcript lengths | ||
| 301 | my $target_gene_accum_length = 0; # sum of all transcript lengths | ||
| 302 | |||
| 303 | # We are only interested in scoring with transcripts that are in the target | ||
| 304 | # gene. The scored mapping matrix may contain scores for transcripts that | ||
| 305 | # aren't in this gene so create a hash of the target genes's transcripts | ||
| 306 | my %target_transcripts = map { $_->id => 1 } | ||
| 307 | @{ $target_gene->get_all_Transcripts }; | ||
| 308 | |||
| 309 | # loop over source transcripts | ||
| 310 | foreach my $source_transcript (@{ $source_gene->get_all_Transcripts }) { | ||
| 311 | |||
| 312 | # now loop over target transcripts and find the highest scoring target | ||
| 313 | # transcript belonging to the target gene | ||
| 314 | my $max_source_score = -1; | ||
| 315 | |||
| 316 | foreach my $target_transcript_id (@{ $transcript_matrix->get_targets_for_source($source_transcript->id) }) { | ||
| 317 | |||
| 318 | next unless ($target_transcripts{$target_transcript_id}); | ||
| 319 | |||
| 320 | my $score = $transcript_matrix->get_score( | ||
| 321 | $source_transcript->id, $target_transcript_id); | ||
| 322 | $max_source_score = $score if ($score > $max_source_score); | ||
| 323 | } | ||
| 324 | |||
| 325 | if ($max_source_score > 0) { | ||
| 326 | $source_gene_score += ($max_source_score * $source_transcript->length); | ||
| 327 | } | ||
| 328 | |||
| 329 | $source_gene_accum_length += $source_transcript->length; | ||
| 330 | } | ||
| 331 | |||
| 332 | # now do the same for target genes | ||
| 333 | my %source_transcripts = map { $_->id => 1 } | ||
| 334 | @{ $source_gene->get_all_Transcripts }; | ||
| 335 | |||
| 336 | # loop over target transcripts | ||
| 337 | foreach my $target_transcript (@{ $target_gene->get_all_Transcripts }) { | ||
| 338 | |||
| 339 | # now loop over source transcripts and find the highest scoring source | ||
| 340 | # transcript belonging to the source gene | ||
| 341 | my $max_target_score = -1; | ||
| 342 | |||
| 343 | foreach my $source_transcript_id (@{ $transcript_matrix->get_sources_for_target($target_transcript->id) }) { | ||
| 344 | |||
| 345 | next unless ($source_transcripts{$source_transcript_id}); | ||
| 346 | |||
| 347 | my $score = $transcript_matrix->get_score( | ||
| 348 | $source_transcript_id, $target_transcript->id); | ||
| 349 | $max_target_score = $score if ($score > $max_target_score); | ||
| 350 | } | ||
| 351 | |||
| 352 | if ($max_target_score > 0) { | ||
| 353 | $target_gene_score += ($max_target_score * $target_transcript->length); | ||
| 354 | } | ||
| 355 | |||
| 356 | $target_gene_accum_length += $target_transcript->length; | ||
| 357 | } | ||
| 358 | |||
| 359 | # calculate overall score for this gene | ||
| 360 | my $gene_score = 0; | ||
| 361 | |||
| 362 | if (($source_gene_accum_length + $target_gene_accum_length) > 0) { | ||
| 363 | |||
| 364 | $gene_score = ($source_gene_score + $target_gene_score) / | ||
| 365 | ($source_gene_accum_length + $target_gene_accum_length); | ||
| 366 | |||
| 367 | } else { | ||
| 368 | |||
| 369 | $self->logger->warning("Combined transcript length of source (".$source_gene->id.") and target (".$target_gene->id.") gene is zero!\n", 1); | ||
| 370 | |||
| 371 | } | ||
| 372 | |||
| 373 | if ($gene_score > 1) { | ||
| 374 | $self->logger->warning("Illegal gene score: $gene_score (". | ||
| 375 | join("|", $source_gene_score, $target_gene_score, | ||
| 376 | $source_gene_accum_length, $target_gene_accum_length).")\n", 1); | ||
| 377 | } | ||
| 378 | |||
| 379 | return $gene_score; | ||
| 380 | } | ||
| 381 | |||
| 382 | |||
| 383 | # | ||
| 384 | # Simplified scoring for genes. Score is best scoring transcript pair. | ||
| 385 | # This is used when the more elaborate gene representing score does not | ||
| 386 | # distinguish very well. | ||
| 387 | # | ||
| 388 | sub simple_gene_gene_score { | ||
| 389 | my $self = shift; | ||
| 390 | my $source_gene = shift; | ||
| 391 | my $target_gene = shift; | ||
| 392 | my $transcript_matrix = shift; | ||
| 393 | |||
| 394 | my $gene_score = 0; | ||
| 395 | |||
| 396 | foreach my $source_transcript (@{ $source_gene->get_all_Transcripts }) { | ||
| 397 | foreach my $target_transcript (@{ $target_gene->get_all_Transcripts }) { | ||
| 398 | |||
| 399 | my $score = $transcript_matrix->get_score($source_transcript->id, | ||
| 400 | $target_transcript->id); | ||
| 401 | |||
| 402 | $gene_score = $score if ($score > $gene_score); | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | return $gene_score; | ||
| 407 | } | ||
| 408 | |||
| 409 | |||
| 410 | sub simple_gene_rescore { | ||
| 411 | my $self = shift; | ||
| 412 | my $gene_matrix = shift; | ||
| 413 | my $transcript_matrix = shift; | ||
| 414 | |||
| 415 | $gene_matrix = $self->score_matrix_from_flag_matrix($gene_matrix, | ||
| 416 | $transcript_matrix, 1); | ||
| 417 | } | ||
| 418 | |||
| 419 | # | ||
| 420 | # penalise scores between genes with different biotypes. | ||
| 421 | # entries are modified in place | ||
| 422 | # | ||
| 423 | sub biotype_gene_rescore { | ||
| 424 | my $self = shift; | ||
| 425 | my $matrix = shift; | ||
| 426 | |||
| 427 | unless ($matrix | ||
| 428 | and $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix') ) | ||
| 429 | { | ||
| 430 | throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 431 | } | ||
| 432 | |||
| 433 | my $i = 0; | ||
| 434 | |||
| 435 | foreach my $entry ( @{ $matrix->get_all_Entries } ) { | ||
| 436 | |||
| 437 | my $source_gene = | ||
| 438 | $self->cache->get_by_key( 'genes_by_id', 'source', | ||
| 439 | $entry->source ); | ||
| 440 | |||
| 441 | my $target_gene = | ||
| 442 | $self->cache->get_by_key( 'genes_by_id', 'target', | ||
| 443 | $entry->target ); | ||
| 444 | |||
| 445 | if ( $source_gene->biotype() ne $target_gene->biotype() ) { | ||
| 446 | # PENALTY: Lower the score for mappings that differ in biotype. | ||
| 447 | $matrix->set_score( $entry->source(), $entry->target(), | ||
| 448 | 0.9*$entry->score() ); | ||
| 449 | $i++; | ||
| 450 | } | ||
| 451 | } | ||
| 452 | |||
| 453 | $self->logger->debug( "Scored genes with biotype mismatch: $i\n", 1 ); | ||
| 454 | } ## end sub biotype_gene_rescore | ||
| 455 | |||
| 456 | |||
| 457 | sub location_gene_rescore { | ||
| 458 | my $self = shift; | ||
| 459 | my $matrix = shift; | ||
| 460 | |||
| 461 | unless ($matrix | ||
| 462 | and $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix') ) | ||
| 463 | { | ||
| 464 | throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 465 | } | ||
| 466 | |||
| 467 | my $i = 0; | ||
| 468 | |||
| 469 | foreach my $entry ( @{ $matrix->get_all_Entries } ) { | ||
| 470 | |||
| 471 | my $source_gene = | ||
| 472 | $self->cache->get_by_key( 'genes_by_id', 'source', | ||
| 473 | $entry->source ); | ||
| 474 | |||
| 475 | my $target_gene = | ||
| 476 | $self->cache->get_by_key( 'genes_by_id', 'target', | ||
| 477 | $entry->target ); | ||
| 478 | |||
| 479 | if ( $source_gene->seq_region_name() ne $target_gene->seq_region_name() ) { | ||
| 480 | # PENALTY: Lower the score for mappings that are not on the same slice. | ||
| 481 | $matrix->set_score( $entry->source(), $entry->target(), | ||
| 482 | 0.9*$entry->score() ); | ||
| 483 | $i++; | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | } | ||
| 488 | |||
| 489 | |||
| 490 | 1; | ||
| 491 |