Class: GeneCache
- Inherits:
-
Object
- Object
- GeneCache
- Defined in:
- lib/genecache.rb
Overview
TODO: check we have working sqlite3 executable for fast import of data
Constant Summary collapse
- @@db =
SQLite3::Database.new GeneCache.db_path
- @@tables =
@@db.execute("SELECT name FROM sqlite_master where type = 'table'")
Class Method Summary collapse
Class Method Details
.convert(species, from, to, id) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/genecache.rb', line 23 def self.convert(species, from, to, id) table = "#{species}_#{from}_#{to}" unless @@tables.include?([table]) # TODO: Update table sometimes! self.download!(species, from, to) end r = @@db.execute "select #{to} from #{table} where #{from} = '#{id}'" r.flatten end |
.db_path ⇒ Object
15 16 17 18 |
# File 'lib/genecache.rb', line 15 def self.db_path FileUtils.mkdir_p ENV['HOME'] + "/.genecache" return ENV['HOME'] + "/.genecache/db.sqlite3" end |
.download!(species, from, to) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/genecache.rb', line 34 def self.download!(species, from, to) table = "#{species}_#{from}_#{to}" $log.info "Downloading table: #{table}" @@db.execute "drop table if exists #{table}" @@db.execute "drop index if exists from_index" @@db.execute "drop index if exists to_index" url = "http://biodb.jp/tmp/#{table}.zip" zip_data = open(url).read Zip::Archive.open_buffer(zip_data) do |ar| ar.fopen(0) do |f| tmp = Tempfile.new 'biodb' data = f.read.split("\n") header = data.first.split throw "Invalid header!" if header.sort != [from, to].sort @@db.execute "create table #{table} (#{header.first} VARCHAR(255), #{header.last} VARCHAR(255))" rest = data[1..-5] * "\n" tmp.write rest tmp.close system(%$echo -e ".mode tabs \\n.import #{File.(tmp.path)} #{table}" | sqlite3 #{self.db_path}$) @@db.execute "create index from_index on #{table} (#{header.first})" @@db.execute "create index to_index on #{table} (#{header.last})" end # Update local list of tables @@tables = @@db.execute("SELECT name FROM sqlite_master where type = 'table' and name = '#{table}'") end end |