EnsEMBL/IdMapping/InternalIdMapper/BaseMapper.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 42 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 213 CC 18 SCAR 32.1
CC
18
Cov
0%
basic_mapping
72.0
higher_score_exists
12.0
ambiguous_mapping
12.0
CRAP 24.9
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  
22 =head1 CONTACT
23  
24 Please email comments or questions to the public Ensembl
25 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
26  
27 Questions may also be sent to the Ensembl help desk at
28 <http://www.ensembl.org/Help/Contact>.
29  
30 =cut
31  
32 =head1 NAME
33  
34 =head1 SYNOPSIS
35  
36 =head1 DESCRIPTION
37  
38 =head1 METHODS
39  
40 =cut
41  
42 package Bio::EnsEMBL::IdMapping::InternalIdMapper::BaseMapper;
43 $Bio::EnsEMBL::IdMapping::InternalIdMapper::BaseMapper::VERSION = '114.0.0_50'; # TRIAL
44 $Bio::EnsEMBL::IdMapping::InternalIdMapper::BaseMapper::VERSION = '114.0.050';
45 use strict;
46 use warnings;
47 no warnings 'uninitialized';
48  
49 use Bio::EnsEMBL::IdMapping::BaseObject;
50 our @ISA = qw(Bio::EnsEMBL::IdMapping::BaseObject);
51  
52 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
53 use Bio::EnsEMBL::Utils::ScriptUtils qw(path_append);
54 use Bio::EnsEMBL::IdMapping::MappingList;
55  
56 # scores are considered the same if (2.0 * (s1-s2))/(s1 + s2) < this
57 use constant SIMILAR_SCORE_RATIO => 0.01;
58  
59 #
60 # find the highest unambiguous score for all sources and targets in a scoring
61 # matrix
62 #
63 sub basic_mapping {
64 my $self = shift;
65 my $matrix = shift;
66 my $mapping_name = shift;
67  
68 # argument checks
69 unless ($matrix
70 and $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix') )
71 {
72 throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.');
73 }
74  
75 throw('Need a name for serialising the mapping.')
76 unless ($mapping_name);
77  
78 # Create a new MappingList object. Specify AUTO_LOAD to load
79 # serialised existing mappings if found
80 my $dump_path =
81 path_append( $self->conf->param('basedir'), 'mapping' );
82  
83 my $mappings =
84 Bio::EnsEMBL::IdMapping::MappingList->new(
85 -DUMP_PATH => $dump_path,
86 -CACHE_FILE => "${mapping_name}.ser",
87 -AUTO_LOAD => 1, );
88  
89 # checkpoint test: return a previously stored MappingList
90 if ( $mappings->loaded ) {
91 $self->logger->info(
92 "Read existing mappings from ${mapping_name}.ser.\n");
93 return $mappings;
94 }
95  
96 my $sources_done = {};
97 my $targets_done = {};
98  
99 # sort scoring matrix entries by descending score
100 my @sorted_entries =
101 sort { $b->score <=> $a->score } @{ $matrix->get_all_Entries };
102  
103 # debug
104 #my $idx = substr($mapping_name, -1);
105  
106 while ( my $entry = shift(@sorted_entries) ) {
107  
108 #$self->logger->debug("\nxxx$idx ".$entry->to_string." ");
109  
110 # we already found a mapping for either source or target
111 next
112 if ( $sources_done->{ $entry->source }
113 or $targets_done->{ $entry->target } );
114  
115 #$self->logger->debug('d');
116  
117 # there's a better mapping for either source or target
118 next
119 if ( $self->higher_score_exists(
120 $entry, $matrix, $sources_done, $targets_done
121 ) );
122  
123 #$self->logger->debug('h');
124  
125 # check for ambiguous mappings; they are dealt with later
126 my $other_sources = [];
127 my $other_targets = [];
128  
129 if ( $self->ambiguous_mapping( $entry, $matrix,
130 $other_sources, $other_targets ) )
131 {
132 #$self->logger->debug('a');
133  
134 $other_sources =
135 $self->filter_sources( $other_sources, $sources_done );
136 $other_targets =
137 $self->filter_targets( $other_targets, $targets_done );
138  
139 next if ( scalar(@$other_sources) or scalar(@$other_targets) );
140 }
141  
142 #$self->logger->debug('A');
143  
144 # this is the best mapping, add it
145 $mappings->add_Entry($entry);
146  
147 $sources_done->{ $entry->source } = 1;
148 $targets_done->{ $entry->target } = 1;
149 } ## end while ( my $entry = shift...)
150  
151 # create checkpoint
152 $mappings->write_to_file;
153  
154 return $mappings;
155 } ## end sub basic_mapping
156  
157 sub higher_score_exists {
158 my ( $self, $entry, $matrix, $sources_done, $targets_done ) = @_;
159  
160 my $source = $entry->source;
161 my $target = $entry->target;
162 my $score = $entry->score;
163  
164 foreach
165 my $other_source ( @{ $matrix->get_sources_for_target($target) } )
166 {
167 if ( $other_source != $source
168 and !$sources_done->{$other_source}
169 and $score < $matrix->get_score( $other_source, $target ) )
170 {
171 return 1;
172 }
173 }
174  
175 foreach
176 my $other_target ( @{ $matrix->get_targets_for_source($source) } )
177 {
178 if ( $other_target != $target
179 and !$targets_done->{$other_target}
180 and $score < $matrix->get_score( $source, $other_target ) )
181 {
182 return 1;
183 }
184 }
185  
186 return 0;
187 } ## end sub higher_score_exists
188  
189 #
190 # find ambiguous mappings (see scores_similar() for definition)
191 #
192 sub ambiguous_mapping {
193 my ( $self, $entry, $matrix, $other_sources, $other_targets ) = @_;
194  
195 my $source = $entry->source;
196 my $target = $entry->target;
197 my $score = $entry->score;
198  
199 my $retval = 0;
200  
201 foreach
202 my $other_source ( @{ $matrix->get_sources_for_target($target) } )
203 {
204 my $other_score = $matrix->get_score( $other_source, $target );
205  
206 if ( $other_source != $source
207 and ( $self->scores_similar( $score, $other_score )
208 or $score < $other_score ) )
209 {
210 $retval = 1;
211 push @{$other_sources}, $other_source;
212 }
213 }
214  
215 foreach
216 my $other_target ( @{ $matrix->get_targets_for_source($source) } )
217 {
218 my $other_score = $matrix->get_score( $source, $other_target );
219  
220 if ( $other_target != $target
221 and ( $self->scores_similar( $score, $other_score )
222 or $score < $other_score ) )
223 {
224 $retval = 1;
225 push @{$other_targets}, $other_target;
226 }
227 }
228  
229 return $retval;
230 } ## end sub ambiguous_mapping
231  
232 #
233 # rule for similarity taken from java code...
234 #
235 sub scores_similar {
236 my ( $self, $s1, $s2 ) = @_;
237  
238 # always give priority to exact matches over very similar ones
239 return 0 if ( $s1 == 1 and $s2 < 1 );
240  
241 my $diff = $s1 - $s2;
242 $diff = -$diff if ( $diff < 0 );
243  
244 my $pc = 2*$diff/( $s1 + $s2 );
245  
246 return ( $pc < SIMILAR_SCORE_RATIO );
247 }
248  
249 sub filter_sources {
250 my ( $self, $other_sources, $sources_done ) = @_;
251  
252 unless ( scalar( @{$other_sources} )
253 and scalar( keys %{$sources_done} ) )
254 {
255 return $other_sources;
256 }
257  
258 my @tmp = ();
259  
260 foreach my $e ( @{$other_sources} ) {
261 push @tmp, $e unless ( $sources_done->{$e} );
262 }
263  
264 return \@tmp;
265 }
266  
267 sub filter_targets {
268 my ( $self, $other_targets, $targets_done ) = @_;
269  
270 unless ( scalar( @{$other_targets} )
271 and scalar( keys %{$targets_done} ) )
272 {
273 return $other_targets;
274 }
275  
276 my @tmp = ();
277  
278 foreach my $e ( @{$other_targets} ) {
279 push @tmp, $e unless ( $targets_done->{$e} );
280 }
281  
282 return \@tmp;
283 }
284  
285 1;