my $self = shift;
my $matrix = shift;
my $mapping_name = shift;
# argument checks
unless ($matrix
and $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix') )
{
throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.');
}
throw('Need a name for serialising the mapping.')
unless ($mapping_name);
# Create a new MappingList object. Specify AUTO_LOAD to load
# serialised existing mappings if found
my $dump_path =
path_append( $self->conf->param('basedir'), 'mapping' );
my $mappings =
-DUMP_PATH => $dump_path,
-CACHE_FILE => "${mapping_name}.ser",
-AUTO_LOAD => 1, );
# checkpoint test: return a previously stored MappingList
if ( $mappings->loaded ) {
$self->logger->info(
"Read existing mappings from ${mapping_name}.ser.\n");
return $mappings;
}
my $sources_done = {};
my $targets_done = {};
# sort scoring matrix entries by descending score
my @sorted_entries =
# debug
#my $idx = substr($mapping_name, -1);
while ( my $entry = shift(@sorted_entries) ) {
#$self->logger->debug("\nxxx$idx ".$entry->to_string." ");
# we already found a mapping for either source or target
next
if ( $sources_done->{ $entry->source }
or $targets_done->{ $entry->target } );
#$self->logger->debug('d');
# there's a better mapping for either source or target
next
if ( $self->higher_score_exists(
$entry, $matrix, $sources_done, $targets_done
) );
#$self->logger->debug('h');
# check for ambiguous mappings; they are dealt with later
my $other_sources = [];
my $other_targets = [];
if ( $self->ambiguous_mapping( $entry, $matrix,
$other_sources, $other_targets ) )
{
#$self->logger->debug('a');
$other_sources =
$self->filter_sources( $other_sources, $sources_done );
$other_targets =
$self->filter_targets( $other_targets, $targets_done );
next if ( scalar(@$other_sources) or scalar(@$other_targets) );
}
#$self->logger->debug('A');
# this is the best mapping, add it
$mappings->add_Entry($entry);
$sources_done->{ $entry->source } = 1;
$targets_done->{ $entry->target } = 1;
} ## end while ( my $entry = shift...)
# create checkpoint
$mappings->write_to_file;
return $mappings;