TogoGenomeRDFDownload
提供:TogoWiki
(版間での差分)
(ページの作成:「== RefSeq の RDF データの生物系統ダウンロード == 要望:RefSeq をコンバートしたデータから Cyanobacteria (1117) だけのサブセットを...」) |
(→Sinatra::Streaming) |
||
| 47行: | 47行: | ||
=== [http://www.sinatrarb.com/contrib/streaming.html Sinatra::Streaming] === | === [http://www.sinatrarb.com/contrib/streaming.html Sinatra::Streaming] === | ||
| - | |||
| - | |||
デフォルトではデータはすべてバッファリングされるので巨大データのダウンロードには Sinatra Streaming を使う必要がある。 | デフォルトではデータはすべてバッファリングされるので巨大データのダウンロードには Sinatra Streaming を使う必要がある。 | ||
| + | |||
| + | * [http://www.xmisao.com/2013/07/03/sinatra-streaming-is-not-working-on-webrick.html Sinatra::Streamingの使い方と注意点] | ||
<pre> | <pre> | ||
2014年11月17日 (月) 08:03時点における最新版
目次 |
RefSeq の RDF データの生物系統ダウンロード
要望:RefSeq をコンバートしたデータから Cyanobacteria (1117) だけのサブセットをとりたい
http://togogenome.org/rdf/togogenome/refseq/release67/prokaryotes.ttl/
指定された taxid の subClassOf を SPARQL 検索し、該当 taxid のファイルがあれば cat して返すような CGI を作れば良さそう
- Nginx は CGI をサポートしていないみたいなので fcgi にするかアプリを作るしかない
- Rack/Sinatra + Unicorn でサーバをたてることに
% gem install sinatra % gem install unicorn
Sinatra アプリの開発
get '/download/refseq/:taxid' do
parent = params[:taxid].to_i
result = ""
turtle = ""
sparql = SPARQL_TEMP.sub('@@TAXID@@', parent.to_s)
SPARQL_SERV.query(sparql, :format => 'json') { |x|
result << x
}
flag = true
data = ""
json = JSON.parse(result)
json["results"]["bindings"].each do |x|
taxid = x["taxid"]["value"][/\d+$/]
Dir.glob("#{REFSEQ_PATH}/#{taxid}/**/*.ttl").each do |file|
File.open(file).each do |line|
if line[/^@/]
data += line if flag
else
data += line
end
end
flag = false
end
end
data
end
Sinatra::Streaming
デフォルトではデータはすべてバッファリングされるので巨大データのダウンロードには Sinatra Streaming を使う必要がある。
% gem install sinatra-contrib
基本的には stream { |out| out << data } でよいみたい。rackup では使えないようなので Unicorn で起動する。
require 'rubygems'
require 'sinatra'
require 'sinatra/base'
require 'sinatra/streaming' # gem install sinatra-contrib
require 'rack'
require 'json'
load 'sparql.rb'
class Download < Sinatra::Base
SPARQL_HOST = "http://togogenome.org/sparql"
SPARQL_SERV = SPARQL.new(SPARQL_HOST)
# GRAPH <http://togogenome.org/graph/taxonomy> {
SPARQL_TEMP = <<-SPARQL
SELECT *
WHERE {
GRAPH <http://togogenome.org/graph/taxonomy/> {
?taxid rdfs:subClassOf* <http://identifiers.org/taxonomy/@@TAXID@@> .
}
}
SPARQL
REFSEQ_PATH = "/data/store/rdf/togogenome/refseq/current/prokaryotes.ttl"
get '/download/refseq/:taxid' do
parent = params[:taxid][/\d+$/].to_i
result = ""
turtle = ""
sparql = SPARQL_TEMP.sub('@@TAXID@@', parent.to_s)
SPARQL_SERV.query(sparql, :format => 'json') { |x|
result << x
}
stream do |out|
flag = true
json = JSON.parse(result)
json["results"]["bindings"].each do |x|
taxid = x["taxid"]["value"][/\d+$/]
Dir.glob("#{REFSEQ_PATH}/#{taxid}/**/*.ttl").each do |file|
if flag
out << `cat #{file}`
flag = false
else
out << `grep -v '^@' #{file}`
end
out.flush
end
end
end
end
end
run Download
Unicorn の設定ファイル
worker_processes 2
#listen '/data/store/download/tmp/unicorn.sock'
listen 20999
stderr_path File.expand_path('log/unicorn.err', File.dirname(__FILE__))
stdout_path File.expand_path('log/unicorn.log', File.dirname(__FILE__))
preload_app true
デーモンとして起動する(終了は kill)
% unicorn -d unicorn.con -D
Nginx の設定
Unicorn なので UNIX ソケットを使う方法もあるが、ここでは↑でポート指定した HTTP にアクセスする。
location /download {
proxy_pass http://localhost:20999;
}