bug 2387
[bioperl-db.git] / Bio / DB / BioSQL / OBDA.pm
blob5be373a8e0e3c3e5b968566bbbaad23f5787af38
1 # $Id$
3 # BioPerl module for Bio::DB::BioSQL::OBDA
5 # Copyright Brian Osborne
7 # You may distribute this module under the same terms as perl itself
9 # POD documentation - main docs before the code
11 =head1 NAME
13 Bio::DB::BioSQL::OBDA
15 =head1 SYNOPSIS
17 This module is meant to be used a part of the OBDA system, e.g.:
19 use Bio::DB::Registry;
21 my $registry = Bio::DB::Registry->new;
22 my $db = $registry->get_database('biosql');
23 my $seq = $db->get_Seq_by_acc('P41932');
25 =head1 DESCRIPTION
27 This module connects code that uses OBDA to the bioperl-db package
28 and the underlying BioSQL database.
30 The Open Biological Database Access (OBDA) system was designed so that one
31 could use the same application code to access data from multiple database
32 types by simply changing a few lines in a configuration file. See
33 L<http://www.bioperl.org/wiki/HOWTO:OBDA> for more information.
35 =head1 FEEDBACK
37 =head2 Mailing Lists
39 User feedback is an integral part of the evolution of this
40 and other Bioperl modules. Send your comments and suggestions preferably
41 to one of the Bioperl mailing lists.
42 Your participation is much appreciated.
44 bioperl-l@bio.perl.org
46 =head2 Reporting Bugs
48 Report bugs to the Bioperl bug tracking system to help us keep track
49 the bugs and their resolution. Bug reports can be submitted via the web:
51 http://bugzilla.open-bio.org
53 =head1 AUTHOR - Brian Osborne
55 Email bosborne at alum.mit.edu
57 =head1 APPENDIX
59 The rest of the documentation details each of the object methods. Internal
60 methods are usually preceded with a _
62 =cut
64 # Let the code begin...
66 package Bio::DB::BioSQL::OBDA;
67 use strict;
68 use Bio::DB::Query::BioQuery;
69 use Bio::DB::BioDB;
70 use base qw(Bio::Root::Root Bio::DB::RandomAccessI);
72 =head2 new_from_registry
74 Title : new_from_registry
75 Usage :
76 Function: Create a database object that can be used by OBDA
77 Returns :
78 Args : Hash containing connection parameters read from an OBDA
79 registry file
81 =cut
83 sub new_from_registry {
84 my ($class, %conf) = @_;
85 my $self = $class->SUPER::new();
86 my ($host,$port);
87 # prevent warning msg by allowing location to be undef for postgresql 'ident sameuser' login)
88 if (defined $conf{'location'}) {
89 ($host,$port) = split ":", $conf{'location'};
91 my $db = Bio::DB::BioDB->new( -database => 'biosql',
92 -host => $host,
93 -port => $port,
94 -dbname => $conf{'dbname'},
95 -driver => $conf{'driver'},
96 -user => $conf{'user' },
97 -pass => $conf{'passwd'} );
98 $self->_db($db);
99 $self;
102 =head1 Methods inherited from Bio::DB::RandomAccessI
104 =head2 get_Seq_by_id
106 Title : get_Seq_by_id
107 Usage : $seq = $db->get_Seq_by_id(12345)
108 Function:
109 Example :
110 Returns : One or more Sequence objects
111 Args : An identifier
113 =cut
115 sub get_Seq_by_id {
116 my ($self,$id) = @_;
117 my $db = $self->_db;
118 my @seqs = ();
119 $self->throw("No identifier given") unless $id;
121 my $query = Bio::DB::Query::BioQuery->new(
122 -datacollections => ['Bio::SeqI seq'],
123 -where => ["seq.primary_id = $id"]);
125 my $seq_adaptor = $db->get_object_adaptor('Bio::SeqI');
126 my $result = $seq_adaptor->find_by_query($query);
128 for my $seq ($result->next_object) {
129 push @seqs,$seq;
131 return wantarray ? @seqs : $seqs[0];
134 =head2 get_Seq_by_acc
136 Title : get_Seq_by_acc
137 Usage : $seq = $db->get_Seq_by_acc('A12345')
138 Function:
139 Example :
140 Returns : One or more Sequence objects
141 Args : An accession number
143 =cut
145 sub get_Seq_by_acc {
146 my ($self,$acc) = @_;
147 my $db = $self->_db;
148 my @seqs = ();
149 $self->throw("No accession given") unless $acc;
151 my $query = Bio::DB::Query::BioQuery->new(
152 -datacollections => ['Bio::SeqI seq'],
153 -where => ["seq.accession_number = '$acc'"]);
155 my $seq_adaptor = $db->get_object_adaptor('Bio::SeqI');
156 my $result = $seq_adaptor->find_by_query($query);
158 for my $seq ($result->next_object) {
159 push @seqs,$seq;
161 return wantarray ? @seqs : $seqs[0];
164 =head2 get_Seq_by_version
166 Title : get_Seq_by_version
167 Usage : $seq = $db->get_Seq_by_version('A12345.3')
168 Function:
169 Example :
170 Returns : One or more Sequence objects
171 Args : A versioned accession number
173 =cut
175 sub get_Seq_by_version {
176 my ($self,$vacc) = @_;
177 my $db = $self->_db;
178 my @seqs = ();
179 my ($acc,$ver) = split /\./, $vacc; # split on period
180 $self->throw("Must supply a versioned accession: <accession>.<version>")
181 unless ($acc && $ver);
183 my $query = Bio::DB::Query::BioQuery->new(
184 -datacollections => ['Bio::SeqI seq'],
185 -where => ["seq.accession_number = '$acc'",
186 "seq.version = $ver"]);
188 my $seq_adaptor = $db->get_object_adaptor('Bio::SeqI');
189 my $result = $seq_adaptor->find_by_query($query);
191 for my $seq ($result->next_object) {
192 push @seqs,$seq;
194 return wantarray ? @seqs : $seqs[0];
197 =head1 Private methods
199 =head2 _db
201 Title : _db
202 Usage :
203 Function: Get or set the BioDB object
204 Example :
205 Returns :
206 Args :
208 =cut
210 sub _db {
211 my ($self,$db) = @_;
212 $self->{_db} = $db if ($db);
213 $self->{_db};
218 __END__