EnsEMBL/Utils/Converter.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 114 CC 11 SCAR 25.4
CC
11
Cov
0%
_guess_module
20.0
_load_module
20.0
new
12.0
CRAP 12.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 =head1 AUTHOR
30  
31 Juguang Xiao <juguang@tll.org.sg>
32  
33 =cut
34  
35 =head1 NAME
36  
37 Bio::EnsEMBL::Utils::Converter, a converter factory
38  
39 =head1 SYNOPSIS
40  
41 my $converter = Bio::EnsEMBL::Utils::Converter->new(
42 -in => 'Bio::SeqFeature::Generic',
43 -out => 'Bio::EnsEMBL::SimpleFeature'
44 );
45  
46 my ( $fearture1, $feature2 );
47 my $ens_simple_features =
48 $converter->convert( [ $feature1, $feature2 ] );
49 my @ens_simple_features = @{$ens_simple_features};
50  
51 =head1 DESCRIPTION
52  
53 Module to converter the business objects between EnsEMBL and any other
54 projects, currently BioPerl.
55  
56 What the ready conversions are,
57  
58 Bio::SeqFeature::Generic <-> Bio::EnsEMBL::SeqFeature, Bio::EnsEMBL::SimpleFeature
59 Bio::SeqFeature::FeaturePair <-> Bio::EnsEMBL::SeqFeature, Bio::EnsEMBL::RepeatFeature
60 Bio::Search::HSP::GenericHSP -> Bio::EnsEMBL::BaseAlignFeature's submodules
61 Bio::Tools::Prediction::Gene -> Bio::EnsEMBL::PredictionTranscript
62 Bio::Tools::Prediction::Exon -> Bio::EnsEMBL::Exon
63 Bio::Pipeline::Analysis -> Bio::EnsEMBL::Analysis
64  
65 =head1 METHODS
66  
67 =cut
68  
69  
70 package Bio::EnsEMBL::Utils::Converter;
71 $Bio::EnsEMBL::Utils::Converter::VERSION = '114.0.0_50'; # TRIAL
72 $Bio::EnsEMBL::Utils::Converter::VERSION = '114.0.050';
73 use strict;
74  
75 =head2 new
76  
77 Title : new
78 Usage :
79 my $converter = Bio::EnsEMBL::Utils::Converter->new(
80 -in => 'Bio::SeqFeature::Generic',
81 -out => 'Bio::EnsEMBL::SimpleFeature'
82 );
83  
84 Function: constructor for converter object
85 Returns : L<Bio::EnsEMBL::Utils::Converter>
86 Args :
87 in - the module name of the input.
88 out - the module name of the output.
89 analysis - a Bio::EnsEMBL::Analysis object, if converting other objects to EnsEMBL features.
90 contig - a Bio::EnsEMBL::RawContig object, if converting other objects to EnsEMBL features.
91  
92 =cut
93  
94 sub new {
95 my ($caller, @args) = @_;
96 my $class = ref($caller) || $caller;
97
98 if($class =~ /Bio::EnsEMBL::Utils::Converter::(\S+)/){
99 my $self = $class->SUPER::new(@args);
100 $self->_initialize(@args);
101 return $self;
102 }else{
103 my %params = @args;
104 @params{map {lc $_} keys %params} = values %params;
105 my $module = $class->_guess_module($params{-in}, $params{-out});
106  
107 return undef unless($class->_load_module($module));
108 return "$module"->new(@args);
109 }
110 }
111  
112 # This would be invoked by sub-module's _initialize.
113  
114 sub _initialize {
115 my ($self, @args) = @_;
116
117 my ($in, $out) = $self->_rearrange([qw(IN OUT)], @args);
118  
119 $self->in($in);
120 $self->out($out);
121 }
122  
123 =head2 _guess_module
124
125 Usage : $module = $class->_guess_module(
126 'Bio::EnsEMBL::SimpleFeature',
127 'Bio::EnsEMBL::Generic'
128 );
129  
130 =cut
131  
132 sub _guess_module {
133 my ($self, $in, $out) = @_;
134 if($in =~ /^Bio::EnsEMBL::(\S+)/ and $out =~ /^Bio::EnsEMBL::(\S+)/){
135 $self->throw("Cannot convert between EnsEMBL objects.\n[$in] to [$out]");
136 }elsif($in =~ /^Bio::EnsEMBL::(\S+)/){
137 return 'Bio::EnsEMBL::Utils::Converter::ens_bio';
138 }elsif($out =~ /^Bio::EnsEMBL::(\S+)/){
139 return 'Bio::EnsEMBL::Utils::Converter::bio_ens';
140 }else{
141 $self->throw("Cannot convert between non-EnsEMBL objects.\n[$in] to [$out]");
142 }
143 }
144  
145 =head2 convert
146
147 Title : convert
148 Usage : my $array_ref = $converter->convert(\@input);
149 Function: does the actual conversion
150 Returns : an array ref of converted objects
151 Args : an array ref of converting objects
152  
153 =cut
154  
155 sub convert{
156 my ($self, $input) = @_;
157  
158 $input || $self->throw("Need a ref of array of input objects to convert");
159
160 my $output_module = $self->out;
161 $self->throw("Cannot load [$output_module] perl module")
162 unless $self->_load_module($output_module);
163  
164 unless(ref($input) eq 'ARRAY'){
165 $self->warn("The input is supposed to be an array ref");
166 return $self->_convert_single($input);
167 }
168  
169 my @output = ();
170 foreach(@{$input}){
171 push(@output, $self->_convert_single($_));
172 }
173  
174 return \@output;
175 }
176  
177 sub _convert_single{
178 shift->throw("Not implemented. Please check the instance subclass");
179 }
180  
181 foreach my $field (qw(in out)){
182 my $slot=__PACKAGE__ ."::$field";
183 no strict 'refs'; ## no critic
184 *$field=sub{
185 my $self=shift;
186 $self->{$slot}=shift if @_;
187 return $self->{$slot};
188 };
189 }
190  
191 =head2 _load_module
192
193 This method is copied from Bio::Root::Root
194  
195 =cut
196  
197 sub _load_module {
198 my ($self, $name) = @_;
199 my ($module, $load, $m);
200 $module = "_<$name.pm";
201 return 1 if $main::{$module};
202  
203 # untaint operation for safe web-based running (modified after a fix
204 # a fix by Lincoln) HL
205 if ($name !~ /^([\w:]+)$/) {
206 $self->throw("$name is an illegal perl package name");
207 }
208  
209 $load = "$name.pm";
210 my $io = Bio::Root::IO->new();
211 # catfile comes from IO
212 $load = $io->catfile((split(/::/,$load)));
213 eval {
214 require $load;
215 };
216 if ( $@ ) {
217 $self->throw("Failed to load module $name. ".$@);
218 }
219 return 1;
220 }
221  
222 1;