class Bio::FlatFileIndex::DataBank

databank

Internal use only.

Attributes

always_check[R]
dbname[R]
index_type[R]

Public Class Methods

filename(dbname) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1131
def self.filename(dbname)
  File.join(dbname, 'config.dat')
end
new(name, idx_type = nil, hash = {}) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1148
def initialize(name, idx_type = nil, hash = {})
  @dbname = name.dup
  @dbname.freeze
  @bdb = nil

  @always_check = true
  self.index_type = (hash['index'] or idx_type)

  if @bdb then
    @config = BDBwrapper.new(@dbname, 'config')
    @bdb_fileids = BDBwrapper.new(@dbname, 'fileids')
    @nsclass_pri = BDB_1::PrimaryNameSpace
    @nsclass_sec = BDB_1::SecondaryNameSpace
  else
    @config = hash
    @nsclass_pri = Flat_1::PrimaryNameSpace
    @nsclass_sec = Flat_1::SecondaryNameSpace
  end
  true
end
open(*arg) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1144
def self.open(*arg)
  self.read(*arg)
end
read(name, mode = 'rb', *bdbarg) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1135
def self.read(name, mode = 'rb', *bdbarg)
  f = File.open(filename(name), mode)
  hash = file2hash(f)
  f.close
  db = self.new(name, nil, hash)
  db.bdb_open(*bdbarg)
  db
end

Public Instance Methods

always_check=(bool) click to toggle source

high level methods

# File lib/bio/io/flatfile/index.rb, line 1305
def always_check=(bool)
  if bool then
    @always_check = true
  else
    @always_check = false
  end
end
bdb_open(*bdbarg) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1204
def bdb_open(*bdbarg)
  if @bdb then
    @config.close
    @config.open(*bdbarg)
    @bdb_fileids.close
    @bdb_fileids.open(*bdbarg)
    true
  else
    nil
  end
end
check_consistency() click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1374
def check_consistency
  fileids.check_all
end
close() click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1234
def close
  DEBUG.print "DataBank: close #{@dbname}\n"
  primary.close
  secondary.close
  fileids.close
  if @bdb then
    @config.close
    @bdb_fileids.close
  end
  nil
end
fileids() click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1288
def fileids
  unless @fileids then
    init_fileids
  end
  @fileids
end
format() click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1281
def format
  unless @format then
    self.format = @config['format']
  end
  @format
end
format=(str) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1277
def format=(str)
  @format = str.to_s.dup
end
get_flatfile_data(f, pos, length) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1314
def get_flatfile_data(f, pos, length)
  fi = fileids[f.to_i]
  if @always_check then
    raise "flatfile #{fi.filename.inspect} may be modified" unless fi.check
  end
  fi.get(pos.to_i, length.to_i)
end
index_type=(str) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1171
def index_type=(str)
  case str
  when MAGIC_BDB
    @index_type = MAGIC_BDB
    @bdb = true
    unless defined?(BDB)
      raise RuntimeError, "Berkeley DB support not found"
    end
  when MAGIC_FLAT, '', nil, false
    @index_type = MAGIC_FLAT
    @bdb = false
  else
    raise 'unknown or unsupported index type'
  end
end
init_fileids() click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1295
def init_fileids
  if @bdb then
    @fileids = FileIDs.new('', @bdb_fileids)
  else
    @fileids = FileIDs.new('fileid_', @config)
  end
  @fileids
end
primary() click to toggle source

parameters

# File lib/bio/io/flatfile/index.rb, line 1247
def primary
  unless @primary then
    self.primary = @config['primary_namespace']
  end
  @primary
end
primary=(pri_name) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1254
def primary=(pri_name)
  if !pri_name or pri_name.empty? then
    pri_name = 'UNIQUE'
  end
  @primary = @nsclass_pri.new(@dbname, pri_name)
  @primary
end
search_all(key) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1341
def search_all(key)
  s = search_all_get_unique_id(key)
  search_primary(*s)
end
search_all_get_unique_id(key) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1322
def search_all_get_unique_id(key)
  s = secondary.search(key)
  p = primary.include?(key)
  s.push p if p
  s.sort!
  s.uniq!
  s
end
search_namespaces(key, *names) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1369
def search_namespaces(key, *names)
  s = search_namespaces_get_unique_id(key, *names)
  search_primary(*s)
end
search_namespaces_get_unique_id(key, *names) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1353
def search_namespaces_get_unique_id(key, *names)
  if names.include?(primary.name) then
    n2 = names.dup
    n2.delete(primary.name)
    p = primary.include?(key)
  else
    n2 = names
    p = nil
  end
  s = secondary.search_names(key, *n2)
  s.push p if p
  s.sort!
  s.uniq!
  s
end
search_primary(*arg) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1331
def search_primary(*arg)
  r = Results.new
  arg.each do |x|
    a = primary.search(x)
    # a is empty or a.size==1 because primary key must be unique
    r.store(x, get_flatfile_data(*a[0])) unless a.empty?
  end
  r
end
search_primary_get_unique_id(key) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1346
def search_primary_get_unique_id(key)
  s = []
  p = primary.include?(key)
  s.push p if p
  s
end
secondary() click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1262
def secondary
  unless @secondary then
    self.secondary = @config['secondary_namespaces']
  end
  @secondary
end
secondary=(sec_names) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1269
def secondary=(sec_names)
  if !sec_names then
    sec_names = []
  end
  @secondary = NameSpaces.new(@dbname, @nsclass_sec, sec_names)
  @secondary
end
to_s() click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1187
def to_s
  a = ""
  a << "index\t#{@index_type}\n"

  unless @bdb then
    a << "format\t#{@format}\n"
    @fileids.each_with_index do |x, i|
      a << "#{x.to_s(i)}\n"
    end
    a << "primary_namespace\t#{@primary.name}\n"
    a << "secondary_namespaces\t"
    a << @secondary.names.join("\t")
    a << "\n"
  end
  a
end
write(mode = 'wb', *bdbarg) click to toggle source
# File lib/bio/io/flatfile/index.rb, line 1216
def write(mode = 'wb', *bdbarg)
  unless FileTest.directory?(@dbname) then
    Dir.mkdir(@dbname)
  end
  f = File.open(self.class.filename(@dbname), mode)
  f.write self.to_s
  f.close

  if @bdb then
    bdb_open(*bdbarg)
    @config['format'] = format
    @config['primary_namespace'] = @primary.name
    @config['secondary_namespaces'] = @secondary.names.join("\t")
    @bdb_fileids.writeback_array('', fileids, *bdbarg)
  end
  true
end