EnsEMBL/Utils/RNAProductTypeMapper.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 78 CC 6 SCAR 17.7
CC
6
Cov
0%
class_to_type_code
12.0
mapper
6.0
class_attribute_cache_map
6.0
CRAP 5.9
1  
2 =head1 LICENSE
3  
4 See the NOTICE file distributed with this work for additional information
5 regarding copyright ownership.
6  
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10  
11 http://www.apache.org/licenses/LICENSE-2.0
12  
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18  
19 =cut
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::Utils::RNAProductTypeMapper - Utility class for mapping
34 between RNA-product types used in the Ensembl database and respective
35 Perl API classes.
36  
37 =head1 DESCRIPTION
38  
39 The purpose of this class is to hide from the users the mappings
40 between classes representing various mature RNA products
41 (e.g. generic, miRNA, circRNA etc.) and their respective identifiers
42 in the Ensembl database. In principle there should be no need for
43 users to call the mapper directly; it is invoked internally whenever
44 such mappings are needed (e.g. in RNAProduct constructor or in
45 RNAProductAdaptor).
46  
47 Note that the type_code<->class_name mappings are hardcoded here,
48 specifically in the constructor, instead of being fetched from the
49 database. This is so that it is not possible for someone to trigger
50 construction of arbitrary objects by having modified class names
51 stored in the database.
52  
53 =head1 SYNOPSIS
54  
55 my $rpt_mapper = Bio::EnsEMBL::Utils::RNAProductTypeMapper->new();
56  
57 my $class_name = $rpt_mapper->type_code_to_class( 'generic' );
58 my $type_code = $rpt_mapper->class_to_type_code( 'Bio::EnsEMBL::MicroRNA' );
59  
60 =cut
61  
62 package Bio::EnsEMBL::Utils::RNAProductTypeMapper;
63 $Bio::EnsEMBL::Utils::RNAProductTypeMapper::VERSION = '114.0.0_50'; # TRIAL
64 $Bio::EnsEMBL::Utils::RNAProductTypeMapper::VERSION = '114.0.050';
65 use strict;
66 use warnings;
67  
68 use Bio::EnsEMBL::Utils::Exception qw( throw warning );
69  
70  
71  
72 my $type_mapper;
73  
74  
75 =head2 mapper
76  
77 Example : my $mapper = Bio::EnsEMBL::Utils::RNAProductTypeMapper->mapper();
78 Description: Retrieves an instance of RNAProductTypeMapper from its module,
79 reusing an existing one should it exist.
80 Returntype : Bio::EnsEMBL::Utils::RNAProductTypeMapper
81 Exceptions : none
82 Caller : internal
83 Status : Stable
84  
85 =cut
86  
87 sub mapper {
88 if ( !defined($type_mapper) ) {
89 $type_mapper = Bio::EnsEMBL::Utils::RNAProductTypeMapper->new();
90 }
91 return $type_mapper;
92 }
93  
94  
95 =head2 new
96  
97 Example : my $mapper = Bio::EnsEMBL::Utils::RNAProductTypeMapper->new();
98 Description: Constructor. Creates a new RNAProductTypeMapper object.
99 Not particularly useful for end users because all
100 RNAProductTypeMapper objects are identical; use mapper()
101 instead.
102 Returntype : Bio::EnsEMBL::Utils::RNAProductTypeMapper
103 Exceptions : none
104 Caller : internal
105 Status : Stable
106  
107 =cut
108  
109 sub new {
110 my ( $caller ) = @_;
111 my $class = ref( $caller ) || $caller;
112  
113 # Declare this here rather than in the package scope so that the map
114 # cannot be modified (we do not presently use Readonly in the Core
115 # API code), accidentally or otherwise.
116 my $class_attribute_cache_map = {
117 'Bio::EnsEMBL::RNAProduct' => { },
118 'Bio::EnsEMBL::MicroRNA' => {
119 'arm' => 'mirna_arm',
120 },
121 };
122 my $type_to_class_map = {
123 'generic' => 'Bio::EnsEMBL::RNAProduct',
124 'miRNA' => 'Bio::EnsEMBL::MicroRNA',
125 };
126  
127 my $self = bless {
128 'class_attribute_cache_map' => $class_attribute_cache_map,
129 'type_to_class_map' => $type_to_class_map,
130 'class_to_type_map' => undef,
131 }, $class;
132  
133 return $self;
134 }
135  
136  
137 =head2 class_attribute_cache_map
138  
139 Arg [1] : string $class_name - fully qualified RNA-product class name
140 Example : my $attr_cache_map
141 = $mapper->class_attribute_cache_map( 'Bio::EnsEMBL::MicroRNA' );
142 Description: For the given name of a class representing a mature RNA
143 product, returns the map indicating which local members variables
144 should be synchronised with which Attributes.
145 Returntype : hashref
146 Exceptions : throw if the class does not represent known RNA-product type
147 Caller : internal
148 Status : Stable
149  
150 =cut
151  
152 sub class_attribute_cache_map {
153 my ( $self, $class_name ) = @_;
154  
155 my %map = %{ $self->{'class_attribute_cache_map'} };
156 if ( !exists $map{$class_name} ) {
157 throw( "Unknown RNA-product class name " . $class_name );
158 }
159  
160 return $map{$class_name};
161 }
162  
163  
164 =head2 class_to_type_code
165  
166 Arg [1] : string $class_name - fully qualified RNA-product class name
167 Example : my $type_code
168 = $mapper->class_to_type_code( 'Bio::EnsEMBL::MicroRNA' );
169 Description: For the given name of a class representing a mature RNA
170 product, returns the type code used to represent it in the
171 Ensembl database.
172 Returntype : string
173 Exceptions : throw if the class does not represent known RNA-product type
174 Caller : internal
175 Status : Stable
176  
177 =cut
178  
179 sub class_to_type_code {
180 my ( $self, $class_name ) = @_;
181  
182 if ( !defined( $self->{'class_to_type_map'} ) ) {
183 $self->_generate_reverse_map();
184 }
185  
186 my %map = %{ $self->{'class_to_type_map'} };
187 if ( !exists $map{$class_name} ) {
188 throw( "Unknown RNA-product class name " . $class_name );
189 }
190  
191 return $map{$class_name};
192 }
193  
194  
195 =head2 type_code_to_class
196  
197 Arg [1] : string $type_code - type code of RNA product
198 Example : my $class_name = $mapper->class_to_type_code( 1 );
199 Description: For the type code representing a mature RNA product in the
200 Ensembl database, return its API class name
201 Returntype : string
202 Exceptions : throw if the code does not represent known RNA-product type
203 Caller : internal
204 Status : Stable
205  
206 =cut
207  
208 sub type_code_to_class {
209 my ( $self, $type_code ) = @_;
210  
211 my %map = %{ $self->{'type_to_class_map'} };
212 if ( !exists $map{$type_code} ) {
213 throw( "Unknown RNA-product type ID " . $type_code );
214 }
215  
216 return $map{$type_code};
217 }
218  
219  
220 =head2 _generate_reverse_map
221  
222 Description: PRIVATE generates class_name->type_code map from the
223 type_code->class_name one.
224 Returntype : none
225 Exceptions : none
226 Caller : internal
227 Status : Stable
228  
229 =cut
230  
231 sub _generate_reverse_map {
232 my ($self) = @_;
233  
234 # Safe to use reverse because both keys and values are unique
235 my %reversed_map = reverse %{$self->{'type_to_class_map'}};
236 $self->{'class_to_type_map'} = \%reversed_map;
237  
238 return;
239 }
240  
241  
242 1;