PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/external/lib/perl/Bio/EnsEMBL/Utils/Converter.pm

https://bitbucket.org/intogen/mutations-analysis
Perl | 213 lines | 167 code | 43 blank | 3 comment | 6 complexity | bff55a6061913ea0527d6a019bee5579 MD5 | raw file
  1. =head1 LICENSE
  2. Copyright (c) 1999-2012 The European Bioinformatics Institute and
  3. Genome Research Limited. All rights reserved.
  4. This software is distributed under a modified Apache license.
  5. For license details, please see
  6. http://www.ensembl.org/info/about/code_licence.html
  7. =head1 CONTACT
  8. Please email comments or questions to the public Ensembl
  9. developers list at <dev@ensembl.org>.
  10. Questions may also be sent to the Ensembl help desk at
  11. <helpdesk@ensembl.org>.
  12. =head1 AUTHOR
  13. Juguang Xiao <juguang@tll.org.sg>
  14. =cut
  15. =head1 NAME
  16. Bio::EnsEMBL::Utils::Converter, a converter factory
  17. =head1 SYNOPSIS
  18. my $converter = Bio::EnsEMBL::Utils::Converter->new(
  19. -in => 'Bio::SeqFeature::Generic',
  20. -out => 'Bio::EnsEMBL::SimpleFeature'
  21. );
  22. my ( $fearture1, $feature2 );
  23. my $ens_simple_features =
  24. $converter->convert( [ $feature1, $feature2 ] );
  25. my @ens_simple_features = @{$ens_simple_features};
  26. =head1 DESCRIPTION
  27. Module to converter the business objects between EnsEMBL and any other
  28. projects, currently BioPerl.
  29. What the ready conversions are,
  30. Bio::SeqFeature::Generic <-> Bio::EnsEMBL::SeqFeature, Bio::EnsEMBL::SimpleFeature
  31. Bio::SeqFeature::FeaturePair <-> Bio::EnsEMBL::SeqFeature, Bio::EnsEMBL::RepeatFeature
  32. Bio::Search::HSP::GenericHSP -> Bio::EnsEMBL::BaseAlignFeature's submodules
  33. Bio::Tools::Prediction::Gene -> Bio::EnsEMBL::PredictionTranscript
  34. Bio::Tools::Prediction::Exon -> Bio::EnsEMBL::Exon
  35. Bio::Pipeline::Analysis -> Bio::EnsEMBL::Analysis
  36. =head1 METHODS
  37. =cut
  38. package Bio::EnsEMBL::Utils::Converter;
  39. use strict;
  40. use Bio::EnsEMBL::Root;
  41. our @ISA =qw(Bio::EnsEMBL::Root);
  42. =head2 new
  43. Title : new
  44. Usage :
  45. my $converter = Bio::EnsEMBL::Utils::Converter->new(
  46. -in => 'Bio::SeqFeature::Generic',
  47. -out => 'Bio::EnsEMBL::SimpleFeature'
  48. );
  49. Function: constructor for converter object
  50. Returns : L<Bio::EnsEMBL::Utils::Converter>
  51. Args :
  52. in - the module name of the input.
  53. out - the module name of the output.
  54. analysis - a Bio::EnsEMBL::Analysis object, if converting other objects to EnsEMBL features.
  55. contig - a Bio::EnsEMBL::RawContig object, if converting other objects to EnsEMBL features.
  56. =cut
  57. sub new {
  58. my ($caller, @args) = @_;
  59. my $class = ref($caller) || $caller;
  60. if($class =~ /Bio::EnsEMBL::Utils::Converter::(\S+)/){
  61. my $self = $class->SUPER::new(@args);
  62. $self->_initialize(@args);
  63. return $self;
  64. }else{
  65. my %params = @args;
  66. @params{map {lc $_} keys %params} = values %params;
  67. my $module = $class->_guess_module($params{-in}, $params{-out});
  68. return undef unless($class->_load_module($module));
  69. return "$module"->new(@args);
  70. }
  71. }
  72. # This would be invoked by sub-module's _initialize.
  73. sub _initialize {
  74. my ($self, @args) = @_;
  75. my ($in, $out) = $self->_rearrange([qw(IN OUT)], @args);
  76. $self->in($in);
  77. $self->out($out);
  78. }
  79. =head2 _guess_module
  80. Usage : $module = $class->_guess_module(
  81. 'Bio::EnsEMBL::SimpleFeature',
  82. 'Bio::EnsEMBL::Generic'
  83. );
  84. =cut
  85. sub _guess_module {
  86. my ($self, $in, $out) = @_;
  87. if($in =~ /^Bio::EnsEMBL::(\S+)/ and $out =~ /^Bio::EnsEMBL::(\S+)/){
  88. $self->throw("Cannot convert between EnsEMBL objects.\n[$in] to [$out]");
  89. }elsif($in =~ /^Bio::EnsEMBL::(\S+)/){
  90. return 'Bio::EnsEMBL::Utils::Converter::ens_bio';
  91. }elsif($out =~ /^Bio::EnsEMBL::(\S+)/){
  92. return 'Bio::EnsEMBL::Utils::Converter::bio_ens';
  93. }else{
  94. $self->throw("Cannot convert between non-EnsEMBL objects.\n[$in] to [$out]");
  95. }
  96. }
  97. =head2 convert
  98. Title : convert
  99. Usage : my $array_ref = $converter->convert(\@input);
  100. Function: does the actual conversion
  101. Returns : an array ref of converted objects
  102. Args : an array ref of converting objects
  103. =cut
  104. sub convert{
  105. my ($self, $input) = @_;
  106. $input || $self->throw("Need a ref of array of input objects to convert");
  107. my $output_module = $self->out;
  108. $self->throw("Cannot load [$output_module] perl module")
  109. unless $self->_load_module($output_module);
  110. unless(ref($input) eq 'ARRAY'){
  111. $self->warn("The input is supposed to be an array ref");
  112. return $self->_convert_single($input);
  113. }
  114. my @output = ();
  115. foreach(@{$input}){
  116. push(@output, $self->_convert_single($_));
  117. }
  118. return \@output;
  119. }
  120. sub _convert_single{
  121. shift->throw("Not implemented. Please check the instance subclass");
  122. }
  123. foreach my $field (qw(in out)){
  124. my $slot=__PACKAGE__ ."::$field";
  125. no strict 'refs';
  126. *$field=sub{
  127. my $self=shift;
  128. $self->{$slot}=shift if @_;
  129. return $self->{$slot};
  130. };
  131. }
  132. =head2 _load_module
  133. This method is copied from Bio::Root::Root
  134. =cut
  135. sub _load_module {
  136. my ($self, $name) = @_;
  137. my ($module, $load, $m);
  138. $module = "_<$name.pm";
  139. return 1 if $main::{$module};
  140. # untaint operation for safe web-based running (modified after a fix
  141. # a fix by Lincoln) HL
  142. if ($name !~ /^([\w:]+)$/) {
  143. $self->throw("$name is an illegal perl package name");
  144. }
  145. $load = "$name.pm";
  146. my $io = Bio::Root::IO->new();
  147. # catfile comes from IO
  148. $load = $io->catfile((split(/::/,$load)));
  149. eval {
  150. require $load;
  151. };
  152. if ( $@ ) {
  153. $self->throw("Failed to load module $name. ".$@);
  154. }
  155. return 1;
  156. }
  157. 1;