Skip to content

Commit

Permalink
New shell plugin ncbirest.rb to provide efetch etc.
Browse files Browse the repository at this point in the history
 * New shell plugin lib/bio/shell/plugin/ncbirest.rb, providing
   "efetch", "einfo", "esearch", and "esearch_count" methods.
   They act the same as those defined in Bio::NCBI::REST, except
   that efetch automatically selects databases if no options
   are given and only 1 id is given.
 * A shell command "getobj" is changed to use "efetch" method
   when "gb" or some variant is specified as the database.
  • Loading branch information
ngoto committed Jan 26, 2009
1 parent a3941e7 commit c482e18
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/bio/shell.rb
Expand Up @@ -5,7 +5,7 @@
# Toshiaki Katayama <k@bioruby.org>
# License:: The Ruby License
#
# $Id: shell.rb,v 1.20 2007/07/09 11:17:09 k Exp $
# $Id:$
#

require 'bio'
Expand Down Expand Up @@ -36,6 +36,7 @@ module Bio::Shell
require 'bio/shell/plugin/emboss'
require 'bio/shell/plugin/blast'
require 'bio/shell/plugin/psort'
require 'bio/shell/plugin/ncbirest'

extend Ghost

Expand Down
12 changes: 9 additions & 3 deletions lib/bio/shell/plugin/entry.rb
Expand Up @@ -5,7 +5,7 @@
# Toshiaki Katayama <k@bioruby.org>
# License:: The Ruby License
#
# $Id: entry.rb,v 1.10 2007/04/05 23:35:41 trevor Exp $
# $Id:$
#

module Bio::Shell
Expand Down Expand Up @@ -88,8 +88,14 @@ def getent(arg)

# KEGG API at http://www.genome.jp/kegg/soap/
else
puts "Retrieving entry from KEGG API (#{arg})"
entry = bget(arg)
case db.to_s.downcase
when 'genbank', 'gb', 'embl', 'emb', 'ddbj', 'dbj'
puts "Retrieving entry from NCBI eUtils"
entry = efetch(entry_id)
else
puts "Retrieving entry from KEGG API (#{arg})"
entry = bget(arg)
end
end
end

Expand Down
66 changes: 66 additions & 0 deletions lib/bio/shell/plugin/ncbirest.rb
@@ -0,0 +1,66 @@
#
# = bio/shell/plugin/ncbirest.rb - plugin for NCBI eUtils
#
# Copyright:: Copyright (C) 2009
# Naohisa Goto <ng@bioruby.org>
# License:: The Ruby License
#
# $Id:$
#

module Bio::Shell

private

# NCBI eUtils EFetch service.
# When two or more arguments are given, or multiple accession numbers
# are given it acts the same as Bio::NCBI::REST.efetch.
# Otherwise, assumes nucleotide or protein accessin is given, and
# automatically tries several databases.
def efetch(ids, *arg)
if !arg.empty? or ids.kind_of?(Array) or /\,/ =~ ids then
return Bio::NCBI::REST.efetch(ids, *arg)
end

rettype = 'gb'
prot_dbs = [ 'protein' ]
nucl_dbs = [ 'nuccore', 'nucleotide', 'nucgss', 'nucest' ]

case ids
when /\A[A-Z][A-Z][A-Z][0-9]+(\.[0-9]+)?\z/i,
/\A[OPQ][A-Z0-9]+(\.[0-9]+)?\z/i
# protein accession
dbs = prot_dbs
when /\A[0-9]+\z/, /\A[A-Z0-9]+\_[A-Z0-9]+\z/i
# NCBI GI or UniProt accession (with fail-safe)
dbs = prot_dbs + nucl_dbs
else
# nucleotide accession
dbs = nucl_dbs
end
result = nil
dbs.each do |db|
hash = { 'db' => db, 'rettype' => 'gb' }
result = Bio::NCBI::REST.efetch(ids, hash)
break if result and !result.empty?
end
result
end

# NCBI eUtils EInfo
def einfo
Bio::NCBI::REST.einfo
end

# NCBI eUtils ESearch
def esearch(str, *arg)
Bio::NCBI::REST.esearch(str, *arg)
end

# Same as Bio::NCBI::REST.esearch_count
def esearch_count(str, *arg)
Bio::NCBI::REST.esearch_count(str, *arg)
end

end

0 comments on commit c482e18

Please sign in to comment.