Hi Theresa!
I am surprised that there are still people trying to do bioinformatics with Ruby. Besides me.
Perhaps
if link.uri.to_s =~ /dbget-bin/
links << link
end
is not working as expected. Because link.uri.to_s does not include dbget-bin.
I think what you are trying to do is scraping. Scraping is an important technique, but it is complicated to perform. Here I recommend using TogoWS.
http://togows.dbcls.jp/

#! /usr/bin/env ruby
require 'open-uri'
require 'json'
require 'optparse'
require 'tty-progressbar'
def ko2genes(koid)
url = "http://togows.org/entry/kegg-orthology/#{koid}.json"
tf = URI.open(url)
ko = JSON.parse(tf.read)
ko[0]['genes'].map do |k, v|
v.map do |i|
k + ':' + i
end
end
end
def gene2ntseq(genes, n = 20, interval: 1)
bar = TTY::ProgressBar.new('gene2ntseq [:bar] :current/:total :percent ET::elapsed ETA::eta :rate/s',
total: genes.size)
genes.each_slice(n).map do |s_genes|
gs = s_genes.join(',')
url = "http://togows.org/entry/kegg-genes/#{gs}/ntseq.json"
tf = URI.open(url)
ary = JSON.parse(tf.read)
raise if ary.size != s_genes.size
result = s_genes.zip(ary)
sleep(interval)
bar.advance(n)
result
end.flatten(1)
end
opt = OptionParser.new
@n = 20
@interval = 1.0
opt.banner = "Usage: ruby #{$0} [options] <ko>"
opt.on('-n INT', Numeric, 'number of sequences to fetch at one time [20]') { |v| @n = v }
opt.on('-i SEQ', '--interval', Float, 'interval to connect to server (seconds) [1.0]') { |v| @interval = v }
opt.parse!(ARGV)
if ARGV.empty?
puts opt.help
exit
end
# get all genes
genes = ko2genes(ARGV[0]).flatten
# get all seqs
seqs = gene2ntseq(genes, @n, interval: @interval)
# output as FASTA
seqs.each do |n, s|
puts ">#{n}"
puts s.scan(/.{1,80}/)
end
Use with caution, as there is a good chance that bugs may remain in the script as it has not been tested.
ruby this_scritp.rb K01505 > k01505.fasta
ruby this_scritp.rb -n 40 K01505 > k01505.fasta # faster