| 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'; | ||
| 72 | use strict; | ||
| 73 | |||
| 74 | =head2 new | ||
| 75 | |||
| 76 | Title : new | ||
| 77 | Usage : | ||
| 78 | my $converter = Bio::EnsEMBL::Utils::Converter->new( | ||
| 79 | -in => 'Bio::SeqFeature::Generic', | ||
| 80 | -out => 'Bio::EnsEMBL::SimpleFeature' | ||
| 81 | ); | ||
| 82 | |||
| 83 | Function: constructor for converter object | ||
| 84 | Returns : L<Bio::EnsEMBL::Utils::Converter> | ||
| 85 | Args : | ||
| 86 | in - the module name of the input. | ||
| 87 | out - the module name of the output. | ||
| 88 | analysis - a Bio::EnsEMBL::Analysis object, if converting other objects to EnsEMBL features. | ||
| 89 | contig - a Bio::EnsEMBL::RawContig object, if converting other objects to EnsEMBL features. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub new { | ||
| 94 | my ($caller, @args) = @_; | ||
| 95 | my $class = ref($caller) || $caller; | ||
| 96 | |||
| 97 | if($class =~ /Bio::EnsEMBL::Utils::Converter::(\S+)/){ | ||
| 98 | my $self = $class->SUPER::new(@args); | ||
| 99 | $self->_initialize(@args); | ||
| 100 | return $self; | ||
| 101 | }else{ | ||
| 102 | my %params = @args; | ||
| 103 | @params{map {lc $_} keys %params} = values %params; | ||
| 104 | my $module = $class->_guess_module($params{-in}, $params{-out}); | ||
| 105 | |||
| 106 | return undef unless($class->_load_module($module)); | ||
| 107 | return "$module"->new(@args); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | # This would be invoked by sub-module's _initialize. | ||
| 112 | |||
| 113 | sub _initialize { | ||
| 114 | my ($self, @args) = @_; | ||
| 115 | |||
| 116 | my ($in, $out) = $self->_rearrange([qw(IN OUT)], @args); | ||
| 117 | |||
| 118 | $self->in($in); | ||
| 119 | $self->out($out); | ||
| 120 | } | ||
| 121 | |||
| 122 | =head2 _guess_module | ||
| 123 | |||
| 124 | Usage : $module = $class->_guess_module( | ||
| 125 | 'Bio::EnsEMBL::SimpleFeature', | ||
| 126 | 'Bio::EnsEMBL::Generic' | ||
| 127 | ); | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub _guess_module { | ||
| 132 | my ($self, $in, $out) = @_; | ||
| 133 | if($in =~ /^Bio::EnsEMBL::(\S+)/ and $out =~ /^Bio::EnsEMBL::(\S+)/){ | ||
| 134 | $self->throw("Cannot convert between EnsEMBL objects.\n[$in] to [$out]"); | ||
| 135 | }elsif($in =~ /^Bio::EnsEMBL::(\S+)/){ | ||
| 136 | return 'Bio::EnsEMBL::Utils::Converter::ens_bio'; | ||
| 137 | }elsif($out =~ /^Bio::EnsEMBL::(\S+)/){ | ||
| 138 | return 'Bio::EnsEMBL::Utils::Converter::bio_ens'; | ||
| 139 | }else{ | ||
| 140 | $self->throw("Cannot convert between non-EnsEMBL objects.\n[$in] to [$out]"); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | =head2 convert | ||
| 145 | |||
| 146 | Title : convert | ||
| 147 | Usage : my $array_ref = $converter->convert(\@input); | ||
| 148 | Function: does the actual conversion | ||
| 149 | Returns : an array ref of converted objects | ||
| 150 | Args : an array ref of converting objects | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub convert{ | ||
| 155 | my ($self, $input) = @_; | ||
| 156 | |||
| 157 | $input || $self->throw("Need a ref of array of input objects to convert"); | ||
| 158 | |||
| 159 | my $output_module = $self->out; | ||
| 160 | $self->throw("Cannot load [$output_module] perl module") | ||
| 161 | unless $self->_load_module($output_module); | ||
| 162 | |||
| 163 | unless(ref($input) eq 'ARRAY'){ | ||
| 164 | $self->warn("The input is supposed to be an array ref"); | ||
| 165 | return $self->_convert_single($input); | ||
| 166 | } | ||
| 167 | |||
| 168 | my @output = (); | ||
| 169 | foreach(@{$input}){ | ||
| 170 | push(@output, $self->_convert_single($_)); | ||
| 171 | } | ||
| 172 | |||
| 173 | return \@output; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub _convert_single{ | ||
| 177 | shift->throw("Not implemented. Please check the instance subclass"); | ||
| 178 | } | ||
| 179 | |||
| 180 | foreach my $field (qw(in out)){ | ||
| 181 | my $slot=__PACKAGE__ ."::$field"; | ||
| 182 | no strict 'refs'; ## no critic | ||
| 183 | *$field=sub{ | ||
| 184 | my $self=shift; | ||
| 185 | $self->{$slot}=shift if @_; | ||
| 186 | return $self->{$slot}; | ||
| 187 | }; | ||
| 188 | } | ||
| 189 | |||
| 190 | =head2 _load_module | ||
| 191 | |||
| 192 | This method is copied from Bio::Root::Root | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | sub _load_module { | ||
| 197 | my ($self, $name) = @_; | ||
| 198 | my ($module, $load, $m); | ||
| 199 | $module = "_<$name.pm"; | ||
| 200 | return 1 if $main::{$module}; | ||
| 201 | |||
| 202 | # untaint operation for safe web-based running (modified after a fix | ||
| 203 | # a fix by Lincoln) HL | ||
| 204 | if ($name !~ /^([\w:]+)$/) { | ||
| 205 | $self->throw("$name is an illegal perl package name"); | ||
| 206 | } | ||
| 207 | |||
| 208 | $load = "$name.pm"; | ||
| 209 | my $io = Bio::Root::IO->new(); | ||
| 210 | # catfile comes from IO | ||
| 211 | $load = $io->catfile((split(/::/,$load))); | ||
| 212 | eval { | ||
| 213 | require $load; | ||
| 214 | }; | ||
| 215 | if ( $@ ) { | ||
| 216 | $self->throw("Failed to load module $name. ".$@); | ||
| 217 | } | ||
| 218 | return 1; | ||
| 219 | } | ||
| 220 | |||
| 221 | 1; |