EnsEMBL/IdMapping/StableIdGenerator/EnsemblGeneric.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 12 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 161 CC 22 SCAR 46.6
CC
22
Cov
0%
calculate_version
210.0
initial_stable_id
30.0
increment_stable_id
12.0
CRAP 105.7
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::StableIdGenerator::EnsemblGeneric - default Ensembl
34 StableIdGenerator implementation
35  
36 =head1 SYNOPSIS
37  
38 # inject the confiured StableIdGenerator plugin
39 my $stable_id_generator = $conf->param('plugin_stable_id_generator');
40 inject($stable_id_generator);
41  
42 # create a new StableIdGenerator object
43 my $generator_instance = $stable_id_generator->new(
44 -LOGGER => $self->logger,
45 -CONF => $self->conf,
46 -CACHE => $self->cache
47 );
48  
49 # determine starting stable ID for new assignments
50 my $new_stable_id = $generator_instance->initial_stable_id('gene');
51  
52 # loop over genes
53 foreach my $target_gene (@all_target_genes) {
54  
55 # if the stable Id for this gene was mapped, assign it
56 if ( $mapping_exists{ $target_gene->id } ) {
57 my $source_gene = $mappings{ $target_gene->id };
58 $target_gene->stable_id( $source_gene->stable_id );
59  
60 # calculate and set version
61 my $version =
62 $generator_instance->calculate_version( $source_gene,
63 $target_gene );
64 $target_gene->version($version);
65  
66 # no mapping exists, assign a new stable Id
67 } else {
68 $target_gene->stable_id($new_stable_id);
69 $target_gene->version('1');
70  
71 # increment the stable Id (to be assigned to the next unmapped gene)
72 $new_stable_id =
73 $generator_instance->increment_stable_id($new_stable_id);
74 }
75 }
76  
77 =head1 DESCRIPTION
78  
79 This is the default implementation for a StableIdGenerator, which
80 is used by Bio::EnsEMBL::IdMapping::StableIdMapper to generate new
81 stable Ids and increment versions on mapped stable Ids. Refer to the
82 documentation in this module if you would like to implement your own
83 StableIdGenerator.
84  
85 The stable Id mapping application allows you to plugin your own
86 implementation by specifying it with the --plugin_stable_id_generator
87 configuration parameter.
88  
89 Requirements for a StableIdGenerator plugin:
90  
91 - inherit from Bio::EnsEMBL::IdMapping::BaseObject
92 - implement all methods listed in METHODS below (see method POD for
93 signatures)
94  
95 =head1 METHODS
96  
97 initial_stable_id
98 increment_stable_id
99 calculate_version
100  
101 =cut
102  
103 package Bio::EnsEMBL::IdMapping::StableIdGenerator::EnsemblGeneric;
104 $Bio::EnsEMBL::IdMapping::StableIdGenerator::EnsemblGeneric::VERSION = '114.0.0_50'; # TRIAL
105 $Bio::EnsEMBL::IdMapping::StableIdGenerator::EnsemblGeneric::VERSION = '114.0.050';
106 use strict;
107 use warnings;
108 no warnings 'uninitialized';
109  
110 use Bio::EnsEMBL::IdMapping::BaseObject;
111 our @ISA = qw(Bio::EnsEMBL::IdMapping::BaseObject);
112  
113 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
114  
115  
116 =head2 initial_stable_id
117  
118 Arg[1] : String $type - an entity type (gene|transcript|translation|exon)
119 Example : my $new_stable_id = $generator->initial_stable_id('gene');
120 Description : Determine the initial stable Id to use for new assignments. This
121 method is called once at the beginning of stable Id mapping.
122 Return type : String - a stable Id of appropriate type
123 Exceptions : none
124 Caller : Bio::EnsEMBL::IdMapping::StableIdMapper::map_stable_ids()
125 Status : At Risk
126 : under development
127  
128 =cut
129  
130 sub initial_stable_id {
131 my ( $self, $type ) = @_;
132  
133 my $init_stable_id;
134  
135 # Use stable ID from configuration if set.
136 $init_stable_id = $self->conf->param("starting_${type}_stable_id");
137 if ( defined($init_stable_id) ) {
138 $self->logger->debug( "Using pre-configured $init_stable_id " .
139 "as base for new $type stable IDs.\n" );
140 return $init_stable_id;
141 }
142  
143 my $s_dba = $self->cache->get_DBAdaptor('source');
144 my $s_dbh = $s_dba->dbc->db_handle;
145  
146 # look in the ${type} table first
147 my $sql = qq(
148 SELECT MAX(stable_id)
149 FROM ${type}
150 WHERE stable_id LIKE "ENS%"
151 OR stable_id LIKE "ASMPATCH%"
152 OR stable_id LIKE "BRAKER%"
153 );
154  
155 $init_stable_id = $self->fetch_value_from_db( $s_dbh, $sql );
156  
157 # Also look in gene_archive to make sure there are no larger IDs
158 # there.
159 if ( $type ne 'exon' ) {
160 $sql = qq(SELECT MAX(${type}_stable_id) FROM gene_archive);
161 my $archived_stable_id = $self->fetch_value_from_db( $s_dbh, $sql );
162 if ( $archived_stable_id &&
163 $self->is_valid($archived_stable_id) &&
164 ( $archived_stable_id gt $init_stable_id ) )
165 {
166 $init_stable_id = $archived_stable_id;
167 }
168 }
169  
170 if ( defined($init_stable_id) ) {
171 # Since $init_stable_id now is the highest existing stable ID for
172 # this object type, we need to increment it to find the first one we
173 # want to use for new assignments.
174 $init_stable_id = $self->increment_stable_id($init_stable_id);
175  
176 $self->logger->debug(
177 "Using $init_stable_id as base for new $type stable IDs.\n");
178  
179 }
180 else {
181 $self->logger->warning(
182 "Can't find highest ${type}_stable_id in source db.\n" );
183 }
184  
185 return $init_stable_id;
186 } ## end sub initial_stable_id
187  
188  
189 =head2 increment_stable_id
190  
191 Arg[1] : String $stable_id - the stable Id to increment
192 Example : $next_stable_id = $generator->increment_stable_id(
193 $current_stable_id);
194 Description : Increments the stable Id used for new assigments. This method is
195 called after each new stable Id assigment to generate the next
196 stable Id to be used.
197 Return type : String - the next new stable Id
198 Exceptions : thrown on missing or malformed argument
199 Caller : Bio::EnsEMBL::IdMapping::StableIdMapper::map_stable_ids()
200 Status : At Risk
201 : under development
202  
203 =cut
204  
205 sub increment_stable_id {
206 my $self = shift;
207 my $stable_id = shift;
208  
209 if ( !$self->is_valid($stable_id) ) {
210 throw( sprintf( "Unknown or missing stable ID '%s'", $stable_id ) );
211 }
212  
213 if ( $stable_id =~ /^LRG/ ) {
214 throw( sprintf( "We do not increment LRG genes... (got '%s'). "
215 . "Something's wrong.",
216 $stable_id ) );
217 }
218  
219 $stable_id =~ /^(ENS|ASMPATCH|BRAKER)([A-Z]+)(\d+)$/;
220  
221 my $number = $3;
222 my $new_stable_id = $1 . $2 . ( ++$number );
223  
224 return $new_stable_id;
225 }
226  
227  
228 =head2 is_valid
229  
230 Arg[1] : String $stable_id - the stable Id to check
231 Example : unless ($generator->is_valid($stable_id)) {
232 die "Invalid stable Id: $stable_id.\n";
233 }
234 Description : Tests a stable Id to be valid (according to the Ensembl stable
235 Id format definition).
236 Return type : Boolean - TRUE if valid, FALSE otherwise
237 Exceptions : none
238 Caller : general
239 Status : At Risk
240 : under development
241  
242 =cut
243  
244 sub is_valid {
245 my ( $self, $stable_id ) = @_;
246  
247 if ( defined($stable_id) ) {
248 if ( $stable_id =~ /^(ENS|ASMPATCH|BRAKER)([A-Z]+)(\d+)$/
249 || $stable_id =~ /^LRG/ )
250 {
251 return 1;
252 }
253 }
254  
255 return 0;
256 }
257  
258  
259 =head2 calculate_version
260  
261 Arg[1] : Bio::EnsEMBL::IdMapping::TinyFeature $s_obj - source object
262 Arg[2] : Bio::EnsEMBL::IdMapping::TinyFeature $t_obj - target object
263 Example : my $version = $generator->calculate_version($source_gene,
264 $target_gene);
265 $target_gene->version($version);
266 Description : Determines the version for a mapped stable Id. For Ensembl
267 genes, the rules for incrementing the version number are:
268 - exons: if exon sequence changed
269 - transcript: if spliced exon sequence changed or if number of exons changed
270 - translation: if translated sequence changed
271 - gene: if any of its transcript changed
272 Return type : String - the version to be used
273 Exceptions : thrown on wrong argument
274 Caller : Bio::EnsEMBL::IdMapping::StableIdMapper::map_stable_ids()
275 Status : At Risk
276 : under development
277  
278 =cut
279  
280 sub calculate_version {
281 my ( $self, $s_obj, $t_obj ) = @_;
282  
283 my $version = $s_obj->version();
284  
285 if ( $s_obj->isa('Bio::EnsEMBL::IdMapping::TinyExon') ) {
286 # increment version if sequence changed
287 if ( $s_obj->seq() ne $t_obj->seq() ) { ++$version }
288 }
289 elsif ( $s_obj->isa('Bio::EnsEMBL::IdMapping::TinyTranscript') ) {
290 my $change = 0;
291 # increment version if spliced exon sequence changed
292 if ( $s_obj->seq_md5_sum() ne $t_obj->seq_md5_sum() ) { $change = 1 }
293  
294 # Look for changes in exon version
295 my $source_exon_string;
296 my $target_exon_string;
297 foreach my $exon (@{ $s_obj->get_all_Exons() } ) {
298 $source_exon_string .= $exon->start();
299 $source_exon_string .= $exon->end();
300 }
301 foreach my $exon (@{ $t_obj->get_all_Exons() } ) {
302 $target_exon_string .= $exon->start();
303 $target_exon_string .= $exon->end();
304 }
305  
306 if ($source_exon_string ne $target_exon_string) { $change = 1; }
307  
308 # increment version if translation sequence changed
309 # Can happen if Havana move initiation start site or stop codon
310 if ($s_obj->translation and $t_obj->translation) {
311 if ($s_obj->translation->seq ne $t_obj->translation->seq) { $change = 1; }
312 }
313  
314 # Look for changes on the region
315 if ( $s_obj->seq_region_name() ne $t_obj->seq_region_name() ) { $change = 1 }
316  
317 if ($change) { ++$version }
318  
319 }
320 elsif ( $s_obj->isa('Bio::EnsEMBL::IdMapping::TinyTranslation') ) {
321 # increment version if transcript or translation sequences changed
322 if ( $s_obj->seq() ne $t_obj->seq() ) { ++$version }
323 }
324 elsif ( $s_obj->isa('Bio::EnsEMBL::IdMapping::TinyGene') ) {
325 # increment version if any transcript changed
326  
327 my $s_tr_ident = join(
328 ":",
329 map { $_->stable_id() . '.' . $_->version() } sort {
330 $a->stable_id() cmp $b->stable_id()
331 } @{ $s_obj->get_all_Transcripts() } );
332 my $t_tr_ident = join(
333 ":",
334 map { $_->stable_id() . '.' . $_->version() } sort {
335 $a->stable_id() cmp $b->stable_id()
336 } @{ $t_obj->get_all_Transcripts() } );
337  
338 if ( $s_tr_ident ne $t_tr_ident ) { ++$version }
339 }
340 else {
341 throw( "Unknown object type: " . ref($s_obj) );
342 }
343  
344 return $version;
345 } ## end sub calculate_version
346  
347  
348 1;
349