Bio::DB::Universal: move into its own distribution
[bioperl-live.git] / Bio / DB / Failover.pm
blobfc40bdc37c3926d891145a7181899f92535263f0
2 # POD documentation - main docs before the code
4 =head1 NAME
6 Bio::DB::Failover - A Bio::DB::RandomAccessI compliant class which
7 wraps a prioritized list of DBs
9 =head1 SYNOPSIS
11 $failover = Bio::DB::Failover->new();
13 $failover->add_database($db);
15 # fail over Bio::DB::RandomAccessI.pm
17 # this will check each database in priority, returning when
18 # the first one succeeds
20 $seq = $failover->get_Seq_by_id($id);
22 =head1 DESCRIPTION
24 This module provides fail over access to a set of Bio::DB::RandomAccessI
25 objects.
27 =head1 CONTACT
29 Ewan Birney E<lt>birney@ebi.ac.ukE<gt> originally wrote this class.
31 =head2 Support
33 Please direct usage questions or support issues to the mailing list:
35 I<bioperl-l@bioperl.org>
37 rather than to the module maintainer directly. Many experienced and
38 reponsive experts will be able look at the problem and quickly
39 address it. Please include a thorough description of the problem
40 with code and data examples if at all possible.
42 =head2 Reporting Bugs
44 Report bugs to the Bioperl bug tracking system to help us keep track
45 the bugs and their resolution. Bug reports can be submitted via the
46 web:
48 https://github.com/bioperl/bioperl-live/issues
50 =head1 APPENDIX
52 The rest of the documentation details each of the object
53 methods. Internal methods are usually preceded with a _
55 =cut
57 # Let the code begin...
59 package Bio::DB::Failover;
61 use strict;
63 use base qw(Bio::Root::Root Bio::DB::RandomAccessI);
65 sub new {
66 my ($class,@args) = @_;
68 my $self = $class->SUPER::new(@args);
70 $self->{'_database'} = [];
71 return $self;
74 =head2 add_database
76 Title : add_database
77 Usage : add_database(%db)
78 Function: Adds a database to the Failover object
79 Returns : Count of number of databases
80 Args : Array of db resources
81 Throws : Not a RandomAccessI exception
83 =cut
85 sub add_database {
86 my ($self,@db) = @_;
87 for my $db ( @db ) {
88 if ( !ref $db || !$db->isa('Bio::DB::RandomAccessI') ) {
89 $self->throw("Database object $db is a not a Bio::DB::RandomAccessI");
90 next;
93 push(@{$self->{'_database'}},$db);
95 scalar @{$self->{'_database'}};
99 =head2 get_Seq_by_id
101 Title : get_Seq_by_id
102 Usage : $seq = $db->get_Seq_by_id('ROA1_HUMAN')
103 Function: Gets a Bio::Seq object by its name
104 Returns : a Bio::Seq object
105 Args : the id (as a string) of a sequence
106 Throws : "no id" exception
108 =cut
110 sub get_Seq_by_id {
111 my ($self,$id) = @_;
113 if( !defined $id ) {
114 $self->throw("no id is given!");
117 foreach my $db ( @{$self->{'_database'}} ) {
118 my $seq;
120 eval {
121 $seq = $db->get_Seq_by_id($id);
123 $self->warn($@) if $@;
124 if ( defined $seq ) {
125 return $seq;
126 } else {
127 $self->warn("No sequence retrieved by database " . ref($db));
131 return;
134 =head2 get_Seq_by_acc
136 Title : get_Seq_by_acc
137 Usage : $seq = $db->get_Seq_by_acc('X77802');
138 Function: Gets a Bio::Seq object by accession number
139 Returns : A Bio::Seq object
140 Args : accession number (as a string)
141 Throws : "no id" exception
143 =cut
145 sub get_Seq_by_acc {
146 my ($self,$id) = @_;
148 if( !defined $id ) {
149 $self->throw("no id is given!");
152 foreach my $db ( @{$self->{'_database'}} ) {
153 my $seq;
154 eval {
155 $seq = $db->get_Seq_by_acc($id);
157 $self->warn($@) if $@;
158 if ( defined $seq ) {
159 return $seq;
160 } else {
161 $self->warn("No sequence retrieved by database " . ref($db));
164 return;
167 =head2 get_Seq_by_version
169 Title : get_Seq_by_version
170 Usage : $seq = $db->get_Seq_by_acc('X77802.2');
171 Function: Gets a Bio::Seq object by versioned accession number
172 Returns : A Bio::Seq object
173 Args : accession number (as a string)
174 Throws : "acc does not exist" exception
176 =cut
178 sub get_Seq_by_version {
179 my ($self,$id) = @_;
181 if( !defined $id ) {
182 $self->throw("no acc is given!");
185 foreach my $db ( @{$self->{'_database'}} ) {
186 my $seq;
187 eval {
188 $seq = $db->get_Seq_by_version($id);
190 $self->warn($@) if $@;
191 if ( defined $seq ) {
192 return $seq;
193 } else {
194 $self->warn("No sequence retrieved by database " . ref($db));
197 return;
200 ## End of Package
204 __END__