EnsEMBL/IdMapping/InternalIdMapper/EnsemblTranscriptGeneric.pm untested

filter: Statementstmt 0.0% 0 / 137 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 15 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 194 CC 12 SCAR 29.7
CC
12
Cov
0%
same_gene_transcript_mapping
56.0
non_exact_translation
6.0
mapped_gene
6.0
CRAP 19.4
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  
42 package Bio::EnsEMBL::IdMapping::InternalIdMapper::EnsemblTranscriptGeneric;
43 $Bio::EnsEMBL::IdMapping::InternalIdMapper::EnsemblTranscriptGeneric::VERSION = '114.0.0_50'; # TRIAL
44 $Bio::EnsEMBL::IdMapping::InternalIdMapper::EnsemblTranscriptGeneric::VERSION = '114.0.050';
45 use strict;
46 use warnings;
47 no warnings 'uninitialized';
48  
49 use Bio::EnsEMBL::IdMapping::InternalIdMapper::BaseMapper;
50 our @ISA = qw(Bio::EnsEMBL::IdMapping::InternalIdMapper::BaseMapper);
51  
52 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
53 use Bio::EnsEMBL::Utils::ScriptUtils qw(path_append);
54  
55
56 #
57 # basic mapping
58 #
59 sub init_basic {
60 my $self = shift;
61 my $num = shift;
62 my $tsb = shift;
63 my $mappings = shift;
64 my $transcript_scores = shift;
65  
66 $self->logger->info("Basic transcript mapping...\n", 0, 'stamped');
67  
68 $mappings = $self->basic_mapping($transcript_scores,
69 "transcript_mappings$num");
70 $num++;
71 my $new_scores = $tsb->create_shrinked_matrix($transcript_scores, $mappings,
72 "transcript_matrix$num");
73  
74 return ($new_scores, $mappings);
75 }
76  
77  
78 #
79 # handle cases with exact match but different translation
80 #
81 sub non_exact_translation {
82 my $self = shift;
83 my $num = shift;
84 my $tsb = shift;
85 my $mappings = shift;
86 my $transcript_scores = shift;
87  
88 $self->logger->info("Exact Transcript non-exact Translation...\n", 0, 'stamped');
89
90 unless ($transcript_scores->loaded) {
91 $tsb->different_translation_rescore($transcript_scores);
92 $transcript_scores->write_to_file;
93 }
94
95 $mappings = $self->basic_mapping($transcript_scores,
96 "transcript_mappings$num");
97 $num++;
98 my $new_scores = $tsb->create_shrinked_matrix($transcript_scores, $mappings,
99 "transcript_matrix$num");
100  
101 return ($new_scores, $mappings);
102 }
103  
104  
105 #
106 # reduce score for mappings of transcripts which do not belong to mapped
107 # genes
108 #
109 sub mapped_gene {
110 my $self = shift;
111 my $num = shift;
112 my $tsb = shift;
113 my $mappings = shift;
114 my $transcript_scores = shift;
115 my $gene_mappings = shift;
116  
117 $self->logger->info("Transcripts in mapped genes...\n", 0, 'stamped');
118
119 unless ($transcript_scores->loaded) {
120 $tsb->non_mapped_gene_rescore($transcript_scores, $gene_mappings);
121 $transcript_scores->write_to_file;
122 }
123
124 $mappings = $self->basic_mapping($transcript_scores,
125 "transcript_mappings$num");
126 $num++;
127 my $new_scores = $tsb->create_shrinked_matrix($transcript_scores, $mappings,
128 "transcript_matrix$num");
129  
130 return ($new_scores, $mappings);
131 }
132  
133 #
134 # rescore by penalising scores between transcripts with different biotypes
135 #
136 sub biotype {
137 my $self = shift;
138 my $num = shift;
139 my $tsb = shift;
140 my $mappings = shift;
141 my $transcript_scores = shift;
142  
143 $self->logger->info( "Retry with biotype disambiguation...\n",
144 0, 'stamped' );
145  
146 unless ( $transcript_scores->loaded() ) {
147 $tsb->biotype_transcript_rescore($transcript_scores);
148 $transcript_scores->write_to_file();
149 }
150  
151 my $new_mappings = $self->basic_mapping( $transcript_scores,
152 "transcript_mappings$num" );
153 $num++;
154 my $new_scores =
155 $tsb->create_shrinked_matrix( $transcript_scores, $new_mappings,
156 "transcript_matrix$num" );
157  
158 return ( $new_scores, $new_mappings );
159 }
160  
161 #
162 # selectively rescore by penalising scores between transcripts with
163 # different internalIDs
164 #
165 sub internal_id {
166 my $self = shift;
167 my $num = shift;
168 my $tsb = shift;
169 my $mappings = shift;
170 my $transcript_scores = shift;
171  
172 $self->logger->info("Retry with internalID disambiguation...\n", 0, 'stamped');
173
174 unless ($transcript_scores->loaded) {
175 $tsb->internal_id_rescore($transcript_scores);
176 $transcript_scores->write_to_file;
177 }
178  
179 $mappings = $self->basic_mapping($transcript_scores,
180 "transcript_mappings$num");
181 $num++;
182 my $new_scores = $tsb->create_shrinked_matrix($transcript_scores, $mappings,
183 "transcript_matrix$num");
184  
185 return ($new_scores, $mappings);
186 }
187  
188  
189 #
190 # handle ambiguities between transcripts in single genes
191 #
192 sub single_gene {
193 my $self = shift;
194 my $num = shift;
195 my $tsb = shift;
196 my $mappings = shift;
197 my $transcript_scores = shift;
198  
199 $self->logger->info("Transcripts in single genes...\n", 0, 'stamped');
200
201 unless ($transcript_scores->loaded) {
202 $transcript_scores->write_to_file;
203 }
204
205 $mappings = $self->same_gene_transcript_mapping($transcript_scores,
206 "transcript_mappings$num");
207 $num++;
208 my $new_scores = $tsb->create_shrinked_matrix($transcript_scores, $mappings,
209 "transcript_matrix$num");
210  
211 return ($new_scores, $mappings);
212 }
213  
214  
215 #
216 # modified basic mapper that maps transcripts that are ambiguous within one gene
217 #
218 sub same_gene_transcript_mapping {
219 my $self = shift;
220 my $matrix = shift;
221 my $mapping_name = shift;
222  
223 # argument checks
224 unless ($matrix and
225 $matrix->isa('Bio::EnsEMBL::IdMapping::ScoredMappingMatrix')) {
226 throw('Need a Bio::EnsEMBL::IdMapping::ScoredMappingMatrix.');
227 }
228  
229 throw('Need a name for serialising the mapping.') unless ($mapping_name);
230  
231 # Create a new MappingList object. Specify AUTO_LOAD to load serialised
232 # existing mappings if found
233 my $dump_path = path_append($self->conf->param('basedir'), 'mapping');
234
235 my $mappings = Bio::EnsEMBL::IdMapping::MappingList->new(
236 -DUMP_PATH => $dump_path,
237 -CACHE_FILE => "${mapping_name}.ser",
238 -AUTO_LOAD => 1,
239 );
240
241 # checkpoint test: return a previously stored MappingList
242 if ($mappings->loaded) {
243 $self->logger->info("Read existing mappings from ${mapping_name}.ser.\n");
244 return $mappings;
245 }
246  
247 my $sources_done = {};
248 my $targets_done = {};
249  
250 # sort scoring matrix entries by descending score
251 my @sorted_entries = sort { $b->score <=> $a->score ||
252 $a->source <=> $b->source || $a->target <=> $b->target }
253 @{ $matrix->get_all_Entries };
254  
255 while (my $entry = shift(@sorted_entries)) {
256
257 # $self->logger->debug("\nxxx4 ".$entry->to_string." ");
258  
259 # we already found a mapping for either source or target yet
260 next if ($sources_done->{$entry->source} or
261 $targets_done->{$entry->target});
262  
263 #$self->logger->debug('d');
264  
265 my $other_sources = [];
266 my $other_targets = [];
267 my %source_genes = ();
268 my %target_genes = ();
269  
270 if ($self->ambiguous_mapping($entry, $matrix, $other_sources, $other_targets)) {
271 #$self->logger->debug('a');
272  
273 $other_sources = $self->filter_sources($other_sources, $sources_done);
274 $other_targets = $self->filter_targets($other_targets, $targets_done);
275  
276 $source_genes{$self->cache->get_by_key('genes_by_transcript_id',
277 'source', $entry->source)} = 1;
278 $target_genes{$self->cache->get_by_key('genes_by_transcript_id',
279 'target', $entry->target)} = 1;
280  
281 foreach my $other_source (@{ $other_sources }) {
282 $source_genes{$self->cache->get_by_key('genes_by_transcript_id',
283 'source', $other_source)} = 1;
284 }
285
286 foreach my $other_target (@{ $other_targets }) {
287 $target_genes{$self->cache->get_by_key('genes_by_transcript_id',
288 'target', $other_target)} = 1;
289 }
290
291 # only add mapping if only one source and target gene involved
292 if (scalar(keys %source_genes) == 1 and scalar(keys %target_genes) == 1) {
293 #$self->logger->debug('O');
294 $mappings->add_Entry($entry);
295 }
296  
297 } else {
298 #$self->logger->debug('A');
299  
300 # this is the best mapping, add it
301 $mappings->add_Entry($entry);
302 }
303  
304 $sources_done->{$entry->source} = 1;
305 $targets_done->{$entry->target} = 1;
306 }
307  
308 # create checkpoint
309 $mappings->write_to_file;
310  
311 return $mappings;
312 }
313  
314  
315 1;
316