ensembl-hive  2.8.1
bio_ens.pm
Go to the documentation of this file.
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@fugu-sg.org>
32 
33 =cut
34 
35 =head1 NAME
36 
38 
39 =head1 SYNOPISIS
40 
41 You should not use this module directly. Please check out the
43 
44 =head1 DESCRIPTION
45 
46 =head1 METHODS
47 
48 =cut
49 
50 package Bio::EnsEMBL::Utils::Converter::bio_ens;
51 
52 use strict;
53 use vars qw(@ISA);
57 use Scalar::Util qw(weaken);
59 
60 =head2 new
61 
63 
64 =cut
65 
66 sub new {
67  my ($caller, @args) = @_;
68  my $class = ref($caller) || $caller;
69 
70  if($class eq 'Bio::EnsEMBL::Utils::Converter::bio_ens'){
71  my %params = @args;
72  @params{map{lc $_} keys %params} = values %params;
73  my $module = $class->_guess_module($params{-in}, $params{-out});
74  return undef unless ($class->_load_module($module));
75  return "$module"->new(@args);
76  }else{
77  my $self = $class->SUPER::new(@args);
78 # $self->_initialize(@args);
79  return $self;
80  }
81 }
82 
83 sub _initialize {
84  my ($self, @args) = @_;
85  $self->SUPER::_initialize(@args);
86 
87  my ($dbadaptor,
88  $dbdriver, $dbhost, $dbport, $dbuser, $dbpass, $dbname,
89  $analysis, $analysis_dbid, $analysis_logic_name,
90  $contig, $contig_dbid, $contig_name,
91  $translation_id) =
92 
93  $self->_rearrange([qw(DBADAPTOR
94  DBDRIVER DBHOST DBPORT DBUSER DBPASS DBNAME
95  ANALYSIS ANALYSIS_DBID ANALYSIS_LOGIC_NAME
96  CONTIG CONTIG_DBID CONTIG_NAME
97  TRANSLATION_ID)], @args);
98 
99 
100  if(defined $dbadaptor){
101  $self->dbadaptor($dbadaptor);
102  }elsif(defined $dbname){
103  $self->ensembl_db(@args);
104  }else{
105  # No db information.
106  }
107 
108  if(defined $analysis){
109  $self->analysis($analysis);
110  # then ignore the analysis_dbid and analysis_logic_name
111  }elsif(defined $analysis_dbid){
112  $self->analysis_dbID($analysis_dbid);
113  }elsif(defined $analysis_logic_name){
114  $self->analysis_logic_name($analysis_logic_name);
115  }else{
116  # No analysis information offered
117  }
118 
119  if(defined $contig){
120  ($contig) = ref($contig) eq 'ARRAY' ? @{$contig} : $contig;
121  $self->contig($contig);
122  }elsif(defined $contig_dbid){
123  $self->contig_dbID($contig_dbid);
124  }elsif(defined $contig_name){
125  $self->contig_name($contig_name);
126  }else{
127  # No contig information
128  }
129 
130  if(defined $translation_id){
131  $self->translation_id($translation_id);
132  }
133 }
134 
135 
136 sub _guess_module {
137  my ($self, $in, $out) = @_;
138  my $tail;
139  if($in eq 'Bio::Search::HSP::GenericHSP'){
140  $tail = 'bio_ens_hsp';
141  }elsif($in eq 'Bio::SeqFeature::Generic'){
142  $tail = 'bio_ens_seqFeature';
143  }elsif($in eq 'Bio::SeqFeature::FeaturePair'){
144  $tail = 'bio_ens_featurePair';
145  }elsif($in eq 'Bio::Pipeline::Analysis'){
146  $tail = 'bio_ens_analysis';
147  }elsif($in eq 'Bio::Tools::Prediction::Gene'){
148  $tail = 'bio_ens_predictionGene';
149  }elsif($in eq 'Bio::Tools::Prediction::Exon'){
150  $tail = 'bio_ens_predictionExon';
151  }elsif($in eq 'Bio::SeqFeature::Gene::GeneStructure'){
152  $tail = 'bio_ens_gene';
153  }elsif($in eq 'Bio::SeqFeature::Gene::Transcript'){
154  $tail = 'bio_ens_transcript';
155  }elsif($in eq 'Bio::SeqFeature::Gene::Exon'){
156  $tail = 'bio_ens_exon';
157  }else{
158  $self->throw("[$in] to [$out], not supported");
159  }
160  return "Bio::EnsEMBL::Utils::Converter::$tail";
161 }
162 
163 
164 =head2 analysis
165 
166  Title : analysis
167  Usage : $self->analysis
168  Function: get and set for analysis
169  Return : L<Bio::EnsEMBL::Analysis>
170  Args : L<Bio::EnsEMBL::Analysis>
171 
172 =cut
173 
174 sub analysis {
175  my ($self, $arg) = @_;
176  if(defined($arg)){
177  # convert the analysis, if it's not Bio::Pipeline::Analysis
178  if($arg->isa('Bio::Pipeline::Analysis')){
179  my $converter_for_analysis = new Bio::EnsEMBL::Utils::Converter(
180  -in => 'Bio::Pipeline::Analysis',
181  -out => 'Bio::EnsEMBL::Analysis'
182  );
183  ($arg) = @{ $converter_for_analysis->convert([$arg]) };
184  }
185 
186  $self->throws("A Bio::EnsEMBL::Analysis object expected.")
187  unless($arg->isa('Bio::EnsEMBL::Analysis'));
188  $self->{_analysis} = $arg;
189  $self->{_analysis_dbid} = $arg->dbID;
190  $self->{_analysis_logic_name} = $arg->logic_name;
191  }
192  return $self->{_analysis};
193 }
194 
195 
196 =head2 contig
197 
198  Title : contig
199  Usage : $self->contig
200  Function: get and set for contig
201  Return :
202  Args :
203 
204 =cut
205 
206 sub contig {
207  my ($self, $arg) = @_;
208  if(defined($arg)){
209  if($arg->isa('Bio::EnsEMBL::RawContig')){
210  $self->{_contig_dbid} = $arg->dbID;
211  $self->{_contig_name} = $arg->name;
212  }elsif($arg->isa('Bio::EnsEMBL::Slice')){
213  $self->{_slice_dbid} = $arg->dbID;
214  }elsif($arg->isa('Bio::PrimarySeqI')){
215  ;
216  }else{
217  $self->throw("a Bio::EnsEMBL::RawContig needed");
218  }
219  $self->{_contig} = $arg;
220 
221  }
222  return $self->{_contig};
223 }
224 
225 =head2 dbadaptor
226 
227  Title : dbadaptor
228  Usage : $self->dbadaptor
229  Function: get and set for dbadaptor
230  Return : L<Bio::EnsEMBL::DBSQL::DBAdaptor>
231  Args : L<Bio::EnsEMBL::DBSQL::DBAdaptor>
232 
233 =cut
234 
235 sub dbadaptor {
236  my ($self, $arg) = @_;
237  if(defined($arg)){
238  $self->throws("A Bio::EnsEMBL::DBSQL::DBAdaptor object expected.") unless(defined $arg);
239  weaken($self->{_dbadaptor} = $arg);
240  }
241  return $self->{_dbadaptor};
242 }
243 
244 =head2 ensembl_db
245 
246  Title : ensembl_db
247  Usage :
248  Function:
249  Return :
250  Args :
251 
252 =cut
253 
254 sub ensembl_db {
255  my ($self, @args) = @_;
256 
257  my ($dbdriver, $dbhost, $dbport, $dbuser, $dbpass, $dbname) = $self->_rearrange(
258  [qw(DBDRIVER DBHOST DBPORT DBUSER DBPASS DBNAME)], @args);
259 
260  my $dbadaptor = new Bio::EnsEMBL::DBSQL::DBAdaptor(
261  -driver => $dbdriver,
262  -host => $dbhost,
263  -port => $dbport,
264  -user => $dbuser,
265  -pass => $dbpass,
266  -dbname => $dbname
267  );
268  $self->dbadaptor($dbadaptor);
269 }
270 
271 =head2 analysis_dbID
272 
273  Title : analysis_dbID
274  Usage :
275  Function:
276  Return :
277  Args :
278 
279 =cut
280 
281 sub analysis_dbID {
282  my ($self, $arg) = @_;
283 
284  if(defined $arg){
285  my $analysis;
286  eval{
287  $analysis = $self->dbadaptor->get_AnalysisAdaptor->fetch_by_dbID($arg);
288  };
289  $self->throw("Failed during fetching analysis by dbID\n$@") if($@);
290  $self->analysis($analysis);
291  }
292  $self->{_analysis_dbid};
293 }
294 
295 
296 =head2 analysis_logic_name
297 
298  Title : analysis_logic_name
299  Usage :
300  Function:
301  Return :
302  Args :
303 
304 =cut
305 
306 sub analysis_logic_name {
307  my ($self, $arg) = @_;
308 
309  return $self->{_analysis_logic_name} unless(defined $arg);
310  my $analysis;
311  eval{
312  $analysis =
313  $self->dbadaptor->get_AnalysisAdaptor->fetch_by_logic_name($arg);
314  };
315  $self->throw("Not found analysis with logic name as \[$arg\]\n$@") if($@);
316 
317  $self->analysis($analysis);
318  return $self->{_analysis_logic_name};
319 }
320 
321 =head2 contig_dbID
322 
323  Title : contig_dbID
324  Usage : $self->contig_dbID
325  Function: get and set for contig_dbID
326  Return :
327  Args :
328 
329 =cut
330 
331 sub contig_dbID {
332  my ($self, $arg) = @_;
333  if(defined($arg)){
334  my $contig;
335  eval{
336  $contig =
337  $self->dbadaptor->get_RawContigAdaptor->fetch_by_dbID($arg);
338  };
339  $self->throw("Failed during fetching contig by dbID\n$@") if($@);
340  $self->contig($contig);
341  }
342  return $self->{_contig_dbid};
343 }
344 
345 =head2 contig_name
346  Title : contig_name
347  Usage : $self->contig_name
348  Function: get and set for contig_name
349  Return :
350  Args :
351 =cut
352 
353 sub contig_name {
354  my ($self, $arg) = @_;
355  if(defined($arg)){
356  my $contig;
357  eval{
358  $contig =
359  $self->dbadaptor->get_RawContigAdaptor->fetch_by_name($arg);
360  };
361  $self->throw("Failed during fetching contig by dbID\n$@") if($@);
362  $self->contig($contig);
363  }
364  return $self->{_contig_name};
365 }
366 
367 =head2 slice_dbID
368  Title : slice
369  Usage : $self->slice
370  Function: get and set for slice
371  Return : L<Bio::EnsEMBL::Slice>
372  Args : L<Bio::EnsEMBL::Slice>
373 =cut
374 
375 sub slice_dbID {
376  my ($self, $arg) = @_;
377  if(defined($arg)){
378  my $slice;
379  $self->throw("undefined dbadpator") unless defined $self->dbadpaotr;
380 
381  eval{
382  my $sliceAdaptor = $self->dbadaptor->get_SliceAdaptor;
383  $slice = $sliceAdaptor->fetch_by_dbID($arg);
384  };
385 
386  $self->throw("Failed to fetch slice by dbID\n$@") if($@);
387  $self->contig($slice);
388  }
389 }
390 
391 =head2 slice_chr_start_end
392  Title : slice_chr_start_end
393  Usage : my $slice = $self->slice_chr_start_end($chr, $start, $end);
394  Function: get and set for slice_chr_start_end
395  Return :
396  Args :
397 =cut
398 
399 sub slice_chr_start_end {
400  my ($self, $chr, $start, $end) = @_;
401  if(defined($chr) && defined($start) && defined($end)){
402  my $slice;
403  eval{
404  my $sliceAdaptor = $self->dbadaptor->get_SliceAdaptor;
405  $slice = $sliceAdaptor->fetch_by_chr_start_end($chr, $start, $end);
406  };
407  $self->throw("Failed to fetch slice by chr start end\n$@") if($@);
408  $self->contig($slice);
409  }
410 }
411 
412 sub translation_id {
413  my ($self, $arg) = @_;
414  return $self->{_translation_id} = $arg if(defined($arg));
415  return $self->{_translation_id};
416 }
417 1;
Bio::EnsEMBL::DBSQL::DBAdaptor
Definition: DBAdaptor.pm:40
map
public map()
Bio::EnsEMBL::Utils::Converter::bio_ens
Definition: bio_ens.pm:12
Bio::EnsEMBL::Utils::Converter
Definition: bio_ens.pm:8
Bio::EnsEMBL::Analysis
Definition: PairAlign.pm:3
Bio::EnsEMBL::Utils::Converter::new
public L new()