EnsEMBL/Utils/RNAProductTypeMapper.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 79 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';
64 use strict;
65 use warnings;
66  
67 use Bio::EnsEMBL::Utils::Exception qw( throw warning );
68  
69  
70  
71 my $type_mapper;
72  
73  
74 =head2 mapper
75  
76 Example : my $mapper = Bio::EnsEMBL::Utils::RNAProductTypeMapper->mapper();
77 Description: Retrieves an instance of RNAProductTypeMapper from its module,
78 reusing an existing one should it exist.
79 Returntype : Bio::EnsEMBL::Utils::RNAProductTypeMapper
80 Exceptions : none
81 Caller : internal
82 Status : Stable
83  
84 =cut
85  
86 sub mapper {
87 if ( !defined($type_mapper) ) {
88 $type_mapper = Bio::EnsEMBL::Utils::RNAProductTypeMapper->new();
89 }
90 return $type_mapper;
91 }
92  
93  
94 =head2 new
95  
96 Example : my $mapper = Bio::EnsEMBL::Utils::RNAProductTypeMapper->new();
97 Description: Constructor. Creates a new RNAProductTypeMapper object.
98 Not particularly useful for end users because all
99 RNAProductTypeMapper objects are identical; use mapper()
100 instead.
101 Returntype : Bio::EnsEMBL::Utils::RNAProductTypeMapper
102 Exceptions : none
103 Caller : internal
104 Status : Stable
105  
106 =cut
107  
108 sub new {
109 my ( $caller ) = @_;
110 my $class = ref( $caller ) || $caller;
111  
112 # Declare this here rather than in the package scope so that the map
113 # cannot be modified (we do not presently use Readonly in the Core
114 # API code), accidentally or otherwise.
115 my $class_attribute_cache_map = {
116 'Bio::EnsEMBL::RNAProduct' => { },
117 'Bio::EnsEMBL::MicroRNA' => {
118 'arm' => 'mirna_arm',
119 },
120 };
121 my $type_to_class_map = {
122 'generic' => 'Bio::EnsEMBL::RNAProduct',
123 'miRNA' => 'Bio::EnsEMBL::MicroRNA',
124 };
125  
126 my $self = bless {
127 'class_attribute_cache_map' => $class_attribute_cache_map,
128 'type_to_class_map' => $type_to_class_map,
129 'class_to_type_map' => undef,
130 }, $class;
131  
132 return $self;
133 }
134  
135  
136 =head2 class_attribute_cache_map
137  
138 Arg [1] : string $class_name - fully qualified RNA-product class name
139 Example : my $attr_cache_map
140 = $mapper->class_attribute_cache_map( 'Bio::EnsEMBL::MicroRNA' );
141 Description: For the given name of a class representing a mature RNA
142 product, returns the map indicating which local members variables
143 should be synchronised with which Attributes.
144 Returntype : hashref
145 Exceptions : throw if the class does not represent known RNA-product type
146 Caller : internal
147 Status : Stable
148  
149 =cut
150  
151 sub class_attribute_cache_map {
152 my ( $self, $class_name ) = @_;
153  
154 my %map = %{ $self->{'class_attribute_cache_map'} };
155 if ( !exists $map{$class_name} ) {
156 throw( "Unknown RNA-product class name " . $class_name );
157 }
158  
159 return $map{$class_name};
160 }
161  
162  
163 =head2 class_to_type_code
164  
165 Arg [1] : string $class_name - fully qualified RNA-product class name
166 Example : my $type_code
167 = $mapper->class_to_type_code( 'Bio::EnsEMBL::MicroRNA' );
168 Description: For the given name of a class representing a mature RNA
169 product, returns the type code used to represent it in the
170 Ensembl database.
171 Returntype : string
172 Exceptions : throw if the class does not represent known RNA-product type
173 Caller : internal
174 Status : Stable
175  
176 =cut
177  
178 sub class_to_type_code {
179 my ( $self, $class_name ) = @_;
180  
181 if ( !defined( $self->{'class_to_type_map'} ) ) {
182 $self->_generate_reverse_map();
183 }
184  
185 my %map = %{ $self->{'class_to_type_map'} };
186 if ( !exists $map{$class_name} ) {
187 throw( "Unknown RNA-product class name " . $class_name );
188 }
189  
190 return $map{$class_name};
191 }
192  
193  
194 =head2 type_code_to_class
195  
196 Arg [1] : string $type_code - type code of RNA product
197 Example : my $class_name = $mapper->class_to_type_code( 1 );
198 Description: For the type code representing a mature RNA product in the
199 Ensembl database, return its API class name
200 Returntype : string
201 Exceptions : throw if the code does not represent known RNA-product type
202 Caller : internal
203 Status : Stable
204  
205 =cut
206  
207 sub type_code_to_class {
208 my ( $self, $type_code ) = @_;
209  
210 my %map = %{ $self->{'type_to_class_map'} };
211 if ( !exists $map{$type_code} ) {
212 throw( "Unknown RNA-product type ID " . $type_code );
213 }
214  
215 return $map{$type_code};
216 }
217  
218  
219 =head2 _generate_reverse_map
220  
221 Description: PRIVATE generates class_name->type_code map from the
222 type_code->class_name one.
223 Returntype : none
224 Exceptions : none
225 Caller : internal
226 Status : Stable
227  
228 =cut
229  
230 sub _generate_reverse_map {
231 my ($self) = @_;
232  
233 # Safe to use reverse because both keys and values are unique
234 my %reversed_map = reverse %{$self->{'type_to_class_map'}};
235 $self->{'class_to_type_map'} = \%reversed_map;
236  
237 return;
238 }
239  
240  
241 1;