EnsEMBL/IdMapping/ExonScoreBuilder.pm untested

filter: Statementstmt 0.0% 0 / 341 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 82 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 553 CC 49 SCAR 41.3
CC
49
Cov
0%
build_overlap_scores
182.0
run_exonerate
72.0
write_filtered_exons
56.0
CRAP 62.3
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::ExonScoreBuilder;
45 $Bio::EnsEMBL::IdMapping::ExonScoreBuilder::VERSION = '114.0.0_50'; # TRIAL
46 $Bio::EnsEMBL::IdMapping::ExonScoreBuilder::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::Argument qw(rearrange);
55 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
56 use Bio::EnsEMBL::Utils::ScriptUtils qw(parse_bytes path_append);
57 use Bio::EnsEMBL::IdMapping::ScoredMappingMatrix;
58  
59  
60 #
61 # exon scoring is done in two steps:
62 # 1. map exons by overlap (if a common coord_system exists)
63 # 2. map remaining and poorly scoring exons using exonerate
64 #
65 sub score_exons {
66 my $self = shift;
67  
68 $self->logger->info( "-- Scoring exons...\n\n", 0, 'stamped' );
69  
70 # score using overlaps, then exonerate
71 my $matrix = $self->overlap_score;
72  
73 my $exonerate_matrix = $self->exonerate_score($matrix);
74  
75 # log stats before matrix merging
76 $self->logger->info("\nOverlap scoring matrix:\n");
77 $self->log_matrix_stats($matrix);
78 $self->logger->info("\nExonerate scoring matrix:\n");
79 $self->log_matrix_stats($exonerate_matrix);
80  
81 # merge matrices
82 $self->logger->info( "\nMerging scoring matrices...\n", 0,
83 'stamped' );
84 $matrix->merge($exonerate_matrix);
85  
86 $self->logger->info( "Done.\n\n", 0, 'stamped' );
87  
88 # debug logging
89 if ( $self->logger->loglevel eq 'debug' ) {
90 $matrix->log( 'exon', $self->conf->param('basedir') );
91 }
92  
93 # log stats of combined matrix
94 $self->logger->info("Combined scoring matrix:\n");
95 $self->log_matrix_stats($matrix);
96  
97 $self->logger->info( "\nDone with exon scoring.\n\n", 0, 'stamped' );
98  
99 return $matrix;
100 } ## end sub score_exons
101  
102  
103 #
104 # direct mapping by overlap (if common coord_system exists)
105 #
106 sub overlap_score {
107 my $self = shift;
108  
109 my $dump_path =
110 path_append( $self->conf->param('basedir'), 'matrix' );
111  
112 my $matrix =
113 Bio::EnsEMBL::IdMapping::ScoredMappingMatrix->new(
114 -DUMP_PATH => $dump_path,
115 -CACHE_FILE => 'exon_overlap_matrix.ser',
116 );
117  
118 my $overlap_cache = $matrix->cache_file;
119  
120 if ( -s $overlap_cache ) {
121  
122 # read from file
123 $self->logger->info(
124 "Reading exon overlap scoring matrix from file...\n",
125 0, 'stamped' );
126 $self->logger->debug( "Cache file $overlap_cache.\n", 1 );
127 $matrix->read_from_file;
128 $self->logger->info( "Done.\n", 0, 'stamped' );
129  
130 }
131 else {
132  
133 # build scoring matrix
134 $self->logger->info(
135 "No exon overlap scoring matrix found. Will build new one.\n");
136  
137 if ( $self->cache->highest_common_cs ) {
138 $self->logger->info( "Overlap scoring...\n", 0, 'stamped' );
139 $matrix = $self->build_overlap_scores($matrix);
140 $self->logger->info( "Done.\n", 0, 'stamped' );
141 }
142  
143 # write scoring matrix to file
144 $matrix->write_to_file;
145  
146 }
147  
148 return $matrix;
149 } ## end sub overlap_score
150  
151  
152 #
153 # map the remaining exons using exonerate
154 #
155 sub exonerate_score {
156 my $self = shift;
157 my $matrix = shift;
158  
159 unless ( $matrix and
160 $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix') )
161 {
162 throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.');
163 }
164  
165 my $dump_path =
166 path_append( $self->conf->param('basedir'), 'matrix' );
167  
168 my $exonerate_matrix =
169 Bio::EnsEMBL::IdMapping::ScoredMappingMatrix->new(
170 -DUMP_PATH => $dump_path,
171 -CACHE_FILE => 'exon_exonerate_matrix.ser',
172 );
173  
174 my $exonerate_cache = $exonerate_matrix->cache_file;
175  
176 if ( -s $exonerate_cache ) {
177  
178 # read from file
179 $self->logger->info( "Reading exonerate matrix from file...\n",
180 0, 'stamped' );
181 $self->logger->debug( "Cache file $exonerate_cache.\n", 1 );
182 $exonerate_matrix->read_from_file;
183 $self->logger->info( "Done.\n", 0, 'stamped' );
184  
185 }
186 else {
187  
188 # build scoring matrix
189 $self->logger->info(
190 "No exonerate matrix found. Will build new one.\n");
191  
192 # dump exons to fasta files
193 my $dump_count = $self->dump_filtered_exons($matrix);
194  
195 if ($dump_count) {
196 # run exonerate
197 $self->run_exonerate;
198  
199 # parse results
200 $self->parse_exonerate_results($exonerate_matrix);
201  
202 }
203 else {
204  
205 $self->logger->info( "No source and/or target exons dumped, " .
206 "so don't need to run exonerate.\n" );
207  
208 }
209  
210 # write scoring matrix to file
211 $exonerate_matrix->write_to_file;
212  
213 } ## end else [ if ( -s $exonerate_cache)]
214  
215 return $exonerate_matrix;
216 } ## end sub exonerate_score
217  
218 #
219 # Algorithm:
220 # Get a lists of exon containers for source and target. Walk along both
221 # lists, set a flag when you first encounter an exon (i.e. it starts).
222 # Record all alternative exons until you encounter the exon again (exon
223 # ends), then score against all alternative exons you've recorded.
224 #
225 sub build_overlap_scores {
226 my ( $self, $matrix ) = @_;
227  
228 unless ($matrix
229 and $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix') )
230 {
231 throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.');
232 }
233  
234 # get sorted list of exon containers
235 $self->logger->info( "Reading sorted exons from cache...\n",
236 1, 'stamped' );
237  
238 my @source_exons =
239 $self->sort_exons( [
240 values %{ $self->cache->get_by_name( 'exons_by_id', 'source' ) }
241 ] );
242  
243 my @target_exons =
244 $self->sort_exons( [
245 values %{ $self->cache->get_by_name( 'exons_by_id', 'target' ) }
246 ] );
247  
248 $self->logger->info( "Done.\n", 1, 'stamped' );
249  
250 # get first source and target exon container
251 my $source_ec = shift(@source_exons);
252 my $target_ec = shift(@target_exons);
253  
254 my %source_overlap = ();
255 my %target_overlap = ();
256  
257 $self->logger->info( "Scoring...\n", 1, 'stamped' );
258  
259 while ( defined($source_ec) || defined($target_ec) ) {
260 my $add_source = 0;
261 my $add_target = 0;
262  
263 # compare exon containers
264 if ( defined($source_ec) && defined($target_ec) ) {
265 my $cmp =
266 $self->compare_exon_containers( $source_ec, $target_ec );
267 if ( $cmp <= 0 ) { $add_source = 1 }
268 if ( $cmp >= 0 ) { $add_target = 1 }
269 }
270 elsif ( defined($source_ec) ) {
271 $add_source = 1;
272 }
273 elsif ( defined($target_ec) ) {
274 $add_target = 1;
275 }
276 else {
277 die('The world is a strange place');
278 }
279  
280 if ($add_source) {
281 if ( exists( $source_overlap{ $source_ec->[0] } ) ) {
282 # remove exon from list of overlapping source exons to score
283 # target against
284 delete $source_overlap{ $source_ec->[0] };
285 }
286 else {
287 # add exon to list of overlapping source exons to score target
288 # against
289 $source_overlap{ $source_ec->[0] } = $source_ec->[0];
290  
291 # score source exon against all target exons in current overlap
292 # list
293 foreach my $target_exon ( values %target_overlap ) {
294 if ( defined( $matrix->get_score(
295 $source_ec->[0]->id, $target_exon->id
296 ) ) )
297 {
298 next;
299 }
300  
301 $self->calc_overlap_score( $source_ec->[0], $target_exon,
302 $matrix );
303 }
304 }
305  
306 # get next source exon container
307 $source_ec = shift(@source_exons);
308 } ## end if ($add_source)
309  
310 if ($add_target) {
311 if ( exists( $target_overlap{ $target_ec->[0] } ) ) {
312 # remove exon from list of overlapping target exons to score
313 # source against
314 delete $target_overlap{ $target_ec->[0] };
315 }
316 else {
317 # add exon to list of overlapping target exons to score source
318 # against
319 $target_overlap{ $target_ec->[0] } = $target_ec->[0];
320  
321 # score target exon against all source exons in current overlap
322 # list
323 foreach my $source_exon ( values %source_overlap ) {
324 if ( defined( $matrix->get_score(
325 $source_exon->id, $target_ec->[0]->id
326 ) ) )
327 {
328 next;
329 }
330  
331 $self->calc_overlap_score( $source_exon, $target_ec->[0],
332 $matrix );
333 }
334 }
335  
336 # get next target exon container
337 $target_ec = shift(@target_exons);
338 } ## end if ($add_target)
339 } ## end while ( defined($source_ec...))
340  
341 $self->logger->info( "Done.\n", 1, 'stamped' );
342  
343 return $matrix;
344 } ## end sub build_overlap_scores
345  
346  
347 #
348 # Return a list of exon containers, sorted by seq_region_name, then location
349 # (where location is either start-1 or end, so each exon is in the list twice).
350 # An exon container is a listrefs of a TinyExon object and its location. This
351 # implements the ExonSortContainer in the java application.
352 #
353 sub sort_exons {
354 my $self = shift;
355 my $exons = shift;
356  
357 ## no critic
358 return sort {
359 ( $a->[0]->common_sr_name cmp $b->[0]->common_sr_name ) ||
360 ( $a->[1] <=> $b->[1] )
361 } ( map { [ $_, $_->common_start - 1 ] } @$exons ),
362 ( map { [ $_, $_->common_end ] } @$exons );
363 }
364  
365 sub compare_exon_containers {
366 my $self = shift;
367 my $e1 = shift;
368 my $e2 = shift;
369  
370 return ( ( $e1->[0]->common_sr_name cmp $e2->[0]->common_sr_name ) ||
371 ( $e1->[1] <=> $e2->[1] ) );
372 }
373  
374 #
375 # Calculates overlap score between two exons. Its done by dividing
376 # overlap region by exons sizes. 1.0 is full overlap on both exons.
377 # Score of at least 0.5 are added to the exon scoring matrix.
378 #
379 sub calc_overlap_score {
380 my $self = shift;
381 my $source_exon = shift;
382 my $target_exon = shift;
383 my $matrix = shift;
384  
385 my ( $start, $end );
386  
387 # don't score if exons on different strand
388 return unless ( $source_exon->strand == $target_exon->strand );
389  
390 # determine overlap start
391 if ( $source_exon->start > $target_exon->start ) {
392 $start = $source_exon->start;
393 }
394 else {
395 $start = $target_exon->start;
396 }
397  
398 # determine overlap end
399 if ( $source_exon->end < $target_exon->end ) {
400 $end = $source_exon->end;
401 }
402 else {
403 $end = $target_exon->end;
404 }
405  
406 #
407 # Calculate score, which is defined as average overlap / exon length
408 # ratio.
409 #
410  
411 my $overlap = $end - $start + 1;
412 my $source_length = $source_exon->end - $source_exon->start + 1;
413 my $target_length = $target_exon->end - $target_exon->start + 1;
414  
415 my $score = ( $overlap/$source_length + $overlap/$target_length )/2;
416  
417 # The following penalty was removed because it meant that an exon
418 # whose sequence and position had not changed would become a
419 # candidate for similarity mapping when its phase changed. This
420 # caused lots of criss-crossing stable ID history entries between
421 # genes/transcripts/translations in some gene families.
422 #
423 ## PENALTY:
424 ## penalise by 10% if phase if different
425 if ( $source_exon->phase != $target_exon->phase ) {
426 $score *= 0.9;
427 }
428  
429 # add score to scoring matrix if it's at least 0.5
430 if ( $score >= 0.5 ) {
431 $matrix->add_score( $source_exon->id, $target_exon->id, $score );
432 }
433  
434 } ## end sub calc_overlap_score
435  
436  
437 sub run_exonerate {
438 my $self = shift;
439  
440 my $source_file = $self->exon_fasta_file('source');
441 my $target_file = $self->exon_fasta_file('target');
442 my $source_size = -s $source_file;
443 my $target_size = -s $target_file;
444  
445 # check if fasta files exist and are not empty
446 unless ($source_size and $target_size) {
447 throw("Can't find exon fasta files.");
448 }
449  
450 # create an empty lsf log directory
451 my $logpath = path_append($self->logger->logpath, 'exonerate');
452 system("rm -rf $logpath") == 0 or
453 $self->logger->error("Unable to delete lsf log dir $logpath: $!\n");
454 system("mkdir -p $logpath") == 0 or
455 $self->logger->error("Can't create lsf log dir $logpath: $!\n");
456  
457 # delete exonerate output from previous runs
458 my $dump_path = $self->cache->dump_path;
459  
460 opendir(my $dumpdir, $dump_path) or
461 $self->logger->error("Can't open $dump_path for reading: $!");
462  
463 while (defined(my $file = readdir($dumpdir))) {
464 next unless /exonerate_map\.\d+/;
465  
466 unlink("$dump_path/$file") or
467 $self->logger->error("Can't delete $dump_path/$file: $!");
468 }
469
470 closedir($dumpdir);
471  
472 # determine number of jobs to split task into
473 my $bytes_per_job = $self->conf->param('exonerate_bytes_per_job')
474 || 250000;
475 my $num_jobs = $self->conf->param('exonerate_jobs');
476 $num_jobs ||= int( $source_size/$bytes_per_job + 1 );
477  
478 my $percent =
479 int( ( $self->conf->param('exonerate_threshold') || 0.5 )*100 );
480 my $lsf_name = 'idmapping_exonerate_' . time;
481 my $exonerate_path = $self->conf->param('exonerate_path');
482 my $exonerate_extra_params =
483 $self->conf->param('exonerate_extra_params');
484  
485 #
486 # run exonerate jobs using lsf
487 #
488 my $exonerate_job =
489 qq{$exonerate_path } .
490 qq{--query $source_file --target $target_file } .
491 q{--querychunkid $LSB_JOBINDEX } .
492 qq{--querychunktotal $num_jobs } .
493 q{--model ungapped -M 1000 -D 100 } .
494 q{--showalignment FALSE --subopt no } . qq{--percent $percent } .
495 $self->conf->param('exonerate_extra_params') . " " .
496 q{--ryo 'myinfo: %qi %ti %et %ql %tl\n' } .
497 qq{| grep '^myinfo:' > $dump_path/exonerate_map.\$LSB_JOBINDEX} .
498 "\n";
499  
500 $self->logger->info("Submitting $num_jobs exonerate jobs to lsf.\n");
501 $self->logger->debug("$exonerate_job\n\n");
502  
503 my $bsub_cmd = sprintf(
504 "|bsub -J '%s[1-%d]%%%d' -o %s/exonerate.%%I.out %s",
505 $lsf_name,
506 $num_jobs,
507 $self->conf()->param('exonerate_concurrent_jobs') || 200,
508 $logpath,
509 $self->conf()->param('lsf_opt_exonerate') );
510  
511 local *BSUB;
512 open( BSUB, $bsub_cmd ) ## no critic
513 or $self->logger->error("Could not open open pipe to bsub: $!\n");
514  
515 print BSUB $exonerate_job;
516 $self->logger->error("Error submitting exonerate jobs: $!\n")
517 unless ($? == 0);
518 close BSUB;
519  
520 # submit dependent job to monitor finishing of exonerate jobs
521 $self->logger->info("Waiting for exonerate jobs to finish...\n", 0, 'stamped');
522  
523 my $dependent_job =
524 qq{bsub -K -w "ended($lsf_name)" -q production } .
525 qq{-M 1000 -R 'select[mem>1000]' -R 'rusage[mem=1000]' } .
526 qq{-o $logpath/exonerate_depend.out /bin/true};
527  
528 system($dependent_job) == 0 or
529 $self->logger->error("Error submitting dependent job: $!\n");
530  
531 $self->logger->info("All exonerate jobs finished.\n", 0, 'stamped');
532  
533 #
534 # check results
535 #
536 my @missing;
537 my @error;
538
539 for (my $i = 1; $i <= $num_jobs; $i++) {
540
541 # check that output file exists
542 my $outfile = "$dump_path/exonerate_map.$i";
543 push @missing, $outfile unless (-f "$outfile");
544  
545 # check no errors occurred
546 my $errfile = "$logpath/exonerate.$i.err";
547 push @error, $errfile if (-s "$errfile");
548 }
549  
550 if (@missing) {
551 $self->logger->info("Couldn't find all exonerate output files. These are missing:\n");
552 foreach (@missing) {
553 $self->logger->info("$_\n", 1);
554 }
555  
556 exit(1);
557 }
558  
559 if (@error) {
560 $self->logger->info("One or more exonerate jobs failed. Check these error files:\n");
561 foreach (@error) {
562 $self->logger->info("$_\n", 1);
563 }
564  
565 exit(1);
566 }
567  
568 }
569  
570  
571 sub exon_fasta_file {
572 my $self = shift;
573 my $type = shift;
574  
575 throw("You must provide a type.") unless $type;
576  
577 return $self->cache->dump_path."/$type.exons.fasta";
578 }
579  
580  
581 sub dump_filtered_exons {
582 my $self = shift;
583 my $matrix = shift;
584  
585 unless ($matrix and
586 $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) {
587 throw('You must provide a ScoredMappingMatrix.');
588 }
589  
590 # write exons to fasta files
591 my $source_count = $self->write_filtered_exons('source', $matrix);
592 my $target_count = $self->write_filtered_exons('target', $matrix);
593  
594 # return true if both source and target exons were written; otherwise we
595 # don't need to run exonerate
596 return (($source_count > 0) and ($target_count > 0));
597 }
598  
599  
600 sub write_filtered_exons {
601 my $self = shift;
602 my $type = shift;
603 my $matrix = shift;
604  
605 throw("You must provide a type.") unless $type;
606 unless ($matrix and
607 $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) {
608 throw('You must provide a ScoredMappingMatrix.');
609 }
610  
611 $self->logger->info("\nDumping $type exons to fasta file...\n", 0, 'stamped');
612  
613 # don't dump exons shorter than this
614 my $min_exon_length = $self->conf->param('min_exon_length') || 15;
615  
616 # counters
617 my $total_exons = 0;
618 my $dumped_exons = 0;
619  
620 # filehandle for fasta files
621 my $fh;
622 my $file = $self->exon_fasta_file($type);
623 open($fh, '>', $file) or throw("Unable to open $file for writing: $!");
624  
625 # loop over exons, dump sequence to fasta file if longer than threshold and
626 # score < 1
627 EXON:
628 foreach my $eid (sort { $b <=> $a }
629 keys %{ $self->cache->get_by_name('exons_by_id', $type) }) {
630  
631 my $exon = $self->cache->get_by_key('exons_by_id', $type, $eid);
632  
633 $total_exons++;
634  
635 # skip if exon shorter than threshold
636 next EXON if ($exon->length < $min_exon_length);
637  
638 # skip if overlap score with any other exon is 1
639 if ( $type eq 'source' ) {
640 foreach my $target ( @{ $matrix->get_targets_for_source($eid) } )
641 {
642 if ( $matrix->get_score( $eid, $target ) > 0.9999 ) {
643 next EXON;
644 }
645 }
646 } else {
647 foreach my $source ( @{ $matrix->get_sources_for_target($eid) } )
648 {
649 if ( $matrix->get_score( $source, $eid ) > 0.9999 ) {
650 next EXON;
651 }
652 }
653 }
654  
655 # write exon to fasta file
656 print $fh '>', $eid, "\n", $exon->seq, "\n";
657  
658 $dumped_exons++;
659  
660 }
661  
662 close($fh);
663  
664 # log
665 my $fmt = "%-30s%10s\n";
666 my $size = -s $file;
667 $self->logger->info(sprintf($fmt, 'Total exons:', $total_exons), 1);
668 $self->logger->info(sprintf($fmt, 'Dumped exons:', $dumped_exons), 1);
669 $self->logger->info(sprintf($fmt, 'Dump file size:', parse_bytes($size)), 1);
670 $self->logger->info("Done.\n\n", 0, 'stamped');
671  
672 return $dumped_exons;
673 }
674  
675 sub parse_exonerate_results {
676 my ( $self, $exonerate_matrix ) = @_;
677  
678 unless ( defined($exonerate_matrix)
679 &&
680 $exonerate_matrix->isa(
681 'Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')
682 )
683 {
684 throw('You must provide a ScoredMappingMatrix.');
685 }
686  
687 $self->logger->info( "Parsing exonerate results...\n", 0, 'stamped' );
688  
689 # loop over all result files
690 my $dump_path = $self->cache->dump_path;
691 my $num_files = 0;
692 my $num_lines = 0;
693  
694 opendir( my $dumpdir, $dump_path ) or
695 $self->logger->error("Can't open $dump_path for reading: $!");
696  
697 my $penalised = 0;
698 my $killed = 0;
699  
700 while ( defined( my $file = readdir($dumpdir) ) ) {
701 unless ( $file =~ /exonerate_map\.\d+/ ) { next }
702  
703 $num_files++;
704  
705 open( my $fh, '<', "$dump_path/$file" );
706  
707 my $threshold = $self->conf->param('exonerate_threshold') || 0.5;
708  
709 while (<$fh>) {
710 $num_lines++;
711 chomp;
712  
713 # line format:
714 # myinfo: source_id target_id match_length source_length target_length
715 my ( undef, $source_id, $target_id, $match_length, $source_length,
716 $target_length )
717 = split;
718  
719 my $score = 0;
720  
721 if ( $source_length == 0 or $target_length == 0 ) {
722 $self->logger->warning(
723 "Alignment length is 0 for $source_id or $target_id.\n");
724 }
725 else {
726 $score = 2*$match_length/( $source_length + $target_length );
727  
728 }
729  
730 if ( $score > $threshold ) {
731 my $source_sr =
732 $self->cache()
733 ->get_by_key( 'exons_by_id', 'source', $source_id )
734 ->seq_region_name();
735 my $target_sr =
736 $self->cache()
737 ->get_by_key( 'exons_by_id', 'target', $target_id )
738 ->seq_region_name();
739  
740 if ( $source_sr ne $target_sr ) {
741 # PENALTY: The target and source are not on the same
742 # seq_region.
743 $score *= 0.70;
744  
745 # With a penalty of 0.7, exonerate scores need to be above
746 # approximately 0.714 to make the 0.5 threshold.
747  
748 ++$penalised;
749 }
750  
751 if ( $score > $threshold ) {
752 $exonerate_matrix->add_score( $source_id, $target_id,
753 $score );
754 }
755 else {
756 ++$killed;
757 }
758 } ## end if ( $score > $threshold)
759  
760 } ## end while (<$fh>)
761  
762 close($fh);
763 } ## end while ( defined( my $file...))
764  
765 closedir($dumpdir);
766  
767 $self->logger->info(
768 "Done parsing $num_lines lines from $num_files result files.\n",
769 0, 'stamped' );
770 $self->logger->info( "Penalised $penalised exon alignments " .
771 "for not being on the same seq_region " .
772 "($killed killed).\n",
773 0,
774 'stamped' );
775  
776 return $exonerate_matrix;
777 } ## end sub parse_exonerate_results
778  
779  
780 sub non_mapped_transcript_rescore {
781 my $self = shift;
782 my $matrix = shift;
783 my $transcript_mappings = shift;
784  
785 # argument checks
786 unless ($matrix
787 and $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix') )
788 {
789 throw(
790 'Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix of exons.');
791 }
792  
793 unless ( $transcript_mappings
794 and
795 $transcript_mappings->isa('Bio::EnsEMBL::IdMapping::MappingList') )
796 {
797 throw(
798 'Need a Bio::EnsEMBL::IdMapping::MappingList of transcripts.');
799 }
800  
801 # create a lookup hash of mapped source transcripts to target
802 # transcripts
803 my %transcript_lookup =
804 map { $_->source => $_->target }
805 @{ $transcript_mappings->get_all_Entries };
806  
807 my $i = 0;
808  
809 foreach my $entry ( @{ $matrix->get_all_Entries } ) {
810  
811 my @source_transcripts = @{
812 $self->cache->get_by_key( 'transcripts_by_exon_id', 'source',
813 $entry->source ) };
814 my @target_transcripts = @{
815 $self->cache->get_by_key( 'transcripts_by_exon_id', 'target',
816 $entry->target ) };
817  
818 my $found_mapped = 0;
819  
820 TR:
821 foreach my $source_tr (@source_transcripts) {
822 foreach my $target_tr (@target_transcripts) {
823 my $mapped_target = $transcript_lookup{ $source_tr->id };
824  
825 if ( $mapped_target && $mapped_target == $target_tr->id ) {
826 $found_mapped = 1;
827 last TR;
828 }
829 }
830 }
831  
832 unless ($found_mapped) {
833 # PENALTY: The exon appears to belong to a transcript that has not
834 # been mapped.
835 $matrix->set_score( $entry->source(), $entry->target(),
836 0.9*$entry->score() );
837 $i++;
838 }
839 } ## end foreach my $entry ( @{ $matrix...})
840  
841 $self->logger->debug( "Scored exons in non-mapped transcripts: $i\n",
842 1 );
843 } ## end sub non_mapped_transcript_rescore
844  
845  
846  
847 1;