| 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 | =head1 METHODS | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | package Bio::EnsEMBL::IdMapping::InternalIdMapper::EnsemblExonGeneric; | ||
| 42 | $Bio::EnsEMBL::IdMapping::InternalIdMapper::EnsemblExonGeneric::VERSION = '114.0.0_50'; # TRIAL | ||
| 43 | $Bio::EnsEMBL::IdMapping::InternalIdMapper::EnsemblExonGeneric::VERSION = '114.0.050'; | ||
| 44 | use strict; | ||
| 45 | use warnings; | ||
| 46 | no warnings 'uninitialized'; | ||
| 47 | |||
| 48 | use Bio::EnsEMBL::IdMapping::InternalIdMapper::BaseMapper; | ||
| 49 | our @ISA = qw(Bio::EnsEMBL::IdMapping::InternalIdMapper::BaseMapper); | ||
| 50 | |||
| 51 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 52 | use Bio::EnsEMBL::Utils::ScriptUtils qw(path_append); | ||
| 53 | |||
| 54 | |||
| 55 | # | ||
| 56 | # basic mapping | ||
| 57 | # | ||
| 58 | sub init_basic { | ||
| 59 | my $self = shift; | ||
| 60 | my $num = shift; | ||
| 61 | my $esb = shift; | ||
| 62 | my $mappings = shift; | ||
| 63 | my $exon_scores = shift; | ||
| 64 | |||
| 65 | $self->logger->info("Basic exon mapping...\n", 0, 'stamped'); | ||
| 66 | |||
| 67 | $mappings = $self->basic_mapping($exon_scores, "exon_mappings$num"); | ||
| 68 | $num++; | ||
| 69 | my $new_scores = $esb->create_shrinked_matrix($exon_scores, $mappings, | ||
| 70 | "exon_matrix$num"); | ||
| 71 | |||
| 72 | return ($new_scores, $mappings); | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | # | ||
| 77 | # reduce score for mappings of exons which do not belong to mapped | ||
| 78 | # transcripts | ||
| 79 | # (ie where source exon transcript does not map target exon transcript) | ||
| 80 | # | ||
| 81 | sub mapped_transcript { | ||
| 82 | my $self = shift; | ||
| 83 | my $num = shift; | ||
| 84 | my $esb = shift; | ||
| 85 | my $mappings = shift; | ||
| 86 | my $exon_scores = shift; | ||
| 87 | my $transcript_mappings = shift; | ||
| 88 | |||
| 89 | $self->logger->info("Exons in mapped transcript...\n", 0, 'stamped'); | ||
| 90 | |||
| 91 | unless ($exon_scores->loaded) { | ||
| 92 | $esb->non_mapped_transcript_rescore($exon_scores, $transcript_mappings); | ||
| 93 | $exon_scores->write_to_file; | ||
| 94 | } | ||
| 95 | |||
| 96 | $mappings = $self->basic_mapping($exon_scores, "exon_mappings$num"); | ||
| 97 | $num++; | ||
| 98 | my $new_scores = $esb->create_shrinked_matrix($exon_scores, $mappings, | ||
| 99 | "exon_matrix$num"); | ||
| 100 | |||
| 101 | return ($new_scores, $mappings); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub single_transcript { | ||
| 105 | my $self = shift; | ||
| 106 | my $num = shift; | ||
| 107 | my $esb = shift; | ||
| 108 | my $mappings = shift; | ||
| 109 | my $exon_scores = shift; | ||
| 110 | |||
| 111 | $self->logger->info("Exons in single transcript...\n", 0, 'stamped'); | ||
| 112 | |||
| 113 | unless ($exon_scores->loaded) { | ||
| 114 | $exon_scores->write_to_file; | ||
| 115 | } | ||
| 116 | |||
| 117 | $mappings = $self->same_transcript_exon_mapping($exon_scores, "exon_mappings$num"); | ||
| 118 | $num++; | ||
| 119 | my $new_scores = $esb->create_shrinked_matrix($exon_scores, $mappings, | ||
| 120 | "exon_matrix$num"); | ||
| 121 | |||
| 122 | return ($new_scores, $mappings); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub same_transcript_exon_mapping { | ||
| 126 | my $self = shift; | ||
| 127 | my $matrix = shift; | ||
| 128 | my $mapping_name = shift; | ||
| 129 | |||
| 130 | # argument checks | ||
| 131 | unless ($matrix and | ||
| 132 | $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) { | ||
| 133 | throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.'); | ||
| 134 | } | ||
| 135 | |||
| 136 | throw('Need a name for serialising the mapping.') unless ($mapping_name); | ||
| 137 | |||
| 138 | # Create a new MappingList object. Specify AUTO_LOAD to load serialised | ||
| 139 | # existing mappings if found | ||
| 140 | my $dump_path = path_append($self->conf->param('basedir'), 'mapping'); | ||
| 141 | |||
| 142 | my $mappings = Bio::EnsEMBL::IdMapping::MappingList->new( | ||
| 143 | -DUMP_PATH => $dump_path, | ||
| 144 | -CACHE_FILE => "${mapping_name}.ser", | ||
| 145 | -AUTO_LOAD => 1, | ||
| 146 | ); | ||
| 147 | |||
| 148 | # checkpoint test: return a previously stored MappingList | ||
| 149 | if ($mappings->loaded) { | ||
| 150 | $self->logger->info("Read existing mappings from ${mapping_name}.ser.\n"); | ||
| 151 | return $mappings; | ||
| 152 | } | ||
| 153 | |||
| 154 | my $sources_done = {}; | ||
| 155 | my $targets_done = {}; | ||
| 156 | |||
| 157 | # sort scoring matrix entries by descending score | ||
| 158 | my @sorted_entries = sort { $b->score <=> $a->score || | ||
| 159 | $a->source <=> $b->source || $a->target <=> $b->target } | ||
| 160 | @{ $matrix->get_all_Entries }; | ||
| 161 | |||
| 162 | while (my $entry = shift(@sorted_entries)) { | ||
| 163 | |||
| 164 | # $self->logger->debug("\nxxx4 ".$entry->to_string." "); | ||
| 165 | |||
| 166 | # we already found a mapping for either source or target yet | ||
| 167 | next if ($sources_done->{$entry->source} or | ||
| 168 | $targets_done->{$entry->target}); | ||
| 169 | |||
| 170 | #$self->logger->debug('d'); | ||
| 171 | |||
| 172 | my $other_sources = []; | ||
| 173 | my $other_targets = []; | ||
| 174 | my %source_transcripts = (); | ||
| 175 | my %target_transcripts = (); | ||
| 176 | |||
| 177 | if ($self->ambiguous_mapping($entry, $matrix, $other_sources, $other_targets)) { | ||
| 178 | #$self->logger->debug('a'); | ||
| 179 | |||
| 180 | $other_sources = $self->filter_sources($other_sources, $sources_done); | ||
| 181 | $other_targets = $self->filter_targets($other_targets, $targets_done); | ||
| 182 | |||
| 183 | $source_transcripts{$self->cache->get_by_key('transcripts_by_exon_id', | ||
| 184 | 'source', $entry->source)} = 1; | ||
| 185 | $target_transcripts{$self->cache->get_by_key('transcripts_by_exon_id', | ||
| 186 | 'target', $entry->target)} = 1; | ||
| 187 | |||
| 188 | foreach my $other_source (@{ $other_sources }) { | ||
| 189 | $source_transcripts{$self->cache->get_by_key('transcripts_by_exon_id', | ||
| 190 | 'source', $other_source)} = 1; | ||
| 191 | } | ||
| 192 | |||
| 193 | foreach my $other_target (@{ $other_targets }) { | ||
| 194 | $target_transcripts{$self->cache->get_by_key('transcripts_by_exon_id', | ||
| 195 | 'target', $other_target)} = 1; | ||
| 196 | } | ||
| 197 | |||
| 198 | # only add mapping if only one source and target gene involved | ||
| 199 | if (scalar(keys %source_transcripts) == 1 and scalar(keys %target_transcripts) == 1) { | ||
| 200 | #$self->logger->debug('O'); | ||
| 201 | $mappings->add_Entry($entry); | ||
| 202 | } | ||
| 203 | |||
| 204 | } else { | ||
| 205 | #$self->logger->debug('A'); | ||
| 206 | |||
| 207 | # this is the best mapping, add it | ||
| 208 | $mappings->add_Entry($entry); | ||
| 209 | } | ||
| 210 | |||
| 211 | $sources_done->{$entry->source} = 1; | ||
| 212 | $targets_done->{$entry->target} = 1; | ||
| 213 | } | ||
| 214 | |||
| 215 | # create checkpoint | ||
| 216 | $mappings->write_to_file; | ||
| 217 | |||
| 218 | return $mappings; | ||
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | # | ||
| 223 | # selectively rescore by penalising scores between exons with | ||
| 224 | # different internalIDs | ||
| 225 | # | ||
| 226 | sub internal_id { | ||
| 227 | my $self = shift; | ||
| 228 | my $num = shift; | ||
| 229 | my $esb = shift; | ||
| 230 | my $mappings = shift; | ||
| 231 | my $exon_scores = shift; | ||
| 232 | |||
| 233 | $self->logger->info( "Retry with internalID disambiguation...\n", | ||
| 234 | 0, 'stamped' ); | ||
| 235 | |||
| 236 | if ( !$exon_scores->loaded() ) { | ||
| 237 | $esb->internal_id_rescore($exon_scores); | ||
| 238 | $exon_scores->write_to_file(); | ||
| 239 | } | ||
| 240 | |||
| 241 | $mappings = $self->basic_mapping( $exon_scores, "exon_mappings$num" ); | ||
| 242 | $num++; | ||
| 243 | my $new_scores = | ||
| 244 | $esb->create_shrinked_matrix( $exon_scores, $mappings, | ||
| 245 | "exon_matrix$num" ); | ||
| 246 | |||
| 247 | return ( $new_scores, $mappings ); | ||
| 248 | } | ||
| 249 | |||
| 250 | |||
| 251 | 1; | ||
| 252 |