Archive Ensembl HomeArchive Ensembl Home
bio_ens.pm
Go to the documentation of this file.
00001 =head1 LICENSE
00002 
00003   Copyright (c) 1999-2012 The European Bioinformatics Institute and
00004   Genome Research Limited.  All rights reserved.
00005 
00006   This software is distributed under a modified Apache license.
00007   For license details, please see
00008 
00009     http://www.ensembl.org/info/about/code_licence.html
00010 
00011 =head1 CONTACT
00012 
00013   Please email comments or questions to the public Ensembl
00014   developers list at <dev@ensembl.org>.
00015 
00016   Questions may also be sent to the Ensembl help desk at
00017   <helpdesk@ensembl.org>.
00018 
00019 =head1 AUTHOR
00020 
00021 Juguang Xiao <juguang@fugu-sg.org>
00022 
00023 =cut
00024 
00025 =head1 NAME
00026 
00027 Bio::EnsEMBL::Utils::Converter::bio_ens
00028 
00029 =head1 SYNOPISIS
00030 
00031 You should not use this module directly. Please check out the
00032 Bio::EnsEMBL::Utils::Converter module.
00033 
00034 =head1 DESCRIPTION
00035 
00036 =head1 METHODS
00037 
00038 =cut
00039 
00040 package Bio::EnsEMBL::Utils::Converter::bio_ens;
00041 
00042 use strict;
00043 use vars qw(@ISA);
00044 use Bio::EnsEMBL::Analysis;
00045 use Bio::EnsEMBL::DBSQL::DBAdaptor;
00046 use Bio::EnsEMBL::Utils::Converter;
00047 use Scalar::Util qw(weaken);
00048 @ISA = qw(Bio::EnsEMBL::Utils::Converter);
00049 
00050 =head2 new
00051 
00052 Please see Bio::EnsEMBL::Utils::Converter::new
00053 
00054 =cut
00055 
00056 sub new {
00057     my ($caller, @args) = @_;
00058     my $class = ref($caller) || $caller;
00059 
00060     if($class eq 'Bio::EnsEMBL::Utils::Converter::bio_ens'){
00061         my %params = @args;
00062         @params{map{lc $_} keys %params} = values %params;
00063         my $module = $class->_guess_module($params{-in}, $params{-out});
00064         return undef unless ($class->_load_module($module));
00065         return "$module"->new(@args);
00066     }else{
00067         my $self = $class->SUPER::new(@args);
00068 #        $self->_initialize(@args);
00069         return $self;
00070     }
00071 }
00072 
00073 sub _initialize {
00074     my ($self, @args) = @_;
00075     $self->SUPER::_initialize(@args);
00076     
00077     my ($dbadaptor, 
00078         $dbdriver, $dbhost, $dbport, $dbuser, $dbpass, $dbname,
00079         $analysis, $analysis_dbid, $analysis_logic_name, 
00080         $contig, $contig_dbid, $contig_name, 
00081         $translation_id) =
00082         
00083         $self->_rearrange([qw(DBADAPTOR 
00084             DBDRIVER DBHOST DBPORT DBUSER DBPASS DBNAME
00085             ANALYSIS ANALYSIS_DBID ANALYSIS_LOGIC_NAME 
00086             CONTIG CONTIG_DBID CONTIG_NAME
00087             TRANSLATION_ID)], @args);
00088 
00089     
00090     if(defined $dbadaptor){
00091         $self->dbadaptor($dbadaptor);
00092     }elsif(defined $dbname){
00093         $self->ensembl_db(@args);
00094     }else{
00095         # No db information.
00096     }
00097     
00098     if(defined $analysis){
00099         $self->analysis($analysis);
00100         # then ignore the analysis_dbid and analysis_logic_name
00101     }elsif(defined $analysis_dbid){
00102         $self->analysis_dbID($analysis_dbid);
00103     }elsif(defined $analysis_logic_name){
00104         $self->analysis_logic_name($analysis_logic_name);
00105     }else{
00106         # No analysis information offered
00107     }
00108     
00109     if(defined $contig){
00110         ($contig) = ref($contig) eq 'ARRAY' ? @{$contig} : $contig;
00111         $self->contig($contig);
00112     }elsif(defined $contig_dbid){
00113         $self->contig_dbID($contig_dbid);
00114     }elsif(defined $contig_name){
00115         $self->contig_name($contig_name);
00116     }else{
00117         # No contig information
00118     }
00119 
00120     if(defined $translation_id){
00121         $self->translation_id($translation_id);
00122     }
00123 }
00124 
00125 
00126 sub _guess_module {
00127     my ($self, $in, $out) = @_;
00128     my $tail;
00129     if($in eq 'Bio::Search::HSP::GenericHSP'){
00130         $tail = 'bio_ens_hsp';
00131     }elsif($in eq 'Bio::SeqFeature::Generic'){
00132         $tail = 'bio_ens_seqFeature';
00133     }elsif($in eq 'Bio::SeqFeature::FeaturePair'){
00134         $tail = 'bio_ens_featurePair';
00135     }elsif($in eq 'Bio::Pipeline::Analysis'){
00136         $tail = 'bio_ens_analysis';
00137     }elsif($in eq 'Bio::Tools::Prediction::Gene'){
00138         $tail = 'bio_ens_predictionGene';
00139     }elsif($in eq 'Bio::Tools::Prediction::Exon'){
00140         $tail = 'bio_ens_predictionExon';
00141     }elsif($in eq 'Bio::SeqFeature::Gene::GeneStructure'){
00142         $tail = 'bio_ens_gene';
00143     }elsif($in eq 'Bio::SeqFeature::Gene::Transcript'){
00144         $tail = 'bio_ens_transcript';
00145     }elsif($in eq 'Bio::SeqFeature::Gene::Exon'){
00146         $tail = 'bio_ens_exon';
00147     }else{
00148         $self->throw("[$in] to [$out], not supported");
00149     }
00150     return "Bio::EnsEMBL::Utils::Converter::$tail";
00151 }
00152 
00153 
00154 =head2 analysis
00155 
00156   Title   : analysis
00157   Usage   : $self->analysis
00158   Function: get and set for analysis
00159   Return  : L<Bio::EnsEMBL::Analysis>
00160   Args    : L<Bio::EnsEMBL::Analysis>   
00161 
00162 =cut
00163 
00164 sub analysis {
00165     my ($self, $arg) = @_;
00166     if(defined($arg)){
00167         # convert the analysis, if it's not Bio::Pipeline::Analysis
00168         if($arg->isa('Bio::Pipeline::Analysis')){
00169             my $converter_for_analysis = new Bio::EnsEMBL::Utils::Converter(
00170                 -in => 'Bio::Pipeline::Analysis',
00171                 -out => 'Bio::EnsEMBL::Analysis'
00172             );
00173             ($arg) = @{ $converter_for_analysis->convert([$arg]) };
00174         }
00175 
00176         $self->throws("A Bio::EnsEMBL::Analysis object expected.") 
00177             unless($arg->isa('Bio::EnsEMBL::Analysis'));
00178         $self->{_analysis} = $arg;
00179         $self->{_analysis_dbid} = $arg->dbID;
00180         $self->{_analysis_logic_name} = $arg->logic_name;
00181     }
00182     return $self->{_analysis};
00183 }
00184 
00185 
00186 =head2 contig
00187 
00188   Title   : contig
00189   Usage   : $self->contig
00190   Function: get and set for contig
00191   Return  : 
00192   Args    :    
00193 
00194 =cut
00195 
00196 sub contig {
00197     my ($self, $arg) = @_;
00198     if(defined($arg)){
00199         if($arg->isa('Bio::EnsEMBL::RawContig')){
00200             $self->{_contig_dbid} = $arg->dbID;
00201             $self->{_contig_name} = $arg->name;
00202         }elsif($arg->isa('Bio::EnsEMBL::Slice')){
00203                 $self->{_slice_dbid} = $arg->dbID;
00204         }elsif($arg->isa('Bio::PrimarySeqI')){
00205             ;
00206         }else{
00207             $self->throw("a Bio::EnsEMBL::RawContig needed");
00208         }
00209         $self->{_contig} = $arg;
00210         
00211     }
00212     return $self->{_contig};
00213 }
00214     
00215 =head2 dbadaptor
00216 
00217   Title   : dbadaptor
00218   Usage   : $self->dbadaptor
00219   Function: get and set for dbadaptor
00220   Return  : L<Bio::EnsEMBL::DBSQL::DBAdaptor>
00221   Args    : L<Bio::EnsEMBL::DBSQL::DBAdaptor>   
00222 
00223 =cut
00224 
00225 sub dbadaptor {
00226     my ($self, $arg) = @_;
00227     if(defined($arg)){
00228         $self->throws("A Bio::EnsEMBL::DBSQL::DBAdaptor object expected.") unless(defined $arg);
00229         weaken($self->{_dbadaptor} = $arg);
00230     }
00231     return $self->{_dbadaptor};
00232 }
00233 
00234 =head2 ensembl_db
00235 
00236   Title   : ensembl_db
00237   Usage   : 
00238   Function: 
00239   Return  :
00240   Args    :
00241 
00242 =cut
00243 
00244 sub ensembl_db {
00245     my ($self, @args) = @_;
00246     
00247     my ($dbdriver, $dbhost, $dbport, $dbuser, $dbpass, $dbname) = $self->_rearrange(
00248         [qw(DBDRIVER DBHOST DBPORT DBUSER DBPASS DBNAME)], @args);
00249 
00250     my $dbadaptor = new Bio::EnsEMBL::DBSQL::DBAdaptor(
00251         -driver => $dbdriver,
00252         -host => $dbhost,
00253         -port => $dbport,
00254         -user => $dbuser,
00255         -pass => $dbpass,
00256         -dbname => $dbname
00257     );
00258     $self->dbadaptor($dbadaptor);
00259 }
00260 
00261 =head2 analysis_dbID
00262 
00263   Title   : analysis_dbID
00264   Usage   : 
00265   Function: 
00266   Return  :
00267   Args    :
00268 
00269 =cut
00270 
00271 sub analysis_dbID {
00272     my ($self, $arg) = @_;
00273 
00274     if(defined $arg){
00275         my $analysis;
00276         eval{
00277             $analysis = $self->dbadaptor->get_AnalysisAdaptor->fetch_by_dbID($arg);
00278         };
00279         $self->throw("Failed during fetching analysis by dbID\n$@") if($@);
00280         $self->analysis($analysis);
00281     }
00282     $self->{_analysis_dbid};    
00283 }
00284 
00285 
00286 =head2 analysis_logic_name
00287 
00288   Title   : analysis_logic_name
00289   Usage   : 
00290   Function: 
00291   Return  :
00292   Args    :
00293 
00294 =cut
00295 
00296 sub analysis_logic_name {
00297     my ($self, $arg) = @_;
00298     
00299     return $self->{_analysis_logic_name} unless(defined $arg);
00300     my $analysis;
00301     eval{
00302         $analysis = 
00303             $self->dbadaptor->get_AnalysisAdaptor->fetch_by_logic_name($arg);
00304     };
00305     $self->throw("Not found analysis with logic name as \[$arg\]\n$@") if($@);
00306 
00307     $self->analysis($analysis);
00308     return $self->{_analysis_logic_name};
00309 }
00310 
00311 =head2 contig_dbID
00312 
00313   Title   : contig_dbID
00314   Usage   : $self->contig_dbID
00315   Function: get and set for contig_dbID
00316   Return  : 
00317   Args    :    
00318 
00319 =cut
00320 
00321 sub contig_dbID {
00322     my ($self, $arg) = @_;
00323     if(defined($arg)){
00324         my $contig;
00325         eval{
00326             $contig = 
00327                 $self->dbadaptor->get_RawContigAdaptor->fetch_by_dbID($arg);
00328         };
00329         $self->throw("Failed during fetching contig by dbID\n$@") if($@);
00330         $self->contig($contig);
00331     }
00332     return $self->{_contig_dbid};
00333 }
00334 
00335 =head2 contig_name
00336   Title   : contig_name
00337   Usage   : $self->contig_name
00338   Function: get and set for contig_name
00339   Return  : 
00340   Args    :    
00341 =cut
00342 
00343 sub contig_name {
00344     my ($self, $arg) = @_;
00345     if(defined($arg)){
00346         my $contig;
00347         eval{
00348             $contig = 
00349                 $self->dbadaptor->get_RawContigAdaptor->fetch_by_name($arg);
00350         };
00351         $self->throw("Failed during fetching contig by dbID\n$@") if($@);
00352         $self->contig($contig);
00353     }
00354     return $self->{_contig_name};
00355 }
00356 
00357 =head2 slice_dbID
00358   Title   : slice
00359   Usage   : $self->slice
00360   Function: get and set for slice
00361   Return  : L<Bio::EnsEMBL::Slice>
00362   Args    : L<Bio::EnsEMBL::Slice>   
00363 =cut
00364 
00365 sub slice_dbID {
00366     my ($self, $arg) = @_;
00367     if(defined($arg)){
00368         my $slice;
00369         $self->throw("undefined dbadpator") unless defined $self->dbadpaotr;
00370 
00371         eval{
00372             my $sliceAdaptor = $self->dbadaptor->get_SliceAdaptor;
00373             $slice = $sliceAdaptor->fetch_by_dbID($arg);
00374         };
00375             
00376         $self->throw("Failed to fetch slice by dbID\n$@") if($@);
00377         $self->contig($slice);
00378     }
00379 }
00380 
00381 =head2 slice_chr_start_end
00382   Title   : slice_chr_start_end
00383   Usage   : my $slice = $self->slice_chr_start_end($chr, $start, $end);
00384   Function: get and set for slice_chr_start_end
00385   Return  : 
00386   Args    :    
00387 =cut
00388 
00389 sub slice_chr_start_end {
00390     my ($self, $chr, $start, $end) = @_;
00391     if(defined($chr) && defined($start) && defined($end)){
00392         my $slice;
00393         eval{
00394             my $sliceAdaptor = $self->dbadaptor->get_SliceAdaptor;
00395             $slice = $sliceAdaptor->fetch_by_chr_start_end($chr, $start, $end);
00396         };
00397         $self->throw("Failed to fetch slice by chr start end\n$@") if($@);
00398         $self->contig($slice);
00399     }
00400 }
00401  
00402 sub translation_id {
00403     my ($self, $arg) = @_;
00404     return $self->{_translation_id} = $arg if(defined($arg));
00405     return $self->{_translation_id};
00406 }
00407 1;