| 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::ScoredMappingMatrix - object holding a list of scored | ||
| 34 | Entries | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | # create a new ScoredMappingMatrix | ||
| 39 | my $gene_scores = Bio::EnsEMBL::IdMapping::ScoredMappingMatrix->new( | ||
| 40 | -DUMP_PATH => $dump_path, | ||
| 41 | -CACHE_FILE => 'gene_scores.ser', | ||
| 42 | ); | ||
| 43 | |||
| 44 | # add entries | ||
| 45 | my $gene_scores->add_Entry($entry1); | ||
| 46 | |||
| 47 | # serialise to file | ||
| 48 | $gene_scores->write_to_file; | ||
| 49 | |||
| 50 | # later, read these gene_scores from file | ||
| 51 | my $gene_scores1 = Bio::EnsEMBL::IdMapping::ScoredMappingMatrix->new( | ||
| 52 | -DUMP_PATH => $dump_path, | ||
| 53 | -CACHE_FILE => 'gene_gene_scores.ser', | ||
| 54 | ); | ||
| 55 | $gene_scores1->read_from_file; | ||
| 56 | |||
| 57 | =head1 DESCRIPTION | ||
| 58 | |||
| 59 | This object represents a collection of scores between source and target | ||
| 60 | objects. It holds a list of Bio::EnsEMBL::IdMapping::Entry objects and | ||
| 61 | has methods to retrieve indiviual or all Entries, as well as derived | ||
| 62 | data like number of unique sources or targets, or various counts and | ||
| 63 | averages. | ||
| 64 | |||
| 65 | It is the main collection for dealing with scored relationships in the | ||
| 66 | stable Id mapping application. | ||
| 67 | |||
| 68 | =head1 METHODS | ||
| 69 | |||
| 70 | new | ||
| 71 | flush | ||
| 72 | sub_matrix | ||
| 73 | add_Entry | ||
| 74 | update_Entry | ||
| 75 | remove_Entry | ||
| 76 | add_score | ||
| 77 | set_score | ||
| 78 | get_Entry | ||
| 79 | get_score | ||
| 80 | get_targets_for_source | ||
| 81 | get_Entries_for_source | ||
| 82 | get_sources_for_target | ||
| 83 | get_Entries_for_target | ||
| 84 | get_all_Entries | ||
| 85 | get_all_sources | ||
| 86 | get_all_targets | ||
| 87 | get_entry_count | ||
| 88 | size | ||
| 89 | get_source_count | ||
| 90 | get_target_count | ||
| 91 | get_min_max_scores | ||
| 92 | get_average_score | ||
| 93 | merge | ||
| 94 | log | ||
| 95 | to_string | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | package Bio::EnsEMBL::IdMapping::ScoredMappingMatrix; | ||
| 100 | $Bio::EnsEMBL::IdMapping::ScoredMappingMatrix::VERSION = '114.0.0'; | ||
| 101 | use strict; | ||
| 102 | use warnings; | ||
| 103 | no warnings 'uninitialized'; | ||
| 104 | |||
| 105 | use Bio::EnsEMBL::IdMapping::Serialisable; | ||
| 106 | our @ISA = qw(Bio::EnsEMBL::IdMapping::Serialisable); | ||
| 107 | |||
| 108 | use Bio::EnsEMBL::Utils::Argument qw(rearrange); | ||
| 109 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 110 | use Bio::EnsEMBL::Utils::ScriptUtils qw(path_append); | ||
| 111 | use Bio::EnsEMBL::IdMapping::Entry; | ||
| 112 | |||
| 113 | |||
| 114 | =head2 new | ||
| 115 | |||
| 116 | Arg[1-N] : see superclass | ||
| 117 | Example : my $gene_scores = Bio::EnsEMBL::IdMapping::ScoredMappingMatrix->new( | ||
| 118 | -DUMP_PATH => $dump_path, | ||
| 119 | -CACHE_FILE => 'gene_scores.ser', | ||
| 120 | ); | ||
| 121 | Description : Constructor. | ||
| 122 | Return type : Bio::EnsEMBL::IdMapping::ScoredMappingMatrix | ||
| 123 | Exceptions : none | ||
| 124 | Caller : general | ||
| 125 | Status : At Risk | ||
| 126 | : under development | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub new { | ||
| 131 | my $caller = shift; | ||
| 132 | my $class = ref($caller) || $caller; | ||
| 133 | my $self = $class->SUPER::new(@_); | ||
| 134 | |||
| 135 | # initialise internal datastructure | ||
| 136 | unless ($self->loaded) { | ||
| 137 | $self->{'cache'}->{'matrix'} = {}; | ||
| 138 | $self->{'cache'}->{'source_list'} = {}; | ||
| 139 | $self->{'cache'}->{'target_list'} = {}; | ||
| 140 | } | ||
| 141 | |||
| 142 | return $self; | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | =head2 flush | ||
| 147 | |||
| 148 | Example : $gene_scores->flush; | ||
| 149 | Description : Flushes (empties) the scoring matrix. | ||
| 150 | Return type : none | ||
| 151 | Exceptions : none | ||
| 152 | Caller : general | ||
| 153 | Status : At Risk | ||
| 154 | : under development | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | sub flush { | ||
| 159 | my $self = shift; | ||
| 160 | |||
| 161 | # reset caches | ||
| 162 | $self->{'cache'}->{'matrix'} = {}; | ||
| 163 | $self->{'cache'}->{'source_list'} = {}; | ||
| 164 | $self->{'cache'}->{'target_list'} = {}; | ||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | =head2 sub_matrix | ||
| 169 | |||
| 170 | Arg[1] : Int $start - start index (inclusive) | ||
| 171 | Arg[2] : Int $end - end index (inclusive) | ||
| 172 | Example : # get the first 1000 elements in the matrix | ||
| 173 | my $sub_matrix = $gene_scores->sub_matrix(1, 1000); | ||
| 174 | Description : Returns a sub-matrix of the ScoredMappingMatrix. The arguments | ||
| 175 | ($start and $end) specify the position of the first and last | ||
| 176 | element to return (inclusive, counting starts with element 1, | ||
| 177 | not 0) | ||
| 178 | Return type : Bio::EnsEMBL::IdMapping::ScoredMappingMatrix | ||
| 179 | Exceptions : none | ||
| 180 | Caller : general | ||
| 181 | Status : At Risk | ||
| 182 | : under development | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub sub_matrix { | ||
| 187 | my $self = shift; | ||
| 188 | my $start = shift; | ||
| 189 | my $end = shift; | ||
| 190 | |||
| 191 | # default to returning the full matrix if no start/end provided | ||
| 192 | $start ||= 1; | ||
| 193 | $end ||= $self->size; | ||
| 194 | |||
| 195 | my $sub_matrix = Bio::EnsEMBL::IdMapping::ScoredMappingMatrix->new( | ||
| 196 | -DUMP_PATH => $self->dump_path, | ||
| 197 | -CACHE_FILE => $self->cache_file_name, | ||
| 198 | ); | ||
| 199 | my $i = 0; | ||
| 200 | |||
| 201 | foreach my $key (sort keys %{ $self->{'cache'}->{'matrix'} }) { | ||
| 202 | $i++; | ||
| 203 | next if ($i < $start); | ||
| 204 | last if ($i > $end); | ||
| 205 | |||
| 206 | my ($source, $target) = split(/:/, $key); | ||
| 207 | $sub_matrix->add_score($source, $target, | ||
| 208 | $self->{'cache'}->{'matrix'}->{$key}); | ||
| 209 | } | ||
| 210 | |||
| 211 | return $sub_matrix; | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | =head2 add_Entry | ||
| 216 | |||
| 217 | Arg[1] : Bio::EnsEMBL::IdMapping::Entry $entry - Entry to add | ||
| 218 | Example : $gene_scores->add_Entry($entry); | ||
| 219 | Description : Adds an Entry to the scoring matrix. | ||
| 220 | Return type : Float - the Entry's score | ||
| 221 | Exceptions : thrown on wrong or missing argument | ||
| 222 | Caller : general | ||
| 223 | Status : At Risk | ||
| 224 | : under development | ||
| 225 | |||
| 226 | =cut | ||
| 227 | |||
| 228 | sub add_Entry { | ||
| 229 | my $self = shift; | ||
| 230 | my $entry = shift; | ||
| 231 | |||
| 232 | unless ($entry and $entry->isa('Bio::EnsEMBL::IdMapping::Entry')) { | ||
| 233 | throw("Need a Bio::EnsEMBL::IdMapping::Entry"); | ||
| 234 | } | ||
| 235 | |||
| 236 | return $self->add_score($entry->source, $entry->target, $entry->score); | ||
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | =head2 update_Entry | ||
| 241 | |||
| 242 | Arg[1] : Bio::EnsEMBL::IdMapping::Entry $entry - Entry to update | ||
| 243 | Example : $gene_scores->update_Entry($entry); | ||
| 244 | Description : Updates an Entry (or rather its score) in the scoring matrix. | ||
| 245 | Actually delegates to add_Entry(), only there as an intuitively | ||
| 246 | named wrapper. | ||
| 247 | Return type : Float - the Entry's score | ||
| 248 | Exceptions : thrown on wrong or missing argument | ||
| 249 | Caller : general | ||
| 250 | Status : At Risk | ||
| 251 | : under development | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | sub update_Entry { | ||
| 256 | return $_[0]->add_Entry($_[1]); | ||
| 257 | } | ||
| 258 | |||
| 259 | |||
| 260 | # | ||
| 261 | # not needed in the current application, so not implemented | ||
| 262 | # | ||
| 263 | sub remove_Entry { | ||
| 264 | warning('Method ScoredMappingMatrix->remove_Entry not implemented (yet).'); | ||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | =head2 add_score | ||
| 269 | |||
| 270 | Arg[1] : Int $source - source object's internal Id ("dbID") | ||
| 271 | Arg[2] : Int $target - target object's internal Id ("dbID") | ||
| 272 | Arg[3] : Float $score - score for source/target pair | ||
| 273 | Example : $gene_scores->add_score(1234, 5678, 0.997); | ||
| 274 | Description : Adds a score for a source/target pair to the scoring matrix. | ||
| 275 | This is a low-level version of add_Entry(). | ||
| 276 | Return type : Float - the score | ||
| 277 | Exceptions : none | ||
| 278 | Caller : general | ||
| 279 | Status : At Risk | ||
| 280 | : under development | ||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | sub add_score { | ||
| 285 | my $self = shift; | ||
| 286 | my $source = shift; | ||
| 287 | my $target = shift; | ||
| 288 | my $score = shift; | ||
| 289 | |||
| 290 | # make sure you don't put duplicates on the source and target lists | ||
| 291 | unless (exists($self->{'cache'}->{'matrix'}->{"$source:$target"})) { | ||
| 292 | push @{ $self->{'cache'}->{'source_list'}->{$source} }, $target; | ||
| 293 | push @{ $self->{'cache'}->{'target_list'}->{$target} }, $source; | ||
| 294 | } | ||
| 295 | |||
| 296 | $self->{'cache'}->{'matrix'}->{"$source:$target"} = $score; | ||
| 297 | } | ||
| 298 | |||
| 299 | |||
| 300 | =head2 set_score | ||
| 301 | |||
| 302 | Arg[1] : Int $source - source object's internal Id ("dbID") | ||
| 303 | Arg[2] : Int $target - target object's internal Id ("dbID") | ||
| 304 | Arg[3] : Float $score - score for source/target pair | ||
| 305 | Example : $gene_scores->set_score(1234, 5678, 0.997); | ||
| 306 | Description : Sets the score for a source/target pair in the scoring matrix. | ||
| 307 | This method is similar to add_score, but assumes that the Entry | ||
| 308 | has been added before, so won't update the sources and target | ||
| 309 | lists. | ||
| 310 | Return type : Float - the score | ||
| 311 | Exceptions : none | ||
| 312 | Caller : general | ||
| 313 | Status : At Risk | ||
| 314 | : under development | ||
| 315 | |||
| 316 | =cut | ||
| 317 | |||
| 318 | sub set_score { | ||
| 319 | my $self = shift; | ||
| 320 | my $source = shift; | ||
| 321 | my $target = shift; | ||
| 322 | my $score = shift; | ||
| 323 | |||
| 324 | $self->{'cache'}->{'matrix'}->{"$source:$target"} = $score; | ||
| 325 | } | ||
| 326 | |||
| 327 | |||
| 328 | =head2 get_Entry | ||
| 329 | |||
| 330 | Arg[1] : Int $source - source object's internal Id ("dbID") | ||
| 331 | Arg[2] : Int $target - target object's internal Id ("dbID") | ||
| 332 | Example : my $entry = $gene_scores->get_Entry($source_gene->id, | ||
| 333 | $target_gene->id); | ||
| 334 | Description : Gets an Entry from the scoring matrix for a given source and | ||
| 335 | target object. | ||
| 336 | Return type : Bio::EnsEMBL::IdMapping::Entry or undef | ||
| 337 | Exceptions : none | ||
| 338 | Caller : general | ||
| 339 | Status : At Risk | ||
| 340 | : under development | ||
| 341 | |||
| 342 | =cut | ||
| 343 | |||
| 344 | sub get_Entry { | ||
| 345 | my $self = shift; | ||
| 346 | my $source = shift; | ||
| 347 | my $target = shift; | ||
| 348 | |||
| 349 | if (exists($self->{'cache'}->{'matrix'}->{"$source:$target"})) { | ||
| 350 | return Bio::EnsEMBL::IdMapping::Entry->new_fast( | ||
| 351 | [$source, $target, $self->{'cache'}->{'matrix'}->{"$source:$target"}] | ||
| 352 | ); | ||
| 353 | } else { | ||
| 354 | return undef; | ||
| 355 | } | ||
| 356 | } | ||
| 357 | |||
| 358 | |||
| 359 | =head2 get_score | ||
| 360 | |||
| 361 | Arg[1] : Int $source - source object's internal Id ("dbID") | ||
| 362 | Arg[2] : Int $target - target object's internal Id ("dbID") | ||
| 363 | Example : my $score = $gene_scores->get_score($source_gene->id, | ||
| 364 | $target_gene->id); | ||
| 365 | Description : Gets the score from the scoring matrix for a given source and | ||
| 366 | target object. | ||
| 367 | Return type : Float or undef | ||
| 368 | Exceptions : none | ||
| 369 | Caller : general | ||
| 370 | Status : At Risk | ||
| 371 | : under development | ||
| 372 | |||
| 373 | =cut | ||
| 374 | |||
| 375 | sub get_score { | ||
| 376 | my $self = shift; | ||
| 377 | my $source = shift; | ||
| 378 | my $target = shift; | ||
| 379 | |||
| 380 | |||
| 381 | if (exists($self->{'cache'}->{'matrix'}->{"$source:$target"})) { | ||
| 382 | return $self->{'cache'}->{'matrix'}->{"$source:$target"}; | ||
| 383 | } else { | ||
| 384 | return undef; | ||
| 385 | } | ||
| 386 | } | ||
| 387 | |||
| 388 | |||
| 389 | =head2 get_targets_for_source | ||
| 390 | |||
| 391 | Arg[1] : Int $source - source object's internal Id ("dbID") | ||
| 392 | Example : my @targets = @{ $gene_scores->get_targets_for_source(1234) }; | ||
| 393 | Description : Returns a list of all targets which have a score against a given | ||
| 394 | source object. | ||
| 395 | Return type : Arrayref of Int (target objects' internal Ids) | ||
| 396 | Exceptions : none | ||
| 397 | Caller : general | ||
| 398 | Status : At Risk | ||
| 399 | : under development | ||
| 400 | |||
| 401 | =cut | ||
| 402 | |||
| 403 | sub get_targets_for_source { | ||
| 404 | my $self = shift; | ||
| 405 | my $source = shift; | ||
| 406 | |||
| 407 | return $self->{'cache'}->{'source_list'}->{$source} || []; | ||
| 408 | } | ||
| 409 | |||
| 410 | |||
| 411 | =head2 get_Entries_for_source | ||
| 412 | |||
| 413 | Arg[1] : Int $source - source object's internal Id ("dbID") | ||
| 414 | Example : my @entries = @{ $gene_scores->get_Entries_for_source(1234) }; | ||
| 415 | Description : Returns a list of all Entries in the scoring matrix for a given | ||
| 416 | source object. | ||
| 417 | Return type : Arrayref of Bio::EnsEMBL::IdMapping::Entry objects | ||
| 418 | Exceptions : none | ||
| 419 | Caller : general | ||
| 420 | Status : At Risk | ||
| 421 | : under development | ||
| 422 | |||
| 423 | =cut | ||
| 424 | |||
| 425 | sub get_Entries_for_source { | ||
| 426 | my $self = shift; | ||
| 427 | my $source = shift; | ||
| 428 | |||
| 429 | return [ map { $self->get_Entry($source, $_) } | ||
| 430 | @{ $self->{'cache'}->{'source_list'}->{$source} || [] } ]; | ||
| 431 | } | ||
| 432 | |||
| 433 | |||
| 434 | =head2 get_sources_for_target | ||
| 435 | |||
| 436 | Arg[1] : Int $target - target object's internal Id ("dbID") | ||
| 437 | Example : my @sources = @{ $gene_scores->get_sources_for_target(5678) }; | ||
| 438 | Description : Returns a list of all sources which have a score against a given | ||
| 439 | target object. | ||
| 440 | Return type : Arrayref of Int (source objects' internal Ids) | ||
| 441 | Exceptions : none | ||
| 442 | Caller : general | ||
| 443 | Status : At Risk | ||
| 444 | : under development | ||
| 445 | |||
| 446 | =cut | ||
| 447 | |||
| 448 | sub get_sources_for_target { | ||
| 449 | my $self = shift; | ||
| 450 | my $target = shift; | ||
| 451 | |||
| 452 | return $self->{'cache'}->{'target_list'}->{$target} || []; | ||
| 453 | } | ||
| 454 | |||
| 455 | |||
| 456 | =head2 get_Entries_for_target | ||
| 457 | |||
| 458 | Arg[1] : Int $target - target object's internal Id ("dbID") | ||
| 459 | Example : my @entries = @{ $gene_scores->get_Entries_for_target(5678) }; | ||
| 460 | Description : Returns a list of all Entries in the scoring matrix for a given | ||
| 461 | target object. | ||
| 462 | Return type : Arrayref of Bio::EnsEMBL::IdMapping::Entry objects | ||
| 463 | Exceptions : none | ||
| 464 | Caller : general | ||
| 465 | Status : At Risk | ||
| 466 | : under development | ||
| 467 | |||
| 468 | =cut | ||
| 469 | |||
| 470 | sub get_Entries_for_target { | ||
| 471 | my $self = shift; | ||
| 472 | my $target = shift; | ||
| 473 | |||
| 474 | return [ map { $self->get_Entry($_, $target) } | ||
| 475 | @{ $self->{'cache'}->{'target_list'}->{$target} || [] } ]; | ||
| 476 | } | ||
| 477 | |||
| 478 | |||
| 479 | =head2 get_all_Entries | ||
| 480 | |||
| 481 | Example : foreach my $entry (@{ $gene_scores->get_all_Entries }) { | ||
| 482 | # do something with the entry | ||
| 483 | } | ||
| 484 | Description : Returns a list of all Entries in the scoring matrix. | ||
| 485 | Return type : Arrayref of Bio::EnsEMBL::IdMapping::Entry objects | ||
| 486 | Exceptions : none | ||
| 487 | Caller : general | ||
| 488 | Status : At Risk | ||
| 489 | : under development | ||
| 490 | |||
| 491 | =cut | ||
| 492 | |||
| 493 | sub get_all_Entries { | ||
| 494 | my $self = shift; | ||
| 495 | |||
| 496 | my @result = (); | ||
| 497 | |||
| 498 | foreach my $key (keys %{ $self->{'cache'}->{'matrix'} }) { | ||
| 499 | my ($source, $target) = split(/:/, $key); | ||
| 500 | push @result, Bio::EnsEMBL::IdMapping::Entry->new_fast( | ||
| 501 | [$source, $target, $self->{'cache'}->{'matrix'}->{$key}] | ||
| 502 | ); | ||
| 503 | } | ||
| 504 | |||
| 505 | return \@result; | ||
| 506 | } | ||
| 507 | |||
| 508 | |||
| 509 | =head2 get_all_sources | ||
| 510 | |||
| 511 | Example : my @sources = @{ $gene_scores->get_all_sources }; | ||
| 512 | Description : Returns a list of all sources in the scoring matrix. | ||
| 513 | Return type : Arrayref of Int (source objects' internal Ids) | ||
| 514 | Exceptions : none | ||
| 515 | Caller : general | ||
| 516 | Status : At Risk | ||
| 517 | : under development | ||
| 518 | |||
| 519 | =cut | ||
| 520 | |||
| 521 | sub get_all_sources { | ||
| 522 | my $self = shift; | ||
| 523 | return [keys %{ $self->{'cache'}->{'source_list'} }]; | ||
| 524 | } | ||
| 525 | |||
| 526 | |||
| 527 | =head2 get_all_targets | ||
| 528 | |||
| 529 | Example : my @targets = @{ $gene_scores->get_all_targets }; | ||
| 530 | Description : Returns a list of all targets in the scoring matrix. | ||
| 531 | Return type : Arrayref of Int (target objects' internal Ids) | ||
| 532 | Exceptions : none | ||
| 533 | Caller : general | ||
| 534 | Status : At Risk | ||
| 535 | : under development | ||
| 536 | |||
| 537 | =cut | ||
| 538 | |||
| 539 | sub get_all_targets { | ||
| 540 | my $self = shift; | ||
| 541 | return [keys %{ $self->{'cache'}->{'target_list'} }]; | ||
| 542 | } | ||
| 543 | |||
| 544 | |||
| 545 | =head2 get_entry_count | ||
| 546 | |||
| 547 | Example : my $num_entries = $gene_scores->get_entry_count; | ||
| 548 | Description : Returns the number of Entries in the scoring matrix. | ||
| 549 | Return type : Int | ||
| 550 | Exceptions : none | ||
| 551 | Caller : general | ||
| 552 | Status : At Risk | ||
| 553 | : under development | ||
| 554 | |||
| 555 | =cut | ||
| 556 | |||
| 557 | sub get_entry_count { | ||
| 558 | my $self = shift; | ||
| 559 | return scalar(keys %{ $self->{'cache'}->{'matrix'} }); | ||
| 560 | } | ||
| 561 | |||
| 562 | |||
| 563 | =head2 size | ||
| 564 | |||
| 565 | Example : my $size = $gene_scores->size; | ||
| 566 | Description : Returns the size of the scoring matrix. Same value as returned | ||
| 567 | by get_entry_count(). | ||
| 568 | Return type : Int | ||
| 569 | Exceptions : none | ||
| 570 | Caller : general | ||
| 571 | Status : At Risk | ||
| 572 | : under development | ||
| 573 | |||
| 574 | =cut | ||
| 575 | |||
| 576 | sub size { | ||
| 577 | return $_[0]->get_entry_count; | ||
| 578 | } | ||
| 579 | |||
| 580 | |||
| 581 | =head2 get_source_count | ||
| 582 | |||
| 583 | Example : my $num_sources = $gene_scores->get_source_count; | ||
| 584 | Description : Returns the number of distinct sources in the scoring matrix. | ||
| 585 | Return type : Int | ||
| 586 | Exceptions : none | ||
| 587 | Caller : general | ||
| 588 | Status : At Risk | ||
| 589 | : under development | ||
| 590 | |||
| 591 | =cut | ||
| 592 | |||
| 593 | sub get_source_count { | ||
| 594 | my $self = shift; | ||
| 595 | return scalar(keys %{ $self->{'cache'}->{'source_list'} }); | ||
| 596 | } | ||
| 597 | |||
| 598 | |||
| 599 | =head2 get_target_count | ||
| 600 | |||
| 601 | Example : my $num_targets = $gene_scores->get_target_count; | ||
| 602 | Description : Returns the number of distinct targets in the scoring matrix. | ||
| 603 | Return type : Int | ||
| 604 | Exceptions : none | ||
| 605 | Caller : general | ||
| 606 | Status : At Risk | ||
| 607 | : under development | ||
| 608 | |||
| 609 | =cut | ||
| 610 | |||
| 611 | sub get_target_count { | ||
| 612 | my $self = shift; | ||
| 613 | return scalar(keys %{ $self->{'cache'}->{'target_list'} }); | ||
| 614 | } | ||
| 615 | |||
| 616 | |||
| 617 | =head2 get_min_max_scores | ||
| 618 | |||
| 619 | Example : my ($min_score, $max_score) = | ||
| 620 | @{ $gene_scores->get_min_max_scores }; | ||
| 621 | Description : Returns the mininum and maximum score in the scoring matrix. | ||
| 622 | Return type : Arrayref of Float [min_score, max_score] | ||
| 623 | Exceptions : none | ||
| 624 | Caller : general | ||
| 625 | Status : At Risk | ||
| 626 | : under development | ||
| 627 | |||
| 628 | =cut | ||
| 629 | |||
| 630 | sub get_min_max_scores { | ||
| 631 | my $self = shift; | ||
| 632 | |||
| 633 | my @keys = keys %{ $self->{'cache'}->{'matrix'} }; | ||
| 634 | |||
| 635 | return [undef, undef] unless (@keys); | ||
| 636 | |||
| 637 | # initialise; this should make loop quicker | ||
| 638 | my $min = $self->{'cache'}->{'matrix'}->{$keys[0]}; | ||
| 639 | my $max = $self->{'cache'}->{'matrix'}->{$keys[0]}; | ||
| 640 | |||
| 641 | foreach my $key (@keys) { | ||
| 642 | $min = $self->{'cache'}->{'matrix'}->{$key} if ($min > $self->{'cache'}->{'matrix'}->{$key}); | ||
| 643 | $max = $self->{'cache'}->{'matrix'}->{$key} if ($max < $self->{'cache'}->{'matrix'}->{$key}); | ||
| 644 | } | ||
| 645 | |||
| 646 | return [$min, $max]; | ||
| 647 | } | ||
| 648 | |||
| 649 | |||
| 650 | =head2 get_average_score | ||
| 651 | |||
| 652 | Example : my $avg_score = $gene_scores->get_average_score; | ||
| 653 | Description : Returns the average (mean) score in the matrix. | ||
| 654 | Return type : Float | ||
| 655 | Exceptions : none | ||
| 656 | Caller : general | ||
| 657 | Status : At Risk | ||
| 658 | : under development | ||
| 659 | |||
| 660 | =cut | ||
| 661 | |||
| 662 | sub get_average_score { | ||
| 663 | my $self = shift; | ||
| 664 | |||
| 665 | my @keys = keys %{ $self->{'cache'}->{'matrix'} }; | ||
| 666 | |||
| 667 | return undef unless (@keys); | ||
| 668 | |||
| 669 | my $total = 0; | ||
| 670 | |||
| 671 | foreach my $key (@keys) { | ||
| 672 | $total += $self->{'cache'}->{'matrix'}->{$key}; | ||
| 673 | } | ||
| 674 | |||
| 675 | return $total/scalar(@keys); | ||
| 676 | } | ||
| 677 | |||
| 678 | |||
| 679 | =head2 merge | ||
| 680 | |||
| 681 | Arg[1] : Bio::EnsEMBL::IdMapping::ScoredMappingMatrix $matrix - another | ||
| 682 | matrix to merge with | ||
| 683 | Example : my $update_count = $gene_scores->merge($more_gene_scores); | ||
| 684 | Description : Merges two scoring matrices. If there's an Entry for a | ||
| 685 | source/target pair in both matrices, the higher score will be | ||
| 686 | retained. | ||
| 687 | Return type : Int - number of Entries added or updated | ||
| 688 | Exceptions : thrown on wrong or missing argument | ||
| 689 | Caller : general | ||
| 690 | Status : At Risk | ||
| 691 | : under development | ||
| 692 | |||
| 693 | =cut | ||
| 694 | |||
| 695 | sub merge { | ||
| 696 | my $self = shift; | ||
| 697 | my $matrix = shift; | ||
| 698 | |||
| 699 | unless ($matrix | ||
| 700 | and $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix') ) | ||
| 701 | { | ||
| 702 | throw( | ||
| 703 | 'You must provide a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix' | ||
| 704 | ); | ||
| 705 | } | ||
| 706 | |||
| 707 | my $c = 0; | ||
| 708 | |||
| 709 | # merge the matrices | ||
| 710 | foreach my $key ( keys %{ $matrix->{'cache'}->{'matrix'} } ) { | ||
| 711 | if ( !defined( $self->{'cache'}->{'matrix'}->{$key} ) | ||
| 712 | or ( $self->{'cache'}->{'matrix'}->{$key} < | ||
| 713 | $matrix->{'cache'}->{'matrix'}->{$key} ) ) | ||
| 714 | { | ||
| 715 | $self->{'cache'}->{'matrix'}->{$key} = | ||
| 716 | $matrix->{'cache'}->{'matrix'}->{$key}; | ||
| 717 | $c++; | ||
| 718 | } | ||
| 719 | } | ||
| 720 | |||
| 721 | # merge sources and target lists | ||
| 722 | foreach my $key ( keys %{ $matrix->{'cache'}->{'source_list'} } ) { | ||
| 723 | if ( defined( $self->{'cache'}->{'source_list'}->{$key} ) ) { | ||
| 724 | # need to merge lists | ||
| 725 | my %unique = | ||
| 726 | map { $_ => 1 } @{ $self->get_targets_for_source($key) }; | ||
| 727 | map { $unique{$_} = 1 } | ||
| 728 | @{ $matrix->get_targets_for_source($key) }; | ||
| 729 | $self->{'cache'}->{'source_list'}->{$key} = [ keys %unique ]; | ||
| 730 | } else { | ||
| 731 | # no merging needed | ||
| 732 | $self->{'cache'}->{'source_list'}->{$key} = | ||
| 733 | $matrix->{'cache'}->{'source_list'}->{$key}; | ||
| 734 | } | ||
| 735 | } | ||
| 736 | |||
| 737 | foreach my $key ( keys %{ $matrix->{'cache'}->{'target_list'} } ) { | ||
| 738 | if ( defined( $self->{'cache'}->{'target_list'}->{$key} ) ) { | ||
| 739 | # need to merge lists | ||
| 740 | my %unique = | ||
| 741 | map { $_ => 1 } @{ $self->get_sources_for_target($key) }; | ||
| 742 | map { $unique{$_} = 1 } | ||
| 743 | @{ $matrix->get_sources_for_target($key) }; | ||
| 744 | $self->{'cache'}->{'target_list'}->{$key} = [ keys %unique ]; | ||
| 745 | } else { | ||
| 746 | # no merging needed | ||
| 747 | $self->{'cache'}->{'target_list'}->{$key} = | ||
| 748 | $matrix->{'cache'}->{'target_list'}->{$key}; | ||
| 749 | } | ||
| 750 | } | ||
| 751 | |||
| 752 | return $c; | ||
| 753 | } ## end sub merge | ||
| 754 | |||
| 755 | |||
| 756 | =head2 log | ||
| 757 | |||
| 758 | Arg[1] : String $type - object type (e.g. 'gene') | ||
| 759 | Arg[2] : String $dump_path - path for writing output | ||
| 760 | Example : $gene_scores->log('gene', $conf->param('basedir')); | ||
| 761 | Description : Logs all Entries in the scoring matrix to a file. Used for | ||
| 762 | debugging. | ||
| 763 | Return type : none | ||
| 764 | Exceptions : thrown on I/0 error | ||
| 765 | Caller : general | ||
| 766 | Status : At Risk | ||
| 767 | : under development | ||
| 768 | |||
| 769 | =cut | ||
| 770 | |||
| 771 | sub log { | ||
| 772 | my $self = shift; | ||
| 773 | my $type = shift; | ||
| 774 | my $dump_path = shift; | ||
| 775 | |||
| 776 | my $debug_path = path_append($dump_path, 'debug'); | ||
| 777 | my $logfile = "$debug_path/${type}_scores.txt"; | ||
| 778 | |||
| 779 | open(my $fh, '>', $logfile) or | ||
| 780 | throw("Unable to open $logfile for writing: $!"); | ||
| 781 | |||
| 782 | foreach my $entry (@{ $self->get_all_Entries }) { | ||
| 783 | print $fh ($entry->to_string."\n"); | ||
| 784 | } | ||
| 785 | |||
| 786 | close($fh); | ||
| 787 | } | ||
| 788 | |||
| 789 | |||
| 790 | =head2 to_string | ||
| 791 | |||
| 792 | Example : print LOG $gene_scores->to_string, "\n"; | ||
| 793 | Description : Returns a string representation of the scoring matrix. This is | ||
| 794 | simply a multi-line string, where each line is a stringified | ||
| 795 | Entry. | ||
| 796 | Useful for debugging and logging. | ||
| 797 | Return type : String | ||
| 798 | Exceptions : none | ||
| 799 | Caller : general | ||
| 800 | Status : At Risk | ||
| 801 | : under development | ||
| 802 | |||
| 803 | =cut | ||
| 804 | |||
| 805 | sub to_string { | ||
| 806 | my $self = shift; | ||
| 807 | |||
| 808 | my $string = ''; | ||
| 809 | |||
| 810 | foreach my $entry (@{ $self->get_all_Entries }) { | ||
| 811 | $string .= $entry->to_string."\n"; | ||
| 812 | } | ||
| 813 | |||
| 814 | return $string; | ||
| 815 | } | ||
| 816 | |||
| 817 | |||
| 818 | 1; | ||
| 819 |