diff --git a/modules/Bio/EnsEMBL/Utils/Converter.pm b/modules/Bio/EnsEMBL/Utils/Converter.pm new file mode 100644 index 0000000000000000000000000000000000000000..dcbcc4eb1d2bb66714ee2e55378410a8913848fa --- /dev/null +++ b/modules/Bio/EnsEMBL/Utils/Converter.pm @@ -0,0 +1,279 @@ +# EnsEMBL module for conversion of objects between EnsEMBL and other project, such as BioPerl +# +# Created and Cared for by Juguang Xiao <juguang@fugu-sg.org> +# +# Copyright Juguang Xiao +# +# You may distribute this module under the same terms as perl itself +# +# POD documentation +# + +=head1 NAME + +Bio::EnsEMBL::Utils::Converter, a converter factory + +=head1 SYNOPSIS + + my $converter = Bio::EnsEMBL::Utils::Converter->new( + -in => 'Bio::SeqFeature::Generic', + -out => 'Bio::EnsEMBL::SimpleFeature' + ); + + my ($fearture1, $feature2); + my $ens_simple_features = $converter->convert([$feature1, $feature2]); + my @ens_simple_features = @{$ens_simple_features}; + +=head1 DESCRIPTION + + Module to converter the business objects between EnsEMBL and any other projects, currently BioPerl. + +=head1 FEEDBACK + + +=head1 AUTHOR - Juguang Xiao + +Juguang Xiao <juguang@fugu-sg.org> + +=head1 APPENDIX + +The rest of the documentation details each of the object methods. +Internal methods are usually preceded with a _ + +=cut + +# Let the code begin ... + + +package Bio::EnsEMBL::Utils::Converter; + +use strict; +use vars qw(@ISA); + +use Bio::EnsEMBL::Root; + +@ISA =qw(Bio::EnsEMBL::Root); + +=head2 new + Title : new + Usage : + my $converter = Bio::EnsEMBL::Utils::Converter->new( + -in => 'Bio::SeqFeature::Generic', + -out => 'Bio::EnsEMBL::SimpleFeature' + ); + + Function: constructor for converter object + Returns : L<Bio::EnsEMBL::Utils::Converter> + Args : + in - the module name of the input. + out - the module name of the output. + analysis - a Bio::EnsEMBL::Analysis object, if converting other objects to EnsEMBL features. + contig - a Bio::EnsEMBL::RawContig object, if converting other objects to EnsEMBL features. + +=cut + +sub new { + my ($caller, @args) = @_; + my $class = ref($caller) || $caller; + + if($class =~ /Bio::EnsEMBL::Utils::Converter::(\S+)/){ + my $self = $class->SUPER::new(@args); + $self->_initialize(@args); + return $self; + }else{ + my %params = @args; + @params{map {lc $_} keys %params} = values %params; + my $module = $class->_guess_module($params{-in}, $params{-out}); + + return undef unless($class->_load_module($module)); + return "$module"->new(@args); + } +} + +# This would be invoked by sub-module's _initialize. + +sub _initialize { + my ($self, @args) = @_; + + my ($in, $out) = $self->_rearrange([qw(IN OUT)], @args); + + $self->in($in); + $self->out($out); +} + +=head2 _guess_module + Usage : $module = $class->_guess_module( + 'Bio::EnsEMBL::SimpleFeature', + 'Bio::EnsEMBL::Generic' + ); +=cut + +sub _guess_module { + my ($self, $in, $out) = @_; + if($in =~ /^Bio::EnsEMBL::(\S+)/ and $out =~ /^Bio::EnsEMBL::(\S+)/){ + $self->throw("Cannot convert between EnsEMBL objects.\n[$in] to [$out]"); + }elsif($in =~ /^Bio::EnsEMBL::(\S+)/){ + return 'Bio::EnsEMBL::Utils::Converter::ens_bio'; + }elsif($out =~ /^Bio::EnsEMBL::(\S+)/){ + return 'Bio::EnsEMBL::Utils::Converter::bio_ens'; + }else{ + $self->throw("Cannot convert between non-EnsEMBL objects.\n[$in] to [$out]"); + } +} + +=head2 convert + Title : convert + Usage : my $array_ref = $converter->convert(\@input); + Function: does the actual conversion + Returns : an array ref of converted objects + Args : an array ref of converting objects + +=cut + +sub convert{ + my ($self, $input) = @_; + + $input || $self->throw("Need a ref of array of input objects to convert"); + + my $output_module = $self->out; + $self->throw("Cannot load [$output_module] perl module") + unless $self->_load_module($output_module); + + unless(ref($input) eq 'ARRAY'){ + $self->warn("The input is supposed to be an array ref"); + return $self->_convert_single($input); + } + + my @output = (); + foreach(@{$input}){ + push(@output, $self->_convert_single($_)); + } + + return \@output; +} + +sub _converter_single{ + my ($self, $input) = @_; + $self->throw("Not implemented. Please check the instance subclass"); +} + +=head2 in + + Title : in + Useage : $self->in + Function : get and set for in + Return : + Args : + +=cut + +sub in { + my($self, $arg) = @_; + if(defined($arg)){ + $self->{_in} = $arg; + } + return $self->{_in}; +} + +=head2 out + + Title : out + Useage : $self->out + Function : get and set for out + Return : + Args : + +=cut + +sub out { + my($self, $arg) = @_; + if(defined($arg)){ + $self->{_out} = $arg; + } + return $self->{_out}; +} + +=head2 analysis + + Title : analysis + Useage : $self->analysis + Function : get and set for analysis + Return : L<Bio::EnsEMBL::Analysis> + Args : L<Bio::EnsEMBL::Analysis> + +=cut + +sub analysis { + my($self, $arg) = @_; + if(defined($arg)){ + $self->throws("A Bio::EnsEMBL::Analysis object expected.") unless(defined $arg); + $self->{_analysis} = $arg; + } + return $self->{_analysis}; +} + +=head2 contig + + Title : contig + Useage : $self->contig + Function : get and set for contig + Return : L<Bio::EnsEMBL::RawContig> + Args : L<Bio::EnsEMBL::RawContig> + +=cut + +sub contig { + my($self, $arg) = @_; + if(defined($arg)){ + $self->throws("A Bio::EnsEMBL::RawContig object expected.") unless(defined $arg); + $self->{_contig} = $arg; + } + return $self->{_contig}; +} + +sub _getset { + my ($self, $tag, $value, $type) = @_; + + $tag || $self->throw("a tag needed"); + + if(defined($value)){ + $self->{_$tag} = $value; + } + return $self->{_$tag}; +} + +sub getset{ + my ($self, $arg) = @_; + return $self->_getset('getset', $arg); +} + +=head2 _load_module + This method is copied from Bio::Root::Root +=cut + +sub _load_module { + my ($self, $name) = @_; + my ($module, $load, $m); + $module = "_<$name.pm"; + return 1 if $main::{$module}; + + # untaint operation for safe web-based running (modified after a fix + # a fix by Lincoln) HL + if ($name !~ /^([\w:]+)$/) { + $self->throw("$name is an illegal perl package name"); + } + + $load = "$name.pm"; + my $io = Bio::Root::IO->new(); + # catfile comes from IO + $load = $io->catfile((split(/::/,$load))); + eval { + require $load; + }; + if ( $@ ) { + $self->throw("Failed to load module $name. ".$@); + } + return 1; +} + +1; diff --git a/modules/Bio/EnsEMBL/Utils/Converter/bio_ens.pm b/modules/Bio/EnsEMBL/Utils/Converter/bio_ens.pm new file mode 100644 index 0000000000000000000000000000000000000000..9677ac17ded5b93ab3828352254918c984c9ac33 --- /dev/null +++ b/modules/Bio/EnsEMBL/Utils/Converter/bio_ens.pm @@ -0,0 +1,141 @@ +# Bio::EnsEMBL::Utils::Converter::bio_ens +# +# Created and cared for by Juguang Xiao <juguang@fugu-sg.org> +# Created date: 4/3/2003 +# +# Copyright Juguang Xiao +# +# You may distribute this module under the same terms as perl itself +# +# POD documentation +# + +=head1 NAME + +Bio::EnsEMBL::Utils::Converter::bio_ens + +=head1 SYNOPISIS + +You should not use this module directly. Please check out the +Bio::EnsEMBL::Utils::Converter module. + +=head1 DESCRIPTION + + +=head1 FEEDBACK + +=head2 Mailing Lists + +=head2 Reporting Bugs + + +=head1 AUTHOR Juguang Xiao + +Juguang Xiao <juguang@fugu-sg.org> + +=head1 APPENDIX + +The rest of the documentation details each of the object methods. +Internal methods are usually preceded with a _ + +=cut + +# Let the code begin ... + +package Bio::EnsEMBL::Utils::Converter::bio_ens; + +use strict; +use vars qw(@ISA); +use Bio::EnsEMBL::Analysis; +use Bio::EnsEMBL::Utils::Converter; +@ISA = qw(Bio::EnsEMBL::Utils::Converter); + +=head2 new + +Please see Bio::EnsEMBL::Utils::Converter::new + +=cut + +sub new { + my ($caller, @args) = @_; + my $class = ref($caller) || $caller; + + if($class eq 'Bio::EnsEMBL::Utils::Converter::bio_ens'){ + my %params = @args; + @params{map{lc $_} keys %params} = values %params; + my $module = $class->_guess_module($params{-in}, $params{-out}); + return undef unless ($class->_load_module($module)); + return "$module"->new(@args); + }else{ + my $self = $class->SUPER::new(@args); +# $self->_initialize(@args); + return $self; + } +} + +sub _initialize { + my ($self, @args) = @_; + $self->SUPER::_initialize(@args); + + my ($analysis, $contig) = + $self->_rearrange([qw(analysis contig)], @args); + + $self->analysis($analysis); + $self->contig($contig); +} + + +sub _guess_module { + my ($self, $in, $out) = @_; + my $tail; + if($in eq 'Bio::Search::HSP::GenericHSP'){ + $tail = 'bio_ens_hit'; + }elsif($in eq 'Bio::SeqFeature::Generic'){ + $tail = 'bio_ens_seqFeature'; + }elsif($in eq 'Bio::SeqFeature::FeaturePair'){ + $tail = 'bio_ens_featurePair'; + }else{ + $self->throw("[$in] to [$out], not supported"); + } + return "Bio::EnsEMBL::Utils::Converter::$tail"; +} + + +=head2 analysis + Title : analysis + Usage : $self->analysis + Function: get and set for analysis + Return : L<Bio::EnsEMBL::Analysis> + Args : L<Bio::EnsEMBL::Analysis> +=cut + +sub analysis { + my ($self, $arg) = @_; + if(defined($arg)){ + $self->throws("A Bio::EnsEMBL::Analysis object expected.") unless(defined $arg); + $self->{_analysis} = $arg; + } + return $self->{_analysis}; +} + + + +=head2 contig + Title : contig + Usage : $self->contig + Function: get and set for contig + Return : + Args : +=cut + +sub contig { + my ($self, $arg) = @_; + if(defined($arg)){ + $self->{_contig} = $arg; + } + return $self->{_contig}; +} + + + +1; diff --git a/modules/Bio/EnsEMBL/Utils/Converter/bio_ens_featurePair.pm b/modules/Bio/EnsEMBL/Utils/Converter/bio_ens_featurePair.pm new file mode 100644 index 0000000000000000000000000000000000000000..99744ceca5d5175e6d28a1f5fcd47192cd9c9e59 --- /dev/null +++ b/modules/Bio/EnsEMBL/Utils/Converter/bio_ens_featurePair.pm @@ -0,0 +1,111 @@ +# Bio::EnsEMBL::Utils::Converter::bio_ens_featurePair +# +# Created and cared for by Juguang Xiao <juguang@fugu-sg.org> +# Created date: 5/3/2003 +# +# Copyright Juguang Xiao +# +# You may distribute this module under the same terms as perl itself +# +# POD documentation +# + +=head1 NAME + +Bio::EnsEMBL::Utils::Converter::bio_ens_featurePair + +=head1 SYNOPISIS + + + +=head1 DESCRIPTION + + +=head1 FEEDBACK + +=head2 Mailing Lists + +=head2 Reporting Bugs + + +=head1 AUTHOR Juguang Xiao + +Juguang Xiao <juguang@fugu-sg.org> + +=head1 APPENDIX + +The rest of the documentation details each of the object methods. +Internal methods are usually preceded with a _ + +=cut + +# Let the code begin ... + +package Bio::EnsEMBL::Utils::Converter::bio_ens_featurePair; + +use strict; +use vars qw(@ISA); + +use Bio::EnsEMBL::RepeatConsensus; + +use Bio::EnsEMBL::Utils::Converter::bio_ens; +@ISA = qw(Bio::EnsEMBL::Utils::Converter::bio_ens); + +sub _convert_single { + my ($self, $pair) = @_; + if($self->out eq 'Bio::EnsEMBL::RepeatFeature'){ + return $self->_convert_single_to_repeatFeature($pair); + }else{ + my $output_module = $self->out; + $self->throw("Cannot covert to [$output_module]"); + } +} + +sub _convert_single_to_repeatFeature { + my ($self, $pair) = @_; + my $feature1 = $pair->feature1; + my $feature2 = $pair->feature2; + my $ens_repeatfeature = new Bio::EnsEMBL::RepeatFeature( + -seqname => $feature1->seq_id, + -start => $feature1->start, + -end => $feature1->end, + -strand => $feature1->strand, + -source_tag => $feature1->source_tag, + ); + + my ($h_start, $h_end); + if($feature1->strand == 1){ + $h_start = $feature2->start; + $h_end = $feature2->end; + }elsif($feature1->strand == -1){ + $h_start = $feature2->end; + $h_end = $feature2->start; + }else{ + $self->throw("strand cannot be outside of (1, -1)"); + } + + $ens_repeatfeature->hstart($h_start); + $ens_repeatfeature->hend($h_end); + my $repeat_name = $feature2->seq_id; + my $repeat_class = $feature1->primary_tag; + $repeat_class ||= $feature2->primary_tag; + $repeat_class ||= "not sure"; + my $ens_repeat_consensus = + $self->_create_consensus($repeat_name, $repeat_class); + $ens_repeatfeature->repeat_consensus($ens_repeat_consensus); + + $ens_repeatfeature->attach_seq($self->contig); + $ens_repeatfeature->analysis($self->analysis); + return $ens_repeatfeature; +} + +sub _create_consensus{ + my ($self, $repeat_name, $repeat_class) = @_; + my $consensus = new Bio::EnsEMBL::RepeatConsensus; + $consensus->name($repeat_name); + $consensus->repeat_class($repeat_class); + return $consensus; +} + + +1; diff --git a/modules/Bio/EnsEMBL/Utils/Converter/bio_ens_seqFeature.pm b/modules/Bio/EnsEMBL/Utils/Converter/bio_ens_seqFeature.pm new file mode 100644 index 0000000000000000000000000000000000000000..9812b461d1cd927d6e14c94eb5dcb9b0fa1cfa28 --- /dev/null +++ b/modules/Bio/EnsEMBL/Utils/Converter/bio_ens_seqFeature.pm @@ -0,0 +1,87 @@ +# Bio::EnsEMBL::Utils::Converter::bio_ens_seqFeature +# +# Created and cared for by Juguang Xiao <juguang@fugu-sg.org> +# Created date: 5/3/2003 +# +# Copyright Juguang Xiao +# +# You may distribute this module under the same terms as perl itself +# +# POD documentation +# + +=head1 NAME + +Bio::EnsEMBL::Utils::Converter::bio_ens_seqFeature + +=head1 SYNOPISIS + +Please read Bio::EnsEMBL::Utils::Converter + +=head1 DESCRIPTION + + +=head1 FEEDBACK + +=head2 Mailing Lists + +=head2 Reporting Bugs + + +=head1 AUTHOR Juguang Xiao + +Juguang Xiao <juguang@fugu-sg.org> + +=head1 APPENDIX + +The rest of the documentation details each of the object methods. +Internal methods are usually preceded with a _ + +=cut + +# Let the code begin ... + +package Bio::EnsEMBL::Utils::Converter::bio_ens_seqFeature; + +use strict; +use vars qw(@ISA); +use Bio::EnsEMBL::Utils::Converter::bio_ens; +@ISA = qw(Bio::EnsEMBL::Utils::Converter::bio_ens); + +sub _convert_single { + my ($self, $in) = @_; + + unless($in && defined($in) && ref($in) && $in->isa('Bio::SeqFeature::Generic')){ + $self->throw("a Bio::SeqFeature::Generic object needed"); + } + + my $ens_seqFeature; + my @args = ( + -start => $in->start, + -end => $in->end, + -strand => $in->strand, + -score => $in->score + -analysis => $self->analysis, + -source_tag => $in->source_tag + -seqname => $in->seq_id + ); + + my $output_module = $self->out; + + if($output_module eq 'Bio::EnsEMBL::SeqFeature'){ + + $ens_seqFeature = new Bio::EnsEMBL::SeqFeature(@args); + }elsif($self->out eq 'Bio::EnsEMBL::SimpleFeature'){ + $ens_seqFeature = new Bio::EnsEMBL::SimpleFeature(@args); + # The field that there is in SimpleFeature, but not in SeqFeature. + $ens_seqFeature->display_label('__NONE__'); + }else{ + $self->throw("[$output_module] as -out, not supported"); + } + + $ens_seqFeature->attach_seq($self->contig); + + return $ens_seqFeature; +} + +1; diff --git a/modules/Bio/EnsEMBL/Utils/Converter/ens_bio.pm b/modules/Bio/EnsEMBL/Utils/Converter/ens_bio.pm new file mode 100644 index 0000000000000000000000000000000000000000..37575aff63a5f48c174bd0a22e1126b0af7293d7 --- /dev/null +++ b/modules/Bio/EnsEMBL/Utils/Converter/ens_bio.pm @@ -0,0 +1,93 @@ +# Bio::EnsEMBL::Utils::Converter::ens_bio +# +# Created and cared for by Juguang Xiao <juguang@fugu-sg.org> +# Created date: 5/3/2003 +# +# Copyright Juguang Xiao +# +# You may distribute this module under the same terms as perl itself +# +# POD documentation +# + +=head1 NAME + +Bio::EnsEMBL::Utils::Converter::ens_bio + +=head1 SYNOPISIS + +You are not supposed to use this module directly. Please read +Bio::EnsEMBL::Utils::Converter + +=head1 DESCRIPTION + +This is a helper module to assist Bio::EnsEMBL::Utils::Converter find which +converter instance should be used, based on the -in and -out parameters. + +=head1 FEEDBACK + +=head2 Mailing Lists + +=head2 Reporting Bugs + + +=head1 AUTHOR Juguang Xiao + +Juguang Xiao <juguang@fugu-sg.org> + +=head1 APPENDIX + +The rest of the documentation details each of the object methods. +Internal methods are usually preceded with a _ + +=cut + +# Let the code begin ... + +package Bio::EnsEMBL::Utils::Converter::ens_bio; + +use strict; +use vars qw(@ISA); +use Bio::EnsEMBL::Utils::Converter; +@ISA = qw(Bio::EnsEMBL::Utils::Converter); + +=head2 new + Please see Bio::EnsEMBL::Utils::Converter::new +=cut + +sub new { + my ($caller, @args) = @_; + my $class = ref($caller) || $caller; + + if($class eq 'Bio::EnsEMBL::Utils::Converter::ens_bio'){ + my %params = @args; + @params{map{lc $_} keys %params} = values %params; + my $module = $class->_guess_module($params{-in}, $params{-out}); + return undef unless ($class->_load_module($module)); + return "$module"->new(@args); + }else{ + my $self = $class->SUPER::new(@args); +# $self->_initialize(@args); + return $self; + } + +} + +# Unlike bio_ens, ens_bio does not need _initialize method for analysis and +# contig information. +# + +sub _guess_module { + my ($self, $in, $out) = @_; + my $tail; + if($in eq 'Bio::EnsEMBL::SeqFeature'){ + $tail = 'ens_bio_seqFeature'; + }elsif($in eq 'Bio::Ens::EMBL::FeaturePair'){ + $tail = 'ens_bio_featurePair'; + }else{ + $self->throw("[$in] to [$out], not supported"); + } + return "Bio::EnsEMBL::Utils::Converter::$tail"; +} + +1; diff --git a/modules/Bio/EnsEMBL/Utils/Converter/ens_bio_featurePair.pm b/modules/Bio/EnsEMBL/Utils/Converter/ens_bio_featurePair.pm new file mode 100644 index 0000000000000000000000000000000000000000..b46760d0a5439a2ea3e40c38af98020d2e552cb8 --- /dev/null +++ b/modules/Bio/EnsEMBL/Utils/Converter/ens_bio_featurePair.pm @@ -0,0 +1,100 @@ +# Bio::EnsEMBL::Utils::Converter::ens_bio_featurePair +# +# Created and cared for by Juguang Xiao <juguang@fugu-sg.org> +# Created date: 5/3/2003 +# +# Copyright Juguang Xiao +# +# You may distribute this module under the same terms as perl itself +# +# POD documentation +# + +=head1 NAME + +Bio::EnsEMBL::Utils::Converter::ens_bio_featurePair + +=head1 SYNOPISIS + + + +=head1 DESCRIPTION + + +=head1 FEEDBACK + +=head2 Mailing Lists + +=head2 Reporting Bugs + + +=head1 AUTHOR Juguang Xiao + +Juguang Xiao <juguang@fugu-sg.org> + +=head1 APPENDIX + +The rest of the documentation details each of the object methods. +Internal methods are usually preceded with a _ + +=cut + +# Let the code begin ... + +package Bio::EnsEMBL::Utils::Converter::ens_bio_featurePair; + +use strict; +use vars qw(@ISA); +use Bio::SeqFeature::Generic; +use Bio::SeqFeature::FeaturePair; +use Bio::EnsEMBL::Utils::Converter::ens_bio; +@ISA = qw(Bio::EnsEMBL::Utils::Converter::ens_bio); + +sub _convert_single { + my ($self, @args) = @_; + +} + +# convert object from Bio::EnsEMBL::RepeatFeature +# to Bio::SeqFeature::FeaturePair + +sub _convert_single_repeatFeature { + my ($self, $ens_repeat) = @_; + + my $feature1 = new Bio::SeqFeature::Generic( + -start => $ens_repeat->start, + -end => $ens_repeat->end, + -strand => $ens_repeat->strand, + -source_tag => $ens_repeat->source_tag + -primary_tag => $ens_repeat->repeat_class, + -seq_id => $ens_repeat->seqname + ); + + my ($start2, $end2); + if($ens_repeat->strand == 1){ + $start2 = $ens_repeat->hstart; + $end2 = $ens_repeat->hend; + }elsif($ens_repeat->strand == -1){ + $start2 = $ens_repeat->hend; + $end2 = $ens_repeat->hstart; + }else{ + $self->throw("strand cannot be out of range (1, -1)"); + } + + my $feature2 = new Bio::SeqFeature::Generic( + -start => $start2, + -end => $end2, + -source_tag => $ens_repeat->source_tag, + -primary_tag => $ens_repeat->repeat_class, + -seq_id => $ens_repeat->repeat_name + ); + + my $output_module = $self->out; + require "$output_module"; + return new Bio::SeqFeature::FeaturePair( + -feature1 => $feature1, + -feature2 => $feature2 + ); +} + +1; diff --git a/modules/Bio/EnsEMBL/Utils/Converter/ens_bio_seqFeature.pm b/modules/Bio/EnsEMBL/Utils/Converter/ens_bio_seqFeature.pm new file mode 100644 index 0000000000000000000000000000000000000000..fbbdde585a4befc591db3b493e3795b643f3011b --- /dev/null +++ b/modules/Bio/EnsEMBL/Utils/Converter/ens_bio_seqFeature.pm @@ -0,0 +1,73 @@ +# Bio::EnsEMBL::Utils::Converter::ens_bio_seqFeature +# +# Created and cared for by Juguang Xiao <juguang@fugu-sg.org> +# Created date: 5/3/2003 +# +# Copyright Juguang Xiao +# +# You may distribute this module under the same terms as perl itself +# +# POD documentation +# + +=head1 NAME + +Bio::EnsEMBL::Utils::Converter::ens_bio_seqFeature + +=head1 SYNOPISIS + + + +=head1 DESCRIPTION + + +=head1 FEEDBACK + +=head2 Mailing Lists + +=head2 Reporting Bugs + + +=head1 AUTHOR Juguang Xiao + +Juguang Xiao <juguang@fugu-sg.org> + +=head1 APPENDIX + +The rest of the documentation details each of the object methods. +Internal methods are usually preceded with a _ + +=cut + +# Let the code begin ... + +package Bio::EnsEMBL::Utils::Converter::ens_bio_seqFeature; + +use strict; +use vars qw(@ISA); +use Bio::EnsEMBL::Utils::Converter::ens_bio; +@ISA = qw(Bio::EnsEMBL::Utils::Converter::ens_bio); + +sub _convert_single { + my ($self, $in) = @_; + + $self->throw("Input not defined") unless($in && defined($in)); + unless(ref($in) && $in->isa('Bio::EnsEMBL::SeqFeature')){ + $self->throw('A Bio::EnsEMBL::SeqFeature object needed'); + } + + my @args = ( + -start => $in->start, + -end => $in->end, + -strand => $in->strand, + -score => $in->score, + -source_tag => $in->source_tag, + -seq_id => $in->seqname + ); + + my $seqFeature = new Bio::SeqFeature::Generic(@args); + + return $seqFeature; +} + +1; diff --git a/modules/t/converter.t b/modules/t/converter.t new file mode 100644 index 0000000000000000000000000000000000000000..85fecca2c8babf3a2959455832b623a24693310c --- /dev/null +++ b/modules/t/converter.t @@ -0,0 +1,133 @@ +use lib 't'; +use strict; + +BEGIN { + $| = 1; + use Test; + plan tests => 22; +} + +use Bio::EnsEMBL::Utils::Converter; + +END { + +} + + +use Bio::EnsEMBL::Utils::Converter; + +use Bio::EnsEMBL::Analysis; +use Bio::EnsEMBL::RawContig; +use Bio::SeqFeature::Generic; +use Bio::SeqFeature::FeaturePair; + +# Global constants + +my $ens_analysis = new Bio::EnsEMBL::Analysis( + -dbID => 1, + -logic_name => 'test_fake_analysis' +); + +my $ens_contig = new Bio::EnsEMBL::RawContig(); + +my $seqFeature1 = new Bio::SeqFeature::Generic( + -start => 100, + -end => 200, + -strand => 1, + -score => 10 +); + +my $seqFeature2 = new Bio::SeqFeature::Generic( + -start => 1234, + -end => 5678, + -strand => -1, + -score => 14 +); + +my $featurePair1 = new Bio::SeqFeature::FeaturePair( + -feature1 => $seqFeature1, + -feature2 => $seqFeature2 +); + +&test_SeqFeature; +&test_FeaturePair; +&test_hit; + +# Test for SeqFeature, 10 OKs +sub test_SeqFeature{ + # bio to ens + my $converter = new Bio::EnsEMBL::Utils::Converter( + -in => 'Bio::SeqFeature::Generic', + -out => 'Bio::EnsEMBL::SeqFeature', + -analysis => $ens_analysis, + -contig => $ens_contig + ); + + my ($ens_seqFeature1, $ens_seqFeature2) = + @{$converter->convert([$seqFeature1, $seqFeature2])}; + + ok ref($ens_seqFeature1), 'Bio::EnsEMBL::SeqFeature'; + ok ref($ens_seqFeature2), 'Bio::EnsEMBL::SeqFeature'; + + ok $seqFeature1->start, $ens_seqFeature1->start; + ok $seqFeature1->end, $ens_seqFeature1->end; + ok $seqFeature1->strand, $ens_seqFeature1->strand; + ok $seqFeature1->score, $ens_seqFeature1->score; + ok $seqFeature1->source_tag, $ens_seqFeature1->source_tag; + + # from ens to bio + $converter = new Bio::EnsEMBL::Utils::Converter( + -in => 'Bio::EnsEMBL::SeqFeature', + -out => 'Bio::SeqFeature::Generic', + ); + + my ($bio_seqFeature1, $bio_seqFeature2) = + @{$converter->convert([$ens_seqFeature1, $ens_seqFeature2])}; + + ok ref($bio_seqFeature1), 'Bio::SeqFeature::Generic'; + ok ref($bio_seqFeature2), 'Bio::SeqFeature::Generic'; + + ok $seqFeature1->start, $bio_seqFeature1->start; + ok $seqFeature1->end, $bio_seqFeature1->end; + ok $seqFeature1->strand, $bio_seqFeature1->strand; + ok $seqFeature1->score, $bio_seqFeature1->score; + ok $seqFeature1->source_tag, $bio_seqFeature1->source_tag; + +} + + +sub test_FeaturePair{ + + my $converter = new Bio::EnsEMBL::Utils::Converter( + -in => 'Bio::SeqFeature::FeaturePair', + -out => 'Bio::EnsEMBL::RepeatFeature', + -analysis => $ens_analysis, + -contig => $ens_contig + ); + + my ($ens_repeatFeature1) = @{$converter->convert([$featurePair1])}; + + ok ref($ens_repeatFeature1), 'Bio::EnsEMBL::RepeatFeature'; + + ok $ens_repeatFeature1->start, $featurePair1->feature1->start; + ok $ens_repeatFeature1->end, $featurePair1->feature1->end; + ok $ens_repeatFeature1->strand, $featurePair1->feature1->strand; + ok $ens_repeatFeature1->seqname, $featurePair1->feature1->seq_id; + ok $ens_repeatFeature1->repeat_consensus->name, $featurePair1->feature2->seq_id; + + if($featurePair1->feature1->strand == 1){ + ok $ens_repeatFeature1->hstart, $featurePair1->feature2->start; + ok $ens_repeatFeature1->hend, $featurePair1->feature2->end; + }else{ + ok $ens_repeatFeature1->hstart, $featurePair1->feature2->end; + ok $ens_repeatFeature1->hend, $featurePair1->feature2->start; + } + +} + + +sub test_hit { + + +} +