lib/bio/io/fetch.rb in bio-1.2.1 vs lib/bio/io/fetch.rb in bio-1.3.0

- old
+ new

@@ -3,11 +3,11 @@ # # Copyright:: Copyright (C) 2002, 2005 Toshiaki Katayama <k@bioruby.org>, # Copyright (C) 2006 Jan Aerts <jan.aerts@bbsrc.ac.uk> # License:: The Ruby License # -# $Id: fetch.rb,v 1.10 2007/04/05 23:35:41 trevor Exp $ +# $Id:$ # # == DESCRIPTION # # Using BioRuby BioFetch server # @@ -24,10 +24,11 @@ # puts Bio::Fetch.query('genbank', 'J00231') # puts Bio::Fetch.query('genbank', 'J00231', 'raw', 'fasta') # require 'uri' +require 'cgi' require 'bio/command' module Bio # = DESCRIPTION # The Bio::Fetch class provides an interface to dbfetch servers. Given @@ -100,15 +101,16 @@ # * _database_: name of database to query (see Bio::Fetch#databases to get list of supported databases) # * _id_: single ID or ID list separated by commas or white space # * _style_: [raw|html] (default = 'raw') # * _format_: name of output format (see Bio::Fetch#formats) def fetch(db, id, style = 'raw', format = nil) - query = [ "db=#{db}", "id=#{id}", "style=#{style}" ] - query.push("format=#{format}") if format - query = query.join('&') + query = [ [ 'db', db ], + [ 'id', id ], + [ 'style', style ] ] + query.push([ 'format', format ]) if format - Bio::Command.read_uri(@url + '?' + URI.escape(query)) + _get(query) end # Shortcut for using BioRuby's BioFetch server. You can fetch an entry # without creating an instance of BioFetch server. This method uses the # default dbfetch server, which is http://bioruby.org/cgi-bin/biofetch.rb @@ -137,13 +139,11 @@ # of databases available from the EBI, see the EBI website at # http://www.ebi.ac.uk/cgi-bin/dbfetch/ # --- # *Returns*:: array of database names def databases - query = "info=dbs" - - Bio::Command.read_uri(@url + '?' + URI.escape(query)).strip.split(/\s+/) + _get_single('info', 'dbs').strip.split(/\s+/) end # Lists the formats that are available for a given database. Like the # Bio::Fetch#databases method, this method is only available on # the bioruby dbfetch server. @@ -154,13 +154,13 @@ # *Arguments*: # * _database_:: name of database you want the supported formats for # *Returns*:: array of formats def formats(database = @database) if database - query = "info=formats;db=#{database}" - - Bio::Command.read_uri(@url + '?' + URI.escape(query)).strip.split(/\s+/) + query = [ [ 'info', 'formats' ], + [ 'db', database ] ] + _get(query).strip.split(/\s+/) end end # A dbfetch server will only return entries up to a given maximum number. # This method retrieves that number from the server. As for the databases @@ -168,14 +168,28 @@ # dbfetch server. # --- # *Arguments*: none # *Returns*:: number def maxids - query = "info=maxids" + _get_single('info', 'maxids').to_i + end - Bio::Command.read_uri(@url + '?' + URI.escape(query)).to_i + private + # (private) query to the server. + # ary must be nested array, e.g. [ [ key0, val0 ], [ key1, val1 ], ... ] + def _get(ary) + query = ary.collect do |a| + "#{CGI.escape(a[0])}=#{CGI.escape(a[1])}" + end.join('&') + Bio::Command.read_uri(@url + '?' + query) end - + + # (private) query with single parameter + def _get_single(key, val) + query = "#{CGI.escape(key)}=#{CGI.escape(val)}" + Bio::Command.read_uri(@url + '?' + query) + end + end end # module Bio