Created
December 14, 2017 07:12
-
-
Save ddbj-repo/aa94320d3ab88ae27e53371868885cfd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- ruby -*- | |
require "json" | |
require "net/http" | |
require "uri" | |
class BlastCondition | |
attr_accessor :params | |
def initialize | |
self.params = { | |
"querySequence" => nil, | |
"datasets" => nil, | |
"database" => nil, | |
"program" => nil, | |
"parameters" => nil, | |
"format" => nil, | |
"result" => nil, | |
"address" => nil, | |
} | |
end | |
end | |
WABI_URL_ROOT = "http://ddbj.nig.ac.jp/wabi" | |
GET_INTERVAL = 1.0 # Note: 1.0 (sec) | |
def read_blast_condition | |
cond = BlastCondition.new | |
cond.params["querySequence"] = File.read("blast_condition.fasta") | |
File.foreach("blast_condition.txt") do |line| | |
tokens = line.chomp.split("\t") | |
next unless cond.params.has_key?(tokens[0]) | |
cond.params[tokens[0]] = tokens[1] | |
end | |
cond | |
end | |
def post_to_wabi(blast_condition) | |
url = URI.parse("#{WABI_URL_ROOT}/blast") | |
res = Net::HTTP.post_form(url, blast_condition.params) | |
response = JSON.parse(res.body) | |
case res | |
when Net::HTTPSuccess | |
return response["requestId"] | |
when Net::HTTPClientError | |
warn "BLAST condition errors: #{response['error-messages'].inspect}" | |
exit 1 | |
else | |
warn "WABI server error" | |
exit 2 | |
end | |
end | |
def get_status(request_id) | |
url = URI.parse("#{WABI_URL_ROOT}/blast/#{request_id}?info=status&format=json") | |
while true | |
sleep GET_INTERVAL | |
res = Net::HTTP.get_response(url) | |
case res | |
when Net::HTTPSuccess | |
response = JSON.parse(res.body) | |
case response["status"] | |
when "waiting" | |
puts "#{Time.now}: waiting" | |
next | |
when "running" | |
puts "#{Time.now}: running" | |
next | |
when "finished" | |
puts "#{Time.now}: finished" | |
return | |
else | |
warn "No such Request-id. time-out" | |
exit 3 | |
end | |
when Net::HTTPClientError | |
warn "No such Request-id. Cause: time-out or wrong Request-ID" | |
exit 3 | |
else | |
warn "WABI server error" | |
exit 2 | |
end | |
end | |
end | |
def get_result(request_id) | |
url = URI.parse("#{WABI_URL_ROOT}/blast/#{request_id}?info=result&format=bigfile") | |
res = Net::HTTP.get_response(url) | |
case res | |
when Net::HTTPSuccess | |
return res.body | |
when Net::HTTPClientError | |
warn "BLAST result not found. Cause: time-out, not-finished, or wrong Request-ID" | |
exit 4 | |
else | |
warn "WABI server error" | |
exit 2 | |
end | |
end | |
def main | |
blast_condition = read_blast_condition | |
puts "Load BLAST condition" | |
request_id = post_to_wabi(blast_condition) | |
puts "POST : success (Request-ID=#{request_id})" | |
get_status request_id | |
puts "Request : finished" | |
blast_result = get_result(request_id) | |
puts "GET BLAST result : success" | |
File.open("#{request_id}.txt", "w") do |out| | |
out.puts blast_result | |
end | |
end | |
main | |
# example.rb ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment