| 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 | Bio::EnsEMBL::IdMapping::ScoreBuilder - score builder base class | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | This class is not instantiated. Please see subclasses for usage examples | ||
| 38 | (e.g. GeneScoreBuilder). | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | This is the base class for the score builders used in the stable Id | ||
| 43 | mapping application. It contains methods which are used by more than one | ||
| 44 | ScoreBuilder. | ||
| 45 | |||
| 46 | =head1 METHODS | ||
| 47 | |||
| 48 | create_shrinked_matrix | ||
| 49 | internal_id_rescore | ||
| 50 | log_matrix_stats | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | package Bio::EnsEMBL::IdMapping::ScoreBuilder; | ||
| 55 | $Bio::EnsEMBL::IdMapping::ScoreBuilder::VERSION = '114.0.0_50'; # TRIAL | ||
| 56 | $Bio::EnsEMBL::IdMapping::ScoreBuilder::VERSION = '114.0.050'; | ||
| 57 | use strict; | ||
| 58 | use warnings; | ||
| 59 | no warnings 'uninitialized'; | ||
| 60 | |||
| 61 | use Bio::EnsEMBL::IdMapping::BaseObject; | ||
| 62 | our @ISA = qw(Bio::EnsEMBL::IdMapping::BaseObject); | ||
| 63 | |||
| 64 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 65 | use Bio::EnsEMBL::Utils::ScriptUtils qw(path_append); | ||
| 66 | use Bio::EnsEMBL::IdMapping::ScoredMappingMatrix; | ||
| 67 | |||
| 68 | |||
| 69 | =head2 create_shrinked_matrix | ||
| 70 | |||
| 71 | Arg[1] : Bio::EnsEMBL::Idmapping::ScoredMappingMatrix $matrix - a scoring | ||
| 72 | matrix | ||
| 73 | Arg[2] : Bio::EnsEMBL::Idmapping::MappingList $mappings - mappings | ||
| 74 | Arg[3] : String $cache_file - base name of a cache file (extension '.ser' | ||
| 75 | will be added automatically) for the returned matrix | ||
| 76 | Example : my $new_scores = $score_builder->create_shrinked_matrix( | ||
| 77 | $gene_scores, $mappings, "gene_matrix1"); | ||
| 78 | Description : Create a shrinked scoring matrix which doesn't contain entries | ||
| 79 | which were already mapped. It also logs how many new mappings | ||
| 80 | were added in this process. | ||
| 81 | Return type : Bio::EnsEMBL::IdMapping::ScoredMappingMatrix | ||
| 82 | Exceptions : thrown on wrong or missing arguments | ||
| 83 | Caller : InternalIdMapper plugin | ||
| 84 | Status : At Risk | ||
| 85 | : under development | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | # TODO: shrinked = shrunken? | ||
| 90 | sub create_shrinked_matrix { | ||
| 91 | my $self = shift; | ||
| 92 | my $matrix = shift; | ||
| 93 | my $mappings = shift; | ||
| 94 | my $cache_file = shift; # base name, extension '.ser' will be added | ||
| 95 | |||
| 96 | # argument checks | ||
| 97 | unless ($matrix and | ||
| 98 | $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) { | ||
| 99 | throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 100 | } | ||
| 101 | |||
| 102 | unless ($mappings and | ||
| 103 | $mappings->isa('Bio::EnsEMBL::IdMapping::MappingList')) { | ||
| 104 | throw('Need a gene Bio::EnsEMBL::IdMapping::MappingList.'); | ||
| 105 | } | ||
| 106 | |||
| 107 | throw('Need a cache file name.') unless ($cache_file); | ||
| 108 | |||
| 109 | my $dump_path = path_append($self->conf->param('basedir'), 'matrix'); | ||
| 110 | $cache_file .= '.ser'; | ||
| 111 | |||
| 112 | my $shrinked_matrix = Bio::EnsEMBL::IdMapping::ScoredMappingMatrix->new( | ||
| 113 | -DUMP_PATH => $dump_path, | ||
| 114 | -CACHE_FILE => $cache_file, | ||
| 115 | -AUTO_LOAD => 1, | ||
| 116 | ); | ||
| 117 | |||
| 118 | # if we already found a saved matrix, just return it | ||
| 119 | if ($shrinked_matrix->loaded) { | ||
| 120 | |||
| 121 | $self->logger->info("Read existing scoring matrix from $cache_file.\n"); | ||
| 122 | |||
| 123 | } else { | ||
| 124 | |||
| 125 | # create lookup hashes for sources and targets in the MappingList | ||
| 126 | my %sources = (); | ||
| 127 | my %targets = (); | ||
| 128 | |||
| 129 | foreach my $entry (@{ $mappings->get_all_Entries }) { | ||
| 130 | $sources{$entry->source} = 1; | ||
| 131 | $targets{$entry->target} = 1; | ||
| 132 | } | ||
| 133 | |||
| 134 | # add all entries to shrinked matrix which are not in the MappingList | ||
| 135 | foreach my $entry (@{ $matrix->get_all_Entries }) { | ||
| 136 | unless ($sources{$entry->source} or $targets{$entry->target}) { | ||
| 137 | $shrinked_matrix->add_Entry($entry); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | } | ||
| 142 | |||
| 143 | # log shrinking stats | ||
| 144 | $self->logger->info('Sources '.$matrix->get_source_count.' --> '. | ||
| 145 | $shrinked_matrix->get_source_count."\n"); | ||
| 146 | $self->logger->info('Targets '.$matrix->get_target_count.' --> '. | ||
| 147 | $shrinked_matrix->get_target_count."\n"); | ||
| 148 | $self->logger->info('Entries '.$matrix->get_entry_count.' --> '. | ||
| 149 | $shrinked_matrix->get_entry_count."\n"); | ||
| 150 | $self->logger->info('New mappings: '.$mappings->get_entry_count."\n\n"); | ||
| 151 | |||
| 152 | return $shrinked_matrix; | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | =head2 internal_id_rescore | ||
| 157 | |||
| 158 | Arg[1] : Bio::EnsEMBL::Idmapping::ScoredMappingMatrix $matrix - a scoring | ||
| 159 | matrix | ||
| 160 | Example : $score_builder->internal_id_rescore($gene_scores); | ||
| 161 | Description : Rescore ambiguous mappings based on internal Ids. This is the | ||
| 162 | last disambiguation step and is only useful if objects with the | ||
| 163 | same internal Id were used in source and target dbs (e.g. in | ||
| 164 | patch builds or if objects were copied from source to target). | ||
| 165 | |||
| 166 | If a source and target gene have the same internal Id and there | ||
| 167 | are mappings to other target genes then these *other* mappings | ||
| 168 | are penalised. | ||
| 169 | Return type : none | ||
| 170 | Exceptions : thrown on wrong or missing argument | ||
| 171 | Caller : InternalIdMapper plugins | ||
| 172 | Status : At Risk | ||
| 173 | : under development | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | sub internal_id_rescore { | ||
| 178 | my $self = shift; | ||
| 179 | my $matrix = shift; | ||
| 180 | |||
| 181 | unless ($matrix | ||
| 182 | and $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix') ) | ||
| 183 | { | ||
| 184 | throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 185 | } | ||
| 186 | |||
| 187 | my $i = 0; | ||
| 188 | |||
| 189 | foreach my $source ( @{ $matrix->get_all_sources } ) { | ||
| 190 | my @entries = | ||
| 191 | sort { $b <=> $a } @{ $matrix->get_Entries_for_source($source) }; | ||
| 192 | |||
| 193 | # nothing to do if we only have one mapping | ||
| 194 | if ( scalar(@entries) == 1 ) { next } | ||
| 195 | |||
| 196 | # only penalise if mappings are ambiguous | ||
| 197 | if ( $entries[0]->score != $entries[1]->score ) { next } | ||
| 198 | |||
| 199 | # only penalise if one source id == target id where score == best | ||
| 200 | # score | ||
| 201 | my $ambiguous = 0; | ||
| 202 | |||
| 203 | foreach my $e (@entries) { | ||
| 204 | if ( $e->target == $source and $e->score == $entries[0]->score() ) | ||
| 205 | { | ||
| 206 | $ambiguous = 1; | ||
| 207 | last; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | if ( !$ambiguous ) { next } | ||
| 212 | |||
| 213 | # now penalise those where source id != target id and score == best | ||
| 214 | # score | ||
| 215 | foreach my $e (@entries) { | ||
| 216 | if ( $e->target != $source and $e->score == $entries[0]->score() ) | ||
| 217 | { | ||
| 218 | # PENALTY: Reduce score for ambiguous mappings. | ||
| 219 | $matrix->set_score( $source, $e->target(), 0.9*$e->score() ); | ||
| 220 | $i++; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | } ## end foreach my $source ( @{ $matrix...}) | ||
| 225 | |||
| 226 | $self->logger->debug("Scored entries with internal ID mismatch: $i\n", | ||
| 227 | 1 ); | ||
| 228 | } ## end sub internal_id_rescore | ||
| 229 | |||
| 230 | |||
| 231 | =head2 log_matrix_stats | ||
| 232 | |||
| 233 | Arg[1] : Bio::EnsEMBL::Idmapping::ScoredMappingMatrix $matrix - a scoring | ||
| 234 | matrix | ||
| 235 | Example : $score_builder->log_matrix_stats; | ||
| 236 | Description : Logs scoring matrix statistics (number of entries, min/max/avg | ||
| 237 | scores). | ||
| 238 | Return type : none | ||
| 239 | Exceptions : thrown on wrong or missing argument | ||
| 240 | Caller : general | ||
| 241 | Status : At Risk | ||
| 242 | : under development | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub log_matrix_stats { | ||
| 247 | my $self = shift; | ||
| 248 | my $matrix = shift; | ||
| 249 | |||
| 250 | unless ($matrix and | ||
| 251 | $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) { | ||
| 252 | throw('You must provide a ScoredMappingMatrix.'); | ||
| 253 | } | ||
| 254 | |||
| 255 | my $fmt1 = "%-40s%10.0f\n"; | ||
| 256 | my $fmt2 = "%-40s%10.5f\n"; | ||
| 257 | |||
| 258 | $self->logger->info(sprintf($fmt1, "Scoring matrix entries:", | ||
| 259 | $matrix->get_entry_count), 1); | ||
| 260 | |||
| 261 | $self->logger->info(sprintf($fmt1, "Scoring matrix sources:", | ||
| 262 | $matrix->get_source_count), 1); | ||
| 263 | |||
| 264 | $self->logger->info(sprintf($fmt1, "Scoring matrix targets:", | ||
| 265 | $matrix->get_target_count), 1); | ||
| 266 | |||
| 267 | $self->logger->info(sprintf($fmt2, "Average score:", | ||
| 268 | $matrix->get_average_score), 1); | ||
| 269 | |||
| 270 | my ($min, $max) = @{ $matrix->get_min_max_scores }; | ||
| 271 | $self->logger->info(sprintf($fmt2, "Min. score:", $min), 1); | ||
| 272 | $self->logger->info(sprintf($fmt2, "Max. score:", $max), 1); | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | 1; | ||
| 277 |