Bump RC to 2; will tag, bag, and ship tomorrow after tests
[bioperl-live.git] / Bio / DB / Failover.pm
blobcc89b8c730436e536e93641b08953b53fe17568e
1 # $Id$
3 # POD documentation - main docs before the code
5 =head1 NAME
7 Bio::DB::Failover - A Bio::DB::RandomAccessI compliant class which
8 wraps a prioritized list of DBs
10 =head1 SYNOPSIS
12 $failover = Bio::DB::Failover->new();
14 $failover->add_database($db);
16 # fail over Bio::DB::RandomAccessI.pm
18 # this will check each database in priority, returning when
19 # the first one succeeds
21 $seq = $failover->get_Seq_by_id($id);
23 =head1 DESCRIPTION
25 This module provides fail over access to a set of Bio::DB::RandomAccessI
26 objects.
28 =head1 CONTACT
30 Ewan Birney E<lt>birney@ebi.ac.ukE<gt> originally wrote this class.
32 =head2 Reporting Bugs
34 Report bugs to the Bioperl bug tracking system to help us keep track
35 the bugs and their resolution. Bug reports can be submitted via the
36 web:
38 http://bugzilla.open-bio.org/
40 =head1 APPENDIX
42 The rest of the documentation details each of the object
43 methods. Internal methods are usually preceded with a _
45 =cut
47 # Let the code begin...
49 package Bio::DB::Failover;
51 use strict;
53 use base qw(Bio::Root::Root Bio::DB::RandomAccessI);
55 sub new {
56 my ($class,@args) = @_;
58 my $self = $class->SUPER::new(@args);
60 $self->{'_database'} = [];
61 return $self;
64 =head2 add_database
66 Title : add_database
67 Usage : add_database(%db)
68 Function: Adds a database to the Failover object
69 Returns : Count of number of databases
70 Args : Array of db resources
71 Throws : Not a RandomAccessI exception
73 =cut
75 sub add_database {
76 my ($self,@db) = @_;
77 for my $db ( @db ) {
78 if ( !ref $db || !$db->isa('Bio::DB::RandomAccessI') ) {
79 $self->throw("Database object $db is a not a Bio::DB::RandomAccessI");
80 next;
83 push(@{$self->{'_database'}},$db);
85 scalar @{$self->{'_database'}};
89 =head2 get_Seq_by_id
91 Title : get_Seq_by_id
92 Usage : $seq = $db->get_Seq_by_id('ROA1_HUMAN')
93 Function: Gets a Bio::Seq object by its name
94 Returns : a Bio::Seq object
95 Args : the id (as a string) of a sequence
96 Throws : "no id" exception
98 =cut
100 sub get_Seq_by_id {
101 my ($self,$id) = @_;
103 if( !defined $id ) {
104 $self->throw("no id is given!");
107 foreach my $db ( @{$self->{'_database'}} ) {
108 my $seq;
110 eval {
111 $seq = $db->get_Seq_by_id($id);
113 $self->warn($@) if $@;
114 if ( defined $seq ) {
115 return $seq;
116 } else {
117 $self->warn("No sequence retrieved by database " . ref($db));
121 return;
124 =head2 get_Seq_by_acc
126 Title : get_Seq_by_acc
127 Usage : $seq = $db->get_Seq_by_acc('X77802');
128 Function: Gets a Bio::Seq object by accession number
129 Returns : A Bio::Seq object
130 Args : accession number (as a string)
131 Throws : "no id" exception
133 =cut
135 sub get_Seq_by_acc {
136 my ($self,$id) = @_;
138 if( !defined $id ) {
139 $self->throw("no id is given!");
142 foreach my $db ( @{$self->{'_database'}} ) {
143 my $seq;
144 eval {
145 $seq = $db->get_Seq_by_acc($id);
147 $self->warn($@) if $@;
148 if ( defined $seq ) {
149 return $seq;
150 } else {
151 $self->warn("No sequence retrieved by database " . ref($db));
154 return;
157 =head2 get_Seq_by_version
159 Title : get_Seq_by_version
160 Usage : $seq = $db->get_Seq_by_acc('X77802.2');
161 Function: Gets a Bio::Seq object by versioned accession number
162 Returns : A Bio::Seq object
163 Args : accession number (as a string)
164 Throws : "acc does not exist" exception
166 =cut
168 sub get_Seq_by_version {
169 my ($self,$id) = @_;
171 if( !defined $id ) {
172 $self->throw("no acc is given!");
175 foreach my $db ( @{$self->{'_database'}} ) {
176 my $seq;
177 eval {
178 $seq = $db->get_Seq_by_version($id);
180 $self->warn($@) if $@;
181 if ( defined $seq ) {
182 return $seq;
183 } else {
184 $self->warn("No sequence retrieved by database " . ref($db));
187 return;
190 ## End of Package
194 __END__